Skip to content

fix(host): recover external sessions after a confirmed late idle release - #1182

Merged
iruriQ merged 2 commits into
BytePioneer-AI:mainfrom
ChaceLyee2101:fix/recover-confirmed-idle-release
Nov 25, 2026
Merged

iruriQ merged 2 commits into
BytePioneer-AI:mainfrom
ChaceLyee2101:fix/recover-confirmed-idle-release

Conversation

@ChaceLyee2101

Copy link
Copy Markdown
Contributor

Summary

An idle release that exceeds its 60-second deadline leaves the external Thread blocked even if the original shutdown later completes.

  • Record completion after the original session closes, its output drains, and cleanup finishes. A later request rechecks ownership and persisted history inside the Thread lifecycle before detaching the closed instance.
  • Keep returning -32075 while release or persistence remains unconfirmed. Concurrent retries resume the same native session once.

Related issues

Refs #1167. The causes of the Claude shutdown failures and native Codex rollout errors in the report remain unverified.

UI Demo

N/A (Host runtime and RPC behavior).

Test plan

  • Reproduced the failure with a fake clock and adapter on the upstream code. Both requests stayed blocked after confirmed shutdown.
  • npx vitest run --config tests/vitest.config.js packages/host-runtime/test/external-thread-idle-release.test.ts packages/host-runtime/test/external-thread-recovery.test.ts — 37 tests passed. The regression covers pending cleanup, persisted-history failure and concurrent resume with the original native session reference.
  • npx tsc -b packages/host-runtime, focused ESLint and git diff --check passed locally on macOS.
  • The reporter's Windows installation and live Harness processes were outside this verification.

@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • 错误处理
    • 空闲释放超时后,只有在原会话关闭、输出处理和身份持久化均确认完成后,后续请求才可恢复;未完成或持久化失败时仍会返回错误。
    • 会话关闭尚未确认时,提示等待关闭完成后重试。
  • 恢复行为
    • 关闭完成后,并发请求共享同一个恢复会话,避免重复恢复。
  • 稳定性
    • Windows 上文件更新遇到短暂占用时会自动重试,减少偏好设置和扩展更新失败的情况。

本次变更调整空闲释放关闭确认后的线程移除和恢复规则,并将 Windows 重命名重试逻辑集中到共享函数。相关测试覆盖并发恢复、持久化失败及 Windows 重命名重试场景。

Changes: 空闲释放恢复

Layer / File(s) Summary
释放确认与实例移除条件
packages/host-runtime/src/external-thread-idle-release.ts, docs/proposals/external-harness-idle-unload-proposal.md
记录关闭确认状态。只有关闭已确认、持久化身份有效且线程实例仍匹配时,才移除原线程。提案文档更新访问限制、恢复条件和测试预期。
定位接入与恢复验证
packages/host-runtime/src/external-thread-runtime.ts, packages/host-runtime/test/external-thread-idle-release.test.ts
定位时检查已加载线程是否可移除;未移除时继续沿存储记录处理。测试覆盖关闭确认后并发请求共享一次恢复,以及持久化失败时阻止恢复。

Windows 重命名重试

Layer / File(s) Summary
共享重命名实现与调用
packages/harness-plugin-files/src/rename-with-windows-retry.ts, packages/harness-plugin-files/src/host-extension-io.ts, packages/harness-plugin-files/src/host-extension-files.ts, packages/harness-plugin-files/src/preferences.ts, packages/harness-plugin-files/test/preferences.test.ts
新增共享重命名函数。Windows 遇到 EPERM、EACCES 或 EBUSY 时按 10、30、70、150、300 毫秒间隔重试。扩展目录操作、扩展文件写入和偏好文件更新改用该函数;测试覆盖偏好文件重试。

用量路由测试调整

Layer / File(s) Summary
用量路由测试设置
packages/host-runtime/test/app-server-host.usage-routing.test.ts
测试改为本地定义 THREAD_USAGE_UPDATED_METHOD 常量,并通过 Object.assign 设置 session.readAccountCredits。

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

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant ExternalThreadRuntime
  participant ExternalThreadIdleRelease
  participant Repository
  ExternalThreadRuntime->>ExternalThreadIdleRelease: 检查并尝试移除已确认释放的线程
  ExternalThreadRuntime->>Repository: 查询线程持久化记录
  ExternalThreadRuntime->>ExternalThreadRuntime: 并发恢复请求共享新线程
Loading

Possibly related issues

Possibly related PRs

Suggested labels: 类型:缺陷修复, 范围:宿主运行时

Suggested reviewers: bytepioneer-ai

Merge Risk: Minimal · up to e6da7

This change lets a later request recover an external Thread after a late but confirmed idle release. Requests still receive an error until release and persisted identity are confirmed. It also moves the Windows rename retry into one shared function. No concrete defect remains, and the change is ready to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed 标题“fix(host): recover external sessions after a confirmed late idle release”与变更内容一致。变更涉及空闲释放超时后的确认、后续请求对已关闭实例的移除,以及并发恢复只执行一次。标题准确描述了主要修复点,并且使用 Conventional Commits 格式。
Description check Passed 描述说明了空闲释放超过 60 秒截止时间后线程被阻塞的问题,并列出了记录完成时机、返回 -32075 的行为、并发恢复只执行一次等改动。描述还包含测试计划和相关 issue,与变更集直接相关。
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.
Finishing Touches

Generate unit tests (beta)

  • 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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between f2ae1f5 and e6da7a1.

Files selected for processing: 9

  • packages/harness-plugin-files/src/host-extension-files.ts
  • packages/harness-plugin-files/src/host-extension-io.ts
  • packages/harness-plugin-files/src/preferences.ts
  • packages/harness-plugin-files/src/rename-with-windows-retry.ts
  • packages/harness-plugin-files/test/preferences.test.ts
  • packages/host-runtime/src/external-thread-idle-release.ts
  • packages/host-runtime/src/external-thread-runtime.ts
  • packages/host-runtime/test/app-server-host.usage-routing.test.ts
  • packages/host-runtime/test/external-thread-idle-release.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: 8

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

Code guidelines: 2

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

Source excerpt: Host Runtime loads installed plugins through public contracts, not direct imports of concrete Adapter packages.

CodeRabbit inference engine (CLAUDE.md)

Files:

  • packages/host-runtime/src/external-thread-idle-release.ts
  • packages/host-runtime/src/external-thread-runtime.ts
  • packages/host-runtime/test/app-server-host.usage-routing.test.ts
  • packages/host-runtime/test/external-thread-idle-release.test.ts
Source excerpt: Use package public exports for cross-package dependencies.

CodeRabbit inference engine (AGENTS.md)

Files:

  • packages/harness-plugin-files/src/host-extension-files.ts
  • packages/harness-plugin-files/src/host-extension-io.ts
  • packages/harness-plugin-files/src/preferences.ts
  • packages/harness-plugin-files/src/rename-with-windows-retry.ts
  • packages/harness-plugin-files/test/preferences.test.ts
  • packages/host-runtime/src/external-thread-idle-release.ts
  • packages/host-runtime/src/external-thread-runtime.ts
  • packages/host-runtime/test/app-server-host.usage-routing.test.ts
  • packages/host-runtime/test/external-thread-idle-release.test.ts
Learnings: 1

Learning: 2026-11-17T12:08:43.655Z

Learnt from: CR
Repo: BytePioneer-AI/codex-host PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-11-17T12:08:43.655Z
Learning: Source excerpt:
# Repository Guidelines

## Testing & Completion

- Writing tests. Every test is code to maintain and CI time to pay; add one only when it earns that.
  - Small, low-risk changes need no new tests. Add focused tests for high-risk or cross-package changes, or when asked.
  - A new test must catch a regression the existing tests would miss. Extend the nearest existing case or `test.each` instead of adding near-duplicates.
  - Assert observable behavior (inputs, outputs, protocol messages, persisted state), not internal call order, private state, or mock call counts.
  - Mock only external boundaries (network, clock, real CLIs, the OS), never the code under test. Avoid large snapshots and long literal expectations that can only be updated mechanically.
  - A bug-fix test must fail before the fix. Never weaken assertions or delete tests just to make a change pass; report the conflict instead.
  - When a change makes existing tests redundant or ties them to removed internals...

Applied to files:

  • packages/host-runtime/test/external-thread-idle-release.test.ts
Additional comments: 1

packages/host-runtime/src/external-thread-runtime.ts: 1

649-649: Stability & Availability

同 ID 的第二个请求会等待首个恢复操作完成后再进入 #locate。

ExternalThreadLifecycle.run 的门控覆盖整个异步 operation(),而不是只覆盖回调调用。resolve 把 #locate 和 #restore 都放在 #withLifecycle 中,因此二者都位于同一个门控内。第二个请求只能在首个请求的 #restore 完成后进入 #locate,读取到的是已注册的恢复实例。提出的两个请求同时读取未加载状态的路径不成立。

@ChaceLyee2101
ChaceLyee2101 force-pushed the fix/recover-confirmed-idle-release branch from f2ae1f5 to e6da7a1 Compare November 25, 2026 13:29
@iruriQ
iruriQ merged commit 432c544 into BytePioneer-AI:main Nov 25, 2026
6 of 8 checks passed
@iruriQ

iruriQ commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

感谢贡献!迟到空闲关闭确认与实例回收逻辑严密、无并发重复写入风险,且与刚合入的 #1171 正向协同,现已通过 merge commit 合入 main(保留 #1167 继续跟踪 Adapter 侧关闭问题)。

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