Skip to content

fix(data): save a validated reply from the owner's copy, not the tab's - #2941

Merged
trueadm merged 5 commits into
mainfrom
fix/data-owner-save-unchanged
Nov 16, 2026
Merged

trueadm merged 5 commits into
mainfrom
fix/data-owner-save-unchanged

Conversation

@trueadm

@trueadm trueadm commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Why

  • The owner is the only writer of the cache, but only a tab can validate a value, so every save went through the tab. The owner already held the same value.

Changes

  • owner.ts, tab side: a request says whether the tab saves that read (keep: the tab has persist, the read has persist, and it is not a view, whose saved rows are not its reply). A reply or broadcast value the owner kept is remembered by identity. At save time, a value that is still that very object is saved by token: stored.value is left out. Anything else is sent in full as before: a validator that returned another representation, a value edited since, a primitive, a view's rows, and every later save of the same value.
  • owner.ts, owner side: keeps a reply for each such read until a tab saves it or a newer request's value. A reply that lands while another is still kept is not kept, so its tabs send it back in full. A save without a value takes the kept reply if its request matches; if not, that reply or a newer request's value was already saved, and the save is dropped. The ordering from fix(data): keep the newest request's value in the shared owner's cache #2940 still applies, so a reply superseded while a tab validates it stays superseded.
  • docs/data.md: the Cross-tab owner section states the guarantee and its limits.
  • No protocol bump: keep and kept are optional. An older owner never says kept, so a newer tab sends values in full; an older tab never asks to keep, so a newer owner keeps nothing for it.

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-persist.test.ts and data-owner.test.ts in --project octane (105 pass), react-compat/data.test.ts in --project octane-hosted-react-dev (12 pass).

  • Failing first: saves a value a tab validated unchanged from the owner’s copy, so no tab sends it back records everything two tabs post to their owner, for a tab that asked and a tab that was told the value. Before the fix it fails on the save message carrying the rows; after it, no message carries them and a restarted tab still starts from the saved value with no request.

  • Neighbouring behaviour: saves the representation a tab’s validator returned, where it is not the owner’s pins that a transforming validator's output is what is saved. Making the owner use its own copy for every save of a kept reply turns it red (expected { checked: 1 } to deeply equal { checked: 2 }). The crossing-replies test from fix(data): keep the newest request's value in the shared owner's cache #2940 uses identity validators, so it now runs through this path and covers a reply superseded while it validates.

  • Review finding (a newer reply replacing a kept one that no tab had saved): saves a value a tab validated when a newer reply that no tab can save lands before it does failed with a restart restoring Kindred while the tab showed Beloved; it passes now.

  • Measurement: time inside the tab's postMessage of the save message, same probe on both branches, Node 24 MessageChannel, arrays of four-field objects, identity validator, median of 7:

    Rows Before After
    5,000 0.675 ms (0.546–1.289) 0.008 ms (0.003–0.020)
    50,000 5.816 ms (5.697–8.860) 0.005 ms (0.004–0.010)

    Not measured in a browser with a real SharedWorker; the issue's Chromium numbers for the same message were 0.8 ms and 8.9 ms.

  • perf-review: node scripts/perf-review-scan.mjs reports no candidates. Touched paths run once per request, reply or forwarded save, never per render or node. Per save the tab does one WeakMap.get; the fast path allocates two small objects in place of cloning the value. No DOM, scheduling or compiled-output change.

  • concise-code: src +83 −13, all in owner.ts (runtime, octane/data only); no new files or exports; alternatives: save the owner's reply as soon as it arrives (smaller, rejected: the tab's validation is what admits a value to the cache, and a persist: false read would be saved), keep every reply without the tab's keep hint (about 10 lines fewer, rejected: the worker would hold persist: false values and view windows no tab saves), keep the value for every later save too (rejected: a second copy of the whole cache in worker memory); bytes: not applicable.

Risk / follow-ups

  • The owner holds one reply per read key from the reply until a tab saves it, which is normally the next task. If the tab that asked never saves it (its validation failed, or its store was retired), the reply stays until a tab saves a later reply for that key, or sign-out; until then replies for that key are sent back in full.
  • Later saves of the same value still send it: a stale mark or an edit re-saves the whole value from the tab. Sending those as metadata is a possible follow-up; it needs the owner to rewrite a saved value's metadata without the value.
  • Views are unchanged: a view saves its confirmed rows, which the owner does not have.

Provenance

  • An agent produced this diff (agent-authored)

Note

Medium Risk
Changes the cross-tab owner persistence protocol and save ordering; optional keep/kept preserve older peers, but incorrect kept-state handling could corrupt the shared cache.

Overview
Fixes redundant structured cloning when a tab shares an octane/data owner: previously a read crossed worker → page for validation, then the whole value was posted back for persistence even though the owner already held the same reply.

Tabs now mark persistable reads with keep, the owner retains eligible replies in kept, and replies/value broadcasts can carry kept. After an identity-preserving output validator, flush sends a save with no stored.value and a validation token (requestedAt); the owner fills the save from its kept copy. Values still cross in full when the validator returns a different representation, the object was edited, or an older reply could not be kept.

docs/data.md documents the guarantee and limits (persist: false, views). data-persist.test.ts adds coverage including message inspection so book titles never appear in postMessage payloads, ordering when a newer reply cannot be saved, and transforming validators still persisting the tab's representation.

Reviewed by Cursor Bugbot for commit d2d8cbe. 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
A read fetched by the shared owner crossed to the tab for validation and then
crossed back in full to be saved, a second structured clone on the page.

A tab now says which reads it saves, and the owner keeps its reply to each
until a tab saves it. Where the tab's validator returned the value it was
given, the tab's save names the request and carries no value, and the owner
saves its own copy. A validator that returns another representation, and a
value changed since it loaded, are sent as before.

Fixes #2935
@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 22:03
@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 d2d8cbe Commit Preview URL

Branch Preview URL
Nov 15 2026, 10:50 PM

@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-mcp d2d8cbe Commit Preview URL

Branch Preview URL
Nov 15 2026, 10:51 PM

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

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

Compares 1950df730, the merge commit's first parent (main has moved 1 commit(s) past the pull request's recorded base 801df5d4f) with 5b7e3cda6 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.458 2.483 ±0.0% −3.8% … +1.9% ⚪ within noise
octane-tsrx replace 4.583 4.592 −0.0% −2.2% … +2.2% ⚪ within noise
octane-tsrx add 2.092 2.142 +0.0% ±0.0% … +4.5% ⚪ within noise
octane-tsrx update 0.087 0.087 +0.2% −0.5% … +2.1% ⚪ within noise
octane-tsrx select 0.005 0.005 +0.6% +0.3% … +1.1% ⚪ within noise
octane-tsrx swap 0.016 0.016 −0.3% −0.6% … +0.0% ⚪ within noise
octane-tsrx remove 0.013 0.013 +1.0% ±0.0% … +2.9% ⚪ within noise
octane-tsrx runlots 21.008 19.958 +2.1% −2.5% … +3.6% ⚪ within noise
octane-tsrx select_lots 0.006 0.006 +1.0% +0.5% … +1.1% ⚪ within noise
octane-tsrx clear 43.133 44.542 +0.5% −0.8% … +2.3% ⚪ within noise
octane-jsx run 3.250 3.208 ±0.0% −0.0% … +3.0% ⚪ within noise
octane-jsx replace 5.500 5.417 +1.6% −4.4% … +8.3% ⚪ within noise
octane-jsx add 3.025 2.992 ±0.0% −3.2% … +3.2% ⚪ within noise
octane-jsx update 0.160 0.162 +1.8% ±0.0% … +2.8% ⚪ within noise
octane-jsx select 0.075 0.076 +0.9% +0.0% … +1.8% ⚪ within noise
octane-jsx swap 0.120 0.119 ±0.0% −0.6% … +0.3% ⚪ within noise
octane-jsx remove 0.101 0.100 −0.6% −1.1% … +0.3% ⚪ within noise
octane-jsx runlots 25.233 24.092 −1.2% −3.6% … +1.8% ⚪ within noise
octane-jsx select_lots 0.858 0.849 −0.9% −1.2% … ±0.0% ⚪ within noise
octane-jsx clear 45.000 45.692 +1.8% −0.9% … +3.1% ⚪ 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.

…ta-owner-save-unchanged

# Conflicts:
#	docs/data.md
#	packages/octane/src/data/owner.ts

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Newer reply can drop unsaved keep
    • Guarded kept.set so an existing entry whose at exceeds newestSaved is never overwritten; the newer tab receives kept: false and sends the full value in its save instead, preventing the older validated reply from being silently dropped.

Create PR

Or push these changes by commenting:

@cursor push beb056f83b
Preview (beb056f83b)
diff --git a/packages/octane/src/data/owner.ts b/packages/octane/src/data/owner.ts
--- a/packages/octane/src/data/owner.ts
+++ b/packages/octane/src/data/owner.ts
@@ -497,9 +497,14 @@ export function createDataOwner(options: {
 					(reply) => {
 						if (!reply.ok) return;
 						// Kept for the tabs that save it, unless a newer request's reply was kept or saved.
-						const newer = kept.get(cacheKey)?.at ?? newestSaved.get(cacheKey) ?? 0;
-						if (asking.keep && !receipt && options.persist && newer < at) {
-							kept.set(cacheKey, { at, value: reply.value });
+						const existing = kept.get(cacheKey);
+						const savedAt = newestSaved.get(cacheKey) ?? 0;
+						if (asking.keep && !receipt && options.persist && (existing?.at ?? savedAt) < at) {
+							// An unsaved kept reply must not be overwritten: the tab it was
+							// promised to will send a valueless save that needs the value here.
+							if (!existing || existing.at <= savedAt) {
+								kept.set(cacheKey, { at, value: reply.value });
+							}
 						}
 						// A newer read has replaced this one; its value is the one to share.
 						if (newest.get(key) !== started) return;

@@ -497,9 +497,14 @@ export function createDataOwner(options: {
 					(reply) => {
 						if (!reply.ok) return;
 						// Kept for the tabs that save it, unless a newer request's reply was kept or saved.
-						const newer = kept.get(cacheKey)?.at ?? newestSaved.get(cacheKey) ?? 0;
-						if (asking.keep && !receipt && options.persist && newer < at) {
-							kept.set(cacheKey, { at, value: reply.value });
+						const existing = kept.get(cacheKey);
+						const savedAt = newestSaved.get(cacheKey) ?? 0;
+						if (asking.keep && !receipt && options.persist && (existing?.at ?? savedAt) < at) {
+							// An unsaved kept reply must not be overwritten: the tab it was
+							// promised to will send a valueless save that needs the value here.
+							if (!existing || existing.at <= savedAt) {
+								kept.set(cacheKey, { at, value: reply.value });
+							}
 						}
 						// A newer read has replaced this one; its value is the one to share.
 						if (newest.get(key) !== started) return;

You can send follow-ups to the cloud agent here.

Reviewed by Cursor Bugbot for commit 885557e. Configure here.

Comment thread packages/octane/src/data/owner.ts Resolved
A newer reply to the same read replaced the one the owner kept. If that newer
reply was never saved, such as one that failed validation, the tab holding the
earlier value had its save dropped and the value never reached the cache.

The owner now keeps a reply until a tab saves it or a newer request's value,
and a reply that lands meanwhile is sent back in full by its tabs.
@trueadm
trueadm merged commit 82828d1 into main Nov 16, 2026
35 checks passed
@trueadm
trueadm deleted the fix/data-owner-save-unchanged branch November 16, 2026 00:37
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: SharedWorker persistence makes the page clone a worker-originated read back to its owner

1 participant