Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions PLUGIN.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ Context-level visibility for plugin UIs (status bars / slash commands):
"outputTokens": 40021,
"cachedTokens": 180000,
"requests": 42,
"lastCompressAt": 1755300000123,
"blocks": [{ "id": "b3", "tier": 1, "active": true }],
"lastSeen": 1755300000000
}
Expand All @@ -132,6 +133,8 @@ Forwarding a new model request replaces the previous observation with an estimat

`contextTokensAt` is the observation time in Unix milliseconds (not the status read time), independently of the last usage timestamp. `contextGeneration` is an opaque change detector covering a unique observation id, context, revision, model and limit: successive observations remain distinct even at the same timestamp with identical token counts; repeated status reads do not create observations. Unavailable context has null tokens, timestamp and generation. Legacy persisted sessions without an independently attributed observation are unavailable until another request establishes one. `sessionId` and nullable `sessionRevision` identify the resolved session and its fork state. Pre-first-request/chain-only responses explicitly report unavailable context. Persisted sessions are loaded by the requested id before considering an explicitly requested `fallback=latest`. Errors: `400` missing `conversationId`, `404` unknown conversation.

`lastCompressAt` is the Unix-millisecond time of the last landed fold — a successful model-driven `compress` or a preflight overflow compress, both stamped when the fold lands — or `null` until the first fold in this process. The pi/omp plugin's compaction-takeover watchdog (#2110) reads it to release native auto-compaction after a bounded streak of cancelled passes without a NEWER fold; field ABSENCE (an older proxy that never had it) means `null` (new proxy, no folds yet) and selects the legacy unconditional-cancel behavior instead.

`compressibleRanges` is the live kernel recommendation as structured `{startRef, endRef, count, ...}` entries for `sessionRevision`, or `null` if it cannot be computed. An empty list means there is no recommended range. Read the exact conversation without `fallback=latest`, match the snapshot revision and ordered refs, and submit manual `compress` with `expectedRevision`; never parse the human panel or guess a fixed prefix. Hosts must exclude their retained first-user anchor from summaries: kernel pruning retains it even if a recommendation spans it. A concurrent mutation is rejected by the revision guard.

### 5. `POST /__bili/plugin/compact`
Expand Down
114 changes: 102 additions & 12 deletions src/agent/pi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,17 @@ function noProxyWarning(agent: string): string {

const RETRY_INTERVAL_MS = 10000;

// #2110: takeover watchdog defaults — how many consecutive cancelled
// auto-compaction passes without a newer proxy fold (streak) or how much wall
// time (timeout) may elapse before bili releases compaction ownership back to
// the host's native pass. Hardcoded on purpose (config-surface discipline):
// the remedy for a broken proxy is fixing/updating it, not tuning plugin
// constants. The test seam is the factory opts below, not an env var.
const TAKEOVER_MAX_STALE_CANCELS = 8;
const TAKEOVER_TIMEOUT_MS = 600_000;

type TakeoverEntry = { activeSince: number; staleCancels: number; knownFoldAt: number };

type RegisterState = { sid?: string; toolsFor?: string; toolsReady?: boolean; pending?: Promise<void>; retryAt?: number; identityAt?: string; carriedSids?: Set<string>; retryIntervalMs: number; manifestPrime?: { base: string; tools: Promise<ManifestTool[] | undefined> } };

async function registerTools(pi: ExtensionAPI, ctx: Ctx, state: RegisterState, agent: string, awaitNativeOrigin = true): Promise<void> {
Expand Down Expand Up @@ -409,10 +420,13 @@ async function registerTools(pi: ExtensionAPI, ctx: Ctx, state: RegisterState, a
}
}

export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalMs?: number }): (pi: ExtensionAPI) => void {
export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalMs?: number; takeover?: { maxStaleCancels?: number; timeoutMs?: number } }): (pi: ExtensionAPI) => void {
return function biliPlugin(pi: ExtensionAPI): void {
const agent = agentName(agentOverride);
const state: RegisterState = { retryIntervalMs: opts?.retryIntervalMs ?? RETRY_INTERVAL_MS };
// #2110: per-conversation takeover watchdog state (see takeoverVerdict).
const takeoverLimits = { maxStaleCancels: opts?.takeover?.maxStaleCancels ?? TAKEOVER_MAX_STALE_CANCELS, timeoutMs: opts?.takeover?.timeoutMs ?? TAKEOVER_TIMEOUT_MS };
const takeovers = new Map<string, TakeoverEntry>();
// #1217: -p single-shot fires round 1 before session_start's manifest
// fetch can resolve, leaving the request unmarked → anonymous
// proxy-mode session. Prime the fetch at load time: the launcher
Expand Down Expand Up @@ -487,12 +501,19 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM
// content-hash identity, where avoiding double compression still
// wins). Probe failure (proxy down/hung) means NO evidence → defer to
// native compaction: a surviving native pass is safe (#395), a wrong
// cancel overflows the session. The handlers are async on purpose —
// pi's runner awaits session_before_compact handlers (verified in
// pi-coding-agent dist) before consulting .cancel/.compaction.
const ownsCompaction = async (ctx: Ctx | undefined, branchEntries?: unknown): Promise<boolean> => {
// cancel overflows the session. #2110 adds the layer above ownership:
// owning compaction is necessary but not sufficient — the takeover must
// actually PRODUCE folds; when it doesn't (nudge-gate starvation, a
// preflight fold that never lands), the watchdog releases passes back
// to the host's native compaction after a bounded streak of
// evidence-less cancels (takeoverVerdict below). The handlers are
// async on purpose — pi's runner awaits session_before_compact
// handlers (verified in pi-coding-agent dist) before consulting
// .cancel/.compaction.
type OwnershipVerdict = { owned: boolean; proxyBase?: string; status?: Record<string, unknown> };
const ownsCompaction = async (ctx: Ctx | undefined, branchEntries?: unknown): Promise<OwnershipVerdict> => {
const proxyBase = proxyBaseForCtx(ctx, branchEntries);
if (proxyBase === undefined) return false;
if (proxyBase === undefined) return { owned: false };
const baseUrl = ctx?.model?.baseUrl;
if (typeof baseUrl === "string" && baseUrl.length > 0 && !/^https?:\/\//i.test(baseUrl)) {
// #1392: an opaque-scheme baseUrl (e.g. pi-claude-bridge's "claude-bridge")
Expand All @@ -501,24 +522,89 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM
// evidence below (carriedSids / status probe) still decides, so unrouted traffic
// never cancels and #1382 cannot recur for a newly-opted-in provider class.
const provider = ctx?.model?.provider;
if (!(typeof provider === "string" && provider.length > 0 && nonHttpProvidersFromEnv().has(provider))) return false;
if (!(typeof provider === "string" && provider.length > 0 && nonHttpProvidersFromEnv().has(provider))) return { owned: false };
}
const sid = ctx === undefined ? undefined : sessionIdOf(ctx);
if (sid === undefined || sid.length === 0) return true;
if (agent === "pi" ? state.carriedSids?.has(sid) === true : state.identityAt === sid) return true;
if (sid === undefined || sid.length === 0) return { owned: true };
if (agent === "pi" ? state.carriedSids?.has(sid) === true : state.identityAt === sid) return { owned: true, proxyBase };
try {
return (await fetchStatus(proxyBase, sid)) !== undefined;
const status = await fetchStatus(proxyBase, sid);
return status === undefined ? { owned: false } : { owned: true, proxyBase, status };
} catch (err) {
console.error(`bili-plugin(${agent}): compaction ownership probe failed (${err instanceof Error ? err.message : String(err)}) — leaving native compaction enabled`);
return { owned: false };
}
};
// #2110: the cancel is deterministic but bili's REPLACEMENT is not — when
// the nudge gates starve (slow-growth sessions: pendingT1 < threshold AND
// growth < floor on every pass) or a preflight fold never lands, cancelling
// every native pass leaves the session growing into overflow with zero
// compression ("Auto-compaction cancelled" spam until 348% of the effective
// window). Watch the takeover itself: every cancelled pass must be backed by
// evidence that a proxy fold actually landed since the previous one —
// /__bili/plugin/status lastCompressAt, stamped by BOTH model-driven compress
// and preflight folds (applyRanges). A streak of cancelled passes without a
// newer fold (or an unreachable/unknown status) means the takeover produces
// nothing: release the pass back to the host's native compaction instead. A
// surviving native pass is safe (#395 archives the blocks it makes
// unreachable), and the release self-heals both ways — after a native pass
// the context sits below the threshold so the hook stops firing, and if the
// proxy resumes folding the credit below re-arms the takeover immediately.
// A status WITHOUT the field (an older proxy) keeps the historical
// unconditional cancel: we cannot distinguish "old proxy folding fine" from
// "old proxy starving", and flipping that behavior mid-version would silently
// change mixed-version installs.
const takeoverVerdict = async (sid: string, verdict: OwnershipVerdict): Promise<boolean> => {
// Evidence: reuse the status the remote-probe ownership check already
// fetched; local-evidence owners get one extra loopback read per event.
let status = verdict.status;
if (status === undefined && verdict.proxyBase !== undefined) {
try {
status = await fetchStatus(verdict.proxyBase, sid);
} catch {
status = undefined;
}
}
const now = Date.now();
let entry = takeovers.get(sid);
if (status !== undefined && "lastCompressAt" in status) {
const foldAt = status.lastCompressAt;
if (typeof foldAt === "number" && (entry === undefined || foldAt > entry.knownFoldAt)) {
// A fold landed since we last saw one — the takeover is alive.
takeovers.set(sid, { activeSince: now, staleCancels: 0, knownFoldAt: foldAt });
return false;
}
// null (no fold yet this process) or a repeat of the same fold:
// falls through to the stale counter below.
} else if (status !== undefined) {
return false;
}
if (entry === undefined) {
if (takeovers.size >= 64) {
const oldest = takeovers.keys().next().value;
if (oldest !== undefined) takeovers.delete(oldest);
}
entry = { activeSince: now, staleCancels: 0, knownFoldAt: Number.NEGATIVE_INFINITY };
takeovers.set(sid, entry);
}
entry.staleCancels += 1;
if (entry.staleCancels < takeoverLimits.maxStaleCancels && now - entry.activeSince < takeoverLimits.timeoutMs) {
return false;
}
takeovers.delete(sid);
console.error(`bili-plugin(${agent}): takeover watchdog (#2110) — ${entry.staleCancels} consecutive cancelled auto-compaction passes without a newer proxy fold over ${Math.round((now - entry.activeSince) / 1000)}s; releasing this pass to the host's native compaction`);
return true;
};
if (agent === "pi" || agent === "omp") {
if (agent === "pi") {
pi.on("session_before_compact", async (event, ctx) => {
const ev = event as unknown as { reason?: unknown; branchEntries?: unknown };
if (ev.reason !== "threshold" && ev.reason !== "overflow") return undefined;
if (!(await ownsCompaction(ctx, ev.branchEntries))) return undefined;
const verdict = await ownsCompaction(ctx, ev.branchEntries);
if (!verdict.owned) return undefined;
const sid = ctx === undefined ? "" : sessionIdOf(ctx) ?? "";
if (sid.length === 0) return { cancel: true };
if (await takeoverVerdict(sid, verdict)) return undefined;
return { cancel: true };
});
} else {
Expand All @@ -531,8 +617,12 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM
});
pi.on("session_before_compact", async (event, ctx) => {
if (!autoPending) return undefined;
if (!(await ownsCompaction(ctx))) return undefined;
const verdict = await ownsCompaction(ctx);
if (!verdict.owned) return undefined;
autoPending = false;
const sid = ctx === undefined ? "" : sessionIdOf(ctx) ?? "";
if (sid.length === 0) return { cancel: true };
if (await takeoverVerdict(sid, verdict)) return undefined;
return { cancel: true };
});
}
Expand Down
5 changes: 5 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,11 @@ export function handlePluginStatus(conversationId: string, res: import("node:htt
outputTokens: session.stats.outputTokens,
cachedTokens: session.stats.cachedTokens,
requests: session.stats.requests,
// #2110: epoch ms of the last landed fold (model-driven compress OR
// preflight — both stamp it in applyRanges), null until the first fold
// in this process. The pi/omp plugin's takeover watchdog reads this to
// distinguish "owning compaction" from "compaction actually producing".
lastCompressAt: session.lastCompress?.at ?? null,
blocks: session.state.blocks.map((b) => ({ id: b.blockId, tier: b.tier, active: b.active })),
compressibleRanges: nudge?.compressibleRanges ?? null,
panel,
Expand Down
53 changes: 53 additions & 0 deletions tests/issue2110-status-lastcompress.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// #2110: the pi/omp plugin's takeover watchdog reads lastCompressAt from
// /__bili/plugin/status to tell "owning compaction" apart from "compaction
// actually producing". Pin the 200-path field contract: epoch ms of the last
// landed fold (model-driven compress OR preflight — both stamp it in
// applyRanges), null until the first fold in this process. An OLDER proxy
// omits the field entirely; the plugin treats absent (legacy unconditional
// cancel) differently from null (new proxy, no folds yet), so presence itself
// is part of the contract.
import { test } from "node:test";
import assert from "node:assert/strict";
import http from "node:http";
import { createCore, defaultConfig } from "acp-kernel";
import { _resetSessionsForTest, getSession } from "../src/session.ts";
import { handlePluginStatus, recordPluginSession } from "../src/plugin.ts";

function mockRes(): { res: http.ServerResponse; status: number; body: string } {
const out = { res: undefined as unknown as http.ServerResponse, status: 0, body: "" };
const res = {
writeHead(code: number) { out.status = code; return res; },
end(chunk?: unknown) { if (typeof chunk === "string") out.body = chunk; return res; },
} as unknown as http.ServerResponse;
out.res = res;
return out;
}

test("#2110: /__bili/plugin/status exposes lastCompressAt (null until the first fold)", () => {
_resetSessionsForTest();
try {
const deps = { core: createCore(), config: defaultConfig(200000), log: (_l: string, _m: string) => {} };

const before = getSession("sess-wd-before", { protocol: "anthropic", label: "WD" });
recordPluginSession("conv-wd-before", before.id);
const r0 = mockRes();
handlePluginStatus("conv-wd-before", r0.res, deps, false);
assert.equal(r0.status, 200);
const j0 = JSON.parse(r0.body) as { ok: boolean; conversationId: string; lastCompressAt: number | null };
assert.equal(j0.ok, true);
assert.equal(j0.conversationId, "conv-wd-before");
assert.equal(j0.lastCompressAt, null, "no fold yet → null (field PRESENT, unlike an older proxy)");

const after = getSession("sess-wd-after", { protocol: "anthropic", label: "WD" });
after.lastCompress = { at: 1_760_000_000_123, shrinkRatio: 0.42, foldPoint: "m00042", blocks: 3, tokensCompressed: 55_000 };
recordPluginSession("conv-wd-after", after.id);
const r1 = mockRes();
handlePluginStatus("conv-wd-after", r1.res, deps, false);
assert.equal(r1.status, 200);
const j1 = JSON.parse(r1.body) as { ok: boolean; lastCompressAt: number | null };
assert.equal(j1.ok, true);
assert.equal(j1.lastCompressAt, 1_760_000_000_123, "fold landed → epoch ms passes through byte-exact");
} finally {
_resetSessionsForTest();
}
});
Loading
Loading