Skip to content

fix(host-runtime): preserve External Side Chat context via thread/inject_items - #551

Closed
bwndlct wants to merge 1 commit into
BytePioneer-AI:mainfrom
bwndlct:fix/external-side-chat-context-injection
Closed

bwndlct wants to merge 1 commit into
BytePioneer-AI:mainfrom
bwndlct:fix/external-side-chat-context-injection

Conversation

@bwndlct

@bwndlct bwndlct commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Summary

Codex Desktop initializes Side Chat by sending thread/inject_items with a boundary message ("inherited history is reference-only…"). For External Harness Threads this method was not in the External Thread allow-list, so Host answered -32076 External Thread does not support thread/inject_items and Desktop reported Error opening side chat (Case B in #550).

External Harness sessions have no raw item-history mutation API, and dropping or fake-acknowledging the message would change Side Chat semantics. This PR takes option 2 from #550: Host buffers the boundary context and merges it into the first real Turn input, without creating a synthetic native Turn.

  • thread/inject_items is routed to External Threads. Only user message items whose content parts are text (input_text / text) are accepted; anything else is rejected with -32602, so context that would be lost is never acknowledged.
  • Accepted text is kept in ExternalThread.pendingInjectedText (deduplicated) and combined with the next turn/start text (pending…\n\ntext). The buffer is cleared only after the native turn.start succeeds, so a failed start keeps the context for the retry.
  • Idle release is blocked while injected text is pending, so the context is not dropped with the Session.
  • New helpers live in external-thread-inject.ts to keep app-server-host.ts from growing further.

Related issues

Refs #550. This covers Case B (completed source Turn → thread/inject_items). It does not address Case A (source Turn still running → -32080 External Fork Checkpoint is unavailable), so this PR should not close the issue.

Test plan

Automated (run in this worktree, macOS arm64, based on main 1aa9f65):

  • npm run typecheck — passed
  • npm run lint (eslint + tools/check-boundaries.mjs) — passed
  • npx prettier --check on the 4 changed files — passed
  • npx vitest run --config tests/vitest.config.js packages/host-runtime/test/app-server-host.projection-3.test.ts — 49 passed, including two new tests:
    • Fork → thread/inject_items → first Turn: the derived Session's Turn input is Side conversation boundary.\n\nsynthetic.
    • Non-text item (input_image) → -32602.
  • npx vitest run --config tests/vitest.config.js packages/host-runtime — 1048 passed, 1 failed on the full-package run: harness-plugin-loader.test.ts > gives later plugins a full timeout after earlier plugins finish (diagnose called 4 times). That file is not touched by this PR; it passed 50/50 on two separate single-file runs. I did not confirm the failure on a clean main baseline (the attempted baseline checkout had no built dist), so I can't say whether it is a pre-existing load-sensitive flake.

Manual: with Codex Desktop launched through the locally built codexhost from this branch (macOS arm64), Side Chat now opens on an External Harness session.

Not verified: that the Harness model actually follows the boundary text across several Side Chat turns; each Harness separately (the issue reproduces on DSH and Claude Code; I have not recorded which one was used for the manual check); Windows/Linux; the Case A path; behavior across a Host restart (the buffer is in-memory only).

@coderabbitai

coderabbitai Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: BytePioneer-AI/codex-host/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f344512d-55c8-4556-967e-889a90fb698b
📥 Commits

Reviewing files that changed from the base of the PR and between 1aa9f65 and a7f9e48.

📒 Files selected for processing: 4
  • packages/host-runtime/src/app-server-host.ts
  • packages/host-runtime/src/external-thread-inject.ts
  • packages/host-runtime/src/external-thread-runtime.ts
  • packages/host-runtime/test/app-server-host.projection-3.test.ts

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: 4
  • GitHub Check: Check ubuntu-22.04
  • GitHub Check: Check Linux ARM64
  • GitHub Check: Check macos-14
  • GitHub Check: Check windows-latest
📝 Summary

Summary by CodeRabbit

  • 新功能
    • 支持向外部线程注入文本上下文,并将其与下一轮输入合并处理。
    • 注入内容格式无效时会返回错误;执行失败时,待处理文本会保留以便后续重试。

新增外部线程的 thread/inject_items 处理。系统校验并排队文本,在下一轮 Turn 中合并文本和用户输入。执行成功后清空待注入文本;执行失败时保留文本。

Changes: 外部线程文本注入

Layer / File(s) Summary
待注入文本的数据与处理
packages/host-runtime/src/external-thread-runtime.ts, packages/host-runtime/src/external-thread-inject.ts
外部线程新增待注入文本列表。新增解码、去重追加和文本合并函数。
注入路由与队列管理
packages/host-runtime/src/app-server-host.ts
新增 thread/inject_items 路由。无效参数返回 -32602;有效文本进入待处理列表。待注入文本非空时,线程不会因 idle release 条件而释放。
Turn 输入合并与验证
packages/host-runtime/src/app-server-host.ts, packages/host-runtime/test/app-server-host.projection-3.test.ts
Turn 输入和初始投影使用合并文本。执行失败时保留待注入文本,成功后清空。测试覆盖文本合并和图片内容拒绝。

Priority: ➖ Normal

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

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant AppServerHost
  participant ExternalThread
  participant Harness
  Client->>AppServerHost: 发送 thread/inject_items
  AppServerHost->>ExternalThread: 排队有效文本
  Client->>AppServerHost: 启动下一轮 Turn
  AppServerHost->>Harness: 执行合并后的输入
  Harness-->>AppServerHost: 返回执行结果
  AppServerHost->>ExternalThread: 成功后清空待注入文本
Loading

Suggested reviewers: bytepioneer-ai

Merge Risk: 🔵 Low · up to a7f9e

Side Chat context can be silently lost if its session faults before the next turn. This is a bounded recovery risk rather than a routine turn failure.

Pre-merge checks: Passed 4
Check name Status Explanation
Title check Passed 标题准确概括了主要变更:通过 thread/inject_items 保留 External Side Chat 上下文。标题简洁、具体,并与变更内容一致。
Description check Passed 描述与变更内容直接相关。它说明了 External Thread 路由、文本校验、上下文缓存、turn/start 合并、失败重试和测试结果。
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

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

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.

1 participant