Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 45 additions & 9 deletions src/durable-message-guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,38 @@ export function dshImageHandlePath(msg: CoreMessage): string | undefined {
: undefined;
}

/** dsh image-offload placeholder (#6021, production evidence 2026-11-09):
* when DSH's compaction-image-offload rewrites an OLD message's inline image
* to fit the route's request budget, the cell's text becomes "[image omitted
* to fit request image limits; "NAME" (sha256:…). Normalized copy …]" — or,
* on text-only routes, "[image omitted because this model accepts text only;
* attachment sha256:…]" (offloadedImageText, @deepseek-ai/dsh-compaction-
* image-offload). The rewrite changes the cell's content identity, so bili
* re-ingests it as NEW content: fresh ref number, fresh storage sequence and
* timestamp — a two-day-old message that now sits at the TAIL of the resent
* history. Unclassified, it is a plain user-role text message: once
* everything after the newest human request has folded away, that stale prose
* is the last visible user message, wins the Rule-3 last-user anchor (#5109)
* and gets pinned into the protected zone — the model then answers the stale
* topic for the whole turn (the #6021 repro). #5752 shipped the sibling live
* handle knowing this slot would next carry this placeholder and deliberately
* left it unclassified — this family is that deferral now carrying production
* traffic. SUBSTRING match on purpose, unlike the prefix families above: the
* placeholder opens the rewritten cell only when the original message was
* image-only — a [text, image] message keeps its prose AFTER the "]" tail, so
* a prefix match misses the spliced shape (m03269 in the #6021 evidence).
* Both route variants share one path (same emitter, same lane, same failure
* mode). One-shot like the live handle: the rewritten cell never returns in
* its old form, retirement accounted via oneShotCarrierPaths. A literal user
* echo of the phrase is benign in both directions (slot exclusion only, stays
* foldable). #2766: assistant-role messages are never carriers. */
export function dshImageOffloadPlaceholderPath(msg: CoreMessage): string | undefined {
return msg.role !== "assistant" && msg.contentType === "text" && typeof msg.text === "string" &&
(msg.text.includes("image omitted to fit request image limits;") || msg.text.includes("image omitted because this model accepts text only;"))
? "oneshot/image-offload-placeholder"
: 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). */
Expand All @@ -328,8 +360,8 @@ function startsWithAsciiCI(hay: string, needle: string): boolean {
return true;
}

/** dsh lane classifier: any of the eleven evidence-backed identities → its
* canonical path (four durable carriers + seven foldable one-shot injections).
/** dsh lane classifier: any of the twelve evidence-backed identities → its
* canonical path (four durable carriers + eight 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
Expand All @@ -346,7 +378,8 @@ function dshDurableToolPath(msg: CoreMessage): string | undefined {
dshMnemonPath(msg) ??
dshSubagentSettledPath(msg) ??
dshAgentRelayPath(msg) ??
dshImageHandlePath(msg)
dshImageHandlePath(msg) ??
dshImageOffloadPlaceholderPath(msg)
);
}

Expand Down Expand Up @@ -404,15 +437,16 @@ export const oneShotCarrierPaths: Record<string, DurableMessageClassifier> = {
dshMnemonPath(msg) ??
dshSubagentSettledPath(msg) ??
dshAgentRelayPath(msg) ??
dshImageHandlePath(msg),
dshImageHandlePath(msg) ??
dshImageOffloadPlaceholderPath(msg),
};

/** #5142: raw-content pre-filter markers for the stored-row backfill — the
* 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. */
* exact marker strings of the twelve dsh classifiers above (fourteen markers:
* the mnemon and image-offload-placeholder families contribute two each), 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:",
Expand All @@ -426,4 +460,6 @@ export const dshCarrierRawMarkers: readonly string[] = [
"Background subagent ",
" sent a message: ",
"; request preview ",
"image omitted to fit request image limits;",
"image omitted because this model accepts text only;",
];
10 changes: 8 additions & 2 deletions tests/issue5142-expected-rebase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,13 @@ describe("#5142 expected-rebase accounting", () => {
const r = reconcileFoldCoverage(session, wire2, fullOpts);
assert.equal(recordOf(session)?.[img.id!], "image-offload", "the production placeholder must pair structurally at its slot");
assert.ok(!logs.some(([, m]) => m.includes("resent history drifted")), "production offload must not read as drift");
assert.equal(r.expectedRetired, 1);
// #6021: the placeholder cell is ALSO a classified one-shot carrier now,
// so the production pair accounts BOTH sides — the retired image row
// (structural image-offload pairing, channel 3) and the fresh
// placeholder cell (channel-1 carrier mark covering its own later
// retirement). Pre-#6021 the unclassified placeholder accounted only
// the image row (1).
assert.equal(r.expectedRetired, 2);
});

test("T3a real head truncation still alarms — no expected-rebase masking (#3617 negative)", () => {
Expand Down Expand Up @@ -441,7 +447,7 @@ 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 >= 12, "twelve raw markers for the eleven known dsh carrier families");
assert.ok(dshCarrierRawMarkers.length >= 14, "fourteen 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).
Expand Down
3 changes: 2 additions & 1 deletion tests/issue5173-last-user-anchor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe("#5173 new dsh injection classifiers", () => {
});

describe("#5173 registry wiring", () => {
test("all eleven dsh families resolve in BOTH the slot-exclusion and drift-accounting registries", () => {
test("all twelve dsh families resolve in BOTH the slot-exclusion and drift-accounting registries", () => {
const samples: Array<[string, string]> = [
["\x3csystem-reminder\x3e \x3cavailable_skills\x3e updated catalog \x3c/available_skills\x3e", "durable/skill-catalog"],
["Instructions from: /home/x/.dsh/AGENTS.md", "durable/agents-md"],
Expand All @@ -69,6 +69,7 @@ describe("#5173 registry wiring", () => {
["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"],
['[image omitted to fit request image limits; "image.png" (sha256:0e596a186ab8f0c2). Normalized copy (read-only; may be resized or re-encoded): "normalized/image.png" (1584x892px, image/png).]', "oneshot/image-offload-placeholder"],
];
for (const [text, expected] of samples) {
const msg = mk("w", "user", text);
Expand Down
122 changes: 122 additions & 0 deletions tests/issue6021-image-offload-anchor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// #6021: dsh compaction-image-offload rewrites an OLD message's inline image
// into placeholder prose when the route's request budget is exceeded. The
// rewrite changes content identity, so bili re-ingests the cell as a NEW row
// (fresh ref / storage seq / timestamp) — a two-day-old message now sitting at
// the TAIL of the resent history. Unclassified, the placeholder is plain
// user-role text: once everything after the newest human request has folded
// away, that stale prose is the last visible user message, wins the Rule-3
// last-user anchor (#5109), pins the protected zone on the stale topic and the
// model answers it for the whole turn. #5752 shipped the sibling live handle
// with this slot deliberately unclassified; this file pins the deferred family
// at all three levels (classifier units per #3617, BOTH-registries wiring, and
// end-to-end anchor geometry through the REAL dsh classifier).
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 {
dshImageHandlePath,
dshImageOffloadPlaceholderPath,
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;
}

const PLACEHOLDER = '[image omitted to fit request image limits; "image.png" (sha256:0e596a186ab8f0c2d4e6a7b9). Normalized copy (read-only; may be resized or re-encoded): "C:/Users/x/.dsh/attachments/v1/objects/0e/0e596a186ab8f0c2d4e6a7b9" (1584x892px, image/png).]';
const PLACEHOLDER_TEXT_ONLY = "[image omitted because this model accepts text only; attachment sha256:0e596a186ab8f0c2d4e6a7b9.]";

describe("#6021 dshImageOffloadPlaceholderPath units", () => {
test("positives: production spliced shape, pure placeholder, text-only route", () => {
// m03269 shape: placeholder opens the cell, the user's own prose follows
// AFTER the "]" tail (the original message was [text, image]).
assert.equal(dshImageOffloadPlaceholderPath(mk("u1", "user", `${PLACEHOLDER}有点小问题,这些unknown的按键应该是手柄按键,不应该统计`)), "oneshot/image-offload-placeholder");
assert.equal(dshImageOffloadPlaceholderPath(mk("u2", "user", PLACEHOLDER)), "oneshot/image-offload-placeholder", "m03272 shape: pure placeholder");
assert.equal(dshImageOffloadPlaceholderPath(mk("u3", "user", PLACEHOLDER_TEXT_ONLY)), "oneshot/image-offload-placeholder", "text-only route variant shares the path");
assert.equal(dshImageOffloadPlaceholderPath(mk("u4", "user", `saw ${PLACEHOLDER} in the log yesterday`)), "oneshot/image-offload-placeholder", "substring match is deliberate: prefix match misses the spliced shape");
});

test("negatives: assistant echo, pi-lane wording, live handle, prose near-miss", () => {
assert.equal(dshImageOffloadPlaceholderPath(mk("a1", "assistant", PLACEHOLDER)), undefined, "assistant echo must never classify (#2766)");
assert.equal(dshImageOffloadPlaceholderPath(mk("p1", "user", "(image omitted: model does not support images)")), undefined, "pi-lane non-vision placeholder is different wording (src/agent/pi-msgids.ts)");
const handle = 'Image "x.png" (sha256:2e896b95dec003df); request preview 1600x900px. Normalized copy (read-only; may be resized or re-encoded): "normalized/x.png" (1600x900px, image/png).';
assert.equal(dshImageOffloadPlaceholderPath(mk("h1", "user", handle)), undefined, "live handle stays its own family");
assert.equal(dshImageHandlePath(mk("h2", "user", PLACEHOLDER)), undefined, "placeholder does not claim the live-handle slot either (#5142 negative pin stays true)");
assert.equal(dshImageOffloadPlaceholderPath(mk("n1", "user", "the image omitted part confused me")), undefined, "prose without the full marker stays unmatched");
const tool = { id: "t1", role: "tool" as const, contentType: "tool-result" as const, text: PLACEHOLDER } as CoreMessage;
assert.equal(dshImageOffloadPlaceholderPath(tool), undefined, "tool channel is not a carrier surface");
});

test("BOTH registries resolve the production shape (wiring)", () => {
const msg = mk("w", "user", `${PLACEHOLDER}有点小问题,这些unknown的按键应该是手柄按键,不应该统计`);
assert.equal(durableMessageToolPaths.dsh!(msg), "oneshot/image-offload-placeholder", "slot-exclusion registry");
assert.equal(oneShotCarrierPaths.dsh!(msg), "oneshot/image-offload-placeholder", "drift-accounting registry");
});
});

describe("#6021 end-to-end anchor geometry", () => {
test("fold covering the offloaded tail keeps the live request visible (THE repro)", () => {
const config = withDurableProtection(
defaultConfig(200000, { preserveRecentMessages: 1, preserveRecentTokens: 0 }),
durableMessageToolPaths.dsh!,
);
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 (mirrors m02528 -> m03269/m03272 in #6021): the offload
// rewrite lands AFTER the real question. Pre-fix the anchor was ph (the
// stale placeholder prose), so #3953 truncated at ph and folded live —
// the instruction the model is currently answering. Post-fix ph loses
// slot eligibility and the anchor is live again.
{
// PAD clears config.compress.minCompressRange (5000 chars) on the
// foldable subset (o1+o2).
const PAD = "lorem ipsum dolor sit amet consectetur adipiscing elit ".repeat(60);
const LIVE = "current live request: check why the unknown keys register";
const msgs: CoreMessage[] = [
mk("o1", "user", `old question ${PAD}`),
mk("o2", "assistant", `old answer ${PAD}`),
mk("live", "user", LIVE),
mk("ph", "user", `${PLACEHOLDER}有点小问题,这些unknown的按键应该是手柄按键,不应该统计`),
mk("a3", "assistant", `working on it ${PAD}`),
];
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(), ["o1", "o2"], "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 the rewritten cell sits BEFORE the live request
// (steady state on later turns), it folds like any other history —
// classification removes slot eligibility only, never foldability.
{
const PAD2 = "lorem ipsum dolor sit amet consectetur adipiscing elit ".repeat(60);
const msgs: CoreMessage[] = [
mk("q1", "user", `old question ${PAD2}`),
mk("a1", "assistant", `old answer ${PAD2}`),
mk("ph1", "user", PLACEHOLDER),
mk("live", "user", "current live request: continue"),
];
const res = run(msgs, "m00001", "m00003");
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", "ph1", "q1"].sort(), "classified placeholders fold normally when they are ahead of the live request");
}
});
});
Loading