Skip to content

fix(hydration): key signal owners below returned JSX by authored invocations only - #1601

Merged
trueadm merged 6 commits into
mainfrom
fix/returned-jsx-signal-owner
Oct 2, 2026
Merged

trueadm merged 6 commits into
mainfrom
fix/returned-jsx-signal-owner

Conversation

@trueadm

@trueadm trueadm commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

A signal component rendered below .tsx returned-JSX components got a different owner instance key on the server than in the browser, so hydration could not match the server-seeded owners. This happens on main.

  • Server (authoritative): one level per authored component invocation, e.g. ["","root"]["c:Wrap"]["c:Mid"]["c:Leaf"].
  • Client: an extra level for every component's synthesized _frag$N renderer, e.g. [root][c:frag][c:Wrap][c:frag][c:Mid][c:frag][c:Leaf]. In a module that imports octane/signals, every value-position host descriptor block added a "legacy" level instead.

The server side is authoritative. Renderers and host bodies are how the client represents a component's JSX; they are not authored invocations. The server renders the same JSX inline, a .tsrx body has no renderer, and identity must not depend on the representation. On main the client keys even changed when a module began importing octane/signals.

Changes

  • Compiler (compile.js): synthesized fragment renderers now carry a renderer invocation site ('r:') in place of a hashed c: site. That covers the client _frag$N (lowerHostFragment) and the server __sfragment and _sfrag$N wrappers (ssrEmitTsrxExpression, serverValueDirectiveFold). The shorter literal also saves bytes in every returned-JSX component.
  • Client (runtime.ts): Blocks with that site, value-position host descriptor Blocks (hostElementBody), and dynamic tags that resolve to a host string now add no key segment. All the work happens when the Block is stamped (stampSignalInstanceKey):
    • A renderer takes its parent scope's stamp, so it resolves to the parent's key. That parent can be a stamped lite scope its Block chain would skip.
    • Below an unstamped parent (an arm or a row), the renderer stays unstamped, so keys walk through it and keep the pending @for/list item tokens.
    • structuralSignalInstanceKey and scopeSignalOwner are unchanged from main. The first push changed them, which put the fix on every bundle's path and failed the root-chained-jsx budget.
  • Server (runtime.server.ts): ssrComponent with the renderer site keeps the enclosing SIGNAL_COMPONENT_INSTANCE_KEY. Its frame inherits the pending SIGNAL_LIST_KEYS instead of resetting them. A discovery job for such a frame retains those list keys, so a suspended renderer replay doesn't create an owner the final pass and the browser never use.
  • Docs: docs/signals.md now states that only authored component invocations shape instance identity. Changeset (octane patch).

A temporary probe compared server and client keys for 23 returned-JSX shapes in both lowerings (plain and signals-importing) with .tsx/.tsrx leaves. On main, 20 of the 23 shapes diverge in at least one variant: return chains, host values in props and children, .map rows, direct calls, dynamic host tags, a host value inside a hookless .tsrx component, and @for rows of returned-JSX components. With this change all 92 of those variants agree. The .tsrx {props.children} render-function level ("legacy") was already the same on both sides and is unchanged. One related shape still diverges: a host or component value rendered under an @if arm inside a hookless (lite) .tsrx component. That is a separate pre-existing bug on main: the Block chain under a lite scope skips the lite level and adds a stray u: token. It is not caused by renderers, so it's left out of this PR (see follow-ups).

Validation

  • pnpm format:check: not run repo-wide. pnpm format:files:check passes on every changed file.
  • pnpm typecheck: not run repo-wide. pnpm typecheck:files is clean for runtime.ts, runtime.server.ts and the test file.
  • pnpm test: not run locally, to keep load off the shared machine. CI runs it.
  • targeted tests: vitest run --project octane packages/octane/tests/hydration/signal-instance-identity.test.ts passes 153/153 on the current head. With the renderer stamp disabled, all 40 new cases fail. The new cases:
    • keeps signal owners below returned JSX identical through hydration: 32 cases covering 8 shapes, keyed host .map rows among them, plain and signals-importing app modules, dev and prod. It asserts that server and client owner instance keys are equal, that the <output> nodes are adopted, and and that an edit through the returned JSX's onClick updates both that JSX's output and the component body's own read. All 32 fail on main at the key assertion.
    • resumes server queries below returned JSX under their server owners: 8 cases. Using prerender, streamed signals and hydration, it asserts the browser never calls its loader, the server result and nodes are kept, and the server's set of query owners equals the browser's. All 8 fail on main. With only the server job change disabled, the 2 plain-module keyed-row cases fail on a stray discovery owner that lacks the row key.
  • pnpm sync leaves the tree clean.
  • node benchmarks/bundle-size/run-minimal.mjs --budgets root-static-local root-chained-jsx hooks-state prop-attributes context passes. root-chained-jsx, root-static-local, hooks-state, prop-attributes, cli-spa-starter and hydrate-root are byte-identical to base. Bundles that reach the generic componentSlot/childSlot paths grow by about 165–195 raw bytes (context: +166 raw, +76 brotli). server-render grows by +198 raw.

Risk / follow-ups

  • Identity change: signal instance keys change for every component below returned JSX. Server and client change together, so this only matters for state persisted across a deploy and keyed by instance (none known).
  • Performance: non-signal apps pay one store or typeof check in childSlot/componentSlot, and one comparison when a Block is stamped while signals are enabled. Key resolution stays lazy. Server ssrComponent gains one string compare. Compiled returned-JSX output shrinks slightly ('r:' replaces an 8-hex-digit site). Timing lanes are within noise.
  • Owner semantics: a renderer below a stamped scope gets its own owner carrying that scope's instance key. Its cells are separate (they are keyed by owner object) and its seeds match the server's. Handlers in returned JSX keep resolving the component's cells, which the new tests check.
  • Not fixed here: values under an @if arm inside a lite component lose the lite level (pre-existing walk bug at the lite Block stand-in, described above).
  • Not fixed here (pre-existing on main, found while writing the resume test): a .tsx component that calls query$(...).get() during render re-renders without end after hydrating a resumed query. The test therefore uses a .tsrx query component with a bound {value$} child.

Provenance

  • An agent produced this diff (agent-authored)

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Note

Medium Risk
Changes signal instance key computation across compiler, client, and SSR runtimes for all trees under returned JSX; server and client move together but any external state keyed by old instance IDs would break.

Overview
Fixes signal owner / instance key mismatches between SSR and hydration when signals or queries sit under returned JSX (.tsx returns, host value props, dynamic host tags, keyed rows, etc.). The client used to add extra identity levels for compiled fragment renderers (_frag$N) and host descriptors; the server inlined that markup, so keys diverged and hydration could not resume server cells or query seeds.

The compiler now tags those renderers with a shared 'r:' renderer invocation site instead of a per-location c: hash. Client stampSignalInstanceKey treats that site as transparent (inherits the enclosing component’s key; dynamic host tags get the same treatment). Server ssrComponent skips a new instance segment for renderers, continues pending list keys, and discovery replays retain those keys for suspended rows.

Docs state that only authored component invocations shape instance identity (importing octane/signals no longer shifts keys). New hydration tests cover many returned-JSX shapes plus streamed query resume.

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

…cations only

The client gave every compiled `_frag$N` fragment renderer and every host
descriptor Block its own signal instance level, while the server renders the
same returned JSX inline. A signal component below `.tsx` returned-JSX
components therefore had a different owner instance key on each side, so
hydration missed the server's cells and seeds.

Compiler-synthesized renderers (client `_frag$N`, server `__sfragment` and
`_sfrag$N`) now carry a renderer invocation site. Both runtimes give such
renderers, host descriptors and dynamic tags that resolve to a host no key
segment: keys below them continue from the scope they were stamped under,
keeping pending list keys, and their Blocks borrow the enclosing owner. Server
discovery jobs for a renderer frame retain the list keys it continued.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix labels Oct 2, 2026
@trueadm
trueadm marked this pull request as ready for review October 2, 2026 19:33
@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

  • 🔴 bundle-size: 23 value(s) increased
  • 🔴 bundle-reachability: 36 value(s) increased

Compares 3ec43daa4 with d95a58406 on the same runner. Timing verdicts require a change beyond the combined margin of error (at least 5%) and one 0.1ms timer tick, with the fastest sample also moving beyond that margin. Workflow run

js-framework: 1k/10k row operations (ms, lower is better)

target operation base head Δ noise
octane-tsrx runlots 41.90 34.17 −18.4% ±16% 🟢 faster
All 20 timed operations (19 within noise)
target operation base head Δ noise
octane-tsrx run 4.31 4.49 +4.2% ±32% ⚪ within noise
octane-tsrx replace 10.25 10.08 −1.7% ±36% ⚪ within noise
octane-tsrx add 4.19 3.97 −5.3% ±21% ⚪ within noise
octane-tsrx update 0.64 0.67 +4.7% ±53% ⚪ within noise
octane-tsrx select 0.28 0.24 −14.3% ±59% ⚪ within noise
octane-tsrx swap 0.56 0.52 −7.1% ±42% ⚪ within noise
octane-tsrx remove 0.53 0.48 −9.4% ±40% ⚪ within noise
octane-tsrx runlots 41.90 34.17 −18.4% ±16% 🟢 faster
octane-tsrx select_lots 0.37 0.26 −29.7% ±48% ⚪ within noise
octane-tsrx clear 49.20 48.46 −1.5% ±25% ⚪ within noise
octane-jsx run 4.56 5.00 +9.6% ±32% ⚪ within noise
octane-jsx replace 11.09 10.03 −9.6% ±22% ⚪ within noise
octane-jsx add 3.86 4.02 +4.1% ±14% ⚪ within noise
octane-jsx update 0.88 0.86 −2.3% ±40% ⚪ within noise
octane-jsx select 0.63 0.64 +1.6% ±37% ⚪ within noise
octane-jsx swap 0.88 0.89 +1.1% ±42% ⚪ within noise
octane-jsx remove 0.67 0.71 +6.0% ±24% ⚪ within noise
octane-jsx runlots 32.02 33.98 +6.1% ±18% ⚪ within noise
octane-jsx select_lots 2.95 2.92 −1.0% ±23% ⚪ within noise
octane-jsx clear 52.46 57.58 +9.8% ±14% ⚪ within noise

Application bundle size (bytes)

target metric base head Δ
octane-tsrx todo_js_raw 141,842 142,008 +166 (+0.1%) 🔴
octane-tsrx todo_js_gzip 46,829 46,870 +41 (+0.1%) 🔴
octane-tsrx todo_js_brotli 41,092 41,050 −42 (−0.1%) 🟢
octane-tsrx todo_app_gzip 2,476 2,477 +1 (+0.0%) 🔴
octane-tsrx todo_app_brotli 2,121 2,119 −2 (−0.1%) 🟢
octane-tsrx todo_fw_raw 136,732 136,898 +166 (+0.1%) 🔴
octane-tsrx todo_fw_gzip 44,353 44,393 +40 (+0.1%) 🔴
octane-tsrx todo_fw_brotli 38,971 38,931 −40 (−0.1%) 🟢
octane-tsrx chat_js_raw 141,051 141,217 +166 (+0.1%) 🔴
octane-tsrx chat_js_gzip 46,781 46,816 +35 (+0.1%) 🔴
octane-tsrx chat_js_brotli 41,136 41,160 +24 (+0.1%) 🔴
octane-tsrx chat_app_gzip 2,979 2,977 −2 (−0.1%) 🟢
octane-tsrx chat_app_brotli 2,627 2,633 +6 (+0.2%) 🔴
octane-tsrx chat_fw_raw 134,398 134,564 +166 (+0.1%) 🔴
octane-tsrx chat_fw_gzip 43,802 43,839 +37 (+0.1%) 🔴
octane-tsrx chat_fw_brotli 38,509 38,527 +18 (+0.0%) 🔴
octane-tsrx weather_js_raw 178,001 178,167 +166 (+0.1%) 🔴
octane-tsrx weather_js_gzip 58,195 58,224 +29 (+0.0%) 🔴
octane-tsrx weather_js_brotli 50,613 50,585 −28 (−0.1%) 🟢
octane-tsrx weather_app_brotli 7,578 7,556 −22 (−0.3%) 🟢
octane-tsrx weather_fw_raw 151,266 151,432 +166 (+0.1%) 🔴
octane-tsrx weather_fw_gzip 49,150 49,179 +29 (+0.1%) 🔴
octane-tsrx weather_fw_brotli 43,035 43,029 −6 (−0.0%) 🟢
octane-jsx js_raw 226,534 226,719 +185 (+0.1%) 🔴
octane-jsx js_gzip 72,280 72,345 +65 (+0.1%) 🔴
octane-jsx js_brotli 62,530 62,703 +173 (+0.3%) 🔴
octane-jsx app_raw 8,467 8,459 −8 (−0.1%) 🟢
octane-jsx app_gzip 2,944 2,936 −8 (−0.3%) 🟢
octane-jsx app_brotli 2,497 2,499 +2 (+0.1%) 🔴
octane-jsx fw_raw 218,067 218,260 +193 (+0.1%) 🔴
octane-jsx fw_gzip 69,336 69,409 +73 (+0.1%) 🔴
octane-jsx fw_brotli 60,033 60,204 +171 (+0.3%) 🔴

13 other measured values are unchanged.

Public-import reachability bundles (bytes)

target metric base head Δ
root-static raw 202,854 203,047 +193 (+0.1%) 🔴
root-static gzip 64,487 64,541 +54 (+0.1%) 🔴
root-static brotli 55,913 55,935 +22 (+0.0%) 🔴
context raw 110,689 110,855 +166 (+0.1%) 🔴
context gzip 36,668 36,707 +39 (+0.1%) 🔴
context brotli 32,470 32,471 +1 (+0.0%) 🔴
deferred-hydration raw 230,392 230,558 +166 (+0.1%) 🔴
deferred-hydration gzip 73,105 73,140 +35 (+0.0%) 🔴
deferred-hydration brotli 63,041 63,080 +39 (+0.1%) 🔴
suspense-transition raw 167,220 167,386 +166 (+0.1%) 🔴
suspense-transition gzip 53,743 53,800 +57 (+0.1%) 🔴
suspense-transition brotli 46,939 47,001 +62 (+0.1%) 🔴
server-render raw 64,842 65,040 +198 (+0.3%) 🔴
server-render gzip 22,351 22,423 +72 (+0.3%) 🔴
server-render brotli 20,120 20,253 +133 (+0.7%) 🔴
binding-base-ui-vite raw 213,045 213,238 +193 (+0.1%) 🔴
binding-base-ui-vite gzip 68,059 68,108 +49 (+0.1%) 🔴
binding-base-ui-vite brotli 59,041 59,031 −10 (−0.0%) 🟢
binding-base-ui-esbuild raw 211,004 211,195 +191 (+0.1%) 🔴
binding-base-ui-esbuild gzip 68,038 68,094 +56 (+0.1%) 🔴
binding-base-ui-esbuild brotli 59,014 59,071 +57 (+0.1%) 🔴
binding-aria-esbuild gzip 3,363 3,361 −2 (−0.1%) 🟢
binding-radix-vite raw 214,975 215,168 +193 (+0.1%) 🔴
binding-radix-vite gzip 68,623 68,682 +59 (+0.1%) 🔴
binding-radix-vite brotli 59,498 59,573 +75 (+0.1%) 🔴
binding-radix-esbuild raw 212,894 213,085 +191 (+0.1%) 🔴
binding-radix-esbuild gzip 68,614 68,679 +65 (+0.1%) 🔴
binding-radix-esbuild brotli 59,485 59,627 +142 (+0.2%) 🔴
binding-mantine-hooks-esbuild raw 97,678 97,682 +4 (+0.0%) 🔴
binding-mantine-hooks-esbuild gzip 32,788 32,790 +2 (+0.0%) 🔴
binding-mantine-hooks-esbuild brotli 29,107 29,141 +34 (+0.1%) 🔴
binding-apollo-client-vite raw 358,100 358,293 +193 (+0.1%) 🔴
binding-apollo-client-vite gzip 112,190 112,238 +48 (+0.0%) 🔴
binding-apollo-client-vite brotli 97,843 97,849 +6 (+0.0%) 🔴
binding-apollo-client-esbuild raw 361,051 361,242 +191 (+0.1%) 🔴
binding-apollo-client-esbuild gzip 114,404 114,445 +41 (+0.0%) 🔴
binding-apollo-client-esbuild brotli 99,665 99,547 −118 (−0.1%) 🟢
binding-usehooks-ts-esbuild raw 98,300 98,304 +4 (+0.0%) 🔴
binding-usehooks-ts-esbuild gzip 32,933 32,936 +3 (+0.0%) 🔴
binding-usehooks-ts-esbuild brotli 29,275 29,236 −39 (−0.1%) 🟢

65 other measured values are unchanged.

trueadm and others added 2 commits October 2, 2026 20:59
The first version taught structuralSignalInstanceKey and scopeSignalOwner to
see through renderer Blocks. Both are reachable in every bundle, so even
signal-free apps paid about 320 bytes and root-chained-jsx exceeded its
committed budget.

A renderer now takes its parent scope's stamp when it is created: it resolves
to the parent's key, adds no segment, and stays unstamped below an unstamped
parent so keys walk on through it. The key walk and owner lookup are unchanged
from main, so the minimal bundles are byte-identical to it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@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.

Stale Bugbot comment from a previous run.

Comment thread packages/octane/src/runtime.ts Resolved
trueadm and others added 2 commits October 2, 2026 21:19
…wn cell

Read the Leaf's signal in its body as well as in its returned JSX, and assert
that a click in the returned JSX updates both. A renderer Block keeps its own
owner (carrying the component's instance key), so this guards that handlers
and reads inside it still resolve the component's cell.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@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 is ON. A cloud agent has been kicked off to fix the reported issue. You can view the agent here.

Reviewed by Cursor Bugbot for commit 259c79c. Configure here.

Comment thread packages/octane/src/runtime.ts Resolved
Each `.map` row that returns a keyed host element renders through its own
host renderer inside the row. Assert that both rows keep separate, server-equal
signal owners, so a renderer never hides its row's item key.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cursor

cursor Bot commented Oct 2, 2026

Copy link
Copy Markdown

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

  • ✅ Fixed: Renderers mint duplicate signal owners
    • Pre-mark renderer scopes with false in SCOPE_SIGNAL_OWNERS during stampSignalInstanceKey, then detect the borrowed identity in scopeSignalOwner by matching recipe fields against the enclosing scope's, delegating to the enclosing scope's owner instead of creating a duplicate.

Create PR

Or push these changes by commenting:

@cursor push b948375229
Preview (b948375229)
diff --git a/packages/octane/src/runtime.ts b/packages/octane/src/runtime.ts
--- a/packages/octane/src/runtime.ts
+++ b/packages/octane/src/runtime.ts
@@ -913,6 +913,11 @@
 	scope.signalInstanceValue = renderer ? parent.signalInstanceValue : key.key;
 	scope.signalInstanceHasKey = renderer ? parent.signalInstanceHasKey : key.hasKey;
 	scope.signalInstanceResolved = renderer ? parent.signalInstanceResolved : undefined;
+	// A renderer's identity is its parent's: scopeSignalOwner must borrow the
+	// enclosing scope's owner rather than mint a duplicate with the same
+	// instanceKey. Pre-mark false so it enters the borrowed-identity path and
+	// deletion does not retire the parent's cells.
+	if (renderer) SCOPE_SIGNAL_OWNERS.set(scope, false);
 }
 
 function structuralSignalInstanceKey(
@@ -1017,6 +1022,25 @@
 			parent = parent.parent ?? parent.block.parentBlock;
 		}
 	}
+	// A renderer carries its enclosing scope's signal instance recipe
+	// (stampSignalInstanceKey, RENDERER_INVOCATION_SITE) and is pre-marked
+	// false. Borrow the enclosing scope's owner so the renderer does not mint
+	// a duplicate with the same instanceKey: the server keeps one owner per
+	// key, and retiring a duplicate on unmount would dispose the parent's cells.
+	if (
+		owner === false &&
+		(scope.signalInstanceParent !== null || scope.signalInstanceResolved !== undefined)
+	) {
+		const enclosing: Scope | null = scope.parent ?? scope.block.parentBlock;
+		if (
+			enclosing !== null &&
+			!(enclosing instanceof LiteBlockImpl) &&
+			scope.signalInstanceParent === enclosing.signalInstanceParent &&
+			scope.signalInstanceSite === enclosing.signalInstanceSite
+		) {
+			return scopeSignalOwner(enclosing);
+		}
+	}
 	const retired = owner === null;
 	if (!owner || owner.documentOwner !== documentOwner) {
 		const ids = scope.block.idState;

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

@trueadm

trueadm commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator Author

Not adopting the Bugbot Autofix branch (cursor/duplicate-signal-owners-252b, b948375). Three reasons:

  • Budget: its borrow lives in scopeSignalOwner, which every bundle reaches. With it, node benchmarks/bundle-size/run-minimal.mjs --budgets … root-chained-jsx … fails at 86,257 raw against the committed 86,016, the same PR gate the first version of this PR failed.
  • Correctness: it finds the enclosing scope through scope.parent ?? scope.block.parentBlock and skips LiteBlockImpl. A renderer whose parent is a hookless (lite) component would still get its own owner. Matching recipe fields is also a heuristic: false entries are written for ordinary scopes too, when an event token escapes.
  • Not needed: a renderer keeping its own owner, which carries the component's instance key, is deliberate, as explained on the resolved thread. Handler writes reach the component body's own read, which 9000e0d pins. Seeds match the server. Declarations a native renderer evaluates in its lazy children share that Block's lifetime, like the hooks evaluated there.

🤖 Addressed by Claude Code

@trueadm
trueadm merged commit 3971840 into main Oct 2, 2026
33 checks passed
@trueadm
trueadm deleted the fix/returned-jsx-signal-owner branch October 2, 2026 21:28
trueadm added a commit that referenced this pull request Oct 2, 2026
…another request (#1618)

A hydration frame rejected a resource seed whose request no longer matched the
client's selection with a hard SignalFrameError ("The presented query
definition does not match"), even under the native adoption resolver. The live
binding already treats a seed for another request as no seed and starts the
client load, so only the historical read failed.

The frame now leaves such a read to the live graph during hydration, exactly
like a request the server never seeded: the client loads its own selection once,
the boundary (or root) waits for it, and adoption keeps the server's nodes while
reporting changed text as a recoverable hydration mismatch. An explicit
`beginAdoption` frame keeps the error, since it has no live fallback.

The defect predates #1601: a `.tsrx` app whose props select another request
than the server's already failed. #1601 gave `.tsx` returned-JSX components the
server's owner key, which exposed it to #1607's tests, whose browser-only
selection previously landed under an unseeded owner.

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

1 participant