Merge pull request #5473 from ranxianglei/2026-11-01_dsh-injection-slot-exclusion

[PR] fix: classify remaining dsh user-role injections so they cannot steal the last-user anchor (#5173)
This commit is contained in:
ranxianglei
2026-11-06 10:03:32 +08:00
committed by GitHub
4 changed files with 340 additions and 12 deletions
+115 -8
View File
@@ -166,8 +166,9 @@ const TIME_CONTEXT_MARKER = "Time sampled while preparing turn";
/** dsh per-turn time-context carrier (#5142, superseding #5109's naming):
* the timestamp notice injected before each turn (marker above).
* Deliberately FOLDABLE by design — unlike the four durable carriers it
* carries no state beyond its own turn and must NEVER be pinned, so it
* stays OUT of dshDurableToolPath / kernel protection. Its problem is
* carries no state beyond its own turn and must NEVER be pinned, so its
* path lives in the oneshot/ namespace where no protection pattern matches
* it (kernel-foldable) while still riding dshDurableToolPath. Its problem is
* retirement, not foldability: the host re-sends it only on the turn it
* was sampled for, so once folded it leaves the resent history FOREVER —
* a permanent missing covered id that fold-reconcile used to count as
@@ -225,6 +226,93 @@ export function dshToolJobNoticePath(msg: CoreMessage): string | undefined {
: undefined;
}
/** #5173 reporter jsonl measurement (source.kind=dsh-mnemon, omdsh-dev/dsh-mnemon
* memory plugin): recalled-memory injections ride user-role messages prefixed
* `[MNEMON] ` or `[MNEMON RUNTIME MEMORY` (both observed in the production
* session journal that filed the issue; the plugin ships built, so the
* literal is not source-verifiable — stability owned by the plugin author,
* same bar as the #2446 mcp-catalog entry; if the prefix ever changes the
* classifier simply stops matching: status quo ante, no harm done). One-shot
* per recall event: a folded instance is never re-sent verbatim, so its
* retirement is accounted via oneShotCarrierPaths. Prefix match (asciiCI) on
* the leading-trimmed text — trimStart on purpose because the marker's TRAILING
* space is part of the shape (it separates the tag from the payload) while a
* host may still pad the front. Slot exclusion is the point (#5173): a mnemon
* line landing right after the newest human request steals the Rule-3
* last-user anchor (#5109). #2766: assistant-role messages are never carriers. */
export function dshMnemonPath(msg: CoreMessage): string | undefined {
return msg.role !== "assistant" && msg.contentType === "text" && typeof msg.text === "string" &&
(startsWithAsciiCI(msg.text.trimStart(), "[MNEMON] ") || startsWithAsciiCI(msg.text.trimStart(), "[MNEMON RUNTIME MEMORY"))
? "oneshot/mnemon"
: undefined;
}
/** dsh background-subagent settlement notice (#5173, source evidence:
* deepseek-harness MIT packages/subagent/subagent/src/continuation-messages.ts
* settlementSummary(): subject `Background subagent ${childId}` followed by a
* fixed ending (" finished and will do no further work unless you send it
* more." / " was stopped before it finished." / " ran out of room before it
* finished."), spliced into the PARENT session as a user-role message when a
* child settles. One-shot: emitted once per settlement and never re-sent —
* retirement accounted via oneShotCarrierPaths. Prefix match (asciiCI) on the
* leading-trimmed text INCLUDING the marker's trailing space, which keeps
* human prose merely starting with the words ("Background subagents are
* handy") unmatched. Slot exclusion is the point (#5173): a settlement notice
* landing right after the newest human request steals the Rule-3 last-user
* anchor (#5109). #2766: assistant-role messages are never carriers. */
export function dshSubagentSettledPath(msg: CoreMessage): string | undefined {
return msg.role !== "assistant" && msg.contentType === "text" && typeof msg.text === "string" &&
startsWithAsciiCI(msg.text.trimStart(), "Background subagent ")
? "oneshot/subagent-settled"
: undefined;
}
/** dsh cross-agent relay message (#5173, source evidence: deepseek-harness MIT
* packages/subagent/subagent/src/continuation-messages.ts wrapper
* `{ type: "text", text: \`Agent ${sender.id} sent a message: \` }` + the
* sender's content parts, spliced into the recipient session as a user-role
* message). One-shot: sent once per relay and never re-sent — retirement
* accounted via oneShotCarrierPaths. Both halves case-sensitive on purpose
* (deterministic template literal in the emitter, like the case-sensitive
* `Instructions from:` of #2447): the `Agent ` lead-in plus the distinctive
* ` sent a message: ` joint. A literal user echo of the phrase is benign in
* both directions — slot exclusion only, the message stays foldable. Slot
* exclusion is the point (#5173): a relay landing right after the newest
* human request steals the Rule-3 last-user anchor (#5109). #2766:
* assistant-role messages are never carriers. */
export function dshAgentRelayPath(msg: CoreMessage): string | undefined {
return msg.role !== "assistant" && msg.contentType === "text" && typeof msg.text === "string" &&
msg.text.startsWith("Agent ") && msg.text.includes(" sent a message: ")
? "oneshot/agent-relay"
: undefined;
}
/** dsh request-image handle annotation (#5142 residual, session evidence
* 2026-11-04): the user-role text cell dsh emits beside a pasted image's
* request copy — "Image NAME (sha256:DIGEST); request preview WxHpx."
* plus the normalized-copy access note (requestImageHandleText in
* @deepseek-ai/dsh-llm/lib/types/content.js).
* It rides ONLY the turn its attachment is rendered for; from the next turn
* the same wire slot carries the offload placeholder instead ("[image omitted
* to fit request image limits; …", offloadedImageText), so the handle retires
* one-shot exactly like the ptc paste template above. Session evidence: in a
* cross-provider replay drop this handle was the SINGLE non-reasoning id left
* in an 111-id missing set, and its absence from the expected record kept the
* #2193 reasoning-only verdict from firing for the whole episode — one
* unaccounted text id turned a tolerated switch into a per-turn drift warn.
* Single-marker discipline like every other classifier here: the match is on
* the `; request preview ` separator alone. The offload placeholder opens
* `[image omitted to fit request image limits;` and never carries that
* separator, so a placeholder can never be misread as a live handle (that
* pairing belongs to C3's structural slot evidence). A literal user echo is
* benign in both directions, same argument as dshPastedImagePath. #2766:
* assistant-role messages are never carriers. */
export function dshImageHandlePath(msg: CoreMessage): string | undefined {
return msg.role !== "assistant" && msg.contentType === "text" && typeof msg.text === "string" && msg.text.includes("; request preview ")
? "oneshot/image-handle"
: undefined;
}
/** Case-insensitive ASCII prefix check over the first needle.length chars —
* mirrors the asciiCI convention of src/server.ts (kept local so this
* module stays dependency-free). */
@@ -240,16 +328,25 @@ function startsWithAsciiCI(hay: string, needle: string): boolean {
return true;
}
/** dsh lane classifier: any of the five evidence-backed identities → its
* canonical path (four durable carriers + the foldable time-context
* injection, #5109). */
/** dsh lane classifier: any of the eleven evidence-backed identities → its
* canonical path (four durable carriers + seven foldable one-shot injections).
* Every classified message loses Rule-3 last-user anchor eligibility (#5109/
* #5173); only `durable/*` paths are actually pinned by the default policy —
* the oneshot/ families ride here purely so an injection can never displace
* the newest human request from the protected zone. */
function dshDurableToolPath(msg: CoreMessage): string | undefined {
return (
dshSkillCatalogPath(msg) ??
dshWorkspaceInstructionsPath(msg) ??
dshRuntimeContextPath(msg) ??
dshMcpCatalogPath(msg) ??
dshTimeContextPath(msg)
dshTimeContextPath(msg) ??
dshPastedImagePath(msg) ??
dshToolJobNoticePath(msg) ??
dshMnemonPath(msg) ??
dshSubagentSettledPath(msg) ??
dshAgentRelayPath(msg) ??
dshImageHandlePath(msg)
);
}
@@ -303,11 +400,16 @@ export const oneShotCarrierPaths: Record<string, DurableMessageClassifier> = {
dshMcpCatalogPath(msg) ??
dshTimeContextPath(msg) ??
dshPastedImagePath(msg) ??
dshToolJobNoticePath(msg),
dshToolJobNoticePath(msg) ??
dshMnemonPath(msg) ??
dshSubagentSettledPath(msg) ??
dshAgentRelayPath(msg) ??
dshImageHandlePath(msg),
};
/** #5142: raw-content pre-filter markers for the stored-row backfill — the
* exact marker strings of the seven dsh classifiers above, so a unified-store
* exact marker strings of the eleven dsh classifiers above (twelve markers:
* the mnemon 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. */
@@ -319,4 +421,9 @@ export const dshCarrierRawMarkers: readonly string[] = [
"Time sampled while preparing turn",
"\x3ctype\x3eimage\x3c/type\x3e",
"background job",
"[MNEMON] ",
"[MNEMON RUNTIME MEMORY",
"Background subagent ",
" sent a message: ",
"; request preview ",
];
@@ -1,6 +1,7 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import {
dshImageHandlePath,
dshMcpCatalogPath,
dshRuntimeContextPath,
dshSkillCatalogPath,
@@ -28,6 +29,7 @@ const CARRIERS: Array<[name: string, marker: string, path: string, cls: (m: Core
["workspace instructions", "Instructions from: ~/.dsh/AGENTS.md", "durable/agents-md", dshWorkspaceInstructionsPath],
["mcp catalog", "<mcp_catalog>\n- server: fs\n</mcp_catalog>", "durable/mcp-catalog", dshMcpCatalogPath],
["runtime context", "Current runtime context. This snapshot supersedes earlier runtime-context snapshots.", "durable/runtime-context", dshRuntimeContextPath],
["request-image handle", 'Image "x.png" (sha256:2e896b95dec003df); request preview 1600x900px.', "oneshot/image-handle", dshImageHandlePath],
];
for (const [name, marker, path, cls] of CARRIERS) {
+57 -4
View File
@@ -8,7 +8,7 @@ import {
positionalFingerprintWorkCount,
METADATA_EXPECTED_REBASE,
} from "../src/fold-reconcile.ts";
import { dshPastedImagePath, dshTimeContextPath, dshToolJobNoticePath, oneShotCarrierPaths, dshCarrierRawMarkers } from "../src/durable-message-guards.ts";
import { dshImageHandlePath, dshPastedImagePath, dshTimeContextPath, dshToolJobNoticePath, oneShotCarrierPaths, dshCarrierRawMarkers } from "../src/durable-message-guards.ts";
import type { CoreMessage } from "acp-kernel";
import type { Session } from "../src/session.ts";
@@ -23,7 +23,9 @@ import type { Session } from "../src/session.ts";
// control as carriers retire (T1),
// - the verdict names expected rebase, not drift/truncation (T1/T2),
// - genuine truncation/edits STILL alarm — bidirectional pins (#3617) (T3),
// - upgrade residue backfills from stored bytes, once (T4).
// - upgrade residue backfills from stored bytes, once (T4),
// - the single unaccounted image-handle text id no longer blocks the
// reasoning-only verdict (T5).
const FILLER = "lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor ";
let seq = 0;
@@ -359,6 +361,43 @@ describe("#5142 expected-rebase accounting", () => {
});
assert.ok(w2 <= w1 / 2, `second pass ${w2} not far below the backfill pass ${w1} — memoization regressed`);
});
test("T5 the lone retired image handle no longer blocks the reasoning-only verdict (live 111-id shape)", () => {
// Production shape (session f3b87753, 2026-11-04): a cross-provider
// replay drop retired 110 reasoning cores plus exactly ONE text id —
// the request-image handle annotation, which no classifier knew. That
// single unaccounted id held realUnmatched off the reasoning-only
// verdict, so the episode warned as drift on EVERY turn instead of
// taking the tolerated #2193 channel. The first pass here seeds
// anchors from LIVE bytes (production shape); the handle is retired
// before the classifier existed, so this exercises the C2 stored-byte
// backfill exactly as production does.
const { session, block, logs, opts } = harness("dsh");
const reasoning = Array.from({ length: 12 }, (_, i) => ({
id: `r${i}`, role: "assistant" as const, contentType: "reasoning" as const,
text: `reasoning core ${i} weighing options at length`,
})) as CoreMessage[];
const handleText = "Image \"image.png\" (sha256:6d0d1ca416fa10d7f2ab80db8bd12eeaf72f2c418a8171308ff7b658d45f9e66); request preview 1657x811px. Normalized copy (read-only; may be resized or re-encoded): \"C:/Users/Suda/.dsh/attachments/v1/objects/6d/6d0d1ca416fa10d7f2ab80db8bd12eeaf72f2c418a8171308ff7b658d45f9e66\" (1657x811px, image/png). Source dimensions, format, and byte size may differ. Copy to a writable path ending in .png before editing.";
const handleId = "h_image_handle";
const rest = baseHistory(8);
block.effectiveMessageIds.push(...reasoning.map((m) => m.id!), handleId, ...rest.map((m) => m.id!));
const storedRowLookup = (ids: readonly string[]) => ids.filter((id) => id === handleId).map((id) => ({
seq: 1,
msgId: id,
content: JSON.stringify({ id, role: "user", contentType: "text", text: handleText, wireIndex: 102 }),
}));
const fullOpts = { ...opts, storedRowLookup };
reconcileFoldCoverage(session, [...reasoning, ...rest, { id: handleId, role: "user", contentType: "text", text: handleText } as CoreMessage], fullOpts);
const wire = (): CoreMessage[] => [...rest,
{ id: "f0", role: "user" as const, contentType: "text" as const, text: "post-switch fresh turn zero" },
{ id: "f1", role: "assistant" as const, contentType: "text" as const, text: "post-switch fresh answer one" }];
for (let pass = 1; pass <= 3; pass++) reconcileFoldCoverage(session, wire(), fullOpts);
assert.equal(recordOf(session)?.[handleId], "oneshot/image-handle", "the handle takes the expected-rebase channel");
assert.equal(logs.filter(([, m]) => m.includes("covered reasoning core(s) dropped by the host")).length, 3, "every pass takes the tolerated #2193 channel");
assert.ok(!logs.some(([, m]) => m.includes("resent history drifted")), "one unaccounted text id must not turn a switch into drift");
assert.equal(session.metadata.foldDriftStreak, undefined, "tolerated episode accrues no destroy streak");
assert.equal(session.metadata.foldDriftEscalated, undefined, "no latch");
});
});
describe("#5142 carrier classifier units", () => {
@@ -389,14 +428,28 @@ describe("#5142 carrier classifier units", () => {
assert.equal(dshToolJobNoticePath(mk("user", " Background Job abc finished.")), "oneshot/tool-job-notice", "asciiCI prefix like the other auto-injected families");
assert.equal(dshToolJobNoticePath(mk("assistant", job)), undefined, "assistant echo must never classify (#2766)");
assert.equal(dshToolJobNoticePath(mk("user", "the background job failed again")), undefined, "marker mid-text is not a carrier");
// request-image handle annotation (#5142 residual, production sample):
// rides only its attachment's turn, then the slot holds the offload
// placeholder instead.
const handle = "Image \"image.png\" (sha256:6d0d1ca416fa10d7f2ab80db8bd12eeaf72f2c418a8171308ff7b658d45f9e66); request preview 1657x811px. Normalized copy (read-only; may be resized or re-encoded): \"C:/Users/Suda/.dsh/attachments/v1/objects/6d/6d0d1ca416fa10d7f2ab80db8bd12eeaf72f2c418a8171308ff7b658d45f9e66\" (1657x811px, image/png). Source dimensions, format, and byte size may differ. Copy to a writable path ending in .png before editing.";
assert.equal(dshImageHandlePath(mk("user", handle)), "oneshot/image-handle");
assert.equal(dshImageHandlePath(mk("assistant", handle)), undefined, "assistant echo must never classify (#2766)");
assert.equal(dshImageHandlePath(mk("user", "[image omitted to fit request image limits; \"spawn_preview.png\" (sha256:2e896b95dec003df). Normalized copy (read-only; may be resized or re-encoded): \"normalized/spawn_preview.png\" (1600x900px, image/png).]")), undefined, "the offload placeholder is C3 slot evidence, never a live handle");
assert.equal(dshImageHandlePath(mk("user", "Image \"x.png\" (sha256:2e896b95dec003df) with no preview separator")), undefined, "plain image talk is not a handle");
});
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 >= 7, "seven known dsh carrier families");
assert.ok(dshCarrierRawMarkers.length >= 12, "twelve raw markers for the eleven 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<string, string> = {
" sent a message: ": "Agent session-1",
};
for (const marker of dshCarrierRawMarkers) {
const msg = { id: "lk", role: "user" as const, contentType: "text" as const, text: marker } as CoreMessage;
const msg = { id: "lk", role: "user" as const, contentType: "text" as const, text: `${MARKER_CONTEXT[marker] ?? ""}${marker}` } as CoreMessage;
assert.notEqual(cls(msg), undefined, `marker ${JSON.stringify(marker)} no longer matches any dsh classifier`);
}
});
+166
View File
@@ -0,0 +1,166 @@
// #5173: dsh host-injected user-role messages landing right after the newest
// human request stole the Rule-3 "last user message" protected slot, so the
// live instruction folded away and the model answered a finished topic.
// The kernel half (#5109: lastUserAnchor skips virtual-path messages) shipped
// in v0.1.210 covering the five then-classified families; this file pins the
// remaining dsh families at both levels — classifier units (#3617 positive +
// negative rows) for the three NEW classifiers, the BOTH-registries wiring
// (the defect class here was exactly "classified but unwired into the slot
// exclusion set"), and the end-to-end anchor geometry through the REAL dsh
// classifier rather than an id-based stub.
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { assignRefs, createCore, createInitialState, defaultConfig } from "acp-kernel";
import type { CoreMessage } from "acp-kernel";
import {
dshAgentRelayPath,
dshMnemonPath,
dshSubagentSettledPath,
durableMessageToolPaths,
oneShotCarrierPaths,
withDurableProtection,
} from "../src/durable-message-guards.ts";
function mk(id: string, role: CoreMessage["role"], text: string): CoreMessage {
return { id, role, contentType: "text", text } as CoreMessage;
}
describe("#5173 new dsh injection classifiers", () => {
test("dshMnemonPath: prefix positives, role/contentType negatives, near-misses", () => {
assert.equal(dshMnemonPath(mk("u1", "user", "[MNEMON] recall: user prefers terse diffs")), "oneshot/mnemon");
assert.equal(dshMnemonPath(mk("u2", "user", "[MNEMON RUNTIME MEMORY] session budget note")), "oneshot/mnemon");
assert.equal(dshMnemonPath(mk("u3", "user", " [mnemon] leading pad + lowercase tag still machine-generated")), "oneshot/mnemon");
assert.equal(dshMnemonPath(mk("u4", "assistant", "[MNEMON] echo must never classify (#2766)")), undefined, "assistant echo must never classify");
assert.equal(dshMnemonPath(mk("u5", "user", "we noted [MNEMON] earlier in chat")), undefined, "marker mid-text is not a carrier");
assert.equal(dshMnemonPath(mk("u6", "user", "[MNEMONX] different tag")), undefined, "no closing-bracket-space boundary → different tag");
const tool = { id: "u7", role: "tool" as const, contentType: "tool-result" as const, text: "[MNEMON] x" } as CoreMessage;
assert.equal(dshMnemonPath(tool), undefined, "tool channel is not a carrier surface");
});
test("dshSubagentSettledPath: settlement positives, prose near-miss, role negative", () => {
const settled = "Background subagent session-a1b2 finished and will do no further work unless you send it more.";
assert.equal(dshSubagentSettledPath(mk("s1", "user", settled)), "oneshot/subagent-settled");
assert.equal(dshSubagentSettledPath(mk("s2", "user", "background subagent 3 was stopped before it finished.")), "oneshot/subagent-settled", "asciiCI like the other auto-injected families");
assert.equal(dshSubagentSettledPath(mk("s3", "assistant", settled)), undefined, "assistant echo must never classify (#2766)");
assert.equal(dshSubagentSettledPath(mk("s4", "user", "Background subagents are handy for parallel work")), undefined, "prose starting with the words stays unmatched (trailing-space boundary)");
assert.equal(dshSubagentSettledPath(mk("s5", "user", "the background subagent failed again")), undefined, "marker mid-text is not a carrier");
});
test("dshAgentRelayPath: wrapper positives, echo/near-miss negatives", () => {
assert.equal(dshAgentRelayPath(mk("r1", "user", "Agent session-42 sent a message: please review the draft")), "oneshot/agent-relay");
assert.equal(dshAgentRelayPath(mk("r2", "user", "Agent 7 sent a message: done")), "oneshot/agent-relay", "counter-form session id");
assert.equal(dshAgentRelayPath(mk("r3", "assistant", "Agent session-42 sent a message: forged echo (#2766)")), undefined, "assistant echo must never classify");
assert.equal(dshAgentRelayPath(mk("r4", "user", "I sent a message: hello there")), undefined, "no Agent lead-in → plain prose");
assert.equal(dshAgentRelayPath(mk("r5", "user", "Agent X sent a message:no-space joint")), undefined, "the wrapper joint always carries the space");
});
});
describe("#5173 registry wiring", () => {
test("all eleven dsh families resolve in BOTH the slot-exclusion and drift-accounting registries", () => {
const samples: Array<[string, string]> = [
["\x3csystem-reminder\x3e \x3cavailable_skills\x3e updated catalog \x3c/available_skills\x3e", "durable/skill-catalog"],
["Instructions from: /home/x/.dsh/AGENTS.md", "durable/agents-md"],
["Current runtime context. This snapshot supersedes earlier runtime-context snapshots.", "durable/runtime-context"],
["\x3cmcp_catalog\x3e registered servers \x3c/mcp_catalog\x3e", "durable/mcp-catalog"],
["Time sampled while preparing turn 9, step 1: 2026-11-01T00:00:00Z", "oneshot/time-context"],
["\x3cpath\x3ex.png\x3c/path\x3e \x3ctype\x3eimage\x3c/type\x3e \x3ccontent\x3epng bytes\x3c/content\x3e", "oneshot/ptc-image"],
['background job bash-abcd (bash: "true") finished [status: completed, exit code: 0]. Read its output with job_output.', "oneshot/tool-job-notice"],
["[MNEMON] recall note", "oneshot/mnemon"],
["Background subagent session-x finished and will do no further work unless you send it more.", "oneshot/subagent-settled"],
["Agent session-y sent a message: over there", "oneshot/agent-relay"],
['Image "x.png" (sha256:2e896b95dec003df); request preview 1600x900px. Normalized copy (read-only; may be resized or re-encoded): "normalized/x.png" (1600x900px, image/png). Source dimensions, format, and byte size may differ.', "oneshot/image-handle"],
];
for (const [text, expected] of samples) {
const msg = mk("w", "user", text);
assert.equal(durableMessageToolPaths.dsh!(msg), expected, `slot-exclusion registry misses ${expected}`);
assert.equal(oneShotCarrierPaths.dsh!(msg), expected, `drift-accounting registry misses ${expected}`);
}
});
});
describe("#5173 end-to-end anchor geometry", () => {
test("fold covering the injection tail keeps the live request visible (THE repro)", () => {
// Filler pads every message past config.compress.minCompressRange
// (mirrors kernel/tests/issue5109's .repeat(120) bodies); appended
// AFTER each marker so prefix classifiers still see a clean head.
const PAD = "lorem ipsum dolor sit amet consectetur adipiscing elit ".repeat(12);
const LIVE = "current live request: run the migration now";
const all: CoreMessage[] = [
mk("p1", "user", `old question ${PAD}`),
mk("p2", "assistant", `old answer ${PAD}`),
mk("p3", "user", LIVE),
mk("p4", "user", `\x3csystem-reminder\x3e \x3cavailable_skills\x3e updated catalog \x3c/available_skills\x3e ${PAD}`),
mk("p5", "user", `Time sampled while preparing turn 9, step 1: 2026-11-01T00:00:00Z ${PAD}`),
mk("p6", "user", `[MNEMON] recall: prefer terse diffs ${PAD}`),
mk("p7", "user", `Background subagent session-a1b2 finished and will do no further work unless you send it more. ${PAD}`),
mk("p8", "user", `Agent session-c3d4 sent a message: draft is ready ${PAD}`),
mk("p9", "user", `\x3cpath\x3ex.png\x3c/path\x3e \x3ctype\x3eimage\x3c/type\x3e \x3ccontent\x3epng bytes\x3c/content\x3e ${PAD}`),
mk("p10", "user", `background job bash-ef01 (bash: "echo ok") finished [status: completed, exit code: 0]. Read its output with job_output. ${PAD}`),
mk("p11", "user", `Image "x.png" (sha256:2e896b95dec003df); request preview 1600x900px. Normalized copy (read-only; may be resized or re-encoded): "normalized/x.png" (1600x900px, image/png). ${PAD}`),
mk("p12", "assistant", `working on the migration ${PAD}`),
];
const core = createCore();
const state = createInitialState();
state.messageRefs = assignRefs(all, { existing: state.messageRefs, nextIndex: 1 }).map;
const config = withDurableProtection(
defaultConfig(200000, { preserveRecentMessages: 1, preserveRecentTokens: 0 }),
durableMessageToolPaths.dsh!,
);
const result = core.applyCompression({
ranges: [{ startRef: "m00002", endRef: "m00010", summary: "A long enough summary body to satisfy the minimum summary length gate of fifty characters." }],
messages: all,
state,
config,
});
const run = (msgs: CoreMessage[], startRef: string, endRef: string) => {
const c = createCore();
const st = createInitialState();
st.messageRefs = assignRefs(msgs, { existing: st.messageRefs, nextIndex: 1 }).map;
return c.applyCompression({
ranges: [{ startRef, endRef, summary: "A long enough summary body to satisfy the minimum summary length gate of fifty characters." }],
messages: msgs,
state: st,
config,
});
};
// THE repro: the requested range spans the live request plus the
// injection tail. Pre-fix the anchor was p10 (job notice), so #3953
// truncated at p10 and folded p3 — the instruction the model is
// currently answering. Post-fix the anchor is p3, so NOTHING at or
// after it may fold; only the pre-anchor content (p2) becomes the
// block. This assertion is the data-loss pin.
{
const res = run(all, "m00002", "m00010");
assert.equal(res.result.blocksCreated, 1, `expected one block, got errors=${JSON.stringify(res.result.errors)} warnings=${JSON.stringify(res.result.warnings)}`);
const block = res.state.blocks.find((b) => b.active)!;
assert.deepEqual([...block.effectiveMessageIds].sort(), ["p2"], "only pre-anchor content folds; #3953 keeps everything at/after the live request visible");
assert.ok(
res.result.warnings.some((w) => w.startsWith("Excluded") && w.includes("Last-user anchor: m00003")),
`receipt names the anchor ref, got: ${JSON.stringify(res.result.warnings)}`,
);
}
// No-bloat pin: once an injection sits BEFORE the live request (the
// normal steady state on the next turn), it folds like any other
// history — classification removes slot eligibility only, never
// foldability. Without this, classifying every dsh carrier would
// silently accumulate context.
{
// PAD2 clears config.compress.minCompressRange (5000 chars) on
// the four foldable messages; PAD alone leaves the range short.
const PAD2 = "lorem ipsum dolor sit amet consectetur adipiscing elit ".repeat(30);
const msgs: CoreMessage[] = [
mk("q1", "user", `old question ${PAD2}`),
mk("a1", "assistant", `old answer ${PAD2}`),
mk("t1", "user", `Time sampled while preparing turn 3, step 1: 2026-11-01T00:00:00Z ${PAD2}`),
mk("n1", "user", `[MNEMON] recall: prefer terse diffs ${PAD2}`),
mk("live", "user", LIVE),
];
const res = run(msgs, "m00001", "m00004");
assert.equal(res.result.blocksCreated, 1, `expected one block, got errors=${JSON.stringify(res.result.errors)} warnings=${JSON.stringify(res.result.warnings)}`);
const block = res.state.blocks.find((b) => b.active)!;
assert.deepEqual([...block.effectiveMessageIds].sort(), ["a1", "n1", "q1", "t1"].sort(), "classified injections fold normally when they are ahead of the live request");
}
});
});