Skip to content

fix(claude): 会话闲着时的后台压缩不再让卡片卡在「思考中」 - #388

Merged
wxtsky merged 2 commits into
wxtsky:mainfrom
Junyu06:fix/claude-idle-compaction-card
Oct 28, 2026
Merged

wxtsky merged 2 commits into
wxtsky:mainfrom
Junyu06:fix/claude-idle-compaction-card

Conversation

@Junyu06

@Junyu06 Junyu06 commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Claude Code 的会话做完、闲了大约 54 分钟后,卡片会变回「思考中」并一直停着,已经被空闲清理掉的卡片也会带着「思考中」重新出现。这是 Claude Code 在后台提前压缩上下文:它发出 PreCompact(trigger 是 auto)和执行压缩的 agent 的 SubagentStop,之后没有 Stop,卡片就回不到空闲。

改了什么

来自 Claude 的自动 PreCompact / PostCompact,如果卡片已经空闲或已经不在,就不改卡片,也不新建卡片。一轮进行中的压缩和手动 /compact 照旧显示「Compacting context…」。

验证

  • 新增 6 个测试,复现 bug 的 3 个改之前失败、改之后通过;全量 swift test 只有 main 上本来就失败的 CodexTmuxRoutingTests.testProbeAgainstIsolatedTmuxServer。
  • 在 app 里(CODEISLAND_ISOLATED=1)用 hook 重放这串事件:1.0.40 上,做完的卡片和已被清理的卡片都停在「思考中」;这个分支上都没有卡片进入「思考中」。

While a session sits idle, Claude Code precomputes its next compaction
(about 54 minutes after the turn ended; seen with Claude Code 2.1.308
and CodeIsland 1.0.40). It reports PreCompact with trigger "auto" and
the SubagentStop of the agent that ran it, then no Stop. CodeIsland read
the PreCompact as work: a finished card went to "thinking" for good, and
a card the idle sweep had removed came back as "thinking".

An automatic PreCompact/PostCompact from Claude on an idle card, or on
one that is gone, now changes nothing and opens no card
(isIdleBackgroundCompaction, checked in the reducer and in
AppState.handleEvent before it creates a card). A manual /compact and a
compaction mid-turn show as before.

The SubagentStop is handled as before: it still records the agent as
closed, so late permission requests from it get no card
(ApprovalTrustBoundaryTests). A card the sweep had removed therefore
comes back idle with that stop, until the idle timeout sweeps it again.
A card restored idle after CodeIsland restarts mid-turn no longer shows
"Compacting" for an automatic compaction; its next tool call sets it
running.

ClaudeIdleCompactionTests (reducer) and AppStateIdleCompactionTests
cover a finished card, a swept card, a compaction mid-turn and a manual
/compact.
…e too

A compaction Claude Code runs on a finished session fires more than its
PreCompact. The agent that writes the summary stops when it is done
(SubagentStop, under an id the card never saw start), and where Claude
Code applies the summary at once (its idle "compact" mode, rather than
keeping the summary for the user's return) the conversation then starts
over from it (SessionStart, source "compact"; read in Claude Code
2.1.312). With only the PreCompact dropped, the stop still brought back
a card the idle sweep had removed, idle for another full timeout, and
reset a finished card's idle clock; the restart rebuilt a finished card,
wiping its last reply, and rang the session-start sound where it is on.

AppState now asks BackgroundCompactionFilter first. Once a background
compaction has begun on a card, the hooks of agents the card does not
know, and the restart from the summary, change nothing while the card is
still finished or gone. The compaction ends with that restart, with
anything else from the session's main thread (the next prompt, above
all), or after ten minutes. Requests and questions never pass through
handleEvent, so nothing is answered for the user here.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@wxtsky
wxtsky merged commit 6bfcb46 into wxtsky:main Oct 28, 2026
wxtsky added a commit that referenced this pull request Oct 28, 2026
A card removed with "Remove idle cards" (#385) came back for work, and a
compaction starting counted as work, so Claude Code's background
compaction of the finished session (#388) passed the removed-card check
first: it spent the card's tombstone, and only then did the compaction
filter drop the event. The card stayed away, but the next discovery scan
brought it back. The filter now runs before the removed-card check, so
none of the compaction's hooks touch a removed card or its tombstone; a
/compact the user types still brings the card back.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
wxtsky added a commit that referenced this pull request Oct 28, 2026
… idle removal and more

Unreleased notes for this round: the island's 20 fps clock (#381, #391),
"Always allow" holding in its session and Codex rules Codex can use
(#380), shortcuts and pinned displays with a second monitor (#384), pi
and Oh My Pi ringing once a task (#386), the Claude Desktop answer and
background compaction fixes (#389, #388), removing idle cards (#385),
the Codex Desktop thread jump (#387), the DeepSeek whale (#390) and the
Grok / DeepSeek Harness sections in Group by CLI.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@wxtsky

wxtsky commented Oct 28, 2026

Copy link
Copy Markdown
Owner

感谢 @Junyu06!排查很细致。PreCompact/PostCompact 这部分的判断是对的,只限 Claude 也对:Codex 的 PreCompact 必带 turn_id,其他 agent 的压缩也都发生在一轮之内。

我在你的提交上面加了一个维护者提交(39e61d2),把这次后台压缩剩下的 hook 也收住了:

  • 写摘要的那个 agent 发出的 SubagentStop,会把已被空闲清理掉的卡片以空闲状态重新建出来,又要再挂一个超时周期;它还会刷新已完成卡片的 lastActivity,让卡片躲过空闲清理。默认 30 分钟超时下,到第 54 分钟时卡片通常已被清理,所以这是常见情况。
  • Claude Code 的空闲压缩还有一种当场应用摘要的模式,之后会再发一个 SessionStart(source 为 compact)。它会重建已完成的卡片、丢掉最后一条回复,开了"会话开始"提示音的话还会响一声。

现在后台压缩开始后,只要卡片仍是空闲的或已经不在,卡片不认识的 agent 发来的 hook 和这个 SessionStart 都不会改动任何东西。收到这个 SessionStart、主线程的其他事件(比如下一条提示),或者过了 10 分钟,这次压缩就算结束。原来断言"被清理的卡片会以空闲状态回来"的测试改成了断言不回来,另外补了只限 Claude 的测试。

另外,和同时合入的 #385(一键移除空闲卡片)一起用时,后台压缩也不会让用户手动移除的卡片回来,这部分在 main 上单独补了一个提交(5367be1)。已 squash 合并,会随下个版本发布。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants