Skip to content

fix(pi-subagents): persist delegate completion notifications via turn_end steering (#2546) - #2550

Merged
ranxianglei merged 1 commit into
masterfrom
2026-10-09_delegate-notify-persisted-steer
Oct 9, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-10-09_delegate-notify-persisted-steer

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #2546

Problem

While the parent was mid-task, the pi plugin delivered async acp_delegate completion notifications through pi's request-local context event (commitAtContextBoundary): one merged user message appended to the outgoing message list of exactly ONE request — pi restores the original state afterward. The message was never written to the session, so:

  1. Cache: history stopped being append-only → stateful upstreams resuming live sessions by comparing the echoed transcript (e.g. the magpie gateway running Claude Code) dropped the session and replayed the whole conversation.
  2. Correctness: the model saw the notification once, then lost the runId/result-path from its own history on later turns.

Fix

Commit at turn_end instead — the moment a turn's tool execution finishes, right before the agent loop drains steering for the next LLM call — and send ONE merged notification through sendUserMessage(steer), the same persisted carrier the idle tier already used. Both tiers now share one mechanism, differing only in trigger point.

Why everything the context boundary provided is preserved:

The old assistant-message gate is gone by design: turn_end fires only for this host's real agent-loop turns, and steering delivery is session-global (there is no per-call message list that auxiliary calls could consume).

Behavior change (intended — this is the fix)

  • The notification becomes a real user message in pi's session file (it was absent before) and stays in every later request at its position — what stateful upstreams need.
  • pendingMessageCount may transiently be 1 during the task (between the turn_end commit and the loop's drain, one macrotask apart); it was 0 before. Quiescence still requires it back to 0.
  • Which LLM call first sees the notification is UNCHANGED: the next call after completion.

Tests

  • pi-subagents/tests/delegate-context-notify.test.ts → delegate-turn-end-notify.test.ts: busy-tier tests now drive turn_end and assert on captured steering sends; new test: a failed send keeps the batch scheduled and recoverable via the undelivered-runs path. Idle-tier tests unchanged.
  • tests/e2e/e2e-delegate-notify.test.ts (real pi RPC lane, deterministic fake upstream):
  • Local runs: pi-subagents suite 224/224, root typecheck + build clean, e2e-delegate-notify 2/2 (oracle rows show notif=1 on every request after delivery), e2e-native-pi 6/6, bili pi-lane unit 37/37. Full regression left to CI.

pi-subagents 0.1.3 → 0.1.4 (CI-enforced version discipline).

…_end steering (#2546)

The busy-tier commit appended the notification to the `context` event's outgoing message list — request-local by design: pi restores the original state afterward, so the message lived in exactly one request and vanished from later ones. On stateful upstreams that resume live sessions by comparing the echoed transcript this dropped the session off its cache (full history replay); the model also lost the runId/result-path from its own history.

Commit at `turn_end` instead — the moment a turn's tool execution finishes, right before the agent loop drains steering for the next LLM call — and send ONE merged notification through sendUserMessage(steer), the same persisted carrier the idle tier already used. Delivery timing is unchanged (next LLM call after completion); the final ownership re-check is equally fresh (claims can only happen inside tool execution, which ends before turn_end), so the #2301 post-commit race stays closed and #2320's mid-task timing holds. A failed send keeps the batch scheduled for the next boundary.

pi-subagents 0.1.3 -> 0.1.4
@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-09_delegate-notify-persisted-steer (81fdd84)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2729-pr.2550.ba1afcf

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr2550.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

PR #2550 审查结论(独立复核)

这是我先前开的 PR(#2546 thread 已给出首轮结论)。本轮按 review 规范做独立复核:拉分支通读 diff、逐项清点既有行为、核对测试牙齿、独立查 CI。base 已是当前 master(merge-base == master head,非 stale);单提交、5 文件,全部落在 delegate 通知机制内,diff 干净无跑题、无空白/重排噪音。

结构化八问

  1. 是什么问题:父 agent 在 tool loop 中时,async acp_delegate 完成通知只活在恰好一次请求里(被追加进 pi context 事件的出参消息列表),从未写入会话文件;下一次请求起它从历史中间消失。

  2. 什么原因导致的:双层断裂。(a) commitAtContextBoundary 返回修改后的消息数组,但 pi 的 context 事件按设计是请求本地的("Pi restores that state afterward"),handler 返回值不进 session;(b) run 同时被标 injected=true,idle flush 因此永不再补投。两条路都断 → 通知彻底丢失。根因是「拿请求本地载体去承载一条必须持久化的消息」,不是投递时机算错。

  3. 影响范围与严重程度:所有 pi plugin 模式「delegate 在父任务中途完成」的场景(最常见用法)。分两级:对按回显 transcript 恢复活会话的 stateful upstream(magpie+Claude Code),每次触发都丢会话、整段历史重写(issue 实测 96K–113K tokens 重放);普通前缀缓存仅损失尾部;另有正确性损失——后续 turn 丢失 runId/结果路径。属「特定条件下有实质影响」,非全局不可用。

  4. 怎么修复的:把 busy-tier 提交点从「每次 LLM 调用前的 context 事件」移到「turn_end(当前 turn 工具执行刚结束、agent loop 为下一次调用 drain steering 队列之前)」,通知改走 sendUserMessage({deliverAs:"steer"}) —— 与 idle tier 同一个持久化载体。消息从此写入 session、后续每个请求在原位置保留它。check→enqueue 之间无 await,所有权终检新鲜度不变(认领只能发生在工具执行内部,而它在 turn_end 前已结束),[Bug][Pi] notifyIfRead="skip" still delivers stale follow-ups after the final result was read #2301 竞态保持关闭;feat(compress): configurable compression prompts (three-level, risk-gated) #157 同边界合并、[Bug][Pi] acp_delegate 完成通知使用 followUp,主任务运行期间长期滞留队列;建议改用 steer #2320 中途送达语义均保留;发送失败时批次留在队列等下一边界重试(对齐 idle tier 恢复纪律)。两 tier 现在只差触发点。

  5. 是否有回归问题:逐项审过既有行为并给结论——read-suppression-at-commit([Bug][Pi] notifyIfRead="skip" still delivers stale follow-ups after the final result was read #2301)/waiter·consumed 排除/同边界合并(feat(compress): configurable compression prompts (three-level, risk-gated) #157)/idle 迟到补投/per-host 隔离:全部保持,各有单测钉住。usage 记账由「恒标记」改为「成功或 separate 才标记」,与 idle tier 完全一致(防失败重试双计)——属有意对齐,非漂移。唯一残余观察项:若某 host 发 context 却不发 turn_end,busy-tier 会退化到仅 idle-tier 投递(延迟变长、不丢数据);已由 real-pi e2e(pi-stable@0.83.6 钉死)覆盖参考版本,列为观察项而非缺陷。旧 assistant-message 门控按设计移除:turn_end 只在本 host 真实 agent-loop turn 触发,且 steering 投递是会话级、无 per-call 消息列表可供辅助调用消费,门控已无作用对象。

  6. 是否破坏缓存:这正是修缓存的问题——历史恢复 append-only,通知一经投递永久停留在其位置,stateful upstream 不再因 transcript 突变丢会话。可观察瞬态仅一处:commit→drain 之间(一个宏任务)pendingMessageCount 会为 1(此前恒 0),静息判定仍要求回到 0;线上 wire 形状无变化。

  7. 是否改了设计方向:无架构转向。两 tier 收敛到同一持久化 steering 载体,只差触发点(turn_end vs settled);busy tier 载体从「请求本地 context 追加」变为「会话持久化 user message」——即本 fix 本身。

  8. 是否引入了新的配置:无。

测试有效性:单测(delegate-turn-end-notify.test.ts)是真测试——驱动 turn_end 断言捕获到的 steering 发送(deliverAs、合并条数、read 抑制、失败重试、per-host 隔离、waiter 排除都有牙);e2e-delegate-notify 新增 notifCount oracle + 「投递后每个后续非 title 请求仍带通知」断言,正好钉住 #2546 持久化性质(buggy 路径只在恰好一行出现 notif=1,修复路径每行都带);maxPending<=1(瞬态 steering 停车即机制本身,>1 才是双排队)。非恒真、非 mock-only。

CI(独立查 head 81fdd84):关键门禁全绿——real pi package-native lane(= e2e-native-pi + e2e-delegate-notify,即本 fix 的载荷验证)、pi-subagents-guard(版本纪律 0.1.3→0.1.4)、ubuntu 全量 test 矩阵、kernel/version/docs/lane/unused-exports guard 均 success;其余 codex/dsh/opencode/registry e2e 全绿。仅剩 2 个 Windows 全量 test 矩阵 job 仍 in_progress(通用回归,与本机制无关)。

判定:方向正确、根因修复、既有行为逐项保持、测试有牙、diff 干净、载荷 e2e 已绿。待最后 2 个 Windows 矩阵 job 转绿即可合并。我不代合,请 owner 自行 merge:#2550

@ranxianglei
ranxianglei merged commit 063e567 into master Oct 9, 2026
25 checks passed
@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.194-master.063e567 (rolling master tag).

npm install -g billion-context@0.0.194-master.063e567  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant