Skip to content

fix(mapping-store): recover Windows stale locks after system PID reuse - #627

Merged
BytePioneer-AI merged 1 commit into
mainfrom
fix/612-windows-mapping-store-lock
Oct 28, 2026
Merged

BytePioneer-AI merged 1 commit into
mainfrom
fix/612-windows-mapping-store-lock

Conversation

@BytePioneer-AI

Copy link
Copy Markdown
Owner

问题与根因

Fixes #612

Windows 重启后,Mapping Store 残留锁记录的 Node PID 可能被 svchost.exe 等系统进程复用。普通权限下 Get-Process 的 Path / StartTime 均可能不可读;现有实现因此无法识别进程身份变化,持续返回 STORE_LOCKED,阻止 Host 启动。

此外,直接将 PowerShell DateTime 交给 Windows PowerShell 的 ConvertTo-Json,不能保证得到 Date.parse 可读取的 ISO 时间。

修改

  • 锁冲突时改用本机 Get-CimInstance Win32_Process 读取 ExecutablePath / CreationDate,利用创建时间识别路径不可读的系统进程 PID 复用。
  • 在 PowerShell 内将创建时间显式转换为 UTC round-trip ISO 字符串,避免 DateTime 默认 JSON 格式及本地化差异。
  • 进程身份查询限制为 5 秒;失败、超时、空或无效身份仍保留锁。
  • 保持既有路径匹配、5 秒创建时间容差、当前 PID 快捷检查和锁格式不变。不终止复用 PID 的进程;不将身份未知视为可删锁,也不以旧锁的 startedAt 代替缺失的 processStartedAt。
  • 无锁冲突时不运行 PowerShell。macOS/Linux 路径不变,无新增依赖。

基于 origin/main 的独立 worktree/分支,不包含原工作目录的未提交改动。

测试

  • 新增 21 项跨平台模拟 Windows 身份响应的测试,通过真实 MappingStore.initialize() / 磁盘锁验证:路径不可读但创建时间不同、同路径 PID 复用、真实持锁身份、时间容差、旧锁、查询失败/超时/无效输出、EPERM 与已退出进程等。
  • 核心回归测试在修改前失败并复现 STORE_LOCKED;修改后通过。
  • 新增 3 项仅在 Windows 执行的真实子进程测试,走实际 PowerShell/CIM 查询,验证同 PID/路径但创建时间不同的回收、活动持锁者保护和旧格式锁保护。由现有平台敏感测试筛选自动纳入 Windows CI。
  • 同步现有运行时文档说明查询与安全边界。

本地已执行(macOS arm64 / Node 24.18.0)

  • npx vitest run --config tests/vitest.config.js packages/mapping-store:92 passed,3 skipped(Windows 原生测试)。
  • npm run typecheck:通过;测试文件 lint 调整后另跑 npx tsc -p tests/tsconfig.json --noEmit --pretty false 通过。
  • 改动 TypeScript 文件的 ESLint、改动文件的 Prettier、node tools/check-boundaries.mjs、git diff --check:通过。

尚未验证

  • 本地没有 Windows 环境,3 项 Windows 原生测试待 CI 执行;模拟测试不代表已验证 Windows 命令。
  • 原报告中“普通权限 + 重启后 PID 被受限 svchost 复用”的完整实机场景仍需报告者验证候选版本;CI 的普通 Node 子进程测试不等价于该权限场景。
  • 未运行全量测试或 Rust 检查:本次仅修改 Mapping Store Windows 身份查询。

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T03:56:22.696721Z a85d660 PR opened
🔒 Security Review ✅ Completed 2026-10-27T03:56:33.624237Z a85d660 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 671b1de and a85d660.

Files selected for processing: 4
  • docs/architecture/harness-plugin-runtime.md
  • packages/mapping-store/src/mapping-store.ts
  • packages/mapping-store/test/windows-lock-native.test.ts
  • packages/mapping-store/test/windows-lock.test.ts

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

Recent review details
Context from checks skipped due to timeout: 4
  • GitHub Check: Check macos-14
  • GitHub Check: Check ubuntu-22.04
  • GitHub Check: Check windows-latest
  • GitHub Check: Check Linux ARM64
Additional context used
Code guidelines: 1
AGENTS.md — auto-discovered
Path-based instructions: 1
Source excerpt: `codexhost` runs external Agent Harnesses as independent Threads inside Codex Desktop, preserving the official shell and native Codex path.

CodeRabbit inference engine (AGENTS.md)

Files:

  • packages/mapping-store/src/mapping-store.ts
  • docs/architecture/harness-plugin-runtime.md
  • packages/mapping-store/test/windows-lock-native.test.ts
  • packages/mapping-store/test/windows-lock.test.ts
Learnings: 1
Common learnings
Learnt from: CR
Repo: BytePioneer-AI/codex-host PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-10-23T15:37:32.051Z
Learning: Applies to ** : Source excerpt:
# Repository Guidelines

## Testing & Completion

- Small, low-risk changes do not require tests. For high-risk or cross-package changes, or when explicitly requested, add focused tests for changed behavior and boundary conditions; do not run full test suites by default.
ast-grep (0.45.3)
packages/mapping-store/test/windows-lock-native.test.ts

[warning] 1-1: Importing child_process exposes a command-execution surface; ensure any command/argument built from input is validated, and prefer execFile/spawn with an argument array over exec.
Context: import { spawn, type ChildProcess } from "node:child_process";
Note: [CWE-78] Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection').

(detect-child-process-typescript)

packages/mapping-store/test/windows-lock.test.ts

[warning] 1-1: Importing child_process exposes a command-execution surface; ensure any command/argument built from input is validated, and prefer execFile/spawn with an argument array over exec.
Context: import { execFileSync } from "node:child_process";
Note: [CWE-78] Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection').

(detect-child-process-typescript)

Additional comments: 4
packages/mapping-store/src/mapping-store.ts: 1

249-269: LGTM!

docs/architecture/harness-plugin-runtime.md: 1

205-206: LGTM!

packages/mapping-store/test/windows-lock.test.ts: 1

1-176: LGTM!

packages/mapping-store/test/windows-lock-native.test.ts: 1

1-79: LGTM!

📝 Summary

Summary by CodeRabbit

  • 问题修复
    • 改进 Windows 锁冲突识别:检测到锁记录对应的 PID 已被其他进程复用时,可回收旧锁,且不会终止该 PID 对应的进程。
    • 对仍在运行的锁所有者、身份信息不足或查询失败等情况,继续保留锁并报告 STORE_LOCKED,降低误回收有效锁的风险。

Windows Mapping Store 现在通过 CIM 查询进程路径和创建时间,并据此处理锁冲突。新增模拟测试和 Windows 原生测试,覆盖 PID 复用、活动锁及身份信息不可用等情形。

Changes: Windows 锁身份检查

Layer / File(s) Summary
CIM 身份查询与锁处理
packages/mapping-store/src/mapping-store.ts, packages/mapping-store/test/windows-lock.test.ts, docs/architecture/harness-plugin-runtime.md
Windows 进程身份查询改用 CIM,读取路径和创建时间,并将创建时间转换为 UTC ISO 字符串。测试覆盖 PID 复用、身份信息不足、查询失败和活动锁等情况。
原生 Windows 锁行为验证
packages/mapping-store/test/windows-lock-native.test.ts
新增 Windows 原生测试,验证 PID 复用时回收旧锁且不终止子进程,并验证活动所有者锁和缺少创建时间的旧格式锁会被保留。

Priority: Normal

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

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: omsd512-w

Merge Risk: Minimal · up to a85d6

No actionable merge-blocking issue is established. Windows CI remains the planned check for the native tests.

Pre-merge checks: Passed 4
Check name Status Explanation
Title check Passed 标题准确概括了主要变更:修复 Windows PID 复用导致的 Mapping Store 过期锁恢复问题。标题简洁、具体,并与改动范围一致。
Description check Passed 描述与变更内容直接相关。它说明了问题根因、CIM 身份查询、超时和安全边界、测试结果及未验证项。
Linked Issues check Passed [#612] 要求:Windows 上识别残留锁的 PID 已被其他进程复用,安全回收旧锁;不结束无关进程;身份未知时不删锁;真实持锁者仍受单实例保护。

已核对的实现(mapping-store.ts 的 processIdentity 差异):
- 锁冲突时改用 Get-CimInstance Win32_Process 读取 ExecutablePath 和 CreationDate。路径不…
Out of Scope Changes check Passed 已检查的变更均与 [#612] 的 Mapping Store 锁身份检查直接相关:
- packages/mapping-store/src/mapping-store.ts:processIdentity 从 Get-Process 改为 Get-CimInstance,增加 UTC ISO 转换和 5 秒超时。该变更直接实现 #612 的需求。
- packages/mapping-stor…
Full details: Linked Issues check

Explanation

[#612] 要求:Windows 上识别残留锁的 PID 已被其他进程复用,安全回收旧锁;不结束无关进程;身份未知时不删锁;真实持锁者仍受单实例保护。

已核对的实现(mapping-store.ts 的 processIdentity 差异):

  • 锁冲突时改用 Get-CimInstance Win32_Process 读取 ExecutablePath 和 CreationDate。路径不可读时,仍可依据创建时间判断 PID 复用。
  • CreationDate 在 PowerShell 内转换为 UTC round-trip ISO 字符串('o' 格式),避免默认 DateTime JSON 格式问题。
  • 查询设置 timeout: 5_000。失败、空输出或无效输出时返回空身份,锁保留。
  • 代码中未见 kill 或删除进程的调用。

测试覆盖(windows-lock.test.ts,模拟 Windows 响应):

  • 路径不可读、创建时间相差超过 5 秒:回收旧锁。
  • 同路径但创建时间不同:回收旧锁。
  • 创建时间在 5 秒容差内,或路径等价写法相同:保留锁。
  • 查询失败(EACCES、ETIMEDOUT、ENOENT)、空输出、畸形输出:保留锁并返回 STORE_LOCKED。
  • 无锁冲突时不调用 PowerShell。
  • 对 process.kill 仅使用信号 0 探测。

残余不确定性:

  • 三项 Windows 原生测试(windows-lock-native.test.ts)未在本地执行,待 Windows CI 验证。
  • 原报告的普通权限 svchost.exe 实机场景未经报告者在候选版本上验证。
  • 上述不确定性属于验证范围,不构成代码层面的未满足需求。
Full details: Out of Scope Changes check

Explanation

已检查的变更均与 [#612] 的 Mapping Store 锁身份检查直接相关:

  • packages/mapping-store/src/mapping-store.ts:processIdentity 从 Get-Process 改为 Get-CimInstance,增加 UTC ISO 转换和 5 秒超时。该变更直接实现 #612 的需求。
  • packages/mapping-store/test/windows-lock.test.ts:模拟 Windows 身份响应的单元测试,验证上述行为。
  • packages/mapping-store/test/windows-lock-native.test.ts:仅 Windows 执行的原生测试,验证真实子进程的 PID 复用和活动持锁保护。
  • 运行时架构文档(docs/architecture/harness-plugin-runtime.md):同步查询方式与安全边界说明,属于对实现的文档化。

未发现与 #612 无关的功能变更。PR 描述中的 macOS/Linux 路径不变、无新增依赖,与代码差异一致。未审阅的 ignored_files 为空,无排除影响。

Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • 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

Comment @coderabbitai help to get the list of available commands.

@BytePioneer-AI
BytePioneer-AI merged commit 41ad5d7 into main Oct 28, 2026
5 checks passed
zxd-666 added a commit to zxd-666/codex-host that referenced this pull request Oct 29, 2026
codexhost v0.15.0

## What's New

Import native Sessions from a selected Host, answer multi-select questions, and use Grok's native questions. This release also adds Antigravity usage statistics and improves conversation reliability across Claude Code, Pi, Windows, and Codex Desktop.
支持从所选 Host 导入原生 Session、回答多选问题,并接入 Grok 原生提问。本版还新增 Antigravity 用量统计,改进 Claude Code、Pi、Windows 和 Codex Desktop 的会话可靠性。

### Features

- Select a local or remote Host for Session import, keeping import and opening on the selected machine; support macOS Broker-backed imports for Claude Code, Cursor CLI, CodeBuddy, and WorkBuddy (BytePioneer-AI#207)
- Answer external Harness multi-select questions in a shared card, with accompanying single-choice and text fields and drafts retained while switching chats (BytePioneer-AI#625)
- Answer Grok's native single-choice, multi-select, and text questions, with cancellation and native withdrawal support (BytePioneer-AI#625)
- Read Antigravity's native historical request and Token usage, including Sessions created outside codexhost; model and cost information remain unavailable when absent from native records (BytePioneer-AI#588)

### Fixes

- Steer Claude Code continuations within the same Turn without stopping background Subagents, and keep accepted instructions attached to that Turn (BytePioneer-AI#617)
- Release Claude Code Turns after refused Subagent messages, and allow Fork and message-edit recovery after entering a native worktree (BytePioneer-AI#626, BytePioneer-AI#629)
- Improve Pi's large-history RPC performance and ignore late replies to expired read-only queries without faulting the Session (BytePioneer-AI#607)
- Recover stale Windows Mapping Store locks when a PID has been reused by another process (BytePioneer-AI#627)
- Keep Antigravity questions working across Turns, preserve the project working directory, and bound process cleanup waits (BytePioneer-AI#586)
- Keep Antigravity Conversation totals separate from current context usage (BytePioneer-AI#587)
- Disable background self-update by default for Host-started Antigravity processes on Windows and macOS, while retaining explicit updates (BytePioneer-AI#585)
- Explain unavailable ZCode plans or quotas without asking an already signed-in account to log in again (BytePioneer-AI#621)
- Compact Composer controls in narrow layouts and keep native question and approval inputs separate from chat controls (BytePioneer-AI#624, BytePioneer-AI#625)
- Support Codex Desktop 26.1023 native language settings (BytePioneer-AI#636)
- Fix availability checks routed through the Harness Broker

### 新功能

- 会话导入支持选择本地或远程 Host,导入和打开始终使用所选机器;macOS 上通过 Broker 托管的 Claude Code、Cursor CLI、CodeBuddy 和 WorkBuddy 支持导入 (BytePioneer-AI#207)
- 外部 Harness 多选问题使用公共卡片,可与单选、文本字段一起回答,切换聊天时保留未提交草稿 (BytePioneer-AI#625)
- 接入 Grok 原生单选、多选和文本提问,支持取消及原生问题撤回 (BytePioneer-AI#625)
- 从 Antigravity 原生历史记录统计请求和 Token,包括未经 codexhost 创建的 Session;原生记录缺少模型信息时显示为未知模型且不计价 (BytePioneer-AI#588)

### 修复

- Claude Code 后台 Subagent 运行期间支持同一 Turn 内继续插入指令,不必停止后台任务;已接受的指令保持归属当前 Turn (BytePioneer-AI#617)
- Claude Code 子 Agent 消息被拒绝后正确释放 Turn;进入原生 worktree 后仍可 Fork 和进行消息编辑恢复 (BytePioneer-AI#626, BytePioneer-AI#629)
- 改善 Pi 大历史 RPC 处理性能;只读查询超时后的迟到回复不再导致 Session 故障 (BytePioneer-AI#607)
- Windows 上 PID 被其他进程复用时,可以回收残留的 Mapping Store 锁 (BytePioneer-AI#627)
- 修复 Antigravity 多轮提问失效,明确项目工作目录,并限制进程清理等待时间 (BytePioneer-AI#586)
- 避免把 Antigravity 整段 Conversation 的累计用量误当作当前上下文占用 (BytePioneer-AI#587)
- Windows 和 macOS 上由 Host 启动的 Antigravity 进程默认关闭后台自更新,保留显式更新操作 (BytePioneer-AI#585)
- ZCode 已登录但套餐或额度不可用时显示正确原因,不再要求重复登录 (BytePioneer-AI#621)
- 窄布局下压缩 Composer 控件,并避免聊天控件干扰原生提问与审批输入 (BytePioneer-AI#624, BytePioneer-AI#625)
- 适配 Codex Desktop 26.1023 的原生语言设置 (BytePioneer-AI#636)
- 修复通过 Harness Broker 路由的可用性检查

### Breaking Changes

- Language overrides require Codex Desktop's native settings service. Desktops exposing only the legacy locale RPC are no longer supported for language overrides; reading can fall back to the browser language, but changing the override requires the native service (BytePioneer-AI#636)

### 破坏性变更

- 语言覆盖设置需要 Codex Desktop 原生 settings 服务。仅提供旧 locale RPC 的 Desktop 不再支持语言覆盖;读取时可以降级使用浏览器语言,但修改覆盖值需要原生服务 (BytePioneer-AI#636)

## Installation

### npm

```bash
npm install -g @codexhost/cli
codexhost
```

For remote Session import and multi-select questions, update the Desktop installation and the relevant remote Hosts and Brokers together.

### Installer

Download the installer matching your OS and CPU architecture from this release's assets.

### macOS Gatekeeper

After installing on macOS, if codexhost shows "Apple cannot verify this app" when you open it for the first time, run the following command in Terminal and reopen codexhost:

```bash
xattr -dr com.apple.quarantine /Applications/codexhost.app
```

## Contributors

Thanks to everyone who contributed to this release:

- @BeiLuoqu (BytePioneer-AI#585, BytePioneer-AI#586, BytePioneer-AI#587, BytePioneer-AI#588)
- @bwndlct (BytePioneer-AI#617)
- @BytePioneer-AI (BytePioneer-AI#207, BytePioneer-AI#625, BytePioneer-AI#626, BytePioneer-AI#627, BytePioneer-AI#629, BytePioneer-AI#636)
- @cy2s (BytePioneer-AI#624)
- @karlorz (BytePioneer-AI#625)
- @kunkun9527 (BytePioneer-AI#607)
- @SuperGoodGame (BytePioneer-AI#207)
- @ump45nose (BytePioneer-AI#621)

# Conflicts:
#	Cargo.lock
#	Cargo.toml
#	package-lock.json
#	package.json
#	packages/renderer-extension/src/renderer-binding-probe.ts
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.

[Bug] Windows 重启后 Mapping Store 残留锁的 PID 被 svchost 复用,误报 STORE_LOCKED 阻止启动

1 participant