Skip to content

fix(#743): make compress failures retryable in the same turn - #1006

Closed
asuan-dev wants to merge 4 commits into
ranxianglei:masterfrom
asuan-dev:fix/743-compress-retry
Closed

asuan-dev wants to merge 4 commits into
ranxianglei:masterfrom
asuan-dev:fix/743-compress-retry

Conversation

@asuan-dev

Copy link
Copy Markdown

Problem

A malformed compress call (missing/invalid content) fails schema validation and the model often gives up instead of retrying — the nudge then re-fires every turn while history keeps growing.

Fix

  • Soft-fail schema: move compress content out of schema-required. Bad calls now reach the bili executor, which returns a rich error + a retry template instead of a silent schema rejection.
  • Rich retry receipt: applyRanges failures (stream.ts) carry "Compression FAILED ... Retry NOW in this same turn" guidance.
  • stripFailedCompressCalls() (server.ts): before forwarding, drop failed compress tool-call/result pairs from closed turns; the current turn keeps them so the model can retry; successful compress calls are never matched (they are plugin-mode block carriers).

No 3-strike silencing — that would deadlock compression.

Tests

tests/compress-retry.test.ts — 8 tests green: 3 surface schemas drop required content; strip: closed-turn dropped, current-turn kept, validation marker dropped, success kept, non-compress kept, no-user unchanged. Typecheck clean.

阿酸 added 3 commits September 19, 2026 20:35
…a frozen watermark

Three root causes behind "session hangs / context keeps growing after a
restart or model switch":

1. A plain prompt_cache_key is not plugin ownership. Only a real omp
   registration or a persisted pluginAgent may unwrap the |sub: namespace;
   otherwise instructions drift (model switch, async tool registration)
   forked the main conversation into an empty namespace whose compression
   state was gone, so every existing block was invalidated.
2. Existing conversation mappings now restore their original session and
   compression blocks instead of minting a fresh bare-id session.
3. A failed preflight freezes the original history BOUNDARY rather than
   the range worklist it happened to select: every successful fold changes
   which ranges stay viable, so replaying a stale worklist compressed
   nothing and the retry looked bigger. The walk now recomputes remaining
   viable ranges against that boundary after each fold, including the
   soft-protected recent zone under overflow, while turns appended during
   the failure window stay out of the compression input.

# Conflicts:
#	CHANGELOG.md
#	src/plugin.ts
#	src/preflight.ts
#	src/server.ts
…eline wedges preflight

The preflight trigger floors on session.stats.lastInputTokens. After an
as-is forward under preflight (ranxianglei#300) the measured baseline can stay pinned
above the window while the prepared payload itself fits (~127k against a
256000 window on a live session). Every turn then re-ran the fold walk on
ranges the payload no longer needed: three 60s summarization attempts,
structured 502 on upstream 500, dead-end cooldown, repeat.

Forward as-is when a known-baseline session's payload estimate fits and
its measured baseline sits above the window (ranxianglei#301: as-is is safe; the next
usage report self-heals the baseline). Inside-window folding is untouched —
the codex headroom target still walks fitting 90-100% payloads.

# Conflicts:
#	CHANGELOG.md
Hosts (claude-code/openai/codex) pre-validate tool arguments against the
served schema and reject a missing 'content' CLIENT-side, so the model
only ever sees a bare 'content: is required' — observed in production,
it gives up instead of retrying, leaving the nudge pending forever.

- compress-tool: 'content' no longer schema-required on any served
  surface; description documents that validation happens at execution
  and how to recover a rejected call.
- stream: malformed calls now reach the executor and get the actionable
  [Compression FAILED ...] receipt they can fix in the same loop.
- server: failed compress call+result pairs are dropped from CLOSED
  turns before forwarding; current-turn pairs stay for in-place retry,
  successful compresses are never matched (plugin-mode block carriers).

Tests: tests/compress-retry.test.ts; ranxianglei#349 and ranxianglei#841 pin the schema deltas.

# Conflicts:
#	CHANGELOG.md
#	src/compress-tool.ts
#	tests/search-context-cross-session.test.ts
…1006 review)

- re-indent the blocksCreated===0 return in applyRanges (dedented 4 spaces)
- drop a stray blank line before applyRanges
- restore the two-space JSDoc continuation on refNum
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Reviewed — the fix itself is sound and I verified it green in isolation, but the branch has 3 blockers that need your action before this can merge. Details below.

Verified (what I ran)

The branch bundles three commits, so I verified the actual PR scope by cherry-picking only 0a807da onto current master (3859b97, v0.1.125) — it applies cleanly except a trivial CHANGELOG conflict (the release moved entries). On that head:

  • npm run typecheck ✓ clean
  • npm test ✓ 1973 pass / 0 fail / 2 skipped
  • npm run build ✓
  • Full suite also green on your branch as-is (1960/0/2)

Design checks (both compression modes per AGENTS.md §6):

  • Schema softening covers every serving surface. All wire surfaces consume the BILI_ACP_TOOLS_* arrays (server.ts:2417/2769/2781, plugin manifest plugin.ts:528-530; the MCP shell fetches the manifest, so it inherits too). No surface serves the raw kernel COMPRESS_TOOL anymore.
  • Strip works in both modes. Plugin-mode compress executes through the proxy-side executeProxyTool (loop/core.ts:161, plugin.ts:761) — the same applyRanges (stream.ts:58) every path routes through — so the [Compression FAILED] marker is identical in plugin and proxy mode. Legacy standalone extensions (separate repos) execute locally with their own wording; the strip simply won't match there (degrades to today's behavior — fine).
  • Wire stays valid. The strip drops call+result as a pair (shared toolCallId), so no dangling tool_use/tool_call/function_call_output on any of the three wires. State accounting (applyCompactionArchive/reapOrphanBlocks, server.ts:2054-2055) still sees the un-stripped list — failed pairs create no blocks, so no divergence.
  • Closed-turn boundary semantics check out: current turn keeps the pair for in-place retry; the next user message closes the turn and the pair is dropped from then on. Successful compresses can never match (their receipts don't carry the markers).

Blockers (need you)

  1. Scope violation — two unmerged [Question] Can we fix gpt 516 problem in bili proxy? #739 commits ride along. eeaf10c (restore compression across restarts/frozen watermark) and 87f64f7 (stale usage baseline wedges preflight) touch preflight.ts, plugin.ts, server.ts, AGENTS.md and add ~440 lines that have nothing to do with compress-retry. One issue = one scope; they belong in their own PR with their own review. As it stands GitHub reports the PR mergeable: false (dirty) — those commits conflict with current master (confirmed: AGENTS.md conflict when rebasing eeaf10c).
  2. Stale base. Branch base is cc53ca0 (v0.1.123 era); master is at v0.1.125 (+ single-writer plugin guard, opencode-plugin-preserve). After splitting, rebase the remaining work onto current master — I verified the compress-retry commit cherry-picks clean (only the CHANGELOG bullet needs re-anchoring under the new [Unreleased] section).
  3. Wrong issue reference. fix(#743) points at feat(status): surface meta — active pack in acp_status + plugin panel, session audit stamp (#742) #743, which is the closed, unrelated "[PR] feat(status): surface meta" PR. I searched open+closed issues and found no bug-report issue exists for the "model gives up on malformed compress, nudge re-fires forever" behavior this fix addresses. A fix PR needs its source issue — please point the PR at the right one (or @ranxianglei opens one from your production observation; I can't file issues from a working session). Note the two [Question] Can we fix gpt 516 problem in bili proxy? #739 CHANGELOG bullets should leave with the split commits.

Fixed directly on the branch (commit 8ab59e6, pushed)

Formatting regressions lost in your rebase — zero behavior change:

  • src/stream.ts:108 — the blocksCreated === 0 return had been dedented 4 spaces
  • stray double blank line before applyRanges
  • JSDoc continuation spacing on refNum restored

Suggestion (your call — not applied)

Incomplete first pass: only the no-ranges path got the rich retry receipt. The other two failure exits — blocksCreated === 0 (stream.ts:108, invalid/stale refs) and the catch-all exception (stream.ts:159) — still return a bare [Compression FAILED: ${errs}]. Stale-ref failures (model citing a dead mNNNNN from an old summary) hit exactly those paths, which is the same give-up failure mode this PR fixes. Suggested addition for :108:

[Compression FAILED: ${errs}. NOTHING was compressed — the nudge is still pending. Refs cited in old summaries may be stale — get current refs from acp_status. Retry NOW in this same turn with valid ranges.]

Minor note (non-blocking): the second strip marker 'Validation failed for tool "compress"' appears nowhere in this repo or acp-kernel — presumably real client-side output you observed. It's inert until matched, which is safe, but if that client wording can drift, a stable marker token emitted by whatever produces it would be more robust than substring matching.

Process notes

  • No CI runs exist for this fork PR (none triggered on the head SHA), so the local runs above are the evidence.
  • I could not run the codex E2E here (local sglang upstream down). Per repo rules, request-pipeline changes (server.ts/stream.ts) want an e2e pass before merge — the manual-dispatch CI workflow or a local upstream will do.
  • Auto-merge gate: N/A regardless — this rewrites forwarded history shape (both modes) and touches server.ts, so it stays human-merged.

中文摘要:修复方向正确、代码本身在当前 master 上验证全绿(typecheck + 1973 测试 + build),但分支混入了两个未合并的 #739 preflight 提交(导致与 master 冲突、GitHub 判定不可合并)、且 fix(#743) 引用了错误的 issue(#743 是无关的已关闭 PR,真正的 bug 报告 issue 尚不存在)——请拆分出 #739 两个提交单独提 PR、rebase 到当前 master 并更正 issue 引用后即可进入合并流程;格式回归我已在分支上直接修好(8ab59e6),另建议把另外两条失败路径的回执也补上同款的"立即重试"指引。

@ranxianglei

Copy link
Copy Markdown
Owner

Closing as stacked — contained in #1008 (e4a4700). One note carried forward: this branch has the review fixup 8ab59e6 ("restore original formatting lost in rebase") that #1008 does not yet include — it will be folded into #1008 during its update to current master. (Queue triage per #1020.)

cskwork pushed a commit to cskwork/billion-context that referenced this pull request Oct 21, 2026
…1006 review)

- re-indent the blocksCreated===0 return in applyRanges (dedented 4 spaces)
- drop a stray blank line before applyRanges
- restore the two-space JSDoc continuation on refNum
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.

2 participants