Skip to content

Stop paying the multimodal tax. Your AI agent keeps every image it has seen but only sends the ones it needs: lighter chats, no endless reconnecting, no starting over. Works with Codex, Claude Code, Cline, Gemini CLI, OpenCode, WorkBuddy and more. Install once, forget it.

fix(thread-index): 会话记录第一行还没写好时,不缓存读到的结果

chipfighter81c161d3cd58
diff --git a/plugin/src/thread-index.ts b/plugin/src/thread-index.ts
index 369538a..2e10e0d 100644
--- a/plugin/src/thread-index.ts
+++ b/plugin/src/thread-index.ts
@@ -296,9 +296,10 @@ export function forkOrigin(sessionsDir: string, threadId: string): { parent: str
const first = segments.get(threadId);
return first ? { found: true, record: firstRecord(first) } : { found: false, record: null };
});
- // No rollout yet: do not cache, it may appear once the thread starts.
- if (!meta.found) return null;
- const origin = meta.record?.type === "session_meta" && meta.record.payload.forked_from_id ? { parent: meta.record.payload.forked_from_id, at: meta.record.payload.timestamp ?? "" } : null;
+ // No rollout yet: do not cache, it may appear once the thread starts. v0.7: nor while its first line is not written
+ // yet: Codex creates the file a moment before, and a request can come in between.
+ if (!meta.found || !meta.record) return null;
+ const origin = meta.record.type === "session_meta" && meta.record.payload.forked_from_id ? { parent: meta.record.payload.forked_from_id, at: meta.record.payload.timestamp ?? "" } : null;
origins.set(threadId, origin);
return origin;
}
@@ -307,7 +308,7 @@ const spawners = new Map<string, string | null>();
// v0.7: the thread that started a subagent without history (session_meta source.subagent.thread_spawn); null for
// anything else: the user's tasks, forks (a subagent started with history is one), Codex's background tasks (review,
-// compaction, memory). Cached once the rollout exists: it never changes.
+// compaction, memory). Cached once the rollout's first line is there (as forkOrigin): it never changes.
export function spawnParent(sessionsDir: string, threadId: string): string | null {
const key = `${sessionsDir}\0${threadId}`;
if (spawners.has(key)) return spawners.get(key)!;
@@ -315,8 +316,8 @@ export function spawnParent(sessionsDir: string, threadId: string): string | nul
const first = segments.get(threadId);
return first ? { found: true, record: firstRecord(first) } : { found: false, record: null };
});
- if (!meta.found) return null;
- const payload = meta.record?.type === "session_meta" ? meta.record.payload : null;
+ if (!meta.found || !meta.record) return null;
+ const payload = meta.record.type === "session_meta" ? meta.record.payload : null;
const spawn = payload?.source?.subagent?.thread_spawn;
const parent = payload && !payload.forked_from_id && spawn ? spawn.parent_thread_id ?? payload.parent_thread_id : null;
spawners.set(key, typeof parent === "string" ? parent : null);
@@ -326,7 +327,8 @@ export function spawnParent(sessionsDir: string, threadId: string): string | nul
const starts = new Map<string, string | null>();
// v0.6: when a thread started, from its session_meta; a fork counts from the thread it came from (followed to the
-// first one; the fork's own time when that one is gone). null while the thread has no rollout. Cached once known.
+// first one; the fork's own time when that one is gone). null while the thread has no rollout, or no first line yet.
+// Cached once known.
export function threadStartedAt(sessionsDir: string, threadId: string, depth = 0): string | null {
const origin = forkOrigin(sessionsDir, threadId);
if (origin && depth < 16) return threadStartedAt(sessionsDir, origin.parent, depth + 1) ?? (origin.at || null);
@@ -336,8 +338,8 @@ export function threadStartedAt(sessionsDir: string, threadId: string, depth = 0
const first = segments.get(threadId);
return first ? { found: true, record: firstRecord(first) } : { found: false, record: null };
});
- if (!meta.found) return null;
- const at = (meta.record?.type === "session_meta" ? meta.record.payload.timestamp : null) ?? meta.record?.timestamp ?? null;
+ if (!meta.found || !meta.record) return null;
+ const at = (meta.record.type === "session_meta" ? meta.record.payload.timestamp : null) ?? meta.record.timestamp ?? null;
starts.set(key, typeof at === "string" ? at : null);
return starts.get(key)!;
}
diff --git a/spike/src/thread-index.test.ts b/spike/src/thread-index.test.ts
index f456986..2f6e91e 100644
--- a/spike/src/thread-index.test.ts
+++ b/spike/src/thread-index.test.ts
@@ -12,7 +12,7 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import test from "node:test";
import { zstdCompressSync } from "node:zlib";
-import { buildIndex, hasRollout, loadThreadIndex, readThreadHistory, setPixelCache, threadsStartedSince } from "../../plugin/src/thread-index.ts";
+import { buildIndex, forkOrigin, hasRollout, loadThreadIndex, readThreadHistory, setPixelCache, spawnParent, threadStartedAt, threadsStartedSince } from "../../plugin/src/thread-index.ts";
import { startThread, todayFolder } from "./testfixtures.ts";
import { png } from "./testkit.ts";
@@ -191,3 +191,25 @@ test("an image the model fetched with cam_view_image is a copy without an id, an
assert.deepEqual([...index.copies.values()], [{ key: "custom_tool_call_output:c1#0", of: "IMG-001", turn: 2 }]);
assert.equal(index.byKey.has("custom_tool_call_output:c1#0"), false);
});
+
+// v0.7 (self-test 2026-10-13): Codex creates a subagent's rollout file a moment before it writes the first line, and
+// the subagent's first request can come in between. What is read from an empty file must not stick.
+test("a rollout whose first line is not written yet is read again later: who started it, what it was forked from, when", () => {
+ const root = mkdtempSync(join(tmpdir(), "cam-early-"));
+ const sessionsDir = join(root, "sessions");
+ const folder = todayFolder(sessionsDir);
+ const CHILD = "01a0d307-0000-7000-8000-0000000000f1";
+ const FORK = "01a0d307-0000-7000-8000-0000000000f2";
+ const PARENT = "01a0d307-0000-7000-8000-0000000000f0";
+ const childFile = join(folder, `rollout-2026-10-13T10-00-00-${CHILD}.jsonl`);
+ const forkFile = join(folder, `rollout-2026-10-13T10-00-00-${FORK}.jsonl`);
+ writeFileSync(childFile, "");
+ writeFileSync(forkFile, "");
+ assert.deepEqual([spawnParent(sessionsDir, CHILD), forkOrigin(sessionsDir, FORK), threadStartedAt(sessionsDir, CHILD)], [null, null, null]);
+ const meta = (id: string, extra: Record<string, unknown>) => `${JSON.stringify({ timestamp: "2026-10-13T10:00:01Z", type: "session_meta", payload: { id, timestamp: "2026-10-13T10:00:00Z", ...extra } })}\n`;
+ writeFileSync(childFile, meta(CHILD, { parent_thread_id: PARENT, source: { subagent: { thread_spawn: { parent_thread_id: PARENT, depth: 1 } } } }));
+ writeFileSync(forkFile, meta(FORK, { forked_from_id: PARENT }));
+ assert.equal(spawnParent(sessionsDir, CHILD), PARENT);
+ assert.deepEqual(forkOrigin(sessionsDir, FORK), { parent: PARENT, at: "2026-10-13T10:00:00Z" });
+ assert.equal(threadStartedAt(sessionsDir, CHILD), "2026-10-13T10:00:00Z");
+});