Skip to content

feat: deterministic pre-crush channel in front of model absorb (#1094) - #1098

Closed
ranxianglei wants to merge 2 commits into
masterfrom
2026-09-21_absorb-pre-crush
Closed

ranxianglei wants to merge 2 commits into
masterfrom
2026-09-21_absorb-pre-crush

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Sep 21, 2026 •

Copy link
Copy Markdown
Owner

⚠️ BLOCKED — do not merge. Owner ruling on #1094 (2026-09-22): valuation moves into the kernel (token/cost estimation, threshold check, two-tier gate decision) and compression becomes an open plugin interface with kernel-shipped defaults — tracked at acp-kernel#354; must also reconcile with the content-addressed store in #1097 (ID-reference wins; pre-crush demotes to fallback/retrieve-time). This branch stays intact as the starting material for the kernel default plugins (src/pre-crush.ts + tests/pre-crush.test.ts) and will be reworked after the kernel API lands — see the latest comments on #1094.


Closes #1094

What

Opt-in compress.absorb.preCrush.enabled runs a deterministic, model-free denoise over each eligible oversized tool result before the [ACP absorb] decision. New src/pre-crush.ts (pure functions, zero new runtime deps); wired into applyAbsorbView (src/absorb.ts) so every prepare/preflight path gets it; config parsed/merged/resolved in src/config.ts + src/compress-settings.ts following the existing three-level deepest-wins convention.

Three conservative strategies:

  • JSON (lossless): constant-field hoisting + identical-row/run folding into annotated __pre_crush envelopes that decode back byte-for-byte.
  • Code (Python, JS/TS; lossy by design): comment/docstring elision behind a string-literal-aware scanner; bails (fail-open) on unterminated constructs; template literals and assigned triple-quoted strings survive verbatim.
  • Logs (lossy by design): port of headroom's LogCompressor (Apache-2.0) — level classification, summary-line detection, format detection (pytest/npm/cargo/jest/make), selection under a fixed 100-line cap keeping ERROR/FAIL lines with priority (first+last preserved, top-scored within a fixed 10-line budget each), warnings deduped via digit/hex/path normalization, runtime stack frames collapsed into [… N frames collapsed], honest [N lines omitted: …] footer instead of CCR hashes.

The two-tier gate falls out of the existing threshold: results crushed below absorb.minToolTokens skip the model round-trip entirely; results still over it are forwarded denoised with the normal absorb prompt. Crushed bytes are a pure function of the payload, so re-sent history stays byte-stable for prefix caching.

Safety

  • Off unless BOTH absorb.enabled and preCrush.enabled are true; minReduction (default 0.1) rejects marginal savings.
  • Fail-open everywhere: unparseable payloads, prose, logs <50 lines, and anything confidently JSON or source never reach the wrong selector; any anomaly passes bytes through unchanged.
  • error/fail lines keep selection priority (first+last always kept); overflow beyond the budgets is reported by the honest footer.
  • No kernel bump, no wire-shape change beyond the tool-result text itself (both compression modes covered — crush happens before the mode-specific absorb carrier logic).

Deliberate deviations from headroom (flagged for review)

  1. CCR retrieval dropped — bili has no headroom_retrieve channel; dangling hash refs would be noise, so the footer states honest counts instead.
  2. Kneedle adaptive sizer replaced by headroom's fixed defaults (cap 100 lines) — the adaptive curve needs line-diversity statistics that buy little once the absorb threshold + model compression already bound cost downstream.
  3. TOIN field-importance learning and Kompress ML fallback not ported — incompatible with bili's stateless-per-payload, dependency-free footprint.
  4. minTokens from the issue folded into the existing absorb.minToolTokens — a separate size knob would have duplicated the eligibility gate.
  5. minReduction default 0.1 rather than the triage sketch's ≥50% — below-threshold results skip the model regardless of percentage; above-threshold ones benefit from even modest denoise before distillation. User-tunable either way.
  6. Relationship to 从 opencode-acp#354 移交:Headroom / 外部压缩引擎在 billion-context 的集成 #445 (closed "Headroom integration", owner verdict: benefit small / risk huge): this ships only the slice 从 opencode-acp#354 移交:Headroom / 外部压缩引擎在 billion-context 的集成 #445 left open — provably-redundant folding (JSON), comment elision, and log selection where errors are always kept — with honest markers, fail-open, and config-gated default-off as the risk mitigations discussed there.

Pre-flight

  • npm run typecheck — clean
  • npm test — 2191 pass / 0 fail / 2 skipped (e2e gates)
  • npm run build — success
  • New: tests/pre-crush.test.ts (29 tests: envelope losslessness, scanner bail-outs, log budgets, dispatch guards, two-tier integration through applyAbsorbView, config parse/merge/resolve); extended tests/absorb.test.ts merge expectations.

@github-actions

github-actions Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-21_absorb-pre-crush (6f7cccb)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-1098

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

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr1098.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] 🏷 收到 — 这是已完成的工作(PR #1098,标记 ework-agent-pr),我不重做,按流程核验:拉取分支核对代码 → 查 CI 状态 → 本地跑 typecheck / test / build 全量门禁。稍后回结论。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

核对完成 — 可以合并(合并留给人工操作)。我没有重做工作,只做了拉取核对 + 门禁 + CI 确认,外加一处 review 中发现的小问题已直接修掉。

Verified

Base & diff hygiene

  • Original head ae2a8b6 sat exactly one commit above master (9c01dbe), merge-base = master tip — fresh base, no drift.
  • 10 files, +1774/−19: new src/pre-crush.ts (+1263), new tests/pre-crush.test.ts (+416), wiring in src/absorb.ts/src/config.ts/src/compress-settings.ts, docs (CHANGELOG/CONFIGURATION en+zh/README en+zh). No version bump, no lockfile churn, acp-kernel pin unchanged (0.0.82), zero new runtime deps.

Code review (both compression modes considered)

  • Single insertion point applyAbsorbView (src/absorb.ts) covers all six server prepare sites + preflight.
  • Two-tier gate is correct, including the subtle part: pre-crush slices the payload at any kernel-baked [ACP absorb] marker before crushing, so appendAbsorbPrompts (kernel, runs inside processTurn on raw size) re-decides on the post-crush size — below minToolTokens ⇒ no marker survives ⇒ zero model round-trip; above ⇒ a fresh prompt on the denoised payload. The integration tests drive real core.processTurn + applyAbsorbView and assert exactly this.
  • Determinism holds: crush output is a pure function of the payload (no Date.now/Math.random on the crush path), the transform is view-level (new array, shallow msg copies, ids/refs preserved, session.state never mutated) → byte-stable for prefix caching in both proxy and plugin mode.
  • Fail-open verified strategy-by-strategy: JSON-shaped payloads never reach the code/log selectors and vice versa; Python/JS-TS scanners bail to null on unterminated constructs; logs <50 lines and prose pass through byte-identical; minReduction floor (default 0.1) rejects marginal savings; any anomaly → unchanged bytes.
  • Config plumbing follows the existing conventions: strict parse (percent-string accepted, malformed block rejected wholesale), deepest-wins sub-field merge across global/provider/model, host-side-only resolution, off unless BOTH absorb.enabled and preCrush.enabled.
  • Tests match the claim: 29 new tests covering envelope losslessness (decode-back deepEqual), scanner bail-outs, log budgets, dispatch guards, two-tier integration, and config parse/merge/resolve.

Gates

  • Local on PR head: npm run typecheck clean · npm test 2191 pass / 0 fail / 2 skipped (e2e gates) · npm run build success.
  • CI: all 8 runs green (test matrix ubuntu/windows × Node 22/24, version-guard, build-artifact, codex CODEX_HOME overlay, real-codex zero-token e2e). PR mergeable_state: clean.

Found & fixed directly during review (docs overclaim)

The shipped docs said "ERROR lines are never dropped" (CHANGELOG.md, CONFIGURATION.md/.zh-CN.md, README.md/.zh-CN.md, and the PR body), but the code — and its own test, which asserts errorLines.length <= 10 with an "error budget respected" comment — caps ERROR and FAIL lines at 10 each (LOG_MAX_ERRORS), keeping first+last always and top-scoring the rest within budget. With >10 error lines, middle ones do get shed (reported honestly in the omission footer). Behavior is fine and matches the design intent; the prose overstated the guarantee. Fix: reworded all five doc files + the PR body to state the actual contract ("kept with priority — first+last preserved, top-scored within a fixed 10-line budget; overflow reported by the honest footer"). Docs-only commit 6f7cccb pushed to the PR branch, CI re-ran fully green. No code changed.


中文摘要:核对了 PR #1098 的代码、测试与 CI(两级门槛、fail-open、确定性/前缀缓存稳定均验证通过,本地 typecheck/test/build 与全部 8 项 CI 检查全绿),发现文档把日志错误行的选择预算(每级固定 10 行、首末必留)夸大为「ERROR 行永不丢弃」,已在 5 个文档文件和 PR 描述里直接修正措辞并补了一个 docs-only commit(6f7cccb,CI 复跑全绿),现在可以合并。

@ranxianglei ranxianglei left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

审查结论:HOLD 维持(block 通知在且正确);两个发现请带进 #1097 后的重设计

确认:PR body 的 ⚠️ BLOCKED 通知与 #1094 封存决策时序一致(PR 13:26 开、owner 13:52 封 issue、body 随后更新),无违规。实作本身质量好:view 级替换不动 store({ ...msg, text: crushed.text } 共享 msg.id,src/pre-crush.ts:1260——不破坏 #1097 的原件可检索前提,冲突只在 wire 槽位归属,与封存理由一致)、parse 失败逐字透传有测试、默认关双重门(absorb.enabled && preCrush.enabled)。

重设计时请带上:

  • [Medium] [ACP absorb] 标记截断丢 payload 尾部:src/pre-crush.ts:1246-1247 payload = text.slice(0, markerAt),之后视图消息只剩 crushed 前缀——工具输出里天然出现该字面标记(例如开发者把 bili transcript cat 进工具)时,标记之后的全部内容从模型视图中静默消失。
  • [Medium] JSON 数字保真漏洞:crushJson(src/pre-crush.ts:299)经 JSON.parse/JSON.stringify 重序列化,实测 1e999(合法 JSON 文法)round-trip 变 null——折叠发生时模型视图里的静默数值损坏。fail-open 只接异常不接值漂移。
  • [Low] __pre_crush 键碰撞只文档未守卫(pre-crush.ts:80):用户 payload 合法携带该保留键时与 crushed 信封不可区分;"输入含该键即 bail" 一行可闭。

@ranxianglei

Copy link
Copy Markdown
Owner Author

@ework — please re-review the current head 6f7cccb in full. Additional context since your last pass: an independent deep review is at #1098 (review) (hold maintained per the #1094 freeze; three findings — marker-truncation payload loss, JSON numeric fidelity via 1e999, __pre_crush key collision — to carry into the #1097 redesign).

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

对 head 6f7cccb 的全量复审完成。结论:同意 HOLD(遵循 #1094 封存);独立评审的三条发现我全部独立复现确认,细节已带进 #1097。

Re-verification on 6f7cccb

  • Delta vs last-pass head ae2a8b6: docs-only(5 个 md 文件、7 行 —— 即错误行预算措辞修正),无代码变动。
  • Local gates re-run on 6f7cccb: typecheck clean · npm test 2191 pass / 0 fail / 2 skipped (e2e gates) · build success。CI 全绿,mergeable_state: clean。

The three findings — all confirmed with runtime evidence

1. Marker truncation (Medium) — real, and the consequence is worse than a "missing tail". src/pre-crush.ts:1246-1247 slices at the first [ACP absorb] occurrence; a legitimate occurrence inside the payload (transcript cat'ed into a tool) chops everything after it before crushing. Repro: 125-line log result, marker line at line 121, two ERROR lines after → view shrank 5127→29 bytes, both ERROR lines gone from the model view while intact in session state; control (neutral marker text) keeps both. Strict-JSON payloads fail open on this path, so exposure is log/code-shaped content. This refines my first-pass conclusion that marker-stripping was safe: the happy path is, the embedded-marker edge is not — my earlier pass missed it. Fix direction recorded in #1097: cut at lastIndexOf("\n\n" + marker) (the kernel-appended prompt is always the last occurrence).

2. JSON numeric fidelity (Medium) — real. crushJson re-stringifies parsed values; 1e999 (valid JSON grammar) parses to Infinity and serializes back to "big":null in the crushed output; underflow variant 1e-324 → 0. Fold demonstrably fired on an 8-row fixture; the decoded envelope contradicts input semantics. fail-open catches exceptions, not value drift. Fix direction: post-parse finiteness walk + lexical exponent-threshold bail; >2^53 precision noted as an inherent JS residual. Recorded in #1097 as an invariant for any deterministic transform that round-trips user JSON.

3. __pre_crush collision (Low) — confirmed documented-but-un-guarded (src/pre-crush.ts:80, header doc lines 17-18). Latent today (legit payloads simply fail open); the one-line bail-if-key-present guard closes it. Also flagged the same class for #1099's [stored #mNNNNN] placeholder marker — pick format + collision guard together.

Disposition

Per the #1094 freeze I pushed no fixes to this branch — all three findings land as design inputs on #1097 (comment posted there with mechanism, repro numbers, and fix directions), where the digest layer and the eventual pre-crush unfreeze will consume them. The rest of the implementation stands as verified in my first pass; nothing new found in this re-review. Merge stays with you — and given HOLD, no merge action is expected until #1097 lands.


中文摘要:对 6f7cccb 做了全量复审(增量仅文档、本地门禁与 CI 全绿),独立评审的三条发现全部经运行时复现确认——标记截断会丢合法 payload 尾部(连 ERROR 行都从模型视图消失)、1e999 经 JSON 往返变 null、__pre_crush 保留键无代码守卫;按 #1094 封存决定未向本分支推任何修复,三条发现已连同复现证据与修复方向带进 #1097 作为重设计输入,维持 HOLD。

@ranxianglei

Copy link
Copy Markdown
Owner Author

Superseded by #1176 — reworked on top of acp-kernel 0.0.84 (#1175), which now ships the crush strategies natively (kernel PR ranxianglei/acp-kernel#359, pipeline node between absorb-hide and absorb-prompt). #1176 keeps only the config surface (compress.crush three-level merge → kernel CrushConfig) and drops the bili-local 1263-line strategy duplicate. Closing in favor of #1176; Fixes #1094 rides there.

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.

Feature: deterministic pre-crush channel in front of model absorb (JSON + code)

1 participant