Skip to content

fix: form Float precedence groups in tree discovery order on client mounts - #739

Merged
trueadm merged 2 commits into
mainfrom
fix/try-precedence-group-order
Aug 12, 2026
Merged

trueadm merged 2 commits into
mainfrom
fix/try-precedence-group-order

Conversation

@trueadm

@trueadm trueadm commented Aug 12, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Float precedence groups now form in tree discovery order on client mounts — parent before child, suspended @try arms at reveal — matching SSR and React. Previously groups formed in body-completion order, so a nested child's or arm's group could precede its parent's ([three, one, two] where SSR gives [one, two, three]).
  • Fixes a latent SSR crash the same placement rule exposed: a hoisted head element or Float resource whose attribute reads a setup local (const slug = …; <link href={slug} precedence …>) emitted its registration ahead of the local's declaration — a TDZ ReferenceError at render. This affected ssrHeadEl metadata hoists too, not just resources.

Closes the "adjudicate: client precedence group order under nested @try" item in #711 — adjudicated as fix, since group order is CSS cascade order and Octane disagreed with its own SSR output.

Why

Group order is decided by first registration (insertPrecedenced appends new groups after the last one). The client emitted resource calls at the body tail (plan.head, after constructs), so children — which mount inside the parent's constructs — registered first. The server emits head statements at the body front, which is what ships arm-root sheets with the streaming shell, but is only evaluable when the statement reads nothing the body itself declares.

Changes

packages/octane/src/compiler/compile.js (compiler-only; zero runtime changes, no new statements — the same calls move):

  • emitHeadClient now returns {head, resources}: scope-owned headBlock metadata stays at the body tail (slot numbering untouched — resource slots were already left unoccupied); global resource calls (stylesheetResource/styleResource/scriptResource) land after setup, before the constructs mount children. Setup locals are initialized by then, and no child has rendered yet, so groups form in discovery order. A suspending setup means the arm's resource registers on its completing pass — like React's commit-time insertion.
  • The server body emitter partitions head statements by capture-freeness (via the existing collectFreeIdentifiers/collectStatementBindings helpers): capture-free statements keep the historical front placement — arm-root sheets still ship with the streaming shell (fix: ship Float sheet resources discovered after the streaming shell #727's pinned behavior) — while statements reading setup locals run right after setup, still ahead of the return that renders children.

Tests: 6 new cases in float-resources.test.ts + _fixtures/float-group-order.tsrx (parent/child order, suspended-arm-at-reveal order, nested-arm waves, SSR order pin, setup-local href on both sides, failed-arm resource persistence). Doc: one clarifying sentence in differences-from-react.md (first-encounter = tree discovery order, all sides agree).

Red-first evidence

  • Pre-fix: parent/child and arm-after-sibling fail with inverted group order; setup-local href fails with the exact TDZ (Cannot access 'slug' before initialization) in renderComponentFramed.
  • Deliberate re-break of the client placement after the fix: exactly the three placement tests go red (including the failed-arm persistence case, added post-fix); restored, 48/48.
  • The two wave tests (nested @try across waves, SSR order) pass pre-fix and are kept as contract pins — stated so their green isn't mistaken for fix coverage.

Performance

Compiler-only statement reordering: generated output has identical statement count and size, so no runtime benchmark applies. The compile-time addition is one free-identifier scan per head statement, only in bodies that hoist head elements, and only when the body declares bindings. No per-render, per-node, or bundle cost changes.

Adversarial self-review (what it changed)

  • Extended the server partition beyond resources to all head statements after probing showed ssrHeadEl metadata had the same TDZ hazard.
  • New client behavior pinned deliberately: an arm that registers its sheet and then fails while rendering a child keeps the sheet — this now matches the server (registration precedes child evaluation there too) where the old client behavior arbitrarily dropped it.
  • Known bound, documented in a code comment: a var declared inside a nested setup block escapes the capture scan and keeps front placement (pre-existing behavior; head attrs read top-level setup locals in practice).

Review round 1 (Bugbot, fixed in 088b0be)

Valid finding: the server partition preserved the front/deferred split but not source order between them — a capture-free head statement appearing after a capturing one still jumped to the body front, so mixed static and setup-local resources in one body could cascade differently on SSR than on a client mount. Fixed with a prefix rule: only the capture-free prefix of the head-statement list leads the body; from the first capturing statement on, everything defers together in source order — the exact sequence the client produces. Red-first: MixedLocalStatic (setup-local sheet followed by a static sheet) asserted SSR order matched the client and failed before the rule ([static, dynamic] on SSR), passes after. The nested-pending-boundary shell test still passes: a capture-free arm-root sheet remains the list prefix, so it keeps shipping with the shell.

Validation

  • pnpm format:files:check (changed files)
  • pnpm typecheck
  • pnpm test — 20,930 passed; the one failing file (doom-browser, 3 cases) is full-suite CPU-contention flake: 7/7 in isolation, and the same lane failed identically on unmodified main in an earlier session's gate
  • targeted: float-resources 48/48 (dev + prod projects), hydration/ + float-evidence-{1,2,3} + resource-hints + streaming-ssr 880/880, compiler/ 1,149/1,149, differential/ 304/304
  • after round 1: float-resources 50/50 (dev + prod), neighbor sweep (hydration/, float-evidence-{1,2,3}, resource-hints, streaming-ssr, compiler/) 2,029/2,029

Risk / follow-ups

  • Suspended-pass registration timing on the client is unchanged (registers on the completing pass); only the position within a completing pass moved. SSR keeps eager arm-root registration — the documented stronger-than-React shell behavior.
  • The var-in-nested-block capture-scan bound above.
  • Issue React 19 parity: outstanding gaps and follow-ups after #709 #711: tick the adjudication item after merge.

Provenance

  • An agent produced this diff (agent-authored)

Note

Medium Risk
Compiler-only reordering changes CSS cascade group order on client mounts and when SSR runs head registration, affecting hydration parity for Float stylesheets; no runtime logic changes but behavior is user-visible.

Overview
Float precedence groups on client mounts now follow tree discovery order (parent before child, @try arms when they reveal), aligning client-only mounts with SSR and React. The compiler moves global resource calls (stylesheetResource / styleResource / scriptResource) to run after setup and before child constructs mount, instead of at the body tail with headBlock metadata.

SSR head emission is split by capture-freeness: registrations that do not read setup locals still lead the body (streaming shell behavior); hoists whose attributes depend on setup locals (e.g. <link href={slug} precedence …>) run after setup so they no longer hit a TDZ ReferenceError.

Adds fixtures/tests for parent/child order, suspended arms, nested @try waves, SSR order, setup-local href, and failed-arm sheet retention; docs clarify first-encounter = discovery order.

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

…ounts

Precedence group order is CSS cascade order. Client mounts created groups in
body-completion order, so a nested child's or @Try arm's group could precede
its parent's and the same tree cascaded differently on a client-only mount
than on an SSR'd page. Resource registrations now run after a component's
setup but before its constructs mount children, forming groups parent-first
with suspended arms at reveal — matching SSR and React.

The server twin fixes a latent TDZ crash the placement rule exposed: a hoisted
head element or Float resource whose attribute reads a setup local emitted its
registration ahead of the local's declaration and threw at render. Capture-free
head statements keep the historical front placement (arm-root sheets still
ship with the streaming shell); capturing ones now run right after setup,
still ahead of the children the return renders.

Adjudicates the client group-order item from #711 as "fix".

Co-Authored-By: Claude Fable 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 Aug 12, 2026

@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: SSR head partition reorders groups
    • Changed the SSR head partition to defer all statements after the first capturing one, preserving source order across the front/deferred boundary and matching the client's sequential registration.

Create PR

Or push these changes by commenting:

@cursor push 8f4a0eb18e
Preview (8f4a0eb18e)
diff --git a/packages/octane/src/compiler/compile.js b/packages/octane/src/compiler/compile.js
--- a/packages/octane/src/compiler/compile.js
+++ b/packages/octane/src/compiler/compile.js
@@ -9783,15 +9783,17 @@
 		for (const s of inlinedSubs) collectStatementBindings(s, bodyBindings);
 		if (bodyBindings.size > 0) {
 			frontHead = [];
+			let seenCapture = false;
 			for (const stmt of headStatements) {
-				let captures = false;
-				for (const free of collectFreeIdentifiers(stmt, [])) {
-					if (bodyBindings.has(free)) {
-						captures = true;
-						break;
+				if (!seenCapture) {
+					for (const free of collectFreeIdentifiers(stmt, [])) {
+						if (bodyBindings.has(free)) {
+							seenCapture = true;
+							break;
+						}
 					}
 				}
-				(captures ? deferredHead : frontHead).push(stmt);
+				(seenCapture ? deferredHead : frontHead).push(stmt);
 			}
 		}
 	}

diff --git a/packages/octane/tests/_fixtures/float-group-order.tsrx b/packages/octane/tests/_fixtures/float-group-order.tsrx
--- a/packages/octane/tests/_fixtures/float-group-order.tsrx
+++ b/packages/octane/tests/_fixtures/float-group-order.tsrx
@@ -84,6 +84,19 @@
 	</>
 }
 
+// A static sheet, then a setup-local-computed sheet, then another static sheet.
+// SSR must preserve source order across the partition boundary — the static
+// trailing sheet must NOT be reordered ahead of the setup-local one.
+export function MixedStaticAndLocal(props) @{
+	const slug = '/' + props.name + '-local.css';
+	<>
+		<link rel="stylesheet" href="/first-static.css" precedence="one" />
+		<link rel="stylesheet" href={slug} precedence="two" />
+		<link rel="stylesheet" href="/last-static.css" precedence="three" />
+		<div class="mixed">{'M'}</div>
+	</>
+}
+
 function Exploding(props) @{
 	const boom = () => {
 		throw new Error('mount failure');

diff --git a/packages/octane/tests/float-resources.test.ts b/packages/octane/tests/float-resources.test.ts
--- a/packages/octane/tests/float-resources.test.ts
+++ b/packages/octane/tests/float-resources.test.ts
@@ -44,6 +44,7 @@
 	NestedTryOrder,
 	LocalHref,
 	FailingArm,
+	MixedStaticAndLocal,
 } from './_fixtures/float-group-order.tsrx';
 
 function sheetHrefs(): string[] {
@@ -613,6 +614,21 @@
 		expect(sheetHrefs()).toEqual(['/x-dep.css']);
 	});
 
+	it('SSR preserves source order when static and setup-local head hoists are mixed', async () => {
+		const App = () => Server.createElement(srvOrder.MixedStaticAndLocal, { name: 'x' }) as any;
+		const r = await Server.renderToString(App as any);
+		const first = r.html.indexOf('/first-static.css');
+		const local = r.html.indexOf('/x-local.css');
+		const last = r.html.indexOf('/last-static.css');
+		expect(first).toBeGreaterThan(-1);
+		expect(local).toBeGreaterThan(first);
+		expect(last).toBeGreaterThan(local);
+
+		// Client control: same discovery order.
+		await act(() => mountInto(MixedStaticAndLocal, { name: 'x' }));
+		expect(sheetHrefs()).toEqual(['/first-static.css', '/x-local.css', '/last-static.css']);
+	});
+
 	it('an arm that fails while rendering a child keeps its already-registered sheet', async () => {
 		// Resources are page-global and never removed; registration precedes
 		// child evaluation on the server too, so client and server agree that a

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

Reviewed by Cursor Bugbot for commit f0b5392. Configure here.

Comment thread packages/octane/src/compiler/compile.js Resolved
Registration order is precedence-group order, so the capture split must not
reorder the list: a capture-free head statement after a capturing one used to
jump to the body front, cascading differently on SSR than on a client mount.
Only the capture-free prefix leads now; from the first capturing statement on,
everything defers together in source order. (Bugbot review finding.)

Co-Authored-By: Claude Fable 5 <[email protected]>
@trueadm
trueadm marked this pull request as ready for review August 12, 2026 19:13
@trueadm
trueadm merged commit 765134a into main Aug 12, 2026
30 of 42 checks passed
@trueadm
trueadm deleted the fix/try-precedence-group-order branch August 12, 2026 19:21
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