Skip to content
Open
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
4 changes: 2 additions & 2 deletions kernel/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion kernel/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "acp-kernel",
"version": "0.0.200",
"version": "0.0.201",
"description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.",
"license": "MIT",
"author": "ranxianglei",
Expand Down
20 changes: 17 additions & 3 deletions kernel/src/media-refs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
*
* The fix splits the class: a media message is FOLDABLE when it exposes at
* least one extractable re-access handle (a non-data: URL, a file id, a
* fileUri). Folding then loses nothing unrecoverable — the handles ride the
* fileUri, a DSH attachment id — #6875). Folding then loses nothing
* unrecoverable — the handles ride the
* stored summary verbatim (collectFoldMediaRefs appends them mechanically at
* apply time, below), and decompress restores the original message
* losslessly regardless. Only media with NO extractable ref stays pinned,
Expand Down Expand Up @@ -88,6 +89,19 @@ export function looksEphemeralSignedUrl(ref: string): boolean {
return false;
}

/** #6875: a DSH image part's re-access handle is its attachment's
* content-addressed id — push it verbatim (bare-id parallel to a DeepSeek
* file_id; pushRef's inline-data/ephemeral guards do not apply to it).
* An attachment with no usable id pushes nothing, so the message keeps
* pinning (conservative). */
function pushDshAttachmentRef(
refs: string[],
part: Record<string, unknown>,
): void {
if (part.type !== "image") return;
pushRef(refs, asObj(part.attachment)?.attachmentId);
}

function pushRef(refs: string[], ref: unknown): void {
if (typeof ref !== "string" || ref.length === 0 || isInlineDataUrl(ref)) return;
// #5865-P2: an expiring handle is not a re-access handle — keep the
Expand All @@ -111,7 +125,7 @@ export function extractMediaRefs(message: CoreMessage): string[] {
else if (roc.type === "file") {
pushRef(refs, roc.file_id);
pushRef(refs, roc.url);
}
} else if (roc.type === "image") pushDshAttachmentRef(refs, roc);
}
if (Array.isArray(m.rawOpenaiContentParts)) {
for (const p of m.rawOpenaiContentParts) {
Expand All @@ -121,7 +135,7 @@ export function extractMediaRefs(message: CoreMessage): string[] {
else if (part.type === "file") {
pushRef(refs, part.file_id);
pushRef(refs, part.url);
}
} else if (part.type === "image") pushDshAttachmentRef(refs, part);
}
}

Expand Down
60 changes: 55 additions & 5 deletions kernel/src/wire/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ function imageFingerprintsOfParts(parts: unknown[]): string[] {
return out;
}

/** #6875: a DSH image part — `{type:"image",
* attachment:{attachmentId, ...}}` — is the one tool-result shape the
* codec preserves into a sidecar. The attachmentId is a durable
* content-addressed handle (media-refs.ts enumerates it), so carrying it
* through is what makes the message foldable instead of invisibly
* media-carrying. Any other opaque tool-result part keeps the legacy
* text-only reduction: a sidecar the classifier does not recognize would
* newly pin the message (the #6875 trap — codec and enumeration must land
* together). */
function isDshAttachmentPart(part: unknown): part is OpenAIContentPart {
if (!part || typeof part !== "object") return false;
const p = part as { type?: unknown; attachment?: unknown };
if (p.type !== "image") return false;
const attachment = p.attachment;
if (!attachment || typeof attachment !== "object") return false;
const id = (attachment as { attachmentId?: unknown }).attachmentId;
return typeof id === "string" && id.length > 0;
}

export type OpenAIContentPart =
| { type: "text"; text: string }
| { type: "image_url"; image_url: { url: string } }
Expand Down Expand Up @@ -326,13 +345,27 @@ export function openaiToCore(body: OpenAIRequestBody): Flat {
{ toolName: m.name || toolNames.get(m.tool_call_id ?? "") },
);
const name = m.name || toolNames.get(m.tool_call_id ?? "");
// #6875: a DSH read_image result carries its image as
// `{type:"image", attachment:{attachmentId, ...}}` in the content
// array. stringContent() below drops every non-text part, so
// without a sidecar the attachment never reaches CoreMessage and
// extractMediaRefs can never see it — the message looks like plain
// text while its media is invisible. Preserve ONLY the
// attachment-carrying image parts (see isDshAttachmentPart): any
// other opaque part keeps the legacy text-only reduction.
const attachmentParts = Array.isArray(m.content)
? m.content.filter(isDshAttachmentPart)
: [];
msgs.push({
id: clusters.next(base),
role: "tool",
contentType: "tool-result",
toolCallId: m.tool_call_id ?? "",
text: stringContent(m.content),
...(name ? { toolName: name } : {}),
...(attachmentParts.length > 0
? { rawOpenaiContentParts: attachmentParts }
: {}),
});
break;
}
Expand Down Expand Up @@ -529,11 +562,28 @@ export function coreToOpenai(messages: BiliMessage[]): OpenAIMessage[] {
}
userRun.push(m);
} else if (m.role === "tool") {
out.push({
role: "tool",
tool_call_id: m.toolCallId ?? "",
content: m.text ?? "",
});
// #6875: a tool result carrying preserved DSH attachment parts
// re-emits them after its text part (mirror of flushUserRun) so
// the parse→rebuild round trip keeps the attachment; text-only
// results keep the legacy plain-string shape byte-identically.
const parts = m.rawOpenaiContentParts;
if (Array.isArray(parts) && parts.length > 0) {
const content: OpenAIContentPart[] = [];
if (m.text) content.push({ type: "text", text: m.text });
for (const part of parts)
content.push(part as OpenAIContentPart);
out.push({
role: "tool",
tool_call_id: m.toolCallId ?? "",
content,
});
} else {
out.push({
role: "tool",
tool_call_id: m.toolCallId ?? "",
content: m.text ?? "",
});
}
}
}
}
Expand Down
217 changes: 217 additions & 0 deletions kernel/tests/issue6875-dsh-attachment-refs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import {
extractMediaRefs,
hasRefRecoverableMediaPayload,
MEDIA_REFS_MARKER,
} from "../src/media-refs.js";
import { hasUnrecoverableMediaPayload } from "../src/protected.js";
import { assignRefs, BLOCKED_REF } from "../src/refs.js";
import { createCore } from "../src/compress.js";
import { createInitialState } from "../src/state.js";
import type { Config, CoreMessage } from "../src/types.js";

// #6875: DSH read_image / screenshot results carry the image as
// { type: "image", attachment: { attachmentId, mediaType, bytes, ... } } —
// a local blob in the DSH attachment store, addressable by attachmentId
// (blobs verified present by the reporter: 504 attachments, missing = 0).
// The id is a durable re-access handle in the #5620 sense (same class as a
// DeepSeek file_id: a bare id, durable by definition), but extractMediaRefs
// never enumerated the shape, so every such message classified as
// unrecoverable media, pinned forever, and its refs went BLOCKED.
const DSH_ID = "sha256:a08caa79deadbeef";
const DSH_PART = {
type: "image",
attachment: {
attachmentId: DSH_ID,
mediaType: "image/png",
bytes: 62368,
width: 896,
height: 651,
name: "printwindow_flag0.png",
},
};

function textMsg(
id: string,
role: CoreMessage["role"],
text: string,
): CoreMessage {
return { id, role, contentType: "text", text };
}

function sidecarMsg(id: string, extra: object): CoreMessage {
return Object.assign(
{ id, role: "user" as const, contentType: "text" as const, text: "" },
extra,
);
}

function config(): Config {
return {
tiers: { enabled: true, tier2Trigger: 5, tier3Trigger: 10 },
nudge: {
maxContextLimitPct: 0.55,
minContextLimitPct: 0.45,
frequency: 5,
iterationThreshold: 15,
force: "soft",
growthRatio: 0.05,
growthFloor: 6000,
growthCap: 50000,
minGrowthFloor: 5000,
minGrowthRatio: 0.45,
emergencyThresholdPct: 0.98,
tier2GrowthMultiplier: 1.5,
},
promotionThreshold: 5,
truncate: { threshold: 1 },
compress: { minCompressRange: 0, maxSummaryLength: 0, minSummaryLength: 0 },
protectedTools: [],
preserveRecentMessages: 0,
preserveRecentTokens: 0,
modelContextLimit: 100000,
};
}

test("extractMediaRefs reads the DSH attachment id from every sidecar family", () => {
assert.deepEqual(
extractMediaRefs(sidecarMsg("a", { rawOpenaiContentParts: [DSH_PART] })),
[DSH_ID],
"openai plural — the #6875 wire shape (unknown opaque part, #1205)",
);
assert.deepEqual(
extractMediaRefs(sidecarMsg("b", { rawOpenaiContent: DSH_PART })),
[DSH_ID],
"openai singular",
);
assert.deepEqual(
extractMediaRefs(
sidecarMsg("c", {
rawOpenaiContentParts: [{ type: "text", text: "screenshot" }, DSH_PART],
}),
),
[DSH_ID],
"mixed text + attachment parts — only the handle",
);
});

test("DSH attachment media is ref-recoverable, not unrecoverable (#6875)", () => {
const openai = sidecarMsg("o", { rawOpenaiContentParts: [DSH_PART] });
assert.equal(hasRefRecoverableMediaPayload(openai), true);
assert.equal(
hasUnrecoverableMediaPayload(openai),
false,
"folds like #5620 media",
);
const lone = sidecarMsg("p", { rawOpenaiContent: DSH_PART });
assert.equal(hasRefRecoverableMediaPayload(lone), true);
assert.equal(
hasUnrecoverableMediaPayload(lone),
false,
"folds like #5620 media",
);
});

test("an attachment without an id still pins (conservative, #6875)", () => {
const noId = sidecarMsg("x", {
rawOpenaiContentParts: [
{ type: "image", attachment: { mediaType: "image/png", bytes: 100 } },
],
});
assert.deepEqual(extractMediaRefs(noId), []);
assert.equal(hasRefRecoverableMediaPayload(noId), false);
assert.equal(
hasUnrecoverableMediaPayload(noId),
true,
"no handle — pre-#5620 pin",
);
const emptyId = sidecarMsg("y", {
rawOpenaiContentParts: [
{ type: "image", attachment: { attachmentId: "" } },
],
});
assert.deepEqual(extractMediaRefs(emptyId), []);
assert.equal(hasUnrecoverableMediaPayload(emptyId), true);
const nonObject = sidecarMsg("z", {
rawOpenaiContentParts: [{ type: "image", attachment: "sha256:abc" }],
});
assert.deepEqual(extractMediaRefs(nonObject), []);
assert.equal(hasUnrecoverableMediaPayload(nonObject), true);
});

test("a DSH image message gets a numeric ref, never BLOCKED (#6875)", () => {
const messages = [
textMsg("a", "user", "alpha"),
sidecarMsg("dshimg", { rawOpenaiContentParts: [DSH_PART] }),
textMsg("b", "assistant", "beta"),
];
const state = createInitialState();
const res = assignRefs(messages, {
existing: state.messageRefs,
nextIndex: 1,
isProtected: hasUnrecoverableMediaPayload,
});
assert.equal(res.map.byRaw["dshimg"], "m00002");
});

// #6875 Q2: already-BLOCKED history needs no migration code — protection is
// re-evaluated every compress pass, so a fixed classifier lifts the pin and
// the #2607 migration mints a fresh number on the next pass.
test("legacy BLOCKED ref on DSH attachment media migrates to a fresh number (#6875)", () => {
const messages = [
textMsg("a", "user", "alpha"),
sidecarMsg("dshimg", { rawOpenaiContentParts: [DSH_PART] }),
textMsg("b", "assistant", "beta"),
];
const state = createInitialState();
state.messageRefs.byRaw["a"] = "m00001";
state.messageRefs.byRaw["dshimg"] = BLOCKED_REF;
state.messageRefs.byRaw["b"] = "m00002";
const res = assignRefs(messages, {
existing: state.messageRefs,
nextIndex: 3,
isProtected: hasUnrecoverableMediaPayload,
});
assert.equal(
res.map.byRaw["dshimg"],
"m00003",
"fresh number — never a reuse",
);
});

test("applyCompression records the DSH attachment id in the block summary (#6875)", () => {
const core = createCore();
const state = createInitialState();
const messages = [
textMsg("u", "user", "the task"),
sidecarMsg("img", { rawOpenaiContentParts: [DSH_PART] }),
textMsg("t", "assistant", "analyzing"),
];
state.messageRefs = assignRefs(messages, {
existing: state.messageRefs,
nextIndex: 1,
}).map;

const result = core.applyCompression({
ranges: [
{
startRef: "m00001",
endRef: "m00003",
summary: "folded work",
topic: "t",
},
],
messages,
state,
config: config(),
});
assert.equal(
result.result.errors.length,
0,
JSON.stringify(result.result.errors),
);
const block = result.state.blocks[0]!;
assert.ok(block.summary.includes(MEDIA_REFS_MARKER));
assert.ok(block.summary.includes(`- ${DSH_ID}`));
});
Loading
Loading