diff --git a/src/config.ts b/src/config.ts index ef473e7a98..709659f113 100644 --- a/src/config.ts +++ b/src/config.ts @@ -389,8 +389,13 @@ export type CompressSettings = { * 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: `durable/*` latest-per-identity in + * older catalogs/baselines fold away). The agents-md carrier is the one + * exception to "one identity per carrier": its mid-session REVISIONS + * (`Updated|Additional instructions from:` / `Instructions removed:`) + * project to `durable/agents-md-update/` so a revision can never + * demote the multi-file baseline it does not fully carry — latest-per- + * path there means "the newest revision of EACH touched file" (#2447). + * Default when nothing is configured: `durable/*` latest-per-identity in * `protectedLatestTools` (pi-reference single-live-copy semantics — * skill/MCP visibility can never be lost, superseded snapshots * fold). */ @@ -420,7 +425,10 @@ export type CompressSettings = { * (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. */ + * normally. Note the agents-md revisions carry their own identity, so + * that exact pattern leaves them foldable; add + * `"durable/agents-md-update/*"` (or use `"durable/*"`) to keep the + * whole family. 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 43ae3d8ef9..52f7b99ab4 100644 --- a/src/durable-message-guards.ts +++ b/src/durable-message-guards.ts @@ -90,6 +90,47 @@ export function dshSkillCatalogPath(msg: CoreMessage): string | undefined { : undefined; } +/** dsh workspace-instructions REVISION identity: the reconciliation shapes of + * the #2447 carrier — @deepseek-ai/dsh-agent-instructions' changedSectionText + * emits one message per changed file, framed by the same + * \x3csystem-reminder\x3e wrapper but with a lowercase marker (or a different + * verb) than the baseline's `Instructions from:`: + * `Updated instructions from: ` — file content edited + * `Additional instructions from: ` — a file discovered LATER (its + * content was never in the + * baseline at all) + * `Instructions removed: ` — the file no longer applies + * These are NOT ordinary consumable history, which is what the earlier + * case-sensitive match assumed. baselineIdentity excludes file CONTENT + * (lib/index.js:39: projectRoot + discovery/budget config only), so a + * mid-session edit never re-emits a baseline — the revision is the ONLY + * carrier of the change. Folding it leaves the pinned, superseded baseline in + * context and drops the correction: the model keeps obeying rules the user + * already rewrote, and cannot tell it lost the update. That is strictly worse + * than folding both (then the model at least knows it has no rules). + * + * IDENTITY IS PER PATH, deliberately not one shared `durable/agents-md` + * bucket: the default policy is latest-per-identity (protectedLatestTools), + * so a shared bucket would let the newest revision DEMOTE the baseline — and + * a baseline merges EVERY discovered file into one message, while a revision + * carries only its own file. On a multi-file session that trade loses rules: + * measured on a real base+d1 pair, the baseline carried 2 files (global + + * project root) and the `Additional` revision carried 1, so a shared bucket + * would have gone from "keep 2, lose 1" to "keep 1, lose 2". Per-path + * identities keep each file's newest version and bound the pinned set to + * "one baseline + one revision per touched file". + * + * `durable/*` still matches the suffixed path: matchToolPath compares only + * the first segment for a `/*` pattern. Matched case-sensitively on + * purpose: the three shapes are fixed DSH template literals, so case folding + * would only grow the false-positive surface. #2766 role gate as above. */ +function agentsMdRevisionPath(text: string): string | undefined { + const m = /^\s*\x3csystem-reminder\x3e\s*(?:(?:Updated|Additional)\s+instructions\s+from|Instructions\s+removed)\s*:[ \t]*([^\n]+)/.exec(text); + if (m === null) return undefined; + const path = m[1]!.trim(); + return path.length > 0 ? `durable/agents-md-update/${path}` : undefined; +} + /** 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 @@ -99,16 +140,21 @@ export function dshSkillCatalogPath(msg: CoreMessage): string | undefined { * baselineIdentity (projectRoot + discovery/budget config, NOT file contents) * changes, so one fold of it silently kills hard constraints like "read X * before any web access" forever. Marker-over-wrapper rationale as #2419. - * The match is case-sensitive on purpose — reconciliation change messages - * use lowercase variants ("Updated instructions from:") that remain ordinary - * consumable history. At most ~one such baseline lives per session - * (replacements are rare identity changes), so pinning every match is - * bounded. #2766: assistant-role messages are never carriers (see - * dshSkillCatalogPath). */ + * At most ~one such baseline lives per session (replacements are rare + * identity changes), so pinning every match is bounded. #2766: assistant-role + * messages are never carriers (see dshSkillCatalogPath). + * + * THE SAME CARRIER HAS A SECOND SHAPE — its REVISIONS (see + * agentsMdRevisionPath above): the baseline projects to `durable/agents-md`, + * each mid-session revision to its own per-path identity. The baseline marker + * stays case-sensitive for a different reason than before: the revision + * shapes are classified FIRST, because a revision body can itself contain the + * uppercase label (a user documenting bili's matching string puts + * `Instructions from:` inside their own AGENTS.md) and would otherwise be + * mistaken for a baseline. */ export function dshWorkspaceInstructionsPath(msg: CoreMessage): string | undefined { - return msg.role !== "assistant" && msg.contentType === "text" && typeof msg.text === "string" && msg.text.includes("Instructions from:") - ? "durable/agents-md" - : undefined; + if (msg.role === "assistant" || msg.contentType !== "text" || typeof msg.text !== "string") return undefined; + return agentsMdRevisionPath(msg.text) ?? (msg.text.includes("Instructions from:") ? "durable/agents-md" : undefined); } /** dsh mcp-catalog identity (#2446): the durable MCP-server catalog injected @@ -447,17 +493,22 @@ export const oneShotCarrierPaths: Record = { }; /** #5142: raw-content pre-filter markers for the stored-row backfill — the - * exact marker strings of the twelve dsh classifiers above (fifteen markers: - * the runtime-context family contributes two — the pre-0.2.2-rc.3 "Current - * runtime context" literal and the 0.2.2-rc.3 lead-in without its trailing - * colon, which both the first-snapshot and re-send intros share, #7085 — the - * mnemon family contributes two, the image-offload family contributes two), so a - * unified-store row can be screened with cheap substring checks BEFORE - * JSON.parse (rows may carry megabytes of base64 media). Lockstep with the - * classifiers is enforced in tests/issue5142-expected-rebase.test.ts. */ + * exact marker strings of the twelve dsh classifiers above (eighteen markers: + * the workspace-instructions family contributes four, the runtime-context + * family two — the pre-0.2.2-rc.3 "Current runtime context" literal and the + * 0.2.2-rc.3 lead-in without its trailing colon, which both the + * first-snapshot and re-send intros share, #7085 — the mnemon family + * contributes two, the agent-relay family two, the image-offload family + * two), so a unified-store row can be screened with cheap substring checks + * BEFORE JSON.parse (rows may carry megabytes of base64 media). Lockstep + * with the classifiers is enforced in + * tests/issue5142-expected-rebase.test.ts. */ export const dshCarrierRawMarkers: readonly string[] = [ "\x3cavailable_skills\x3e", "Instructions from:", + "Updated instructions from:", + "Additional instructions from:", + "Instructions removed:", "Current runtime context", "The following information describes your current session", "\x3cmcp_catalog\x3e", diff --git a/tests/issue2447-dsh-workspace-instructions.test.ts b/tests/issue2447-dsh-workspace-instructions.test.ts index 43fafc21cf..f6e1c5c638 100644 --- a/tests/issue2447-dsh-workspace-instructions.test.ts +++ b/tests/issue2447-dsh-workspace-instructions.test.ts @@ -135,7 +135,38 @@ test("dshWorkspaceInstructionsPath: projects the baseline label to durable/agent const msg = (contentType: string, text: unknown) => ({ id: "id-1", createdAt: 0, contentType, text }) as never; 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"); + // Revisions are the SAME durable carrier, not consumable history: the + // baseline is re-emitted only when baselineIdentity changes (projectRoot + + // discovery/budget config, NOT file contents), so a mid-session edit rides + // this message ALONE. Identity is per path so a revision cannot demote the + // (multi-file) baseline under the default latest-per-identity policy. + assert.equal(dshWorkspaceInstructionsPath(msg("text", CHANGE_MSG)), "durable/agents-md-update/AGENTS.md", "an Updated revision projects to its own per-path identity"); + assert.equal( + dshWorkspaceInstructionsPath(msg("text", `\x3csystem-reminder\x3e\nAdditional instructions from: neighbor-help-frontend/AGENTS.md\n\nThese instructions apply to work under \x60neighbor-help-frontend\x60.\n\nmost-specific rules first\n\x3c/system-reminder\x3e`)), + "durable/agents-md-update/neighbor-help-frontend/AGENTS.md", + "an Additional revision (a file the baseline never carried) projects too", + ); + assert.equal( + dshWorkspaceInstructionsPath(msg("text", `\x3csystem-reminder\x3e\nInstructions removed: AGENTS.md\n\nThe previously loaded instructions from this file no longer apply.\n\x3c/system-reminder\x3e`)), + "durable/agents-md-update/AGENTS.md", + "a removal notice projects (folding it would keep applying deleted rules)", + ); + // The baseline's BODY may quote the uppercase label (documenting this very + // matcher is a realistic AGENTS.md line). Guards against a silent no-op: + // if the injection ever stops landing, the assertion below would pass + // vacuously on an unmodified fixture. + const baselineQuotingLabel = INSTR_MSG.replace("## \u89c4\u5219", "## \u89c4\u5219\n(see bili's `Instructions from:` matcher)"); + assert.notEqual(baselineQuotingLabel, INSTR_MSG, "fixture injection must actually land"); + assert.equal( + dshWorkspaceInstructionsPath(msg("text", baselineQuotingLabel)), + "durable/agents-md", + "a baseline whose BODY quotes the uppercase label is still a baseline", + ); + assert.equal( + dshWorkspaceInstructionsPath(msg("text", `\x3csystem-reminder\x3e\nUpdated instructions from: AGENTS.md\n\nThe old copy said \x60Instructions from: AGENTS.md\x60.\n\x3c/system-reminder\x3e`)), + "durable/agents-md-update/AGENTS.md", + "the revision shape wins even when its body quotes the uppercase baseline label", + ); 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"]; @@ -148,7 +179,7 @@ test("dshWorkspaceInstructionsPath: projects the baseline label to durable/agent * instructions baseline (it sits mid-history between two tagged fillers), * execute a real compress through /__bili/plugin/tool, and return the next * turn's upstream body. */ -async function runFoldScenario(agent: string, conv: string): Promise<{ postFoldBody: string; toolJson: Record }> { +async function runFoldScenario(agent: string, conv: string, revision?: string): Promise<{ postFoldBody: string; toolJson: Record }> { const rig = await startRig(); try { const url = `http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${rig.upstreamPort}/v1/chat/completions`; @@ -165,6 +196,13 @@ async function runFoldScenario(agent: string, conv: string): Promise<{ postFoldB msgs.push({ role: "user", content: INSTR_MSG }); await (await post(msgs)).text(); msgs.push({ role: "assistant", content: filler("A2") }); + // …optionally a mid-session REVISION of the same carrier (same shape as + // dsh's changedSectionText when the file is edited after load)… + if (revision !== undefined) { + msgs.push({ role: "user", content: revision }); + await (await post(msgs)).text(); + msgs.push({ role: "assistant", content: filler("A2b") }); + } // …and turns 3–8 grow past it. for (let i = 3; i <= 8; i++) { msgs.push({ role: "user", content: filler(`F${i}`) }); @@ -216,6 +254,21 @@ test("dsh lane (#2447): the workspace-instructions baseline survives a fold that assert.ok(!postFoldBody.includes("F5-"), "folded filler after the baseline must be gone"); }); +test("dsh lane: a mid-session REVISION of the instructions survives a fold that covers it", async () => { + // The baseline above proves the FIRST copy of the rules is pinned. This one + // pins the SECOND half of the contract: dsh never re-emits a baseline when + // file CONTENTS change (baselineIdentity excludes content), so this + // `Updated instructions from:` message is the only carrier of the new + // rules. Before the revision identity landed, the fold dropped it and left + // the model with the superseded baseline alone. + const { postFoldBody } = await runFoldScenario("dsh", "i2447-dsh-revision", CHANGE_MSG); + assert.ok(postFoldBody.includes("[Compressed conversation section]"), "the fold must have happened (protection may not veto the whole range)"); + assert.ok(postFoldBody.includes("always run lint AND typecheck before committing"), "the revised rule text must survive the fold (it exists nowhere else)"); + assert.ok(postFoldBody.includes(RULE_MARKER), "the baseline survives alongside it (separate per-path identity)"); + assert.ok(!postFoldBody.includes("A1-"), "folded filler before the carrier must be gone (the fold really consumed its range)"); + assert.ok(!postFoldBody.includes("F5-"), "folded filler after the carrier must be gone"); +}); + test("unregistered lane: without the guard the baseline folds away (pins lane scoping)", async () => { const { postFoldBody } = await runFoldScenario("test-agent", "i2447-plain"); assert.ok(postFoldBody.includes("[Compressed conversation section]"), "the fold must have happened"); diff --git a/tests/issue5142-expected-rebase.test.ts b/tests/issue5142-expected-rebase.test.ts index 4358e22710..c87c818024 100644 --- a/tests/issue5142-expected-rebase.test.ts +++ b/tests/issue5142-expected-rebase.test.ts @@ -469,15 +469,26 @@ describe("#5142 carrier classifier units", () => { test("dshCarrierRawMarkers stay in lockstep with the dsh lane classifiers", () => { const cls = oneShotCarrierPaths.dsh; assert.ok(cls !== undefined, "dsh lane must have a classifier registry entry"); - assert.ok(dshCarrierRawMarkers.length >= 15, "fifteen raw markers for the twelve known dsh carrier families (runtime-context, mnemon, image-offload each contribute two)"); + assert.ok(dshCarrierRawMarkers.length >= 18, "eighteen raw markers for the twelve known dsh carrier families"); // Markers that only occur mid-template need their fixed left context to // be a standalone classifiable shape (the relay wrapper's sender-id slot // varies, so the joint alone cannot classify). const MARKER_CONTEXT: Record = { " sent a message: ": "Agent session-1", + "Updated instructions from:": "\x3csystem-reminder\x3e ", + "Additional instructions from:": "\x3csystem-reminder\x3e ", + "Instructions removed:": "\x3csystem-reminder\x3e ", + }; + // The workspace-instructions classifiers are anchored AND require a + // non-empty path after the colon, so those markers also need a sample + // path appended (their real rows always carry one). + const MARKER_SUFFIX: Record = { + "Updated instructions from:": "/x/AGENTS.md", + "Additional instructions from:": "/x/AGENTS.md", + "Instructions removed:": "/x/AGENTS.md", }; for (const marker of dshCarrierRawMarkers) { - const msg = { id: "lk", role: "user" as const, contentType: "text" as const, text: `${MARKER_CONTEXT[marker] ?? ""}${marker}` } as CoreMessage; + const msg = { id: "lk", role: "user" as const, contentType: "text" as const, text: `${MARKER_CONTEXT[marker] ?? ""}${marker}${MARKER_SUFFIX[marker] ?? ""}` } as CoreMessage; assert.notEqual(cls(msg), undefined, `marker ${JSON.stringify(marker)} no longer matches any dsh classifier`); } });