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.

fix(index): 原任务归档后,从它分叉的任务也能打开面板

chipfighter786c336e5639
diff --git a/docs/plan.md b/docs/plan.md
index 4401964..6807192 100644
--- a/docs/plan.md
+++ b/docs/plan.md
@@ -418,6 +418,7 @@ P1 通过后,再按用户的节奏一步步探索改写。
- **每个任务各有一个插件服务进程**。所以分叉出来的任务第一次开面板时,插件服务是新起的,内存里的缓存是空的。
- **任务记录的存放**:放在 `sessions/YYYY/MM/DD`,按任务创建那天的本地日期分目录。新任务、分叉和长任务的后续分页,都落在当天的目录里。
- **记录第一行的时间**:每行开头的 `timestamp` 是写入这一行的时间。新任务的记录是发第一条消息时才写的,所以第一行的时间就是任务开始的时间。
+- **归档的任务**:用户归档任务后,Codex 把它的记录挪到 `<CODEX_HOME>/archived_sessions`。从它分叉出来的任务,记录开头仍然指向它的某一段历史(`history_base`)。用户 2026-09-25 实测:原任务归档后,分叉出来的任务打开面板报“rollout segment not found”(v0.1-12)。
- **压缩的旧记录**:新版 Codex 会把一周没动过的任务记录压缩成 `.jsonl.zst`。要往里追加时,先解压回 `.jsonl`、再删掉压缩文件。以前的代码只认 `.jsonl`,面板打开这种旧任务时是空的。
### 设计
@@ -449,3 +450,4 @@ P1 通过后,再按用户的节奏一步步探索改写。
- 之后每次读:2–6 毫秒;
- 每张缩略图 0.08 秒,一张张补上,不挡列表。
- **压缩的旧记录**:`.jsonl.zst` 用 Node 自带的 zstd 解压后整份读入;同时存在 `.jsonl` 时以它为准。
+- **归档的任务**:完整查找时把 `archived_sessions` 也算上,同一段记录两边都有时以 `sessions` 里的为准。读的时候找不到某一段(例如上次查找之后才归档),立刻重新查找一遍,最多每 5 秒一次。
diff --git a/docs/tasks.md b/docs/tasks.md
index 88a1f6f..7627817 100644
--- a/docs/tasks.md
+++ b/docs/tasks.md
@@ -64,6 +64,9 @@
- 插件服务记录超过 300 毫秒的调用和耗时(`plugin-server.jsonl` 里的 `slow`),用户实测时可以对照。
- 顺带:面板能读 Codex 压缩过的旧任务记录(`.jsonl.zst`);以前打开一周没动的任务,面板是空的。
- 待用户在桌面版上实测。
+- [x] **v0.1-12 原任务归档后,从它分叉的任务打开面板报错**(用户 2026-09-25 报告:“rollout segment not found”;`thread-index.ts`;设计见 plan.md 第 15 节;证据:新增单元测试覆盖“先归档”和“读过之后才归档”两种情况;用修好的代码只读地跑用户那个任务,读出 16 轮、10 张图,标题也对)
+ - 原因:分叉出来的任务,记录开头指向原任务的某一段历史;原任务归档后,那一段挪到了 `archived_sessions`,面板只在 `sessions` 里找。
+ - 和任务里的图片数量、mp3 文件无关。
- [x] **v0.1-7 许可证和 CI**(用户 2026-09-25 定:MIT,并加上 CI):`LICENSE`(插件目录里也放一份);`.github/workflows/ci.yml` 在 Windows、macOS、Linux 上跑单元测试;问题反馈模板,方便 macOS、Linux 用户在 GitHub 上报问题。
- 待定:Windows 上不先停用就移除插件,Codex 会断连,只能靠卸载命令恢复;要不要用计划任务让引擎不依赖插件启动,见 spec.md 第 5 节。
diff --git a/plugin/src/thread-index.ts b/plugin/src/thread-index.ts
index f0150b1..0b7a9f7 100644
--- a/plugin/src/thread-index.ts
+++ b/plugin/src/thread-index.ts
@@ -6,10 +6,12 @@
// today and yesterday in between; fingerprints can be shared between processes through a cache folder. Rollouts
// Codex compressed after a week without activity (.jsonl.zst) are read as well.
// v0.1-10 — the first line of recent rollouts, so a panel opened on a new chat can find the thread that started there.
+// v0.1-12 — archived tasks (<CODEX_HOME>/archived_sessions) are looked up too: a fork's history may start in a page of
+// a task the user has archived since.
// Input: the sessions directory and a thread id. Output: ThreadIndex (in memory; files are parsed incrementally).
import { closeSync, existsSync, mkdirSync, openSync, readdirSync, readFileSync, readSync, statSync, writeFileSync } from "node:fs";
-import { basename, join } from "node:path";
+import { basename, dirname, join } from "node:path";
import { zstdDecompressSync } from "node:zlib";
import { findImages, type ImageRef } from "./images.ts";
import { decodePng } from "./png.ts";
@@ -87,10 +89,14 @@ function records(plain: string): Parsed[] {
// <thread>_<segment>. A fork's history_base points into its parent's segments, so bases are looked up globally.
// Codex files each rollout under <sessions>/YYYY/MM/DD of the day it was created (local time), so new threads, forks
// and later pages all land in today's folder: between full walks only today's and yesterday's folders (local and UTC)
-// are listed again. A file moved elsewhere is found by the next full walk, or at once when a read misses it.
-type Tree = { walkedAt: number; segments: Map<string, string>; threadOf: Map<string, string> };
+// are listed again. Archived tasks move to <CODEX_HOME>/archived_sessions, which full walks include (a task's own
+// folder wins). A file moved elsewhere is found by the next full walk, or at once when a read misses it (at most one
+// such extra walk every 5 s, so a history whose page was deleted does not walk the tree on every poll).
+type Tree = { walkedAt: number; missedAt: number; segments: Map<string, string>; threadOf: Map<string, string> };
const FULL_WALK_MS = 60_000;
+const MISSED_WALK_MS = 5_000;
const trees = new Map<string, Tree>();
+const archivedOf = (sessionsDir: string) => join(dirname(sessionsDir), "archived_sessions");
export function recentFolders(sessionsDir: string, now = Date.now()): string[] {
const pad = (n: number) => String(n).padStart(2, "0");
@@ -110,12 +116,13 @@ function addSegment(tree: Tree, file: string): void {
tree.threadOf.set(match[2] ?? match[1], match[1]);
}
-function allSegments(sessionsDir: string, fresh = false): Tree {
+function allSegments(sessionsDir: string, missed = false): Tree {
const now = Date.now();
let tree = trees.get(sessionsDir);
- if (!tree || fresh || now - tree.walkedAt > FULL_WALK_MS) {
- tree = { walkedAt: now, segments: new Map(), threadOf: new Map() };
- for (const file of walk(sessionsDir)) addSegment(tree, file);
+ const again = missed && (!tree || now - tree.missedAt >= MISSED_WALK_MS);
+ if (!tree || again || now - tree.walkedAt > FULL_WALK_MS) {
+ tree = { walkedAt: now, missedAt: again ? now : tree?.missedAt ?? 0, segments: new Map(), threadOf: new Map() };
+ for (const file of [...walk(archivedOf(sessionsDir)), ...walk(sessionsDir)]) addSegment(tree, file);
trees.set(sessionsDir, tree);
return tree;
}
@@ -139,7 +146,8 @@ const baseOf = (file: string): Json | null => (records(file)[0]?.type === "sessi
function effective(segments: Map<string, string>, segmentId: string, endOffset = Number.POSITIVE_INFINITY): Parsed[] {
const file = segments.get(segmentId);
- if (!file) throw new Error(`rollout segment not found: ${segmentId}`);
+ // Not listed (yet): the caller walks the tree again once.
+ if (!file) throw Object.assign(new Error(`rollout segment not found: ${segmentId}`), { code: "ENOENT" });
const base = baseOf(file);
const inherited = base ? effective(segments, base.thread_id, base.end_byte_offset) : [];
return [...inherited, ...records(file).filter((record) => record.offset < endOffset)];
diff --git a/spike/src/thread-index.test.ts b/spike/src/thread-index.test.ts
index 90356a0..cc88711 100644
--- a/spike/src/thread-index.test.ts
+++ b/spike/src/thread-index.test.ts
@@ -1,7 +1,7 @@
// Purpose: P2 — the thread index read from rollouts: stable ids in history order, turn numbers, incremental
// re-reads of an appended file, and paginated segments chained through history_base.
// v0.1-10/11 — compressed rollouts, new rollouts found in today's folder between full walks, a moved rollout found
-// again, and pixel fingerprints shared through the cache folder.
+// again, and pixel fingerprints shared through the cache folder. v0.1-12 — a fork's history in an archived task's page.
// Input: synthetic rollouts in a temporary directory; output: Node test assertions only.
import assert from "node:assert/strict";
@@ -136,3 +136,26 @@ test("pixel fingerprints are shared through the cache folder: a stored one is us
setPixelCache(null);
}
});
+
+test("a fork's history may start in a page of a task the user archived, before or after the panel first read it", () => {
+ const PARENT = "01a0d301-0000-7000-8000-0000000000aa";
+ const PAGE = "01a0d301-0000-7000-8000-0000000000ab";
+ const pageName = `rollout-2026-09-23T10-00-00-${PARENT}_${PAGE}.jsonl`;
+ const parentPage = line("session_meta", { id: PARENT }) + turn("t1") + upload("msg_1", "t1", "a.png", red);
+ const fork = line("session_meta", { id: THREAD, forked_from_id: PARENT, history_base: { thread_id: PAGE, end_byte_offset: Buffer.byteLength(parentPage) } }) + turn("t2") + upload("msg_2", "t2", "b.png", blue);
+ for (const archivedFirst of [true, false]) {
+ const root = mkdtempSync(join(tmpdir(), "cam-archived-"));
+ const dir = join(root, "sessions");
+ const archived = join(root, "archived_sessions");
+ const parentDay = join(dir, "2026", "09", "23");
+ const day = join(dir, "2026", "09", "25");
+ for (const folder of [archived, parentDay, day]) mkdirSync(folder, { recursive: true });
+ writeFileSync(join(archivedFirst ? archived : parentDay, pageName), parentPage + upload("msg_x", "t1", "after-fork.png", blue));
+ writeFileSync(join(day, `rollout-2026-09-25T18-00-00-${THREAD}.jsonl`), fork);
+ assert.deepEqual(loadThreadIndex(dir, THREAD).images.map((i) => i.name), ["a.png", "b.png"], archivedFirst ? "archived before" : "not archived yet");
+ if (!archivedFirst) {
+ renameSync(join(parentDay, pageName), join(archived, pageName));
+ assert.deepEqual(loadThreadIndex(dir, THREAD).images.map((i) => i.name), ["a.png", "b.png"], "archived after the first read");
+ }
+ }
+});