Skip to content

fix(backend): 修复 tmux pipe 后端生命周期 - #12

Merged
deepcoldy merged 3 commits into
deepcoldy:masterfrom
LikaiLee:bugfix/20260517_tmux_pipe_backend_lifecycle
May 17, 2026
Merged

deepcoldy merged 3 commits into
deepcoldy:masterfrom
LikaiLee:bugfix/20260517_tmux_pipe_backend_lifecycle

Conversation

@LikaiLee

@LikaiLee LikaiLee commented May 17, 2026 •

Copy link
Copy Markdown
Contributor

背景

安装 tmux 后,botmux 会从原来的 PTY backend 自动切到 tmux backend。旧实现用 tmux attach-session / attached tmux new-session 作为 worker 的观察层:

worker -> node-pty -> tmux attach-session/new-session -> tmux pane -> Claude/Codex

这导致两个问题:

  1. viewer 退出被误判为 CLI 崩溃

    • tmux attach-session 在 daemon/node-pty 环境中可能因为 open terminal failed: not a terminal / lost tty 退出。
    • 退出的是 tmux viewer/client,不是 pane 里的 Claude/Codex。
    • worker 统一把 backend exit 上报为 claude_exit,daemon 触发 auto-restart,连续多次后出现:
      Claude crashed 4 times in 1 min, not auto-restarting。
  2. 真实 CLI 生命周期和观察层生命周期混在一起

    • PTY 模式下 backend 进程就是 CLI,onExit 等价于 CLI 退出。
    • tmux 模式下 backend 进程只是 tmux client,真实 CLI 运行在 tmux pane 中。
    • 需要把“观察层退出”和“pane/CLI 真实退出”分开处理。

修改思路

把 botmux 自有 tmux session 也改成 pipe-pane 模型:

worker -> tmux pipe-pane/capture-pane/send-keys -> tmux pane -> Claude/Codex

这样 worker 不再依赖 tmux attach-session 这个 viewer 进程:

  • 新建 session:使用 detached tmux new-session -d 启动 CLI,再通过 pipe-pane 观察。
  • 恢复已有 session:直接通过 pipe-pane 观察已有 bmx-* session。
  • 写入:继续用 tmux send-keys / paste-buffer。
  • 初始画面:reattach 时用 capture-pane 初始化 Web 终端/卡片,不再依赖 attach client 输出。

主要改动

  • 新增 session-backend-selector,集中选择 backend:
    • tmux disabled / 探测失败:PtyBackend
    • tmux session 已存在:owned TmuxPipeBackend reattach
    • tmux session 不存在:managed TmuxPipeBackend 创建 detached session
  • 扩展 TmuxPipeBackend:
    • 支持创建 botmux-owned detached bmx-* session
    • 主动 kill() / destroySession() 时只停止 observer,不向 daemon 上报 claude_exit
    • 真实 pane 消失时才触发 onExit
    • watcher 使用 #{pane_id},把异常或空输出都视为 pane gone,覆盖 display-message status=0 但 stdout 为空的 tmux 行为
    • owned session 支持 resize-window
    • 新建 owned session 后应用原 tmux options:status off、mouse on、set-clipboard on、history-limit 50000、window-size largest
  • 调整 worker:
    • 用 selector 初始化 backend
    • 只在 reattach pipe mode 下执行 captureCurrentScreen()
    • capture 放在 idle detector 注册之后,避免静止 prompt 绕过 idle detector 导致 pending 消息不 flush
  • 补充回归测试覆盖:
    • existing tmux session 走 owned pipe backend
    • new tmux session 走 managed detached pipe backend
    • lifecycle watcher 对 pane missing / 空 pane id 触发 exit
    • 主动 kill 不触发 onExit
    • owned resize 与 adopt resize 行为区分
    • 初始 capture 顺序在 idle detector 注册之后
    • selector 不再暴露已无用的 tmuxBackend 兼容字段

为什么这样改

直接修补 tmux attach-session 的退出判断只能避免误报,但仍然让 botmux 依赖一个不稳定的 viewer 进程。pipe-pane 更符合 daemon 场景:botmux 后台观察/写入 tmux pane,而不是伪装成一个交互式终端用户 attach 进去。

这次改动把生命周期语义重新对齐:

  • observer 主动停止:资源清理,不触发 restart
  • tmux pane 真实消失:上报 claude_exit,daemon 可以按现有逻辑 restart / crash guard
  • existing session:保留 /close kill bmx-* 的 ownership 语义
  • new session:保留 tmux backend 的跨 daemon 重启能力

Test plan

  • vitest run test/tmux-reattach-backend.test.ts test/tmux-pipe-backend.test.ts test/worker-pipe-initial-screen-order.test.ts test/tmux-copy-mode.test.ts test/session-backend-selector-shape.test.ts
  • tsc --noEmit
  • tsc && esbuild src/dashboard/web/app.ts --bundle --outfile=dist/dashboard-web/app.js --platform=browser --format=iife --minify

🤖 Generated with Claude Code

LikaiLee and others added 3 commits May 17, 2026 22:48
- 在 TmuxPipeBackend 类里显式声明 claudeJsonlPath / cliPid / cliCwd 三个可选字段,
  与 PtyBackend / TmuxBackend 对齐,避免 worker 用类型强转动态挂字段
- 把 private isReattach 改成 _isReattach + public get isReattach(),
  worker.ts 去掉 (backend as any).isReattach
- reattach 路径(ownsSession + _isReattach)也跑一次 applySessionOptions,
  补齐旧 bmx-* session 升级后缺失的 set-clipboard / window-size largest 等配置
@deepcoldy

Copy link
Copy Markdown
Owner

@LikaiLee 感谢这次扎实的修复!我和 Codex(在飞书话题里)一起 review 了这个 PR,结论是方向完全正确 —— tmux attach-session 作为 daemon 里的 viewer 本来就不健康,改成 pipe-pane 模型是后台观察的标准做法。把 observer 退出和真实 CLI 退出语义解耦是这次最有价值的改动。

我顺手 push 了一个 cleanup commit(bc98c0e)补三个小细节,都是零行为变化:

  1. TmuxPipeBackend 补 claudeJsonlPath / cliPid / cliCwd 字段声明 —— 之前 worker.ts 通过 (backend as TmuxBackend | PtyBackend).x = ... 强转挂字段,TS 编译过但类型层面是 lie。补上声明让类型系统也对得上。
  2. isReattach 改成 public getter —— 去掉 worker.ts 里的 (backend as any).isReattach,与 TmuxBackend.isReattach 对齐风格。
  3. reattach 路径也跑一次 applySessionOptions() —— 旧 TmuxBackend 在 new 和 reattach 时都会 backfill;新 pipe backend 之前只在 createSession 后跑,已有 bmx-* session 升级后会漏 set-clipboard / window-size largest 等。applySessionOptions 本身幂等 + try-catch 兜底,安全。

剩余几个建议给后续 follow-up(不阻塞这次合并):

  • fd 泄漏 · fs.openSync 拿的 fd 在 kill() / handlePaneExit() 里没显式 closeSync。pipe backend 变成正常 tmux 路径后影响面比 adopt 大,建议补一行 closeSync(fd)。
  • remain-on-exit on 时 lifecycle watcher 漏报 · Codex 在 tmux 3.3a 实测 pane_id 仍存在但 pane_dead=1,现在 watcher 不会触发。建议同时检查 #{pane_dead},并在 applySessionOptions 里设 remain-on-exit off 兜底。
  • owned resize-window 高频同步 · 拖窗触发时会同步 spawn tmux 命令,建议 debounce 或 execFile 异步。
  • lifecycle watcher 1s execSync 轮询 · 多 session 累积成本可观,后续可换 tmux hook 或异步 execFile。
  • captureCurrentScreen -S - · 长 session reattach 时可能瞬时推几 MB scrollback,后续可考虑截近段或字节预算 cap。
  • isTmuxMode && !isPipeMode · selector 落地后这条分支已经走不到,可清理或加注释标 legacy。
  • pipe-pane -O · 注释里说 -O 表示 "overwrite previous pipe",实际 -O 是输出方向;overwrite 是默认行为。纯注释问题。

完整的 review 讨论过程包括 Codex 在本地的 tmux 3.3a 复现实测(resize-window 在无 client 时实际生效、remain-on-exit 行为、fd 数监测 34→35→35)。再次感谢这个 PR ❤️

@deepcoldy
deepcoldy merged commit a7f52eb into deepcoldy:master May 17, 2026
sensuossss added a commit that referenced this pull request Jun 24, 2026
按多 agent review 逐条修(合入 master 前):

High
- #1 runtime: revisit 后 D#001/D#002 同 node.id 并发,inFlight/nodeControllers/
  nodeAbortCleanups 改按 dispatchKey(instance) 存取 + 守卫删除;cancel 按 instance
  abort。原先无守卫 inFlight.delete(node.id) 会删掉活着的新实例 → 整 run 误崩。
- #2 host: architect 手搓 BotSnapshot 漏 disableCliBypass → 受限 bot 在 architect
  步骤悄悄提权。改用权威 botToSnapshot()。
- #3 architect: 复用 attempts/001 不清旧 manifest,watcher 见旧 manifest 直接
  finish('ok') → revise-dag 后修订不生效。dispatch 前 rmSync(attemptDir)。
- #4 test: 加 runWorkflow 集成测试,断言 restricted override 真的写进 worker 的
  req.botSnapshot.disableCliBypass(降权红线看门)。

Medium
- #5/#12 daemon-run: driveV3Run 顶部记 wasAlreadyTerminal,已终态短路,避免
  coalesce 重驱 / "/start" 重试重发 gate 卡与 done/failed 消息。
- #6 security: gate handler 的 waitId 未校验就 path-join → 加 isValidWaitId
  (含 # 给 A#001-gate 形态) 拦 ../ 逃逸。
- #7 dashboard: catalog 前缀路由用 startsWith 误吞 runId 以 catalog 开头的 v3 run
  详情页 → 改带边界正则。(本就是合并时我引入的)
- #8 daemon: 线程内 "/workflow new" grill fall-through 时 cmdContent 仍是字面量,
  被待回答 ask 拦截器当答案吞掉 → ask 拦截加 !threadGrill。
- #9 test: 补 manifest 软链逃逸(symlink→外部)的拒绝用例,防 realpath 被简化成 resolve。

Nit
- #16 v3-blocked handler: String(formValue) 把 object/array 强转成非空垃圾过校验
  → 改 typeof==='string'。
- #17 v3.ts: 抽 isTerminalRunStatus(),统一 poll/loop 的终态停轮判定(消死代码)。

校验:pnpm build 通过;v3/daemon/dashboard 相关单测全绿(含新增 2 条)。
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