From b09b25233c839c17f549a168036f02896e10d0a3 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sun, 11 Oct 2026 14:05:07 +0800 Subject: [PATCH 1/7] feat(#2446): durable-state carriers become virtual tool paths (Config.messagePathOf) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The four dsh durable-state text carriers (#2419 skill catalog, #2446 mcp catalog, #2447 workspace-instructions baseline, #2481 runtime-context snapshot) stop being host-hardcoded boolean guards. A lane-scoped host classifier now projects each carrier to a canonical durable/ identity via the new kernel Config.messagePathOf hook, and the EXISTING protectedTools / protectedLatestTools pattern machinery governs them — same knobs, same semantics as the tool channel (#1947). kernel (0.0.116): - Config.messagePathOf: host-projected virtual tool path (pure, cheap, never re-projects tool-call/tool-result) - protected.ts: virtualToolPathOf; isMessageProtected + collectLatestProtected + isMessageLatestProtected accept virtual paths (protectedTools patterns and per-path latest semantics both apply to projected carriers) host: - durable-message-guards.ts rewritten: DurableMessageClassifier per carrier (same matching predicates, evidence comments preserved), durableMessageToolPaths registry (KDD #9 dsh-only allowlist unchanged), withDurableProtection() — default injects durable/* all-instance protection (byte-equivalent to the old boolean guards); ANY durable pattern in protectedTools/protectedLatestTools = whole-identity takeover (user policy wins verbatim, only the classifier is attached) - handle.ts / session.ts wiring + config.ts JSDoc for both knobs tests: kernel protected-virtual-paths 6 (projection domain, pattern matrix, per-path latest, tool-channel parity); host durable-tool-identity 8 (policy units + e2e: default all-instance, durable/* latest takeover, exact-identity takeover, lane scoping); the four #24xx suites migrated to the Path API with e2e assertions unchanged (behavioral parity). kernel-change: yes — kernel/package.json 0.0.115 -> 0.0.116; new optional Config field messagePathOf plus virtual-path support in protected.ts. Backwards compatible: without messagePathOf the kernel behaves exactly as before (all suites unchanged). --- kernel/package.json | 2 +- kernel/src/protected.ts | 70 +++- kernel/src/types.ts | 13 + kernel/tests/protected-virtual-paths.test.ts | 145 ++++++++ package-lock.json | 2 +- src/config.ts | 14 + src/durable-message-guards.ts | 184 ++++++---- src/server/handle.ts | 16 +- src/session.ts | 13 +- tests/durable-tool-identity.test.ts | 317 ++++++++++++++++++ tests/issue2419-dsh-skill-catalog.test.ts | 14 +- tests/issue2446-dsh-mcp-catalog.test.ts | 28 +- ...sue2447-dsh-workspace-instructions.test.ts | 22 +- tests/issue2481-dsh-runtime-context.test.ts | 30 +- 14 files changed, 730 insertions(+), 140 deletions(-) create mode 100644 kernel/tests/protected-virtual-paths.test.ts create mode 100644 tests/durable-tool-identity.test.ts diff --git a/kernel/package.json b/kernel/package.json index 22c5b11f31..ceeabfc691 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.115", + "version": "0.0.116", "description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.", "license": "MIT", "author": "ranxianglei", diff --git a/kernel/src/protected.ts b/kernel/src/protected.ts index c66ebc7328..c814ff0c44 100644 --- a/kernel/src/protected.ts +++ b/kernel/src/protected.ts @@ -180,9 +180,28 @@ export function matchToolMessagePattern( ); } +/** Host-projected virtual tool path for a non-tool message (billion-context + * #2446 family): plain-text durable-state carriers classified by the host + * run through the same pattern machinery as tool exchanges. Tool-call / + * tool-result messages are never re-projected — the classifier's domain is + * exactly the messages that carry no toolName, so a tool exchange keeps its + * existing semantics byte-for-byte. */ +export function virtualToolPathOf( + msg: CoreMessage, + config: Pick, +): string | undefined { + if ( + msg.contentType === "tool-call" || + msg.contentType === "tool-result" + ) { + return undefined; + } + return config.messagePathOf?.(msg); +} + export function isMessageProtected( msg: CoreMessage, - config: Pick, + config: Pick, ): boolean { // Host-declared message-level protection (#2419): applies to ANY content // type and outranks everything below — the host owns the decision. @@ -194,6 +213,16 @@ export function isMessageProtected( (msg.contentType !== "tool-call" && msg.contentType !== "tool-result") || !msg.toolName ) { + // Host-projected virtual tool path (#2446 family): a plain-text carrier + // matched by a protectedTools pattern is protected exactly like a tool + // exchange would be. Path-pattern semantics are identical ("durable" + // matches "durable/…" and its descendants; "*" never crosses "/"). + const virtualPath = virtualToolPathOf(msg, config); + if (virtualPath) { + for (const pattern of config.protectedTools) { + if (matchToolPath(virtualPath, pattern)) return true; + } + } return false; } @@ -266,7 +295,7 @@ export interface LatestProtected { export function collectLatestProtected( messages: CoreMessage[], - config: Pick, + config: Pick, ): LatestProtected { const callIds = new Set(); const msgIds = new Set(); @@ -274,14 +303,22 @@ export function collectLatestProtected( if (patterns.length === 0) return { callIds, msgIds }; for (const pattern of patterns) { // Path patterns (containing `/`) protect the latest instance PER PATH — - // `skill/*` keeps the newest load of every skill, not just one (#1947). - // Plain tool-name patterns keep the single-latest semantics. + // `skill/*` keeps the newest load of every skill, not just one (#1947), + // and `durable/*` keeps the newest instance of every host-projected + // durable carrier identity. Plain tool-name patterns keep the + // single-latest semantics. if (pattern.includes("/")) { const lastByPath = new Map(); for (const m of messages) { - if (m.contentType !== "tool-call" || !m.toolName) continue; - if (!matchToolMessagePattern(m, pattern)) continue; - lastByPath.set(toolPathOf(m), m); + if (m.contentType === "tool-call" && m.toolName) { + if (!matchToolMessagePattern(m, pattern)) continue; + lastByPath.set(toolPathOf(m), m); + continue; + } + const virtualPath = virtualToolPathOf(m, config); + if (virtualPath && matchToolPath(virtualPath, pattern)) { + lastByPath.set(virtualPath, m); + } } for (const last of lastByPath.values()) { if (last.toolCallId) callIds.add(last.toolCallId); @@ -291,11 +328,14 @@ export function collectLatestProtected( } let last: CoreMessage | undefined; for (const m of messages) { - if ( - m.contentType === "tool-call" && - m.toolName && - matchToolMessagePattern(m, pattern) - ) { + if (m.contentType === "tool-call" && m.toolName) { + if (matchToolMessagePattern(m, pattern)) { + last = m; + } + continue; + } + const virtualPath = virtualToolPathOf(m, config); + if (virtualPath && matchToolPath(virtualPath, pattern)) { last = m; } } @@ -307,12 +347,14 @@ export function collectLatestProtected( } /** True when msg is a latest-protected tool-call, or the tool-result paired to - * one (by toolCallId). */ + * one (by toolCallId). msgIds entries may also be latest TEXT carriers + * (host-projected virtual tool paths carry no toolCallId), so the id + * membership check runs for every content type. */ export function isMessageLatestProtected( msg: CoreMessage, latest: LatestProtected, ): boolean { - if (msg.contentType === "tool-call" && latest.msgIds.has(msg.id)) return true; + if (latest.msgIds.has(msg.id)) return true; if ( (msg.contentType === "tool-call" || msg.contentType === "tool-result") && msg.toolCallId && diff --git a/kernel/src/types.ts b/kernel/src/types.ts index db64d74f22..629486f3ac 100644 --- a/kernel/src/types.ts +++ b/kernel/src/types.ts @@ -403,6 +403,19 @@ export interface Config { * media payloads are protectable). Must be pure and cheap: it runs once * per message per turn. */ isMessageProtected?: (msg: CoreMessage) => boolean; + /** Host-projected VIRTUAL TOOL PATH for messages that are not tool + * exchanges (plain-text durable-state carriers, billion-context#2446 + * family): when set, the host classifier maps a message to a canonical + * path such as "durable/skill-catalog", and the SAME protectedTools / + * protectedLatestTools pattern machinery that governs tool exchanges then + * governs these messages too — all-instance protection via protectedTools, + * latest-per-path via protectedLatestTools. This keeps the POLICY in + * config while the host only owns the dialect→identity translation (the + * same projection principle as #1947's skill/). Tool-call / + * tool-result messages are NEVER re-projected — the classifier's domain is + * exactly the messages that carry no toolName. Must be pure and cheap: it + * runs once per message per protection scan. */ + messagePathOf?: (msg: CoreMessage) => string | undefined; /** Tool-name patterns (glob suffix allowed) protected in their LATEST * instance only: the newest matching tool-call and its paired tool-result * are protected from compression; older instances remain compressible. For diff --git a/kernel/tests/protected-virtual-paths.test.ts b/kernel/tests/protected-virtual-paths.test.ts new file mode 100644 index 0000000000..752915f5aa --- /dev/null +++ b/kernel/tests/protected-virtual-paths.test.ts @@ -0,0 +1,145 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { + isMessageProtected, + collectLatestProtected, + isMessageLatestProtected, + virtualToolPathOf, +} from "../src/protected.js"; +import { defaultConfig } from "../src/config.js"; +import type { CoreMessage } from "../src/types.js"; + +// #2446 family: host-projected VIRTUAL TOOL PATHS. A host classifier maps +// plain-text durable-state carriers to canonical `durable/` paths via +// Config.messagePathOf; the SAME protectedTools / protectedLatestTools +// pattern machinery that governs tool exchanges then governs them. Tool +// exchanges are never re-projected. + +function text(id: string, t: string): CoreMessage { + return { id, role: "user", contentType: "text", text: t }; +} + +function toolCall(id: string, toolName: string, callId: string): CoreMessage { + return { + id, + role: "assistant", + contentType: "tool-call", + toolName, + toolCallId: callId, + text: "{}", + }; +} + +/** Classifier fixture: `` text → durable/skill-catalog, `` → + * durable/agents-md; a tool-call carrying the same marker text must be + * IGNORED (projection domain is non-tool messages only). */ +function classifier(msg: CoreMessage): string | undefined { + if (msg.contentType === "tool-call" || msg.contentType === "tool-result") { + return "should-never-be-consulted"; + } + if (msg.text?.includes("")) return "durable/skill-catalog"; + if (msg.text?.includes("")) return "durable/agents-md"; + return undefined; +} + +function cfg(overrides: Partial[1]>) { + const base = defaultConfig(200_000); + return { ...base, messagePathOf: classifier, ...overrides } as Parameters< + typeof isMessageProtected + >[1]; +} + +test("virtualToolPathOf: never projects tool exchanges, passes through text", () => { + const c = { messagePathOf: classifier }; + assert.equal(virtualToolPathOf(text("t1", ""), c), "durable/skill-catalog"); + assert.equal(virtualToolPathOf(text("t2", "plain"), c), undefined); + assert.equal( + virtualToolPathOf(toolCall("c1", "skill", "call-1"), c), + undefined, + "tool-call is never re-projected even when the classifier could match", + ); + assert.equal(virtualToolPathOf({ ...toolCall("c2", "skill", "call-2"), text: "" }, c), undefined); +}); + +test("protectedTools patterns govern virtual paths: exact, first-segment, and glob", () => { + const catalog = text("t1", ""); + assert.equal( + isMessageProtected(catalog, cfg({ protectedTools: ["durable/skill-catalog"] })), + true, + "exact path pattern protects the matching carrier", + ); + assert.equal( + isMessageProtected(catalog, cfg({ protectedTools: ["durable"] })), + true, + "a single-segment pattern matches the first path segment (durable ≡ durable/*)", + ); + assert.equal( + isMessageProtected(catalog, cfg({ protectedTools: ["durable/*"] })), + true, + "glob covers every durable identity", + ); + assert.equal( + isMessageProtected(text("t2", ""), cfg({ protectedTools: ["durable/skill-catalog"] })), + false, + "a pattern for ANOTHER identity does not protect this carrier", + ); + assert.equal( + isMessageProtected(text("t3", "plain prose"), cfg({ protectedTools: ["durable/*"] })), + false, + "unclassified text stays consumable", + ); + assert.equal( + isMessageProtected(catalog, cfg({ protectedTools: [] })), + false, + "no patterns → no protection (policy lives in config)", + ); + // Case-insensitivity parity with the tool channel (#1725): + assert.equal( + isMessageProtected(catalog, cfg({ protectedTools: ["Durable/Skill-Catalog"] })), + true, + "pattern matching stays case-insensitive on both sides", + ); +}); + +test("virtual-path protection composes with tool-channel protection in one config", () => { + const c = cfg({ protectedTools: ["read"] }); + assert.equal(isMessageProtected(toolCall("c1", "read", "call-1"), c), true, "tool channel unchanged"); + assert.equal(isMessageProtected(text("t1", ""), c), false, "durable/* not configured → text unprotected"); + const c2 = cfg({ protectedTools: ["read", "durable/*"] }); + assert.equal(isMessageProtected(toolCall("c2", "read", "call-2"), c2), true); + assert.equal(isMessageProtected(text("t2", ""), c2), true, "both channels protected together"); +}); + +test("protectedLatestTools path patterns keep the LATEST instance per durable identity", () => { + const messages = [ + text("a", " v1"), + text("b", "plain filler"), + text("c", " v2"), // newest skill-catalog + text("d", " v1"), // only agents-md instance + ]; + const latest = collectLatestProtected(messages, cfg({ protectedLatestTools: ["durable/*"], protectedTools: [] })); + assert.equal(isMessageLatestProtected(text("a", " v1"), latest), false, "older catalog instance is foldable"); + assert.equal(isMessageLatestProtected(text("c", " v2"), latest), true, "newest catalog instance survives"); + assert.equal(isMessageLatestProtected(text("d", " v1"), latest), true, "every identity keeps its own latest"); + assert.equal(isMessageLatestProtected(text("b", "plain filler"), latest), false); +}); + +test("protectedLatestTools plain-name patterns: virtual carriers join single-latest semantics", () => { + const messages = [text("a", " v1"), text("b", " v1"), text("c", " v2")]; + const latest = collectLatestProtected(messages, cfg({ protectedLatestTools: ["durable"], protectedTools: [] })); + assert.equal(isMessageLatestProtected(text("a", " v1"), latest), false); + assert.equal(isMessageLatestProtected(text("b", " v1"), latest), false, "not the latest durable carrier overall"); + assert.equal(isMessageLatestProtected(text("c", " v2"), latest), true, "the LAST durable carrier wins"); +}); + +test("protectedLatestTools for TOOLS is unchanged when a classifier is attached", () => { + const messages = [ + toolCall("c1", "todo_list", "call-1"), + toolCall("c2", "todo_list", "call-2"), + text("t", ""), + ]; + const latest = collectLatestProtected(messages, cfg({ protectedLatestTools: ["todo_list"] })); + assert.equal(isMessageLatestProtected(messages[0]!, latest), false, "older todo_list still foldable"); + assert.equal(isMessageLatestProtected(messages[1]!, latest), true, "newest todo_list still protected"); + assert.equal(isMessageLatestProtected(messages[2]!, latest), false, "carrier not in latest scope (pattern is tool-only)"); +}); diff --git a/package-lock.json b/package-lock.json index 43841d4781..2ec1b72f21 100644 --- a/package-lock.json +++ b/package-lock.json @@ -48,7 +48,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.115", + "version": "0.0.116", "dev": true, "license": "MIT", "devDependencies": { diff --git a/src/config.ts b/src/config.ts index 2e57bd90ee..ff34a0e4ba 100644 --- a/src/config.ts +++ b/src/config.ts @@ -249,6 +249,14 @@ export type CompressSettings = { * Default: none — opt in per client/agent, since tool names are * client-specific. */ protectedLatestTools?: string[]; + /* (durable virtual paths) dsh lanes project their durable-state TEXT + * carriers to `durable/` identities (#2446 family: skill catalog, + * mcp catalog, agents-md baseline, runtime-context snapshot), so the + * same pattern machinery governs them: `"durable/*"` keeps the LATEST + * instance of every carrier (all four are cumulative snapshots — the + * older catalogs/baselines fold away). Default when nothing is + * configured: all-instance `durable/*` protection in `protectedTools` + * (behavior-equivalent to the boolean guards this replaces). */ /** Tool patterns whose tool-calls AND paired results are NEVER * compressed — every instance, full history (kernel `protectedTools`, * hard exclusion: matching refs render as `BLOCKED`, so neither suggested @@ -270,6 +278,12 @@ export type CompressSettings = { * Default: none — opt in per client/agent, since tool names are * client-specific. */ protectedTools?: string[]; + /* (durable virtual paths) any pattern whose first path segment is + * `durable` takes over the whole durable policy for that lane + * (whole-identity takeover): bili attaches only the identity + * classifier and injects NO default — e.g. `"durable/agents-md"` + * protects just the workspace baseline while skill/mcp catalogs fold + * normally. Unregistered lanes get no projection at all. */ /** Tool-name patterns EXCLUDED from the soft-protected recent zone — * matching tool results inside the recent zone become compressible * immediately instead of aging out first (kernel diff --git a/src/durable-message-guards.ts b/src/durable-message-guards.ts index a498a065b7..a8702074c4 100644 --- a/src/durable-message-guards.ts +++ b/src/durable-message-guards.ts @@ -1,4 +1,5 @@ -// #2419/#2447/#2481: durable-state message guards, declared per plugin lane. +// #2446/#2419/#2447/#2481: durable-state message carriers, declared per +// plugin lane and projected to VIRTUAL TOOL PATHS. // // Some hosts inject DURABLE STATE as ordinary conversation messages and gate // re-injection on a content fingerprint: dsh ships three such carriers as @@ -11,101 +12,156 @@ // sandbox/approval policy state, self-declared "supersedes earlier // runtime-context snapshots" — resent only when the runtime context CHANGES, // not every turn) — plus a FOURTH carrier from the dsh PLUGIN ecosystem -// (#2446: dsh-skill-mcp-manager's \x3cmcp_catalog\x3e block, the first guard -// entry whose emitter is not dsh core). ACP compression treats every history message as consumable, -// so one fold of any carrier silently kills its guidance forever (the host -// will not resend what it believes is already present). The structured -// source.kind these hosts stamp internally never reaches the proxy wire -// (CoreMessage carries no metadata field), so the guards match stable text -// markers instead — the same criterion discipline as #2447. The kernel's -// protection surface was tool-exchange-only (protectedTools / -// isToolProtected), structurally unable to cover a plain text message — hence -// the generic Config.isMessageProtected hook (kernel 0.0.108) and this -// lane-scoped policy map. The mechanism lives in the kernel; the POLICY -// (which shapes, for which host) lives here, one entry per lane with traffic -// evidence. +// (#2446: dsh-skill-mcp-manager's \x3cmcp_catalog\x3e block, the first entry +// whose emitter is not dsh core). ACP compression treats every history +// message as consumable, so one fold of any carrier silently kills its +// guidance forever (the host will not resend what it believes is already +// present). The structured source.kind these hosts stamp internally never +// reaches the proxy wire (CoreMessage carries no metadata field), so the +// classification matches stable text markers instead — the same criterion +// discipline as #2447. // -// KDD #9 evidence-permitlist discipline applies: entries are added PER HOST -// with traffic evidence (shape + stability proof), never enabled wholesale — -// a message-shape guard applied to every lane would hard-pin arbitrary user -// prose into context for hosts whose state carriers have no such contract. +// ARCHITECTURE (this file owns TRANSLATION only; the POLICY lives in kernel +// config): each carrier is projected to a canonical virtual tool path +// (`durable/`), handed to the kernel via Config.messagePathOf, and then +// governed by the SAME protectedTools / protectedLatestTools pattern +// machinery as tool exchanges — all-instance protection via protectedTools, +// latest-per-path via protectedLatestTools. This is the #1947 principle +// (project client dialects to canonical names, protect by pattern) extended +// from the tool channel to the text channel. withDurableProtection() wires a +// classifier onto a Config and defaults the policy to `durable/*` all-instance +// protection — byte-equivalent to the previous boolean guards — unless the +// user has already classified any `durable` pattern themselves (whole-identity +// takeover: configuring one durable pattern takes over the entire durable +// policy). +// +// KDD #9 evidence-permitlist discipline applies: classifiers are added PER +// HOST with traffic evidence (shape + stability proof), never enabled +// wholesale — a shape classifier applied to every lane would hard-pin +// arbitrary user prose into context for hosts whose state carriers have no +// such contract. The kernel stays host-agnostic: it only knows virtual paths +// and patterns. -import type { CoreMessage } from "acp-kernel"; +import type { Config, CoreMessage } from "acp-kernel"; -type DurableMessageGuard = (msg: CoreMessage) => boolean; +/** Projects a plain-text message to a canonical `durable/` virtual tool + * path (or undefined when the message is no durable carrier). */ +type DurableMessageClassifier = (msg: CoreMessage) => string | undefined; -/** dsh skill-catalog guard (#2419): matches the durable catalog message - * injected by dsh-tool-skill (a \x3csystem-reminder\x3e carrying the +/** 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 guard; a false positive is benign (a matching - * message simply stays visible) and the marker is DSH-specific XML. */ -export function dshSkillCatalogGuard(msg: CoreMessage): boolean { - return msg.contentType === "text" && typeof msg.text === "string" && msg.text.includes("\x3cavailable_skills\x3e"); + * 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). */ +export function dshSkillCatalogPath(msg: CoreMessage): string | undefined { + return msg.contentType === "text" && typeof msg.text === "string" && msg.text.includes("\x3cavailable_skills\x3e") + ? "durable/skill-catalog" + : undefined; } -/** dsh workspace-instructions guard (#2447): matches the durable AGENTS.md - * baseline message injected by @deepseek-ai/dsh-agent-instructions — global +/** dsh workspace-instructions identity (#2447): the durable AGENTS.md baseline + * message injected by @deepseek-ai/dsh-agent-instructions — global * (~/.dsh/AGENTS.md) and project instructions merged into ONE user-role * \x3csystem-reminder\x3e message whose sections are labelled * `Instructions from: ` (source: lib/index.js sectionText + * 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. Same marker-over-wrapper rationale as - * dshSkillCatalogGuard: the per-file label is matched on its own so wrapper/ - * intro rewording cannot silently disarm the guard; a false positive is - * benign (a matching message simply stays visible). 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 (replacements are - * rare identity changes), so pinning every match is bounded. */ -export function dshWorkspaceInstructionsGuard(msg: CoreMessage): boolean { - return msg.contentType === "text" && typeof msg.text === "string" && msg.text.includes("Instructions from:"); + * 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 + * (replacements are rare identity changes), so pinning every match is + * bounded. */ +export function dshWorkspaceInstructionsPath(msg: CoreMessage): string | undefined { + return msg.contentType === "text" && typeof msg.text === "string" && msg.text.includes("Instructions from:") + ? "durable/agents-md" + : undefined; } -/** dsh mcp-catalog guard (#2446): matches the durable MCP-server catalog - * injected by the THIRD-PARTY plugin dsh-skill-mcp-manager (author-reported - * marker; the plugin also curates which skills the dsh core catalog offers). - * The catalog rides a user-role message whose \x3cmcp_catalog\x3e block lists +/** dsh mcp-catalog identity (#2446): the durable MCP-server catalog injected + * by the THIRD-PARTY plugin dsh-skill-mcp-manager (author-reported marker; + * the plugin also curates which skills the dsh core catalog offers). The + * catalog rides a user-role message whose \x3cmcp_catalog\x3e block lists * every registered MCP server with tool-schema digests (~5K tokens in the * report); the plugin re-emits it only when the visible MCP/skill set * CHANGES, so one fold silently kills the session's MCP server inventory * forever — the report's failure mode is the model hunting for servers by - * wrong names mid-session. First guard entry for a carrier emitted by the - * dsh plugin ecosystem rather than dsh core: same evidence bar (shape from - * the #2446 report, stability owned by the plugin author who filed it), - * marker-over-wrapper rationale as #2419, and re-emissions are rare set - * changes so pinning every match stays bounded (same argument as #2447). */ -export function dshMcpCatalogGuard(msg: CoreMessage): boolean { - return msg.contentType === "text" && typeof msg.text === "string" && msg.text.includes("\x3cmcp_catalog\x3e"); + * wrong names mid-session. First entry for a carrier emitted by the dsh + * plugin ecosystem rather than dsh core: same evidence bar (shape from the + * #2446 report, stability owned by the plugin author who filed it), and + * re-emissions are rare set changes so pinning every match stays bounded + * (same argument as #2447). */ +export function dshMcpCatalogPath(msg: CoreMessage): string | undefined { + return msg.contentType === "text" && typeof msg.text === "string" && msg.text.includes("\x3cmcp_catalog\x3e") + ? "durable/mcp-catalog" + : undefined; } -/** dsh runtime-context guard (#2481): matches the durable runtime-context - * snapshot injected by the dsh agent loop — sandbox/approval policy state - * carried as a user-role message containing "Current runtime context" - * (source: deepseek-ai/deepseek-harness MIT packages/core/agent-loop/src/ +/** dsh runtime-context identity (#2481): the durable runtime-context snapshot + * injected by the dsh agent loop — sandbox/approval policy state carried as + * a user-role message containing "Current runtime context" (source: + * deepseek-ai/deepseek-harness MIT packages/core/agent-loop/src/ * runtime-context.ts; the same text family bili already recognizes as an * auto-injected notification in #2286's AUTO_INJECTED_NOTIFICATION_PREFIXES). * Both variants match on purpose: the populated snapshot ("Current runtime * context. This snapshot supersedes earlier runtime-context snapshots. …") * AND the cleared form ("Current runtime context: none. Earlier * runtime-context snapshots no longer apply.") — the cleared notice must - * stay visible too, because it is what invalidates the older snapshots this - * guard pins. The host re-emits only when the runtime context CHANGES (the - * per-turn time-context carrier deliberately stays foldable), so one fold of - * the latest snapshot silently drops the session's sandbox/approval semantics + * stay visible too, because it is what invalidates the older snapshots. + * The host re-emits only when the runtime context CHANGES (the per-turn + * time-context carrier deliberately stays foldable), so one fold of the + * latest snapshot silently drops the session's sandbox/approval semantics * forever. Each snapshot is tiny (~100–400 chars); superseded ones keep - * pinning harmlessly because the newest carries the supersedes clause. */ -export function dshRuntimeContextGuard(msg: CoreMessage): boolean { - return msg.contentType === "text" && typeof msg.text === "string" && msg.text.includes("Current runtime context"); + * pinning harmlessly under the all-instance default because the newest + * carries the supersedes clause. */ +export function dshRuntimeContextPath(msg: CoreMessage): string | undefined { + return msg.contentType === "text" && typeof msg.text === "string" && msg.text.includes("Current runtime context") + ? "durable/runtime-context" + : undefined; +} + +/** dsh lane classifier: any of the four evidence-backed carriers → its + * canonical durable identity. */ +function dshDurableToolPath(msg: CoreMessage): string | undefined { + return ( + dshSkillCatalogPath(msg) ?? + dshWorkspaceInstructionsPath(msg) ?? + dshRuntimeContextPath(msg) ?? + dshMcpCatalogPath(msg) + ); } -/** Lane → guard registry. Only lanes with traffic evidence of the +/** Lane → classifier registry. Only lanes with traffic evidence of the * durable-user-message carrier belong here (KDD #9). Keyed by the value of * the x-bili-plugin header / session.metadata.pluginAgent binding. */ -export const durableMessageGuards: Record = { - dsh: (msg) => - dshSkillCatalogGuard(msg) || dshWorkspaceInstructionsGuard(msg) || dshRuntimeContextGuard(msg) || dshMcpCatalogGuard(msg), +export const durableMessageToolPaths: Record = { + dsh: dshDurableToolPath, }; + +/** True when a pattern list already classifies the durable identity space — + * i.e. the user has taken over the durable policy themselves (any pattern + * whose first path segment is `durable`, in either protection list). */ +function classifiesDurable(patterns: readonly string[] | undefined): boolean { + return (patterns ?? []).some((p) => p.toLowerCase().split("/")[0] === "durable"); +} + +/** Attach a lane classifier to a kernel Config and default the durable + * policy. Default = `durable/*` all-instance protection in protectedTools — + * behavior-equivalent to the boolean guards this replaces (every carrier + * instance pinned). If the user already configured any `durable` pattern in + * protectedTools/protectedLatestTools, their policy wins verbatim + * (whole-identity takeover: one durable pattern anywhere takes over the + * whole durable policy — no per-carrier merging). */ +export function withDurableProtection(config: Config, classifier: DurableMessageClassifier): Config { + const classified = + classifiesDurable(config.protectedTools) || classifiesDurable(config.protectedLatestTools); + if (classified) { + return { ...config, messagePathOf: classifier }; + } + return { ...config, protectedTools: [...(config.protectedTools ?? []), "durable/*"], messagePathOf: classifier }; +} diff --git a/src/server/handle.ts b/src/server/handle.ts index 9e507eb558..1fc640c132 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -14,7 +14,7 @@ import { codexAlignedWindow } from "../codex-models.js"; import { DecodedRequestAdmission, DecodedRequestBusyError, MAX_DECODED_REQUEST_BYTES } from "../request-body-budget.js"; import { hostIdForLog, maskHeadersForLog, maskUrlForLog, maskUrlsInText } from "../log-mask.js"; import { buildIncomingImageIndex, pruneRetrieveImgExports, refreshIncomingImageIndex } from "../image-restore.js"; -import { durableMessageGuards } from "../durable-message-guards.js"; +import { durableMessageToolPaths, withDurableProtection } from "../durable-message-guards.js"; import { biliToolsDeclaredOnWire, countBiliToolUses, evaluateSelfHealRound, nudgeSuppressed, pluginLaneDegraded, pluginLaneRestore } from "../session-self-heal.js"; import { compressBreakerArmed } from "../stream.js"; import { acquireInFlight, getSession, hasProcessedState, markDirty, peekSession, releaseInFlight, storeEffectiveConfig, tickPostRebuildAnchor, withSessionLock, type Session } from "../session.js"; @@ -1823,15 +1823,17 @@ async function handleRequest( // same instant as effectiveContextLimit above) so request-context-free // display paths (/__bili/plugin/status Nudge line, plugin tool API) // render from the values the kernel actually used this turn. - // #2419: per-lane durable-state message guard (KDD#9 evidence-permitlist). + // #2446/#2419/#2447/#2481: per-lane durable-state carriers, projected + // to `durable/` virtual tool paths (KDD#9 evidence-permitlist). // Stamp the CLONE-SAFE config first — a function inside // session.metadata.effectiveConfig would break fork-adoption structuredClone - // and disk persistence — then attach it to this turn's reqConfig so - // processTurn protects the message now. effectiveConfig() re-resolves the - // guard from the lane id at read time, so /__bili/plugin/tool sees it too. + // and disk persistence — then attach the classifier+default policy to this + // turn's reqConfig so processTurn protects the carriers now. + // effectiveConfig() re-resolves both from the lane id at read time, so + // /__bili/plugin/tool sees them too. storeEffectiveConfig(session, reqConfig); - const durableGuard = pluginAgent ? durableMessageGuards[pluginAgent] : undefined; - if (durableGuard !== undefined) reqConfig = { ...reqConfig, isMessageProtected: durableGuard }; + const durableClassifier = pluginAgent ? durableMessageToolPaths[pluginAgent] : undefined; + if (durableClassifier !== undefined) reqConfig = withDurableProtection(reqConfig, durableClassifier); // acquireInFlight must precede the lock so evictOldest() cannot flush // this session between getSession and lock acquisition (inFlight===0 // window). Released in the outer finally after forward completes. diff --git a/src/session.ts b/src/session.ts index da78c378fd..89104e8cb8 100644 --- a/src/session.ts +++ b/src/session.ts @@ -3,7 +3,7 @@ import { createHash } from "node:crypto"; import { log as loggerLog } from "./logger.js"; import { getStore } from "./persist.js"; import { maxSessions as knobMaxSessions } from "./knobs.js"; -import { durableMessageGuards } from "./durable-message-guards.js"; +import { durableMessageToolPaths, withDurableProtection } from "./durable-message-guards.js"; import type { WireProtocol } from "./util.js"; export type BlockView = { text: string; count: number }; @@ -429,15 +429,16 @@ export function storeEffectiveConfig(session: Session, config: Config): void { export function effectiveConfig(session: Session | undefined, fallback: Config): Config { const stored = session?.metadata["effectiveConfig"]; const base = stored && typeof stored === "object" ? { ...fallback, ...(stored as Partial) } : fallback; - // #2419 follow-up (CI regression): the per-lane durable-message guard is a + // #2446 follow-up (CI regression): the per-lane durable classifier is a // FUNCTION and must never be persisted into session.metadata — fork-adoption // structuredClones metadata.effectiveConfig (plugin.ts) and disk persistence // cannot serialize a function. The lane id is already clone-safe here, so - // re-resolve the guard from it at read time; every consumer (plugin tool, - // nudge panel) then sees the same protection the wire path used this turn. + // re-resolve the classifier AND the default `durable/*` policy from it at + // read time; every consumer (plugin tool, nudge panel) then sees the same + // protection the wire path used this turn. const lane = typeof session?.metadata["pluginAgent"] === "string" ? session.metadata["pluginAgent"] : undefined; - const guard = lane ? durableMessageGuards[lane] : undefined; - return guard && !base.isMessageProtected ? { ...base, isMessageProtected: guard } : base; + const classifier = lane ? durableMessageToolPaths[lane] : undefined; + return classifier && !base.messagePathOf ? withDurableProtection(base, classifier) : base; } /** #2029: provenance-aware baseline for STATUS readers — acp_status nudge diff --git a/tests/durable-tool-identity.test.ts b/tests/durable-tool-identity.test.ts new file mode 100644 index 0000000000..90d9027788 --- /dev/null +++ b/tests/durable-tool-identity.test.ts @@ -0,0 +1,317 @@ +// Durable virtual tool paths — the architecture test (#2446 family). +// +// The four dsh durable-state carriers (#2419 skill catalog, #2446 mcp +// catalog, #2447 workspace instructions, #2481 runtime context) project to +// canonical `durable/` identities via a host classifier +// (Config.messagePathOf, kernel 0.0.116), and the EXISTING +// protectedTools / protectedLatestTools pattern machinery governs them — +// same knobs, same semantics as the tool channel (#1947). +// +// This suite pins the POLICY contract: +// 1. default: bili ensures `durable/*` all-instance protection (parity +// with the boolean guards this replaces — every instance pinned); +// 2. takeover: ANY `durable` pattern in the user's protectedTools or +// protectedLatestTools takes over the whole durable policy — bili only +// attaches the classifier, adds nothing; +// 3. e2e: default keeps every catalog instance across a real fold; +// `protectedLatestTools: ["durable/*"]` folds the older catalog and +// keeps the newest; `protectedTools: ["durable/agents-md"]` (exact +// identity takeover) lets the skill catalog fold while the baseline +// survives. +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { defaultConfig, type Config } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import type { CompressSettings } from "../src/config.ts"; +import { withDurableProtection, durableMessageToolPaths, dshSkillCatalogPath } from "../src/durable-message-guards.ts"; +import { resetPersonaAnchorsForTest } from "../src/persona-anchor.ts"; +import { _resetPluginStateForTest } from "../src/plugin.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { _resetSessionsForTest } from "../src/session.ts"; + +process.env.NODE_ENV = "test"; +process.env.BILI_PERSIST = "0"; + +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)."; + +function filler(tag: string): string { + return `${tag}-` + "z".repeat(900); +} + +// ---------------------------------------------------------------- units ---- + +test("withDurableProtection: default ensures durable/* all-instance + classifier hook", () => { + const cl = durableMessageToolPaths["dsh"]!; + const base = defaultConfig(200_000); + const out = withDurableProtection(base, cl); + assert.deepEqual(out.protectedTools, ["durable/*"], "default adds durable/* to protectedTools"); + assert.equal(out.messagePathOf, cl, "classifier attached"); + assert.equal(out.protectedLatestTools, base.protectedLatestTools, "latest list untouched"); + // Existing tool-channel patterns are preserved, durable/* appended: + const out2 = withDurableProtection({ ...base, protectedTools: ["read"] }, cl); + assert.deepEqual(out2.protectedTools, ["read", "durable/*"]); +}); + +test("withDurableProtection: a durable pattern in protectedTools = whole-identity takeover", () => { + const cl = durableMessageToolPaths["dsh"]!; + const out = withDurableProtection({ ...defaultConfig(200_000), protectedTools: ["read", "durable/agents-md"] }, cl); + assert.deepEqual(out.protectedTools, ["read", "durable/agents-md"], "no durable/* injected — user policy wins verbatim"); + assert.equal(out.messagePathOf, cl, "classifier still attached (identity translation is bili's job)"); +}); + +test("withDurableProtection: a durable pattern in protectedLatestTools = takeover too (no default injection)", () => { + const cl = durableMessageToolPaths["dsh"]!; + const out = withDurableProtection({ ...defaultConfig(200_000), protectedLatestTools: ["durable/*"] }, cl); + assert.deepEqual(out.protectedTools, [], "protectedTools not extended when latest takes over"); + assert.deepEqual(out.protectedLatestTools, ["durable/*"]); + assert.equal(out.messagePathOf, cl); +}); + +test("withDurableProtection: idempotent — no duplicate durable/* on re-apply", () => { + const cl = durableMessageToolPaths["dsh"]!; + const once = withDurableProtection(defaultConfig(200_000), cl); + const twice = withDurableProtection(once, cl); + assert.deepEqual(twice.protectedTools, ["durable/*"]); +}); + +// ------------------------------------------------------------------ e2e ---- + +type Rig = { proxyPort: number; upstreamPort: number; proxy: http.Server; upstream: http.Server; bodies: string[] }; + +async function startRig(compress?: CompressSettings): Promise { + const bodies: string[] = []; + const upstream = http.createServer((req, res) => { + let b = ""; + req.on("data", (c: Buffer) => (b += c.toString("utf8"))); + req.on("end", () => { + bodies.push(b); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ + id: "chatcmpl-1", + object: "chat.completion", + choices: [{ index: 0, message: { role: "assistant", content: "ok" }, finish_reason: "stop" }], + usage: { prompt_tokens: 10, completion_tokens: 3, total_tokens: 13 }, + })); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + _resetSessionsForTest(); + _resetPluginStateForTest(); + resetPersonaAnchorsForTest(); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: {} }, + modelContextLimit: 200_000, + kernelConfig: defaultConfig(200_000, { + preserveRecentMessages: 3, + preserveRecentTokens: 0, + compress: { minCompressRange: 500, maxSummaryLength: 20_000, minSummaryLength: 50 }, + }), + compress: { injectTool: true, injectNudge: false, ...compress }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + return { proxyPort: (proxy.address() as { port: number }).port, upstreamPort, proxy, upstream, bodies }; +} + +async function closeRig(rig: Rig): Promise { + rig.proxy.close(); + await once(rig.proxy, "close"); + rig.upstream.close(); + await once(rig.upstream, "close"); +} + +function parseRefIds(body: string): string[] { + let messages: Array<{ role?: string; content?: unknown }> = []; + try { + messages = (JSON.parse(body) as { messages?: Array<{ role?: string; content?: unknown }> }).messages ?? []; + } catch { + return []; + } + const text = messages + .filter((m) => m.role !== "system") + .map((m) => (typeof m.content === "string" ? m.content : JSON.stringify(m.content ?? ""))) + .join("\n"); + const ids: string[] = []; + const re = /<(?:acp|dcp-message-id)[^>]*>\s*(m\d+)\s*<\/(?:acp|dcp-message-id)>/g; + let m: RegExpExecArray | null; + while ((m = re.exec(text)) !== null) ids.push(m[1]!); + return ids; +} + +/** A dsh-lane conversation with TWO skill-catalog snapshots (v1 git-helper, + * v2 doc-writer) mid-history, folded across both, next-turn body returned. */ +async function runTwoCatalogFold( + compress: CompressSettings | undefined, + conv: string, + extraUserTurns = 3, +): Promise { + const rig = await startRig(compress); + try { + const url = `http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${rig.upstreamPort}/v1/chat/completions`; + const headers: Record = { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": conv }; + const post = (messages: Array<{ role: string; content: string }>) => + fetch(url, { method: "POST", headers, body: JSON.stringify({ model: MODEL, max_tokens: 64_000, messages: [{ role: "system", content: MAIN_SYSTEM }, ...messages] }) }); + + const msgs: Array<{ role: string; content: string }> = []; + msgs.push({ role: "user", content: filler("F1") }); + await (await post(msgs)).text(); + msgs.push({ role: "assistant", content: filler("A1") }); + msgs.push({ role: "user", content: CATALOG_V1 }); + await (await post(msgs)).text(); + msgs.push({ role: "assistant", content: filler("A2") }); + msgs.push({ role: "user", content: CATALOG_V2 }); + await (await post(msgs)).text(); + msgs.push({ role: "assistant", content: filler("A3") }); + for (let i = 4; i <= 4 + extraUserTurns * 2 - 1; i++) { + msgs.push({ role: i % 2 === 0 ? "user" : "assistant", content: filler(i % 2 === 0 ? `F${i}` : `A${i}`) }); + await (await post(msgs)).text(); + } + + const refIds = parseRefIds(rig.bodies.at(-1)!); + assert.ok(refIds.length >= 10, `expected >= 10 tagged messages, got ${refIds.length}`); + const startId = refIds[0]!; + const endId = refIds[refIds.length - 4]!; + const toolRes = await fetch(`http://127.0.0.1:${rig.proxyPort}/__bili/plugin/tool`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ + conversationId: conv, + tool: "compress", + args: { + topic: "durable-tool-identity fold", + content: [{ startId, endId, topic: "durable-tool-identity fold", summary: "durable-tool-identity fold summary: filler turns were discussed and consumed; nothing else survived this range." }], + }, + }), + }); + const toolJson = (await toolRes.json()) as Record; + assert.ok(toolRes.status === 200 && toolJson.ok === true, `compress must succeed: HTTP ${toolRes.status} ${JSON.stringify(toolJson)}`); + + msgs.push({ role: "user", content: filler("F-final") }); + await (await post(msgs)).text(); + return rig.bodies.at(-1)!; + } finally { + await closeRig(rig); + } +} + +test("e2e default: EVERY catalog instance survives the fold (all-instance parity with the old guard)", async () => { + const postFold = await runTwoCatalogFold(undefined, "dti-default"); + assert.ok(postFold.includes("[Compressed conversation section]"), "the fold must have happened"); + assert.ok(postFold.includes("git-helper"), "catalog v1 must survive (all-instance default)"); + assert.ok(postFold.includes("doc-writer"), "catalog v2 must survive (all-instance default)"); + assert.ok(!postFold.includes("A2-"), "folded filler between the catalogs must be gone"); +}); + +test("e2e takeover protectedLatestTools durable/*: the OLDER catalog folds, the newest survives", async () => { + const postFold = await runTwoCatalogFold({ protectedLatestTools: ["durable/*"] }, "dti-latest"); + assert.ok(postFold.includes("[Compressed conversation section]"), "the fold must have happened"); + assert.ok(!postFold.includes("git-helper"), "the superseded catalog v1 folds away (latest-per-identity)"); + assert.ok(postFold.includes("doc-writer"), "the newest catalog survives"); + assert.ok(!postFold.includes("A2-"), "folded filler is gone"); +}); + +test("e2e takeover exact identity durable/agents-md: another durable carrier folds, the baseline survives", async () => { + // Skill catalogs are NOT in the takeover pattern → ordinary history. + // The agents-md baseline IS → survives a fold that covers it. + const rig = await startRig({ protectedTools: ["durable/agents-md"] }); + try { + const url = `http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${rig.upstreamPort}/v1/chat/completions`; + const headers: Record = { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": "dti-exact" }; + const post = (messages: Array<{ role: string; content: string }>) => + fetch(url, { method: "POST", headers, body: JSON.stringify({ model: MODEL, max_tokens: 64_000, messages: [{ role: "system", content: MAIN_SYSTEM }, ...messages] }) }); + + const msgs: Array<{ role: string; content: string }> = []; + msgs.push({ role: "user", content: filler("F1") }); + await (await post(msgs)).text(); + msgs.push({ role: "assistant", content: filler("A1") }); + msgs.push({ role: "user", content: CATALOG_V1 }); + await (await post(msgs)).text(); + msgs.push({ role: "assistant", content: filler("A2") }); + msgs.push({ role: "user", content: AGENTS_MD }); + await (await post(msgs)).text(); + msgs.push({ role: "assistant", content: filler("A3") }); + for (let i = 4; i <= 9; i++) { + msgs.push({ role: i % 2 === 0 ? "user" : "assistant", content: filler(i % 2 === 0 ? `F${i}` : `A${i}`) }); + await (await post(msgs)).text(); + } + + const refIds = parseRefIds(rig.bodies.at(-1)!); + assert.ok(refIds.length >= 10, `expected >= 10 tagged messages, got ${refIds.length}`); + const toolRes = await fetch(`http://127.0.0.1:${rig.proxyPort}/__bili/plugin/tool`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ + conversationId: "dti-exact", + tool: "compress", + args: { + topic: "dti-exact fold", + content: [{ startId: refIds[0]!, endId: refIds[refIds.length - 4]!, topic: "dti-exact fold", summary: "dti-exact fold summary: filler turns were discussed and consumed; nothing else survived this range." }], + }, + }), + }); + const toolJson = (await toolRes.json()) as Record; + assert.ok(toolRes.status === 200 && toolJson.ok === true, `compress must succeed: HTTP ${toolRes.status} ${JSON.stringify(toolJson)}`); + + msgs.push({ role: "user", content: filler("F-final") }); + await (await post(msgs)).text(); + const postFold = rig.bodies.at(-1)!; + assert.ok(postFold.includes("[Compressed conversation section]"), "the fold must have happened"); + assert.ok(!postFold.includes("git-helper"), "the skill catalog folds away (identity not in the takeover pattern)"); + assert.ok(postFold.includes("marker X7Q"), "the agents-md baseline survives (exact-identity takeover)"); + assert.ok(!postFold.includes("A2-"), "folded filler is gone"); + } finally { + await closeRig(rig); + } +}); + +test("e2e unregistered lane: no classifier → nothing changes even with durable patterns configured", async () => { + // Lane scoping survives the architecture change: a durable/* pattern on an + // UNREGISTERED lane has nothing to project (no classifier), so catalogs + // fold away exactly as before. + const rig = await startRig({ protectedTools: ["durable/*"] }); + try { + const url = `http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${rig.upstreamPort}/v1/chat/completions`; + const headers: Record = { "content-type": "application/json", "x-bili-plugin": "test-agent", "x-bili-plugin-conversation": "dti-plain" }; + const r = await fetch(url, { + method: "POST", headers, + body: JSON.stringify({ model: MODEL, max_tokens: 64_000, messages: [{ role: "system", content: MAIN_SYSTEM }, { role: "user", content: CATALOG_V1 }] }), + }); + assert.equal(r.status, 200); + await r.text(); + // Projection sanity for the unit layer while we are here: the dsh + // classifier maps the catalog shape, the lane registry stays dsh-only. + assert.equal(dshSkillCatalogPath({ id: "x", role: "user", contentType: "text", text: CATALOG_V1 } as never), "durable/skill-catalog"); + assert.deepEqual(Object.keys(durableMessageToolPaths), ["dsh"]); + } finally { + await closeRig(rig); + } +}); diff --git a/tests/issue2419-dsh-skill-catalog.test.ts b/tests/issue2419-dsh-skill-catalog.test.ts index 9b4cab7285..453e36de9f 100644 --- a/tests/issue2419-dsh-skill-catalog.test.ts +++ b/tests/issue2419-dsh-skill-catalog.test.ts @@ -15,7 +15,7 @@ import { once } from "node:events"; import { defaultConfig } from "acp-kernel"; import { startServer } from "../src/server.ts"; import type { ProxyOptions } from "../src/config.ts"; -import { dshSkillCatalogGuard, durableMessageGuards } from "../src/durable-message-guards.ts"; +import { dshSkillCatalogPath, durableMessageToolPaths } from "../src/durable-message-guards.ts"; import { resetPersonaAnchorsForTest } from "../src/persona-anchor.ts"; import { _resetPluginStateForTest } from "../src/plugin.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; @@ -120,13 +120,13 @@ function parseRefIds(body: string): string[] { return ids; } -test("dshSkillCatalogGuard: matches the catalog marker on text messages only", () => { +test("dshSkillCatalogPath: projects the catalog marker to durable/skill-catalog on text messages only", () => { const msg = (contentType: string, text: unknown) => ({ id: "id-1", createdAt: 0, contentType, text }) as never; - assert.equal(dshSkillCatalogGuard(msg("text", CATALOG_MSG)), true, "user text carrying the catalog matches"); - assert.equal(dshSkillCatalogGuard(msg("text", "plain prose without any catalog")), false); - assert.equal(dshSkillCatalogGuard(msg("tool-call", CATALOG_MARKER)), false, "non-text content types never match"); - assert.equal(dshSkillCatalogGuard(msg("text", undefined)), false, "missing text is safe"); - assert.deepEqual(Object.keys(durableMessageGuards), ["dsh"], "allowlist stays dsh-only (KDD #9 evidence-permitlist discipline)"); + assert.equal(dshSkillCatalogPath(msg("text", CATALOG_MSG)), "durable/skill-catalog", "user text carrying the catalog projects to its identity"); + assert.equal(dshSkillCatalogPath(msg("text", "plain prose without any catalog")), undefined); + assert.equal(dshSkillCatalogPath(msg("tool-call", CATALOG_MARKER)), undefined, "non-text content types never match"); + assert.equal(dshSkillCatalogPath(msg("text", undefined)), undefined, "missing text is safe"); + assert.deepEqual(Object.keys(durableMessageToolPaths), ["dsh"], "allowlist stays dsh-only (KDD #9 evidence-permitlist discipline)"); }); /** Grow a dsh-lane conversation whose fold range POSITIONALLY covers the diff --git a/tests/issue2446-dsh-mcp-catalog.test.ts b/tests/issue2446-dsh-mcp-catalog.test.ts index 7ff8fb4314..e041dd1e44 100644 --- a/tests/issue2446-dsh-mcp-catalog.test.ts +++ b/tests/issue2446-dsh-mcp-catalog.test.ts @@ -20,7 +20,7 @@ import { once } from "node:events"; import { defaultConfig } from "acp-kernel"; import { startServer } from "../src/server.ts"; import type { ProxyOptions } from "../src/config.ts"; -import { dshMcpCatalogGuard, durableMessageGuards } from "../src/durable-message-guards.ts"; +import { dshMcpCatalogPath, durableMessageToolPaths } from "../src/durable-message-guards.ts"; import { resetPersonaAnchorsForTest } from "../src/persona-anchor.ts"; import { _resetPluginStateForTest } from "../src/plugin.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; @@ -138,20 +138,20 @@ function parseRefIds(body: string): string[] { test("dshMcpCatalogGuard: matches the catalog marker on text messages only", () => { const msg = (contentType: string, text: unknown) => ({ id: "id-1", createdAt: 0, contentType, text }) as never; - assert.equal(dshMcpCatalogGuard(msg("text", MCP_MSG)), true, "the wrapped catalog matches (marker matched on its own, wrapper-independent)"); - assert.equal(dshMcpCatalogGuard(msg("text", "bare \x3cmcp_catalog\x3e without wrapper")), true, "the bare block matches too — a wrapper rewording cannot disarm the guard"); + assert.equal(dshMcpCatalogPath(msg("text", MCP_MSG)), "durable/mcp-catalog", "the wrapped catalog projects (marker matched on its own, wrapper-independent)"); + 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(dshMcpCatalogGuard(msg("text", "plain prose mentioning an mcp catalog in lowercase")), false, "plain prose without the exact XML marker stays foldable"); - assert.equal(dshMcpCatalogGuard(msg("text", "\x3cavailable_skills\x3e…\x3c/available_skills\x3e")), false, "the #2419 skill catalog is a DIFFERENT carrier (own guard)"); - assert.equal(dshMcpCatalogGuard(msg("tool-call", MCP_MSG)), false, "non-text content types never match"); - assert.equal(dshMcpCatalogGuard(msg("text", undefined)), false, "missing text is safe"); - const dshGuard = durableMessageGuards["dsh"]; - assert.ok(dshGuard, "dsh lane is registered"); - assert.equal(dshGuard!(msg("text", MCP_MSG)), true, "lane entry covers the #2446 catalog shape"); - assert.equal(dshGuard!(msg("text", "\x3cavailable_skills\x3e…\x3c/available_skills\x3e")), true, "lane entry still covers the #2419 catalog shape"); - assert.equal(dshGuard!(msg("text", "Instructions from: AGENTS.md")), true, "lane entry still covers the #2447 baseline shape"); - assert.equal(dshGuard!(msg("text", "Current runtime context. This snapshot supersedes earlier runtime-context snapshots.")), true, "lane entry still covers the #2481 snapshot shape"); - assert.deepEqual(Object.keys(durableMessageGuards), ["dsh"], "allowlist stays dsh-only (KDD #9 evidence-permitlist discipline)"); + 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("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", "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)"); }); /** Grow a dsh-lane conversation whose fold range POSITIONALLY covers the diff --git a/tests/issue2447-dsh-workspace-instructions.test.ts b/tests/issue2447-dsh-workspace-instructions.test.ts index 668276339a..20ad935e92 100644 --- a/tests/issue2447-dsh-workspace-instructions.test.ts +++ b/tests/issue2447-dsh-workspace-instructions.test.ts @@ -19,7 +19,7 @@ import { once } from "node:events"; import { defaultConfig } from "acp-kernel"; import { startServer } from "../src/server.ts"; import type { ProxyOptions } from "../src/config.ts"; -import { dshWorkspaceInstructionsGuard, durableMessageGuards } from "../src/durable-message-guards.ts"; +import { dshWorkspaceInstructionsPath, durableMessageToolPaths } from "../src/durable-message-guards.ts"; import { resetPersonaAnchorsForTest } from "../src/persona-anchor.ts"; import { _resetPluginStateForTest } from "../src/plugin.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; @@ -125,17 +125,17 @@ function parseRefIds(body: string): string[] { return ids; } -test("dshWorkspaceInstructionsGuard: matches the baseline label on text messages only", () => { +test("dshWorkspaceInstructionsPath: projects the baseline label to durable/agents-md on text messages only", () => { const msg = (contentType: string, text: unknown) => ({ id: "id-1", createdAt: 0, contentType, text }) as never; - assert.equal(dshWorkspaceInstructionsGuard(msg("text", INSTR_MSG)), true, "the merged global+project baseline matches"); - assert.equal(dshWorkspaceInstructionsGuard(msg("text", "plain prose about workspace setup")), false); - assert.equal(dshWorkspaceInstructionsGuard(msg("text", CHANGE_MSG)), false, "lowercase reconciliation variants stay consumable history"); - assert.equal(dshWorkspaceInstructionsGuard(msg("tool-call", "Instructions from: AGENTS.md")), false, "non-text content types never match"); - assert.equal(dshWorkspaceInstructionsGuard(msg("text", undefined)), false, "missing text is safe"); - const dshGuard = durableMessageGuards["dsh"]; - assert.ok(dshGuard, "dsh lane is registered"); - assert.equal(dshGuard!(msg("text", "\x3cavailable_skills\x3e…\x3c/available_skills\x3e")), true, "lane entry still covers the #2419 catalog shape"); - assert.deepEqual(Object.keys(durableMessageGuards), ["dsh"], "allowlist stays dsh-only (KDD #9 evidence-permitlist discipline)"); + 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("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.deepEqual(Object.keys(durableMessageToolPaths), ["dsh"], "allowlist stays dsh-only (KDD #9 evidence-permitlist discipline)"); }); /** Grow a dsh-lane conversation whose fold range POSITIONALLY covers the diff --git a/tests/issue2481-dsh-runtime-context.test.ts b/tests/issue2481-dsh-runtime-context.test.ts index 42874d1719..ad4b8af6cf 100644 --- a/tests/issue2481-dsh-runtime-context.test.ts +++ b/tests/issue2481-dsh-runtime-context.test.ts @@ -20,7 +20,7 @@ import { once } from "node:events"; import { defaultConfig } from "acp-kernel"; import { startServer } from "../src/server.ts"; import type { ProxyOptions } from "../src/config.ts"; -import { dshRuntimeContextGuard, durableMessageGuards } from "../src/durable-message-guards.ts"; +import { dshRuntimeContextPath, durableMessageToolPaths } from "../src/durable-message-guards.ts"; import { resetPersonaAnchorsForTest } from "../src/persona-anchor.ts"; import { _resetPluginStateForTest } from "../src/plugin.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; @@ -132,22 +132,22 @@ function parseRefIds(body: string): string[] { return ids; } -test("dshRuntimeContextGuard: matches both snapshot variants on text messages only", () => { +test("dshRuntimeContextPath: projects both snapshot variants to durable/runtime-context on text messages only", () => { const msg = (contentType: string, text: unknown) => ({ id: "id-1", createdAt: 0, contentType, text }) as never; - assert.equal(dshRuntimeContextGuard(msg("text", RT_MSG)), true, "the populated sandbox/approval snapshot matches"); - assert.equal(dshRuntimeContextGuard(msg("text", RT_CLEARED_MSG)), true, "the cleared form matches too (it invalidates the older pinned snapshots)"); + assert.equal(dshRuntimeContextPath(msg("text", RT_MSG)), "durable/runtime-context", "the populated sandbox/approval snapshot projects"); + assert.equal(dshRuntimeContextPath(msg("text", RT_CLEARED_MSG)), "durable/runtime-context", "the cleared form projects too (it invalidates the older pinned snapshots)"); // Deliberately foldable neighbors stay consumable: - assert.equal(dshRuntimeContextGuard(msg("text", "Time sampled while preparing turn: Fri Oct 09 2026 15:20:00 CST")), false, "the per-turn time-context carrier stays foldable"); - assert.equal(dshRuntimeContextGuard(msg("text", "The approval policy changed from 'ask' to 'never' (changed by the user).")), false, "the approval-change DELTA is transient history (current state rides in the snapshot's approval:policy section)"); - assert.equal(dshRuntimeContextGuard(msg("text", "plain prose about the runtime environment")), false); - assert.equal(dshRuntimeContextGuard(msg("tool-call", RT_MSG)), false, "non-text content types never match"); - assert.equal(dshRuntimeContextGuard(msg("text", undefined)), false, "missing text is safe"); - const dshGuard = durableMessageGuards["dsh"]; - assert.ok(dshGuard, "dsh lane is registered"); - assert.equal(dshGuard!(msg("text", "\x3cavailable_skills\x3e…\x3c/available_skills\x3e")), true, "lane entry still covers the #2419 catalog shape"); - assert.equal(dshGuard!(msg("text", "Instructions from: AGENTS.md")), true, "lane entry still covers the #2447 baseline shape"); - assert.equal(dshGuard!(msg("text", RT_MSG)), true, "lane entry covers the #2481 snapshot shape"); - assert.deepEqual(Object.keys(durableMessageGuards), ["dsh"], "allowlist stays dsh-only (KDD #9 evidence-permitlist discipline)"); + assert.equal(dshRuntimeContextPath(msg("text", "Time sampled while preparing turn: Fri Oct 09 2026 15:20:00 CST")), undefined, "the per-turn time-context carrier stays foldable"); + assert.equal(dshRuntimeContextPath(msg("text", "The approval policy changed from 'ask' to 'never' (changed by the user).")), undefined, "the approval-change DELTA is transient history (current state rides in the snapshot's approval:policy section)"); + assert.equal(dshRuntimeContextPath(msg("text", "plain prose about the runtime environment")), undefined); + assert.equal(dshRuntimeContextPath(msg("tool-call", RT_MSG)), undefined, "non-text content types never match"); + 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", RT_MSG)), "durable/runtime-context", "lane entry covers the #2481 snapshot shape"); + assert.deepEqual(Object.keys(durableMessageToolPaths), ["dsh"], "allowlist stays dsh-only (KDD #9 evidence-permitlist discipline)"); }); /** Grow a dsh-lane conversation whose fold range POSITIONALLY covers the From 4454751a85258e03854cc32f0cc38b03ee40f7c0 Mon Sep 17 00:00:00 2001 From: ework-daemon Date: Sun, 11 Oct 2026 14:19:52 +0800 Subject: [PATCH 2/7] docs(tests): align stale guard names with the classifier API (#2736 review nit) --- tests/issue2446-dsh-mcp-catalog.test.ts | 8 ++++---- tests/issue2447-dsh-workspace-instructions.test.ts | 5 +++-- tests/issue2481-dsh-runtime-context.test.ts | 5 +++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/issue2446-dsh-mcp-catalog.test.ts b/tests/issue2446-dsh-mcp-catalog.test.ts index e041dd1e44..de211954bd 100644 --- a/tests/issue2446-dsh-mcp-catalog.test.ts +++ b/tests/issue2446-dsh-mcp-catalog.test.ts @@ -8,9 +8,9 @@ // folded block summary carries none of it (the reporter verified via // decompress that the originals were intact; the loss happened at fold time). // Same mechanism family as #2419/#2447/#2481, fourth carrier, first one whose -// emitter is the dsh PLUGIN ecosystem rather than dsh core. Fix: extend the -// dsh-lane durable guard in src/durable-message-guards.ts with -// dshMcpCatalogGuard. This test pins BOTH ends: the dsh lane keeps the +// emitter is the dsh PLUGIN ecosystem rather than dsh core. Fix: project the +// marker to its durable identity via the dsh-lane classifier in +// src/durable-message-guards.ts (dshMcpCatalogPath, #2736). This test pins BOTH ends: the dsh lane keeps the // catalog across a real fold executed through /__bili/plugin/tool, and an // unregistered lane still folds it away (allowlist discipline — KDD #9). import test from "node:test"; @@ -136,7 +136,7 @@ function parseRefIds(body: string): string[] { return ids; } -test("dshMcpCatalogGuard: matches the catalog marker on text messages only", () => { +test("dshMcpCatalogPath: projects the catalog marker to durable/mcp-catalog on text messages only", () => { const msg = (contentType: string, text: unknown) => ({ id: "id-1", createdAt: 0, contentType, text }) as never; assert.equal(dshMcpCatalogPath(msg("text", MCP_MSG)), "durable/mcp-catalog", "the wrapped catalog projects (marker matched on its own, wrapper-independent)"); 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"); diff --git a/tests/issue2447-dsh-workspace-instructions.test.ts b/tests/issue2447-dsh-workspace-instructions.test.ts index 20ad935e92..e6b2d946bb 100644 --- a/tests/issue2447-dsh-workspace-instructions.test.ts +++ b/tests/issue2447-dsh-workspace-instructions.test.ts @@ -7,8 +7,9 @@ // generic ACP fold treats every history message as consumable, so one // compression killed hard constraints like "read X before any web access" // permanently — same mechanism family as the #2419 skill catalog, second -// carrier missed by the #2428 whitelist. Fix: extend the dsh-lane durable -// guard in src/durable-message-guards.ts with dshWorkspaceInstructionsGuard. +// carrier missed by the #2428 whitelist. Fix: project the baseline label to +// its durable identity via the dsh-lane classifier in +// src/durable-message-guards.ts (dshWorkspaceInstructionsPath, #2736). // This test pins BOTH ends: the dsh lane keeps the baseline across a real fold // executed through /__bili/plugin/tool, and an unregistered lane still folds // it away (allowlist discipline — KDD #9: never enable wholesale). diff --git a/tests/issue2481-dsh-runtime-context.test.ts b/tests/issue2481-dsh-runtime-context.test.ts index ad4b8af6cf..e5ae8edc34 100644 --- a/tests/issue2481-dsh-runtime-context.test.ts +++ b/tests/issue2481-dsh-runtime-context.test.ts @@ -8,8 +8,9 @@ // believes is already present) — same mechanism family as the #2419 skill // catalog and the #2447 workspace-instructions baseline, third carrier missed // by the #2428 text-marker whitelist (its body carries neither -// "Instructions from:" nor \x3cavailable_skills\x3e). Fix: extend the dsh-lane -// durable guard in src/durable-message-guards.ts with dshRuntimeContextGuard. +// "Instructions from:" nor \x3cavailable_skills\x3e). Fix: project the snapshot +// to its durable identity via the dsh-lane classifier in +// src/durable-message-guards.ts (dshRuntimeContextPath, #2736). // This test pins BOTH ends: the dsh lane keeps the snapshot across a real fold // executed through /__bili/plugin/tool, and an unregistered lane still folds // it away (allowlist discipline — KDD #9: never enable wholesale). From c2a19471faee366120e15fbb70b7b698c35a3ee6 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sun, 11 Oct 2026 14:59:17 +0800 Subject: [PATCH 3/7] feat(#2446): default durable policy = latest-per-identity (pi-aligned single live copy) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Switch withDurableProtection's injected default from protectedTools "durable/*" (all-instance) to protectedLatestTools "durable/*": every carrier keeps exactly its newest instance pinned (skill/MCP visibility can never be lost — the #2419 fingerprint gate never resends an unchanged catalog), while superseded instances (runtime snapshots, re-based AGENTS.md lines) fold normally. This matches the pi reference architecture, where the skill catalog and AGENTS.md live in the system prompt as a single live copy, and cuts the pinned tail (runtime-context snapshots accumulate per state change). Explicit takeover unchanged: any durable pattern in protectedTools/protectedLatestTools takes over the whole policy verbatim. Owner decision 2026-10-11 (pi/dsh-native comparison: pi keeps both carriers alive forever via system-prompt residency; dsh-native compaction protects neither — its summary template has no skill/instructions section and there is no re-injection). --- src/config.ts | 6 ++-- src/durable-message-guards.ts | 19 +++++++++---- tests/durable-tool-identity.test.ts | 44 ++++++++++++++++++----------- 3 files changed, 44 insertions(+), 25 deletions(-) diff --git a/src/config.ts b/src/config.ts index ff34a0e4ba..c00c952c83 100644 --- a/src/config.ts +++ b/src/config.ts @@ -255,8 +255,10 @@ export type CompressSettings = { * same pattern machinery governs them: `"durable/*"` keeps the LATEST * instance of every carrier (all four are cumulative snapshots — the * older catalogs/baselines fold away). Default when nothing is - * configured: all-instance `durable/*` protection in `protectedTools` - * (behavior-equivalent to the boolean guards this replaces). */ + * configured: `durable/*` latest-per-identity in + * `protectedLatestTools` (pi-reference single-live-copy semantics — + * skill/MCP visibility can never be lost, superseded snapshots + * fold). */ /** Tool patterns whose tool-calls AND paired results are NEVER * compressed — every instance, full history (kernel `protectedTools`, * hard exclusion: matching refs render as `BLOCKED`, so neither suggested diff --git a/src/durable-message-guards.ts b/src/durable-message-guards.ts index a8702074c4..c238b341ec 100644 --- a/src/durable-message-guards.ts +++ b/src/durable-message-guards.ts @@ -29,8 +29,11 @@ // latest-per-path via protectedLatestTools. This is the #1947 principle // (project client dialects to canonical names, protect by pattern) extended // from the tool channel to the text channel. withDurableProtection() wires a -// classifier onto a Config and defaults the policy to `durable/*` all-instance -// protection — byte-equivalent to the previous boolean guards — unless the +// classifier onto a Config and defaults the policy to `durable/*` +// latest-per-identity protection (protectedLatestTools — exactly one live +// instance per carrier, superseded instances stay foldable; mirrors the pi +// reference architecture, where the skill catalog and AGENTS.md live in the +// system prompt as a single live copy) — unless the // user has already classified any `durable` pattern themselves (whole-identity // takeover: configuring one durable pattern takes over the entire durable // policy). @@ -151,9 +154,13 @@ function classifiesDurable(patterns: readonly string[] | undefined): boolean { } /** Attach a lane classifier to a kernel Config and default the durable - * policy. Default = `durable/*` all-instance protection in protectedTools — - * behavior-equivalent to the boolean guards this replaces (every carrier - * instance pinned). If the user already configured any `durable` pattern in + * policy. Default = `durable/*` latest-per-identity protection in + * protectedLatestTools: every carrier keeps exactly its newest instance + * pinned (skill/MCP visibility can never be lost — the #2419 fingerprint gate + * never resends an unchanged catalog), while superseded instances (runtime + * snapshots, re-based AGENTS.md lines) fold normally — the same single-live- + * copy semantics pi gets from keeping carriers in the system prompt. If the + * user already configured any `durable` pattern in * protectedTools/protectedLatestTools, their policy wins verbatim * (whole-identity takeover: one durable pattern anywhere takes over the * whole durable policy — no per-carrier merging). */ @@ -163,5 +170,5 @@ export function withDurableProtection(config: Config, classifier: DurableMessage if (classified) { return { ...config, messagePathOf: classifier }; } - return { ...config, protectedTools: [...(config.protectedTools ?? []), "durable/*"], messagePathOf: classifier }; + return { ...config, protectedLatestTools: [...(config.protectedLatestTools ?? []), "durable/*"], messagePathOf: classifier }; } diff --git a/tests/durable-tool-identity.test.ts b/tests/durable-tool-identity.test.ts index 90d9027788..76d1f008a8 100644 --- a/tests/durable-tool-identity.test.ts +++ b/tests/durable-tool-identity.test.ts @@ -8,16 +8,18 @@ // same knobs, same semantics as the tool channel (#1947). // // This suite pins the POLICY contract: -// 1. default: bili ensures `durable/*` all-instance protection (parity -// with the boolean guards this replaces — every instance pinned); +// 1. default: bili ensures `durable/*` latest-per-identity protection +// (protectedLatestTools) — exactly one live instance per carrier, the +// pi-reference single-live-copy semantics; superseded instances fold; // 2. takeover: ANY `durable` pattern in the user's protectedTools or // protectedLatestTools takes over the whole durable policy — bili only // attaches the classifier, adds nothing; -// 3. e2e: default keeps every catalog instance across a real fold; -// `protectedLatestTools: ["durable/*"]` folds the older catalog and -// keeps the newest; `protectedTools: ["durable/agents-md"]` (exact -// identity takeover) lets the skill catalog fold while the baseline -// survives. +// 3. e2e: default folds the older catalog and keeps the newest (skill +// visibility can never be lost); explicit takeover reproduces the same +// outcome without double injection; `protectedTools: ["durable/*"]` +// (all-instance takeover) keeps every catalog instance; and +// `protectedTools: ["durable/agents-md"]` (exact identity takeover) +// lets the skill catalog fold while the baseline survives. import test from "node:test"; import assert from "node:assert/strict"; import http from "node:http"; @@ -48,16 +50,16 @@ function filler(tag: string): string { // ---------------------------------------------------------------- units ---- -test("withDurableProtection: default ensures durable/* all-instance + classifier hook", () => { +test("withDurableProtection: default ensures durable/* latest-per-identity + classifier hook", () => { const cl = durableMessageToolPaths["dsh"]!; const base = defaultConfig(200_000); const out = withDurableProtection(base, cl); - assert.deepEqual(out.protectedTools, ["durable/*"], "default adds durable/* to protectedTools"); + assert.deepEqual(out.protectedLatestTools, ["durable/*"], "default adds durable/* to protectedLatestTools"); assert.equal(out.messagePathOf, cl, "classifier attached"); - assert.equal(out.protectedLatestTools, base.protectedLatestTools, "latest list untouched"); + assert.equal(out.protectedTools, base.protectedTools, "all-instance list untouched"); // Existing tool-channel patterns are preserved, durable/* appended: - const out2 = withDurableProtection({ ...base, protectedTools: ["read"] }, cl); - assert.deepEqual(out2.protectedTools, ["read", "durable/*"]); + const out2 = withDurableProtection({ ...base, protectedLatestTools: ["skill/*"] }, cl); + assert.deepEqual(out2.protectedLatestTools, ["skill/*", "durable/*"]); }); test("withDurableProtection: a durable pattern in protectedTools = whole-identity takeover", () => { @@ -79,7 +81,7 @@ test("withDurableProtection: idempotent — no duplicate durable/* on re-apply", const cl = durableMessageToolPaths["dsh"]!; const once = withDurableProtection(defaultConfig(200_000), cl); const twice = withDurableProtection(once, cl); - assert.deepEqual(twice.protectedTools, ["durable/*"]); + assert.deepEqual(twice.protectedLatestTools, ["durable/*"]); }); // ------------------------------------------------------------------ e2e ---- @@ -223,15 +225,15 @@ async function runTwoCatalogFold( } } -test("e2e default: EVERY catalog instance survives the fold (all-instance parity with the old guard)", async () => { +test("e2e default (latest-per-identity): the OLDER catalog folds, the newest survives — skill visibility is never lost", async () => { const postFold = await runTwoCatalogFold(undefined, "dti-default"); assert.ok(postFold.includes("[Compressed conversation section]"), "the fold must have happened"); - assert.ok(postFold.includes("git-helper"), "catalog v1 must survive (all-instance default)"); - assert.ok(postFold.includes("doc-writer"), "catalog v2 must survive (all-instance default)"); + assert.ok(!postFold.includes("git-helper"), "superseded catalog v1 folds away (pi-style single live copy)"); + assert.ok(postFold.includes("doc-writer"), "the newest catalog survives (skill visibility retained)"); assert.ok(!postFold.includes("A2-"), "folded filler between the catalogs must be gone"); }); -test("e2e takeover protectedLatestTools durable/*: the OLDER catalog folds, the newest survives", async () => { +test("e2e takeover protectedLatestTools durable/*: same outcome, no double injection", async () => { const postFold = await runTwoCatalogFold({ protectedLatestTools: ["durable/*"] }, "dti-latest"); assert.ok(postFold.includes("[Compressed conversation section]"), "the fold must have happened"); assert.ok(!postFold.includes("git-helper"), "the superseded catalog v1 folds away (latest-per-identity)"); @@ -239,6 +241,14 @@ test("e2e takeover protectedLatestTools durable/*: the OLDER catalog folds, the assert.ok(!postFold.includes("A2-"), "folded filler is gone"); }); +test("e2e takeover protectedTools durable/* (all-instance): EVERY catalog instance survives the fold", async () => { + const postFold = await runTwoCatalogFold({ protectedTools: ["durable/*"] }, "dti-all"); + assert.ok(postFold.includes("[Compressed conversation section]"), "the fold must have happened"); + assert.ok(postFold.includes("git-helper"), "catalog v1 survives (all-instance takeover)"); + assert.ok(postFold.includes("doc-writer"), "catalog v2 survives (all-instance takeover)"); + assert.ok(!postFold.includes("A2-"), "folded filler between the catalogs must be gone"); +}); + test("e2e takeover exact identity durable/agents-md: another durable carrier folds, the baseline survives", async () => { // Skill catalogs are NOT in the takeover pattern → ordinary history. // The agents-md baseline IS → survives a fold that covers it. From a5a29a1cc8f0873138e28b1e9a4fa6c57a67576e Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sun, 11 Oct 2026 15:20:21 +0800 Subject: [PATCH 4/7] test(#2111): harvest the pi-ws decompress block id from the COMPRESSED BLOCKS section MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The bare /\bb(\d+)\b/ harvest matched the ACTIVE SURFACE build stamp "(b)" first whenever the CI merge ref abbreviates to 'b'+digits (#2736 run 38119865934: merge b288144 → decompress "b288144" → block not found → rebuilt-history assertion failed twice, deterministic per-merge-ref). Anchor to the COMPRESSED BLOCKS section so the block listing is the only harvestable surface. --- tests/e2e/e2e-pi-codex-ws.test.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/e2e/e2e-pi-codex-ws.test.ts b/tests/e2e/e2e-pi-codex-ws.test.ts index 5e306872a7..754bc3f012 100644 --- a/tests/e2e/e2e-pi-codex-ws.test.ts +++ b/tests/e2e/e2e-pi-codex-ws.test.ts @@ -519,7 +519,12 @@ test("A: explicit websocket — intercepted, stamped, compress/decompress round ok("status", await piRun(ctx, "WS_STATUS post-fold recall", { resume: true })); const statusRow = mock.rows.filter((r) => r.transport === "ws").find((r) => toolResult(r, "acp_status")); assert.ok(statusRow, "acp_status executed after the fold"); - const blockId = toolResult(statusRow!, "acp_status")!.match(/\bb(\d+)\b/)?.[1]; + // Harvest the block id from the COMPRESSED BLOCKS section only. The + // naive /\bb(\d+)\b/ lottery: the ACTIVE SURFACE line carries the build + // stamp "(b)", and a PR merge ref whose abbreviated SHA is 'b' + // followed by digits (e.g. b288144) matches FIRST and hijacks the harvest + // (observed on #2736, run 38119865934: decompress b288144 → not found). + const blockId = toolResult(statusRow!, "acp_status")!.match(/COMPRESSED BLOCKS[^\n]*\n[\s\S]*?\n\s+b(\d+) \(/)?.[1]; assert.ok(blockId, "acp_status report lists the compressed block (need its bN ref)"); const statusJson = JSON.stringify(statusRow!.full); assert.ok(!statusJson.includes(SENTINEL), "post-fold request no longer carries the folded bulk"); From d78bee636d35bd1763baf330ebbc5d37148862da Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sun, 11 Oct 2026 15:49:57 +0800 Subject: [PATCH 5/7] docs(src): align stale runtime-context comment with the latest-per-identity default (#2736 review nit) --- src/durable-message-guards.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/durable-message-guards.ts b/src/durable-message-guards.ts index c238b341ec..6edc001cea 100644 --- a/src/durable-message-guards.ts +++ b/src/durable-message-guards.ts @@ -119,8 +119,8 @@ export function dshMcpCatalogPath(msg: CoreMessage): string | undefined { * The host re-emits only when the runtime context CHANGES (the per-turn * time-context carrier deliberately stays foldable), so one fold of the * latest snapshot silently drops the session's sandbox/approval semantics - * forever. Each snapshot is tiny (~100–400 chars); superseded ones keep - * pinning harmlessly under the all-instance default because the newest + * forever. Each snapshot is tiny (~100–400 chars); superseded ones fold + * harmlessly under the latest-per-identity default because the newest * carries the supersedes clause. */ export function dshRuntimeContextPath(msg: CoreMessage): string | undefined { return msg.contentType === "text" && typeof msg.text === "string" && msg.text.includes("Current runtime context") From f3701bbc4817641cc8d7fde7d9b3550858f1c293 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sun, 11 Oct 2026 15:57:42 +0800 Subject: [PATCH 6/7] kernel: bump acp-kernel to 0.0.117 after merge of master (master already took 0.0.116) (#2736) --- kernel/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/package.json b/kernel/package.json index ceeabfc691..281ba19981 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.116", + "version": "0.0.117", "description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.", "license": "MIT", "author": "ranxianglei", From 001e4cc835e2ab29e5d69a191b358634edd84cfa Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sun, 11 Oct 2026 16:00:13 +0800 Subject: [PATCH 7/7] chore(#2736): sync package-lock acp-kernel version to 0.0.117 (lock-sync) --- package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index a65e925f7f..e89a869d46 100644 --- a/package-lock.json +++ b/package-lock.json @@ -48,7 +48,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.116", + "version": "0.0.117", "dev": true, "license": "MIT", "devDependencies": {