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(panel): 面板只跟最新开始的任务,不再每隔几分钟重复询问;清理一周前的切换记录

chipfightera495fac6849d
diff --git a/plugin/src/binding.ts b/plugin/src/binding.ts
index c0c89c4..a26915b 100644
--- a/plugin/src/binding.ts
+++ b/plugin/src/binding.ts
@@ -2,10 +2,13 @@
// Codex may drop for a new one when the message is sent; the panel keeps asking about the prepared one (v0.1-8).
// Here: which threads the user just started (for the panel to switch to), and which thread such a panel was switched
// to, so every later call from that tab is about the new thread.
+// v0.1-16 — Codex hands such a panel a freshly prepared thread every few minutes, so the panel is offered its task again
+// each time: only the newest thread started while it was on screen counts (and any started within 20 s of it, which
+// the user picks from), and binding records older than a week are dropped.
// Input: the sessions directory (read-only) and the tool's data directory.
// Output: <data dir>/bindings/<the panel's thread id>.json = { "threadId": "<the thread it shows>", "at": "…" }.
-import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
+import { mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { bindingsDirOf } from "./paths.ts";
import { hasRollout, threadsStartedSince } from "./thread-index.ts";
@@ -31,7 +34,10 @@ export function isUserThread(meta: Json | null): boolean {
// seconds, so a thread started up to 5 s after the last note still counts (the user sent and left at once).
export type Shown = Array<[number, number]>;
export const LEFT_AT_ONCE_MS = 5_000;
+// Threads that started this close to the newest one started "at once" (e.g. two windows): the user picks.
+export const SAME_MOMENT_MS = 20_000;
const MAX_PERIODS = 20;
+const KEEP_BINDINGS_MS = 7 * 86_400_000;
export function newThreads(sessionsDir: string, shown: unknown): Array<{ threadId: string; startedAt: number }> {
const periods = (Array.isArray(shown) ? shown : [])
@@ -39,9 +45,11 @@ export function newThreads(sessionsDir: string, shown: unknown): Array<{ threadI
.slice(-MAX_PERIODS);
if (!periods.length) return [];
const since = Math.min(...periods.map(([from]) => from));
- return threadsStartedSince(sessionsDir, since)
- .filter((entry) => isUserThread(entry.meta) && periods.some(([from, to]) => entry.startedAt >= from && entry.startedAt <= to + LEFT_AT_ONCE_MS))
- .map(({ threadId, startedAt }) => ({ threadId, startedAt }));
+ const started = threadsStartedSince(sessionsDir, since)
+ .filter((entry) => isUserThread(entry.meta) && periods.some(([from, to]) => entry.startedAt >= from && entry.startedAt <= to + LEFT_AT_ONCE_MS));
+ // Oldest first: the newest is last. A task started earlier on this page is not offered once a newer one exists.
+ const newest = started.at(-1)?.startedAt ?? 0;
+ return started.filter((entry) => newest - entry.startedAt <= SAME_MOMENT_MS).map(({ threadId, startedAt }) => ({ threadId, startedAt }));
}
export function boundThread(threadId: string, dir = bindingsDirOf()): string | null {
@@ -63,6 +71,11 @@ export function bindThread(threadId: string, target: string, dir = bindingsDirOf
writeFileSync(`${file}.tmp`, JSON.stringify({ threadId: target, at: new Date().toISOString() }));
renameSync(`${file}.tmp`, file);
known.set(`${dir}|${threadId}`, target);
+ // One record is written each time Codex hands the panel a new prepared thread; a week is plenty.
+ for (const name of readdirSync(dir)) {
+ const old = join(dir, name);
+ try { if (name.endsWith(".json") && Date.now() - statSync(old).mtimeMs > KEEP_BINDINGS_MS) rmSync(old, { force: true }); } catch { /* gone meanwhile */ }
+ }
}
// The thread a panel's call is about: the panel's own once that has a rollout, otherwise the one it was switched to.
diff --git a/spike/src/binding.test.ts b/spike/src/binding.test.ts
index 5cfe621..b78347b 100644
--- a/spike/src/binding.test.ts
+++ b/spike/src/binding.test.ts
@@ -4,7 +4,7 @@
// Input: synthetic rollouts in today's folder of temporary sessions directories; output: Node test assertions only.
import assert from "node:assert/strict";
-import { mkdirSync, mkdtempSync } from "node:fs";
+import { existsSync, mkdirSync, mkdtempSync, utimesSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import test from "node:test";
@@ -69,3 +69,27 @@ test("a panel's calls follow its binding until its own thread has a rollout", ()
assert.equal(resolveThread(PANEL, sessionsDir, bindings), PANEL, "its own rollout wins");
assert.throws(() => bindThread(PANEL, "../x", bindings), /not a thread id/);
});
+
+test("of the tasks started while the panel was on screen, the newest is offered; two started at once are both offered", () => {
+ const { sessionsDir } = dirs();
+ const now = Date.now();
+ const shown: Shown = [[now - 120_000, now]];
+ startThread(sessionsDir, USER, {}, 60_000);
+ startThread(sessionsDir, FORK.replace("c3", "d3"), {}, 5_000);
+ assert.deepEqual(newThreads(sessionsDir, shown).map((entry) => entry.threadId), [FORK.replace("c3", "d3")], "the later task wins; the panel does not ask again every few minutes");
+ startThread(sessionsDir, AUTO.replace("c5", "d5"), {}, 1_000);
+ assert.deepEqual(newThreads(sessionsDir, shown).map((entry) => entry.threadId), [FORK.replace("c3", "d3"), AUTO.replace("c5", "d5")], "started within 20 s of each other: the user picks");
+});
+
+test("binding records older than a week are dropped when a new one is written", () => {
+ const { sessionsDir, bindings } = dirs();
+ startThread(sessionsDir, USER);
+ mkdirSync(bindings, { recursive: true });
+ const stale = join(bindings, "01a0d301-0000-7000-8000-00000000aaaa.json");
+ const recent = join(bindings, "01a0d301-0000-7000-8000-00000000bbbb.json");
+ for (const file of [stale, recent]) writeFileSync(file, JSON.stringify({ threadId: USER, at: "2026-09-01T00:00:00Z" }));
+ const eightDaysAgo = new Date(Date.now() - 8 * 86_400_000);
+ utimesSync(stale, eightDaysAgo, eightDaysAgo);
+ bindThread(PANEL, USER, bindings);
+ assert.deepEqual([existsSync(stale), existsSync(recent), existsSync(join(bindings, `${PANEL}.json`))], [false, true, true]);
+});