Skip to content

fix(delegation): let queued follow-ups start before delegated sends - #1241

Merged
iruriQ merged 1 commit into
BytePioneer-AI:mainfrom
lpmasser:pr/delegated-send-queue-first
Nov 27, 2026
Merged

iruriQ merged 1 commit into
BytePioneer-AI:mainfrom
lpmasser:pr/delegated-send-queue-first

Conversation

@lpmasser

@lpmasser lpmasser commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

外部 Harness Thread 空闲、但 Desktop/Web 跟进队列里还有消息时(包括 Stop 或 Turn 失败后处于“队列已暂停”的状态),委派的普通 thread send(CLI、HTTP、MCP)会直接启动一个新 Turn,抢在队列前面。thread watch 的通知也走同一条发送路径,因此会越过用户已暂停的队列把 Thread 唤醒,用户排好的跟进消息反而排到 Agent 消息之后。

Host 处理 Web 的 turn/start { delivery: "queue" } 时,已经有一条规则:队列不为空就把新输入排到队尾。本 PR 让委派发送遵循同一规则:

  • 外部 Thread 的跟进队列不为空时,普通 thread send 返回 THREAD_BUSY,下一轮留给队列。watch 通知按既有重试,等队列清空后再送达。
  • 显式的 --steer true 和 --interrupt 仍可先于队列开始。注册表在 interrupt 停下 Turn 之后重发消息时带上 interrupt,让被停下的 Thread 接受这条消息。
  • 只作用于外部 Harness Thread。原生 Codex Thread 的队列由 Desktop 主进程管理,Host 看不到,行为不变。

文档:docs/architecture/external-thread-queue.md。

Related issues

N/A。#1242(thread send --mailbox)以本 PR 为前置。

UI Demo

No visual changes: 只改 Host 的委派发送路由。

Test plan

  • npx vitest run --config tests/vitest.config.js packages/host-runtime/test/harness-delegation-coordinator.test.ts packages/host-runtime/test/delegation-interrupt-send.test.ts packages/host-runtime/test/delegation-watch.test.ts:99 个全部通过。
    • 新增用例:队列有消息时,普通发送返回 THREAD_BUSY;interrupt 和 steer 两种情况都能先于队列开始。
    • 修改用例:interrupt 停下后的重发带上 interrupt: true。
  • npx tsc -b 通过。
  • npx tsc -p tests/tsconfig.json --noEmit 报 5 个错误,与当前 main 上完全相同,位于 thread-tree-watch、peer-delegation、peer-thread-links,不涉及本 PR 改动的文件。
  • 已与 feat(delegation): add thread send --mailbox #1242 一起在本地 macOS arm64 构建上日常使用。“暂停队列后由 watch 通知唤醒”这一场景,尚未在真实 Desktop 中单独复现验证。

@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • 行为调整
    • 线程队列中有待处理任务(包括暂停的任务)时,普通发送会返回 THREAD_BUSY;队列清空后才处理下一轮,相关通知也会延后。
    • 显式转向或中断发送不受此限制。中断发送会先停止当前轮次,再优先交付消息。
    • 由 Desktop 主进程管理的原生 Codex Thread 队列不适用此规则。
  • 文档
    • 补充了队列繁忙时的发送规则说明。

委派发送现在会检查 Desktop 或 Web 的外部线程队列。队列有等待项时,普通发送返回 THREAD_BUSY;显式 steer 或 interrupt 可绕过检查。本地中断发送会传递 interrupt: true。

Changes: 委派发送与外部队列

Layer / File(s) Summary
传递中断发送标志
packages/host-runtime/src/delegation-control-registry.ts, packages/host-runtime/src/delegation-types.ts, packages/host-runtime/test/delegation-interrupt-send.test.ts
本地中断发送传递 interrupt: true。类型注释说明该消息先于已排队的后续消息发送,测试检查发送参数。
接入外部队列并限制普通发送
packages/host-runtime/src/app-server-host.ts, packages/host-runtime/src/harness-delegation-coordinator.ts, packages/host-runtime/test/harness-delegation-coordinator.test.ts, docs/architecture/external-thread-queue.md
AppServerHost 通过 ExternalThreadQueue 检查队列状态。队列有等待项时,未指定 steer 或 interrupt 的发送会被拒绝;测试覆盖拒绝和绕过检查的行为。文档说明 THREAD_BUSY 规则及 watch 通知的延后行为。

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant ThreadSendCaller
  participant HarnessDelegationCoordinator
  participant ExternalThreadQueue
  ThreadSendCaller->>HarnessDelegationCoordinator: 请求发送消息
  HarnessDelegationCoordinator->>ExternalThreadQueue: 查询线程的等待项
  ExternalThreadQueue-->>HarnessDelegationCoordinator: 返回队列状态
  alt 队列有等待项且未指定 steer 或 interrupt
    HarnessDelegationCoordinator-->>ThreadSendCaller: 返回 THREAD_BUSY
  else 队列为空或指定 steer 或 interrupt
    HarnessDelegationCoordinator-->>ThreadSendCaller: 继续发送
  end
Loading

Possibly related issues

  • BytePioneer-AI/codex-host#1128:该 Issue 涉及父 Thread 的 watch 通知投递。本 PR 修改队列非空时的发送行为,并说明通知延后至队列清空;但没有实现该 Issue 所述的限额判定或额度刷新续跑逻辑。

Possibly related PRs

  • BytePioneer-AI/codex-host#1242:该 PR 基于本 PR 的 THREAD_BUSY 队列规则增加显式 --mailbox 发送,并在忙碌时等待队列完成后合并投递信箱消息与 watch 通知。

Suggested reviewers: bytepioneer-ai, ump45nose

Merge Risk: Minimal · up to 7152f

Ordinary delegated sends to an external Thread now return THREAD_BUSY while Desktop or Web follow-ups are queued, and explicit steer or interrupt sends can still go ahead of the queue. Watch notifications keep retrying until the queue drains. No concrete defect was found, and the behavior is covered by tests and documentation. The change is low risk to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed 标题“fix(delegation): let queued follow-ups start before delegated sends”与主要变更一致。变更让外部 Harness Thread 的跟进队列非空时,委派的普通 thread send 返回 THREAD_BUSY,队列消息优先开始。标题指出了核心行为,并使用了 fix 前缀。标题为英文,与仓库中其他 PR 标题风格可能不同,但这…
Description check Passed 描述与变更高度相关。描述说明了问题(外部 Thread 空闲且队列有消息时,委派发送会抢先启动 Turn)、解决方案(队列非空时返回 THREAD_BUSY,显式 steer 和 interrupt 不受限制)、影响范围(仅外部 Harness Thread),并给出了测试计划和未验证的场景。
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.
Full details: Title check

Explanation

标题“fix(delegation): let queued follow-ups start before delegated sends”与主要变更一致。变更让外部 Harness Thread 的跟进队列非空时,委派的普通 thread send 返回 THREAD_BUSY,队列消息优先开始。标题指出了核心行为,并使用了 fix 前缀。标题为英文,与仓库中其他 PR 标题风格可能不同,但这不影响判定。

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 52f5216 and 7152fc0.

Files selected for processing: 2

  • packages/host-runtime/src/app-server-host.ts
  • packages/host-runtime/src/harness-delegation-coordinator.ts

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

Recent review details

Context from checks skipped due to timeout: 8

  • GitHub Check: CodeRabbit
  • GitHub Check: Check macos-26
  • GitHub Check: Static checks
  • GitHub Check: Vitest
  • GitHub Check: Check ubuntu-22.04
  • GitHub Check: Web Host tests
  • GitHub Check: Check Linux ARM64
  • GitHub Check: Check windows-latest
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/app-server-host.ts
  • packages/host-runtime/src/harness-delegation-coordinator.ts
Source excerpt: Use package public exports for cross-package dependencies.

CodeRabbit inference engine (AGENTS.md)

Files:

  • packages/host-runtime/src/app-server-host.ts
  • packages/host-runtime/src/harness-delegation-coordinator.ts
Additional comments: 1

packages/host-runtime/src/harness-delegation-coordinator.ts: 1

531-531: Functional Correctness

Watch 通知不会因队列清空而停止。

这个问题不成立。THREAD_BUSY 被归类为 rejected,表示发送前已拒绝,不会重复投递。待投递的 watch 由 #schedule 定时轮询,默认每 2 秒执行一次(delegation-watch.ts:438-456)。队列删除后,下一次轮询即可投递,等待时间受轮询间隔限制,不依赖 onThreadIdle。CLI 帮助也写明忙碌的 Thread 会在最多 6 小时内重试(delegation-cli-help.ts:94)。

A plain `thread send` to an idle external Thread with Desktop/Web follow-ups
still queued (including a queue paused after Stop) started a Turn ahead of
them, and watch notifications used the same path, so they woke a Thread whose
queue the user had paused. Such a send now returns THREAD_BUSY, which leaves
the next Turn to the queue; watch notifications wait until it has drained.

Explicit `--steer true` and `--interrupt` still go ahead of the queue: the
registry marks the resend of an interrupt with `interrupt`, so the stopped
Thread takes it. Native Codex Threads keep their queue in Desktop and are
unaffected.
@lpmasser
lpmasser force-pushed the pr/delegated-send-queue-first branch from 52f5216 to 7152fc0 Compare November 27, 2026 03:12
@iruriQ
iruriQ merged commit 5dca5ee into BytePioneer-AI:main Nov 27, 2026
4 of 8 checks passed
@iruriQ

iruriQ commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

感谢贡献!队列优先逻辑语义清晰且无边界隐患,已合入 main(同时也已解除 #1242 的前置依赖)。

@lpmasser
lpmasser deleted the pr/delegated-send-queue-first branch November 28, 2026 11:18
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