Skip to content

Fix kept-order byte accounting after concurrent page reads - #111

Merged
shuv1337 merged 5 commits into
v2from
fix/108-kept-order-accounting
Nov 20, 2026
Merged

shuv1337 merged 5 commits into
v2from
fix/108-kept-order-accounting

Conversation

@shuv1337

@shuv1337 shuv1337 commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fix M1 from #108: after SQL returns, a kept-order reader only touches or forgets the exact order it read. This prevents it from resurrecting an uncounted entry or changing the entry another page admitted. SQL, stamp validation, and ranking are unchanged. Fork CI adds one named self-host step for kept orders, the search walk, and the new races.

Invariants and coverage

  • J1 — Exact accounting. ordersBytes and oversizedHeld equal their respective map sums. The identity check and touch run synchronously; a successful touch changes recency only. forget, admit, and remember use current entries and update counters synchronously. The eviction test observes the pre-ranking room check incorrectly finding room by subtracting an uncounted order; the separate oversized accounting is unchanged and the existing kept-orders scenario exercises oversized isolation.
  • J2 — At most 32 orders and 6 MB. Only admit can insert a new order; it recalculates room after ranking and evicts only sufficiently idle entries. The eviction test uses four ~1.52 MB orders: it tests byte pressure well below 32 entries. The count bound follows from the synchronous admission check and the no-resurrection guard; the existing 40-search scenario also checks that active orders are retained under count pressure.
  • J3 — No stale touch or stale forget. Both operations require object identity with the order the page read. The replacement hit checks that the next page is still kept; the stamp miss fills the active cache past the pre-ranking threshold so the resumed miss must rank without dropping the replacement. A new ranking, when room allows, can still perform an ordinary fresh admission.
  • J4 — Same rows. The stamp and SQL are untouched. The held hit returns the same snapshot as before eviction/replacement; the held miss returns the new rows, matching the later kept page. The assertions compare MCP results as well as search-order spans.

Test host only

The existing statement-hold fixture intercepts just the kept-page SQL fragment and signals after the reply, with no SQL connection held. The scenario owns the waiting MCP call and releases the hold on failure as well as success. Its separate monotonic-clock Layer offsets only Effect's monotonic readings; wall time, tracing timestamps, and live sleep are delegated unchanged. Checked call sites: kept-order recency is the only product consumer of that reading; the CodeMode deadline Clock forwards it and delegates other sleeps, and Effect timeouts race a Clock.sleep. The controls are mounted only by the loopback self-host test entry point, never by a production entry point. The app's public cache invalidation changes the listing in place; a redeploy would change the listing ID and miss the same-key race.

Verification

On shuvdev with Node 24 (matching fork CI), nice -n 19 bun run e2e:self-host --test-name '^A held kept-order read' --workers 1 against the original 7193e6a51 store and the new tests failed on all three intended assertions (exit 1):

A resurrected order must not make room for another admission: expected [ 'ordered' ] to deeply equal [ 'ranked' ]
O1 must not overwrite O2: expected [ 'ordered' ] to deeply equal [ 'kept' ]
O1's stamp miss must not discard O2 and admit another order: expected [ 'ordered' ] to deeply equal [ 'ranked' ]
Tests  3 failed (3)

With the fix restored, all three race scenarios passed. The same named self-host run also passed mcp-search-kept-orders, mcp-search-walk, mcp-search-statements, and both the named held-check case and the access case that exercises the shared SQL hold (8 passed total). The named local mcp-search-concise run passed (1 passed). No full E2E suite ran. The runs used nice -n 19, four CPUs and one E2E worker, one run at a time. bun run format, nice -n 19 bun run check (9/9 typecheck projects), and e2e:prepare passed.

actionlint and git diff --check passed. The upstream-style e2e:check-description command reaches a pre-existing fork/v2 coverage error for cloudOAuthProxyRefusal (pc_b9c87aaf36d5), before it can check the selection. Fork CI runs explicit title filters; no Cloud or production runs were made.

Review notes

  • N1: Not a bug. e2e/sdk/scenario.ts:91–108 starts a fresh process per self-host scenario; the kept-orders and walk scenarios do not share its map.
  • N2: Comment only: new walks go uncached once orders read in the past 30 s hold more than keptBytes − keptOrderBytes (4 MB), even if there are fewer than 32 orders.
  • admit() note: forget(key) before the fit check can drop a same-key entry if its replacement cannot fit. That is a cache miss, not an accounting error; deliberately unchanged. M2, M3, N3, and N4 are outside this PR.

Rat-stack pages followed: skills/model-a-protocol (controlled interleavings and failing/passing twins; no committed model under Executor's E2E-only rule), lore/model-based-testing, skills/test-behaviour, lore/concurrent-work-needs-an-owner, lore/cache-completed-values, lore/testing-ladder, and lore/tests-that-earn-their-place.

Refs #108

mcp-search-kept-order-races.spec.ts
mcp-search-kept-orders.spec.ts
mcp-search-walk.spec.ts
mcp-search-statements.spec.ts
mcp-search-concise.spec.ts
tool-call-held-check.spec.ts
cloud-database-placement.spec.ts
mcp-catalog-scale.spec.ts

@shuv1337
shuv1337 merged commit b51071a into v2 Nov 20, 2026
5 checks passed
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.

1 participant