diff --git a/kernel/package-lock.json b/kernel/package-lock.json index da011d94e1..4c5e2d79e9 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.164", + "version": "0.0.165", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.164", + "version": "0.0.165", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/package-lock.json b/package-lock.json index 380212c96e..15a6e3f4e9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,7 +50,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.163", + "version": "0.0.165", "dev": true, "license": "MIT", "devDependencies": { diff --git a/src/agent/pi.ts b/src/agent/pi.ts index 0fc7598aa2..7e77408778 100644 --- a/src/agent/pi.ts +++ b/src/agent/pi.ts @@ -31,8 +31,14 @@ export type Ctx = { sessionManager?: { getSessionId?: () => string; getHeader?: () => unknown; getBranch?: () => unknown } | undefined; model?: { contextWindow?: number; baseUrl?: string; provider?: string; id?: string; api?: string; [key: string]: unknown } | undefined; // #2186: acp_delegate surface notifies stand-downs through the host toast - // when present (same channel CommandCtx already declares). - ui?: { notify?: (message: string, type?: string) => void } | undefined; + // when present (same channel CommandCtx already declares). #3912: the + // keyed footer status slot (pi 0.83+ ExtensionUIContext.setStatus, verified + // against pi-coding-agent dist types.d.ts) — optional because older hosts + // lack it; only the pi lane uses it here. + ui?: { notify?: (message: string, type?: string) => void; setStatus?: (key: string, text: string | undefined) => void } | undefined; + // #3912: pi's own projection estimate (ExtensionContext.getContextUsage — a + // pure read of the local session view). Optional because older hosts lack it. + getContextUsage?: () => { tokens?: number | null; contextWindow?: number; percent?: number | null } | undefined; // #1961: pi 0.99+ exposes the live model catalog on the extension ctx; // optional because older hosts lack it. The real ModelRegistry surface is // find(provider, modelId) — there is no getModel (verified against pi @@ -585,6 +591,55 @@ function renderAcpStatus(s: Record): string { return lines.join("\n"); } +// #3912: pi's native footer percent comes from PI'S OWN projection estimator: +// once any recovery omission (a failed attempt re-sent without the failed +// turn) lands after the last provider-usage anchor, pi distrusts that anchor +// and falls back to a chars/4 count over its FULL local history. bili keeps +// that history unfolded on purpose (the wire carries the folded view; pi's +// disk copy stays raw for decompress fidelity), so a healthy folded session +// can read 500%+ in the native footer while the effective request is ~20% of +// the window. This line shows the PROXY's effective-request observation with +// an honest grade label ("usage" = provider-measured input of the last +// settled request; "est" = bili's calibrated estimate of the current payload, +// which is what a failed request leaves behind — stamped fresh at prepare +// time), a staleness marker once no newer observation lands, and an explicit +// reconciliation note when pi's own estimate diverges far enough that the two +// numbers need explaining side by side. Rendered into pi's keyed status slot +// only (additive — never replaces the native footer); wiring in +// createBiliPlugin. +const CONTEXT_LINE_REFRESH_THROTTLE_MS = 2000; +const CONTEXT_LINE_STALE_SOFT_MS = 5 * 60 * 1000; +const CONTEXT_LINE_STALE_HARD_MS = 60 * 60 * 1000; +const CONTEXT_LINE_DIVERGE_POINTS = 25; +export const BILI_CONTEXT_STATUS_KEY = "billion-context"; + +export function renderBiliContextLine( + status: Record | undefined, + hostPercent: number | null | undefined, + now: number, +): string | undefined { + if (status === undefined) return undefined; + const num = (v: unknown): number | null => (typeof v === "number" && Number.isFinite(v) ? v : null); + const tokens = num(status.contextTokens); + if (tokens === null || tokens <= 0) return undefined; + const source = typeof status.contextTokensSource === "string" ? status.contextTokensSource : "unavailable"; + if (source !== "usage" && source !== "estimate") return undefined; + const limit = num(status.contextLimit); + const pct = limit !== null && limit > 0 ? (tokens / limit) * 100 : null; + let line = `bili ${pct !== null ? `${pct.toFixed(1)}% ` : ""}${fmtTok(tokens)}${limit !== null && limit > 0 ? `/${fmtTok(limit)}` : ""}`; + line += source === "usage" ? " · usage" : " · est"; + const at = num(status.contextTokensAt); + if (at !== null) { + const ageMs = now - at; + if (ageMs >= CONTEXT_LINE_STALE_HARD_MS) line += " · stale"; + else if (ageMs >= CONTEXT_LINE_STALE_SOFT_MS) line += ` · ${Math.floor(ageMs / 60_000)}m`; + } + if (pct !== null && hostPercent !== undefined && hostPercent !== null && Number.isFinite(hostPercent) && Math.abs(hostPercent - pct) >= CONTEXT_LINE_DIVERGE_POINTS) { + line += ` · host est ${hostPercent.toFixed(1)}%`; + } + return line; +} + function outcomeDetailsOf(output: ForwardedToolResult): Record | undefined { if (output.outcome === undefined && output.blocksCreated === undefined) return undefined; const details: Record = {}; @@ -1681,6 +1736,59 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM const name = (event as { name?: unknown }).name; reportSessionName(ctx, typeof name === "string" ? name : ""); }); + // #3912: persistent effective-context line (semantics in + // renderBiliContextLine above). Additive keyed status slot only — + // the native footer and other extensions' setFooter claims are + // untouched. Refreshed by the events that can change the proxy's + // observation: turn_end / agent_settled (settled turns) and + // after_provider_response (also fires for error-status responses, + // where no turn completes). session_start (any reason: startup / + // resume / new / fork / reload) force-refreshes so the incoming + // session repopulates or honestly clears; session_tree clears + // because a local rewind invalidates the last observed payload + // until the next request re-establishes it. Throttled; a failed + // refresh keeps the previous line rather than guessing. Pi lane + // only (issue scope); omp shares none of this wiring. + if (agent === "pi") { + let contextLineRefreshAt = 0; + let contextLineRefreshGen = 0; + const clearContextLine = (ctx: Ctx | undefined): void => { + const setStatus = ctx?.ui?.setStatus; + if (typeof setStatus !== "function") return; + try { setStatus(BILI_CONTEXT_STATUS_KEY, undefined); } catch { /* best-effort UI clear */ } + }; + const refreshContextLine = async (ctx: Ctx | undefined, force: boolean): Promise => { + const setStatus = ctx?.ui?.setStatus; + if (typeof setStatus !== "function" || ctx === undefined) return; + const now = Date.now(); + if (!force && now - contextLineRefreshAt < CONTEXT_LINE_REFRESH_THROTTLE_MS) return; + const gen = ++contextLineRefreshGen; + contextLineRefreshAt = now; + try { + const proxyBase = proxyBaseForCtx(ctx); + const sid = sessionIdOf(ctx); + if (proxyBase === undefined || sid === undefined || sid.length === 0) return; + const status = await fetchStatus(proxyBase, sid); + let hostPercent: number | null | undefined; + try { + const usage = ctx.getContextUsage?.(); + hostPercent = usage !== undefined && typeof usage.percent === "number" && Number.isFinite(usage.percent) ? usage.percent : null; + } catch { + hostPercent = null; + } + if (gen !== contextLineRefreshGen) return; // superseded — a newer refresh owns the slot + setStatus(BILI_CONTEXT_STATUS_KEY, renderBiliContextLine(status, hostPercent, Date.now())); + } catch (err) { + logHostEvent("error", "[native-client]", `bili-plugin(pi): context line refresh failed (${err instanceof Error ? err.message : String(err)})`); + } + }; + pi.on("turn_end", (_event, ctx) => { void refreshContextLine(ctx, false); }); + pi.on("agent_settled", (_event, ctx) => { void refreshContextLine(ctx, false); }); + pi.on("after_provider_response", (_event, ctx) => { void refreshContextLine(ctx, false); }); + pi.on("session_start", (_event, ctx) => { void refreshContextLine(ctx, true); }); + pi.on("session_shutdown", (_event, ctx) => clearContextLine(ctx)); + pi.on("session_tree", (_event, ctx) => clearContextLine(ctx)); + } // #2186: acp_delegate surface for the pi lane, inlined from // billion-context-pi-subagents. omp never reaches the wiring (the // gate repeats inside) and a previously claimed surface stands down. diff --git a/tests/issue3912-pi-context-line.test.ts b/tests/issue3912-pi-context-line.test.ts new file mode 100644 index 0000000000..666d161ffe --- /dev/null +++ b/tests/issue3912-pi-context-line.test.ts @@ -0,0 +1,251 @@ +// #3912: after a failed attempt's recovery omission, pi's native footer +// shows ITS OWN raw-history projection estimate (chars/4 over the FULL +// unfolded local history — 500%+ of the window) while the effective request +// bili sends is a fraction of that. The pi lane answers with a persistent, +// labeled effective-context line in pi's keyed status slot. These tests pin: +// ① renderer semantics — grade labels (usage/est), staleness tiers, the +// host-divergence reconciliation note, and honesty on missing data +// (no number is ever rendered without a grade; nothing is clamped); +// ② wiring lifecycle — which events refresh vs clear, the refresh +// throttle (both directions), force-refresh on session_start, and the +// no-cross-session-bleed guarantee (an unknown session CLEARS the line +// instead of keeping the previous session's numbers). +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { BILI_CONTEXT_STATUS_KEY, createBiliPlugin, renderBiliContextLine } from "../src/agent/pi.ts"; + +const NOW = 1_760_000_000_000; + +function statusOf(over: Record = {}): Record { + return { ok: true, contextTokens: 104_733, contextLimit: 500_000, contextTokensSource: "usage", contextTokensAt: NOW - 1000, ...over }; +} + +test("usage-grade line with limit", () => { + assert.equal(renderBiliContextLine(statusOf(), null, NOW), "bili 20.9% 104.7K/500.0K · usage"); +}); + +test("estimate-grade line is labeled est", () => { + assert.equal(renderBiliContextLine(statusOf({ contextTokensSource: "estimate" }), null, NOW), "bili 20.9% 104.7K/500.0K · est"); +}); + +test("no limit renders the absolute count only", () => { + assert.equal(renderBiliContextLine(statusOf({ contextTokens: 45_000, contextLimit: null }), null, NOW), "bili 45.0K · usage"); +}); + +test("missing status renders nothing", () => { + assert.equal(renderBiliContextLine(undefined, 562.8, NOW), undefined); +}); + +test("zero/negative/non-finite token counts render nothing", () => { + assert.equal(renderBiliContextLine(statusOf({ contextTokens: 0 }), null, NOW), undefined); + assert.equal(renderBiliContextLine(statusOf({ contextTokens: -5 }), null, NOW), undefined); + assert.equal(renderBiliContextLine(statusOf({ contextTokens: Number.NaN }), null, NOW), undefined); +}); + +test("unavailable or missing source renders nothing (never unlabeled)", () => { + assert.equal(renderBiliContextLine(statusOf({ contextTokensSource: "unavailable" }), null, NOW), undefined); + const noSource = statusOf(); + delete noSource.contextTokensSource; + assert.equal(renderBiliContextLine(noSource, null, NOW), undefined); +}); + +test("fresh observation (<5min) carries no staleness marker", () => { + assert.equal(renderBiliContextLine(statusOf({ contextTokensAt: NOW - 4 * 60_000 }), null, NOW), "bili 20.9% 104.7K/500.0K · usage"); +}); + +test("5-60min age gets a minute-age marker", () => { + assert.equal(renderBiliContextLine(statusOf({ contextTokensAt: NOW - 10 * 60_000 }), null, NOW), "bili 20.9% 104.7K/500.0K · usage · 10m"); +}); + +test(">60min age is marked stale", () => { + assert.equal(renderBiliContextLine(statusOf({ contextTokensAt: NOW - 90 * 60_000 }), null, NOW), "bili 20.9% 104.7K/500.0K · usage · stale"); +}); + +test("host divergence >= 25 points gets a reconciliation note", () => { + assert.equal( + renderBiliContextLine(statusOf(), 562.8, NOW), + "bili 20.9% 104.7K/500.0K · usage · host est 562.8%", + ); +}); + +test("host divergence exactly at the threshold is noted", () => { + assert.ok(renderBiliContextLine(statusOf({ contextTokens: 100_000 }), 45.0, NOW)?.endsWith("· host est 45.0%")); +}); + +test("small host divergence is not noted", () => { + assert.equal(renderBiliContextLine(statusOf(), 25.0, NOW), "bili 20.9% 104.7K/500.0K · usage"); +}); + +test("host percent null/undefined yields no note", () => { + assert.equal(renderBiliContextLine(statusOf(), null, NOW), "bili 20.9% 104.7K/500.0K · usage"); + assert.equal(renderBiliContextLine(statusOf(), undefined, NOW), "bili 20.9% 104.7K/500.0K · usage"); +}); + +test("without a limit there is nothing to compare against the host", () => { + assert.equal(renderBiliContextLine(statusOf({ contextTokens: 45_000, contextLimit: null }), 562.8, NOW), "bili 45.0K · usage"); +}); + +test("staleness and divergence combine", () => { + assert.equal( + renderBiliContextLine(statusOf({ contextTokensAt: NOW - 90 * 60_000 }), 562.8, NOW), + "bili 20.9% 104.7K/500.0K · usage · stale · host est 562.8%", + ); +}); + +function makeFakePi() { + const events = new Map unknown)[]>(); + return { + events, + registerTool: () => {}, + registerCommand: () => {}, + registerProvider: () => {}, + getSessionName: () => "test", + on: (event: string, handler: (event: never, ctx: never) => unknown) => { + const list = events.get(event) ?? []; + list.push(handler as (event: unknown, ctx: unknown) => unknown); + events.set(event, list); + }, + fire: (name: string, event: unknown, ctx: unknown): unknown => { + const list = events.get(name) ?? []; + let last: unknown; + for (const h of list) last = h(event, ctx); + return last; + }, + fireAsync: async (name: string, event: unknown, ctx: unknown): Promise => { + const list = events.get(name) ?? []; + let last: unknown; + for (const h of list) last = await h(event, ctx); + return last; + }, + }; +} + +type FakePi = ReturnType; + +test("wiring: keyed-slot refresh/clear lifecycle against a live status endpoint", async (t) => { + const dir = mkdtempSync(join(tmpdir(), "bili-issue3912-")); + t.after(() => rmSync(dir, { recursive: true, force: true })); + for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = dir; + process.env.NODE_ENV = "test"; + const prevProxy = process.env.BILLION_CONTEXT_PROXY; + const prevKill = process.env.BILLION_CONTEXT_PLUGIN; + delete process.env.BILLION_CONTEXT_PLUGIN; + + let statusHits = 0; + const mock = http.createServer((req, res) => { + if (req.url?.startsWith("/__bili/plugin/status")) { + statusHits++; + const sid = new URL(`http://x${req.url}`).searchParams.get("conversationId") ?? ""; + if (sid !== "sid-x") { + res.writeHead(404, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: false, error: "unknown plugin conversation" })); + return; + } + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: true, conversationId: "sid-x", contextTokens: 104_733, contextLimit: 500_000, contextTokensSource: "usage", contextTokensAt: Date.now() })); + return; + } + res.writeHead(404, { "content-type": "application/json" }); + res.end("{}"); + }); + mock.listen(0, "127.0.0.1"); + await once(mock, "listening"); + const port = (mock.address() as { port: number }).port; + process.env.BILLION_CONTEXT_PROXY = `http://127.0.0.1:${port}`; + + try { + const pi = makeFakePi(); + createBiliPlugin("pi")(pi as never); + for (const name of ["turn_end", "agent_settled", "after_provider_response", "session_start", "session_shutdown", "session_tree"]) { + assert.ok((pi.events.get(name) ?? []).length > 0, `handler registered for ${name}`); + } + const lines: Array = []; + const ctx = { + sessionManager: { getSessionId: () => "sid-x" }, + model: { id: "claude-test", contextWindow: 500_000, api: "anthropic", baseUrl: "https://api.example.com/v1" }, + ui: { notify: () => {}, setStatus: (key: string, text: string | undefined) => { assert.equal(key, BILI_CONTEXT_STATUS_KEY); lines.push(text); } }, + getContextUsage: () => ({ tokens: 2_813_956, contextWindow: 500_000, percent: 562.8 }), + cwd: dir, + }; + const EXPECTED_LINE = "bili 20.9% 104.7K/500.0K · usage · host est 562.8%"; + const waitFor = async (pred: () => boolean, what: string): Promise => { + const deadline = Date.now() + 3000; + while (!pred()) { + if (Date.now() > deadline) throw new Error(`timeout waiting for ${what}`); + await new Promise((r) => setTimeout(r, 25)); + } + }; + // A settled turn refreshes the slot, with the host-divergence note + // (the #3912 repro shape: native footer 562.8%, effective 20.9%). The + // refresh is fire-and-forget on purpose (it must never block the host + // event loop), so the test polls for the slot write. + await pi.fireAsync("turn_end", { type: "turn_end", turnIndex: 0 }, ctx); + await waitFor(() => lines.length === 1, "first slot write"); + assert.deepEqual(lines, [EXPECTED_LINE]); + // Back-to-back events are throttled (one status read per window). + await pi.fireAsync("after_provider_response", { type: "after_provider_response", status: 200 }, ctx); + await new Promise((r) => setTimeout(r, 150)); + assert.equal(statusHits, 1); + assert.equal(lines.length, 1); + // session_start force-refreshes even inside the throttle window + // (resume/reload must repopulate immediately). + await pi.fireAsync("session_start", { type: "session_start", reason: "resume" }, ctx); + await waitFor(() => lines.length === 2 && statusHits === 2, "forced re-read"); + assert.equal(lines.at(-1), EXPECTED_LINE); + // Unknown session (switched away): the line CLEARS instead of + // keeping the previous session's numbers. + ctx.sessionManager = { getSessionId: () => "sid-elsewhere" }; + await pi.fireAsync("session_start", { type: "session_start", reason: "new" }, ctx); + await waitFor(() => lines.length === 3, "clear write for unknown session"); + assert.equal(lines.at(-1), undefined); + // Local rewind invalidates the observed payload until the next request. + ctx.sessionManager = { getSessionId: () => "sid-x" }; + pi.fire("session_tree", { type: "session_tree" }, ctx); + assert.equal(lines.at(-1), undefined); + pi.fire("session_shutdown", { type: "session_shutdown" }, ctx); + assert.equal(lines.at(-1), undefined); + } finally { + await new Promise((resolve) => mock.close(() => resolve())); + if (prevProxy === undefined) delete process.env.BILLION_CONTEXT_PROXY; + else process.env.BILLION_CONTEXT_PROXY = prevProxy; + if (prevKill === undefined) delete process.env.BILLION_CONTEXT_PLUGIN; + else process.env.BILLION_CONTEXT_PLUGIN = prevKill; + } +}); + +test("wiring: hosts without the status slot or proxy degrade silently", async () => { + const dir = mkdtempSync(join(tmpdir(), "bili-issue3912-noui-")); + test.after(() => rmSync(dir, { recursive: true, force: true })); + for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = dir; + process.env.NODE_ENV = "test"; + const prevProxy = process.env.BILLION_CONTEXT_PROXY; + const prevKill = process.env.BILLION_CONTEXT_PLUGIN; + delete process.env.BILLION_CONTEXT_PROXY; + delete process.env.BILLION_CONTEXT_PLUGIN; + try { + const pi = makeFakePi(); + createBiliPlugin("pi")(pi as never); + const ctx = { + sessionManager: { getSessionId: () => "sid-x" }, + model: { id: "m", contextWindow: 500_000, api: "openai", baseUrl: "https://api.example.com/v1" }, + ui: { notify: () => {} }, + cwd: dir, + }; + // No ui.setStatus (older host) AND no resolvable proxy base: every + // trigger must be a silent no-op, never a throw into the host loop. + await pi.fireAsync("turn_end", { type: "turn_end", turnIndex: 0 }, ctx); + await pi.fireAsync("after_provider_response", { type: "after_provider_response", status: 500 }, ctx); + pi.fire("session_tree", { type: "session_tree" }, ctx); + pi.fire("session_shutdown", { type: "session_shutdown" }, ctx); + } finally { + if (prevProxy === undefined) delete process.env.BILLION_CONTEXT_PROXY; + else process.env.BILLION_CONTEXT_PROXY = prevProxy; + if (prevKill === undefined) delete process.env.BILLION_CONTEXT_PLUGIN; + else process.env.BILLION_CONTEXT_PLUGIN = prevKill; + } +});