A local plugin for image-heavy chats in Codex, Claude Code and other agents: earlier images stop going out with every message, and the model fetches the one it needs by its ID, also after compaction.
TypeScript ai-agentsclaude-codeclaude-code-plugincodexcodex-plugincontext-managementdeveloper-toolsimage-generationmcpmultimodal
feat(features): send an interrupted turn's unlabelled images once more; allow corrections
chipfightere82d0b86d70d
diff --git a/plugin/src/claude-index.ts b/plugin/src/claude-index.tsindex 4ca58b5..4b56360 100644--- a/plugin/src/claude-index.ts+++ b/plugin/src/claude-index.ts@@ -54,6 +54,8 @@ export type ClaudeIndex = ThreadIndex & {};const SESSION_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;+// v0.10: the text Claude Code records as a prompt when the user stops a turn.+const INTERRUPTED = /^\[Request interrupted by user/;const FETCHED_LINE = new RegExp(`^\\[(?:图片|Image) (IMG-\\d{3,}) (?:${FETCHED_STATE.zh}|${FETCHED_STATE.en})(?:|| \\|)`);export function claudeHome(env: NodeJS.ProcessEnv = process.env): string {@@ -277,7 +279,7 @@ const numberings = new Map<string, Map<string, string>>();function oldNumbering(chain: ClaudeRecord[], from: number, until: string): Map<string, string> {let end = from;while (end < chain.length && !(typeof chain[end].timestamp === "string" && chain[end].timestamp >= until)) end++;- const cacheKey = `${chain[from].uuid} ${chain[end - 1]?.uuid} ${end - from}`;+ const cacheKey = `${chain[from].uuid}\0${chain[end - 1]?.uuid}\0${end - from}`;const known = numberings.get(cacheKey);if (known) return known;const keys = new Map<string, string>();@@ -337,6 +339,8 @@ export function buildClaudeIndex(sessionId: string, records: ClaudeRecord[], tralet previous: string[] = [];// v0.8: what the model noted in record lines at the end of its replies (notes.ts), by image id.const notes = new Map<string, Notes>();+ const interrupted = new Set<number>();+ const skipped = new Set<number>();// v0.10: ids written under another numbering, back to this one's (through the image they named).const numberingAt = translator(chain, options.eras);const ownId = (numbering: Map<string, string> | null) => (id: string) => (numbering ? byKey.get(numbering.get(id) ?? "")?.id ?? null : id);@@ -359,6 +363,9 @@ export function buildClaudeIndex(sessionId: string, records: ClaudeRecord[], traturnNumbers.set(record.uuid!, turn);if (current.length) previous = current;current = [];+ // v0.10: Claude Code's mark that the user stopped the turn before: that turn was interrupted, this one is no turn.+ const said = typeof content === "string" ? content : Array.isArray(content) ? content.map(textOf).filter(Boolean).join("\n") : "";+ if (INTERRUPTED.test(said)) { if (turn > 1) interrupted.add(turn - 1); skipped.add(turn); }}const compacted = position < lastBoundary;const pasted: string[] = [];@@ -410,7 +417,7 @@ export function buildClaudeIndex(sessionId: string, records: ClaudeRecord[], traconst twin = images.find((other) => other.name && (other.contentId === entry.contentId || (entry.pixelSha256 !== null && other.pixelSha256 === entry.pixelSha256)));if (twin) entry.name = twin.name;}- return { threadId: sessionId, images, byKey, turns: turn, turnNumbers, copies, notes, uploads, chain, title: titleOf(records, chain), replies, transcript, summary, previous: previousOf(sessionId, options.eras?.family ?? []) };+ return { threadId: sessionId, images, byKey, turns: turn, turnNumbers, copies, notes, uploads, chain, title: titleOf(records, chain), replies, transcript, summary, previous: previousOf(sessionId, options.eras?.family ?? []), interrupted, skipped };}// v0.10: the sessions this one continues, most recent first, each with the time this one took over: the one it was@@ -563,7 +570,7 @@ export function withHistory(file: string, records: ClaudeRecord[], home = claudeif (!first || !isBoundary(first) || depth > 16) return { records, links: new Map() };const original = originalOf(file, records, first, home);if (!original) return { records, links: new Map() };- const key = `${original} ${first.uuid}`;+ const key = `${original}\0${first.uuid}`;let stamp: string;try { stamp = stampOf(original); } catch { return { records, links: new Map() }; }let cached = prefixes.get(key);@@ -638,7 +645,7 @@ export function loadClaudeIndex(sessionId: string, home = claudeHome(), since: sif (!file) return emptyClaudeIndex(sessionId);const own = readTranscript(file);const { records, links } = withHistory(file, own, home);- const key = [own.length, records.length, since, listings.get(dirname(file))?.names.length ?? -1].join(" ");+ const key = [own.length, records.length, since, listings.get(dirname(file))?.names.length ?? -1].join("\0");const known = loaded.get(own);if (known && known.key === key) return known.index;const index = indexFor(sessionId, file, records, links, since);diff --git a/plugin/src/claude-rewrite.ts b/plugin/src/claude-rewrite.tsindex 853cc8b..56082bc 100644--- a/plugin/src/claude-rewrite.ts+++ b/plugin/src/claude-rewrite.ts@@ -94,6 +94,8 @@ export type ClaudeRewriteOptions = {noted?: boolean;// v0.8: the feature line note and ids on this turn's new images too (rewrite.ts).features?: boolean;+ // v0.10: keys of the images sent once more for their feature line; their labels say so.+ awaiting?: Set<string>;};export function rewriteMessages(@@ -103,12 +105,13 @@ export function rewriteMessages(pixelHash: (image: RequestImage) => string | null,options: ClaudeRewriteOptions = {},): { messages: Json[]; report: RewriteReport } {- const { lang = "zh", copies = new Map<string, string>(), auto, noted = false, features = false } = options;+ const { lang = "zh", copies = new Map<string, string>(), auto, noted = false, features = false, awaiting = new Set<string>() } = options;const refs = findRequestImages(messages, index);const describe = (ref: RequestImage): ClaudeImage | undefined => {const image = ref.key ? index.byKey.get(ref.key) : undefined;const notes = image ? index.notes?.get(image.id) : undefined;- return image && notes ? { ...image, notes } : image;+ if (!image) return undefined;+ return notes || awaiting.has(image.key) ? { ...image, ...(notes ? { notes } : {}), ...(awaiting.has(image.key) ? { awaiting: true } : {}) } : image;};const report: RewriteReport = { images: refs.length, replaced: [], locked: [], sentContentIds: [], copies: [] };const copied = refs.filter((ref) => ref.key !== null && copies.has(ref.key));diff --git a/plugin/src/proxy.ts b/plugin/src/proxy.tsindex d3fd705..c414d8d 100644--- a/plugin/src/proxy.ts+++ b/plugin/src/proxy.ts@@ -45,7 +45,7 @@ import { readRoute, registerRoute, routeBaseUrl, routingPort, validateBaseUrl, timport { addCodexCompacted, rewriteItems, type Described } from "./rewrite.ts";import { connectDirectly, pluginGone, usesEngine } from "./setup.ts";import { claudeSelection, effectiveSelection, ensureAutoDefault, ensureStableIds, selectionDir, sideChatSelection, stableIdsSince } from "./selection.ts";-import { imageData, loadThreadIndex, pixelHashOf, pixelHashOfData, setPixelCache, sideChatOrigin, writesNotes, type ThreadIndex } from "./thread-index.ts";+import { awaitingLine, imageData, loadThreadIndex, pixelHashOf, pixelHashOfData, setPixelCache, sideChatOrigin, writesNotes, type ThreadIndex } from "./thread-index.ts";type Json = Record<string, any>;const here = dirname(fileURLToPath(import.meta.url));@@ -334,11 +334,13 @@ export function rewriteBody(original: Buffer, encoding: string | undefined, threlet index: ThreadIndex;try { index = loadThreadIndex(sessionsDir, origin ?? threadId); } catch (error) { return { body: original, report: { skipped: `thread index: ${String(error)}` } }; }const selection = side ?? effectiveSelection(threadId, sessionsDir, dir);+ // v0.10: images sent once more for their feature line (awaitingLine), marked in their labels.+ const awaiting = new Set<string>();const describe = (ref: ImageRef): Described | undefined => {const known = index.byKey.get(ref.key);// v0.8: with what the model noted about it, for its placeholder.const notes = known ? index.notes.get(known.id) : undefined;- if (known) return notes ? { ...known, notes } : known;+ if (known) return { ...known, ...(notes ? { notes } : {}), ...(awaiting.has(known.key) ? { awaiting: true } : {}) };const stored = selection.unchecked[ref.key];return stored ? { id: stored.id, name: ref.name, label: ref.label, kind: ref.kind, turn: null, width: ref.width, height: ref.height } : undefined;};@@ -346,12 +348,13 @@ export function rewriteBody(original: Buffer, encoding: string | undefined, threconst current = side ? index.turns + 1 : currentTurnOf(index, turnId);const copies = new Map([...index.copies.values()].filter((copy) => copy.turn === null || copy.turn < current).map((copy) => [copy.key, copy.of]));let leaveOut = new Set(Object.keys(selection.unchecked));+ // v0.8: feature lines in a task's own requests, not a side chat's or a subagent's.+ const features = !origin && writesNotes(sessionsDir, threadId);if (selection.auto) {const pinned = selection.pinned ?? {};- leaveOut = new Set(index.images.filter((image) => image.replaceable && image.turn !== null && image.turn < current && !pinned[image.key]).map((image) => image.key));+ for (const image of index.images) if (features && awaitingLine(index, image, current)) awaiting.add(image.key);+ leaveOut = new Set(index.images.filter((image) => image.replaceable && image.turn !== null && image.turn < current && !pinned[image.key] && !awaiting.has(image.key)).map((image) => image.key));}- // v0.8: feature lines in a task's own requests, not a side chat's or a subagent's.- const features = !origin && writesNotes(sessionsDir, threadId);const rewritten = rewriteItems(json.input, describe, leaveOut, pixels, { lang: currentLang(), copies, features, ...(selection.auto ? { auto: { currentTurn: current } } : {}) });const { report } = rewritten;let items = rewritten.items;@@ -445,9 +448,11 @@ export function rewriteClaudeBody(original: Buffer, encoding: string | undefinedconst current = index.turns;const copies = new Map([...index.copies.values()].filter((copy) => copy.turn === null || copy.turn < current).map((copy) => [copy.key, copy.of]));let leaveOut = new Set(Object.keys(selection.unchecked));+ // v0.10: images of an interrupted turn without a feature line yet are sent once more (awaitingLine).+ const awaiting = new Set(selection.auto ? index.images.filter((image) => awaitingLine(index, image, current)).map((image) => image.key) : []);if (selection.auto) {const pinned = selection.pinned ?? {};- leaveOut = new Set(index.images.filter((image) => image.replaceable && image.turn !== null && image.turn < current && !pinned[image.key]).map((image) => image.key));+ leaveOut = new Set(index.images.filter((image) => image.replaceable && image.turn !== null && image.turn < current && !pinned[image.key] && !awaiting.has(image.key)).map((image) => image.key));}const pixels = (ref: RequestImage) => (ref.key ? index.byKey.get(ref.key)?.pixelSha256 : undefined) ?? pixelHashOfData(ref, ref.block.source.data);const lang = currentLang();@@ -466,7 +471,7 @@ export function rewriteClaudeBody(original: Buffer, encoding: string | undefined});const compactedOut = summaryIn !== null && resend.length < compactedImages.length;// v0.8: feature lines in every main conversation (a subagent's requests do not come here).- const rewrote = rewriteMessages(json.messages, index, leaveOut, pixels, { lang, copies, noted: compactedOut, features: true, ...(selection.auto ? { auto: { currentTurn: current } } : {}) });+ const rewrote = rewriteMessages(json.messages, index, leaveOut, pixels, { lang, copies, noted: compactedOut, features: true, awaiting, ...(selection.auto ? { auto: { currentTurn: current } } : {}) });const report = rewrote.report;let messages = rewrote.messages;let compacted: Json | null = null;diff --git a/plugin/src/rewrite.ts b/plugin/src/rewrite.tsindex dffbc43..7fd1672 100644--- a/plugin/src/rewrite.ts+++ b/plugin/src/rewrite.ts@@ -22,8 +22,9 @@ import type { Lang } from "./language.ts";import type { Notes, Rating } from "./notes.ts";type Json = Record<string, any>;-// notes (v0.8): what the model noted about the image, shown in its placeholder.-export type Described = { id: string; name: string | null; label: string | null; kind: ImageKind; turn: number | null; width: number | null; height: number | null; notes?: Notes };+// notes (v0.8): what the model noted about the image, shown in its placeholder. awaiting (v0.10): an image of an+// interrupted turn sent once more for its feature line (thread-index.ts, awaitingLine); its label says so.+export type Described = { id: string; name: string | null; label: string | null; kind: ImageKind; turn: number | null; width: number | null; height: number | null; notes?: Notes; awaiting?: boolean };export type Replacement = { id: string; key: string; kind: ImageKind; mode: "plain" | "duplicate"; sameAs: string | null; base64Chars: number };// noteAt: where the developer message explaining omitted images was inserted, if one was. copies: the ids whose copies// fetched in an earlier turn were left out. v0.8: featureAt, where the feature line note went; labeled, how many@@ -34,6 +35,8 @@ type FetchWords = { title: string; description: string; ids: string; missing: (itype Words = {kind: Record<ImageKind, string>; unnamed: string; turn: (n: number) => string; thisTurn: string; newImage: string;omitted: string; notRepeated: string; included: string; separator: string;+ // v0.10: the label of an image sent once more for its feature line (Described.awaiting).+ includedAwaiting: string;heading: (id: string, label: string | null, state: string, fields: string[]) => string;plain: (id: string) => string; duplicate: (id: string, where: string) => string; note: string;autoPlain: (id: string) => string; autoNote: string; copy: (id: string) => string; fetch: FetchWords;@@ -65,6 +68,7 @@ const WORDS: Record<Lang, Words> = {omitted: "已省略",notRepeated: "未重复提供",included: "已提供",+ includedAwaiting: "已提供,还没有特征行",separator: ",",heading: (id, label, state, fields) => `[图片 ${id}${label ? `(${label})` : ""} ${state}|${fields.join("|")}]`,// v0.8: shorter; the context management note says why the image is left out (it was "原图被用户省略以节省上下文,情况@@ -148,6 +152,8 @@ const WORDS: Record<Lang, Words> = {"- 一张图里有好几个地点的(分镜、抽帧拼图)写“多个地点”;不在任何地方的,写它是什么:角色、物品、文档、网页。","- 同一个地点沿用你之前用过的叫法。","- 只写看得见的东西,不写“好看”“像谁”这类判断。每张图只写一次,以后不改。用户上传的、你生成的、你查看的、工具返回的图都要写;用 cam_view_image 取回的图和没有编号的图不用写。",+ "- 标着“[图片 IMG-xxx 已提供,还没有特征行…]”的图,是之前那一轮被打断、没来得及写特征行的,这一轮再提供一次:也给它写一行。",+ "- 写过的特征行有错(比如用户指出了),照同样的格式整行重写,开头加“更正:”,例如“[//]: # (cam IMG-005 更正:角色|正面近景,左眉有疤)”,它会替换原来那一行。","- 用户评价某张图时(包括隔了几轮才说),先确定是哪一张,再照用户的原话记一行,15 字以内,例如“[//]: # (cam IMG-003 评价:太暗)”。评价是用户自己的看法,你不认同也照记。拿不准是哪一张就不记,可以在正文里问用户指的是哪一张,但不要提记录的事。用户只是评价、没有提要求时,正文简短回应评价本身就行,不要说明你记了什么、对应到了哪张图。","- 这些行写在正文前面,和正文之间空一行,一张图写一行;括号里不要用半角括号。这一轮没有新图、也没有评价时,不写这些行。这些行只给这个工具读,用户在界面上看不到。正文照常回答,不用提到这些行;用户评价图片时,也不用回应记录这件事,比如“记下了”“我会记住”“之后会参考”。","- 以后被省略的图,占位里会带着你写的特征和记下的评价,对话压缩之后也还在。要选图时先看这些;拿不准时,先把原图要回来看(怎么要,见“上下文管理说明”),或者问用户是哪一张。",@@ -167,6 +173,7 @@ const WORDS: Record<Lang, Words> = {omitted: "omitted",notRepeated: "not repeated",included: "included",+ includedAwaiting: "included, no feature line yet",separator: ", ",heading: (id, label, state, fields) => `[Image ${id}${label ? ` (${label})` : ""} ${state} | ${fields.join(" | ")}]`,plain: (id) => `To see it again, write "need ${id}" in your reply.`,@@ -226,6 +233,8 @@ const WORDS: Record<Lang, Words> = {"- An image of several places (a storyboard, a contact sheet) gets \"several places\"; one set nowhere gets what it is: character, object, document, web page.","- For a place you named before, reuse that name.","- Only what can be seen; no judgments such as \"beautiful\" or \"looks like someone\". One line per image, written once and never changed. Write one for the images the user uploaded, the ones you generated or viewed, and those in tool results; not for images you fetched with cam_view_image or images without an ID.",+ "- An image marked \"[Image IMG-xxx included, no feature line yet…]\" is from an earlier turn that was interrupted before you wrote its line; it is included once more in this turn: write its line too.",+ "- If a feature line you wrote turns out wrong (for example, the user points it out), write the whole line again in the same form, starting with \"corrected:\", for example \"[//]: # (cam IMG-005 corrected: character | front close-up, scar on the left eyebrow)\"; it replaces the old one.","- When the user rates an image, even turns later, first make sure which one it is, then note the user's own words in a line of about 8 words at most, for example \"[//]: # (cam IMG-003 rated: too dark)\". The rating is the user's own view: note it even if you see it differently. If you are not sure which image it is, do not note it; you may ask the user which one they mean, without mentioning the noting. When the user only rates images and asks for nothing, a short reply to what they said is enough; do not explain what you noted or which image you matched it to.","- Put these lines before your reply with a blank line after them, one image per line, and no parentheses inside them. In a turn with no new image and no rating, write none of these lines. These lines are for this tool only; the user does not see them on screen. Answer as usual and do not mention these lines; when the user rates an image, do not acknowledge the noting either, such as \"noted\", \"I'll keep that in mind\" or \"I'll refer to this later\".","- Images left out later carry your feature line and the ratings you noted in their placeholders, also after a compaction. Check them when you pick an image; when unsure, get the original back first (the \"Context management note\" says how) or ask the user which one.",@@ -284,7 +293,7 @@ export function duplicatePlaceholder(image: Described, same: Described, lang: La// Put right before an image that is still sent, so the model can tell which id it is: Codex's own labels ("[Image #2]")// start again in every message.export function includedLabel(image: Described, lang: Lang = "zh"): string {- return heading(WORDS[lang], image, WORDS[lang].included);+ return heading(WORDS[lang], image, image.awaiting ? WORDS[lang].includedAwaiting : WORDS[lang].included);}// v0.3: automatic selection.diff --git a/plugin/src/thread-index.ts b/plugin/src/thread-index.tsindex 72b77b3..746b73a 100644--- a/plugin/src/thread-index.ts+++ b/plugin/src/thread-index.ts@@ -26,8 +26,18 @@ type Json = Record<string, any>;export type IndexedImage = ImageRef & { id: string; turn: number | null; bytes: number; pixelSha256: string | null; compacted?: boolean };// v0.3: an image the model fetched with our tool, a copy of `of` sent in its own turn only (spec: automatic selection).export type FetchedCopy = { key: string; of: string; turn: number | null };-// v0.10 (Codex): items, the items of the history, which image positions point into.-export type ThreadIndex = { threadId: string; images: IndexedImage[]; byKey: Map<string, IndexedImage>; turns: number; turnNumbers: Map<string, number>; copies: Map<string, FetchedCopy>; notes: Map<string, Notes>; items?: Json[] };+// v0.10 (Codex): items, the items of the history, which image positions point into. interrupted: the turns the user+// stopped before the model wrote its last reply (its record lines); skipped: turns that are only Claude Code's mark of+// such a stop.+export type ThreadIndex = { threadId: string; images: IndexedImage[]; byKey: Map<string, IndexedImage>; turns: number; turnNumbers: Map<string, number>; copies: Map<string, FetchedCopy>; notes: Map<string, Notes>; items?: Json[]; interrupted?: Set<number>; skipped?: Set<number> };++// v0.10 (docs/v0.10/plan.md): an image of an interrupted turn without a feature line yet, with no turn finished since:+// sent once more in the current turn, so the model can write its line (automatic selection would leave it out).+export function awaitingLine(index: ThreadIndex, image: IndexedImage, current: number): boolean {+ if (image.turn === null || image.turn >= current || index.notes.get(image.id)?.line || !index.interrupted?.has(image.turn)) return false;+ for (let turn = image.turn + 1; turn < current; turn++) if (!index.interrupted.has(turn) && !index.skipped?.has(turn)) return false;+ return true;+}export type Parsed = { offset: number; type: string; payload: Json; timestamp: string | null };type FileCache = { size: number; mtimeMs: number; parsedTo: number; records: Parsed[] };@@ -232,6 +242,7 @@ export function buildIndex(threadId: string, history: Parsed[]): ThreadIndex {let current: string | null = null;// v0.10: the latest compaction of Codex: how many items came before it, and the images of the history it kept.let compaction: { before: number; kept: Set<string> } | null = null;+ const interrupted = new Set<number>();for (const record of history) {if (record.type === "compacted" && Array.isArray(record.payload.replacement_history)) {compaction = { before: items.length, kept: new Set(findImages(record.payload.replacement_history).map((ref) => ref.key)) };@@ -240,6 +251,7 @@ export function buildIndex(threadId: string, history: Parsed[]): ThreadIndex {current = record.payload.turn_id;if (!turnNumbers.has(current!)) turnNumbers.set(current!, turnNumbers.size + 1);}+ if (record.type === "event_msg" && record.payload.type === "turn_aborted" && current) interrupted.add(turnNumbers.get(current)!);if (record.type !== "response_item") continue;items.push(record.payload);activeTurn.push(current);@@ -276,7 +288,7 @@ export function buildIndex(threadId: string, history: Parsed[]): ThreadIndex {if (twin) entry.name = twin.name;}if (compaction) for (const entry of images) if (entry.item < compaction.before && !compaction.kept.has(entry.key)) entry.compacted = true;- return { threadId, images, byKey, turns: turnNumbers.size, turnNumbers, copies, notes, items };+ return { threadId, images, byKey, turns: turnNumbers.size, turnNumbers, copies, notes, items, interrupted };}// The first line alone (session_meta can be long), read in chunks without parsing the rest of the file.diff --git a/spike/src/feature-backfill.test.ts b/spike/src/feature-backfill.test.tsnew file mode 100644index 0000000..7f2a496--- /dev/null+++ b/spike/src/feature-backfill.test.ts@@ -0,0 +1,100 @@+// Purpose: v0.10 — an image of a turn the user interrupted before the model wrote its feature line is sent once more+// in the next turn, its label saying it still needs one (thread-index.ts awaitingLine; proxy.ts); and the feature line+// note says how to write it, and how to correct a wrong line (rewrite.ts, notes.ts).+// Input: synthetic rollouts and transcripts in temporary folders; output: Node test assertions only.++import assert from "node:assert/strict";+import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";+import test from "node:test";+import zlib from "node:zlib";+import { buildClaudeIndex } from "../../plugin/src/claude-index.ts";+import { rewriteBody, rewriteClaudeBody } from "../../plugin/src/proxy.ts";+import { featureNote } from "../../plugin/src/rewrite.ts";+import { writeSelection } from "../../plugin/src/selection.ts";+import { awaitingLine, loadThreadIndex } from "../../plugin/src/thread-index.ts";+import { image, pngA, pngB, text, transcript } from "./claude-fixtures.ts";+import { png } from "./testkit.ts";++type Json = Record<string, any>;+const threadId = "01a0d301-0000-7000-8000-0000000000bf";+const red = `data:image/png;base64,${png(16, 16, () => [220, 30, 30]).toString("base64")}`;+const upload = (id: string, turn: string, url: string): Json => ({+ type: "message", id, role: "user",+ content: [{ type: "input_text", text: "看图" }, { type: "input_text", text: String.raw`<image name=[Image #1] path="C:\w\a.png">` }, { type: "input_image", image_url: url }, { type: "input_text", text: "</image>" }],+ internal_chat_message_metadata_passthrough: { turn_id: turn },+});++function codex(rollout: Json[]) {+ const root = mkdtempSync(join(tmpdir(), "cam-backfill-"));+ const sessions = join(root, "sessions");+ const selections = join(root, "selection");+ mkdirSync(join(sessions, "2026", "10", "31"), { recursive: true });+ writeFileSync(join(sessions, "2026", "10", "31", `rollout-2026-10-31T01-00-00-${threadId}.jsonl`), [{ type: "session_meta", payload: { id: threadId } }, ...rollout].map((record) => JSON.stringify(record)).join("\n") + "\n");+ writeSelection({ threadId, unchecked: {}, auto: true, autoAt: "2026-10-31T00:00:00Z", autoSince: "2026-10-31T00:00:00Z" }, selections);+ return { sessions, selections };+}+const turn = (id: string) => ({ type: "event_msg", payload: { type: "task_started", turn_id: id } });+const item = (payload: Json) => ({ type: "response_item", payload });+const request = (items: Json[]) => zlib.zstdCompressSync(Buffer.from(JSON.stringify({ model: "m", input: items, stream: true })));+const sentText = (out: Buffer) => zlib.zstdDecompressSync(out).toString("utf8");++test("Codex: the image of an interrupted turn without a feature line goes out once more, labelled", () => {+ const first = upload("msg_1", "t1", red);+ const next = { type: "message", id: "msg_2", role: "user", content: [{ type: "input_text", text: "继续" }] };+ const { sessions, selections } = codex([turn("t1"), item(first), { type: "event_msg", payload: { type: "turn_aborted", turn_id: "t1" } }, turn("t2"), item(next)]);+ const index = loadThreadIndex(sessions, threadId);+ assert.deepEqual([...index.interrupted!], [1]);+ assert.equal(awaitingLine(index, index.images[0], 2), true);+ const { body, report } = rewriteBody(request([first, next]), "zstd", threadId, sessions, selections, "t2");+ assert.deepEqual(report.replaced, []);+ assert.ok(sentText(body).includes(red.slice(40)), "sent again");+ assert.match(sentText(body), /\[图片 IMG-001(\[Image #1\]) 已提供,还没有特征行|a\.png|用户上传|第 1 轮/);+ // Once a turn finished after it, it is left out as usual, line or not.+ const done = codex([turn("t1"), item(first), { type: "event_msg", payload: { type: "turn_aborted", turn_id: "t1" } }, turn("t2"), item(next), { type: "event_msg", payload: { type: "task_complete", turn_id: "t2" } }, turn("t3")]);+ const later = rewriteBody(request([first, next, { type: "message", role: "user", content: [{ type: "input_text", text: "再来" }] }]), "zstd", threadId, done.sessions, done.selections, "t3");+ assert.deepEqual(later.report.replaced.map((entry: Json) => entry.id), ["IMG-001"]);+ // A turn that was not interrupted: its images are left out in the next one, as before.+ const plain = codex([turn("t1"), item(first), { type: "event_msg", payload: { type: "task_complete", turn_id: "t1" } }, turn("t2"), item(next)]);+ assert.deepEqual(rewriteBody(request([first, next]), "zstd", threadId, plain.sessions, plain.selections, "t2").report.replaced.map((entry: Json) => entry.id), ["IMG-001"]);+ // With its line written (in the reply that was cut short), nothing is sent again.+ const lined = codex([turn("t1"), item(first), item({ type: "message", role: "assistant", content: [{ type: "output_text", text: "[//]: # (cam IMG-001 书房|红色方块)\n\n…" }] }), { type: "event_msg", payload: { type: "turn_aborted", turn_id: "t1" } }, turn("t2"), item(next)]);+ assert.deepEqual(rewriteBody(request([first, next]), "zstd", threadId, lined.sessions, lined.selections, "t2").report.replaced.map((entry: Json) => entry.id), ["IMG-001"]);+});++test("Claude: an image before Claude Code's interruption mark goes out once more, labelled", async () => {+ const t = transcript();+ const p1 = t.prompt([image(pngA), text("look")]);+ t.assistant(text("Looking…"));+ t.prompt("[Request interrupted by user]");+ t.prompt([image(pngB), text("and this one")]);+ const index = buildClaudeIndex("s", t.records);+ assert.deepEqual([[...index.interrupted!], [...index.skipped!]], [[1], [2]]);+ assert.deepEqual(index.images.map((entry) => awaitingLine(index, entry, 3)), [true, false]);+ const home = mkdtempSync(join(tmpdir(), "cam-backfill-claude-"));+ const dir = mkdtempSync(join(tmpdir(), "cam-backfill-sel-"));+ const session = "11111111-2222-4333-8444-5555555555bf";+ mkdirSync(join(home, "projects", "D--work"), { recursive: true });+ writeFileSync(join(home, "projects", "D--work", `${session}.jsonl`), t.records.map((record) => JSON.stringify(record)).join("\n") + "\n");+ writeSelection({ threadId: session, unchecked: {}, auto: true, autoAt: "2026-10-31T00:00:00Z", autoSince: "2026-10-31T00:00:00Z" }, dir);+ const body = Buffer.from(JSON.stringify({ model: "claude-haiku-5-5", messages: [+ { role: "user", content: [image(pngA), text("look")] },+ { role: "assistant", content: [text("Looking…")] },+ { role: "user", content: [text("[Request interrupted by user]")] },+ { role: "user", content: [image(pngB), text("and this one")] },+ ] }));+ const out = rewriteClaudeBody(body, undefined, session, dir, home, {});+ const messages = JSON.parse(out.body.toString("utf8")).messages as Json[];+ assert.deepEqual(out.report.replaced, []);+ const labels = messages.flatMap((message) => (Array.isArray(message.content) ? message.content : [])).filter((block: Json) => block.type === "text" && /^\[图片 IMG-/.test(block.text)).map((block: Json) => block.text.split("|")[0]);+ assert.deepEqual(labels, ["[图片 IMG-001 已提供,还没有特征行", "[图片 IMG-002 已提供"]);+ assert.equal(p1.length > 0, true);+});++test("the feature line note says how to write a missing line and how to correct a wrong one", () => {+ assert.match(featureNote("zh"), /标着“\[图片 IMG-xxx 已提供,还没有特征行…\]”的图,是之前那一轮被打断、没来得及写特征行的/);+ assert.match(featureNote("zh"), /开头加“更正:”/);+ assert.match(featureNote("en"), /"\[Image IMG-xxx included, no feature line yet…\]"/);+ assert.match(featureNote("en"), /starting with "corrected:"/);+});