Skip to content

feat(worktree): 完善话题工作区生命周期 - #956

Merged
deepcoldy merged 85 commits into
deepcoldy:masterfrom
wzono:feat/topic-worktree-lifecycle
Sep 12, 2026
Merged

deepcoldy merged 85 commits into
deepcoldy:masterfrom
wzono:feat/topic-worktree-lifecycle

Conversation

@wzono

@wzono wzono commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

改动内容

新增的话题 / 工作区命令

命令 作用
/th [任务] 新建话题,并直接沿用当前会话目录。等价于 /t here [任务]。
/tw [任务] 新建话题,并强制为该话题创建或复用确定性的独立 Git worktree。等价于 /t worktree [任务]。
/repo here 在待选择项目的话题中,直接选择当前会话目录,不再展示项目选择卡片。
/close wt / /close worktree 关闭共享该 worktree 的相关会话,并在确认与最终复检后安全移除 worktree。
/cleanup-wt <ID> worktree 删除失败时,根据持久化任务 ID 在无原会话的情况下安全重试清理。

补充说明:

  • /t [任务] 与 /topic [任务] 原本已经存在;本 PR 为它们增加 here / worktree 两种模式及 /th / /tw 快捷别名。
  • /repo wt ... 原本已经存在,不属于本 PR 新增命令。
  • 本 PR 没有新增独立的 /here 或 /worktree 命令;here / worktree 是 /t、/repo、/close 的参数或模式。

话题 worktree 生命周期

  • 话题 worktree 使用稳定目标路径与分支,可在并发请求间安全创建或复用。
  • 创建锁覆盖目录发布与 worker admission,避免创建/复用与清理并发时误删正在接管的 worktree。
  • /tw 的待处理配置会持久化,daemon 重启后仍保留 force、路径、分支、复用策略和仓库相对子目录。
  • /close wt 使用跨 Bot 的严格活跃会话清单;无法证明安全时 fail closed。
  • 清理指纹覆盖工作区内容、暂存区、冲突状态及已初始化子模块,并在关闭会话后、删除前做最终复检。
  • 同团队 Bot 通过租户稳定的 union_id 授权;不把 app-scoped open_id 当作跨应用身份。
  • 删除失败会持久化清理任务,供 /cleanup-wt <ID> 重新校验后继续执行。

可靠性补充

  • 延迟启动、项目卡选择与 daemon 恢复路径会在投递本轮输入前准备 trigger-user CLI 身份。
  • PM2 只读 jlist 在输出较大时等待 stdout flush 后再退出,避免 JSON 被截断。
  • 早期加入的 passthrough reaction、子代理等待状态和 CLI busy 判定已全部从本 PR 移除并对齐主干。

为什么要改

  • 允许用户明确选择“沿用当前目录”或“为新话题隔离 worktree”。
  • 让话题 worktree 从创建、复用、重启恢复到关闭、失败重试形成可审计且 fail-closed 的完整生命周期。

影响范围

  • Lark 话题创建、项目选择卡片、会话恢复与 worktree 清理。
  • 公共 worker-pool 的延迟 raw input 凭据准备路径。
  • PM2 只读列表命令的输出可靠性。
  • 不包含 reaction / 执行状态语义变更,不改变其它 CLI 的输入协议。

验证

  • bun run build:通过。
  • worktree 生命周期相关 10 个定向测试文件:674 tests passed。
  • git diff --check origin/master...HEAD:通过。
  • 最新 head 的 GitHub CI:build、Node 测试分片与 Bun binary 构建已通过;bun-test 当前有一项编译 sandbox shim 临时目录探测失败,需后续重跑/确认。

@wzono
wzono force-pushed the feat/topic-worktree-lifecycle branch from d67c241 to 441c32b Compare August 20, 2026 10:06
@wzono
wzono force-pushed the feat/topic-worktree-lifecycle branch from 441c32b to a410421 Compare August 24, 2026 04:01
zhongweijie.x and others added 22 commits August 28, 2026 11:29
…-lifecycle

# Conflicts:
#	test/command-handler.test.ts
…-lifecycle

Co-authored-by: TRAE CLI <[email protected]>

# Conflicts:
#	test/pm2-existing-client.test.ts
@wzono wzono changed the title feat(worktree): 完善话题工作区生命周期与执行状态反馈 feat(worktree): 完善话题工作区生命周期 Sep 11, 2026

@deepcoldy deepcoldy left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

双审(Claude + pi)七轮收敛,0 阻断。worktree 生命周期(/here、/tw、/close wt fail-closed 回收、/cleanup-wt 重试)+ 延迟启动轮次凭据准备 + PM2 jlist flush;B1-B4 全部修复并经变异验证。合入最新 master(8946fef) 本地零冲突、tsc 0、定向 617 测试绿,CI 9/9。

@deepcoldy
deepcoldy merged commit 798943c into deepcoldy:master Sep 12, 2026
9 checks passed
deepcoldy added a commit that referenced this pull request Sep 12, 2026
外部作者 Ding-Ye(土豆有点)的 #1366。将飞书文档原生评论从「整篇文档共用一个
CLI 会话」改为「按 commentId 线程隔离」,解决同文档多条独立评论被并入同一 turn、
部分评论长期无正式回复的问题。

冲突解决(src/core/worker-pool.ts):保留 master 上 #1333 的
cliInstanceBinding adopt 守卫,同时采用本 PR 的 forkAdoptWorker
'accepted'|'rejected' 返回契约,二者并存,纯机械合并。
test/daemon-rename-route.test.ts 的 mock 工厂并集保留 #956 runAutoWorktreeCommit
与本 PR persistStreamCardState 两个键。

合并树已逐字节核对:14 个 PR 文件与基于当前 master 的本地验证树一致,
其余文件与 master 尖端一致;通过 tsc、相关单测与 bun run build。

Co-Authored-By: Claude Code <[email protected]>
@github-actions

Copy link
Copy Markdown

🚀 Released in v3.22.0

LucasIcarus added a commit that referenced this pull request Sep 14, 2026
主干 #956 新增的 findActiveSessionsByWorkingDirStrict 按旧协议把 legacy JSON
当作可读 store(解析行、坏行 fail closed)。本 PR 收口后跨进程读取面只认
.db、JSON 一律不解析,于是「另一个 bot 的 sessions-<appId>.json 仍在、.db
未建」在原实现里会被静默当成空 store——升级窗口内未重启的 bot 若在该
worktree 上仍有活动会话,删除 worktree 的安全清单会漏掉它。

改为:清单前先按 listUnmigratedAppIds 找出待迁移的 store;有待迁移时再按
仍存在于本机(bots.json / 在线 descriptor / 本进程 appId)的 bot 过滤,任一
命中即抛 SessionStoreUnmigratedError。「哪些 bot 仍然存在」在这里必须是结
论性的:knownBotAppIds 新增 strict 模式,bots.json 读不到 / 不是 JSON / 没有
bot 列表、或 descriptor 目录列不出时直接抛错(listOnlineDaemons 同步加
strict),不把「无法判定」折成「废弃数据」。已移出 bots.json 的残留 JSON
与本 PR 的既有裁定一致,视为废弃不阻塞。command-handler 的重试任务与
/close 的兄弟会话清单两处调用已有 try/catch,抛错会转成「无法完整读取同
worktree 会话清单,已取消删除」;删除锁内的最终清单沿用主干既有的 handler
级 catch(只记日志),两次清单之间状态未变时不会走到那里。

rebase 时删除了主干那条「legacy JSON 坏行 fail closed」用例(JSON 已不
再被读取),以「已知 bot 未迁移 fail closed / 未知 bot 残留忽略 / bots.json
不可读 fail closed / 无待迁移时不读 bots.json」四条用例替代。

Co-Authored-By: Claude Fable 5.1 <[email protected]>
LucasIcarus added a commit that referenced this pull request Sep 14, 2026
§3.3 原文只写了常规跨进程读「不把 unmigrated 的 bot 计入结果并提示」。
主干 #956 新增的 worktree 回收是破坏性路径,按同一规则跳过会漏掉未重启
bot 在该目录上的活动会话,故裁定为 fail closed,且「哪些 bot 仍然存在」
的判定在此处必须是结论性的(bots.json / descriptor 目录读不到即抛错)。

§3.8 补记:编译版二进制里 dashboard 进程读到的 current 是自身烘焙版本而
非磁盘版本,「运行中 daemon vs 磁盘」在 dashboard 侧按无法判定处理。

Co-Authored-By: Claude Fable 5.1 <[email protected]>
LucasIcarus added a commit that referenced this pull request Sep 14, 2026
rebase 到含 #1333 / #1348 / #956 的主干后,restore-zombie-close 的
「legacy-bound Codex session」三例与 daemon-rename-route 的「serializes pool
creation」一例仍用无参 init() 重置会话库,本 PR 已把 init(appId) 改为必填,
CI shard 1/3 因此红。按各文件既有的 appId 补参(app_test / APP),验收 grep
`sessionStore.init(\s*)` 在 test 下仍为 0。

Co-Authored-By: Claude Fable 5.1 <[email protected]>
LucasIcarus added a commit that referenced this pull request Sep 16, 2026
主干 #956 新增的 findActiveSessionsByWorkingDirStrict 按旧协议把 legacy JSON
当作可读 store(解析行、坏行 fail closed)。本 PR 收口后跨进程读取面只认
.db、JSON 一律不解析,于是「另一个 bot 的 sessions-<appId>.json 仍在、.db
未建」在原实现里会被静默当成空 store——升级窗口内未重启的 bot 若在该
worktree 上仍有活动会话,删除 worktree 的安全清单会漏掉它。

改为:清单前先按 listUnmigratedAppIds 找出待迁移的 store;有待迁移时再按
仍存在于本机(bots.json / 在线 descriptor / 本进程 appId)的 bot 过滤,任一
命中即抛 SessionStoreUnmigratedError。「哪些 bot 仍然存在」在这里必须是结
论性的:knownBotAppIds 新增 strict 模式,bots.json 读不到 / 不是 JSON / 没有
bot 列表、或 descriptor 目录列不出时直接抛错(listOnlineDaemons 同步加
strict),不把「无法判定」折成「废弃数据」。已移出 bots.json 的残留 JSON
与本 PR 的既有裁定一致,视为废弃不阻塞。command-handler 的重试任务与
/close 的兄弟会话清单两处调用已有 try/catch,抛错会转成「无法完整读取同
worktree 会话清单,已取消删除」;删除锁内的最终清单沿用主干既有的 handler
级 catch(只记日志),两次清单之间状态未变时不会走到那里。

rebase 时删除了主干那条「legacy JSON 坏行 fail closed」用例(JSON 已不
再被读取),以「已知 bot 未迁移 fail closed / 未知 bot 残留忽略 / bots.json
不可读 fail closed / 无待迁移时不读 bots.json」四条用例替代。

Co-Authored-By: Claude Fable 5.1 <[email protected]>
LucasIcarus added a commit that referenced this pull request Sep 16, 2026
§3.3 原文只写了常规跨进程读「不把 unmigrated 的 bot 计入结果并提示」。
主干 #956 新增的 worktree 回收是破坏性路径,按同一规则跳过会漏掉未重启
bot 在该目录上的活动会话,故裁定为 fail closed,且「哪些 bot 仍然存在」
的判定在此处必须是结论性的(bots.json / descriptor 目录读不到即抛错)。

§3.8 补记:编译版二进制里 dashboard 进程读到的 current 是自身烘焙版本而
非磁盘版本,「运行中 daemon vs 磁盘」在 dashboard 侧按无法判定处理。

Co-Authored-By: Claude Fable 5.1 <[email protected]>
LucasIcarus added a commit that referenced this pull request Sep 16, 2026
rebase 到含 #1333 / #1348 / #956 的主干后,restore-zombie-close 的
「legacy-bound Codex session」三例与 daemon-rename-route 的「serializes pool
creation」一例仍用无参 init() 重置会话库,本 PR 已把 init(appId) 改为必填,
CI shard 1/3 因此红。按各文件既有的 appId 补参(app_test / APP),验收 grep
`sessionStore.init(\s*)` 在 test 下仍为 0。

Co-Authored-By: Claude Fable 5.1 <[email protected]>
LucasIcarus added a commit that referenced this pull request Sep 29, 2026
主干 #956 新增的 findActiveSessionsByWorkingDirStrict 按旧协议把 legacy JSON
当作可读 store(解析行、坏行 fail closed)。本 PR 收口后跨进程读取面只认
.db、JSON 一律不解析,于是「另一个 bot 的 sessions-<appId>.json 仍在、.db
未建」在原实现里会被静默当成空 store——升级窗口内未重启的 bot 若在该
worktree 上仍有活动会话,删除 worktree 的安全清单会漏掉它。

改为:清单前先按 listUnmigratedAppIds 找出待迁移的 store;有待迁移时再按
仍存在于本机(bots.json / 在线 descriptor / 本进程 appId)的 bot 过滤,任一
命中即抛 SessionStoreUnmigratedError。「哪些 bot 仍然存在」在这里必须是结
论性的:knownBotAppIds 新增 strict 模式,bots.json 读不到 / 不是 JSON / 没有
bot 列表、或 descriptor 目录列不出时直接抛错(listOnlineDaemons 同步加
strict),不把「无法判定」折成「废弃数据」。已移出 bots.json 的残留 JSON
与本 PR 的既有裁定一致,视为废弃不阻塞。command-handler 的重试任务与
/close 的兄弟会话清单两处调用已有 try/catch,抛错会转成「无法完整读取同
worktree 会话清单,已取消删除」;删除锁内的最终清单沿用主干既有的 handler
级 catch(只记日志),两次清单之间状态未变时不会走到那里。

rebase 时删除了主干那条「legacy JSON 坏行 fail closed」用例(JSON 已不
再被读取),以「已知 bot 未迁移 fail closed / 未知 bot 残留忽略 / bots.json
不可读 fail closed / 无待迁移时不读 bots.json」四条用例替代。

Co-Authored-By: Claude Fable 5.1 <[email protected]>
LucasIcarus added a commit that referenced this pull request Sep 29, 2026
§3.3 原文只写了常规跨进程读「不把 unmigrated 的 bot 计入结果并提示」。
主干 #956 新增的 worktree 回收是破坏性路径,按同一规则跳过会漏掉未重启
bot 在该目录上的活动会话,故裁定为 fail closed,且「哪些 bot 仍然存在」
的判定在此处必须是结论性的(bots.json / descriptor 目录读不到即抛错)。

§3.8 补记:编译版二进制里 dashboard 进程读到的 current 是自身烘焙版本而
非磁盘版本,「运行中 daemon vs 磁盘」在 dashboard 侧按无法判定处理。

Co-Authored-By: Claude Fable 5.1 <[email protected]>
LucasIcarus added a commit that referenced this pull request Sep 29, 2026
rebase 到含 #1333 / #1348 / #956 的主干后,restore-zombie-close 的
「legacy-bound Codex session」三例与 daemon-rename-route 的「serializes pool
creation」一例仍用无参 init() 重置会话库,本 PR 已把 init(appId) 改为必填,
CI shard 1/3 因此红。按各文件既有的 appId 补参(app_test / APP),验收 grep
`sessionStore.init(\s*)` 在 test 下仍为 0。

Co-Authored-By: Claude Fable 5.1 <[email protected]>
LucasIcarus added a commit that referenced this pull request Sep 29, 2026
rebase 到含 #1333 / #1348 / #956 的主干后,restore-zombie-close 的
「legacy-bound Codex session」三例与 daemon-rename-route 的「serializes pool
creation」一例仍用无参 init() 重置会话库,本 PR 已把 init(appId) 改为必填,
CI shard 1/3 因此红。按各文件既有的 appId 补参(app_test / APP),验收 grep
`sessionStore.init(\s*)` 在 test 下仍为 0。

Co-Authored-By: Claude Fable 5.1 <[email protected]>
LucasIcarus added a commit that referenced this pull request Sep 29, 2026
rebase 到含 #1333 / #1348 / #956 的主干后,restore-zombie-close 的
「legacy-bound Codex session」三例与 daemon-rename-route 的「serializes pool
creation」一例仍用无参 init() 重置会话库,本 PR 已把 init(appId) 改为必填,
CI shard 1/3 因此红。按各文件既有的 appId 补参(app_test / APP),验收 grep
`sessionStore.init(\s*)` 在 test 下仍为 0。

Co-Authored-By: Claude Fable 5.1 <[email protected]>
LucasIcarus added a commit that referenced this pull request Sep 29, 2026
rebase 到含 #1333 / #1348 / #956 的主干后,restore-zombie-close 的
「legacy-bound Codex session」三例与 daemon-rename-route 的「serializes pool
creation」一例仍用无参 init() 重置会话库,本 PR 已把 init(appId) 改为必填,
CI shard 1/3 因此红。按各文件既有的 appId 补参(app_test / APP),验收 grep
`sessionStore.init(\s*)` 在 test 下仍为 0。

Co-Authored-By: Claude Fable 5.1 <[email protected]>
LucasIcarus added a commit that referenced this pull request Sep 30, 2026
主干 #956 把 `/th` `/tw` `/t here|worktree` 做成 daemon 入口里的两条正则预判(先改写成 `/t …`
再交给 parseTopicHeader),command-handler 里还留了一份同构的 parseForceTopicInvocation 只给测试
用;rebase 之后头部 `/repo wt` 与它相遇时只能沿用主干的静默优先级(生命周期目录优先、`/repo`
被丢掉)。本次按命令路由器的原则(解析只有一处、fail closed)收口:

- topic-header:`/th` `/tw` 成为分隔符别名,`/t` `/topic` 紧跟的裸 `here|worktree` 一词被吃成
  `header.lifecycle`;变体算「已写头部」(未知 /xxx fail closed)与「声明了规格」(thread 里回
  「只在新话题第一条生效」,不再当普通输入送进 CLI)
- topic-spec:`lifecycle` 透传;与 `/repo`(含 `/repo wt`、裸 `/repo`)同写 →
  `lifecycle_conflicts_repo`,与其它错误一并收齐;新增 zh/en 文案
- daemon:删掉两条正则与改写,`forceTopicMode = forceTopic.lifecycle ?? 'default'`;头部 wt 不再
  需要「forceTopicMode === 'default' 时才生效」的让位
- command-handler:删掉 parseForceTopicInvocation / ForceTopicMode(无生产调用方)
- schema:`/th` `/tw` 进 FORCE_TOPIC_COMMANDS(触发 API 里同为保留命令);docs-site 补一行说明

有意的行为变化(记入设计文档 §9/§16):`标题 /tw 干活` 现在是带标题的生命周期头部(主干里
正则锚在行首,不识别);chat 模式群里 `/tw` 与 `/t` 一样强制开新话题;`/tw /repo x` 由静默丢掉
`/repo` 改为拒绝。

验证:`bun run build`;topic-header / topic-spec / command-handler / slash-commands-doc-sync /
topic-directive-header / daemon-rename-route / command-schema / command-trigger* / event-dispatcher /
command-router-oracle-diff 全绿;daemon-rename-route 里 `/tw` 的 targetSubdir 用例在本机 master
基线上同样失败(macOS /var 与 /private/var 的 realpath 差异),非本次引入。

Claude-Session: https://claude.ai/code/session_01Vu96pufjDoyTRmKFqCzWGQ
deepcoldy pushed a commit that referenced this pull request Oct 13, 2026
stdout 接管道时 write() 是异步的,紧跟显式 process.exit() 会丢弃未排空的尾部(实测父进程恒定只收到约 146 KiB,与 payload 大小无关)。

- 新增 src/cli/stdout-flush.ts 的 writeAndFlush:以 write 完成回调为边界
- botmux ask --json:写完结果再 ack;写失败不 ack、exit 1,daemon 仍可回收回执
- botmux hook:directive 内嵌用户文字作答(无长度上限)时等写完再 exit 0
- sandbox relay:send 与 dispatch 两处统一 mirrorRelayResponseAndExit,stdout/stderr 均写完再镜像宿主退出码
- pm2 只读 jlist 同类修复已在 master 落地(#956),保留大注册表(>200KB)真实子进程回归钉住;另新增 2MiB 管道背压回归与 ask 50 万字符 comment 端到端回归

验证:tsc 与 bun run build 通过;相关 10 个测试文件 159 通过 / 3 跳过(node vitest 与 bun test 双运行时);反向变异(撤掉修复)下三个回归测试均变红。

Co-Authored-By: Claude Code <[email protected]>
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