fix(background): rank tombstone eviction by ordinal, not key order - #1505
Merged
Conversation
`windowRequestResponded` evicted the oldest tombstones by filtering `Object.keys(requests)`, on the stated assumption that "Insertion order = the order requests were first registered". That is false for the keys this map actually holds: `requestId` is page-generated (`generateRequestId`, src/content/sdk.ts), so an integer-like id such as `"42"` is one the wallet accepts, and integer-like keys enumerate ahead of every string key in ascending numeric order however recently they were written. A dapp numbering its requests could therefore have its newest tombstone evicted first while genuinely older ones survived, and the cap stopped bounding what it was meant to bound. Both arms of `Request` now carry a `seq` stamped at registration, and eviction ranks on it. The tombstone keeps the ordinal because it is tombstones that eviction ranks. The ordinal is derived from the map (`max + 1`) rather than held in a counter field. A counter would be state that survives nothing: after a service-worker restart it would resume at zero and re-issue ordinals the map still holds. The vault solved the same problem for its payload map, but its `orderOldestFirst` also handles rows written before the field existed, ranking unstamped hoisted keys newest. Nothing here can be unstamped — every row is stamped by this reducer at birth — so those branches are not ported; on a file held to 100% coverage they would be unreachable code demanding tests.
ost-ptk
force-pushed
the
WALLET-1419-cw-tombstone-ordinal
branch
from
August 25, 2026 08:43
f4b696f to
26f1ae6
Compare
ost-ptk
marked this pull request as ready for review
August 25, 2026 11:38
Comp0te
reviewed
Aug 25, 2026
Comp0te
left a comment
Collaborator
There was a problem hiding this comment.
Reviewed the ordinal-based tombstone eviction and the fixture updates that follow it. The ranking change itself is right — enumeration order was genuinely hoisting integer-like keys — and the tests pin that. One comment below, on what the ordinal is measured from.
Comp0te
approved these changes
Aug 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
windowRequestRespondedevicted the oldest tombstones by filteringObject.keys(requests), on the stated assumption that "Insertion order = the order requests were first registered".That is false for the keys this map actually holds.
requestIdis page-generated (generateRequestId,src/content/sdk.ts), so an integer-like id such as"42"is one the wallet accepts — and integer-like keys enumerate ahead of every string key in ascending numeric order, however recently they were written. A dapp numbering its requests could therefore have its newest tombstone evicted first while genuinely older ones survived, andMAX_RESPONDED_TOMBSTONESstopped bounding what it was meant to bound. Losing a tombstone early means the response dedup no longer drops a late duplicate for that id.What changed
Requestcarry aseq. The open row is stamped at registration; the tombstone is restamped at response time (nextSeqover the pre-update map), because the cap's stated policy is eviction by when a request was answered — inheriting the registration ordinal would let answering the oldest-registered request evict its own just-written tombstone (review finding, pinned by a dedicated test).seqascending instead of on key order.max + 1) rather than held in a counter field. A counter would be state that survives nothing: after an MV3 service-worker restart it would resume at zero and re-issue ordinals the map still holds.Why not reuse the vault's helpers verbatim
vault/reducer.tssolved the same problem for its payload map (WALLET-1418), but itsorderOldestFirstadditionally handles rows written before the field existed, ranking unstamped hoisted keys newest. Nothing here can be unstamped — every row is stamped by this reducer at birth — so those branches are not ported. On a file held to 100% coverage they would be unreachable code demanding tests.The helpers were also deliberately not moved into
request-map.ts:collectCoverageFrommatchessrc/background/redux/**/reducer.tsbut notrequest-map.ts, so relocating them would silently drop covered code out of the enforced universe for both slices.Verification
npx jest src/background/redux/windowManagement/ src/background/handlers/ src/background/open-window.test.ts src/background/redux/sagas/vault-sagas.test.ts— 22 suites, 394 tests pass.npx tsc --noEmit— clean.windowManagement/reducer.tscoverage: 100% statements / branches / functions / lines.Object.keysrestored,"42"is evicted while the genuinely olderr0survives.Context
This is a prerequisite for the rest of WALLET-1419. The planned fix mirrors
windowManagement's request state intochrome.storage.sessionso it survives a service-worker restart; that mirror round-trips through a JSON-shaped structure, which re-applies the same key hoisting, and its write-side row cap needs an age order to drop by. The ordinal has to exist first.The eviction bug is live on
developindependently of that work, so this stands on its own.Linked tickets
WALLET-1419
Checklist
Make sure this PR title follows semantic release conventions: https://semantic-release.gitbook.io/semantic-release/#commit-message-format
If the PR adds any new text to the UI, make sure they are localized — no UI text added
Include a screenshot or recording if implementing significant UI or user flow change — background-only, no UI change
When this PR affects architecture changes wait for review from Dmytro before merging