Skip to content

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.

feat(spike): 分叉任务沿用原任务在分叉时的勾选,之后各自独立

chipfighter05a623543689
diff --git a/docs/p3-report.md b/docs/p3-report.md
index 36206e8..89b25bd 100644
--- a/docs/p3-report.md
+++ b/docs/p3-report.md
@@ -41,9 +41,12 @@
- 由 `cam.ts install` 在终端里拉起的引擎不受这个影响,它靠自己检查 Codex 进程,Codex 全部退出 30 秒后自行退出。
- 如果以后这种断开真的碍事,可以让引擎彻底脱离 Codex 的管理,例如用 Windows 计划任务来启动它。不过这需要在用户的系统里注册一个任务,届时再请用户决定。
-## 5. 需要用户决定
+## 5. 分叉任务的勾选
-- **分叉任务的勾选**:分叉出来的任务有自己独立的勾选状态,默认全部勾选。所以在父任务里取消的图,到了分叉任务里仍会照常发送(自测已确认)。要不要在分叉时继承父任务的勾选?
+- 自测发现:分叉出来的任务原本从“全部勾选”开始,所以父任务里取消的图,到了分叉任务里又会照常发送。
+- 用户决定分叉时沿用父任务的勾选。
+- 现在的做法:分叉任务第一次用到时,如果还没有自己的勾选,就复制父任务在分叉之前取消的那些,存成自己的;之后两边各管各的。分叉的分叉会逐层向上找。
+- 父任务的编号和分叉时间,取自分叉任务本地记录的第一行(`forked_from_id`、`timestamp`)。4 个单元测试覆盖。
## 6. 下一步
diff --git a/docs/spec.md b/docs/spec.md
index c47640d..0c0fa87 100644
--- a/docs/spec.md
+++ b/docs/spec.md
@@ -27,6 +27,7 @@
- 新出现的图默认勾选,照常发送,包括和前面内容相同的副本(用户 2026-09-24 决定)。用户没有取消任何图时,请求原样转发,Codex 的表现和没装工具时一样。
- 任何一条出现记录都可以在任何一轮取消或重新勾选,从下一次请求起生效,不换任务、不重建。取消的记录仍留在列表里。
+- 分叉出来的任务,沿用原任务在分叉那一刻的勾选;之后两边各管各的(用户 2026-09-24 决定)。
- 取消时由工具判断用哪种占位符:本次请求里还有一条内容相同、仍在发送的记录,就用“重复”占位符指向它;没有,就用普通占位符。
- 只是看起来相似的图(例如缩放或重新压缩过)不算内容相同。
- 工具认不出的图,或者没法安全改写的请求,一律原样发送,并记下原因。只有用户明确取消的图才会被换掉。
diff --git a/spike/src/panel-state.ts b/spike/src/panel-state.ts
index 8f93960..21f5ee9 100644
--- a/spike/src/panel-state.ts
+++ b/spike/src/panel-state.ts
@@ -6,7 +6,7 @@
import { existsSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { dataDir, requestStatsDirOf, selectionDirOf } from "./paths.ts";
-import { readSelection, writeSelection } from "./selection.ts";
+import { effectiveSelection, writeSelection } from "./selection.ts";
import { buildIndex, imageData, readThreadHistory, type IndexedImage, type ThreadIndex } from "./thread-index.ts";
import { pngThumbnail } from "./thumbnail.ts";
@@ -82,7 +82,7 @@ function load(threadId: string, options: PanelOptions) {
const root = options.dataRoot ?? dataDir();
const history = readThreadHistory(options.sessionsDir, threadId);
const index = buildIndex(threadId, history);
- const selection = readSelection(threadId, selectionDirOf(root));
+ const selection = effectiveSelection(threadId, options.sessionsDir, selectionDirOf(root));
const statsFile = join(requestStatsDirOf(root), `${threadId}.json`);
const lastRequest = existsSync(statsFile) ? JSON.parse(readFileSync(statsFile, "utf8")) : null;
return { root, history, index, selection, lastRequest };
diff --git a/spike/src/proxy.ts b/spike/src/proxy.ts
index 553e475..8acbea1 100644
--- a/spike/src/proxy.ts
+++ b/spike/src/proxy.ts
@@ -21,7 +21,7 @@ import { DEFAULT_PORT, ENGINE_SERVICE, engineHealth, watchForCodex } from "./eng
import type { ImageRef } from "./images.ts";
import { dataDir, proxyLogDirOf, requestStatsDirOf } from "./paths.ts";
import { rewriteItems, type Described } from "./rewrite.ts";
-import { readSelection, selectionDir } from "./selection.ts";
+import { effectiveSelection, selectionDir } from "./selection.ts";
import { loadThreadIndex, pixelHashOf, type ThreadIndex } from "./thread-index.ts";
type Json = Record<string, any>;
@@ -168,9 +168,9 @@ export function hasUnsafeInteger(json: string): boolean {
return false;
}
-export function hasUnchecked(threadId: string | null, dir = selectionDir): boolean {
+export function hasUnchecked(threadId: string | null, dir = selectionDir, sessionsDir = join(codexHome(), "sessions")): boolean {
if (!threadId) return false;
- try { return Object.keys(readSelection(threadId, dir).unchecked).length > 0; } catch { return false; }
+ try { return Object.keys(effectiveSelection(threadId, sessionsDir, dir).unchecked).length > 0; } catch { return false; }
}
// P2: replace the thread's unchecked images with placeholders. Whenever the body cannot be handled safely it is
@@ -185,7 +185,7 @@ export function rewriteBody(original: Buffer, encoding: string | undefined, thre
if (hasUnsafeInteger(text)) return { body: original, report: { skipped: "integer beyond 2^53 would change when re-serialized" } };
let index: ThreadIndex;
try { index = loadThreadIndex(sessionsDir, threadId); } catch (error) { return { body: original, report: { skipped: `thread index: ${String(error)}` } }; }
- const selection = readSelection(threadId, dir);
+ const selection = effectiveSelection(threadId, sessionsDir, dir);
const describe = (ref: ImageRef): Described | undefined => {
const known = index.byKey.get(ref.key);
if (known) return known;
diff --git a/spike/src/selection.test.ts b/spike/src/selection.test.ts
new file mode 100644
index 0000000..327c76e
--- /dev/null
+++ b/spike/src/selection.test.ts
@@ -0,0 +1,66 @@
+// Purpose: P3 — a forked thread starts with what its parent had unchecked when the fork was made, then the two are
+// independent; ordinary threads are unaffected. Covers forks of forks and threads whose rollout appears later.
+// Input: synthetic rollouts and selection files in temporary folders; output: Node test assertions only.
+
+import assert from "node:assert/strict";
+import { existsSync, mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import test from "node:test";
+import { hasUnchecked } from "./proxy.ts";
+import { effectiveSelection, readSelection, writeSelection } from "./selection.ts";
+
+const PARENT = "01a0d301-0000-7000-8000-0000000000f0";
+const FORK = "01a0d301-0000-7000-8000-0000000000f1";
+const FORK2 = "01a0d301-0000-7000-8000-0000000000f2";
+const PLAIN = "01a0d301-0000-7000-8000-0000000000f3";
+const LATE = "01a0d301-0000-7000-8000-0000000000f4";
+
+function setup() {
+ const root = mkdtempSync(join(tmpdir(), "cam-fork-"));
+ const day = join(root, "sessions", "2026", "09", "24");
+ mkdirSync(day, { recursive: true });
+ const rollout = (id: string, meta: Record<string, unknown>) =>
+ writeFileSync(join(day, `rollout-2026-09-24T10-00-00-${id}.jsonl`), `${JSON.stringify({ type: "session_meta", payload: { id, ...meta } })}\n`);
+ rollout(PARENT, { timestamp: "2026-09-24T10:00:00Z" });
+ rollout(FORK, { forked_from_id: PARENT, timestamp: "2026-09-24T12:00:00Z" });
+ rollout(FORK2, { forked_from_id: FORK, timestamp: "2026-09-24T12:30:00Z" });
+ rollout(PLAIN, { timestamp: "2026-09-24T10:00:00Z" });
+ const dir = join(root, "selection");
+ writeSelection({ threadId: PARENT, unchecked: { "msg_1#0": { id: "IMG-001", at: "2026-09-24T11:00:00Z" }, "msg_2#0": { id: "IMG-002", at: "2026-09-24T13:00:00Z" } } }, dir);
+ return { sessionsDir: join(root, "sessions"), day, dir, rollout };
+}
+
+test("a fork takes what its parent had unchecked by the time of the fork, and keeps it as its own", () => {
+ const { sessionsDir, dir } = setup();
+ const fork = effectiveSelection(FORK, sessionsDir, dir);
+ assert.deepEqual(Object.values(fork.unchecked).map((entry) => entry.id), ["IMG-001"], "IMG-002 was unchecked after the fork");
+ assert.equal(fork.inheritedFrom, PARENT);
+ assert.equal(existsSync(join(dir, `${FORK}.json`)), true);
+ assert.equal(hasUnchecked(FORK, dir, sessionsDir), true);
+});
+
+test("after the fork the two selections are independent", () => {
+ const { sessionsDir, dir } = setup();
+ effectiveSelection(FORK, sessionsDir, dir);
+ writeSelection({ threadId: FORK, unchecked: {} }, dir);
+ assert.equal(Object.keys(readSelection(PARENT, dir).unchecked).length, 2, "checking in the fork leaves the parent alone");
+ writeSelection({ threadId: PARENT, unchecked: {} }, dir);
+ writeSelection({ threadId: FORK, unchecked: { "msg_1#0": { id: "IMG-001", at: "2026-09-24T14:00:00Z" } } }, dir);
+ assert.equal(Object.keys(effectiveSelection(FORK, sessionsDir, dir).unchecked).length, 1, "the parent's change does not reach the fork");
+});
+
+test("a fork of a fork asks its own parent; ordinary threads stay empty and get no file", () => {
+ const { sessionsDir, dir } = setup();
+ assert.deepEqual(Object.values(effectiveSelection(FORK2, sessionsDir, dir).unchecked).map((entry) => entry.id), ["IMG-001"]);
+ assert.deepEqual(effectiveSelection(PLAIN, sessionsDir, dir).unchecked, {});
+ assert.equal(existsSync(join(dir, `${PLAIN}.json`)), false);
+ assert.equal(hasUnchecked(PLAIN, dir, sessionsDir), false);
+});
+
+test("a thread whose rollout is not written yet is looked up again later", () => {
+ const { sessionsDir, dir, rollout } = setup();
+ assert.deepEqual(effectiveSelection(LATE, sessionsDir, dir).unchecked, {});
+ rollout(LATE, { forked_from_id: PARENT, timestamp: "2026-09-24T12:00:00Z" });
+ assert.deepEqual(Object.values(effectiveSelection(LATE, sessionsDir, dir).unchecked).map((entry) => entry.id), ["IMG-001"]);
+});
diff --git a/spike/src/selection.ts b/spike/src/selection.ts
index 354088b..f64f092 100644
--- a/spike/src/selection.ts
+++ b/spike/src/selection.ts
@@ -1,12 +1,15 @@
// Purpose: P2 — per-thread selection state: which image occurrences the user unchecked. Written by the CLI (later
// the panel), read by the proxy on every request of that thread.
+// P3 — a forked thread starts with what its parent had unchecked when the fork was made (user decision 2026-09-24);
+// from then on the two are independent.
// Input/Output: <data dir>/selection/<thread id>.json, e.g. { "threadId": "…", "unchecked": { "<key>": { "id": "IMG-003", "at": "…" } } }.
import { existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { selectionDirOf } from "./paths.ts";
+import { forkOrigin } from "./thread-index.ts";
-export type Selection = { threadId: string; unchecked: Record<string, { id: string; at: string }> };
+export type Selection = { threadId: string; unchecked: Record<string, { id: string; at: string }>; inheritedFrom?: string };
export const selectionDir = selectionDirOf();
const THREAD_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
@@ -35,3 +38,17 @@ export function writeSelection(selection: Selection, dir = selectionDir): void {
writeFileSync(`${file}.tmp`, JSON.stringify(selection, null, 2));
renameSync(`${file}.tmp`, file);
}
+
+// The thread's own selection; a fork without one takes the images its parent had unchecked by the time of the fork
+// (a fork of a fork asks its own parent in turn) and keeps them as its own file from then on.
+export function effectiveSelection(threadId: string, sessionsDir: string, dir = selectionDir, depth = 0): Selection {
+ if (existsSync(fileFor(threadId, dir)) || depth > 16) return readSelection(threadId, dir);
+ const origin = forkOrigin(sessionsDir, threadId);
+ if (!origin) return { threadId, unchecked: {} };
+ const parent = effectiveSelection(origin.parent, sessionsDir, dir, depth + 1);
+ const unchecked = Object.fromEntries(Object.entries(parent.unchecked).filter(([, entry]) => !origin.at || entry.at <= origin.at));
+ if (!Object.keys(unchecked).length) return { threadId, unchecked: {} };
+ const inherited: Selection = { threadId, unchecked, inheritedFrom: origin.parent };
+ writeSelection(inherited, dir);
+ return inherited;
+}
diff --git a/spike/src/thread-index.ts b/spike/src/thread-index.ts
index 72d04a7..59f8640 100644
--- a/spike/src/thread-index.ts
+++ b/spike/src/thread-index.ts
@@ -132,6 +132,42 @@ export function buildIndex(threadId: string, history: Parsed[]): ThreadIndex {
return { threadId, images, byKey, turns: turnNumbers.size };
}
+// The first line alone (session_meta can be long), read in chunks without parsing the rest of the file.
+function firstLine(file: string): string {
+ const fd = openSync(file, "r");
+ try {
+ const chunks: Buffer[] = [];
+ const chunk = Buffer.alloc(64 * 1024);
+ for (let position = 0; ; position += chunk.length) {
+ const read = readSync(fd, chunk, 0, chunk.length, position);
+ if (read <= 0) break;
+ const end = chunk.subarray(0, read).indexOf(0x0a);
+ chunks.push(Buffer.from(chunk.subarray(0, end >= 0 ? end : read)));
+ if (end >= 0 || read < chunk.length) break;
+ }
+ return Buffer.concat(chunks).toString("utf8");
+ } finally {
+ closeSync(fd);
+ }
+}
+
+const origins = new Map<string, { parent: string; at: string } | null>();
+
+// A forked thread's session_meta names the thread it came from and when it was forked. Cached: it never changes.
+export function forkOrigin(sessionsDir: string, threadId: string): { parent: string; at: string } | null {
+ if (origins.has(threadId)) return origins.get(threadId)!;
+ const first = allSegments(sessionsDir).segments.get(threadId);
+ // No rollout yet: do not cache, it may appear once the thread starts.
+ if (!first) return null;
+ let origin: { parent: string; at: string } | null = null;
+ try {
+ const meta = JSON.parse(firstLine(first));
+ if (meta.type === "session_meta" && meta.payload?.forked_from_id) origin = { parent: meta.payload.forked_from_id, at: meta.payload.timestamp ?? "" };
+ } catch { /* unreadable first line: treat as not a fork */ }
+ origins.set(threadId, origin);
+ return origin;
+}
+
// The thread's model-visible history records, following paginated segments back through history_base.
export function readThreadHistory(sessionsDir: string, threadId: string): Parsed[] {
const { segments, threadOf } = allSegments(sessionsDir);