Skip to content

[PR] perf: rewrite fold coverage without repeated scans - #6705

Merged
ranxianglei merged 1 commit into
ranxianglei:masterfrom
Afloat16:2026-11-14_linear-fold-rewrites
Nov 14, 2026
Merged

ranxianglei merged 1 commit into
ranxianglei:masterfrom
Afloat16:2026-11-14_linear-fold-rewrites

Conversation

@Afloat16

Copy link
Copy Markdown
Contributor

Closes #6704

Problem

After reconciliation has matched a resent history to existing folds, rewriteBlocks() rewrites effective and direct coverage one ID at a time. Each target performs a full-array membership scan, and each replacement shifts the remaining array with splice(). A full remint or a model switch between split reasoning/text and merged assistant turns therefore incurs quadratic coverage-array work even when every match is unambiguous.

Change

Build occurrence counts for the current list, then emit a stable replacement list in one pass. The counts include unread originals and replacements already emitted, preserving the existing membership decision at each step. Copy the result back into the original array so callers retain the same array references, including shared effective/direct coverage arrays.

The change is confined to coverage rewriting. Matching, reconciliation modes, anchor storage, turn-integrity enforcement, and kernel code keep their existing behavior. The rewrite uses temporary counts and an output array proportional to the coverage size.

Verification

  • Six new tests call the actual exported reconcileFoldCoverage() path. They cover a full remint, reasoning/text-pair collapse, merged-turn expansion, shared coverage arrays, warn-mode non-mutation, unchanged resends, and mixed stable/reminted IDs with repeated claims.
  • Numeric reads/writes on the actual coverage arrays are counted without mocking reconciliation. On unchanged base 9495c764d36909eec263f404d4802d68b608f23f, the three complexity regressions fail with 5,132,800, 1,445,400, and 2,883,600 operations respectively; the other three controls pass. The assertions also verify exact ordered contents, successful reconciliation, and original array identity before enforcing the work bound.
  • 122/122 related tests pass after the correction: the new file plus existing fold reconciliation, positional reconciliation, and fix(dsh): model switch drops all folds — fold-reconcile #2396 detect-only cannot re-anchor rewritten tool-call ids → doom-probe 502 session death #2915 order-centric suites.
  • A separate deterministic oracle compares the actual old and new rewrite helpers on 20,000 generated cases, including repeated IDs, empty/self/cyclic/multi-target claims, missing lists, and aliased arrays. Mutated contents, return values, and array identities agree.
  • An uninstrumented benchmark calls the full public reconciliation path with 20,000 reminted user messages and two coverage arrays under Node 24.22.1. After warmup, three paired sequential runs have median elapsed time 1047.1 ms → 282.3 ms and median process CPU 1094.6 ms → 371.5 ms. Every pair has the same result/state SHA-256 (a86713cda51998ae46bfd47bd18ec30be22a8cb9548ed1062ca53e1851f98763). These figures describe this synthetic case, not every history shape.
  • Full npm run typecheck, npm run build, and git diff --check pass. Full repository tests were not run locally; hosted CI remains pending.

Prior work checked

The complete Afloat16 production-patch index has only #6492 touching this file; that change bounds claimed-message index extrema and does not modify rewriteBlocks(). Actual relevant patches from #2644 (the existing one-to-many/many-to-one rewrite), #4353 (turn integrity), #4085 (retained inactive coverage), #2738, and #2300 do not implement this optimization. All-state rewrite/quadratic-reconciliation searches found no matching change.

@ranxianglei
ranxianglei merged commit fa0ed27 into ranxianglei:master Nov 14, 2026
48 checks passed
ranxianglei pushed a commit that referenced this pull request Nov 15, 2026
The rebase onto f648217 (which carries #6705's fold-coverage linear
rewrite, the instruction-preservation reconcile fixes, and 7402629's
OpenAI cache-attribution golden regen) changes the persisted state the
drive produces. Regenerated with the sanctioned
scripts/update-storage-render-goldens.ts; only digest values move
(stateDigest/stateDigestAligned, stats/metadata sections) — no wire
body or structural change.
ranxianglei pushed a commit that referenced this pull request Nov 17, 2026
The rebase onto f648217 (which carries #6705's fold-coverage linear
rewrite, the instruction-preservation reconcile fixes, and 7402629's
OpenAI cache-attribution golden regen) changes the persisted state the
drive produces. Regenerated with the sanctioned
scripts/update-storage-render-goldens.ts; only digest values move
(stateDigest/stateDigestAligned, stats/metadata sections) — no wire
body or structural change.
ranxianglei pushed a commit that referenced this pull request Nov 18, 2026
The rebase onto f648217 (which carries #6705's fold-coverage linear
rewrite, the instruction-preservation reconcile fixes, and 7402629's
OpenAI cache-attribution golden regen) changes the persisted state the
drive produces. Regenerated with the sanctioned
scripts/update-storage-render-goldens.ts; only digest values move
(stateDigest/stateDigestAligned, stats/metadata sections) — no wire
body or structural change.
ranxianglei pushed a commit that referenced this pull request Nov 19, 2026
The rebase onto f648217 (which carries #6705's fold-coverage linear
rewrite, the instruction-preservation reconcile fixes, and 7402629's
OpenAI cache-attribution golden regen) changes the persisted state the
drive produces. Regenerated with the sanctioned
scripts/update-storage-render-goldens.ts; only digest values move
(stateDigest/stateDigestAligned, stats/metadata sections) — no wire
body or structural change.
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.

Fold coverage rewrites repeat full-list scans on long reminted histories

2 participants