Skip to content

fix(host-runtime): keep non-Desktop callers off the local Host Runtime - #899

Merged
BytePioneer-AI merged 12 commits into
BytePioneer-AI:mainfrom
ump45nose:fix/non-desktop-caller-routing
Nov 17, 2026
Merged

BytePioneer-AI merged 12 commits into
BytePioneer-AI:mainfrom
ump45nose:fix/non-desktop-caller-routing

Conversation

@ump45nose

Copy link
Copy Markdown
Contributor

Summary

按 #766 的最后一条 review 意见,基于当前 main(4112ad65)重新收敛成独立的一支,只保留"谁能拥有本地 Host Runtime"这一部分,主干已经覆盖的内容全部剔除。

已删除(主干已覆盖)

保留(6 个文件,+472/−46)

  • crates/shim/src/desktop_invocation.rs:用官方 CLI 所在的、经过校验的 Desktop bundle 祖先判定 Shim 调用方,不再依赖 Launcher 可执行文件快照;Launcher 退出或 PID 被复用后,Computer Use 等 native helper 仍走官方 CLI。
  • crates/shim/src/local_runtime_lease.rs + lib.rs:admit_local_runtime 对"确认不是 Desktop"的调用方,在本地 Host Runtime 或 Mapping Store 已被其它存活进程持有时回退到官方 CLI,而不是让 Desktop 启动失败;Host Runtime 在身份登记前退出时报告子进程退出状态。
  • managed remote listener:拒绝泄漏的本地 Launcher 会话或缺失的 CODEXHOST_DATA_DIR,避免它锁住本地 Mapping Store。
  • docs/platforms/macos/macos-native-tools.md:说明新的调用方分类。

Related issues

Test plan

  • cargo fmt -p codexhost-shim --check:通过。
  • cargo clippy -p codexhost-shim --features test-utils --all-targets:无 warning / error。
  • cargo test -p codexhost-shim --features test-utils(macOS 15 arm64,rustc 1.97.1):
    • 单测 26 passed;
    • host_runtime_selection 5 passed;
    • proxy 48 passed / 1 ignored / 3 failed。失败的三个是 managed_remote_listener_detaches_and_reuses_a_matching_socket_owner、remote_lifecycle_refuses_a_mismatched_installed_command、remote_lifecycle_terminates_only_the_matching_socket_listener。同一份环境里在未修改的 main(4112ad65)上单独跑 --test proxy 也以同样的报错失败(remote Host socket owner does not match the requested installed listener,以及等待 <tmp>/ready 超时),因此判定为本机环境失败,与本分支无关;本分支新增的 managed_remote_listener_refuses_a_local_launcher_session_or_missing_data_directory 通过。
  • 未执行:完整 CI(Desktop / Renderer / TS 套件)与实机 Desktop 验证。本分支只改 crates/shim 与文档,未改 TypeScript / Renderer 代码。

(AI 辅助完成;以上命令在作者机器上实际执行。)

ump45nose and others added 10 commits November 7, 2026 22:02
A remote app-server listener started with a leaked local Launcher
session could lock the local Mapping Store. Codex Desktop then failed to
start its Host Runtime with "Another codexhost process owns Mapping
Store", but showed only the Shim's cleanup note "terminated official CLI
descendants after root exit". On macOS, Computer Use helpers kept
starting full Host Runtimes after the Launcher exited, failing on the
same lock about every ten minutes.

- Shim: classify macOS Desktop helpers by validated Desktop ancestry when
  Launcher metadata is stale (Launcher exited or its PID reused).
- Shim: a caller positively not started by Codex Desktop runs on the
  official CLI when a live Host Runtime owns the Mapping Store or another
  live Desktop owns the local Host Runtime lease. Desktop's main
  app-server keeps the explicit failure.
- Shim: refuse to start a managed remote listener inside a local Launcher
  session or without CODEXHOST_DATA_DIR, so it cannot take the local
  Mapping Store.
- Shim: label Host Runtime children as Host Runtime, and end a failed Host
  Runtime's stderr with its last diagnostic so Desktop shows the cause.
- Mapping Store: record the owner role in store.lock and name the owner's
  directory, PID, role and start time in STORE_LOCKED.
- Host Runtime: restate a Host initialization failure as the final
  diagnostic before exiting.

Signed-off-by: Vincent <[email protected]>
The summary unit test was gated to macOS and Linux, which left its import
unused on Windows and failed clippy with -D warnings. Build the failed
exit status per platform instead.

Signed-off-by: Vincent <[email protected]>
…ration

The fixture exited immediately, so under parallel load the Shim could
fail to inspect the already-exited child while publishing the lease and
report "cannot inspect PID" instead of the startup failure under test.
Give it a short startup time, as a real Host Runtime has.

Signed-off-by: Vincent <[email protected]>
…corded

When the child exits before the Shim records it in the local runtime
lease, the process snapshot fails and the proxy returned that error
without copying the child's stderr. Wait briefly for the exit and fall
through to the normal exit report instead, and drop the fixture delay
that hid this path.

Signed-off-by: Vincent <[email protected]>
Resolve proxy test conflicts by keeping this branch's CODEXHOST_DATA_DIR
fixtures together with main's CODEXHOST_REMOTE_EXECUTION_SESSION and
concurrent reconnect coverage.
Rebase the lock-coordination work onto current main and drop what main now
already covers (BytePioneer-AI#828 store ownership diagnostics, BytePioneer-AI#859 stale-lock takeover,
the managed listener's stale socket replacement and the Host Runtime failure
summary). What stays is the part that decides who may own the local Host
Runtime at all:

- classify a Shim caller by the validated Desktop ancestry of its official
  CLI instead of the Launcher executable snapshot, so Computer Use helpers
  keep working after the Launcher exits or its PID is reused;
- a caller positively known not to be Desktop falls back to the official CLI
  when another live process owns the Host Runtime or the Mapping Store,
  instead of failing Desktop startup;
- a managed remote listener refuses a leaked local Launcher session or a
  missing CODEXHOST_DATA_DIR, so it cannot lock the local Mapping Store;
- report the child's exit instead of a registration error when the Host
  Runtime exits before its identity is recorded.
@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • 功能改进
    • macOS 原生工具调用会根据启动来源和进程关系选择 Host Runtime 或官方 CLI;当其他 Desktop 或 Host Runtime 占用运行时,符合条件的调用将改用官方 CLI。
    • 远程 listener 启动会检查 Launcher 环境及数据目录配置,不符合要求时拒绝启动。
  • 错误提示
    • Host Runtime 启动失败时,Desktop 错误信息会包含退出码和最后一条 Host 诊断;官方 CLI 的错误输出保持不变。

Shim 更新 macOS Desktop helper 分类、本地 Host Runtime 所有权处理和托管远程 listener 启动校验。相关回归测试覆盖 Launcher 状态、运行时所有权冲突及 listener 环境缺失情况。

Changes: Shim 路由与运行时接入

Layer / File(s) Summary
Desktop 调用方分类
crates/shim/src/desktop_invocation.rs, crates/shim/tests/proxy.rs, docs/platforms/macos/macos-native-tools.md
Shim 根据已验证的 Desktop 安装和进程祖先链分类调用方。直接 Desktop 子进程保留 Host 路由,更深层后代作为 helper 处理。测试覆盖 Launcher 存活、退出和 PID 复用情况。
本地 Host Runtime 接入与回退
crates/shim/src/local_runtime_lease.rs, crates/shim/src/lib.rs, crates/shim/tests/proxy.rs, crates/shim/tests/remote_start_failure.rs, docs/platforms/macos/macos-native-tools.md
新增运行时接入判定和 Mapping Store 活动所有者查询。符合条件的非 Desktop 调用方在运行时已被占用时改用官方 CLI;Desktop app-server 保留所有权冲突错误。启动期间已退出的子进程不再按租约注册失败路径处理。
托管远程 listener 环境校验
crates/shim/src/lib.rs, crates/shim/tests/proxy.rs, docs/platforms/macos/macos-native-tools.md
托管远程 listener 在启动前拒绝 Launcher PID 已设置或 CODEXHOST_DATA_DIR 缺失、为空的环境。测试验证拒绝结果,并为既有 listener 测试设置数据目录。

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant Shim
  participant ProcessTree as Desktop进程祖先链
  participant MappingLock as MappingStore锁
  participant Lease as HostRuntime租约
  participant HostRuntime
  participant StockCLI as 官方CLI
  Shim->>ProcessTree: 确认已验证Desktop祖先和调用方
  Shim->>MappingLock: 查询活动Node所有者
  alt 检测到其他活动所有者
    Shim->>StockCLI: 改用官方CLI
  else 未检测到活动所有者
    Shim->>Lease: 尝试获取本地运行时租约
    Lease-->>Shim: 返回Owner或所有权冲突
    alt 获得租约
      Shim->>HostRuntime: 启动本地Host Runtime
    else 非Desktop调用方遇到其他Desktop持有租约
      Shim->>StockCLI: 改用官方CLI
    end
  end
Loading

Possibly related issues

Possibly related PRs

  • BytePioneer-AI/codex-host#766:该 PR 与本 PR 都涉及 macOS Desktop helper 分类、Host Runtime 租约冲突回退和托管远程 listener 环境校验。

Suggested reviewers: mouzhi, bytepioneer-ai

Merge Risk: Low · previous review 04884

The new fallback to the official CLI for non-Desktop callers covers the normal case. In a rare timing window, a non-Desktop caller could still stop a remote Host Runtime that starts at the same moment. This is a low, bounded risk and does not block merging.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed 标题指出主要变更:让非 Desktop 调用方不再占用本地 Host Runtime。变更集中,包括 crates/shim 中的调用方判定、本地 Host Runtime 租约回退到官方 CLI、托管远程 listener 的环境检查,以及相关文档和测试,与标题一致。标题使用 fix(host-runtime) 前缀,符合约定。
Description check Passed 描述说明了本 PR 从 #766 重新收敛的背景,列出已删除和保留的文件与行为,并给出测试结果和未执行的验证。描述与变更内容直接相关。
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 4112ad6 and 04884ed.

Files selected for processing: 6

  • crates/shim/src/desktop_invocation.rs
  • crates/shim/src/lib.rs
  • crates/shim/src/local_runtime_lease.rs
  • crates/shim/tests/proxy.rs
  • crates/shim/tests/remote_start_failure.rs
  • docs/platforms/macos/macos-native-tools.md

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Recent review details

Context from checks skipped due to timeout: 7

  • GitHub Check: CodeRabbit
  • GitHub Check: Check macos-26
  • GitHub Check: Check windows-latest
  • GitHub Check: Vitest
  • GitHub Check: Web Host tests
  • GitHub Check: Check ubuntu-22.04
  • GitHub Check: Check Linux ARM64
Additional context used

Code guidelines: 2

AGENTS.md — auto-discovered
CLAUDE.md — auto-discovered
Path-based instructions: 2

Source excerpt: Rust owns native launch, process management, update installation, and platform integration.

CodeRabbit inference engine (AGENTS.md)

Files:

  • crates/shim/src/desktop_invocation.rs
  • crates/shim/src/lib.rs
  • crates/shim/src/local_runtime_lease.rs
  • crates/shim/tests/proxy.rs
  • crates/shim/tests/remote_start_failure.rs
Source excerpt: For core logic or major feature changes, update the corresponding feature document in `docs/`, creating one only when needed.

CodeRabbit inference engine (AGENTS.md)

Files:

  • docs/platforms/macos/macos-native-tools.md

@BytePioneer-AI
BytePioneer-AI merged commit 6fb272c into BytePioneer-AI:main Nov 17, 2026
3 of 7 checks passed
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