Skip to content

fix(tests): pin AC12 restart shape with a synchronous persist barrier - #3345

Merged
ranxianglei merged 1 commit into
masterfrom
2026-10-16_ac12-persist-pin
Oct 16, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-10-16_ac12-persist-pin

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What

tests/lineage-reconcile-boot.test.ts AC12 ("a multi-hop grandchild stays eligible when its intermediate parent has nothing to adopt yet") failed intermittently — including on pure master (issue repro: 1/10 local runs, CI shard hit on #3144). This makes the test deterministic.

Test-only change. No runtime behavior change.

Closes #3336

Root cause (test-side race, implementation verified sound)

The test simulates a bili restart by mutating session memory directly (blocks.length = 0, delete metadata.derivedBlocksAdopted) while debounced saves are still armed, then reads back whatever landed on disk. The debounced save builder captures whichever in-memory snapshot exists at fire time, and close(proxy) performs no store flush.

The decisive window: at the second boot, the boot reconcile legitimately adopts the child from the root parent (#2807 behavior) and arms the child's debounce timer. If the event-loop span from that boot to the test's strip exceeds the debounce window (default 500ms — easily exceeded under CI load / GC / shard contention), the timer fires pre-strip and persists the child's adopted record (blocks>0, marker set). The final boot then loads the child WITH blocks; since the sweep visits the grandchild first (lastSeen descending) and the grandchild replays the root's full history byte-exactly, the adoption succeeds, derivedBlocksAdopted is set on the grandchild, and the assertion at line 468 fails. When the span stays under the window (typical locally), the post-strip snapshot wins and the test passes — hence the low local rate and the CI-only signature.

Probe evidence: identical runs produced both disk shapes for the child (adopted record surviving to final boot vs stripped record); the failing interleaving requires exactly {child file = adopted, grandchild file = stripped}, which the sweep order then turns into the observed single-assertion failure.

Implementation audit (no change needed): every non-success branch in reconcileDerivedLineage leaves the gate open (parent-not-on-disk / parent-no-blocks / no-refs / adoptedActive===0 / catch all continue without setting the marker); success requires the parent to genuinely hold blocks whose effective ids are a subset of the child's raw ids. A parent that legitimately persisted blocks → child adopting is correct #2807 behavior, not a gate mis-close. Multi-hop convergence at ≤1 hop per boot is documented intended behavior.

Fix

After each artificial strip, force a synchronous persist of the exact post-strip state: getStore().flushSync(session) (asserted). Per id this cancels the armed debounce timer, writes the record now, and bumps the commit fence so any stale in-flight async write for that id cannot land afterwards. Sites:

  • setupForkOnDisk: flush the stripped child and the parent's folded ledger (every boot-reconcile assertion in this file depends on it being on disk).
  • AC12 strip loop: flush both generations after stripping.

This also removes the same latent exposure in AC9/AC10/AC11 (their premise "the child is exactly the post-restart state" was previously timing-dependent; e.g. AC11 could have tripped its own marker !== true assertion if the adopted record leaked).

Verification

  • npm run typecheck clean.
  • Full file run: 4/4 pass.
  • AC12 loop @ default 500ms debounce: 20/20 pass (issue acceptance: ≥20, 0 failures).
  • AC12 loop @ BILI_PERSIST_DEBOUNCE_MS=200 (amplified window pressure): 10/10 pass.
  • Worst-case stall variant (throwaway copy, 700ms event-loop stall injected between the phase-2 boot and the register fetch — the condition that flips the pre/post-strip capture — plus an explicit on-disk assertion that both records are pinned stripped): 5/5 pass.

The simulated-restart strips mutated session memory directly while debounced
saves were still armed; whichever snapshot a timer found at fire time became
the record the next boot read. When the phase-2 boot adoption's debounce fired
before the strip (event-loop span > debounce window, e.g. CI load), the child
loaded WITH blocks at final boot and legally adopted the grandchild, closing
its gate and tripping the derivedBlocksAdopted assertion. flushSync after each
strip cancels the timer, writes the stripped record now, and bumps the commit
fence against stale in-flight writes. Test-only; no runtime behavior change.
@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-16_ac12-persist-pin (6f780f0)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.3946-pr.3345.3db5721

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr3345.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei
ranxianglei merged commit 745be9f into master Oct 16, 2026
38 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.486-master.745be9f (rolling master tag).

npm install -g billion-context@0.0.486-master.745be9f  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant