Skip to content

fix(#2694): scope pi-subagent child detection to system carriers - #2696

Merged
ranxianglei merged 2 commits into
masterfrom
2026-10-10_pi-child-marker-system-carrier
Oct 11, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-10-10_pi-child-marker-system-carrier

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #2694

What

A main pi session whose HISTORY quoted the pi-subagents child marker (repo docs about subagents inside tool results / assistant text) was misclassified as a restricted child by the #2268 channel-fallback gate and demoted from plugin mode to the proxy-style channel — native ACP tool cards degraded to prose receipts even though pi.subagents was off and the session was a plain main conversation.

Root cause: detectPiSubagentChildSignal byte-scanned the ENTIRE raw request body for <active_agent name= with no role/position awareness and accepted bare prefixes with empty names; handle.ts then closed pluginMode on that signal. Second latent gap: exposesBiliInjectableTool did not recurse into Responses-wire namespace wraps ({type:"namespace", tools:[...]}), so namespaced ACP grants were invisible to the capability check.

Fix (decision source, not payload masking)

  • Detection parses the body and searches only the SYSTEM-PROMPT CARRIERS of all four wire shapes (top-level system, responses instructions, google systemInstruction.parts, messages[]/input[] role system|developer) — identical carrier list to estimateWireOverhead (src/server/budget.ts). History text can no longer flip the channel.
  • Marker contract tightened to COMPLETE: quoted non-empty name after the prefix (the producer in nicobailon/pi-subagents always emits a full tag — verified against its child-launch.ts). Bare prefixes / empty names are documentation residue; unparseable or non-object bodies fail closed (a genuine child request is always valid JSON).
  • Tool walk recurses into nested tools arrays (depth-capped at 8) so namespaced ACP grants count as grants.
  • In-passing nit (own commit): removed the dead piSubagentChannelFallback import in src/server.ts (only call site is src/server/handle.ts).

Behavior changes (old → new)

case old new
marker only in history (tool results / assistant / user text) fallback ON (bug) stays plugin mode
incomplete marker (bare prefix, empty name, truncated) present if bytes matched never counts
unparseable body containing marker bytes lenient raw scan could trigger fallback fail closed (no signal)
ACP grant inside a Responses namespace wrap treated as absent → fallback seen → stays plugin mode
real tagged ACP-less child (#2268 repro) fallback ON unchanged (server cases A–D green)

Every tightening fails toward "stay plugin mode" = the documented graceful degrade (pre-#2268 behavior), so an upstream format drift degrades safely instead of misrouting main sessions.

Tests

tests/pi-subagent-channel.test.ts: inverted the two expectations that encoded the bug (history-only marker, truncated marker); added carrier-shape coverage for all 4 wires, incomplete-marker cases, fail-closed cases, namespaced-grant walker + composition cases, and server-level case E where a pi-stamped MAIN session quoting the marker in history keeps plugin mode through the real pipeline. 15/15 pass locally; typecheck + build green on this head; full suite left to CI.

Config surface: not applicable (no new/changed config fields). No kernel/ or pi-subagents/ changes. No wire-format change — only which requests bili classifies into the existing proxy-style channel.

@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-10_pi-child-marker-system-carrier (9213b7a)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2944-pr.2696.ed07205

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-pr2696.tgz
npm install -g package

Install instructions are refreshed on each push.

ranxianglei pushed a commit that referenced this pull request Oct 10, 2026
… caller has no parsed body (#2696)

Review hardening for #2694: when parsed was absent the gate re-parsed the buffer for the marker but checked grants against an undefined tools field, so a direct caller passing null with a valid body containing a namespaced ACP grant would be misdemoted despite the grant. Unreachable from the sole call site (handle.ts passes null only when the same buffer already failed JSON.parse), but the exported contract now holds standalone: signal and tools always read from the same parsed object, unparseable still fails closed. Regression test pins the parsed=null + namespaced-grant combination.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review done — one small fix pushed to the PR branch (commit 38d32e9), otherwise ready.

Verified against live code (not just the diff):

Small fix I pushed directly (小问题直接修): in piSubagentChannelFallback, the parsed=null branch re-parsed the buffer for the marker but then checked grants against an undefined tools field — a direct caller passing null with a valid body containing a namespaced ACP grant would be misdemoted despite the grant. Proven unreachable from the sole call site (handle.ts passes null only when the same buffer already failed JSON.parse, so the re-parse fails identically), but the exported contract hole was real and untested: the existing tests pinned "re-parse finds marker" (with ACP-less tools) and "caller-parsed sees namespaced grant" separately, never the cross-combination. Fix: signal and tools always read from the SAME parsed object (prefer caller's, else parse once; unparseable still fails closed). Zero behavior change at the call site. Added regression test parsed=null + namespaced grant → stays plugin mode and proved it has teeth: it fails against the pre-fix function (15/16) and passes after (16/16).

Regression audit (existing behaviors on touched paths):

behavior verdict
real tagged ACP-less child (#2268 repro) → proxy-style channel preserved (server cases A–D untouched, locally green)
granted role / flat or namespaced ACP grant → pure plugin mode preserved, namespaced now more correctly seen
non-pi agents, headerless sessions untouched (gate scope pluginAgent==="pi" unchanged)
marker extraction for the log line now regex-group on parsed text (JSON escapes decoded instead of backslash-stripped bytes) — logging only
chain-checkpoint paths (#1421) unaffected (see verification above)
disclosed behavior changes (PR table rows 1–4) all intentional, all fail toward documented graceful degrade

Diff cleanliness: 5 files, every line on-purpose; en+zh CLIENTS sync present; dead-import removal is its own labeled commit; no lockfile/churn/version touch. Config surface: N/A. No kernel/ / pi-subagents/ changes.

结构化八问:

  1. 是什么问题:主 pi 会话的历史文本引用了 <active_agent name= 标记就被误判为受限子代理、降级到 proxy 通道(原生工具卡退化成 prose 回执)。
  2. 什么原因导致的:检测函数对整段原始请求体做无角色感知的字节扫描且接受空名裸前缀——判定源本身错了,不是某个 payload 的问题;次生缺口是工具能力检查不递归 Responses namespace 包裹。
  3. 影响范围与严重程度:所有「历史里出现过该标记字节」的 pi 主会话(例如读过本仓库 subagents 文档的会话);特定条件下功能降级,非崩溃。
  4. 怎么修复的:把判定源改到决策点——只在四种 wire 的系统载体里找完整标记(与 budget.ts 同载体清单)、不可解析 fail-closed、工具遍历递归嵌套 tools 数组;不做任何 payload 掩码。
  5. 是否有回归问题:逐项审过([Pi / pi-subagents] 默认内置角色的 tools 白名单阻挡 ACP:bili 自动加载成功,但上下文工具仍不可调用 #2268 真实子代理仍降级、已授权角色保持纯 plugin、非 pi 范围不动、[design] chain checkpoint step 3: carrier stamp shape on role-alternation-strict wires (anthropic/google) #1421 链检查点路径不受影响);PR 表格披露的四项行为变更均为有意且方向安全。我另发现并顺手修掉一个导出函数契约隐患(parsed=null 分支 signal 与 tools 来源不一致,调用点不可达但导出契约不成立),独立 commit + 有牙回归测试。
  6. 是否破坏缓存:不涉及——不改消息重建、摘要载体或前缀稳定性,只改 bili 内部通道分类。
  7. 是否改了设计方向:不涉及——仍是 [Pi / pi-subagents] 默认内置角色的 tools 白名单阻挡 ACP:bili 自动加载成功,但上下文工具仍不可调用 #2268 的同一降级机制,只是判定更准。
  8. 是否引入了新的配置:不涉及。

合并判定:可合(等 CI 在新 head 38d32e9 上绿)。按规矩我不合 PR,请人工合并:#2696

@ranxianglei
ranxianglei force-pushed the 2026-10-10_pi-child-marker-system-carrier branch from 38d32e9 to e30875a Compare October 10, 2026 16:44
ranxianglei pushed a commit that referenced this pull request Oct 10, 2026
… caller has no parsed body (#2696)

Review hardening for #2694: when parsed was absent the gate re-parsed the buffer for the marker but checked grants against an undefined tools field, so a direct caller passing null with a valid body containing a namespaced ACP grant would be misdemoted despite the grant. Unreachable from the sole call site (handle.ts passes null only when the same buffer already failed JSON.parse), but the exported contract now holds standalone: signal and tools always read from the same parsed object, unparseable still fails closed. Regression test pins the parsed=null + namespaced-grant combination.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Update: rebased onto current master (6064a0e7a), new head e30875af0. The CI red is NOT this PR — it's a pre-existing breakage on master, now tracked as #2699.

What happened while CI ran: master moved ~15 commits ahead. Four jobs failed (test ubuntu-22/ubuntu-24/windows-22 + real-image billing e2e), all with the same two typecheck errors in tests/image-request-budget.test.ts — a file this PR never touches:

(49,58): TS2353 'stripImages' does not exist in type 'CompressSettings & { injectTool; injectNudge }'
(111,23): TS2339 Property 'stripImages' does not exist ...

Root cause (verified against origin/master): #2688 (remove-strip-images) removed compress.stripImages/stripImagesKeepRecent from CompressSettings first; #2613 (image-request-budget test, external contributor) merged after it still referencing both keys — stale-base churn reintroducing references to a removed field. Master's own push CI is green because the push workflow doesn't run the full typecheck matrix, so it slipped through. Every open PR's matrix is red until that file is fixed.

Evidence this PR is clean on the rebased head:

Filed #2699 (来源 marker included, repro + suggested fix for the test file, to be coordinated with #2613's author).

判定不变:本 PR 本身可合 — 等 #2699 修掉后 CI 转绿即可人工合并:#2696

@ranxianglei
ranxianglei force-pushed the 2026-10-10_pi-child-marker-system-carrier branch from c7442fe to 9213b7a Compare October 11, 2026 02:08
@ranxianglei
ranxianglei merged commit 4ecd82f into master Oct 11, 2026
30 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.254-master.4ecd82f (rolling master tag).

npm install -g billion-context@0.0.254-master.4ecd82f  # 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

Development

Successfully merging this pull request may close these issues.

[Pi] 历史文档中的 child marker 将主会话误判为受限子代理,ACP 工具卡退化为正文回显

1 participant