diff --git a/.gitattributes b/.gitattributes index d59bfcb6e5..35d55e752c 100644 --- a/.gitattributes +++ b/.gitattributes @@ -19,3 +19,8 @@ tests/golden/web-hitrate/*.svg text eol=lf # "every fixture artifact matches its recorded digest and size" failed every # windows-latest leg (run 38982246862). tests/fixtures/vertex-anthropic/* text eol=lf + +# #6692: docs/ Markdown is prose rather than byte-exact, but Git-for-Windows' +# default autocrlf=true rewrote whole files to CRLF on commit (a +18-line docs +# PR landed as +1509/-1489). Pin LF so Windows-side edits stay line-accurate. +docs/**/*.md text eol=lf diff --git a/docs/technical-notes.md b/docs/technical-notes.md index 0e5142f790..c3ce68259d 100644 --- a/docs/technical-notes.md +++ b/docs/technical-notes.md @@ -7,6 +7,9 @@ here instead of between the options. ## Rejected-request token measurements Context-overflow recovery distinguishes the upstream context window from the rejected request's total, input-message count, and completion reservation. The parser accepts both the OpenAI/DeepSeek parenthesized split (`requested N tokens (M in the messages, C in the completion)`) and the SGLang/LiteLLM colon-separated split (`requested a total of N tokens: M tokens from the input messages and C tokens for the completion`). Explicit input counts feed the existing calibration ring; completion counts arm the existing output-headroom reservation. A window-only or incomplete error provides no invented token measurements. This parsing is shared by plugin and proxy modes and does not change the declared model window. + +When a 400/413 reports a complete, consistent token split and the input fits, the relay can retry that payload once with the output budget capped at the measured remaining space. It checks the reported completion reservation against the actual output field and preserves an explicit thinking budget. The same recovery applies after a refold. Requests with an egress transform configured use the existing refold path because the transform can change the input between attempts. + ## Fork recovery after a short source view Fork adoption judges retained compression blocks against the child replay, rather than the source's last `active` flags. A short source request may temporarily deactivate a fold whose complete original span returns in the child. Fully present retained spans are copied as active blocks; explicitly expanded blocks, consumed tier children, and spans crossing the fork boundary are not independently activated. Tier children accompany an adopted ancestor as inactive records. Planning and adoption never mutate the source session. diff --git a/docs/technical-notes.zh-CN.md b/docs/technical-notes.zh-CN.md index 89d4515675..f051d78c66 100644 --- a/docs/technical-notes.zh-CN.md +++ b/docs/technical-notes.zh-CN.md @@ -5,6 +5,9 @@ README 三种使用方式背后的机制级说明。README 里每种方式只保 ## 被拒请求的 token 计量 上下文溢出恢复分别处理上游窗口上限、被拒请求的总量、输入消息量和回复预留量。解析器同时识别 OpenAI/DeepSeek 的括号句式(`requested N tokens (M in the messages, C in the completion)`)及 SGLang/LiteLLM 的冒号句式(`requested a total of N tokens: M tokens from the input messages and C tokens for the completion`)。明确的输入计数进入现有校准环;回复计数触发现有的输出余量预留。只有窗口上限或拆分不完整的错误不会产生虚构计量。插件模式与代理模式共用该解析,不改写声明的模型窗口。 + +当 400/413 给出完整且一致的 token 计数,并且输入本身能放进窗口时,中继可以按实测剩余空间缩减回复额度,并用同一份输入重试一次。中继会核对报告的回复预留量与实际请求字段,并为显式思考预算保留空间。重新折叠后的请求也可以按同样规则恢复。配置了出站改写的请求继续走现有的重新折叠流程,因为改写可能让两次发送的输入不同。 + ## 来源短视图之后的 fork 恢复 fork 继承依据子会话重放内容判断保留的压缩块,不直接沿用来源上一次请求的 `active` 标记。来源的一次短请求可能暂时停用某个块,而子会话重放时包含它覆盖的完整原文。完整存在的保留块独立复制并激活;明确解压的块、被高层块消费的子块及跨越 fork 边界的块不会独立激活。被继承高层块的子块作为非活动记录随行。规划和继承均不修改来源会话。 diff --git a/hermes-plugin/__init__.py b/hermes-plugin/__init__.py index c9e04e4d8d..4551e30419 100644 --- a/hermes-plugin/__init__.py +++ b/hermes-plugin/__init__.py @@ -193,21 +193,44 @@ def sidecar_path() -> Path: return Path(__file__).resolve().parent / "bili.json" +def bundled_dist() -> Optional[Path]: + """A host-managed copy of the package: `hermes plugins install` runs npm in this + plugin's own directory when its package.json declares billion-context.""" + dist = Path(__file__).resolve().parent / "node_modules" / "billion-context" / "dist" / "index.js" + return dist if dist.is_file() else None + + +def path_dist() -> Optional[Path]: + """The reference lane (#1196): the global install's dist, reached through the `bili` + entry point on PATH — an executable symlink onto dist/index.js.""" + exe = shutil.which("bili") + if not exe: + return None + real = Path(os.path.realpath(exe)) + return real if real.is_file() and real.suffix == ".js" else None + + def load_sidecar() -> Optional[Dict[str, str]]: - """The installer's sidecar: which bili dist + node binary to spawn.""" + """Which bili dist + node binary to spawn. First hit wins, nothing is ever written: + 1. bili.json, the sidecar `bili plugin install hermes` drops (reference lane); + 2. this plugin's node_modules, when the host installed the package itself; + 3. the global install's dist, via `bili` on PATH.""" try: data = json.loads(sidecar_path().read_text(encoding="utf-8")) except Exception: - return None + data = {} if not isinstance(data, dict): + data = {} + node = data.get("nodePath") or shutil.which("node") + if not node or not isinstance(node, str): return None script = data.get("proxyScript") - node = data.get("nodePath") or shutil.which("node") if not script or not isinstance(script, str) or not Path(script).is_file(): + script = "" + found = Path(script) if script else (bundled_dist() or path_dist()) + if found is None: return None - if not node or not isinstance(node, str): - return None - return {"script": script, "node": node} + return {"script": str(found), "node": node} # — gating -------------------------------------------------------------------- @@ -1871,8 +1894,9 @@ def register(ctx: Any) -> None: return sidecar = load_sidecar() if not sidecar: - logger.warning("billion-context: no usable bili.json sidecar next to the hermes plugin " - "(run `bili plugin install hermes`) — staying inert") + logger.warning("billion-context: no bili dist found (no usable bili.json sidecar, no " + "node_modules/billion-context, no `bili` on PATH) — run `bili plugin " + "install hermes` — staying inert") return # 1) Everything cheap and blocking-free goes in FIRST. The host caps import+register() at # its load deadline (10s) and ABANDONS the load on timeout — every registration made diff --git a/hermes-plugin/plugin.yaml b/hermes-plugin/plugin.yaml index 9ba101a03f..c46874b10c 100644 --- a/hermes-plugin/plugin.yaml +++ b/hermes-plugin/plugin.yaml @@ -6,5 +6,14 @@ provides_tools: - compress - decompress - acp_status + - acp_cache + - search_context + # withheld when imageRescale is off; the host warns on declared-but-unregistered, never fails + - acp_read_original_image +provides_hooks: + - pre_api_request +provides_middleware: + - llm_request +# alias for hosts that predate provides_hooks (plugins_cmd_catalog reads provides_hooks, then hooks) hooks: - pre_api_request diff --git a/package-lock.json b/package-lock.json index 5800875683..aa5de419bf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,7 @@ "bili-proxy": "dist/index.js" }, "devDependencies": { + "@deepseek-ai/cordis": "4.1.0", "@earendil-works/pi-coding-agent": "0.83.0", "@earendil-works/pi-tui": "0.83.0", "@types/node": "^22.0.0", @@ -104,6 +105,39 @@ "node": ">= 14.17.0" } }, + "node_modules/@deepseek-ai/cordis": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/@deepseek-ai/cordis/-/cordis-4.1.0.tgz", + "integrity": "sha512-CJrjNyBkKn1SfvBQUcgFYKFnLjS+uKrhdMsbHU60xabvi12d+xQ+IQSNlME5Z+B+4Zi6n4BDknKY94tEPeo47w==", + "dev": true, + "license": "MIT", + "dependencies": { + "@deepseek-ai/cosmokit": "~1.9.0", + "@standard-schema/spec": "^1.1.0" + }, + "bin": { + "cordis": "bin.js" + }, + "peerDependencies": { + "@deepseek-ai/cordis-plugin-include": "~1.1.0", + "@deepseek-ai/cordis-plugin-loader": "~1.1.0" + }, + "peerDependenciesMeta": { + "@deepseek-ai/cordis-plugin-include": { + "optional": true + }, + "@deepseek-ai/cordis-plugin-loader": { + "optional": true + } + } + }, + "node_modules/@deepseek-ai/cosmokit": { + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/@deepseek-ai/cosmokit/-/cosmokit-1.9.0.tgz", + "integrity": "sha512-5O1S53mZiJyZYcvxxD0Qv1EY7qFPSxbGyD8CbVK6gw7W9FvcXZj2YIX1ZkOqyQQ3Xt2rFkZDHJv0+V3Zmmp8CA==", + "dev": true, + "license": "MIT" + }, "node_modules/@earendil-works/pi-coding-agent": { "version": "0.83.0", "resolved": "https://registry.npmjs.org/@earendil-works/pi-coding-agent/-/pi-coding-agent-0.83.0.tgz", @@ -3610,6 +3644,13 @@ "url": "https://github.com/sindresorhus/is?sponsor=1" } }, + "node_modules/@standard-schema/spec": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@standard-schema/spec/-/spec-1.1.0.tgz", + "integrity": "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w==", + "dev": true, + "license": "MIT" + }, "node_modules/@tokenizer/inflate": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/@tokenizer/inflate/-/inflate-0.4.1.tgz", diff --git a/package.json b/package.json index ee35baf823..64d8438cea 100644 --- a/package.json +++ b/package.json @@ -68,6 +68,7 @@ "jimp": "1.6.1" }, "devDependencies": { + "@deepseek-ai/cordis": "4.1.0", "@earendil-works/pi-coding-agent": "0.83.0", "@earendil-works/pi-tui": "0.83.0", "@types/node": "^22.0.0", diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index daecb744c2..01e0252ca4 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -149,7 +149,7 @@ type ToolDefinition = { type CommandOutcome = { kind: "success" | "error"; text: string }; export type PluginContext = { - tools: { register: (definition: ToolDefinition) => unknown }; + tools: { register: (definition: ToolDefinition) => unknown; get?: (name: string) => unknown }; commands: { register: (command: { name: string; description: string; handler: (invocation?: CommandInvocation) => Promise }) => unknown }; agents: { currentInitiator?: () => AgentLike | undefined; get?: (id: string) => AgentLike | undefined }; // Runtime-info sources (#955), resolved via dynamic ctx.inject when the @@ -216,18 +216,9 @@ export function planNativeDsh(env: NodeJS.ProcessEnv): { mode: "off" | "attach" const state: NativeInterceptState = { origin: undefined, ready: Promise.resolve(undefined) }; -// One registration lifecycle per HOST CONTEXT. toolsReady is the ONLY gate -// for header stamping (pi.ts discipline): stamped headers flip the proxy into -// plugin mode, which suppresses wire tool injection — stamping before the -// local tools exist would send a tool-less request. -// #3380: registrations live in the host process's tool layer and die ONLY -// with their context (the apply() re-arm below is the sole reset). Proxy -// death merely moves the origin — the execute closures read the LIVE base -// (#983) — so proxy-death paths must NOT clear toolsReady: doing so forced a -// doomed re-registration that DSH rejects ("tool X is already registered"), -// locked the latch false forever, and silently stripped every request of its -// identity headers (wire mode → anonymous zero-fold ledgers → 413 on large -// sessions). +// Tool readiness belongs to one activation, including Cordis restarts that +// reuse their context. Proxy death only moves the live base read by execute +// closures (#3380); it does not invalidate surviving host registrations. type RegisterState = { base: string | undefined; toolsReady: boolean; @@ -982,39 +973,109 @@ function fireGiveUp(): void { armRecoveryTimer(); } -async function registerTools(ctx: PluginContext): Promise { - if (register.pending !== undefined) return register.pending; - const base = register.base; - if (register.toolsReady || base === undefined) return; - register.pending = (async () => { - const tools = await fetchManifest(base, "anthropic"); - // #2082: land on the CURRENT context, not the one that started this - // flight — a re-activation mid-fetch must not register into (or die - // on) the deactivated context captured at call time. - const target = activeCtx ?? ctx; +type ToolsOwner = { + ctx: PluginContext; + closed: boolean; + restoring: boolean; + definitions: Map; +}; +let toolsOwner: ToolsOwner | undefined; + +function toolsAreReady(): boolean { + const owner = toolsOwner; + if (owner === undefined) return register.toolsReady; + register.toolsReady = !owner.closed && owner.definitions.size > 0 + && (owner.ctx.tools.get === undefined ? register.toolsReady + : [...owner.definitions.keys()].every((name) => owner.ctx.tools.get?.(name) !== undefined)); + return register.toolsReady; +} + +function restoreTools(owner: ToolsOwner): void { + if (owner !== toolsOwner || owner.closed || owner.restoring) return; + owner.restoring = true; + register.toolsReady = false; + try { let adopted = 0; - for (const tool of tools) { + for (const [name, definition] of owner.definitions) { + if (owner.ctx.tools.get?.(name) !== undefined) { + adopted++; + continue; + } try { - target.tools.register(toolDefinition(tool)); + // The HOST owns the registration's lifetime: DSH's ToolsService + // attaches it to the calling fiber via layers.effect(ctx, …), so + // a restart or dispose removes it and tools/change lets the next + // activation restore (#6702). Proxy death runs no effect — the + // registration survives it (#3380). + owner.ctx.tools.register(definition); } catch (err) { - // #3380: idempotent adoption — a name this process ALREADY - // holds in the host tool layer throws "already registered"; - // that registration is exactly what we want, so skip it and - // register the rest (also converges a partial registration - // left by an earlier interrupted flight). if (!errMessage(err).includes("already registered")) throw err; - adopted += 1; + adopted++; } } if (adopted > 0 && !regAdoptLogged) { regAdoptLogged = true; persistClientEvent(`tool registration adopted ${adopted} pre-existing registration(s) from this process — continuing`); } - register.toolsReady = true; + register.toolsReady = owner.definitions.size > 0; + if (!toolsAreReady()) throw new Error("bili: tool registrations did not become visible"); + } finally { + owner.restoring = false; + } +} + +function beginToolsOwner(ctx: PluginContext): ToolsOwner { + if (ctx.effect === undefined && toolsOwner?.ctx === ctx && !toolsOwner.closed) return toolsOwner; + if (toolsOwner !== undefined) toolsOwner.closed = true; + const owner: ToolsOwner = { ctx, closed: false, restoring: false, definitions: new Map() }; + toolsOwner = owner; + register.toolsReady = false; + register.pending = undefined; + register.retryAt = 0; + ctx.effect?.(() => () => { + owner.closed = true; + if (toolsOwner === owner) { + register.toolsReady = false; + register.pending = undefined; + } + }, "bili: tool registration lifetime"); + ctx.on?.("tools/change", () => { + if (owner !== toolsOwner || owner.closed || owner.restoring || owner.definitions.size === 0) return; + // A real Cordis teardown fires tools/change from the tool disposers + // BEFORE the fiber's own disposers run: restoring while the fiber is + // past its active window (states beyond LOADING=1/ACTIVE=2 — FAILED, + // DISPOSED, UNLOADING) would resurrect registrations that teardown is + // releasing. Hosts without a fiber expose no state and keep restoring. + const fiberState = (owner.ctx as { fiber?: { state?: number } }).fiber?.state; + if (fiberState !== undefined && fiberState !== 1 && fiberState !== 2) return; + try { + restoreTools(owner); + } catch (err) { + register.toolsReady = false; + register.lastRegistrationError = errMessage(err); + armRecoveryTimer(); + } + }); + return owner; +} + +async function registerTools(ctx: PluginContext): Promise { + const owner = toolsOwner; + if (owner === undefined || owner.closed || owner.ctx !== ctx) return; + if (register.pending !== undefined) return register.pending; + const base = register.base; + if (toolsAreReady() || base === undefined) return; + const pending = (async () => { + const tools = await fetchManifest(base, "anthropic"); + if (owner !== toolsOwner || owner.closed || register.base !== base) return; + owner.definitions = new Map(tools.map((tool) => [tool.name, toolDefinition(tool)])); + restoreTools(owner); register.failStreak = 0; register.lastRegistrationError = undefined; })() .catch((err: unknown) => { + if (owner !== toolsOwner || owner.closed) return; + register.toolsReady = false; // Cordis deactivates the plugin context while the host tears down // (dsh --help, early CLI exits): every later register attempt hits // "cannot get required service ... in inactive context" and would @@ -1042,9 +1103,10 @@ async function registerTools(ctx: PluginContext): Promise { persistClientEvent(`manifest registration failed (${errMessage(err)}) — retrying; requests stay in wire mode until it succeeds`); }) .finally(() => { - register.pending = undefined; + if (owner === toolsOwner && register.pending === pending) register.pending = undefined; }); - return register.pending; + register.pending = pending; + return pending; } // #2082: recovery retries are traffic-driven (headersFor on model @@ -1057,13 +1119,7 @@ async function registerTools(ctx: PluginContext): Promise { // ops knob); absent it tracks RETRY_INTERVAL_MS. Tests that do not opt in // never start a timer at all (NODE_TEST_CONTEXT guard keeps suites quiet). let recoveryTimer: NodeJS.Timeout | undefined; -// #2082: the LATEST context seen by apply()/maybeRetry. Every async recovery -// path (the timer tick below, an in-flight registration landing after a -// re-activation) must resolve its context at landing time, not from a capture -// taken when the retry was armed — a captured context may already be -// deactivated by then (dsh reload / profile switch), and registering into it -// throws "inactive context" and would re-poison register.dead, exactly what -// this fix removes. +// #2082: the recovery timer follows the latest activation's context. let activeCtx: PluginContext | undefined; function recoveryIntervalMs(): number { const raw = process.env.BILI_DSH_RECOVERY_INTERVAL_MS; @@ -1091,7 +1147,10 @@ function armRecoveryTimer(): void { } function maybeRetry(ctx: PluginContext): void { + if (toolsOwner === undefined) beginToolsOwner(ctx); + if (toolsOwner?.closed || toolsOwner?.ctx !== ctx) return; activeCtx = ctx; + toolsAreReady(); // #3380: the base-less state heals through the RESPAWN branch below even // when toolsReady is true (registrations survive proxy death) — gating on // toolsReady alone would strand a given-up register with no driver left. @@ -1909,28 +1968,7 @@ export async function apply(ctx: PluginContext): Promise { // (disabling the plugin must not brick session creation, #3662). declareDshCompactionActive(); - // #2082: a fresh activation invalidates the "host tearing down" verdict. - // Without this reset, ONE registerTools rejection carrying "inactive - // context" (a cordis deactivate/reactivate cycle — dsh reloading the - // plugin after the desktop profile copy was swapped in place, a profile - // switch, a settings toggle) poisons register.dead for the whole process - // lifetime: every later maybeRetry() returns immediately and the bili - // tools never come back until dsh restarts. - // #2101: a re-activation ALSO invalidates a SUCCESSFUL registration. - // Registrations die with their context: the tools registered into the - // previous activation are disposed when that context deactivates, but - // register.toolsReady keeps its stale stamp — and every - // registerTools()/maybeRetry() early-returns on it, so the new context - // never gets the bili tools (the #2082 symptom reached from the success - // ordering: a settings toggle, a profile switch or a plugin reload - // AFTER a healthy boot loses the tools until dsh restarts). Clear the - // stale stamp so the retry loop re-registers into the live context; - // retryAt goes with it (#1783: a fresh activation must not stand behind - // a stale back-off wall). - if (register.toolsReady && activeCtx !== undefined && activeCtx !== ctx) { - register.toolsReady = false; - register.retryAt = 0; - } + const owner = beginToolsOwner(ctx); register.dead = false; // #3380: a fresh activation starts a clean registration cycle — stale // failure bookkeeping from the previous context must not carry over @@ -2750,7 +2788,7 @@ export async function apply(ctx: PluginContext): Promise { resolveToolsGate(); } else { void state.ready.then(async (origin) => { - if (origin !== undefined) await registerTools(ctx).catch(() => {}); + if (origin !== undefined && owner === toolsOwner && !owner.closed) await registerTools(ctx).catch(() => {}); resolveToolsGate(); }); } @@ -2919,6 +2957,8 @@ export function _resetRegisterForTest(base: string | undefined, toolsReady = fal recoveryTimer = undefined; } activeCtx = undefined; + if (toolsOwner !== undefined) toolsOwner.closed = true; + toolsOwner = undefined; // #2187: apply() now reaches the respawn arming on every plan, so a stale // single-flight wrapper (capturing a previous test's spawn stub) must not // survive a reset. diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index 1e22572fe2..1c4d4da2cd 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -1872,6 +1872,7 @@ export function settleUsageReport( // #793: a zero-total sample carries no information (gateway placeholder or // relay echo) — it must not clobber the last trusted lastInputTokens. if (s.total > 0) { + if (session.transient?.nudgeHeldAfterReject) delete session.transient.nudgeHeldAfterReject; session.stats.inputTokens += s.total; // #4899: USD spend ledger — input/cache-read/cache-write billed at the // per-request stamped price, in lockstep with the token counters above diff --git a/src/client-ua-registry.ts b/src/client-ua-registry.ts index 48910b16d2..29903b3efe 100644 --- a/src/client-ua-registry.ts +++ b/src/client-ua-registry.ts @@ -19,6 +19,7 @@ import { isCodexClient } from "./codex-compact.js"; import { sniffScanClient } from "./thirdparty-scan.js"; +import { safePrefix } from "./text-safe.js"; // Read directly (wire literal) instead of via plugin.ts — same discipline as // codex-compact.ts / session-id.ts: importing plugin.ts back into the request @@ -62,7 +63,7 @@ export function clientFamilyOf(headers: Record): void { const raw = headerString(headers, "user-agent"); if (!raw || !raw.trim()) return; - const ua = raw.trim().slice(0, UA_MAX_CHARS); + const ua = safePrefix(raw.trim(), UA_MAX_CHARS); const now = Date.now(); let fam = families.get(clientFamilyOf(headers)); if (!fam) { diff --git a/src/incremental-lane.ts b/src/incremental-lane.ts index 1e706dd204..9b8f6b4d9c 100644 --- a/src/incremental-lane.ts +++ b/src/incremental-lane.ts @@ -77,7 +77,10 @@ export const INCREMENTAL_TURN_PATH = "/__bili/plugin/turn"; * conversations, side lanes) — the shape is the final word, so a marker'd * ordinary body replays through the normal pipeline untouched. */ export function incrementalTurnBodyShape(raw: Buffer): boolean { - if (raw.length < 2 || raw.length > 64 * 1024 * 1024) return false; + // readBody already enforces the receive-size budget before this check. + // A large lane-only envelope must not fall through to ordinary upstream + // relay merely because its seed crosses a second, unrelated size limit. + if (raw.length < 2) return false; let parsed: unknown; try { parsed = JSON.parse(raw.toString("utf8")); diff --git a/src/launcher.ts b/src/launcher.ts index e394bd3444..b09d4a80ad 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -7693,7 +7693,7 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} const mcpFile = path.join(os.tmpdir(), `bili-mcp-${Date.now()}.json`); fs.writeFileSync(mcpFile, JSON.stringify(buildMcpConfig(origin))); tmpFiles.push(mcpFile); - clientArgs = ["--mcp-config", mcpFile, ...clientArgs]; + clientArgs = [`--mcp-config=${mcpFile}`, ...clientArgs]; } } diff --git a/src/loop/adapter-anthropic.ts b/src/loop/adapter-anthropic.ts index 5cf936aedc..0ed396aa03 100644 --- a/src/loop/adapter-anthropic.ts +++ b/src/loop/adapter-anthropic.ts @@ -332,8 +332,9 @@ export function createAnthropicAdapter(requestBody: Record, ori const requestText = JSON.stringify(requestBody); const makeFilter = () => composeStreamFilters( composeStreamFilters( - createTagEchoFilter((snippet) => { - loggerLog("warn", `[tag-echo] stripped model-emitted render tag: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); + createTagEchoFilter((snippet, duringDrain) => { + const what = duringDrain === true ? "held head dropped during drain" : "stripped model-emitted render tag"; + loggerLog("warn", `[tag-echo] ${what}: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); }, (snippet) => { // #2190: residue audit — log-only, see plugin.ts twins. loggerLog("warn", `[tag-echo] filter released echo-residue-shaped bytes (#2190): ${snippet.slice(0, 80).replace(/\n/g, " ")}`); diff --git a/src/loop/adapter-commandcode.ts b/src/loop/adapter-commandcode.ts index 0f8f6b5e6f..ff16644df3 100644 --- a/src/loop/adapter-commandcode.ts +++ b/src/loop/adapter-commandcode.ts @@ -81,8 +81,9 @@ export function createCommandcodeAdapter( async *parseStream(upstream: ReadableStream, round: number): AsyncGenerator { void round; - const tagFilter = composeStreamFilters(createTagEchoFilter((snippet) => { - loggerLog("warn", `[tag-echo] stripped model-emitted render tag (commandcode proxy mode): ${snippet.slice(0, 80).replace(/\n/g, " ")}`); + const tagFilter = composeStreamFilters(createTagEchoFilter((snippet, duringDrain) => { + const what = duringDrain === true ? "held head dropped during drain" : "stripped model-emitted render tag (commandcode proxy mode)"; + loggerLog("warn", `[tag-echo] ${what}: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); }), createMarkerLineFilter()); let sawReasoning = false; let sawRealToolCall = false; diff --git a/src/loop/adapter-google.ts b/src/loop/adapter-google.ts index 68971b419e..4d17d5d374 100644 --- a/src/loop/adapter-google.ts +++ b/src/loop/adapter-google.ts @@ -334,8 +334,9 @@ export function createGoogleAdapter( const requestText = JSON.stringify(requestBody); const makeFilter = () => composeStreamFilters( composeStreamFilters( - createTagEchoFilter((snippet) => { - loggerLog("warn", `[tag-echo] stripped model-emitted render tag: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); + createTagEchoFilter((snippet, duringDrain) => { + const what = duringDrain === true ? "held head dropped during drain" : "stripped model-emitted render tag"; + loggerLog("warn", `[tag-echo] ${what}: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); }, (snippet) => { // #2190: residue audit — log-only, see plugin.ts twins. loggerLog("warn", `[tag-echo] filter released echo-residue-shaped bytes (#2190): ${snippet.slice(0, 80).replace(/\n/g, " ")}`); diff --git a/src/loop/adapter-openai.ts b/src/loop/adapter-openai.ts index 2134b45790..009c8fbd79 100644 --- a/src/loop/adapter-openai.ts +++ b/src/loop/adapter-openai.ts @@ -282,8 +282,9 @@ export function createOpenaiAdapter(requestBody: Record, client const requestText = JSON.stringify(requestBody); const makeFieldFilter = () => composeStreamFilters( composeStreamFilters( - createTagEchoFilter((snippet) => { - loggerLog("warn", `[tag-echo] stripped model-emitted render tag: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); + createTagEchoFilter((snippet, duringDrain) => { + const what = duringDrain === true ? "held head dropped during drain" : "stripped model-emitted render tag"; + loggerLog("warn", `[tag-echo] ${what}: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); }, (snippet) => { // #2190: residue audit — log-only, see plugin.ts twins. loggerLog("warn", `[tag-echo] filter released echo-residue-shaped bytes (#2190): ${snippet.slice(0, 80).replace(/\n/g, " ")}`); diff --git a/src/loop/adapter-responses.ts b/src/loop/adapter-responses.ts index c764c4791f..a0decf5065 100644 --- a/src/loop/adapter-responses.ts +++ b/src/loop/adapter-responses.ts @@ -402,8 +402,9 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp const absorbArmedLocal = absorbName !== undefined; const makeFilter = () => composeStreamFilters( composeStreamFilters( - createTagEchoFilter((snippet) => { - loggerLog("warn", `[tag-echo] stripped model-emitted render tag: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); + createTagEchoFilter((snippet, duringDrain) => { + const what = duringDrain === true ? "held head dropped during drain" : "stripped model-emitted render tag"; + loggerLog("warn", `[tag-echo] ${what}: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); }, (snippet) => { // #2190: residue audit — log-only, see plugin.ts twins. loggerLog("warn", `[tag-echo] filter released echo-residue-shaped bytes (#2190): ${snippet.slice(0, 80).replace(/\n/g, " ")}`); diff --git a/src/loop/core.ts b/src/loop/core.ts index 4b66472153..fa301f487c 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -14,7 +14,7 @@ import { billOutputUsage } from "../billing.js"; import { maybeJudgeContinuity } from "../invariant-judge.js"; import type { SeparatorSlot } from "../server/responses-ordering.js"; import { countImagesInParsedBody, countImagesInRawBody } from "../image-tokens.js"; -import { diagnoseSuccessWithoutUsage, lastCompressSuffix, withSessionLock, type Session } from "../session.js"; +import { diagnoseSuccessWithoutUsage, lastCompressSuffix, releaseNudgeHold, withSessionLock, type Session } from "../session.js"; import type { BiliMessage } from "acp-kernel/wire"; import { coreToAnthropic } from "acp-kernel/wire"; import { @@ -1396,6 +1396,7 @@ export async function* runCompressLoop( recordUsage(ctx, usage, round); } else { diagnoseSuccessWithoutUsage(ctx.session, "acp-loop"); + releaseNudgeHold(ctx.session); } let resolvedText = assistantText; let allCalls = calls; diff --git a/src/loop/tag-echo-filter.ts b/src/loop/tag-echo-filter.ts index fb4f1d33f0..7b47e93265 100644 --- a/src/loop/tag-echo-filter.ts +++ b/src/loop/tag-echo-filter.ts @@ -247,6 +247,34 @@ const BROKEN_ATTRS = new RegExp("\x3c" + NAME + "\\s[^<>]*(?=\x3c)"); const MANGLED_OPEN = new RegExp("\x3c" + NAME + "\\s*=\\s*[^<>]*>"); const DEFINITE_TAIL = new RegExp("^\x3c" + NAME + "\\s|^\x3c" + NAME + "=|^\x3c\\/" + NAME); const OPEN_WITH_ATTRS = new RegExp("^\x3c" + NAME + "\\s"); +// #6557: a model-emitted opening whose `type` names a TOOL lane +// (`type="tool:acp_cache"`) is not prose wearing tags. The kernel renders tool +// results under that type, so a model writing one in assistant TEXT is +// inventing a tool result: the body between the tags is fabricated output, not +// its own words. The #1720/#2023 rule — "content that is not refs-only is +// prose wearing tags: the tags go, the content stays" — was written for +// `type="text"`, where the body IS the model's prose; applied to a tool-typed +// echo it commits the fabricated result into the assistant turn, and the model +// then imitates "tag + that body" at the head of every later reply +// (production session, #6557: the same JSON block 25 times in 23 minutes). So +// a tool-typed span dies WHOLE — attrs through the close, or through the end +// of the field when the imitation never closes — in the whole-text path +// (TOOL_TYPED_PAIR) and in the streaming swallow (swallowReleases, below). A +// self-closing imitation is a complete unit, not an opening — it dies in place +// via SELF_CLOSE, prose after it surviving. +// Case folds per #1731: an echoed type drifts casing the same way an echoed +// name does. The quote after `type=` is part of the match, so a `type=` that +// appears inside another attribute's VALUE (an unquoted-looking `data-x=a +// type=tool:` run) cannot be mistaken for the attribute itself. +const TOOL_TYPE_ATTR = "\\s+[tT][yY][pP][eE]\\s*=\\s*[\"']\\s*[tT][oO][oO][lL]:"; +const TOOL_TYPE_ATTR_RE = new RegExp(TOOL_TYPE_ATTR); +// The `[^/<>]` before the final `>` excludes the SELF-CLOSING form +// (``): that is a complete unit, not an opening, so it must not be +// read as one and made to own the rest of the field. It falls through to the +// SELF_CLOSE pass (killed in place) — the same verdict the streaming path +// reaches via candidate order, keeping both paths split-invariant (#4178/#4210). +const TOOL_TYPED_OPEN = new RegExp("\x3c" + NAME + "(?:\\s[^<>]*?)?" + TOOL_TYPE_ATTR + "[^<>]*[^/<>]>"); +const TOOL_TYPED_PAIR = new RegExp(TOOL_TYPED_OPEN.source + "[\\s\\S]*?\x3c\\/" + NAME + ">"); const CLOSE_HEAD = "\x3c/"; const CLOSE_NAME_ANCHORED = new RegExp("^" + NAME); const HOLD_LIMIT = 128; @@ -468,6 +496,24 @@ function stripPass(text: string, dropToolCallEmission: boolean, requestText: str if (wrapped === null) break; out = out.slice(0, wrapped.start) + out.slice(wrapped.end); } + // #6557: a tool-typed echo dies WHOLE (see TOOL_TYPE_ATTR). RUNS BEFORE the + // paired/lone-open passes for the same reason the wrapped span does: either + // of those would strip the markup alone and leave the fabricated tool + // result as assistant text — the exact residue this channel exists to + // prevent. An opening with no close in the field owns the rest of it: the + // imitation was never terminated, so everything after it is its payload + // (the wrapped-turn EOF rule, reused). + for (;;) { + const open = TOOL_TYPED_OPEN.exec(out); + if (open === null) break; + const pair = TOOL_TYPED_PAIR.exec(out); + if (pair !== null && pair.index === open.index) { + out = out.slice(0, pair.index) + out.slice(pair.index + pair[0].length); + continue; + } + out = out.slice(0, open.index); + break; + } out = out.replace(new RegExp(PAIRED.source, "g"), ""); // #2023: a refs run sitting directly against an UNMATCHED close is residue // (its open was consumed elsewhere or never came) and goes with the close. @@ -546,9 +592,10 @@ export function stripAcpTags(text: string, dropToolCallEmission = false, request * so entry-vs-exit comparisons never false-positive on shapes one side * legitimately removed. Single source of truth by construction — when the * filter family grows, the judge's normalization grows with it. Content- - * preserving for prose (paired shapes keep their payload; only tag bytes go), - * and the tool-call emission span is deliberately NOT touched (args are user - * intent, #1039). */ + * preserving for prose (paired shapes keep their payload — except a tool-typed + * span, whose body is a fabricated tool result and dies with the tags, #6557; + * otherwise only tag bytes go), and the tool-call emission span is + * deliberately NOT touched (args are user intent, #1039). */ export function normalizeEchoArtifacts(text: string): string { if ( !containsRenderTagText(text) && @@ -1007,7 +1054,7 @@ export function createBiliArtifactFilter(onDrop?: (snippet: string) => void): Ta }; } -export function createTagEchoFilter(onDrop?: (snippet: string) => void, onResidueWarn?: (snippet: string) => void, absorbInstructed?: boolean, requestText?: string): TagEchoFilter { +export function createTagEchoFilter(onDrop?: (snippet: string, duringDrain?: boolean) => void, onResidueWarn?: (snippet: string) => void, absorbInstructed?: boolean, requestText?: string): TagEchoFilter { let held = ""; /** Settled prose deferred alongside a NON-definite tag-head tail: it * carries a dead ref-close unit whose verdict is content-determined @@ -1055,25 +1102,37 @@ export function createTagEchoFilter(onDrop?: (snippet: string) => void, onResidu // the cap drops repeatedly until its close. swallowSessionCounted keeps // any one swallow session at exactly one count. let swallowSessionCounted = false; - const recordDrop = (snippet: string) => { + // #5192: a drain drop is a different event from an ordinary strip — the + // stream ended while the filter still held bytes, so the one-shot notice + // below (spent by the response's FIRST drop) left it with no line at all. + // It gets its own one-shot budget so a held head at EOF is always audible. + let drainNoticed = false; + const recordDrop = (snippet: string, duringDrain = false) => { droppedAny = true; + if (duringDrain) { + if (onDrop && !drainNoticed) { + drainNoticed = true; + onDrop(snippet, true); + } + return; + } if (onDrop && !notified) { notified = true; onDrop(snippet); } }; /** One self-contained artifact resolved by a single drop call. */ - const drop = (snippet: string) => { + const drop = (snippet: string, duringDrain = false) => { dropCount += 1; - recordDrop(snippet); + recordDrop(snippet, duringDrain); }; /** Any drop inside a live swallow session — the session counts once. */ - const dropSwallowSpan = (snippet: string) => { + const dropSwallowSpan = (snippet: string, duringDrain = false) => { if (!swallowSessionCounted) { dropCount += 1; swallowSessionCounted = true; } - recordDrop(snippet); + recordDrop(snippet, duringDrain); }; /** #2862: strip dead ref-close units (ref + unterminated close head) from * a span about to be RELEASED as prose; each unit is one self-contained @@ -1430,9 +1489,17 @@ export function createTagEchoFilter(onDrop?: (snippet: string) => void, onResidu // than released at the #644 budget. Every genuine opening has // balanced quotes: `tokens="1" type="text"`. const wrapped = ((m[0].match(/"/g) ?? []).length & 1) === 1; + // #6557: a tool-typed opening frames a FABRICATED tool result, + // not the model's prose (see TOOL_TYPE_ATTR) — its body is + // discarded on the attested-imitation budget instead of being + // released at the #644 one, in both the closed and the + // never-closed (EOF) shape. Gated to attrs-bearing openings: a + // bare opening carries no type at all, so its #1881 + // release-as-prose path is untouched. + const toolTyped = !bare && TOOL_TYPE_ATTR_RE.test(m[0]); swallowUntilClose = true; - swallowLimit = wrapped ? IMITATION_SWALLOW_CAP : SWALLOW_CAP; - swallowReleases = !wrapped; + swallowLimit = wrapped || toolTyped ? IMITATION_SWALLOW_CAP : SWALLOW_CAP; + swallowReleases = !wrapped && !toolTyped; swallowBareOpen = bare; swallowed = ""; } else { @@ -1522,7 +1589,7 @@ export function createTagEchoFilter(onDrop?: (snippet: string) => void, onResidu // imitation (never released, #1720), or an attrs-bearing // opening whose tail never closed — that tail is tag content // (a ref, possibly truncated), not prose (#644 EOF rule). - if (rest.length > 0) dropSwallowSpan(rest); + if (rest.length > 0) dropSwallowSpan(rest, true); result = ""; } else if (wasSwallowing) { // A BARE opening (#1881): prose may genuinely wear one, so an @@ -1531,15 +1598,15 @@ export function createTagEchoFilter(onDrop?: (snippet: string) => void, onResidu // truncated open/close tail is dead markup. const t = new RegExp(TRUNC_OPEN.source).exec(rest); if (t) { - dropSwallowSpan(t[0]); + dropSwallowSpan(t[0], true); result = rest.slice(0, t.index); } else { const tc = new RegExp(TRUNC_CLOSE.source).exec(rest); if (tc) { - dropSwallowSpan(tc[0]); + dropSwallowSpan(tc[0], true); result = rest.slice(0, tc.index); } else if (REFS_ONLY.test(rest)) { - dropSwallowSpan(rest); + dropSwallowSpan(rest, true); result = ""; } else { // Payload accumulated under the session may carry @@ -1564,13 +1631,13 @@ export function createTagEchoFilter(onDrop?: (snippet: string) => void, onResidu if (payload === "") break; const t2 = new RegExp(TRUNC_OPEN.source).exec(payload); if (t2 !== null) { - dropSwallowSpan(t2[0]); + dropSwallowSpan(t2[0], true); r += payload.slice(0, t2.index); break; } const tc2 = new RegExp(TRUNC_CLOSE.source).exec(payload); if (tc2 !== null) { - dropSwallowSpan(tc2[0]); + dropSwallowSpan(tc2[0], true); r += payload.slice(0, tc2.index); break; } @@ -1579,7 +1646,7 @@ export function createTagEchoFilter(onDrop?: (snippet: string) => void, onResidu // ahead of a refs tail was emitted, the tail is a // prose-cited ref and must be released (#1760). if (r === "" && REFS_ONLY.test(payload)) { - dropSwallowSpan(payload); + dropSwallowSpan(payload, true); break; } if (payload === prev || guard >= 64) { @@ -1596,12 +1663,12 @@ export function createTagEchoFilter(onDrop?: (snippet: string) => void, onResidu } else { const t = new RegExp(TRUNC_OPEN.source).exec(rest); if (t) { - drop(t[0]); + drop(t[0], true); result = rest.slice(0, t.index); } else { const tc = new RegExp(TRUNC_CLOSE.source).exec(rest); if (tc) { - drop(tc[0]); + drop(tc[0], true); result = rest.slice(0, tc.index); } else { result = rest; diff --git a/src/plugin.ts b/src/plugin.ts index 7eaed67704..45cf4e2e38 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -8,7 +8,7 @@ import path from "node:path"; import { createHash } from "node:crypto"; import { CORRUPT_SIBLING_CAP, getStore, pruneCorruptSiblings, storageStatusLabel, kvStore } from "./persist.js"; import { cloneStoreForRefs } from "./store.js"; -import { acquireInFlight, createSession, getSessionAsync, publishForkSession, diagnoseSuccessWithoutUsage, effectiveConfig, findSessionByCanonicalId, listSessions, markCompactionBoundary, markDirty, noteAbortedTurn, noteTruncatedUpstream, peekSession, releaseInFlight, statusInputBaseline, withSessionLock, type Session } from "./session.js"; +import { acquireInFlight, createSession, getSessionAsync, publishForkSession, diagnoseSuccessWithoutUsage, effectiveConfig, findSessionByCanonicalId, listSessions, markCompactionBoundary, markDirty, noteAbortedTurn, noteTruncatedUpstream, peekSession, releaseInFlight, releaseNudgeHold, statusInputBaseline, withSessionLock, type Session } from "./session.js"; import { clientConversationHeader } from "./session-id.js"; import { buildContextView } from "./context-view.js"; import { ABSORB_TOOL_NAME, BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_OPENAI_NO_RANGE, BILI_ACP_TOOLS_RESPONSES_NO_RANGE, PROXY_TOOL_NAMES, READ_ORIGINAL_IMAGE_TOOL, READ_ORIGINAL_IMAGE_TOOL_NAME, READ_ORIGINAL_IMAGE_TOOL_OPENAI, READ_ORIGINAL_IMAGE_TOOL_RESPONSES, RETRIEVE_TOOL_NAME, RULE_TOOL, RULE_TOOL_NAME, RULE_TOOL_OPENAI, RULE_TOOL_RESPONSES, SEARCH_CONTEXT_TOOL_NAME, absorbToolsFor, blockOpsTools, retrieveToolsFor, withAcpNotes, withAcpNotesOpenAI } from "./compress-tool.js"; @@ -2810,10 +2810,11 @@ export async function pipePluginChatWithStrip( // write through the request logger landed in the SAME tee file, doubling // every count taken from bili.log. const who = session ? `[${session.id}] ` : ""; - const onTagDrop = (snippet: string) => { + const onTagDrop = (snippet: string, duringDrain?: boolean) => { droppedTagInFrame = true; sawStrippedEcho = true; - loggerLog("warn", `${who}[tag-echo] stripped model-emitted render tag (plugin passthrough): ${snippet.slice(0, 80).replace(/\n/g, " ")}`); + const what = duringDrain === true ? "held head dropped during drain" : "stripped model-emitted render tag (plugin passthrough)"; + loggerLog("warn", `${who}[tag-echo] ${what}: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); }; // #2190: the filter released bytes that still carry echo-residue shape — // the leak is now observable instead of silent. Log-only; the bytes were @@ -3288,7 +3289,10 @@ export async function pipePluginChatWithStrip( // #1595: clean completion but no input usage sample — name it (the // cut-stream paths below have sawTerminal=false, so they stay // distinguishable from transport failures). - if (sawTerminal && acc.inputTokens === undefined) diagnoseSuccessWithoutUsage(session, `plugin-passthrough-${protocol}`); + if (sawTerminal && acc.inputTokens === undefined) { + diagnoseSuccessWithoutUsage(session, `plugin-passthrough-${protocol}`); + releaseNudgeHold(session); + } }; // #498: whether a terminal event ([DONE] / message_stop) was seen. A // stream that ends without one was cut mid-flight. @@ -3832,7 +3836,10 @@ export async function pipePluginChatWithStrip( // structured or user data (#1039) and are excluded from the audit. if (!googleFrameHasNonText(ev)) auditRawForward(rawEvent); if (drain.length > 0) return drain + rawEvent + "\n\n"; - if (!hadText && anyPending()) return flushTails() + rawEvent + "\n\n"; + // #5848 twin on the google lane: a frame carrying only empty parts has + // nothing a held tail must precede, and draining on it split a render tag + // so the remainder reached the host as prose (the #5192 shape). + if (!hadText && anyPending() && googleFrameHasNonText(ev)) return flushTails() + rawEvent + "\n\n"; return rawEvent + "\n\n"; }; try { @@ -4066,9 +4073,11 @@ export async function pipePluginJsonlWithStrip( // delta as a whole line, so the filters' held tails are only resolved at // the terminal frame / EOF / read failure — never per line (a per-line // flush destroys the cross-delta tag/marker state this filter exists for). - const onStrip = (snippet: string) => { - loggerLog("warn", `[tag-echo] stripped model-emitted render tag (commandcode plugin mode): ${snippet.slice(0, 80).replace(/\n/g, " ")}`); - log?.(`[tag-echo] stripped model-emitted render tag: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); + const onStrip = (snippet: string, duringDrain?: boolean) => { + const what = duringDrain === true ? "held head dropped during drain" : "stripped model-emitted render tag (commandcode plugin mode)"; + const head = duringDrain === true ? "held head dropped during drain" : "stripped model-emitted render tag"; + loggerLog("warn", `[tag-echo] ${what}: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); + log?.(`[tag-echo] ${head}: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); }; const textFilter = composeStreamFilters(createTagEchoFilter(onStrip), createMarkerLineFilter()); const reasonFilter = composeStreamFilters(createTagEchoFilter(onStrip), createMarkerLineFilter()); @@ -4386,8 +4395,9 @@ export async function pipePluginResponsesWithStrip( if (diagLastTypes.length > 5) diagLastTypes.shift(); }; const who = session ? `[${session.id}] ` : ""; - const onTagDrop = (snippet: string) => { - loggerLog("warn", `${who}[tag-echo] stripped model-emitted render tag (plugin passthrough): ${snippet.slice(0, 80).replace(/\n/g, " ")}`); + const onTagDrop = (snippet: string, duringDrain?: boolean) => { + const what = duringDrain === true ? "held head dropped during drain" : "stripped model-emitted render tag (plugin passthrough)"; + loggerLog("warn", `${who}[tag-echo] ${what}: ${snippet.slice(0, 80).replace(/\n/g, " ")}`); }; // #2190: log-only residue audit — see the twin above. const onResidueWarn = (snippet: string) => { @@ -4528,7 +4538,10 @@ export async function pipePluginResponsesWithStrip( markDirty(session); } // #1595: same as the chat-pipe twin — sawTerminal gates out cuts. - if (sawTerminal && acc.inputTokens === undefined) diagnoseSuccessWithoutUsage(session, "plugin-passthrough-responses"); + if (sawTerminal && acc.inputTokens === undefined) { + diagnoseSuccessWithoutUsage(session, "plugin-passthrough-responses"); + releaseNudgeHold(session); + } }; // #498: whether a terminal event (done-family / [DONE]) was seen. A // stream that ends without one was cut mid-flight. @@ -5263,7 +5276,10 @@ export async function pipePluginJson( } } // #1595: parsed success body carrying no input usage report — name it. - if (session && !sawInputSample) diagnoseSuccessWithoutUsage(session, "plugin-json"); + if (session && !sawInputSample) { + diagnoseSuccessWithoutUsage(session, "plugin-json"); + releaseNudgeHold(session); + } // #1685: a non-streaming plugin response still carries the model's // complete tool calls — same witness feed as the SSE pipes, from the // one full-body parse this path already does. Observe-only (#1039). diff --git a/src/server.ts b/src/server.ts index 03912500e6..7255880683 100644 --- a/src/server.ts +++ b/src/server.ts @@ -55,7 +55,7 @@ import { prepareResponses, prepareResponsesCompact } from "./server/prepare-resp import { FORCE_TEXT_PROTOCOL } from "./server/inject.js"; export { isChatGptCodexUpstream, isCodexResponsesLite, resolvePromptCacheKey, shouldInjectPromptCacheKey } from "./server/prepare-responses.js"; import { biliToolsDeclaredOnWire, countBiliToolUses, evaluateSelfHealRound, nudgeSuppressed, pluginLaneDegraded, pluginLaneRestore } from "./session-self-heal.js"; -import { getSession, hasProcessedState, listSessions, peekSession, type PendingRetrieval, type Session, initSessions, markDirty, flushAllSessions, acquireInFlight, releaseInFlight, totalInFlight, snapshotMessages, ensureCanonicalId, storeEffectiveConfig, foldCoverage, postRebuildAnchorTokens, setPostRebuildAnchor, tickPostRebuildAnchor, splitSessionWarnings, usageAnchorCycleStale, staleSmallAnchorFallback } from "./session.js"; +import { getSession, hasProcessedState, listSessions, peekSession, type PendingRetrieval, type Session, type NudgeStamps, initSessions, markDirty, flushAllSessions, acquireInFlight, releaseInFlight, totalInFlight, snapshotMessages, ensureCanonicalId, storeEffectiveConfig, foldCoverage, postRebuildAnchorTokens, setPostRebuildAnchor, tickPostRebuildAnchor, splitSessionWarnings, usageAnchorCycleStale, staleSmallAnchorFallback } from "./session.js"; import type { SeparatorManifest } from "./server/responses-ordering.js"; import { detectStaleInstall, isVersionNewer } from "./update.js"; import { decideEaddrinuse, eaddrinuseExitCode, isPortBusyError, postBindCanLadder, UPGRADE_REPLACEMENT_ENV, type FailFastReason } from "./listen-policy.js"; @@ -2059,6 +2059,8 @@ export type Prepared = { * before delivery was attempted (suppression / breaker / gate off) — * consumers test for "injected" (+ "gated" where pressure counts). */ nudgeOutcome?: "injected" | "vetoed" | "skipped" | "gated"; + /** The nudge stamps before this turn, so a rejected request can un-show its nudge (rejectNudge). */ + nudgeStampsBefore?: NudgeStamps; /** Render strategy the prepare used for processTurn ("none" for codex * compaction triggers / ACP_RENDER_NONE). The #422 fold-refresh hook in * forward() re-runs processTurn with the same strategy so the re-request @@ -4863,5 +4865,5 @@ function logMsg(opts: ProxyOptions, level: string, msg: string): void { export { getUnrecognizedPathStats, logDumpFailure, logUnrecognizedPath } from "./server/observability.js"; export { BILI_HOP_HEADER, parseLauncherModelWindows, anthropicBetaContextWindow, anthropicBetaMessageThreads, capRegistryWindowByStandard, expandedContextSuffixWindow } from "./server/context-window.js"; -export { BILI_TOOL_NAMES, isSideRequest, isStructuredOutputCall, outputBudgetField, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools, _resetNoOutputCeilingWarningsForTest, _resetDshMidBandWarningsForTest, OUTPUT_RESTORE_ABSOLUTE_FLOOR, type OutputBudgetField } from "./server/side-request.js"; +export { BILI_TOOL_NAMES, isAudioOutputCall, isSideRequest, isStructuredOutputCall, outputBudgetField, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools, _resetNoOutputCeilingWarningsForTest, _resetDshMidBandWarningsForTest, OUTPUT_RESTORE_ABSOLUTE_FLOOR, type OutputBudgetField } from "./server/side-request.js"; export { countSystemAndToolsTokens, estimateInputTokens, estimateWireOverhead, clampOutputBudget, emergencyNudge, projectThinkingMass, type ThinkingMassInput } from "./server/budget.js"; diff --git a/src/server/budget.ts b/src/server/budget.ts index 014aaa23d2..fe1fa22c22 100644 --- a/src/server/budget.ts +++ b/src/server/budget.ts @@ -4,9 +4,9 @@ import { estimateCoreMessages } from "../preflight.js"; import { SELF_HEAL_SUPPRESS_CAP_MS, reductionKeyOf } from "../session-self-heal.js"; import type { Session } from "../session.js"; import { applyEstimateCalibration } from "../util.js"; -import { readOutputBudget, writeOutputBudget, type OutputBudgetField } from "./side-request.js"; +import { outputBudgetField, readOutputBudget, writeOutputBudget, type OutputBudgetField } from "./side-request.js"; -export { readOutputBudget }; +export { outputBudgetField, readOutputBudget, writeOutputBudget }; // #453 hard backstop: cap the forwarded output budget so input+output can never // exceed the window on request-rebuilding upstreams (vLLM rejects an oversized diff --git a/src/server/handle.ts b/src/server/handle.ts index 8263a88cb1..00122ada6a 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -70,7 +70,7 @@ import { buildForgeResponse, classifierForgeVariant, estimateCjkTokens, isAutoMo import { extractConversationId, isV1BackgroundSideRequest, isV1InternalRequest, unwrapV1Internal } from "../v1internal.js"; import { anthropicBetaContextWindow, anthropicBetaMessageThreads, BILI_HOP_HEADER, capRegistryWindowByStandard, expandedContextSuffixWindow, launcherContextWindow, launcherMaxOutput, noteUnresolvedModelSession, UNRESOLVED_MODEL_SESSION_THRESHOLD, windowSourceLogged } from "./context-window.js"; import { NO_IDENTITY_MESSAGE, safeSessionId } from "./headers.js"; -import { demoteGate, hasLeakedBiliToolsOnly, hasSingleShotBudget, isClaudeCodeTitleRequest, isDshAutoReviewRequest, isDshSingleShotSidecar, isDshTitleRequest, isServerToolUtilityCall, isSideRequest, isStructuredOutputCall, resolveSideLane, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools } from "./side-request.js"; +import { demoteGate, hasLeakedBiliToolsOnly, hasSingleShotBudget, isAudioOutputCall, isClaudeCodeTitleRequest, isDshAutoReviewRequest, isDshSingleShotSidecar, isDshTitleRequest, isResponsesServerToolSingleShot, isServerToolUtilityCall, isSideRequest, isStructuredOutputCall, resolveSideLane, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools } from "./side-request.js"; import { DSH_COMPACTION_SHAPE_MSGS, DSH_REFUSAL_STORM_LOG_EVERY, detectPassthroughDshCompactionCall, dshCompactionRefusal, dshPassthroughGuardStats, isDshCompactionCall } from "./dsh-compaction-guard.js"; import { buildDshCompactionForgeBody, renderDshAuthoredCheckpoint } from "./dsh-authored-compaction.js"; import { artifactSeedHit, detectAcpArtifacts } from "./chain-artifacts.js"; @@ -1627,6 +1627,19 @@ async function handleRequest( if (!clientProvided) { anonAffinity = prefixAffinity.resolve(affinityMessageList()); if (!anonAffinity) { + // #6548: a single-shot server-tool-only Responses call (the + // pi-web-access xAI web search: string input, web_search / + // x_search tools, no history) is not a conversation turn — it + // carries no history to fingerprint and creates no session + // state, so the #286 refusal's rationale does not apply to + // it, and the client cannot carry an identity here. Forward + // it verbatim (same raw relay as the #1117 passthrough) so + // the search backend keeps working instead of hard-400ing. + if (protocol === "responses" && isResponsesServerToolSingleShot(parsed)) { + log("info", `single-shot server-tool Responses call — no conversation identity possible; forwarding verbatim (#6548): ${maskUrlForLog(req.url ?? "")}`); + await forward(req, res, opts, scrubCompatDrop(scrubAnthropicPck(protocol, bodyBuffer, log), compatDropPaths, log), null, core, config, log, route, instanceId, undefined, undefined, undefined, googleModel ?? vertexModel); + return; + } log("warn", `400: no stable conversation identity on ${protocol} request → ${maskUrlsInText(upstreamOrigin)}; refusing to create a content-fingerprint session (#286)`); res.writeHead(400, { "content-type": "application/json" }); res.end(JSON.stringify(protocol === "anthropic" @@ -2422,6 +2435,14 @@ async function handleRequest( // outputBudgetHighWater from it would poison the first starved restore // (same class as the #1897/#2500 skip). const structuredOutputUtility = isStructuredOutputCall(parsed); + // #6636: a tool-less audio-output request (TTS on the chat wire) is + // the same category — a one-shot utility call under the main session + // id. It rides the #388 side passthrough via isSideRequest, and must + // ALSO skip restoreOutputBudget here: its max_tokens sizes the + // spoken reply, not a main turn, and seeding + // outputBudgetHighWater from it would poison the first starved + // restore (same class as the #1897/#2500/#5073 skip). + const audioOutputUtility = isAudioOutputCall(parsed); // #546: restore a client-shrunk output budget BEFORE the side gate so a // tool-carrying main request re-enters the pipeline at full budget (see // restoreOutputBudget for the starvation mechanism). #1665/#1840: the @@ -2433,7 +2454,7 @@ async function handleRequest( // outputBudgetHighWater from it would poison the first starved restore // (title requests arrive FIRST, at session start). Starved all-bili requests // never reach here demoted: stripLeakedBiliTools vetoes them per #546. - if (!demotedSide && !serverToolUtility && !structuredOutputUtility) { + if (!demotedSide && !serverToolUtility && !structuredOutputUtility && !audioOutputUtility) { // #3438: the dsh host contract (pi-ai buildBaseOptions) stamps the model-declared // maxTokens on every main turn, so on this lane a below-ceiling tool-carrying // budget is a raw-history collapse, not intent — arm the mid-band rescue. @@ -2635,7 +2656,7 @@ async function handleRequest( // #2500 server-tool utility shape) diverts under a receipt. // #2170 measure 1: the decision itself is resolveSideLane() (pure, // truth-table-tested); demotedSide ⊆ lane==="side" by construction. - const sideLane = resolveSideLane({ countTokens, responsesCompact, protocol, stripApplied: demotedSide, sideIntent, requestAgent, sideLabel: serverToolUtility ? "server-tool utility call (#2500)" : structuredOutputUtility ? "structured-output utility call (#5073)" : dshTitleSidecar ? "dsh title-gen sidecar (#2503)" : dshReviewSidecar ? "dsh auto-review sidecar (#4438)" : dshSingleShotSidecar ? "dsh single-shot sidecar (#5656)" : ccTitleSidecar ? "claude-code title-gen sidecar (#5135)" : undefined }); + const sideLane = resolveSideLane({ countTokens, responsesCompact, protocol, stripApplied: demotedSide, sideIntent, requestAgent, sideLabel: serverToolUtility ? "server-tool utility call (#2500)" : structuredOutputUtility ? "structured-output utility call (#5073)" : audioOutputUtility ? "audio-output utility call (#6636)" : dshTitleSidecar ? "dsh title-gen sidecar (#2503)" : dshReviewSidecar ? "dsh auto-review sidecar (#4438)" : dshSingleShotSidecar ? "dsh single-shot sidecar (#5656)" : ccTitleSidecar ? "claude-code title-gen sidecar (#5135)" : undefined }); if (sideLane.lane === "side") { // #554: the passthrough below skips EVERY input-side guard by design // (#388) — a full-history side request over the window is a diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index 51a28bd890..cc61f31ae4 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -16,7 +16,7 @@ import { maybeSnapshotView } from "./snapshot-render.js"; import { storedMessagesFor, storedRowLookupFor } from "../fold-decomposition-lookup.js"; import { nudgeSuppressed } from "../session-self-heal.js"; import { compressBreakerArmed } from "../stream.js"; -import { absorbAbortedTurnGrowth, applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type PendingRetrieval, type Session } from "../session.js"; +import { absorbAbortedTurnGrowth, nudgeStampsOf, restoreNudgeStamps, applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type PendingRetrieval, type Session } from "../session.js"; import { getStore } from "../persist.js"; import { carriesDshLocalCompactionSummary, DSH_LOCAL_COMPACTION_MIN_MISSING } from "./dsh-compaction-guard.js"; import { carriesBiliSnapshotCheckpoint } from "./dsh-authored-compaction.js"; @@ -171,6 +171,7 @@ export async function prepareAnthropic( let judgeBaseline: readonly CoreMessage[] | undefined; let nudge: NudgeDecision | undefined; let nudgeOutcome: Prepared["nudgeOutcome"]; + let nudgeStampsBefore: Prepared["nudgeStampsBefore"]; let rebuiltMessages = parsed.messages; let anthropicCacheMarks: Map | undefined; let clientCacheControls: Map | undefined; @@ -375,8 +376,11 @@ export async function prepareAnthropic( await yieldToEventLoop(); // #5293: re-anchor the nudge growth reference after a user-aborted turn (no-op otherwise). absorbAbortedTurnGrowth(session, tokenCount); + nudgeStampsBefore = nudgeStampsOf(session); const turn = core.processTurn({ messages: renderMsgs, state: session.state, config: loopConfig, tokenCount, tokenCountSource, renderTags: renderStrategy, contentStore: contentStoreOf(session) }); session.state = turn.state; + const nudgeHeldAfterReject = session.transient?.nudgeHeldAfterReject === true; + if (nudgeHeldAfterReject) restoreNudgeStamps(session, nudgeStampsBefore); adoptContentStore(session, turn.contentStore); // #4528: split the kernel pass from its post-processing scan passes so // each is a bounded synchronous stretch rather than one long block. @@ -431,7 +435,7 @@ export async function prepareAnthropic( // #3482 Step 2.5: the ONE nudge gate. This lane has no compaction // trigger and no intercept health window — those differences are // explicit inputs on the responses lane. - const willInjectNudge = nudgeOn({ + const willInjectNudge = !nudgeHeldAfterReject && nudgeOn({ injectNudge: opts.compress.injectNudge, hasNudgePlan: !!turn.nudge, compressSurfaceOn: laneDecisions.compressSurfaceOn, @@ -687,7 +691,7 @@ export async function prepareAnthropic( // main-scale tiebreak sees this request's own upper bound. probeRequestMembership(session, collectRequestIds(incomingMsgIds, originalMessages), session.stats.localInputEstimate ?? 0); if (upstreamOrigin) session.stats.lastLocalTextEstimateOrigin = upstreamOrigin; - return { body: JSON.stringify(rebuilt), parsedBody: rebuilt, session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, judgeBaseline, anthropicSystem: parsed.system, anthropicBillingBlock, anthropicCacheMarks, anthropicClientCacheControls: clientCacheControls, systemNotes: sysNotes, protocol: "anthropic", stream, compressInjected: injectTools, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, prompts, surface, renderTags: renderStrategy, dropReasoning: (msgs: BiliMessage[]) => applyReasoningDrop(msgs, reasoningPlan, log, sessionId) } as Prepared; + return { body: JSON.stringify(rebuilt), parsedBody: rebuilt, session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, judgeBaseline, anthropicSystem: parsed.system, anthropicBillingBlock, anthropicCacheMarks, anthropicClientCacheControls: clientCacheControls, systemNotes: sysNotes, protocol: "anthropic", stream, compressInjected: injectTools, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, nudgeStampsBefore: nudgeOutcome === "injected" ? nudgeStampsBefore : undefined, prompts, surface, renderTags: renderStrategy, dropReasoning: (msgs: BiliMessage[]) => applyReasoningDrop(msgs, reasoningPlan, log, sessionId) } as Prepared; } export const TRAILING_ASSISTANT_NUDGE = "[bili] Earlier context was compressed above. Continue the task."; diff --git a/src/server/prepare-google.ts b/src/server/prepare-google.ts index f85b4dc1c1..98486be07d 100644 --- a/src/server/prepare-google.ts +++ b/src/server/prepare-google.ts @@ -15,7 +15,7 @@ import { maybeSnapshotView } from "./snapshot-render.js"; import { storedMessagesFor, storedRowLookupFor } from "../fold-decomposition-lookup.js"; import { nudgeSuppressed } from "../session-self-heal.js"; import { compressBreakerArmed } from "../stream.js"; -import { absorbAbortedTurnGrowth, applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type Session } from "../session.js"; +import { absorbAbortedTurnGrowth, nudgeStampsOf, restoreNudgeStamps, applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type Session } from "../session.js"; import { getStore } from "../persist.js"; import { recordConflict } from "../conflict-watch.js"; import { carriesDshLocalCompactionSummary, DSH_LOCAL_COMPACTION_MIN_MISSING } from "./dsh-compaction-guard.js"; @@ -98,6 +98,7 @@ export async function prepareGoogle( let judgeBaseline: readonly CoreMessage[] | undefined; let nudge: NudgeDecision | undefined; let nudgeOutcome: Prepared["nudgeOutcome"]; + let nudgeStampsBefore: Prepared["nudgeStampsBefore"]; let rebuiltContents: GoogleContent[] = Array.isArray(parsed.contents) ? parsed.contents : []; let toolsOut: GoogleTool[] | undefined = parsed.tools; @@ -205,8 +206,14 @@ export async function prepareGoogle( await yieldToEventLoop(); // #5293: re-anchor the nudge growth reference after a user-aborted turn (no-op otherwise). absorbAbortedTurnGrowth(session, tokenCount); + // #6531: snapshot before the kernel pass so a 4xx reject of the + // nudge-carrying request rolls the cadence back to pre-turn (lane + // mirror of the anthropic-lane fix). + nudgeStampsBefore = nudgeStampsOf(session); const turn = core.processTurn({ messages: renderMsgs, state: session.state, config: loopConfig, tokenCount, tokenCountSource, renderTags: renderStrategy, contentStore: contentStoreOf(session) }); session.state = turn.state; + const nudgeHeldAfterReject = session.transient?.nudgeHeldAfterReject === true; + if (nudgeHeldAfterReject) restoreNudgeStamps(session, nudgeStampsBefore); adoptContentStore(session, turn.contentStore); // #4528: split the kernel pass from its post-processing scan passes so // each is a bounded synchronous stretch rather than one long block. @@ -258,7 +265,7 @@ export async function prepareGoogle( // explicit inputs on the responses lane. The standalone `shouldInject` // conjunct is subsumed by compressSurfaceOn (=== shouldInject && // compressReachable). - const willInjectNudge = nudgeOn({ + const willInjectNudge = !nudgeHeldAfterReject && nudgeOn({ injectNudge: opts.compress.injectNudge, hasNudgePlan: !!turn.nudge, compressSurfaceOn: laneDecisions.compressSurfaceOn, @@ -419,7 +426,7 @@ export async function prepareGoogle( // #3397 durability-before-render (see prepare-anthropic.ts): rows durable NOW, before the model call. getStore().ingestRowsNow(session); markDirty(session); - return { body: JSON.stringify(rebuilt), parsedBody: rebuilt, session, processedMessages, originalMessages, judgeBaseline, protocol: "google", stream, compressInjected: injectTools, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, prompts, surface, google: { system: googleClientSystem, model }, systemNotes: sysNotes, renderTags: renderStrategy } as Prepared; + return { body: JSON.stringify(rebuilt), parsedBody: rebuilt, session, processedMessages, originalMessages, judgeBaseline, protocol: "google", stream, compressInjected: injectTools, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, nudgeStampsBefore: nudgeOutcome === "injected" ? nudgeStampsBefore : undefined, prompts, surface, google: { system: googleClientSystem, model }, systemNotes: sysNotes, renderTags: renderStrategy } as Prepared; } /** `POST /v1beta/models/:countTokens` — the fold-prune twin of diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index 84a453fcee..59d9ac4fae 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -19,7 +19,7 @@ import { maybeSnapshotView } from "./snapshot-render.js"; import { storedMessagesFor, storedRowLookupFor } from "../fold-decomposition-lookup.js"; import { nudgeSuppressed } from "../session-self-heal.js"; import { compressBreakerArmed } from "../stream.js"; -import { absorbAbortedTurnGrowth, applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type PendingRetrieval, type Session } from "../session.js"; +import { absorbAbortedTurnGrowth, nudgeStampsOf, restoreNudgeStamps, applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type PendingRetrieval, type Session } from "../session.js"; import { getStore } from "../persist.js"; import { carriesDshLocalCompactionSummary, DSH_LOCAL_COMPACTION_MIN_MISSING } from "./dsh-compaction-guard.js"; import { carriesBiliSnapshotCheckpoint } from "./dsh-authored-compaction.js"; @@ -95,6 +95,7 @@ export async function prepareOpenai( let judgeBaseline: readonly CoreMessage[] | undefined; let nudge: NudgeDecision | undefined; let nudgeOutcome: Prepared["nudgeOutcome"]; + let nudgeStampsBefore: Prepared["nudgeStampsBefore"]; let rebuiltMessages = parsed.messages; let toolsOut = parsed.tools; // #2483: single source for the effective render strategy — processTurn, the @@ -324,8 +325,14 @@ export async function prepareOpenai( await yieldToEventLoop(); // #5293: re-anchor the nudge growth reference after a user-aborted turn (no-op otherwise). absorbAbortedTurnGrowth(session, tokenCount); + // #6531: snapshot before the kernel pass so a 4xx reject of the + // nudge-carrying request rolls the cadence back to pre-turn (lane + // mirror of the anthropic-lane fix). + nudgeStampsBefore = nudgeStampsOf(session); const turn = core.processTurn({ messages: renderMsgs, state: session.state, config: loopConfig, tokenCount, tokenCountSource, renderTags: renderStrategy, contentStore: contentStoreOf(session) }); session.state = turn.state; + const nudgeHeldAfterReject = session.transient?.nudgeHeldAfterReject === true; + if (nudgeHeldAfterReject) restoreNudgeStamps(session, nudgeStampsBefore); adoptContentStore(session, turn.contentStore); // #4528: split the kernel pass from its post-processing scan passes so // each is a bounded synchronous stretch rather than one long block. @@ -384,7 +391,7 @@ export async function prepareOpenai( // explicit inputs on the responses lane. The standalone `shouldInject` // conjunct is subsumed by compressSurfaceOn (=== shouldInject && // compressReachable). - const willInjectNudge = nudgeOn({ + const willInjectNudge = !nudgeHeldAfterReject && nudgeOn({ injectNudge: opts.compress.injectNudge, hasNudgePlan: !!turn.nudge, compressSurfaceOn: laneDecisions.compressSurfaceOn, @@ -652,7 +659,7 @@ export async function prepareOpenai( }); if (Object.keys(wpResponseHeaders).length === 0) wpResponseHeaders = undefined; } - return { body: JSON.stringify(rebuilt), parsedBody: rebuilt, session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, judgeBaseline, protocol: "openai", stream, compressInjected: injectTools, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, prompts, surface, openaiSystemText, systemNotes: sysNotes, renderTags: renderStrategy, responseExtraHeaders: wpResponseHeaders, dropReasoning: (msgs: BiliMessage[]) => applyReasoningDrop(msgs, reasoningPlan, log, sessionId) } as Prepared; + return { body: JSON.stringify(rebuilt), parsedBody: rebuilt, session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, judgeBaseline, protocol: "openai", stream, compressInjected: injectTools, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, nudgeStampsBefore: nudgeOutcome === "injected" ? nudgeStampsBefore : undefined, prompts, surface, openaiSystemText, systemNotes: sysNotes, renderTags: renderStrategy, responseExtraHeaders: wpResponseHeaders, dropReasoning: (msgs: BiliMessage[]) => applyReasoningDrop(msgs, reasoningPlan, log, sessionId) } as Prepared; } /** Append the ephemeral nudge to a Gemini `contents` array. Gemini is diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts index bd9b491f2a..9040148149 100644 --- a/src/server/prepare-responses.ts +++ b/src/server/prepare-responses.ts @@ -19,7 +19,7 @@ import { buildAnchoredView, maybeSnapshotView, noteAnchoredRender, SnapshotAncho import { storedMessagesFor, storedRowLookupFor } from "../fold-decomposition-lookup.js"; import { nudgeSuppressed, pluginLaneDegraded } from "../session-self-heal.js"; import { compressBreakerArmed } from "../stream.js"; -import { absorbAbortedTurnGrowth, applyCompactionArchive, coveredRealHistoryIds, foldCoverage, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type Session } from "../session.js"; +import { absorbAbortedTurnGrowth, nudgeStampsOf, restoreNudgeStamps, applyCompactionArchive, coveredRealHistoryIds, foldCoverage, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type Session } from "../session.js"; import { getStore } from "../persist.js"; import { carriesDshLocalCompactionSummary, DSH_LOCAL_COMPACTION_MIN_MISSING } from "./dsh-compaction-guard.js"; import { carriesBiliSnapshotCheckpoint } from "./dsh-authored-compaction.js"; @@ -112,6 +112,7 @@ export async function prepareResponses( let judgeBaseline: readonly CoreMessage[] | undefined; let nudge: NudgeDecision | undefined; let nudgeOutcome: Prepared["nudgeOutcome"]; + let nudgeStampsBefore: Prepared["nudgeStampsBefore"]; let responsesProjection: ResponsesProjection | undefined; let rebuiltInput: ResponseInputItem[] | string = parsed.input; let toolsOut = parsed.tools; @@ -371,8 +372,14 @@ reconcileFoldCoverage(session, msgs, { mode: foldMode, sessionId, log, markerStr await yieldToEventLoop(); // #5293: re-anchor the nudge growth reference after a user-aborted turn (no-op otherwise). absorbAbortedTurnGrowth(session, tokenCount); + // #6531: snapshot before the kernel pass so a 4xx reject of the + // nudge-carrying request rolls the cadence back to pre-turn (lane + // mirror of the anthropic-lane fix). + nudgeStampsBefore = nudgeStampsOf(session); const turn = core.processTurn({ messages: renderMsgs, state: session.state, config: loopConfig, tokenCount, tokenCountSource, renderTags, contentStore: contentStoreOf(session) }); session.state = turn.state; + const nudgeHeldAfterReject = session.transient?.nudgeHeldAfterReject === true; + if (nudgeHeldAfterReject) restoreNudgeStamps(session, nudgeStampsBefore); adoptContentStore(session, turn.contentStore); // #4528: split the kernel pass from its post-processing scan passes so // each is a bounded synchronous stretch rather than one long block. @@ -426,7 +433,7 @@ reconcileFoldCoverage(session, msgs, { mode: foldMode, sessionId, log, markerStr // two DELIBERATE differences are explicit inputs: the compaction // trigger (#2155/#2432 — a trigger request never carries the nudge) // and the codex-intercept health window (#2774). - const willInjectNudge = nudgeOn({ + const willInjectNudge = !nudgeHeldAfterReject && nudgeOn({ injectNudge: opts.compress.injectNudge, hasNudgePlan: !!turn.nudge, compressSurfaceOn: laneDecisions.compressSurfaceOn, @@ -766,6 +773,7 @@ reconcileFoldCoverage(session, msgs, { mode: foldMode, sessionId, log, markerStr responsesTextProtocol, nudge, nudgeOutcome, + nudgeStampsBefore: nudgeOutcome === "injected" ? nudgeStampsBefore : undefined, prompts, surface, renderTags, diff --git a/src/server/relay.ts b/src/server/relay.ts index a9123e7253..a41da78297 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -50,7 +50,7 @@ import { pipePluginChatWithStrip, pipePluginJson, pipePluginJsonlWithStrip, pipe import { describeCoreTextDuplication, estimateCoreMessages, estimateCoreMessagesUpperBytes } from "../preflight.js"; import { reasoningGuardEngages, runReasoningGuard } from "../reasoning-guard.js"; import { wrapStreamWithRunawayGuard } from "../runaway-guard.js"; -import { diagnoseSuccessWithoutUsage, markDirty, markNativeCompactionBoundary, withSessionLock, type Session } from "../session.js"; +import { diagnoseSuccessWithoutUsage, markDirty, releaseNudgeHold, rejectNudge, markNativeCompactionBoundary, withSessionLock, type Session } from "../session.js"; import { adoptContentStore, ccrLoopConfig, commitRetrievalNotes, commitRetrievals, contentStoreOf, dropRetrievals } from "../store.js"; import { emitStreamError } from "../stream-error.js"; import { rewriteGoogleJsonResponseAsync } from "../stream-google.js"; @@ -70,7 +70,7 @@ import { DUMP_PLAINTEXT_BANNER } from "../dump-banner.js"; import { recordPckAblation } from "../pck-probe.js"; import { detectThinkingSignatureRejection, rememberStrictThinkingPosture, stripUnsignedThinkingFromWire, thinkingPostureIsStrict } from "./thinking-posture.js"; import { applyOutputSteering, applyOutputSteeringJson } from "../output-steering.js"; -import { estimateWireOverhead } from "./budget.js"; +import { estimateWireOverhead, outputBudgetField, readOutputBudget, writeOutputBudget } from "./budget.js"; import { buildForwardHeaders, connectionNamedHeaders, RESPONSE_ONLY_STRIP_HEADERS, safeSessionId, UPSTREAM_HOP_HEADERS } from "./headers.js"; import { bodyDumpEnabled, logDumpFailure } from "./observability.js"; import { rejectionDiagnosticSuffix } from "./rejection-diagnostics.js"; @@ -78,6 +78,28 @@ import { effectiveAbsorbBlock, effectiveBlockOpsEnabled } from "./prepare-respon import { awaitDrain, bufferToStream, dumpStreamToFile, pipeThrough, readStreamToBuffer } from "./stream-io.js"; import { buildForwardTarget, measuredBreakdownClause, measuredOutbound, outboundPayloadBreakdown, registerRequestAbort, beginStreamKeepalive, effectiveTokenCount, googlePathKind, imageBillingFor, imageTokenCapFor, inboundResignCredential, outboundContextEstimates, resolveUpstream, stripKernelSummaries, usageGradeInputBaseline, DOOMED_PROBE_ABSOLUTE_BYTES, DOOMED_PROBE_BYTES_PER_TOKEN, type Prepared } from "../server.js"; +/** Retry the same input when the provider proved only its output reservation overflows. */ +function capOverflowOutput(body: string | Buffer, info: ContextOverflowInfo): { body: string; cap: number } | null { + const { window, requestedTotal: total, requestedMessages: input, requestedCompletion: output } = info; + if (!info.isOverflow || typeof body !== "string" || window === undefined || input === undefined || output === undefined) return null; + const cap = window - input; + if (!Number.isSafeInteger(window) || !Number.isSafeInteger(input) || input <= 0 || cap <= 0 || cap >= output) return null; + if (total !== input + output) return null; + + let parsed: Record; + try { + parsed = JSON.parse(body) as Record; + } catch { + return null; + } + const field = outputBudgetField(parsed); + if (field === null || readOutputBudget(parsed, field) !== output) return null; + const thinking = parsed.thinking as { budget_tokens?: unknown } | undefined; + if (typeof thinking?.budget_tokens === "number" && cap <= thinking.budget_tokens) return null; + writeOutputBudget(parsed, field, cap); + return { body: JSON.stringify(parsed), cap }; +} + /** Infer the wire protocol from the request path for compat-role rewrites on * requests the pipeline did not prepare (passthrough). Mirrors the path * checks in handleRequest; returns null when unknown (no rewrite). */ @@ -1820,12 +1842,9 @@ const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepare }; // #1195: a context overflow used to arm the shrink and pass the 400 through // verbatim, expecting the NEXT turn to fold — but wire clients treat the - // error as fatal and end the session, so the armed rescue never runs and the - // session locks. With a refold hook: arm, let the caller re-run prepare+ - // preflight against the window the upstream STATED (per-call override, no - // learning), and re-send the folded body ONCE within this same request. An - // unchanged body (nothing foldable), a null refold, or a transport failure - // falls back to today's verbatim passthrough below. + // error as fatal and end the session. Try a budget-only retry when the + // provider proves the current input fits. Otherwise refold within this + // request, with the same budget-only option on the newly measured body. if ( overflowRefold && prepared?.session && @@ -1928,46 +1947,64 @@ const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepare // margin under it, the same headroom discipline as the arm's // own min(declared, est) clamp — the rejection proved the // ceiling is below the payload, give the fold real room. - const armedWindow = overflowInfo.window ?? (prepared.session.stats.lastInputTokensSource === "overflow-arm" ? Math.floor(prepared.session.stats.lastInputTokens * 0.9) : undefined); - const refolded = await overflowRefold(armedWindow).catch(() => null); - if (refolded) { - try { - // #816 family: the refold rebuilds the body from state - // summaries — scrub string bodies again so a lone - // surrogate can't poison the retry (see the #1884 seam). - const wireRefolded = typeof refolded.body === "string" ? scrubLoneSurrogatesOnWire(refolded.body) : refolded.body; - const retried = await fetchWithTimeout(upstreamUrl, { ...init, body: await finalizeEgress(wireRefolded) }, undefined, clientAbort.signal); - if (retried.response.ok) { - upstreamResult.clearTimer(); - wireBody = wireRefolded; // #1900: track the accepted re-send as the wire base - upstreamResult = retried; - log("info", `[${prepared.session.id}] context overflow — refolded and re-sent within the same request, upstream accepted (#1195)`); - prepared = refolded.prepared; - } else { - let retryErrText: string | null = null; - if (retried.response.body) { - try { - retryErrText = (await readStreamToBuffer(retried.response.body)).toString("utf8"); - } catch { - retryErrText = null; - } + const s = prepared.session; + const armedWindow = overflowInfo.window ?? (s.stats.lastInputTokensSource === "overflow-arm" ? Math.floor(s.stats.lastInputTokens * 0.9) : undefined); + // Keep the latest complete upstream verdict if recovery cannot continue. + const resendOverflow = async (retryBody: string | Buffer): Promise => { + const retried = await fetchWithTimeout(upstreamUrl, { ...init, body: await finalizeEgress(retryBody) }, undefined, clientAbort.signal); + upstreamResult.clearTimer(); + if (retried.response.ok) { + wireBody = retryBody; // #1900: the accepted body is the next wire base + upstreamResult = retried; + return null; + } + const retryText = retried.response.body + ? await readStreamToBuffer(retried.response.body).then(body => body.toString("utf8"), () => "") + : ""; + upstreamResult = { + ...retried, + response: new Response(retryText, { + status: retried.response.status, + statusText: retried.response.statusText, + headers: new Headers(retried.response.headers), + }), + }; + if (retried.response.status !== 400 && retried.response.status !== 413) { + return { isOverflow: false, message: "" }; + } + return inspectContextOverflow(retried.response.status, retryText); + }; + // A user transform can change the input again on the retry stage, + // so the rejected token count cannot bound that send. + const mayRetryWithCap = !egressBaseFields; + const resendWithCap = async (recovery: { body: string; cap: number }): Promise => { + log("info", `[${s.id}] context overflow — retrying with output capped at ${recovery.cap} using upstream's measured input (#1195)`); + return resendOverflow(recovery.body); + }; + try { + let rejection: ContextOverflowInfo | null = overflowInfo; + const cap = mayRetryWithCap ? capOverflowOutput(wireBody, overflowInfo) : null; + if (cap !== null && !clientAbort.signal.aborted) { + rejection = await resendWithCap(cap); + if (rejection === null) log("info", `[${s.id}] context overflow — budget-only retry accepted (#1195)`); + } + if (rejection?.isOverflow && !clientAbort.signal.aborted) { + const refolded = await overflowRefold(rejection.window ?? armedWindow).catch(() => null); + if (refolded) { + const retryBody = typeof refolded.body === "string" ? scrubLoneSurrogatesOnWire(refolded.body) : refolded.body; + rejection = await resendOverflow(retryBody); + if (mayRetryWithCap && rejection?.isOverflow && !clientAbort.signal.aborted) { + const foldedCap = capOverflowOutput(retryBody, rejection); + if (foldedCap !== null) rejection = await resendWithCap(foldedCap); + } + if (rejection === null) { + prepared = refolded.prepared; + log("info", `[${s.id}] context overflow — refolded and re-sent within the same request, upstream accepted (#1195)`); } - // Answer with the retry's own verdict, not the stale first 400. - upstreamResult.clearTimer(); - upstreamResult = { - response: new Response(retryErrText ?? "", { - status: retried.response.status, - statusText: retried.response.statusText, - headers: new Headers(retried.response.headers), - }), - clearTimer: retried.clearTimer, - stopIdleTimer: retried.stopIdleTimer, - }; - log("warn", `[${prepared.session.id}] context overflow — refold retry still rejected (HTTP ${retried.response.status}); passing the retry response through (#1195)`); } - } catch { - // transport failure — the buffered original 400 answers below } + } catch { + // The most recent buffered rejection answers below. } } } @@ -2121,6 +2158,7 @@ const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepare // here depends on it. const terminalCause = rejectionDiagnosticSuffix(bodyText); loggerLog("warn", `[${errSid}] ← upstream ${upstream.status}${reqIdText}: ${snippet}${terminalCause}`); + if (prepared?.session && prepared.nudgeStampsBefore && upstream.status >= 400 && upstream.status < 500) rejectNudge(prepared.session, prepared.nudgeStampsBefore, upstream.status); // #4204: feed this send's FINAL verdict. A model-scoped KeyRing 429 is // selected only by its structured code plus exact request-model match; // keyring_all_limited and every ambiguous response remain account-wide. @@ -2890,6 +2928,7 @@ const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepare loggerLog("info", `[${prepared.session.id}] [acp-usage] input=${billed} ${hitPct === undefined ? "(no cache report)" : `cached=${reportedCached} (cache hit ${hitPct}%)`}${billed <= 0 ? " (zero-total: lastInputTokens kept)" : ""}`); } else { diagnoseSuccessWithoutUsage(prepared.session, "proxy-json"); + releaseNudgeHold(prepared.session); } if (typeof out === "number") { prepared.session.stats.outputTokens += out; diff --git a/src/server/side-request.ts b/src/server/side-request.ts index d991c40feb..b6c1cb04b0 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -73,6 +73,60 @@ export function isServerToolUtilityCall(parsed: unknown): boolean { return false; } +// #6548: the Responses-wire analogue of isServerToolUtilityCall. pi-web-access +// sends its xAI web-search backend as a deliberately minimal single-shot +// Responses body — `{ model, input, tools: [{type:"web_search"}, +// {type:"x_search"}] }` — string input, no message history, no +// prompt_cache_key, and it exposes no config knob for extra headers or a +// session id, so the client cannot carry an identity here. That shape is NOT +// a conversation turn: it has no history to fingerprint and creates no +// session state, so the #286 refusal's rationale (content-fingerprint +// sessions would silently orphan state) does not apply to it — the refusal +// branch forwards it verbatim instead of hard-400ing. +// Decisive conditions (all must hold): +// - `input` is a plain string (the single-shot prompt); an array input is +// message history, and a missing input is degenerate; +// - non-empty tools where EVERY entry's type is a Responses server-executed +// search type (`web_search`, `web_search_preview`, `x_search`) — mixing +// ANY client-side tool (`function`, `mcp`, ...) vetoes; +// - no identity/history signal in the body: `previous_response_id`, +// `session_id` or `prompt_cache_key` present means the call chains onto +// a conversation (or carries an identity the #286 path already honors); +// - tool_choice absent, "auto" or "required", or an object forcing a +// DECLARED server tool; "none" vetoes (a request that forbids tool use +// is not a tool call), as does forcing a client tool or an unrecognized +// shape. +// Deliberately proto-safe by shape alone: the Anthropic detector above +// requires `messages`, so a Responses body can never match it — and this one +// requires string `input`, so an Anthropic body can never match it either. +const RESPONSES_SERVER_SEARCH_TOOL_TYPES: ReadonlySet = new Set(["web_search", "web_search_preview", "x_search"]); + +export function isResponsesServerToolSingleShot(parsed: unknown): boolean { + if (!parsed || typeof parsed !== "object") return false; + const p = parsed as Record; + if (typeof p.input !== "string") return false; + if (typeof p.previous_response_id === "string" && p.previous_response_id !== "") return false; + if (typeof p.session_id === "string" && p.session_id !== "") return false; + if (typeof p.prompt_cache_key === "string" && p.prompt_cache_key !== "") return false; + const tools = p.tools; + if (!Array.isArray(tools) || tools.length === 0) return false; + const declared = new Set(); + for (const t of tools) { + if (t === null || typeof t !== "object") return false; + const type = (t as { type?: unknown }).type; + if (typeof type !== "string" || !RESPONSES_SERVER_SEARCH_TOOL_TYPES.has(type)) return false; + declared.add(type); + } + const choice = p.tool_choice; + if (choice === undefined) return true; + if (choice === "auto" || choice === "required") return true; + if (choice !== null && typeof choice === "object") { + const c = choice as { type?: unknown }; + if (typeof c.type === "string" && declared.has(c.type)) return true; + } + return false; +} + const STRUCTURED_FORMAT_TYPES: ReadonlySet = new Set(["json_schema", "json_object"]); /** #5073: structural identification of a tool-less structured-output utility @@ -101,6 +155,35 @@ export function isStructuredOutputCall(parsed: unknown): boolean { return typeof type === "string" && STRUCTURED_FORMAT_TYPES.has(type); } +/** #6636: structural identification of a tool-less audio-output utility + * call — a TTS request on the chat wire carries a top-level `audio` object + * (`{voice, format}`, OpenAI chat-completions audio output) and no tools. + * It is a one-shot media side call, not an agent turn: agent turns always + * carry an action surface (#546), and a TTS model behind an + * OpenAI-compatible endpoint rejects the ACP decoration outright — the + * injected leading system prompt 400s ("messages[0] system role is not + * allowed for TTS model", Xiaomi MiMo mimo-v2.5-tts) and the per-message + * prefixes would be read aloud as text. Routed through the #388 side + * passthrough like the #2500/#5073 utility shapes above, so the request + * leaves the machine exactly as the client wrote it. + * The no-tools veto is part of the predicate (same discipline as + * isStructuredOutputCall): a tool-carrying request is a main turn even + * when it asks for audio output. An empty `audio` object does not count — + * it carries no media request. Residual, accepted: a tool-less plain chat + * turn that asks for audio output (voice-mode chat with no tools) also + * rides the side lane for that one turn — it forwards verbatim and its + * fold state simply does not advance; the ACP decoration is never needed + * for the reply to work, and the next tool-carrying turn rejoins the full + * pipeline. */ +export function isAudioOutputCall(parsed: unknown): boolean { + if (!parsed || typeof parsed !== "object") return false; + const p = parsed as Record; + if (Array.isArray(p.tools) && p.tools.length > 0) return false; + const audio = p.audio; + if (audio === null || typeof audio !== "object" || Array.isArray(audio)) return false; + return Object.keys(audio).length > 0; +} + export function isSideRequest(parsed: unknown, requestAgent?: string): boolean { if (!parsed || typeof parsed !== "object") return false; if (requestAgent !== undefined && SIDE_REQUEST_AGENTS.has(requestAgent)) return true; @@ -120,6 +203,12 @@ export function isSideRequest(parsed: unknown, requestAgent?: string): boolean { // pair with a 400. Routed through the #388 side passthrough like the #2500 // server-tool shape above. if (isStructuredOutputCall(parsed)) return true; + // #6636: a tool-less request asking for audio output (top-level `audio` + // object — TTS on the chat wire) is a one-shot media side call, not an + // agent turn. The ACP decoration 400s it (leading system prompt) and + // would corrupt the spoken text (per-message tags), so it rides the + // #388 side passthrough verbatim like the utility shapes above. + if (isAudioOutputCall(parsed)) return true; const field = outputBudgetField(parsed); if (!field) return false; const raw = readOutputBudget(p, field); diff --git a/src/session.ts b/src/session.ts index 33c9833c52..f518aa9ca0 100644 --- a/src/session.ts +++ b/src/session.ts @@ -338,6 +338,10 @@ export type Session = { * In-memory only: the abort→resume causality spans one process * lifetime, and a restart replants nothing. */ abortReanchor?: { anchor: number; at: number }; + /** The upstream rejected a request that carried a nudge: its stamps were + * rolled back, and no nudge rides until a request succeeds (the nudge may + * be what drew the rejection). Cleared by settleUsageReport. */ + nudgeHeldAfterReject?: boolean; /** #5683/#5943: the conversation's live nudge CREDIT plus the policy * flags, for the unprompted/out-of-bounds gates (src/unprompted-gate.ts). * Re-stamped on every prepare (injected rounds re-mint the window, @@ -1136,6 +1140,39 @@ export function absorbAbortedTurnGrowth(session: Session, tokenCount: number): v loggerLog("info", `[${session.id}] aborted-turn growth absorbed: nudge reference ${oldRef}→${nudge.lastPerMessageNudgeTokens} (meter ${tokenCount}, pre-abort anchor ${flag.anchor}, growth debt +${recovered} → ${nudge.absorbedDebt ?? 0} over ${nudge.absorbCount} absorb(s)) (#5293/#5415)`); } +export type NudgeStamps = { shown: number; byTier: Session["state"]["nudge"]["lastShownByTier"] }; + +export function nudgeStampsOf(session: Session): NudgeStamps { + return { shown: session.state.nudge.lastNudgeShownTokens, byTier: session.state.nudge.lastShownByTier }; +} + +export function restoreNudgeStamps(session: Session, stamps: NudgeStamps): void { + session.state.nudge.lastNudgeShownTokens = stamps.shown; + session.state.nudge.lastShownByTier = stamps.byTier; +} + +/** The kernel stamps a nudge as shown when it decides to inject it. When the + * upstream rejects that request the model never saw the nudge, so the stamp + * would hold the cadence against a nudge nobody received (a Claude Code session, + * 2026-11-11: the session's only nudge drew a 400 and it never nudged again). + * Roll the stamps back and hold nudges until the next success. */ +export function rejectNudge(session: Session, before: NudgeStamps, status: number): void { + restoreNudgeStamps(session, before); + (session.transient ??= {}).nudgeHeldAfterReject = true; + markDirty(session); + loggerLog("info", `[${session.id}] nudge rejected with its request (upstream ${status}): stamps rolled back to shown=${before.shown}; nudges held until a request succeeds`); +} + +/** The hold set by rejectNudge must end on ANY success — including ones that + * never report usage. settleUsageReport only clears it for s.total > 0 + * samples, so on routes where the upstream never reports usage the flag + * would latch forever (a single 4xx would kill nudging for the rest of the + * process lifetime — the exact failure #6531 fixes, relocated to the #1595 + * route class). Called at every no-usage success site. */ +export function releaseNudgeHold(session: Session): void { + if (session.transient?.nudgeHeldAfterReject) delete session.transient.nudgeHeldAfterReject; +} + // #1820: post-rebuild meter anchor. Right after a big preflight rebuild the // usage-grade baseline is momentarily absent (the rebuild request's own report // hasn't landed yet, or the upstream never reports one), so effectiveTokenCount diff --git a/src/stream.ts b/src/stream.ts index ddf8d60bfb..289ad37ce8 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -415,6 +415,17 @@ function highestMappedRef(state: Pick): number return highest; } +/** #6712: number-preserving scrub marker for a stale requested ref. `m00010` + * becomes `[stale-ref:00010]` — the digits stay in the receipt so the caller + * can tell which ref it used (and the `range …:` diagnostic line stays + * readable), while the bare `mNNNNN` token never reaches the client-persisted + * receipt, so the #2146 echo channel stays dead. Non-m ids (unreachable when + * `beyond` fires, which requires m-refs) fall back to the bare marker. */ +function staleRefMarker(id: string): string { + const m = /^m(\d+)$/i.exec(id.trim()); + return m ? `[stale-ref:${m[1]}]` : "[stale-ref]"; +} + /** #2146: every requested endpoint sits strictly ABOVE the session's highest * mapped m-ref. Such refs cannot exist in this session (refs are never * pre-allocated); the kernel now classifies that case as above-frontier @@ -1068,7 +1079,34 @@ export function applyRanges(parsed: ReturnType, ctx: const gate = trackLoop ? unpromptedGateOf(ctx.session) : undefined; if (gate?.requireNudge && !gate.armed) { ctx.log("[acp-proxy: refused unprompted compress — no live nudge credit (window expired or never minted) (compress.requireNudge)]"); - return compressResult(`[Compression REFUSED: unprompted compression is disabled on this route (compress.requireNudge) — you may only call compress while a nudge credit is live, i.e. within ${NUDGE_CREDIT_ROUNDS} model rounds of a compression nudge that was actually injected. No live credit right now: the last injected nudge's window has expired, or no nudge was ever injected. Continue the task without folding; when a nudge appears, follow its ranges.]`, "refused", 0, "unprompted"); + // #6728: gate refusals charge the failure ledger like every other + // reject path — otherwise a model re-submitting the same range gets + // byte-identical receipts forever and the breaker never arms. + const specKey = + parsed.ranges.length > 0 + ? normalizedSpecKey(parsed.ranges) + : "unprompted"; + // The spec-repeat guard text recordCompressFailure returns is dropped + // by the collapse below (one counter, stated once — same as the scope + // gate under it); only the ledger charge matters here. + recordCompressFailure(ctx.session, specKey); + const loopGuard = trackLoop ? noteCompressLoopFailure(ctx, specKey) : ""; + const occurrences = compressFailOccurrencesOf(ctx.session, specKey); + // #6728: repeat-collapse, same shape as the parse-failure one-liner + // below — the first refusal keeps the full teaching receipt; every + // repeat gets one line pointing back at it. + if (trackLoop && occurrences >= 2) { + ctx.log( + `[acp-proxy: unprompted-gate receipt collapsed to one-liner (attempt ${occurrences})]`, + ); + return compressResult( + `[Compression REFUSED again — this exact call has now been refused ${occurrences} time(s) the same way (compress.requireNudge); nothing was compressed. The full refusal and its conditions are in the FIRST refused receipt above — do not re-send this call verbatim.${loopGuard}]`, + "refused", + 0, + "unprompted", + ); + } + return compressResult(`[Compression REFUSED: unprompted compression is disabled on this route (compress.requireNudge) — you may only call compress while a nudge credit is live, i.e. within ${NUDGE_CREDIT_ROUNDS} model rounds of a compression nudge that was actually injected. No live credit right now: the last injected nudge's window has expired, or no nudge was ever injected. Continue the task without folding; when a nudge appears, follow its ranges.${loopGuard}]`, "refused", 0, "unprompted"); } let { ranges, diagnostics } = parsed; if (ranges.length === 0) { @@ -1220,7 +1258,36 @@ export function applyRanges(parsed: ReturnType, ctx: if (offending.length > 0) { const listed = offending.slice(0, 3).join(", ") + (offending.length > 3 ? ` (+${offending.length - 3} more)` : ""); ctx.log(`[acp-proxy: refused out-of-recommendation compress — span(s) ${listed} outside the current recommendation set (compress.requireRecommendation)]`); - return compressResult(`[Compression REFUSED: out-of-bounds compression is disabled on this route (compress.requireRecommendation) — span(s) ${listed} contain content bili does not currently recommend folding. Call acp_status for the current compressible ranges and stay within them: subdividing a listed span into smaller chunks is fine, and re-folding an already-compressed block to update its summary is fine.]`, "refused", 0, "out-of-recommendation"); + // #6728: charge the ledger (see the nudge gate above). The + // spec-repeat guard text recordCompressFailure returns is dropped + // on purpose (one counter, stated once — the collapse below says + // it); only the ledger charge matters here. + const specKey = normalizedSpecKey(ranges); + recordCompressFailure(ctx.session, specKey); + const loopGuard = trackLoop ? noteCompressLoopFailure(ctx, specKey) : ""; + const occurrences = compressFailOccurrencesOf(ctx.session, specKey); + if (trackLoop && occurrences >= 2) { + ctx.log( + `[acp-proxy: out-of-recommendation receipt collapsed to one-liner (attempt ${occurrences})]`, + ); + return compressResult( + `[Compression REFUSED again — span(s) ${listed} have now been refused ${occurrences} time(s) the same way (compress.requireRecommendation); nothing was compressed. The full refusal and the current compressible ranges are in the FIRST refused receipt above — do not re-send this call verbatim.${loopGuard}]`, + "refused", + 0, + "out-of-recommendation", + ); + } + // #6728: when nothing is recommended at all — no prepare-time + // offer and no live ranges — "call acp_status and stay within + // them" is a dead end; the context is already at its minimum, + // so say so plainly (#4812-style terminal wording) instead. + const recommendedEmpty = + recommendedIntervals(ctx).length === 0 && + (liveCompressionRanges(ctx)?.ranges.length ?? 0) === 0; + const advice = recommendedEmpty + ? " No compressible ranges remain — the context is already at its minimum; continue the task without compressing. Compression happens again only when there is genuinely new content to fold." + : " Call acp_status for the current compressible ranges and stay within them: subdividing a listed span into smaller chunks is fine, and re-folding an already-compressed block to update its summary is fine."; + return compressResult(`[Compression REFUSED: out-of-bounds compression is disabled on this route (compress.requireRecommendation) — span(s) ${listed} contain content bili does not currently recommend folding.${advice}${loopGuard}]`, "refused", 0, "out-of-recommendation"); } } if (ctx.messages.length > 0) { @@ -1494,10 +1561,15 @@ export function applyRanges(parsed: ReturnType, ctx: // keeps the real refs for the operator. Live refs are untouched: // when the note fires, every requested ref exceeds the session's // highest mapped ref, so no live snapshot number can collide. + // #6712: the scrub must not erase WHICH ref was requested — a bare + // `[stale-ref]` leaves the caller unable to tell what it asked + // for, so it cannot self-correct. The marker keeps the ref number + // (`[stale-ref:00010]`) while never emitting the bare `mNNNNN` + // token, so the #2146 echo channel stays dead. if (beyond !== "") { for (const rg of ranges) { for (const id of [rg.startRef, rg.endRef]) { - receipt = receipt.split(id).join("[stale-ref]"); + receipt = receipt.split(id).join(staleRefMarker(id)); } } } @@ -1643,10 +1715,11 @@ export function applyRanges(parsed: ReturnType, ctx: return prev === undefined || prev !== b.summary; }); syncSubagentSessions(ctx.session, changedBlocks, ctx.compressMessages ?? ctx.messages); - // #4313 toggle (PR #5236): the [start – end] coverage span rides the - // same flag as the wire tags — default OFF keeps receipts byte-identical - // to pre-feature master. Storage still stamps unconditionally; this only - // gates the display span. + // #4313 toggle (PR #5236; default-on follow-up): the [start – end] + // coverage span rides the same flag as the wire tags — default ON; the + // opt-out (file `compress.messageTimestamps: false` or env "0") keeps + // receipts byte-identical to pre-feature master. Storage still stamps + // unconditionally; this only gates the display span. const messageTimestampsOn = resolveMessageTimestamps(loadConfigFile().compress); const subagentMap = subagentSessionsOf(ctx.session); for (const b of changedBlocks) { diff --git a/src/web/styles.ts b/src/web/styles.ts index 5ccb073fb1..183c8068ad 100644 --- a/src/web/styles.ts +++ b/src/web/styles.ts @@ -57,9 +57,14 @@ export const WEB_STYLES = String.raw` framed document's prefers-color-scheme. An explicit pin outranks the OS signal, so the media query below is narrowed to documents WITHOUT one; the pinned palette repeats the same list (CSS cannot share declarations - across media boundaries — keep the two lists in sync). */ + across media boundaries — keep the two lists in sync). + #6587: each dark palette declares color-scheme: dark. The UA paints native + control chrome (a select's option popup, checkbox/radio glyphs, scrollbars) + from color-scheme rather than from the CSS vars, so the declaration keeps + those controls on the dark ground the palette establishes. */ @media (prefers-color-scheme: dark) { :root:not([data-theme]) { + color-scheme: dark; --bg: #0d1117; --bg-elev: #161b22; --bg-muted: #1c2129; diff --git a/tests/cache-fold-registration.test.ts b/tests/cache-fold-registration.test.ts index 6645483500..efc5d36056 100644 --- a/tests/cache-fold-registration.test.ts +++ b/tests/cache-fold-registration.test.ts @@ -111,7 +111,8 @@ test("duplicates and out-of-order block IDs keep the existing high-water behavio assert.deepEqual(ledger.folds.map((fold) => [fold.bid, fold.seq, fold.k]), [["b1", 1, 0], ["b5", 2, 0], ["b7", 3, null]]); }); -test("batches and individual calls produce identical complete ledgers and reports across usage samples", () => { +test("batches and individual calls produce identical complete ledgers and reports across usage samples", (t) => { + t.mock.method(Date, "now", () => T0 + 10_000); const batched = createSession("fold-comparison"); const separate = createSession("fold-comparison"); for (const session of [batched, separate]) recordCacheSample(session, { at: T0, input: 10_000, cached: 9_000 }); diff --git a/tests/dsh-settings-ui.test.ts b/tests/dsh-settings-ui.test.ts index 5c55c28055..6c8ac47268 100644 --- a/tests/dsh-settings-ui.test.ts +++ b/tests/dsh-settings-ui.test.ts @@ -2260,3 +2260,33 @@ test("#4406: the Context tab marks the conversation scroll container and hides t m.runCleanups(); assert.equal(scrollAttrs.has("data-bili-surface"), false, "leaving the tab drops the marker"); }); + +test("#6587: both dark palettes bind color-scheme so native controls follow the theme", () => { + // `color-scheme` is what the UA paints native control chrome from: a + // (#/config -> compress.promptPack) pops a white + // option list over the dark settings face while the page itself is dark. + // Scope the read: the base :root also declares `color-scheme: light dark`, + // so a bare includes() passes even with both dark blocks stripped. + const styles = fs.readFileSync(new URL("../src/web/styles.ts", import.meta.url), "utf8").replace(/\r\n/g, "\n"); + const decls = (open: string): string[] => { + const i = styles.indexOf(open); + assert.ok(i >= 0, `${open} present`); + return styles + .slice(i + open.length, styles.indexOf("}", i)) + .split("\n") + .map((l) => l.trim().replace(/;\s*$/, "")) + .filter((l) => l !== ""); + }; + assert.ok( + decls(':root[data-theme="dark"] {').includes("color-scheme: dark"), + "the pinned dark palette binds color-scheme: dark", + ); + assert.ok( + decls(":root:not([data-theme]) {").includes("color-scheme: dark"), + "the OS-driven dark palette binds color-scheme: dark for native controls", + ); +}); diff --git a/tests/dsh-tool-registration-lifetime.test.ts b/tests/dsh-tool-registration-lifetime.test.ts new file mode 100644 index 0000000000..d7b0bc71b2 --- /dev/null +++ b/tests/dsh-tool-registration-lifetime.test.ts @@ -0,0 +1,288 @@ +import test, { afterEach, mock } from "node:test"; +import assert from "node:assert/strict"; +import { Context, Service, type Plugin } from "@deepseek-ai/cordis"; +import { + apply, maybeRetryForTest, _resetRegisterForTest, _settleNativeForTest, _nativeStateForTest, + _registerStateForTest, _stateToolsReadyForTest, _stateHeadersForTest, + type PluginContext, +} from "../src/agent/dsh-native.ts"; + +const names = ["compress", "decompress", "search_context", "acp_status", "acp_cache", "bili_status"]; +type Definition = Parameters[0]; +const manifest = (toolNames = names) => Response.json({ tools: { anthropic: toolNames.map((name) => ({ + name, description: name, input_schema: { type: "object", properties: {} }, +})) } }); +const deferred = () => { + let resolve!: () => void; + const promise = new Promise((done) => { resolve = done; }); + return { promise, resolve }; +}; + +function host() { + const definitions = new Map(); + const listeners = new Set<() => void>(); + const changed = () => { for (const listener of listeners) listener(); }; + function owner() { + const disposers: Array<() => void> = []; + const ctx: PluginContext = { + tools: { + get: (name) => definitions.get(name), + register: (definition) => { + if (definitions.has(definition.name)) throw new Error(`tool "${definition.name}" is already registered`); + definitions.set(definition.name, definition); + const dispose = () => { + if (definitions.get(definition.name) !== definition) return; + definitions.delete(definition.name); + changed(); + }; + disposers.push(dispose); + changed(); + return dispose; + }, + }, + commands: { register() {} }, + agents: { currentInitiator: () => ({ session: { id: "lifetime-session" } }) }, + inject() {}, + effect: (effect) => { const dispose = effect(); if (dispose) disposers.push(dispose); }, + on: (event, listener) => { + if (event !== "tools/change") return; + const changedListener = listener as () => void; + listeners.add(changedListener); + disposers.push(() => { listeners.delete(changedListener); }); + }, + }; + return { ctx, dispose: () => { for (const dispose of disposers.splice(0)) dispose(); } }; + } + return { definitions, owner }; +} + +function reset() { + _resetRegisterForTest("http://bili-lifetime.invalid"); + _nativeStateForTest().ready = Promise.resolve("http://bili-lifetime.invalid"); +} + +afterEach(() => { + mock.restoreAll(); + void _settleNativeForTest(); + _resetRegisterForTest(undefined); +}); + +test("same-context restart re-registers all tools and disposal clears readiness", async () => { + const registry = host(); + const owner = registry.owner(); + let requests = 0; + mock.method(globalThis, "fetch", async () => { requests++; return manifest(); }); + reset(); + await apply(owner.ctx); + await _stateToolsReadyForTest(); + assert.deepEqual([...registry.definitions.keys()], names); + owner.dispose(); + assert.equal(_registerStateForTest().toolsReady, false); + assert.equal(registry.definitions.size, 0); + await apply(owner.ctx); + await _stateToolsReadyForTest(); + assert.equal(requests, 2); + assert.deepEqual([...registry.definitions.keys()], names); + assert.equal(_stateHeadersForTest()?.("http://model.invalid/v1/messages")?.["x-bili-plugin"], "dsh"); + owner.dispose(); + maybeRetryForTest(owner.ctx); + assert.equal(registry.definitions.size, 0, "disposed activation must never resurrect its tools"); +}); + +test("borrowed registrations survive new-owner disposal and heal after old-owner disposal", async () => { + const registry = host(); + const oldOwner = registry.owner(); + const newOwner = registry.owner(); + mock.method(globalThis, "fetch", async () => manifest()); + reset(); + await apply(oldOwner.ctx); + await _stateToolsReadyForTest(); + const borrowed = registry.definitions.get("compress"); + await apply(newOwner.ctx); + await _stateToolsReadyForTest(); + assert.equal(registry.definitions.get("compress"), borrowed); + oldOwner.dispose(); + assert.deepEqual([...registry.definitions.keys()].sort(), [...names].sort()); + assert.notEqual(registry.definitions.get("compress"), borrowed); + assert.equal(_registerStateForTest().toolsReady, true); + const foreignOwner = registry.owner(); + const definition = registry.definitions.get("compress"); + assert.ok(definition); + newOwner.dispose(); + foreignOwner.ctx.tools.register(definition); + const borrowingOwner = registry.owner(); + await apply(borrowingOwner.ctx); + await _stateToolsReadyForTest(); + assert.equal(registry.definitions.get("compress"), definition); + borrowingOwner.dispose(); + assert.equal(registry.definitions.get("compress"), definition, "foreign registration keeps its original lifetime"); + foreignOwner.dispose(); + assert.equal(registry.definitions.size, 0); +}); + +test("a late old manifest cannot register tools or clear the new activation's pending flight", async () => { + const registry = host(); + const owner = registry.owner(); + const entered = [deferred(), deferred()]; + const held = [deferred(), deferred()]; + let requests = 0; + mock.method(globalThis, "fetch", async () => { + const index = requests++; + assert.ok(index < 2, "new activation must retain its single-flight latch"); + entered[index].resolve(); + await held[index].promise; + return manifest(index === 0 ? ["stale_tool"] : names); + }); + reset(); + await apply(owner.ctx); + await entered[0].promise; + const oldGate = _stateToolsReadyForTest(); + owner.dispose(); + await apply(owner.ctx); + await entered[1].promise; + held[0].resolve(); + await oldGate; + assert.equal(registry.definitions.size, 0, "old manifest must not pollute the new activation"); + assert.equal(_registerStateForTest().toolsReady, false); + maybeRetryForTest(owner.ctx); + held[1].resolve(); + await _stateToolsReadyForTest(); + assert.equal(requests, 2); + assert.deepEqual([...registry.definitions.keys()], names); + assert.equal(_registerStateForTest().toolsReady, true); + owner.dispose(); +}); + +// #6702: the mock owners above cannot reproduce the failure orderings that +// caused the bug — Cordis restarts reuse one context while its effect-owned +// registrations die — so the final scenario runs against a real fiber with the +// `@deepseek-ai/cordis` dev dependency. TestTools mirrors the real +// ToolsService ownership: extending cordis Service makes `this.ctx` resolve to + // the CALLING plugin's context, so each registration is held by that fiber's + // effect and teardown emits tools/change. + +class TestTools extends Service { + definitions = new Map(); + + constructor(ctx: Context) { + super(ctx, "tools"); + } + + get(name: string) { + return this.definitions.get(name); + } + + register(definition: Definition) { + if (this.definitions.has(definition.name)) throw new Error(`tool "${definition.name}" is already registered`); + return this.ctx.effect(() => { + this.definitions.set(definition.name, definition); + return () => { + if (this.definitions.get(definition.name) !== definition) return; + this.definitions.delete(definition.name); + this.ctx.events.emit("tools/change"); + }; + }, `bili-test: ${definition.name}`); + } +} + +test("#6702 real Cordis fibers own registration lifetime across restart, adoption and teardown", async () => { + const ctx = new Context(); + const owners: PluginContext[] = []; + let tools: TestTools | undefined; + const liveNames = () => names.filter((name) => tools?.definitions.get(name) !== undefined); + const biliPlugin = { + inject: ["tools", "commands", "agents"], + apply(owner: PluginContext) { + owners.push(owner); + return apply(owner); + }, + }; + try { + await ctx.plugin({ apply(owner) { + owner.provide("commands", { register() {} }); + owner.provide("agents", { currentInitiator: () => ({ session: { id: "lifetime-session" } }) }); + tools = new TestTools(owner); + } }); + const load = (plugin: unknown) => ctx.plugin(plugin as Plugin.Object); + + // Same-context restart: one context, two activations, each owning its + // own registrations — the reused context never inherits the stale + // ready stamp, and the old activation's registrations die with it. + reset(); + let requests = 0; + mock.method(globalThis, "fetch", async () => { requests++; return manifest(); }); + const first = await load(biliPlugin); + await _stateToolsReadyForTest(); + assert.deepEqual(liveNames(), names); + assert.equal(_registerStateForTest().toolsReady, true); + await first.restart(); + await _stateToolsReadyForTest(); + assert.equal(owners[0], owners[1], "a Cordis restart reuses one context"); + assert.equal(requests, 2, "the reused context re-registers instead of trusting the stale latch"); + assert.deepEqual(liveNames(), names); + assert.equal(_registerStateForTest().toolsReady, true); + await first.dispose(); + assert.deepEqual(liveNames(), [], "disposing the fiber removes its own registrations"); + assert.equal(_registerStateForTest().toolsReady, false); + + // A same-named registration held by another fiber keeps its original + // lifetime: adopting it must not transfer ownership to bili, and the + // borrower's disposal must not remove it. + reset(); + const foreignDefinition: Definition = { + name: "compress", + description: "foreign", + parameters: { type: "object", properties: {} }, + output: { schema: {}, render: () => [{ type: "text", text: "foreign" }] }, + execute: async () => "foreign", + }; + const foreign = await ctx.plugin({ inject: ["tools"], apply(owner) { + (owner as Context & { tools: TestTools }).tools.register(foreignDefinition); + } }); + const borrower = await load(biliPlugin); + await _stateToolsReadyForTest(); + assert.deepEqual(liveNames(), names, "the new activation registers around the adopted foreign tool"); + await borrower.dispose(); + assert.deepEqual(liveNames(), ["compress"], "disposing the borrower keeps the foreign registration"); + assert.equal(_registerStateForTest().toolsReady, false); + const replacementBorrower = await load(biliPlugin); + await _stateToolsReadyForTest(); + await foreign.dispose(); + assert.deepEqual(liveNames(), names, "the active borrower restores registrations removed with the old owner"); + assert.notEqual(tools?.get("compress"), foreignDefinition); + await replacementBorrower.dispose(); + assert.deepEqual(liveNames(), [], "the replacement registrations belong to the active borrower"); + + // A late old manifest must not pollute the next activation or clear + // its single-flight latch. + reset(); + const entered = [deferred(), deferred()]; + const held = [deferred(), deferred()]; + let index = 0; + mock.method(globalThis, "fetch", async () => { + const i = index++; + assert.ok(i < 2, "the new activation must retain its single-flight latch"); + entered[i].resolve(); + await held[i].promise; + return manifest(i === 0 ? ["stale_tool"] : names); + }); + const pending = await load(biliPlugin); + await entered[0].promise; + const oldGate = _stateToolsReadyForTest(); + await pending.restart(); + await entered[1].promise; + held[0].resolve(); + await oldGate; + assert.deepEqual(liveNames(), [], "the old manifest must not pollute the new activation"); + assert.equal(_registerStateForTest().toolsReady, false); + maybeRetryForTest(owners.at(-1)!); + held[1].resolve(); + await _stateToolsReadyForTest(); + assert.equal(index, 2); + assert.deepEqual(liveNames(), names); + await pending.dispose(); + assert.deepEqual(liveNames(), [], "final disposal leaves no registrations behind"); + } finally { + await ctx.fiber.dispose(); + } +}); diff --git a/tests/e2e-session-identity.test.ts b/tests/e2e-session-identity.test.ts index 289709ff7d..b10ccbb674 100644 --- a/tests/e2e-session-identity.test.ts +++ b/tests/e2e-session-identity.test.ts @@ -375,6 +375,55 @@ test("e2e session identity: anonymous degenerate request (no history signal) → } }); +test("e2e session identity: anonymous single-shot Responses server-tool call forwards verbatim, no session (#6548)", async () => { + const h = await startHarness(); + try { + // pi-web-access's xAI web-search backend: string input, web_search / + // x_search tools only, no history, and no knob for a session id — the + // client cannot carry an identity here. Pre-fix the #286 refusal + // hard-400d it; post-fix it rides the verbatim forward like any + // utility call and creates no session state. + const body = { model: "gpt-test", stream: true, input: "latest news on context compression", tools: [{ type: "web_search" }, { type: "x_search" }] }; + const resp = await postRaw(h, 0, "/v1/responses", body, { authorization: "Bearer " }); + assert.equal(resp.status, 200, `proxy returned ${resp.status}: ${await resp.text()}`); + assert.equal((await getSessions(h)).length, 0, "verbatim forward must not create a session"); + } finally { + await h.close(); + } +}); + +test("e2e session identity: anonymous Responses single-shot shape with a client tool still 400s (#6548 negative)", async () => { + const h = await startHarness(); + try { + // A function tool makes it an agent turn, not a server-tool utility + // call — the #286 refusal still applies. + const body = { model: "gpt-test", stream: true, input: "what is the weather", tools: [{ type: "function", name: "get_weather", description: "weather", parameters: { type: "object", properties: {} } }] }; + const resp = await postRaw(h, 0, "/v1/responses", body, { authorization: "Bearer " }); + assert.equal(resp.status, 400); + const json = (await resp.json()) as { error?: { message?: string } }; + assert.match(json.error?.message ?? "", /Missing stable conversation identity/); + assert.equal((await getSessions(h)).length, 0); + } finally { + await h.close(); + } +}); + +test("e2e session identity: anonymous Responses search call chaining previous_response_id still 400s (#6548 negative)", async () => { + const h = await startHarness(); + try { + // previous_response_id chains the call onto a prior response — a + // conversation continuation, not a single shot. + const body = { model: "gpt-test", stream: true, input: "follow-up search", tools: [{ type: "web_search" }], previous_response_id: "resp_abc" }; + const resp = await postRaw(h, 0, "/v1/responses", body, { authorization: "Bearer " }); + assert.equal(resp.status, 400); + const json = (await resp.json()) as { error?: { message?: string } }; + assert.match(json.error?.message ?? "", /Missing stable conversation identity/); + assert.equal((await getSessions(h)).length, 0); + } finally { + await h.close(); + } +}); + test("e2e session identity: anonymous openai request → prefix-affinity session; distinct conversations split (#309)", async () => { const h = await startHarness(); try { diff --git a/tests/hermes-native.test.ts b/tests/hermes-native.test.ts index 2c3c79dc26..26cf1057a0 100644 --- a/tests/hermes-native.test.ts +++ b/tests/hermes-native.test.ts @@ -368,6 +368,34 @@ def install_sidecar(bad): with open(os.path.join(PLUGIN_DIR, "bili.json"), "w") as f: json.dump({"proxyScript": script, "nodePath": os.environ["BC_NODE"]}, f) +def write_dist(base_dir): + d = os.path.join(base_dir, "billion-context", "dist") + os.makedirs(d, exist_ok=True) + p = os.path.join(d, "index.js") + with open(p, "w") as f: + f.write("// dummy bili dist entry point\n") + os.chmod(p, 0o755) # npm chmods the bin it links onto; \`bili\` must be executable on PATH + return p + +def stub_bili(bin_dir, target): + p = os.path.join(bin_dir, "bili") + if os.path.exists(p): + os.remove(p) + os.symlink(target, p) + return p + +def clean_node_bin(): + # A bin dir holding ONLY a node shim. The "no bili anywhere" assertions must hold against a + # PATH that has node but no bili; the node's own bin dir is NOT safe for that, because a real + # global install puts its \`bili\` symlink next to \`node\` on the standard npm layout. + d = os.path.join(BASE, "cleanbin") + os.makedirs(d, exist_ok=True) + node = os.path.join(d, "node") + if os.path.lexists(node): + os.remove(node) + os.symlink(os.environ["BC_NODE"], node) + return d + # --- #2905 instance-record fixtures (identity fields discovery now checks) --- def fp_of(bad): @@ -499,7 +527,45 @@ elif SCENARIO.startswith("gate-"): os.environ[envvar] = "http://127.0.0.1:1" if envvar == "BILLION_CONTEXT_PROXY" else "0" mod.register(ctx) elif SCENARIO == "no-sidecar": + # Hermetic: a \`bili\` on the machine's own PATH must not turn "no dist anywhere" into a + # resolution — the ladder's last rung is asserted against a PATH that holds node only. + # clean_node_bin(), not the node's own dir: a real global install sits next to node there. + os.environ["PATH"] = clean_node_bin() mod.register(ctx) +elif SCENARIO == "load-sidecar": + # #6672: the sidecar resolution ladder, first hit wins and nothing is ever written. + before = sorted(os.listdir(PLUGIN_DIR)) + res = {} + install_sidecar(False) + res["sidecar"] = mod.load_sidecar() + os.remove(os.path.join(PLUGIN_DIR, "bili.json")) + global_dist = write_dist(os.path.join(BASE, "global")) + write_dist(os.path.join(PLUGIN_DIR, "node_modules")) + res["bundled"] = mod.load_sidecar() + shutil.rmtree(os.path.join(PLUGIN_DIR, "node_modules")) + if os.name == "nt": + res["path"] = res["path_wrapper"] = res["none"] = "skip" + else: + bin_dir = os.path.join(BASE, "bin") + os.makedirs(bin_dir, exist_ok=True) + # node_dir must hold node but NO bili: clean_node_bin(), not the node's own bin dir, + # which on a standard install also carries the real global \`bili\` symlink. + node_dir = clean_node_bin() + wrapper = os.path.join(BASE, "wrapper.sh") + with open(wrapper, "w") as f: + f.write("#!/bin/sh\nexit 0\n") + os.chmod(wrapper, 0o755) + stub_bili(bin_dir, global_dist) + os.environ["PATH"] = bin_dir + os.pathsep + node_dir + res["path"] = mod.load_sidecar() + # A shell wrapper is not the dist: the realpath+.js guard falls through to inert. + stub_bili(bin_dir, wrapper) + res["path_wrapper"] = mod.load_sidecar() + os.remove(os.path.join(bin_dir, "bili")) + os.environ["PATH"] = node_dir + res["none"] = mod.load_sidecar() + out["sidecar_resolution"] = res + out["plugin_dir_unchanged"] = sorted(os.listdir(PLUGIN_DIR)) == before elif SCENARIO == "spawn-fail": install_sidecar(True) mod.register(ctx) @@ -1272,6 +1338,8 @@ interface DriverOut { rf_dead?: unknown; rf_dup_pid?: string | null; rf_none?: unknown; + sidecar_resolution?: Record; + plugin_dir_unchanged?: boolean; } function runDriver(scenario: string, extraEnv: Record = {}): { out: DriverOut | null; err: string } { @@ -1667,6 +1735,38 @@ describe("python plugin runtime (subprocess)", () => { assert.equal(out!.env_https_proxy, null); }); + test("no sidecar: resolves the dist from a host-managed copy, then from `bili` on PATH (#6672)", () => { + if (!PY) { + console.warn("skip: no python3/python interpreter on this machine (hermes requires Python 3.11+)"); + return; + } + const { out, err } = runDriver("load-sidecar"); + assert.ok(out, err); + const res = out!.sidecar_resolution!; + const scriptOf = (v: unknown): string | null => (v && typeof v === "object" ? (v as { script: string }).script : null); + // 1. the installer's sidecar still wins untouched + assert.match(scriptOf(res.sidecar)!, /[/\\]proxy\.js$/, "bili.json stays the first hit"); + // 2. a host-managed copy in the plugin's own node_modules + assert.match( + scriptOf(res.bundled)!, + /[/\\]node_modules[/\\]billion-context[/\\]dist[/\\]index\.js$/, + "no sidecar: the plugin resolves the dist its own npm install put there", + ); + if (process.platform !== "win32") { + // 3. the global install, reached through `bili` on PATH (realpath onto dist/index.js) + assert.match( + scriptOf(res.path)!, + /[/\\]global[/\\]billion-context[/\\]dist[/\\]index\.js$/, + "no sidecar, no local copy: the reference lane's global dist is resolved off PATH", + ); + // 3b. a shell wrapper named bili is not the dist — the realpath+.js guard keeps it inert + assert.equal(res.path_wrapper, null, "a non-.js entry point falls through instead of spawning it"); + // 4. nothing anywhere is still inert + assert.equal(res.none, null); + } + assert.equal(out!.plugin_dir_unchanged, true, "resolution is read-only: no sidecar is ever written"); + }); + test("spawn failure: child dies -> inert, env untouched, starting marker cleaned up", () => { if (!PY) { console.warn("skip: no python3/python interpreter on this machine (hermes requires Python 3.11+)"); diff --git a/tests/incremental-large-body.test.ts b/tests/incremental-large-body.test.ts new file mode 100644 index 0000000000..e8d92696f0 --- /dev/null +++ b/tests/incremental-large-body.test.ts @@ -0,0 +1,21 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { incrementalTurnBodyShape } from "../src/incremental-lane.js"; +import { incrementalEnvelopeConversationId } from "../src/agent/incremental-client.js"; + +test("large incremental envelopes keep the same lane classification as the client", () => { + const envelope = { conversationId: "synthetic-large-seed", sinceSeq: 0, messages: [{ role: "user", content: "synthetic" }], request: { model: "fixture-model" } }; + assert.equal(incrementalEnvelopeConversationId(envelope), envelope.conversationId); + // Valid JSON whitespace crosses the old heuristic without allocating a + // huge nested text object. readBody owns the actual receive-size budget. + const raw = Buffer.from(JSON.stringify(envelope) + " ".repeat(64 * 1024 * 1024)); + assert.ok(raw.length > 64 * 1024 * 1024); + assert.equal(incrementalTurnBodyShape(raw), true, "must not relay a lane-only envelope as an ordinary model request"); +}); + +test("large marked ordinary requests retain their ordinary classification", () => { + const ordinary = { model: "fixture-model", messages: [{ role: "user", content: "synthetic" }] }; + const raw = Buffer.from(JSON.stringify(ordinary) + " ".repeat(64 * 1024 * 1024)); + assert.equal(incrementalEnvelopeConversationId(ordinary), undefined); + assert.equal(incrementalTurnBodyShape(raw), false); +}); diff --git a/tests/issue1195-overflow-refold.test.ts b/tests/issue1195-overflow-refold.test.ts index 1d4faca825..eca972199c 100644 --- a/tests/issue1195-overflow-refold.test.ts +++ b/tests/issue1195-overflow-refold.test.ts @@ -114,7 +114,10 @@ interface RecordedUpstream { // Records every request body in order. Streaming requests are answered by // `onStreaming(callIndex)`; stream:false (summarization) requests by the // in-band summary response. -async function recordedUpstream(onStreaming: (callIndex: number, res: http.ServerResponse) => void): Promise { +async function recordedUpstream( + onStreaming: (callIndex: number, res: http.ServerResponse, body: string) => void, + summarize = summaryJson, +): Promise { const bodies: string[] = []; let streamingCall = 0; const server = http.createServer((req, res) => { @@ -131,12 +134,12 @@ async function recordedUpstream(onStreaming: (callIndex: number, res: http.Serve } if (parsed.stream === false) { res.writeHead(200, { "content-type": "application/json" }); - res.end(summaryJson()); + res.end(summarize()); return; } const call = streamingCall; streamingCall += 1; - onStreaming(call, res); + onStreaming(call, res, raw); }); }); server.listen(0, "127.0.0.1"); @@ -336,3 +339,90 @@ test("e2e #1195 C: when the folded retry is ALSO rejected, the client sees the R assert.equal(s!.stats.overflowArmTokens, 128000, "#1110 arm record stays at the stated window"); assert.equal(s!.metadata.confirmedContextLimits, undefined, "#987: nothing learned"); }); + +for (const scenario of ["fits-before-refold", "fits-after-refold", "still-oversized", "budget-retry-rate-limited", "mismatched-output"] as const) { + test(`e2e #1195: LiteLLM input ${scenario}`, async (t) => { + const window = 128_000; + const firstInput = scenario === "fits-after-refold" || scenario === "still-oversized" ? 150_000 : 108_000; + const foldedInput = scenario === "still-oversized" ? 129_000 : 108_000; + const upstream = await recordedUpstream((call, res, raw) => { + const { max_tokens: output } = JSON.parse(raw) as { max_tokens: number }; + const input = call === 0 ? firstInput : foldedInput; + if (scenario === "budget-retry-rate-limited" && call === 1) { + res.writeHead(429, { "content-type": "application/json", "retry-after": "12" }); + res.end('{"error":{"message":"rate limited"}}'); + return; + } + if (input + output <= window) { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end( + 'data: {"id":"reply","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","content":"recovered"},"finish_reason":null}]}\n\n' + + 'data: {"id":"reply","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":108000,"completion_tokens":1,"total_tokens":108001}}\n\n' + + "data: [DONE]\n\n", + ); + return; + } + const statedOutput = scenario === "mismatched-output" ? output + 1 : output; + res.writeHead(400, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: { type: "BadRequestError", message: + `Requested token count exceeds the model's maximum context length of ${window} tokens. You requested a total of ${input + statedOutput} tokens: ${input} tokens from the input messages and ${statedOutput} tokens for the completion.` + } })); + }, () => JSON.stringify({ choices: [{ message: { role: "assistant", content: SUMMARY_TEXT }, finish_reason: "stop" }] })); + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const proxy = await startServer(proxyBaseOptions(upstream.port, 400_000)); + await once(proxy, "listening"); + t.after(async () => { + proxy.close(); + proxy.closeIdleConnections?.(); + await once(proxy, "close"); + upstream.server.close(); + upstream.server.closeIdleConnections?.(); + await once(upstream.server, "close"); + }); + + const proxyPort = (proxy.address() as { port: number }).port; + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstream.port}/v1/chat/completions`; + const body = JSON.stringify({ model: "claude-test", max_tokens: 32_000, stream: true, messages: bigConversation() }); + const headers = { "content-type": "application/json", "x-acp-session": `refold-split-${scenario}` }; + const response = await fetch(url, { method: "POST", headers, body }); + const responseText = await response.text(); + const sends = upstream.bodies + .map(raw => JSON.parse(raw) as { stream?: boolean; max_tokens: number }) + .filter(body => body.stream === true); + if (scenario === "budget-retry-rate-limited") { + assert.equal(response.status, 429); + assert.equal(response.headers.get("retry-after"), "12"); + assert.match(responseText, /rate limited/); + assert.equal(upstream.bodies.length, 2); + return; + } + if (scenario === "mismatched-output") { + assert.equal(response.status, 400); + assert.equal(sends.length, 2, "only the existing refold is sent when reported output differs from the wire"); + assert.ok(JSON.stringify(sends[1]).includes(SUMMARY_TEXT)); + return; + } + if (scenario === "fits-before-refold") { + assert.equal(response.status, 200, responseText); + assert.match(responseText, /recovered/); + assert.equal(upstream.bodies.length, 2, "the exact payload fits after a budget-only retry, without any summarization"); + assert.deepEqual(sends[1], { ...sends[0], max_tokens: window - firstInput }); + return; + } + assert.ok(sends[1].max_tokens > window - foldedInput, "the folded retry must be rejected by the billed-token wall"); + assert.ok(JSON.stringify(sends[1]).includes(SUMMARY_TEXT), "the second send contains the refold"); + if (scenario === "fits-after-refold") { + assert.equal(response.status, 200, responseText); + assert.match(responseText, /recovered/); + assert.equal(sends.length, 3); + assert.equal(sends[2].max_tokens, window - foldedInput); + assert.deepEqual(sends[2], { ...sends[1], max_tokens: window - foldedInput }); + } else { + assert.equal(response.status, 400); + assert.match(responseText, /129000 tokens from the input messages/); + assert.equal(sends.length, 2, "an output clamp cannot rescue oversized input"); + } + }); +} diff --git a/tests/issue2146-compress-loop-breaker.test.ts b/tests/issue2146-compress-loop-breaker.test.ts index 1618a0a14c..ffd18d83f1 100644 --- a/tests/issue2146-compress-loop-breaker.test.ts +++ b/tests/issue2146-compress-loop-breaker.test.ts @@ -188,7 +188,8 @@ test("#2146: stale-generation receipts scrub the phantom refs; in-frontier failu assert.ok(stale.startsWith("[Compression FAILED:"), `unknown refs fail (got: ${stale.slice(0, 120)})`); assert.match(stale, /above this session's highest ref \(m00008\)/, "frontier diagnosis present"); assert.doesNotMatch(stale, /m00010|m00014/, "phantom endpoints are NOT echoed back into the client-persisted receipt"); - assert.match(stale, /\[stale-ref\]/, "scrubbed marker takes their place"); + assert.match(stale, /\[stale-ref:00010\]/, "scrubbed marker takes their place — with the ref number kept (#6712)"); + assert.match(stale, /\[stale-ref:00014\]/, "end ref number kept too (#6712)"); const mixed = applyRanges(parseCompressInput(compressArgs("m00001", "m00009")), ctx).text; assert.ok(mixed.startsWith("[Compressed "), "#4521 flip: an out-frontier endId clamps to the highest allocated ref instead of failing"); diff --git a/tests/issue5192-drain-head-audit.test.ts b/tests/issue5192-drain-head-audit.test.ts new file mode 100644 index 0000000000..36a2ea9f77 --- /dev/null +++ b/tests/issue5192-drain-head-audit.test.ts @@ -0,0 +1,194 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { createTagEchoFilter } from "../src/loop/tag-echo-filter.ts"; +import { pipePluginChatWithStrip } from "../src/plugin.ts"; +import { setLogCapture } from "../src/logger.ts"; +import type { Session } from "../src/session.ts"; + +const LT = "\x3c"; +const OPEN = `${LT}acp `; +const CLOSE = `${LT}/acp>`; + +interface Drop { + snippet: string; + duringDrain: boolean; +} + +/** Records what the filter reports, keeping the drain flag verbatim so a + * missing second argument is observable rather than silently falsy. */ +function recorder(): { drops: Drop[]; onDrop: (snippet: string, duringDrain?: boolean) => void } { + const drops: Drop[] = []; + return { + drops, + onDrop: (snippet: string, duringDrain?: boolean) => { + drops.push({ snippet, duringDrain: duringDrain === true }); + }, + }; +} + +function tag(ref: string, tokens = 9): string { + return `${OPEN}tokens="${tokens}" type="text">${ref}${CLOSE}`; +} + +const HEAD = `${LT}acp tokens="12" type="text" time="2026-11-13 17:51:02"`; + +test("#5192: a tag head discarded by flush() is reported as a drain drop", () => { + const { drops, onDrop } = recorder(); + const f = createTagEchoFilter(onDrop); + assert.equal(f.push(`前の文章。${HEAD}`), "前の文章。"); + assert.equal(f.flush(), ""); + assert.equal(drops.length, 1); + assert.equal(drops[0]!.snippet, HEAD); + assert.equal(drops[0]!.duringDrain, true); +}); + +test("#5192: a completed tag strip is not reported as a drain drop", () => { + const { drops, onDrop } = recorder(); + const f = createTagEchoFilter(onDrop); + assert.equal(f.push(`前の文章。${tag("m00011")}後`), "前の文章。後"); + assert.equal(f.flush(), ""); + assert.equal(drops.length, 1); + assert.equal(drops[0]!.duringDrain, false); +}); + +test("#5192: a drain drop is reported even after an earlier strip claimed the one-shot notice", () => { + const { drops, onDrop } = recorder(); + const f = createTagEchoFilter(onDrop); + assert.equal(f.push(`前の文章。${tag("m00011")}中${HEAD}`), "前の文章。中"); + assert.equal(f.flush(), ""); + assert.equal(drops.length, 2, "the drain drop must not be swallowed by the one-shot notice"); + assert.equal(drops[1]!.snippet, HEAD); + assert.equal(drops[1]!.duringDrain, true); +}); + +test("#5192: a truncated close tag discarded by flush() is also a drain drop", () => { + const { drops, onDrop } = recorder(); + const f = createTagEchoFilter(onDrop); + assert.equal(f.push(`前の文章。${LT}/acp`), "前の文章。"); + assert.equal(f.flush(), ""); + assert.equal(drops.length, 1); + assert.equal(drops[0]!.snippet, `${LT}/acp`); + assert.equal(drops[0]!.duringDrain, true); +}); + +test("#5192: the one-shot notice for ordinary strips is unchanged", () => { + const { drops, onDrop } = recorder(); + const f = createTagEchoFilter(onDrop); + assert.equal(f.push(`前${tag("m00011", 1)}中${tag("m00012", 2)}後`), "前中後"); + assert.equal(f.flush(), ""); + assert.equal(drops.length, 1, "only the first ordinary strip is announced"); + assert.equal(drops[0]!.duringDrain, false); +}); + +test("#5192: a drain drop carries no closing bracket, so it is never mistakable for a full tag", () => { + const { drops, onDrop } = recorder(); + const f = createTagEchoFilter(onDrop); + f.push(`前の文章。${HEAD}`); + f.flush(); + assert.equal(drops.length, 1); + assert.equal(drops[0]!.snippet.includes(">"), false); +}); + +test("#5192: a swallowed tag tail discarded at EOF is reported as a drain drop", () => { + const { drops, onDrop } = recorder(); + const f = createTagEchoFilter(onDrop); + // A complete opening tag followed by content, but no close: the stream + // died mid-tag. The opening tag is an ordinary strip; the swallowed tail + // held at EOF is a drain drop (previously silent). + assert.equal(f.push(`prose ${LT}acp tokens="12" type="text">m00582`), "prose "); + assert.equal(f.flush(), ""); + assert.equal(drops.length, 2, "the drain drop must not be swallowed by the one-shot notice"); + assert.equal(drops[0]!.duringDrain, false, "the opening tag strip is an ordinary drop"); + assert.equal(drops[1]!.snippet, "m00582"); + assert.equal(drops[1]!.duringDrain, true, "the swallowed tail is a drain drop"); +}); + +test("#5192: a swallowed tag tail drain drop is reported even after an earlier strip claimed the one-shot notice", () => { + const { drops, onDrop } = recorder(); + const f = createTagEchoFilter(onDrop); + // An earlier completed tag claims the one-shot strip notice; the swallowed + // tail of a later unclosed tag is a drain drop (previously silent). + assert.equal(f.push(`前${tag("m00011")}中${LT}acp tokens="12" type="text">m00582`), "前中"); + assert.equal(f.flush(), ""); + assert.equal(drops.length, 2, "the drain drop must not be swallowed by the one-shot notice"); + assert.equal(drops[1]!.snippet, "m00582"); + assert.equal(drops[1]!.duringDrain, true, "the swallowed tail is a drain drop"); +}); + +function makeSession(): Session { + return { + id: "drainsess", + protocol: "openai", + upstreamOrigin: "http://127.0.0.1:9/v1", + label: "test", + createdAt: 0, + lastUsedAt: 0, + requests: 0, + lastInputTokens: 0, + stats: {}, + dirty: false, + } as unknown as Session; +} + +function makeRes(chunks: string[]) { + return { + write(b: Buffer | string) { + chunks.push(typeof b === "string" ? b : b.toString("utf8")); + return true; + }, + end(b?: Buffer | string) { + if (b !== undefined) chunks.push(typeof b === "string" ? b : b.toString("utf8")); + }, + once() {}, + destroyed: false, + writableEnded: false, + } as unknown as import("node:http").ServerResponse; +} + +function streamOf(events: string[]): ReadableStream { + const enc = new TextEncoder(); + let i = 0; + return new ReadableStream({ + pull(controller) { + if (i < events.length) { + controller.enqueue(enc.encode(events[i])); + i += 1; + } else { + controller.close(); + } + }, + }); +} + +function chatChunk(delta: Record): string { + return `data: ${JSON.stringify({ id: "chatcmpl-1", object: "chat.completion.chunk", created: 1, model: "qwen", choices: [{ index: 0, delta, finish_reason: null }] })}\n\n`; +} + +test("#5192: the passthrough logs a held head dropped at stream end as a drain drop", async () => { + const logs: string[] = []; + setLogCapture((_level, msg) => { logs.push(msg); }); + try { + const out: string[] = []; + // The completed tag claims the one-shot strip notice; the head held at + // EOF is the second drop of the response and used to leave no line. + const events: string[] = [ + chatChunk({ role: "assistant" }), + chatChunk({ content: `前の文章。${tag("m00042")}中` }), + chatChunk({ content: `後${HEAD}` }), + "data: [DONE]\n\n", + ]; + await pipePluginChatWithStrip(streamOf(events), makeRes(out), "openai", makeSession()); + + const tagLines = logs.filter((l) => l.includes("[tag-echo]")); + const stripLines = tagLines.filter((l) => l.includes("stripped model-emitted render tag")); + const drainLines = tagLines.filter((l) => l.includes("held head dropped during drain")); + assert.equal(stripLines.length, 1, `expected one strip line, got: ${tagLines.join(" | ")}`); + assert.equal(drainLines.length, 1, `expected one drain line, got: ${tagLines.join(" | ")}`); + assert.ok(drainLines[0]!.includes("2026-11-13 17:51:02"), "the drained head itself is named in the line"); + const text = out.join(""); + assert.ok(!text.includes("m00042"), "the completed tag never reaches the client"); + assert.ok(text.includes("前の文章。") && text.includes("中") && text.includes("後"), "prose around the tag survives"); + } finally { + setLogCapture(null); + } +}); diff --git a/tests/issue5950-phantom-read-forwarding.test.ts b/tests/issue5950-phantom-read-forwarding.test.ts new file mode 100644 index 0000000000..b62e03b773 --- /dev/null +++ b/tests/issue5950-phantom-read-forwarding.test.ts @@ -0,0 +1,142 @@ +// #5950: pins the faithful-forwarding contract for the incident shape — +// a server-side acp_status continuation followed by a model-emitted call. +// A mock upstream replays the turn: round 1 carries reasoning plus the +// acp_status call, round 2 either answers in text or emits the Read call +// for the nonexistent path from the report. Both directions: +// (a) the proxy never fabricates a tool call — a text-only round 2 +// reaches the client with no tool_call frame at all; +// (b) the proxy never suppresses or rewrites one — a model-emitted +// Read reaches the client byte-exact (same id, name, arguments). +// The round-2 request body is asserted in both cases: the continuation +// replays the acp_status call/result pair with matching ids and the +// round-1 reasoning intact. Related to #5950; this test documents and +// locks in the correct behavior, it does not fix the model-side +// hallucination behind the report. +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 { createOpenaiAdapter, runCompressLoop } from "../src/loop/index.ts"; +import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; + +const READ_PATH = "/Users/eee/.zcode/workspace/default/86f5c4b2cf708f31ec94f98b78e5f47c/test_user.py"; +const READ_ARGS = JSON.stringify({ path: READ_PATH }); + +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: "openai" as const, + }; +} + +function openaiSse(frames: Array>): string { + return frames.map((f) => `data: ${JSON.stringify(f)}\n\n`).join("") + "data: [DONE]\n\n"; +} + +// Round 1 of the incident turn: the model reasons, then calls acp_status +// (proxy-owned, executed server-side). The call carries a normal upstream id. +const ROUND1 = openaiSse([ + { id: "chatcmpl_1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, + { id: "chatcmpl_1", choices: [{ index: 0, delta: { reasoning_content: "present this concisely in Chinese" }, finish_reason: null }] }, + { id: "chatcmpl_1", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "call_5950_r1", type: "function", function: { name: "acp_status", arguments: "{}" } }] }, finish_reason: null }] }, + { id: "chatcmpl_1", choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] }, +]); + +// Round 2, variant A: the model answers in text only. +const ROUND2_TEXT = openaiSse([ + { id: "chatcmpl_2", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, + { id: "chatcmpl_2", choices: [{ index: 0, delta: { content: "status summarized" }, finish_reason: null }] }, + { id: "chatcmpl_2", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }, +]); + +// Round 2, variant B: the model itself emits the phantom Read for the +// nonexistent path from #5950. +const ROUND2_READ = openaiSse([ + { id: "chatcmpl_2", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, + { id: "chatcmpl_2", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "call_read_1", type: "function", function: { name: "Read", arguments: READ_ARGS } }] }, finish_reason: null }] }, + { id: "chatcmpl_2", choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] }, +]); + +async function drainOpenai(first: string, retries: string[], id: string): Promise<{ out: string; fetchCalls: number; bodies: string[] }> { + let fetchCalls = 0; + const bodies: string[] = []; + const orig = globalThis.fetch; + globalThis.fetch = (async (_url: unknown, init?: { body?: unknown }) => { + fetchCalls++; + if (init?.body !== undefined) bodies.push(typeof init.body === "string" ? init.body : String(init.body)); + const body = retries[fetchCalls - 1] ?? ""; + return new Response(body, { status: 200 }); + }) as typeof fetch; + const chunks: Buffer[] = []; + try { + const ctx = makeCtx(id); + for await (const chunk of runCompressLoop( + new Response(first, { status: 200 }).body!, + ctx, + { model: "deepseek-chat", stream: true }, + { url: "http://mock", headers: {} }, + createOpenaiAdapter({ model: "deepseek-chat", stream: true }), + buildCompressSystemPrompt(), + )) { + chunks.push(chunk); + } + } finally { + globalThis.fetch = orig; + } + return { out: Buffer.concat(chunks).toString("utf8"), fetchCalls, bodies }; +} + +// The continuation request the proxy sends for round 2: history plus the +// round-1 acp_status call/result pair (matching ids, upstream id kept +// byte-exact) and the round-1 reasoning replayed intact. +function assertContinuationBody(rawBody: string): void { + const body = JSON.parse(rawBody) as { messages: Array> }; + const asst = body.messages.find((m) => m.role === "assistant" && Array.isArray(m.tool_calls)); + assert.ok(asst, `round-2 body must replay the acp_status call, got: ${rawBody.slice(0, 500)}`); + const tc = (asst!.tool_calls as Array>)[0]!; + assert.equal((tc.function as Record).name, "acp_status"); + assert.equal(tc.id, "call_5950_r1", "the upstream call id is kept byte-exact in the continuation"); + const toolMsg = body.messages.find((m) => m.role === "tool"); + assert.ok(toolMsg, "round-2 body must carry the acp_status tool result"); + assert.equal(toolMsg!.tool_call_id, "call_5950_r1", "tool result pairs with the assistant call on the same id"); + assert.ok(rawBody.includes("present this concisely in Chinese"), "round-1 reasoning is replayed into the continuation"); +} + +test("#5950: text-only round 2 — the proxy fabricates no tool call", async () => { + const { out, fetchCalls, bodies } = await drainOpenai(ROUND1, [ROUND2_TEXT], "s5950-f1"); + assert.equal(fetchCalls, 1, "the proxy call continues server-side exactly once"); + assert.equal(bodies.length, 1, "one re-request body observed"); + assertContinuationBody(bodies[0]!); + assert.ok(out.includes("[ACP] acp_status result:"), "the acp_status marker reaches the client"); + assert.ok(out.includes("status summarized"), "the round-2 text reaches the client"); + assert.ok(!out.includes("tool_calls"), `no tool_call frame may appear when the model answered in text, got: ${out.slice(0, 500)}`); + assert.ok(!out.includes("test_user.py"), "no phantom path may appear when the model answered in text"); +}); + +test("#5950: model-emitted Read in round 2 — the proxy forwards it byte-exact", async () => { + const { out, fetchCalls, bodies } = await drainOpenai(ROUND1, [ROUND2_READ], "s5950-f2"); + assert.equal(fetchCalls, 1, "the proxy call continues server-side exactly once"); + assert.equal(bodies.length, 1, "one re-request body observed"); + assertContinuationBody(bodies[0]!); + assert.ok(out.includes("[ACP] acp_status result:"), "the acp_status marker reaches the client"); + assert.ok(out.includes(READ_PATH), "the model-emitted Read path reaches the client verbatim"); + assert.ok(out.includes("call_read_1"), "the model-emitted call id reaches the client verbatim"); + assert.ok(out.includes(JSON.stringify(READ_ARGS)), "the model-emitted call arguments reach the client verbatim (as the wire-escaped arguments string)"); + assert.equal((out.match(/test_user\.py/g) || []).length, 1, "the Read frame is forwarded once, never duplicated or rewritten"); +}); diff --git a/tests/issue6557-tool-typed-echo-span.test.ts b/tests/issue6557-tool-typed-echo-span.test.ts new file mode 100644 index 0000000000..03af5ec6cd --- /dev/null +++ b/tests/issue6557-tool-typed-echo-span.test.ts @@ -0,0 +1,131 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { stripAcpTags, createTagEchoFilter, stripOpenaiChatText } from "../src/loop/tag-echo-filter.ts"; + +// #6557: a model-emitted opening whose type names a TOOL lane frames a +// FABRICATED tool result. The #1720 rule ("prose wearing tags: the tags go, +// the content stays") was written for type="text" and is wrong here: keeping +// the body commits the invention into the assistant turn, and the model then +// imitates "tag + that body" at the head of every later reply. The span dies +// whole, in the whole-text and the streaming path alike. +const LT = "\x3c"; +const GT = "\x3e"; +const FABRICATED = + '{"actualContent": "acp_cache read-only: grand total in=2,874,000 cacheRead=2,753,682", "reason": "result compressed for budget", "tokens": 1000}'; +const toolOpen = (type = "tool:acp_cache") => `${LT}acp tokens="2312" type="${type}" time="2026-11-13T15:52:59Z"${GT}`; +const textOpen = `${LT}acp tokens="78" type="text" time="2026-11-13T15:52:47Z"${GT}`; +const close = `${LT}/acp${GT}`; + +function stream(chunks: string[]): string { + const f = createTagEchoFilter(); + let out = ""; + for (const c of chunks) out += f.push(c); + out += f.flush(); + return out; +} + +/** Every 2-way split of s, plus byte-at-a-time — the chunk boundary must not + * change the verdict (the #4178/#4210 split-invariance discipline). */ +function allChunkings(s: string): string[][] { + const out: string[][] = [[s]]; + for (let i = 1; i < s.length; i++) out.push([s.slice(0, i), s.slice(i)]); + out.push([...s]); + return out; +} + +test("#6557: whole-text drops a tool-typed echo span, prose around it survives", () => { + const text = `before ${toolOpen()}${FABRICATED}${close} after`; + assert.equal(stripAcpTags(text), "before after"); +}); + +test("#6557: whole-text drops every tool-typed span in the field", () => { + const one = `${toolOpen()}${FABRICATED}${close}`; + const two = `${toolOpen("tool:acp_status")}{"blocks": []}${close}`; + assert.equal(stripAcpTags(`a ${one} b ${two} c`), "a b c"); +}); + +test("#6557: an unclosed tool-typed echo owns the rest of the field", () => { + // The imitation was never terminated: everything after the opening is its + // payload (the wrapped-turn EOF rule, reused). + assert.equal(stripAcpTags(`${toolOpen()}${FABRICATED}`), ""); + assert.equal(stripAcpTags(`lead ${toolOpen()}${FABRICATED}`), "lead "); +}); + +test("#6557: the type attribute is matched through casing drift", () => { + assert.equal(stripAcpTags(`x ${toolOpen("TOOL:acp_cache")}${FABRICATED}${close} y`), "x y"); + const driftedAttr = `${LT}acp tokens="2312" Type="tool:acp_cache"${GT}${FABRICATED}${close}`; + assert.equal(stripAcpTags(`x ${driftedAttr} y`), "x y"); +}); + +test("#6557: streaming drops the span — and the verdict is split-invariant", () => { + const text = `before ${toolOpen()}${FABRICATED}${close} after`; + for (const chunks of allChunkings(text)) { + assert.equal(stream(chunks), "before after", `chunking ${JSON.stringify(chunks.map((c) => c.length))}`); + } +}); + +test("#6557: streaming drops an unclosed span at EOF", () => { + const text = `lead ${toolOpen()}${FABRICATED}`; + for (const chunks of allChunkings(text)) { + assert.equal(stream(chunks), "lead ", `chunking ${JSON.stringify(chunks.map((c) => c.length))}`); + } +}); + +test("#6557: the drop is reported to the caller (visible in the log callback)", () => { + const seen: string[] = []; + const f = createTagEchoFilter((snippet) => seen.push(snippet)); + f.push(`before ${toolOpen()}${FABRICATED}${close} after`); + f.flush(); + assert.equal(seen.length, 1, "one artifact, one warn"); + assert.ok(seen[0].includes('type="tool:acp_cache"')); +}); + +// ── recall guards: the tool-typed rule must not eat anything that is prose ── + +test("#6557 guard: a type=\"text\" echo keeps its prose body (#1720 unchanged)", () => { + const text = `see ${textOpen}the model's own sentence${close} end`; + assert.equal(stripAcpTags(text), "see the model's own sentence end"); +}); + +test("#6557 guard: a text echo whose body is a refs-only run still dies whole (#1720)", () => { + assert.equal(stripAcpTags(`a ${textOpen}m00120–m00300${close} b`), "a b"); +}); + +test("#6557 guard: markup in a tool-call argument stays byte-exact (#1039)", () => { + const args = JSON.stringify({ command: `echo '${toolOpen()}${FABRICATED}${close}'` }); + const payload = { choices: [{ message: { content: "", tool_calls: [{ function: { name: "bash", arguments: args } }] } }] }; + const out = stripOpenaiChatText(payload) as typeof payload; + assert.equal(out.choices[0].message.tool_calls[0].function.arguments, args); +}); + +test("#6557 guard: a tool-typed echo inside a fenced code block still goes (no fence exemption elsewhere in the filter)", () => { + // Documented behavior of the whole channel: the filter has no markdown + // awareness (see tag-echo.test.ts), so the assertion here only pins that + // the tool-typed branch did not invent one. + const text = "```\n" + `${toolOpen()}${FABRICATED}${close}` + "\n```"; + assert.equal(stripAcpTags(text), "```\n\n```"); +}); + +// ── self-close corner: `` is a complete unit, not an opening ── + +test("#6557: whole-text: a self-closing tool-typed echo dies in place, following prose survives", () => { + const selfClose = `${LT}acp type="tool:acp_cache"/${GT}`; + assert.equal(stripAcpTags(`x ${selfClose} rest of prose`), "x rest of prose"); +}); + +test("#6557: whole-text: a self-closing tool-typed echo does not pair with a later orphan close", () => { + const selfClose = `${LT}acp type="tool:acp_cache"/${GT}`; + assert.equal(stripAcpTags(`x ${selfClose} body ${close} tail`), "x body tail"); +}); + +test("#6557: streaming: a self-closing tool-typed echo dies in place — and the verdict is split-invariant", () => { + const text = `x ${LT}acp type="tool:acp_cache"/${GT} rest of prose`; + for (const chunks of allChunkings(text)) { + assert.equal(stream(chunks), "x rest of prose", `chunking ${JSON.stringify(chunks.map((c) => c.length))}`); + } +}); + +test("#6557 guard: a self-closing type=\"text\" echo still keeps its following prose (SELF_CLOSE semantics unchanged)", () => { + const selfClose = `${LT}acp type="text"/${GT}`; + assert.equal(stripAcpTags(`x ${selfClose} body ${close} tail`), "x body tail"); +}); diff --git a/tests/issue6636-tts-side-request.test.ts b/tests/issue6636-tts-side-request.test.ts new file mode 100644 index 0000000000..137a620951 --- /dev/null +++ b/tests/issue6636-tts-side-request.test.ts @@ -0,0 +1,161 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; +import { mkdirSync, mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _resetPluginStateForTest, resolveConversation } from "../src/plugin.ts"; +import { _resetSessionsForTest } from "../src/session.ts"; +import { _setForTest } from "../src/registry.ts"; +import { resetToolRingForTest } from "../src/tool-ring.ts"; +import { isAudioOutputCall, isSideRequest, resolveSideLane } from "../src/server/side-request.ts"; + +// #6636: dsh-fish-soul synthesizes speech through a TTS model exposed on +// /chat/completions (Xiaomi MiMo mimo-v2.5-tts). The plugin sends a tool-less +// {model, messages:[user, assistant], audio:{format, voice}, stream:false} +// body — a one-shot media side call, not a conversation. Pre-fix bili ran it +// through the full pipeline: the FUNCTION_ACP_TAGS_TIMESTAMPED system prompt +// was injected as messages[0] and every message got an prefix, and the +// upstream 400d with "messages[0] system role is not allowed for TTS model" +// (the same messages undecorated return 200 — control experiment in the +// issue). Post-fix the tool-less audio shape rides the #388 verbatim side +// passthrough: nothing injected, kernel state untouched. Delete the predicate +// in src/server/side-request.ts and this goes red. +process.env.NODE_ENV = "test"; +const testRoot = mkdtempSync(join(tmpdir(), "bili-tts-side-")); +test.after(() => rmSync(testRoot, { recursive: true, force: true })); +for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = testRoot; + +// The issue's exact TTS body (verbatim, including the Chinese text). +const TTS_BODY = { + model: "mimo-v2.5-tts", + messages: [ + { role: "user", content: "请用自然口语的方式朗读下面这句话" }, + { role: "assistant", content: "这条要念的话。" }, + ], + audio: { format: "mp3", voice: "mimo_default" }, + stream: false, +}; + +test("isAudioOutputCall: positive — the captured #6636 TTS shape", () => { + assert.equal(isAudioOutputCall(TTS_BODY), true, "issue shape (audio object, no tools)"); + assert.equal(isAudioOutputCall({ model: "m", messages: [{ role: "user", content: "hi" }], audio: { voice: "v" } }), true, "voice-only audio still counts"); + assert.equal(isAudioOutputCall({ model: "m", messages: [{ role: "user", content: "hi" }], audio: { format: "mp3" } }), true, "format-only audio still counts"); + assert.equal(isAudioOutputCall({ model: "m", messages: [{ role: "user", content: "hi" }], audio: { format: "mp3" }, tools: [] }), true, "an empty tools array is no action surface"); + assert.equal(isAudioOutputCall({ model: "m", messages: [{ role: "user", content: "hi" }], audio: { format: "mp3" }, stream: true }), true, "streaming TTS is still a media side call"); +}); + +test("isAudioOutputCall: negatives — every veto keeps the request on the main pipeline", () => { + assert.equal(isAudioOutputCall(null), false, "null body"); + assert.equal(isAudioOutputCall({}), false, "empty body"); + assert.equal(isAudioOutputCall({ model: "m", messages: [{ role: "user", content: "hi" }] }), false, "no audio field"); + assert.equal(isAudioOutputCall({ model: "m", messages: [{ role: "user", content: "hi" }], audio: {} }), false, "empty audio object carries no media request"); + assert.equal(isAudioOutputCall({ model: "m", messages: [{ role: "user", content: "hi" }], audio: "mp3" }), false, "audio must be an object"); + assert.equal(isAudioOutputCall({ model: "m", messages: [{ role: "user", content: "hi" }], audio: [{ format: "mp3" }] }), false, "audio must not be an array"); + assert.equal(isAudioOutputCall({ ...TTS_BODY, tools: [{ type: "function", function: { name: "bash", parameters: { type: "object", properties: {} } } }] }), false, "any tool entry vetoes (#546: an action surface marks a main turn)"); +}); + +test("isSideRequest: the audio shape is a side request; explicit intent still outranks the structure", () => { + assert.equal(isSideRequest(TTS_BODY), true, "tool-less audio request is a side request"); + assert.equal(isSideRequest(TTS_BODY, "main"), false, "explicit main intent vetoes the structure"); + assert.equal(isSideRequest(TTS_BODY, "title"), true, "declared side agent stays side"); + // The old rules are untouched for everything the structure does not claim. + assert.equal(isSideRequest({ max_tokens: 100 }), true, "tiny-budget heuristic intact"); + assert.equal(isSideRequest({ max_tokens: 1024, tools: [{ name: "compress" }] }), false, "#546 veto intact for client tools"); +}); + +test("resolveSideLane: the #6636 label names the structural shape in the log line", () => { + assert.equal( + resolveSideLane({ countTokens: false, responsesCompact: false, protocol: "openai", stripApplied: false, sideIntent: true, requestAgent: undefined, sideLabel: "audio-output utility call (#6636)" }).reason, + "audio-output utility call (#6636)", + ); +}); + +async function harness() { + const dir = mkdtempSync(join(testRoot, "run-")); + for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = dir; + mkdirSync(join(dir, "billion-context", "sessions"), { recursive: true }); + mkdirSync(join(dir, "sessions"), { recursive: true }); + _resetSessionsForTest(); + _resetPluginStateForTest(); + resetToolRingForTest(); + const store = new SessionStore({ dir: dir + "/sessions", enabled: false, debounceMs: 60000 }); + _setStoreForTest(store); + _setForTest({}); + const forwarded: Record[] = []; + const upstream = await new Promise<{ url: string; close: () => Promise }>((resolve) => { + const server = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + forwarded.push(JSON.parse(Buffer.concat(chunks).toString("utf8")) as Record); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "chat_test", object: "chat.completion", choices: [{ index: 0, message: { role: "assistant", content: "answer" }, finish_reason: "stop" }], usage: { prompt_tokens: 10000, completion_tokens: 10, total_tokens: 10010 } })); + }); + }); + server.listen(0, "127.0.0.1", () => resolve({ url: `http://127.0.0.1:${(server.address() as { port: number }).port}`, close: async () => { server.close(); } })); + }); + const proxy = await startServer({ port: 0, host: "127.0.0.1", upstream: upstream.url, routes: { [upstream.url]: { models: { "fake-a": { context: 400000 } } } }, modelContextLimit: 400000, kernelConfig: defaultConfig(400000), compress: { injectTool: true, injectNudge: true, preserveRecentMessages: 1, preserveRecentTokens: 0, minCompressRangeChars: 100 }, 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 origin = `http://127.0.0.1:${(proxy.address() as { port: number }).port}`; + // dsh shape: x-bili-plugin + conversation headers always; the persona agent + // header is stamped on MAIN turns only — the TTS side call arrives without + // it, like the title sidecar. + const dshSend = async (conversationId: string, body: Record, agent?: string) => { + const headers: Record = { "content-type": "application/json", "x-acp-session": conversationId, "x-bili-plugin": "dsh", "x-bili-plugin-conversation": conversationId }; + if (agent !== undefined) headers["x-bili-plugin-agent"] = agent; + const response = await fetch(`${origin}/bili/${upstream.url}/v1/chat/completions`, { method: "POST", headers, body: JSON.stringify(body) }); + assert.equal(response.status, 200, await response.text()); + }; + const sessionIds = async () => { + const body = (await (await fetch(origin + "/__bili/sessions")).json()) as { sessions?: { id: string }[] }; + assert.ok(Array.isArray(body.sessions), `/__bili/sessions returned no list: ${JSON.stringify(body)}`); + return body.sessions.map((s) => s.id); + }; + const subSessions = async (prefix: string) => (await sessionIds()).filter((id) => id.startsWith(prefix + "|sub:")); + return { forwarded, dshSend, subSessions, origin, close: async () => { store.cancelAll(); proxy.close(); upstream.close(); await new Promise((r2) => setTimeout(r2, 50)); } }; +} + +test("e2e: the #6636 TTS call rides the #388 passthrough verbatim — no system prompt, no tags, no kernel touch", async () => { + const h = await harness(); + try { + // 1. TTS-first on a fresh session: the side call must not claim the + // raw-key anchor with anything (no |sub: ghost, no kernel request). + await h.dshSend("conv-tts-first", TTS_BODY); + assert.deepEqual(h.forwarded.at(-1), TTS_BODY, "TTS call forwarded byte-identical (no system injected, no tags, audio intact)"); + assert.deepEqual(await h.subSessions("conv-tts-first"), [], "the TTS call mints no |sub: ghost"); + assert.equal(resolveConversation("conv-tts-first").session?.stats.requests ?? 0, 0, "the TTS call runs no kernel request"); + + // 2. Control: a real main turn on a second session still gets the ACP + // decoration (leading system prompt + per-message tags). + const history = [ + { role: "user", content: "set up the workspace" }, + { role: "assistant", content: "done" }, + ]; + await h.dshSend("conv-tts-after-main", { model: "fake-a", max_completion_tokens: 8192, stream: false, messages: [{ role: "system", content: "MAIN OPERATING SYSTEM" }, ...history, { role: "user", content: "now configure ci" }], tools: [{ type: "function", function: { name: "bash", description: "run a command", parameters: { type: "object", properties: {} } } }] }, "main"); + const mainFwd = h.forwarded.at(-1) as { messages: { role: string; content: string }[] }; + const mainSys = mainFwd.messages.filter((m) => m.role === "system").map((m) => m.content).join("\n"); + assert.ok(mainSys.includes("ACP TAGS"), "control: the main turn still gets the ACP system prompt"); + assert.ok(mainFwd.messages.some((m) => typeof m.content === "string" && m.content.includes(" tags"); + const mainSession = resolveConversation("conv-tts-after-main").session!; + assert.ok(mainSession, "raw-key session exists"); + assert.equal(mainSession.stats.requests, 1, "the main turn runs the full pipeline exactly once"); + const requestsAfterMain = mainSession.stats.requests; + const inputAfterMain = mainSession.stats.lastInputTokens; + + // 3. The TTS call on the SAME session, after anchoring: still verbatim, + // kernel state and usage baseline untouched. + await h.dshSend("conv-tts-after-main", TTS_BODY); + assert.deepEqual(h.forwarded.at(-1), TTS_BODY, "post-anchor TTS call still forwarded byte-identical"); + assert.equal(mainSession.stats.requests, requestsAfterMain, "the TTS call must not count as a main request"); + assert.equal(mainSession.stats.lastInputTokens, inputAfterMain, "the TTS call must not touch the usage baseline"); + assert.deepEqual(await h.subSessions("conv-tts-after-main"), [], "still no |sub: ghost after anchoring"); + } finally { + await h.close(); + } +}); diff --git a/tests/issue6712-compress-receipt-stale-refs.test.ts b/tests/issue6712-compress-receipt-stale-refs.test.ts new file mode 100644 index 0000000000..1873fad526 --- /dev/null +++ b/tests/issue6712-compress-receipt-stale-refs.test.ts @@ -0,0 +1,83 @@ +import assert from "node:assert/strict"; +import { randomUUID } from "node:crypto"; +import { test } from "node:test"; +import type { Config, CoreMessage } from "acp-kernel"; +import { createCore, createInitialState, assignRefs, emptyRefMap, defaultConfig } from "acp-kernel"; +import type { Session } from "../src/session.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { applyRanges, type RewriteCtx } from "../src/stream.ts"; +import { parseCompressInput } from "../src/compress-tool.ts"; + +_setStoreForTest(new SessionStore({ enabled: false })); + +const SUMMARY = "summary ".repeat(20); + +function textMsg(id: string, role: "user" | "assistant", text: string): CoreMessage { + return { id, role, contentType: "text", text }; +} + +function makeSession(): Session { + return { + id: `issue6712-${randomUUID()}`, + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 15000, compressCreditTokens: 0, contextTokens: 0, retrieveCalls: 0, retrieveHits: 0, retrieveMisses: 0, storedBytes: 0, storeBytesSaved: 0, rangeRestores: 0 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + pendingRetrievals: [], + }; +} + +function makeCtx(messages: CoreMessage[], config?: Config, logs?: string[]): RewriteCtx & { session: Session; config: Config } { + const session = makeSession(); + const res = assignRefs(messages, { existing: emptyRefMap(), nextIndex: 0 }); + session.state.messageRefs = res.map; + return { core: createCore(), config: config ?? defaultConfig(200000), messages, session, log: logs ? (m) => logs.push(m) : () => {} }; +} + +function compressArgs(startId: string, endId: string) { + return JSON.parse(JSON.stringify({ content: [{ startId, endId, summary: SUMMARY }] })); +} + +function compressArgsMulti(specs: Array<[string, string]>) { + return JSON.parse(JSON.stringify({ content: specs.map(([startId, endId]) => ({ startId, endId, summary: SUMMARY })) })); +} + +// Two sub-gate small messages + six big ones: eight refs, frontier m00008. +function fixtureMessages(): CoreMessage[] { + const msgs: CoreMessage[] = [ + textMsg("raw_1", "assistant", "a".repeat(800)), + textMsg("raw_2", "assistant", "b".repeat(800)), + ]; + for (let i = 3; i <= 8; i++) msgs.push(textMsg(`raw_${i}`, "assistant", "x".repeat(5000))); + return msgs; +} + +test("#6712: all-refs-above-frontier receipt keeps the requested ref numbers", () => { + const ctx = makeCtx(fixtureMessages()); + const out = applyRanges(parseCompressInput(compressArgs("m00010", "m00014")), ctx).text; + assert.ok(out.startsWith("[Compression FAILED:"), `unknown refs fail (got: ${out.slice(0, 120)})`); + assert.match(out, /above this session's highest ref \(m00008\)/, "frontier diagnosis present"); + // The #2146 invariant still holds: no bare phantom ref token reaches the + // client-persisted receipt, so the echo channel stays dead. + assert.doesNotMatch(out, /m00010|m00014/, "phantom endpoints are NOT echoed back as bare refs"); + // ...but the caller can still tell WHICH refs it used: the scrub marker + // carries the ref number, so the range line stays diagnosable. + assert.match(out, /\[stale-ref:00010\]/, "start ref number preserved in the scrub marker"); + assert.match(out, /\[stale-ref:00014\]/, "end ref number preserved in the scrub marker"); + assert.match(out, /range \[stale-ref:00010\]..\[stale-ref:00014\]/, "range diagnostic line names the requested numbers"); +}); + +test("#6712: every range in a multi-range request keeps its numbers", () => { + const ctx = makeCtx(fixtureMessages()); + const out = applyRanges(parseCompressInput(compressArgsMulti([["m00010", "m00011"], ["m00012", "m00014"]])), ctx).text; + assert.ok(out.startsWith("[Compression FAILED:"), `unknown refs fail (got: ${out.slice(0, 120)})`); + assert.doesNotMatch(out, /m00010|m00011|m00012|m00014/, "no bare phantom ref token anywhere"); + for (const n of ["00010", "00011", "00012", "00014"]) { + assert.match(out, new RegExp(`\\[stale-ref:${n}\\]`), `ref number ${n} preserved`); + } +}); diff --git a/tests/issue6728-policy-gate-ledger.test.ts b/tests/issue6728-policy-gate-ledger.test.ts new file mode 100644 index 0000000000..6a1b48be0c --- /dev/null +++ b/tests/issue6728-policy-gate-ledger.test.ts @@ -0,0 +1,369 @@ +// #6728: policy-gate REFUSED receipts (compress.requireNudge / +// requireRecommendation) charge no failure ledger — a model re-submitting a +// range bili will never fold gets byte-identical refusals forever and the +// circuit breaker never arms (9 consecutive verbatim refusals in 125 s in the +// reported pi session). The two gate branches in applyRanges return before +// any ledger call, unlike every other reject path in src/stream.ts. +// Gate discipline (#3617): BOTH directions asserted per gate. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { CoreMessage, CompressibleRange, NudgeDecision } from "acp-kernel"; +import { + createCore, + createInitialState, + assignRefs, + emptyRefMap, + defaultConfig, +} from "acp-kernel"; +import { parseCompressInput } from "../src/compress-tool.ts"; +import { + applyRanges, + compressBreakerArmed, + type RewriteCtx, +} from "../src/stream.ts"; +import { storeUnpromptedGate } from "../src/unprompted-gate.ts"; + +const SUM = "SUMMARY-PAYLOAD-LONG-ENOUGH-FOR-THE-KERNEL-MIN-LENGTH-CHECK-XYZ"; + +type Ctx = Omit & { + log: (m: string) => void; + logs: string[]; +}; + +function makeCtx( + messages: CoreMessage[], + overrides?: Record, +): Ctx { + const logs: string[] = []; + return { + core: createCore(), + config: defaultConfig(200000, overrides as never), + messages, + session: { + id: "issue6728-test", + meta: {}, + stats: { + requests: 0, + tokensSaved: 0, + inputTokens: 0, + cachedTokens: 0, + outputTokens: 0, + cacheSamples: 0, + lastInputTokens: 100000, + compressCreditTokens: 0, + contextTokens: 0, + retrieveCalls: 0, + retrieveHits: 0, + retrieveMisses: 0, + storedBytes: 0, + storeBytesSaved: 0, + rangeRestores: 0, + }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + pendingRetrievals: [], + }, + log: (m: string) => { + logs.push(m); + }, + logs, + }; +} + +function withRefs(ctx: Ctx): Ctx { + const res = assignRefs(ctx.messages, { + existing: emptyRefMap(), + nextIndex: 0, + }); + ctx.session.state.messageRefs = res.map; + return ctx; +} + +function textMsg( + id: string, + role: "user" | "assistant", + text: string, +): CoreMessage { + return { id, role, contentType: "text", text }; +} + +/** Nine 5000-char messages; preserveRecentMessages 2 keeps m8-m9 protected, + * so m1-m7 are the live fold space. */ +function freshCtx(): Ctx { + const msgs = Array.from({ length: 9 }, (_, i) => + textMsg( + `raw_${i + 1}`, + i % 2 === 0 ? "user" : "assistant", + "x".repeat(5000), + ), + ); + return withRefs( + makeCtx(msgs, { preserveRecentMessages: 2, preserveRecentTokens: 0 }), + ); +} + +function offerRange( + startRef: string, + endRef: string, + count: number, + tokens = 8000, +): CompressibleRange { + return { + startRef, + endRef, + count, + tokens, + chars: tokens * 4, + toolPct: 0, + textPct: 100, + }; +} + +function setOffer(ctx: Ctx, ranges: CompressibleRange[]): void { + ctx.lastNudge = { + compressibleRanges: ranges, + tier: null, + } as unknown as NudgeDecision; +} + +function runApply( + ctx: Ctx, + args: unknown, + opts?: { loopTracking?: boolean }, +): string { + return applyRanges(parseCompressInput(args), ctx, opts).text; +} + +function nudgeGateCtx(): Ctx { + const ctx = freshCtx(); + storeUnpromptedGate(ctx.session, { + nudgeInjected: false, + requireNudge: true, + requireRecommendation: false, + }); + return ctx; +} + +function scopeGateCtx(): Ctx { + const ctx = freshCtx(); + setOffer(ctx, [offerRange("m00001", "m00004", 4)]); + storeUnpromptedGate(ctx.session, { + nudgeInjected: true, + requireNudge: false, + requireRecommendation: true, + }); + return ctx; +} + +const NUDGE_CALL = { + content: [{ startId: "m00001", endId: "m00004", summary: SUM }], +}; +const SCOPE_CALL = { + content: [{ startId: "m00005", endId: "m00007", summary: SUM }], +}; + +// --------------------------------------------------------------------------- +// Item 1: ledger charging — the breaker must arm on gate refusals. +// --------------------------------------------------------------------------- + +test("#6728: requireNudge refusals charge the ledger — breaker arms by the 3rd identical refusal", () => { + const ctx = nudgeGateCtx(); + const first = runApply(ctx, NUDGE_CALL); + assert.ok( + first.startsWith("[Compression REFUSED"), + `expected refusal, got: ${first.slice(0, 120)}`, + ); + assert.ok( + !compressBreakerArmed(ctx.session), + "breaker must not arm on the first refusal", + ); + runApply(ctx, NUDGE_CALL); + const third = runApply(ctx, NUDGE_CALL); + assert.ok( + compressBreakerArmed(ctx.session), + "breaker must be armed after 3 consecutive gate refusals", + ); + assert.ok( + third.includes("COMPRESS CIRCUIT BREAKER"), + "3rd receipt must carry the breaker paragraph:\n" + third, + ); + assert.ok( + third.includes("STOP calling compress"), + "breaker paragraph must order a stop:\n" + third, + ); +}); + +test("#6728: requireRecommendation refusals charge the ledger — breaker arms by the 3rd identical refusal", () => { + const ctx = scopeGateCtx(); + const first = runApply(ctx, SCOPE_CALL); + assert.ok( + first.startsWith("[Compression REFUSED"), + `expected refusal, got: ${first.slice(0, 120)}`, + ); + assert.ok( + !compressBreakerArmed(ctx.session), + "breaker must not arm on the first refusal", + ); + runApply(ctx, SCOPE_CALL); + const third = runApply(ctx, SCOPE_CALL); + assert.ok( + compressBreakerArmed(ctx.session), + "breaker must be armed after 3 consecutive gate refusals", + ); + assert.ok( + third.includes("COMPRESS CIRCUIT BREAKER"), + "3rd receipt must carry the breaker paragraph:\n" + third, + ); +}); + +test("#6728: one success after arming disarms the breaker (gate refusals behave like other failures)", () => { + const ctx = nudgeGateCtx(); + runApply(ctx, NUDGE_CALL); + runApply(ctx, NUDGE_CALL); + runApply(ctx, NUDGE_CALL); + assert.ok(compressBreakerArmed(ctx.session), "setup: breaker must be armed"); + storeUnpromptedGate(ctx.session, { + nudgeInjected: true, + requireNudge: true, + requireRecommendation: false, + }); + const out = runApply(ctx, NUDGE_CALL); + assert.ok( + out.startsWith("[Compressed"), + `credit live again — fold must succeed, got: ${out.slice(0, 120)}`, + ); + assert.ok( + !compressBreakerArmed(ctx.session), + "one success must clear the breaker", + ); +}); + +// --------------------------------------------------------------------------- +// Item 3: REFUSED repeat-folding — repeats collapse to a one-liner. +// --------------------------------------------------------------------------- + +test("#6728: repeated requireNudge refusals collapse to a one-liner from the 2nd identical call", () => { + const ctx = nudgeGateCtx(); + const first = runApply(ctx, NUDGE_CALL); + const second = runApply(ctx, NUDGE_CALL); + assert.ok( + first.includes("you may only call compress while a nudge credit is live"), + "first refusal keeps the full teaching receipt:\n" + first, + ); + assert.ok( + second.includes("again"), + "repeat must be marked as a repeat:\n" + second, + ); + assert.ok( + !second.includes("you may only call compress while a nudge credit is live"), + "repeat must not re-broadcast the full lecture:\n" + second, + ); + assert.ok( + second.length < first.length, + "repeat one-liner must be shorter than the full receipt", + ); +}); + +test("#6728: repeated requireRecommendation refusals collapse to a one-liner from the 2nd identical call", () => { + const ctx = scopeGateCtx(); + const first = runApply(ctx, SCOPE_CALL); + const second = runApply(ctx, SCOPE_CALL); + assert.ok( + first.includes("Call acp_status for the current compressible ranges"), + "first refusal keeps the full advice:\n" + first, + ); + assert.ok( + second.includes("again"), + "repeat must be marked as a repeat:\n" + second, + ); + assert.ok( + !second.includes("Call acp_status for the current compressible ranges"), + "repeat must not re-broadcast the full advice:\n" + second, + ); + assert.ok( + second.length < first.length, + "repeat one-liner must be shorter than the full receipt", + ); +}); + +// --------------------------------------------------------------------------- +// Item 2: terminal wording when the recommendation set is empty. +// --------------------------------------------------------------------------- + +test("#6728: scope gate with an EMPTY recommendation set uses terminal wording, not a dead-end acp_status pointer", () => { + const ctx = freshCtx(); + ctx.session.state.deadRefs = [ + "m00001", + "m00002", + "m00003", + "m00004", + "m00005", + "m00006", + "m00007", + ]; + storeUnpromptedGate(ctx.session, { + nudgeInjected: false, + requireNudge: false, + requireRecommendation: true, + }); + const out = runApply(ctx, NUDGE_CALL); + assert.ok( + out.startsWith("[Compression REFUSED"), + `expected refusal, got: ${out.slice(0, 120)}`, + ); + assert.ok( + out.includes("requireRecommendation"), + "receipt still names the knob:\n" + out, + ); + assert.ok( + !out.includes("Call acp_status for the current compressible ranges"), + "empty set must not point at acp_status:\n" + out, + ); + assert.ok( + out.includes("already at its minimum"), + "empty set must say the context is already at its minimum:\n" + out, + ); + assert.ok( + out.includes("continue the task without compressing"), + "empty set must order a return to the task:\n" + out, + ); +}); + +test("#6728: scope gate with a NON-empty recommendation set keeps the acp_status advice (negative direction)", () => { + const ctx = scopeGateCtx(); + const out = runApply(ctx, SCOPE_CALL); + assert.ok( + out.startsWith("[Compression REFUSED"), + `expected refusal, got: ${out.slice(0, 120)}`, + ); + assert.ok( + out.includes("Call acp_status for the current compressible ranges"), + "non-empty set must keep the acp_status advice:\n" + out, + ); + assert.ok( + !out.includes("already at its minimum"), + "non-empty set must NOT claim the minimum:\n" + out, + ); +}); + +// --------------------------------------------------------------------------- +// Negative direction: the preflight lane never charges the ledger. +// --------------------------------------------------------------------------- + +test("#6728: preflight lane (loopTracking:false) never charges the ledger on gate-adjacent calls", () => { + const ctx = nudgeGateCtx(); + const out = runApply(ctx, NUDGE_CALL, { loopTracking: false }); + assert.ok( + out.startsWith("[Compressed"), + `preflight self-save must stay available, got: ${out.slice(0, 120)}`, + ); + assert.ok( + !compressBreakerArmed(ctx.session), + "preflight success must not arm the breaker", + ); +}); diff --git a/tests/launcher.test.ts b/tests/launcher.test.ts index 1bb8f56d2f..f3705816a0 100644 --- a/tests/launcher.test.ts +++ b/tests/launcher.test.ts @@ -5857,6 +5857,54 @@ test("runLaunch codex: old binary without --no-daemon launches unchanged (#1867) } }); +test("runLaunch claude: the injected --mcp-config does not swallow a positional prompt", async () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-claude-prompt-")); + const prevHome = process.env.HOME; + const prevClientBin = process.env.BILI_CLIENT_BIN; + const prevPlugin = process.env.BILI_LAUNCHER_PLUGIN; + process.env.HOME = home; + delete process.env.BILI_LAUNCHER_PLUGIN; + const fakeClaude = path.join(home, process.platform === "win32" ? "fake-claude.exe" : "fake-claude"); + fs.writeFileSync(fakeClaude, ""); + process.env.BILI_CLIENT_BIN = fakeClaude; + const clientArgs: string[][] = []; + const spawnImpl: SpawnFn = (cmd, args) => { + if (cmd === fakeClaude) { + clientArgs.push([...(args ?? [])]); + const child = makeFakeChild(0); + const orig = child.on!.bind(child); + (child as { on: SpawnChild["on"] }).on = (event, listener) => { + orig(event, listener); + if (event === "exit") setTimeout(() => listener(0, null), 0); + return child; + }; + return child; + } + return makeFakeChild(42422); + }; + const prevExit = process.exit; + process.exit = (() => undefined) as typeof process.exit; + try { + await runLaunch( + { client: "claude", clientArgs: ["say hi", "-p"], overrides: {} }, + { fetchImpl: async () => ({ ok: true }), fetchHealthInfo: async () => ({ ok: true, pid: lastFakeChildPid }), spawnImpl, sleep: () => Promise.resolve() }, + ); + assert.equal(clientArgs.length, 1); + const args = clientArgs[0]!; + // Claude Code's --mcp-config is variadic: a bare flag takes every following word as a config file. + assert.equal(args.includes("--mcp-config"), false, `bare --mcp-config would eat the prompt: ${JSON.stringify(args)}`); + assert.ok(args.some((a) => a.startsWith("--mcp-config=")), "bili's MCP config is still passed"); + assert.ok(args.includes("say hi")); + } finally { + process.exit = prevExit; + process.env.HOME = prevHome; + if (prevClientBin === undefined) delete process.env.BILI_CLIENT_BIN; + else process.env.BILI_CLIENT_BIN = prevClientBin; + if (prevPlugin !== undefined) process.env.BILI_LAUNCHER_PLUGIN = prevPlugin; + rmrf(home); + } +}); + test("runLaunch claude: CLAUDE_CODE_AUTO_COMPACT_WINDOW injected (built-in table window)", async () => { const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-claude-budget-")); const prevHome = process.env.HOME; diff --git a/tests/notice-registry.test.ts b/tests/notice-registry.test.ts index 8d77fda961..978d89f39c 100644 --- a/tests/notice-registry.test.ts +++ b/tests/notice-registry.test.ts @@ -17,21 +17,19 @@ const cfgPath = path.join(fs.mkdtempSync(path.join(os.tmpdir(), "notice-reg-cfg- process.env.DSH_HOME = fs.mkdtempSync(path.join(os.tmpdir(), "notice-reg-dsh-")); process.env.XDG_STATE_HOME = stateDir; process.env.BILI_CONFIG_FILE = cfgPath; -// The CI baseline preload (tests/preload-storage-baseline.mjs) pins -// BILI_STORAGE_UNIFIED=legacy for the legacy-default corpus. This suite -// asserts the PRODUCTION default (absent spelling ⇒ pure-sqlite ⇒ the -// sqlite-default notice is active), so both storage-selection knobs must be -// unpinned explicitly — relying on the ambient default passes locally (no -// preload) but reds CI (the #4011 lesson written into that baseline file). -delete process.env.BILI_STORAGE_UNIFIED; -delete process.env.BILI_STORAGE_JSON_WRITER; - -// This suite asserts the PRODUCTION default storage posture. The CI corpus -// baseline (tests/preload-storage-baseline.mjs, #2815) forces -// BILI_STORAGE_UNIFIED=legacy for every test file, which flips the -// pure-sqlite-default notice (#6480) off — pin our own selection per the -// baseline's documented rule (the #4011 lesson). -delete process.env.BILI_STORAGE_UNIFIED; +// #6547: the global baseline pins BILI_STORAGE_UNIFIED=legacy, but this suite +// asserts on the NEW default posture (the sqlite-default notice is active +// only when no storage mode is pinned) — clear the inherited storage knobs, +// same discipline as tests/sqlite-default-notice.test.ts. +for (const k of [ + "BILI_STORAGE_UNIFIED", + "BILI_STORAGE_JSON_WRITER", + "BILI_STORAGE_BACKEND", + "BILI_ENCRYPTION_KEY", + "BILI_PERSIST", +]) { + delete process.env[k]; +} const { NOTICES, visibleNotices, SQLITE_DEFAULT_NOTICE_ID } = await import("../src/notices.ts"); const { IOT_INCOMPAT_NOTICE_ID, PCK_NOTICE_ID, WM_FASTPATH_INTRO_ID, dismissUpgradeNotice } = await import("../src/pck-notice.ts"); diff --git a/tests/persist-metadata-fold-gate.test.ts b/tests/persist-metadata-fold-gate.test.ts index 788d4cedaf..3389d7f470 100644 --- a/tests/persist-metadata-fold-gate.test.ts +++ b/tests/persist-metadata-fold-gate.test.ts @@ -48,10 +48,18 @@ function makeSession(id: string): Session { const foldGate = (store: SessionStore): { dropResidentMetadata(session: Session): void } => store as unknown as { dropResidentMetadata(session: Session): void }; +// #6547: full mode opens index.db (+shm/wal) in dir; Windows refuses to +// delete open files, so close every store a test created before rmrf. +function closeAll(stores: SessionStore[]): void { + for (const s of stores) s.closeQuietly(); +} + test("metadata fold is refused while live metadata diverges from the last committed bytes (#5506)", async () => { const dir = mkdtempSync(join(tmpdir(), "bili-fpgate-neg-")); + const stores: SessionStore[] = []; try { const store = new SessionStore({ dir, debounceMs: 60000, enabled: true }); + stores.push(store); const s = makeSession("fpgate-negative"); s.metadata.anonymousPrefixAffinity = { depth: 22, tailHash: "test-tail", via: "prefix" }; await store.writeNow(s); @@ -64,14 +72,17 @@ test("metadata fold is refused while live metadata diverges from the last commit assert.equal((s.metadata as Record).lateStamp, "unsaved", "the unsaved mutation must stay resident"); store.closeQuietly(); // dual-write pin opens index.db — close before teardown (Windows cannot unlink an open SQLite file; see persist-zstd.test.ts for the family) } finally { + closeAll(stores); rmrf(dir); } }); test("metadata fold proceeds once the divergent mutation has been committed (#5506 positive direction)", async () => { const dir = mkdtempSync(join(tmpdir(), "bili-fpgate-pos-")); + const stores: SessionStore[] = []; try { const store = new SessionStore({ dir, debounceMs: 60000, enabled: true }); + stores.push(store); const s = makeSession("fpgate-positive"); s.metadata.anonymousPrefixAffinity = { depth: 22, tailHash: "test-tail", via: "prefix" }; await store.writeNow(s); @@ -82,6 +93,7 @@ test("metadata fold proceeds once the divergent mutation has been committed (#55 await store.writeNow(s); assert.equal(isDroppedMetadata(s.metadata), true, "fold must proceed once live == committed"); const reloaded = new SessionStore({ dir, debounceMs: 60000, enabled: true }); + stores.push(reloaded); const back = reloaded.loadSync("fpgate-positive"); assert.ok(back, "session reloaded"); assert.equal((back?.metadata as Record).lateStamp, "unsaved", "committed mutation survives the round trip"); @@ -89,14 +101,17 @@ test("metadata fold proceeds once the divergent mutation has been committed (#55 store.closeQuietly(); reloaded.closeQuietly(); } finally { + closeAll(stores); rmrf(dir); } }); test("metadata fold stays closed for a session with no committed save yet (#5506 conservative default)", async () => { const dir = mkdtempSync(join(tmpdir(), "bili-fpgate-cold-")); + const stores: SessionStore[] = []; try { const store = new SessionStore({ dir, debounceMs: 60000, enabled: true }); + stores.push(store); const s = makeSession("fpgate-cold"); (s.metadata as Record).onlyInMemory = true; foldGate(store).dropResidentMetadata(s); @@ -104,6 +119,7 @@ test("metadata fold stays closed for a session with no committed save yet (#5506 assert.equal((s.metadata as Record).onlyInMemory, true); store.closeQuietly(); } finally { + closeAll(stores); rmrf(dir); } }); diff --git a/tests/persist-zstd.test.ts b/tests/persist-zstd.test.ts index 887a0d4e49..64e3c8b6a0 100644 --- a/tests/persist-zstd.test.ts +++ b/tests/persist-zstd.test.ts @@ -59,6 +59,10 @@ async function withTempDir(name: string, fn: (h: Harness) => Promise): Pro try { await fn(h); } finally { + // #6547: full mode opens index.db (+shm/wal) in dir; Windows + // refuses to delete open files, so close every store this + // harness created before rmrf. + for (const s of h.stores) s.closeQuietly(); delete process.env.BILI_ENCRYPTION_KEY; delete process.env.BILI_PERSIST_ZSTD; _setZstdAvailableForTest(null); diff --git a/tests/plugin-passthrough-progress-frame-google.test.ts b/tests/plugin-passthrough-progress-frame-google.test.ts new file mode 100644 index 0000000000..c6984c9d9d --- /dev/null +++ b/tests/plugin-passthrough-progress-frame-google.test.ts @@ -0,0 +1,229 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { pipePluginChatWithStrip } from "../src/plugin.ts"; +import type { Session } from "../src/session.ts"; + +// #5848 shipped the inert-frame guard on the chat (OpenAI) lane only. The +// Google lane still drained the tag-echo filter on ANY text-less frame, so the +// same split-tag leak survived there: a render tag split at the frame boundary +// lost its held head and the remainder (` type="text" …>mNNNNN`) no longer +// started with `<`, so it reached the host as prose — the #5192 shape. Google +// frames that carry nothing but empty parts must not drain; a frame with a +// finishReason / functionCall / functionResponse / usageMetadata still must. + +function makeSession(): Session { + return { + id: "testsess", + protocol: "google", + upstreamOrigin: "http://127.0.0.1:9/v1", + label: "test", + createdAt: 0, + lastUsedAt: 0, + requests: 0, + lastInputTokens: 0, + stats: {}, + dirty: false, + } as unknown as Session; +} + +function makeRes(chunks: string[]) { + return { + writes: chunks, + write(b: Buffer | string) { + chunks.push(typeof b === "string" ? b : b.toString("utf8")); + return true; + }, + end(b?: Buffer | string) { + if (b !== undefined) + chunks.push(typeof b === "string" ? b : b.toString("utf8")); + }, + once() {}, + destroyed: false, + writableEnded: false, + } as unknown as import("node:http").ServerResponse; +} + +function streamOf(events: string[]): ReadableStream { + const enc = new TextEncoder(); + let i = 0; + return new ReadableStream({ + pull(controller) { + if (i < events.length) { + controller.enqueue(enc.encode(events[i])); + i += 1; + } else { + controller.close(); + } + }, + }); +} + +const DONE = "data: [DONE]\n\n"; + +function gframe( + parts: Array>, + finishReason?: string, +): string { + const candidate: Record = { + index: 0, + content: { role: "model", parts }, + }; + if (finishReason) candidate["finishReason"] = finishReason; + return `data: ${JSON.stringify({ modelVersion: "gemini-test", candidates: [candidate] })}\n\n`; +} + +/** A progress frame in Gemini's own dialect: no usable text parts at all. */ +function gprogress(): string { + return `data: ${JSON.stringify({ modelVersion: "gemini-test", candidates: [{ index: 0, content: { role: "model", parts: [{ text: "" }] } }] })}\n\n`; +} + +function gTexts(f: Record): string[] { + const out: string[] = []; + const c = f["candidates"]; + if (!Array.isArray(c)) return out; + for (const cand of c) { + if (!cand || typeof cand !== "object") continue; + const content = (cand as Record)["content"]; + if (!content || typeof content !== "object") continue; + const parts = (content as Record)["parts"]; + if (!Array.isArray(parts)) continue; + for (const p of parts) { + if ( + p && + typeof p === "object" && + typeof (p as Record)["text"] === "string" + ) { + out.push((p as Record)["text"] as string); + } + } + } + return out; +} + +/** Concatenate every text part the client would render. */ +function visibleText(text: string): string { + let out = ""; + for (const block of text.split("\n\n")) { + const payload = block + .split("\n") + .filter((l) => l.startsWith("data:")) + .map((l) => l.slice(5).trim()) + .join(""); + if (!payload || payload === "[DONE]") continue; + let obj: Record; + try { + obj = JSON.parse(payload) as Record; + } catch { + continue; + } + for (const t of gTexts(obj)) out += t; + } + return out; +} + +async function visible( + contents: string[], + inertAfter: (i: number) => boolean, +): Promise { + const events: string[] = [gframe([{ text: "" }])]; + contents.forEach((c, i) => { + events.push(gframe([{ text: c }])); + if (inertAfter(i)) events.push(gprogress()); + }); + events.push(gframe([], "STOP"), DONE); + const chunks: string[] = []; + await pipePluginChatWithStrip( + streamOf(events), + makeRes(chunks), + "google", + makeSession(), + ); + return visibleText(chunks.join("")); +} + +const HEAD = 'm00039'; + +test("google: a text-less progress frame inside a paired render tag does not leak the tag's tail", async () => { + const out = await visible( + ["前の文章。", HEAD, REST + "", "後の文章"], + (i) => i === 1, + ); + assert.equal(out, "前の文章。後の文章"); + assert.doesNotMatch(out, /type="text"|m00039/); +}); + +test("google: a text-less progress frame inside an unclosed render tag yields the same text as without it", async () => { + const contents = [HEAD, REST + "\n", "子作成完了"]; + const without = await visible(contents, () => false); + const withProgress = await visible(contents, (i) => i === 0); + assert.equal(withProgress, without); + assert.doesNotMatch(withProgress, /type="text"|m00039/); +}); + +test("google: a text-less progress frame at every chunk boundary leaves the visible text unchanged", async () => { + const contents = [ + ..."前の文章。", + ...`m00039`, + ..."後の文章", + ]; + const without = await visible(contents, () => false); + const withProgress = await visible(contents, () => true); + assert.equal(without, "前の文章。後の文章"); + assert.equal(withProgress, without); +}); + +test("google: a frame carrying usageMetadata still drains the held tail (guard is not a blanket hold)", async () => { + const out: string[] = []; + const usage = `data: ${JSON.stringify({ + modelVersion: "gemini-test", + candidates: [{ index: 0, content: { role: "model", parts: [] } }], + usageMetadata: { + promptTokenCount: 10, + candidatesTokenCount: 3, + totalTokenCount: 13, + }, + })}\n\n`; + await pipePluginChatWithStrip( + streamOf([ + gframe([{ text: "hello world" }], "STOP"), + DONE, + ]), + makeRes(out), + "google", + makeSession(), + ); + const text = visibleText(out.join("")); + // The held ` world" }], "STOP"), + DONE, + ]), + makeRes(control), + "google", + makeSession(), + ); + assert.equal(text, visibleText(control.join(""))); +}); + +test("google: ordinary streams with no held tail are unchanged by the guard", async () => { + const out: string[] = []; + await pipePluginChatWithStrip( + streamOf([ + gframe([{ text: "plain " }]), + gprogress(), + gframe([{ text: "answer" }], "STOP"), + DONE, + ]), + makeRes(out), + "google", + makeSession(), + ); + assert.equal(visibleText(out.join("")), "plain answer"); +}); diff --git a/tests/rejected-nudge-rearm.test.ts b/tests/rejected-nudge-rearm.test.ts new file mode 100644 index 0000000000..297ad92a95 --- /dev/null +++ b/tests/rejected-nudge-rearm.test.ts @@ -0,0 +1,592 @@ +/** + * A nudge the upstream rejects never reached the model, yet the kernel stamped it as + * shown when it decided to inject (lastNudgeShownTokens / lastShownByTier). The growth + * reference then sits at the rejected request's meter, so the cadence holds every later + * nudge until the context grows a full growth floor past the rejected request, although + * the model has seen no nudge. Seen in a Claude Code session on v0.1.213 (2026-11-11): + * the session's first nudge drew `400 messages.N: role 'system' must precede an + * 'assistant' message` (#5432), the next request was smaller than the rejected one and + * the decision read `growth=-21002 ... (ref=178592) blocked: T1 (cadence)`. + * + * Scenario (token model = ceil(chars/4), matching the mock's reported input_tokens): + * grow turn by turn until bili injects its nudge (request k) + * k+1 next request smaller than the rejected one → carries NO nudge (held until a success) + * k+2 next turn small growth → nudge present again (without the fix, cadence-blocked) + * Control session (same sizes, nudge accepted): k+2 carries no nudge — a delivered nudge + * still holds the cadence (negative direction). + */ +import assert from "node:assert/strict"; +import fs from "node:fs"; +import http from "node:http"; +import os from "node:os"; +import path from "node:path"; +import { once } from "node:events"; +import { test } from "node:test"; +import type { ProxyOptions } from "../src/server.ts"; + +const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), "bc-rejected-nudge-")); +process.env.XDG_STATE_HOME = path.join(tmpRoot, "state"); +process.env.XDG_CACHE_HOME = path.join(tmpRoot, "cache"); +process.env.XDG_DATA_HOME = path.join(tmpRoot, "data"); +process.env.XDG_CONFIG_HOME = path.join(tmpRoot, "config"); +delete process.env.ACP_DUMP_BODY; + +const LIMIT = 200_000; +const MODEL = "claude-test"; + +const filler = (seed: number, chars: number): string => { + const words = ["context", "compression", "session", "anchor", "baseline", "growth", "interval", "meter"]; + let out = ""; + let i = 0; + while (out.length < chars) { + out += `${words[(i + seed) % words.length]}-${seed}-${i} `; + i += 1; + } + return out.slice(0, chars); +}; + +const SYSTEM = "You are a helpful coding assistant. Keep answers concise.\n" + filler(999, 800); +const TOOLS = [ + { name: "Bash", description: "Run a shell command.", input_schema: { type: "object", properties: { command: { type: "string" } }, required: ["command"] } }, + { name: "Read", description: "Read a file.", input_schema: { type: "object", properties: { path: { type: "string" } }, required: ["path"] } }, +]; + +type Msg = { role: "user" | "assistant"; content: Array<{ type: "text"; text: string }> }; +const um = (n: number, len: number): Msg => ({ role: "user", content: [{ type: "text", text: `Task ${n}:\n${filler(n, len)}` }] }); +const am = (n: number, len: number): Msg => ({ role: "assistant", content: [{ type: "text", text: `Step ${n} done.\n${filler(n + 20, len)}` }] }); + +const estTok = (s: string) => Math.ceil(s.length / 4); +const msgChars = (m: { content: unknown }): number => { + const c = m.content; + if (typeof c === "string") return c.length; + if (Array.isArray(c)) return c.reduce((n: number, p) => n + (typeof (p as { text?: unknown })?.text === "string" ? ((p as { text: string }).text).length : 0), 0); + return 0; +}; + +const NUDGE_MARK = "efficiency nudge"; + +test("a nudge the upstream rejected is re-armed after the next success; a delivered one is not", { timeout: 120_000 }, async () => { + const { defaultConfig } = await import("acp-kernel"); + const { startServer } = await import("../src/server.ts"); + const { SessionStore, _setStoreForTest } = await import("../src/persist.ts"); + const { _setForTest: setRegistryForTest } = await import("../src/registry.ts"); + const { rmrf } = await import("./tmp-rm.ts"); + const { setLogCapture } = await import("../src/logger.ts"); + const logLines: string[] = []; + setLogCapture((_level: string, msg: string) => { logLines.push(msg); }); + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + + let proxy: http.Server | undefined; + let upstream: http.Server | undefined; + const closeServer = (s: http.Server | undefined): Promise => s ? new Promise((resolve, reject) => { + s.closeAllConnections?.(); + s.close((e) => (e ? reject(e) : resolve())); + }) : Promise.resolve(); + + try { + const bodies: Record = {}; + const rejectFirstNudge = new Set(); + // After the first nudge-carrying request of a session is caught, the + // next `extraRejects[sid]` requests of that session are rejected too + // (regardless of content) — for the consecutive-4xx scenario. + const extraRejects: Record = {}; + const rejectArmed = new Set(); + const rejectRemaining: Record = {}; + const nudgeAt: Record = {}; + upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const sid = String(req.headers["x-claude-code-session-id"] ?? ""); + const body = Buffer.concat(chunks).toString("utf8"); + (bodies[sid] ??= []).push(body); + if (rejectFirstNudge.has(sid) && body.includes(NUDGE_MARK) && !rejectArmed.has(sid)) { + rejectArmed.add(sid); + rejectRemaining[sid] = 1 + (extraRejects[sid] ?? 0); + } + if ((rejectRemaining[sid] ?? 0) > 0) { + rejectRemaining[sid]!--; + res.writeHead(400, { "content-type": "application/json", "request-id": "req_test" }); + res.end(JSON.stringify({ type: "error", error: { type: "invalid_request_error", message: "messages.5: role 'system' must precede an 'assistant' message or end the array" } })); + return; + } + const parsed = JSON.parse(body) as { messages?: Array<{ content: unknown }>; tools?: unknown }; + const inputTokens = estTok(SYSTEM) + estTok(JSON.stringify(parsed.tools ?? [])) + (parsed.messages ?? []).reduce((n, m) => n + Math.ceil(msgChars(m) / 4), 0); + const sse = (ev: string, data: object) => res.write(`event: ${ev}\ndata: ${JSON.stringify(data)}\n\n`); + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + sse("message_start", { type: "message_start", message: { id: `msg-${bodies[sid]!.length}`, role: "assistant", model: MODEL, content: [], usage: { input_tokens: inputTokens, output_tokens: 0 } } }); + sse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }); + sse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } }); + sse("content_block_stop", { type: "content_block_stop", index: 0 }); + sse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 1 } }); + sse("message_stop", { type: "message_stop" }); + res.end(); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + const baseKernel = defaultConfig(LIMIT, { preserveRecentMessages: 3, preserveRecentTokens: 800 }); + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { [MODEL]: { context: LIMIT } } } } as ProxyOptions["routes"], + modelContextLimit: LIMIT, + kernelConfig: { ...baseKernel, nudge: { ...baseKernel.nudge, growthFloor: 4000, growthCap: 4000, minGrowthFloor: 100, minGrowthRatio: 0.1, tierGrowthTokens: { t1: 600 } } }, + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: true, + debug: false, + passthrough: false, + autoUpdate: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + mitm: { enabled: false, domains: [] }, + }; + proxy = await startServer(opts); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/messages`; + + const runSession = async (sid: string): Promise<{ statuses: number[]; nudged: boolean[] }> => { + const headers = { "content-type": "application/json", "user-agent": "claude-cli/2.1.322 (external, cli)", "x-claude-code-session-id": sid }; + const statuses: number[] = []; + const post = async (msgs: Msg[]): Promise => { + const res = await fetch(url, { method: "POST", headers, body: JSON.stringify({ model: MODEL, max_tokens: 4096, stream: true, system: SYSTEM, messages: msgs, tools: TOOLS }) }); + statuses.push(res.status); + await res.text(); + }; + // Grow one turn at a time until bili injects its nudge (request k). + let H: Msg[] = [um(1, 800)]; + let Hprev: Msg[] = H; + let k = -1; + for (let n = 1; n <= 8 && k < 0; n++) { + await post(H); + if ((bodies[sid] ?? []).at(-1)?.includes(NUDGE_MARK)) k = n - 1; + else { Hprev = H; H = [...H, am(n, 6000), um(n + 1, 700)]; } + } + assert.ok(k >= 0, `the nudge fires within 8 turns: ${JSON.stringify({ statuses, n: (bodies[sid] ?? []).map((b) => b.includes(NUDGE_MARK)), keys: Object.keys(bodies) })}`); + // The client's next request is smaller than the rejected one, then it takes a short turn. + const G: Msg[] = [...Hprev, um(50, 700)]; + await post(G); + await post([...G, am(99, 2400), um(100, 700)]); + nudgeAt[sid] = k; + return { statuses, nudged: (bodies[sid] ?? []).map((b) => b.includes(NUDGE_MARK)) }; + }; + + rejectFirstNudge.add("11111111-1111-4111-8111-111111111111"); + const rejected = await runSession("11111111-1111-4111-8111-111111111111"); + const control = await runSession("22222222-2222-4222-8222-222222222222"); + + const kr = nudgeAt["11111111-1111-4111-8111-111111111111"]!; + const kc = nudgeAt["22222222-2222-4222-8222-222222222222"]!; + assert.equal(rejected.statuses[kr], 400, "the upstream rejects the nudged request"); + assert.deepEqual(rejected.statuses.slice(kr + 1), [200, 200]); + assert.equal(rejected.nudged[kr + 1], false, "the request right after the 400 carries no nudge — the nudge may be what drew it"); + assert.equal(rejected.nudged[kr + 2], true, "after a request succeeds, the undelivered nudge is re-armed"); + assert.ok(control.statuses.every((st) => st === 200)); + assert.equal(control.nudged[kc + 2], false, "control: a delivered nudge still holds the cadence on the next turn"); + } catch (err) { + process.stderr.write(logLines.filter((l) => /nudge|upstream 4|error|502|fail/i.test(l)).join("\n") + "\n"); + throw err; + } finally { + setLogCapture(null); + await closeServer(proxy); + await closeServer(upstream); + try { rmrf(tmpRoot); } catch { /* ignore */ } + } +}); + +test("consecutive 4xxs after a rejected nudge neither re-restore the stamps nor clear the hold", { timeout: 120_000 }, async () => { + const { defaultConfig } = await import("acp-kernel"); + const { startServer } = await import("../src/server.ts"); + const { SessionStore, _setStoreForTest } = await import("../src/persist.ts"); + const { _setForTest: setRegistryForTest } = await import("../src/registry.ts"); + const { setLogCapture } = await import("../src/logger.ts"); + const logLines: string[] = []; + setLogCapture((_level: string, msg: string) => { logLines.push(msg); }); + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + + let proxy: http.Server | undefined; + let upstream: http.Server | undefined; + const closeServer = (s: http.Server | undefined): Promise => s ? new Promise((resolve, reject) => { + s.closeAllConnections?.(); + s.close((e) => (e ? reject(e) : resolve())); + }) : Promise.resolve(); + + const SID = "33333333-3333-4333-8333-333333333333"; + try { + const bodies: string[] = []; + let rejectArmed = false; // set the moment a nudge-carrying request arrives + let rejectRemaining = 0; // budget: the nudge request itself + the very next one + upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const body = Buffer.concat(chunks).toString("utf8"); + bodies.push(body); + if (!rejectArmed && body.includes(NUDGE_MARK)) { + rejectArmed = true; + rejectRemaining = 2; // reject the nudge request AND the next one + } + if (rejectRemaining > 0) { + rejectRemaining--; + res.writeHead(400, { "content-type": "application/json", "request-id": "req_test" }); + res.end(JSON.stringify({ type: "error", error: { type: "invalid_request_error", message: "messages.5: role 'system' must precede an 'assistant' message or end the array" } })); + return; + } + const parsed = JSON.parse(body) as { messages?: Array<{ content: unknown }>; tools?: unknown }; + const inputTokens = estTok(SYSTEM) + estTok(JSON.stringify(parsed.tools ?? [])) + (parsed.messages ?? []).reduce((n, m) => n + Math.ceil(msgChars(m) / 4), 0); + const sse = (ev: string, data: object) => res.write(`event: ${ev}\ndata: ${JSON.stringify(data)}\n\n`); + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + sse("message_start", { type: "message_start", message: { id: `msg-${bodies.length}`, role: "assistant", model: MODEL, content: [], usage: { input_tokens: inputTokens, output_tokens: 0 } } }); + sse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }); + sse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } }); + sse("content_block_stop", { type: "content_block_stop", index: 0 }); + sse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 1 } }); + sse("message_stop", { type: "message_stop" }); + res.end(); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + const baseKernel = defaultConfig(LIMIT, { preserveRecentMessages: 3, preserveRecentTokens: 800 }); + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { [MODEL]: { context: LIMIT } } } } as ProxyOptions["routes"], + modelContextLimit: LIMIT, + kernelConfig: { ...baseKernel, nudge: { ...baseKernel.nudge, growthFloor: 4000, growthCap: 4000, minGrowthFloor: 100, minGrowthRatio: 0.1, tierGrowthTokens: { t1: 600 } } }, + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: true, + debug: false, + passthrough: false, + autoUpdate: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + mitm: { enabled: false, domains: [] }, + }; + proxy = await startServer(opts); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/messages`; + const headers = { "content-type": "application/json", "user-agent": "claude-cli/2.1.322 (external, cli)", "x-claude-code-session-id": SID }; + const statuses: number[] = []; + const post = async (msgs: Msg[]): Promise => { + const res = await fetch(url, { method: "POST", headers, body: JSON.stringify({ model: MODEL, max_tokens: 4096, stream: true, system: SYSTEM, messages: msgs, tools: TOOLS }) }); + statuses.push(res.status); + await res.text(); + }; + + // Grow until the nudge fires (request k); it and the next request both 400. + let H: Msg[] = [um(1, 800)]; + let Hprev: Msg[] = H; + let k = -1; + for (let n = 1; n <= 8 && k < 0; n++) { + await post(H); + if (bodies.at(-1)?.includes(NUDGE_MARK)) k = n - 1; + else { Hprev = H; H = [...H, am(n, 6000), um(n + 1, 700)]; } + } + assert.ok(k >= 0, `the nudge fires within 8 turns: ${JSON.stringify(statuses)}`); + assert.equal(statuses[k], 400, "the nudged request is rejected"); + const G: Msg[] = [...Hprev, um(50, 700)]; + await post(G); // k+1: rejected again, MUST be nudge-free + await post([...G, am(99, 2400), um(100, 700)]); // k+2: 200, hold still set at prepare time + await post([...G, am(99, 2400), um(100, 700), am(101, 2400), um(102, 700)]); // k+3: 200, re-armed + + assert.deepEqual(statuses.slice(k), [400, 400, 200, 200]); + assert.equal(bodies[k].includes(NUDGE_MARK), true, "the rejected request carried the nudge"); + assert.equal(bodies[k + 1].includes(NUDGE_MARK), false, "k+1 is nudge-free (hold) and its 4xx must not re-restore or clear anything"); + assert.equal(bodies[k + 2].includes(NUDGE_MARK), false, "k+2: the hold survives until a usage-settled success"); + assert.equal(bodies[k + 3].includes(NUDGE_MARK), true, "k+3: after the success settled usage, the undelivered nudge re-arms exactly once"); + } catch (err) { + process.stderr.write(logLines.filter((l) => /nudge|upstream 4|error|502|fail/i.test(l)).join("\n") + "\n"); + throw err; + } finally { + setLogCapture(null); + await closeServer(proxy); + await closeServer(upstream); + } +}); + +/** + * Lane-coverage mirror (#6531 extension): the nudge rides every protocol lane + * (anthropic/google/openai/responses), so the reject re-arm must not be + * anthropic-only. This drives the GOOGLE lane end to end: a rejected nudge + * request rolls the cadence back, the hold suppresses the next request, and a + * usage-settled success re-arms the nudge — the same three-step contract as + * the anthropic test above. + */ +type GPart = { text: string }; +type GMsg = { role: "user" | "model"; parts: GPart[] }; +const gu = (n: number, len: number): GMsg => ({ role: "user", parts: [{ text: `Task ${n}:\n${filler(n, len)}` }] }); +const gm = (n: number, len: number): GMsg => ({ role: "model", parts: [{ text: `Step ${n} done.\n${filler(n + 20, len)}` }] }); +const gChars = (m: { parts: unknown }): number => (m.parts as Array<{ text?: unknown }>).reduce((n, p) => n + (typeof p.text === "string" ? p.text.length : 0), 0); + +test("the google lane re-arms a rejected nudge the same way the anthropic lane does", { timeout: 120_000 }, async () => { + const { defaultConfig } = await import("acp-kernel"); + const { startServer } = await import("../src/server.ts"); + const { SessionStore, _setStoreForTest } = await import("../src/persist.ts"); + const { _setForTest: setRegistryForTest } = await import("../src/registry.ts"); + const { setLogCapture } = await import("../src/logger.ts"); + const logLines: string[] = []; + setLogCapture((_level: string, msg: string) => { logLines.push(msg); }); + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + + let proxy: http.Server | undefined; + let upstream: http.Server | undefined; + const closeServer = (s: http.Server | undefined): Promise => s ? new Promise((resolve, reject) => { + s.closeAllConnections?.(); + s.close((e) => (e ? reject(e) : resolve())); + }) : Promise.resolve(); + + const SID = "44444444-4444-4444-8444-444444444444"; + try { + const bodies: string[] = []; + let rejectArmed = false; + let rejectRemaining = 0; + upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const body = Buffer.concat(chunks).toString("utf8"); + bodies.push(body); + if (!rejectArmed && body.includes(NUDGE_MARK)) { + rejectArmed = true; + rejectRemaining = 1; // reject the nudge request itself + } + if (rejectRemaining > 0) { + rejectRemaining--; + res.writeHead(400, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: { code: 400, status: "INVALID_ARGUMENT", message: "contents: part 7: unexpected nudge fragment (synthetic upstream rejection)" } })); + return; + } + const parsed = JSON.parse(body) as { contents?: Array<{ parts?: Array<{ text?: string }> }> }; + const promptTokens = estTok(SYSTEM) + (parsed.contents ?? []).reduce((n, m) => n + Math.ceil(gChars(m as { parts: Array<{ text?: string }> }) / 4), 0); + const frame = { + candidates: [{ content: { role: "model", parts: [{ text: "ok" }] }, finishReason: "STOP" }], + usageMetadata: { promptTokenCount: promptTokens, candidatesTokenCount: 1, totalTokenCount: promptTokens + 1 }, + }; + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(`data: ${JSON.stringify(frame)}\n\n`); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + const baseKernel = defaultConfig(LIMIT, { preserveRecentMessages: 3, preserveRecentTokens: 800 }); + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { [MODEL]: { context: LIMIT } } } } as ProxyOptions["routes"], + modelContextLimit: LIMIT, + kernelConfig: { ...baseKernel, nudge: { ...baseKernel.nudge, growthFloor: 4000, growthCap: 4000, minGrowthFloor: 100, minGrowthRatio: 0.1, tierGrowthTokens: { t1: 600 } } }, + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: true, + debug: false, + passthrough: false, + autoUpdate: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + mitm: { enabled: false, domains: [] }, + }; + proxy = await startServer(opts); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1beta/models/${MODEL}:streamGenerateContent?alt=sse`; + const statuses: number[] = []; + const post = async (contents: GMsg[]): Promise => { + const res = await fetch(url, { method: "POST", headers: { "content-type": "application/json", "x-acp-session": SID }, body: JSON.stringify({ contents, systemInstruction: { parts: [{ text: SYSTEM }] }, generationConfig: { maxOutputTokens: 4096 } }) }); + statuses.push(res.status); + await res.text(); + }; + + let H: GMsg[] = [gu(1, 800)]; + let Hprev: GMsg[] = H; + let k = -1; + for (let n = 1; n <= 8 && k < 0; n++) { + await post(H); + if (bodies.at(-1)?.includes(NUDGE_MARK)) k = n - 1; + else { Hprev = H; H = [...H, gm(n, 6000), gu(n + 1, 700)]; } + } + assert.ok(k >= 0, `the nudge fires within 8 turns on the google lane: ${JSON.stringify(statuses)}`); + assert.equal(statuses[k], 400, "the upstream rejects the nudged request"); + const G: GMsg[] = [...Hprev, gu(50, 700)]; + await post(G); + await post([...G, gm(99, 2400), gu(100, 700)]); + assert.deepEqual(statuses.slice(k + 1), [200, 200]); + assert.equal(bodies[k + 1].includes(NUDGE_MARK), false, "k+1 carries no nudge (hold)"); + assert.equal(bodies[k + 2].includes(NUDGE_MARK), true, "k+2: the undelivered nudge re-arms after the usage-settled success"); + } catch (err) { + process.stderr.write(logLines.filter((l) => /nudge|upstream 4|error|502|fail/i.test(l)).join("\n") + "\n"); + throw err; + } finally { + setLogCapture(null); + await closeServer(proxy); + await closeServer(upstream); + } +}); + +/** + * Route-coverage mirror (#6531 extension): the reject hold must end on ANY + * success — including upstreams that never report usage (#1595 route class). + * settleUsageReport only clears the flag for s.total > 0 samples, so on this + * route the hold latched forever before the fix: one 4xx killed nudging for + * the rest of the process lifetime. This fake upstream emits a complete + * Anthropic stream with NO usage fields at all (input stays undefined, so the + * no-usage success site is the one that must release the hold). + */ +test("a rejected nudge re-arms after a no-usage success on the #1595 route", { timeout: 120_000 }, async () => { + const { defaultConfig } = await import("acp-kernel"); + const { startServer } = await import("../src/server.ts"); + const { SessionStore, _setStoreForTest } = await import("../src/persist.ts"); + const { _setForTest: setRegistryForTest } = await import("../src/registry.ts"); + const { setLogCapture } = await import("../src/logger.ts"); + const logLines: string[] = []; + setLogCapture((_level: string, msg: string) => { logLines.push(msg); }); + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + + let proxy: http.Server | undefined; + let upstream: http.Server | undefined; + const closeServer = (s: http.Server | undefined): Promise => s ? new Promise((resolve, reject) => { + s.closeAllConnections?.(); + s.close((e) => (e ? reject(e) : resolve())); + }) : Promise.resolve(); + + const SID = "44444444-4444-4444-8444-444444444444"; + try { + const bodies: string[] = []; + let rejectArmed = false; + let rejectRemaining = 0; + upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const body = Buffer.concat(chunks).toString("utf8"); + bodies.push(body); + if (!rejectArmed && body.includes(NUDGE_MARK)) { + rejectArmed = true; + rejectRemaining = 1; // reject only the nudge request itself + } + if (rejectRemaining > 0) { + rejectRemaining--; + res.writeHead(400, { "content-type": "application/json", "request-id": "req_test" }); + res.end(JSON.stringify({ type: "error", error: { type: "invalid_request_error", message: "messages.5: role 'system' must precede an 'assistant' message or end the array" } })); + return; + } + // Complete stream, NO usage fields anywhere — the #1595 route. + const sse = (ev: string, data: object) => res.write(`event: ${ev}\ndata: ${JSON.stringify(data)}\n\n`); + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + sse("message_start", { type: "message_start", message: { id: `msg-${bodies.length}`, role: "assistant", model: MODEL, content: [] } }); + sse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }); + sse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } }); + sse("content_block_stop", { type: "content_block_stop", index: 0 }); + sse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn" } }); + sse("message_stop", { type: "message_stop" }); + res.end(); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + const baseKernel = defaultConfig(LIMIT, { preserveRecentMessages: 3, preserveRecentTokens: 800 }); + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { [MODEL]: { context: LIMIT } } } } as ProxyOptions["routes"], + modelContextLimit: LIMIT, + kernelConfig: { ...baseKernel, nudge: { ...baseKernel.nudge, growthFloor: 4000, growthCap: 4000, minGrowthFloor: 100, minGrowthRatio: 0.1, tierGrowthTokens: { t1: 600 } } }, + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: true, + debug: false, + passthrough: false, + autoUpdate: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + mitm: { enabled: false, domains: [] }, + }; + proxy = await startServer(opts); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/messages`; + const headers = { "content-type": "application/json", "user-agent": "claude-cli/2.1.322 (external, cli)", "x-claude-code-session-id": SID }; + const statuses: number[] = []; + const post = async (msgs: Msg[]): Promise => { + const res = await fetch(url, { method: "POST", headers, body: JSON.stringify({ model: MODEL, max_tokens: 4096, stream: true, system: SYSTEM, messages: msgs, tools: TOOLS }) }); + statuses.push(res.status); + await res.text(); + }; + + // Grow until the nudge fires (request k) — on local estimates, since the + // upstream never reports usage — then it is rejected exactly once. + let H: Msg[] = [um(1, 800)]; + let Hprev: Msg[] = H; + let k = -1; + for (let n = 1; n <= 8 && k < 0; n++) { + await post(H); + if (bodies.at(-1)?.includes(NUDGE_MARK)) k = n - 1; + else { Hprev = H; H = [...H, am(n, 6000), um(n + 1, 700)]; } + } + assert.ok(k >= 0, `the nudge fires within 8 turns on the no-usage route: ${JSON.stringify(statuses)}`); + assert.equal(statuses[k], 400, "the nudged request is rejected"); + const G: Msg[] = [...Hprev, um(50, 700)]; + await post(G); // k+1: 200, no usage reported — the hold must end here + await post([...G, am(99, 2400), um(100, 700)]); // k+2: re-armed + + assert.deepEqual(statuses.slice(k), [400, 200, 200]); + assert.equal(bodies[k].includes(NUDGE_MARK), true, "the rejected request carried the nudge"); + assert.equal(bodies[k + 1].includes(NUDGE_MARK), false, "k+1 is nudge-free (hold)"); + assert.equal(bodies[k + 2].includes(NUDGE_MARK), true, "k+2: the no-usage success released the hold and the undelivered nudge re-arms — without the release it latched forever"); + } catch (err) { + process.stderr.write(logLines.filter((l) => /nudge|upstream 4|error|502|fail/i.test(l)).join("\n") + "\n"); + throw err; + } finally { + setLogCapture(null); + await closeServer(proxy); + await closeServer(upstream); + } +}); diff --git a/tests/side-lane-invariants.test.ts b/tests/side-lane-invariants.test.ts index 2f9501f7c2..1d23a43f70 100644 --- a/tests/side-lane-invariants.test.ts +++ b/tests/side-lane-invariants.test.ts @@ -349,17 +349,25 @@ test("(F) extracted protocol modules keep the side-lane boundary (#1440 P2 cut 2 // isDshSingleShotSidecar — same category once more: the generic single-shot // dsh sidecar shape predicate (no tools + one-shot body, any system text) // consulted to set sideIntent and to keep unknown sidecars off the raw-key -// anchor before the first main turn can claim it. +// anchor before the first main turn can claim it. #6548 adds +// isResponsesServerToolSingleShot — same category once more: the Responses +// server-tool-only single-shot shape predicate (string input + search tools, +// no history) consulted by the #286 refusal branch, which forwards that shape +// verbatim instead of hard-400ing a call that cannot carry an identity. // #4774 raw-key anchor race. #5073 adds // isStructuredOutputCall — same category once more: a pure toolless // structured-output shape predicate (no threshold, no session state) consulted // to set sideIntent and to keep the call out of the restoreOutputBudget -// high-water path, exactly like the #2500 server-tool predicate. +// high-water path, exactly like the #2500 server-tool predicate. #6636 adds +// isAudioOutputCall — same category once more: a pure toolless audio-output +// shape predicate (no threshold, no session state) consulted to set sideIntent +// and to keep the call out of the restoreOutputBudget high-water path, +// exactly like the #5073 structured-output predicate. test("(G) extracted pipeline module keeps the side-lane boundary (#1440 P2 cut 3)", () => { const text = readFileSync(fileURLToPath(new URL("../src/server/handle.ts", import.meta.url)), "utf8"); const laneInternals = /\bSIDE_REQUEST_MAX_TOKENS\b|\bSIDE_REQUEST_AGENTS\b|\bDemoteGateSignals\b|\bSideLaneSignals\b|\bSideLaneDecision\b|\bBILI_TOOL_NAMES\b|\bOutputBudgetField\b|\boutputBudgetField\b|\breadOutputBudget\b|\bwriteOutputBudget\b|\b_resetNoOutputCeilingWarningsForTest\b/; assert.doesNotMatch(text, laneInternals, "handle.ts must stay decoupled from the side-lane engine internals"); - const allowlist = new Set(["hasLeakedBiliToolsOnly", "hasSingleShotBudget", "isClaudeCodeTitleRequest", "isDshAutoReviewRequest", "isDshSingleShotSidecar", "isDshTitleRequest", "isServerToolUtilityCall", "isSideRequest", "isStructuredOutputCall", "resolveSideLane", "demoteGate", "stripLeakedBiliTools", "restoreOutputBudget", "sideRequestGuard"]); + const allowlist = new Set(["hasLeakedBiliToolsOnly", "hasSingleShotBudget", "isAudioOutputCall", "isClaudeCodeTitleRequest", "isDshAutoReviewRequest", "isDshSingleShotSidecar", "isDshTitleRequest", "isResponsesServerToolSingleShot", "isServerToolUtilityCall", "isSideRequest", "isStructuredOutputCall", "resolveSideLane", "demoteGate", "stripLeakedBiliTools", "restoreOutputBudget", "sideRequestGuard"]); let sawImport = false; for (const m of text.matchAll(/\{([^}]*)\}\s*from\s*["'][^"']*side-request\.js["']/g)) { sawImport = true; diff --git a/tests/web-diagnostics-unicode.test.ts b/tests/web-diagnostics-unicode.test.ts index 138a2e824d..12e651ed93 100644 --- a/tests/web-diagnostics-unicode.test.ts +++ b/tests/web-diagnostics-unicode.test.ts @@ -5,6 +5,7 @@ import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs"; import type http from "node:http"; import { tmpdir } from "node:os"; import path from "node:path"; +import { recordClientUa, _resetClientUaRegistryForTest } from "../src/client-ua-registry.js"; import { recordConflict } from "../src/conflict-watch.js"; import { closeLogger, configureLogger } from "../src/logger.js"; import { SessionStore, _setStoreForTest } from "../src/persist.js"; @@ -15,7 +16,7 @@ import { makeOpts } from "./corrupt-index-child.js"; const ENV_KEYS = ["BILI_CONFIG_FILE", "BILI_SESSIONS_DIR", "DSH_HOME", "XDG_STATE_HOME", "XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME"] as const; type Report = Awaited>; -type Source = "storage log" | "conflict log" | "config" | "conflict detail" | "topic"; +type Source = "storage log" | "conflict log" | "config" | "conflict detail" | "topic" | "user-agent"; type Boundary = "split pair" | "whole pair" | "ASCII"; interface Fixture { config: string; log: string } @@ -38,12 +39,14 @@ async function withFixture(fn: (fixture: Fixture) => Promise): Promise { await withFixture(async (fixture) => { diff --git a/tests/websearch-side-request.test.ts b/tests/websearch-side-request.test.ts index 82772cde17..c21b3a8bdb 100644 --- a/tests/websearch-side-request.test.ts +++ b/tests/websearch-side-request.test.ts @@ -4,7 +4,7 @@ import http from "node:http"; import { once } from "node:events"; import { defaultConfig, createInitialState } from "acp-kernel"; import { startServer } from "../src/server.ts"; -import { isServerToolUtilityCall, isSideRequest, resolveSideLane, SIDE_REQUEST_MAX_TOKENS } from "../src/server/side-request.ts"; +import { isResponsesServerToolSingleShot, isServerToolUtilityCall, isSideRequest, resolveSideLane, SIDE_REQUEST_MAX_TOKENS } from "../src/server/side-request.ts"; import type { ProxyOptions } from "../src/config.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; @@ -72,6 +72,49 @@ test("isServerToolUtilityCall: negatives — every veto keeps the request on the assert.equal(isServerToolUtilityCall(searchBody({ tools: [{ type: "web_search", name: "web_search" }] })), false, "unversioned web_search type never matches"); }); +// #6548: pi-web-access's xAI web-search backend sends a deliberately minimal +// single-shot Responses body — string input, web_search/x_search tools only, +// no message history — and pi-web-access exposes no knob for a session id, +// so the client cannot carry an identity. The proxy's #286 refusal hard-400s +// it before the side lane ever sees it. The Responses-wire predicate below is +// the shape the refusal branch consults. +function responsesSearchBody(over: Record = {}): Record { + return { + model: "grok-4.7", + input: "latest news on context compression", + tools: [{ type: "web_search" }, { type: "x_search" }], + ...over, + }; +} + +test("isResponsesServerToolSingleShot: positive — the captured pi-web-access shape (#6548)", () => { + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody()), true, "captured shape (web_search + x_search, string input)"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tools: [{ type: "web_search" }] })), true, "web_search alone"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tools: [{ type: "web_search_preview" }] })), true, "web_search_preview family"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tool_choice: "auto" })), true, "string auto"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tool_choice: "required" })), true, "string required forces one of the declared server tools"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tool_choice: { type: "web_search" } })), true, "object forcing a DECLARED server tool"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tool_choice: undefined })), true, "absent tool_choice"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ instructions: "Answer concisely." })), true, "instructions carry no history"); +}); + +test("isResponsesServerToolSingleShot: negatives — every veto keeps the request under the #286 refusal", () => { + assert.equal(isResponsesServerToolSingleShot(null), false, "null body"); + assert.equal(isResponsesServerToolSingleShot({}), false, "empty body"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ input: [{ type: "message", role: "user", content: [{ type: "input_text", text: "q" }] }] })), false, "array input is message history, not a single shot"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ input: undefined })), false, "missing input"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tools: [{ type: "function", name: "get_weather" }] })), false, "client function tool does not match"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tools: [{ type: "web_search" }, { type: "function", name: "get_weather" }] })), false, "mixing ANY client-side tool vetoes"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tools: [] })), false, "empty tools array"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tools: [null] })), false, "non-object tool entry"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tool_choice: "none" })), false, "string none vetoes"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tool_choice: { type: "function", name: "get_weather" } })), false, "forcing a client tool vetoes"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ tool_choice: { type: "required" } })), false, "unrecognized tool_choice shape vetoes"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ previous_response_id: "resp_abc" })), false, "chained continuation is a conversation turn, not a single shot"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ session_id: "sess-1" })), false, "body session_id is an identity signal"); + assert.equal(isResponsesServerToolSingleShot(responsesSearchBody({ prompt_cache_key: "pck-1" })), false, "prompt_cache_key is an identity signal"); +}); + test("isSideRequest: the structural shape outranks the #546 tools veto; explicit intent still outranks the structure", () => { const body = searchBody(); assert.equal(isSideRequest(body), true, "server-tool-only utility call is a side request despite non-empty tools and a large budget"); diff --git a/tests/wire-contract-fakes.ts b/tests/wire-contract-fakes.ts index 4de315438d..9b6797ee1b 100644 --- a/tests/wire-contract-fakes.ts +++ b/tests/wire-contract-fakes.ts @@ -222,6 +222,14 @@ export const WIRE_RULES: readonly WireRule[] = [ provenance: 'bili #5998 production report (DSH self-compression rewrote session messages and the rebuilt request carried signature-less thinking blocks; an opus→sonnet mid-session switch amplifies the failure because signatures are bound to the producing model). Mechanism verified in code: kernel anthropicToCore stamps thinkingSignature only when the inbound block carries one, while coreToAnthropic re-emits EVERY reasoning core as a thinking block — unsigned cores therefore produce signature-less blocks on the wire, which Anthropic proper rejects ("thinking.signature: Field required", #2501). The two affected upstream personalities are byte-indistinguishable on the inbound wire — never-signing relays (DeepSeek /anthropic, #5495) require the unsigned blocks back verbatim — so the repair is learn-on-failure at bili\'s relay egress seam (src/server/thinking-posture.ts): first "Field required" 400 ⇒ one strip-and-resend probe; accepted ⇒ the session drops unsigned thinking proactively for the rest of its life. Constraint source: Anthropic Messages API extended-thinking contract (preserved thinking must be replayed verbatim); the kernel round-trip suite pins the positive half ("signature reattached — Anthropic rejects thinking without it").', }, + { + id: "WC-025", + wire: "openai-chat", + summary: + 'a TTS-shaped chat-completions request (a top-level `audio` object — voice/format for audio output) must not carry a leading role:"system" message — TTS models behind OpenAI-compatible endpoints reject it with 400 ("messages[0] system role is not allowed for TTS model"); bili must never inject its ACP system prompt into such a request, and the per-message tags must never be stamped onto text that will be read aloud', + provenance: + 'bili #6636 production 400 (dsh-fish-soul TTS through bili 0.1.213, Xiaomi MiMo mimo-v2.5-tts over an OpenAI-compatible /chat/completions endpoint): the plugin sends {model, messages:[user, assistant], audio:{format:"mp3",voice:"mimo_default"}, stream:false} with no tools; bili injected the FUNCTION_ACP_TAGS_TIMESTAMPED system prompt as messages[0] plus an prefix on every message, and the upstream answered 400 "messages[0] system role is not allowed for TTS model". Control experiment in the issue: the same messages undecorated return 200 with audio. Repair: isAudioOutputCall (src/server/side-request.ts) routes tool-less audio requests through the #388 verbatim side passthrough, so neither the system prompt nor the tags are ever injected. The validator keys on the `audio` field, not the model name — the shape, not the spelling, is what makes the call a TTS request.', + }, ]; // --------------------------------------------------------------------------- @@ -491,10 +499,25 @@ function validateAnthropicBody(body: unknown): string[] { return out; } -/** WC-004 on an OpenAI chat/completions body (only function-typed entries). */ +/** WC-004 + WC-025 on an OpenAI chat/completions body (only function-typed entries for WC-004). */ export function validateOpenAiChatBody(body: unknown): string[] { const out: string[] = []; - if (!isPlainObject(body) || !Array.isArray(body.tools)) return out; + if (!isPlainObject(body)) return out; + // WC-025 (#6636): runs before the tools early-return — a TTS-shaped + // request (top-level `audio` object, no tools) must never carry a leading + // system message: TTS models behind OpenAI-compatible endpoints 400 it + // ("messages[0] system role is not allowed for TTS model"). The tripwire + // keys on the audio shape, not the model name. The no-tools veto mirrors + // isAudioOutputCall: a tool-carrying request is a main turn (its model + // has an action surface, is not a TTS model, and accepts a leading + // system), so bili's ACP decoration there is legal. + const wc025NoTools = !Array.isArray(body.tools) || body.tools.length === 0; + if (wc025NoTools + && isPlainObject(body.audio) && Object.keys(body.audio).length > 0 + && Array.isArray(body.messages) && body.messages.length > 0 + && isPlainObject(body.messages[0]) && body.messages[0].role === "system") + out.push(`WC-025 messages[0]: leading role:"system" message on a TTS-shaped request (top-level audio object) — TTS models reject a system message with 400 (#6636)`); + if (!Array.isArray(body.tools)) return out; body.tools.forEach((t, i) => { if (!isPlainObject(t)) return; const fn = t.function; diff --git a/tests/wire-contract.test.ts b/tests/wire-contract.test.ts index 89de540dd1..bda8c0071e 100644 --- a/tests/wire-contract.test.ts +++ b/tests/wire-contract.test.ts @@ -166,6 +166,66 @@ test("WC-024 thinking blocks must carry a signature (#5998)", () => { })[0], /WC-024/); }); +test("WC-025 TTS-shaped requests must not carry a leading system message (#6636)", () => { + // Illegal: the #6636 shape — audio output + a leading system message is + // what bili injected (ACP tags prompt) and what the TTS upstream 400s. + assert.match(VALIDATORS["openai-chat"]({ + model: "mimo-v2.5-tts", + messages: [ + { role: "system", content: "ACP TAGS\n\nAddressable messages…" }, + { role: "user", content: "read this aloud" }, + { role: "assistant", content: "the text to speak." }, + ], + audio: { format: "mp3", voice: "mimo_default" }, + stream: false, + })[0], /WC-025/); + // Legal: the same TTS call without the leading system (the issue's + // control experiment — returns 200). + assert.deepEqual(VALIDATORS["openai-chat"]({ + model: "mimo-v2.5-tts", + messages: [ + { role: "user", content: "read this aloud" }, + { role: "assistant", content: "the text to speak." }, + ], + audio: { format: "mp3", voice: "mimo_default" }, + stream: false, + }), []); + // Legal: a leading system message with no audio object is a plain chat + // turn, not a TTS request — the rule keys on the audio shape. + assert.deepEqual(VALIDATORS["openai-chat"]({ + model: "fake-a", + messages: [ + { role: "system", content: "ACP TAGS\n\nAddressable messages…" }, + { role: "user", content: "hello" }, + ], + stream: false, + }), []); + // Legal: an empty audio object carries no media request. + assert.deepEqual(VALIDATORS["openai-chat"]({ + model: "mimo-v2.5-tts", + messages: [ + { role: "system", content: "ACP TAGS" }, + { role: "user", content: "hello" }, + ], + audio: {}, + stream: false, + }), []); + // Legal: a tool-carrying audio request is a main turn, not a TTS call — + // its model has an action surface and accepts a leading system message, + // so bili's ACP decoration there is legal (no-tools veto, mirrors + // isAudioOutputCall). + assert.deepEqual(VALIDATORS["openai-chat"]({ + model: "voice-chat", + messages: [ + { role: "system", content: "ACP TAGS" }, + { role: "user", content: "hello" }, + ], + audio: { format: "wav", voice: "alloy" }, + tools: [{ type: "function", function: { name: "bash", parameters: { type: "object", properties: {} } } }], + stream: false, + }), []); +}); + test("WC-016 validates global-scope breakpoint ordering (render order: tools → system → messages)", () => { // #2648 shape: unmarked tools render ahead of a global system block. assert.match(VALIDATORS.anthropic({ @@ -274,6 +334,8 @@ test("wire-contract ledger: every rule has a live enforcement clause", () => { { model: "gemini-synthetic", tools: [{ type: "function", function: { name: "compress", parameters: { type: "object", properties: { content: { type: ["array", "string"] } } } } }] }, { tools: [{ type: "function", function: { name: "x".repeat(65), parameters: { type: "object", properties: {} } } }] }, { tools: [{ type: "function", function: { name: "ok", parameters: { anyOf: [] } } }] }, + // WC-025: the #6636 shape — TTS audio object + a leading system message. + { model: "mimo-v2.5-tts", messages: [{ role: "system", content: "ACP TAGS" }, { role: "user", content: "hi" }], audio: { format: "mp3", voice: "mimo_default" } }, ], responses: [ { type: "response.create", input: [], stream: true },