Skip to content

fix(tmux): 修复 Codex 和 Claude Code 会话定位及窗口跳转 - #373

Merged
wxtsky merged 11 commits into
wxtsky:mainfrom
cynicalight:fix/codex-cli-terminal-routing
Oct 22, 2026
Merged

wxtsky merged 11 commits into
wxtsky:mainfrom
cynicalight:fix/codex-cli-terminal-routing

Conversation

@cynicalight

@cynicalight cynicalight commented Oct 14, 2026 •

Copy link
Copy Markdown

这次修改主要修复了在 Ghostty + tmux 中使用 Codex 和 Claude Code 时,会话跳到错误窗口或找不到对应窗口的问题。

  • Codex CLI 会话有终端信息时,优先回到终端,避免误打开 Codex 桌面应用。
  • 多个 Codex 会话可能继承同一个 tmux pane 信息,点击不同会话却跳到同一个窗口。现在会检查正在运行的进程与 pane 的对应关系;共享后台进程的会话会结合标题和工作目录查找唯一匹配。无法确定位置时会提示,避免错误跳转。
  • 部分 Claude Code 会话缺少终端信息。现在会从对应的本地运行进程中补全 Ghostty/tmux 信息,并保留已有的 hook 信息。
  • 同时修复打开 App 时出现空白 Settings 窗口的问题:改为打开实际设置页,并提供常驻菜单栏入口。

验证情况:

  • 已在 macOS 上构建并运行 Release App,使用 Ghostty + tmux 进行了本机测试,当前版本在本机使用正常。
  • 相关自动化测试共 17 项,15 项通过,2 项需要本机进程数据的可选测试跳过,无失败。
  • Release 构建及应用签名校验通过。

共享后台进程的会话如果标题重复、无法唯一匹配,或者无法确定 tmux 客户端,目前会提示无法定位,不会猜测目标窗口。

@cynicalight

Copy link
Copy Markdown
Author

@codex review

@cynicalight cynicalight changed the title fix: 修复 Ghostty/tmux 中 Codex CLI 会话被优先跳转到桌面应用 fix(tmux): 修复 Codex 和 Claude Code 会话定位及窗口跳转 Oct 20, 2026
wxtsky and others added 9 commits October 22, 2026 11:46
…rified

The verified Codex path refused the jump with a modal alert whenever it
could not prove the pane: tmux outside the three known paths, no answer
from the server, the session's process gone or its PID reused, a
detached server, or several attached clients with the hook's client
gone. main switched panes in all of those cases.

Resolution is now three-way. A pane found through the process tree (or
the one live Codex pane showing the session's title) is selected. Only a
live process that runs outside every pane of the server, with no unique
title match, is refused: there the hook's pane is known to be another
session's. Anything that can't be checked falls back to the hook's pane,
like every other source.

The client to switch is one already showing the pane's session (the
hook's client first), else the hook's client, else the only client; with
none of those tmux picks, as switch-client without -c always did.

The isolated-server test now uses a socket path it owns and removes the
socket file tmux leaves behind after kill-server.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The alert was hard-coded English in an app that ships seven languages,
and its text ("keep the session open with a distinct title") no longer
fits most of the cases that used to reach it. It now only appears when
the hook's pane is known to be another session's and no single Codex
pane shows this session's title, and says exactly that, in all seven
tables. A modal alert can't run under XCTest, so tests skip it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Dropping "codex" from sourceToNativeAppBundleId stops a running Codex.app
from stealing a CLI session's jump, but ESP32FocusCoordinator reads the
same map for Buddy's focus button when the agent has no card at all, so
pressing the Codex button stopped raising Codex.app. Sessionless callers
now ask desktopAppBundleId(forSource:), which still knows Codex.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…fill

backfillClaudeTerminalMetadata runs from tryMonitorSession, which every
hook event and every discovery scan reaches. Its guard passes whenever
tmuxPane is nil, so for every Claude session outside tmux (most of them)
it re-read the process's whole environment on the main actor with each
event, forever, without anything new to find: an environment is fixed
at exec. The process whose environment was read is now remembered per
session, as hostHarnessProbes does for the harness probe (wxtsky#321), and
forgotten with the session.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Splits the settings/menu-bar part of this PR out of the tmux fix: the
always-on MenuBarExtra replaces the status item that only appears with
"Auto-hide When No Active Session" (wxtsky#39), which is a product change of
its own. AppDelegate.swift and CodeIslandApp.swift match main again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The app's only scene is Settings { EmptyView() }; the real settings are
SettingsWindowController's window. While that window is open the app is
a regular app with a menu bar, and the generated "Settings..." item (and
Cmd-,) opened the empty scene as a blank window. That command now opens
the real window. Reopening the running app (Finder, Spotlight, Dock)
shows the real settings too, with the reopen handler from cynicalight's
wxtsky#373, without the always-on menu bar item that PR also added.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
testLiveSavedSessionsWhenRequested and testLiveClaudeProcessesWhenRequested
only ran with CODEISLAND_TMUX_FIXTURE / CODEISLAND_CLAUDE_FIXTURE pointing
at a JSON dump of one machine's sessions. They hard-code the Homebrew tmux
path and Ghostty, read the real tmux server and process environments, and
skip everywhere else, so they test nothing in CI. The resolution logic
they exercised is covered by the injected-process-tree tests and the
isolated tmux server test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…us pane

When a Codex session's hook pane is known to be another session's and no
single Codex pane shows its title in its folder, the jump showed a modal
NSAlert: CodeIsland took focus from the terminal and the panel was blocked
until it was dismissed. It now brings the session's terminal app forward
at app level (no tab, window or tmux pane picked, CodeIsland stays in the
background), and leaves tmux as it is. The alert's two strings leave all
seven L10n tables.

The jump now asks CodexTmuxRouting.resolution(for:tmuxPath:), which only
second-guesses Codex sessions in a tmux pane, so the visibility check can
share it next.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
TerminalVisibilityDetector checked the hook's tmux pane, which for a
Codex session in a shared background process is the pane that process
started in: Smart Suppress and follow-up reminders could treat one
session's pane as another's. Its tab-level check now asks the same
CodexTmuxRouting.resolution(for:tmuxPath:) as the jump: the located pane
is the one checked, and an ambiguous session is never visible.

Jump validation (auto-collapse after a jump) was "tab visible or terminal
frontmost", so a jump that could only raise the terminal would still
count as landed. It now goes through didJumpLand: the session's tab
showing, or its terminal in front only when the session is locatable.
That is a new Probe member, isSessionLocatable, defaulting to true, so
test probes keep answering as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@wxtsky
wxtsky merged commit b31282b into wxtsky:main Oct 22, 2026
@wxtsky

wxtsky commented Oct 22, 2026

Copy link
Copy Markdown
Owner

感谢 @cynicalight!用进程树定位 pane、共享后台进程时用标题 + 目录兜底、无法确定时不乱跳,这个思路很好,隔离 tmux server 的测试也写得很用心。审查后我们在你的分支上补了几个提交,已合并为 b31282b,会随下个版本发布。

tmux 定位

  1. 验证失败时回退(7f6ed9c):原实现只要验证不了就弹窗、不跳转,但以下情况在 main 上都能正常跳:多个 tmux client 且 hook 记录的 client 已断开、session 已 detach、tmux 不在三个常见路径、server 没有响应、Codex 进程已退出或 PID 被复用。现在只有确定 hook 给的 pane 属于别的会话时才不切换(进程不在任何 pane 里,且标题 + 目录找不到唯一的 pane);无法验证时照旧使用 hook 的 pane。切换时优先选已经显示该 session 的 client,否则交给 tmux 自己选(从 tmux 外部执行不带 -c 的 switch-client -t 可以正常工作)。同一提交还修了两处:隔离 tmux 测试每次运行都会在 /tmp/tmux-501 留下 socket 文件,现在改用测试自己的 -S 路径并在结束时删除;pane 标题字段挪到最后,避免标题里有 tab 时整行被丢弃。
  2. 去掉模态弹窗(cd2d2e6):真正有歧义时,只把会话所在的终端 app 提到前台(应用级:不选 tab/窗口,也不切换 tmux 的 client/window/pane),CodeIsland 自己不抢焦点。弹窗文案也从 7 个语言表里删掉了。
  3. 跳转和可见性检测用同一个结果(532c10b):目标 pane 只由 CodexTmuxRouting.resolution(for:tmuxPath:) 决定。TerminalVisibilityDetector(智能抑制、跟进提醒)现在检查的是跳转实际定位到的 pane,而不是 hook 继承来的 pane;有歧义时视为不可见。"跳转后自动收起"的成功校验在有歧义时,不会再因为终端在前台就判定成功。只有 Codex 会话会多执行一次 tmux 命令,其他会话的行为不变。

其他
4. Buddy(675a213):去掉 sourceToNativeAppBundleId["codex"] 之后,ESP32 Buddy 在没有 Codex 会话时按 Codex 键不会再唤起 Codex.app,为此加了 desktopAppBundleId(forSource:)。
5. Claude 补全只读一次(f53a9a6):tryMonitorSession 在每个 hook 事件和每次会话发现扫描时都会执行,不在 tmux 里的 Claude 会话会在主线程上反复读取进程环境变量。现在每个进程只读一次,做法和 hostHarnessProbes 一样。
6. 删除两个需要手动开启的本机测试(00d542b):CODEISLAND_TMUX_FIXTURE / CODEISLAND_CLAUDE_FIXTURE 依赖某一台机器的会话数据,写死了 Homebrew 的 tmux 路径和 Ghostty,还会读取真实的 tmux server 和进程环境。它们覆盖的逻辑已经由注入进程树的测试和隔离 tmux server 的测试覆盖。

设置窗口和菜单栏(303d52d、bab8232)
空白窗口的问题确实存在。app 唯一的 scene 是 Settings { EmptyView() },真正的设置窗口由 SettingsWindowController 打开;设置窗口打开时 app 会切换成普通 app 并显示菜单栏,这时系统自动生成的"设置..."(⌘,)会打开那个空的 SwiftUI Settings scene。现在用最小的改动修复:⌘, / "设置..." 和重新打开 app(Finder、Spotlight、Dock)都会打开真正的设置窗口,你写的 applicationShouldHandleReopen 也保留了。

常驻菜单栏图标这次没有保留。按 #39 的设计,菜单栏图标只在开启"无活跃会话时自动隐藏"后出现:那时灵动岛完全隐藏,图标是唯一的入口;其他时候灵动岛本身(面板里的设置按钮)就是入口,不需要再多占一个菜单栏位置,带刘海的 MacBook 右侧菜单栏空间本来就紧张。如果以后要做常驻图标,更适合做成一个设置项,并复用现有的 StatusItemController,而不是再加一个 MenuBarExtra。

全量测试已通过(App 1313 项、Core 889 项)。

还有两个问题想请教:

  • "共享后台进程"具体是什么用法(Codex 版本、怎么启动的)?进程树定位要求这个进程不在任何 pane 里(你的 trace 里它挂在 launchd 下:24422 → 1)。如果它是某个 pane 的子进程,所有会话都会被定位到那个 pane。
  • 通过 npm 安装的 Codex,pane 的 pane_current_command 是 node 而不是 codex,标题 + 目录的兜底匹配不到,只能把终端提到前台。你那边用的是哪种安装方式?有没有更可靠的方式识别 Codex 的 pane?

再次感谢!

wxtsky added a commit that referenced this pull request Oct 22, 2026
…jumps land on each session's pane (#373)

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

Copy link
Copy Markdown
Author

抱歉,“共享后台进程”这个说法不够准确。我们当时看到多个 Codex session 被记录成同一个 PID 和 pane,就推测它们共用了后台进程,但没有验证具体原因,所以目前无法提供对应的版本和启动方式来复现这个推测。

我自己的 Codex 不是通过 npm 安装的,本机测试能正常匹配。之前没有考虑到 npm 安装后显示为 node 的情况,这部分遗漏了。目前还没有验证过更可靠的识别方法。

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