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
fix(claude-index): number images of parallel calls left off the chain
chipfighterc005268f0ba4
diff --git a/plugin/src/claude-index.ts b/plugin/src/claude-index.tsindex 4b56360..eb81af1 100644--- a/plugin/src/claude-index.ts+++ b/plugin/src/claude-index.ts@@ -175,6 +175,9 @@ function relinked(records: ClaudeRecord[], byUuid: Map<string, ClaudeRecord>): Mreturn parents;}+// v0.10: the calls activeChain found off the chain (out-of-order parallel results) and their results.+const offChain = new WeakSet<ClaudeRecord>();+// The conversation as it stands: from the newest message (not a subagent's sidechain) back to the start. v0.10:// `links` are where boundaries whose history sits in an earlier transcript lead (withHistory).export function activeChain(records: ClaudeRecord[], links: Map<string, string> = new Map()): ClaudeRecord[] {@@ -198,6 +201,28 @@ export function activeChain(records: ClaudeRecord[], links: Map<string, string>record = parent ? byUuid.get(parent) : undefined;}chain.reverse();+ // v0.10: the results can come back out of order (Claude Code 2.1.309, 2026-10-31). When the conversation goes on from+ // a result that is not the last call's, the calls made after that one hang off the chain too. Claude Code gives all+ // the calls of one reply its message id and sends them together, so they join the chain after that reply's records+ // and the result the conversation went on from, in the order they were made; their results join below like the+ // others. Their images were left out before: what was written then is translated (offChain, oldNumbering).+ const replyOf = (record: ClaudeRecord): string | null => (record.type === "assistant" && typeof record.message?.id === "string" ? record.message.id : null);+ const replies = new Set(chain.map(replyOf).filter((id): id is string => id !== null));+ const strays = new Map<string, ClaudeRecord[]>();+ for (const record of byUuid.values()) {+ const reply = replyOf(record);+ if (reply === null || !replies.has(reply) || seen.has(record.uuid!) || record.isSidechain || !blocksOf(record).some((block) => block?.type === "tool_use")) continue;+ const group = strays.get(reply) ?? [];+ if (!record.parentUuid || !(seen.has(record.parentUuid) || group.some((other) => other.uuid === record.parentUuid))) continue;+ strays.set(reply, [...group, record]);+ }+ for (const [reply, group] of strays) {+ let at = chain.length - 1;+ while (at >= 0 && replyOf(chain[at]) !== reply) at--;+ while (at + 1 < chain.length && chain[at + 1].type === "user" && blocksOf(chain[at + 1]).length > 0 && blocksOf(chain[at + 1]).every((block) => block?.type === "tool_result")) at++;+ chain.splice(at + 1, 0, ...group);+ for (const record of group) { seen.add(record.uuid!); offChain.add(record); }+ }// Parallel tool calls (Claude Code 2.1.293): each result hangs off its own call's record and the conversation goes// on from the last result, so the others sit off this chain though Claude Code sends them with it. Each result whose// call is on the chain and has no result there joins it right after its call; a result on an abandoned branch@@ -216,6 +241,7 @@ export function activeChain(records: ClaudeRecord[], links: Map<string, string>const results = blocksOf(record).filter((block) => block?.type === "tool_result");if (results.length === 0 || !results.every((block) => calls.has(String(block.tool_use_id)) && !answered.has(String(block.tool_use_id)))) continue;for (const block of results) answered.add(String(block.tool_use_id));+ if (offChain.has(byUuid.get(record.parentUuid)!)) offChain.add(record);after.set(record.parentUuid, [...(after.get(record.parentUuid) ?? []), record]);}return after.size === 0 ? chain : chain.flatMap((record) => [record, ...(after.get(record.uuid!) ?? [])]);@@ -273,7 +299,8 @@ export type Eras = { family: FamilyFile[]; since: string | null };export type ClaudeIndexOptions = { links?: Map<string, string>; eras?: Eras; chain?: ClaudeRecord[] };// The ids a transcript started at chain position `from` gave its images, as the plugin numbered it then: one by one-// from there, a copy our fetch tool returned (its label naming an id of this numbering, same content) taking none.+// from there, a copy our fetch tool returned (its label naming an id of this numbering, same content) taking none, and+// the calls found off the chain (offChain) taking none either: the plugin did not see them then.// Only up to `until` (nothing written later is translated), so it stays the same as the session goes on: kept.const numberings = new Map<string, Map<string, string>>();function oldNumbering(chain: ClaudeRecord[], from: number, until: string): Map<string, string> {@@ -287,7 +314,7 @@ function oldNumbering(chain: ClaudeRecord[], from: number, until: string): Map<sconst content = new Map<string, { contentId: string; pixels: string | null }>();for (let position = from; position < end; position++) {const record = chain[position];- if (record.type !== "user") continue;+ if (record.type !== "user" || offChain.has(record)) continue;for (const found of keysOf(record)) {if (seen.has(found.key)) continue;seen.add(found.key);@@ -311,13 +338,18 @@ function translator(chain: ClaudeRecord[], eras: Eras | undefined): (timestamp:const positions = new Map<string, number>();chain.forEach((record, at) => { if (typeof record.uuid === "string") positions.set(record.uuid, at); });const family = eras.family.filter((file) => positions.has(file.start)).sort((a, b) => a.created.localeCompare(b.created));- if (!family.some((file) => file.start !== chain[0].uuid)) return () => null;+ // Calls found off the chain shift every id after theirs, in the session's first transcript too.+ const hidden = chain.some((record) => offChain.has(record));+ if (!hidden && !family.some((file) => file.start !== chain[0].uuid)) return () => null;const used = new Map<string, Map<string, string>>();return (timestamp) => {if (typeof timestamp !== "string" || timestamp >= eras.since!) return null;let start: string | null = null;for (const file of family) if (file.created <= timestamp) start = file.start;- if (start === null || start === chain[0].uuid) return null;+ if (start === null || start === chain[0].uuid) {+ if (!hidden) return null;+ start = chain[0].uuid!;+ }if (!used.has(start)) used.set(start, oldNumbering(chain, positions.get(start)!, eras.since!));return used.get(start)!;};diff --git a/plugin/src/selection.ts b/plugin/src/selection.tsindex e899cc8..784bc4b 100644--- a/plugin/src/selection.ts+++ b/plugin/src/selection.ts@@ -81,7 +81,24 @@ function sinceIn(name: string, dir: string): string | null {export const ensureAutoDefault = (dir = selectionDir, now = new Date()): void => ensureSince(AUTO_DEFAULT, dir, now);// null until this version has run here: then nothing is on by default.export const autoDefaultSince = (dir = selectionDir): string | null => sinceIn(AUTO_DEFAULT, dir);-export const ensureStableIds = (dir = selectionDir, now = new Date()): void => ensureSince(STABLE_IDS, dir, now);+// The numbering rules the ids follow. 2: the calls of a reply found off the chain, when their results came back out of+// order, get ids too (claude-index.ts). Under newer rules the start time starts again, so what was written under the+// earlier ones is translated.+const ID_RULES = 2;+export function ensureStableIds(dir = selectionDir, now = new Date()): void {+ const file = join(dir, STABLE_IDS);+ let rules: unknown;+ try {+ rules = JSON.parse(readFileSync(file, "utf8")).rules;+ } catch {+ mkdirSync(dir, { recursive: true });+ try { writeFileSync(file, JSON.stringify({ since: now.toISOString(), rules: ID_RULES }, null, 2), { flag: "wx" }); } catch (error) { if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error; }+ return;+ }+ if (rules === ID_RULES) return;+ writeFileSync(file, JSON.stringify({ since: now.toISOString(), rules: ID_RULES }, null, 2));+ sinceOf.delete(file);+}// null until this version has run here: then nothing is translated.export const stableIdsSince = (dir = selectionDir): string | null => sinceIn(STABLE_IDS, dir);diff --git a/spike/src/claude-fixtures.ts b/spike/src/claude-fixtures.tsindex 6a18e24..c255ddc 100644--- a/spike/src/claude-fixtures.ts+++ b/spike/src/claude-fixtures.ts@@ -29,7 +29,11 @@ export function transcript(base = 0) {return {records,prompt: (content: Json[] | string, extra: Json = {}, parent?: string | null) => add({ type: "user", message: { role: "user", content }, ...extra }, parent),- assistant: (block: Json, extra: Json = {}, parent?: string | null) => add({ type: "assistant", message: { role: "assistant", content: [block] }, ...extra }, parent),+ // messageId: the API message the block belongs to (Claude Code writes each block of one reply as its own record).+ assistant: (block: Json, extra: Json = {}, parent?: string | null) => {+ const { messageId, ...rest } = extra;+ return add({ type: "assistant", message: { role: "assistant", content: [block], ...(messageId ? { id: messageId } : {}) }, ...rest }, parent);+ },toolResult: (toolUseId: string, content: Json[], extra: Json = {}, parent?: string | null) => add({ type: "user", message: { role: "user", content: [{ type: "tool_result", tool_use_id: toolUseId, content }] }, toolUseResult: {}, ...extra }, parent),attachment: () => add({ type: "attachment", attachment: { type: "x" } }),boundary: () => add({ type: "system", subtype: "compact_boundary", compactMetadata: {}, logicalParentUuid: last }, null),diff --git a/spike/src/claude-index.test.ts b/spike/src/claude-index.test.tsindex 31c9a01..6d892e4 100644--- a/spike/src/claude-index.test.ts+++ b/spike/src/claude-index.test.ts@@ -60,6 +60,50 @@ test("parallel tool calls: a result hanging off its own call is part of the conv]);});+// The calls of one reply, the conversation going on from b's result: Claude Code 2.1.309, 2026-10-31.+function outOfOrder(t: ReturnType<typeof transcript>, at = "") {+ const reply = { messageId: "msg_parallel", ...(at ? { timestamp: at } : {}) };+ const read = (name: string) => ({ type: "tool_use", id: `toolu_${name}`, name: "Read", input: { file_path: `C:\\work\\${name}.png` } });+ const useA = t.assistant(read("a"), reply);+ const useB = t.assistant(read("b"), reply);+ const useC = t.assistant(read("c"), reply);+ // The results came back a, c, b: the third call hangs off the second one's record, beside b's result.+ t.toolResult("toolu_a", [image(pngA)], {}, useA);+ t.toolResult("toolu_c", [image(pngC)], {}, useC);+ return t.toolResult("toolu_b", [image(pngB)], {}, useB);+}++test("parallel tool calls whose results came back out of order: calls off the chain count too, in call order", () => {+ const t = transcript();+ t.prompt("Read a.png, b.png and c.png together");+ const resultB = outOfOrder(t);+ t.assistant(text("Three images."), { messageId: "msg_next" }, resultB);+ assert.deepEqual(buildClaudeIndex("s", t.records).images.map((entry) => [entry.id, entry.key, entry.name]), [+ ["IMG-001", "tool:toolu_a#0", "a.png"],+ ["IMG-002", "tool:toolu_b#0", "b.png"],+ ["IMG-003", "tool:toolu_c#0", "c.png"],+ ]);+});++test("record lines written before this version, which left such a call out, are translated to the ids it gives now", () => {+ const t = transcript();+ t.prompt("Read a.png, b.png and c.png together", { timestamp: "2026-10-30T10:00:00.000Z" });+ const resultB = outOfOrder(t, "2026-10-30T10:00:01.000Z");+ t.assistant(text("Three images."), { messageId: "msg_2", timestamp: "2026-10-30T10:00:02.000Z" }, resultB);+ t.prompt("Read d.png", { timestamp: "2026-10-30T10:01:00.000Z" });+ t.assistant({ type: "tool_use", id: "toolu_d", name: "Read", input: { file_path: "C:\\work\\d.png" } }, { messageId: "msg_3", timestamp: "2026-10-30T10:01:01.000Z" });+ t.toolResult("toolu_d", [image(pngA)], { timestamp: "2026-10-30T10:01:02.000Z" });+ // Written while c.png had no id: this IMG-003 is d.png.+ t.assistant(text("[//]: # (cam IMG-003 desk|d sheet)\n\nOne more."), { messageId: "msg_4", timestamp: "2026-10-30T10:01:03.000Z" });+ const before = buildClaudeIndex("s", t.records, null, { eras: { family: [], since: "2026-10-31T00:00:00.000Z" } });+ assert.equal(before.images.find((entry) => entry.key === "tool:toolu_d#0")?.id, "IMG-004");+ assert.equal(before.notes.get("IMG-004")?.line, "desk|d sheet");+ assert.equal(before.notes.get("IMG-003"), undefined);+ // Written by this version: its IMG-003 is c.png.+ const after = buildClaudeIndex("s", t.records, null, { eras: { family: [], since: "2026-10-29T00:00:00.000Z" } });+ assert.equal(after.notes.get("IMG-003")?.line, "desk|d sheet");+});+test("a tool result left on an abandoned branch stays out", () => {const t = transcript();t.prompt("first");diff --git a/spike/src/selection.test.ts b/spike/src/selection.test.tsindex 2323e81..78b88f6 100644--- a/spike/src/selection.test.ts+++ b/spike/src/selection.test.ts@@ -8,7 +8,7 @@ import { tmpdir } from "node:os";import { join } from "node:path";import test from "node:test";import { hasUnchecked, needsRewrite } from "../../plugin/src/proxy.ts";-import { autoDefaultSince, effectiveSelection, ensureAutoDefault, mainTaskOf, readSelection, sideChatSelection, writeSelection } from "../../plugin/src/selection.ts";+import { autoDefaultSince, effectiveSelection, ensureAutoDefault, ensureStableIds, mainTaskOf, readSelection, sideChatSelection, stableIdsSince, writeSelection } from "../../plugin/src/selection.ts";import { startThread } from "./testfixtures.ts";const PARENT = "01a0d301-0000-7000-8000-0000000000f0";@@ -100,6 +100,24 @@ test("v0.6: the default's start time is written once and kept", () => {assert.equal(autoDefaultSince(dir), "2026-09-24T11:00:00.000Z");});+// v0.10: the ids' start time is written once too, but starts again when the numbering rules change: what was written+// under the earlier rules is then translated (claude-index.ts).+test("v0.10: the stable ids' start time is kept, and starts again under newer numbering rules", () => {+ const { dir } = setup();+ assert.equal(stableIdsSince(dir), null);+ ensureStableIds(dir, new Date("2026-11-01T00:00:00Z"));+ ensureStableIds(dir, new Date("2026-11-02T00:00:00Z"));+ assert.equal(stableIdsSince(dir), "2026-11-01T00:00:00.000Z");+ // Written by a build before parallel calls off the chain got ids (no rules in the file).+ const older = mkdtempSync(join(tmpdir(), "cam-stable-"));+ writeFileSync(join(older, "stable-ids.json"), JSON.stringify({ since: "2026-10-31T01:13:00.000Z" }));+ assert.equal(stableIdsSince(older), "2026-10-31T01:13:00.000Z");+ ensureStableIds(older, new Date("2026-11-03T00:00:00Z"));+ assert.equal(stableIdsSince(older), "2026-11-03T00:00:00.000Z");+ ensureStableIds(older, new Date("2026-11-04T00:00:00Z"));+ assert.equal(stableIdsSince(older), "2026-11-03T00:00:00.000Z");+});+test("v0.6: a task started after the default began has automatic selection on, unless it has its own switch", () => {const { sessionsDir, dir, rollout } = setup();ensureAutoDefault(dir, new Date("2026-09-24T11:00:00Z"));