Skip to content

fix: name the live compressible span in failed compress receipts - #1008

Closed
asuan-dev wants to merge 4 commits into
ranxianglei:masterfrom
asuan-dev:fix/failed-compress-span-hint
Closed

asuan-dev wants to merge 4 commits into
ranxianglei:masterfrom
asuan-dev:fix/failed-compress-span-hint

Conversation

@asuan-dev

Copy link
Copy Markdown

Problem

When compress fails (already compressed / dangling anchors / no valid ranges), the rich error only said "go run acp_status" — the model had to burn a round trip (or give up) to find the currently compressible span.

Fix

compressibleSpanHint(state) (src/stream.ts, exported): highest live m-ref minus active-block endRef boundary → "Live compressible refs: mXXXXX–mYYYYY ... Retry NOW"; falls back to naming the ACTIVE block refs (b12..b15) when nothing newer exists. Wired into both applyRanges failure branches (plugin + proxy paths share applyRanges).

Tests

tests/compress-retry.test.ts gains "failed compress receipts carry the live compressible span" (state fake: byRef refs + active blocks) — 9/9 pass. stream*/compress*/loop* suites 169/169. Typecheck clean.

阿酸 added 4 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
After a fold the model keeps anchoring on refs an earlier block already
consumed (01a0b0c4, 2026-09-18: three consecutive [Compression FAILED]
receipts, two full T1 checkpoints wasted, then it gave up and let the
nudge re-fire every turn). The kernel errors say 'run acp_status and
retry', but recovery needs two more round-trips a mid-task model won't
take. Both failure receipts now append the concrete span that is still
raw and directly compressible, computed as the highest mapped raw ref
minus the newest active-block boundary; when no raw refs remain it
suggests compressing a run of ACTIVE blocks.

# Conflicts:
#	CHANGELOG.md
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Reviewed PR #1008 (head asuan-dev:fix/failed-compress-span-hint @ e4a4700). I pulled the branch, reviewed the diff, and ran the full local gate — GitHub CI never ran on this PR (see blocker 1), so these are the only green signals right now:

Local verification on head e4a4700

  • npm run typecheck — clean
  • npm test — 1961 pass / 0 fail / 2 skip
  • npm run build — success

Code review (the PR's own commit e4a4700, 3 files +31/−2)

  • compressibleSpanHint() (src/stream.ts) is sound against the real kernel types (acp-kernel 0.0.77): messageRefs.byRef is Record<string,string> so Object.keys is correct, and CompressionBlock.endRef is optional — handled with ?..
  • The math is conservatively correct: boundary = max endRef over all blocks, so m(boundary+1)–m(highest) is guaranteed not covered by any block. It can under-report compressible gaps below the boundary (non-contiguous blocks) and diverge from acp_status's authoritative viableRanges(nudge.compressibleRanges) (e.g. protected-recent-zone refs) — both in the safe direction for an advisory hint; worst case is one more failed retry, still better than today.
  • Mode coverage confirmed: all wire paths converge on applyRanges (Anthropic via src/loop/core.ts:162, OpenAI chat via src/stream-openai.ts:25 / src/compress-loop-responses.ts:92, Responses via src/stream-responses.ts:39), so the hint reaches plugin and proxy modes alike, as claimed.
  • Two minor gaps, non-blocking (your call):
    1. applyRanges has a third failure branch — the catch (err) at src/stream.ts:174 still returns [Compression FAILED: ${String(err)}] without the hint.
    2. The new test exercises compressibleSpanHint directly but nothing pins the wiring — deleting the ${compressibleSpanHint(...)} interpolation from either receipt would leave the suite green. A small assertion through applyRanges (or on the emitted receipt string) would close that.
  • One pre-existing wart adjacent to the change (not introduced here): src/stream.ts:66 logs byRef?.size — a plain object has no .size, so it always prints ?. Worth a follow-up, don't bundle it here.

Blockers before merge

  1. mergeable_state: dirty — the branch conflicts with current master, and GitHub won't create a merge ref for a conflicting PR, which is why zero check-runs exist on e4a4700. The only conflicting file is AGENTS.md: both sides appended a numbered item after item 6 (master added "Single-writer plugin copies (native模式下自动更新问题需要解决 #991)" as new item 6 and renumbered two-compression-modes to 7; your branch added the plugin-conversations note as item 7). Resolution is the union — after rebasing onto master, keep master's items 6+7 and renumber your note to item 8. I tried to fix it directly but have no push access to your fork (push rejected), so this needs you: rebase fix/failed-compress-span-hint onto current master and resolve AGENTS.md as above.
  2. Stacked branch: the head carries 4 commits — yours plus the open fix(#743): make compress failures retryable in the same turn #1006 (0a807da), fix(#739): forward fitting payloads when a stale usage baseline wedges preflight #1004 (87f64f7), fix(#739): restore compression across restarts and within a frozen watermark #1003 (eeaf10c) — so GitHub currently shows +540/−54 over 12 files against master. Merge order must be fix(#739): restore compression across restarts and within a frozen watermark #1003 → fix(#739): forward fitting payloads when a stale usage baseline wedges preflight #1004 → fix(#743): make compress failures retryable in the same turn #1006 → fix: name the live compressible span in failed compress receipts #1008 (rebase after each lands); once the siblings land, this PR's diff shrinks to exactly src/stream.ts + tests/compress-retry.test.ts + CHANGELOG.md.

Duplicate screening: no duplicate issue/PR found (closest relatives: #743 retryability = PR #1006, #389 stale acp_status ranges, #849 compress-failure loops — this PR complements them; the commit's 01a0b0c4 incident citation makes the root cause plausible: the model re-anchors on refs an earlier fold consumed, and the bare "run acp_status" cost an unrecoverable round trip mid-task).

Verdict: the change itself is correct, minimal, and fully green under local verification — mergeable once the branch is rebased onto master (AGENTS.md union) and the sibling PRs land first. The two test/wiring gaps are optional polish.

中文摘要:修复了 compress 失败回执只让模型"去跑 acp_status"导致多烧一轮的问题(回执直接给出当前可压缩的 live ref 区间),代码正确、本地全量验证通过;当前分支与 master 冲突(仅 AGENTS.md,按并集解)且叠在 #1003/#1004/#1006 之上,rebase 并按序合并后即可合并。

@ranxianglei

Copy link
Copy Markdown
Owner

PR 队列扫描更新(issue #1020):

已关闭的堆叠兄弟 PR:#1003 / #1004 / #1006 已判定为被本 PR 完整包含(eeaf10c → 87f64f7 → 0a807da 链都在 #1008 的历史里),作为重复项关闭,合并以本 PR 为准。

两件事需要作者处理:

  1. 缺失 fix(#743): make compress failures retryable in the same turn #1006 的评审修复 8ab59e64(fix(stream): restore original formatting lost in rebase,src/stream.ts 5 行,恢复 refNum JSDoc 的两空格续行缩进)。该提交只在 fix(#743): make compress failures retryable in the same turn #1006 分支顶端,fix: name the live compressible span in failed compress receipts #1008 不包含它,fix(#743): make compress failures retryable in the same turn #1006 关闭后这个修复会丢失——请 cherry-pick 过来。
  2. rebase 到最新 master:当前与 master 冲突仅 AGENTS.md(按并集解决即可)。fork 分支我无法代推,需要你在自己 fork 上操作。

本地已验证该代码在当前 master 上语义不变,rebase + cherry-pick 后即可进入合并队列。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Both requested items are done and verified — I built the exact final state locally and pushed it as a ready-to-retarget branch, since I have no push access to @asuan-dev's fork (verified with a real push attempt, rejected).

Verification of your claims

What I did
Rebased the full 4-commit stack onto 5f7c00b and cherry-picked 8ab59e64, resolving every conflict as union-of-intent:

  1. AGENTS.md — kept master's items 6 (Single-writer plugin copies) + 7 (Two compression modes), appended the branch's plugin-conversations note as item 8.
  2. src/preflight.ts — kept the stack's hoisted outer-scope maps = refMaps(...) (once per turn) together with master's let startRef/endRef needed by its 模型切换后压缩块跨模型失配,新模型收到 61.3 万 token 全量上下文 #1001 swap block; hoist is safe because ref numbers are never reassigned (kernel contract).
  3. src/server.ts (both Anthropic + OpenAI wire paths) — the stripFailedCompressCalls(...) wrapping from fix(#743): make compress failures retryable in the same turn #1006 plus master's 模型切换后压缩块跨模型失配,新模型收到 61.3 万 token 全量上下文 #1001 rewrite-detection block, both retained.
  4. src/stream.ts — merged receipts: master's dual-shape documentation + fix(#743): make compress failures retryable in the same turn #1006's "NOTHING was compressed … Retry NOW" semantics + this PR's compressibleSpanHint(state) inserted mid-string (…acp_status.{hint} Retry NOW…). The cherry-picked formatting commit then applied cleanly around it (JSDoc hunk auto-applied; the blocksCreated return keeps the hint at 12-space indent). normalizeRangeOrder and the new exported compressibleSpanHint coexist; the test file landed intact (+105 lines).

Result: branch 2026-09-20_pr1008-rebase-master pushed to ranxianglei/billion-context at 60d5615 (stack: ea59597 → 1f4e49c → 167c6d3 → c9bc2a1 → 60d5615 over 5f7c00b). Full local gate green on that head: npm ci (kernel 0.0.79) · typecheck clean · npm test 1993 pass / 0 fail / 2 skip · build success. Version untouched (0.1.126). Note CI only triggers on PR events, so a plain push of that branch runs nothing — CI will re-confirm automatically once the head moves.

To land it, pick one:

  • Option A (fastest, owner): on PR fix: name the live compressible span in failed compress receipts #1008, change the head to ranxianglei:2026-09-20_pr1008-rebase-master (Settings dropdown on the PR page). Title/body/commit authors are preserved; CI runs on the new head; then merge when green.
  • Option B (author): reproduce in your own fork:
    git fetch https://github.com/ranxianglei/billion-context.git 2026-09-20_pr1008-rebase-master
    git checkout -B fix/failed-compress-span-hint FETCH_HEAD
    git push --force-with-lease <your-fork-remote> fix/failed-compress-span-hint
    
    The PR goes green as-is — no manual conflict work needed.

中文摘要:已按最新 master(5f7c00b)完成整条栈的 rebase 并 cherry-pick 8ab59e64,冲突全部按并集解决(#1001 的 preflight/server/stream 改动与本 PR 的失败回执提示合并保留),本地 typecheck、1993 项测试、build 全绿;已把成品推到 ranxianglei/billion-context 的 2026-09-20_pr1008-rebase-master 分支,owner 在 PR 页面一键改头(或作者按上面命令同步到 fork)后即可进入合并队列。

@ranxianglei

Copy link
Copy Markdown
Owner

解决冲突 并简述本pr动机 如何修改的

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 本 PR 动机

#1008 本身解决:compress 失败时(已压缩过 / anchor 悬空 / 无有效区间),回执只说"去跑 acp_status"——模型要多烧一轮往返(或直接放弃)才能找到当前可压缩的范围。修复方式:失败回执里直接点名当前活着的可压缩 span。

因 #1003/#1004/#1006 已判定被本 PR 完整包含而关闭,合并本 PR 实际带入一条 5 提交栈(按序):

如何修改的(核心提交 c9bc2a1,3 文件 +31/−2)

  1. src/stream.ts 新增导出 compressibleSpanHint(state: CompressionState): string:取 byRef 中最高存活 m-ref,减去所有 block 的最大 endRef 边界 → "Live compressible refs: mXXXXX–mYYYYY …";若无更新 ref,回退为点名 ACTIVE block(如 b12..b15)。
  2. 接入 applyRanges 的两个失败分支(no-valid-ranges、blocksCreated===0)——所有 wire 路径(Anthropic / OpenAI chat / Responses)都汇聚到 applyRanges,一处接线全模式生效(plugin 与 proxy 模式共享)。
  3. tests/compress-retry.test.ts 新增用例 "failed compress receipts carry the live compressible span"(state fake:byRef refs + active blocks)。

冲突如何解决(rebase 至 master 5f7c00b,全部按意图并集,共 4 个文件)

  1. AGENTS.md — master 新条目 6(Single-writer plugin copies)、7(Two compression modes)保留,分支的 plugin-conversations 注记重编号为条目 8 追加。
  2. src/preflight.ts — 保留分支把 maps = refMaps(...) 提升到外层作用域(每轮一次),同时保留 master 模型切换后压缩块跨模型失配,新模型收到 61.3 万 token 全量上下文 #1001 引入的 let startRef/endRef(其 reversed-range swap 块依赖 let);提升安全因为 ref 号永不复用(kernel 契约)。
  3. src/server.ts(Anthropic + OpenAI 两条 wire 路径各一处)— 分支的 stripFailedCompressCalls(...) 包装与 master 模型切换后压缩块跨模型失配,新模型收到 61.3 万 token 全量上下文 #1001 的未宣告历史重写检测块并存,两边语义互不干扰。
  4. src/stream.ts — 失败回执三方合并:master 的双形态参数文档(object {startId,endId,summary} / line-form 字符串)+ fix(#743): make compress failures retryable in the same turn #1006 的同轮重试语义 + 本 PR 的 span hint 插入中段(…acp_status.{hint} Retry NOW…);master 的 normalizeRangeOrder 与本 PR 的 compressibleSpanHint 共存;cherry-pick 的格式提交在其上干净落地。

验证状态:head 60d5615 上 typecheck 干净、npm test 1993 pass / 0 fail、build 通过;成品分支 2026-09-20_pr1008-rebase-master 已推在 ranxianglei/billion-context,PR 改头后 CI 复验即可进合并队列。

@ranxianglei

Copy link
Copy Markdown
Owner

冲突 ewrok结局

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 冲突结局:4 个文件,全部按"意图并集"解决——两边语义都保留,没有丢弃任何一方。最终状态(head 60d5615)逐处如下:

1. AGENTS.md(AGENTS.md:84-86)
master 新条目 6(Single-writer plugin copies #991)、7(Two compression modes)原样保留;分支的中文注记重编号为 条目 8 追加在 7 之后。纯编号冲突,无内容取舍。

2. src/preflight.ts(:785 + :861-870)
分支把 const maps = refMaps(messages, deps.session.state) 提升到外层作用域(每轮一次,原先在循环内重复计算)——保留;master #1001 把 span 内的引用变量改成 let startRef/endRef——也保留,因为 master 新增的 swap 块要重新赋值:

let startRef = maps.idxToRef.get(cs);
let endRef = maps.idxToRef.get(ce);
if (!startRef || !endRef) continue;
// #1001: after a client history rewrite, ref numbers are not monotonic ...
if (refNum(startRef) > refNum(endRef)) {
    deps.log("warn", `[preflight] normalized reversed range ${startRef}–${endRef} → ...`);
    [startRef, endRef] = [endRef, startRef];
}

两者不互斥:提升后的 maps 只读、每轮不变,swap 只交换局部变量。

3. src/server.ts(:2061 / :2211,Anthropic + OpenAI 两条 wire 路径各一处)
分支的包装行与 master #1001 的历史重写检测块上下相邻共存:

processedMessages = stripReasoning(stripFailedCompressCalls(stripKernelSummaries(turn.messages, turn.state)));
// #1001: a silent client history rewrite takes the same archive+prune path ...
{
    const rewrite = detectUnannouncedHistoryRewrite(session, knownRefsBefore, msgs.map((m) => m.id));
    if (rewrite.detected) { log("warn", ...); markCompactionBoundary(session); }
}
applyCompactionArchive(session, activeBefore, new Set(msgs.map((m) => m.id)), log);

顺序正确:先净化消息(stripFailedCompressCalls),再做重写检测与归档。第三条 wire 路径(Responses)自动合并于 :2425,同样带包装。

4. src/stream.ts(:66 / :88 / :103 / :151)
master 的 normalizeRangeOrder(:66)与本 PR 的 compressibleSpanHint(:88)相邻共存,互不干扰。no-valid-ranges 回执三方合并(:103):master 的双形态参数文档(object {startId,endId,summary} 或 line-form 字符串)+ #1006 的同轮重试语义("NOTHING was compressed … Retry NOW")+ 本 PR 的 span hint 插在 acp_status. 与 Retry NOW 之间。blocksCreated===0 回执(:151)保留 hint 并采用 master 侧的 12 空格缩进:

return `[Compression FAILED: ${errs}${compressibleSpanHint(ctx.session.state)}]`;

cherry-pick 的格式提交(60d5615)在此之上干净落地:refNum JSDoc 两空格续行、多余空行删除均已生效。

结局一句话:每个冲突点都是"master 的新机制 + 本栈的旧修复"叠加而非二选一;head 60d5615 上 typecheck 干净、npm test 1993 pass / 0 fail、build 通过,成品在 ranxianglei/billion-context 分支 2026-09-20_pr1008-rebase-master,PR 改头后 CI 复验即可合并。

@ranxianglei

Copy link
Copy Markdown
Owner

Thanks @asuan-dev — the span-hint idea here is exactly right (it fixes the 2026-09-18 failure loop where the model kept re-issuing refs an earlier fold had consumed, burned three retries and two T1 checkpoints, then gave up).

Closing this in favor of a slimmed maintainer port: #1036 keeps only the head commit (e4a4700 — compressibleSpanHint() + the two receipt wirings) rebased onto current master, and adapts it to the #849-era receipt texts:

Co-authored-by: asuan-dev credit is on the commit. Fixes #1026.

ranxianglei pushed a commit that referenced this pull request Sep 20, 2026
Port of PR #1008's head commit, slimmed to just the span hint: both failure
receipts (no-valid-ranges and blocksCreated===0) append compressibleSpanHint()
— the raw-ref span not covered by an ACTIVE block — so one retry lands on a
valid range instead of re-issuing refs an earlier fold consumed. Boundary
counts active blocks only (decompress releases it); exhausted fallback names
real active block ids for a higher-tier b-range fold (kernel-first-class).

Co-authored-by: asuan-dev <asuan_10907@163.com>
cskwork pushed a commit to cskwork/billion-context that referenced this pull request Oct 21, 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.

2 participants