From 5b9ce15517771197f1db15eb6448e6b37f0e0941 Mon Sep 17 00:00:00 2001 From: resec Date: Wed, 18 Nov 2026 14:56:30 +0800 Subject: [PATCH 01/12] fix(dsh): a never-configured host is not a config error (#7126) loadDeclaredModelEndpoints folded three different situations into one console.error: no config file at all (the documented default for a host that never declared custom endpoints), a file that exists but cannot be read, and a file that exists but is not a usable declaration. The first one fires on every fresh install's very first boot and reads like a failure, so a new user's first impression is a broken setup. ENOENT is now silent; a present-but-unusable config keeps exactly one line, now naming the reason (read error code, or the JSON/parse complaint) so it points at the file. The never-throws and one-shot contracts are unchanged. Tests split accordingly: absent file asserts zero stderr; malformed JSON and structurally invalid declarations assert one line each, including the file name and the offending index. --- src/model-endpoints.ts | 44 +++++++++++++++++++++-------- tests/model-endpoints.test.ts | 52 +++++++++++++++++++++++++++++++---- 2 files changed, 79 insertions(+), 17 deletions(-) diff --git a/src/model-endpoints.ts b/src/model-endpoints.ts index f80a1d92af..f90bd7bf3c 100644 --- a/src/model-endpoints.ts +++ b/src/model-endpoints.ts @@ -107,22 +107,44 @@ export function matchModelEndpoint(patterns: readonly ModelEndpointPattern[] | n // Client-side (in-host process) loader. Unlike the proxy's loadOptions this // NEVER throws: a missing or unreadable config file is the normal state for // hosts that never configure custom endpoints, and a broken config must not -// take down the host process. Best-effort + one warning on failure. +// take down the host process. Best-effort + at most one warning, and only for +// a config that is actually PRESENT but unusable — see below. let loadWarned = false; +function warnOnce(message: string): void { + if (loadWarned) return; + loadWarned = true; + // console.error is deliberate here: this loader runs inside HOST + // processes (the agent-side fetch interceptor), where the proxy's + // loggerLog has no meaningful sink — stderr is the only channel the + // host user will ever see. Same policy as the agent-side modules. + console.error(message); +} export async function loadDeclaredModelEndpoints(): Promise { + const file = configFile(); + let text: string; + try { + text = await readFile(file, "utf8"); + } catch (err) { + // (#7126) No config file at all is the documented default for a host + // that never declared custom endpoints. Saying anything here made a + // fresh install read as a failure on its very first boot, so ENOENT is + // silent; anything else (EACCES, EISDIR, …) is a real condition and + // gets the one-shot line. + const code = (err as NodeJS.ErrnoException)?.code; + if (code !== "ENOENT") { + warnOnce(`bili-native: cannot read ${file} (${code ?? "unknown error"}); declared endpoints stay unclaimed until it becomes readable`); + } + return []; + } try { - const text = await readFile(configFile(), "utf8"); const json = JSON.parse(text.replace(/^\uFEFF/, "")) as Record; return parseModelEndpointPatterns(json.modelEndpointPatterns); - } catch { - if (!loadWarned) { - loadWarned = true; - // console.error is deliberate here: this loader runs inside HOST - // processes (the agent-side fetch interceptor), where the proxy's - // loggerLog has no meaningful sink — stderr is the only channel the - // host user will ever see. Same policy as the agent-side modules. - console.error(`bili-native: could not resolve modelEndpointPatterns from ${configFile()}; declared endpoints stay unclaimed until the host restarts with a readable config`); - } + } catch (err) { + // The file EXISTS but is not a usable declaration (malformed JSON, or a + // structurally invalid modelEndpointPatterns). That one is worth a line, + // with the reason, so the readout points at the file instead of looking + // like the plugin itself failed. + warnOnce(`bili-native: ${file} is not a usable billion-context config (${(err as Error)?.message ?? "invalid"}); declared endpoints stay unclaimed`); return []; } } diff --git a/tests/model-endpoints.test.ts b/tests/model-endpoints.test.ts index 2876fd136c..caa1a061a5 100644 --- a/tests/model-endpoints.test.ts +++ b/tests/model-endpoints.test.ts @@ -90,23 +90,63 @@ test("loadDeclaredModelEndpoints: reads the global config file", async () => { } }); -test("loadDeclaredModelEndpoints: missing file or malformed config degrades to empty (never throws)", async () => { +/** Collect console.error lines emitted while `fn` runs. */ +async function captureStderr(fn: () => Promise): Promise<{ result: T; lines: string[] }> { + const lines: string[] = []; + const original = console.error; + console.error = (...args: unknown[]) => { lines.push(args.map((a) => String(a)).join(" ")); }; + try { + const result = await fn(); + return { result, lines }; + } finally { + console.error = original; + } +} + +test("loadDeclaredModelEndpoints: a host with no config file is the default state and stays silent", async () => { const saved = process.env.BILI_CONFIG_FILE; _resetLoadWarningForTest(); try { process.env.BILI_CONFIG_FILE = path.join(os.tmpdir(), `bili-model-ep-absent-${process.pid}-${Date.now()}.json`); - assert.deepEqual(await loadDeclaredModelEndpoints(), []); - const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-model-ep-")); + const { result, lines } = await captureStderr(() => loadDeclaredModelEndpoints()); + assert.deepEqual(result, []); + assert.deepEqual(lines, [], "a never-configured host must not look broken on first boot"); + } finally { + if (saved === undefined) delete process.env.BILI_CONFIG_FILE; + else process.env.BILI_CONFIG_FILE = saved; + _resetLoadWarningForTest(); + } +}); + +test("loadDeclaredModelEndpoints: a present but unusable config warns once, naming the reason", async () => { + const saved = process.env.BILI_CONFIG_FILE; + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-model-ep-")); + _resetLoadWarningForTest(); + try { const bad = path.join(dir, "billion-context.json"); fs.writeFileSync(bad, "{ not json"); process.env.BILI_CONFIG_FILE = bad; - assert.deepEqual(await loadDeclaredModelEndpoints(), []); + const first = await captureStderr(() => loadDeclaredModelEndpoints()); + assert.deepEqual(first.result, []); + assert.equal(first.lines.length, 1, "a malformed config is worth exactly one line"); + assert.match(first.lines[0]!, /is not a usable billion-context config/); + assert.match(first.lines[0]!, /billion-context\.json/, "the line must point at the file"); + + // Still one-shot: a second load in the same process adds nothing. + const second = await captureStderr(() => loadDeclaredModelEndpoints()); + assert.deepEqual(second.result, []); + assert.deepEqual(second.lines, []); + const badEntry = path.join(dir, "bad-entry.json"); fs.writeFileSync(badEntry, JSON.stringify({ modelEndpointPatterns: [{ wire: "openai" }] })); process.env.BILI_CONFIG_FILE = badEntry; - assert.deepEqual(await loadDeclaredModelEndpoints(), []); - fs.rmSync(dir, { recursive: true, force: true }); + _resetLoadWarningForTest(); + const third = await captureStderr(() => loadDeclaredModelEndpoints()); + assert.deepEqual(third.result, []); + assert.equal(third.lines.length, 1, "structurally invalid declarations are the same class of problem"); + assert.match(third.lines[0]!, /modelEndpointPatterns\[0\]/); } finally { + fs.rmSync(dir, { recursive: true, force: true }); if (saved === undefined) delete process.env.BILI_CONFIG_FILE; else process.env.BILI_CONFIG_FILE = saved; _resetLoadWarningForTest(); From 2b1c95568e9b6bdb83ff3c40e9612aaca96f4028 Mon Sep 17 00:00:00 2001 From: resec Date: Wed, 18 Nov 2026 14:58:36 +0800 Subject: [PATCH 02/12] fix(dsh): read the patch layer's disabled value, not the literal `true` (#7128) parseDshPatchEntries is the single judgement for 'is this plugin turned off in cordis.patch.yml', and both consumers act on it: the cost advisory treats !disabled as 'enabled, warn about spend', and the host-plugin notice treats disabled as 'the user handled it, stay quiet'. The old scan matched only the literal `true`. dsh's patch layer is not limited to that spelling. In one stock web-profile dump, 52 `disabled:` lines carry only 29 literal trues; the other 23 are `!!js` expressions, plus YAML 1.1 forms and quoted booleans. Every one of those read as ENABLED, so a user who disabled a billed plugin kept seeing its cost hint, and a user who disabled a host plugin could not dismiss the notice. Read the value instead of the literal: YAML booleans (true/True/yes/on), quoted forms, and a trailing comment. A `!!js` expression cannot be evaluated from text, so it counts as 'turned off' -- the safe direction for both consumers (under-reporting a hint beats nagging someone who already acted). Block attribution and the truex-style non-boolean rejection are unchanged. Measured on the stock dump: 29/52 -> 52/52 recognised. Negative control: restoring the literal-true regex turns the new case red; the two consumer suites (2462 advisory, 2861 notice) stay green. --- src/dsh-channel.ts | 31 +++++++++++++++++++++++-- tests/dsh-patch-entries.test.ts | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/dsh-channel.ts b/src/dsh-channel.ts index 633b147b2a..bca6499964 100644 --- a/src/dsh-channel.ts +++ b/src/dsh-channel.ts @@ -62,10 +62,34 @@ interface DshPatchEntry { * callers decide how duplicates combine. The single source of truth for * patch-layer enablement semantics: both the #2462 cost advisory and the * #2861 host-plugin notice scan through this so they can never drift apart. */ +/** #7128: is this `disabled:` value a "row is off" declaration? + * + * The patch layer is YAML with one extension: dsh's loader also accepts + * `disabled: !!js `, and the dump of a stock web profile carries four + * such forms today. A text scan cannot evaluate an expression, so a declared + * `!!js` value is treated as "the user turned this row off". That is the + * safer error direction for both consumers: under-reporting a cost hint beats + * nagging someone who already disabled the plugin, and the host-plugin notice + * is suppressed exactly when the user acted. Plain booleans are still read + * exactly, including YAML 1.1's `yes`/`on` and quoted forms. */ +function isDisabledValue(raw: string): boolean { + let v = raw.trim(); + // A YAML comment needs whitespace before '#', so split there and keep the rest out. + const comment = v.indexOf(" #"); + if (comment >= 0) v = v.slice(0, comment).trim(); + if (v.length === 0) return false; + if (v.startsWith("!!js")) return true; + const quoted = + v.length > 1 && + ((v.startsWith('"') && v.endsWith('"')) || (v.startsWith("'") && v.endsWith("'"))); + if (quoted) v = v.slice(1, -1).trim(); + return /^(?:true|yes|on)$/i.test(v); +} + export function parseDshPatchEntries(text: string): DshPatchEntry[] { const itemRe = /^\s*-\s*id\s*:\s*(\S+)/; const listItemRe = /^\s*-\s/; - const disabledRe = /^\s+disabled\s*:\s*true\b/; + const disabledRe = /^\s+disabled\s*:\s*(.*)$/; const out: DshPatchEntry[] = []; let current: DshPatchEntry | undefined; for (const line of text.split(/\r?\n/)) { @@ -83,7 +107,10 @@ export function parseDshPatchEntries(text: string): DshPatchEntry[] { current = undefined; continue; } - if (current && disabledRe.test(line)) current.disabled = true; + if (current) { + const disabled = line.match(disabledRe); + if (disabled && isDisabledValue(disabled[1] ?? "")) current.disabled = true; + } } if (current) out.push(current); return out; diff --git a/tests/dsh-patch-entries.test.ts b/tests/dsh-patch-entries.test.ts index 4afa35172e..c9d1996429 100644 --- a/tests/dsh-patch-entries.test.ts +++ b/tests/dsh-patch-entries.test.ts @@ -56,3 +56,44 @@ test("parseDshPatchEntries: duplicate ids kept per occurrence", () => { { id: "e", disabled: true }, ]); }); + +test("parseDshPatchEntries: `disabled` is read by value, not by the literal `true`", () => { + // The patch layer accepts more than a lowercase boolean: YAML 1.1 spellings, + // quoted forms, and dsh's `!!js` extension (a stock web-profile dump carries + // four such rows today). A text scan that only matched `true` reported every + // one of these as ENABLED, which turned into a wrong cost hint and a notice + // the user could not dismiss. + const text = [ + "- id: bool-lower", + " disabled: true", + "- id: bool-title", + " disabled: True", + "- id: yaml-short", + " disabled: on", + "- id: quoted", + ' disabled: "true"', + "- id: js-const", + " disabled: !!js true", + "- id: js-expr", + " disabled: !!js process.platform === 'win32'", + "- id: commented", + " disabled: true # because it doubles the bill", + "- id: still-on-false", + " disabled: false", + "- id: still-on-false-title", + " disabled: False", + "- id: not-a-boolean", + " disabled: truex", + ].join("\n"); + const byId = new Map(parseDshPatchEntries(text).map((e) => [e.id, e.disabled])); + assert.equal(byId.get("bool-lower"), true); + assert.equal(byId.get("bool-title"), true, "YAML 1.1 `True`"); + assert.equal(byId.get("yaml-short"), true, "YAML 1.1 `on`"); + assert.equal(byId.get("quoted"), true, "a quoted boolean is still a declaration"); + assert.equal(byId.get("js-const"), true, "`!!js true` is off"); + assert.equal(byId.get("js-expr"), true, "an unevaluable expression counts as off (conservative)"); + assert.equal(byId.get("commented"), true, "a trailing comment does not hide the value"); + assert.equal(byId.get("still-on-false"), false); + assert.equal(byId.get("still-on-false-title"), false); + assert.equal(byId.get("not-a-boolean"), false, "`truex` is not a boolean"); +}); From e237d0a6e07c7d02a315f04be44904f7b3f85b89 Mon Sep 17 00:00:00 2001 From: Afloat Date: Wed, 18 Nov 2026 15:49:25 +0800 Subject: [PATCH 03/12] fix: reject partial pnpm ledger repairs Propagate structural failures from every dependency section before committing ledger changes. Keep sections without the target dependency skippable. --- src/dsh-ledger.ts | 2 +- .../issue3496-dsh-ledger-convergence.test.ts | 52 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/dsh-ledger.ts b/src/dsh-ledger.ts index d3e95a3426..61fbc43b6a 100644 --- a/src/dsh-ledger.ts +++ b/src/dsh-ledger.ts @@ -383,7 +383,7 @@ export function patchPnpmLock(text: string, spec: string, version: string): Pnpm const sub = patchNestedEntries(lines.slice(range[0], range[1]), 0, range[1] - range[0], spec, version); if (sub.kind === "fail") { // an entry-less section is fine when another section holds the entry - if (patched === 0 && !sub.reason.startsWith("no billion-context entry")) return sub; + if (!sub.reason.startsWith("no billion-context entry")) return sub; continue; } lines.splice(range[0], range[1] - range[0], ...sub.next.split("\n")); diff --git a/tests/issue3496-dsh-ledger-convergence.test.ts b/tests/issue3496-dsh-ledger-convergence.test.ts index dd7de54a36..56cfb57033 100644 --- a/tests/issue3496-dsh-ledger-convergence.test.ts +++ b/tests/issue3496-dsh-ledger-convergence.test.ts @@ -653,6 +653,58 @@ test("#3496: patchPnpmLock structural failures — no partial result", () => { assert.ok(r2.kind === "fail" && r2.reason.includes("no billion-context entry")); }); +for (const field of ["specifier", "version"] as const) { + test(`patchPnpmLock rejects a devDependency missing ${field} after a valid dependency`, () => { + const broken = pnpmLockV6("^0.1.196", "0.1.196") + [ + "devDependencies:", + " billion-context:", + field === "specifier" ? " version: 0.1.196" : " specifier: ^0.1.196", + "", + ].join("\n"); + const result = patchPnpmLock(broken, "^0.1.197", "0.1.197"); + assert.equal(result.kind, "fail"); + assert.ok(result.kind === "fail" && result.reason.includes("lacks both")); + }); +} + +test("ledger convergence leaves every file untouched when a later pnpm section is malformed", () => { + const root = mkdtempSync(path.join(tmpdir(), "bc-ledger-partial-")); + try { + const broken = pnpmLockV6("^0.1.196", "0.1.196") + [ + "devDependencies:", + " billion-context:", + " specifier: ^0.1.196", + "", + ].join("\n"); + const dir = makeProfile(root, "partial", { spec: "^1.2.9", copyVersion: "0.1.197", pnpmLock: broken }); + const before = profileFiles(dir); + const beforeMt = profileMtimes(dir); + const result = convergeDshLedger(dir, "0.1.197"); + assert.match(result.error ?? "", /lacks both/); + assert.deepEqual(result.changed, []); + assert.deepEqual(result.details, []); + assertZeroWrites(dir, before, beforeMt); + } finally { + rmrf(root); + } +}); + +test("patchPnpmLock accepts a later section containing only unrelated dependencies", () => { + const lock = pnpmLockV6("^0.1.196", "0.1.196") + [ + "devDependencies:", + " other-dev:", + " specifier: ^2.0.0", + " version: 2.0.0", + "", + ].join("\n"); + const result = patchPnpmLock(lock, "^0.1.197", "0.1.197"); + assert.equal(result.kind, "ok"); + if (result.kind === "ok") { + assert.match(result.next, /specifier: \^0\.1\.197\n version: 0\.1\.197/); + assert.ok(result.next.endsWith(lock.slice(lock.indexOf("devDependencies:")))); + } +}); + test("#3496: patchNpmLock without the node_modules entry fails cleanly", () => { const r = patchNpmLock(JSON.stringify({ packages: { "": { dependencies: { "billion-context": "^1.2.9" } } } }), "^0.1.197", "0.1.197"); assert.equal(r.kind, "fail"); From 6228c3954218edbcdd550c6e990d91a2d57034e4 Mon Sep 17 00:00:00 2001 From: Markus Eisele Date: Wed, 18 Nov 2026 09:18:05 +0100 Subject: [PATCH 04/12] fix(openai): keep provider usage fields in synthesized completion Preserve the last upstream usage object when emitting the synthesized completion chunk. OpenAI clients that price a turn from provider fields (for example cost/credits) otherwise lose them. Forward the trailing usage-only frame to clients that set stream_options.include_usage, including text-only rounds, and reset the remembered usage per stream round so a later round cannot inherit a previous cost. Adds five regression tests in tests/openai-usage-frame-forward.test.ts. --- src/loop/adapter-openai.ts | 31 ++++++- tests/openai-usage-frame-forward.test.ts | 104 +++++++++++++++++++++++ 2 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 tests/openai-usage-frame-forward.test.ts diff --git a/src/loop/adapter-openai.ts b/src/loop/adapter-openai.ts index a195352343..4a14d2173a 100644 --- a/src/loop/adapter-openai.ts +++ b/src/loop/adapter-openai.ts @@ -166,8 +166,24 @@ function openaiCachedTokens(u: Record): number | null { export function createOpenaiAdapter(requestBody: Record, clientSystem?: string, absorbName?: string, notes?: string[], errorShape: "protocol" | "completion" = "protocol"): CompressLoopAdapter { const model = (requestBody.model as string) ?? "unknown"; + // OpenAI clients opt into the trailing usage frame with + // stream_options.include_usage. bili forces the upstream option on for its + // own ledger, but the frame must also reach the CLIENT when it asked for + // it: cost/context-aware clients read provider fields from that frame, and + // swallowing it on plain-text rounds silently loses them. Tool-call rounds + // keep the existing verbatim forward. + const clientWantsUsage = (() => { + const so = (requestBody as { stream_options?: unknown }).stream_options; + return typeof so === "object" && so !== null + && (so as Record).include_usage === true; + })(); let responseId = `chatcmpl-proxy-${Date.now()}`; let toolIndex = 0; + // Last upstream usage object seen on the wire. The synthesized completion + // frame re-states tokens; provider-specific fields (cost/credits, cache + // details, validity flags) must survive that re-statement — dropping them + // breaks any client that prices or accounts the turn from them. + let lastUsageRaw: Record | null = null; const makeBase = () => ({ id: responseId, @@ -251,6 +267,9 @@ export function createOpenaiAdapter(requestBody: Record, client }, async *parseStream(upstream, round) { + // Per-round scope: a later round that emits no usage frame must not + // inherit the previous round's provider fields (e.g. `credits`). + lastUsageRaw = null; const pending = new Map(); // #206: strip model-imitated render tags from PROSE deltas; the // filter may hold back a short tail, flushed at finish/[DONE]. @@ -506,6 +525,7 @@ export function createOpenaiAdapter(requestBody: Record, client if (!choice) { if (parsed.usage) { const u = parsed.usage as Record; + lastUsageRaw = u; yield { kind: "usage", inputTokens: typeof u.prompt_tokens === "number" ? u.prompt_tokens : undefined, @@ -516,7 +536,7 @@ export function createOpenaiAdapter(requestBody: Record, client // from this trailing empty-choices frame; raw tool-call rounds // must forward it verbatim, not swallow it into the internal // ledger. - if (sawRealToolCall) { + if (sawRealToolCall || clientWantsUsage) { yield { kind: "meta", chunk: rawBuf } as ParsedStreamEvent; } } @@ -549,6 +569,7 @@ export function createOpenaiAdapter(requestBody: Record, client // only the ledger gains the sample. if (parsed.usage && !finishReason && delta && Object.keys(delta).length === 0) { const u = parsed.usage as Record; + lastUsageRaw = u; yield { kind: "usage", inputTokens: typeof u.prompt_tokens === "number" ? u.prompt_tokens : undefined, @@ -562,6 +583,7 @@ export function createOpenaiAdapter(requestBody: Record, client const hadToolCalls = [...pending.values()].some((tc) => tc.name.length > 0 || tc.id.length > 0); yield* settleToolCalls(); const u = parsed.usage as Record | undefined; + if (u) lastUsageRaw = u; yield { kind: "usage", inputTokens: typeof u?.prompt_tokens === "number" ? u.prompt_tokens : undefined, @@ -692,13 +714,18 @@ export function createOpenaiAdapter(requestBody: Record, client // Numeric fields must never serialize to absent/undefined: strict // clients (dsh's mapUsage) compute over prompt_tokens/completion_tokens // and a usage object missing them yields NaN (non-JSON-serializable). + const raw = lastUsageRaw ?? {}; + const rawDetails = raw.prompt_tokens_details && typeof raw.prompt_tokens_details === "object" + ? raw.prompt_tokens_details as Record + : undefined; const usage = opts?.usage ? { + ...raw, prompt_tokens: opts.usage.inputTokens ?? 0, completion_tokens: opts.usage.outputTokens ?? 0, total_tokens: (opts.usage.inputTokens ?? 0) + (opts.usage.outputTokens ?? 0), ...(typeof opts.usage.cachedTokens === "number" - ? { prompt_tokens_details: { cached_tokens: opts.usage.cachedTokens } } + ? { prompt_tokens_details: { ...rawDetails, cached_tokens: opts.usage.cachedTokens } } : {}), } : null; diff --git a/tests/openai-usage-frame-forward.test.ts b/tests/openai-usage-frame-forward.test.ts new file mode 100644 index 0000000000..6052dd73d4 --- /dev/null +++ b/tests/openai-usage-frame-forward.test.ts @@ -0,0 +1,104 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { createOpenaiAdapter } from "../src/loop/index.ts"; +import type { ParsedStreamEvent } from "../src/loop/core.ts"; + +// Regression: a plain-text OpenAI turn with stream_options.include_usage must +// forward the trailing usage-only frame to the client. bili used to swallow it +// unless a tool call was seen, so clients that price the turn from that frame +// (IBM Bob) reported cost 0 while the account was still charged. + +const enc = new TextEncoder(); +const dec = new TextDecoder(); + +const frame = (obj: Record) => enc.encode(`data: ${JSON.stringify(obj)}\n\n`); + +const textTurn = (...extra: Uint8Array[]): ReadableStream => + new ReadableStream({ + start(controller) { + controller.enqueue(frame({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: { role: "assistant" }, finish_reason: null }] })); + controller.enqueue(frame({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: { content: "ok" }, finish_reason: null }] })); + controller.enqueue(frame({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] })); + for (const e of extra) controller.enqueue(e); + controller.enqueue(enc.encode("data: [DONE]\n\n")); + controller.close(); + }, + }); + +const usageFrame = (prompt: number, completion: number) => + frame({ + id: "c1", + object: "chat.completion.chunk", + created: 1, + model: "gpt", + choices: [], + usage: { prompt_tokens: prompt, completion_tokens: completion, total_tokens: prompt + completion }, + }); + +const collect = async (body: Record, stream: ReadableStream) => { + const adapter = createOpenaiAdapter(body); + const events: ParsedStreamEvent[] = []; + for await (const ev of adapter.parseStream(stream, 1)) events.push(ev); + return events; +}; + +const wire = (events: ParsedStreamEvent[]): string => + events + .filter((ev) => ev.kind === "meta") + .map((ev) => dec.decode((ev as { chunk: Uint8Array }).chunk)) + .join(""); + +test("include_usage client receives the trailing usage-only frame on a text turn", async () => { + const events = await collect( + { model: "gpt", stream: true, stream_options: { include_usage: true } }, + textTurn(usageFrame(123, 4)), + ); + const out = wire(events); + assert.match(out, /"usage":\{"prompt_tokens":123/); + assert.match(out, /"completion_tokens":4/); +}); + +test("client that did not opt in does not receive the usage-only frame", async () => { + const events = await collect({ model: "gpt", stream: true }, textTurn(usageFrame(123, 4))); + assert.doesNotMatch(wire(events), /"prompt_tokens"/); +}); + +test("tool-call round still forwards the usage frame verbatim", async () => { + const toolCall = frame({ + id: "c1", + object: "chat.completion.chunk", + created: 1, + model: "gpt", + choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "call_1", type: "function", function: { name: "bash", arguments: "{}" } }] }, finish_reason: "tool_calls" }], + }); + const events = await collect({ model: "gpt", stream: true }, textTurn(toolCall, usageFrame(50, 2))); + assert.match(wire(events), /"prompt_tokens":50/); +}); + +test("synthesized completion preserves provider-specific usage fields (credits)", async () => { + const adapter = createOpenaiAdapter({ model: "gpt", stream: true, stream_options: { include_usage: true } }); + const usageWithCredits = frame({ + choices: [{ delta: {}, index: 0 }], + created: 1, + id: "c1", + model: "gpt", + object: "chat.completion.chunk", + usage: { prompt_tokens: 123, completion_tokens: 4, total_tokens: 127, credits: 0.42, cache_validity: false }, + }); + for await (const _ of adapter.parseStream(textTurn(usageWithCredits), 1)) { /* drain */ } + const completion = adapter.emitCompletion({ usage: { inputTokens: 123, outputTokens: 4, cachedTokens: 0 } }).toString("utf8"); + assert.match(completion, /"credits":0.42/); + assert.match(completion, /"prompt_tokens":123/); +}); + +test("a round without usage does not inherit the previous round's credits", async () => { + const adapter = createOpenaiAdapter({ model: "gpt", stream: true, stream_options: { include_usage: true } }); + const withCredits = frame({ + choices: [{ delta: {}, index: 0 }], created: 1, id: "c1", model: "gpt", object: "chat.completion.chunk", + usage: { prompt_tokens: 10, completion_tokens: 2, total_tokens: 12, credits: 0.42 }, + }); + for await (const _ of adapter.parseStream(textTurn(withCredits), 1)) { /* drain */ } + for await (const _ of adapter.parseStream(textTurn(), 2)) { /* drain, no usage frame */ } + const completion = adapter.emitCompletion({ usage: { inputTokens: 10, outputTokens: 2, cachedTokens: 0 } }).toString("utf8"); + assert.doesNotMatch(completion, /"credits"/); +}); From 7996c49487398a6bccf9e0d2780e940a1210d023 Mon Sep 17 00:00:00 2001 From: Afloat Date: Wed, 18 Nov 2026 18:49:59 +0800 Subject: [PATCH 05/12] fix: abort superseded cache keepalive requests Let each snapshot own cancellation through response-body consumption and release its upstream connection when replaced or disarmed. Preserve the existing timeout and failure policy, including early Node 20 support. Fixes #7142 --- src/cache-keepalive.ts | 98 ++++++----- tests/issue7142-keepalive-flight.test.ts | 210 +++++++++++++++++++++++ 2 files changed, 266 insertions(+), 42 deletions(-) create mode 100644 tests/issue7142-keepalive-flight.test.ts diff --git a/src/cache-keepalive.ts b/src/cache-keepalive.ts index 1dcb82fd07..036e82d4cc 100644 --- a/src/cache-keepalive.ts +++ b/src/cache-keepalive.ts @@ -176,6 +176,7 @@ interface KeepaliveEntry { lastHitAt?: number; timerId: ReturnType | undefined; pingInFlight: boolean; + pingController?: AbortController; } const registry = new Map(); @@ -345,6 +346,7 @@ function armInternal(inp: KeepaliveArmInput): void { pingInFlight: false, }; registry.set(inp.sessionId, entry); + prev?.pingController?.abort(); entry.timerId = scheduleFn(() => { void firePing(entry); }, entry.cadenceMs); @@ -372,52 +374,63 @@ async function firePing(entry: KeepaliveEntry): Promise { return; } entry.pingInFlight = true; + const controller = new AbortController(); + entry.pingController = controller; + // Keep cancellation live until the response body is consumed. Relay the + // deadline without AbortSignal.any, which is absent in early Node 20. + const timeout = AbortSignal.timeout(PING_TIMEOUT_MS); + const onTimeout = () => controller.abort(timeout.reason); + timeout.addEventListener("abort", onTimeout, { once: true }); entry.lastPingAt = clock(); - let resp: Response; try { - resp = await sendPing(entry); - } catch (err) { - entry.pingInFlight = false; - handleFailure(entry, `network: ${err instanceof Error ? err.message : String(err)}`); - return; - } - entry.pingInFlight = false; - if (registry.get(entry.sessionId) !== entry) return; - let text: string; - try { - text = await resp.text(); - } catch (err) { - if (resp.status !== 429) { - handleFailure(entry, `response body: ${err instanceof Error ? err.message : String(err)}`); + let resp: Response; + try { + resp = await sendPing(entry, controller.signal); + } catch (err) { + handleFailure(entry, `network: ${err instanceof Error ? err.message : String(err)}`); return; } - text = ""; - } - if (registry.get(entry.sessionId) !== entry) return; - if (!resp.ok) { - if (resp.status === 429) { - disarm(entry.sessionId, "rate-limited(429)"); + if (registry.get(entry.sessionId) !== entry) return; + let text: string; + try { + text = await resp.text(); + } catch (err) { + if (resp.status !== 429) { + handleFailure(entry, `response body: ${err instanceof Error ? err.message : String(err)}`); + return; + } + text = ""; + } + if (registry.get(entry.sessionId) !== entry) return; + if (!resp.ok) { + if (resp.status === 429) { + disarm(entry.sessionId, "rate-limited(429)"); + return; + } + handleFailure(entry, `http ${resp.status}: ${text.slice(0, 200)}`); return; } - handleFailure(entry, `http ${resp.status}: ${text.slice(0, 200)}`); - return; - } - let usage: Record | undefined; - try { - const j = JSON.parse(text) as { usage?: unknown }; - if (j && typeof j === "object" && j.usage && typeof j.usage === "object") usage = j.usage as Record; - } catch { - usage = undefined; - } - const cached = usage ? usageTotals("anthropic", usage).cached : undefined; - entry.pingCount += 1; - if (typeof cached === "number" && cached > 0) { - entry.failStreak = 0; - entry.lastHitAt = clock(); - loggerLog("info", `[${entry.sessionId}] [keepalive] ping#${entry.pingCount} hit=${cached}tok`); - reschedule(entry); - } else { - disarm(entry.sessionId, "prefix-dead(cache_read=0)"); + let usage: Record | undefined; + try { + const j = JSON.parse(text) as { usage?: unknown }; + if (j && typeof j === "object" && j.usage && typeof j.usage === "object") usage = j.usage as Record; + } catch { + usage = undefined; + } + const cached = usage ? usageTotals("anthropic", usage).cached : undefined; + entry.pingCount += 1; + if (typeof cached === "number" && cached > 0) { + entry.failStreak = 0; + entry.lastHitAt = clock(); + loggerLog("info", `[${entry.sessionId}] [keepalive] ping#${entry.pingCount} hit=${cached}tok`); + reschedule(entry); + } else { + disarm(entry.sessionId, "prefix-dead(cache_read=0)"); + } + } finally { + timeout.removeEventListener("abort", onTimeout); + entry.pingInFlight = false; + entry.pingController = undefined; } } @@ -439,7 +452,7 @@ function reschedule(entry: KeepaliveEntry): void { }, entry.cadenceMs); } -async function sendPing(entry: KeepaliveEntry): Promise { +async function sendPing(entry: KeepaliveEntry, signal: AbortSignal): Promise { const headers: Record = {}; for (const [k, v] of Object.entries(entry.headers)) { const lk = k.toLowerCase(); @@ -453,7 +466,7 @@ async function sendPing(entry: KeepaliveEntry): Promise { method: "POST", headers, body: buildPingBody(entry.body), - signal: AbortSignal.timeout(PING_TIMEOUT_MS), + signal, }; if (entry.dispatcher) init.dispatcher = entry.dispatcher; return await fetch(entry.upstreamUrl, init as RequestInit) as Response; @@ -464,6 +477,7 @@ function disarm(sessionId: string, reason: string): void { if (!entry) return; registry.delete(sessionId); if (entry.timerId !== undefined) cancelFn(entry.timerId); + entry.pingController?.abort(); loggerLog("info", `[${sessionId}] [keepalive] disarmed reason=${reason} pings=${entry.pingCount}`); } diff --git a/tests/issue7142-keepalive-flight.test.ts b/tests/issue7142-keepalive-flight.test.ts new file mode 100644 index 0000000000..07bf8e8ae4 --- /dev/null +++ b/tests/issue7142-keepalive-flight.test.ts @@ -0,0 +1,210 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { test, type TestContext } from "node:test"; +import { Agent } from "undici"; +import { + _resetKeepaliveForTest, + _setKeepaliveSchedulerForTest, + keepaliveStatusFor, + noteSuccessfulForward, + type KeepaliveArmInput, +} from "../src/cache-keepalive.ts"; +import { _resetSessionsForTest, getSession } from "../src/session.ts"; + +const ENV_KEYS = [ + "BILI_CACHE_KEEPALIVE", "BILI_CACHE_KEEPALIVE_TTL_FACTOR", "BILI_CACHE_KEEPALIVE_MAX_DURATION_MS", + "BILI_CACHE_KEEPALIVE_MAX_PINGS", "BILI_CACHE_KEEPALIVE_ASSUME_TTL", + "BILI_CACHE_KEEPALIVE_ARM_WITHOUT_BREAKPOINTS", "BILI_CACHE_KEEPALIVE_INTERVAL_MS", +] as const; +const HIT = JSON.stringify({ usage: { input_tokens: 1, cache_read_input_tokens: 1234 } }); +const BODY = JSON.stringify({ + model: "keepalive-test", max_tokens: 32, stream: true, + system: [{ type: "text", text: "cached prefix", cache_control: { type: "ephemeral" } }], + messages: [{ role: "user", content: "hello" }], +}); +const NO_BREAKPOINT = JSON.stringify({ model: "keepalive-test", messages: [{ role: "user", content: "hello" }] }); + +function deferred(): { promise: Promise; resolve: (value: T) => void } { + let resolve!: (value: T) => void; + return { promise: new Promise(r => { resolve = r; }), resolve }; +} + +async function within(promise: Promise, label: string): Promise { + let timer: ReturnType | undefined; + try { + return await Promise.race([ + promise, + new Promise((_, reject) => { timer = setTimeout(() => reject(new Error(`${label} timed out`)), 2000); }), + ]); + } finally { + if (timer !== undefined) clearTimeout(timer); + } +} + +async function setup(t: TestContext, phase: "headers" | "body") { + const env = Object.fromEntries(ENV_KEYS.map(key => [key, process.env[key]])); + for (const key of ENV_KEYS) delete process.env[key]; + process.env.BILI_CACHE_KEEPALIVE = "1"; + process.env.BILI_CACHE_KEEPALIVE_MAX_PINGS = "1"; + _resetKeepaliveForTest(); + _resetSessionsForTest(); + const timers = new Map void>(); + let timerId = 0; + _setKeepaliveSchedulerForTest({ + schedule(fn) { const id = ++timerId; timers.set(id, fn); return id; }, + cancel(id) { timers.delete(id as number); }, + }); + const received = deferred(); + const consumingBody = deferred(); + const oldClosed = deferred(); + let held: http.ServerResponse; + let pingCount = 0; + const requests: Array<{ url: string; body: string }> = []; + const server = http.createServer((req, res) => { + let body = ""; + req.on("data", chunk => { body += chunk; }); + req.on("end", () => { + requests.push({ url: req.url ?? "", body }); + if (req.url === "/ping" && ++pingCount === 1) { + held = res; + res.once("close", () => oldClosed.resolve()); + if (phase === "body") { + res.writeHead(200, { "content-type": "application/json" }); + res.write(HIT.slice(0, 9)); + } + received.resolve(); + return; + } + res.writeHead(200, { "content-type": "application/json" }); + res.end(req.url === "/ping" ? HIT : JSON.stringify({ normal: true })); + }); + }); + await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)); + const address = server.address(); + assert.ok(address && typeof address !== "string"); + const origin = `http://127.0.0.1:${address.port}`; + const dispatcher = new Agent({ connections: 1, pipelining: 1 }); + const realFetch = globalThis.fetch; + let pingSignal: AbortSignal | null | undefined; + globalThis.fetch = async (input, init) => { + if (input === `${origin}/ping`) pingSignal = init?.signal; + const response = await realFetch(input, init); + if (input === `${origin}/ping`) { + const readText = response.text.bind(response); + Object.defineProperty(response, "text", { + value: () => { consumingBody.resolve(); return readText(); }, + }); + } + return response; + }; + const sessionId = `keepalive-7142-${phase}-${t.name}`; + getSession(sessionId, { protocol: "anthropic" }); + const input: KeepaliveArmInput = { + sessionId, protocol: "anthropic", reqMethod: "POST", sidePassthrough: false, resignLane: false, + wireBody: BODY, upstreamUrl: `${origin}/ping`, headers: { "content-type": "application/json", "x-api-key": "old-test-key" }, + dispatcher, routes: undefined, + }; + t.after(async () => { + _resetKeepaliveForTest(); + globalThis.fetch = realFetch; + await dispatcher.destroy(); + server.closeAllConnections(); + await new Promise(resolve => server.close(() => resolve())); + _resetSessionsForTest(); + for (const key of ENV_KEYS) { + if (env[key] === undefined) delete process.env[key]; + else process.env[key] = env[key]; + } + }); + const fire = () => { + assert.equal(timers.size, 1); + const [id, callback] = timers.entries().next().value!; + timers.delete(id); + callback(); + }; + noteSuccessfulForward(input); + fire(); + await within(phase === "headers" ? received.promise : consumingBody.promise, "ping stage"); + return { + input, timers, fire, requests, oldClosed, getSignal: () => pingSignal, + finishOld() { + if (phase === "headers") held.writeHead(200, { "content-type": "application/json" }); + held.end(phase === "body" ? HIT.slice(9) : HIT); + }, + async normalRequest() { + const init: Omit & { dispatcher: object } = { + dispatcher, signal: AbortSignal.timeout(1000), + }; + const response = await realFetch(`${origin}/normal`, init as RequestInit); + assert.equal(response.status, 200); + assert.deepEqual(await response.json(), { normal: true }); + }, + }; +} + +for (const phase of ["headers", "body"] as const) { + for (const change of ["replace", "disable", "rotate", "evict"] as const) { + test(`${change} releases an in-flight keepalive waiting for ${phase}`, async t => { + const f = await setup(t, phase); + let currentSession: string | undefined; + if (change === "replace") { + noteSuccessfulForward({ ...f.input, wireBody: BODY.replace("hello", "new turn") }); + currentSession = f.input.sessionId; + } else if (change === "disable") { + process.env.BILI_CACHE_KEEPALIVE = "0"; + noteSuccessfulForward(f.input); + } else if (change === "rotate") { + noteSuccessfulForward({ ...f.input, wireBody: NO_BREAKPOINT, headers: { ...f.input.headers, "x-api-key": "new-test-key" } }); + } else { + currentSession = `${f.input.sessionId}-other`; + getSession(currentSession, { protocol: "anthropic" }); + noteSuccessfulForward({ ...f.input, sessionId: currentSession }); + } + await f.normalRequest(); + await within(f.oldClosed.promise, "invalidated ping close"); + assert.equal(f.getSignal()?.aborted, true); + assert.equal(f.requests.filter(request => request.url === "/normal").length, 1); + assert.equal(f.requests[0]!.body, JSON.stringify({ ...JSON.parse(BODY), max_tokens: 1, stream: false })); + if (currentSession) { + assert.equal(keepaliveStatusFor(currentSession)?.failStreak, 0); + assert.equal(keepaliveStatusFor(currentSession)?.pingCount, 0); + f.fire(); + await f.normalRequest(); + assert.equal(keepaliveStatusFor(currentSession)?.pingCount, 1); + assert.equal(keepaliveStatusFor(currentSession)?.failStreak, 0); + assert.equal(f.timers.size, 1); + } else { + assert.equal(keepaliveStatusFor(f.input.sessionId), undefined); + assert.equal(f.timers.size, 0); + } + }); + } + + test(`an unmodified snapshot keeps its live ${phase} request and records the hit`, async t => { + const f = await setup(t, phase); + noteSuccessfulForward({ ...f.input, wireBody: NO_BREAKPOINT }); + assert.equal(f.getSignal()?.aborted, false); + assert.ok(keepaliveStatusFor(f.input.sessionId)); + f.finishOld(); + await f.normalRequest(); + assert.equal(keepaliveStatusFor(f.input.sessionId)?.pingCount, 1); + assert.equal(keepaliveStatusFor(f.input.sessionId)?.failStreak, 0); + assert.equal(f.timers.size, 1); + }); +} + +test("keepalive cancellation works without AbortSignal.any on early Node 20", async t => { + const original = Object.getOwnPropertyDescriptor(AbortSignal, "any"); + Object.defineProperty(AbortSignal, "any", { value: undefined, configurable: true, writable: true }); + t.after(() => { + if (original) Object.defineProperty(AbortSignal, "any", original); + else Reflect.deleteProperty(AbortSignal, "any"); + }); + const f = await setup(t, "body"); + process.env.BILI_CACHE_KEEPALIVE = "0"; + noteSuccessfulForward(f.input); + await f.normalRequest(); + await within(f.oldClosed.promise, "invalidated ping close"); + assert.equal(f.getSignal()?.aborted, true); + assert.equal(keepaliveStatusFor(f.input.sessionId), undefined); +}); From 60d67f3bef27d2960c6a51b8634dcf8182ef8ae2 Mon Sep 17 00:00:00 2001 From: resec Date: Wed, 18 Nov 2026 20:26:09 +0800 Subject: [PATCH 06/12] fix(dsh): match carriers on their emitted shape, not a bare marker (#7153) A human message that merely quoted a durable marker was classified as a host injection. Two consumers made that a correctness bug: the kernel's Rule-3 last-user anchor refuses a virtual-path message (so the live request could be folded away silently), and durable/* goes into protectedLatestTools (so a quote took the identity over from the real carrier and was pinned forever). Shape gates on the four classifiers whose bare marker collides with human or relay text: the durable pair must open the system-reminder frame both producers emit, and the two one-shot notices must carry their emitter's real shape (job id + detail/EOL, a settlement ending). Every emitter shape keeps matching; a future emitter change degrades to a miss, never a false positive. Existing fixtures that encoded the bare-marker shape were updated to the producers' real output; assertions untouched. Real-session check (343 dsh journals, 3009 user-role messages): 1061 -> 1060 classifications, human false positives 1 -> 0, and the misclassified relay report now resolves as oneshot/agent-relay instead of stealing durable/skill-catalog. --- src/durable-message-guards.ts | 85 +++++++--- tests/durable-tool-identity.test.ts | 2 +- tests/issue2446-dsh-mcp-catalog.test.ts | 6 +- ...sue2447-dsh-workspace-instructions.test.ts | 4 +- tests/issue2481-dsh-runtime-context.test.ts | 4 +- tests/issue2766-durable-role-gate.test.ts | 6 +- tests/issue5142-expected-rebase.test.ts | 16 +- tests/issue5173-last-user-anchor.test.ts | 2 +- tests/issue7153-carrier-shape-gates.test.ts | 156 ++++++++++++++++++ 9 files changed, 245 insertions(+), 36 deletions(-) create mode 100644 tests/issue7153-carrier-shape-gates.test.ts diff --git a/src/durable-message-guards.ts b/src/durable-message-guards.ts index 43ae3d8ef9..e4d12ba943 100644 --- a/src/durable-message-guards.ts +++ b/src/durable-message-guards.ts @@ -71,21 +71,46 @@ import type { Config, CoreMessage } from "acp-kernel"; * path (or undefined when the message is no durable carrier). */ type DurableMessageClassifier = (msg: CoreMessage) => string | undefined; +/** The machine frame every dsh durable injection is wrapped in + * (\x3csystem-reminder\x3e … \x3c/system-reminder\x3e). Both producers that + * own a durable carrier emit the COMPLETE frame themselves — + * dsh-tool-skill's renderCatalogMessage/renderCatalogUpdate and + * dsh-agent-instructions' buildInstructionText ("This producer owns the + * complete frame") — so the frame is a stable property of + * the carrier, not of some outer host wrapper. */ +const SYSTEM_REMINDER_FRAME = "\x3csystem-reminder\x3e"; + +/** True when the message OPENS the system-reminder frame. The marker-only + * match this replaces could not tell a real injection from a message that + * merely QUOTES one (#7153): a human paste or a subagent report carrying the + * block took the newest instance of `durable/`, so it was pinned + * forever AND the real carrier lost the identity (latest-per-identity) — + * and Rule 3 stopped treating the quoting message as the last human request + * (#5109), which strands the live ask outside the protected zone. A future + * producer that stops emitting the frame degrades to a MISS (the carrier + * folds as ordinary history): the direction this file prefers over a false + * positive. */ +function opensSystemReminderFrame(text: string): boolean { + return text.trimStart().startsWith(SYSTEM_REMINDER_FRAME); +} + /** dsh skill-catalog identity (#2419): the durable catalog message injected * by dsh-tool-skill (a \x3csystem-reminder\x3e carrying the * \x3cavailable_skills\x3e block; source: asar dsh-tool-skill/lib/index.js, * createUserMessage({ source: { kind: "skill-catalog" } })). The marker is - * matched on its own rather than on the wrapper so a future wrapper change - * cannot silently disarm the classifier; a false positive is benign (a - * matching message simply stays visible) and the marker is DSH-specific - * XML. Re-emitted only when the visible skill set CHANGES, so the newest - * instance is the source of truth (`protectedLatestTools` candidate). + * matched INSIDE the frame rather than on its own (#7153): the bare marker + * also matches a human paste or a relay report that quotes the block, and + * under latest-per-identity such a quote takes the identity over from the + * real catalog — it gets pinned forever while the catalog folds. Re-emitted + * only when the visible skill set CHANGES, so the newest instance is the + * source of truth (`protectedLatestTools` candidate). * #2766: assistant-role messages are never carriers — every carrier is a * host injection into the user channel, so a model ECHO of the marker must * not project (under latest-per-identity it would take the identity over * from the real carrier and get it folded away). */ export function dshSkillCatalogPath(msg: CoreMessage): string | undefined { - return msg.role !== "assistant" && msg.contentType === "text" && typeof msg.text === "string" && msg.text.includes("\x3cavailable_skills\x3e") + return msg.role !== "assistant" && msg.contentType === "text" && typeof msg.text === "string" + && opensSystemReminderFrame(msg.text) && msg.text.includes("\x3cavailable_skills\x3e") ? "durable/skill-catalog" : undefined; } @@ -98,15 +123,20 @@ export function dshSkillCatalogPath(msg: CoreMessage): string | undefined { * createUserMessage). The host re-emits the baseline only when * baselineIdentity (projectRoot + discovery/budget config, NOT file contents) * changes, so one fold of it silently kills hard constraints like "read X - * before any web access" forever. Marker-over-wrapper rationale as #2419. - * The match is case-sensitive on purpose — reconciliation change messages - * use lowercase variants ("Updated instructions from:") that remain ordinary - * consumable history. At most ~one such baseline lives per session + * before any web access" forever. Match the `Instructions from:` section + * label INSIDE the \x3csystem-reminder\x3e frame (#7153) — a human pasting a + * rule file ("Instructions from: AGENTS.md" quoted in prose) is ordinary + * history, not the baseline: matching it would pin the paste and take the + * identity over from the real baseline under latest-per-identity. + * The label match stays case-sensitive on purpose — reconciliation change + * messages use lowercase variants ("Updated instructions from:") that remain + * ordinary consumable history. At most ~one such baseline lives per session * (replacements are rare identity changes), so pinning every match is * bounded. #2766: assistant-role messages are never carriers (see * dshSkillCatalogPath). */ export function dshWorkspaceInstructionsPath(msg: CoreMessage): string | undefined { - return msg.role !== "assistant" && msg.contentType === "text" && typeof msg.text === "string" && msg.text.includes("Instructions from:") + return msg.role !== "assistant" && msg.contentType === "text" && typeof msg.text === "string" + && opensSystemReminderFrame(msg.text) && msg.text.includes("Instructions from:") ? "durable/agents-md" : undefined; } @@ -227,13 +257,19 @@ export function dshPastedImagePath(msg: CoreMessage): string | undefined { * background job ("background job (…) finished [status: …, exit code: * N]. Read its output with job_output."), one per job completion (×39 in the * #5139 session). One-shot like time-context: resent only while the notice - * is fresh, then gone forever after use. Prefix match (asciiCI, trimmed) on - * the machine-generated lead-in; the phrasing is dsh-specific and a plain - * user question starting that way stays on the wire, so a misclassification - * never becomes an accounting effect (same benign-FP argument as - * dshPastedImagePath). #2766: assistant-role messages are never carriers. */ + * is fresh, then gone forever after use. The match requires the job ID after + * the lead-in (#7153) — the bare prefix also matches ordinary English + * ("background jobs keep failing because …"), and a misclassified HUMAN + * message loses its Rule-3 anchor role and folds as a one-shot carrier. + * All three emitter shapes keep matching (packages/jobs/tool-jobs/src/ + * index.ts): inline "background job (kind: label) finished …", + * truncated "background job \\n[notice truncated]…", compact + * "background job \\nDone; job_output." — hence " (" / EOL. #2766: + * assistant-role messages are never carriers. */ +const TOOL_JOB_NOTICE_RE = /^background job \S+(?: \(|$|\r?\n)/i; + export function dshToolJobNoticePath(msg: CoreMessage): string | undefined { - return msg.role !== "assistant" && msg.contentType === "text" && typeof msg.text === "string" && startsWithAsciiCI(msg.text.trim(), "background job") + return msg.role !== "assistant" && msg.contentType === "text" && typeof msg.text === "string" && TOOL_JOB_NOTICE_RE.test(msg.text.trim()) ? "oneshot/tool-job-notice" : undefined; } @@ -266,15 +302,20 @@ export function dshMnemonPath(msg: CoreMessage): string | undefined { * more." / " was stopped before it finished." / " ran out of room before it * finished."), spliced into the PARENT session as a user-role message when a * child settles. One-shot: emitted once per settlement and never re-sent — - * retirement accounted via oneShotCarrierPaths. Prefix match (asciiCI) on the - * leading-trimmed text INCLUDING the marker's trailing space, which keeps - * human prose merely starting with the words ("Background subagents are - * handy") unmatched. Slot exclusion is the point (#5173): a settlement notice + * retirement accounted via oneShotCarrierPaths. The match requires the + * settlement ENDING after the child id (#7153): the bare `Background + * subagent ` lead-in also matches human prose that names a subagent + * ("Background subagent X is stuck again — why?"), and a misclassified human + * message loses its Rule-3 anchor role. All six endings the emitter can + * produce stay matched (finished / was stopped / ran out of room / declined / + * failed / ended). Slot exclusion is the point (#5173): a settlement notice * landing right after the newest human request steals the Rule-3 last-user * anchor (#5109). #2766: assistant-role messages are never carriers. */ +const SUBAGENT_SETTLED_RE = /^background subagent .+? (?:finished|was stopped|ran out of room|declined|failed|ended)\b/i; + export function dshSubagentSettledPath(msg: CoreMessage): string | undefined { return msg.role !== "assistant" && msg.contentType === "text" && typeof msg.text === "string" && - startsWithAsciiCI(msg.text.trimStart(), "Background subagent ") + SUBAGENT_SETTLED_RE.test(msg.text.trim()) ? "oneshot/subagent-settled" : undefined; } diff --git a/tests/durable-tool-identity.test.ts b/tests/durable-tool-identity.test.ts index e08aff4edd..6ff3defcf0 100644 --- a/tests/durable-tool-identity.test.ts +++ b/tests/durable-tool-identity.test.ts @@ -45,7 +45,7 @@ const MODEL = "gpt-test"; const MAIN_SYSTEM = "You are DeepSeek Harness, the main coding agent."; const CATALOG_V1 = `\x3csystem-reminder\x3e\nThe following skills are available in this workspace.\n\x3cavailable_skills\x3e\n- name: git-helper\n description: commit conventions and rebase safety\n\x3c/available_skills\x3e\n\x3c/system-reminder\x3e`; const CATALOG_V2 = `\x3csystem-reminder\x3e\nThe following skills are available in this workspace.\n\x3cavailable_skills\x3e\n- name: doc-writer\n description: draft documentation from merged PRs\n\x3c/available_skills\x3e\n\x3c/system-reminder\x3e`; -const AGENTS_MD = "Instructions from: AGENTS.md\nrepo policy: always run tests before pushing (marker X7Q)."; +const AGENTS_MD = "\x3csystem-reminder\x3e\nInstructions from: AGENTS.md\nrepo policy: always run tests before pushing (marker X7Q).\n\x3c/system-reminder\x3e"; function filler(tag: string): string { return `${tag}-` + "z".repeat(900); diff --git a/tests/issue2446-dsh-mcp-catalog.test.ts b/tests/issue2446-dsh-mcp-catalog.test.ts index bea9440da8..abb57c76dd 100644 --- a/tests/issue2446-dsh-mcp-catalog.test.ts +++ b/tests/issue2446-dsh-mcp-catalog.test.ts @@ -147,14 +147,14 @@ test("dshMcpCatalogPath: projects the catalog marker to durable/mcp-catalog on t assert.equal(dshMcpCatalogPath(msg("text", "bare \x3cmcp_catalog\x3e without wrapper")), "durable/mcp-catalog", "the bare block projects too — a wrapper rewording cannot disarm the classifier"); // Deliberately foldable neighbors stay consumable: assert.equal(dshMcpCatalogPath(msg("text", "plain prose mentioning an mcp catalog in lowercase")), undefined, "plain prose without the exact XML marker stays foldable"); - assert.equal(dshMcpCatalogPath(msg("text", "\x3cavailable_skills\x3e…\x3c/available_skills\x3e")), undefined, "the #2419 skill catalog is a DIFFERENT carrier (own identity)"); + assert.equal(dshMcpCatalogPath(msg("text", "\x3csystem-reminder\x3e\n\x3cavailable_skills\x3e\n- name: x\n\x3c/available_skills\x3e\n\x3c/system-reminder\x3e")), undefined, "the #2419 skill catalog is a DIFFERENT carrier (own identity)"); assert.equal(dshMcpCatalogPath(msg("tool-call", MCP_MSG)), undefined, "non-text content types never match"); assert.equal(dshMcpCatalogPath(msg("text", undefined)), undefined, "missing text is safe"); const dshClassifier = durableMessageToolPaths["dsh"]; assert.ok(dshClassifier, "dsh lane is registered"); assert.equal(dshClassifier!(msg("text", MCP_MSG)), "durable/mcp-catalog", "lane entry covers the #2446 catalog shape"); - assert.equal(dshClassifier!(msg("text", "\x3cavailable_skills\x3e…\x3c/available_skills\x3e")), "durable/skill-catalog", "lane entry still covers the #2419 catalog shape"); - assert.equal(dshClassifier!(msg("text", "Instructions from: AGENTS.md")), "durable/agents-md", "lane entry still covers the #2447 baseline shape"); + assert.equal(dshClassifier!(msg("text", "\x3csystem-reminder\x3e\n\x3cavailable_skills\x3e\n- name: x\n\x3c/available_skills\x3e\n\x3c/system-reminder\x3e")), "durable/skill-catalog", "lane entry still covers the #2419 catalog shape"); + assert.equal(dshClassifier!(msg("text", "\x3csystem-reminder\x3e\nInstructions from: AGENTS.md\n\x3c/system-reminder\x3e")), "durable/agents-md", "lane entry still covers the #2447 baseline shape"); assert.equal(dshClassifier!(msg("text", "Current runtime context. This snapshot supersedes earlier runtime-context snapshots.")), "durable/runtime-context", "lane entry still covers the #2481 snapshot shape"); assert.deepEqual(Object.keys(durableMessageToolPaths), ["dsh"], "allowlist stays dsh-only (KDD #9 evidence-permitlist discipline)"); }); diff --git a/tests/issue2447-dsh-workspace-instructions.test.ts b/tests/issue2447-dsh-workspace-instructions.test.ts index 43fafc21cf..06afc64441 100644 --- a/tests/issue2447-dsh-workspace-instructions.test.ts +++ b/tests/issue2447-dsh-workspace-instructions.test.ts @@ -136,11 +136,11 @@ test("dshWorkspaceInstructionsPath: projects the baseline label to durable/agent assert.equal(dshWorkspaceInstructionsPath(msg("text", INSTR_MSG)), "durable/agents-md", "the merged global+project baseline projects"); assert.equal(dshWorkspaceInstructionsPath(msg("text", "plain prose about workspace setup")), undefined); assert.equal(dshWorkspaceInstructionsPath(msg("text", CHANGE_MSG)), undefined, "lowercase reconciliation variants stay consumable history"); - assert.equal(dshWorkspaceInstructionsPath(msg("tool-call", "Instructions from: AGENTS.md")), undefined, "non-text content types never match"); + assert.equal(dshWorkspaceInstructionsPath(msg("tool-call", "\x3csystem-reminder\x3e\nInstructions from: AGENTS.md\n\x3c/system-reminder\x3e")), undefined, "non-text content types never match"); assert.equal(dshWorkspaceInstructionsPath(msg("text", undefined)), undefined, "missing text is safe"); const dshClassifier = durableMessageToolPaths["dsh"]; assert.ok(dshClassifier, "dsh lane is registered"); - assert.equal(dshClassifier!(msg("text", "\x3cavailable_skills\x3e…\x3c/available_skills\x3e")), "durable/skill-catalog", "lane entry still covers the #2419 catalog shape"); + assert.equal(dshClassifier!(msg("text", "\x3csystem-reminder\x3e\n\x3cavailable_skills\x3e\n- name: x\n\x3c/available_skills\x3e\n\x3c/system-reminder\x3e")), "durable/skill-catalog", "lane entry still covers the #2419 catalog shape"); assert.deepEqual(Object.keys(durableMessageToolPaths), ["dsh"], "allowlist stays dsh-only (KDD #9 evidence-permitlist discipline)"); }); diff --git a/tests/issue2481-dsh-runtime-context.test.ts b/tests/issue2481-dsh-runtime-context.test.ts index 44637e839f..81331b1cc7 100644 --- a/tests/issue2481-dsh-runtime-context.test.ts +++ b/tests/issue2481-dsh-runtime-context.test.ts @@ -181,8 +181,8 @@ test("dshRuntimeContextPath: projects both snapshot variants to durable/runtime- assert.equal(dshRuntimeContextPath(msg("text", undefined)), undefined, "missing text is safe"); const dshClassifier = durableMessageToolPaths["dsh"]; assert.ok(dshClassifier, "dsh lane is registered"); - assert.equal(dshClassifier!(msg("text", "\x3cavailable_skills\x3e…\x3c/available_skills\x3e")), "durable/skill-catalog", "lane entry still covers the #2419 catalog shape"); - assert.equal(dshClassifier!(msg("text", "Instructions from: AGENTS.md")), "durable/agents-md", "lane entry still covers the #2447 baseline shape"); + assert.equal(dshClassifier!(msg("text", "\x3csystem-reminder\x3e\n\x3cavailable_skills\x3e\n- name: x\n\x3c/available_skills\x3e\n\x3c/system-reminder\x3e")), "durable/skill-catalog", "lane entry still covers the #2419 catalog shape"); + assert.equal(dshClassifier!(msg("text", "\x3csystem-reminder\x3e\nInstructions from: AGENTS.md\n\x3c/system-reminder\x3e")), "durable/agents-md", "lane entry still covers the #2447 baseline shape"); assert.equal(dshClassifier!(msg("text", RT_MSG)), "durable/runtime-context", "lane entry covers the #2481 snapshot shape"); assert.equal(dshClassifier!(msg("text", RT_MSG_NEW)), "durable/runtime-context", "lane entry covers the #7085 reworded snapshot shape"); assert.equal(dshClassifier!(msg("text", RT_MSG_NEW_RESEND)), "durable/runtime-context", "lane entry covers the #7085 re-send intro shape"); diff --git a/tests/issue2766-durable-role-gate.test.ts b/tests/issue2766-durable-role-gate.test.ts index 4cadb07545..e0ca4332ec 100644 --- a/tests/issue2766-durable-role-gate.test.ts +++ b/tests/issue2766-durable-role-gate.test.ts @@ -26,8 +26,8 @@ function msg(role: CoreMessage["role"], text: string, id = "m1"): CoreMessage { } const CARRIERS: Array<[name: string, marker: string, path: string, cls: (m: CoreMessage) => string | undefined]> = [ - ["skill catalog", "", "durable/skill-catalog", dshSkillCatalogPath], - ["workspace instructions", "Instructions from: ~/.dsh/AGENTS.md", "durable/agents-md", dshWorkspaceInstructionsPath], + ["skill catalog", "\x3csystem-reminder\x3e\n\x3cavailable_skills\x3e\n- name: x\n\x3c/available_skills\x3e\n\x3c/system-reminder\x3e", "durable/skill-catalog", dshSkillCatalogPath], + ["workspace instructions", "\x3csystem-reminder\x3e\nInstructions from: ~/.dsh/AGENTS.md\n\x3c/system-reminder\x3e", "durable/agents-md", dshWorkspaceInstructionsPath], ["mcp catalog", "\n- server: fs\n", "durable/mcp-catalog", dshMcpCatalogPath], ["runtime context", "Current runtime context. This snapshot supersedes earlier runtime-context snapshots.", "durable/runtime-context", dshRuntimeContextPath], ["request-image handle", 'Image "x.png" (sha256:2e896b95dec003df); request preview 1600x900px.', "oneshot/image-handle", dshImageHandlePath], @@ -46,7 +46,7 @@ for (const [name, marker, path, cls] of CARRIERS) { } test("Path: system-role carrier text still projects — the gate targets model output only (#2766)", () => { - assert.equal(dshSkillCatalogPath(msg("system", "")), "durable/skill-catalog"); + assert.equal(dshSkillCatalogPath(msg("system", "\x3csystem-reminder\x3e\n\x3cavailable_skills\x3e\n- name: x\n\x3c/available_skills\x3e\n\x3c/system-reminder\x3e")), "durable/skill-catalog"); }); test("kernel: an assistant echo cannot take over the latest durable identity (#2766)", () => { diff --git a/tests/issue5142-expected-rebase.test.ts b/tests/issue5142-expected-rebase.test.ts index 4358e22710..c92c187567 100644 --- a/tests/issue5142-expected-rebase.test.ts +++ b/tests/issue5142-expected-rebase.test.ts @@ -430,7 +430,7 @@ describe("#5142 carrier classifier units", () => { // tool-jobs notice production shape (#5142 reporter sample, command redacted): const job = 'background job bash-abc123 (bash: "echo ok") finished [status: completed, exit code: 0]. Read its output with job_output.'; assert.equal(dshToolJobNoticePath(mk("user", job)), "oneshot/tool-job-notice"); - assert.equal(dshToolJobNoticePath(mk("user", " Background Job abc finished.")), "oneshot/tool-job-notice", "asciiCI prefix like the other auto-injected families"); + assert.equal(dshToolJobNoticePath(mk("user", ' Background Job bash-abc123 (bash: "echo ok") finished [status: completed].')), "oneshot/tool-job-notice", "asciiCI prefix like the other auto-injected families"); assert.equal(dshToolJobNoticePath(mk("assistant", job)), undefined, "assistant echo must never classify (#2766)"); assert.equal(dshToolJobNoticePath(mk("user", "the background job failed again")), undefined, "marker mid-text is not a carrier"); // request-image handle annotation (#5142 residual, production sample): @@ -475,9 +475,21 @@ describe("#5142 carrier classifier units", () => { // varies, so the joint alone cannot classify). const MARKER_CONTEXT: Record = { " sent a message: ": "Agent session-1", + // #7153: the two durable producers own a complete system-reminder + // frame, so the marker alone is no longer a classifiable shape. + "\x3cavailable_skills\x3e": "\x3csystem-reminder\x3e\n", + "Instructions from:": "\x3csystem-reminder\x3e\n", + }; + // #7153: the two notice families also need their right context — the + // emitter puts a job id / a settlement ending after the marker, and a + // bare "background job"/"Background subagent " prefix is ordinary prose. + const MARKER_SUFFIX: Record = { + "background job": ' bash-1 (bash: "true") finished [status: completed]', + "Background subagent ": "session-1 finished.", }; for (const marker of dshCarrierRawMarkers) { - const msg = { id: "lk", role: "user" as const, contentType: "text" as const, text: `${MARKER_CONTEXT[marker] ?? ""}${marker}` } as CoreMessage; + const text = `${MARKER_CONTEXT[marker] ?? ""}${marker}${MARKER_SUFFIX[marker] ?? ""}`; + const msg = { id: "lk", role: "user" as const, contentType: "text" as const, text } as CoreMessage; assert.notEqual(cls(msg), undefined, `marker ${JSON.stringify(marker)} no longer matches any dsh classifier`); } }); diff --git a/tests/issue5173-last-user-anchor.test.ts b/tests/issue5173-last-user-anchor.test.ts index 9be9d0d87f..a1c3476518 100644 --- a/tests/issue5173-last-user-anchor.test.ts +++ b/tests/issue5173-last-user-anchor.test.ts @@ -76,7 +76,7 @@ describe("#5173 registry wiring", () => { test("all twelve dsh families resolve in BOTH the slot-exclusion and drift-accounting registries", () => { const samples: Array<[string, string]> = [ ["\x3csystem-reminder\x3e \x3cavailable_skills\x3e updated catalog \x3c/available_skills\x3e", "durable/skill-catalog"], - ["Instructions from: /home/x/.dsh/AGENTS.md", "durable/agents-md"], + ["\x3csystem-reminder\x3e\nInstructions from: /home/x/.dsh/AGENTS.md\n\x3c/system-reminder\x3e", "durable/agents-md"], ["Current runtime context. This snapshot supersedes earlier runtime-context snapshots.", "durable/runtime-context"], ["\x3cmcp_catalog\x3e registered servers \x3c/mcp_catalog\x3e", "durable/mcp-catalog"], ["Time sampled while preparing turn 9, step 1: 2026-11-01T00:00:00Z", "oneshot/time-context"], diff --git a/tests/issue7153-carrier-shape-gates.test.ts b/tests/issue7153-carrier-shape-gates.test.ts new file mode 100644 index 0000000000..3a9b3d6c96 --- /dev/null +++ b/tests/issue7153-carrier-shape-gates.test.ts @@ -0,0 +1,156 @@ +// #7153: the dsh carrier classifiers used to match a BARE marker +// (`includes`/`startsWith`), so a HUMAN message that merely quotes or +// mentions one was treated as a host injection. Two consumers make that a +// correctness bug, not a cosmetic one: +// - kernel `lastUserAnchor()` refuses a virtual-path message the Rule-3 +// anchor role, so the live request stops being protected and an +// intra-turn fold can drop it (the #5109 failure mode); +// - `durable/*` goes into protectedLatestTools, so a quote takes the +// identity over from the real carrier (latest-per-identity) and gets +// pinned forever while the real one folds. +// Fix: shape gates on the four classifiers whose bare marker collides with +// human/relay text — the durable pair must open the `` frame +// the producers emit, and the two one-shot notices must carry their emitter's +// real shape (job id + detail/EOL; a settlement ending). Every emitter shape +// keeps matching; a future emitter change degrades to a MISS, never a false +// positive (the direction this module's doctrine prefers). +import test from "node:test"; +import assert from "node:assert/strict"; +import { + dshSkillCatalogPath, + dshSubagentSettledPath, + dshToolJobNoticePath, + dshWorkspaceInstructionsPath, + durableMessageToolPaths, + withDurableProtection, +} from "../src/durable-message-guards.ts"; +import { defaultConfig } from "../kernel/src/config.js"; +import { lastUserAnchor } from "../kernel/src/protected.js"; +import { buildCompressibleRanges, computeProtectedRefs } from "../kernel/src/recommend.js"; +import { createInitialState } from "../kernel/src/state.js"; +import { assignRefs } from "../kernel/src/refs.js"; +import type { CoreMessage } from "../kernel/src/types.js"; + +function msg(id: string, text: string, role: CoreMessage["role"] = "user"): CoreMessage { + return { id, role, contentType: "text", text }; +} + +/** The shape dsh-tool-skill emits (renderCatalogMessage). */ +const REAL_CATALOG = [ + "\x3csystem-reminder\x3e", + "A skill is a reusable set of task-specific instructions. The following skills are available in this session:", + "", + "\x3cavailable_skills\x3e", + "- name: git-helper", + " description: commit conventions", + "\x3c/available_skills\x3e", + "\x3c/system-reminder\x3e", +].join("\n"); + +/** A human prompt that QUOTES a catalog block mid-text (the #7153 field shape). */ +const HUMAN_PROMPT_QUOTING_CATALOG = [ + "You are researching the runtime. Compare this prompt with ours and report the diff.", + "", + "\x3cavailable_skills\x3e", + "- name: other-helper", + "\x3c/available_skills\x3e", + "", + "Start with the prompt, not the harness.", +].join("\n"); + +/** A relayed subagent report that quotes the block (the 43 KB field shape). */ +const RELAY_QUOTING_CATALOG = "Agent 423c357e-d37a-405e-92d2-1399fff01e0c sent a message: REPORT\n\nFor reference the header is\n\x3cavailable_skills\x3e\n- name: x\n\x3c/available_skills\x3e"; + +test("#7153 durable carriers still match when they open the system-reminder frame", () => { + assert.equal(dshSkillCatalogPath(msg("c1", REAL_CATALOG)), "durable/skill-catalog"); + const instructions = "\x3csystem-reminder\x3e\nworkspace instructions\n\nInstructions from: AGENTS.md\n\n# rules\nalways lint\n\x3c/system-reminder\x3e"; + assert.equal(dshWorkspaceInstructionsPath(msg("i1", instructions)), "durable/agents-md"); +}); + +test("#7153 a human paste / relay report that quotes a durable marker is NOT a carrier", () => { + assert.equal(dshSkillCatalogPath(msg("h1", HUMAN_PROMPT_QUOTING_CATALOG)), undefined, "a human prompt quoting the catalog keeps its anchor role"); + assert.equal(dshSkillCatalogPath(msg("h2", RELAY_QUOTING_CATALOG)), undefined, "a relay report quoting the catalog must not take the catalog identity"); + assert.equal(dshSkillCatalogPath(msg("h3", "why does this say Instructions from: AGENTS.md?")), undefined); + assert.equal( + dshWorkspaceInstructionsPath(msg("h4", "I pasted this from another repo:\n\nInstructions from: /other/AGENTS.md\n\nwhy does that rule exist?")), + undefined, + "a quoted rule file is ordinary history, not the baseline", + ); + assert.equal(dshSkillCatalogPath(msg("h5", REAL_CATALOG, "assistant")), undefined, "#2766 assistant gate survives"); +}); + +test("#7153 every tool-job notice emitter shape still matches, ordinary English does not", () => { + // packages/jobs/tool-jobs/src/index.ts: inline / truncated / compact. + const inline = 'background job bash-abcd (bash: "true") finished [status: completed, exit code: 0]. Read its output with job_output.'; + const truncated = "background job bash-abcd\n[notice truncated]\nDone; job_output."; + const compact = "background job bash-abcd\nDone; job_output."; + assert.equal(dshToolJobNoticePath(msg("j1", inline)), "oneshot/tool-job-notice"); + assert.equal(dshToolJobNoticePath(msg("j2", truncated)), "oneshot/tool-job-notice"); + assert.equal(dshToolJobNoticePath(msg("j3", compact)), "oneshot/tool-job-notice"); + + assert.equal( + dshToolJobNoticePath(msg("p1", "background jobs keep failing with exit code 1 — please read the logs and fix the root cause")), + undefined, + "a human question about background jobs keeps the anchor role", + ); + assert.equal(dshToolJobNoticePath(msg("p2", "background job keeps failing, why?")), undefined); + assert.equal(dshToolJobNoticePath(msg("p3", inline, "assistant")), undefined); +}); + +test("#7153 every settlement ending still matches, prose about a subagent does not", () => { + const endings = [ + "finished and will do no further work unless you send it more.", + "finished. It cannot receive follow-up messages.", + "was stopped before it finished.", + "ran out of room before it finished.", + "declined the task.", + "failed before it finished.", + ]; + for (const ending of endings) { + assert.equal( + dshSubagentSettledPath(msg("s1", `Background subagent session-a1b2 ${ending}`)), + "oneshot/subagent-settled", + `emitter ending must keep matching: ${ending}`, + ); + } + assert.equal(dshSubagentSettledPath(msg("p1", "Background subagent session-a1b2 is stuck again — why?")), undefined); + assert.equal(dshSubagentSettledPath(msg("p2", "Background subagents are handy for parallel work")), undefined); +}); + +test("#7153 the live request keeps the Rule-3 anchor and stays out of the compressible zone", () => { + const messages: CoreMessage[] = [ + msg("m1", "earlier question about the build"), + msg("m2", "answer one ".repeat(120), "assistant"), + msg("m3", "background jobs keep failing with exit code 1 — please read the logs and fix the root cause"), + ]; + for (let i = 4; i <= 14; i++) messages.push(msg(`m${i}`, `working step ${i} ` + "payload ".repeat(120), "assistant")); + + const state = createInitialState(); + state.messageRefs = assignRefs(messages, { existing: state.messageRefs, nextIndex: 1 }).map; + const config = withDurableProtection( + defaultConfig(200000, { preserveRecentMessages: 2, preserveRecentTokens: 0 }), + durableMessageToolPaths.dsh!, + ); + + assert.equal(lastUserAnchor(messages, new Set(), config)?.id, "m3", "the live request is the anchor"); + const protectedRefs = computeProtectedRefs(messages, state, config); + assert.ok(protectedRefs.has("m00003"), "Rule 3 protects the live request"); + const ranges = buildCompressibleRanges(messages, state, config, protectedRefs); + const covering = ranges.compressible.filter((r) => r.startRef <= "m00003" && r.endRef >= "m00003"); + assert.deepEqual(covering, [], "no compressible range may contain the live request"); +}); + +test("#7153 a real notice after the request still loses the anchor role (the exclusion keeps working)", () => { + const messages: CoreMessage[] = [ + msg("m1", "earlier question"), + msg("m2", "answer two ".repeat(120), "assistant"), + msg("m3", "please summarise the build output"), + msg("m4", 'background job bash-abcd (bash: "make") finished [status: completed, exit code: 0]. Read its output with job_output.'), + ]; + const config = withDurableProtection( + defaultConfig(200000, { preserveRecentMessages: 2, preserveRecentTokens: 0 }), + durableMessageToolPaths.dsh!, + ); + assert.equal(lastUserAnchor(messages, new Set(), config)?.id, "m3", "the machine notice never displaces the human request"); + assert.equal(dshToolJobNoticePath(messages[3]!), "oneshot/tool-job-notice", "the notice is still recognised as a carrier"); +}); From 27289a0c846ac36dd3b969c8e17b31d0bbf0b68e Mon Sep 17 00:00:00 2001 From: resec Date: Wed, 18 Nov 2026 20:28:23 +0800 Subject: [PATCH 07/12] fix(dsh): stand the host-plane /compact command down on a bili-owned lane (#7154) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit /compact drives the host's own compaction call — the same envelope bili's wire guard refuses unconditionally (#1729) — so on a bili-owned lane the command has no outcome except failure. dsh reports that failure as "Compaction could not produce a useful summary", blaming the model for a policy refusal, and every retry fails identically because the refusal is terminal. Disable the command entry in both patch carriers (persistent bundle + launcher overlay) with the same static semantics as the adjacent compaction-basic auto:false row. Escape hatch: a profile-level `disabled: false` for the same id brings it back; invocations are still refused at the wire. --- dsh.bundle.patch.yml | 11 ++++ src/launcher.ts | 2 +- ...issue7154-command-compact-disabled.test.ts | 58 +++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 tests/issue7154-command-compact-disabled.test.ts diff --git a/dsh.bundle.patch.yml b/dsh.bundle.patch.yml index d7b815f249..2b0327ab5d 100644 --- a/dsh.bundle.patch.yml +++ b/dsh.bundle.patch.yml @@ -26,6 +26,17 @@ - id: compaction-basic config: auto: false +# #7154: `/compact` drives the SAME host-side compaction call bili's wire +# guard refuses unconditionally (#1729) — on a lane bili owns the command has +# no outcome except failure, and dsh reports that failure as "Compaction could +# not produce a useful summary", which blames the model for a policy refusal. +# Stand it down with the same static semantics as the `auto: false` row above +# (a kill-switched bili-native does not bring it back either). To get the +# command back, set `disabled: false` for this id in the profile's own +# cordis.patch.yml — be aware every invocation is still refused at the wire. +# Keep in sync with the launcher overlay (writeDshAcpPatch). +- id: command-compact + disabled: true # #3354: keep in sync with writeDshAcpPatch in src/launcher.ts — entry inject # lists are REPLACED by patches, not concatenated; if a future dsh ships its # own inject list on these entries, merge manually in BOTH layers. diff --git a/src/launcher.ts b/src/launcher.ts index b09d4a80ad..008e1ecfd6 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -3611,7 +3611,7 @@ export function writeDshAcpPatch(dshHome: string, entryName?: string): string | writeOverlayFileAtomic( dir, ".bili-acp.patch.yml", - `- insert:\n - id: bili-native\n name: ${pluginUrl}\n - id: bili-compaction-gate\n name: ${gateUrl}\n- id: compaction-basic\n config:\n auto: false\n# #3354/#3662: keep in sync with dsh.bundle.patch.yml — web-profile presets run their own compaction-basic in nested rows no patch layer can address by id (#1772); these gates make them wait for the load-time overlay the always-on bili-compaction-gate entry registers (vendored from deshark42/dsh-compaction-basic-disable, MIT)\n${compactionDisablePatchRows()}# #2605: keep in sync with dsh.bundle.patch.yml — a patch replaces the row's whole config, so every default code is restated\n- id: llm-deepseek\n config:\n retryPolicy:\n mode: normal\n retryableCodes: [EMPTY_RESPONSE, RATE_LIMIT, SERVER, TIMEOUT, TRANSPORT, MALFORMED_RESPONSE]\n- id: llm-deepseek-account\n config:\n retryPolicy:\n mode: normal\n retryableCodes: [EMPTY_RESPONSE, RATE_LIMIT, SERVER, TIMEOUT, TRANSPORT, MALFORMED_RESPONSE]\n`, + `- insert:\n - id: bili-native\n name: ${pluginUrl}\n - id: bili-compaction-gate\n name: ${gateUrl}\n- id: compaction-basic\n config:\n auto: false\n# #7154: keep in sync with dsh.bundle.patch.yml — /compact drives the host compaction call the #1729 wire guard refuses unconditionally, so on a bili-owned lane it can only fail (and dsh blames the model for it)\n- id: command-compact\n disabled: true\n# #3354/#3662: keep in sync with dsh.bundle.patch.yml — web-profile presets run their own compaction-basic in nested rows no patch layer can address by id (#1772); these gates make them wait for the load-time overlay the always-on bili-compaction-gate entry registers (vendored from deshark42/dsh-compaction-basic-disable, MIT)\n${compactionDisablePatchRows()}# #2605: keep in sync with dsh.bundle.patch.yml — a patch replaces the row's whole config, so every default code is restated\n- id: llm-deepseek\n config:\n retryPolicy:\n mode: normal\n retryableCodes: [EMPTY_RESPONSE, RATE_LIMIT, SERVER, TIMEOUT, TRANSPORT, MALFORMED_RESPONSE]\n- id: llm-deepseek-account\n config:\n retryPolicy:\n mode: normal\n retryableCodes: [EMPTY_RESPONSE, RATE_LIMIT, SERVER, TIMEOUT, TRANSPORT, MALFORMED_RESPONSE]\n`, ); const file = path.join(dir, ".bili-acp.patch.yml"); try { diff --git a/tests/issue7154-command-compact-disabled.test.ts b/tests/issue7154-command-compact-disabled.test.ts new file mode 100644 index 0000000000..09f9018473 --- /dev/null +++ b/tests/issue7154-command-compact-disabled.test.ts @@ -0,0 +1,58 @@ +// #7154: on a bili-owned lane, `/compact` can only fail. The command drives +// the host's own compaction call (compaction-basic → COMPACTION_INSTRUCTION), +// the same envelope bili's wire guard refuses unconditionally (#1729), and +// dsh reports the refusal as "Compaction could not produce a useful summary" +// — a policy decision surfaced as a model failure, with no working retry. +// Both patch carriers therefore stand the command down, with the same static +// semantics as the `compaction-basic: auto:false` row that precedes them +// (a kill-switched bili-native does not bring it back either). This test pins +// BOTH carriers, because they are the only two ways the lane gets mounted: +// the persistent bundle (dsh.bundle.patch.yml) and the launcher's --patch +// overlay (writeDshAcpPatch). +import test from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; +import { writeDshAcpPatch } from "../src/launcher.ts"; + +const TESTS_DIR = path.dirname(fileURLToPath(import.meta.url)); + +const AUTO_OFF_ROW = "- id: compaction-basic\n config:\n auto: false\n"; +const COMPACT_COMMAND_ROW = "- id: command-compact\n disabled: true\n"; + +test("#7154 the bundle layer stands the host-plane /compact command down", () => { + const bundle = fs.readFileSync(path.join(TESTS_DIR, "..", "dsh.bundle.patch.yml"), "utf8").replaceAll("\r\n", "\n"); + assert.ok(bundle.includes(COMPACT_COMMAND_ROW), "dsh.bundle.patch.yml must disable command-compact"); + assert.ok(bundle.includes(AUTO_OFF_ROW), "the existing auto:false row must stay (this row follows the same static semantics)"); + // The row must be a top-level patch entry, never part of the insert list: + // an insert would mount a SECOND command-compact beside the host's. + const insertStart = bundle.indexOf("- insert:"); + const insertEnd = bundle.indexOf("\n- id: compaction-basic"); + assert.ok(insertStart !== -1 && insertEnd > insertStart, "insert block and the compaction-basic row must both exist"); + assert.ok( + !bundle.slice(insertStart, insertEnd).includes("command-compact"), + "command-compact must not be inserted (that would leave the host's own entry live)", + ); +}); + +test("#7154 the launcher overlay stands the same command down (both carriers agree)", () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-cc-")); + try { + const file = writeDshAcpPatch(home, "billion-context"); + assert.ok(file, "overlay patch must be written"); + const overlay = fs.readFileSync(file!, "utf8").replaceAll("\r\n", "\n"); + assert.ok(overlay.includes(COMPACT_COMMAND_ROW), "the launcher overlay must disable command-compact too"); + assert.ok(overlay.includes(AUTO_OFF_ROW), "the auto:false row must stay in the overlay"); + assert.ok( + overlay.indexOf(COMPACT_COMMAND_ROW) > overlay.indexOf(AUTO_OFF_ROW), + "the two rows stay adjacent and ordered (auto:false first) so a reader sees them as one decision", + ); + // The overlay must not disable the entry by inserting a second one. + assert.ok(!/- insert:\n(?: {4}- .*\n)* {4}- id: command-compact/.test(overlay), "command-compact must not be inserted"); + } finally { + fs.rmSync(home, { recursive: true, force: true }); + fs.rmSync(`${home}-bili`, { recursive: true, force: true }); + } +}); From b2f669401c05c29740275064d2154a37305e7861 Mon Sep 17 00:00:00 2001 From: resec Date: Wed, 18 Nov 2026 20:32:32 +0800 Subject: [PATCH 08/12] fix(dsh): decide the launcher overlay from the profile being booted (#7155) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit dshNativeInstalled() answered for the whole machine ('any profile has the bundle'), and the launcher used that answer for THIS launch. On a machine where a sibling profile has the bundle but the booted one does not — a scratch profile created after 'bili plugin install dsh', a profile whose dep was removed by hand — the overlay was skipped and the session ran with zero bili entries: no ACP tools, no takeover, native compaction armed (which the wire guard then refuses turn after turn), while the launch note described the other profile. Take an optional profile argument (omitting it keeps the machine-wide answer for status/tests), parse the booted profile from the dsh argv the way dsh does (bare first argument abbreviates --profile), and say plainly when the booted profile lacks the bundle. --- src/dsh-channel.ts | 25 ++++++ src/launcher.ts | 15 +++- src/plugin-install.ts | 25 ++++-- ...dsh-native-installed-profile-scope.test.ts | 89 +++++++++++++++++++ 4 files changed, 145 insertions(+), 9 deletions(-) create mode 100644 tests/issue7155-dsh-native-installed-profile-scope.test.ts diff --git a/src/dsh-channel.ts b/src/dsh-channel.ts index 633b147b2a..3dbb7c2017 100644 --- a/src/dsh-channel.ts +++ b/src/dsh-channel.ts @@ -184,6 +184,31 @@ export function dshProfileDependsOnBili(profileDir: string): boolean { return dshProfileDepSpec(profileDir) !== undefined; } +/** #7155: the profile a `dsh` argv boots, mirroring dsh's own parser — a bare + * first argument abbreviates `--profile ` (except the `plugin` + * subcommand, which takes its own required flag), otherwise the explicit + * `--profile ` / `--profile=` wins. undefined when the argv names + * no profile; dsh itself then errors out, so callers fall back to the + * machine-wide answer. The launcher needs this to ask "is THIS profile + * installed" instead of "does any profile on this machine have a copy". */ +export function dshProfileFromArgs(args: readonly string[]): string | undefined { + const first = args[0]; + if (first !== undefined && !first.startsWith("-") && first !== "plugin") return first; + for (let i = 0; i < args.length; i++) { + const arg = args[i]!; + if (arg === "--") break; + if (arg === "--profile") { + const value = args[i + 1]; + return value !== undefined && value !== "" && !value.startsWith("-") ? value : undefined; + } + if (arg.startsWith("--profile=")) { + const value = arg.slice("--profile=".length); + return value !== "" ? value : undefined; + } + } + return undefined; +} + /** #2894: converge this profile's declared billion-context spec to the version * bili just installed IN PLACE (the dsh desktop lane — #1575). The desktop * copy's node_modules entry is swapped by bili's own tarball installer while diff --git a/src/launcher.ts b/src/launcher.ts index b09d4a80ad..3363e22847 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -61,6 +61,7 @@ import { stateDir } from "./paths.js"; import { TWIN_ADOPT_EXIT_CODE } from "./listen-policy.js"; import { tcpConnectable } from "./liveness.js"; import { selfPackageRoot, isBiliPiEntry, ompPluginLoadedFrom, dshNativeInstalled, claudeNativeInstalled, piPackageStrings } from "./plugin-install.js"; +import { dshProfileFromArgs } from "./dsh-channel.js"; import { applyOmpFirstEventTimeout, applyOmpMessageThreadsOff, envMillis } from "./agent/native-bootstrap.js"; import { logHostEvent } from "./agent/host-log.js"; import { compactionDisablePatchRows } from "./agent/dsh-compaction-disable.js"; @@ -7280,8 +7281,20 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} const prevNodePath = env.NODE_PATH; env.NODE_PATH = prevNodePath !== undefined && prevNodePath.length > 0 ? `${dshNm}${path.delimiter}${prevNodePath}` : dshNm; let dshAcpPatch: string | undefined; - if (!dshNativeInstalled()) { + // #7155: ask about the profile this run is about to boot, not the + // machine. The machine-wide form made a bundle-less profile look + // installed whenever a sibling profile had the bundle: the overlay was + // skipped and the session ran with ZERO bili entries (no compression + // tools, no takeover, native compaction armed) while the note below + // described the other profile. + const targetProfile = dshProfileFromArgs(params.clientArgs); + if (!dshNativeInstalled(process.env, targetProfile)) { dshAcpPatch = writeDshAcpPatch(dshHomeDir, dshPluginEntry(dshOverlayHome ?? dshHomeDir)); + if (targetProfile !== undefined && dshNativeInstalled()) { + console.error( + `bili: profile "${targetProfile}" does not install billion-context (another profile on this machine does) — this run mounts the launcher overlay, so compression works for this session only. Install it permanently with: dsh plugin --profile ${targetProfile} add billion-context`, + ); + } } else { // Unconditional (#3666): the #2028 allowDshCompaction opt-out was // removed, so a native install always leaves dsh compaction armed diff --git a/src/plugin-install.ts b/src/plugin-install.ts index 3e46c60eed..a5d00241e5 100644 --- a/src/plugin-install.ts +++ b/src/plugin-install.ts @@ -1696,20 +1696,29 @@ function dshStatus(): string { return "not installed"; } -/** True when any profile carries the persistent native install — either the - * bundle-channel dep or a pre-unification managed block. The `bili dsh` - * launcher consults this before adding its own --patch overlay: cordis - * rejects duplicate loader entry ids across layers, so a second - * `id: bili-native` insert would hard-fail dsh boot whenever the persistent - * install is present. */ -export function dshNativeInstalled(env: NodeJS.ProcessEnv = process.env): boolean { +/** True when the given profile (or, with no profile, ANY profile) carries the + * persistent native install — either the bundle-channel dep or a + * pre-unification managed block. The `bili dsh` launcher consults this + * before adding its own --patch overlay. + * + * #7155: the launcher must ask about the profile it is ABOUT TO BOOT, not + * the machine. The old any-profile form made a bundle-less profile look + * installed whenever a sibling profile had the bundle: the overlay was + * skipped, the session ran with zero bili entries (no compression tools, no + * takeover, native compaction armed) and the launch note described the OTHER + * profile's state. Omitting `profile` keeps the machine-wide answer for the + * callers that genuinely need it (status/tests). */ +export function dshNativeInstalled(env: NodeJS.ProcessEnv = process.env, profile?: string): boolean { let dirs: string[]; try { dirs = dshProfileDirs(env); } catch { return false; } - return dirs.some((dir) => dshBundleInstalled(dir) || dshHasLegacyManagedBlock(dir)); + const scoped = profile === undefined || profile === "" + ? dirs + : dirs.filter((dir) => path.basename(dir) === profile); + return scoped.some((dir) => dshBundleInstalled(dir) || dshHasLegacyManagedBlock(dir)); } // — kimi ———————————————————————————————————————————————————————————————— diff --git a/tests/issue7155-dsh-native-installed-profile-scope.test.ts b/tests/issue7155-dsh-native-installed-profile-scope.test.ts new file mode 100644 index 0000000000..1120169db3 --- /dev/null +++ b/tests/issue7155-dsh-native-installed-profile-scope.test.ts @@ -0,0 +1,89 @@ +// #7155: `bili dsh --profile X` used the MACHINE-wide install answer +// (`dshNativeInstalled()` = "any profile has the bundle") to decide whether to +// mount its own --patch overlay. On a machine where a sibling profile has the +// bundle but X does not (a scratch profile created after `bili plugin install +// dsh`, a profile whose dep was removed by hand), the overlay was skipped and +// the launch note described the OTHER profile — so the session ran with zero +// bili entries: no ACP compression tools, no takeover, and native compaction +// armed (which the wire guard then refuses turn after turn). +// Fix: ask about the profile being booted, parsing `--profile` the way dsh +// itself does, and say plainly when the profile lacks the bundle. +import test from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { dshNativeInstalled } from "../src/plugin-install.ts"; +import { dshProfileFromArgs } from "../src/dsh-channel.ts"; + +function withHome(home: string, fn: () => T): T { + const prev = process.env.DSH_HOME; + process.env.DSH_HOME = home; + try { + return fn(); + } finally { + if (prev === undefined) delete process.env.DSH_HOME; + else process.env.DSH_HOME = prev; + } +} + +function profile(dir: string, bundles?: string[]): void { + fs.mkdirSync(dir, { recursive: true }); + if (bundles !== undefined) { + fs.writeFileSync(path.join(dir, "package.json"), JSON.stringify({ dsh: { profile: { bundles } } })); + } +} + +test("#7155 dshNativeInstalled(profile) answers for THAT profile; omitting it stays machine-wide", () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-7155-")); + try { + withHome(home, () => { + profile(path.join(home, "profiles", "installed"), ["@deepseek-ai/dsh-base", "billion-context"]); + profile(path.join(home, "profiles", "scratch"), ["@deepseek-ai/dsh-base"]); + profile(path.join(home, "profiles", "empty")); + + assert.equal(dshNativeInstalled(), true, "machine-wide answer unchanged (backward compatible)"); + assert.equal(dshNativeInstalled(process.env, "installed"), true); + assert.equal(dshNativeInstalled(process.env, "scratch"), false, "the booted profile has no bundle"); + assert.equal(dshNativeInstalled(process.env, "empty"), false); + assert.equal(dshNativeInstalled(process.env, "never-created"), false, "an unknown profile cannot be installed"); + assert.equal(dshNativeInstalled(process.env, ""), true, "empty profile id falls back to the machine-wide answer"); + }); + } finally { + fs.rmSync(home, { recursive: true, force: true }); + } +}); + +test("#7155 dshNativeInstalled(profile) honours a legacy managed block and a missing profiles root", () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-7155-legacy-")); + try { + withHome(home, () => { + assert.equal(dshNativeInstalled(process.env, "anything"), false, "no profiles root at all"); + const legacy = path.join(home, "profiles", "old"); + profile(legacy); + fs.writeFileSync(path.join(legacy, "cordis.patch.yml"), "# bili begin (managed by billion-context — `bili plugin install dsh`)\n- insert:\n - id: bili-native\n name: /opt/bili\n# bili end\n"); + assert.equal(dshNativeInstalled(process.env, "old"), true, "a pre-unification managed block counts for that profile"); + assert.equal(dshNativeInstalled(process.env, "other"), false); + }); + } finally { + fs.rmSync(home, { recursive: true, force: true }); + } +}); + +test("#7155 dshProfileFromArgs mirrors dsh's own --profile parsing", () => { + // A bare first argument abbreviates --profile (dsh's expansion rule). + assert.equal(dshProfileFromArgs(["web"]), "web"); + assert.equal(dshProfileFromArgs(["headless", "run the tests"]), "headless"); + assert.equal(dshProfileFromArgs(["--profile", "tui"]), "tui"); + assert.equal(dshProfileFromArgs(["--profile=tui"]), "tui"); + assert.equal(dshProfileFromArgs(["--no-open", "--profile", "tui", "--port", "0"]), "tui"); + // `plugin` takes its own required flag; a positional after it is not a profile. + assert.equal(dshProfileFromArgs(["plugin", "--profile", "x", "add", "billion-context"]), "x"); + assert.equal(dshProfileFromArgs(["plugin", "add", "billion-context"]), undefined); + // No profile named at all (dsh errors out itself; the caller falls back). + assert.equal(dshProfileFromArgs([]), undefined); + assert.equal(dshProfileFromArgs(["--dump-config"]), undefined); + assert.equal(dshProfileFromArgs(["--profile"]), undefined); + // `--` terminates launcher flags: what follows belongs to the app. + assert.equal(dshProfileFromArgs(["--", "--profile", "nope"]), undefined); +}); From c3172605faf864d10fe5c561af5f6c91706b6aba Mon Sep 17 00:00:00 2001 From: Edmond Date: Wed, 18 Nov 2026 21:58:35 +0800 Subject: [PATCH 09/12] test(dsh): pin the two remaining bare-marker guard fixtures to the emitter shape (#7153) The #7153 carrier gates require the complete frame around the workspace-instructions label, but two runtime fixtures still carried the bare label, so their guard message stopped classifying as durable/agents-md and was no longer pinned: the wire handed out a real ref where null was asserted. - tests/plugin-fork.test.ts (#2676): the durable-guard history row now carries the frame dsh-agent-instructions emits. - tests/session-continuity-proof.test.ts (#629/#2620/#2625/#2676): GUARD_TEXT likewise; the scenario-B fork-copy assertions are unchanged. CI was red on the one assertion in each file (ubuntu 22/3, ubuntu 24/3, windows 22/1, windows 22/4). --- tests/plugin-fork.test.ts | 5 ++++- tests/session-continuity-proof.test.ts | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/plugin-fork.test.ts b/tests/plugin-fork.test.ts index 8e555df402..049b2e0c34 100644 --- a/tests/plugin-fork.test.ts +++ b/tests/plugin-fork.test.ts @@ -488,7 +488,10 @@ test("HTTP dsh-lane durable guards keep plain-text snapshots, forks and revision try { const history = [ { role: "user", content: "Synthetic snapshot test" }, - { role: "user", content: `Instructions from: /repo/AGENTS.md\n# repo rules\nbehave` }, + // #7153: the carrier must look like what dsh-agent-instructions emits — + // the label inside the complete \x3csystem-reminder\x3e frame (a bare label is + // ordinary history now, so it would no longer be pinned). + { role: "user", content: `\x3csystem-reminder\x3e\nInstructions from: /repo/AGENTS.md\n# repo rules\nbehave\n\x3c/system-reminder\x3e` }, { role: "assistant", content: "Starting" }, ...Array.from({ length: 4 }, (_, i) => ({ role: i % 2 ? "assistant" : "user", content: `tail ${i}` })), ]; diff --git a/tests/session-continuity-proof.test.ts b/tests/session-continuity-proof.test.ts index 31c74b7cf6..872872acef 100644 --- a/tests/session-continuity-proof.test.ts +++ b/tests/session-continuity-proof.test.ts @@ -64,7 +64,10 @@ const hash = (v: unknown): string => createHash("sha256").update(JSON.stringify( const SYSTEM_A = "You are DeepSeek Harness, the main coding agent.\nModel: GLM-4.6.\nWorkspace: /tmp."; const SYSTEM_B = "You are DeepSeek Harness, the main coding agent.\nModel: GLM-5.3.\nWorkspace: /tmp."; -const GUARD_TEXT = "Instructions from: /repo/AGENTS.md\n# repo rules\nbehave conservatively and keep diffs minimal."; +// #7153: the durable baseline carrier is classified on the shape dsh-agent-instructions +// actually emits — the complete \x3csystem-reminder\x3e frame around the section label. +// A bare label is now ordinary history (a human paste must not be pinned). +const GUARD_TEXT = "\x3csystem-reminder\x3e\nInstructions from: /repo/AGENTS.md\n# repo rules\nbehave conservatively and keep diffs minimal.\n\x3c/system-reminder\x3e"; const FILLER = "the quick brown fox jumps over the lazy dog, again and again, folding context away. "; From 57eea20ff6097d5cd0b8fa92f5adba5d72699086 Mon Sep 17 00:00:00 2001 From: resec Date: Wed, 18 Nov 2026 22:28:00 +0800 Subject: [PATCH 10/12] fix(dsh): release the fast-path origin when a pinned attach target is proven dead (#7176) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit apply() primes the interceptor's origin plane with the launcher-provided attach origin before any health verdict exists — deliberate, so a normal boot does not pay a probe. When the verdict then lands and the target is dead, verifyAttachAndRecover's pinned branch cleared the tool base but left that plane bound. readyOrigin() short-circuits on it, so every later model request was rewritten onto the dead origin and failed; the documented direct (uncompressed) degradation was unreachable for the rest of the process, and the user saw only a bare transport/503 per turn. The sibling spawnFallback() tail already drops the same field (together with the preset unfreeze it needs because it then spawns). This branch must NOT spawn — per #1365 a second instance would serve tools while the model channel stayed pinned, and every bili tool call would 404 — so only the plane is cleared: the preset stays, maybeRetry keeps re-probing, and traffic degrades to direct until the target answers again. One log line records the transition. Measured with a local Messages mock on the dsh lane: with the origin dead, three consecutive turns failed TRANSPORT and the upstream saw zero requests before this change; after it, requests that follow the verdict take the no-origin direct lane. The first request of a process that booted against an already-dead target still races the verdict (fails once) — proving the origin before priming the plane, or giving this lane the resolver lanes' per-request liveness check, are the remaining options and are not part of this change. --- src/agent/dsh-native.ts | 18 +++ ...sue7176-dead-attach-origin-release.test.ts | 115 ++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 tests/issue7176-dead-attach-origin-release.test.ts diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index 5d83069fb5..7e4634fd10 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -831,6 +831,24 @@ export async function verifyAttachAndRecover(attachOrigin: string): Promise(fn: () => Promise): Promise { + const saved: Record = {}; + for (const k of ENV_KEYS) saved[k] = process.env[k]; + process.env.BILLION_CONTEXT_ATTACH = DEAD; + process.env.BILLION_CONTEXT_PROXY = DEAD; + // the verdict must land inside the test, not after the suite times out + process.env.BILI_ATTACH_HEALTH_DEADLINE_MS = "300"; + process.env.BILI_ATTACH_EVIDENCE_GRACE_MS = "10"; + try { + return await fn(); + } finally { + for (const k of ENV_KEYS) { + if (saved[k] === undefined) delete process.env[k]; + else process.env[k] = saved[k]!; + } + } +} + +test("#7176 a proven-dead pinned attach target releases the fast-path origin plane (no second instance)", async () => { + let spawnCalls = 0; + _setSpawnForTest(async () => { + spawnCalls += 1; + return "http://127.0.0.1:18998"; + }); + try { + await withDeadAttachEnv(async () => { + _resetRegisterForTest(DEAD); + const state = _nativeStateForTest(); + // apply() primes the fast path with the attach origin before the + // health verdict exists — this is the state under test + state.origin = DEAD; + + const landed = await verifyAttachAndRecover(DEAD); + assert.equal(landed, undefined, "a dead pinned target resolves to no origin"); + assert.equal(spawnCalls, 0, "#1365: never spawn a second instance behind a pinned channel"); + + assert.equal(state.origin, undefined, "the dead origin must leave the fast path"); + assert.equal(await readyOrigin(state), undefined, "later requests take the no-origin direct lane"); + assert.equal(await readyOrigin(state), undefined, "and stay there for the rest of the process"); + }); + } finally { + _setSpawnForTest(undefined); + _resetRegisterForTest(undefined); + } +}); + +test("#7176 a live pinned attach target keeps the fast path bound", async () => { + // control arm: the release above must not fire when the target answers. + // A local server speaking the plugin version endpoint stands in for the proxy. + const http = await import("node:http"); + let versionRequests = 0; + const server = http.createServer((req, res) => { + if (req.url === "/__bili/plugin/manifest") { + versionRequests += 1; + // a NEWER version so the #3544 rotation path stays out of this arm + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ version: "999.0.0", tools: {} })); + return; + } + res.writeHead(404); + res.end(); + }); + const origin = await new Promise((resolve) => { + server.listen(0, "127.0.0.1", () => { + const addr = server.address() as { port: number }; + resolve(`http://127.0.0.1:${addr.port}`); + }); + }); + _setSpawnForTest(async () => { + throw new Error("a healthy pinned target must never spawn"); + }); + try { + const saved: Record = {}; + for (const k of ENV_KEYS) saved[k] = process.env[k]; + process.env.BILLION_CONTEXT_ATTACH = origin; + process.env.BILLION_CONTEXT_PROXY = origin; + process.env.BILI_ATTACH_HEALTH_DEADLINE_MS = "2000"; + try { + _resetRegisterForTest(origin); + const state = _nativeStateForTest(); + state.origin = origin; + const landed = await verifyAttachAndRecover(origin); + assert.equal(landed, origin, "healthy target attaches as-is"); + assert.equal(state.origin, origin, "the live origin stays on the fast path"); + assert.ok(versionRequests >= 1, "the manifest/version probe ran"); + } finally { + for (const k of ENV_KEYS) { + if (saved[k] === undefined) delete process.env[k]; + else process.env[k] = saved[k]!; + } + } + } finally { + _setSpawnForTest(undefined); + _resetRegisterForTest(undefined); + server.close(); + } +}); From 10d1f1ff1fc728893f4fd8136108a11f59b66c84 Mon Sep 17 00:00:00 2001 From: resec Date: Wed, 18 Nov 2026 22:49:10 +0800 Subject: [PATCH 11/12] test(dsh): register the #7176 suite in LIVE_FILES and make its dead origin hermetic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit run-ci-tests.mjs refuses any .test.ts matching SPAWN_RE that is not in scripts/live-files.mjs, and this suite's own identifier (_setSpawnForTest) plus the header comment tripped it — every test shard failed before a single test ran. Registered at the end of the table (slot = index drives each file's zone port band, so entries are append-only) with the concurrency note the table asks for: no shared port, no child process, both arms reset the module-level origin plane in finally. While here, the dead arm no longer hardcodes a port. It binds :0, reads the port, closes it and reuses the number, so the 'nothing listens here' premise is guaranteed at runtime instead of assuming a fixed number stays free on a shard runner, and no zone band is involved. --- scripts/live-files.mjs | 8 ++ ...sue7176-dead-attach-origin-release.test.ts | 119 +++++++++--------- 2 files changed, 70 insertions(+), 57 deletions(-) diff --git a/scripts/live-files.mjs b/scripts/live-files.mjs index 30c4a60135..469a3e141c 100644 --- a/scripts/live-files.mjs +++ b/scripts/live-files.mjs @@ -150,6 +150,14 @@ export const LIVE_FILES = [ // One Linux-only Node child reads fixture system CAs and exits within 10s; // no child ports or network I/O. In-process HTTPS fixtures use listen(0). "tests/upstream-ca-default-trust.test.ts", + // #7176: the pinned-attach decision test. Every origin is a listen(0) + // pick (the dead arm binds :0, reads the port, closes it and requires + // ECONNREFUSED there), so no fixed port and no zone band is involved; no + // child process is spawned either — the entry is for the SPAWN_RE + // tripwire alone (its hit is the identifier "spawn-fallback" wording plus + // _setSpawnForTest). Both arms reset the module-level origin plane in + // `finally`, so rows cannot leak into each other. + "tests/issue7176-dead-attach-origin-release.test.ts", ]; /** Band width per live file: the zone ladder drifts +1 per EADDRINUSE retry; diff --git a/tests/issue7176-dead-attach-origin-release.test.ts b/tests/issue7176-dead-attach-origin-release.test.ts index e6745127d6..5ded356c15 100644 --- a/tests/issue7176-dead-attach-origin-release.test.ts +++ b/tests/issue7176-dead-attach-origin-release.test.ts @@ -3,80 +3,91 @@ // state.origin (native-intercept), so a dead origin left bound there rewrites // every later model request onto it — the documented "goes direct // (uncompressed)" degradation is unreachable and the session fails turn after -// turn until the proxy returns. spawnFallback() already clears the field; the -// pinned branch must do the same WITHOUT spawning (#1365: a second instance -// serves tools while the model channel stays pinned, so every bili tool call -// would 404). +// turn until the proxy returns. The spawn-fallback tail already clears the +// field; the pinned branch must do the same WITHOUT spawning (#1365: a second +// instance serves tools while the model channel stays pinned, so every bili +// tool call would 404). import assert from "node:assert/strict"; +import http from "node:http"; import test from "node:test"; import { verifyAttachAndRecover, _nativeStateForTest, _resetRegisterForTest, _setSpawnForTest } from "../src/agent/dsh-native.ts"; import { readyOrigin } from "../src/agent/native-intercept.ts"; -const DEAD = "http://127.0.0.1:18999"; // nothing listens here const ENV_KEYS = ["BILLION_CONTEXT_ATTACH", "BILLION_CONTEXT_PROXY", "BILI_ATTACH_HEALTH_DEADLINE_MS", "BILI_ATTACH_EVIDENCE_GRACE_MS"] as const; -async function withDeadAttachEnv(fn: () => Promise): Promise { +/** A loopback origin nothing listens on: bind :0, read the port, close, reuse + * the number. Hermetic — no fixed port to collide with a zone band or another + * suite in the same shard. */ +async function deadOrigin(): Promise { + const server = http.createServer(); + const port = await new Promise((resolve) => { + server.listen(0, "127.0.0.1", () => resolve((server.address() as { port: number }).port)); + }); + await new Promise((resolve) => server.close(() => resolve())); + return `http://127.0.0.1:${port}`; +} + +function saveEnv(): Record { const saved: Record = {}; for (const k of ENV_KEYS) saved[k] = process.env[k]; - process.env.BILLION_CONTEXT_ATTACH = DEAD; - process.env.BILLION_CONTEXT_PROXY = DEAD; - // the verdict must land inside the test, not after the suite times out - process.env.BILI_ATTACH_HEALTH_DEADLINE_MS = "300"; - process.env.BILI_ATTACH_EVIDENCE_GRACE_MS = "10"; - try { - return await fn(); - } finally { - for (const k of ENV_KEYS) { - if (saved[k] === undefined) delete process.env[k]; - else process.env[k] = saved[k]!; - } + return saved; +} + +function restoreEnv(saved: Record): void { + for (const k of ENV_KEYS) { + if (saved[k] === undefined) delete process.env[k]; + else process.env[k] = saved[k]!; } } test("#7176 a proven-dead pinned attach target releases the fast-path origin plane (no second instance)", async () => { + const dead = await deadOrigin(); + const saved = saveEnv(); + // the verdict must land inside the test, not after the suite times out + process.env.BILLION_CONTEXT_ATTACH = dead; + process.env.BILLION_CONTEXT_PROXY = dead; + process.env.BILI_ATTACH_HEALTH_DEADLINE_MS = "300"; + process.env.BILI_ATTACH_EVIDENCE_GRACE_MS = "10"; let spawnCalls = 0; _setSpawnForTest(async () => { spawnCalls += 1; - return "http://127.0.0.1:18998"; + return dead; }); try { - await withDeadAttachEnv(async () => { - _resetRegisterForTest(DEAD); - const state = _nativeStateForTest(); - // apply() primes the fast path with the attach origin before the - // health verdict exists — this is the state under test - state.origin = DEAD; + _resetRegisterForTest(dead); + const state = _nativeStateForTest(); + // apply() primes the fast path with the attach origin before the health + // verdict exists — this is the state under test + state.origin = dead; - const landed = await verifyAttachAndRecover(DEAD); - assert.equal(landed, undefined, "a dead pinned target resolves to no origin"); - assert.equal(spawnCalls, 0, "#1365: never spawn a second instance behind a pinned channel"); + const landed = await verifyAttachAndRecover(dead); + assert.equal(landed, undefined, "a dead pinned target resolves to no origin"); + assert.equal(spawnCalls, 0, "#1365: never spawn a second instance behind a pinned channel"); - assert.equal(state.origin, undefined, "the dead origin must leave the fast path"); - assert.equal(await readyOrigin(state), undefined, "later requests take the no-origin direct lane"); - assert.equal(await readyOrigin(state), undefined, "and stay there for the rest of the process"); - }); + assert.equal(state.origin, undefined, "the dead origin must leave the fast path"); + assert.equal(await readyOrigin(state), undefined, "later requests take the no-origin direct lane"); + assert.equal(await readyOrigin(state), undefined, "and stay there for the rest of the process"); } finally { _setSpawnForTest(undefined); _resetRegisterForTest(undefined); + restoreEnv(saved); } }); test("#7176 a live pinned attach target keeps the fast path bound", async () => { // control arm: the release above must not fire when the target answers. - // A local server speaking the plugin version endpoint stands in for the proxy. - const http = await import("node:http"); - let versionRequests = 0; + let manifestRequests = 0; const server = http.createServer((req, res) => { if (req.url === "/__bili/plugin/manifest") { - versionRequests += 1; + manifestRequests += 1; // a NEWER version so the #3544 rotation path stays out of this arm res.writeHead(200, { "content-type": "application/json" }); res.end(JSON.stringify({ version: "999.0.0", tools: {} })); return; } res.writeHead(404); - res.end(); + res.end("{}"); }); const origin = await new Promise((resolve) => { server.listen(0, "127.0.0.1", () => { @@ -84,32 +95,26 @@ test("#7176 a live pinned attach target keeps the fast path bound", async () => resolve(`http://127.0.0.1:${addr.port}`); }); }); + const saved = saveEnv(); + process.env.BILLION_CONTEXT_ATTACH = origin; + process.env.BILLION_CONTEXT_PROXY = origin; + process.env.BILI_ATTACH_HEALTH_DEADLINE_MS = "2000"; _setSpawnForTest(async () => { throw new Error("a healthy pinned target must never spawn"); }); try { - const saved: Record = {}; - for (const k of ENV_KEYS) saved[k] = process.env[k]; - process.env.BILLION_CONTEXT_ATTACH = origin; - process.env.BILLION_CONTEXT_PROXY = origin; - process.env.BILI_ATTACH_HEALTH_DEADLINE_MS = "2000"; - try { - _resetRegisterForTest(origin); - const state = _nativeStateForTest(); - state.origin = origin; - const landed = await verifyAttachAndRecover(origin); - assert.equal(landed, origin, "healthy target attaches as-is"); - assert.equal(state.origin, origin, "the live origin stays on the fast path"); - assert.ok(versionRequests >= 1, "the manifest/version probe ran"); - } finally { - for (const k of ENV_KEYS) { - if (saved[k] === undefined) delete process.env[k]; - else process.env[k] = saved[k]!; - } - } + _resetRegisterForTest(origin); + const state = _nativeStateForTest(); + state.origin = origin; + + const landed = await verifyAttachAndRecover(origin); + assert.equal(landed, origin, "healthy target attaches as-is"); + assert.equal(state.origin, origin, "the live origin stays on the fast path"); + assert.ok(manifestRequests >= 1, "the manifest/version probe ran"); } finally { _setSpawnForTest(undefined); _resetRegisterForTest(undefined); - server.close(); + restoreEnv(saved); + await new Promise((resolve) => server.close(() => resolve())); } }); From 1c8cac2c3038ee070c1c112ebcdfd9bd309fb9f8 Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 17 Nov 2026 12:35:40 +0800 Subject: [PATCH 12/12] fix: note the output-budget override in the compat.dropFields log (#6752) When compat.dropFields removes a budget field that restoreOutputBudget just restored, the drop log now says so explicitly so the log is honest (the restore fired, but the field is absent from the forwarded body). One-line note appended to both drop-log sites (graph and string paths). (cherry picked from commit 1fcc31d11415fbc11fae6130c86ee5a875a0fe2f) --- src/server/relay.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/server/relay.ts b/src/server/relay.ts index 7ce093b91b..1b4f67dfc6 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -687,6 +687,13 @@ const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepare // bytes. Protocol-neutral (any JSON-object body), unlike the role // rewrite above which is openai/responses-only. compatDropPaths = resolveCompatDropFields(opts.routes, upstreamUrl, opts.compat?.dropFields); + // #6752: when the drop overrides an earlier "output budget restored" log, + // say so explicitly so the log is honest (the restore fired, but the field + // is gone by the time the wire is built). + const OUTPUT_BUDGET_DROP_PATHS = ["max_tokens", "max_completion_tokens", "max_output_tokens", "generationConfig.maxOutputTokens"]; + const budgetOverrideNote = compatDropPaths.some(p => OUTPUT_BUDGET_DROP_PATHS.includes(p)) + ? " (overrides the earlier 'output budget restored' log — the field is absent from the forwarded body)" + : ""; if (compatDropPaths.length > 0 && typeof wireBody === "string") { // #564/#2627: drops run on the SAME graph as every other transform, so // identity decides — not the drop path's shape. A drop that deletes or @@ -698,14 +705,14 @@ const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepare const dropped = dropCompatFieldsJson(graph, compatDropPaths); if (dropped > 0) { commitGraph(); - log("info", `[${prepared?.session.id ?? "passthrough"}] [compat] dropped ${dropped} field(s) per compat.dropFields (${compatDropPaths.join(", ")}) (#1757)`); + log("info", `[${prepared?.session.id ?? "passthrough"}] [compat] dropped ${dropped} field(s) per compat.dropFields (${compatDropPaths.join(", ")})${budgetOverrideNote} (#1757)`); } } else { const applied = applyCompatDropFields(wireBody, compatDropPaths); if (applied.dropped > 0) { wireBody = applied.body; invalidateSeparatorOwnership(); - log("info", `[${prepared?.session.id ?? "passthrough"}] [compat] dropped ${applied.dropped} field(s) per compat.dropFields (${compatDropPaths.join(", ")}) (#1757)`); + log("info", `[${prepared?.session.id ?? "passthrough"}] [compat] dropped ${applied.dropped} field(s) per compat.dropFields (${compatDropPaths.join(", ")})${budgetOverrideNote} (#1757)`); } } }