Skip to content

fix(idle-compact): 闲置计时只从真实对话轮之后起算,冷启动/重连不再误压缩 - #3247

Merged
deepcoldy merged 1 commit into
deepcoldy:masterfrom
Wait845:fix/idle-compact-stretch
Nov 9, 2026
Merged

deepcoldy merged 1 commit into
deepcoldy:masterfrom
Wait845:fix/idle-compact-stretch

Conversation

@Wait845

@Wait845 Wait845 commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

问题

闲置自动压缩(#2682)用屏幕「忙→闲」边沿作为闲置计时起点。但 CLI 刚启动或刚重连时第一次报空闲,也会被当成一段新的闲置:

  • daemon 重启重连已存活的 CLI
  • 挂起后冷恢复
  • 在网页终端 / Dashboard 打开会话冷启动
  • CLI 在同一 worker 内崩溃重启、/restart、改模型重启
  • claude --resume 启动大会话(>15s)时,屏幕先闪「工作中」再变空闲

结果:一个已经闲置两天的会话,碰上其中一种,55 分钟后就被压缩并报「会话已闲置 55 分钟,已自动 /compact」。而此时 Anthropic 的提示缓存(1h)早已过期,压缩反而要无缓存冷读整段上下文——正是这个功能想省掉的开销。原测试里还有一条把「挂起后打开会话、55 分钟后压缩」当成正确行为写死了。

改动

把判据从「屏幕忙→闲」改成「只有 botmux 发起的对话轮(唯一能证明有请求打到模型、缓存是热的)之后的空闲,才开新的热区间」:

  • noteIdleAutoCompactTurnStarted 记一个带时间戳的 arm({ at, busy });看到该轮的忙碌期后,紧接着的空闲边沿把区间起点定在空闲时刻
  • arm 在忙碌边沿持久化到会话行(setting.armed),daemon 在对话轮进行中重启后仍能开区间;但此时按「轮开始时间」起算(请求不会早于它,区间只会更旧、不会更热),停机数天后自然落到过期分支、直接跳过
  • 其它忙→闲边沿(启动转圈、本地斜杠命令、终端直接输入)沿用已有区间;没有区间即为冷,永不压缩
  • 自身的 /compact(auto-compact:idle: 前缀)不开区间;用户 Ctrl-C 掉它之后不会 55 分钟后再压一次
  • 10 分钟内从未进入忙碌的 arm 作废(轮被拒、从未投递);卡在用量上限数小时后再变空闲也不算热
  • 本地应答、不打模型的斜杠命令(/model、/cost、/context 等白名单)在 deliverPassthroughToExistingSession 里不开区间;/review、/compact、技能等会打模型的照常计时
  • 挂起(clearIdleAutoCompactStretch)一并清掉热区间、arm 与 inFlight
  • 取舍:在网页终端里直接输入不再开区间,这类场景会漏压缩(只损失一次省钱机会,不会花钱)

影响面

仅 core/idle-auto-compact.ts 的闲置压缩判定,经 worker-pool.ts 的 stampIdleSinceAt、daemon.ts 的 beginNewTurn / passthrough 路径触达。不改其它 CLI / 后端 / 会话类型的行为;该功能本就只对 Claude Code 与 tmux 上的 Codex 生效。

验证

  • bunx tsc --noEmit:通过
  • bun run build:通过
  • 相关 17 个测试文件、312 条测试通过(idle-auto-compact 及其 store/terminal、command-idlecompact、prompt-cache、idle-worker-sweeper、idle-suspend、worker-pool、test-hygiene)
  • 新增回归用例:冷启动先「工作中」再空闲不压缩、闲置两天 daemon 重启不压缩、停机数天 mid-turn 恢复按轮起算、卡用量上限后恢复不算热、轮被拒不warm后续闪动、Ctrl-C 后不再压、本地命令白名单
  • 变异验证:逐条摘掉「只认对话轮」「arm 持久化」「按轮起算 vs 按空闲起算」「busy 宽限」「挂起清理」「重启载入 arm」「re-affirm 保留 arm」等守卫,对应测试均变红

提交前独立对抗复核

按仓库规范(持久状态语义变更)在提交前做了两轮独立对抗复核,用真实模块 + 假时钟复现:

  • 第一轮发现原「仅凭 CLI 刚连上判冷」的三个旁路(working 先于首个 idle、同 worker 内崩溃重启 prev 仍为 idle、crash 重启在热区间中途新开区间可能越过缓存窗压缩),以及两个漏压缩/重复压缩场景——据此重做为「对话轮 arm」判据
  • 第二轮发现 arm 为布尔时可被长时间停机后的无关边沿消费(停机 mid-turn、卡用量上限数小时、轮被拒从未忙碌),以及若干会打模型却不开区间的真实入口——据此把 arm 改为带时间戳、加 10 分钟忙碌宽限、按轮起算、补 passthrough 白名单;复核确认 Ctrl-C 两种事件顺序、重启 mid-compact、挂起后冷恢复、开关切换等场景均已守住

🤖 Generated with Claude Code

原来闲置自动压缩以屏幕「忙→闲」边沿作为闲置起点。CLI 刚启动或重连
(daemon 重启重连、挂起后冷恢复、网页终端/Dashboard 打开、CLI 在 worker
内崩溃重启、--resume 启动先闪「工作中」)的第一次空闲也会开一段新区间,
于是闲置数天的会话 55 分钟后被压缩并报「闲置 55 分钟」——此时提示缓存早已
过期,压缩反而要冷读整段上下文。

改为:只有 botmux 发起的对话轮(beginNewTurn)能开新的热区间。轮开始记
arm(时间戳),看到它的忙碌期后,紧接着的空闲边沿开区间;arm 在忙碌边沿
持久化,daemon 中途重启后按轮开始时间起算(只会更旧,不会更热)。其它
忙→闲(启动转圈、本地斜杠命令、终端直接输入)沿用已有区间;没有区间即
为冷,不计时。自身 /compact 不开区间,Ctrl-C 后不会再压一次;10 分钟内
未进入忙碌的 arm 作废;本地应答的斜杠命令(/model、/cost 等)不开区间。

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-09T09:30:42.616456Z ad01f3d PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ad01f3dc73

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

const persisted = ds.session.idleAutoCompact?.idleSince;
return persisted !== undefined && persisted <= live ? persisted : live;
if (ds.idleSinceAt === undefined) return undefined;
return ds.idleAutoCompactWarmSince ?? ds.session.idleAutoCompact?.idleSince;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Discard unproven legacy idle stamps

An enabled row can contain idleSince written before this change, when any idle edge could create the marker. A cold CLI that boots or reconnects without a botmux request is indistinguishable from a turn-backed stretch here; if the legacy timestamp is past the configured threshold but under 60 minutes and the session is otherwise eligible, the sweep can send /compact against the cold context. Version or clear legacy timestamps unless they have turn proof.

AGENTS.md reference: AGENTS.md:L164-L168

Useful? React with 👍 / 👎.

if (turnId.startsWith(IDLE_AUTO_COMPACT_TURN_PREFIX)) return;
// Started while the CLI is already busy: no new busy edge will follow, the
// turn runs on in the same busy period.
ds.idleAutoCompactArm = { at: now, busy: BUSY_SCREEN_STATUSES.has(ds.lastScreenStatus ?? '') };

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Persist arms for turns started while already busy

For an enabled session, a turn admitted while the CLI is already working or analyzing gets an in-memory busy: true arm, but the persisted setting.armed is not updated here. Repeated same-status screen updates return before syncing it, so if the daemon restarts before the next idle edge, restore has no arm for this request (or only an older one), and the turn's warm stretch is lost or dated from the previous turn. Persist this arm when recording it for the no-new-busy-edge case.

AGENTS.md reference: AGENTS.md:L164-L168

Useful? React with 👍 / 👎.

Comment thread src/daemon.ts
// must not arm a warm idle-auto-compact stretch (core/idle-auto-compact.ts).
const idleCompactArm = ds.idleAutoCompactArm;
beginNewTurn(ds, commandContent, turnId);
if (isLocalCliCommand(cmd)) ds.idleAutoCompactArm = idleCompactArm;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Arm cold-start /goal requests

On Claude and Codex, /goal is explicitly a cold-start passthrough that starts goal work (src/adapters/cli/claude-code.ts:1701-1703, src/adapters/cli/codex.ts:604-610). The initial-session path stages it as pendingRawInput, which prompt_ready sends directly (src/core/worker-pool.ts:19247-19283) without calling beginNewTurn or this arm. If idle-compact is enabled during or after that first task, its idle edge leaves the warm start unset, so that model-backed stretch is never eligible until another botmux turn. Arm accepted initial raw turns too.

AGENTS.md reference: AGENTS.md:L146-L148

Useful? React with 👍 / 👎.

@deepcoldy
deepcoldy merged commit af0f83d into deepcoldy:master Nov 9, 2026
12 checks passed
@deepcoldy

Copy link
Copy Markdown
Owner

感谢贡献,已合并 🎉

这个改动把闲置自动压缩的计时判据收紧到「只有 botmux 对话轮之后的闲边沿才开热区间」,冷启动、重连、崩溃重启等无请求场景不再误压缩,方向和测试覆盖都很扎实。

评审中确认的已知边界(失败方向均为少压缩一次或一次性过渡,不丢消息、不重复压缩)已汇总到 follow-up issue #3259,包括:

  • 同一 worker 内 CLI 进程退出后热状态不重置(崩溃原地重启等路径)
  • 忙碌中开始的轮 arm 不落盘、不经过 beginNewTurn 的请求不 arm
  • arm 可能被轮中漏出的瞬时闲边沿消费的依赖
  • 旧版 idleSince 的一次性过渡,以及几处测试补强

欢迎继续参与,也欢迎在 #3259 下补充想法。

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown

@Wait845 @deepcoldy 合入 master 后的 Midscene E2E 失败:Feishu live Midscene 失败。

这次运行覆盖 c53afd0…af0f83d7f(7 个提交),同批合入的还有 #3244 #3236 #3243 #3240 #2843 #3246,失败不一定由本 PR 引起。

用例 跑道 状态 失败信息
Claude basic bot flow feishu-core-claude failed waitFor timeout: Based on the screenshot, the main content area displays a chat interface. There is a streaming card (green header) located above the "ACK-e2e-clause..." text bubble. This card contai…
OpenCode basic bot flow feishu-core-clis failed waitFor timeout: Based on the screenshot, I am examining the main content area which displays a conversation thread. There is a prominent card (streaming card) located above the text bubble containin…
Pi basic bot flow feishu-core-clis failed waitFor timeout: Based on the screenshot, I am examining the main content area which displays a conversation thread. There is a prominent card (streaming card) located above the text bubble containin…
OhMyPi basic bot flow feishu-core-clis failed waitFor timeout: The current screenshot shows the main content area displaying a conversation topic titled "e2e-ohmypi-1794226089094". The visible message in this topic is a text instruction from use…
Kimi basic bot flow feishu-core-clis failed waitFor timeout: Based on the screenshot, I am examining the main content area (right panel) which displays a conversation thread. There is a prominent card component located above the text bubble co…
Antigravity basic bot flow feishu-core-clis failed waitFor timeout: Based on the screenshot, I am examining the main content area (right panel) which displays a chat topic. There is a specific card from "Antigravity" located above the text bubble con…
Grok basic bot flow feishu-core-clis failed waitFor timeout: Based on the screenshot, I am examining the main content area (right panel) which displays a conversation thread. There is a prominent card titled "Grok Build • e2e-grok-179422803607…

@github-actions

Copy link
Copy Markdown

🚀 Released in v3.59.0

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