From a2b2822579bd3e3ebc857d674361f5c4ccd71793 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Mon, 31 Aug 2026 17:29:52 +0800 Subject: [PATCH 01/22] =?UTF-8?q?fix:=20#381=20review=20=E2=80=94=20SQLite?= =?UTF-8?q?=20set=20adjudication,=20-journal,=20conflict=20rename,=20link?= =?UTF-8?q?=20kind?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Follow-up to #382 (Windows overlay symlink fix), addressing code review: - mergeSqliteSet: adjudicate the whole set by the MAIN db's mtime (winner's set moves as a unit) instead of per-member mtime — per-member could splice a newer main db with a newer WAL from the other side (salt/page mismatch -> WAL discarded -> overlay session's uncheckpointed writes lost). Orphan sets (no main db) are stale residue, discarded, never moved individually. - sqliteSetMembers: include -journal (rollback-journal mode). - conflict renames (mergeSqliteSet + mergeOverlayEntry): non-colliding name (.bili-conflict, .1, .2, ...) so a retry never clobbers the prior round's loser. - linkOverlayEntry: explicit symlink kind (win32 dir -> junction, file -> file) so a dir target is never a file-tag reparse point. 3 new tests; 871 pass, typecheck + build green. --- src/launcher.ts | 163 +++++++++++++++++++++++++---------------- tests/launcher.test.ts | 94 +++++++++++++++++++++++- 2 files changed, 189 insertions(+), 68 deletions(-) diff --git a/src/launcher.ts b/src/launcher.ts index d34e4c6d4f..9683255782 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -645,29 +645,36 @@ function livePidHoldsOverlay(overlay: string): number | undefined { } /** - * Link one real-home entry into the overlay: plain symlink first (the Linux/ - * macOS path, and the Windows path when Developer Mode grants - * SeCreateSymbolicLinkPrivilege). On EPERM/EACCES/EINVAL — the default for an - * unprivileged Windows process, #381 — fall back to privilege-free links so - * the overlay is populated instead of silently hollow: directories → junction - * (mklink /J), files → hardlink (same volume, write-through) → copy. + * Link one real-home entry into the overlay. The reparse-point kind is chosen + * explicitly from the target's type: on Windows a directory must be a junction + * (privilege-free, unambiguous) — libuv's type-omitted default creates a + * file-tag symlink for dirs that Win32 readdir(withFileTypes) and some + * backup/indexers don't follow (#381 review) — while a file is a 'file' + * symlink (needs SeCreateSymbolicLinkPrivilege). Non-Windows ignores the type, + * so "dir"/"file" are just plain symlinks there. On EPERM/EACCES/EINVAL — the + * default for an unprivileged Windows process, #381 — a file falls back to a + * privilege-free hardlink (same volume, write-through) → copy; a directory + * retries the junction. */ function linkOverlayEntry(realHome: string, overlay: string, entry: string): boolean { const target = path.join(realHome, entry); const link = path.join(overlay, entry); - try { - fs.symlinkSync(target, link); - return true; - } catch (err) { - const code = (err as NodeJS.ErrnoException).code; - if (code !== "EPERM" && code !== "EACCES" && code !== "EINVAL") return false; - } let st: fs.Stats; try { st = fs.lstatSync(target); } catch { return false; } + const kind: "dir" | "file" | "junction" = st.isDirectory() + ? (process.platform === "win32" ? "junction" : "dir") + : "file"; + try { + fs.symlinkSync(target, link, kind); + return true; + } catch (err) { + const code = (err as NodeJS.ErrnoException).code; + if (code !== "EPERM" && code !== "EACCES" && code !== "EINVAL") return false; + } if (st.isDirectory()) { try { fs.symlinkSync(target, link, "junction"); @@ -702,26 +709,61 @@ function isWriteThroughHardlink(overlayPath: string, realPath: string, st: fs.St } } -/** SQLite three-piece set for a main-db name. A WAL is only valid against its - * exact main db, so the set must move as a unit — splitting it corrupts the +/** SQLite set for a main-db name: the db plus its WAL, shared-memory, and + * rollback-journal sidecars. A WAL/journal is only valid against its exact + * main db, so the set must move as a unit — splitting it corrupts the * database (#381). */ function sqliteSetMembers(base: string): string[] { - return [base, `${base}-wal`, `${base}-shm`]; + return [base, `${base}-wal`, `${base}-shm`, `${base}-journal`]; +} + +/** A `.bili-conflict` target that does not already exist, so a retry + * round never silently overwrites a previous round's preserved loser (#381 + * review): renameSync clobbers an existing target, so append `.1`, `.2`, … + * until the name is free. */ +function freeConflictName(dst: string): string { + let candidate = `${dst}.bili-conflict`; + let n = 1; + while (n < 100000) { + try { + fs.lstatSync(candidate); + candidate = `${dst}.bili-conflict.${n}`; + n += 1; + } catch { + return candidate; + } + } + return candidate; } /** Move a SQLite set (see sqliteSetMembers) from overlay to real home as one - * unit (#381): all present members move together, and if any rename fails - * (real db open/locked on Windows) the moved ones roll back and the set stays - * for the next launch. Per member mtime-newer wins (loser → .bili-conflict). */ + * unit (#381). The authoritative generation is decided ONCE by the main db's + * mtime — a WAL/journal is only valid against its exact main db, so the whole + * set must come from a single side: per-member mtime adjudication could splice + * a newer main db with a newer WAL from the other side and corrupt the + * database. The winner's members become the real home's active set; every + * losing member is preserved as `.bili-conflict` (never overwritten). A + * set with no main db on either side (orphan sidecars) is stale residue and is + * preserved wholesale as conflicts, never moved in as an active db. If any + * rename fails (real db open/locked on Windows) the moved ones roll back and + * the set stays for the next launch. */ function mergeSqliteSet(overlay: string, realHome: string, base: string): boolean { - const members = sqliteSetMembers(base).filter((m) => { + const members = sqliteSetMembers(base); + const statFile = (dir: string, m: string): fs.Stats | undefined => { try { - return fs.lstatSync(path.join(overlay, m)).isFile(); + const st = fs.lstatSync(path.join(dir, m)); + return st.isFile() ? st : undefined; } catch { - return false; + return undefined; } - }); - if (members.length === 0) return true; + }; + const oMain = statFile(overlay, base); + const rMain = statFile(realHome, base); + let winner: "overlay" | "real" | "orphan"; + if (oMain && rMain) winner = rMain.mtimeMs >= oMain.mtimeMs ? "real" : "overlay"; + else if (oMain) winner = "overlay"; + else if (rMain) winner = "real"; + else winner = "orphan"; const undo: (() => void)[] = []; const rollback = (): void => { for (const step of undo.reverse()) { @@ -731,45 +773,38 @@ function mergeSqliteSet(overlay: string, realHome: string, base: string): boolea } undo.length = 0; }; + const movePreserving = (src: string, dst: string): void => { + let dstStat: fs.Stats | undefined; + try { + dstStat = fs.lstatSync(dst); + } catch {} + if (dstStat) { + if (dstStat.isDirectory()) throw new Error("target is a directory"); + const conflict = freeConflictName(dst); + fs.renameSync(dst, conflict); + undo.push(() => fs.renameSync(conflict, dst)); + } + fs.renameSync(src, dst); + undo.push(() => fs.renameSync(dst, src)); + }; + const preserveAsConflict = (src: string, name: string): void => { + const conflict = freeConflictName(path.join(realHome, name)); + fs.renameSync(src, conflict); + undo.push(() => fs.renameSync(conflict, src)); + }; try { for (const m of members) { - const src = path.join(overlay, m); - const dst = path.join(realHome, m); - let dstStat: fs.Stats | undefined; - try { - dstStat = fs.lstatSync(dst); - } catch {} - if (dstStat && dstStat.isDirectory()) { - rollback(); - return false; - } - const srcStat = fs.lstatSync(src); - if (dstStat && dstStat.mtimeMs >= srcStat.mtimeMs) { - try { - fs.renameSync(src, `${dst}.bili-conflict`); - } catch { - rollback(); - return false; - } - undo.push(() => fs.renameSync(`${dst}.bili-conflict`, src)); - continue; - } - if (dstStat) { - try { - fs.renameSync(dst, `${dst}.bili-conflict`); - } catch { - rollback(); - return false; - } - undo.push(() => fs.renameSync(`${dst}.bili-conflict`, dst)); - } - try { - fs.renameSync(src, dst); - } catch { - rollback(); - return false; + const o = statFile(overlay, m); + const r = statFile(realHome, m); + if (winner === "overlay") { + if (o) movePreserving(path.join(overlay, m), path.join(realHome, m)); + else if (r) preserveAsConflict(path.join(realHome, m), m); + } else if (winner === "real") { + if (o) preserveAsConflict(path.join(overlay, m), m); + } else { + if (o) preserveAsConflict(path.join(overlay, m), m); + else if (r) preserveAsConflict(path.join(realHome, m), m); } - undo.push(() => fs.renameSync(dst, src)); } return true; } catch { @@ -935,7 +970,7 @@ function mergeOverlayEntry(src: string, dst: string): boolean { if (st.isDirectory()) { if (dstStat && !dstStat.isDirectory()) { try { - fs.renameSync(dst, `${dst}.bili-conflict`); + fs.renameSync(dst, freeConflictName(dst)); dstStat = undefined; } catch { return false; @@ -960,7 +995,7 @@ function mergeOverlayEntry(src: string, dst: string): boolean { } if (dstStat && dstStat.isDirectory()) { try { - fs.renameSync(src, `${dst}.bili-conflict`); + fs.renameSync(src, freeConflictName(dst)); return true; } catch { return false; @@ -982,7 +1017,7 @@ function mergeOverlayEntry(src: string, dst: string): boolean { } if (dstStat && dstStat.mtimeMs >= st.mtimeMs) { try { - fs.renameSync(src, `${dst}.bili-conflict`); + fs.renameSync(src, freeConflictName(dst)); return true; } catch { return false; @@ -990,7 +1025,7 @@ function mergeOverlayEntry(src: string, dst: string): boolean { } if (dstStat) { try { - fs.renameSync(dst, `${dst}.bili-conflict`); + fs.renameSync(dst, freeConflictName(dst)); } catch { return false; } diff --git a/tests/launcher.test.ts b/tests/launcher.test.ts index 7df4c7840d..9a50dec518 100644 --- a/tests/launcher.test.ts +++ b/tests/launcher.test.ts @@ -1,7 +1,8 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import fs from "node:fs"; -import type { PathLike, SymlinkType } from "node:fs"; +import type { PathLike } from "node:fs"; +type SymlinkKind = "dir" | "file" | "junction"; import net from "node:net"; import os from "node:os"; import path from "node:path"; @@ -1154,7 +1155,7 @@ test("prepareOmpHttpRewrite: EPERM fallback links dir→junction, file→hardlin fs.writeFileSync(path.join(home, "settings.json"), "real-settings"); const rw = [{ key: "a", realUpstream: "http://example.com/v1" }]; const realSymlinkSync = fs.symlinkSync; - fs.symlinkSync = ((target: PathLike, link: PathLike, type?: SymlinkType) => { + fs.symlinkSync = ((target: PathLike, link: PathLike, type?: SymlinkKind) => { if (type === "junction") return realSymlinkSync(target, link, "junction"); throw denyErr("EPERM"); }) as typeof fs.symlinkSync; @@ -1184,7 +1185,7 @@ test("prepareOmpHttpRewrite: EPERM fallback copies file when hardlink also denie const rw = [{ key: "a", realUpstream: "http://example.com/v1" }]; const realSymlinkSync = fs.symlinkSync; const realLinkSync = fs.linkSync; - fs.symlinkSync = ((target: PathLike, link: PathLike, type?: SymlinkType) => { + fs.symlinkSync = ((target: PathLike, link: PathLike, type?: SymlinkKind) => { if (type === "junction") return realSymlinkSync(target, link, "junction"); throw denyErr("EPERM"); }) as typeof fs.symlinkSync; @@ -1296,7 +1297,7 @@ test("prepareOmpHttpRewrite: write-through hardlink is re-pointed, never merged const rw = [{ key: "a", realUpstream: "http://example.com/v1" }]; const overlay = `${home}-bili`; const realSymlinkSync = fs.symlinkSync; - fs.symlinkSync = ((target: PathLike, link: PathLike, type?: SymlinkType) => { + fs.symlinkSync = ((target: PathLike, link: PathLike, type?: SymlinkKind) => { if (type === "junction") return realSymlinkSync(target, link, "junction"); throw denyErr("EPERM"); }) as typeof fs.symlinkSync; @@ -1319,6 +1320,91 @@ test("prepareOmpHttpRewrite: write-through hardlink is re-pointed, never merged } }); +test("prepareOmpHttpRewrite: SQLite set adjudicated by main db — no cross-side WAL splice (#381)", () => { + const home = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "bili-omphome-"))); + fs.writeFileSync(path.join(home, "models.yml"), ["providers:", " a:", " baseUrl: http://example.com/v1"].join("\n")); + const now = Date.now(); + fs.writeFileSync(path.join(home, "agent.db"), "real-db"); + fs.writeFileSync(path.join(home, "agent.db-wal"), "real-wal"); + const overlay = `${home}-bili`; + fs.mkdirSync(overlay); + fs.writeFileSync(path.join(overlay, "agent.db"), "overlay-db"); + fs.writeFileSync(path.join(overlay, "agent.db-wal"), "overlay-wal"); + // main db: real newer than overlay; wal: overlay newer than real — the + // cross-generational splice per-member mtime adjudication would produce. + fs.utimesSync(path.join(home, "agent.db"), new Date(now - 10000), new Date(now - 10000)); + fs.utimesSync(path.join(home, "agent.db-wal"), new Date(now - 50000), new Date(now - 50000)); + fs.utimesSync(path.join(overlay, "agent.db"), new Date(now - 60000), new Date(now - 60000)); + fs.utimesSync(path.join(overlay, "agent.db-wal"), new Date(now - 5000), new Date(now - 5000)); + const rw = [{ key: "a", realUpstream: "http://example.com/v1" }]; + let tmp: string | undefined; + try { + tmp = prepareOmpHttpRewrite(home, "http://127.0.0.1:8787", rw, []); + assert.equal(tmp, overlay); + assert.equal(fs.readFileSync(path.join(home, "agent.db"), "utf8"), "real-db", "real main db (newer) wins"); + assert.equal(fs.readFileSync(path.join(home, "agent.db-wal"), "utf8"), "real-wal", "wal comes from the SAME side as the db (no splice)"); + assert.equal(fs.readFileSync(path.join(home, "agent.db.bili-conflict"), "utf8"), "overlay-db", "losing overlay db preserved"); + assert.equal(fs.readFileSync(path.join(home, "agent.db-wal.bili-conflict"), "utf8"), "overlay-wal", "losing overlay wal preserved"); + } finally { + if (tmp) fs.rmSync(tmp, { recursive: true, force: true }); + fs.rmSync(home, { recursive: true, force: true }); + } +}); + +test("prepareOmpHttpRewrite: SQLite -journal sidecar moves with the set, not separately (#381)", () => { + const home = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "bili-omphome-"))); + fs.writeFileSync(path.join(home, "models.yml"), ["providers:", " a:", " baseUrl: http://example.com/v1"].join("\n")); + const now = Date.now(); + fs.writeFileSync(path.join(home, "agent.db"), "real-db"); + const overlay = `${home}-bili`; + fs.mkdirSync(overlay); + fs.writeFileSync(path.join(overlay, "agent.db"), "overlay-db"); + fs.writeFileSync(path.join(overlay, "agent.db-journal"), "overlay-journal"); + // real main db newer than overlay's; the overlay journal must NOT be + // spliced in as an active file against the real (newer) db. + fs.utimesSync(path.join(home, "agent.db"), new Date(now - 10000), new Date(now - 10000)); + fs.utimesSync(path.join(overlay, "agent.db"), new Date(now - 60000), new Date(now - 60000)); + fs.utimesSync(path.join(overlay, "agent.db-journal"), new Date(now - 5000), new Date(now - 5000)); + const rw = [{ key: "a", realUpstream: "http://example.com/v1" }]; + let tmp: string | undefined; + try { + tmp = prepareOmpHttpRewrite(home, "http://127.0.0.1:8787", rw, []); + assert.equal(tmp, overlay); + assert.equal(fs.readFileSync(path.join(home, "agent.db"), "utf8"), "real-db", "real main db (newer) wins"); + assert.equal(fs.existsSync(path.join(home, "agent.db-journal")), false, "journal NOT spliced in as an active file"); + assert.equal(fs.readFileSync(path.join(home, "agent.db-journal.bili-conflict"), "utf8"), "overlay-journal", "journal preserved as a conflict (same side as the losing db)"); + } finally { + if (tmp) fs.rmSync(tmp, { recursive: true, force: true }); + fs.rmSync(home, { recursive: true, force: true }); + } +}); + +test("prepareOmpHttpRewrite: conflict rename never clobbers a previous round's loser (#381)", () => { + const home = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "bili-omphome-"))); + fs.writeFileSync(path.join(home, "models.yml"), ["providers:", " a:", " baseUrl: http://example.com/v1"].join("\n")); + const now = Date.now(); + fs.writeFileSync(path.join(home, "agent.db"), "real-db"); + fs.writeFileSync(path.join(home, "agent.db.bili-conflict"), "previous-loser"); + const overlay = `${home}-bili`; + fs.mkdirSync(overlay); + fs.writeFileSync(path.join(overlay, "agent.db"), "overlay-db-v1"); + fs.writeFileSync(path.join(overlay, "agent.db-wal"), "overlay-wal-v1"); + fs.utimesSync(path.join(home, "agent.db"), new Date(now - 10000), new Date(now - 10000)); + fs.utimesSync(path.join(overlay, "agent.db"), new Date(now - 60000), new Date(now - 60000)); + const rw = [{ key: "a", realUpstream: "http://example.com/v1" }]; + let tmp: string | undefined; + try { + tmp = prepareOmpHttpRewrite(home, "http://127.0.0.1:8787", rw, []); + assert.equal(tmp, overlay); + assert.equal(fs.readFileSync(path.join(home, "agent.db"), "utf8"), "real-db", "real main db (newer) wins"); + assert.equal(fs.readFileSync(path.join(home, "agent.db.bili-conflict"), "utf8"), "previous-loser", "previous round's loser NOT overwritten"); + assert.equal(fs.readFileSync(path.join(home, "agent.db.bili-conflict.1"), "utf8"), "overlay-db-v1", "this round's loser gets a suffixed name"); + } finally { + if (tmp) fs.rmSync(tmp, { recursive: true, force: true }); + fs.rmSync(home, { recursive: true, force: true }); + } +}); + test("readOpencodeConfig: reads provider baseURLs from opencode.json", () => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "oc-cfg-")); try { From 877711463f912c6fd3783ffb5406b1380b674bef Mon Sep 17 00:00:00 2001 From: ework-agent Date: Mon, 31 Aug 2026 23:51:48 +0800 Subject: [PATCH 02/22] fix: archive blocks across omp native compaction instead of silently deactivating them (#395) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit omp's in-session native compaction re-sends a shortened history (its summary + retained tail) without rotating the session id, so bili's per-sid registry reuses stale state and syncBlocks silently deactivates every block whose raw ids left the history — making decompress permanently unreachable and decoupling acp_status from the real visible context (the ledger break #19 shelved). Wire the omp session_compact hook bili was ignoring: - agent plugin (pi/omp) POSTs /__bili/plugin/compact on session_compact - proxy marks a compaction boundary; the next processTurn degrades the active->inactive blocks into an auditable pre-compaction archive - acp_status lists archived blocks with reason; decompress returns an explicit error instead of silent failure or wrong-block content - prune byRaw/byRef to live raw ids at the boundary (stops the #390 additive leak); nextIndex is left alone so a freed ref slot is never re-allocated onto a retained tail's live tag --- src/agent/pi.ts | 16 +++++ src/decompress-shared.ts | 6 +- src/loop/core.ts | 11 ++- src/plugin.ts | 29 +++++++- src/server.ts | 19 ++++- src/session.ts | 76 ++++++++++++++++++++ tests/compaction-archive.test.ts | 116 +++++++++++++++++++++++++++++++ 7 files changed, 268 insertions(+), 5 deletions(-) create mode 100644 tests/compaction-archive.test.ts diff --git a/src/agent/pi.ts b/src/agent/pi.ts index 967f844ffe..e5fca5f17c 100644 --- a/src/agent/pi.ts +++ b/src/agent/pi.ts @@ -304,6 +304,22 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM state.sid = undefined; void registerTools(pi, ctx, state, agent).catch((err: unknown) => console.error(`bili-plugin(${agent}): ${err instanceof Error ? err.message : String(err)}`)); }); + // omp fires session_compact on in-session native compaction (sid does + // not rotate), so the proxy reuses stale state — notify it to archive + // the now-unreachable blocks (#395). Fire-and-forget: a failed + // notification must never break the agent's compaction. + pi.on("session_compact", (_event, ctx) => { + const proxyBase = proxyBaseForCtx(ctx); + if (proxyBase === undefined) return; + const sid = sessionIdOf(ctx); + if (sid === undefined || sid.length === 0) return; + fetch(`${proxyBase}/__bili/plugin/compact`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ conversationId: sid }), + signal: AbortSignal.timeout(5000), + }).catch(() => {}); + }); }; } diff --git a/src/decompress-shared.ts b/src/decompress-shared.ts index cf6e176fed..fdf789a1e8 100644 --- a/src/decompress-shared.ts +++ b/src/decompress-shared.ts @@ -8,7 +8,7 @@ import { import { mkdirSync, unlinkSync, writeFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { tmpdir } from "node:os"; -import type { Session } from "./session.js"; +import { preCompactionArchiveOf, type Session } from "./session.js"; /** Bounded retention for large-decompress temp files. Each decompress with * body > 10000 writes one file under tmpdir(); the reaper unlinks oldest past @@ -73,6 +73,10 @@ export function resolveDecompress( const blockId = rawBlockId.trim(); const block = ctx.core.decompress(blockId, ctx.session.state); if (!block) return `[Block ${blockId} not found]`; + const archived = preCompactionArchiveOf(ctx.session); + if (archived[blockId] !== undefined) { + return `[decompress FAILED: block ${blockId} is a pre-compaction archive — its content was in the history BEFORE the client's native compaction and is no longer reachable (replaced by the client's compaction summary). decompress is unavailable for archived blocks.]`; + } const full = args.full === true; const cached = ctx.session.blockContents.get(blockId); diff --git a/src/loop/core.ts b/src/loop/core.ts index ca10e68df0..e4331c3c1d 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -8,7 +8,7 @@ import { type CoreMessage, type NudgeDecision, } from "acp-kernel"; -import { lastCompressSuffix, type Session } from "../session.js"; +import { lastCompressSuffix, preCompactionArchiveOf, type Session } from "../session.js"; import type { BiliMessage } from "acp-kernel/wire"; import { parseCompressInput, @@ -141,6 +141,8 @@ function handleAcpStatus(args: Record, ctx: LoopCtx): string { const nudge = ctx.nudge; const ranges = nudge?.compressibleRanges ?? []; const protectedRanges = nudge?.protectedRanges ?? []; + const archive = preCompactionArchiveOf(ctx.session); + const archivedIds = Object.keys(archive); const extra: string[] = []; if (nudge) { extra.push(""); @@ -150,6 +152,13 @@ function handleAcpStatus(args: Record, ctx: LoopCtx): string { extra.push(""); extra.push(formatRanges(ranges, protectedRanges)); } + if (archivedIds.length > 0) { + extra.push(""); + extra.push(`PRE-COMPACTION ARCHIVE — ${archivedIds.length} block(s): content was replaced by the client's native compaction summary, so it is no longer in the session history and decompress is unavailable.`); + for (const id of archivedIds) { + extra.push(` ${id} — ${archive[id].reason}`); + } + } return extra.length > 0 ? `${base}\n${extra.join("\n")}` : base; } diff --git a/src/plugin.ts b/src/plugin.ts index 093bdb440e..f5caa9e416 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -3,7 +3,7 @@ import { buildStatusPanel } from "acp-kernel/panel"; import { fileURLToPath } from "node:url"; import fs from "node:fs"; import path from "node:path"; -import { acquireInFlight, listSessions, markDirty, peekSession, releaseInFlight, withSessionLock, type Session } from "./session.js"; +import { acquireInFlight, listSessions, markCompactionBoundary, markDirty, peekSession, releaseInFlight, withSessionLock, type Session } from "./session.js"; import { ACP_TOOLS_ANTHROPIC, ACP_TOOLS_OPENAI, ACP_TOOLS_RESPONSES, PROXY_TOOL_NAMES } from "./compress-tool.js"; import { executeProxyTool } from "./loop/core.js"; import { normalizeSseLineEndings } from "./sse-util.js"; @@ -279,6 +279,33 @@ export function handlePluginRegister(payload: string, res: import("node:http").S res.end(JSON.stringify({ ok: true, conversationId, agent })); } +export function handlePluginCompact(payload: string, res: import("node:http").ServerResponse): void { + let parsed: { conversationId?: unknown }; + try { + parsed = JSON.parse(payload) as { conversationId?: unknown }; + } catch { + res.writeHead(400, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: false, error: "invalid JSON body" })); + return; + } + const conversationId = typeof parsed.conversationId === "string" ? parsed.conversationId.trim() : ""; + if (!conversationId) { + res.writeHead(400, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: false, error: "conversationId is required" })); + return; + } + const entry = conversations.get(conversationId); + const session = entry ? peekSession(entry.sessionId) : undefined; + if (!entry || !session) { + res.writeHead(404, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: false, error: "unknown plugin conversation (no model request has arrived with this conversation id yet)" })); + return; + } + markCompactionBoundary(session); + entry.lastSeen = Date.now(); + res.end(JSON.stringify({ ok: true, conversationId })); +} + export function handlePluginManifest(res: import("node:http").ServerResponse): void { res.writeHead(200, { "content-type": "application/json" }); res.end(JSON.stringify({ diff --git a/src/server.ts b/src/server.ts index 351ea5f517..bae7382695 100644 --- a/src/server.ts +++ b/src/server.ts @@ -41,7 +41,7 @@ import { conversationSignalResponses, subagentNamespace, } from "acp-kernel/wire"; -import { getSession, listSessions, type Session, initSessions, markDirty, flushAllSessions, acquireInFlight, releaseInFlight, withSessionLock, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, snapshotMessages } from "./session.js"; +import { getSession, listSessions, type Session, initSessions, markDirty, flushAllSessions, acquireInFlight, releaseInFlight, withSessionLock, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, snapshotMessages, applyCompactionArchive } from "./session.js"; import { COMPRESS_TOOL, ACP_TOOLS_ANTHROPIC, ACP_TOOLS_OPENAI, ACP_TOOLS_RESPONSES, ACP_READONLY_TOOLS_RESPONSES, COMPRESS_TOOL_NAME, buildCompressSystemPrompt, buildCompressHybridSystemPrompt, withStagedCompressGuidance } from "./compress-tool.js"; import { rewriteSseStream, rewriteJsonResponse, type RewriteCtx } from "./stream.js"; import { applyRanges } from "./stream.js"; @@ -62,7 +62,7 @@ import { observeResponsesTerminalState } from "./stream-terminal.js"; import { emitStreamError } from "./stream-error.js"; import { affinityToken, clientConversationHeader, codexTurnIdentity, preferPromptCacheKeyIdentity, type ConversationIdentity } from "./session-id.js"; import { prefixAffinity, type AnonymousAffinity } from "./prefix-affinity.js"; -import { consumePluginRegisterFor, flushConversations, handlePluginManifest, handlePluginRegister, handlePluginStatus, handlePluginTool, loadConversations, pipePluginJson, pipePluginResponsesWithStrip, pipeThroughWithUsage, pluginAgentHeader, pluginConversationHeader, pluginReportedContextWindow, recordPluginSession, rememberPluginMessages, takePendingPluginRegister } from "./plugin.js"; +import { consumePluginRegisterFor, flushConversations, handlePluginCompact, handlePluginManifest, handlePluginRegister, handlePluginStatus, handlePluginTool, loadConversations, pipePluginJson, pipePluginResponsesWithStrip, pipeThroughWithUsage, pluginAgentHeader, pluginConversationHeader, pluginReportedContextWindow, recordPluginSession, rememberPluginMessages, takePendingPluginRegister } from "./plugin.js"; import { setupMitm, readMitmUpstream } from "./mitm.js"; import type { BiliMessage } from "acp-kernel/wire"; import { hoistMidSystemMessages, isLoopbackAddress, inspectContextOverflow, reserveOutputHeadroom, shouldReserveOutputHeadroom, usageTotals } from "./util.js"; @@ -681,6 +681,17 @@ async function handle( return; } } + if (req.method === "POST" && req.url === "/__bili/plugin/compact") { + try { + const body = await readBody(req); + handlePluginCompact(body.toString("utf8"), res); + return; + } catch (err) { + res.writeHead(err instanceof BodyTooLargeError ? 413 : 400, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: false, error: String(err) })); + return; + } + } // Unknown /__bili/ or /__acp/ path → 404 locally. These are bili's own // management prefixes; forwarding would leak the internal path to the // upstream (which 403s it) — #346. @@ -1314,6 +1325,7 @@ function prepareAnthropic( // estimates to the kernel. extractSystem(parsed.system); const tokenCount = session.stats.lastInputTokens; + const activeBefore = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount, renderTags: "text-only" }); session.state = turn.state; // The fold from last turn's compress has now materialized in state — @@ -1332,6 +1344,7 @@ function prepareAnthropic( log("info", diagTagSummary(turn.messages, sessionId, "text-only")); log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model)); processedMessages = stripKernelSummaries(turn.messages, turn.state); + applyCompactionArchive(session, activeBefore, new Set(msgs.map((m) => m.id)), log); reapOrphanBlocks(session, msgs, deactivateBlock); rebuiltMessages = coreToAnthropic(processedMessages as BiliMessage[], cacheControls); @@ -1408,6 +1421,7 @@ function prepareOpenai( // tokenCount = upstream's real input_tokens from the previous turn // (see anthropic branch comment). Never an estimate. const tokenCount = session.stats.lastInputTokens; + const activeBefore = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount, renderTags: "text-only" }); session.state = turn.state; // The fold from last turn's compress has now materialized in state — @@ -1426,6 +1440,7 @@ function prepareOpenai( log("info", diagTagSummary(turn.messages, sessionId, "text-only")); log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model)); processedMessages = stripKernelSummaries(turn.messages, turn.state); + applyCompactionArchive(session, activeBefore, new Set(msgs.map((m) => m.id)), log); reapOrphanBlocks(session, msgs, deactivateBlock); rebuiltMessages = hoistMidSystemMessages(coreToOpenai(processedMessages as BiliMessage[])); diff --git a/src/session.ts b/src/session.ts index c180c2ab38..a313166309 100644 --- a/src/session.ts +++ b/src/session.ts @@ -287,6 +287,82 @@ export function reconcileNativeCompactionBoundary(session: Session): boolean { return true; } +/** Mark a client-side native compaction (omp /compact on the anthropic wire). + * The sid does NOT rotate on in-session compaction, so the per-sid registry + * reuses the same key with stale state. Consumed by the NEXT processTurn via + * applyCompactionArchive (#395). Distinct from nativeCompactionBoundary + * (Responses/codex, which rebases by resetting state). */ +export function markCompactionBoundary(session: Session): void { + session.metadata.compactionBoundary = { + at: Date.now(), + pending: true, + }; + markDirty(session); +} + +type PreCompactionArchive = Record; + +function readPreCompactionArchive(session: Session): PreCompactionArchive { + const raw = session.metadata.preCompactionArchive; + if (raw && typeof raw === "object" && !Array.isArray(raw)) { + return raw as PreCompactionArchive; + } + return {}; +} + +export function preCompactionArchiveOf(session: Session): PreCompactionArchive { + return readPreCompactionArchive(session); +} + +// Must run AFTER the processTurn that followed markCompactionBoundary (so +// syncBlocks has deactivated the blocks whose raw ids left the shortened +// history). Blocks active in `activeBefore` but inactive now are archived; +// byRaw/byRef are pruned to liveRawIds (stops the #390 additive leak). +// nextIndex is left alone so a freed ref slot is never re-allocated onto a +// retained tail's live tag. +export function applyCompactionArchive( + session: Session, + activeBefore: Set, + liveRawIds: Set, + log: (level: string, msg: string) => void, +): void { + const boundary = session.metadata.compactionBoundary; + if (!boundary || typeof boundary !== "object" || !(boundary as Record).pending) { + return; + } + const activeAfter = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); + const deactivated = [...activeBefore].filter((id) => !activeAfter.has(id)); + if (deactivated.length > 0) { + const archive = readPreCompactionArchive(session); + const at = Date.now(); + for (const id of deactivated) { + archive[id] = { at, reason: "content replaced by client native compaction summary" }; + } + session.metadata.preCompactionArchive = archive; + } + + const { byRaw, byRef } = session.state.messageRefs; + const prunedByRaw: Record = {}; + for (const [rawId, ref] of Object.entries(byRaw)) { + if (liveRawIds.has(rawId)) prunedByRaw[rawId] = ref; + } + const prunedByRef: Record = {}; + for (const [ref, rawId] of Object.entries(byRef)) { + if (liveRawIds.has(rawId)) prunedByRef[ref] = rawId; + } + session.state.messageRefs.byRaw = prunedByRaw; + session.state.messageRefs.byRef = prunedByRef; + + session.metadata.compactionBoundary = { + ...(boundary as Record), + pending: false, + archivedAt: Date.now(), + archivedBlocks: deactivated, + }; + markDirty(session); + log("info", `[${session.id}] native compaction boundary: archived ${deactivated.length} pre-compaction block(s)${deactivated.length > 0 ? ` (${deactivated.join(", ")})` : ""}; pruned ref maps to ${prunedByRaw.length} live raw id(s)`); +} + /** Flush a session to disk and drop it from memory (LRU eviction). Refuses to * evict sessions that are in-flight or whose flush failed (would lose a * never-persisted session permanently). Returns true if a slot was freed. */ diff --git a/tests/compaction-archive.test.ts b/tests/compaction-archive.test.ts new file mode 100644 index 0000000000..354f6fdb0d --- /dev/null +++ b/tests/compaction-archive.test.ts @@ -0,0 +1,116 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { createCore, createInitialState, defaultConfig, type CoreMessage } from "acp-kernel"; +import type { Session } from "../src/session.ts"; +import { applyCompactionArchive, markCompactionBoundary, preCompactionArchiveOf } from "../src/session.ts"; +import { resolveDecompress } from "../src/decompress-shared.ts"; +import { executeProxyTool, type LoopCtx } from "../src/loop/core.ts"; + +function makeSession(): Session { + return { + id: `test-${Math.random().toString(36).slice(2)}`, + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, contextTokens: 0 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + }; +} + +const noLog = (_level: string, _msg: string): void => {}; + +// One active block over m00001-m00007; tail = m00008-m00012 (omp's post-compact resend). +function sessionWithActiveBlock(): { session: Session; blockId: string; tail: CoreMessage[] } { + const session = makeSession(); + const core = createCore(); + const config = defaultConfig(200000); + const msgs: CoreMessage[] = []; + for (let i = 0; i < 12; i++) { + msgs.push({ id: `h_distinct${i}`, role: i % 2 === 0 ? "user" : "assistant", contentType: "text", text: `message ${i} ${"x".repeat(2000)}` }); + } + const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount: 9999, renderTags: "text-only" }); + session.state = turn.state; + const res = core.applyCompression({ + ranges: [{ startRef: "m00001", endRef: "m00007", summary: "compressed early history that is long enough to pass the min summary length check" }], + messages: turn.messages, + state: turn.state, + config, + }); + session.state = res.state; + const block = session.state.blocks[session.state.blocks.length - 1]; + const tail: CoreMessage[] = []; + for (let i = 7; i < 12; i++) { + tail.push({ id: `h_distinct${i}`, role: i % 2 === 0 ? "user" : "assistant", contentType: "text", text: `message ${i} ${"x".repeat(2000)}` }); + } + return { session, blockId: block.blockId, tail }; +} + +// Mirrors server.ts prepareAnthropic: capture active ids → shortened-history +// turn (syncBlocks deactivates) → applyCompactionArchive. +function runShortenedTurn(session: Session, tail: CoreMessage[], boundary: boolean): string { + const core = createCore(); + const config = defaultConfig(200000); + const activeBefore = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); + if (boundary) markCompactionBoundary(session); + const turn = core.processTurn({ messages: tail, state: session.state, config, tokenCount: 9999, renderTags: "text-only" }); + session.state = turn.state; + applyCompactionArchive(session, activeBefore, new Set(tail.map((m) => m.id)), noLog); + return [...activeBefore].join(","); +} + +test("compaction archive: shortened-history resend degrades the block into an auditable archive", () => { + const { session, blockId, tail } = sessionWithActiveBlock(); + runShortenedTurn(session, tail, true); + const block = session.state.blocks.find((b) => b.blockId === blockId); + assert.equal(block?.active, false, "syncBlocks deactivates the block whose raw ids left the history"); + const archive = preCompactionArchiveOf(session); + assert.ok(archive[blockId] !== undefined, "block is recorded in the pre-compaction archive (not silently deactivated)"); + assert.match(archive[blockId]!.reason, /native compaction/); + const boundary = session.metadata.compactionBoundary as Record; + assert.equal(boundary.pending, false, "boundary is consumed"); + assert.ok(Array.isArray(boundary.archivedBlocks) && (boundary.archivedBlocks as string[]).includes(blockId), "archivedBlocks records the generation"); +}); + +test("compaction archive: WITHOUT the boundary the block is silently deactivated (old behavior preserved)", () => { + const { session, blockId, tail } = sessionWithActiveBlock(); + runShortenedTurn(session, tail, false); + const block = session.state.blocks.find((b) => b.blockId === blockId); + assert.equal(block?.active, false, "still deactivated by syncBlocks"); + const archive = preCompactionArchiveOf(session); + assert.equal(archive[blockId], undefined, "no archive entry without a marked boundary"); +}); + +test("compaction archive: byRaw/byRef are pruned to live ids (stops the additive leak)", () => { + const { session, tail } = sessionWithActiveBlock(); + const before = Object.keys(session.state.messageRefs.byRaw).length; + runShortenedTurn(session, tail, true); + const liveIds = new Set(tail.map((m) => m.id)); + for (const rawId of Object.keys(session.state.messageRefs.byRaw)) { + assert.ok(liveIds.has(rawId), `byRaw entry ${rawId} should have been pruned`); + } + assert.ok(Object.keys(session.state.messageRefs.byRaw).length < before, "byRaw shrank after the boundary"); +}); + +test("compaction archive: decompress on an archived block gives an explicit reason, not silent/wrong content", () => { + const { session, blockId, tail } = sessionWithActiveBlock(); + runShortenedTurn(session, tail, true); + const ctx = { core: createCore(), config: defaultConfig(200000), messages: tail, session, log: (_msg: string) => {} }; + const out = resolveDecompress({ blockId }, ctx); + assert.match(out, /decompress FAILED/); + assert.match(out, /pre-compaction archive/); + assert.match(out, /no longer reachable/); +}); + +test("compaction archive: acp_status surfaces the archive instead of listing it as active", () => { + const { session, blockId, tail } = sessionWithActiveBlock(); + runShortenedTurn(session, tail, true); + const ctx: LoopCtx = { core: createCore(), config: defaultConfig(200000), messages: tail, session, log: (_msg: string) => {} }; + const out = executeProxyTool("acp_status", {}, ctx); + assert.match(out, /PRE-COMPACTION ARCHIVE/); + assert.match(out, /decompress is unavailable/); + assert.ok(out.includes(blockId), "acp_status names the archived block"); +}); From 1b258eef0f26e331ffcd48c3330097923e7c1a15 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Mon, 31 Aug 2026 23:57:17 +0800 Subject: [PATCH 03/22] docs: document POST /__bili/plugin/compact in the plugin protocol (#395) --- PLUGIN.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/PLUGIN.md b/PLUGIN.md index 8eeae76a96..8faf25363b 100644 --- a/PLUGIN.md +++ b/PLUGIN.md @@ -115,11 +115,21 @@ Context-level visibility for plugin UIs (status bars / slash commands): `contextTokens` is the last reported context size (input + cache-read) — the same value the nudge decision reads. Errors: `400` missing `conversationId`, `404` unknown conversation. -### 5. MITM transparent-proxy mode +### 5. `POST /__bili/plugin/compact` + +Notify the proxy that the agent performed an **in-session native compaction** (e.g. omp's `/compact` or its auto threshold): the next model request re-sends a shortened history (compaction summary + retained tail) under the SAME conversation id. Fire-and-forget is fine — a failed notification must never break the agent's compaction. + +```json +{ "conversationId": "the same value you send as x-bili-plugin-conversation" } +``` + +Effect: the proxy marks a one-shot compaction boundary on the session. On the next model request, blocks that were active before the compaction but no longer anchor into the shortened history are downgraded to a pre-compaction archive (listed by `acp_status` with a reason; `decompress` on one returns an explicit "unavailable" error instead of failing silently), and stale `byRaw`/`byRef` mappings are pruned to the live ids. Errors: `400` invalid JSON / missing `conversationId`, `404` unknown conversation. + +### 6. MITM transparent-proxy mode The `/bili/` prefix is absent in MITM mode, so URL-based detection cannot work. Instead the proxy's own launcher (`bili pi` / `bili codex` / `bili claude`) exports `BILLION_CONTEXT_PROXY=http://127.0.0.1:` in the child env, next to the `HTTPS_PROXY` + CA vars it already sets. A plugin detects cooperative mode by reading that env var (the proxy origin for all `/__bili/plugin/*` calls); everything else (headers, tool forwarding, status) is identical — the `x-bili-plugin*` headers pass through the MITM tunnel into the same pipeline. This is also where `x-bili-plugin-context-window` matters most: MITM upstreams are often private relays the models.dev registry doesn't know. -### 6. Lifecycle of one compression (what the plugin does) +### 7. Lifecycle of one compression (what the plugin does) 1. Model replies with a native `compress` tool call (args contain `startId`/`endId` refs it read from the tag-annotated context). 2. The agent ends the assistant turn; the plugin's tool handler fires. From 29fc896261c997b165c4d8e99dc4ccf7418b1fda Mon Sep 17 00:00:00 2001 From: ework-agent Date: Wed, 2 Sep 2026 09:27:34 +0800 Subject: [PATCH 04/22] fix: acp-loop zero-side-effect truncation retry + well-formed anthropic error stream (#413) --- .../DESIGN.md | 109 +++++++++ .../REQ.md | 57 +++++ .../WORKLOG.md | 58 +++++ src/loop/adapter-anthropic.ts | 46 +++- src/loop/core.ts | 136 ++++++++--- tests/loop-truncation.test.ts | 230 ++++++++++++++++++ 6 files changed, 594 insertions(+), 42 deletions(-) create mode 100644 devlog/2026-09-02_acp-loop-truncation-retry/DESIGN.md create mode 100644 devlog/2026-09-02_acp-loop-truncation-retry/REQ.md create mode 100644 devlog/2026-09-02_acp-loop-truncation-retry/WORKLOG.md create mode 100644 tests/loop-truncation.test.ts diff --git a/devlog/2026-09-02_acp-loop-truncation-retry/DESIGN.md b/devlog/2026-09-02_acp-loop-truncation-retry/DESIGN.md new file mode 100644 index 0000000000..990ead4dc0 --- /dev/null +++ b/devlog/2026-09-02_acp-loop-truncation-retry/DESIGN.md @@ -0,0 +1,109 @@ +# DESIGN: acp-loop truncation retry + well-formed Anthropic error stream + +## Problem shape + +`runCompressLoop` is an `AsyncGenerator`: it reads the upstream SSE +stream, forwards client-facing chunks as it parses them, and only knows the +stream was truncated *after* the parse loop ends without a done event +(`!sawDone`). `fetchWithRetry` cannot see this failure — it inspects +`response.ok` after headers and never reads the body. So recovery has to live +at loop level, where the "did I already commit to the client?" state is known. + +## Retry predicate — the zero-side-effect invariant + +A truncated round can be transparently replayed **iff** nothing observable +happened. All of these must hold at the end of the parse loop: + +- `!sawDone` — the stream actually truncated; +- `!forwardedAny` — no chunk was yielded to the client this attempt. A + per-attempt flag set by `fwd()` wrapping the five yield sites in the parse + loop (raw text, synthetic text, raw reasoning, synthetic reasoning, meta). + Ping-only streams count as forwarded (conservative: the client saw + something, so a retry would duplicate it); +- `calls.length === 0` — no tool call was collected. The `tool_call` / + marker / `emitText` yields happen *after* the parse loop, so they are not + yet covered by `forwardedAny` and need explicit checks: + - `calls.length === 0` rules out tool_call and marker yields (both are + derived from `calls`); + - `!(ctx.textProtocol && assistantText.length > 0)` rules out the + text-protocol `emitText` yield (only emitted when textProtocol and the + round produced text); +- `!signal?.aborted` — the client is still connected; +- `!truncationRetried` — function-level flag: at most ONE retry per request + (the issue says "走一次"). The flag is never reset, so a second truncated + round (round 2+) also fails fast. + +Placement: the check runs **after** the parse loop and **before** +`recordUsage`. This matters: a round-2+ `message_start` sets `usage` without +forwarding anything, so recording before the retry check would double-count +the retried attempt's tokens. + +Mechanics: the parse loop is wrapped in an inner `for (;;)` attempt loop. +On a qualifying truncation the loop calls `fetchUpstream(roundBody)` (the +hoisted `fetchWithRetry` wrapper — same backoff, same `BILI_REPLAY_RETRY_*` +env knobs, same #189 rejection logging) and `continue`s; the outer round +number is unchanged, so a round-1 retry re-parses as round 1 and its +`message_start` is forwarded exactly once (attempt 1 forwarded nothing). +Per-attempt state (text/reasoning/calls/usage/finishReason/sawDone/ +suppressCompletion/forwardedAny) is reset at the top of the attempt loop. + +`roundBody` (new): the re-request path replaces the body each round +(`compressMessages`); the old code re-fetched with `requestOptions` + a local +`newBody`. The retry must re-send **the body that produced the truncated +stream** — on round 1 that is the original `requestBody`, on round N it is +the compressed body. `roundBody` is updated right before the +`currentUpstream` swap, so it always holds the last body actually sent. + +Failure of the retry itself falls through to the existing truncation error +path (empty body on the retry response is converted to +`UpstreamHttpError` so it is caught by the same handler). + +## Adapter: tracking what was forwarded + +The Anthropic adapter closure gains two pieces of state: + +- `messageStartForwarded: boolean` — set where the round-1 `message_start` + is forwarded (always forwarded in round 1, so the flag is set there); +- `openBlocks: number[]` — client indices of blocks whose + `content_block_start` was forwarded but whose `content_block_stop` was not. + Pushed in the non-tool `content_block_start` branch (forwarded in all + rounds); removed in the `content_block_stop` branches via an `indexOf` + guard (tool_use blocks are never pushed — they are buffered in `pending` + and emitted as a whole later). + +`emitError(message)` now emits, in order: + +1. synthetic `message_start` (if `!messageStartForwarded`) — + `buildSyntheticMessageStart()`: the captured `messageId` if any, else a + generated `msg_acp_error_` id; empty `content`, null stop reason, + zero usage, the request model if known. Zero usage is honest: nothing + completed. +2. `content_block_stop` for every index in `openBlocks` (drained); +3. the existing error text block (`[acp-proxy: ]`); +4. the existing terminal (`message_delta` + `message_stop`). + +Result: the stream is always well-formed — starting frame first, every block +closed, terminal last — regardless of where the truncation hit (before +`message_start`, mid-thinking-block, mid-text-block, or after everything). + +On the retry path the adapter state is all-initial at re-parse time +(`messageId` undefined, `clientIndex` 0, flag false, no open blocks), so the +re-parsed stream is shaped exactly like a fresh first parse — consistent. + +## Logging + +The truncation path keeps `ctx.log` (session-prefixed `info` line, which is +what session-level dashboards key on) and drops the global +`loggerLog("error", ...)` duplicate. One event, one line. (The upstream-HTTP +failure path on re-request still double-logs — same pattern, out of scope, +reported in the PR.) + +## What is NOT retried (and why) + +- Anything that forwarded a single byte to the client (retry would emit a + duplicate/partial stream the client cannot reconcile); +- Any round that collected a tool call or ran a proxy tool (side effects); +- Text-protocol rounds that accumulated text (the post-loop `emitText` yield + would double it); +- Aborted requests; +- Second and later truncations within one request (one retry, per the issue). diff --git a/devlog/2026-09-02_acp-loop-truncation-retry/REQ.md b/devlog/2026-09-02_acp-loop-truncation-retry/REQ.md new file mode 100644 index 0000000000..1a6ebf9406 --- /dev/null +++ b/devlog/2026-09-02_acp-loop-truncation-retry/REQ.md @@ -0,0 +1,57 @@ +# REQ: acp-loop upstream truncation — zero-side-effect retry, well-formed Anthropic error stream, single-point logging + +Issue #413: when the upstream LLM returns 200 but the SSE stream ends early +(no `message_delta`/`message_stop`), the compress loop (`src/loop/core.ts`) +fails fast with `emitError` and no recovery. Three concrete defects, all +confirmed by code read + red tests before the fix: + +1. **No retry for zero-side-effect truncation.** `fetchWithRetry` only wraps + the re-request fetches (it inspects `response.ok` after headers and never + reads the body — mid-stream EOF is only observable at loop level via the + missing done event). A round that forwarded nothing to the client + (`0 text + 0 reasoning + 0 executed tools`, nothing yielded) is a + zero-side-effect retry candidate; the loop instead surfaced the upstream + flake to the host session. Production evidence: 3 truncation events in + 3 days, two of them 0+0 (2026-08-31T00:35:24Z, 2026-08-31T06:03:43Z). +2. **Orphan Anthropic stream on first-round truncation.** The adapter's + `emitError` is stateless w.r.t. what was already forwarded: it emits + `content_block_start/delta/stop + message_delta + message_stop` directly. + If the stream truncated before `message_start` reached the client, the + client sees a `content_block_*` sequence with no starting frame — + malformed per the Anthropic stream protocol. Open `thinking` blocks left + by the truncation are never closed either. The responses branch already + fixed the same class (see `src/stream-error.ts` comment); the Anthropic + adapter was missed. +3. **Double logging.** Every truncation event was logged twice: `ctx.log` + (→ `log("info", "[sid] ...")`) plus a global `loggerLog("error", ...)` — + both tee to the same file + stderr. + +## Acceptance criteria (from the issue) + +- Stub upstream: headers then 0-event EOF → exactly 1 retry observed; after + the failed retry the client stream passes "SSE start frame before + content_block" schema validation. +- Truncation fixture with open blocks: every `content_block_start` has a + matching `content_block_stop` before stream end. +- Each single truncation produces exactly 1 log line. + +## Constraints + +- The retry must be provably side-effect-free: nothing forwarded to the + client, no tool calls executed, no usage recorded, no state mutation. +- At most ONE retry per request (the issue says "走一次"). +- No new dependencies; `fetchWithRetry` reuse (same backoff/env knobs). +- The re-request path's behavior (degraded retry, #189 rejection logging) + is unchanged; only its `fetchUpstream` definition is hoisted. + +## Non-goals (reported in the PR, not fixed) + +- `adapter-responses.ts` `emitError`: a `response.failed` without a prior + `response.created` is the same orphan class if the stream truncated before + the created event. +- `core.ts` upstream-error path (HTTP failure on re-request) also double-logs + (`ctx.log` + `loggerLog("error")`) — same pattern; the issue only reported + the truncation one. +- Truncation with `reRequest = true` (truncated after a complete proxy tool + call, before the done event) is silently swallowed — the loop continues to + the next round. Arguably benign; unchanged. diff --git a/devlog/2026-09-02_acp-loop-truncation-retry/WORKLOG.md b/devlog/2026-09-02_acp-loop-truncation-retry/WORKLOG.md new file mode 100644 index 0000000000..bd41022ada --- /dev/null +++ b/devlog/2026-09-02_acp-loop-truncation-retry/WORKLOG.md @@ -0,0 +1,58 @@ +# WORKLOG: acp-loop upstream truncation — zero-side-effect retry, well-formed Anthropic error stream, single-point logging + +- Task ID: `2026-09-02_acp-loop-truncation-retry` +- Home Repo: `billion-context` +- Status: Done +- Updated: 2026-09-02 + +## 1. Summary + +- **What was done**: (1) the compress loop now retries once when an upstream + stream truncates (200 + early EOF) after forwarding nothing to the client; + (2) the Anthropic adapter's `emitError` now synthesizes a `message_start` + if none was forwarded and closes every block still open at error time; + (3) the truncation event is logged once (via `ctx.log`) instead of twice. +- **Why**: zero-side-effect truncations are the most common recoverable + upstream flake (3 in 3 days in production, two of them 0+0); failing fast + lands the flake in the host session. The orphan stream shape violates the + Anthropic stream protocol and can crash strict clients (the responses + branch already fixed its own copy of this class). Double logging makes + incident triage count events wrong. +- **Behavior / compatibility changes**: Yes, three — + 1. A first-round (or any-round) truncation that forwarded nothing and + executed no tool calls now triggers exactly one retry through + `fetchWithRetry` before the error is surfaced. Clients see either a + clean successful stream or the error stream — never a partial one. + 2. The Anthropic error stream is now always well-formed: starts with + `message_start`, every `content_block_start` has a matching + `content_block_stop`, ends with `message_delta` + `message_stop`. + 3. Truncation is logged once at `info` (session-prefixed) instead of twice + (`info` + `error`). +- **Risk level**: Low. The retry is gated on a strict zero-side-effect + predicate (nothing forwarded, no tool calls, nothing accumulated for the + host, not aborted, not already retried); the adapter change only adds + frames that strict clients require; logging change is cosmetic. + +## 2. Change Log + +### Commits + +| Commit | Description | +|--------|-------------| +| `` | fix: acp-loop zero-side-effect truncation retry + well-formed anthropic error stream (#413) | + +### Key Files + +- `src/loop/core.ts` — retry loop around the per-round parse; hoisted + `fetchUpstream`; dropped the second (global error) log line on truncation; + `roundBody` tracks the last body actually sent so a retry re-sends the + compressed round body, not the original request. +- `src/loop/adapter-anthropic.ts` — `messageStartForwarded` / `openBlocks` + tracking in the adapter closure; `buildSyntheticMessageStart()`; + `emitError` closes open blocks and backfills the starting frame. +- `tests/loop-truncation.test.ts` — new; 6 tests covering all three + acceptance criteria plus retry-body tracking and the once-per-request cap. + +## 3. Design & Implementation Notes + +See `DESIGN.md` for the dataflow and the safety invariants. diff --git a/src/loop/adapter-anthropic.ts b/src/loop/adapter-anthropic.ts index 175ca4502f..c986abf5c3 100644 --- a/src/loop/adapter-anthropic.ts +++ b/src/loop/adapter-anthropic.ts @@ -120,6 +120,35 @@ export function createAnthropicAdapter(requestBody: Record, ori const model = (requestBody.model as string) ?? undefined; let messageId: string | undefined; let clientIndex = 0; + let messageStartForwarded = false; + const openBlocks: number[] = []; + + const removeOpenBlock = (index: number): void => { + const i = openBlocks.indexOf(index); + if (i >= 0) openBlocks.splice(i, 1); + }; + + // #413: emitError must terminate a well-formed stream even when the + // upstream died before sending message_start — synthesize the start frame + // so content blocks are never orphaned (strict clients reject them). + const buildSyntheticMessageStart = (): Buffer => { + const extra: Record = {}; + if (model) extra.model = model; + const msg: Record = { + id: messageId ?? `msg_acp_error_${Date.now()}`, + type: "message", + role: "assistant", + content: [], + stop_reason: null, + stop_sequence: null, + usage: { input_tokens: 0, output_tokens: 0 }, + ...extra, + }; + return Buffer.from( + `event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: msg })}\n\n`, + "utf8", + ); + }; const buildTextBlock = (index: number, text: string): Buffer => Buffer.from( @@ -206,6 +235,7 @@ export function createAnthropicAdapter(requestBody: Record, ori if (typeof u.input_tokens === "number") roundInput = u.input_tokens; if (typeof u.cache_read_input_tokens === "number") roundCached = u.cache_read_input_tokens; if (round === 1) { + messageStartForwarded = true; yield { kind: "meta", chunk: rawBuf, firstRoundOnly: true } as ParsedStreamEvent; } } else if (type === "ping") { @@ -221,6 +251,7 @@ export function createAnthropicAdapter(requestBody: Record, ori if (block.type === "thinking" || block.type === "redacted_thinking") thinkingIndexes.add(upstreamIndex); const ci = clientIndex++; indexMap.set(upstreamIndex, ci); + openBlocks.push(ci); yield { kind: "meta", chunk: remapIndexInEvent(eventStr, ci), firstRoundOnly: round === 1 } as ParsedStreamEvent; } } else if (type === "content_block_delta") { @@ -272,10 +303,12 @@ export function createAnthropicAdapter(requestBody: Record, ori // Seal the current thinking segment so interleaved thinking // blocks each keep their own signature on rebuild. const ci = indexMap.get(upstreamIndex) ?? upstreamIndex; + removeOpenBlock(ci); yield { kind: "meta", chunk: remapIndexInEvent(eventStr, ci), firstRoundOnly: false } as ParsedStreamEvent; yield { kind: "reasoning", delta: "", blockEnd: true } as ParsedStreamEvent; } else { const ci = indexMap.get(upstreamIndex) ?? upstreamIndex; + removeOpenBlock(ci); if (lastTextIndex !== null) { const tail = tagFilter.flush(); if (tail.length > 0) { @@ -385,8 +418,17 @@ export function createAnthropicAdapter(requestBody: Record, ori }, emitError(message) { - const errBlock = buildTextBlock(clientIndex++, `\n[acp-proxy: ${message}]\n`); - return Buffer.concat([errBlock, buildTerminal("end_turn", 0, 0, 0)]); + const parts: Buffer[] = []; + if (!messageStartForwarded) { + parts.push(buildSyntheticMessageStart()); + messageStartForwarded = true; + } + for (const index of openBlocks.splice(0)) { + parts.push(Buffer.from(`event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index })}\n\n`, "utf8")); + } + parts.push(buildTextBlock(clientIndex++, `\n[acp-proxy: ${message}]\n`)); + parts.push(buildTerminal("end_turn", 0, 0, 0)); + return Buffer.concat(parts); }, }; } diff --git a/src/loop/core.ts b/src/loop/core.ts index ca10e68df0..fc7244810f 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -199,8 +199,30 @@ export async function* runCompressLoop( ): AsyncGenerator { let activeClearTimer: (() => void) | null = null; let currentUpstream = upstream; + let roundBody: Record = requestBody; const coreMessages: CoreMessage[] = [...ctx.messages]; let degradedRetried = false; + let truncationRetried = false; + + const fetchUpstream = (body: Record) => + fetchWithRetry( + requestOptions.url, + { + method: "POST", + headers: requestOptions.headers, + body: JSON.stringify(body), + ...(ctx.proxyUrl ? { dispatcher: proxyDispatcher(ctx.proxyUrl) } : {}), + }, + undefined, + signal, + (info) => { + // #189: correlate the rejection with the rewrite that + // preceded it (shrink ratio + fold point), if any. + const lc = lastCompressSuffix(ctx.session.lastCompress); + ctx.log(`[acp-proxy: upstream rejected replay (HTTP ${info.status}: ${info.detail.slice(0, 120)}); likely provider risk-control — retrying in ${info.delayMs}ms (attempt ${info.attempt}/${info.maxAttempts})${lc}]`); + loggerLog("warn", `[acp-loop] upstream rejected replay (HTTP ${info.status}); retrying in ${info.delayMs}ms (attempt ${info.attempt}/${info.maxAttempts})${lc}`); + }, + ); try { for (let round = 1; round <= MAX_LOOP_ROUNDS; round++) { @@ -214,15 +236,32 @@ export async function* runCompressLoop( let finishReason: string | undefined; let sawDone = false; let suppressCompletion = false; - - for await (const ev of adapter.parseStream(currentUpstream, round)) { - if (signal?.aborted) break; + let forwardedAny = false; + const fwd = (chunk: Buffer): Buffer => { + forwardedAny = true; + return chunk; + }; + + for (;;) { + assistantText = ""; + assistantReasoning = ""; + reasoningSegments.length = 0; + reasoningSealed = true; + calls.length = 0; + usage = {}; + finishReason = undefined; + sawDone = false; + suppressCompletion = false; + forwardedAny = false; + + for await (const ev of adapter.parseStream(currentUpstream, round)) { + if (signal?.aborted) break; if (ev.kind === "text") { assistantText += ev.delta; if (!ctx.textProtocol && ev.raw) { - yield ev.raw; + yield fwd(ev.raw); } else if (!ctx.textProtocol && round > 1 && ev.delta.length > 0) { - yield adapter.emitText(ev.delta); + yield fwd(adapter.emitText(ev.delta)); } } else if (ev.kind === "reasoning") { assistantReasoning += ev.delta; @@ -243,22 +282,59 @@ export async function* runCompressLoop( } } } else if (ev.kind === "tool_call") { - calls.push({ name: ev.name, callId: ev.callId, arguments: ev.arguments, passthrough: ev.passthrough }); - } else if (ev.kind === "usage") { - usage = { - inputTokens: ev.inputTokens, - outputTokens: ev.outputTokens, - cachedTokens: ev.cachedTokens, - }; - } else if (ev.kind === "done") { - sawDone = true; - finishReason = ev.finishReason; - suppressCompletion = ev.suppressCompletion === true; - } else if (ev.kind === "meta") { - if (round === 1 || !ev.firstRoundOnly) { - yield ev.chunk; + calls.push({ name: ev.name, callId: ev.callId, arguments: ev.arguments, passthrough: ev.passthrough }); + } else if (ev.kind === "usage") { + usage = { + inputTokens: ev.inputTokens, + outputTokens: ev.outputTokens, + cachedTokens: ev.cachedTokens, + }; + } else if (ev.kind === "done") { + sawDone = true; + finishReason = ev.finishReason; + suppressCompletion = ev.suppressCompletion === true; + } else if (ev.kind === "meta") { + if (round === 1 || !ev.firstRoundOnly) { + yield fwd(ev.chunk); + } } } + + // #413: zero-side-effect truncation — the client received + // nothing from this attempt (no text/reasoning/meta/tool + // bytes), so re-fetching the same round is invisible to it. + // One retry per request; 200+early-EOF flakiness (common on + // relays) no longer lands in the agent session. + if ( + !sawDone && + !forwardedAny && + calls.length === 0 && + !(ctx.textProtocol && assistantText.length > 0) && + !signal?.aborted && + !truncationRetried + ) { + truncationRetried = true; + ctx.log(`[acp-loop] round ${round}: upstream truncated before any content reached the client; retrying fetch once`); + try { + const respResult = await fetchUpstream(roundBody); + if (!respResult.response.body) { + respResult.clearTimer(); + throw new UpstreamHttpError(respResult.response.status, "(empty response body)", 1); + } + currentUpstream = respResult.response.body as ReadableStream; + if (activeClearTimer) activeClearTimer(); + activeClearTimer = respResult.clearTimer; + continue; + } catch (e) { + if (e instanceof UpstreamHttpError) { + const suffix = e.attempts > 1 ? ` after ${e.attempts} attempt(s)` : ""; + ctx.log(`[acp-loop] round ${round}: truncation retry failed (upstream error ${e.status}${suffix}: ${e.body.slice(0, 200)})`); + } else { + ctx.log(`[acp-loop] round ${round}: truncation retry failed (${e instanceof Error ? e.message : String(e)})`); + } + } + } + break; } if ( @@ -403,7 +479,6 @@ export async function* runCompressLoop( const partialReasoning = assistantReasoning.length; const msg = `upstream stream truncated (no completion event; round ${round}, ${partialText} text chars + ${partialReasoning} reasoning chars received)`; ctx.log(`[acp-loop] round ${round}: ${msg}`); - loggerLog("error", `[acp-loop] ${msg}`); yield adapter.emitError(msg); return; } @@ -429,26 +504,6 @@ export async function* runCompressLoop( if (signal?.aborted) break; - const fetchUpstream = (body: Record) => - fetchWithRetry( - requestOptions.url, - { - method: "POST", - headers: requestOptions.headers, - body: JSON.stringify(body), - ...(ctx.proxyUrl ? { dispatcher: proxyDispatcher(ctx.proxyUrl) } : {}), - }, - undefined, - signal, - (info) => { - // #189: correlate the rejection with the rewrite that - // preceded it (shrink ratio + fold point), if any. - const lc = lastCompressSuffix(ctx.session.lastCompress); - ctx.log(`[acp-proxy: upstream rejected replay (HTTP ${info.status}: ${info.detail.slice(0, 120)}); likely provider risk-control — retrying in ${info.delayMs}ms (attempt ${info.attempt}/${info.maxAttempts})${lc}]`); - loggerLog("warn", `[acp-loop] upstream rejected replay (HTTP ${info.status}); retrying in ${info.delayMs}ms (attempt ${info.attempt}/${info.maxAttempts})${lc}`); - }, - ); - let newBody = adapter.buildRequest(coreMessages, systemPrompt, requestBody); if (process.env.ACP_DUMP_BODY === "1") { try { @@ -498,6 +553,7 @@ export async function* runCompressLoop( } currentUpstream = respResult.response.body as ReadableStream; + roundBody = newBody; if (activeClearTimer) activeClearTimer(); activeClearTimer = respResult.clearTimer; } diff --git a/tests/loop-truncation.test.ts b/tests/loop-truncation.test.ts new file mode 100644 index 0000000000..f788b16516 --- /dev/null +++ b/tests/loop-truncation.test.ts @@ -0,0 +1,230 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { Config, CoreMessage } from "acp-kernel"; +import { createCore, createInitialState } from "acp-kernel"; +import type { Session } from "../src/session.ts"; +import { runCompressLoop, createAnthropicAdapter } from "../src/loop/index.ts"; +import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; +import { log as loggerLog, setLogCapture } from "../src/logger.ts"; + +// #413: upstream truncation (200 + early SSE EOF) — zero-side-effect retry, +// well-formed Anthropic error stream (message_start before content blocks, +// all blocks closed), single-point logging. + +const ANTHROPIC_BODY = { model: "claude", messages: [], stream: true, max_tokens: 10 }; + +function makeCtx(id: string, logSink?: string[], wireThroughLogger = false): { + core: ReturnType; + config: Config; + messages: CoreMessage[]; + session: Session; + log: (m: string) => void; +} { + return { + core: createCore(), + config: { modelContextLimit: 200000 } as Config, + messages: [], + session: { + id, + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, contextTokens: 0 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + }, + log: (m: string) => { + logSink?.push(m); + if (wireThroughLogger) loggerLog("info", `[${id}] ${m}`); + }, + }; +} + +async function drain(stream: ReadableStream, ctx: ReturnType): Promise { + const adapter = createAnthropicAdapter(ANTHROPIC_BODY); + const chunks: Buffer[] = []; + for await (const chunk of runCompressLoop(stream, ctx, ANTHROPIC_BODY, { url: "http://mock", headers: {} }, adapter, buildCompressSystemPrompt())) { + chunks.push(chunk); + } + return Buffer.concat(chunks).toString("utf8"); +} + +function mockFetch(handler: (call: number) => Response): { calls: () => number; restore: () => void } { + let n = 0; + const orig = globalThis.fetch; + globalThis.fetch = (async () => { + n++; + return handler(n); + }) as typeof fetch; + return { calls: () => n, restore: () => { globalThis.fetch = orig; } }; +} + +const ev = (type: string, data: unknown): string => `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; + +const MESSAGE_START = ev("message_start", { + type: "message_start", + message: { + id: "msg_1", type: "message", role: "assistant", model: "claude", + content: [], stop_reason: null, stop_sequence: null, + usage: { input_tokens: 5, output_tokens: 0 }, + }, +}); +const textStart = (index: number) => ev("content_block_start", { type: "content_block_start", index, content_block: { type: "text", text: "" } }); +const textDelta = (index: number, text: string) => ev("content_block_delta", { type: "content_block_delta", index, delta: { type: "text_delta", text } }); +const textBlock = (index: number, text: string) => textStart(index) + textDelta(index, text) + blockStop(index); +const thinkingStart = (index: number) => ev("content_block_start", { type: "content_block_start", index, content_block: { type: "thinking", thinking: "" } }); +const thinkingDelta = (index: number, thinking: string) => ev("content_block_delta", { type: "content_block_delta", index, delta: { type: "thinking_delta", thinking } }); +const blockStop = (index: number) => ev("content_block_stop", { type: "content_block_stop", index }); +const toolUseBlock = (index: number, id: string, name: string, input: string) => + ev("content_block_start", { type: "content_block_start", index, content_block: { type: "tool_use", id, name, input: {} } }) + + ev("content_block_delta", { type: "content_block_delta", index, delta: { type: "input_json_delta", partial_json: input } }) + + blockStop(index); +const MESSAGE_DELTA = (stopReason = "end_turn") => ev("message_delta", { type: "message_delta", delta: { stop_reason: stopReason, stop_sequence: null }, usage: { input_tokens: 5, output_tokens: 3 } }); +const MESSAGE_STOP = ev("message_stop", { type: "message_stop" }); + +interface SseEvent { type: string; data: Record; } + +function parseSse(s: string): SseEvent[] { + const out: SseEvent[] = []; + for (const block of s.split("\n\n")) { + if (!block.trim()) continue; + let type = ""; + const dataLines: string[] = []; + for (const line of block.split("\n")) { + if (line.startsWith("event:")) type = line.slice(6).trim(); + else if (line.startsWith("data:")) dataLines.push(line.slice(5).replace(/^ /, "")); + } + if (!type) continue; + const jsonStr = dataLines.join("\n").trim(); + out.push({ type, data: jsonStr ? (JSON.parse(jsonStr) as Record) : {} }); + } + return out; +} + +// Anthropic stream protocol invariants: message_start first, every +// content_block_start matched by a stop before the terminal, message_stop last. +function assertAnthropicStreamSchema(s: string): void { + const events = parseSse(s); + assert.ok(events.length > 0, "stream non-empty"); + assert.equal(events[0].type, "message_start", `first event must be message_start (got ${events[0].type})`); + const open = new Map(); + let sawTerminal = false; + for (const e of events) { + if (e.type === "content_block_start") { + const idx = e.data.index as number; + const cb = (e.data.content_block ?? {}) as Record; + assert.ok(!open.has(idx), `block ${idx} started twice`); + open.set(idx, String(cb.type)); + } else if (e.type === "content_block_stop") { + const idx = e.data.index as number; + assert.ok(open.has(idx), `content_block_stop for never-started block ${idx}`); + open.delete(idx); + } else if (e.type === "message_delta" || e.type === "message_stop") { + assert.equal(open.size, 0, `all blocks closed before ${e.type} (still open: ${[...open.entries()].map(([i, t]) => `${i}:${t}`).join(", ")})`); + sawTerminal = true; + } + } + assert.ok(sawTerminal, "stream has message_delta/message_stop terminal"); + assert.equal(events[events.length - 1].type, "message_stop", "last event is message_stop"); +} + +test("#413 T1: 0-event EOF → retried once; failed retry → well-formed error stream, single log line", async () => { + process.env.BILI_REPLAY_RETRY_MAX = "1"; + const captured: { level: string; msg: string }[] = []; + setLogCapture((level, msg) => captured.push({ level, msg })); + const logSink: string[] = []; + const ctx = makeCtx("trunc-t1", logSink, true); + const mock = mockFetch(() => new Response('{"error":"boom"}', { status: 500, headers: { "content-type": "application/json" } })); + try { + const out = await drain(new Response("", { status: 200 }).body!, ctx); + assert.equal(mock.calls(), 1, "zero-side-effect truncation retried exactly once"); + assertAnthropicStreamSchema(out); + assert.ok(out.includes("upstream stream truncated"), "truncation surfaced to client"); + const truncCaptured = captured.filter((l) => l.msg.includes("upstream stream truncated")); + assert.equal(truncCaptured.length, 1, `truncation logged exactly once (got ${truncCaptured.length}): ${JSON.stringify(truncCaptured)}`); + assert.equal(truncCaptured[0].level, "info", "the single line is the ctx.log info line — no global error duplicate"); + assert.equal(logSink.filter((l) => l.includes("upstream stream truncated")).length, 1, "ctx.log called once for the truncation event"); + } finally { + mock.restore(); + setLogCapture(null); + delete process.env.BILI_REPLAY_RETRY_MAX; + } +}); + +test("#413 T2: 0-event EOF → retry succeeds → one clean stream, no error surfaced", async () => { + const round2 = [MESSAGE_START, textBlock(0, "Hello retry"), MESSAGE_DELTA(), MESSAGE_STOP].join(""); + const mock = mockFetch(() => new Response(round2, { status: 200 })); + const ctx = makeCtx("trunc-t2"); + try { + const out = await drain(new Response("", { status: 200 }).body!, ctx); + assert.equal(mock.calls(), 1, "retried once"); + assertAnthropicStreamSchema(out); + assert.ok(out.includes("Hello retry"), "retried round's content delivered"); + assert.ok(!out.includes("upstream stream truncated"), "no error surfaced on successful retry"); + } finally { + mock.restore(); + } +}); + +test("#413 T3: truncation with open thinking block → block closed, no retry (content already forwarded)", async () => { + const round1 = [MESSAGE_START, thinkingStart(0), thinkingDelta(0, "let me think")].join(""); + const mock = mockFetch(() => new Response("{}", { status: 200 })); + const ctx = makeCtx("trunc-t3"); + try { + const out = await drain(new Response(round1, { status: 200 }).body!, ctx); + assert.equal(mock.calls(), 0, "no retry when content already reached the client"); + assertAnthropicStreamSchema(out); + assert.ok(out.includes("upstream stream truncated"), "truncation surfaced to client"); + } finally { + mock.restore(); + } +}); + +test("#413 T4: truncation with open text block → block closed before error block", async () => { + const round1 = [MESSAGE_START, textStart(0), textDelta(0, "partial")].join(""); + const mock = mockFetch(() => new Response("{}", { status: 200 })); + const ctx = makeCtx("trunc-t4"); + try { + const out = await drain(new Response(round1, { status: 200 }).body!, ctx); + assert.equal(mock.calls(), 0, "no retry when content already reached the client"); + assertAnthropicStreamSchema(out); + assert.ok(out.includes("partial"), "already-forwarded text preserved"); + assert.ok(out.includes("upstream stream truncated"), "truncation surfaced to client"); + } finally { + mock.restore(); + } +}); + +test("#413 T5: retry itself truncated → no second retry (one per request)", async () => { + const mock = mockFetch(() => new Response("", { status: 200 })); + const ctx = makeCtx("trunc-t5"); + try { + const out = await drain(new Response("", { status: 200 }).body!, ctx); + assert.equal(mock.calls(), 1, "exactly one retry per request"); + assertAnthropicStreamSchema(out); + assert.ok(out.includes("upstream stream truncated"), "truncation surfaced after exhausted retry"); + } finally { + mock.restore(); + } +}); + +test("#413 T6: round-2 truncation (after proxy tool) retries with the round-2 body", async () => { + const round1 = [MESSAGE_START, toolUseBlock(0, "toolu_1", "acp_status", "{}"), MESSAGE_DELTA("tool_use"), MESSAGE_STOP].join(""); + const round2 = [MESSAGE_START, textBlock(0, "after retry"), MESSAGE_DELTA(), MESSAGE_STOP].join(""); + const mock = mockFetch((call) => + call === 1 ? new Response("", { status: 200 }) : new Response(round2, { status: 200 }), + ); + const ctx = makeCtx("trunc-t6"); + try { + const out = await drain(new Response(round1, { status: 200 }).body!, ctx); + assert.equal(mock.calls(), 2, "re-request + one truncation retry"); + assert.ok(out.includes("[ACP]"), "proxy tool marker surfaced to client"); + assert.ok(out.includes("after retry"), "retried round-2 content delivered"); + assertAnthropicStreamSchema(out); + } finally { + mock.restore(); + } +}); From 70850f35abb230dadee033fbd9626d3962a5f4c7 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Wed, 2 Sep 2026 09:44:51 +0800 Subject: [PATCH 05/22] fix: responses adapter synthesizes response.created before orphan response.failed (#440) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The loop-level responses adapter (src/loop/adapter-responses.ts) emitted a response.failed with no preceding response.created when the upstream stream truncated (200 + early EOF) before response.created reached the client. An orphan response.failed crashes strict clients (codex) — the same class as the anthropic message_start gap fixed in #413. Two emit paths were affected, not just emitError: - 0-event/partial EOF routes through emitCompletion's failed branch (the responses parseStream yields a fallback done:failed on truncation, unlike the anthropic adapter), so the common case never hit emitError. - emitError covers HTTP-error / empty-body paths. Both now track whether response.created was forwarded (createdForwarded, set in round 1) and synthesize one before emitting response.failed, reusing the forwarded created id when present so created/failed stay id-consistent. Tests: tests/loop-responses-orphan.test.ts (0-event EOF, truncation-after-created, emitError synthesis). All fail on the pre-fix adapter, pass after. --- src/loop/adapter-responses.ts | 38 ++++++++-- tests/loop-responses-orphan.test.ts | 113 ++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+), 6 deletions(-) create mode 100644 tests/loop-responses-orphan.test.ts diff --git a/src/loop/adapter-responses.ts b/src/loop/adapter-responses.ts index fad8fa2b4d..773ea68504 100644 --- a/src/loop/adapter-responses.ts +++ b/src/loop/adapter-responses.ts @@ -252,6 +252,19 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp let responseObj: Record | null = null; let terminalRaw: Buffer | null = null; let terminalKind: string | null = null; + // #440: response.failed with no preceding response.created is an orphan that + // crashes strict clients (codex) — same class as the anthropic gap (#413). + let createdForwarded = false; + let createdRespId: string | null = null; + const ensureCreated = (): Buffer | null => { + if (createdForwarded) return null; + createdForwarded = true; + if (!createdRespId) createdRespId = `resp-proxy-${Date.now()}`; + return Buffer.from( + `event: response.created\ndata: ${JSON.stringify({ type: "response.created", response: { id: createdRespId, status: "in_progress", output: [] } })}\n\n`, + "utf8", + ); + }; return { buildRequest(coreMessages, systemPrompt, requestBody) { @@ -318,6 +331,11 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp } if (type === "response.created" || type === "response.in_progress") { + if (type === "response.created") { + const resp = obj.response as Record | undefined; + if (resp && typeof resp.id === "string") createdRespId = resp.id; + if (round === 1) createdForwarded = true; + } yield { kind: "meta", chunk: rawBuf, firstRoundOnly: true } as ParsedStreamEvent; } else if (type === "response.output_item.added") { const item = obj.item as Record | undefined; @@ -484,15 +502,19 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp return terminalRaw; } if (!responseObj && opts?.finishReason === "failed") { + const parts: Buffer[] = []; + const created = ensureCreated(); + if (created) parts.push(created); const failed = { - id: `resp-error-${Date.now()}`, + id: createdRespId ?? `resp-error-${Date.now()}`, status: "failed", error: { code: "server_error", message: "upstream returned no response" }, }; - return Buffer.from( + parts.push(Buffer.from( `event: response.failed\ndata: ${JSON.stringify({ type: "response.failed", response: failed })}\n\n`, "utf8", - ); + )); + return Buffer.concat(parts); } let resp = responseObj ? ({ ...responseObj } as Record) @@ -517,15 +539,19 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp }, emitError(message) { + const parts: Buffer[] = []; + const created = ensureCreated(); + if (created) parts.push(created); const resp = { - id: `resp-error-${Date.now()}`, + id: createdRespId ?? `resp-error-${Date.now()}`, status: "failed", error: { code: "server_error", message }, }; - return Buffer.from( + parts.push(Buffer.from( `event: response.failed\ndata: ${JSON.stringify({ type: "response.failed", response: resp })}\n\n`, "utf8", - ); + )); + return Buffer.concat(parts); }, extractTextTriggers(text) { diff --git a/tests/loop-responses-orphan.test.ts b/tests/loop-responses-orphan.test.ts new file mode 100644 index 0000000000..1d0f58cc17 --- /dev/null +++ b/tests/loop-responses-orphan.test.ts @@ -0,0 +1,113 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { Config, CoreMessage } from "acp-kernel"; +import { createCore, createInitialState } from "acp-kernel"; +import type { Session } from "../src/session.ts"; +import { runCompressLoop, createResponsesAdapter } from "../src/loop/index.ts"; +import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; + +// #440: responses adapter must not emit response.failed with no preceding +// response.created (orphan stream crashes codex; same class as #413). + +const BODY = { model: "gpt-5", input: [], stream: true }; + +function sse(event: string, obj: Record): string { + return `event: ${event}\ndata: ${JSON.stringify({ type: event, ...obj })}\n\n`; +} + +function makeCtx(id: string) { + return { + core: createCore(), + config: { modelContextLimit: 200000 } as Config, + messages: [] as CoreMessage[], + session: { + id, + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, contextTokens: 0 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + } as unknown as Session, + log: () => {}, + protocol: "responses" as const, + }; +} + +async function drain(stream: ReadableStream, ctx: ReturnType): Promise { + const chunks: Buffer[] = []; + for await (const chunk of runCompressLoop( + stream, + ctx, + BODY, + { url: "http://mock", headers: {} }, + createResponsesAdapter(), + buildCompressSystemPrompt(), + )) { + chunks.push(chunk); + } + return Buffer.concat(chunks).toString("utf8"); +} + +interface OutEvent { event: string; data: Record } + +function parseOut(out: string): OutEvent[] { + const events: OutEvent[] = []; + for (const block of out.split("\n\n")) { + let event = ""; + const dataLines: string[] = []; + for (const line of block.split("\n")) { + if (line.startsWith("event:")) event = line.slice(6).trim(); + else if (line.startsWith("data:")) dataLines.push(line.slice(5).replace(/^ /, "")); + } + if (!event || dataLines.length === 0) continue; + try { + events.push({ event, data: JSON.parse(dataLines.join("\n")) as Record }); + } catch { } + } + return events; +} + +function assertCreatedBeforeFailed(events: OutEvent[]): void { + const failedIdx = events.findIndex((e) => e.event === "response.failed"); + assert.ok(failedIdx >= 0, "stream has a response.failed"); + const createdIdx = events.findIndex((e) => e.event === "response.created"); + assert.ok(createdIdx >= 0, "stream has a response.created"); + assert.ok(createdIdx < failedIdx, `response.created (idx ${createdIdx}) precedes response.failed (idx ${failedIdx})`); +} + +test("#440 T1: 0-event EOF → first event is response.created, failed preceded by created", async () => { + const ctx = makeCtx("resp-orphan-t1"); + const out = await drain(new Response("", { status: 200 }).body!, ctx); + const events = parseOut(out); + assert.ok(events.length > 0, "stream non-empty"); + assert.equal(events[0].event, "response.created", `first event must be response.created (got ${events[0].event})`); + assertCreatedBeforeFailed(events); +}); + +test("#440 T2: truncation after created forwarded → single created, failed references same id", async () => { + const round1 = sse("response.created", { response: { id: "resp_1", status: "in_progress", output: [] } }); + const ctx = makeCtx("resp-orphan-t2"); + const out = await drain(new Response(round1, { status: 200 }).body!, ctx); + const events = parseOut(out); + const created = events.filter((e) => e.event === "response.created"); + assert.equal(created.length, 1, `exactly one response.created (no synthetic duplicate); got ${created.length}`); + assertCreatedBeforeFailed(events); + const failed = events.find((e) => e.event === "response.failed")!; + const failedId = (failed.data.response as Record)?.id; + assert.equal(failedId, "resp_1", "failed references the forwarded created's id"); +}); + +test("#440 T3: emitError with no created forwarded → synthesizes created before failed", () => { + const adapter = createResponsesAdapter(); + const out = adapter.emitError("upstream stream truncated").toString("utf8"); + const events = parseOut(out); + assert.equal(events[0].event, "response.created", "emitError leads with a synthesized response.created"); + assertCreatedBeforeFailed(events); + const createdId = (events[0].data.response as Record)?.id; + const failedId = (events.find((e) => e.event === "response.failed")!.data.response as Record)?.id; + assert.equal(createdId, failedId, "synthesized created and failed share an id"); +}); From 037070dc4f6d5286210ea803284f21bc5751d4ea Mon Sep 17 00:00:00 2001 From: ework-agent Date: Wed, 2 Sep 2026 23:20:57 +0800 Subject: [PATCH 06/22] fix: bili pi launcher disables pi's native auto-compaction (#447) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `bili pi` launcher never disabled pi's own context compaction, so pi's native summarizer (shouldCompact, enabled by default) could fire ahead of / alongside bili's ACP compression and lossily rewrite the context bili is managing — losing the detailed history. The pi overlay (-bili) is now ALWAYS built when the real pi home exists and always carries a settings.json with compaction.enabled=false (merged over the user's real settings.json, which is never mutated). This makes bili's ACP compression the sole compression path under `bili pi`, mirroring how codex/claude already get their native auto-compact aligned. - refreshOverlayHome / mergeOverlayEntry now accept a SET of generated files (models.json + settings.json) so both stay out of the real-home symlink and merge-back sets. - models.json is still rewritten for HTTP/wrapped-HTTPS upstreams when present. - preparePiHttpRewrite returns undefined only for a missing real home. --- src/launcher.ts | 108 +++++++++++++++++++++++++---------------- tests/launcher.test.ts | 41 +++++++++++----- 2 files changed, 93 insertions(+), 56 deletions(-) diff --git a/src/launcher.ts b/src/launcher.ts index 9ecca8b132..f5f31da8b7 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -782,7 +782,10 @@ function mergeSqliteSet(overlay: string, realHome: string, base: string): boolea } } -function refreshOverlayHome(realHome: string, overlay: string, generatedFile: string): boolean { +function refreshOverlayHome(realHome: string, overlay: string, generatedFile: string | string[]): boolean { + const generatedFiles = new Set(Array.isArray(generatedFile) ? generatedFile : [generatedFile]); + const isGeneratedDraft = (name: string): boolean => + [...generatedFiles].some((g) => name.startsWith(`.${g}.`) && name.endsWith(".tmp")); try { fs.mkdirSync(overlay, { recursive: true }); } catch { @@ -814,7 +817,7 @@ function refreshOverlayHome(realHome: string, overlay: string, generatedFile: st // re-points the db; any other set moves wholesale. const dbSets: { base: string; keepSidecars: boolean }[] = []; for (const entry of overlayEntries) { - if (!entry.endsWith(".db") || entry === generatedFile) continue; + if (!entry.endsWith(".db") || generatedFiles.has(entry)) continue; const members = sqliteSetMembers(entry); if (!members.some((m) => m !== entry && overlayEntries.includes(m))) continue; let mainSt: fs.Stats | undefined; @@ -832,9 +835,9 @@ function refreshOverlayHome(realHome: string, overlay: string, generatedFile: st } } for (const entry of overlayEntries) { - if (entry === generatedFile) continue; + if (generatedFiles.has(entry)) continue; const overlayPath = path.join(overlay, entry); - if (entry.startsWith(`.${generatedFile}.`) && entry.endsWith(".tmp")) { + if (isGeneratedDraft(entry)) { try { fs.unlinkSync(overlayPath); } catch {} @@ -864,7 +867,7 @@ function refreshOverlayHome(realHome: string, overlay: string, generatedFile: st try { fs.unlinkSync(overlayPath); } catch {} - } else if (mergeOverlayEntry(overlayPath, realPath, generatedFile)) { + } else if (mergeOverlayEntry(overlayPath, realPath, generatedFiles)) { try { fs.rmSync(overlayPath, { recursive: true, force: true }); } catch {} @@ -886,7 +889,7 @@ function refreshOverlayHome(realHome: string, overlay: string, generatedFile: st let total = 0; const linkFailures: string[] = []; for (const entry of realEntries) { - if (entry === generatedFile) continue; + if (generatedFiles.has(entry)) continue; total += 1; const overlayPath = path.join(overlay, entry); let present = false; @@ -925,7 +928,7 @@ function refreshOverlayHome(realHome: string, overlay: string, generatedFile: st return true; } -function mergeOverlayEntry(src: string, dst: string, excludedName?: string): boolean { +function mergeOverlayEntry(src: string, dst: string, excludedNames?: ReadonlySet): boolean { let st: fs.Stats; try { st = fs.lstatSync(src); @@ -961,8 +964,8 @@ function mergeOverlayEntry(src: string, dst: string, excludedName?: string): boo // #410: generated configs must never merge back into the real // home, at ANY depth — a nested promote bakes proxy URLs into // the user's real config. - if (excludedName !== undefined && entry === excludedName) continue; - if (!mergeOverlayEntry(path.join(src, entry), path.join(dst, entry), excludedName)) ok = false; + if (excludedNames?.has(entry)) continue; + if (!mergeOverlayEntry(path.join(src, entry), path.join(dst, entry), excludedNames)) ok = false; } return ok; } @@ -1038,50 +1041,69 @@ function writeOverlayFileAtomic(overlay: string, fileName: string, contents: str } } +function writePiCompactionDisabledSettings(piHome: string, overlay: string): void { + let settings: Record = {}; + try { + const parsed: unknown = JSON.parse(fs.readFileSync(path.join(piHome, "settings.json"), "utf8")); + if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) { + settings = parsed as Record; + } + } catch {} + const compactionRaw = settings.compaction; + const compaction = + compactionRaw && typeof compactionRaw === "object" && !Array.isArray(compactionRaw) + ? { ...(compactionRaw as Record) } + : {}; + compaction.enabled = false; + settings.compaction = compaction; + writeOverlayFileAtomic(overlay, "settings.json", JSON.stringify(settings, null, 2)); +} + export function preparePiHttpRewrite( piHome: string, origin: string, httpRewrites: HttpRewrite[], httpsRewrites: HttpRewrite[], ): string | undefined { - if (httpRewrites.length === 0 && httpsRewrites.length === 0) return undefined; - const modelsPath = path.join(piHome, "models.json"); - let txt: string; - try { - txt = fs.readFileSync(modelsPath, "utf8"); - } catch { - return undefined; - } - let parsed: unknown; + if (!fs.existsSync(piHome)) return undefined; + const overlay = `${piHome}-bili`; + // #447: the overlay always carries a settings.json disabling pi's native + // auto-compaction (its summarizer must never fire alongside bili's ACP + // compression); models.json is generated only when present, so both stay + // out of the real-home symlink set. + const generated: string[] = ["settings.json"]; + let modelsRoot: Record | undefined; try { - parsed = JSON.parse(txt); - } catch { - return undefined; - } - if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return undefined; - const root = parsed as Record; - const providersVal = root.providers; - if (providersVal && typeof providersVal === "object" && !Array.isArray(providersVal)) { - const providers = providersVal as Record; - for (const r of httpRewrites) { - const prov = providers[r.key]; - if (prov && typeof prov === "object" && !Array.isArray(prov)) { - const p = prov as { baseUrl?: unknown }; - const existing = typeof p.baseUrl === "string" ? p.baseUrl : r.realUpstream; - p.baseUrl = wrapUpstream(origin, unwrapUpstream(existing)); - } + const parsed: unknown = JSON.parse(fs.readFileSync(path.join(piHome, "models.json"), "utf8")); + if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) { + modelsRoot = parsed as Record; + generated.push("models.json"); } - for (const r of httpsRewrites) { - const prov = providers[r.key]; - if (prov && typeof prov === "object" && !Array.isArray(prov)) { - const p = prov as { baseUrl?: unknown }; - p.baseUrl = r.realUpstream; + } catch {} + if (!refreshOverlayHome(piHome, overlay, generated)) return undefined; + if (modelsRoot) { + const providersVal = modelsRoot.providers; + if (providersVal && typeof providersVal === "object" && !Array.isArray(providersVal)) { + const providers = providersVal as Record; + for (const r of httpRewrites) { + const prov = providers[r.key]; + if (prov && typeof prov === "object" && !Array.isArray(prov)) { + const p = prov as { baseUrl?: unknown }; + const existing = typeof p.baseUrl === "string" ? p.baseUrl : r.realUpstream; + p.baseUrl = wrapUpstream(origin, unwrapUpstream(existing)); + } + } + for (const r of httpsRewrites) { + const prov = providers[r.key]; + if (prov && typeof prov === "object" && !Array.isArray(prov)) { + const p = prov as { baseUrl?: unknown }; + p.baseUrl = r.realUpstream; + } } } + writeOverlayFileAtomic(overlay, "models.json", JSON.stringify(modelsRoot)); } - const overlay = `${piHome}-bili`; - if (!refreshOverlayHome(piHome, overlay, "models.json")) return undefined; - writeOverlayFileAtomic(overlay, "models.json", JSON.stringify(root)); + writePiCompactionDisabledSettings(piHome, overlay); return overlay; } @@ -1842,7 +1864,7 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} // Native tooling out of the box: when the user has NOT installed the // plugin, ride pi's `-e ` (loads for this run only, writes // nothing) instead of leaving them on wire-mode fallback. When they - // HAVE installed it, settings.json (symlinked into the overlay home) + // HAVE installed it, settings.json (merged into the overlay home) // already loads it — adding `-e` too would double-register. const piExt = selfDistFile("agent/pi.js"); if (piExt && fs.existsSync(piExt) && !piPluginInstalled(resolvePiHome(process.env))) { diff --git a/tests/launcher.test.ts b/tests/launcher.test.ts index 2a4ac54032..a62f60584c 100644 --- a/tests/launcher.test.ts +++ b/tests/launcher.test.ts @@ -938,36 +938,51 @@ test("preparePiHttpRewrite: rewrites matching provider, leaves others, symlinks assert.equal(out.providers.b.baseUrl, "https://secure.example.com"); assert.equal(fs.lstatSync(path.join(tmp!, "auth.json")).isSymbolicLink(), true); assert.equal(fs.realpathSync(path.join(tmp!, "auth.json")), path.join(home, "auth.json")); + const settings = JSON.parse(fs.readFileSync(path.join(tmp!, "settings.json"), "utf8")); + assert.equal(settings.compaction.enabled, false, "pi native compaction disabled"); } finally { if (tmp) fs.rmSync(tmp, { recursive: true, force: true }); fs.rmSync(home, { recursive: true, force: true }); } }); -test("preparePiHttpRewrite: returns undefined when no rewrites", () => { - assert.equal(preparePiHttpRewrite("/whatever", "http://127.0.0.1:8787", [], []), undefined); +test("preparePiHttpRewrite: non-existent home → undefined", () => { + assert.equal(preparePiHttpRewrite("/nonexistent-bili-pi-home-xyz", "http://127.0.0.1:8787", [], []), undefined); }); -test("preparePiHttpRewrite: returns undefined when models.json missing or unparseable", () => { - const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-pihome-")); - const rw: HttpRewrite[] = [{ key: "a", realUpstream: "http://x/v1" }]; +test("preparePiHttpRewrite: no rewrites → overlay built, pi native compaction disabled, models.json copied verbatim", () => { + const home = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "bili-pihome-"))); + fs.writeFileSync(path.join(home, "models.json"), JSON.stringify({ providers: { a: { baseUrl: "https://keep.example.com" } } })); try { - assert.equal(preparePiHttpRewrite(home, "http://127.0.0.1:8787", rw, []), undefined); - fs.writeFileSync(path.join(home, "models.json"), "not-json{"); - assert.equal(preparePiHttpRewrite(home, "http://127.0.0.1:8787", rw, []), undefined); + const overlay = preparePiHttpRewrite(home, "http://127.0.0.1:8787", [], []); + assert.ok(typeof overlay === "string" && overlay.length > 0); + const settings = JSON.parse(fs.readFileSync(path.join(overlay!, "settings.json"), "utf8")); + assert.equal(settings.compaction.enabled, false, "pi native compaction disabled"); + const out = JSON.parse(fs.readFileSync(path.join(overlay!, "models.json"), "utf8")); + assert.equal(out.providers.a.baseUrl, "https://keep.example.com", "no rewrite → baseUrl untouched"); } finally { fs.rmSync(home, { recursive: true, force: true }); + fs.rmSync(`${home}-bili`, { recursive: true, force: true }); } }); -test("preparePiHttpRewrite: returns undefined when models.json is not an object", () => { - const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-pihome-")); - const rw: HttpRewrite[] = [{ key: "a", realUpstream: "http://x/v1" }]; +test("preparePiHttpRewrite: preserves other settings.json keys, disables only compaction", () => { + const home = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "bili-pihome-"))); + fs.writeFileSync( + path.join(home, "settings.json"), + JSON.stringify({ packages: ["/opt/pi-plugin"], theme: "dark", compaction: { reserveTokens: 9999 } }), + ); try { - fs.writeFileSync(path.join(home, "models.json"), JSON.stringify([1, 2, 3])); - assert.equal(preparePiHttpRewrite(home, "http://127.0.0.1:8787", rw, []), undefined); + const overlay = preparePiHttpRewrite(home, "http://127.0.0.1:8787", [], []); + assert.ok(typeof overlay === "string"); + const settings = JSON.parse(fs.readFileSync(path.join(overlay!, "settings.json"), "utf8")); + assert.equal(settings.compaction.enabled, false, "compaction disabled"); + assert.equal(settings.compaction.reserveTokens, 9999, "other compaction keys preserved"); + assert.deepEqual(settings.packages, ["/opt/pi-plugin"], "packages preserved"); + assert.equal(settings.theme, "dark", "theme preserved"); } finally { fs.rmSync(home, { recursive: true, force: true }); + fs.rmSync(`${home}-bili`, { recursive: true, force: true }); } }); From 3175990049d9e52ec8b3bd84e40a7f5d47883e01 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Thu, 3 Sep 2026 00:43:50 +0800 Subject: [PATCH 07/22] fix: keep sibling text when a mixed chunk's other field is a pure tag echo (#462) plugin-mode openai-chat SSE strip (pipePluginChatWithStrip/processOpenai) latched droppedText per-field, so a chunk whose delta carried e.g. content='real answer' plus a pure render-tag echo in reasoning_content was dropped whole, losing the real content. Now the chunk is dropped only when EVERY managed text field strips to empty (keptText guard); the existing non-text payload check (finish_reason/tool_calls) is unchanged. --- src/plugin.ts | 15 +++++++--- .../plugin-passthrough-tag-strip-chat.test.ts | 29 +++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/plugin.ts b/src/plugin.ts index ad3c1f0fbb..7089eaedb8 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -604,6 +604,7 @@ export async function pipePluginChatWithStrip( } let rebuilt: Record | null = null; let droppedText = false; + let keptText = false; let hadText = false; for (let ci = 0; ci < choices.length; ci++) { const ch = choices[ci] as Record | null; @@ -614,10 +615,14 @@ export async function pipePluginChatWithStrip( const v = dd[field]; if (typeof v !== "string") continue; hadText = true; - if (!containsRenderTagText(v) && !anyPending()) continue; + if (!containsRenderTagText(v) && !anyPending()) { + if (v.length > 0) keptText = true; + continue; + } const index = typeof ch?.["index"] === "number" ? ch["index"] : ci; const [clean, changed] = pushField(field, index, v); if (clean.length === 0) droppedText = true; + else keptText = true; if (changed) { if (!rebuilt) { rebuilt = { ...ev, choices: choices.map((c) => ({ ...(c as Record), delta: { ...((c as Record)["delta"] as Record) } })) }; @@ -627,9 +632,11 @@ export async function pipePluginChatWithStrip( } } if (rebuilt) { - // Delta carried nothing but the (now-stripped) text: drop the whole - // chunk instead of forwarding an empty content delta. - if (droppedText && !hadTextOtherThanTextFields(rebuilt["choices"])) { + // Delta carried no visible text after stripping: drop the whole + // chunk instead of forwarding an empty content delta. Only when + // EVERY managed text field emptied out — a sibling field with real + // content must survive (#462). + if (droppedText && !keptText && !hadTextOtherThanTextFields(rebuilt["choices"])) { return ""; } return rebuildEvent(rawEvent, rebuilt); diff --git a/tests/plugin-passthrough-tag-strip-chat.test.ts b/tests/plugin-passthrough-tag-strip-chat.test.ts index 11b1e994b3..577ebf49df 100644 --- a/tests/plugin-passthrough-tag-strip-chat.test.ts +++ b/tests/plugin-passthrough-tag-strip-chat.test.ts @@ -130,6 +130,35 @@ test("plugin chat passthrough drops a chunk whose delta stripped to empty", asyn } }); +test("plugin chat passthrough keeps sibling text when one field is a pure tag echo (#462)", async () => { + const out: string[] = []; + const res = makeRes(out); + const session = makeSession(); + const events = [ + chatChunk({ content: "real answer", reasoning_content: `${TAG_OPEN}m00044${TAG_CLOSE}` }), + DONE, + ]; + await pipePluginChatWithStrip(streamOf(events), res, session, "openai"); + const text = out.join(""); + assert.ok(!text.includes("m00044"), "echo-only sibling field stripped"); + assert.ok(text.includes("real answer"), "sibling field with real content must survive"); +}); + +test("plugin chat passthrough still drops a chunk where every managed field is a pure tag echo (#462)", async () => { + const out: string[] = []; + const res = makeRes(out); + const session = makeSession(); + const events = [ + chatChunk({ content: `${TAG_OPEN}m1${TAG_CLOSE}`, reasoning_content: `${TAG_OPEN}m2${TAG_CLOSE}` }), + DONE, + ]; + await pipePluginChatWithStrip(streamOf(events), res, session, "openai"); + const text = out.join(""); + assert.ok(!text.includes("m1") && !text.includes("m2"), "echoes stripped"); + const dataLines = text.split("\n").filter((l) => l.startsWith("data:")).filter((l) => !l.includes("[DONE]")); + assert.equal(dataLines.length, 0, "all-echo chunk dropped"); +}); + test("plugin chat passthrough keeps tool_calls deltas byte-identical", async () => { const out: string[] = []; const res = makeRes(out); From cf959cb0fbb3fef3235bff5b6e544fb763562d36 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Thu, 3 Sep 2026 00:48:46 +0800 Subject: [PATCH 08/22] fix(#460): strip render-tag echoes from non-injected proxy-mode chat SSE Requests that skip compress injection (OpenAI title-gen exclusion, ACP_NO_INJECT_TOOL, #353 stop_sequences classifier bypass) piped upstream SSE straight through to the client. A model echoing history-borne ACP render tags into prose leaked them to the client TUI, where they re-enter the conversation via replay and amplify (same mechanism as #457/#459). Route those non-injected streaming responses through the same tag-strip pipes as plugin mode (#452), with no session passed: usage accounting must stay off here, or a title-gen call's tiny input_tokens would clobber lastInputTokens and break compression triggering for the main conversation. - pipePluginChatWithStrip: session now optional (signature reordered protocol-before-session); usage sample applied only when a session is present - pipePluginResponsesWithStrip: session now optional, same guard - server.ts !useRewriter branch: strip pipe for streaming requests whose upstream response is text/event-stream; everything else stays raw pipeThrough as before - tests/proxy-noinject-tag-strip.test.ts: anthropic injectTool:false echo split across deltas stripped; tag-free stream byte-identical; openai title-gen stream stripped with [DONE]/finish_reason intact --- src/plugin.ts | 21 +- src/server.ts | 30 ++- .../plugin-passthrough-tag-strip-chat.test.ts | 16 +- tests/proxy-noinject-tag-strip.test.ts | 198 ++++++++++++++++++ 4 files changed, 245 insertions(+), 20 deletions(-) create mode 100644 tests/proxy-noinject-tag-strip.test.ts diff --git a/src/plugin.ts b/src/plugin.ts index ad3c1f0fbb..c5b4fffe63 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -536,12 +536,18 @@ function mergeUsageSample(acc: UsageSample, sample: UsageSample): void { * model prose through the tag-echo state machine — #206 parity with * pipePluginResponsesWithStrip. The verbatim variant let a model-emitted * render tag echo land in the agent's replayed history and amplify into the - * "endless blank output" loop observed with pi + qwen (issue #14). */ + * "endless blank output" loop observed with pi + qwen (issue #14). + * + * Also serves proxy-mode chat SSE that skipped compress injection (#460: + * title-gen exclusion / ACP_NO_INJECT_TOOL / classifier bypass). Pass no + * session there — usage accounting must be skipped or a title-gen call's + * tiny input_tokens would clobber lastInputTokens and break compression + * triggering for the main conversation. */ export async function pipePluginChatWithStrip( stream: ReadableStream, res: import("node:http").ServerResponse, - session: Session, protocol: WireProtocol, + session?: Session, log?: (msg: string) => void, ): Promise { const reader = stream.getReader(); @@ -691,7 +697,7 @@ export async function pipePluginChatWithStrip( } const rest = flushTails(); if (rest.length > 0 && !res.destroyed && !res.writableEnded) await write(rest); - if (acc.inputTokens !== undefined || acc.outputTokens !== undefined || acc.cachedTokens !== undefined) { + if (session && (acc.inputTokens !== undefined || acc.outputTokens !== undefined || acc.cachedTokens !== undefined)) { applyUsageSample(session, acc, protocol); markDirty(session); } @@ -727,11 +733,14 @@ function hadTextOtherThanTextFields(choices: unknown): boolean { * tag-echo state machine the compress loop uses. A held tail is flushed as a * final delta before the first done/completed event so the client's assembled * text never loses content. - */ + * + * Also serves proxy-mode Responses SSE that skipped compress injection + * (#460, e.g. ACP_NO_INJECT_TOOL). Pass no session there — see + * pipePluginChatWithStrip for why usage accounting must be skipped. */ export async function pipePluginResponsesWithStrip( stream: ReadableStream, res: import("node:http").ServerResponse, - session: Session, + session?: Session, log?: (msg: string) => void, ): Promise { const reader = stream.getReader(); @@ -827,7 +836,7 @@ export async function pipePluginResponsesWithStrip( const rest = flushTail(""); if (rest.length > 0) await write(rest); } - if (acc.inputTokens !== undefined || acc.outputTokens !== undefined || acc.cachedTokens !== undefined) { + if (session && (acc.inputTokens !== undefined || acc.outputTokens !== undefined || acc.cachedTokens !== undefined)) { applyUsageSample(session, acc, "responses"); markDirty(session); } diff --git a/src/server.ts b/src/server.ts index afedc02d78..fb2f49bcd8 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2561,7 +2561,7 @@ async function forward( if (prepared.protocol === "responses") { await pipePluginResponsesWithStrip(upstream.body as ReadableStream, res, prepared.session, (msg) => log("info", `[${prepared.session.id}] ${msg}`)); } else { - await pipePluginChatWithStrip(upstream.body as ReadableStream, res, prepared.session, prepared.protocol, (msg) => log("info", `[${prepared.session.id}] ${msg}`)); + await pipePluginChatWithStrip(upstream.body as ReadableStream, res, prepared.protocol, prepared.session, (msg) => log("info", `[${prepared.session.id}] ${msg}`)); } } else { await pipePluginJson(upstream.body as ReadableStream, res, prepared.session, prepared.protocol); @@ -2570,11 +2570,11 @@ async function forward( return; } // We only rewrite when THIS request actually had the compress tool - // injected (per-request). For the OpenAI title-gen path - // (`compressInjected === false`) we must NOT route the stream into a - // rewriter — `rewriteSseStream` below is the *Anthropic* SSE rewriter - // and would mishandle OpenAI `choices[].delta` events. Plain passthrough - // is correct there. + // injected (per-request). Non-injected requests (OpenAI title-gen + // exclusion, ACP_NO_INJECT_TOOL, auto-mode classifier bypass) must NOT + // enter the compress loop — but their chat SSE still gets render-tag + // echo stripping (#460) below, so history-borne tags echoed in model + // prose cannot leak to the client and amplify via its replay. const useRewriter = prepared !== null && prepared.compressInjected && @@ -2592,6 +2592,24 @@ async function forward( } else { log("warn", `[${prepared.session.id}] native compact response terminal=${terminal}; rebase NOT scheduled`); } + } else if ( + prepared && + prepared.stream && + (upstream.headers.get("content-type") ?? "").includes("text/event-stream") + ) { + // #460: same strip pipes as plugin mode; byte-identical for + // tag-free streams. No session is passed: usage accounting must + // stay off here, or a title-gen call's tiny input_tokens would + // clobber lastInputTokens and break compression triggering for + // the main conversation (see pipePluginChatWithStrip docs). + const p = prepared; + const tagLog = (msg: string) => log("info", `[${p.session.id}] ${msg}`); + if (p.protocol === "responses") { + await pipePluginResponsesWithStrip(upstream.body as ReadableStream, res, undefined, tagLog); + } else { + await pipePluginChatWithStrip(upstream.body as ReadableStream, res, p.protocol, undefined, tagLog); + } + clearUpstreamTimer(); } else { await pipeThrough(upstream.body, res); clearUpstreamTimer(); diff --git a/tests/plugin-passthrough-tag-strip-chat.test.ts b/tests/plugin-passthrough-tag-strip-chat.test.ts index 11b1e994b3..d0b64b81fc 100644 --- a/tests/plugin-passthrough-tag-strip-chat.test.ts +++ b/tests/plugin-passthrough-tag-strip-chat.test.ts @@ -68,7 +68,7 @@ test("plugin chat passthrough strips render tags from delta.content", async () = chatChunk({}, { choices: [{ index: 0, delta: {}, finish_reason: "stop" }], usage: { prompt_tokens: 100, completion_tokens: 5 } }), DONE, ]; - await pipePluginChatWithStrip(streamOf(events), res, session, "openai"); + await pipePluginChatWithStrip(streamOf(events), res, "openai", session); const text = out.join(""); assert.ok(!text.includes("m00042"), "render tag content must not pass through"); assert.ok(text.includes("leading trailing"), "non-tag prose survives"); @@ -85,7 +85,7 @@ test("plugin chat passthrough strips tags split across deltas and flushes tail b chatChunk({ content: `0042${TAG_CLOSE} tail` }), DONE, ]; - await pipePluginChatWithStrip(streamOf(events), res, session, "openai"); + await pipePluginChatWithStrip(streamOf(events), res, "openai", session); const text = out.join(""); assert.ok(!text.includes("m00042"), "split tag fully swallowed"); assert.ok(text.includes("ok "), "first delta prose passes"); @@ -104,7 +104,7 @@ test("plugin chat passthrough strips echo from reasoning_content too", async () chatChunk({ reasoning_content: `think ${TAG_OPEN}m00007${TAG_CLOSE} done` }), DONE, ]; - await pipePluginChatWithStrip(streamOf(events), res, session, "openai"); + await pipePluginChatWithStrip(streamOf(events), res, "openai", session); const text = out.join(""); assert.ok(!text.includes("m00007"), "reasoning echo stripped"); assert.ok(text.includes("think done"), "reasoning prose survives"); @@ -120,7 +120,7 @@ test("plugin chat passthrough drops a chunk whose delta stripped to empty", asyn chatChunk({ content: "more" }), DONE, ]; - await pipePluginChatWithStrip(streamOf(events), res, session, "openai"); + await pipePluginChatWithStrip(streamOf(events), res, "openai", session); const text = out.join(""); assert.ok(!text.includes("m1") && !text.includes("m2"), "echo-only chunk stripped"); const dataLines = text.split("\n").filter((l) => l.startsWith("data:")).filter((l) => !l.includes("[DONE]")); @@ -137,7 +137,7 @@ test("plugin chat passthrough keeps tool_calls deltas byte-identical", async () const toolChunk = chatChunk({ tool_calls: [{ index: 0, id: "call_1", type: "function", function: { name: "compress", arguments: "{\"content\":[]}" } }] }); const plain = chatChunk({ content: "plain text no tags" }); const events = [plain, toolChunk, chatChunk({}, { choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] }), DONE]; - await pipePluginChatWithStrip(streamOf(events), res, session, "openai"); + await pipePluginChatWithStrip(streamOf(events), res, "openai", session); const text = out.join(""); assert.ok(text.includes(toolChunk.trim()), "tool_calls delta untouched"); assert.ok(text.includes(plain.trim()), "clean delta untouched"); @@ -148,7 +148,7 @@ test("plugin chat passthrough resolves held tail at stream end without [DONE]", const res = makeRes(out); const session = makeSession(); const events = [chatChunk({ content: `prose ${TAG_OPEN}m0` })]; - await pipePluginChatWithStrip(streamOf(events), res, session, "openai"); + await pipePluginChatWithStrip(streamOf(events), res, "openai", session); const text = out.join(""); assert.ok(text.includes("prose "), "clean prefix passes"); assert.ok(!text.includes("m0"), "unclosed tag content dropped at stream end"); @@ -165,7 +165,7 @@ test("plugin anthropic passthrough strips render tags from text_delta and thinki `event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 3 } })}\n\n`, `event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`, ]; - await pipePluginChatWithStrip(streamOf(events), res, session, "anthropic"); + await pipePluginChatWithStrip(streamOf(events), res, "anthropic", session); const text = out.join(""); assert.ok(!text.includes("m00009") && !text.includes("m00010"), "render tags stripped from both delta types"); assert.ok(text.includes("hi bye"), "text prose survives"); @@ -182,7 +182,7 @@ test("plugin anthropic passthrough strips tags split across deltas and flushes b `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: `0042${TAG_CLOSE} tail` } })}\n\n`, `event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn" } })}\n\n`, ]; - await pipePluginChatWithStrip(streamOf(events), res, session, "anthropic"); + await pipePluginChatWithStrip(streamOf(events), res, "anthropic", session); const text = out.join(""); assert.ok(!text.includes("m00042"), "split tag swallowed"); assert.ok(text.includes("tail"), "tail flushed before message_delta"); diff --git a/tests/proxy-noinject-tag-strip.test.ts b/tests/proxy-noinject-tag-strip.test.ts new file mode 100644 index 0000000000..057ccd2ae5 --- /dev/null +++ b/tests/proxy-noinject-tag-strip.test.ts @@ -0,0 +1,198 @@ +import assert from "node:assert"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { defaultConfig } from "acp-kernel"; +import { startServer, type ProxyOptions } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; + +// Render-tag pieces are assembled from hex escapes so no literal tag sequence +// appears in this file's source. +const LT = "\x3c"; +const GT = "\x3e"; +const TAG = (id: string, tokens = 177) => `${LT}acp tokens="${tokens}" type="text"${GT}${id}${LT}/acp${GT}`; +const OPEN_MARK = `${LT}acp `; +const CLOSE_MARK = `${LT}/acp${GT}`; + +interface Harness { + proxyPort: number; + upstreamPort: number; + close(): Promise; +} + +function anthropicSse(event: string, data: unknown): string { + return `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`; +} + +function openaiChunk(data: unknown): string { + return `data: ${JSON.stringify(data)}\n\n`; +} + +async function startHarness(compress: { injectTool: boolean; injectNudge: boolean }, scripts: string[][]): Promise { + let call = 0; + const upstream = http.createServer((req, res) => { + req.on("data", () => {}); + req.on("end", () => { + res.writeHead(200, { "content-type": "text/event-stream" }); + const script = scripts[Math.min(call, scripts.length - 1)]!; + call += 1; + for (const line of script) res.write(line); + res.end(); + }); + }); + upstream.listen(0, "127.0.0.1"); + return (async () => { + await once(upstream, "listening"); + const upstreamPort = upstream.address().port; + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "claude-test": { context: 400_000 }, "gpt-test": { context: 400_000 } } } }, + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const proxyPort = proxy.address().port; + return { + proxyPort, + upstreamPort, + close: async () => { + proxy.close(); + await once(proxy, "close"); + upstream.close(); + await once(upstream, "close"); + }, + }; + })(); +} + +function tagEchoAnthropicScript(): string[] { + const full = `好的,总结如下 ${TAG("m00155")}${TAG("m00155", 44)}${TAG("m00156", 33)}另外 5 « 6 成立${TAG("m00157")}完毕`; + const splitAt = [7, 20, 26, 41, 48, 60]; + const parts: string[] = []; + let prev = 0; + for (const s of splitAt) { + parts.push(full.slice(prev, s)); + prev = s; + } + parts.push(full.slice(prev)); + return [ + anthropicSse("message_start", { type: "message_start", message: { id: "msg_ninj_1", role: "assistant", usage: { input_tokens: 20 } } }), + anthropicSse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }), + ...parts.map((p) => anthropicSse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: p } })), + anthropicSse("content_block_stop", { type: "content_block_stop", index: 0 }), + anthropicSse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 9 } }), + anthropicSse("message_stop", { type: "message_stop" }), + ]; +} + +function tagEchoOpenAiScript(): string[] { + const full = `title: ${TAG("m00009", 12)}summary ok`; + const parts = [full.slice(0, 6), full.slice(6, 22), full.slice(22)]; + const base = { id: "chatcmpl_ninj", object: "chat.completion.chunk", created: 1, model: "gpt-test" }; + return [ + openaiChunk({ ...base, choices: [{ index: 0, delta: { role: "assistant", content: "" } }] }), + ...parts.map((p) => openaiChunk({ ...base, choices: [{ index: 0, delta: { content: p } }] })), + openaiChunk({ ...base, choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }), + "data: [DONE]\n\n", + ]; +} + +function cleanAnthropicText(raw: string): string { + return [...raw.matchAll(/"type":"text_delta","text":"((?:[^"\\]|\\.)*)"/g)] + .map((m) => JSON.parse(`"${m[1]}"`) as string) + .join(""); +} + +function cleanOpenAiContent(raw: string): string { + let out = ""; + for (const line of raw.split("\n")) { + if (!line.startsWith("data:") || line.slice(5).trim() === "[DONE]") continue; + try { + const obj = JSON.parse(line.slice(5).trim()) as { choices?: Array<{ delta?: { content?: string } }> }; + out += obj.choices?.[0]?.delta?.content ?? ""; + } catch {} + } + return out; +} + +test("#460: non-injected anthropic SSE — echoed render tags stripped, prose intact", async () => { + const h = await startHarness({ injectTool: false, injectNudge: false }, [tagEchoAnthropicScript()]); + try { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/messages`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "noinject-anth" }, + body: JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: true, system: "You are a test assistant.", messages: [{ role: "user", content: "hello" }] }), + }); + assert.equal(resp.status, 200); + let raw = ""; + for await (const chunk of resp.body) raw += Buffer.from(chunk).toString("utf8"); + assert.equal(raw.includes(OPEN_MARK), false, "client stream leaked a render open tag"); + assert.equal(raw.includes(CLOSE_MARK), false, "client stream leaked a render close tag"); + assert.ok(cleanAnthropicText(raw).endsWith("好的,总结如下 另外 5 « 6 成立完毕"), `unexpected client text: ${JSON.stringify(cleanAnthropicText(raw))}`); + assert.ok(raw.includes("message_stop"), "terminal event missing"); + } finally { + await h.close(); + } +}); + +test("#460: non-injected anthropic SSE without tags — byte-identical passthrough", async () => { + const script = [ + anthropicSse("message_start", { type: "message_start", message: { id: "msg_ninj_2", role: "assistant", usage: { input_tokens: 20 } } }), + anthropicSse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }), + anthropicSse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "plain " } }), + anthropicSse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "prose" } }), + anthropicSse("content_block_stop", { type: "content_block_stop", index: 0 }), + anthropicSse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 2 } }), + anthropicSse("message_stop", { type: "message_stop" }), + ]; + const h = await startHarness({ injectTool: false, injectNudge: false }, [script]); + try { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/messages`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "noinject-anth-clean" }, + body: JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: true, system: "s", messages: [{ role: "user", content: "hello" }] }), + }); + assert.equal(resp.status, 200); + let raw = ""; + for await (const chunk of resp.body) raw += Buffer.from(chunk).toString("utf8"); + assert.equal(raw, script.join(""), "tag-free stream must pass through byte-identical"); + } finally { + await h.close(); + } +}); + +test("#460: openai title-gen (max_tokens<=200) SSE — echoed render tags stripped, [DONE] intact", async () => { + const h = await startHarness({ injectTool: true, injectNudge: true }, [tagEchoOpenAiScript()]); + try { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "noinject-openai-title" }, + body: JSON.stringify({ model: "gpt-test", max_tokens: 100, stream: true, messages: [{ role: "user", content: "hello" }] }), + }); + assert.equal(resp.status, 200); + let raw = ""; + for await (const chunk of resp.body) raw += Buffer.from(chunk).toString("utf8"); + assert.equal(raw.includes(OPEN_MARK), false, "client stream leaked a render open tag"); + assert.equal(raw.includes(CLOSE_MARK), false, "client stream leaked a render close tag"); + assert.equal(cleanOpenAiContent(raw), "title: summary ok", `unexpected content: ${JSON.stringify(cleanOpenAiContent(raw))}`); + assert.ok(raw.includes("[DONE]"), "[DONE] missing"); + assert.ok(raw.includes('"finish_reason":"stop"'), "finish_reason chunk missing"); + } finally { + await h.close(); + } +}); From 55a1232026dc0d83a96920e0c42b732f33139a23 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Thu, 3 Sep 2026 01:10:34 +0800 Subject: [PATCH 09/22] fix(#453): hard backstop for plugin-mode context overflow Plugin mode trusted the ACP agent to run compress; when a weak model ignored the nudge the payload grew past the window and the upstream 400'd on input+output (vLLM rejects an oversized total instead of clamping). Proxy mode had preflight as a backstop, but its trigger estimate (estimateCoreMessages) ignores system+tools and can lag by a turn, so it missed the overflow. Two host-side fixes, both modes: - Clamp the forwarded output budget (max_tokens / max_completion_tokens / max_output_tokens) to window - estimated_input - margin so input+output can never exceed the window on request-rebuilding upstreams. Anthropic is exempt (its input limit is independent of max_tokens). estimateInputTokens takes the max of the real previous-turn input and a fresh count that includes system + tool definitions, closing the undershoot that let preflight skip. - Escalate the nudge: force-inject every turn once usage >= EMERGENCY_NUDGE_ ESCALATION_PCT (0.70) even when the kernel's cadence/growth gate stays silent. The kernel already auto-injects above nudge.maxContextLimitPct (default 0.75), so the line sits just under it to fill the pre-limit silent climb. diagNudge now labels forced injections INJECT-ESC instead of logging "idle". tests/issue453-backstop.test.ts: unit coverage of all three helpers plus two end-to-end proxy tests (clamp reduction mirroring the repro's undershoot; escalation-vs-control nudge contrast). typecheck/test/build green; e2e preflight green (full e2e needs the Responses upstream, runs in CI). --- src/server.ts | 85 ++++++++++++- tests/issue453-backstop.test.ts | 206 ++++++++++++++++++++++++++++++++ 2 files changed, 285 insertions(+), 6 deletions(-) create mode 100644 tests/issue453-backstop.test.ts diff --git a/src/server.ts b/src/server.ts index afedc02d78..0e4ce14c03 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1233,6 +1233,7 @@ async function handle( // otherwise the request is degenerate (output >= whole window) and the // self-heal above handles the resulting overflow. Feeds reqConfig (→ // processTurn `config`), so diagNudge shows the reserved window (no extra log). + const nativeWindow = reqConfig.modelContextLimit; if (shouldReserveOutputHeadroom(protocol)) { const p = parsed as Record; const rawMax = p.max_tokens ?? p.max_completion_tokens ?? p.max_output_tokens; @@ -1264,10 +1265,10 @@ async function handle( : protocol === "anthropic" ? prepareAnthropic(parsed as AnthropicRequestBody, req, opts, core, reqConfig, reqPrompts, log, session, pluginMode) : protocol === "openai" - ? prepareOpenai(parsed as OpenAIRequestBody, req, opts, core, reqConfig, reqPrompts, log, session, pluginMode) + ? prepareOpenai(parsed as OpenAIRequestBody, req, opts, core, reqConfig, reqPrompts, log, session, pluginMode, nativeWindow) : responsesCompact ? prepareResponsesCompact(bodyBuffer, parsed as ResponsesRequestBody, session, req, core, reqConfig, log) - : prepareResponses(parsed as ResponsesRequestBody, req, opts, core, reqConfig, reqPrompts, log, session, responsesIdentity!, pluginMode, upstreamOrigin); + : prepareResponses(parsed as ResponsesRequestBody, req, opts, core, reqConfig, reqPrompts, log, session, responsesIdentity!, pluginMode, upstreamOrigin, nativeWindow); prepared = runPrepare(); if (!countTokens && !responsesCompact) { const outcome = await preflightCompressIfNeeded( @@ -1382,7 +1383,9 @@ function diagNudge(turn: { nudge?: { shouldInject: boolean; reason: string; cont // "INJECT" only when the nudge actually reaches the upstream payload. When // armed but suppressed by config/mode, say so explicitly so the log never // lies about delivery (#451, same class as #413). - const inject = !n.shouldInject ? "idle" : willInject ? `INJECT T${n.tier ?? "?"}` : `ARMED-SUPPRESSED T${n.tier ?? "?"}`; + const inject = willInject + ? (n.shouldInject ? `INJECT T${n.tier ?? "?"}` : `INJECT-ESC T${n.tier ?? "?"}`) + : (n.shouldInject ? `ARMED-SUPPRESSED T${n.tier ?? "?"}` : "idle"); const modelTag = model ? ` model=${model}` : ""; return `[${sessionId}] nudge ${inject}: usage=${pct} (${tokenCount}/${limit}), growth=${growth}/${floor} (ref=${ref}, interval=${interval}), pendingT1=${pendingT1}/${interval}${modelTag}, reason="${n.reason.slice(0, 120)}"`; } @@ -1444,7 +1447,7 @@ function prepareAnthropic( if (t) session.meta.title = t; } log("info", diagTagSummary(turn.messages, sessionId, "text-only")); - const willInjectNudge = opts.compress.injectNudge && !!turn.nudge?.shouldInject; + const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && (turn.nudge.shouldInject || emergencyNudge(turn.nudge)); log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model, willInjectNudge)); processedMessages = stripKernelSummaries(turn.messages, turn.state); reapOrphanBlocks(session, msgs, deactivateBlock); @@ -1485,6 +1488,70 @@ function prepareAnthropic( return { body: JSON.stringify(rebuilt), session, processedMessages, originalMessages, anthropicSystem: parsed.system, protocol: "anthropic", stream, compressInjected: injectTools, pluginMode, nudge, prompts, renderTags: "text-only" } as Prepared; } +// #453 hard backstop: cap the forwarded output budget so input+output can never +// exceed the window on request-rebuilding upstreams (vLLM rejects an oversized +// total instead of clamping). Non-Anthropic only — Anthropic enforces its input +// limit independently of max_tokens (see shouldReserveOutputHeadroom). The proxy +// owns both sides of the sum, so capping output to (window - input - margin) +// makes the overflow impossible even when the agent ignores the compress nudge. +const OUTPUT_CLAMP_MARGIN_PCT = 0.05; +const OUTPUT_CLAMP_MIN_MARGIN = 2048; +const OUTPUT_CLAMP_FLOOR = 1024; +// #453 mitigation: host-side escalation line. The kernel already force-injects +// every turn once usage >= nudge.maxContextLimitPct (its pressure branch has no +// cadence gate), so the only cadence-silent zone is BELOW that line. Force the +// nudge each turn once usage reaches this — kept under the default 0.75 +// over-limit line so it fills the pre-limit silent climb seen in #453/#14. Pure +// host-side: renderNudgeText does not depend on shouldInject. +const EMERGENCY_NUDGE_ESCALATION_PCT = 0.7; + +/** Conservative outbound-input estimate: the larger of the upstream-reported + * previous-turn input (real tokenizer count, already includes system+tools) and + * a fresh count of the rebuilt conversation text + system + tool definitions + * (needed on turn 1 / right after a shrink, when lastInputTokens lags). */ +export function estimateInputTokens(processedMessages: CoreMessage[], systemText: string | undefined, tools: unknown, lastInputTokens: number): number { + const est = estimateCoreMessages(processedMessages) + + defaultCountTokens(systemText ?? "") + + defaultCountTokens(JSON.stringify(tools ?? [])); + return Math.max(lastInputTokens > 0 ? lastInputTokens : 0, est); +} + +/** Output-budget cap so input+output <= window. Returns the clamped budget, or + * undefined when no reduction is needed (requested already fits, or the cap + * drops below OUTPUT_CLAMP_FLOOR — i.e. input alone nearly fills the window, + * which is preflight/self-heal territory, not output starvation). */ +export function clampOutputBudget(requested: number, inputEstimate: number, nativeWindow: number): number | undefined { + const margin = Math.max(OUTPUT_CLAMP_MIN_MARGIN, Math.ceil(inputEstimate * OUTPUT_CLAMP_MARGIN_PCT)); + const cap = nativeWindow - inputEstimate - margin; + if (cap < OUTPUT_CLAMP_FLOOR || cap >= requested) return undefined; + return cap; +} + +// Only override genuine cadence silences: skip the kernel's deliberate +// "nothing compressible to offer" suppression (empty ranges). +export function emergencyNudge(nudge: NudgeDecision | null | undefined, escalationPct: number = EMERGENCY_NUDGE_ESCALATION_PCT): boolean { + if (!nudge || nudge.shouldInject) return false; + if (nudge.compressibleRanges.length === 0) return false; + return nudge.contextUsage >= escalationPct; +} + +function clampOutgoingOutput( + rebuilt: Record, + field: "max_tokens" | "max_completion_tokens" | "max_output_tokens", + ctx: { systemText: string; tools: unknown; processedMessages: CoreMessage[]; lastInputTokens: number; nativeWindow: number }, + sessionId: string, + log: (level: string, msg: string) => void, +): void { + const raw = rebuilt[field]; + if (typeof raw !== "number") return; + const inputEstimate = estimateInputTokens(ctx.processedMessages, ctx.systemText, ctx.tools, ctx.lastInputTokens); + const capped = clampOutputBudget(raw, inputEstimate, ctx.nativeWindow); + if (capped !== undefined) { + rebuilt[field] = capped; + log("info", `[${sessionId}] output budget clamped ${raw} -> ${capped} (input~${inputEstimate}, window=${ctx.nativeWindow}); prevents input+output overflow (#453)`); + } +} + function prepareOpenai( parsed: OpenAIRequestBody, req: http.IncomingMessage, @@ -1495,6 +1562,7 @@ function prepareOpenai( log: (level: string, msg: string) => void, session: Session, pluginMode: boolean, + nativeWindow: number, ): Prepared { const sessionId = session.id; const stream = parsed.stream === true; @@ -1544,7 +1612,7 @@ function prepareOpenai( if (t) session.meta.title = t; } log("info", diagTagSummary(turn.messages, sessionId, "text-only")); - const willInjectNudge = opts.compress.injectNudge && !!turn.nudge?.shouldInject && shouldInject; + const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && shouldInject && (turn.nudge.shouldInject || emergencyNudge(turn.nudge)); log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model, willInjectNudge)); processedMessages = stripKernelSummaries(turn.messages, turn.state); reapOrphanBlocks(session, msgs, deactivateBlock); @@ -1584,6 +1652,7 @@ function prepareOpenai( } const rebuilt: OpenAIRequestBody = { ...parsed, messages: rebuiltMessages, tools: toolsOut as OpenAITool[] | undefined }; + clampOutgoingOutput(rebuilt as Record, typeof (parsed as Record).max_completion_tokens === "number" ? "max_completion_tokens" : "max_tokens", { systemText: openaiSystemText, tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, nativeWindow }, sessionId, log); // prompt_cache_retention is an OpenAI-host-only cache directive; the dsh // launcher forces PI_CACHE_RETENTION=long (for the session-id // prompt_cache_key) which makes the client also emit it. Third-party @@ -1618,6 +1687,7 @@ function prepareResponses( identity: ConversationIdentity, pluginMode: boolean, upstreamOrigin: string, + nativeWindow: number, ): Prepared { const sessionId = session.id; const stream = parsed.stream === true; @@ -1704,7 +1774,7 @@ function prepareResponses( if (t) session.meta.title = t; } log("info", diagTagSummary(turn.messages, sessionId, "text-only")); - const willInjectNudge = opts.compress.injectNudge && !!turn.nudge?.shouldInject && shouldInject && !isCompactionTrigger; + const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && shouldInject && !isCompactionTrigger && (turn.nudge.shouldInject || emergencyNudge(turn.nudge)); log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model, willInjectNudge)); processedMessages = stripKernelSummaries(turn.messages, turn.state); reapOrphanBlocks(session, msgs, deactivateBlock); @@ -1790,6 +1860,9 @@ function prepareResponses( } const rebuilt: ResponsesRequestBody = { ...parsed, input: rebuiltInput, tools: toolsOut }; + if (!isCompactionTrigger) { + clampOutgoingOutput(rebuilt as Record, "max_output_tokens", { systemText: (responsesProjection?.systemParts ?? []).join("\n"), tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, nativeWindow }, sessionId, log); + } // Route with the upstream THIS request goes to — session.meta.upstreamOrigin // is first-wins and would keep injecting pck toward a relay we switched // away from (same class of bug as the compressProtocol fix above, #286). diff --git a/tests/issue453-backstop.test.ts b/tests/issue453-backstop.test.ts new file mode 100644 index 0000000000..4f03fc0ef3 --- /dev/null +++ b/tests/issue453-backstop.test.ts @@ -0,0 +1,206 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { defaultConfig, type NudgeDecision } from "acp-kernel"; +import { startServer, type ProxyOptions, clampOutputBudget, estimateInputTokens, emergencyNudge } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; + +// #453: plugin mode has no hard backstop when the agent ignores the compress +// nudge — context grows past the window and the upstream 400s on input+output. +// Two fixes land here: +// 1) clampOutputBudget/estimateInputTokens — cap the forwarded output budget +// so input+output can never exceed the window (the hard backstop). +// 2) emergencyNudge — force the nudge every turn once usage hits the +// escalation line even when the kernel's cadence gate would stay silent. + +// ---- pure-helper unit tests ------------------------------------------------- + +test("clampOutputBudget: reduces an oversized request so input+output fits", () => { + // window 262144, input ~130k, requested 131072 (the #453 repro shape). + const capped = clampOutputBudget(131_072, 130_000, 262_144); + assert.ok(capped !== undefined, "must clamp"); + const margin = Math.max(2048, Math.ceil(130_000 * 0.05)); + assert.equal(capped, 262_144 - 130_000 - margin, "cap = window - input - margin"); + assert.ok(capped < 131_072, "clamped below the requested budget"); + assert.ok((capped ?? 0) + 130_000 <= 262_144, "input + clamped output never exceeds the window"); +}); + +test("clampOutputBudget: no-op when the request already fits", () => { + assert.equal(clampOutputBudget(8_192, 10_000, 200_000), undefined); +}); + +test("clampOutputBudget: no-op when input alone nearly fills the window (preflight territory)", () => { + // input 199k of a 200k window leaves no sane output budget -> leave untouched. + assert.equal(clampOutputBudget(100_000, 199_000, 200_000), undefined); +}); + +test("estimateInputTokens: prefers the real previous-turn count when it lags high", () => { + assert.equal(estimateInputTokens([], "", [], 50_000), 50_000); +}); + +test("estimateInputTokens: counts system + tools on turn 1 (lastInputTokens=0)", () => { + const baseline = estimateInputTokens([], "", [], 0); + const withSystem = estimateInputTokens([], "x".repeat(4_000), [{ name: "tool" }], 0); + assert.ok(withSystem > baseline, "adding system + tools raises the estimate"); + assert.ok(withSystem > 900, "a 4000-char system contributes ~1000 tokens"); +}); + +function mkNudge(over: Partial> = {}): NudgeDecision { + const breakdown = { usage: 0, growth: 0, growthReference: 0, effectiveThreshold: 0, nudgeGrowthTokens: 0, growthFloor: 0, hasPendingNudge: 0, overLimit: 0, emergencyOverride: 0, pendingT1: 0, pendingT2: 0, pendingT3: 0 }; + return { shouldInject: false, reason: "test", compressibleRanges: [], contextUsage: 0, tier: null, breakdown, ...over }; +} + +test("emergencyNudge: never overrides an armed nudge", () => { + assert.equal(emergencyNudge(mkNudge({ shouldInject: true, contextUsage: 0.99, compressibleRanges: [{ startRef: "a", endRef: "b", count: 1, tokens: 1, toolPct: 0, textPct: 0 }] })), false); +}); + +test("emergencyNudge: fires at the escalation line with compressible content", () => { + const ranges = [{ startRef: "a", endRef: "b", count: 1, tokens: 1, toolPct: 0, textPct: 0 }]; + assert.equal(emergencyNudge(mkNudge({ contextUsage: 0.75, compressibleRanges: ranges })), true, "0.75 >= 0.70 default"); + assert.equal(emergencyNudge(mkNudge({ contextUsage: 0.6, compressibleRanges: ranges })), false, "below the line"); +}); + +test("emergencyNudge: respects a custom escalation line and empty ranges", () => { + const ranges = [{ startRef: "a", endRef: "b", count: 1, tokens: 1, toolPct: 0, textPct: 0 }]; + assert.equal(emergencyNudge(mkNudge({ contextUsage: 0.75, compressibleRanges: ranges }), 0.8), false, "0.75 < custom 0.8"); + assert.equal(emergencyNudge(mkNudge({ contextUsage: 0.85, compressibleRanges: ranges }), 0.8), true, "0.85 >= custom 0.8"); + assert.equal(emergencyNudge(mkNudge({ contextUsage: 0.99 })), false, "empty ranges = nothing to offer, no override"); + assert.equal(emergencyNudge(null), false); + assert.equal(emergencyNudge(undefined), false); +}); + +// ---- integration: real proxy + upstream ------------------------------------- + +function okJson(promptTokens: number): string { + return JSON.stringify({ + id: "chatcmpl-1", + object: "chat.completion", + choices: [{ index: 0, message: { role: "assistant", content: "ok" }, finish_reason: "stop" }], + usage: { prompt_tokens: promptTokens, completion_tokens: 3, total_tokens: promptTokens + 3 }, + }); +} + +interface DriveOpts { + sessionId: string; + window: number; + turn1PromptTokens: number; + turn2Body: Record; + extraHeaders?: Record; +} + +async function drive(o: DriveOpts): Promise<{ turn2Body: Record; turn2MessageCount: number }> { + const received: Record[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + received.push(JSON.parse(Buffer.concat(chunks).toString("utf8"))); + res.writeHead(200, { "content-type": "application/json" }); + res.end(okJson(o.turn1PromptTokens)); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upPort = (upstream.address() as { port: number }).port; + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const proxy = await startServer({ + port: 0, host: "127.0.0.1", upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upPort}`]: { models: { m: { context: o.window } } } }, + modelContextLimit: o.window, kernelConfig: defaultConfig(o.window), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", + log: false, debug: false, passthrough: false, autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const pPort = (proxy.address() as { port: number }).port; + + try { + const url = `http://127.0.0.1:${pPort}/bili/http://127.0.0.1:${upPort}/v1/chat/completions`; + const headers = { "content-type": "application/json", "x-acp-session": o.sessionId, ...(o.extraHeaders ?? {}) }; + const r1 = await fetch(url, { method: "POST", headers, body: JSON.stringify({ model: "m", max_tokens: 20_000, messages: [{ role: "user", content: "hello" }] }) }); + assert.equal(r1.status, 200); + await r1.text(); + const r2 = await fetch(url, { method: "POST", headers, body: JSON.stringify(o.turn2Body) }); + assert.equal(r2.status, 200); + await r2.text(); + assert.equal(received.length, 2, "both turns reached the upstream"); + const turn2Body = received[1]!; + return { turn2Body, turn2MessageCount: ((turn2Body.messages ?? []) as unknown[]).length }; + } finally { + proxy.close(); + await once(proxy, "close"); + upstream.close(); + await once(upstream, "close"); + } +} + +// A 29-message history shaped so compressible ranges exist (long text well +// outside the protected recent zone) while its CORE estimate stays modest. +function turn2Compressible(): Record[] { + const longText = "y".repeat(20_000); + const filler: { role: "user" | "assistant"; content: string }[] = []; + for (let i = 1; i <= 12; i++) { + filler.push({ role: "user", content: `q${i} ` + "f".repeat(997) }); + filler.push({ role: "assistant", content: `a${i} ` + "e".repeat(997) }); + } + return [ + { role: "user", content: "hello" }, + { role: "assistant", content: "ok" }, + { role: "user", content: "continue" }, + { role: "assistant", content: longText }, + ...filler, + { role: "user", content: "now summarize" }, + ]; +} + +test("#453 clamp: oversized max_tokens is reduced when input+system nearly fills the window", async () => { + // window 100k, requested max_tokens 40k -> reserved window 60k. The core + // conversation estimates ~48k (under 60k, so preflight skips — it ignores the + // system), but the 60k-char system pushes the TRUE input past 60k. Only the + // clamp sees the system, so it lowers max_tokens to keep input+output under 100k. + const fired = await drive({ + sessionId: "clamp-fire", + window: 100_000, + turn1PromptTokens: 5_000, + turn2Body: { + model: "m", max_tokens: 40_000, + messages: [ + { role: "system", content: "z".repeat(60_000) }, + { role: "user", content: "hi" }, + { role: "assistant", content: "y".repeat(192_000) }, + { role: "user", content: "now" }, + ], + }, + }); + const out = typeof fired.turn2Body.max_tokens === "number" ? fired.turn2Body.max_tokens : undefined; + assert.ok(out !== undefined, "forwarded body carries a numeric max_tokens"); + assert.ok(out! < 40_000, `max_tokens clamped below the 40k request (got ${out})`); + assert.ok(out! > 0, "clamped budget stays positive"); + + // Control: no oversized system -> input fits -> max_tokens passes through. + const ctrl = await drive({ + sessionId: "clamp-ctrl", + window: 100_000, + turn1PromptTokens: 5_000, + turn2Body: { model: "m", max_tokens: 40_000, messages: turn2Compressible() }, + }); + assert.equal(ctrl.turn2Body.max_tokens, 40_000, "unclamped when input fits the window"); +}); + +test("#453 escalation: nudge injected at the escalation line though the kernel cadence-stays silent", async () => { + // window 100k, max_tokens 20k -> reserved 80k. Usage = lastInputTokens/80k. + // 57.6k -> 72% (>= 0.70 escalation line, < 0.75 over-limit) vs 48k -> 60%. + // Fresh session: growth not ready, so the kernel arms nothing at either + // point; only the host-side escalation adds the trailing nudge at 72%. + const escalated = await drive({ sessionId: "esc-on", window: 100_000, turn1PromptTokens: 57_600, turn2Body: { model: "m", max_tokens: 20_000, messages: turn2Compressible() } }); + const quiet = await drive({ sessionId: "esc-off", window: 100_000, turn1PromptTokens: 48_000, turn2Body: { model: "m", max_tokens: 20_000, messages: turn2Compressible() } }); + assert.equal(escalated.turn2MessageCount, quiet.turn2MessageCount + 1, "escalation adds exactly the trailing nudge message"); +}); From 0dde055ef1b922cc0dfe4a6cf35e4913794c1cc1 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Mon, 31 Aug 2026 10:53:28 +0800 Subject: [PATCH 10/22] feat: detect + retry fake completions (tool call written as text, #371) A small model (e.g. MiniMax-M3 via a gateway) can write a tool call as plain text - echoing the tool-call XML template from the context - instead of emitting a real tool block. With no tool block the agent ends the turn early and shows success, but the tool never ran (the "fake completion" from #371, root-cause follow-up to #361). Add a proxy-side detect + retry fallback: - Detect: a response carrying a complete tool-call structure (an opening invoke-style tag, or 2+ distinct closing tags) AND no real tool block for the wire protocol (Anthropic tool_use / OpenAI tool_calls / Responses function_call). Reuses PR #365's containsToolCallXmlFragment detector; the structural guard avoids false-positives on legit prose that merely mentions a single closing tag. - Retry: re-request upstream with a corrective hint appended as a trailing user message. Bounded per turn (max retries) and per session (session.metadata.fakeCompletionStreak, resets on a clean turn). Opt-in (disabled by default): the fallback must buffer the whole response before the client sees it (a fake completion is only knowable at end-of-stream), which defeats incremental streaming. That cost is only worth paying for the low-frequency case, so it is opt-in via BILI_FAKE_COMPLETION_RETRIES (default 0 = pre-#371 passthrough). Tests: tests/fake-completion.test.ts (21) - detection per protocol, the structure guard, hint injection per protocol, and full-proxy integration (fake -> retry -> real tool_use; retry cap via streak; legit prose no-retry). --- src/fake-completion.ts | 124 +++++++++++++++ src/server.ts | 100 ++++++++++++- tests/fake-completion.test.ts | 273 ++++++++++++++++++++++++++++++++++ 3 files changed, 492 insertions(+), 5 deletions(-) create mode 100644 src/fake-completion.ts create mode 100644 tests/fake-completion.test.ts diff --git a/src/fake-completion.ts b/src/fake-completion.ts new file mode 100644 index 0000000000..db30b6aba5 --- /dev/null +++ b/src/fake-completion.ts @@ -0,0 +1,124 @@ +import type { WireProtocol } from "./util.js"; +import { containsToolCallXmlFragment } from "./loop/tag-echo-filter.js"; + +// #371 (root-cause follow-up to #361): a small model can WRITE a tool call as +// plain text (echoing the tool-call XML template from the context) instead of +// emitting a real tool block. With no tool block the agent ends the turn early +// — a "fake completion": the tool never ran but the client shows success. The +// proxy detects this shape (tool-call structure present + no real tool block) +// and retries once with a corrective hint, bounded per turn and per session. + +export const FAKE_COMPLETION_HINT = + "[billion-context] Your last reply described a tool call as plain text instead of invoking it, so no tool actually ran. " + + "To act, invoke the tool through the proper tool-calling mechanism (emit a tool_use / tool_calls / function_call block). " + + "Do not write tool-call markup as text in your reply."; + +// One value serves two bounds: max retries within a single request, and the +// consecutive-fake-completion-turn cap after which a session stops retrying. +// +// DISABLED BY DEFAULT (0): the fallback must buffer the whole response before +// the client sees it (a fake completion is only knowable at end-of-stream), +// which defeats incremental streaming. That cost is only worth paying for the +// low-frequency fake-completion case (small models via gateways), so it is +// opt-in: set BILI_FAKE_COMPLETION_RETRIES=2 to enable. 0 = pre-#371 passthrough. +export function maxFakeCompletionRetries(): number { + const n = Number.parseInt(process.env.BILI_FAKE_COMPLETION_RETRIES ?? "0", 10); + return Number.isFinite(n) && n >= 0 ? n : 0; +} + +// OOM guard for a pathological upstream; LLM responses are bounded by max_tokens +// and normally well under 1 MiB. +export function fakeBufCap(): number { + const n = Number.parseInt(process.env.BILI_FAKE_BUF_CAP ?? String(16 * 1024 * 1024), 10); + return Number.isFinite(n) && n > 0 ? n : 16 * 1024 * 1024; +} + +// "Does this raw response (full SSE stream or JSON body) carry a REAL tool +// block?" A real block means the model actually invoked a tool → not a fake +// completion. Each protocol marks its tool block differently on the wire. +const ANTHROPIC_TOOL_BLOCK = /"type"\s*:\s*"tool_use"/; +const OPENAI_TOOL_BLOCK = /"tool_calls"\s*:\s*\[\s*\{/; +const RESPONSES_TOOL_BLOCK = /"type"\s*:\s*"function_call"/; + +export function hasToolBlock(protocol: WireProtocol, rawText: string): boolean { + switch (protocol) { + case "anthropic": + return ANTHROPIC_TOOL_BLOCK.test(rawText); + case "openai": + return OPENAI_TOOL_BLOCK.test(rawText); + case "responses": + return RESPONSES_TOOL_BLOCK.test(rawText); + } +} + +// Opening tool tag (no leading '/'): the model started writing a call as text — +// the strongest signal. Literal '<' or JSON-escaped '\u003c'; optional antml: +// namespace; case-insensitive, matching containsToolCallXmlFragment. +const TOOL_OPEN = + /\x3c(?!\s*\/)(?:antml:)?(invoke|tool_calls|tool_call|parameter|parameters)\b|\\u003c(?!\s*\/)(?:antml:)?(invoke|tool_calls|tool_call|parameter|parameters)\b/i; + +// Closing tool tags (global): an echoed template tail carries 2+ distinct +// closers ( + ) even when the openers were folded away +// earlier in the context (the #361 shape). +const TOOL_CLOSE_GLOBAL = + /\x3c\/(?:antml:)?(invoke|tool_calls|tool_call|parameter|parameters)\b|\\u003c\/(?:antml:)?(invoke|tool_calls|tool_call|parameter|parameters)\b/gi; + +// Structural guard on top of containsToolCallXmlFragment: require an opening +// tool tag OR 2+ distinct closing tags. A LONE closing tag in prose (a model +// discussing tool-call code) does not qualify — that is the false positive the +// plain fragment detector would otherwise trigger on. +export function hasToolCallStructure(text: string): boolean { + if (!containsToolCallXmlFragment(text)) return false; + if (TOOL_OPEN.test(text)) return true; + const closes = new Set(); + for (const m of text.matchAll(TOOL_CLOSE_GLOBAL)) { + const name = m[1] ?? m[2]; + if (name) closes.add(name.toLowerCase()); + } + return closes.size >= 2; +} + +export function isFakeCompletion(protocol: WireProtocol, rawText: string): boolean { + return hasToolCallStructure(rawText) && !hasToolBlock(protocol, rawText); +} + +// Appends the hint as a trailing user message, merged into the last user +// message when present (avoids back-to-back user turns on strict providers). +// Returns the hinted JSON, or null if the body is unparseable (skip the retry). +export function injectFakeCompletionHint(protocol: WireProtocol, body: string | Buffer): string | null { + const raw = typeof body === "string" ? body : body.toString("utf8"); + let obj: Record; + try { + obj = JSON.parse(raw) as Record; + } catch { + return null; + } + if (protocol === "responses") { + const input = obj.input; + if (!Array.isArray(input)) return null; + const arr = input as Record[]; + const last = arr[arr.length - 1]; + if (last && typeof last === "object" && last.role === "user") { + const c = last.content; + if (typeof c === "string") last.content = `${c}\n\n${FAKE_COMPLETION_HINT}`; + else if (Array.isArray(c)) last.content = [...c, { type: "input_text", text: FAKE_COMPLETION_HINT }]; + else last.content = FAKE_COMPLETION_HINT; + } else { + arr.push({ role: "user", content: [{ type: "input_text", text: FAKE_COMPLETION_HINT }] }); + } + return JSON.stringify(obj); + } + const messages = obj.messages; + if (!Array.isArray(messages)) return null; + const arr = messages as Record[]; + const last = arr[arr.length - 1]; + if (last && typeof last === "object" && last.role === "user") { + const c = last.content; + if (typeof c === "string") last.content = `${c}\n\n${FAKE_COMPLETION_HINT}`; + else if (Array.isArray(c)) last.content = [...c, { type: "text", text: FAKE_COMPLETION_HINT }]; + else last.content = FAKE_COMPLETION_HINT; + } else { + arr.push({ role: "user", content: FAKE_COMPLETION_HINT }); + } + return JSON.stringify(obj); +} diff --git a/src/server.ts b/src/server.ts index afedc02d78..4b427d1697 100644 --- a/src/server.ts +++ b/src/server.ts @@ -55,6 +55,7 @@ import { atomicWriteInstanceFile, clearProxyInstanceFile, isPidAlive, registerIn import { compressLoopResponsesJson } from "./compress-loop-responses.js"; import { runCompressLoop, pickAdapter } from "./loop/index.js"; import { containsToolCallXmlFragment } from "./loop/tag-echo-filter.js"; +import { isFakeCompletion, injectFakeCompletionHint, maxFakeCompletionRetries, fakeBufCap } from "./fake-completion.js"; import { sanitizeResponsesInputIds, dropWhitespaceResponsesMessages, normalizeResponsesMessageItems } from "./loop/adapter-responses.js"; import { codexCompactMode, isCodexClient, hasCompactionTrigger, stripBiliCompactionItems, replaceBiliCompactionItems, codexCompactGate, buildTriggerForgeBody, mergeForgedSummaries } from "./codex-compact.js"; import { rewriteOpenaiJsonResponse } from "./stream-openai.js"; @@ -66,7 +67,7 @@ import { prefixAffinity, type AnonymousAffinity } from "./prefix-affinity.js"; import { consumePluginRegisterFor, flushConversations, handlePluginManifest, handlePluginRegister, handlePluginStatus, handlePluginTool, loadConversations, pipePluginChatWithStrip, pipePluginJson, pipePluginResponsesWithStrip, pluginAgentHeader, pluginConversationHeader, pluginReportedContextWindow, recordPluginSession, rememberPluginMessages, takePendingPluginRegister } from "./plugin.js"; import { setupMitm, readMitmUpstream } from "./mitm.js"; import type { BiliMessage } from "acp-kernel/wire"; -import { systemToUser, isLoopbackAddress, inspectContextOverflow, reserveOutputHeadroom, shouldReserveOutputHeadroom, usageTotals } from "./util.js"; +import { systemToUser, isLoopbackAddress, inspectContextOverflow, reserveOutputHeadroom, shouldReserveOutputHeadroom, usageTotals, type WireProtocol } from "./util.js"; import { BILI_TUNNEL_HEADER, checkTunnelDestination, tunnelAllowlistFromEnv } from "./tunnel-guard.js"; import { decodeRequestBody } from "./content-encoding.js"; @@ -2569,6 +2570,23 @@ async function forward( clearUpstreamTimer(); return; } + // #371: detect + retry a fake completion for every non-plugin streaming + // response (any turn, not just compress-injected). Buffering is required: + // the retry re-requests before the client sees the fake completion. + let responseBody: ReadableStream = upstream.body; + if (prepared !== null && prepared.stream && maxFakeCompletionRetries() > 0) { + const resolvedBuf = await resolveFakeCompletion(upstream.body, { + protocol: prepared.protocol, + body, + upstreamUrl, + reqHeaders: buildForwardHeaders(headers), + proxyUrl, + signal: clientAbort.signal, + session: prepared.session, + log, + }); + responseBody = bufferToStream(resolvedBuf); + } // We only rewrite when THIS request actually had the compress tool // injected (per-request). For the OpenAI title-gen path // (`compressInjected === false`) we must NOT route the stream into a @@ -2581,7 +2599,7 @@ async function forward( prepared.processedMessages.length > 0; if (!useRewriter || prepared === null) { if (prepared && prepared.resetAfterSuccess) { - const [toClient, toObserve] = upstream.body.tee(); + const [toClient, toObserve] = responseBody.tee(); const observed = observeResponsesTerminalState(toObserve, prepared.stream); await pipeThrough(toClient, res); clearUpstreamTimer(); @@ -2593,7 +2611,7 @@ async function forward( log("warn", `[${prepared.session.id}] native compact response terminal=${terminal}; rebase NOT scheduled`); } } else { - await pipeThrough(upstream.body, res); + await pipeThrough(responseBody, res); clearUpstreamTimer(); } return; @@ -2607,10 +2625,10 @@ async function forward( debug: opts.debug, }; if (prepared.stream) { - let streamToRead = upstream.body as ReadableStream; + let streamToRead = responseBody; let dumpRaw: Promise | undefined; if (opts.dumpSse) { - const [a, b] = (upstream.body as ReadableStream).tee(); + const [a, b] = responseBody.tee(); streamToRead = a; dumpRaw = dumpStreamToFile(b, opts.dumpSse, `${Date.now()}-${safeSessionId(prepared.session.id)}-raw.sse`); } @@ -2776,6 +2794,78 @@ async function readStreamToBuffer(stream: ReadableStream, maxBytes = return Buffer.concat(chunks); } +function bufferToStream(buf: Buffer): ReadableStream { + return new ReadableStream({ + start(controller) { + controller.enqueue(new Uint8Array(buf)); + controller.close(); + }, + }); +} + +// #371: buffer the raw upstream response, detect a fake completion (tool-call +// XML, no real tool block), and — bounded per turn and per session — re-request +// upstream with a corrective hint. Returns the bytes to stream to the client +// (the retry's response when a retry recovered, else the original). The session +// streak (metadata.fakeCompletionStreak) counts consecutive fake-completion +// turns: it gates retries (skip once >= cap) and resets to 0 on a clean turn. +async function resolveFakeCompletion( + stream: ReadableStream, + opts: { + protocol: WireProtocol; + body: string | Buffer; + upstreamUrl: string; + reqHeaders: Record; + proxyUrl?: string; + signal: AbortSignal; + session: Session; + log: (level: string, msg: string) => void; + }, +): Promise { + let buffer = await readStreamToBuffer(stream, fakeBufCap()); + const max = maxFakeCompletionRetries(); + const sid = opts.session.id; + const priorStreak = (opts.session.metadata.fakeCompletionStreak as number | undefined) ?? 0; + if (max > 0 && priorStreak < max && isFakeCompletion(opts.protocol, buffer.toString("utf8"))) { + for (let attempt = 1; attempt <= max && !opts.signal.aborted; attempt++) { + const hinted = injectFakeCompletionHint(opts.protocol, opts.body); + if (hinted === null) break; + opts.log("warn", `[${sid}] fake completion (tool-call XML, no tool block); retry ${attempt}/${max} with corrective hint`); + let r: Awaited>; + try { + r = await fetchWithTimeout( + opts.upstreamUrl, + { + method: "POST", + headers: opts.reqHeaders, + body: hinted, + ...(opts.proxyUrl ? { dispatcher: proxyDispatcher(opts.proxyUrl) } : {}), + }, + undefined, + opts.signal, + ); + } catch (e) { + opts.log("warn", `[${sid}] fake-completion retry failed: ${String(e)}; presenting original`); + break; + } + try { + if (!r.response.ok || !r.response.body) { + opts.log("warn", `[${sid}] fake-completion retry rejected (HTTP ${r.response.status}); presenting original`); + break; + } + buffer = await readStreamToBuffer(r.response.body, fakeBufCap()); + } finally { + r.clearTimer(); + } + if (!isFakeCompletion(opts.protocol, buffer.toString("utf8"))) break; + } + } + const stillFake = isFakeCompletion(opts.protocol, buffer.toString("utf8")); + opts.session.metadata.fakeCompletionStreak = stillFake ? priorStreak + 1 : 0; + markDirty(opts.session); + return buffer; +} + async function pipeThrough(stream: ReadableStream, res: http.ServerResponse): Promise { const reader = stream.getReader(); try { diff --git a/tests/fake-completion.test.ts b/tests/fake-completion.test.ts new file mode 100644 index 0000000000..9fea9a3c46 --- /dev/null +++ b/tests/fake-completion.test.ts @@ -0,0 +1,273 @@ +import assert from "node:assert"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; +// The fake-completion fallback is opt-in (disabled by default to preserve +// incremental streaming); these tests exercise it, so enable it explicitly. +process.env.BILI_FAKE_COMPLETION_RETRIES = "2"; + +import { defaultConfig } from "acp-kernel"; +import { startServer, type ProxyOptions } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { + isFakeCompletion, + hasToolBlock, + hasToolCallStructure, + injectFakeCompletionHint, +} from "../src/fake-completion.ts"; + +// Hex escapes so no literal tag sequence appears in this file's source. +const LT = "\x3c"; +const GT = "\x3e"; + +// The #361 shape: the model echoed a tool call as TEXT — an opening invoke +// plus closing tags — with no real tool block. A distinctive marker lets the +// integration tests prove this text is discarded on retry. +const FAKE_XML = `${LT}invoke name="read_file"${GT}\n${LT}parameter name="path"${GT}/fake/completion/marker${LT}/parameter${GT}\n${LT}/invoke${GT}\n${LT}/tool_calls${GT}`; + +test("hasToolBlock: anthropic detects a tool_use block", () => { + assert.equal(hasToolBlock("anthropic", '{"type":"tool_use","name":"x"}'), true); + assert.equal(hasToolBlock("anthropic", '{"type":"text","text":"hi"}'), false); +}); + +test("hasToolBlock: openai detects a non-empty tool_calls array", () => { + assert.equal(hasToolBlock("openai", '{"choices":[{"delta":{"tool_calls":[{"index":0}]}}]}'), true); + assert.equal(hasToolBlock("openai", '{"choices":[{"delta":{"content":"hi"}}]}'), false); +}); + +test("hasToolBlock: responses detects a function_call item", () => { + assert.equal(hasToolBlock("responses", '{"type":"function_call","name":"x"}'), true); + assert.equal(hasToolBlock("responses", '{"type":"message"}'), false); +}); + +test("hasToolCallStructure: an opening tool tag is a structure", () => { + assert.equal(hasToolCallStructure(`${LT}invoke name="x"${GT}`), true); +}); + +test("hasToolCallStructure: two distinct closing tags (the #361 shape) is a structure", () => { + assert.equal(hasToolCallStructure(`${LT}/invoke${GT} ${LT}/tool_calls${GT}`), true); +}); + +test("hasToolCallStructure: a single closing tag is NOT a structure (legit prose)", () => { + assert.equal(hasToolCallStructure(`the ${LT}/invoke${GT} tag closes a call`), false); +}); + +test("hasToolCallStructure: plain text with no tool XML is not a structure", () => { + assert.equal(hasToolCallStructure("just plain text about tools"), false); +}); + +test("isFakeCompletion: anthropic tool-XML with no tool_use block is a fake completion", () => { + assert.equal(isFakeCompletion("anthropic", FAKE_XML), true); +}); + +test("isFakeCompletion: openai tool-XML with no tool_calls is a fake completion", () => { + assert.equal(isFakeCompletion("openai", FAKE_XML), true); +}); + +test("isFakeCompletion: responses tool-XML with no function_call is a fake completion", () => { + assert.equal(isFakeCompletion("responses", FAKE_XML), true); +}); + +test("isFakeCompletion: tool-XML WITH a real tool block is NOT a fake completion", () => { + assert.equal(isFakeCompletion("anthropic", `${FAKE_XML} {"type":"tool_use"}`), false); + assert.equal(isFakeCompletion("openai", `${FAKE_XML} "tool_calls":[{`), false); + assert.equal(isFakeCompletion("responses", `${FAKE_XML} {"type":"function_call"}`), false); +}); + +test("isFakeCompletion: legit prose with a single closing tag is NOT a fake completion", () => { + assert.equal(isFakeCompletion("anthropic", `the ${LT}/invoke${GT} tag closes a call`), false); +}); + +test("isFakeCompletion: plain text is not a fake completion", () => { + assert.equal(isFakeCompletion("anthropic", "all done, nothing to do"), false); +}); + +test("injectFakeCompletionHint: anthropic merges the hint into the last user message", () => { + const body = JSON.stringify({ model: "m", messages: [{ role: "user", content: "hello" }] }); + const out = injectFakeCompletionHint("anthropic", body); + assert.ok(out !== null); + const parsed = JSON.parse(out!) as { messages: Array<{ role: string; content: string }> }; + assert.equal(parsed.messages.length, 1); + assert.ok(parsed.messages[0]!.content.includes("hello")); + assert.ok(parsed.messages[0]!.content.includes("tool-calling mechanism")); +}); + +test("injectFakeCompletionHint: anthropic appends a new user message when the last is assistant", () => { + const body = JSON.stringify({ model: "m", messages: [{ role: "user", content: "hi" }, { role: "assistant", content: "yo" }] }); + const out = injectFakeCompletionHint("anthropic", body); + assert.ok(out !== null); + const parsed = JSON.parse(out!) as { messages: Array<{ role: string; content: string }> }; + assert.equal(parsed.messages.length, 3); + assert.equal(parsed.messages[2]!.role, "user"); + assert.ok(parsed.messages[2]!.content.includes("tool-calling mechanism")); +}); + +test("injectFakeCompletionHint: openai appends the hint to messages", () => { + const body = JSON.stringify({ model: "m", messages: [{ role: "user", content: "hi" }] }); + const out = injectFakeCompletionHint("openai", body); + assert.ok(out !== null); + const parsed = JSON.parse(out!) as { messages: Array<{ role: string; content: string }> }; + const last = parsed.messages[parsed.messages.length - 1]!; + assert.equal(last.role, "user"); + assert.ok(last.content.includes("tool-calling mechanism")); +}); + +test("injectFakeCompletionHint: responses merges an input_text part into the last user input", () => { + const body = JSON.stringify({ model: "m", input: [{ role: "user", content: [{ type: "input_text", text: "hi" }] }] }); + const out = injectFakeCompletionHint("responses", body); + assert.ok(out !== null); + const parsed = JSON.parse(out!) as { input: Array<{ role: string; content: Array<{ type: string; text?: string }> }> }; + const last = parsed.input[parsed.input.length - 1]!; + const texts = last.content.map((c) => c.text ?? "").join(""); + assert.ok(texts.includes("hi")); + assert.ok(texts.includes("tool-calling mechanism")); +}); + +test("injectFakeCompletionHint: returns null for an unparseable body", () => { + assert.equal(injectFakeCompletionHint("anthropic", "not json"), null); +}); + +interface Harness { + proxyPort: number; + upstreamPort: number; + captured: { body: string }[]; + close(): Promise; +} + +function anthropicSse(event: string, data: unknown): string { + return `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`; +} + +function startHarness(scripts: string[][]): Promise { + const captured: { body: string }[] = []; + let call = 0; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + captured.push({ body: Buffer.concat(chunks).toString() }); + res.writeHead(200, { "content-type": "text/event-stream" }); + const script = scripts[Math.min(call, scripts.length - 1)]!; + call += 1; + for (const line of script) res.write(line); + res.end(); + }); + }); + upstream.listen(0, "127.0.0.1"); + return (async () => { + await once(upstream, "listening"); + const upstreamPort = upstream.address().port; + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "claude-test": { context: 400_000 } } } }, + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: false, injectNudge: false }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const proxyPort = proxy.address().port; + return { + proxyPort, + upstreamPort, + captured, + close: async () => { + proxy.close(); + await once(proxy, "close"); + upstream.close(); + await once(upstream, "close"); + }, + }; + })(); +} + +async function callAnthropic(h: Harness, session: string, messages: Array<{ role: string; content: string }>): Promise { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/messages`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": session }, + body: JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: true, system: "You are a test assistant.", messages }), + }); + assert.equal(resp.status, 200); + let raw = ""; + for await (const chunk of resp.body) raw += Buffer.from(chunk).toString("utf8"); + return raw; +} + +function fakeCompletionScript(): string[] { + return [ + anthropicSse("message_start", { type: "message_start", message: { id: "msg_fc_1", role: "assistant", usage: { input_tokens: 10 } } }), + anthropicSse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }), + anthropicSse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: FAKE_XML } }), + anthropicSse("content_block_stop", { type: "content_block_stop", index: 0 }), + anthropicSse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 5 } }), + anthropicSse("message_stop", { type: "message_stop" }), + ]; +} + +function realToolUseScript(): string[] { + return [ + anthropicSse("message_start", { type: "message_start", message: { id: "msg_fc_2", role: "assistant", usage: { input_tokens: 10 } } }), + anthropicSse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "tool_use", id: "toolu_fc_1", name: "read_file", input: {} } }), + anthropicSse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "input_json_delta", partial_json: '{"path":"/foo"}' } }), + anthropicSse("content_block_stop", { type: "content_block_stop", index: 0 }), + anthropicSse("message_delta", { type: "message_delta", delta: { stop_reason: "tool_use", stop_sequence: null }, usage: { output_tokens: 5 } }), + anthropicSse("message_stop", { type: "message_stop" }), + ]; +} + +test("e2e #371: fake completion triggers a retry that yields a real tool call", async () => { + const h = await startHarness([fakeCompletionScript(), realToolUseScript()]); + try { + const raw = await callAnthropic(h, "fc-retry", [{ role: "user", content: "read the file" }]); + assert.equal(h.captured.length, 2, `expected exactly 2 upstream requests (original + 1 retry), got ${h.captured.length}`); + assert.ok(raw.includes('"type":"tool_use"'), "client should receive the real tool_use block from the retry"); + assert.ok(!raw.includes("/fake/completion/marker"), "the fake-completion text must not leak to the client"); + } finally { + await h.close(); + } +}); + +test("e2e #371: retry cap — always-fake upstream exhausts retries, then the session streak blocks further retries", async () => { + const h = await startHarness([fakeCompletionScript()]); + try { + await callAnthropic(h, "fc-cap", [{ role: "user", content: "t1" }]); + assert.equal(h.captured.length, 3, `turn 1: expected 3 requests (1 + 2 retries), got ${h.captured.length}`); + await callAnthropic(h, "fc-cap", [{ role: "user", content: "t2" }]); + assert.equal(h.captured.length, 6, `turn 2: expected 6 total (1 + 2 retries), got ${h.captured.length}`); + await callAnthropic(h, "fc-cap", [{ role: "user", content: "t3" }]); + assert.equal(h.captured.length, 7, `turn 3: expected 7 total (streak cap blocks the retry), got ${h.captured.length}`); + } finally { + await h.close(); + } +}); + +test("e2e #371: legit prose with a single closing tag does NOT trigger a retry", async () => { + const legitScript = [ + anthropicSse("message_start", { type: "message_start", message: { id: "msg_legit", role: "assistant", usage: { input_tokens: 10 } } }), + anthropicSse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }), + anthropicSse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: `The ${LT}/invoke${GT} tag closes a tool call in the template.` } }), + anthropicSse("content_block_stop", { type: "content_block_stop", index: 0 }), + anthropicSse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 5 } }), + anthropicSse("message_stop", { type: "message_stop" }), + ]; + const h = await startHarness([legitScript]); + try { + const raw = await callAnthropic(h, "fc-legit", [{ role: "user", content: "explain the template" }]); + assert.equal(h.captured.length, 1, `legit prose must NOT trigger a retry, got ${h.captured.length} requests`); + assert.ok(raw.includes("closes a tool call"), "legit prose must pass through to the client"); + } finally { + await h.close(); + } +}); From 0c0bf3fb5a3400571a5b8039d7e0a11ec2f935a1 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Thu, 3 Sep 2026 01:17:57 +0800 Subject: [PATCH 11/22] fix(#452): engage tag-echo filter on tag-head tails so split-delta tags are stripped Upstreams streaming a model-imitated render tag in tokenizer-sized fragments (delta.content: '\x3cac','p tokens','...', ...) never trip containsRenderTagText in any single chunk, so the per-chunk fast-path gates in pipePluginChatWithStrip / pipePluginResponsesWithStrip skipped the filter and the tag leaked verbatim to the client (observed live on Zhipu glm-5.3 through bili 0.1.71 plugin mode; captured raw SSE shows per-character tag deltas and zero strip drops for that session). Add mayStartRenderTag() (full detect OR end-anchored PARTIAL_TAIL) and use it at the three per-delta gates (openai content/reasoning fields, anthropic text/thinking deltas, responses output_text.delta). Pure-prose chunks keep byte-identical passthrough; only chunks carrying a complete tag or ending with a tag head (<, { + const out: string[] = []; + const res = makeRes(out); + const session = makeSession(); + const whole = `ok ${TAG_OPEN}m00042${TAG_CLOSE} tail`; + const micro = whole.match(/.{1,4}/g) ?? []; + const events = [...micro.map((piece) => chatChunk({ content: piece })), DONE]; + await pipePluginChatWithStrip(streamOf(events), res, session, "openai"); + const text = out.join(""); + assert.ok(!text.includes("m00042"), "micro-fragmented tag never reassembles downstream"); + assert.ok(!text.includes("\x3cacp") && !text.includes("\x3c/acp"), "no tag fragment survives"); + const joined = text + .split("\n") + .filter((l) => l.startsWith("data:")) + .filter((l) => !l.includes("[DONE]")) + .map((l) => JSON.parse(l.slice(5).trim()) as { choices?: Array<{ delta?: { content?: string } }> }) + .map((c) => c.choices?.[0]?.delta?.content ?? "") + .join(""); + assert.equal(joined, "ok tail", "surrounding prose reassembles to the tag-free text"); + const dataLines = text.split("\n").filter((l) => l.startsWith("data:")).filter((l) => !l.includes("[DONE]")); + for (const l of dataLines) { + assert.doesNotThrow(() => JSON.parse(l.slice(5).trim()), "every forwarded data line stays valid JSON"); + } +}); + +test("plugin anthropic passthrough strips a tag streamed in tokenizer-sized fragments (#468)", async () => { + const out: string[] = []; + const res = makeRes(out); + const session = makeSession(); + const whole = `ok ${TAG_OPEN}m00042${TAG_CLOSE} tail`; + const micro = whole.match(/.{1,4}/g) ?? []; + const events = [ + ...micro.map((piece) => `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: piece } })}\n\n`), + `event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn" } })}\n\n`, + ]; + await pipePluginChatWithStrip(streamOf(events), res, session, "anthropic"); + const text = out.join(""); + assert.ok(!text.includes("m00042"), "micro-fragmented tag never reassembles downstream"); + assert.ok(!text.includes("\x3cacp") && !text.includes("\x3c/acp"), "no tag fragment survives"); + const joined = text + .split("\n") + .filter((l) => l.startsWith("data:")) + .map((l) => JSON.parse(l.slice(5).trim()) as { delta?: { type?: string; text?: string } }) + .filter((ev) => ev.delta?.type === "text_delta") + .map((ev) => ev.delta?.text ?? "") + .join(""); + assert.equal(joined, "ok tail", "surrounding prose reassembles to the tag-free text"); +}); + test("plugin chat passthrough resolves held tail at stream end without [DONE]", async () => { const out: string[] = []; const res = makeRes(out); diff --git a/tests/plugin-passthrough-tag-strip.test.ts b/tests/plugin-passthrough-tag-strip.test.ts index ae559a25d1..08efc32a22 100644 --- a/tests/plugin-passthrough-tag-strip.test.ts +++ b/tests/plugin-passthrough-tag-strip.test.ts @@ -94,6 +94,30 @@ test("plugin passthrough strips tags split across deltas and flushes tail before assert.ok((blocks[flushIdx] ?? "").includes("msg_1"), "flushed tail delta carries item_id"); }); +test("plugin passthrough strips a tag streamed in tokenizer-sized fragments (#468)", async () => { + const out: string[] = []; + const res = makeRes(out); + const session = makeSession(); + const whole = `ok ${TAG_OPEN}m00042${TAG_CLOSE} tail`; + const micro = whole.match(/.{1,4}/g) ?? []; + const events = [ + ...micro.map((piece) => sse({ type: "response.output_text.delta", item_id: "msg_1", output_index: 0, delta: piece })), + sse({ type: "response.completed", response: { usage: { input_tokens: 7, output_tokens: 3 } } }), + ]; + await pipePluginResponsesWithStrip(streamOf(events), res, session); + const text = out.join(""); + assert.ok(!text.includes("m00042"), "micro-fragmented tag never reassembles downstream"); + assert.ok(!text.includes("\x3cacp") && !text.includes("\x3c/acp"), "no tag fragment survives"); + const joined = text + .split("\n") + .filter((l) => l.startsWith("data:")) + .map((l) => JSON.parse(l.slice(5).trim()) as { type?: string; delta?: string }) + .filter((ev) => ev.type === "response.output_text.delta" && typeof ev.delta === "string") + .map((ev) => ev.delta as string) + .join(""); + assert.equal(joined, "ok tail", "surrounding prose reassembles to the tag-free text"); +}); + test("plugin passthrough keeps function_call and non-tag events byte-identical", async () => { const out: string[] = []; const res = makeRes(out); @@ -167,7 +191,8 @@ test("plugin passthrough resolves held tail at stream end without a done-family ]; await pipePluginResponsesWithStrip(streamOf(events2), res2, session); const text2 = out2.join(""); - assert.ok(text2.includes(`"delta":"prose \x3cac"`), "ambiguous prefix passes through untouched at stream end"); + assert.ok(text2.includes(`"delta":"prose "`), "clean prefix passes once the ambiguous head is held"); + assert.ok(text2.includes(`"delta":"\x3cac"`), "ambiguous prefix flushed as its own delta at stream end, never lost"); }); test("plugin passthrough rebuildEvent collapses multi-line data payloads into one line", async () => { diff --git a/tests/tag-echo.test.ts b/tests/tag-echo.test.ts index 68558f4ff6..87cd628e8c 100644 --- a/tests/tag-echo.test.ts +++ b/tests/tag-echo.test.ts @@ -4,7 +4,7 @@ import type { Config, CoreMessage } from "acp-kernel"; import { createCore, createInitialState } from "acp-kernel"; import type { Session } from "../src/session.ts"; import { runCompressLoop, createOpenaiAdapter, createAnthropicAdapter, createResponsesAdapter } from "../src/loop/index.ts"; -import { stripAcpTags, createTagEchoFilter, containsRenderTagText, containsToolCallXmlFragment } from "../src/loop/tag-echo-filter.ts"; +import { stripAcpTags, createTagEchoFilter, containsRenderTagText, containsToolCallXmlFragment, mayStartRenderTag } from "../src/loop/tag-echo-filter.ts"; import { rewriteJsonResponse } from "../src/stream.ts"; import { rewriteOpenaiJsonResponse } from "../src/stream-openai.ts"; import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; @@ -399,3 +399,18 @@ test("tag-echo filter does not strip tool-call XML fragments (warn-only, #361)", const out = f.push(`done ${LT}/invoke> ${LT}/tool_calls>`) + f.flush(); assert.equal(out, `done ${LT}/invoke> ${LT}/tool_calls>`); }); + +test("mayStartRenderTag engages on complete tags and tag-head tails, not prose", () => { + assert.equal(mayStartRenderTag(TAG("m0042")), true); + assert.equal(mayStartRenderTag(`prose ${OPEN}tokens="1"`), true); + assert.equal(mayStartRenderTag("ok \x3c"), true); + assert.equal(mayStartRenderTag("ok \x3c/"), true); + assert.equal(mayStartRenderTag("ok \x3ca"), true); + assert.equal(mayStartRenderTag("ok \x3cac"), true); + assert.equal(mayStartRenderTag(`x ${LT}/ac`), true); + assert.equal(mayStartRenderTag(""), false); + assert.equal(mayStartRenderTag("plain text"), false); + assert.equal(mayStartRenderTag("a < b"), false); + assert.equal(mayStartRenderTag("x\x3caction y"), false); + assert.equal(mayStartRenderTag("\x3cdiv>"), false); +}); From abac2c2296168bc4f67e5c1302daff452ffb3a07 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Thu, 3 Sep 2026 01:28:10 +0800 Subject: [PATCH 12/22] release v0.1.72 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index aac35ef2bc..b3c4b0bd50 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "billion-context", - "version": "0.1.71", + "version": "0.1.72", "description": "Universal context-compression proxy for AI coding agents. Sits between any agent and its model API, rewriting Anthropic/OpenAI streams with acp-kernel compression. Any agent that can set a base URL (Claude Code, Codex, Cursor, Aider) works out of the box.", "type": "module", "main": "dist/index.js", From ab7cbe7c63a9dd18871b418ca281e28dd8606f49 Mon Sep 17 00:00:00 2001 From: Sisyphus Date: Thu, 3 Sep 2026 01:37:28 +0800 Subject: [PATCH 13/22] docs: retarget the sibling-text fix refs to #463 (the #462 refs point at the proxy-mode strip issue) --- src/plugin.ts | 2 +- tests/plugin-passthrough-tag-strip-chat.test.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/plugin.ts b/src/plugin.ts index 9992d68282..3bda979c0c 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -641,7 +641,7 @@ export async function pipePluginChatWithStrip( // Delta carried no visible text after stripping: drop the whole // chunk instead of forwarding an empty content delta. Only when // EVERY managed text field emptied out — a sibling field with real - // content must survive (#462). + // content must survive (#463). if (droppedText && !keptText && !hadTextOtherThanTextFields(rebuilt["choices"])) { return ""; } diff --git a/tests/plugin-passthrough-tag-strip-chat.test.ts b/tests/plugin-passthrough-tag-strip-chat.test.ts index 3442bcbe98..c47a12e539 100644 --- a/tests/plugin-passthrough-tag-strip-chat.test.ts +++ b/tests/plugin-passthrough-tag-strip-chat.test.ts @@ -130,7 +130,7 @@ test("plugin chat passthrough drops a chunk whose delta stripped to empty", asyn } }); -test("plugin chat passthrough keeps sibling text when one field is a pure tag echo (#462)", async () => { +test("plugin chat passthrough keeps sibling text when one field is a pure tag echo (#463)", async () => { const out: string[] = []; const res = makeRes(out); const session = makeSession(); @@ -138,13 +138,13 @@ test("plugin chat passthrough keeps sibling text when one field is a pure tag ec chatChunk({ content: "real answer", reasoning_content: `${TAG_OPEN}m00044${TAG_CLOSE}` }), DONE, ]; - await pipePluginChatWithStrip(streamOf(events), res, session, "openai"); + await pipePluginChatWithStrip(streamOf(events), res, "openai", session); const text = out.join(""); assert.ok(!text.includes("m00044"), "echo-only sibling field stripped"); assert.ok(text.includes("real answer"), "sibling field with real content must survive"); }); -test("plugin chat passthrough still drops a chunk where every managed field is a pure tag echo (#462)", async () => { +test("plugin chat passthrough still drops a chunk where every managed field is a pure tag echo (#463)", async () => { const out: string[] = []; const res = makeRes(out); const session = makeSession(); @@ -152,7 +152,7 @@ test("plugin chat passthrough still drops a chunk where every managed field is a chatChunk({ content: `${TAG_OPEN}m1${TAG_CLOSE}`, reasoning_content: `${TAG_OPEN}m2${TAG_CLOSE}` }), DONE, ]; - await pipePluginChatWithStrip(streamOf(events), res, session, "openai"); + await pipePluginChatWithStrip(streamOf(events), res, "openai", session); const text = out.join(""); assert.ok(!text.includes("m1") && !text.includes("m2"), "echoes stripped"); const dataLines = text.split("\n").filter((l) => l.startsWith("data:")).filter((l) => !l.includes("[DONE]")); From fe9b234dc0ad5ed8ef718c9e6d2016f8cd1fde30 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Thu, 3 Sep 2026 01:37:30 +0800 Subject: [PATCH 14/22] test: fix pipePluginChatWithStrip call order after #466 signature swap --- tests/plugin-passthrough-tag-strip-chat.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/plugin-passthrough-tag-strip-chat.test.ts b/tests/plugin-passthrough-tag-strip-chat.test.ts index 3442bcbe98..e87be49e85 100644 --- a/tests/plugin-passthrough-tag-strip-chat.test.ts +++ b/tests/plugin-passthrough-tag-strip-chat.test.ts @@ -138,7 +138,7 @@ test("plugin chat passthrough keeps sibling text when one field is a pure tag ec chatChunk({ content: "real answer", reasoning_content: `${TAG_OPEN}m00044${TAG_CLOSE}` }), DONE, ]; - await pipePluginChatWithStrip(streamOf(events), res, session, "openai"); + await pipePluginChatWithStrip(streamOf(events), res, "openai", session); const text = out.join(""); assert.ok(!text.includes("m00044"), "echo-only sibling field stripped"); assert.ok(text.includes("real answer"), "sibling field with real content must survive"); @@ -152,7 +152,7 @@ test("plugin chat passthrough still drops a chunk where every managed field is a chatChunk({ content: `${TAG_OPEN}m1${TAG_CLOSE}`, reasoning_content: `${TAG_OPEN}m2${TAG_CLOSE}` }), DONE, ]; - await pipePluginChatWithStrip(streamOf(events), res, session, "openai"); + await pipePluginChatWithStrip(streamOf(events), res, "openai", session); const text = out.join(""); assert.ok(!text.includes("m1") && !text.includes("m2"), "echoes stripped"); const dataLines = text.split("\n").filter((l) => l.startsWith("data:")).filter((l) => !l.includes("[DONE]")); @@ -179,7 +179,7 @@ test("plugin chat passthrough strips a tag streamed in tokenizer-sized fragments const whole = `ok ${TAG_OPEN}m00042${TAG_CLOSE} tail`; const micro = whole.match(/.{1,4}/g) ?? []; const events = [...micro.map((piece) => chatChunk({ content: piece })), DONE]; - await pipePluginChatWithStrip(streamOf(events), res, session, "openai"); + await pipePluginChatWithStrip(streamOf(events), res, "openai", session); const text = out.join(""); assert.ok(!text.includes("m00042"), "micro-fragmented tag never reassembles downstream"); assert.ok(!text.includes("\x3cacp") && !text.includes("\x3c/acp"), "no tag fragment survives"); @@ -207,7 +207,7 @@ test("plugin anthropic passthrough strips a tag streamed in tokenizer-sized frag ...micro.map((piece) => `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: piece } })}\n\n`), `event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn" } })}\n\n`, ]; - await pipePluginChatWithStrip(streamOf(events), res, session, "anthropic"); + await pipePluginChatWithStrip(streamOf(events), res, "anthropic", session); const text = out.join(""); assert.ok(!text.includes("m00042"), "micro-fragmented tag never reassembles downstream"); assert.ok(!text.includes("\x3cacp") && !text.includes("\x3c/acp"), "no tag fragment survives"); From 9883c6cfce6feaa137757df95fb71d26bdee71fd Mon Sep 17 00:00:00 2001 From: Sisyphus Date: Thu, 3 Sep 2026 01:37:46 +0800 Subject: [PATCH 15/22] fix(test): repair the swapped pipePluginChatWithStrip args left by the #469 merge pipePluginChatWithStrip is (stream, res, protocol, session?, log?). The two #468 micro-fragment tests were written before #466 reordered that signature and still pass (stream, res, session, "anthropic"), so protocol received a Session object, protocol === "anthropic" was false, and the anthropic fixture ran through processOpenai. The render tag survived and npm test fails on master at "plugin anthropic passthrough strips a tag streamed in tokenizer-sized fragments (#468)": actual: 'ok m00042 tail' expected: 'ok tail' Tests are not in tsconfig.build.json, so typecheck cannot see the swap. The openai twin passed only by accident: protocol === "anthropic" is false for a Session object, and the false branch is processOpenai. --- tests/plugin-passthrough-tag-strip-chat.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/plugin-passthrough-tag-strip-chat.test.ts b/tests/plugin-passthrough-tag-strip-chat.test.ts index c47a12e539..07079efdc9 100644 --- a/tests/plugin-passthrough-tag-strip-chat.test.ts +++ b/tests/plugin-passthrough-tag-strip-chat.test.ts @@ -179,7 +179,7 @@ test("plugin chat passthrough strips a tag streamed in tokenizer-sized fragments const whole = `ok ${TAG_OPEN}m00042${TAG_CLOSE} tail`; const micro = whole.match(/.{1,4}/g) ?? []; const events = [...micro.map((piece) => chatChunk({ content: piece })), DONE]; - await pipePluginChatWithStrip(streamOf(events), res, session, "openai"); + await pipePluginChatWithStrip(streamOf(events), res, "openai", session); const text = out.join(""); assert.ok(!text.includes("m00042"), "micro-fragmented tag never reassembles downstream"); assert.ok(!text.includes("\x3cacp") && !text.includes("\x3c/acp"), "no tag fragment survives"); @@ -207,7 +207,7 @@ test("plugin anthropic passthrough strips a tag streamed in tokenizer-sized frag ...micro.map((piece) => `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: piece } })}\n\n`), `event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn" } })}\n\n`, ]; - await pipePluginChatWithStrip(streamOf(events), res, session, "anthropic"); + await pipePluginChatWithStrip(streamOf(events), res, "anthropic", session); const text = out.join(""); assert.ok(!text.includes("m00042"), "micro-fragmented tag never reassembles downstream"); assert.ok(!text.includes("\x3cacp") && !text.includes("\x3c/acp"), "no tag fragment survives"); From 9895a8ba8809160b5cb3a33e958f8fade5942867 Mon Sep 17 00:00:00 2001 From: Sisyphus Date: Thu, 3 Sep 2026 01:40:12 +0800 Subject: [PATCH 16/22] fix(#460 residual): strip render-tag echoes on the paths that skip compress injection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #466 closed the non-injected chat SSE hole. Three siblings were still handing the model's echoed render tags back to the client untouched: - native Responses compaction (prepared.resetAfterSuccess) forwarded await pipeThrough(toClient, res) with no filter. A compaction turn is by definition the compression-triggered one, so its context necessarily carries render tags — its echoed prose is the likeliest leak of any Responses stream. - non-injected NON-STREAMING JSON fell to the bare pipeThrough at the end of forward(). The compress loop's JSON rewriters strip echoes on every injected round; the non-injected twin must not skip it. - rewriteResponsesJsonResponse (src/stream-responses.ts) was the only JSON rewriter without the strip: stream.ts strips content[].text, stream-openai.ts strips message.content, this wire did nothing, and its !converted early return previously returned a tag-bearing body verbatim. Fixes: - server.ts: the resetAfterSuccess branch pipes text/event-stream bodies through pipePluginResponsesWithStrip (no session, so usage accounting stays off); the ladder gains an application/json branch that runs pipePluginJson. - stream-responses.ts: probe JSON.stringify(b.output); when it carries a render tag, log a tag-echo warning and stripResponsesText(b) BEFORE the compress loop, so the synthesized compress note is never edited and the strip also covers the early return. - plugin.ts: pipePluginJson takes an optional session so proxy mode can reuse the plugin-mode pipe; usage accounting and markDirty stay behind `session &&`. Tests (tests/proxy-noinject-tag-strip.test.ts, tests/tag-echo.test.ts): native Responses compaction SSE stripped with response.completed intact, tag-free compact stream byte-identical, non-streaming openai/responses JSON stripped, tag-free JSON byte-identical, and the two rewriteResponsesJsonResponse cases — plain echo, plus the differential fixture proving the injected compress note survives while echoed sibling prose strips to the clean text. --- src/plugin.ts | 6 +- src/server.ts | 23 ++- src/stream-responses.ts | 12 ++ tests/proxy-noinject-tag-strip.test.ts | 194 ++++++++++++++++++++++++- tests/tag-echo.test.ts | 40 +++++ 5 files changed, 270 insertions(+), 5 deletions(-) diff --git a/src/plugin.ts b/src/plugin.ts index 3bda979c0c..634e2ed29b 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -875,9 +875,11 @@ function rebuildEvent(rawEvent: string, ev: Record): string { export async function pipePluginJson( stream: ReadableStream, res: import("node:http").ServerResponse, - session: Session, + session?: Session, protocol?: WireProtocol, ): Promise { + // Also serves proxy-mode JSON responses that skipped compress injection + // (#460 residual) — pass no session there so usage accounting stays off. const reader = stream.getReader(); const chunks: Buffer[] = []; for (;;) { @@ -890,7 +892,7 @@ export async function pipePluginJson( try { const json = JSON.parse(text) as Record; const usage = json["usage"] as Record | undefined; - if (usage) { + if (session && usage) { const input = num(usage["prompt_tokens"]) ?? num(usage["input_tokens"]); if (input !== undefined) { applyUsageSample(session, { diff --git a/src/server.ts b/src/server.ts index fb2f49bcd8..258ef74a3e 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2583,7 +2583,17 @@ async function forward( if (prepared && prepared.resetAfterSuccess) { const [toClient, toObserve] = upstream.body.tee(); const observed = observeResponsesTerminalState(toObserve, prepared.stream); - await pipeThrough(toClient, res); + const tagLog = (msg: string) => log("info", `[${prepared.session.id}] ${msg}`); + // #460 residual: a native compaction turn is by definition the + // compression-triggered one, so its context necessarily carries + // render tags — its echoed prose is the likeliest leak of any + // Responses stream. Same pipe as the non-injected branch below; + // no session, so usage accounting stays off. + if ((upstream.headers.get("content-type") ?? "").includes("text/event-stream")) { + await pipePluginResponsesWithStrip(toClient, res, undefined, tagLog); + } else { + await pipeThrough(toClient, res); + } clearUpstreamTimer(); const terminal = await observed; if (terminal === "completed") { @@ -2610,6 +2620,17 @@ async function forward( await pipePluginChatWithStrip(upstream.body as ReadableStream, res, p.protocol, undefined, tagLog); } clearUpstreamTimer(); + } else if ( + prepared && + (upstream.headers.get("content-type") ?? "").includes("application/json") + ) { + // #460 residual: the non-streaming twin of the branch above. The + // compress loop's JSON rewriters strip render tags from every round, + // so a non-injected JSON response must not hand the model's echoes + // back untouched. Same pipe as plugin mode; no session, so usage + // accounting stays off for the same reason as above. + await pipePluginJson(upstream.body as ReadableStream, res, undefined, prepared.protocol); + clearUpstreamTimer(); } else { await pipeThrough(upstream.body, res); clearUpstreamTimer(); diff --git a/src/stream-responses.ts b/src/stream-responses.ts index 5780249dae..ea7607d4c9 100644 --- a/src/stream-responses.ts +++ b/src/stream-responses.ts @@ -2,6 +2,7 @@ import type { CompressionCore, Config, CoreMessage } from "acp-kernel"; import type { Session } from "./session.js"; import { COMPRESS_TOOL_NAME, parseCompressInput } from "./compress-tool.js"; import { applyRanges, type RewriteCtx } from "./stream.js"; +import { containsRenderTagText, stripResponsesText } from "./loop/tag-echo-filter.js"; /** * Responses API (non-streaming) JSON rewriter: strips compress function_call @@ -17,6 +18,17 @@ export function rewriteResponsesJsonResponse(body: unknown, ctx: RewriteCtx): un status?: string; }; if (!Array.isArray(b.output)) return body; + const probe = JSON.stringify(b.output); + // #460 residual: the other two wires' JSON rewriters strip model-emitted + // render tags from prose (stream.ts on content[].text, stream-openai.ts on + // message.content); this wire must too. Strip the upstream items here, on + // every body — before the compress note is synthesized (so the record we + // inject is never edited) and before the !converted early return below + // (which previously handed a tag-bearing body back untouched). + if (containsRenderTagText(probe)) { + ctx.log(`[warn: tag echo] non-stream responses output contains { +async function startHarness(compress: { injectTool: boolean; injectNudge: boolean }, scripts: string[][], json: boolean): Promise { let call = 0; const upstream = http.createServer((req, res) => { req.on("data", () => {}); req.on("end", () => { - res.writeHead(200, { "content-type": "text/event-stream" }); + res.writeHead(200, { "content-type": json ? "application/json" : "text/event-stream" }); const script = scripts[Math.min(call, scripts.length - 1)]!; call += 1; for (const line of script) res.write(line); @@ -57,6 +57,7 @@ async function startHarness(compress: { injectTool: boolean; injectNudge: boolea routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "claude-test": { context: 400_000 }, "gpt-test": { context: 400_000 } } } }, modelContextLimit: 400_000, kernelConfig: defaultConfig(400_000), + promptCache: { routing: "auto" }, compress, sessionHeader: "x-acp-session", log: false, @@ -196,3 +197,192 @@ test("#460: openai title-gen (max_tokens<=200) SSE — echoed render tags stripp await h.close(); } }); + +function responsesSse(event: string, data: unknown): string { + return `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`; +} + +function chunkText(text: string, step: number): string[] { + const out: string[] = []; + for (let i = 0; i < text.length; i += step) out.push(text.slice(i, i + step)); + return out; +} + +function cleanResponsesDeltaText(raw: string): string { + let out = ""; + for (const line of raw.split("\n")) { + if (!line.startsWith("data:")) continue; + try { + const obj = JSON.parse(line.slice(5).trim()) as { type?: string; delta?: string }; + out += obj?.type === "response.output_text.delta" ? obj.delta ?? "" : ""; + } catch {} + } + return out; +} + +function tagEchoResponsesScript(): string[] { + const full = `压缩前 ${TAG("m00155")}${TAG("m00156", 33)}后 5 « 6 成立`; + const chunk = chunkText(full, 7); + let item = "item_ninj_r1"; + return [ + responsesSse("response.created", { type: "response.created", response: { id: "resp_ninj_r1", status: "in_progress", output: [] } }), + responsesSse("response.output_item.added", { type: "response.output_item.added", output_index: 0, item: { type: "message", role: "assistant", content: [] } }), + responsesSse("response.content_part.added", { type: "response.content_part.added", item_id: item, output_index: 0, part: { type: "output_text", text: "" } }), + ...chunk.map((p) => responsesSse("response.output_text.delta", { type: "response.output_text.delta", item_id: item, output_index: 0, delta: p })), + responsesSse("response.output_text.done", { type: "response.output_text.done", item_id: item, output_index: 0, text: full }), + responsesSse("response.content_part.done", { type: "response.content_part.done", item_id: item, output_index: 0, part: { type: "output_text", text: full } }), + responsesSse("response.output_item.done", { type: "response.output_item.done", output_index: 0, item: { type: "message", role: "assistant", content: [{ type: "output_text", text: full }] } }), + responsesSse("response.completed", { type: "response.completed", response: { id: "resp_ninj_r1", status: "completed", output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text: full }] }], usage: { input_tokens: 30, output_tokens: 9, total_tokens: 39 } } }), + ]; +} + +function cleanResponsesScript(): string[] { + const text = "plain prose 5 < 6 holds"; + return [ + responsesSse("response.created", { type: "response.created", response: { id: "resp_ninj_r2", status: "in_progress", output: [] } }), + responsesSse("response.output_item.added", { type: "response.output_item.added", output_index: 0, item: { type: "message", role: "assistant", content: [] } }), + responsesSse("response.output_text.delta", { type: "response.output_text.delta", item_id: "item_ninj_r2", output_index: 0, delta: "plain " }), + responsesSse("response.output_text.delta", { type: "response.output_text.delta", item_id: "item_ninj_r2", output_index: 0, delta: "prose 5 < 6 holds" }), + responsesSse("response.output_text.done", { type: "response.output_text.done", item_id: "item_ninj_r2", output_index: 0, text }), + responsesSse("response.output_item.done", { type: "response.output_item.done", output_index: 0, item: { type: "message", role: "assistant", content: [{ type: "output_text", text }] } }), + responsesSse("response.completed", { type: "response.completed", response: { id: "resp_ninj_r2", status: "completed", output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text }] }], usage: { input_tokens: 28, output_tokens: 5, total_tokens: 33 } } }), + ]; +} + +test("#460 residual: native Responses compaction (resetAfterSuccess) SSE — echoed render tags stripped, response.completed intact", async () => { + const h = await startHarness({ injectTool: true, injectNudge: true }, [tagEchoResponsesScript()]); + try { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/responses`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "noinject-resp-compact" }, + body: JSON.stringify({ model: "gpt-test", stream: true, session_id: "noinject-resp-compact", instructions: "You are a test agent.", input: [{ type: "message", role: "user", content: "hello" }, { type: "compaction_trigger" }] }), + }); + assert.equal(resp.status, 200, "native compact request must still forward to upstream"); + let raw = ""; + for await (const chunk of resp.body) raw += Buffer.from(chunk).toString("utf8"); + assert.equal(raw.includes(OPEN_MARK), false, "client stream leaked a render open tag"); + assert.equal(raw.includes(CLOSE_MARK), false, "client stream leaked a render close tag"); + assert.equal(cleanResponsesDeltaText(raw), "压缩前 后 5 « 6 成立", `unexpected client text: ${JSON.stringify(cleanResponsesDeltaText(raw))}`); + assert.ok(raw.includes("response.completed"), "terminal event missing"); + } finally { + await h.close(); + } +}); + +test("#460 residual: tag-free Responses compaction SSE — byte-identical passthrough", async () => { + const h = await startHarness({ injectTool: true, injectNudge: true }, [cleanResponsesScript()]); + try { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/responses`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "noinject-resp-clean" }, + body: JSON.stringify({ model: "gpt-test", stream: true, session_id: "noinject-resp-clean", instructions: "s", input: [{ type: "message", role: "user", content: "hello" }, { type: "compaction_trigger" }] }), + }); + assert.equal(resp.status, 200); + let raw = ""; + for await (const chunk of resp.body) raw += Buffer.from(chunk).toString("utf8"); + assert.equal(raw, cleanResponsesScript().join(""), "tag-free compact stream must pass through byte-identical"); + } finally { + await h.close(); + } +}); + +test("#460 residual: non-injected NON-STREAMING openai JSON — echoed render tags stripped, body stays valid JSON", async () => { + const full = `title: ${TAG("m00009", 12)}summary ok`; + const completion = { id: "chatcmpl_ninj_json", object: "chat.completion", created: 1, model: "gpt-test", choices: [{ index: 0, message: { role: "assistant", content: full }, finish_reason: "stop" }] }; + const h = await startHarness({ injectTool: true, injectNudge: true }, [[JSON.stringify(completion)]], true); + try { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "noinject-openai-json" }, + body: JSON.stringify({ model: "gpt-test", max_tokens: 100, stream: false, messages: [{ role: "user", content: "hello" }] }), + }); + assert.equal(resp.status, 200); + let bodyText = ""; + for await (const chunk of resp.body) bodyText += Buffer.from(chunk).toString("utf8"); + try { + const parsed = JSON.parse(bodyText) as { choices?: Array<{ message?: { content?: string } }> }; + assert.equal(parsed.choices?.[0]?.message?.content, "title: summary ok", `unexpected content: ${JSON.stringify(bodyText)}`); + } catch { + assert.ok(false, "client body is not valid JSON"); + } + assert.equal(bodyText.includes(OPEN_MARK), false, "client body leaked a render open tag"); + assert.equal(bodyText.includes(CLOSE_MARK), false, "client body leaked a render close tag"); + } finally { + await h.close(); + } +}); + +test("#460 residual: non-injected NON-STREAMING responses JSON — echoed render tags stripped from output items", async () => { + const full = `title: ${TAG("m00009", 12)}summary ok`; + const response = { id: "resp_ninj_json", status: "completed", output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text: full }] }] }; + const h = await startHarness({ injectTool: true, injectNudge: true }, [[JSON.stringify(response)]], true); + try { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/responses`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "noinject-resp-json" }, + body: JSON.stringify({ model: "gpt-test", stream: false, session_id: "noinject-resp-json", input: [{ type: "message", role: "user", content: "hello" }] }), + }); + assert.equal(resp.status, 200); + let bodyText = ""; + for await (const chunk of resp.body) bodyText += Buffer.from(chunk).toString("utf8"); + assert.equal(bodyText.includes(OPEN_MARK), false, "client body leaked a render open tag"); + let text: string | undefined; + try { + const parsed = JSON.parse(bodyText) as { output?: Array<{ content?: Array<{ text: string }> }> }; + text = parsed.output?.[0]?.content?.[0]?.text; + } catch {} + assert.equal(text, "title: summary ok", `unexpected body: ${JSON.stringify(bodyText)}`); + } finally { + await h.close(); + } +}); + +test("#460 residual: tag-free NON-STREAMING JSON — byte-identical passthrough", async () => { + const completion = { id: "chatcmpl_ninj_clean", object: "chat.completion", created: 1, model: "gpt-test", choices: [{ index: 0, message: { role: "assistant", content: "plain 5 < 6 summary" }, finish_reason: "stop" }] }; + const h = await startHarness({ injectTool: true, injectNudge: true }, [[JSON.stringify(completion)]], true); + try { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "noinject-openai-clean" }, + body: JSON.stringify({ model: "gpt-test", max_tokens: 100, stream: false, messages: [{ role: "user", content: "hello" }] }), + }); + assert.equal(resp.status, 200); + let bodyText = ""; + for await (const chunk of resp.body) bodyText += Buffer.from(chunk).toString("utf8"); + assert.equal(bodyText, JSON.stringify(completion), "tag-free JSON body must pass through byte-identical"); + } finally { + await h.close(); + } +}); + +function tagEchoOpenAiSplitScript(): string[] { + const full = `title: ${TAG("m00009", 12)}summary ok`; + const chunk = chunkText(full, 5); + const base = { id: "chatcmpl_split", object: "chat.completion.chunk", created: 1, model: "gpt-test" }; + return [ + openaiChunk({ ...base, choices: [{ index: 0, delta: { role: "assistant", content: "" } }] }), + ...chunk.map((p) => openaiChunk({ ...base, choices: [{ index: 0, delta: { content: p } }] })), + openaiChunk({ ...base, choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }), + "data: [DONE]\n\n", + ]; +} + +test("#468: openai SSE with the render OPEN tag split across chunks — stripped, not echoed", async () => { + const h = await startHarness({ injectTool: false, injectNudge: false }, [tagEchoOpenAiSplitScript()]); + try { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "noinject-openai-split" }, + body: JSON.stringify({ model: "gpt-test", max_tokens: 1024, stream: true, messages: [{ role: "user", content: "hello" }] }), + }); + assert.equal(resp.status, 200); + let raw = ""; + for await (const chunk of resp.body) raw += Buffer.from(chunk).toString("utf8"); + assert.equal(raw.includes(OPEN_MARK), false, "client stream leaked a render open tag"); + assert.equal(raw.includes(CLOSE_MARK), false, "client stream leaked a render close tag"); + assert.equal(cleanOpenAiContent(raw), "title: summary ok", `unexpected content: ${JSON.stringify(cleanOpenAiContent(raw))}`); + assert.ok(raw.includes("[DONE]"), "[DONE] missing"); + } finally { + await h.close(); + } +}); diff --git a/tests/tag-echo.test.ts b/tests/tag-echo.test.ts index 87cd628e8c..637177e0b9 100644 --- a/tests/tag-echo.test.ts +++ b/tests/tag-echo.test.ts @@ -7,6 +7,7 @@ import { runCompressLoop, createOpenaiAdapter, createAnthropicAdapter, createRes import { stripAcpTags, createTagEchoFilter, containsRenderTagText, containsToolCallXmlFragment, mayStartRenderTag } from "../src/loop/tag-echo-filter.ts"; import { rewriteJsonResponse } from "../src/stream.ts"; import { rewriteOpenaiJsonResponse } from "../src/stream-openai.ts"; +import { rewriteResponsesJsonResponse } from "../src/stream-responses.ts"; import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; const TAG = (ref: string, tokens = 177) => `\x3cacp tokens="${tokens}" type="text">${ref}\x3c/acp>`; @@ -295,6 +296,45 @@ test("non-stream openai rewriteOpenaiJsonResponse strips echoed tags", () => { assert.equal(parsed.choices[0].message.content, "answer done"); }); +test("non-stream responses rewriteResponsesJsonResponse strips echoed tags with no compress call (#460)", () => { + const body = { + id: "resp_1", + output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text: `answer ${TAG("m00155")} done` }] }], + status: "incomplete", + }; + const rewritten = rewriteResponsesJsonResponse(structuredClone(body), { core: createCore(), config: { modelContextLimit: 200000 } as Config, messages: [], session: makeCtx("ns-responses").session, log: () => {} }); + const parsed = rewritten as { output: Array<{ content: Array<{ text: string }> }> }; + assert.equal(parsed.output[0].content[0].text, "answer done"); + assert.equal(JSON.stringify(rewritten).includes(OPEN), false, "a render tag survived the responses JSON rewrite"); +}); + +test("non-stream responses rewriteResponsesJsonResponse strips sibling prose but never the compress note (#460)", () => { + const args = "{\"content\":[]}"; + const clean = { + id: "resp_c", + output: [ + { type: "message", role: "assistant", content: [{ type: "output_text", text: "plain prose" }] }, + { type: "function_call", name: "compress", call_id: "c1", arguments: args }, + ], + }; + const echoed = structuredClone(clean) as { output: Array<{ content: Array<{ text: string }> }> }; + echoed.output[0].content[0].text = `plain prose${TAG("m00155")}`; + const cleanOut = rewriteResponsesJsonResponse(structuredClone(clean), { core: createCore(), config: { modelContextLimit: 200000 } as Config, messages: [], session: makeCtx("ns-note-clean").session, log: () => {} }); + const echoOut = rewriteResponsesJsonResponse(echoed, { core: createCore(), config: { modelContextLimit: 200000 } as Config, messages: [], session: makeCtx("ns-note-echo").session, log: () => {} }); + const note = (r: unknown) => { + const o = (r as { output?: Array<{ content?: Array<{ text: string }> }> }).output ?? []; + return o[0]?.content?.[0]?.text ?? ""; + }; + const prose = (r: unknown) => { + const o = (r as { output?: Array<{ content?: Array<{ text: string }> }> }).output ?? []; + return o[1]?.content?.[0]?.text ?? ""; + }; + assert.equal(note(cleanOut), note(echoOut), "the synthesized compress record must not be edited by the strip"); + assert.equal(prose(cleanOut), prose(echoOut), "echoed sibling prose must be stripped to the same text as clean prose"); + assert.ok(note(echoOut).length > 0, "no note was injected at all"); + assert.equal(JSON.stringify(echoOut).includes(OPEN), false, "no render tag reached the client body"); +}); + test("non-stream rewriters leave tag-free text untouched", async () => { const anthropicBody = { id: "m", content: [{ type: "text", text: "clean 5 < 6 text" }], usage: { input_tokens: 1, output_tokens: 1 } }; const cc = makeCtx("ns-clean"); From 6e9092b7a45f6b382381d0416e0e3ebb257ae67b Mon Sep 17 00:00:00 2001 From: Sisyphus Date: Thu, 3 Sep 2026 01:40:30 +0800 Subject: [PATCH 17/22] refactor: drop the dead SSE rewriters that kept causing mis-triage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit src/stream.ts rewriteSseStream and src/stream-openai.ts rewriteOpenaiSseStream have zero production call sites: server.ts imported rewriteSseStream but never called it, and the live proxy SSE path is runCompressLoop + pickAdapter (src/server.ts), whose three adapters already pass text through createTagEchoFilter. They survived only because five test files drove them directly, which is why #460 and #462 both tripped over them — the cited "warn-only, then emitEvent(ev)" code is unreachable. Removed: rewriteSseStream and its private machinery (routeEvent, emitToolReplacement, buildStopReasonRewrite, parseSseEvent, emitEvent, BlockState, NOOP, SseEvent) plus the now-unused normalizeSseLineEndings import; rewriteOpenaiSseStream and its private machinery (newState, routeOpenaiEvent, buildOpenaiTail, extractDataLine, StreamState). Kept: executeAnthropicProxyTool (used by rewriteJsonResponse), applyRanges (preflight, compress-loop-responses, loop/core, stream-openai), refNum, type RewriteCtx, and both JSON rewriters — all live. Tests: deleted the five suites that existed only to feed the dead rewriters (tests/proxy-anthropic-stream, tests/proxy-sse, tests/sse-fixtures — the latter two were near-identical copies — and the two "OpenAI SSE rewriter" cases in tests/basic and tests/proxy-basic) plus the tests/fixtures/*.sse payloads they alone consumed. Flagged in the PR: fixture-level OpenAI passthrough-integrity coverage was bound to the removed implementation; the live buffer-to-finish adapters carry their own coverage in tests/loop-adapters.test.ts. Verification: typecheck clean; npm test GREEN apart from the two known env failures. Four gaps remained after #460/#466, all on proxy-mode paths that bypass the compress loop: - G1: native Responses compaction turns (resetAfterSuccess) still pipeThrough UNSTRIPPED. That turn is by definition the compression-triggered one, so its context necessarily carries render tags — highest echo risk of any Responses stream. Now routed through pipePluginResponsesWithStrip when the upstream answers text/event-stream. - G2: rewriteResponsesJsonResponse (src/stream-responses.ts) never stripped render tags, while the anthropic/openai JSON rewriters do. stripResponsesText now runs before the compress loop, so the synthesized note is never edited and the no-compress early return is covered. stripItemContent also learned to strip a top-level item text, not only content[].text. - G3: non-injected NON-STREAMING JSON (title-gen, ACP_NO_INJECT_TOOL, #353 bypass) still pipeThrough raw. Now routed through pipePluginJson, whose session became optional so usage accounting stays off — a tiny title-gen input_tokens would otherwise clobber lastInputTokens through the last-wins mergeUsageSample and break compression triggering. - fast-path hole (#460 family, not recorded anywhere yet): the plugin passthrough pipes skip the tag filter when a chunk holds no visible tag and nothing is pending. A render tag whose OPENING straddles a chunk boundary — chunk one ends `...m00155` — matches no detector on any single chunk, so the filter was never fed, never went pending, and the whole tag echoed to the client. endsPartialTag keeps a chunk-ending tag-like tail out of the fast path; the filter holds it and re-emits on the next push or at flush(), so prose is neither delayed nor lost. Verification: typecheck clean; npm test 906/908 (the two failures are the known network/launcher env cases, identical on master); E2E codex core 4-phase and the E2E_FORGE=1 native-compact phase both GREEN against the local sglang upstream. PR #464 shipped the mixed-chunk fix but referenced #462 everywhere (test names, code comment); the bug it fixes is #463. Also fixes the two test call sites that kept the pre-reorder pipePluginChatWithStrip argument order once #466's signature landed. plugin-mode openai-chat SSE strip (pipePluginChatWithStrip/processOpenai) latched droppedText per-field, so a chunk whose delta carried e.g. content='real answer' plus a pure render-tag echo in reasoning_content was dropped whole, losing the real content. Now the chunk is dropped only when EVERY managed text field strips to empty (keptText guard); the existing non-text payload check (finish_reason/tool_calls) is unchanged. Requests that skip compress injection (OpenAI title-gen exclusion, ACP_NO_INJECT_TOOL, #353 stop_sequences classifier bypass) piped upstream SSE straight through to the client. A model echoing history-borne ACP render tags into prose leaked them to the client TUI, where they re-enter the conversation via replay and amplify (same mechanism as #457/#459). Route those non-injected streaming responses through the same tag-strip pipes as plugin mode (#452), with no session passed: usage accounting must stay off here, or a title-gen call's tiny input_tokens would clobber lastInputTokens and break compression triggering for the main conversation. - pipePluginChatWithStrip: session now optional (signature reordered protocol-before-session); usage sample applied only when a session is present - pipePluginResponsesWithStrip: session now optional, same guard - server.ts !useRewriter branch: strip pipe for streaming requests whose upstream response is text/event-stream; everything else stays raw pipeThrough as before - tests/proxy-noinject-tag-strip.test.ts: anthropic injectTool:false echo split across deltas stripped; tag-free stream byte-identical; openai title-gen stream stripped with [DONE]/finish_reason intact release v0.1.71 fix: strip plugin-mode render-tag echoes on chat-completions/anthropic passthrough (#206 parity) release v0.1.70 fix(#451): restore plugin-mode nudge injection + honest diagNudge log fix(#422): refresh post-compress re-request view so the model sees the fold 0.1.69 (225ceb4) gated all three nudge injection sites by !pluginMode on the premise that "in plugin mode the agent owns compression". The agent plugins (pi/omp) are pure protocol clients with NO nudge channel of their own, so the gate silently disabled proactive compression in plugin mode — leaving only preflight at the hard limit — while diagNudge kept logging "INJECT" (the log lied about delivery, same class as #413). - Drop !pluginMode from the anthropic/openai/responses nudge gates. The nudge is an ephemeral trailing user message (not persisted, never enters the agent's re-sent history, prefix-cache-anchor safe), so it belongs in BOTH modes. Tool injection stays gated by !pluginMode (the agent provides tools). - diagNudge now takes the actual willInject decision and prints INJECT only when the nudge truly reaches the upstream payload, else ARMED-SUPPRESSED. - README two-modes table: split the tools/nudge row (tools = none in plugin mode, nudge = yes in both modes). - tests/plugin-nudge-injection.test.ts: regression guard asserting the nudge is a trailing user message in BOTH proxy and plugin mode when armed (verified to fail 30!==31 against the old gate). typecheck / test(901) / build green. release v0.1.69 fix(#437): make upstream timeout idle-based so long healthy streams aren't truncated fetchWithTimeout used a single TOTAL timer (UPSTREAM_TIMEOUT_MS = 10min) that only cleared once the body was fully consumed. A healthy stream that legitimately runs longer than 10 minutes (e.g. a DeepSeek generation via bili dsh) was aborted at the 10-minute mark mid-stream: the for-await loop threw, emitStreamError wrote 'stream error' + finish, and the client saw the conversation truncated. Re-arm the timer on every response-body chunk (via an idle-wrapped body), so the timeout is IDLE-based during streaming: healthy long streams survive, while a genuinely stuck stream (no chunk for timeoutMs) still trips the abort. Time-to-first-byte bounding is unchanged. clearTimer/externalSignal semantics are preserved and callers see an identical Response shape (status/headers/body). fix(#429): openai chat — write merged terminal chunk once, don't leak finish_reason Two independent defects in the openai adapter's parseStream, both reachable only when the upstream merges delta.content and finish_reason into the SAME terminal chunk (MiniMax-M3, kimi-k3, ...). The client's check is unconditional: any chunk carrying content after finish_reason is set throws "OpenAI Chat received content after the finish reason". Defect 1 (single round, real tool call): the `if (finishReason)` block yielded the raw chunk as meta but did not `continue`, so control fell through to the text branch and the SAME bytes were written a second time. Add the `continue`. Defect 2 (multi round, proxy tool): a round that only called proxy tools is not terminal (core.ts reRequest) and emits no completion, but its merged chunk still reached the wire carrying finish_reason, so round 2's text landed after the finish. Invariant enforced: a verbatim passthrough chunk must never carry finish_reason and must be written at most once; the sole authoritative completion is the final round's emitCompletion — except a real-tool-call round (suppressCompletion), where the verbatim terminal chunk IS the completion and keeps finish_reason, written once. - stripFinishReasonChunk(): blank finish_reason on a passthrough chunk - reasoning/text/meta branches: strip when the chunk carries finish_reason - sawRealToolCall branch: keep verbatim + continue (no double-write) Tests: tests/openai-finish-reason-dup.test.ts (4 cases: both defects + text-only and reasoning regression guards). Full suite 896/0, typecheck + build clean. fix(#377): re-voice mid-stream acp_summary as user messages (user-carrier) README "Two compression modes" table: add a row for proxy-injected tools + nudge (plugin mode: none; proxy mode: the 4 context tools + nudge). AGENTS.md item 6: "suppresses tool injection" -> "suppresses tool + nudge injection". The nudge (a trailing user message prompting compression) was not gated by !pluginMode, so in plugin mode the proxy could inject a redundant nudge even though the agent owns compression (its own overflow self-heal). Gate all three nudge injection sites (anthropic/openai/responses) by !pluginMode, matching the injectTools gating, so plugin mode is fully clean: no tool injection, no nudge injection, summary carrier = the agent's own compress tool call. Replace hoistMidSystemMessages (hoisted mid-stream system messages to the head as separate system messages) with systemToUser (converts them to role "user", leaving them at their anchor position). Why: strict OpenAI-compatible backends (SGLang) require exactly ONE system message at index 0 and reject a mid-conversation or second system message with 400 "System message must be at the beginning" (#377). The kernel renders each compressed block's summary as role "system" anchored mid-conversation; in proxy mode (plain client) the compress tool call is ephemeral and preflight blocks have none, so the acp_summary is the only carrier. Re-voicing it as a "user" message satisfies SGLang's one-system rule AND keeps the head system message (the prefix-cache anchor) byte-stable across compress turns — a new block no longer invalidates the whole-conversation prefix. In plugin/launcher mode the summary carrier is the compress tool call (in the agent's own re-sent history), so the kernel's acp_summary is stripped by stripKernelSummaries and systemToUser is a no-op there. - src/util.ts: hoistMidSystemMessages -> systemToUser - src/server.ts, src/loop/adapter-openai.ts: call sites + two-mode comments - README.md: new "Two compression modes" section (carriers, why they differ, why user not system/tool-call, coexistence determination) - AGENTS.md: Key Design Decisions item 6 (two modes) + Before Making Changes item 4 (every wire-touching change must be reasoned in BOTH modes) - tests: new system-to-user.test.ts (7 tests), updated e2e-mid-system.test.ts (summaries must ride on user messages, not leak into system), deleted hoist-mid-system.test.ts release v0.1.68 fix: omp plugin install/remove idempotency (scoped match, existence check, overlay redirect) fix: stateless decompress — pure retrieval, no deactivate, cache retained (#400) fix: /bili/ tunnel destination admission + management-plane tunnel block (#409) The /bili/ zero-config branch forwarded to ANY destination with no host/scheme admission — including the proxy's own /__bili/ management plane (the tunnel's inner connection originates from the proxy, so the loopback admin gate passes; PoC in #409), link-local metadata addresses, and, on --host 0.0.0.0 deployments, anything reachable from this machine: a LAN peer could PUT /__bili/config over the tunnel and rewrite upstream routing to exfiltrate API keys, despite the 'management stays loopback-only' promise. The CONNECT side already had its gates (mitm.ts) — this adds the /bili/ counterpart: - src/tunnel-guard.ts: destination × client admission. Self (same port on any local address incl. 127/8) and link-local/metadata (literal or via DNS resolution of the destination name) are ALWAYS denied. Loopback/ private destinations pass for loopback clients (the self-hosted upstream case the launcher httpRewrites flow depends on: sglang 127.0.0.1:8199, ollama 11434) and are denied for remote clients unless listed in BILI_TUNNEL_ALLOWED_HOSTS (host or host:port). Public passes. - forward() stamps x-bili-tunnel: 1 on every tunnel forward; /__bili/* rejects any request carrying it — the management plane stays unreachable through the tunnel even when self-detection misses (NAT hairpin, DNS-rebinding race between check and connect, chained bili). - Startup logs the allowlist when set (the #405 silent-knob lesson). Verified: typecheck, 891/891 tests (10 new), build; integration tests reproduce the #409 PoC (403) and confirm 169.254.169.254 is denied before any socket, local loopback upstreams still proxy, and the marker reaches the upstream. feat: instance governance — attach, liveness, handshake, graceful exit, atomic state (#417) CI caught the stale assertion: win32 stopProxy is intentionally a no-op (child.kill is TerminateProcess, zero flush); the test now pins that contract on both platforms instead of assuming POSIX behavior. L1 identity: proxy-origin file upgraded to JSON {origin, instanceId, pid, startedAt, config fingerprint, launchToken} with atomic writes and legacy plain-string read compatibility; /__bili/health echoes instanceId/pid/startedAt (#403 read side). L2 attach-or-spawn (#394): ensureProxyRunning attaches to a healthy, config-compatible instance instead of doubling writers over one sessions dir; stateDir instances.json registry prunes dead pids and warns loudly on dual live instances. L3 handshake (#407): no more probe-release-rebind TOCTOU — the child self- binds, retries EADDRINUSE (+1 up to 16, then ephemeral) when launched with BILI_LAUNCH_TOKEN, and reports the real origin via the instance file; forced --debug on spawned proxies removed (12MB/day logs). L4 graceful exit (#414): launcher children watch BILI_PARENT_PID and run the flush path when the parent dies (Windows TerminateProcess never flushed); stopProxy is a no-op kill on win32 (the watcher owns shutdown) and leaves attached instances alone. L5 state hygiene (#406, #403 write side): plugin-conversations.json gets tmp+rename atomic writes, a dirty flag (dying paths no longer rewrite the table), and corrupt-byte preservation + warning instead of silent zeroing; plugin install refuses to freeze dead/unverifiable origins into client configs (BILI_MCP_PROXY still wins). L6 overlay antidote (#410): mergeOverlayEntry threads the generatedFile exclusion through directory recursion (nested models.yml can no longer be promoted into the real home); dead proxy URLs baked into a real models.yml are unpacked mechanically on launch (live origins kept). Three defects in the omp plugin idempotency checks (issue #392): 1. Whole-file, unscoped line matching: install/remove/status compared every line of config.yml against the entry value, so a same-valued line under any other key was counted as installed (blocking a needed install) or deleted (corrupting unrelated config). Matching is now scoped to the top-level extensions: block via ompExtensionItemLines(). 2. No existence check, disagreeing with the loader: the launcher's ompPluginLoadedFrom() uses a suffix match + fs.existsSync, but install/remove used exact-string compare with no existence check. A stale entry (target file gone) was reported 'installed' while the launcher treated it as unloaded. All four paths now share OMP_ENTRY_RE + ompBlockLoaded(); status additionally reports 'broken' for a stale entry. 3. PI_CODING_AGENT_DIR drift to the overlay: inside a 'bili omp' shell the env points at the -bili overlay whose config.yml can be a stale copy (merge conflict leaves a .bili-conflict). ompConfigFile() now detects the -bili overlay, redirects to the real home, and prints a note about which file was actually touched. Tests: existing roundtrip fixtures now materialize the gitignored dist artifact (the existence check needs it); new test covers all four acceptance criteria (out-of-block line doesn't block install, remove preserves out-of-block lines, overlay redirect leaves overlay untouched, stale entry reports 'broken'). release v0.1.67 fix: Windows overlay symlink EPERM fallback + fail-loudly + SQLite set merge Unprivileged Windows processes lack SeCreateSymbolicLinkPrivilege, so fs.symlinkSync throws EPERM and refreshOverlayHome's bare catch{} swallowed every failure while still returning true — the bili omp/pi/hermes/dsh overlay ended up hollow (only the generated config + pid marker, zero links), so the client read an empty home and launched fresh/unconfigured every time (the Windows root cause of #289). - linkOverlayEntry: on EPERM/EACCES/EINVAL fall back per entry type — dir -> symlink 'junction' (no privilege needed), file -> hardlink (same volume) -> copy. The overlay now works without Developer Mode. - refreshOverlayHome: track link success/failure per entry; when the overlay comes out hollow (no entry resolvable) log a loud warning and return false so the launcher aborts instead of pointing the client at an empty home. Partial failures are reported, not silently dropped. - mergeSqliteSet: move the SQLite three-piece set (.db/.db-wal/.db-shm) as a unit with two-phase rollback — a locked/half-migrated set can no longer corrupt the real home db. - A write-through hardlink (nlink>1, same dev+ino as the real file) is re-pointed on refresh, never merged back (data-loss guard). Tests: 6 new cases in tests/launcher.test.ts simulate the Windows EPERM condition on Linux by denying fs.symlinkSync for non-junction links. typecheck / test (868) / build all green. release v0.1.66 feat: surface compress rejection reasons to the model (#362 ③) + acp-kernel 0.0.47 - parseCompressInput now returns {ranges, diagnostics} so callers can see WHY entries were dropped (kernel 0.0.47 invalidReasons) - applyRanges failure receipt includes kind + dropped count + per-entry reasons (max 8, 200 chars each) — the model sees exactly which entry was rejected and why, instead of blind-retrying the same bad shape - bump acp-kernel to 0.0.47 (persist retry backoff + spill files + tmp-name-per-attempt; exact pin per AGENTS.md) - .gitignore: tmp/ (e2e sandbox dirs leaked into a commit earlier today) fix: warn on tool-call XML fragments + close-side truncated acp echo (#361) fix: surface raw-dump failures and rate-limit telemetry path noise (#362) Detect the non-acp tool-call XML close-fragment family (, , , antml: namespace, JSON-escaped forms) that a model may echo from the context — the same source as acp tag echo that produces a "fake completion" (tool call written as text, no tool_use block). Warn-only, NOT stripped: these fragments are indistinguishable from legitimate prose discussing tool-call code (see #295 review), so stripping would corrupt real content. A once-per-stream warn in the server streaming loop makes the leak attributable in bili.log. Also: close-side truncated acp tag handling (TRUNC_CLOSE) mirroring TRUNC_OPEN; fix dangling RENDER_TAG_DETECT reference (regression); tests. A varying query (?ts=...) would split one endpoint into unbounded keys, defeating the suppression and growing the map without bound. - Raw dumps (INCOMING/REQ/RES/SSE) that fail now log a rate-limited warn (first failure immediate, repeats suppressed within a 60s window, cumulative count) instead of failing silently. A stopped raw/ dump (disk full, locked dir) was previously invisible. - The 'unrecognized path' telemetry warn (client event-report endpoints forwarded unchanged) is now rate-limited per path (first 3 at warn, one 'suppressed' notice, then silent) instead of logging every hit (~20k lines in one user's capture). - No change to forwarding behavior; observability only. typecheck clean; 845 tests pass; build success. release v0.1.65 fix: omp pck stamp for chat-completions with max_tokens; anthropic wire consumes + strips pck (#268) fix: 404 unknown /__bili/ paths + actionable MITM CA-rejection warning (#346) fix: actionable no-valid-ranges compress error + defensive object tool args fix: hoist mid-conversation system messages to the leading prefix on the OpenAI wire (#355) fix: pass through Claude auto-mode classifier requests untouched (#353) docs: align per-model context declaration precedence with actual resolution order fix: per-route model context declaration outranks the models.dev registry (#344) fix: show real CA path in web UI + Windows ~ expansion doc note (#342) release v0.1.64 fix: default upstream proxy to direct; add gost-style -F forward flag (#346) Address the two non-blocking review nits on #357: - help/PR copy advertised gost's --forward but only -F is implemented (per the 'only -F for now' decision); drop --forward from the help text. - CHANGELOG/PR claimed 'config' proxy wins regardless of mode, but a top-level proxy config still requires mode auto/manual (pre-existing); correct the claim to only -F / BILI_UPSTREAM_PROXY. - Unset upstreamProxyMode now resolves to "direct" (matches the web UI's 直连(默认) and ZCode's default) instead of falling through to env/system proxy auto-detection. Set mode "auto" to follow the system/env proxy; an explicit proxy (-F / BILI_UPSTREAM_PROXY / config) still wins regardless. - Add `-F ` (gost-style --forward) to set the upstream proxy for a run; works for launcher cmds (before the client name) and `start`. HTTP/HTTPS only for now (SOCKS5 is a follow-up). - Log the resolved upstream-proxy decision once per unique (host, proxy, source) — `[upstream-proxy] via (source=)` — with non-public hosts masked (#255), so a silently-picked env proxy is visible. - Fix the misleading `bili pi --mitm-domain ...` help example (that form forwarded the flag to the client; the working form is `bili --mitm-domain ... pi`). Tests: default-direct (unset mode ignores HTTPS_PROXY), auto-mode opt-in still follows the env proxy, and parseArgs -F placement (before/after client, start cmd, composition). CONFIGURATION.md claimed the per-model context declaration is primary and the models.dev registry is only a fallback for undeclared models. The code (src/server.ts native-window resolution) actually checks the WARM registry (peekWindow) BEFORE the per-model declaration (configuredWindow), so for a relay/private host whose model is listed in the registry, the registry value silently outranks the user's declaration. This mismatch is what made the #351 user's 200K declaration get ignored in favor of the registry's 1M. Document the real resolution order and steer users to compress.modelContextLimit (highest-priority source, always wins) for reliably pinning the effective window. Behavior-preserving (docs only). #349: when the model emits an empty compress call ({} -> missing-content) the proxy returned 'Check your startId/endId parameters' — misleading, since the real problem is no content at all. Replace with an actionable message that tells the model to re-issue with a non-empty content array so it can self-correct on the re-request round. Also harden the OpenAI adapter: fn.arguments was only accepted as a string; a non-standard upstream emitting it as an object was silently dropped (-> same missing-content failure). JSON.stringify object args now. typecheck clean; 824/824 tests pass (incl. new #349 regression); build ok. The CONNECT e2e suite covered the MITM happy path, the idle-tunnel timeout, and the remote policy, but never a blind tunnel to a non-whitelisted host passing bytes both ways — the exact path ZCode's official site (zcode.z.ai) uses. Add it: a local TCP server stands in for the upstream, and the test asserts the round-trip plus the new 'tunnel ... established (blind TCP)' log. Confirms the tunnel itself is sound, so the #346 zcode.z.ai failure is on the ZCode/network side, not a bili tunnel bug. tunnelThrough only logged on connect-timeout/failure/close, so a tunneled host (e.g. ZCode's official site zcode.z.ai) that connects but then fails at a higher layer left the log completely blind. Log the established tunnel, mirroring doMitm's 'tunnel established' line but distinguishable (blind TCP, not decrypted). test: widen e2e mock pacing to keep streaming-span margin on slow CI runners fix: generate real changelog in GitHub Releases via generate_release_notes release v0.1.63 feat: codex compact family — combined #322/#323/#324/#325/#317/#319 + UA unification codex 0.147 ships MCP tools to the model as a namespace tool type; self-hosted servers (sglang/vllm/ollama/llama.cpp) do not parse it, so the injected bili MCP tools become silently invisible and the model fumbles for them. Users had no way to discover BILI_LAUNCHER_PLUGIN=0. launcherInjectMcp now falls back to wire-injected flat tools when the codex upstream host is loopback/RFC1918/ULA/link-local/.local and BILI_LAUNCHER_PLUGIN is unset; BILI_LAUNCHER_PLUGIN=1 forces plugin mode. claude is unaffected (it flattens MCP tools itself). ework's pass (656f90d..ccd7036) and my pass (d4fe1ba) fixed the same findings concurrently. This reconciles them, best mechanism per finding: - MAJOR (summary loss after trigger-form forge): hybrid. The echoed fc_bili_ item is REPLACED by a summary-carrying user message (history-borne: kernel-compressible, kept by codex's user-message retention) — and ework's forge-time capture into session.metadata.codexForgedSummaries stays as the fallback: when the echo does NOT come back, the captured summaries are re-injected into the developer message. Exactly one path is active per turn (no duplication), and neither an echoed-item loss nor unbounded permanent injection can drop or bloat the handoff. - tail-window cross-merge: keep ework's offset-0 guard (a head-to-head match is never a truncation reattach) over the byte-size heuristic. - endpoint compact: strip-on-all-paths (ework) + all-or-nothing state commit with restore on gate-fail/rebuild-failure/error (mine). - codexTurnIdentity: validated 'user' turns fall through to the legacy precedence chain (stronger headers may own the id). - pending plugin register can no longer be claimed by a subagent thread. - codex window clamp exempts an operator's explicit compress.modelContextLimit (operator tuning is never floored/clamped). - trigger-form forge honors parsed.stream (JSON when not streaming) and mints usage from max(lastInputTokens, payload estimate, 1). - isCompactionTrigger reuses hasCompactionTrigger. 823/823 unit tests; full e2e incl. forge + pre-forge deep recall pass against local sglang. prepareResponsesCompact mutated state and then returned the original (unstripped) body on gate-fail/catch — the echoed fc_bili_ item reached upstream. Strip before the early return so pass-mode, gate-fail, and catch paths all forward a clean body. Tail-window reattach now excludes offset 0: a head-to-head match is either a full-prefix case (step 1) or a distinct conversation sharing a templated head — never a truncation reattach (the retained suffix of a truncation starts strictly inside the stored chain). Closes the cross-merge of two anonymous conversations with identical templated heads. The trigger-form forge put block summaries into the opaque compaction item, but the kernel deactivates blocks whose messages vanish from the truncated replay (syncBlocks keeps a block only while one of its effectiveMessageIds or its acp_summary_ message is present) — so everything that lived only in compressed blocks disappeared from the model's view on the first post-forge turn. Fix: capture the active blocks' summaries in the kernel's render format at forge time and at echo-strip time (upgrade path for sessions forged by an older build) into session.metadata.codexForgedSummaries (persisted, exact-text dedup), and re-inject them into the developer message on every /responses turn. Tests: unit coverage for renderForgedSummary/mergeForgedSummaries; mock-upstream integration test asserting the post-forge turn strips the echoed fc_bili_ item and re-injects the summary as a developer message; e2e forge phase now also recalls a sentinel that was compressed pre-forge (archive #6, not in codex's retained tail). # Conflicts: # src/server.ts Model: glm-5.3 — test: e2e suite — real codex through bili (warmup/load/compress/purity/forge) fix: warn when running process is stale relative to on-disk install (#327) The /bili/ prefix section had no Claude Code entry. Add it (claude's undici fetch ignores HTTPS_PROXY, so the /bili/ URL form via ANTHROPIC_BASE_URL is the only manual option) plus a note that manual mode must set CLAUDE_CODE_AUTO_COMPACT_WINDOW itself — the bili claude launcher does this automatically (PR-D) but manual /bili/ users do not get the injection. claude clamps the value down to its perceived model window, so over-setting is safe. EN + zh-CN. When BILI_CODEX_COMPACT=intercept, the client is codex (UA codex_cli_rs/), and the ACP state is healthy (transform ok + steady-state < 90% of window + an active block to hand off), bili forges a success response for codex's native compaction requests and never contacts upstream — a deterministic handoff to the ACP state: - trigger form (input ends with compaction_trigger): forged 2-frame SSE (response.output_item.done with one {type:compaction, id:fc_bili_*, encrypted_content:bili:acp:} + response.completed with honest usage). Matches codex's collect_compaction_output contract (exactly one compaction item + response.completed; all other events ignored). - endpoint form (POST /responses/compact): forged JSON {output:[...]} where output is the rebuilt history (recent items + ACP summary), matching codex's CompactHistoryResponse parser. Otherwise (kill-switch off by default, ACP not keeping up >= 90%, or nothing compressed yet) the request passes through to upstream and native compaction backstops (rebased by the PR-C semantic gate). Bili-generated compaction items (fc_bili_ prefix / bili:acp: sentinel) are stripped from subsequent requests so codex's echoed item does not double-count. Pre-flight: typecheck clean; 752/752 tests (7 unit + 5 e2e new); build ok; forge code verified inlined in dist. The rebase marker fired on ANY 2xx upstream response, so a failed, incomplete, or truncated compaction stream scheduled a rebase on top of a compaction that never happened (the #249 false-rebase bug): bili discarded valid blocks and mis-tracked the ledger. - new src/stream-terminal.ts: terminal-state observer for Responses responses. SSE: response.completed -> completed, response.failed / response.incomplete -> failed, no terminal event before EOF -> unknown. JSON: output array -> completed, error field -> failed, unparseable / empty / oversized -> unknown. unknown is the safe default (no rebase; next request self-heals via kernel message-id deactivation). - server.ts: both markNativeCompactionBoundary call sites now gate on observed terminal === completed. 2xx empty body no longer rebases. - trigger-path hardening in prepareResponses: a trailing compaction_trigger request is detected BEFORE injection — no compress system prompt, no compress tools, renderTags none, compressInjected false (response flows through plain passthrough instead of the rewriter), and resetAfterSuccess is set so its terminal state gates the rebase. Nudge skip now keys off the same pre-injection detection. - tests: 16 unit tests for the observer + e2e through the proxy covering completed / failed / truncated / 2xx-error-JSON / empty-body / JSON-output and the clean-forward assertions; finality-test control moved to a separate session (a completed trigger now rebases the compacting session, legitimately disarming its nudge). Tail-pinned matching only reattached when the client retained <= TAIL_WINDOW (8) items; a rolling-window client keeping 20-30 items orphaned on every slide. Search the incoming's leading window across the stored chain's per-item hashes at any offset (<= 57 offsets x 8 compares, trivial). MAX_STORED_ITEMS 64 -> 128 so deeper windows stay searchable. guardian_review / guardian_classifier (Feature string) / memory_consolidation threads previously fell back to the legacy chain and landed on the root session — the exact #150 isolation violation. Invert the discrimination: only "user" joins the root session; every other thread_source with a valid thread_id cross-check partitions by thread-id. A client that drops its oldest messages (truncation) previously failed the full-depth prefix match and was orphaned into a fresh pfa- session, stranding the old session's compression blocks. resolve() now falls back to a tail-window comparison: the incoming's leading items are matched against each stored chain's trailing items (position-independent per-item hashes), so a truncated replay reattaches its original session and the kernel reconciles by message id (verified: blocks deactivate gracefully on truncation and revive on full replay, no throw). New pfa- sessions also record a best-effort lineage (discarded candidates + reason truncated/forked) in session metadata for UI/debug only — never used for matching. Live-edge overwrite semantics unchanged. - src/prefix-affinity.ts: TAIL_WINDOW/MIN_TAIL_MATCH/MAX_STORED_ITEMS/ MIN_FORK_PREFIX; perItemHashes + lcpLength; resolve() gains tail-window reattach + lineage; note() stores per-item hashes; AnonymousAffinity gains via + itemHashes + lineage. - src/server.ts: tail-window reattach log line; note() 4th arg; metadata records via + lineage. - tests/prefix-affinity.test.ts: note() call sites updated to 4 args. - tests/tail-window-reconcile.test.ts (new): tail-window reattach, prefix/new regressions, fork + truncated lineage, and a kernel reconcile anchor test. Independent of PR-A (codex turn-metadata, #317). Pre-flight: typecheck OK, 746/746 tests, build OK. Codex (>=0.147) reuses the root session-id header on subagent threads, so the legacy identity chain mapped every thread of one task onto a single session — subagents inherited the root's compression state, breaking #150's isolation (a guardian subagent must read the user's original authorization verbatim, never a compressed summary). Add codexTurnIdentity(): when x-codex-turn-metadata is present and cross-checked (metadata.thread_id === thread-id header), partition by thread_source — "user" keeps the session-id header (current root semantics, stable across turns), "subagent" gets its own thread-id (fresh independent state per thread). Untrusted metadata (absent / unparseable / mismatched / unknown thread_source) falls through to the legacy chain unchanged, so no-metadata clients (omp pck, etc.) see zero regression. The trusted id enters the verbatim session chain directly (no hashing, no new dimensions) and bypasses subagentNamespace, leaving the kernel's empty-instructions non-anchoring path untouched for metadata-less clients. Tests: unit coverage for both thread_source values, cross-check mismatch, JSON parse failure, unknown thread_source, missing headers, omp pck chain unaffected; plus a server-level test proving subagent and root threads land in distinct isolated sessions. docs: add issue-work delivery requirements to AGENTS.md release v0.1.62 feat: #189 compress observability + gated staged/tail-bias steering (smooth transition) fix(logger): rotated log file orphan inode — reopen or degrade to stderr-only (#210) release v0.1.61 fix: preflight compress fails fast on upstream errors instead of forwarding over-window payloads (#301) fix: honor anthropic-beta context-1m to size the context window fix: detect chained bili instances via x-bili-hop marker (#300) fix: adopt complete terminal usage atomically in anthropic adapter (stitched-stream double-count) release v0.1.60 feat: anonymous prefix-affinity session resolution (#309) Clients that cannot send any identity signal (no headers, no body session_id / prompt_cache_key) still replay their full history every request. Resolve them by longest-prefix hash-chain affinity instead of the #286 hard 400: h_i = sha256(h_{i-1} || msg_i) over the client-raw messages, matched against per-partition (protocol | upstream | auth) stored chains, fork-safe (divergent histories split on their next request), deterministic ids (pfa-) so identical replays reattach after restart. Degenerate requests (empty / system-only) keep the explicit 400. Logging on every resolution (session, match depth, partition, tail hash). - Observability (always on): applyRanges now records the last successful compress (shrink ratio, fold point, blocks, tokens) on the session and logs an [acp-compress-obs] line per compress. - 3007 correlation: the replay/preflight retry callbacks now append a [after compress: shrink N% foldPoint=X ...] suffix so a transient upstream rejection is correlated with the rewrite that preceded it. - Staged compression + prefix survival (gated behind BILI_MAX_SHRINK_PER_COMPRESS): when a single compress shrinks context more than the configured fraction, the tool result steers the model toward smaller, TAIL-biased ranges (keep the stable prefix intact), and the nudge text gains a smooth-transition guidance block. Off by default (legacy behavior). - 10 new tests in tests/compress-observability.test.ts. fix: tag-echo filter strips truncated tag fragments (#294) When an Anthropic-format request carries an anthropic-beta header with a larger-context beta (e.g. context-1m-2025-08-07), the client has negotiated a 1M-token upstream window. The proxy now sizes its own context window to match (1,000,000), overriding the model-table default (200K for claude). The beta is the most direct per-request evidence of the window the upstream will serve, so it is resolved at the top of the native-window cascade and re-read on every request (the header may appear/disappear between requests of the same session). This fixes EMERGENCY / nudge / usage bands firing against the wrong denominator — e.g. 261K input was 131% of 200K (EMERGENCY) when it was really 26% of the 1M window the client negotiated. - anthropicBetaContextWindow(): parses context-Nm betas (N x 1,000,000), largest wins. - e2e: EMERGENCY suppressed at 261K input under the 1M beta; fires at the same input without the beta (200K). Plugin effectiveContextLimit is 1M with the beta and drops to 200K when the header disappears (per-request). When two bili instances are chained (A forward -> B), both run the compression pipeline on the same request, double-compressing and corrupting session state (root cause of #292). - Outbound: stamp x-bili-hop: on every request this instance actually processed (main forward + preflight summarization + compress-loop calls via shared headers). A passthrough forward does NOT claim processing, so a dumb-proxy bili in front of a processing one is unaffected; any inbound marker keeps propagating down the chain. - Inbound: if a request already carries x-bili-hop, an upstream bili already processed it. Skip ALL processing (no tool/tag injection, no acp-loop, no session state) and pass it through verbatim, logging a prominent [warn] naming the upstream instance (self-loop vs foreign chain). - e2e: two tests — B in isolation with a foreign marker (body reaches the LLM byte-identical, no tool/tag injection, marker propagated), and the full A->B chain (single-layer processing, B warns, one LLM request, marker is A's not B's). Clients never send this header, so single-instance use is unaffected. release v0.1.59 test: normalize macOS /var->/private/var tmpdir paths in launcher rewrite tests fix: recognize Codex context-overflow errors and learn conservative window when unparseable release v0.1.58 fix: bili claude/codex auto-inject native MCP tools by default (#290) refs #280 On macOS os.tmpdir() returns /var/folders/..., which is a symlink to /private/var/folders/.... fs.realpathSync in the pi/omp http-rewrite tests resolves through it, so the strictEqual path asserts compared a realpath'd left side against an un-realpath'd right side and failed. Canonicalize the mkdtempSync result up front; no-op on Linux. Full suite 687/687 on Linux. fix: keep Codex compaction_trigger final when injecting the compress nudge docs: how to open PRs without the gh CLI (credential + REST API) Documents the exact procedure the environment actually supports: no gh binary, but git push works via the credential helper and the same credential opens PRs through the GitHub REST API. This unblocked #282 (analysis was complete, PR creation was not). Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus fix: floor fallback-derived context window at 100k after output-headroom reservation fix: resolve registry window for relay hosts via provider-prefixed key scan fix: body dumps off by default (ACP_DUMP_BODY) + reuse log-mask in dumps (#276) feat: session id is the client-provided conversation value verbatim; 400 without identity (#286) - dsh: force PI_CACHE_RETENTION=long so pi-ai stamps prompt_cache_key (the dsh session uuid) on every request; the prompt_cache_retention side-effect field is stripped proxy-side before forwarding upstream. - hermes: write a bili-session-identity provider plugin into the ~/.hermes-bili overlay — it re-registers the bundled custom profile (plus custom: for every proxied provider) with the real hermes session id as prompt_cache_key. Skipped when ~/.hermes already has a plugins/ dir (never write through the overlay symlink). - fix: prepareHermesHome now strips YAML quotes before matching api values — quoted endpoints previously failed to rewrite silently. - prompt_cache_retention stripped on the openai and responses paths (OpenAI-host-only directive; third-party upstreams may reject it). Close #290. BILI_LAUNCHER_PLUGIN now defaults to on for claude/codex (both flags verified on claude 2.1.227 / codex 0.147.0 and soaked since 08-16); set BILI_LAUNCHER_PLUGIN=0 to force pure wire mode. Strong-signal conversation identities (codex session-id/thread-id, claude x-claude-code-session-id, opencode x-opencode-session, Responses body session_id, prompt_cache_key) now derive the session id from hash(protocol|conversation) only. The 4-dim hash (protocol|upstream|credential|conversation) is retained for weak signals (content fingerprints), where the credential/upstream dimensions provide collision isolation. Rationale: the credential dimension is a rotating OAuth bearer for Codex/ChatGPT and the upstream dimension changes when users switch relays — either change orphaned all compression state mid-session (#280). The client's native session id is the only invariant bound to the conversation. One-time cost: existing persisted sessions are re-keyed once (old records orphaned), accepted per issue discussion. refs #280 Codex's native remote-compact request ends with a compaction_trigger input item that the upstream requires to be the FINAL input item. The compress nudge injection appended a user message after it, so any nudge-armed session that hit a native compact got 400 "The 'compaction_trigger' item must be the final input item." and the pre-sampling compact (and the turn) failed. Skip the nudge when the final input item is a compaction_trigger — the native compact IS the compression, and the developer-message / tool injections (head of input) are unaffected. Test: Responses e2e — trailing compaction_trigger stays final with no nudge appended; control request keeps the nudge. Full suite 662/662. closes #280 After a macOS sleep/wake (or any rebind), a restored session starts with lastInputTokens=0, so the nudge is blind and only the preflight fit check can catch an oversized replay. Two gaps left the Codex path stuck: - Codex's overflow error (code context_window_exceeded / "ran out of room in the model's context window") matched none of CONTEXT_OVERFLOW_PATTERNS, so no self-heal ever armed and the same oversized payload was re-sent forever. - Even when the error is recognized, Codex's body carries no window number, so parseOverflowWindow found nothing and the floor stayed 0. Now: both Codex markers are recognized, and when no window is parseable the proxy learns a conservative window from the rejected payload's own size (only-shrink-never-grow). The next turn's self-heal re-centers the limit and preflight compresses the history below it before forward. Tests: 2 unit (Codex error recognition) + Codex e2e self-heal (400 without window number -> learned window -> preflight fold on the retry) + new Responses-protocol e2e for the issue's suggested restore scenario (fresh session, lastInputTokens=0, raw history over the window -> preflight compresses before forward). Full suite: 661/661 pass. - Decouple all plaintext body dumps (dumps/req-*.json, raw/*-REQ.txt, raw/*-RES.txt, raw/*-INCOMING.txt, req-*-REREQUEST.json) from --debug: they are now opt-in via ACP_DUMP_BODY=1, so `bili ` users (launcher hardcodes debug:true) no longer leak conversation bodies to disk by default. - Reuse maskHeadersForLog from src/log-mask.ts for dump header filtering: now covers cookie/set-cookie (the old /key|auth|token/i regex missed them) and non-public hosts. - Mask the upstream URL in raw/*-REQ.txt (maskUrlForLog) and the incoming path in raw/*-INCOMING.txt (maskUrlsInText) so non-public hosts don't land in dump files. - INCOMING dump now honors ACP_RAW_DUMP_DIR (was hardcoded to stateDir/raw). - Add tests/dump-mask.test.ts: dumps off by default under --debug; with ACP_DUMP_BODY=1 dumps carry no credentials and no non-public host; dumps work without --debug. tunnelThrough logged err.message verbatim; OS/undici error text embeds the endpoint ("connect ECONNREFUSED 10.0.0.5:8443", "getaddrinfo ENOTFOUND relay.internal"), re-leaking the host the template just masked. Add maskHostInText() to log-mask.ts and apply it to the two tunnel error log lines; formatUpstreamError now reuses the same helper (also fixes the bracketed/bare IPv6 mismatch in its scrub). +2 tests: unit coverage of maskHostInText, e2e CONNECT tunnel failure asserting no raw target host in any captured log line. bili.log and the launcher tmp log (bili-proxy-${port}.log) received plaintext secrets and non-public upstream endpoints: - 'forward GET/POST -> ' logged the full upstream URL on every request (always-on, not debug-gated) - '-> upstream headers:' / '<- upstream response headers:' logged authorization / x-api-key / cookie / set-cookie values verbatim - formatUpstreamError embedded the raw upstream URL and the failing host (ECONNREFUSED 192.168.x.x, ENOTFOUND relay.internal) - mitm/CONNECT logs exposed tunneled upstream host:port New src/log-mask.ts: - credential headers (key|auth|token|cookie) -> '' - non-public API hosts -> '' (port kept); well-known public endpoints (openai, chatgpt, anthropic, deepseek, googleapis, azure, mistral, groq, ...) stay verbatim for debuggability - userinfo/query/hash stripped from logged URLs (key-leak vectors) - error message/address fields scrubbed of non-public hostnames debug: true in the launcher is intentionally kept (per #255 triage); dump-file gating and GC are tracked in separate issues. release v0.1.57 fix: identity plugin binding survives model switches across sessions fix: preflight-compress oversized context before forward on model switch fix: stamp type:message on type-less Responses input items (omp wire form) fix: omp chat-completions sessions get pck identity + /acp panel release v0.1.56 fix: plugin passthrough review follow-ups (non-streaming strip parity, stream-cut tail flush, multi-line data, malformed parts) regression: #257 omp native plugin mode + #258 drop whitespace-only Responses items estimateCoreMessages/splitChunks/summary-cost used estimateTokensFast (chars/4), which undercounts CJK ~4x. On a stable CJK session the loop's fit-check therefore saw an undersized payload and broke without compressing, forwarding an oversized body -> upstream 400. Use the kernel's CJK-aware defaultCountTokens and floor the loop on the session's measured lastInputTokens (which also covers system prompt + tool defs). Adds a CJK e2e test (red before, green after). 633/633 pass. A failed identity register left state.sid set, so the early-return guard blocked every retry — the session stayed in wire mode (duplicate ACP tools) for its whole life. Don't mark the sid done until the register succeeds, and guard the tool loop with a separate toolsFor flag so a retry re-fetches the manifest without re-registering tools. Make the retry interval injectable (createBiliPlugin opts.retryIntervalMs) so the test can actually assert the post-throttle retry. omp 17.x mounts extension tools that omit loadMode under xd:// devices (invisible to the main turn); declare loadMode:"essential" so the four ACP tools reach the model natively. omp emits no before_provider_headers, so bind the conversation via the launcher identity register (#162 path) once tools are ready — bound sessions run in plugin mode with wire injection suppressed. before_provider_request doubles as the retry driver. Full native roundtrip e2e-verified against omp 17.3.8. fix: net post-compress usage credit + hand launcher context windows to the proxy (#252) release v0.1.55 merge: joint regression branch for PR#246 + PR#248 (dsh launcher + omp zero-config) omp does not ship the bili plugin; the launcher now rides -e dist/agent/omp.js when the config carries no loadable bili entry (mirrors pi, PR#227). omp keeps wire-injected tools (its fork never surfaces extension tools to the model and emits no before_provider_headers); the injected plugin provides the native /acp command. Server log now prints the effective injectTools flag, not the raw one. Stateless full-history replay clients without conversation headers (omp without its plugin) minted a new session every request: the kernel identity chain hashes the ENTIRE input array, which changes as the conversation grows. New session per request meant the nudge always evaluated 0 tokens (usage lands after the first forward), so 90%+-full contexts were never compressed and the upstream affinity churned every turn. prompt_cache_key — the client's own stable conversation id — now replaces only that fingerprint; headers, body session_id and previous_response_id still win. bili dsh now splices a --patch overlay (~/.dsh-bili/.bili-acp.patch.yml) into dsh's argv, inserting dist/agent/dsh-acp.js (a cordis command plugin, same shape as dsh-command-compact) into whatever profile boots: a real /acp slash command on the web/tui surfaces. - src/agent/dsh-acp.ts: /acp → status panel (fallback=latest), else armed-but-idle info (#235 wording), else unreachable error - src/agent/shared.ts: +fetchStatusLatest (conversationId=dsh placeholder; server rejects the empty string) - src/launcher.ts: +writeDshAcpPatch (independent of the settings rewrite, exists with zero custom providers) +dshArgsWithPatch (prepend / after 'web' / skip for 'plugin' & --dump-default-config) - tests: launcher 85 (incl. argv assertions in runLaunch dsh e2e), tests/dsh-acp.test.ts 4 outcomes; 610/610 This reverts commit a2af88ffda51fa94082067e8e49cb4672bd17537. - client-config: resolveDshHome / parseDshSettingsYaml / readDshConfig - launcher: dsh route discovery, prepareDshHome overlay (settings.yaml line rewrite, CRLF-preserving, siblings symlinked), runLaunch branch wiring BILLION_CONTEXT_PROXY + DEEPSEEK_BASE_URL (built-in deepseek-official route) - fix(openai adapter): emitCompletion usage numeric fields ?? 0 — undefined prompt_tokens/completion_tokens serialized away and strict clients (dsh mapUsage) compute NaN → non-JSON-serializable session events - cli help, README/CONFIGURATION (en+zh), 7 new tests release v0.1.54 [Fix] 限制 Responses round-2 重映射 message ID 长度 Sanitize over-long input item ids before the projection reads them so conversations stuck on 400 recover on the next request. Complements the root-cause fix (hashId remapping) in this branch. fix: surface truncated upstream streams instead of silent end_turn fix: allow remote clients when bound to a non-loopback host (#240) - setupMitm: new allowRemoteClients flag; remote CONNECT accepted only for whitelisted model hosts (blind tunnels stay loopback-only — no open relay) - server: derive the flag from --host; honest 0.0.0.0 display + [security] warning on non-loopback binds - tests: #77 case extended to the full remote policy matrix - docs: README/CONFIGURATION remote-agents section (en+zh) Follow-up to #221 (PR #222): the reporter still sees the agent stop while outputting a long conclusion. If the relay drops the SSE stream mid-generation (before message_delta/message_stop), the loop received no done event, finishReason stayed undefined, and emitCompletion defaulted it to a clean end_turn — the client then ended the turn mid-text with no indication anything went wrong, which reads as the agent silently stopping. Detect the abnormal termination (no done event in the round) and emit an explicit [acp-proxy: upstream stream truncated (...)] error block plus an error-level log, so truncation is visible and diagnosable instead of masquerading as a normal turn end. Refs https://github.com/ranxianglei/billion-context/issues/221 release v0.1.53 docs: README header badges + centered install command (match billion-context-pi) release v0.1.52 fix(agent): /acp shows armed/idle info instead of warning before first request A 404 from the status endpoint on a live proxy means the conversation has sent no model request yet (e.g. /acp right after bili pi starts) — not an error. Probe the manifest for liveness+version and show an info notice; keep the old warning only when the proxy is unreachable. release v0.1.51 fix: legacy billion-context-pi entry wrongly suppressed launcher -e injection isPiEntry (PR #227) matched legacy npm:billion-context-pi entries as 'installed', so piPluginInstalled() returned true and the launcher skipped -e — but that legacy package usually is not installed, leaving pi with NO plugin at all (/acp gone). Add isBiliPiEntry() which only matches billion-context itself; install/remove keep the broad isPiEntry for legacy cleanup. docs: recover deleted README usage detail into CONFIGURATION (en+zh) fix: persistent overlay homes for pi/omp/hermes launchers (session history loss) # Conflicts: # README.md # Conflicts: # src/launcher.ts docs: align README with the zh edition, drop plugin-install section Review of the first overlay commit found three paths where overlay-created data could be silently destroyed instead of merged back into the real home: - a regular FILE shadowing a real-home entry was rm'd without merging (client atomic-write-over-symlink lost its newer content) - the dir merge was shallow: new files under subdirs that exist on both sides were skipped and then deleted - a type clash (real-home file vs overlay dir, or vice versa) aborted the merge silently while the caller still rm'd the shadow subtree mergeOverlayDir is replaced by a recursive mergeOverlayEntry that preserves both sides (mtime-newer-wins for files, losers kept as .bili-conflict) and reports failure; the caller now removes the shadow only when the merge fully succeeded, otherwise it is kept in place with a warning. Also add a .bili-launch.pid marker that warns when two bili launches share one overlay (each launch rewrites the generated config with its own proxy origin). Regression tests for all three loss paths plus the pid marker. - English restructured to mirror README.zh-CN.md: Quickstart two ways (launcher / URL change), Option 1 Launcher (intro + usage block only), Option 2 URL change, no plugin-mode deep-dive, no mode matrix, no redirect/CA or discovery tables, no temp-copy details, no subagent/ export sections. - Drop the zh plugin-install note: bili pi now auto-injects the native plugin via -e when not installed (#227), so install is never required. - Status: mock test count 146 -> 500+. fix: bili plugin install pi replaces legacy billion-context-pi entries fix(loop): strip model-emitted render tags to break #206 echo feedback loop The pi/omp/hermes launchers built their /bili/-rewritten config inside a mkdtemp temp home (PI_CODING_AGENT_DIR / HERMES_HOME) and rm -rf'd it when the client exited. Clients persist absolute session paths derived from that temp home into shared state (e.g. omp ~/.omp/agent/terminal-sessions/pts-* resume pointers), and any file the client created directly inside the temp home (agent.db, models.db, ...) was destroyed on exit — making omp session history invisible and leaking temp dirs when the process was killed. Replace temp homes with stable persistent overlays next to the real home (~/.omp/agent-bili, ~/.pi/agent-bili, ~/.hermes-bili): every launch refreshes entry symlinks (dropping dead ones, merging overlay-created entries that shadow real-home entries back into the real home) and rewrites the generated config atomically. The overlay is never deleted, so paths stayed resolvable across runs. opencode/claude/codex launchers are unaffected (env/temp-file only). - fix filter false-swallow after paired tags (wasPaired check) - containsRenderTagText: detect JSON-escaped open tag with attrs - non-stream gates use containsRenderTagText (catch bare lone tags) - openai rewriteContentChunk preserves sibling choices - add e2e tests (real server, compress round-trip) and non-stream tests Models sometimes imitate the render tags injected into compressed history and emit them in visible output. The client stores the echoed tags, replays them next turn, and the imitation amplifies into unbounded repetition (#206). Strip render tags from outgoing text in all three wire adapters (streaming and non-streaming) with a streaming-safe filter; underscore-namespaced text-protocol triggers and ordinary prose pass through untouched. launcher pi branch prepends pi -e only when the user has NOT run plugin install pi (mutually exclusive: pi dedups extensions by resolved path, packages entry vs -e file would double-register). Uninstalled users now get the native tool panel and /acp out of the box, matching bili omp / bili opencode behavior. isPiEntry now recognizes the legacy billion-context-pi npm form, dev checkout paths, and Windows backslash paths, so install leaves exactly one live bili plugin instead of stacking the old 0.1.x package next to the new one. piStatus reports strictly on the current install root. release v0.1.50 docs: cover all six launcher clients + backfill changelog 0.1.41-0.1.50 README keeps only the upstream-proxy (GFW) subsection; the reference material (config file, top-level keys, providers, compression tuning, env vars) lives in the dedicated CONFIGURATION docs. English README mirrors the zh restructure (drop env-var table, Two ways, fix heading). Removed detailed environment variable configuration section from the README file. Removed duplicate sections and clarified usage instructions for the proxy and client configurations. feat(launcher): add hermes to the bili launcher (/bili/ rewrite, no MITM) fix(openai-sse): settle tool calls at finish, not per-chunk (name-split regression) fix: survive upstream rejection of replayed thinking blocks in compress loop PR #190 added fetchWithRetry (transient 429/5xx/risk-control replay retries, BILI_REPLAY_RETRY_MAX opt-out) to the re-request path. Keep that machinery and layer the #221 fix on top: a deterministic 4xx that survives transient retries triggers one degraded retry with loop thinking blocks stripped before the error is surfaced. After a proxy-tool round (compress/decompress/acp_status/search_context), the loop rebuilds the conversation and re-requests upstream. The rebuilt assistant message replays thinking blocks collected from the intercepted turn. Two failure modes halted the agent right after the ACP marker (upstream issue #221, MiniMax M3 via Anthropic-protocol relay): - signature-less thinking (relay never sends signature_delta) cannot be replayed on Anthropic-protocol endpoints that validate -> 400 - even signed thinking replay is rejected by some relays/converter backends Both made the loop emit an error and end_turn, so the agent stopped executing right after the ACP tool result. Fixes: - proactively drop signature-less thinking segments from the replay (they are already streamed to the client verbatim in round 1) - if the re-request is rejected with a 4xx, retry once with all loop-injected thinking blocks stripped (degraded retry, at most one per turn) before surfacing the error Fixes https://github.com/ranxianglei/billion-context/issues/221 fix(loop): auto-retry acp-loop replay on transient upstream rejections (#189) release v0.1.49 fix(config): correct stale context limits + registry-first resolution + Web UI parse-error guard - CONTEXT_LIMIT_TABLE: deepseek 64K -> 128K (V2-era value caused constant emergency compression out of the box); add minimax -> 204.8K - registry: map api.minimax.chat / .com / .io so provider-qualified keys like minimax/MiniMax-M2.1 resolve; new sync peekRegistryContext() - server: native-window order is now plugin header -> warm registry cache -> user route declaration -> built-in table -> async registry fallback. A stale table can no longer lock out fresh registry data; cold start still never blocks on a fetch. - web/api: GET /__bili/config surfaces parseError for an unparseable config; PUT returns 409 instead of silently rebuilding the file from {} (which wiped every field the JSON loader could not read) — reported on #212 as 'config overwritten after npm install' - web/client: toast the parseError path - tests: +8 (registry unit tests; broken-config PUT guard) release v0.1.48 fix(plugin): stamp x-bili-plugin only after tools are registered Review follow-up: the graceful-degradation claim (dead manifest endpoint -> permanent wire mode) had no test. Drive session_start against a fake proxy that 404s every request and assert the plugin stays silent. The header tells the proxy the client owns the ACP tools natively (skip wire injection). Stamping it synchronously while registerTools() is still fetching the manifest sent the first provider request out tool-less in one-shot runs. Gate the stamp on toolsReady; until then the request rides the proxy's wire mode, and a dead manifest endpoint degrades gracefully instead of stranding the session. fix(responses): remap round-2 message lifecycle so codex stops dropping text deltas fix(launcher): never reuse a proxy port — always spawn a fresh instance release v0.1.47 feat(launcher): add opencode as a launch client + --bin flag process.argv[1] stays at the global bin symlink (~/.local/.../bin/bili), so path.dirname(argv[1]) pointed into the bin dir and the existsSync guard silently skipped dist/agent/opencode.js (and dist/mcp.js in the MCP configs). Resolve via selfPackageRoot() (import.meta.url-based, symlink-safe through Node's realpath) instead. Verified via the global bili: opencode run now loads the bili plugin ('[bili-opencode] plugin active') and the session log shows dist/agent/opencode.js loading. - src/agent/opencode.ts: 1.8KB plugin shipped as dist/agent/opencode.js; registers the acp command via the config hook, renders the proxy's buildStatusPanel through an ignored no-reply chat message. Active only under BILLION_CONTEXT_PROXY (launcher), no-op otherwise. - prepareOpencodeHttpRewrite(pluginPath?) injects the plugin's absolute path into the throwaway OPENCODE_CONFIG copy so bili opencode needs no install step and never touches the real opencode.json. - handlePluginStatus(..., fallbackLatest): ?fallback=latest resolves an unknown conversation to the most recently active session (opencode plugins cannot stamp x-bili-plugin headers); response gains fallback:true. Unchanged 404 without the param. - The register-POST path was deliberately dropped: registering suppresses wire tool injection, and this plugin cannot natively register the 4 tools (opencode tool hook needs zod). Wire injection + fallback status keeps the model surface intact (verified: round-2 tools list ends with compress,decompress,search_context,acp_status). cert-MITM cannot intercept plaintext HTTP, so opencode's plaintext providers (e.g. local zhipuai-lb) bypassed the proxy entirely. Mirror the pi/omp pattern: write a /bili/-rewritten copy of opencode.json to a temp dir and point OPENCODE_CONFIG at it (real config untouched); HTTPS providers still ride cert-MITM. - bili opencode: starts the proxy (cert-MITM for discovered HTTPS domains), sets HTTPS_PROXY + NODE_EXTRA_CA_CERTS + BILLION_CONTEXT_PROXY (so opencode-acp self-disables, see opencode-acp PR #335), and spawns opencode. - --bin : override the client binary name for non-standard installs (e.g. bili opencode --bin opencode-stable). feat(plugin): add /acp command to show session ACP status This reverts commit 36c8e031d400ca9fad3dbef1ac7a851291e4efc1. feat(compress): switch parseCompressInput to kernel parseCompressArgs + remove safeJsonParse (de-JSON) The OpenAI adapter buffered ALL tool_call deltas and regenerated them with a proxy-generated ID after finish_reason, breaking client-side tool_call parsing (ID mismatch + reordering). Real tool calls now pass through as raw SSE events (meta), preserving original position and ID — matching plugin-mode behavior. Fixes #209 Avoids a name clash when a user has both billion-context and billion-context-pi installed. The proxy now injects bili_compress / bili_decompress / bili_search_context / bili_status at the wire level; the compress loop maps bili_* → canonical names before dispatch. The /acp command is renamed to /bili. The kernel's prompt text is parameterized separately via withToolNames (acp-kernel PR #138). Aligned with billion-context-pi's format: the panel now shows billion-context@ under the header, read from package.json at runtime (same pattern as src/cli.ts's VERSION constant). conversationIdentityResponses (acp-kernel/wire) does NOT read prompt_cache_key — it falls back to a content fingerprint with clientProvided:false, so a fix keyed on responsesIdentity.clientProvided never fired for omp. Read the field directly from the Responses body and record it unconditionally. Two fixes for /acp in omp (worked in pi from the start): 1. Persist the plugin-conversation map across proxy restarts. The conversationId->sessionId map in src/plugin.ts was in-memory only, so a fresh/resumed proxy had an empty map and /acp said 'no ACP session yet' until a new model request arrived. Now it is persisted to /plugin-conversations.json (debounced write on record, flush on the 3 shutdown paths, load after initSessions at startup). 2. Record omp's session id from the request body. The map is populated by recordPluginSession, called only inside 'if (pluginAgent)' — and pluginAgent comes from the x-bili-plugin header, which the plugin stamps in the before_provider_headers event. omp has NO such event (its dist only emits before_provider_request, body-only), so the header was never stamped and pluginAgent stayed undefined for omp. omp sends its own session id in the body as prompt_cache_key (a Responses-API field), which the kernel's conversationIdentityResponses already extracts as the conversation identity. Now, for responses requests with a client-provided identity, we call recordPluginSession(responsesIdentity.value, session.id) unconditionally — a no-op duplicate for pi, the only path for omp. Verified: typecheck PASS, 526 tests pass, build PASS; e2e 'bili omp -p ' -> conversations file gains the omp session id, and GET /__bili/plugin/status?conversationId= returns ok:true with the rendered buildStatusPanel. The proxy's /__bili/plugin/status now renders the status with acp-kernel's buildStatusPanel (same function billion-context-pi's /acp uses) and returns it as a 'panel' field. The plugin displays that rendered panel verbatim, falling back to the compact renderAcpStatus when absent — so /acp output matches billion-context-pi exactly. The agent plugin (pi/omp) now registers a single /acp slash command via pi.registerCommand() that shows the current session's context-compression status (context tokens/limit/%, in/out/cached, requests, blocks). It reads the proxy's existing /__bili/plugin/status endpoint — no proxy changes. Guarded by typeof pi.registerCommand === 'function' so hosts without the API are unaffected; best-effort (warns, never throws) when the proxy is undetected or the session is unknown. Addresses the spirit of issue #53 (view session acp status). User asked for exactly one command that only shows status (no subcommands). feat(launcher): add omp to the bili launcher (no-config /bili/ rewrite + MITM) omp (oh-my-pi) is pi-based and honors PI_CODING_AGENT_DIR, so the existing pi isolated-home pattern is reused: the launcher reads ~/.omp/agent/models.yml (never edits it), rewrites HTTP providers' baseUrl to the /bili/ prefix in a temp agent dir, whitelists HTTPS providers for cert-MITM, and launches omp pointed at the proxy. - client-config.ts: OmpProvider/OmpConfig, resolveOmpHome, parseOmpYaml, readOmpConfig; wired into loadClientConfig - launcher.ts: omp in LAUNCH_CLIENTS/BaseClientName; launcherInjectMcp excludes omp; discoverRoutes omp branch; prepareOmpHttpRewrite; runLaunch omp branch - cli.ts: help text lists bili omp - tests/launcher.test.ts: 8 new tests refactor: ACP tool surface from acp-kernel 0.0.42 (Phase K1) The 406-line static surface (schemas ×3 wire formats, prompt builders, text tags, parseCompressInput, tool sets) moved verbatim to acp-kernel compress-tools — single source shared with omp. This module keeps the proxy's import paths and names stable: PROXY_TOOL_NAMES / MUTATING_PROXY_TOOLS / READONLY_PROXY_TOOLS alias the kernel's ACP_* names, parseCompressInput wires the kernel onWarn hook into logger. Pins acp-kernel ^0.0.42 — to be set when the kernel release lands. Verified: npm run typecheck clean, 514/514 tests pass against the local kernel build (file: dep, uncommitted). refactor(persist): delegate SessionStore mechanism to acp-kernel/persist ci: per-PR npm preview builds (pr-artifact workflow) On every PR to master: build, publish to npm under tag pr- (version -pr..), upload tarball+dist artifact, and comment on the PR with install instructions (auto-updated per push). Ported from opencode-acp's pr-artifact.yml, adapted per package. fix(anthropic): round-2 thinking deltas carry raw — stop per-delta block fragmentation Windows-22 CI runners take seconds on a cold loopback connect (observed ~3s), racing the manifest fetch behind the fixed flush. Same failure reproduces on master-side commit f755fe4 — pre-existing flake, not from the round-2 thinking fix. release v0.1.46 fix: re-inject the client's openai system around the kernel hoist (0.0.37) Round >1 (proxy-tool/compress re-request rounds) withheld raw on thinking_delta/signature_delta and re-wrapped EACH delta via emitReasoning (a complete start+delta+stop block per chunk). Clients assembled one thinking block per token fragment and replayed them in subsequent requests — zcode showed a stack of duplicate '思考过程' entries and the session accumulated dozens of reasoning fragments. Mirror the 4deb563 vertical-text fix for text: attach raw in all rounds, forward content_block_stop for thinking blocks in all rounds. core.ts raw passthrough then preserves upstream framing byte-for-byte (one block, signature included); the emitReasoning fallback stays as a safety net for adapters that do not provide raw. fix(loop): run applyCompression over the unpruned view The streaming loop ctx passed processedMessages (pruned view with acp_summary_* stripped) to applyCompression, so active blocks whose raws were pruned had no resolvable anchor — bN promotion failed with the self-contradictory 'already compressed' error. Add a dedicated compressMessages field (unpruned originalMessages) used only by applyCompression; wire rebuilds keep using the pruned view. The kernel hoists the contiguous leading system/developer prefix out of the openai fold space (system content is host runtime state - it must not feed ids/fingerprints, and a compression covering it used to delete the model's system prompt from every rebuilt payload). The proxy must now carry it around the fold: - prepareOpenai captures systemText from openaiToCore and re-injects it (client system first, compress prompt after) into every rebuilt payload - Prepared.openapiSystemText flows into pickAdapter so compress-loop rounds re-inject it too, mirroring the anthropic adapter's anthropicSystem path - createOpenaiAdapter gains a clientSystem param chore: acp-kernel 0.0.36, filter sub-viability nudge ranges via kernel viableRanges chore: bump acp-kernel to 0.0.34 After a compress, provider risk-control may briefly reject the replay request (GLM Coding Plan: 400 {"code":3007,"msg":"captcha verify failed"} ~1s after the context rewrite), and the error was passed straight into the agent session. The replay request now retries transient upstream failures with exponential backoff (3 attempts total, 1500ms base, BILI_REPLAY_RETRY_BASE_MS override) on both the streaming loop and the Responses-API JSON loop. Transient = 429/5xx or 4xx bodies matching risk-control markers; plain 4xx still fail fast. Each retry logs a clear "likely provider risk-control — retrying" line; exhausted failures are surfaced with an "after N attempt(s)" suffix. fix(plugin): codexRemove drops header-only bili block with no trailing newline When the [mcp_servers.bili] table was header-only (no keys) and sat as the final line with no trailing newline, after.indexOf('\n') was -1, so the next-table search ran over a slice that still included the header itself and matched it at index 0. end collapsed to start and the header line survived the removal. Compute firstNewline once and treat a missing newline as 'block runs to EOF' so end = text.length. Adds a regression test covering the header-only, no-trailing-newline case. release v0.1.45 chore(deps): bump acp-kernel 0.0.31 -> 0.0.32 feat: builtin thin agent plugins (pi/omp/claude/codex/opencode) + bili plugin install CLI chore(deps): bump acp-kernel 0.0.28 -> 0.0.31 release v0.1.44 fix(update): verify entry files and never overwrite a working install with a broken one (anti-brick) Blockers: - claude install/remove now honor CLAUDE as the binary path; tests inject a missing stub so the failure-path assertion no longer depends on the claude CLI being absent from the machine running the suite. - `bili plugin install --origin` was silently dropped: the plugin branch returns before the generic overrides->process.env merge, so a stale ~/.local/state/billion-context/proxy-origin discovery file won. Apply the flag to the env before dispatching the subcommand. Nits: - drop dead retryTimer field on RegisterState; cache empty session ids so hosts without a sessionManager stop refetching the manifest per request. - fetchJson: an already-aborted external signal never fires "abort"; forward the state immediately instead of waiting out the timeout. - claude remove is a no-op when nothing is installed (matches other agents). - proxy-origin discovery file: respect --host (collapse wildcard/localhost binds to 127.0.0.1, bracket bare IPv6 literals) instead of hardcoding. Tests: 467/467 pass, typecheck clean; --origin end-to-end verified against a stale state file (9999 wins over 46657). fix(compress): accept JSON-string content in parseCompressInput fix: log non-2xx upstream responses (status + request-id + body snippet) fix: answer WebSocket upgrades with a clean 426 so Codex falls back to HTTP immediately (#2) fix(upstream): direct-connect path gets a 10s handshake timeout fix(plugin): harden remembered-snapshot locking + activate registered flag fix: VSCode Copilot BYOK — total_tokens in final chunk + cancel propagation (#177) Two VSCode Copilot BYOK bugs (issue #177): 1. Every response ended with "Cannot read properties of undefined (reading 'headerRequestId')". The proxy's OpenAI streaming adapter rebuilt the final chunk's usage from the loop accumulator and dropped total_tokens. VSCode Copilot's isApiUsage() requires prompt_tokens, completion_tokens AND total_tokens to all be numbers; without it the usage chunk was misclassified as a completion, so solution.requestId.headerRequestId threw. emitCompletion now emits total_tokens = inputTokens + outputTokens. 2. Clicking cancel mid-response left the proxy blocked waiting for the upstream, and the per-session lock stayed held so the whole session got stuck. The abort controller was created after the upstream fetch and its signal only reached the compress loop, never the fetch. The client-abort controller is now created before the fetch and its signal is passed to fetchWithTimeout (externalSignal) and the compress loop, so a client disconnect aborts the upstream request. The write loop also short-circuits when the response is destroyed. Adds tests/vscode-copilot.test.ts with regression tests for both fixes. fix: harden context window — self-heal on upstream overflow + reserve output headroom Non-strict-tool providers (e.g. vLLM openai-completions, supportsStrictTools:false) stringify nested array arguments, so the model may send compress.content as a JSON string instead of an array. Previously Array.isArray(obj.content) was false and toRange(obj) returned null, so the call silently parsed 0 ranges and compression never ran. parseCompressInput now JSON.parse's a string content before the array check (invalid JSON -> warn + 0 ranges, no crash), mirroring the fix in billion-context-pi PR #164 (normalizeRanges in src/compress-tool.ts). Adds a regression test for the JSON-string form. Review fixes for the overflow self-heal: 1. markDirty on the error path: forward()'s non-2xx branch learns the window (metadata.learnedContextLimits) and arms the emergency (stats.lastInputTokens) but returns before forward()'s trailing markDirty — both mutations lived only in memory and were lost on restart, so the next overflow had to be re-learned. Schedule the save in the branch that mutates. Pinned by a scheduleSave spy in the e2e (on-disk assertions can't prove it: the prepare phase's debounced save serializes the live session after the mutation anyway). 2. Strip upstream content-length/transfer-encoding unconditionally on the error path: when readStreamToBuffer breaks the body (errBody null) the response ends with no body, and a content-length claiming bytes that never arrive leaves the client on a broken response. 3. Protocol-aware output headroom: Anthropic's Messages API enforces the input limit independently of max_tokens (separate output budget), so reserving maxOutput from the window shifted every nudge/truncate band down by maxOutput on every Anthropic session with no safety gain. OpenAI-family APIs count output against the window — reserve there (and for unknown protocols, conservatively). New shouldReserveOutputHeadroom() + unit tests. Review fixes: - Per-model learnedContextLimits: the learned window is keyed by the model that overflowed. Previously a session-level scalar capped the WHOLE session — after /model-switching to a bigger model mid- conversation, the stale smaller window forced premature compression for the rest of the session. handle() now looks up the current request's model; the legacy scalar remains as a read fallback (persisted sessions) and is still written when the model is unknown. - inspectContextOverflow: recognize the newer Responses-API phrasing 'exceeds the model's maximum context size of N' (patterns only covered 'context length') — self-heal now fires for /responses too. - readStreamToBuffer: cap retained bytes at 1 MiB (stream still drained) so a huge 400 body from a misbehaving upstream can't spike memory. Tests: new unit case for the size phrasing; e2e now asserts per-model keying (no legacy scalar) and adds a third request on a different model asserting no cross-model crosstalk. The kernel's nudge/truncate bands are a fraction of modelContextLimit, but none of the layers reserved room for the model's OUTPUT. On a small window (e.g. 100k) with a large max_tokens, context + output can exceed the window and overflow before the nudge fires — the most common 'context blew up' cause on a 100k window. Reserve the request's exact max_tokens (the output budget for THIS turn) from the window when handing it to the kernel, so the bands sit below (window - maxOutput). Pure reserveOutputHeadroom() + per-request wiring in handle(); unit-tested (4 cases). Complements the self-heal (reactive recovery) with a preventive reservation — together they prevent + recover from the context+output overflow. When the configured context window is wrong — most commonly the 200k fallback for an unknown model on a private relay (the MITM case) — the nudge/truncate bands sit far above the real window and the upstream 400s on every turn. The proxy previously passed that 400 through verbatim with no recovery, so a non-compliant model left the session permanently stuck 400-ing (the context blows up and the session stalls). forward()'s non-2xx path now buffers the (small) error body and inspects it for a context-overflow (inspectContextOverflow in src/util.ts — a pure, tested helper: only 400/413 with a recognized context-too-long marker counts, and it deliberately does NOT match Bedrock's "too many tokens" throttle phrase, a 429 the client should back off on). On detection it: - learns the real window from the error body (OpenAI "maximum context length is N", Anthropic "X tokens > Y maximum", ...) and persists it to session.metadata.learnedContextLimit (a STABLE field — effectiveContextLimit is re-resolved every turn and would be overwritten); - handle() then self-heals: if the learned window is smaller than what was resolved this turn, it re-centers reqConfig.modelContextLimit on it (spread into a new object — never mutates the shared global config), so the nudge/truncate bands drop below the real limit; - arms an emergency shrink by raising lastInputTokens to the window (a lower bound — it overflowed, so the context is at least that big), so the next turn's usage is >=100% and the kernel's emergency nudge + tool-result truncate fire; - still passes the error through verbatim (no client-visible change). Tests: tests/context-overflow.test.ts (11 unit cases) + tests/e2e-overflow-selfheal.test.ts (overflow -> learn+arm+passthrough -> recover, real usage report overwrites the armed value). fix(usage): compute cache-hit accounting per protocol (no double-count) fix: normalize localhost bind host; simplify codex web card to url-only The roundtrip test asserted the installed extension path with a forward-slash-only regex (dist\/agent\/omp\.js), which cannot match on windows-latest where the path uses backslashes (D:\a\...\dist\agent\omp.js). Use dist[\/]agent[\/]omp\.js so the assertion passes on both platforms. - drop stray .bili-coop-build-stamp; .gitignore entry now matches the actual harness filename (was .bili-thin-build-stamp) - plugin list: per-row try/catch so one broken host config (e.g. ~/.claude.json) no longer crashes bili plugin list - mcp: forwardTool gets a 60s timeout (wrapped as a readable error), register fetch a 5s timeout; proxy origin resolved per request so a restarted proxy on a new port self-heals - requireDistFile warning: process.stderr.write instead of console.error (AGENTS.md: console.error only in cli.ts/index.ts) - tests: plugin list survives broken config; mcp forwardTool timeout Some relays echo a schema-shaped usage in message_delta where input_tokens/cache_read_input_tokens are present but 0 (the spec fields are normally absent — message_start is authoritative, and the input context is fixed within a turn). Merging the 0 overwrote message_start's real values in both usage paths: - loop mode (adapter-anthropic.ts): roundInput/roundCollapsed to 0 → recordUsage set lastInputTokens to the cached portion only - plugin mode (plugin.ts usageFromSseEvent): mergeUsageSample zeroed acc.inputTokens → applyUsageSample collapsed the total to cached-only The nudge/truncate denominator then under-reported by ~6x (e.g. 11 instead of 66 in the regression fixture), so compression never fired on cache-heavy sessions. A 0 can never be a legitimate update to a fixed in-turn input, so only > 0 values are adopted. Adds regression tests for both paths (verified to fail without the fix). - mcp.ts: fetch manifest lazily on first tools/list instead of fetch+exit(1) at startup; codex spawns MCP children with a sanitized env, so a missing BILI_MCP_PROXY used to resolve the dead default origin and kill the handshake ("connection closed: initialize response") before stdin was ever attached. initialize now always responds; tools/list reports a JSON-RPC error when the proxy is unreachable instead of dropping the server for the whole session. - plugin-install codex/claude/opencode: stamp the resolved proxy origin (BILI_MCP_PROXY env > proxy-origin file > default) into the MCP config at install time (TOML env block / claude -e flag / opencode environment field); reinstalling codex refreshes a stale origin. - verified live: codex exec spawns bili MCP, lists mcp__bili__* tools, and completes an acp_status tools/call round-trip through the proxy. - pi/omp ToolResult contract: {content:[{type:"text",text}],isError?} (crashed pi live when forwarding tool output) - strict /bili/ detection (first path segment + /https?:// next), protocol check - registration: per-session-id single-flight, 10s retry throttle, before_provider_headers fallback, hostile host shape defense - AbortSignal passthrough, explicit timeout errors, args default - createBiliPlugin(agent) factory; omp reports omp, custom agents supported - plugin install: OPENCODE_CONFIG/OMP_CONFIG are file paths, CODEX_HOME/ CLAUDE_CONFIG_DIR are dirs; refuse to overwrite broken/non-object JSON; installers throw -> CLI exits 1; omp YAML flow-style/multi-key/column-0 guards + quoted-entry removal; pi stale node_modules entries cleaned; codex line-regex table match; claude exec 15s timeout - cli: --origin/--agent flags, non-empty value validation, plugin-register agent param + 5s timeout, HELP covers mcp/plugin-register pi replaces the boot session while the awaited manifest fetch is in flight; registerTool on the captured ctx then throws. Register per session id instead, catch staleness and defer, and retry from before_provider_headers (fires per request with a live ctx) as the safety net. - src/agent/shared.ts: proxy detection (/bili/ prefix, BILLION_CONTEXT_PROXY, kill switch), manifest fetch, tool forwarding, status fetch - src/agent/pi.ts: pi/omp extension factory, zero runtime deps, registers manifest tools at session_start, stamps x-bili-plugin* headers - src/plugin-install.ts: bili plugin install/remove/list for 5 hosts with backup+idempotency; PI_CODING_AGENT_DIR respected - tests: 7 new tests (460 total green) The streaming compression-loop caller built LoopCtx without `protocol`, so recordUsage always took the Anthropic additive branch (prompt + cached) and double-counted cached tokens for openai/responses responses on the main streaming path — the exact bug this PR fixes, left unfixed here. - server.ts: add `protocol: prepared.protocol` to the runCompressLoop ctx - loop-core.test.ts: regression tests — protocol='responses' → total= input_tokens (1000, not 1900); protocol unset → legacy additive Three defensive cleanups in the cooperative-plugin feature (merged via #161 + #163), found during review: 1. src/mcp.ts: the 'registered' flag was checked in initialize (if (conversationId && !registered)) but never set to true, so the guard was dead and a repeated initialize would re-issue the register POST. Set registered = true once the register request is issued. (Register endpoints are idempotent, so no live harm — the guard is now actually functional.) 2. src/plugin.ts handlePluginTool: remembered.get(session.id) and the derived messages/nudge were read BEFORE withSessionLock. A tool call racing a concurrent model request (between recordPluginSession's remembered.delete and rememberPluginMessages's remembered.set) could capture an empty/stale snapshot and run executeProxyTool on the wrong context. The read now happens inside the lock, where rememberPluginMessages rewrites the snapshot atomically. 3. src/plugin.ts recordPluginSession: removed the out-of-lock remembered.delete(sessionId) (binding step). remembered[sessionId] is now only mutated under the session lock (rememberPluginMessages after forward), closing the stale window that #2 read into. No behavior change in the normal plugin flow (tool calls arrive after the model response, never concurrent with it); this is lock-consistency hardening. Full suite 453/453 pass; typecheck clean. lastInputTokens / cache-hit rate were computed as `prompt + cached` uniformly, which is only correct for Anthropic (where input_tokens is the NEW portion and cache_read is reported separately). For OpenAI and Responses the reported input total ALREADY includes the cached subset, so adding it again double-counted cached tokens: - lastInputTokens inflated by the cached amount -> the nudge decision overestimates context size -> premature compression. - the logged cache-hit % was deflated (cached/(prompt+cached) instead of cached/prompt); e.g. a true 90% hit read ~47%. - cumulative inputTokens was protocol-inconsistent (Anthropic new-only vs OpenAI/Responses total), so the web cacheHitPct could exceed 100%. Fix: normalize to the true TOTAL context per protocol via a shared usageTotals() helper (util.ts) and thread the wire protocol into every usage consumer: - loop/core.ts recordUsage (streaming compress-loop path) - server.ts non-streaming usage block - plugin.ts applyUsageSample (plugin-mode passthrough) Adapters are unchanged (they still emit protocol-native usage to the client-facing completion). The only behavior change is the internal stats: lastInputTokens, cumulative inputTokens, and the hit-rate are now the protocol-correct total. tests: add tests/usage-totals.test.ts pinning the per-protocol totals; update the plugin streaming expectation from 55 (new-only) to 66 (true total = input 55 + cache_read 11) so cacheHitPct stays a valid ratio. - The handle() `upgrade === "websocket"` 426 check is unreachable once the dedicated 'upgrade' listener is registered: Node routes upgrade requests to 'upgrade' and never to the request handler. Verified on both the plain path and the MITM-decrypted (bili codex) path. Replace with a pointer comment. - Add socket.on("error") to the 'upgrade' handler so a client vanishing mid-426-write cannot surface as an uncaught ECONNRESET. Full suite 456/456, typecheck + build clean. feat(launcher): plugin-in-launcher mode — native MCP tools via spawn-time injection (#162) The web Update-branch merge (31efaa8) resolved the #161/#163 conflicts in favor of the pre-f0d3ebd branch side, silently reverting the plugin-header marker gating (session-id.ts) and its tests. This merge keeps the correct union resolution: register symbols + pluginReportedContextWindow + gating. Resolve conflicts with #161 (plugin protocol) landing on master: - src/server.ts import: union — keep launcher register symbols (handlePluginRegister/consumePluginRegisterFor/takePendingPluginRegister), adopt master's pluginReportedContextWindow; drop pluginContextWindowHeader (unused after master's rename, still exported as internal helper) - tests/plugin-protocol.test.ts import: union of both sides' harness symbols Verified: 453/453 tests pass, npm run typecheck clean. feat: cooperative plugin protocol — agent-side native tools + proxy-owned engine The proxied branch of connectThroughProxy already had a 10s CONNECT handshake timeout, but the direct branch (no upstream proxy) relied on the OS default tcp_syn_retries — a black-holed upstream (firewall drop, routing hole) left half-open sockets hanging for ~127s. Extract the direct path into connectDirect() with the same timeout pattern (settled guard, socket destroy, timer cleanup); connectThroughProxy now delegates to it, so both plain and TLS outbound paths (connectTlsThroughProxy reuses it) are covered. The proxy branch's hardcoded 10_000 now shares the CONNECT_TIMEOUT_MS constant. Tests: real-server success for both entry points, immediate ECONNREFUSED still fast-fails, and a never-completing connect (injected via the repo's _setConnectFactoryForTest seam — ESM namespaces preclude module mocking) rejects with the timeout error in milliseconds and destroys the socket. Review finding on #163: injectMcp defaulted ON for claude/codex, changing spawn args for every existing user while hosts older than the verified builds (claude 2.1.227, codex 0.147.0) are untested against --mcp-config / -c mcp_servers.*. Default-off keeps bili claude / bili codex spawns byte-identical until the flags soak; a stderr hint tells users how to opt in. Extracted launcherInjectMcp() with unit coverage; pi stays excluded (native extension, billion-context-pi #154). Flip default to on (with =0 opt-out) after a soak release. release v0.1.43 buildCodexMcpArgs now takes a per-spawn UUID and injects mcp_servers.bili.env.BILI_CONVERSATION_ID. The MCP shell (src/mcp.ts) already reads it and self-registers headlessly on initialize; this closes the loop — previously every `bili codex` native tool call failed with 'no conversation id' because the launcher never passed the id. Binding works on the default MITM route: the first model request that creates a NEW session consumes the pending registration. Direct-URL mode still cannot bind (model traffic bypasses the proxy) — that stays with the codex LLM-routing follow-up. Also honors BILI_LAUNCHER_PLUGIN=0 in direct-URL mode for codex (the direct branch previously ignored the escape hatch). Tests: builder asserts the new -c line; new e2e drives the real MCP shell over stdio with only BILI_CONVERSATION_ID (no CLAUDE_CODE_SESSION_ID, no manual register) → headless self- register → model request binds → tools/call acp_status succeeds. Direct-URL routing was ON by default, so upgrading silently changed the traffic route of every existing 'bili claude' / 'bili codex' setup: - codex: LLM traffic stops going through the proxy -> compression/memory silently stop working - claude (OAuth subscription): subscription traffic breaks - claude (custom relay): ANTHROPIC_BASE_URL is silently bypassed in favor of https://api.anthropic.com A changed traffic route is a compatibility concern and must be a deliberate choice, not a silent upgrade. The default is now the classic transparent- MITM route (byte-for-byte the pre-PR launcher behavior); direct URL is opt-in via BILI_LAUNCHER_DIRECT=1 (BILI_LAUNCHER_MITM never shipped, removed). Enabling direct mode prints a client-specific warning describing the route change. MCP tool injection remains on by default — it is purely additive and degrades gracefully if the MCP server fails to start. - launcherDirectUrl: BILI_LAUNCHER_DIRECT === '1' - runLaunch: direct-mode warnings (claude: relay bypassed unless BILI_CLAUDE_UPSTREAM; codex: LLM traffic not proxied) - README: compatibility-first default + opt-in documented - test: default = MITM route; direct URL opt-in honored - launcher.ts buildCodexMcpArgs: args were double-encoded (JSON.stringify applied twice) so codex received a TOML string where it expects a sequence — every 'bili codex' with injection died at startup with: Error loading config.toml: invalid type: string ... expected a sequence in mcp_servers.bili.args. Now emits args=["