Skip to content

fix: strip redundant in-place acp_summary for plugin-mode folds (#1567) - #1570

Merged
ranxianglei merged 3 commits into
masterfrom
2026-09-28_plugin-mode-summary-strip
Sep 29, 2026
Merged

ranxianglei merged 3 commits into
masterfrom
2026-09-28_plugin-mode-summary-strip

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What

stripKernelSummaries suppressed a block's in-place acp_summary_<blockId> anchor ONLY when an inbound message carried toolCallId === block.compressCallId. Folds executed through POST /__bili/plugin/tool mint a synthetic plugin_<ts> callId that the client can never echo back — yet the client's own re-sent compress tool_use+tool_result pair rides inbound history on every turn and IS the summary carrier by contract (README "Two compression modes"). The strip predicate was therefore unsatisfiable in plugin mode: every post-fold body carried the summary twice — the in-place [Compressed conversation section] anchor plus the pair — one extra full copy of every active summary per turn, compounding linearly with fold count.

Fix

Strip on the plugin_ callId prefix instead of the echo match (isPluginFoldCallId, src/plugin.ts — minted at exactly one site, so the prefix marks precisely the blocks whose client necessarily holds its own pair):

if (isPluginFoldCallId(b.compressCallId) || messages.some((m) => m.contentType === "tool-call" && m.toolCallId === b.compressCallId)) {

Why block-level prefix, not the issue's session-level gate

The issue suggested stripping unconditionally when the session is bound to a plugin agent. The block-level predicate is strictly safer:

  1. Preflight invariant (bili 模式切换模型会报错上下文满 #247) — preflight blocks have no compressCallId (no tool call exists), so their in-place anchor is the ONLY carrier; the existing !b.compressCallId guard keeps them intact under either approach.
  2. Mode-transition edge case — a session that did wire-loop compresses in proxy mode (ephemeral acp_loop_* pairs the client never re-sends) and later flips to plugin mode would, under session-level stripping, lose those earlier blocks' only carrier. Block-prefix stripping leaves them on the id-match logic, unchanged.
  3. Proxy/wire-loop behavior unchanged — their callIds don't carry the prefix, so the existing match logic governs them exactly as before.

Interaction with #1548 (master)

Rebased onto current master. #1548 changed the ephemeral-loop call site to strip against inbound-derived messages only (so proxy-mode round-2 views keep the in-place summary). The two compose: loop blocks keep #1548's survival semantics (prefix doesn't match acp_loop_* ids); plugin blocks are stripped at every call site including that one — correct, since a plugin-mode client always resends its own pair regardless of which path built the view.

Behavior change (old → new)

Old: in plugin mode, the kernel's in-place [Compressed conversation section] anchor rode every post-fold outbound body alongside the client's own compress pair (double carrier; contradicted README's "the agent's view never renders the kernel's acp_summary").
New: the anchor is stripped from all post-fold bodies for blocks created via the plugin tool API; the client's own pair remains the sole visible carrier (its visibility still governed by kernel KEEP_LAST_ORPHANED pruning). Proxy-mode bodies are byte-identical to before (id-match logic untouched).

Wire-shape change → human merge per §7.4.

Regression test

tests/plugin-protocol.test.ts — the existing plugin tool-API fold test now asserts the post-fold upstream body contains no [Compressed conversation section] — <topic> anchor while the orphaned client pair (toolu_c_1) stays visible. Teeth verified both ways on this exact base: with master's src/server.ts the assertion fails; with the fix it passes.

Pre-flight (rebased head d6d2246, base 5ffafae)

  • npm run typecheck — clean
  • npm test — 3046 tests, 0 fail, 12 skipped
  • npm run build — success

Closes #1567

@github-actions

github-actions Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-28_plugin-mode-summary-strip (fa6a7f6)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.1611-pr.1570.b557309

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-pr1570.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

[bot] 🏷 Reviewing PR #1570 now — pulling the branch (d6d2246f), checking the strip-predicate change against all stripKernelSummaries call sites, verifying the regression-test teeth both ways, and re-running pre-flight on the current-master base (master has moved past the PR's base since #1564 landed). Full conclusion in a follow-up comment.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review complete — mergeable (human merge per §7.4, wire-shape change). I rebased the branch onto current master and pushed; CI is fully green on the new head. Details below.

What I did to the branch

The PR base had gone stale (declared base 5ffafae7; master moved to 1c60633c with #1564). Per §7.2 I rebased before claiming anything:

  • Clean rebase, zero conflicts — docs: sync EN README to zh structure, add FAQ (round 3) #1564's one-line comment edit in src/server.ts (README → TECHNICAL-NOTES.md reference) sits adjacent to, not inside, the PR's comment addition, so they compose correctly (verified the resulting comment block reads coherently at src/server.ts:2726-2742).
  • Force-pushed rebased head 4b1827cd to 2026-09-28_plugin-mode-summary-strip (feature branch only — nothing ever touches master).
  • Pre-flight on the rebased head, all local: npm run typecheck clean · npm test 3046 tests, 0 fail, 12 skipped (matches your claim exactly) · npm run build success.
  • CI on 4b1827cd: all 13 checks green (test matrix ubuntu/windows × node 22/24, e2e, hermetic registry e2e, version-guard, build-artifact, all four real-client lanes, codex overlay).

① Original issue — direction check (passed)

#1567's claim is real and I reproduced it rather than trusting the description: with master's src/server.ts swapped under the PR's test file, the new assertion fails with exactly in-place acp_summary carrier must be stripped in plugin mode (#1567) (15/16 pass, 1 fail); with the fix, 16/16. Mechanism confirmed line-by-line: stripKernelSummaries keyed on m.toolCallId === b.compressCallId, while handlePluginTool mints plugin_<ts> at src/plugin.ts:982 — the only mint site in the repo — which the client can never echo, making the predicate structurally unsatisfiable for plugin folds.

Root cause vs symptom: the fix lands at the decision point (the redundancy predicate), not a payload mask — if the same root cause surfaced in another shape tomorrow (another synthetic-id path), the prefix contract still holds because it's tied to the mint site, not to a byte pattern in transit. And the deviation from the issue's suggested session-level gate is the right call: the mode-transition edge is real — a session that did wire-loop compresses in proxy mode then flips to plugin mode would lose those earlier blocks' only carrier under unconditional session-level stripping; block-prefix leaves them on id-match logic, unchanged.

② Regression inventory (existing behaviors on touched paths)

  1. Predicate change reaches all 8 stripKernelSummaries call sites — prepareAnthropic/prepareOpenai/prepareGoogle/prepareGoogleCountTokens/prepareResponses/prepareCountTokens/prepareResponsesCompact + forward→refreshFolded (the codex 版本压缩后缓存大幅度降低问题 #1548 site). For every non-plugin_ callId (acp_loop_* ephemeral ids, client-echoed wire-loop ids, absent preflight ids) isPluginFoldCallId is false and the old id-match path executes byte-identically → proxy mode unchanged, including codex 版本压缩后缓存大幅度降低问题 #1548's round-2 survival semantics.
  2. Preflight invariant (bili 模式切换模型会报错上下文满 #247): the !b.compressCallId guard is untouched — preflight anchors remain the sole carrier. Preserved.
  3. callId minting: string value unchanged (plugin_ + base36 timestamp), only extracted into the exported constant PLUGIN_FOLD_CALLID_PREFIX. No MCP tool-call/result wire change.
  4. refreshFolded (codex 版本压缩后缓存大幅度降低问题 #1548 site): prefix-stripping there is inert in proxy mode (no prefix match) and unreachable in plugin mode (the loop doesn't run — proxy injects no compress tools in plugin mode). Preserved.
  5. Untouched: kernel id-never-reused contract, persistence format/version, config surface (new symbols are additive exports only), MITM/CA/security paths.
  6. Intended behavior change (re-confirming your disclosure): plugin-mode post-fold bodies drop the in-place [Compressed conversation section] anchor; the client's own pair becomes the sole visible carrier (visibility still governed by kernel KEEP_LAST_ORPHANED pruning). One edge worth naming explicitly: if a plugin client ever aggressively evicts its own compress pair from local history, the anchor no longer exists as a fallback net. That is the documented plugin-mode contract ("the agent's view never renders the kernel's acp_summary") and content stays recoverable via decompress/retrieve — accepted as intended, and your PR text discloses the old→new change.

③ Code review

Verified the full chain the fix hinges on, end to end:

handlePluginTool mints plugin_<ts> → executeProxyTool(tool, args, ctx, callId) → parseCompressInput(input, callId) (src/compress-tool.ts:305) → kernel parseCompressArgs attaches the callId to every parsed range (acp-kernel dist/index.js:774: if (callId) for (const r of ranges) r.compressCallId = callId) → core.applyCompression writes it onto each created block (index.js:5529: compressCallId: input.spec.compressCallId). So the prefix marks precisely and only blocks born from the tool API.

Also confirmed every plugin lane funnels through that one endpoint — src/agent/opencode-native.ts:472: "Tool forwarder (tests inject; runtime POSTs /__bili/plugin/tool)" — so no plugin-mode fold escapes the prefix, and the invariant "prefix ⇒ client necessarily holds its own pair" holds by construction (the mint site is exactly where the result returns into the client's history).

Two-way safety of the predicate: even if some client did echo a plugin_<ts> id inbound, stripping would still be correct (pair present inbound ⇒ anchor redundant). The helper is pure, handles undefined, no as any, no console.error.

④ Test validity

Real teeth, verified both ways on the rebased head (not taken on faith): master server.ts → fails on exactly the new assertion; fix → passes. The assertion pins both halves of the contract — absence of the anchor and presence of the orphaned client pair (toolu_c_1, KEEP_LAST_ORPHANED=2) — so it catches regressions in either direction (over-stripping the carrier or the anchor creeping back). It rides the existing real-HTTP plugin harness, not a mock. Coverage note: this pins the Anthropic wire; the predicate itself is wire-agnostic (one shared function over shared block state across all prepare* paths), and #1571's four-wire pinning closes the remaining gap — see follow-up below.

⑤ Diff cleanliness

3 files, +32/−4, every line on-purpose. No whitespace churn, no generated/lock-file noise.

Follow-up after merge

PR #1571 carries the cross-wire pins with its intended-strip assertions skip-gated behind BILI_PLUGIN_STRIP_FIXED=1 (off by default). Once this lands, that gate should be flipped on so the four-wire coexistence behavior is actively asserted instead of pinned-as-is.


中文摘要:修复了插件模式下压缩摘要双重载体的问题——POST /__bili/plugin/tool 铸造的合成 plugin_<ts> callId 使 stripKernelSummaries 的 id 回显匹配永远不成立,in-place 锚点与客户端自身的 compress 对每轮各带一份完整摘要;现改为按 plugin_ 前缀(唯一铸造点)识别插件折叠块,代理模式所有路径逐字节不变、preflight(#247)与 #1548 语义保持;已重基到当前 master(4b1827cd)并完成双向牙齿验证、typecheck/3046 测试/build 与 CI 13 项全绿,属于线格式变更按 §7.4 需人工合并,可以合并。

ework-agent and others added 2 commits September 29, 2026 13:04
stripKernelSummaries keyed suppression on an inbound tool-call matching
block.compressCallId, which is unsatisfiable for folds executed through
POST /__bili/plugin/tool: they mint a synthetic plugin_<ts> id the client
can never echo back, even though the client's own re-sent compress pair
rides inbound history every turn and IS the summary carrier by contract.
Result: the kernel's in-place [Compressed conversation section] anchor rode
every post-fold body alongside the pair — one extra full copy of every
active summary per turn, contradicting README 'Two compression modes'.

Strip on the plugin_ callId prefix instead of (not in addition to) the
echo match: the prefix is minted at exactly one site, so it marks precisely
the blocks whose client necessarily holds its own pair. Preflight blocks
(no compressCallId) keep skipping — no tool call exists for them, so their
in-place anchor remains the only carrier (#247). Proxy/wire-loop blocks
keep the existing id-match logic unchanged.

Regression test: the plugin tool-API fold test now asserts the post-fold
body carries no '[Compressed conversation section] — <topic>' anchor while
the client's orphaned pair stays visible (KEEP_LAST_ORPHANED=2).
…und (#1567 hardening)

The unconditional prefix strip could lose a summary outright: if the client
prunes or fails to re-send its compress pair, the in-place anchor was
stripped with no carrier left (zero carriers). Restore the old predicate's
self-verification — strip per-block only when that exact fold's pair is
present in the (post-prepare) inbound history, matched by tool name plus the
folded range quoted in the call args (flat {startId,endId} or
{content:[...]}). hideConsumedCompressCalls (KEEP_LAST_ORPHANED) runs before
the strip, so a kernel-hidden pair also keeps its anchor — fail-safe in both
directions.

Matrix (#1571) re-pinned to the intended behavior (0 carriers while pairs
ride; exactly 2 pair quotes) and supplemented with two hardening scenarios:
client drops a re-sent pair → anchor re-carries, bytes stable; 3rd concurrent
fold → oldest pair pruned by KEEP_LAST_ORPHANED, oldest anchor kept.
@ranxianglei
ranxianglei force-pushed the 2026-09-28_plugin-mode-summary-strip branch from 4b1827c to efb780a Compare September 29, 2026 06:31
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.

plugin mode: in-place acp_summary never stripped when the client's own compress pair rides inbound — double summary carrier on every post-fold turn

1 participant