Repository navigation
Description
来源: #453 (#453) 分析时发现;owner 在 #453 分阶段方案确认中明确要求「另开 issue 单独跟踪」;PR #467 (#467) 有意未包含此项。
Background
#453: in plugin mode (bili pi) bili trusts the ACP agent to run compress. When a weak model ignores the nudge, the payload grows past the window and the upstream 400s on input+output (vLLM rejects an oversized total instead of clamping) — then the client falls back to native compaction and loses all working state.
PR #467 ships two host-side backstops that cover the common path:
- Clamp the outgoing output budget —
max_tokens/max_completion_tokens/max_output_tokenscapped towindow − estimated_input − margin, so input+output can no longer exceed the window on request-rebuilding upstreams (both modes). - Escalate the nudge — force-inject every turn once usage ≥
EMERGENCY_NUDGE_ESCALATION_PCT(0.70), filling the sub-0.75 cadence-silent zone (kernel's pressure branch ≥0.75 already force-injects; verified againstdecideNudge).
This issue tracks the third line of defense deliberately NOT included in #467: proxy-side emergency truncation in plugin mode.
Why it is deferred (carrier-consistency risk)
The existing preflight/self-heal truncation path builds tool-call-less blocks whose acp_summary is re-voiced as a user message by systemToUser — that is proxy-mode semantics, where the proxy owns compression and the ephemeral tool call never enters the client's history.
In plugin mode the contract is inverted: the ACP-native agent OWNS compression — the compress call + result live in the agent's own re-sent history, and the wire carrier is the tool call itself (proxy suppresses tool/nudge injection of its own; the agent's view never renders the kernel's acp_summary). If the proxy unilaterally compresses/truncates ranges the agent has not compressed:
- the agent's next re-send may contain the unfolded original range → the proxy's block state and the agent's view diverge (double-compress, orphan blocks, or a summary the agent never produced);
- summary carriers become inconsistent within one conversation (agent-produced tool-call summaries vs proxy-produced user-voiced
acp_summary), which strict backends (e.g. SGLang's single-system-at-index-0 constraint, cf. [Bug] ACP: ≥2 compressed blocks render as multiple system messages → SGLang (vLLM-compatible) upstreams return 400 #377) do not tolerate well.
So this needs a design pass before any implementation, e.g.:
- can the proxy restrict emergency truncation to ranges the agent provably cannot claim (already-blocked / protected-only zones)?
- or notify/reserve the ranges in the agent's ref space so its next re-send stays consistent?
- what happens to the agent's pending compress calls that targeted now-truncated refs?
Expected shape (for discussion, not a commitment)
- Last resort only: engage when input alone nearly fills the window (the territory where the output-clamp floor no-ops) and preflight/self-heal did not already fire.
- Must preserve consistency between the agent's re-sent history and the proxy's block/state store.
- Strictly lower priority than fix(#453): hard backstop for plugin-mode context overflow #467's backstops: the clamp guarantees no more input+output 400s, the escalation nudge gives the agent repeated chances to self-compress; truncation is the final fallback for models that ignore both.
References
- plugin mode: no hard backstop when the agent ignores the compress nudge — context grows past the window and upstream 400s #453 — root problem, repro numbers, staged plan confirmed by owner
- fix(#453): hard backstop for plugin-mode context overflow #467 — PR implementing backstops 1+2 (this item split out per owner direction)
Activity
ranxianglei commentedon Sep 2, 2026
[bot] 🏷 Design pass complete. I traced the actual code paths (master@60e76f6) instead of taking the framing at face value, and one finding reframes the whole issue.
Ground truth: plugin-mode proxy-side compression ALREADY EXISTS
The issue frames "proxy-side emergency truncation in plugin mode" as a new third line of defense to design. It already ships today — via
preflightCompressIfNeeded, which runs unconditionally in both modes:src/server.ts:1273— called insideif (!countTokens && !responsesCompact), with nopluginModegate.src/server.ts:2180-2268— takes nopluginModeparam; fires purely ontokenCount >= limitwheretokenCount = max(lastInputTokens, estimateCoreMessages(processedMessages))(line 2200). On fire it callspreflightCompress→applyRanges(...)(src/preflight.ts:321), creating persistent, tool-call-less blocks.src/server.ts:1338(comment): "Blocks created without a tool call (preflight compression…) have NO other carrier… it must survive."src/server.ts:1549+1551—stripKernelSummaries+systemToUserrun inprepareOpenairegardless of mode (pluginModeonly gatesinjectTools, line 1518). A preflight block has nocompressCallId, so itsacp_summaryis not stripped (server.ts:1349-1357only strips carried summaries) and is re-voiced as a user message bysystemToUser.Corroborated by
tests/plugin-nudge-injection.test.ts:13-19: plugin mode's only backstop is literally described as "only preflight at the hard limit."So the exact carrier-inconsistency the issue warns about — a proxy-created, agent-unproduced, user-voiced
acp_summaryover a range the agent still holds unfolded — is live behavior whenever preflight fires in plugin mode, not a hypothetical cost of a new feature. (The README table cell "Preflight blocks (no tool call)? No" for plugin mode appears inaccurate.)What the hazard actually is (softer than stated)
Tracing the fold mechanics, the divergence is milder than the OP suggests:
src/orphan-gc.ts:43), and R's ids are still present so the block stays anchored.systemToUser.Net: existing plugin-mode preflight is "works but degrades fidelity," not "broken."
The residual gap after #467
#467 is still open (not merged); its clamp kills the common input+output 400. The only input-reduction territory left is input alone ≈ full window (where the clamp's
OUTPUT_CLAMP_FLOOR=1024 no-ops). Two sub-problems sit there:estimateCoreMessagesexcludes system+tools (~10-20k). fix(#453): hard backstop for plugin-mode context overflow #467 fixed that undershoot for the clamp (estimateInputTokens) but not for preflight's trigger (src/server.ts:2200still usesestimateCoreMessages). So the mechanism that should catch input-alone overflow still fires late or misses in exactly this territory.How deep is the tail? Input alone must approach the entire window (not just window−maxOutput) before the clamp can't save us — much rarer than the #453 repro.
Recommendation (clear bottom line, not a menu)
estimateInputTokens(or folding system+tools into the estimate atsrc/server.ts:2200). This makes the backstop that already exists reliably engage for input-alone overflow, in both modes, minimal risk. Arguably a completion of plugin mode: no hard backstop when the agent ignores the compress nudge — context grows past the window and upstream 400s #453/fix(#453): hard backstop for plugin-mode context overflow #467 (they fixed the clamp side of the same undershoot and left this side).Happy to (a) open the trigger-fix as a small PR (standalone, or folded into #467 before it merges), and (b) correct the README cell. Say which way you want to take it and I'll go.
ranxianglei commentedon Sep 6, 2026
Fixed in PR #576 (branch
2026-09-06_preflight-wire-overhead, commit 8e0a6ad).Root cause
The preflight trigger and both post-fold fit gates estimated the payload with
estimateCoreMessages(messages only). But the billed input also carries the system prompt and tool definitions — including the proxy-injected compress system prompt (~1.9k tokens) and the ACP tool definitions (~0.5k), plus whatever the client sends (~10-20k on agent clients). So the trigger fired late: the text alone "fit" the window while the real input already overflowed it, guaranteeing a 400 from the upstream. #467 fixed the clamp side only.Fix
src/server.ts: newestimateWireOverhead(protocol, body)— parses the rebuilt wire body and counts system/instructions text (anthropic string + array blocks, openai hoisted system/developer messages, responses instructions) + tools JSON.preflightCompressIfNeeded) and both post-fold fit gates now add the overhead term.src/preflight.ts:PreflightDeps.wireOverhead— the fold loop's budget accounts for it too, otherwise it stops at "text is small enough" while the caller's fit gate still overflows → 502.Tests
tests/wire-overhead.test.ts(6 unit tests: per-protocol system extraction, bad JSON → 0, empty payload).tests/preflight-fail-fast.test.tse2e: text alone fits a 10k window, overhead pushes it to 14130 → preflight folds and forwards.tests/issue453-backstop.test.tsclamp e2e redesigned (window 400k / max_tokens 150k) so the clamp band and preflight band are properly separated; asserts max_tokens < 150k when only the clamp sees the collision.tests/issue330-soft-zone.test.tsbig1/big2 resized 32000→24000 chars (old size forwarded a payload whose billed input exceeded the window — the new fit gate correctly rejects it).Pre-flight: typecheck ✓, npm test 1070/1072 (2 known env fails in plugin-agent.test.ts), build ✓.
Out of scope (not done): stateless request-scoped drop — deferred; the persistent-fold alternative from the design discussion was rejected.
ranxianglei commentedon Sep 6, 2026
[bot] 🏷 Independently verified PR #576 (fetched
2026-09-06_preflight-wire-overhead@8e0a6ad, reviewed the diff, ran typecheck + the touched tests on that commit). It matches the recommendation from my design pass — item 1 — and goes further than my sketch in good ways.What I checked on the actual code:
prepared = runPrepare()(src/server.ts:1497) builds the full rebuilt body beforepreflightCompressIfNeededruns (src/server.ts:1499), soestimateWireOverhead(prepared.protocol, prepared.body)counts the real forwarded system+tools, not a pre-injection estimate.injectOpenaiSystem(fromacp-kernel/wire) returns a messages array with system prepended as role=system/developer entries and there's no top-levelsystemfield → only the in-array branch counts. Anthropic: top-levelsystemfield (src/server.ts:1717) is read directly; Responses:instructions. Each protocol's overhead counted exactly once. Bad JSON → 0 (no crash), empty payload → ~0.payloadEstimate,src/server.ts:~2563), passed into the fold loop viaPreflightDeps.wireOverhead(src/preflight.ts), and applied to both post-fold fit gates (src/server.ts:~2658). Your extra catch on the fold-loop budget is the important one — without it the loop stops at "text fits" while the caller's gate still overflows → the 502 you called out. That path wasn't in my original analysis; nice find.estimateInputTokens" sketch because it counts what's really on the wire rather than re-deriving it.Verification results:
npm run typecheck✓ clean; touched testswire-overhead/preflight-fail-fast/issue453-backstop/issue330-soft-zone→ 21/21 pass. README plugin-mode preflight cell corrected as flagged in the design pass.Scope: agree the stateless request-scoped drop stays deferred and the persistent-fold alternative is correctly rejected — consistent with recommendations #2/#4. Net resolution of this tracking issue: preflight IS the plugin-mode last resort; #576 makes that backstop reliable by counting wire overhead; the extreme input-alone case falls through to client native compaction by design.
#576 is open and mergeable (
mergeable_state: clean). I can't merge it (human-only). Once it lands I'd close #470 as resolved-by-#576 — say the word after merge and I'll close it, or close it now if you prefer tracking closed ahead of merge.Merge pull request #576 from ranxianglei/2026-09-06_preflight-wire-ov…
fix: judge anonymous zero-baseline forks by the char-count upper bound (
fix: count system+tools wire overhead in the preflight trigger (ranxi…
chore: bump acp-kernel 0.0.80 -> 0.0.81
test: absorb kernel 0.0.95 wire deltas (#1414)
fix: pin acp-kernel 0.0.98 (single-object content salvage + one-strin…
fix: count system+tools wire overhead in the preflight trigger (ranxi…
fix(field): live-task receipts — fold-pointer echo acked as no-op, st…
fix(field): live-task receipts — fold-pointer echo acked as no-op, st…
fix(field): live-task receipts — fold-pointer echo acked as no-op, st…
fix(field): live-task receipts — fold-pointer echo acked as no-op, st…