Skip to content

fix(data): keep the newest request's value in the shared owner's cache - #2940

Merged
trueadm merged 2 commits into
mainfrom
fix/data-owner-save-order
Nov 15, 2026
Merged

trueadm merged 2 commits into
mainfrom
fix/data-owner-save-order

Conversation

@trueadm

@trueadm trueadm commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Fixes data: a late replica refresh overwrites the newer persisted value for the same key #2934. Two tabs sharing one data owner could reload the same read and leave the older result in the encrypted cache, so the next session restored stale data while the live tabs showed the newer value.
  • The owner now keeps, for each key, the value read by the newest request it made. A reply to an older request that lands later still shows in the tab that asked for it, and is no longer saved over the newer value.

Why

  • The owner ordered live read results by request (newest), but persistence had no order: each replica forwards its full value and the owner kept whichever save arrived last.
  • A save carried nothing that said which request its value came from. A value's load time (savedAt) cannot stand in for that: tabs stamp one request's value differently (the tab that asked uses its own start, a tab told the value uses the owner's, a write's refreshed value uses its landing time), so ordering by it would drop a tab's later saves for good.

Changes

  • owner.ts: each read the owner starts gets a start time later than every read before it, and its reply carries it (at, as the value broadcast already did). save drops a value read by an older request than the one last saved for that key; values with no request behind them (seeds, restores, feed values, a write's refreshed reads) are saved as before.
  • store.ts: an entry remembers the owner request that read its value (requestedAt), set by a load from its reply and by a broadcast value, kept across a partial edit, cleared by any other source. Saving carries it to the owner.
  • docs/data.md: the Cross-tab owner section states the new clause.
  • No protocol bump: the new fields are optional, and an owner or tab from an older build behaves as before.

Validation

  • pnpm format:check (not run repo-wide; pnpm format:files:check on every changed file passes)
  • pnpm typecheck (not run repo-wide; pnpm typecheck:files on the changed files passes)
  • pnpm test (not run locally; left to CI)
  • targeted tests: data-owner.test.ts, data-persist.test.ts, data-store.test.ts, data-feeds.test.ts, data-views.test.ts in --project octane (216 pass), and react-compat/data.test.ts in --project octane-hosted-react-dev (12 pass).
  • Failing first: the existing two-tab ordering test (read, view, and read after many writes) now runs with the owner's cache enabled and checks a store started offline from that cache. On main all three fail with expected 'Tides' to be 'Atoll' after the older reply lands; with the fix they pass. The test also pins that the tab left behind saves again once it reloads; breaking the load's request stamp turns it red.
  • perf-review: node scripts/perf-review-scan.mjs reports no candidates. Touched paths run once per network request or per forwarded save, never per render or node: one WeakMap.get per load, one more field on Entry (set in its single literal) and on the Saving literal, one number on each read reply. No DOM, scheduling or compiled-output change.
  • concise-code: src +81 −21 (runtime +81 −21); no new files; new exports: requestTimes (store-internal side channel from a replica's reply to its load, called by owner.ts) and the Saving type's optional field; alternatives: order saves by stored.savedAt in the owner alone (about 8 lines, rejected: tabs stamp one request's value at different times, so a tab's later edits would be dropped whenever another tab stamped later), a per-port "superseded" flag in the owner (rejected: the owner cannot see which values a replica adopted, so the flag has no safe point to clear); bytes: not applicable, octane/data only.

Risk / follow-ups

  • After replies cross, the tab holding the older value keeps it until it reloads, as before. Its saves for that key (a stale mark, a partial edit) are now dropped while the cache holds the newer request's value. If the tab holding the newer value has gone, an edit made only in the older tab is not in the cache; a write's stale marks still apply to the cached copy, so it restores stale and reloads.
  • The order is by wall clock so it holds across an owner restart; requests the owner starts in one millisecond are ordered by a one-millisecond step.

Provenance

  • An agent produced this diff (agent-authored)

Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.


Note

Medium Risk
Changes shared cross-tab persistence ordering; wrong logic could drop valid saves or leave stale cache entries, though optional protocol fields preserve older builds.

Overview
Fixes a race when multiple tabs share one data owner: if two tabs reload the same read and the older request finishes last, persistence could write that stale value over a newer one, so the next session restored old data while live tabs showed the update.

The owner now stamps each shared read with a monotonic request start time (at on replies and cross-tab value messages) and tracks newestSaved per cache key. Forwarded save calls from replicas include requestedAt on Saving; the owner ignores saves from an older request once a newer one's value is already stored. Stores record requestedAt on entries (via requestTimes on replies) and pass it through on persist.

Docs in the cross-tab owner section describe this rule. Tests run the two-tab reload scenario with a shared encrypted cache and assert offline restore keeps the newer title.

Reviewed by Cursor Bugbot for commit 168d0d6. Bugbot is set up for automated code reviews on this repo. Configure here.

Two tabs sharing an owner could reload the same read and leave the older
result saved for the next session: the owner ordered live results by request
but kept whichever tab's save arrived last.

Each read the owner starts now has a start time later than every read before
it, which its reply carries. A replica's value remembers the request that read
it, and the owner does not save a value over one a newer request read.

Fixes #2934
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix labels Nov 15, 2026
@trueadm
trueadm marked this pull request as ready for review November 15, 2026 21:53
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
octane-website 168d0d6 Commit Preview URL

Branch Preview URL
Nov 15 2026, 10:00 PM

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
octane-website-mcp 168d0d6 Commit Preview URL

Branch Preview URL
Nov 15 2026, 09:58 PM

@github-actions

Copy link
Copy Markdown
Contributor

Benchmark report

🟢 No byte growth, no added work, and no timing changes beyond ±3%.

Compares 0b5acbc7f, the merge commit's first parent with 38edad55c on the same runner. Any increase in a js-framework work counter fails this check; byte changes are a report. Wall time is a report: base and head samples alternate in one browser, and an operation is called slower or faster only when the 95% confidence interval of its ratio excludes ±3%. Workflow run

js-framework: 1k/10k row operations

All 20 timed operations (20 without a verdict)
target operation base ms head ms head/base 95% CI
octane-tsrx run 2.658 2.708 ±0.0% −1.7% … +3.7% ⚪ within noise
octane-tsrx replace 5.342 5.358 −1.8% −3.9% … +3.7% ⚪ within noise
octane-tsrx add 2.208 2.458 +2.1% −0.0% … +7.0% ⚪ within noise
octane-tsrx update 0.097 0.095 ±0.0% −2.0% … +1.2% ⚪ within noise
octane-tsrx select 0.005 0.006 +2.0% +1.1% … +4.8% ⚪ within noise
octane-tsrx swap 0.019 0.018 −0.4% −2.5% … +2.9% ⚪ within noise
octane-tsrx remove 0.015 0.014 ±0.0% −5.0% … +2.6% ⚪ within noise
octane-tsrx runlots 22.292 23.667 +5.0% +1.0% … +9.3% ⚪ within noise
octane-tsrx select_lots 0.006 0.006 +2.0% +0.3% … +4.0% ⚪ within noise
octane-tsrx clear 39.392 39.575 +0.9% −0.4% … +1.8% ⚪ within noise
octane-jsx run 3.167 3.433 +3.2% −0.0% … +6.1% ⚪ within noise
octane-jsx replace 5.608 5.517 −2.5% −5.5% … +0.9% ⚪ within noise
octane-jsx add 2.942 3.042 +1.7% −1.7% … +6.5% ⚪ within noise
octane-jsx update 0.164 0.160 +2.0% +0.7% … +3.8% ⚪ within noise
octane-jsx select 0.078 0.079 +1.1% +0.5% … +1.9% ⚪ within noise
octane-jsx swap 0.118 0.121 +0.6% −2.1% … +2.5% ⚪ within noise
octane-jsx remove 0.105 0.102 −0.3% −1.2% … +0.9% ⚪ within noise
octane-jsx runlots 26.058 26.308 +3.5% −1.3% … +6.2% ⚪ within noise
octane-jsx select_lots 0.798 0.812 −1.4% −2.7% … −0.5% ⚪ within noise
octane-jsx clear 41.558 42.308 +0.5% −0.5% … +1.6% ⚪ within noise

All 64 work counters are unchanged.

Application bundle size (bytes)

No changes across 111 measured values.

Public-import reachability bundles (bytes)

No changes across 141 measured values.

@trueadm
trueadm merged commit 801df5d into main Nov 15, 2026
32 checks passed
@trueadm
trueadm deleted the fix/data-owner-save-order branch November 15, 2026 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

data: a late replica refresh overwrites the newer persisted value for the same key

1 participant