From 8b162ff401789eff914402d44dddf44e0cdc351b Mon Sep 17 00:00:00 2001 From: ework-agent Date: Tue, 13 Oct 2026 12:41:23 +0800 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20host-stable=20tool-unit=20id=20side?= =?UTF-8?q?=20channel=20=E2=80=94=20ingress=20+=20per-wire=20contract=20(#?= =?UTF-8?q?2938)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PLUGIN.md | 11 ++ src/hostid-channel.ts | 86 +++++++++++ src/plugin.ts | 3 +- src/server/handle.ts | 28 +++- tests/hostid-channel.test.ts | 196 ++++++++++++++++++++++++++ tests/hostid-ingress-contract.test.ts | 186 ++++++++++++++++++++++++ tests/plugin-protocol.test.ts | 6 + 7 files changed, 514 insertions(+), 2 deletions(-) create mode 100644 src/hostid-channel.ts create mode 100644 tests/hostid-channel.test.ts create mode 100644 tests/hostid-ingress-contract.test.ts diff --git a/PLUGIN.md b/PLUGIN.md index d12ef260a1..da6f939f8c 100644 --- a/PLUGIN.md +++ b/PLUGIN.md @@ -75,6 +75,17 @@ Effects on the proxy for that session: - Session identity is keyed by the conversation id (strongest signal, ahead of all legacy session headers). This also fixes multi-session safety for agents that send no session headers at all. - The compression philosophy system prompt, ref tags and nudges keep flowing from the proxy, exactly as in wire mode. +**Optional request-body field** (plugin lanes whose host message model carries stable ids — dsh: `Message.id`, opencode: `UserMessage.id` / `ToolPart.callID`; #2938): + +- `_bili_toolcall_ids: string[]` — the host's own stable identifiers for every **tool unit** (each tool-call block and each tool-result message/block) of the re-sent history, **in transcript order**. It is the host-side identity side channel: fold-state reconciliation matches covered messages against these exact ids instead of guessing from content or from wire-sanitized ids, so representation drift (model switches that re-mint ids, reasoning flattening) no longer breaks fold identity. + +Semantics the proxy applies: + +- **Reserved namespace, stripped unconditionally.** The field is removed from the forwarded body on every prepare path — with or without the `x-bili-plugin` marker — because prepare paths re-serialize the parsed object and any surviving value would reach the upstream provider. Unmarked requests get it stripped but never consumed. +- **Consumed only when well-formed and count-consistent**: an array of non-empty strings (≤ 10 000 entries; an empty array is legal only when the body carries zero tool units) whose length equals the number of tool units the proxy counts in the same body (same walk as the wire-neutral projection). Any mismatch ⇒ the whole channel is ignored for that request (honest degrade to legacy behavior; logged). +- **Latest-wins per session.** The last valid list is kept on the session; an absent or rejected field clears it, so a stopped or misbehaving plugin can never leave a stale mapping behind. +- **Capability gate (mandatory).** The manifest advertises `capabilities.hostIdChannel: { bodyField, maxUnits }`. Plugins MUST check it before stamping: proxies predating this change forward unknown top-level fields verbatim, so an un-gated plugin would leak its id list to the provider. Read the field name from the manifest rather than hard-coding it. + ### 3. `POST /__bili/plugin/tool` Execute a tool against the conversation's compression state. diff --git a/src/hostid-channel.ts b/src/hostid-channel.ts new file mode 100644 index 0000000000..a28cd837a2 --- /dev/null +++ b/src/hostid-channel.ts @@ -0,0 +1,86 @@ +// #2938 Tier 1 — host-stable tool-unit id side channel (ingress half). +// +// Plugin lanes (dsh first) can carry the host's OWN stable identifiers for +// every tool unit — each tool-call block AND each tool-result block, in +// transcript order — in a private top-level body field alongside each model +// request. The proxy strips the field before ANY prepare runs (it must never +// reach an upstream), cross-checks its length against the wire-neutral +// tool-unit count of the same body, and keeps the list on the session so fold +// reconciliation can claim churned covered ids by exact host-id equality +// instead of content heuristics (#2396 family; owner rule 2026-10-12: order +// + self-minted id are first-class, content hash is a guardrail). +// +// Trust boundary: consumed ONLY under the x-bili-plugin marker header (same +// gate as every other x-bili-plugin-* value). Absent ⇒ legacy behavior, zero +// cost. Malformed or miscounted ⇒ channel rejected for THIS request (honest +// degrade — never worse than no channel). The k-th entry aligns to the k-th +// tool unit in transcript order (calls in their assistant message's order, +// results when encountered) — the same walk the wire-neutral projection uses. + +import { neutralTurns } from "./affinity-neutral.js"; + +/** Private protocol field; underscore-prefixed like other bili internals. */ +export const HOSTID_BODY_FIELD = "_bili_toolcall_ids"; +/** Hard cap: 5k tool rounds × (call + result). Bounds memory for hostile input. */ +export const HOSTID_MAX_UNITS = 10_000; + +export interface HostIdDecision { + /** Validated list ready for session storage (undefined = not consumed). */ + list?: string[]; + /** Field was present and removed from parsed — true even when not consumed. */ + stripped: boolean; + /** Why the field was not consumed (absent ⇒ neither flag set). */ + rejected?: "no-marker" | "malformed" | "miscount"; +} + +/** Count tool units (tool-call blocks + tool-result blocks, transcript order) + * over a RAW wire message array using the shared wire-neutral projection — + * the same single source of protocol-shape knowledge #2939 and session-resolve + * run. Null when the array is not a recognizable message list. */ +export function countToolUnits(messages: unknown[]): number { + let n = 0; + for (const t of neutralTurns(messages)) { + if (t.kind === "tool") n += 1; + n += t.calls?.length ?? 0; + } + return n; +} + +/** Extract the side-channel field from the parsed inbound body per protocol + * (responses carries items under `input`, google under `contents`, the rest + * under `messages`). Returns null when no recognizable array is present — + * the caller then rejects the channel rather than trusting it unchecked. */ +function toolUnitCountFor(protocol: string, parsed: Record): number | null { + let messages: unknown[] | null = null; + if (protocol === "responses") messages = Array.isArray(parsed.input) ? (parsed.input as unknown[]) : null; + else if (protocol === "google") messages = Array.isArray(parsed.contents) ? (parsed.contents as unknown[]) : null; + else messages = Array.isArray(parsed.messages) ? (parsed.messages as unknown[]) : null; + return messages ? countToolUnits(messages) : null; +} + +/** Consume the side channel from a parsed inbound body. REMOVES the field + * whenever it is present — marker or not: it is bili's reserved namespace and + * must never ride upstream on ANY path (prepare paths re-serialize the same + * object, so an un-stripped foreign value would leak to the provider). Only + * CONSUMPTION (validation + list return) is marker-gated. Pure w.r.t. policy: + * all decisions land in the returned record so the matrix is testable without HTTP. */ +export function consumeHostIdChannel( + parsed: Record, + opts: { pluginMarker: boolean; protocol: string }, +): HostIdDecision { + if (!(HOSTID_BODY_FIELD in parsed)) return { stripped: false }; + const raw = parsed[HOSTID_BODY_FIELD]; + delete parsed[HOSTID_BODY_FIELD]; + if (!opts.pluginMarker) return { stripped: true, rejected: "no-marker" }; + // Empty is well-formed: a tool-free conversation may legitimately carry + // [] (the count check below then requires zero counted units). + const wellFormed = Array.isArray(raw) + && raw.length <= HOSTID_MAX_UNITS + && raw.every((v) => typeof v === "string" && v.length > 0); + if (!wellFormed) return { stripped: true, rejected: "malformed" }; + const expected = toolUnitCountFor(opts.protocol, parsed); + if (expected === null || expected !== raw.length) { + return { stripped: true, rejected: "miscount" }; + } + return { stripped: true, list: raw as string[] }; +} diff --git a/src/plugin.ts b/src/plugin.ts index c1a076fc1e..3ba9bdade8 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -13,6 +13,7 @@ import { clientConversationHeader } from "./session-id.js"; import { buildContextView } from "./context-view.js"; import { ABSORB_TOOL_NAME, BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_OPENAI_NO_RANGE, BILI_ACP_TOOLS_RESPONSES_NO_RANGE, PROXY_TOOL_NAMES, READ_ORIGINAL_IMAGE_TOOL, READ_ORIGINAL_IMAGE_TOOL_NAME, READ_ORIGINAL_IMAGE_TOOL_OPENAI, READ_ORIGINAL_IMAGE_TOOL_RESPONSES, RETRIEVE_TOOL_NAME, RULE_TOOL, RULE_TOOL_NAME, RULE_TOOL_OPENAI, RULE_TOOL_RESPONSES, SEARCH_CONTEXT_TOOL_NAME, absorbToolsFor, retrieveToolsFor, withAcpNotes, withAcpNotesOpenAI } from "./compress-tool.js"; import { externalSummaryEnabled, externalSummaryMaxToolDurationMs, withExternalSummaryTools } from "./external-summary-surface.js"; +import { HOSTID_BODY_FIELD, HOSTID_MAX_UNITS } from "./hostid-channel.js"; import { salvageCompressArgs } from "./plugin-compress-salvage.js"; import { absorbEnabled, effectiveAbsorbConfig, isProxyToolFor } from "./absorb.js"; import { effectiveRulesEnabled, rulesEnabled } from "./rules-feature.js"; @@ -964,7 +965,7 @@ export function handlePluginManifest(res: import("node:http").ServerResponse, co // #2652: maxToolDurationMs = the budget's totalTimeoutMs ceiling, so agent-side // bridges size their compress-call HTTP wait to the real server bound instead of a // fixed constant. Absent when the chain is off (classic summaries commit locally). - capabilities: { ...(externalSummary ? { externalSummary: { enabled: true, summaryOptional: true, submittedSummary: "hint", maxToolDurationMs: externalSummaryMaxToolDurationMs(config) } } : {}), fork: { protocolVersion: 2, acceptsProtocolVersions: [1, 2], endpoint: "/__bili/plugin/fork", snapshotEndpoint: "/__bili/plugin/snapshot" } }, + capabilities: { ...(externalSummary ? { externalSummary: { enabled: true, summaryOptional: true, submittedSummary: "hint", maxToolDurationMs: externalSummaryMaxToolDurationMs(config) } } : {}), fork: { protocolVersion: 2, acceptsProtocolVersions: [1, 2], endpoint: "/__bili/plugin/fork", snapshotEndpoint: "/__bili/plugin/snapshot" }, hostIdChannel: { bodyField: HOSTID_BODY_FIELD, maxUnits: HOSTID_MAX_UNITS } }, })); } diff --git a/src/server/handle.ts b/src/server/handle.ts index 79d088af05..028480c5fa 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -43,6 +43,7 @@ import { resolveClaudeTranscriptLineage } from "../claude-transcript-lineage.js" import { consumePluginRegisterFor, pluginAgentHeader, pluginConversationHeader, pluginHeadersMatchModel, pluginReportedContextWindow, pluginReportedMaxOutput, pluginReportedModel, pluginRequestAgentHeader, pluginRuntimeInfoFor, pluginRuntimeInfoForConversation, publicForkInputCheck, recordChainVerdict, recordPluginSession, rememberPluginMessages, resolveConversation, runtimeConversationId, takePendingPluginRegister } from "../plugin.js"; import { scheduleAffinityPersist } from "../affinity-persist.js"; import { evaluateChain, extractChainCarriers } from "../chain-checkpoint.js"; +import { consumeHostIdChannel, HOSTID_BODY_FIELD } from "../hostid-channel.js"; import { BILI_PASSTHROUGH_HEADER, BILI_PLUGIN_BYPASS_HEADER, isLoopbackAddress, normalizeUpstreamOrigin, provenCompletionReserve, reserveOutputHeadroom, resolveOutputHeadroomCap, shouldReserveOutputHeadroom, type WireProtocol } from "../util.js"; import { checkTunnelDestination, tunnelAllowlistFromEnv } from "../tunnel-guard.js"; import { resolveCompatDropFields } from "../compat-drop.js"; @@ -527,8 +528,10 @@ async function handleRequest( let v1Meta: Record | undefined; let v1Model: string | undefined; let v1ConvId: string | undefined; + let v1Outer: Record | undefined; if (protocol === "google" && parsed !== null && isV1InternalRequest(urlPath, parsed)) { - const unwrapped = unwrapV1Internal(parsed as Record); + v1Outer = parsed as Record; + const unwrapped = unwrapV1Internal(v1Outer); parsed = unwrapped.inner; v1Meta = unwrapped.meta; v1Model = unwrapped.model ?? undefined; @@ -540,6 +543,24 @@ async function handleRequest( } log("debug", `v1internal envelope unwrapped (model=${v1Model ?? "?"} convId=${v1ConvId ?? "none"})`); } + // #2938 Tier 1: consume the host-stable tool-unit id side channel AFTER the + // v1 unwrap (so google validates against the same final view the pipeline + // sees) and BEFORE chain-checkpoint eval and every prepare — the field is + // bili's own protocol surface and must never ride upstream; on strip the + // buffer is re-serialized from the WHOLE body shape (outer envelope when + // v1internal) so verbatim-forward paths carry clean bytes too. Rejected ⇒ + // honest degrade. + const hostId = protocol && parsed !== null && typeof parsed === "object" && !Array.isArray(parsed) + ? consumeHostIdChannel(parsed as Record, { pluginMarker: pluginAgentHeader(req.headers) !== undefined, protocol }) + : { stripped: false }; + if (hostId.stripped) { + if (v1Outer) delete v1Outer[HOSTID_BODY_FIELD]; + bodyBuffer = Buffer.from(JSON.stringify(v1Outer ?? parsed), "utf8"); + bodyText = bodyBuffer.toString("utf8"); + inboundBytes = bodyBuffer.length; + if (hostId.rejected) log("warn", `[hostid] ${HOSTID_BODY_FIELD} ${hostId.rejected} — channel ignored for this request (#2938)`); + else log("debug", `[hostid] consumed ${hostId.list?.length ?? 0} host tool-unit ids (#2938)`); + } // #2824: one cheap raw-text pass decides whether the incoming-image index // (#1995/#2607) can find anything — a provably image-free body skips BOTH // index builds (each one re-converts the full history wire→core). The @@ -2404,6 +2425,11 @@ async function handleRequest( if (registryProfile !== undefined) session.metadata.cachePriceProfile = registryProfile; else delete session.metadata.cachePriceProfile; } + // #2938 Tier 1: host-stable tool-unit id list, latest-wins per + // request; absent/rejected ⇒ clear, so a stale list from a + // downgraded/stopped plugin cannot misalign k-th claims later. + if (hostId.list) session.metadata.hostToolCallIds = hostId.list; + else delete session.metadata.hostToolCallIds; const visibilityMarkers = cs.visibilityMarkers ?? true; const reasoningCfg = cs.reasoning; // #1995/#2607: index recoverable historical images by ref from the RAW diff --git a/tests/hostid-channel.test.ts b/tests/hostid-channel.test.ts new file mode 100644 index 0000000000..8fbd87b5b2 --- /dev/null +++ b/tests/hostid-channel.test.ts @@ -0,0 +1,196 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { consumeHostIdChannel, countToolUnits, HOSTID_BODY_FIELD, HOSTID_MAX_UNITS } from "../src/hostid-channel.js"; + +const MARKER = { pluginMarker: true }; +const NO_MARKER = { pluginMarker: false }; + +function openaiBody(): Record { + return { + model: "gpt-test", + messages: [ + { role: "system", content: "sys" }, + { role: "user", content: "hi" }, + { role: "assistant", content: null, tool_calls: [ + { id: "c1", type: "function", function: { name: "Bash", arguments: '{"cmd":"ls"}' } }, + { id: "c2", type: "function", function: { name: "Read", arguments: '{"path":"a"}' } }, + ] }, + { role: "tool", tool_call_id: "c1", content: "out1" }, + { role: "tool", tool_call_id: "c2", content: "out2" }, + { role: "assistant", content: "done" }, + ], + }; +} + +function anthropicBody(): Record { + return { + model: "claude-test", + messages: [ + { role: "user", content: "hi" }, + { role: "assistant", content: [ + { type: "tool_use", id: "c1", name: "Bash", input: { cmd: "ls" } }, + { type: "tool_use", id: "c2", name: "Read", input: { path: "a" } }, + ] }, + { role: "user", content: [ + { type: "tool_result", tool_use_id: "c1", content: "out1" }, + { type: "tool_result", tool_use_id: "c2", content: "out2" }, + ] }, + { role: "assistant", content: [{ type: "text", text: "done" }] }, + ], + }; +} + +function responsesBody(): Record { + return { + model: "gpt-test", + input: [ + { type: "message", role: "user", content: "hi" }, + { type: "message", role: "assistant", content: "thinking" }, + { type: "function_call", call_id: "c1", name: "Bash", arguments: '{"cmd":"ls"}' }, + { type: "function_call", call_id: "c2", name: "Read", arguments: '{"path":"a"}' }, + { type: "function_call_output", call_id: "c1", output: "out1" }, + { type: "function_call_output", call_id: "c2", output: "out2" }, + ], + }; +} + +function googleBody(): Record { + return { + contents: [ + { role: "user", parts: [{ text: "hi" }] }, + { role: "model", parts: [{ text: "ok" }, { functionCall: { name: "Bash", args: { cmd: "ls" } } }, { functionCall: { name: "Read", args: { path: "a" } } }] }, + { role: "user", parts: [{ functionResponse: { name: "Bash", response: { output: "out1" } } }] }, + { role: "user", parts: [{ functionResponse: { name: "Read", response: { output: "out2" } } }] }, + ], + }; +} + +test("countToolUnits: openai (2 calls + 2 results = 4)", () => { + const b = openaiBody(); + assert.equal(countToolUnits(b.messages as unknown[]), 4); +}); + +test("countToolUnits: anthropic (multi-block tool_result message splits per block)", () => { + const b = anthropicBody(); + assert.equal(countToolUnits(b.messages as unknown[]), 4); +}); + +test("countToolUnits: responses (function_call items merge into assistant run, outputs are tool turns)", () => { + const b = responsesBody(); + assert.equal(countToolUnits(b.input as unknown[]), 4); +}); + +test("countToolUnits: google (functionCall parts + one functionResponse turn per item)", () => { + const b = googleBody(); + assert.equal(countToolUnits(b.contents as unknown[]), 4); +}); + +test("countToolUnits: leading system run and text turns carry no units", () => { + const msgs = [ + { role: "system", content: "harness state" }, + { role: "user", content: "[model changed: assistant turns above this point were generated by x; the session continues with y]" }, + { role: "user", content: "plain" }, + { role: "assistant", content: "plain too" }, + ]; + assert.equal(countToolUnits(msgs), 0); +}); + +test("consume: absent field ⇒ decision untouched (both with and without marker)", () => { + for (const opts of [MARKER, NO_MARKER]) { + const b = openaiBody(); + const d = consumeHostIdChannel(b, { ...opts, protocol: "openai" }); + assert.deepEqual(d, { stripped: false }); + assert.ok(!(HOSTID_BODY_FIELD in b), "field must not be invented"); + } +}); + +test("consume: no marker ⇒ still stripped (reserved namespace) but never consumed", () => { + const b = openaiBody(); + b[HOSTID_BODY_FIELD] = ["a", "b", "c", "d"]; + const d = consumeHostIdChannel(b, { ...NO_MARKER, protocol: "openai" }); + assert.equal(d.stripped, true, "unmarked value must not ride upstream on any prepare path"); + assert.equal(d.rejected, "no-marker"); + assert.equal(d.list, undefined); + assert.ok(!(HOSTID_BODY_FIELD in b)); +}); + +test("consume: valid list under marker ⇒ consumed + key removed", () => { + const b = openaiBody(); + const ids = ["u-1", "u-2", "u-3", "u-4"]; + b[HOSTID_BODY_FIELD] = ids; + const d = consumeHostIdChannel(b, { ...MARKER, protocol: "openai" }); + assert.equal(d.stripped, true); + assert.equal(d.rejected, undefined); + assert.deepEqual(d.list, ids); + assert.ok(!(HOSTID_BODY_FIELD in b)); +}); + +test("consume: empty list is well-formed only when the body has zero units", () => { + const toolless: Record = { model: "m", messages: [{ role: "user", content: "hi" }] }; + toolless[HOSTID_BODY_FIELD] = []; + assert.deepEqual(consumeHostIdChannel(toolless, { ...MARKER, protocol: "openai" }), { stripped: true, list: [] }); + + const b = openaiBody(); + b[HOSTID_BODY_FIELD] = []; + assert.equal(consumeHostIdChannel(b, { ...MARKER, protocol: "openai" }).rejected, "miscount"); +}); + +for (const wire of ["openai", "anthropic", "responses", "google"] as const) { + test(`consume: ${wire} — all four wires accept their own shape`, () => { + const bodies: Record Record> = { + openai: openaiBody, anthropic: anthropicBody, responses: responsesBody, google: googleBody, + }; + const b = bodies[wire](); + b[HOSTID_BODY_FIELD] = ["u-1", "u-2", "u-3", "u-4"]; + const d = consumeHostIdChannel(b, { ...MARKER, protocol: wire }); + assert.equal(d.stripped, true, `${wire}: expected consumption`); + assert.equal(d.rejected, undefined, `${wire}: rejected=${d.rejected}`); + assert.equal(d.list?.length, 4); + assert.ok(!(HOSTID_BODY_FIELD in b)); + }); +} + +test("consume: malformed shapes are rejected AND still removed", () => { + // ([] is well-formed and covered by the dedicated empty-list case.) + const cases: unknown[] = ["nope", 42, null, { a: 1 }, ["ok", 7], ["", "x"], [null]]; + for (const raw of cases) { + const b = openaiBody(); + b[HOSTID_BODY_FIELD] = raw; + const d = consumeHostIdChannel(b, { ...MARKER, protocol: "openai" }); + assert.equal(d.stripped, true, `raw=${JSON.stringify(raw)} must be stripped`); + assert.equal(d.rejected, "malformed"); + assert.ok(!(HOSTID_BODY_FIELD in b), "malformed value must not survive to upstream"); + } +}); + +test("consume: over-cap list is rejected and removed", () => { + const b = openaiBody(); + b[HOSTID_BODY_FIELD] = Array.from({ length: HOSTID_MAX_UNITS + 1 }, (_, i) => `id-${i}`); + const d = consumeHostIdChannel(b, { ...MARKER, protocol: "openai" }); + assert.equal(d.rejected, "malformed"); + assert.ok(!(HOSTID_BODY_FIELD in b)); +}); + +test("consume: miscount (length ≠ counted units) is rejected and removed", () => { + for (const len of [0, 3, 5, 99]) { + const b = openaiBody(); + b[HOSTID_BODY_FIELD] = Array.from({ length: len }, (_, i) => `id-${i}`); + const d = consumeHostIdChannel(b, { ...MARKER, protocol: "openai" }); + assert.equal(d.stripped, true); + assert.equal(d.rejected, "miscount", `len=${len}`); + assert.ok(!(HOSTID_BODY_FIELD in b)); + } +}); + +test("consume: responses/google read their own arrays (messages key is not theirs)", () => { + const r = responsesBody(); + r[HOSTID_BODY_FIELD] = ["u-1", "u-2", "u-3", "u-4"]; + assert.equal(consumeHostIdChannel(r, { ...MARKER, protocol: "responses" }).rejected, undefined); + + // a responses-shaped body whose ONLY array is `messages` has no recognizable input + const stray: Record = { model: "m", messages: [{ role: "user", content: "x" }] }; + stray[HOSTID_BODY_FIELD] = ["u-1"]; + const d = consumeHostIdChannel(stray, { ...MARKER, protocol: "responses" }); + assert.equal(d.rejected, "miscount", "unrecognizable message array ⇒ never trusted unchecked"); + assert.ok(!(HOSTID_BODY_FIELD in stray)); +}); diff --git a/tests/hostid-ingress-contract.test.ts b/tests/hostid-ingress-contract.test.ts new file mode 100644 index 0000000000..af3917ee97 --- /dev/null +++ b/tests/hostid-ingress-contract.test.ts @@ -0,0 +1,186 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { HOSTID_BODY_FIELD } from "../src/hostid-channel.js"; + +// #2938 Tier 1 — per-wire ingress contract for the host-stable tool-unit id +// side channel: the field must NEVER reach an upstream on any wire, in any +// state (valid / malformed / miscounted / no-marker), while the request itself +// keeps processing normally (honest degrade, not rejection of traffic). + +type Captured = { url: string; body: string }; + +function listen(server: http.Server): Promise { + if (server.listening) return Promise.resolve(); + return once(server, "listening").then(() => undefined); +} + +function close(server: http.Server): Promise { + return new Promise((resolve, reject) => server.close((error) => (error ? reject(error) : resolve()))); +} + +const IDS4 = ["u-1", "u-2", "u-3", "u-4"]; + +function openaiBody(): Record { + return { + model: "gpt-test", + messages: [ + { role: "system", content: "sys" }, + { role: "user", content: "hi" }, + { role: "assistant", content: null, tool_calls: [ + { id: "c1", type: "function", function: { name: "Bash", arguments: '{"cmd":"ls"}' } }, + { id: "c2", type: "function", function: { name: "Read", arguments: '{"path":"a"}' } }, + ] }, + { role: "tool", tool_call_id: "c1", content: "out1" }, + { role: "tool", tool_call_id: "c2", content: "out2" }, + { role: "assistant", content: "done" }, + ], + }; +} + +function anthropicBody(): Record { + return { + model: "claude-test", + messages: [ + { role: "user", content: "hi" }, + { role: "assistant", content: [ + { type: "tool_use", id: "c1", name: "Bash", input: { cmd: "ls" } }, + { type: "tool_use", id: "c2", name: "Read", input: { path: "a" } }, + ] }, + { role: "user", content: [ + { type: "tool_result", tool_use_id: "c1", content: "out1" }, + { type: "tool_result", tool_use_id: "c2", content: "out2" }, + ] }, + { role: "assistant", content: [{ type: "text", text: "done" }] }, + ], + }; +} + +function responsesBody(): Record { + return { + model: "gpt-test", + input: [ + { type: "message", role: "user", content: "hi" }, + { type: "function_call", call_id: "c1", name: "Bash", arguments: '{"cmd":"ls"}' }, + { type: "function_call_output", call_id: "c1", output: "out1" }, + { type: "function_call", call_id: "c2", name: "Read", arguments: '{"path":"a"}' }, + { type: "function_call_output", call_id: "c2", output: "out2" }, + ], + }; +} + +function googleBody(): Record { + return { + contents: [ + { role: "user", parts: [{ text: "hi" }] }, + { role: "model", parts: [{ functionCall: { name: "Bash", args: { cmd: "ls" } } }, { functionCall: { name: "Read", args: { path: "a" } } }] }, + { role: "user", parts: [{ functionResponse: { name: "Bash", response: { output: "out1" } } }] }, + { role: "user", parts: [{ functionResponse: { name: "Read", response: { output: "out2" } } }] }, + ], + }; +} + +test("hostid ingress contract: field never reaches upstream on any wire, any state", async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + + const captured: Captured[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + captured.push({ url: req.url ?? "", body: Buffer.concat(chunks).toString("utf8") }); + const url = req.url ?? ""; + res.setHeader("content-type", "application/json"); + if (url.includes("/v1/messages")) { + res.end(JSON.stringify({ id: "msg_1", type: "message", role: "assistant", model: "claude-test", content: [{ type: "text", text: "ok" }], stop_reason: "end_turn", stop_sequence: null, usage: { input_tokens: 10, output_tokens: 5 } })); + } else if (url.includes("/responses")) { + res.end(JSON.stringify({ id: "resp_1", object: "response", status: "completed", model: "gpt-test", output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text: "ok" }] }] })); + } else if (url.includes(":generateContent")) { + res.end(JSON.stringify({ candidates: [{ content: { role: "model", parts: [{ text: "ok" }] }, finishReason: "STOP" }], usageMetadata: { promptTokenCount: 10, candidatesTokenCount: 5 } })); + } else { + res.end(JSON.stringify({ id: "chatcmpl_1", object: "chat.completion", model: "gpt-test", choices: [{ index: 0, message: { role: "assistant", content: "ok" }, finish_reason: "stop" }], usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 } })); + } + }); + }); + upstream.listen(0, "127.0.0.1"); + await listen(upstream); + const upPort = (upstream.address() as { port: number }).port; + + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { + [`http://127.0.0.1:${upPort}`]: { models: { "gpt-test": { context: 400_000 }, "claude-test": { context: 400_000 } } }, + }, + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + }; + const proxy = await startServer(opts); + await listen(proxy); + const pPort = (proxy.address() as { port: number }).port; + const base = `http://127.0.0.1:${pPort}/bili/http://127.0.0.1:${upPort}`; + + try { + const cases: { name: string; path: string; body: Record; field?: unknown; marker?: boolean }[] = [ + { name: "openai valid", path: "/v1/chat/completions", body: openaiBody(), field: IDS4, marker: true }, + { name: "anthropic valid", path: "/v1/messages", body: anthropicBody(), field: IDS4, marker: true }, + { name: "responses valid", path: "/v1/responses", body: responsesBody(), field: IDS4, marker: true }, + { name: "google valid", path: "/v1beta/models/gpt-test:generateContent", body: googleBody(), field: IDS4, marker: true }, + { name: "openai malformed", path: "/v1/chat/completions", body: openaiBody(), field: "not-an-array", marker: true }, + { name: "openai miscount", path: "/v1/chat/completions", body: openaiBody(), field: ["u-1", "u-2", "u-3"], marker: true }, + { name: "openai over-cap", path: "/v1/chat/completions", body: openaiBody(), field: Array.from({ length: 10_001 }, (_, i) => `x${i}`), marker: true }, + { name: "openai no-marker", path: "/v1/chat/completions", body: openaiBody(), field: IDS4, marker: false }, + { name: "openai absent", path: "/v1/chat/completions", body: openaiBody(), marker: true }, + ]; + + let n = 0; + for (const c of cases) { + n += 1; + const body: Record = { ...c.body }; + if (c.field !== undefined) body[HOSTID_BODY_FIELD] = c.field; + const headers: Record = { "content-type": "application/json", "x-acp-session": `hostid-${n}` }; + if (c.marker !== false) headers["x-bili-plugin"] = "dsh"; + const resp = await fetch(`${base}${c.path}`, { method: "POST", headers, body: JSON.stringify(body) }); + assert.ok(resp.status < 500, `${c.name}: proxy status ${resp.status}`); + await resp.arrayBuffer(); + } + + assert.equal(captured.length, cases.length, "every case must have reached the upstream"); + cases.forEach((c, i) => { + const cap = captured[i]!; + assert.ok(!cap.body.includes(HOSTID_BODY_FIELD), `${c.name}: side-channel field leaked to upstream`); + assert.ok(!cap.body.includes("u-1"), `${c.name}: channel payload leaked to upstream`); + }); + + // The valid cases must still be well-formed model requests: original + // conversation intact apart from the stripped private field. + const first = JSON.parse(captured[0]!.body) as { messages?: unknown[] }; + assert.equal(first.messages?.length, 6, "openai conversation must survive stripping byte-for-content"); + } finally { + await close(proxy); + await close(upstream); + } +}); diff --git a/tests/plugin-protocol.test.ts b/tests/plugin-protocol.test.ts index 7a94c7f69f..455002fcc0 100644 --- a/tests/plugin-protocol.test.ts +++ b/tests/plugin-protocol.test.ts @@ -235,6 +235,7 @@ test("plugin manifest serves the exact wire tool schemas, headers and version", tools: Record>>; headers: { agent: string; conversation: string }; toolEndpoint: string; + capabilities?: { hostIdChannel?: { bodyField?: unknown; maxUnits?: unknown } }; }; assert.equal(manifest.ok, true); assert.equal(manifest.protocolVersion, 1); @@ -264,6 +265,11 @@ test("plugin manifest serves the exact wire tool schemas, headers and version", assert.equal(manifest.headers.agent, "x-bili-plugin"); assert.equal(manifest.headers.conversation, "x-bili-plugin-conversation"); assert.equal(manifest.toolEndpoint, "/__bili/plugin/tool"); + // #2938: the host-stable tool-unit id side channel is advertised so plugins + // capability-gate their stamping — proxies predating it forward unknown + // top-level fields verbatim, so an un-gated plugin would leak its id list. + assert.equal(manifest.capabilities?.hostIdChannel?.bodyField, "_bili_toolcall_ids"); + assert.equal(manifest.capabilities?.hostIdChannel?.maxUnits, 10_000); } finally { await h.close(); } From bd5d87e9e519f40b4fa2b03498ca824f623fee31 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Tue, 13 Oct 2026 12:53:49 +0800 Subject: [PATCH 2/5] =?UTF-8?q?fix:=20un-export=20HostIdDecision=20?= =?UTF-8?q?=E2=80=94=20unused-exports=20gate=20flags=20it=20INTERNAL=5FONL?= =?UTF-8?q?Y?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review fix for #2981: HostIdDecision is only referenced within hostid-channel.ts (as consumeHostIdChannel's return type); the exported name failed scripts/check-unused-exports.mjs (exit 1), which would fail the ci.yml unused-exports job when this stack lands on master. Dropped the leading export per the guard's own remediation; no consumer imports it by name (typecheck + hostid/plugin-protocol suites re-run green). --- src/hostid-channel.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hostid-channel.ts b/src/hostid-channel.ts index a28cd837a2..3620f6cd27 100644 --- a/src/hostid-channel.ts +++ b/src/hostid-channel.ts @@ -24,7 +24,7 @@ export const HOSTID_BODY_FIELD = "_bili_toolcall_ids"; /** Hard cap: 5k tool rounds × (call + result). Bounds memory for hostile input. */ export const HOSTID_MAX_UNITS = 10_000; -export interface HostIdDecision { +interface HostIdDecision { /** Validated list ready for session storage (undefined = not consumed). */ list?: string[]; /** Field was present and removed from parsed — true even when not consumed. */ From 8b9d7dbbe589802c378dee54470e04aab3c45377 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sun, 25 Oct 2026 04:56:43 +0800 Subject: [PATCH 3/5] feat: host-stable id claim pass in fold reconciliation (#2938 Tier-1 increment 2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds the claim side of the host-id side channel (owner rule 2026-10-12: order-centric, hash second-class — a host self-minted id is a first-class verification token, strictly stronger than same-content twin guessing): - zipHostUnits(): zips the inbound tool-unit sequence (messages with a non-empty toolCallId, transcript order — one CoreMessage per call/result block on all four wires) against the channel list; any count mismatch or repeated host id degrades the whole request honestly (undefined), never guesses. - fold-reconcile Pass 1.25 (between Pass 1 exact toolCallId and Pass 1.5 twin): missing covered ids are claimed when their persisted binding (derived raw id -> host id, accumulated in session.metadata.hostIdBindings by reconcileFoldCoverage from each pass's fresh zip, capped at HOSTID_BINDINGS_CAP with clear-on-overflow) maps them to a host id that appears EXACTLY once in the fresh channel AND exactly once among the missing anchors, and the roles agree. Double-uniqueness + role gate = misattribution guard; ambiguity refuses all involved. - plan.byHost counter + drift-log segment "N by host id"; early returns carry byHost: 0. Tests: tests/issue2938-hostid-claim.test.ts (zip semantics + compound-drift regression: id re-mint + reworded texts starve every content/hash pass, channel claims all four units; negative gate without view is byte-identical to legacy behavior; ambiguity and role-gate refusals). One existing deepEqual result literal in tests/fold-reconcile.test.ts gained byHost: 0 (new contract field). --- src/fold-reconcile.ts | 96 +++++++++++++++++-- src/hostid-channel.ts | 31 ++++++ tests/fold-reconcile.test.ts | 2 +- tests/issue2938-hostid-claim.test.ts | 136 +++++++++++++++++++++++++++ 4 files changed, 256 insertions(+), 9 deletions(-) create mode 100644 tests/issue2938-hostid-claim.test.ts diff --git a/src/fold-reconcile.ts b/src/fold-reconcile.ts index ea16e6c838..d699d41231 100644 --- a/src/fold-reconcile.ts +++ b/src/fold-reconcile.ts @@ -133,6 +133,7 @@ import { createHash } from "node:crypto"; import { defaultCountTokens, type CoreMessage } from "acp-kernel"; import { simhashHex, hammingHex } from "./affinity-simhash.js"; +import { HOSTID_BINDINGS_CAP, zipHostUnits } from "./hostid-channel.js"; import { reapDestroyedSubstrate, type Session } from "./session.js"; import { coveredRealHistoryIds, isResponsesTurnSeparatorId } from "./session.js"; import { dshNativeCompactionWitness, recordConflict } from "./conflict-watch.js"; @@ -341,6 +342,9 @@ interface ReconciliationPlan { claims: Map; byPos: number; byTool: number; + /** #2938: claims made by exact host-stable-id equality (the strongest + * channel — self-minted, representation-invariant identifiers). */ + byHost: number; byTwin: number; bySim: number; byOrder: number; @@ -400,6 +404,8 @@ export interface FoldReconcileResult { claims: number; byPos: number; byTool: number; + /** #2938: cells re-anchored by exact host-stable-id equality. */ + byHost: number; byTwin: number; bySim: number; byOrder: number; @@ -811,14 +817,22 @@ function recoveryCandidateIds(blocks: ReadonlyArray): Set { * stored positional fingerprint copy {ids, canon} (#2487's self-contained * storage shape, adopted); Pass 0 claims by position + canonical fingerprint * before the anchor passes run. */ +/** #2938: exact host-id claim view — THIS pass's newRawId→hostId zip plus the + * persisted derivedRawId→hostId join table. Absent ⇒ pure legacy path. */ +export interface HostIdView { + fresh: Map; + bindings: Record; +} + export function planReconciliation( oldOrder: string[], anchors: Record, msgs: CoreMessage[], covered: Set, positions?: { ids: string[]; canon: string[] }, + hostView?: HostIdView, ): ReconciliationPlan { - const plan: ReconciliationPlan = { claims: new Map(), byPos: 0, byTool: 0, byTwin: 0, bySim: 0, byOrder: 0, byNorm: 0, byTurn: 0, byImage: 0, unmatched: [], idRewriteSuspects: 0, newOrder: [], nextCanon: undefined, resyncs: 0, byShape: 0, absentEverywhere: [], nearDupResidual: [] }; + const plan: ReconciliationPlan = { claims: new Map(), byPos: 0, byTool: 0, byHost: 0, byTwin: 0, bySim: 0, byOrder: 0, byNorm: 0, byTurn: 0, byImage: 0, unmatched: [], idRewriteSuspects: 0, newOrder: [], nextCanon: undefined, resyncs: 0, byShape: 0, absentEverywhere: [], nearDupResidual: [] }; // #2480: the positional copy is built on EVERY pass, so the first pass // after an upgrade establishes it and every later drift is // position-claimable. The fast path is reuse-by-ID (#2487): same id = @@ -1017,6 +1031,51 @@ export function planReconciliation( } } + // Pass 1.25 — host-stable id (#2938): the strongest channel available — + // the host's own self-minted identifier, representation-invariant by + // construction (owner rule 2026-10-12: self-minted id + order are + // first-class, content hash a guardrail). A missing covered id claims the + // inbound unit whose FRESH channel slot carries the SAME host id the + // persisted join table says that covered id once carried. Double + // uniqueness gate: the host id must map to exactly ONE missing old id AND + // exactly one unclaimed inbound unit — any ambiguity refuses the claim + // (misattribution is the most expensive failure, MESSAGE-IDENTITY.md). + // Runs before the twin pass so an authoritative match preempts + // same-content guessing. + if (missingMiddle.length > 0 && hostView !== undefined) { + const hostToMissing = new Map(); + for (const oldId of missingMiddle) { + const h = hostView.bindings[oldId]; + if (h === undefined) continue; + const arr = hostToMissing.get(h); + if (arr === undefined) hostToMissing.set(h, [oldId]); + else arr.push(oldId); + } + if (hostToMissing.size > 0) { + const hostToFresh = new Map(); + for (const [nid, h] of hostView.fresh) { + const arr = hostToFresh.get(h); + if (arr === undefined) hostToFresh.set(h, [nid]); + else arr.push(nid); + } + for (const [h, olds] of hostToMissing) { + if (olds.length !== 1) continue; // two old identities claim one host id → refuse both + const oldId = olds[0]; + if (oldId === undefined) continue; + const freshIds = hostToFresh.get(h); + if (freshIds === undefined || freshIds.length !== 1) continue; // host repeated its id → refuse + const freshId = freshIds[0]; + if (freshId === undefined || covered.has(freshId) || claimedCandidates.has(freshId)) continue; + const anchor = anchors[oldId]; + const message = byId.get(freshId); + if (anchor?.r !== undefined && message !== undefined && message.role !== anchor.r) continue; + plan.claims.set(oldId, [freshId]); + claimedCandidates.add(freshId); + plan.byHost++; + } + } + } + // Pass 1.5 — tool-id twins (#2915, promoting #2396's detection-only // buckets to a claim pass — owner rule 2026-10-12: order/content first, // the id is a fast path, never a death sentence). A provider @@ -1610,7 +1669,7 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt const mode = opts.mode ?? resolveFoldReconcileMode(process.env); if (mode === "off") { resetFoldDriftState(session); - return { kind: "off", missing: 0, claims: 0, byPos: 0, byTool: 0, byTwin: 0, bySim: 0, byOrder: 0, byNorm: 0, byTurn: 0, byImage: 0, byShape: 0, reaped: 0, unmatched: 0 }; + return { kind: "off", missing: 0, claims: 0, byPos: 0, byTool: 0, byHost: 0, byTwin: 0, bySim: 0, byOrder: 0, byNorm: 0, byTurn: 0, byImage: 0, byShape: 0, reaped: 0, unmatched: 0 }; } const blocks = (session.state?.blocks ?? []) as BlockLike[]; // Active coverage first (the anchor cap's survivor order, #2334), then the @@ -1620,7 +1679,7 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt for (const id of recoveryCandidateIds(blocks)) covered.add(id); if (covered.size === 0) { resetFoldDriftState(session); - return { kind: "noop", missing: 0, claims: 0, byPos: 0, byTool: 0, byTwin: 0, bySim: 0, byOrder: 0, byNorm: 0, byTurn: 0, byImage: 0, byShape: 0, reaped: 0, unmatched: 0 }; + return { kind: "noop", missing: 0, claims: 0, byPos: 0, byTool: 0, byHost: 0, byTwin: 0, bySim: 0, byOrder: 0, byNorm: 0, byTurn: 0, byImage: 0, byShape: 0, reaped: 0, unmatched: 0 }; } // #2202: auxiliary side-requests (title-gen, WebSearch refinement — #1075) // share the conversation id but do not carry the conversation. Reconciling @@ -1633,9 +1692,9 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt // the episode state is left exactly as found (the resets above stay // reserved for true episode boundaries: reconcile off / no folds at all). if (msgs.length < SIDE_REQUEST_MAX_MSGS || (opts.inboundMessageCount !== undefined && opts.inboundMessageCount < SIDE_REQUEST_MAX_MSGS)) { - return { kind: "noop", missing: 0, claims: 0, byPos: 0, byTool: 0, byTwin: 0, bySim: 0, byOrder: 0, byNorm: 0, byTurn: 0, byImage: 0, byShape: 0, reaped: 0, unmatched: 0 }; + return { kind: "noop", missing: 0, claims: 0, byPos: 0, byTool: 0, byHost: 0, byTwin: 0, bySim: 0, byOrder: 0, byNorm: 0, byTurn: 0, byImage: 0, byShape: 0, reaped: 0, unmatched: 0 }; } - if (!session.metadata) return { kind: "noop", missing: 0, claims: 0, byPos: 0, byTool: 0, byTwin: 0, bySim: 0, byOrder: 0, byNorm: 0, byTurn: 0, byImage: 0, byShape: 0, reaped: 0, unmatched: 0 }; + if (!session.metadata) return { kind: "noop", missing: 0, claims: 0, byPos: 0, byTool: 0, byHost: 0, byTwin: 0, bySim: 0, byOrder: 0, byNorm: 0, byTurn: 0, byImage: 0, byShape: 0, reaped: 0, unmatched: 0 }; let anchors: Record = (session.metadata[METADATA_ANCHORS] as Record | undefined) ?? {}; @@ -1682,7 +1741,27 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt if (changed) anchors = upgraded; } - const plan = planReconciliation(oldOrder, anchors, msgs, covered, positions); + // #2938: exact host-id claim view. The validated per-request list is zipped + // onto THIS pass's tool units; the persisted join table answers "which + // host id did a vanished covered id once carry". A count mismatch between + // the two walks degrades to the legacy path for this request (never guess). + let hostView: HostIdView | undefined; + const hostList = session.metadata.hostToolCallIds; + if (Array.isArray(hostList) && hostList.length > 0) { + const fresh = zipHostUnits(msgs, hostList); + if (fresh !== undefined) { + const bindings = (session.metadata.hostIdBindings ?? {}) as Record; + hostView = { fresh, bindings }; + const merged: Record = { ...bindings }; + for (const [id, h] of fresh) merged[id] = h; + // Overflow clears rather than trims: a partial table would silently + // lose exactly the old entries claims depend on. + if (Object.keys(merged).length > HOSTID_BINDINGS_CAP) delete session.metadata.hostIdBindings; + else session.metadata.hostIdBindings = merged; + } + } + + const plan = planReconciliation(oldOrder, anchors, msgs, covered, positions, hostView); if (plan.pass0Desync) { loggerLog("warn", `[fold-reconcile] Pass 0 disabled: stored positional copy desynced from order backbone (session=${session.id}, copyIds=${positions?.ids.length ?? -1}, backbone=${oldOrder.length})`); } @@ -2156,12 +2235,12 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt } if (plan.unmatched.length === 0 && plan.claims.size === 0) { - return { kind: "resend", missing: 0, claims: 0, byPos: 0, byTool: 0, byTwin: 0, bySim: 0, byOrder: 0, byNorm: 0, byTurn: 0, byImage: 0, byShape: 0, reaped: reapedCount, unmatched: 0 }; + return { kind: "resend", missing: 0, claims: 0, byPos: 0, byTool: 0, byHost: 0, byTwin: 0, bySim: 0, byOrder: 0, byNorm: 0, byTurn: 0, byImage: 0, byShape: 0, reaped: reapedCount, unmatched: 0 }; } if (opts.log !== undefined) { if (plan.claims.size > 0 && mode === "repair") { opts.log(plan.unmatched.length > 0 ? "warn" : "info", - `${tag}[fold-reconcile] resent history drifted: ${plan.claims.size + plan.unmatched.length} covered id(s) missing — reanchored ${plan.claims.size} (${plan.byPos} positional${plan.resyncs > 0 ? `, ${plan.resyncs} order break(s) resynced` : ""}, ${plan.byTool} by toolCallId${plan.byTwin > 0 ? `, ${plan.byTwin} by toolCallId twin` : ""}${plan.bySim > 0 ? `, ${plan.bySim} by simhash corroboration` : ""}${plan.byOrder > 0 ? `, ${plan.byOrder} by order bracket` : ""}, ${plan.byNorm} by normalized identity${plan.byTurn > 0 ? `, ${plan.byTurn} by assistant-turn key` : ""}${plan.byImage > 0 ? `, ${plan.byImage} by image-capability variant` : ""}${plan.byShape > 0 ? `, ${plan.byShape} by shape census` : ""}) onto churned bytes, ${plan.unmatched.length} unmatched re-enter the wire unfolded${plan.idRewriteSuspects > 0 ? `; ${plan.idRewriteSuspects} suspected host-rewritten tool-call ids (identical content under a different toolCallId — #2396)` : ""} (#1921)${driftTok}`); + `${tag}[fold-reconcile] resent history drifted: ${plan.claims.size + plan.unmatched.length} covered id(s) missing — reanchored ${plan.claims.size} (${plan.byPos} positional${plan.resyncs > 0 ? `, ${plan.resyncs} order break(s) resynced` : ""}, ${plan.byTool} by toolCallId${plan.byHost > 0 ? `, ${plan.byHost} by host id` : ""}${plan.byTwin > 0 ? `, ${plan.byTwin} by toolCallId twin` : ""}${plan.bySim > 0 ? `, ${plan.bySim} by simhash corroboration` : ""}${plan.byOrder > 0 ? `, ${plan.byOrder} by order bracket` : ""}, ${plan.byNorm} by normalized identity${plan.byTurn > 0 ? `, ${plan.byTurn} by assistant-turn key` : ""}${plan.byImage > 0 ? `, ${plan.byImage} by image-capability variant` : ""}${plan.byShape > 0 ? `, ${plan.byShape} by shape census` : ""}) onto churned bytes, ${plan.unmatched.length} unmatched re-enter the wire unfolded${plan.idRewriteSuspects > 0 ? `; ${plan.idRewriteSuspects} suspected host-rewritten tool-call ids (identical content under a different toolCallId — #2396)` : ""} (#1921)${driftTok}`); } else if (plan.claims.size > 0) { opts.log("warn", `${tag}[fold-reconcile] resent history drifted: ${plan.claims.size + plan.unmatched.length} covered id(s) missing, ${plan.claims.size} matchable by anchor (${plan.byPos} positional${plan.resyncs > 0 ? `, ${plan.resyncs} resynced` : ""}, ${plan.byTool} toolCallId${plan.byTwin > 0 ? `, ${plan.byTwin} twin` : ""}${plan.bySim > 0 ? `, ${plan.bySim} simhash` : ""}${plan.byOrder > 0 ? `, ${plan.byOrder} order-bracket` : ""}, ${plan.byNorm} normalized${plan.byTurn > 0 ? `, ${plan.byTurn} turn key` : ""}${plan.byImage > 0 ? `, ${plan.byImage} image` : ""}) but reconcile=warn made no repair (#1921)${driftTok}`); @@ -2196,6 +2275,7 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt claims: plan.claims.size, byPos: plan.byPos, byTool: plan.byTool, + byHost: plan.byHost, byTwin: plan.byTwin, bySim: plan.bySim, byOrder: plan.byOrder, diff --git a/src/hostid-channel.ts b/src/hostid-channel.ts index 3620f6cd27..87a1c10f70 100644 --- a/src/hostid-channel.ts +++ b/src/hostid-channel.ts @@ -84,3 +84,34 @@ export function consumeHostIdChannel( } return { stripped: true, list: raw as string[] }; } + +/** Persistent join-table cap (derived raw id → host id). Bounded by a + * session's tool-unit history; overflow clears the whole map — the claim pass + * degrades to legacy heuristics until entries re-accumulate (the plugin + * re-stamps every request, so recovery is at most one request away). */ +export const HOSTID_BINDINGS_CAP = 50_000; + +/** Zip a validated channel list onto a PREPARED transcript's tool units: + * returns newRawId → hostId for every message carrying a non-empty + * toolCallId, in array order. Undefined when the walk count differs from the + * list length — a representation misalignment between the two walks must + * degrade honestly, never guess (misattribution is the most expensive + * failure, MESSAGE-IDENTITY.md). Pure; persistence is the caller's job. */ +export function zipHostUnits( + messages: readonly { id?: string; toolCallId?: string }[], + hostIds: string[], +): Map | undefined { + const unitIds: string[] = []; + for (const m of messages) { + if (m.id !== undefined && m.toolCallId !== undefined && m.toolCallId !== "") unitIds.push(m.id); + } + if (unitIds.length !== hostIds.length) return undefined; + const seen = new Set(); + for (const h of hostIds) { + if (seen.has(h)) return undefined; // a host that repeats an id breaks k-th alignment — degrade + seen.add(h); + } + const out = new Map(); + for (let i = 0; i < unitIds.length; i++) out.set(unitIds[i]!, hostIds[i]!); + return out; +} diff --git a/tests/fold-reconcile.test.ts b/tests/fold-reconcile.test.ts index 849aa0c8ad..64a268770c 100644 --- a/tests/fold-reconcile.test.ts +++ b/tests/fold-reconcile.test.ts @@ -1070,7 +1070,7 @@ describe("reconcileFoldCoverage anchor cap boundary (#2334)", () => { const snapshot = JSON.stringify(session.metadata); resetNormalizedIdentityWork(); const second = reconcileFoldCoverage(session, msgs, opts); - assert.deepEqual(second, { kind: "resend", missing: 0, claims: 0, byPos: 0, byTool: 0, byTwin: 0, bySim: 0, byOrder: 0, byNorm: 0, byTurn: 0, byImage: 0, byShape: 0, reaped: 0, unmatched: 0 }); + assert.deepEqual(second, { kind: "resend", missing: 0, claims: 0, byPos: 0, byTool: 0, byHost: 0, byTwin: 0, bySim: 0, byOrder: 0, byNorm: 0, byTurn: 0, byImage: 0, byShape: 0, reaped: 0, unmatched: 0 }); assert.equal(normalizedIdentityWorkCount(), 0, "the overflow tail must not be normalized+hashed and dropped AGAIN every pass (#2334)"); assert.equal(JSON.stringify(session.metadata), snapshot, "steady-state resend leaves metadata byte-stable"); diff --git a/tests/issue2938-hostid-claim.test.ts b/tests/issue2938-hostid-claim.test.ts new file mode 100644 index 0000000000..dfbcf0763c --- /dev/null +++ b/tests/issue2938-hostid-claim.test.ts @@ -0,0 +1,136 @@ +// #2938 Tier-1 increment 2 — exact host-stable-id claim pass. +// +// Compound drift (id re-mint AND content change in one replay, the dsh model +// switch shape) defeats every content-based pass; the host's own self-minted +// identifiers — carried per request on the validated side channel and joined +// to vanished covered ids through the persisted derived→host table — are the +// only evidence left. These tests pin: the claim fires on exact equality, the +// double-uniqueness guards refuse ambiguity, and WITHOUT the view the legacy +// behavior is byte-identical (negative gate, #3617 discipline). + +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { CoreMessage } from "acp-kernel"; +import { + planReconciliation, + normalizedIdentity, + type FoldAnchor, + type HostIdView, +} from "../src/fold-reconcile.js"; +import { zipHostUnits } from "../src/hostid-channel.js"; + +function msg(id: string, role: string, text: string, extra?: Partial): CoreMessage { + return { id, role, text, ...(extra ?? {}) } as CoreMessage; +} + +function anchorOf(m: CoreMessage): FoldAnchor { + const a: FoldAnchor = { n: normalizedIdentity(m), r: m.role, b: m.text?.length ?? 0 }; + if (m.toolCallId !== undefined && m.toolCallId !== "") a.t = m.toolCallId; + return a; +} + +test("zipHostUnits: aligned walk pairs every tool unit in order", () => { + const msgs = [ + msg("a1", "assistant", "text"), + msg("c1", "assistant", "", { toolCallId: "k1" }), + msg("r1", "tool", "res", { toolCallId: "k1" }), + msg("c2", "assistant", "", { toolCallId: "k2" }), + ]; + const out = zipHostUnits(msgs, ["H1", "H2", "H3"]); + assert.deepEqual([...out!.entries()], [["c1", "H1"], ["r1", "H2"], ["c2", "H3"]]); +}); + +test("zipHostUnits: count mismatch between walk and list degrades to undefined", () => { + const msgs = [msg("c1", "assistant", "", { toolCallId: "k1" })]; + assert.equal(zipHostUnits(msgs, ["H1", "H2"]), undefined); + assert.equal(zipHostUnits([], ["H1"]), undefined); +}); + +test("zipHostUnits: a host repeating its own id degrades to undefined", () => { + const msgs = [ + msg("c1", "assistant", "", { toolCallId: "k1" }), + msg("r1", "tool", "res", { toolCallId: "k1" }), + ]; + assert.equal(zipHostUnits(msgs, ["H1", "H1"]), undefined); +}); + +test("zipHostUnits: empty list against a tool-free transcript yields an empty map", () => { + const out = zipHostUnits([msg("a1", "assistant", "text")], []); + assert.ok(out !== undefined && out.size === 0); +}); + +// One assistant preamble + two tool rounds. The NEW pass re-mints every +// toolCallId (dsh model-switch sanitize shape) AND changes the surrounding +// bytes, so positional (no stored copy), toolCallId-exact, twin and +// normalized-identity passes all starve — exactly the residual gap. +function fixture() { + const oA1 = msg("o-a1", "assistant", "let me check things"); + const oC1 = msg("o-c1", "assistant", "", { toolCallId: "call_a" }); + const oR1 = msg("o-r1", "tool", "result one", { toolCallId: "call_a" }); + const oC2 = msg("o-c2", "assistant", "", { toolCallId: "call_b" }); + const oR2 = msg("o-r2", "tool", "result two", { toolCallId: "call_b" }); + const oldMsgs = [oA1, oC1, oR1, oC2, oR2]; + const oldOrder = oldMsgs.map((m) => m.id!); + const anchors: Record = {}; + for (const m of oldMsgs) anchors[m.id!] = anchorOf(m); + + const nA1 = msg("n-a1", "assistant", "let me check the things, now"); + const nC1 = msg("n-c1", "assistant", "", { toolCallId: "call_a_fc_0" }); + const nR1 = msg("n-r1", "tool", "result one (reworded)", { toolCallId: "call_a_fc_0" }); + const nC2 = msg("n-c2", "assistant", "", { toolCallId: "call_b_fc_1" }); + const nR2 = msg("n-r2", "tool", "result two (reworded)", { toolCallId: "call_b_fc_1" }); + const newMsgs = [nA1, nC1, nR1, nC2, nR2]; + + const bindings: Record = { "o-c1": "H1", "o-r1": "H2", "o-c2": "H3", "o-r2": "H4" }; + const channel = ["H1", "H2", "H3", "H4"]; + return { oldOrder, anchors, newMsgs, covered: new Set(oldOrder), bindings, channel }; +} + +test("compound drift: host-id pass claims every re-minted tool unit (zero tool unmatched)", () => { + const f = fixture(); + const fresh = zipHostUnits(f.newMsgs, f.channel)!; + const view: HostIdView = { fresh, bindings: f.bindings }; + const plan = planReconciliation(f.oldOrder, f.anchors, f.newMsgs, f.covered, undefined, view); + assert.deepEqual(plan.claims.get("o-c1"), ["n-c1"]); + assert.deepEqual(plan.claims.get("o-r1"), ["n-r1"]); + assert.deepEqual(plan.claims.get("o-c2"), ["n-c2"]); + assert.deepEqual(plan.claims.get("o-r2"), ["n-r2"]); + assert.equal(plan.byHost, 4); + // Only the genuinely-changed assistant preamble stays honestly unmatched. + assert.deepEqual(plan.unmatched, ["o-a1"]); +}); + +test("negative gate: identical replay WITHOUT the view keeps legacy behavior", () => { + const f = fixture(); + const plan = planReconciliation(f.oldOrder, f.anchors, f.newMsgs, f.covered); + assert.equal(plan.byHost, 0); + assert.equal(plan.claims.size, 0); + assert.equal(plan.unmatched.length, 5); +}); + +test("ambiguity: two vanished ids bound to one host id → both refused", () => { + const f = fixture(); + const badBindings: Record = { ...f.bindings, "o-c2": "H1" }; + const view: HostIdView = { fresh: zipHostUnits(f.newMsgs, f.channel)!, bindings: badBindings }; + const plan = planReconciliation(f.oldOrder, f.anchors, f.newMsgs, f.covered, undefined, view); + assert.equal(plan.claims.get("o-c1"), undefined); + assert.equal(plan.claims.get("o-c2"), undefined); + // Unambiguous partners still claim. + assert.deepEqual(plan.claims.get("o-r1"), ["n-r1"]); + assert.deepEqual(plan.claims.get("o-r2"), ["n-r2"]); + assert.equal(plan.byHost, 2); +}); + +test("role gate: a result bound to a host id now sitting on a call unit → refused", () => { + const f = fixture(); + // Only o-r1 carries H1 (no other missing id shares it, so the ambiguity + // guard stays quiet): its anchor says role tool, but the fresh unit under + // H1 is n-c1 (assistant call) → refused. + const crossed: Record = { "o-r1": "H1", "o-c2": "H3", "o-r2": "H4" }; + const view: HostIdView = { fresh: zipHostUnits(f.newMsgs, f.channel)!, bindings: crossed }; + const plan = planReconciliation(f.oldOrder, f.anchors, f.newMsgs, f.covered, undefined, view); + assert.equal(plan.claims.get("o-r1"), undefined); + assert.deepEqual(plan.claims.get("o-c2"), ["n-c2"]); + assert.deepEqual(plan.claims.get("o-r2"), ["n-r2"]); + assert.equal(plan.byHost, 2); +}); From fa1ee96054cef6cebdc155549ee40bd5d1b7a9fb Mon Sep 17 00:00:00 2001 From: ci-check Date: Sun, 25 Oct 2026 05:16:57 +0800 Subject: [PATCH 4/5] =?UTF-8?q?fix(gates):=20de-export=20ClientBuildCheck?= =?UTF-8?q?=20=E2=80=94=20inherited=20unused-exports=20master-red=20(#4599?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit master tip 146b34f27 is red on unused-exports for this exact finding (the #4545 train added this type-only export; the 4d34abfaf de-export sweep missed it). PR #4606 carries the canonical fix; per the few-line-fix coordination rule this branch carries the identical one-line de-export so its check matrix regains signal now. Drop this commit when rebasing onto master after #4606 merges. --- src/agent/client-build-note.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/agent/client-build-note.ts b/src/agent/client-build-note.ts index 8ca7e9ddaf..6c08149309 100644 --- a/src/agent/client-build-note.ts +++ b/src/agent/client-build-note.ts @@ -29,7 +29,7 @@ import path from "node:path"; import { isVersionNewer } from "../update.js"; import { logHostEvent } from "./host-log.js"; -export type ClientBuildCheck = { +type ClientBuildCheck = { /** Short human label used in the note text ("dsh", "pi", ...). */ label: string; /** The client's npm id, as shown in the note (@deepseek-ai/dsh, ...). */ From 7aa0bc259a3e8d6391b13c609a72b90e84572b3c Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 14 Nov 2026 09:06:01 +0800 Subject: [PATCH 5/5] test: regenerate wire-contract manifest goldens for hostIdChannel capability (#2938) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The PR adds capabilities.hostIdChannel to the plugin manifest (src/plugin.ts). The wire-contract manifest goldens pin the manifest shape and were not regenerated, so the golden test drifted. Regenerated via scripts/update-wire-contract-goldens.ts — the only change is the new hostIdChannel capability block in both manifest-default.json and manifest-optin.json (kernel.json / bili.json unchanged). --- tests/golden/wire-contract/manifest-default.json | 4 ++++ tests/golden/wire-contract/manifest-optin.json | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/tests/golden/wire-contract/manifest-default.json b/tests/golden/wire-contract/manifest-default.json index c8bdbbdcc4..d2b3d9629e 100644 --- a/tests/golden/wire-contract/manifest-default.json +++ b/tests/golden/wire-contract/manifest-default.json @@ -446,6 +446,10 @@ ], "endpoint": "/__bili/plugin/fork", "snapshotEndpoint": "/__bili/plugin/snapshot" + }, + "hostIdChannel": { + "bodyField": "_bili_toolcall_ids", + "maxUnits": 10000 } } } diff --git a/tests/golden/wire-contract/manifest-optin.json b/tests/golden/wire-contract/manifest-optin.json index 2dc91c326c..ba254bf370 100644 --- a/tests/golden/wire-contract/manifest-optin.json +++ b/tests/golden/wire-contract/manifest-optin.json @@ -634,6 +634,10 @@ ], "endpoint": "/__bili/plugin/fork", "snapshotEndpoint": "/__bili/plugin/snapshot" + }, + "hostIdChannel": { + "bodyField": "_bili_toolcall_ids", + "maxUnits": 10000 } } }