Repository navigation
fix(bridge): Claude 换到新对话(/clear、/resume、/branch)后记下新会话 id,冷启动恢复不再接回清掉的上下文 - #2165
Merged
Merged
Conversation
…掉的上下文 Claude 2.1.305+ 在 /clear、会话内 /resume、/branch 时会立即改写 pid 状态文件里的 sessionId。此前 worker 只在飞书消息提交前后上报会话 id,/clear 之后只跑本地回合 (终端输入、后台任务唤醒)时 daemon 记录的仍是旧 id,下一次需要重新拉起 CLI 时 (pty 后端每次 daemon 重启、tmux pane 丢失)会 --resume 回清空前的对话。 bridge 每轮读 pid 状态文件时,若它与 bridge 正在读的 transcript 一致、id 与记录不同、 且该 transcript 已落盘,就把这个 id 记为恢复目标。只认 pid 状态记录,不认指纹 / mtime 兜底与被判为陈旧的记录。transcript 未落盘时不记(--resume 不存在的 id 会让 Claude 直接退出;--fork-session 启动的新 id 在首次写入前没有文件),记录保持原样。 Closes #2155 Co-Authored-By: Claude Opus 5.5 <[email protected]>
deepcoldy
requested review from
47seek,
LucasIcarus,
xiaoxueSunn and
xiongz-c
as code owners
October 22, 2026 14:17
- 记录门禁从「transcript 已存在」改为「transcript 含 user/assistant 记录」 (transcriptHoldsConversation,只读文件头 256KB,且仅在 id 与记录不同时才读)。 真机 2.1.308:--fork-session 的子 transcript 先只有几行元数据、随后才并入对话; 只含元数据的 transcript 被 --resume 会 "No conversation found" 退出。 - adopt 会话不记:botmux 不会冷启动 adopt 的 CLI,记下来只会改写 adopt 元数据。 - pid 跟随探测的注释补上 2.1.305+ 的行为。 - 假 Claude:fork 子文件先写元数据前导、首轮才并入对话;--resume 要求含对话记录。 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Owner
Author
|
已合并 → 17296ac(squash,subject 带
follow-up(都不阻断):
未做:发版、部署、重启 fleet(需另行指令)。 |
This was referenced Oct 22, 2026
deepcoldy
added a commit
to zsnjuts/botmux
that referenced
this pull request
Oct 22, 2026
按双审 G7 合前对齐:并入 33aef3d(含 deepcoldy#2094/deepcoldy#2089/deepcoldy#2084/deepcoldy#2165/deepcoldy#2157)。 与 PR 同区为 src/bot-registry.ts、src/daemon.ts,git 自动合并无冲突; src/im/lark/identity-cache.ts 沿用上一次合并的解法(完整保留 deepcoldy#2082 函数体, 仅 JSDoc 首行为新措辞)。最终树相对 33aef3d 恰好 20 个 PR 文件、零夹带: tsc 0、13 个同区测试文件 1132 项全绿、bun run build 绿。 Co-Authored-By: Claude Code <[email protected]>
|
🚀 Released in v3.50.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
改了什么
Claude 会话在
/clear(以及会话内/resume、/branch)换到新对话后,worker 把新的会话 id 记为恢复目标。之后需要重新拉起 CLI 时(pty 后端每次 daemon 重启、tmux pane 丢失、/restart),--resume接的是用户当前所在的对话,不再接回刚清掉的上下文。<配置目录>/sessions/<pid>.json)里的sessionId(本机 2.1.308 实测,见下方验证)。--resume能打开)时,经已有的cli_session_id通道上报,由 daemon 持久化。shouldRecordPidStateSessionId(src/services/bridge-rotation-policy.ts),读文件的检查transcriptHoldsConversation(src/services/claude-transcript.ts,只读文件头 256KB)最后才做、且只在 id 与记录不同时才做;worker 侧只有一个调用点。为什么
#2155:此前会话 id 只在飞书消息提交前后上报。
/clear之后只跑了本地回合(终端里输入、后台任务唤醒)就重新拉起 CLI 时,daemon 记录的还是/clear之前的 id,worker 会--resume回清空前的对话。bridge 跟着实际在写的文件走,回复不丢,但用户清掉的上下文又回来了。刻意不做的
--resume一个只有元数据(mode / permission-mode 等)或根本不存在的 transcript,Claude 会直接No conversation found退出。--fork-session启动的新会话会先命名新 id,文件随后出现、开头几行是元数据,对话内容再写入。此时记录保持原样,期间重启照旧恢复(或重新 fork)。/clear更新的老版本 Claude(2.1.123):行为不变。影响面
--resume <cliSessionId ?? sessionId>)。cliSessionId的地方(卡片上下文用量、「本地打开」、/fork的来源)会跟到/clear之后的对话。验证
真实 Claude 2.1.308(私有 tmux socket + 临时目录,做完已清理):
/clear/clear命令记录)/resume <id>/branchclaude --resume <id> --fork-session启动claude --resume <id>,transcript 内容No conversation found with session ID: …,exit 1No conversation found …,exit 1isMeta: true的 user 记录/clear命令记录构建与测试(本地只跑相关文件,全量交给 CI):
bun run build:通过。vitest run test/bridge-rotation-policy.test.ts test/claude-transcript.test.ts test/worker-claude-clear-restore.integration.test.ts test/worker-claude-cold-resume.integration.test.ts test/test-hygiene.test.ts:5 个文件,202 passed。bun test test/worker-claude-cold-resume.integration.test.ts test/bridge-rotation-policy.test.ts test/claude-transcript.test.ts:169 pass。变异(每处单独跑,跑完还原):
/clear(tmux、pty)、/resume、老记录 pane 还在、fork;/compact与老记录 pane 已丢两例本就应当不变,保持绿--resume直接No conversation foundtranscriptHoldsConversation把任意行当对话"type":"user")测试改动
test/worker-claude-cold-resume.integration.test.ts(真实 worker + 假 Claude,CLI 随 worker 一起丢掉后冷启动):/clear(tmux、pty 两种后端)、会话内/resume、/compact之后只跑本地回合:冷启动接的是 CLI 当前所在的对话,旧回复不重放;/clear之后的对话;--fork-session(新 id;子文件先只有元数据,首轮才并入对话)、--resume的 transcript 没有对话记录就退出。test/worker-claude-clear-restore.integration.test.ts的 harness 抽到test/helpers/claude-worker-harness.ts,两个文件共用(原有用例未改);新用例单独成文件,CI 里可以和原文件并行。harness 冷启动时同时关掉托管会话所在的botmuxtmux socket。test/bridge-rotation-policy.test.ts:判定函数的四种情形(含 id 未变时不读 transcript、adopt 不记);test/claude-transcript.test.ts:transcriptHoldsConversation对元数据前导、isMetauser、assistant、缺失文件、半行、嵌套type的判定。已知、不在本 PR 内
src/core/command-handler.ts的注释写着「a Claude adopt never sets cliSessionId」,但既有的飞书消息写入路径在 adopt 会话里本来就会上报并写入cliSessionId(adopt 后端带 CLI pid)。本 PR 不在 adopt 里新增写入;这条注释与既有行为的出入另行处理。defaulttmux socket,fix(tmux): 托管会话改用独立 tmux socket,测试连不到宿主 tmux server #2107 之后托管会话在botmuxsocket 上(本 PR 的 harness 两个都关)。Closes #2155
🤖 Generated with Claude Code