Skip to content

[PR] fix(pi): inject acp_delegate appendix via appendSystemPrompt (order-independent, #2531) - #2536

Closed
ranxianglei wants to merge 2 commits into
masterfrom
2026-10-09_pi-delegate-append-order-independent
Closed

ranxianglei wants to merge 2 commits into
masterfrom
2026-10-09_pi-delegate-append-order-independent

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Root cause

On pi >= 0.87 the before_agent_start event exposes systemPrompt as a live getter, and returning { systemPrompt } from a handler maps to forceSystemPrompt. Pi's buildSystemPromptState() returns only the forced text when forceSystemPrompt is set — so every other extension's structured appendSystemPrompt addendum is silently dropped. Coexistence became load-order dependent: it only worked when this extension loaded last (so our getter read had already captured their append into the forced text).

The acp_delegate injection did exactly this anti-pattern in two places:

  • src/agent/pi-subagents.ts — bili native pi lane wiring
  • pi-subagents/src/index.ts — standalone factory

Both returned a full { systemPrompt } replacement built from event.systemPrompt.

Verified against real pi tarballs: 0.83.0 uses a plain data-property + consumes the returned replacement directly (safe, no drop); 0.87.1 / 0.99.0 / 1.1.0 use the getter + forceSystemPrompt path (the drop). Runtime pi is the user's install (peerDep *, not pinned), so the fix must hold across the whole range.

Fix

New helper injectSystemPromptAppendix(event, block) (in pi-subagents/src/compat.ts, exported from the package):

  • newer pi (event exposes an accessor systemPrompt): compose through event.systemPromptOptions.appendSystemPrompt (idempotent guard), return nothing → no force → all extensions' appends survive, order-independent.
  • older pi (plain data property): return the byte-identical { systemPrompt } replacement as before.

Detection is by event shape (accessor vs data property), not version number, so it holds across every published pi version. Used by both deployment sites.

Behavior change (intended)

pi before after
>= 0.87 delegate appendix injected as a forced full-prompt replacement (forceSystemPrompt) — drops other extensions' appends unless we load last delegate appendix appended to appendSystemPrompt — coexists with third-party appends, order-independent
<= 0.86 full-prompt replacement unchanged (byte-identical)

This is the fix, not a regression: the old behavior was the load-order-dependent data loss reported in #2531.

Package version

billion-context-pi-subagents bumped 0.1.3 → 0.1.4 (Pi-Subagents Boundary rule: any change under pi-subagents/src/** requires a version bump).

Tests

  • New unit suite pi-subagents/tests/system-prompt-composition.test.ts (6 cases: older string/array → replacement; newer getter → mutates append + returns undefined; idempotency; prior-append preservation; missing-options no-op).
  • New wiring test in tests/pi-subagents-wiring.test.ts driving the real host handler with a getter event (asserts prior harness append preserved + ACP_DELEGATE appended, idempotent). Existing old-path test still green.

All green on rebased master: root typecheck, pi-subagents typecheck, wiring 17/17, unit 6/6, pi-subagents suite 229/229.

Scope

References #2531 (migrated from billion-context-pi#630). This fixes the in-repo instance of the mechanism — the acp_delegate surface in the vendored pi-subagents/ and its bili host wiring. The primary reported subject, the standalone billion-context-pi package's buildAcpSystemPrompt, lives in the external repo ranxianglei/billion-context-pi (open PR #631 there) and is out of scope for this PR.

@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-09_pi-delegate-append-order-independent (a6e0935)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2763-pr.2536.6a1f3c4

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

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

继续 看上面消息

awork added 2 commits October 10, 2026 09:33
…endent, #2531)

Injecting the acp_delegate appendix by returning a full { systemPrompt }
replacement from before_agent_start forces the prompt on pi >= 0.87 (a returned
systemPrompt maps to forceSystemPrompt, whose buildSystemPromptState returns only
the forced text and silently drops every other extension's structured
appendSystemPrompt — load-order dependent, #2531 / billion-context-pi#630).

Compose through systemPromptOptions.appendSystemPrompt instead when the event
exposes the newer getter shape; keep the byte-identical replacement fallback for
pi <= 0.86. Detection is by event shape (accessor vs data property on
systemPrompt), not version number, so it holds across the whole supported range.
Idempotent per options object. Covers both deployment modes: bili's native pi
lane (src/agent/pi-subagents.ts) and the standalone factory (pi-subagents/).
…-subagents-guard needs a fresh bump; #2546 took 0.1.4, whose lockfile line drifted at 0.1.3) (#2536)
@ranxianglei
ranxianglei force-pushed the 2026-10-09_pi-delegate-append-order-independent branch from 5d83b4d to a6e0935 Compare October 10, 2026 01:40
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 继续完成本 PR 的核验与收尾。结论:已 rebase 到当前 master、修复版本冲突、CI 26/26 全绿,可合并(merge 由人执行)。

本轮做了什么

  1. 用真实 pi dist 逐一验证了修复的核心假设(不是只看代码推演)。抽取了 4 个真实版本的 dist/core/extensions/runner.js / system-prompt.js:
    • 0.83.0:before_agent_start event 里 systemPrompt 是普通数据属性(字符串)→ 走旧替换路径 ✓
    • 0.87.1 / 0.99.0 / 1.1.0:event 是对象字面量内联 get systemPrompt()(自身 accessor 属性,Object.getOwnPropertyDescriptor 可检测)+ 恒带 systemPromptOptions;handler 返回 {systemPrompt} 时映射为 currentOptions.forceSystemPrompt(runner.js:1137-1144);而 buildSystemPromptState() 在 force 存在时只返回 force 文本(if (input.forceSystemPrompt !== undefined) return { content: input.forceSystemPrompt })——证实了「force 会吞掉其他扩展的 append」这一根因。
    • ⇒ PR 的形状探测(accessor=新 pi 走 append,data prop=旧 pi 走原样替换)在全部 4 个版本上成立。
  2. Rebase 到当前 master(4fce5d3fb,含 refactor(launcher): consolidate per-client binary-resolution copies into shared helpers (#1440 P3) #2553):干净落位,无语义冲突。窗口内唯一触碰本 PR 路径的提交是 acp_delegate completion notifications are injected via pi's request-local context event, so they vanish from later requests and break stateful upstream sessions #2546(也 bump 了 pi-subagents 0.1.3→0.1.4,只动 delegate-tool.ts,与本 PR 无文件重叠)。
  3. 发现并处理版本冲突:acp_delegate completion notifications are injected via pi's request-local context event, so they vanish from later requests and break stateful upstream sessions #2546 先占了 0.1.4,rebase 后本 PR 的版本 hunk 被去重 → pi-subagents-guard(SRC_CHANGED=1 且 BUMPED=0)会挂。已补一个独立小 commit:billion-context-pi-subagents 0.1.4 → 0.1.5(pi-subagents/package.json + package-lock.json 各一行)。顺手修掉一个 master 既有漂移:acp_delegate completion notifications are injected via pi's request-local context event, so they vanish from later requests and break stateful upstream sessions #2546 bump 了 package.json 却漏了 lockfile(master 上 lock 停在 0.1.3、package.json 是 0.1.4),本次两文件统一到 0.1.5 后漂移消除。
  4. 本地定向验证(仅跑与改动直接相关的范围,全量交给 CI):pi-subagents 组件套件 230/230 通过(含新增 6 个 system-prompt-composition 用例)、宿主 wiring 测试 17/17 通过(含新增 getter-event 用例)、根 + 组件 typecheck 均干净。
  5. CI:新 head a6e09354d 上 26/26 全部 success(含 pi-subagents-guard、kernel-guard、version-guard、全部 e2e lane);PR 状态 open、mergeable: true。

结构化八问

  1. 是什么问题:pi ≥ 0.87 下,acp_delegate 的系统提示附录通过返回 {systemPrompt} 注入,该返回值在 pi 侧是 force 通道;force 生效时系统提示只渲染 force 文本,其他扩展经 appendSystemPrompt 贡献的内容被静默丢弃——是否丢内容取决于扩展加载顺序。
  2. 什么原因导致的:注入机制选错了通道。旧代码对新老两种 pi 形状都走「返回整段替换」;新 pi 的结构化提示本来就有可组合的 append 通道(systemPromptOptions.appendSystemPrompt),force 通道语义是「整体接管」。
  3. 影响范围与严重程度:仅 pi ≥ 0.87 且同时存在其他扩展贡献 appendSystemPrompt 的场景(如 Gentle Shell harness 类);属特定条件下的静默数据丢失(别人的提示段消失),不是完全不可用。
  4. 怎么修复的:按事件形状分流(自身属性描述符判定 accessor/data-prop):新形状改 mutate appendSystemPrompt(幂等、保留已有 append、我们的附录排最后)且不返回任何东西(不触发 force);旧形状保持逐字节一致的替换回退。两个部署点(宿主接线 + 独立工厂)共用同一 helper,并补了钉死两种形状的回归测试(断言有牙:若回退成 getter 场景下仍返回替换,wiring 测试会红)。
  5. 是否有回归问题:审过既行为——旧 pi(≤0.86)路径逐字节不变(单测钉住 string/array 两形);新 pi 的行为变化即本 fix 本身(见下方披露);自动更新/dsh/lockfile/systemd/env 模板/消息重建等高危域均未触碰;导出面只增不改。另发现并顺手修复 master 的 lockfile 版本漂移(acp_delegate completion notifications are injected via pi's request-local context event, so they vanish from later requests and break stateful upstream sessions #2546 遗留,见上)。
  6. 是否破坏缓存:不涉及——未触碰前缀缓存键、重试、增量游标或去重策略;只是换了承载附录的字段。
  7. 是否改了设计方向:无架构转向,helper + 双部署点结构不变,仅新 pi 上的注入通道从 force 换成 append 组合,即 [Bug][pi] compat: returned forceSystemPrompt in before_agent_start drops other extensions' appendSystemPrompt when load order flips #2531 提出的方向本身。
  8. 是否引入了新的配置:无。

行为变更披露(有意变更):pi ≥ 0.87 上,acp_delegate 附录注入方式由「强制替换系统提示(会丢弃其他扩展的 append)」变为「经 appendSystemPrompt 组合(保留所有 append,本插件附录排最后)」;pi ≤ 0.86 行为逐字节不变。这是本 PR 要修的 bug 本身,非漂移。

判定:可合并——rebase 至最新 master、guard 冲突已解、CI 全绿、mergeable。请 owner 自行 merge:#2536

@ranxianglei

Copy link
Copy Markdown
Owner Author

(ai: zhipuai-lb/glm-5.3)

孤儿票清理:空挑验证——分支对 master 已无独立提交(内容已全部落地)。关单;误判请 reopen。

@ranxianglei ranxianglei changed the title fix(pi): inject acp_delegate appendix via appendSystemPrompt (order-independent, #2531) [PR] fix(pi): inject acp_delegate appendix via appendSystemPrompt (order-independent, #2531) Oct 24, 2026
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