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(claude): 桌面版闲置时关掉的面板在下一轮重新打开;只有用户关掉的才不再打开

chipfighter1d8db3777f8b
diff --git a/docs/v0.6/plan.md b/docs/v0.6/plan.md
new file mode 100644
index 0000000..4b8861c
--- /dev/null
+++ b/docs/v0.6/plan.md
@@ -0,0 +1,54 @@
+# v0.6 方案:默认开启自动选图;Claude 桌面版自动打开面板
+
+> 状态:用户 2026-10-13 定,开发中。规格以 [spec.md](../spec.md) 为准,实现见 [design.md](../design.md)。
+
+## 1. 背景与目标
+
+- 有些用户连侧边栏都不会打开(用户 2026-10-13)。默认开着自动选图,不打开面板也能省下之前各轮的图。
+- Claude 桌面版里,用 `/cam` 打开的面板有时会自己关掉;用户希望不用输入 `/cam`,面板自己打开并保持打开,除非用户自己关掉。
+- 用户还问:自动选图时模型需要多张图,会不会都取回?
+
+## 2. 规则(用户 2026-10-13 定)
+
+1. **默认开启自动选图**:所有新任务、新会话,Codex 和 Claude 桌面版都一样。用户电脑上已有的任务和会话保持原样。用户在面板里改过的,以用户的为准。
+2. **面板自动打开**(Claude 桌面版):只在有图的会话里打开。桌面版自己关掉的,会话再用时重新打开;用户自己关掉的,这个会话里不再自动打开,直到用户输入 `/cam`。
+
+## 3. 事实与依据
+
+- **面板为什么会自己关**(2026-10-13,Claude 桌面版的日志 `%LOCALAPPDATA%\Claude\Logs\main.log`):会话的一轮结束后开始计时(`[WarmLifecycle:preview] Starting idle timeout … 1800s`),30 分钟里没有新的一轮,就断开这个会话的自带浏览器(`Idle timeout reached, disconnecting`、`[Preview] Stopped servers for worktree`),面板的标签页跟着关掉。Claude Code 进程照常运行,之后的请求照样经过引擎,Mod 的 `session.start` 不会再运行。没有设置项能改这个时间,插件也不该让会话一直醒着。
+- **另一种情况**:内存紧张时,桌面版会停掉闲置很久的会话的进程(`[CliGovernor] pressure evicting … (idle 36301s)`),会话再被使用时重新启动进程(`Resuming session`、`spawn`),这时 `session.start` 会再运行。
+- **自带浏览器的工具**(`Claude_Browser`,桌面版给每个 Code 会话的,不是公开给插件的接口;终端和 VS Code 里没有):`tabs_context` 返回 JSON 加一行文字,列出这个会话的标签页和各自的来源(面板是 `http://localhost:<引擎端口>`);`preview_start` 每调用一次就多开一个标签页,所以打开前要先看有没有。
+- **会话的开始时间**:Codex 任务的会话记录第一条(`session_meta`)带时间,分叉的任务记着来源;Claude Code 的会话记录每条都有时间。
+- **多张图**:`cam_view_image` 一次可以取多张(`ids` 是一组编号,没有张数上限),说明里写着“几张就一次传几个”。
+
+## 4. 做法
+
+- **默认开启自动选图**(`selection.ts`、`thread-index.ts`、`claude-index.ts`):
+ - 引擎和插件服务启动时,在选择目录里写下这个版本第一次运行的时间(`auto-default.json`,只写一次,Codex 和 Claude 各一份)。没有这个文件时没有默认值(测试和旧版本都是这样)。
+ - 任务或会话没有自己的开关时:开始时间不早于那个时间,就当作开着,从开始时算起(`autoSince`);还没有会话记录的算新的。Codex 的分叉任务按它来源的任务算。默认值不写进文件,用户在面板里一改就写下来。
+ - 读选择的地方都经过它:引擎改写、是否走改写(Codex 的 WebSocket 改走 HTTP)、面板、取图工具。
+- **面板自动打开**(`hooks/register.js`):
+ - 只在桌面版(自带浏览器的工具能用)、请求经过引擎时。进程启动时、每轮开始和结束时检查:这个会话有图、面板没开、用户没在这个会话里关过,就用 `preview_start` 打开。所以模型读了图,那一轮结束时打开;用户贴了图,那一轮开始时打开。
+ - 谁关的,按时间分:桌面版只在一轮结束 30 分钟后才关。面板在这之前没了,是用户关的:记下来(插件自己的存储,按会话记,留最近 200 个),之后不再自动打开。停了 30 分钟以上,下一轮开始时面板不在,算桌面版关的,重新打开。为了认出用户在停顿里关掉的,一轮结束 25 分钟后再看一次。剩下认不出的只有停顿的第 25 到 30 分钟之间被用户关掉的,按桌面版关的算。
+ - 进程被桌面版停掉又重启的,从头算:用户关过的照样不开,其余的有图就打开。
+ - `/cam`:清掉这个记录,打开新标签页并关掉旧的,只留一个。`/clear`、`/resume` 后,开着的面板换到新会话。
+- **多张图**:不用改;补测(下面)。
+
+## 5. 测试
+
+- 单元测试:默认值的起始时间只写一次;新任务开、旧任务关、自己的开关优先、没有记录的算新的、分叉按来源算;Claude 会话同样(面板、改写、取图工具、改成关之后保持关);引擎启动写下起始时间。
+- Mod 测试(`claude plugin test`,用测试时钟):有图才打开、开着就不重开、贴图的那一轮开始就打开;停顿 30 分钟以上被桌面版关掉的,下一轮重新打开;用户关掉的(停顿前 25 分钟里关的也算)不再打开,进程重启后也是,`/cam` 重新打开并只留一个标签页;进程重启后有图就打开;不在桌面版或请求不经过引擎时不打开。
+- Codex 自测(`v03-auto-selftest.ts` 新加任务 D):不设开关,看新任务是否默认开着;一个问题同时需要两张没取过的图,看模型是否都取回、答对。
+- Claude 桌面版实测:新会话默认开着、多张图一起取回、面板自动打开和关掉后不再打开。
+
+## 6. 验收
+
+1. 升级之后新开的 Codex 任务和 Claude 会话默认开着自动选图;之前的保持原样;面板里改过的以用户的为准。
+2. 模型需要多张图时都取回(Codex 和 Claude 都测)。
+3. Claude 桌面版里有图的会话自动打开面板;被桌面版关掉的,会话再用时重新打开;用户关掉的不再自动打开,`/cam` 能再打开;只留一个面板标签页。
+4. 原有测试和 Codex 自测不退化。
+
+## 7. 不做
+
+- 让会话保持唤醒、改桌面版的闲置时间:那是桌面版的事,也费资源。
+- 终端和 VS Code 里自动打开面板:那里没有自带浏览器,照旧用 `/cam`。
diff --git a/hooks/register.js b/hooks/register.js
index a3d10a9..1c2dfe0 100644
--- a/hooks/register.js
+++ b/hooks/register.js
@@ -14,11 +14,13 @@
// Also: the model's cam_view_image call gets this session's id (the service may outlive a /clear or /resume), and needs
// no permission prompt: it is the plugin's own read-only tool, approved as in Codex (default_tools_approval_mode
// "approve" there; user 2026-10-10 here). A deny or ask from the user's own hooks or settings still stands.
-// v0.6 (user 2026-10-13): in Claude Desktop the page opens by itself once the session has images, without /cam.
-// Claude Desktop stops an idle session's process after 30 minutes and closes the browser tabs it opened (its log:
-// "WarmLifecycle … Idle timeout reached"); the process starts again when the session is used, and the page opens
-// again then. Closed by the user, it stays closed in that session until /cam: a turn that ends without the tab, in the
-// process that had it open, means the user closed it, kept in the plugin's store for the session.
+// v0.6 (user 2026-10-13): in Claude Desktop the page opens by itself once the session has images, without /cam, and
+// again after Claude Desktop closed it; closed by the user, it stays closed in that session until /cam. Claude Desktop
+// closes a session's browser preview, the page's tab with it, after 30 minutes without a turn (its log: "WarmLifecycle
+// … Idle timeout reached"), while the Claude Code process goes on; under memory pressure it also stops a long-idle
+// session's process and starts it again when the session is used ("CliGovernor … evicting"). So a tab gone before
+// that limit was closed by the user, kept in the plugin's store for the session; one gone over a longer pause opens
+// again with the next turn.
// Input: mod events. Output: env changes for this process, a pane, requests to the engine on 127.0.0.1, the page in
// Claude Desktop's browser.
@@ -32,6 +34,9 @@ const FETCH_TOOL = 'mcp__plugin_codex-attachment-manager_codex_attachment_manage
// v0.6: the sessions where the user closed the page ($.store: session id → when), the newest kept.
const CLOSED = 'closedPanels'
const CLOSED_KEPT = 200
+// v0.6: Claude Desktop's idle limit for a session's browser preview, and the check made a little before it.
+const DESKTOP_IDLE_MS = 30 * 60 * 1000
+const PAUSE_CHECK_MS = 25 * 60 * 1000
const WORDS = {
zh: {
@@ -76,8 +81,13 @@ let inBrowser = false
let overview = null
let paneOpened = false
let copied = false
-// v0.6: the session whose page this process had open in Claude's browser (null: none).
+// v0.6: the session whose page this process had open in Claude's browser (null: none); when the last turn ended
+// (Claude Desktop's idle timer starts then; null while a turn runs); the check before its limit; the page's work,
+// one step at a time (two checks at once could open two tabs).
let pageFor = null
+let quietSince = null
+let pauseCheck = null
+let pageWork = Promise.resolve()
// The engine's panel API answers in JSON; the header marks a request as the panel's (a browser page cannot send it).
async function api($, path) {
@@ -205,34 +215,45 @@ async function markClosed($, sessionId, isClosed) {
await $.store.set(CLOSED, Object.fromEntries(newest))
}
+// The page's work, after whatever is under way.
+function pageStep(work) {
+ const step = pageWork.then(work)
+ pageWork = step.catch(() => {})
+ return step
+}
+
// /cam: the user asked, so a close before no longer counts. One tab of the page: the new one replaces any open.
async function openPanel($) {
routingState = (await readRouting($)) ?? routingState
const sessionId = await $.session.id()
- await markClosed($, sessionId, false).catch(() => {})
- const before = (await pageTabs($)) ?? []
- inBrowser = await openInBrowser($, panelUrl(sessionId))
- if (inBrowser) {
+ await pageStep(async () => {
+ await markClosed($, sessionId, false).catch(() => {})
+ const before = (await pageTabs($)) ?? []
+ inBrowser = await openInBrowser($, panelUrl(sessionId))
+ if (!inBrowser) return
pageFor = sessionId
for (const tab of before) await $.mcp.call('Claude_Browser', 'tabs_close', { tabId: tab.tabId }).catch(() => {})
- }
+ })
if (!inBrowser || !routingState?.active) await openPane($)
else await $.ui.invalidate('ui.render')
}
-// v0.6: in Claude Desktop, with the requests going through the engine, the page opens by itself in a session with
-// images, unless the user closed it there. Run when the process starts (also when Claude Desktop wakes the session),
-// when the desktop attaches, and after each turn.
-async function keepPage($) {
+// v0.6: in Claude Desktop (its browser tools answer), with the requests going through the engine, the page opens by
+// itself in a session with images, unless the user closed it there. `when`: the process started ('start'), a turn
+// starts ('turn', or 'after-pause' once Claude Desktop's idle limit has passed since the last one) or ended ('end'),
+// or the check before that limit ('pause').
+async function keepPage($, when) {
if (!routingState?.active) return
- if (!(await $.session.surfaces()).includes('desktop')) return
const sessionId = await $.session.id()
const open = await pageTabs($)
if (open === null) return
if (open.length) { pageFor = sessionId; return }
- // Open in this process, gone while it runs: the user closed it.
- if (pageFor === sessionId) { pageFor = null; await markClosed($, sessionId, true); return }
- if (await closedHere($, sessionId)) return
+ if (pageFor === sessionId) {
+ // Open in this process and gone: Claude Desktop closes it only after a longer pause; else the user closed it.
+ pageFor = null
+ if (when !== 'after-pause') { await markClosed($, sessionId, true); return }
+ }
+ if (when === 'pause' || (await closedHere($, sessionId))) return
const state = await api($, `/__cam/claude/panel?session=${encodeURIComponent(sessionId)}`).catch(() => null)
if (!state?.totals?.images) return
if (await openInBrowser($, panelUrl(sessionId))) pageFor = sessionId
@@ -242,12 +263,31 @@ async function keepPage($) {
async function followSession($) {
const sessionId = await $.session.id()
const open = await pageTabs($)
- if (!open?.length) { pageFor = null; return keepPage($) }
+ if (!open?.length) { pageFor = null; return keepPage($, 'start') }
await $.mcp.call('Claude_Browser', 'navigate', { url: panelUrl(sessionId), tabId: open[0].tabId })
pageFor = sessionId
}
-const keepPageLater = ($) => $.clock.after(0, () => { keepPage($).catch(() => {}) })
+// After the event that asked, so it is never held up.
+const pageLater = ($, work) => $.clock.after(0, () => { pageStep(work).catch(() => {}) })
+const keepPageLater = ($, when) => pageLater($, () => keepPage($, when))
+
+// v0.6: a turn starts: Claude Desktop may have closed the page during the pause before it.
+async function pageTurnStarts($) {
+ pauseCheck?.cancel()
+ pauseCheck = null
+ const paused = quietSince !== null && (await $.clock.now()) - quietSince >= DESKTOP_IDLE_MS
+ quietSince = null
+ keepPageLater($, paused ? 'after-pause' : 'turn')
+}
+
+// v0.6: a turn ended: Claude Desktop's idle timer starts; a close before its limit is the user's.
+async function pageTurnEnds($) {
+ quietSince = await $.clock.now()
+ pauseCheck?.cancel()
+ pauseCheck = $.clock.after(PAUSE_CHECK_MS, () => { pauseCheck = null; keepPageLater($, 'pause') })
+ keepPageLater($, 'end')
+}
async function loadOverview($) {
try {
@@ -310,14 +350,9 @@ export function register(on) {
// rest of the wait goes on in the background.
const done = await startRouting($, 12).catch(() => true)
if (!done) $.clock.after(0, () => { startRouting($).catch(() => {}) })
- // v0.6: the page, once routing is settled; when it is still waiting for the engine, after the first turn instead.
- else keepPageLater($)
- return next(e)
- })
-
- // v0.6: the desktop connects after the process started (a session woken up, or opened again).
- on('session.attach', { surface: 'desktop' }, async ($, e, next) => {
- keepPageLater($)
+ // v0.6: the page, once routing is settled (a process Claude Desktop started again included); when it is still
+ // waiting for the engine, with the first turn instead.
+ else keepPageLater($, 'start')
return next(e)
})
@@ -326,14 +361,20 @@ export function register(on) {
on('classic.SessionStart', { source: ['clear', 'resume', 'fork'] }, async ($, e, next) => {
if (routingState) await $.state.set(ROUTING, routingState).catch(() => {})
reloadOverview($)
- $.clock.after(0, () => { followSession($).catch(() => {}) })
+ pageLater($, () => followSession($))
+ return next(e)
+ })
+
+ // v0.6: the page is a convenience: whatever happens to it, the turn goes on.
+ on('turn.start', async ($, e, next) => {
+ await pageTurnStarts($).catch(() => {})
return next(e)
})
on('turn.complete', async ($, e, next) => {
const result = await next(e)
reloadOverview($)
- keepPageLater($)
+ await pageTurnEnds($).catch(() => {})
return result
})