Skip to content

fix: emit /acp panel as persistent custom message and strip it from the proxy model context (#359) - #360

Merged
ranxianglei merged 2 commits into
masterfrom
2026-08-30_acp-panel-persistent-strip
Sep 5, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-08-30_acp-panel-persistent-strip

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What

The pi plugin's /acp command rendered the status panel through ctx.ui.notify(), which web hosts like agegr/pi-web show as a transient toast instead of a persistent transcript entry (same class as billion-context-pi#255, fixed there in PR #256).

This is a two-part fix — a plugin-only port is not safe in proxy mode:

1. Plugin (src/agent/pi.ts)

/acp now emits pi.sendMessage({ customType: "bili-acp-status", content, display: true }) when sendMessage is available, falling back to ctx.ui.notify() for older pi. sendMessage produces a persistent custom_message session entry that TUI and web hosts render in the transcript.

2. Proxy (src/acp-panel.ts + the three prepare paths)

Strip panel user-messages from the request before the *ToCore conversion, so the panel never enters the compression state / recent zone and is never forwarded to the model.

Why the proxy side is required: in extension mode (billion-context-pi) the ACP transform runs inside pi and filters acp-status panels out of the sent view by customType. In proxy mode pi's convertToLlm projects every custom message as an ordinary user message and drops the customType — so the panel arrives as indistinguishable real conversation content and would sit in the recent zone, forwarded to the model on every turn. The proxy generates the panel itself (handlePluginStatus → buildStatusPanel), so it matches its own content signature (the buildStatusPanel box — ╭ border + ACP Context Analysis title — or the plugin's renderAcpStatus fallback header 📊 ACP status) rather than the type. Applied in prepareAnthropic / prepareOpenai / prepareResponses.

Files

  • src/acp-panel.ts (new): isAcpPanelText + stripAcpPanelMessages / stripAcpPanelResponsesInput. Plain-text-only matching — mixed/multimodal, assistant, and function-call items are never touched; a message that merely quotes a panel is preserved.
  • src/server.ts: strip calls wired into the three prepare paths (before projection) + import.
  • src/agent/pi.ts: sendMessage on ExtensionAPI + /acp handler uses it with notify fallback.
  • tests/acp-panel.test.ts (new): 10 tests against the real buildStatusPanel output + fallback + edge cases.
  • tests/plugin-agent.test.ts: 3 tests for the sendMessage path (panel string, renderAcpStatus fallback, sendMessage throws → notify).

Pre-flight

  • npm run typecheck — clean
  • npm test — 842 pass, 0 fail
  • npm run build — success

E2E note: tests/e2e/e2e-codex.test.ts could not be run here (no codex binary, no local upstream). It drives codex, not pi, so it never injects a panel — the strip is a no-op for those requests. The full unit suite (all anthropic/openai/responses round-trip + stream tests) plus the new tests already verify the strip is a no-op for legitimate messages and correctly removes panels.

Closes #359.

@github-actions

github-actions Bot commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-08-30_acp-panel-persistent-strip (b96fce5)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-360

Each push to this PR publishes a new version under the pr-360 npm tag.

Option B — Download artifact

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

This comment is automatically updated on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · vllm-qwen/qwen3.8-27b

[bot] Reviewed PR #360 (0a0acd2, branch 2026-08-30_acp-panel-persistent-strip). LGTM — verified locally, cross-checked the plugin-side call against the real pi API, no blocking issues. Two minor notes at the end.

Local verification (ran on the branch):

  • npm run typecheck — clean
  • npm test — 842 pass, 0 fail (matches your claim exactly)
  • npm run build — tsup success
  • Wiring confirmed in src/server.ts: stripAcpPanelMessages at :1236 (before anthropicToCore :1242) and :1333 (before openaiToCore :1343); stripAcpPanelResponsesInput at :1481 (after normalizeResponsesMessageItems/dropWhitespaceResponsesMessages, before responsesToCore :1499). All in-place, each with an info log.
  • Audited the remaining *ToCore/state-mutating sites: prepareCountTokens (:1670) never reassigns session.state (panel would only ride the forwarded count_tokens payload — harmless, arguably correct for the client's own view), and prepareResponsesCompact (:1707) is gated on isCodexClient (:1733) — codex never emits panels. So every pi-reachable compression-state path is covered.

Cross-check against the real pi API (earendil-works/pi):

  • The extension factory object does expose sendMessage(message, options) at runtime (packages/coding-agent/src/core/extensions/loader.ts:357 → runtime.sendMessage), and the message shape { customType, content, display } matches Pick<CustomMessage<T>, "customType" | "content" | "display" | "details">.
  • Important safety property confirmed: triggerTurn defaults to false (agent-session.ts:1449 sendCustomMessage + docs/extensions.md) — with no options the custom message is appended to the session and no LLM turn is triggered. So /acp can't accidentally fire a model call with the panel as a prompt.
  • The typeof pi.sendMessage === "function" guard + try/catch → notify fallback is the right shape: the loader's assertActive() throws when the runtime is inactive, so the catch covers torn-down sessions, and older pi without sendMessage falls through cleanly.

Minor notes (non-blocking):

  1. CHANGELOG.md — repo convention is an entry under [Unreleased] per content PR (every recent merge has one); this PR doesn't touch it. Suggest adding a ### Fixes entry before merge.
  2. E2E — AGENTS.md asks for tests/e2e/e2e-codex.test.ts before merging request-pipeline changes (server.ts). I couldn't run it here either (no codex binary, no local upstream). Your no-op argument for codex is sound (the strip only matches user messages with the panel signature; codex never emits one), but consider triggering the manual-dispatch CI e2e workflow before merge, or explicitly accepting the unit coverage as sufficient.

Residual risk, for the record: the content-signature strip assumes pi projects custom-message content verbatim as the user message text (no prefix/marker). If a future pi version wraps it, the strip silently stops matching and the panel leaks to the model context. The two pinned signatures (real buildStatusPanel box + renderAcpStatus header) make this unlikely, and the tests pin the real output — just the one thing to re-check if pi's convertToLlm projection ever changes.

@ranxianglei
ranxianglei force-pushed the 2026-08-30_acp-panel-persistent-strip branch 2 times, most recently from e6e2da2 to 3273a9c Compare September 1, 2026 13:31
@ranxianglei

Copy link
Copy Markdown
Owner Author

冲突了 另外为啥新搞了一个acp panel?

@ranxianglei
ranxianglei force-pushed the 2026-08-30_acp-panel-persistent-strip branch 2 times, most recently from e9d7136 to 2e5898f Compare September 3, 2026 15:24
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · qwen3.8-27b

[bot] 🏷 两件事都处理了:

1. 冲突已解决 — rebase 到最新 master(v0.1.78,7bd4534),force-push 为 2e5898f。GitHub 现在显示 PR mergeable(无冲突;mergeable_state=blocked 只是 CI checks 还在跑)。

  • 唯一的文本冲突在 src/server.ts import 块:master 往 codex-compact.js 的 import 里加了 codexCompactGatePre,PR 这边加了 acp-panel.js 的 import —— 两边都保留即可。
  • 三个 strip 调用点相对 master 新代码的位置都核实过:anthropic :1469→anthropicToCore、openai :1643→openaiToCore(master 新增的 system-hoisting 在 ToCore 之后,不受影响)、responses :1801→responsesToCore(在 whitespace-drop 之后、compaction-trigger 检测之前,codex-only 路径不受影响)。master 新的 image-tokens clamp/preflight 改动与 strip 不在同一区域,无交互。
  • rebase 后完整 pre-flight:npm run typecheck 干净;npm test 1014 pass / 1 fail——那个 fail 是 tests/launcher.test.ts 的 resolveClientCommand: codex/claude resolve to themselves,与 PR 无关:我在干净的 master 上跑同样失败,原因是本机 PATH 里有 /usr/bin/codex,函数解析成绝对路径而测试断言裸名(环境相关测试,CI runner 上没有 codex 所以一直是绿的)。小问题,只报告不开 issue。
  • 提醒:这个仓库现在几小时一个 release,如果合并前又落了新版本且动了 server.ts,可能还要再 rebase 一次。E2E(e2e-codex)我这边仍跑不了(无本地 upstream),建议合并前走 manual-dispatch CI e2e。

2. 为什么单独搞了个 src/acp-panel.ts —— 不是新功能面,就是放代码的位置选择:

  • strip 逻辑跨两种 wire 格式(anthropic/openai 的 messages[] + Responses 的 input[])+ 内容签名匹配,是一坨内聚的纯函数(81 行、零依赖)。server.ts 已经 3200+ 行,不想再往里堆。
  • 仓库里已有完全相同的先例:同类的 ingress 变换 dropWhitespaceResponsesMessages / normalizeResponsesMessageItems 就住在独立模块 src/loop/adapter-responses.ts,server.ts 只是 import 后调用 + 打日志。acp-panel.ts 是同一个形状(纯函数、in-place splice、返回 removed 计数)。
  • 可测性:tests/acp-pin.test.ts 不对——tests/acp-panel.test.ts(10 个用例)直接 import 该模块,并用真实的 acp-kernel buildStatusPanel 输出做 fixture;如果内联进 server.ts,测试就得拖着整个 server 装配。
  • 对外暴露面没有变大:没有新配置项、没有新端点,server.ts 里就一行 import + 三处调用。

如果你更希望它并进 adapter-responses.ts 或留在 server.ts,说一声我就挪(纯移动,无逻辑变化)。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · qwen3.8-27b

[bot] 🏷 更正上一条里的笔误:测试文件就是 tests/acp-panel.test.ts(10 个用例,直接 import src/acp-panel.ts,用真实 buildStatusPanel 输出做 fixture)。

@ranxianglei

Copy link
Copy Markdown
Owner Author

又冲突了 麻烦解决下吧

@ranxianglei
ranxianglei force-pushed the 2026-08-30_acp-panel-persistent-strip branch from 2e5898f to 3befcbb Compare September 5, 2026 02:13
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · qwen3.8-27b

[bot] 🏷 又解决了:rebase 到最新 master(v0.1.84,1365537),force-push 为 3befcbb。GitHub 已显示 mergeable(无冲突;blocked 只是 CI checks 还在跑)。

这次 master 侧动到了 PR 的三个文件(v0.1.78→v0.1.84 之间 ~30 个 merge),实际冲突只有一处:

rebase 后 pre-flight:npm run typecheck 干净;npm test 1059 pass / 1 fail——仍是那个与 PR 无关的环境相关 launcher 测试(干净 master 上同样失败,本机 PATH 有 codex 所致);npm run build 成功。

这个仓库现在基本每小时都有 release 落 master,合并前如果又冲突了喊我一声就行(或者我可以在你准备 merge 前再 rebase 一次保证干净)。E2E 老话重提:我这边没有本地 upstream 跑不了 e2e-codex,建议合并前走 manual-dispatch CI e2e。

…he proxy model context (#359)

The pi plugin's /acp command rendered the status panel through
ctx.ui.notify(), which web hosts like agegr/pi-web show as a transient
toast instead of a persistent transcript entry (same class as
billion-context-pi#255).

Two-part fix:

1. Plugin (src/agent/pi.ts): /acp now emits
   pi.sendMessage({ customType: "bili-acp-status", content, display: true })
   when sendMessage is available, falling back to ctx.ui.notify() for
   older pi.

2. Proxy (src/acp-panel.ts + the three prepare paths): strip panel
   user-messages from the request before the *ToCore conversion, so the
   panel never enters the compression state / recent zone and is never
   forwarded to the model. pi's convertToLlm projects every custom
   message as an ordinary user message and drops the customType, so the
   proxy matches its own content signature (the buildStatusPanel box or
   the renderAcpStatus fallback header) instead of the type.

A plugin-only port would leak every panel into the model's context; the
proxy-side strip is what makes the persistent custom message safe.
@ranxianglei

Copy link
Copy Markdown
Owner Author

rebase 到当前 master(1502287,含 #540 reasoning_content + #542 SSE decoder + #543 内容落地),force-push a8c78a1。

本次唯一冲突仍是 src/agent/pi.ts 的 ExtensionAPI 类型(master 的 setModel? × 本 PR 的 sendMessage?),两个可选成员都保留。三个 strip 调用点位置核实:anthropic :1572→ToCore :1578、openai :1748→:1758、responses :1906→:1925,均在投影之前;与 #540/#542 改动无区域重叠。

预检:typecheck 干净;build 成功;npm test 1042/1040(2 个失败是已知环境项——测试进程继承 live BILLION_CONTEXT_PROXY,unset 后 plugin-agent.test.ts 35/35 全过,与 PR 无关)。

@ranxianglei
ranxianglei force-pushed the 2026-08-30_acp-panel-persistent-strip branch from 3befcbb to a8c78a1 Compare September 5, 2026 02:43
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · qwen3.8-27b

[bot] 🏷 收到,我独立复核了一遍(不直接采信报告,对仓库实际状态做了验证):

  • master tip = 1502287 ✓,PR 分支 tip = a8c78a1,其 parent 正是 1502287 ✓
  • 5 文件 diff 完整(+344/-1,与之前各轮一致)✓
  • 三处 strip 位置逐行核对无误:anthropic :1572→ToCore :1578、openai :1748→:1758、responses :1906→:1925,均在投影之前;count-tokens(:2114)和 codex forgeBody(:2190)两处 ToCore 按设计保持不 strip ✓
  • ExtensionAPI 三个可选成员共存确认:registerProvider?(:44)、setModel?(:50)、sendMessage?(:54) ✓
  • GitHub API:head a8c78a12d / base 150228707,mergeable=True,mergeable_state=blocked(仅 CI checks 未出结果)

合并前剩两件事:① CI checks 转绿;② 建议跑一次 manual-dispatch CI e2e(e2e-codex;我这边没有本地 upstream 跑不了,strip 对 codex 请求是 no-op 但这是 AGENTS.md 对 server.ts 改动的要求)。都齐了就可以 merge。

@ranxianglei

Copy link
Copy Markdown
Owner Author

CI 之前那颗失败是既有 Windows 老鼠屎,与 PR 内容无关:findFreePort: returns preferred when it is free 随机挑 50000–50999 断言空闲,而 Windows 临时端口范围 49152–65535 覆盖该区间,runner 上无关出站连接可碰巧占用(w22 绿 / w24 红即为概率性证据)。已在 b96fce5 改为确定性写法:listen(0) 向 OS 要端口 → close 等待回调 → findFreePort 断言偏好该端口。本地 3 连跑 99/99。

现在 6/6 全绿(ubuntu 22/24 + windows 22/24 + build-artifact + version-guard),可合并。

@ranxianglei
ranxianglei deleted the 2026-08-30_acp-panel-persistent-strip branch October 15, 2026 05:32
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 plugin /acp panel via ctx.ui.notify() — transient toast in web hosts (same class as billion-context-pi#255)

1 participant