Skip to content

fix(octane): defer JSX returned by direct calls of named functions - #1378

Merged
trueadm merged 2 commits into
mainfrom
claude/modest-tu-472234
Sep 28, 2026
Merged

trueadm merged 2 commits into
mainfrom
claude/modest-tu-472234

Conversation

@trueadm

@trueadm trueadm commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

JSX returned by a direct call of a named function declaration now resolves its non-literal props and children where the value renders, like JSX returned by an arrow function or method. Rendering the same function as a component (<Row />) is unchanged.

function Label() { return <span>{use(Theme)}</span>; }
function Page() {
  const label = Label();
  return <Theme value="inner">{label}</Theme>; // now "inner"; was "outer"
}

Why. In default (non-native-signal) mode, only a top-level named FunctionDeclaration whose return <jsx> is not nested went through the component-like return-JSX path. That path evaluated holes during the call. Every other form deferred: const f = function f(){}, export default function () {}, an if-nested return in the same function, arrows, methods, and all of native mode. Values from named declarations therefore contradicted #361's "JSX values follow the represented render scope" contract, which SharedDescriptorProviders pins. The server also rendered such calls to HTML immediately. As a result, fragment roots and .map() rows returned from these functions server-rendered with the caller's context while the client deferred them, and hydration silently patched the mismatch. Direction confirmed with the maintainer: follow the rendered tree.

Evaluation matrix before this change (default mode; .tsrx = .tsx, dev = prod)

Form client server
top-level named / default-export declaration, component root eager eager
same, fragment root deferred eager (SSR ≠ client)
same, .map() inside the top-level return children deferred eager (SSR ≠ client)
if-nested return, nested declaration, function expression, arrow, method deferred deferred
native-signal mode, all forms deferred deferred

After this change, every row defers on a direct call.

How

  • Body call vs direct call. Every runtime path invokes a body as body(props, scope, extra) with that Scope current. The new internal helper isRenderCall(arg1) in both runtimes tests that condition. The probe uses a spare default parameter, which keeps fn.length unchanged and avoids an arguments object. When the function already has a second parameter that it never reassigns, the probe compares that parameter instead.
  • Client. A returned record that has something to defer is printed once, in a hoisted Fn$record(captures). A body call invokes it directly. A direct call returns deferRecord(Fn$record, …captures), which wraps createScopedValue. Static records, native mode, and binding views are unchanged.
  • Server. The body call keeps its ssrHtml template. A direct call returns a deferred descriptor built in a hoisted Fn$direct(captures), pairing with the client the same way native mode already pairs compiled fragments with the server's _sfrag fold. Hoisted head elements register only on body calls, so a deferred value registers its own on render. The server also defers component and fragment roots, because its body call would otherwise render them to HTML at call time.
  • Guard. A captured local that the function reassigns, or a read of this, arguments, or new.target, keeps an inline closure. Hoisting passes values by argument, and a copy could differ from the live binding at render time.
  • On the client, reads of unreassigned parameters and top-level locals count as static when deciding whether the record needs a direct-call branch (for example <Glyph top={top} />), because the client record only stores those values and interprets them where it mounts. The server's body call renders holes to HTML immediately, so the server defers every non-literal hole.

Performance evidence

Body calls happen on every render of every return-JSX (.tsx-style) component, so this path is hot. Baseline is main at 867ab3a. The harness uses the same compiled components with each checkout's compiler and runtime (prod, esbuild-bundled), one fresh process per sample, and interleaved A/B pairs.

  • Allocation per body call (new-space growth with GC suppressed; deterministic): Row 216/248/232 B in TurboFan/--jitless/Maglev, identical to baseline, and likewise RowWithSetup and Static. Rejected designs cost more: an inline direct-branch closure added +40 B per call, because V8 context-allocates captured locals on every call; arguments[1] added +136 B.
  • Time, JIT (quiet machine, median paired ratio): body Row 1.012, RowWithSetup 1.014; mounted update of 1,000 <Row/> 0.989. The A/A noise band is about ±1–3%.
  • Time, --jitless: isolated body call +9% (the isRenderCall call plus $record); mounted update of 1,000 rows +0.5%.
  • SSR: renderToString of 1,000 return-JSX rows gives B/A 1.000 (A/A 0.996).
  • Direct calls now construct a scoped value, about 7× the old eager construction (+≈290 ns per call). That is the same cost arrow- or method-returned dynamic JSX already pays.
  • Code size (1,076 binding sources: visx, recharts, base-ui, xyflow, puck, shadcn, streamdown, aria .tsx; prod compile, esbuild minify): +2.07% minified, +2.2–2.5% gzip. Most of it is in visx/xyflow (+6–7% minified: many small components, each with several captures). @{} code is unaffected.
  • bundle-size suite: only octane-jsx changes, from 70,966 to 71,289 B gzip (+323 B: app +71, framework +252 for deferRecord and scoped values), still within jsx-budgets.json. All other apps are byte-identical.

Validation

  • pnpm format:check
  • pnpm typecheck
  • pnpm test: the full run aborts with JavaScript heap out of memory on this machine; a concurrent full run on a branch one commit from main ended the same way, and both runs had the same two base-ui-pristine-browser failures. Files that failed under that memory pressure pass when run alone: octane-prod basic, boundary-unwind, behavior-handoff-bundle, behavior-root, and conformance/fragment-refs-misc, plus the browser behavior-root and signals-native-collection. Relying on CI for the complete run.
  • targeted tests on the rebased head: returned-jsx-direct-calls, scoped-jsx-values, directive-value-position, switch-fold, try-fold, ssr-dynamic-component-tags, and all octane-evals user-app graders, across the octane + octane-prod + octane-evals-user-apps projects (1,156 tests at 04b94c2).

The new returned-jsx-direct-calls.test.ts covers TSRX, TSX, and native TSRX, each through client render, SSR, and hydration. Hydration assertions check node adoption, node counts, and the absence of mismatch diagnostics. Scenarios: exported, module-local, and default-exported declarations; fragment root; mapped rows; component root; document metadata; template directive; block-scoped var; reassigned local; a renderable parameter (Frame({ children: <ReadContext /> }), added for the Bugbot finding); several parameters. Also covered: component use as the control, eager setup statements, a value built outside rendering and resolved per provider across updates and server requests, and a local changed after the call. On the baseline compiler, 104 of these fail (all default mode) and native passes. Breaking nested-var capture or the reassignment guard makes the corresponding tests fail.

switch-fold, try-fold, and ssr-dynamic-component-tags now use the shared loadCompiledFixtureSource, with the same compile ids. Their hand-rolled loaders only rewrote octane/server imports. packages/octane-evals/tests/_client-runtime.ts exposes the two new compiler helpers through its bounded bridge.

Risk / follow-ups

  • Semantic change for direct callers. Code that relied on a named declaration's direct call reading context or module state during the call now reads it at render, the same as arrows and methods already do. The Lexical decorate() pattern belongs here; the remedy documented for arrows (read into a local first) applies.
  • Inspection unchanged. A direct call of a dynamic host-root declaration still exposes the compiled-fragment descriptor ABI (type: _frag$N), so cloneElement on it drops added props on both client and server; this is pre-existing. On the server, isValidElement of such a value is now true, where it was previously an HTML carrier.
  • The interpreter-only body-call cost could be reduced by comparing against a live ESM binding of the current scope instead of calling isRenderCall. No runtime exports mutable bindings today, so I left that out.
  • Separately observed, pre-existing: the dev HMR wrapper registers a non-scope second argument as a live block and forwards at most three arguments.

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

High Risk
This is a deliberate semantic change in core JSX evaluation and SSR/hydration for a common pattern (direct calls of return-JSX functions), with broad compiler and runtime surface area.

Overview
Direct calls of named function declarations that return JSX now defer non-literal props and children until the value renders, matching arrow functions and methods. Using the same function as a component (<Label />) is unchanged.

The compiler splits body call vs direct call with isRenderCall(scope) (argument 1 is the current Scope only when the renderer invokes the function as a component body). Direct calls return deferRecord / scoped values on the client and paired deferred SSR on the server, including fragment roots and mapped rows that previously rendered eagerly on the server. Setup code before return still runs during the call.

Docs in differences-from-react.md describe the behavior. New returned-jsx-direct-calls tests cover client, SSR, and hydration across TSX/TSRX/native; a few server tests now use loadCompiledFixtureSource.

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

A named `function` declaration that returns JSX compiled through the
component-like return-JSX path, which evaluated its non-literal props and
children during the call (the server even rendered the value to HTML then).
Arrows, methods, function expressions, nested returns, and native-signal
mode all defer the record until it renders, so the same markup read
different context depending on how its function was declared, and fragment
roots and `.map()` rows in such functions disagreed between SSR and the
client.

Direct calls now return a value that resolves where it renders. Body calls
(`<Row />`) are detected by the scope the runtime already passes as
argument 1 (`isRenderCall`) and keep the compiled fragment. The record is
printed once in a hoisted `Fn$record(captures)`; a direct call wraps it with
`deferRecord`, so body calls allocate nothing extra (an inline closure made
V8 context-allocate every captured local on every call). The server hoists
its direct branch into `Fn$direct(captures)`. Reassigned captured locals,
`this`, and `arguments` keep an inline closure so the value reads live
bindings.

The fold tests' ad-hoc server loaders move to the shared fixture loader,
which already resolves `octane/internal/server`, and the eval client bridge
exposes the two new compiler helpers.

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 Sep 28, 2026
@github-actions
github-actions Bot marked this pull request as draft September 28, 2026 17:57
@trueadm
trueadm marked this pull request as ready for review September 28, 2026 18:02

@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: Stable children skip server deferral
    • Removed the stableNames argument from jsxValueChildrenNeedRenderScope calls in directCallReturnKind so that identifier children (renderables) are no longer treated as static, forcing the server to take the deferred path that matches the client.

Create PR

Or push these changes by commenting:

@cursor push 741d38f7ab
Preview (741d38f7ab)
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
@@ -20671,11 +20671,10 @@
 	if (requiresTemplateNormalization(node, 'html', true, ctx)) return 'fragment';
 	const stable = directCallStableNames(fn);
 	if (node.type !== 'Element' && node.type !== 'JSXElement') {
-		return jsxValueChildrenNeedRenderScope(node, false, stable) ? 'value' : null;
+		return jsxValueChildrenNeedRenderScope(node, false) ? 'value' : null;
 	}
 	if (isComponentTag(node)) return 'value';
-	return jsxValueRootNeedsRenderScope(node, stable) ||
-		jsxValueChildrenNeedRenderScope(node, false, stable)
+	return jsxValueRootNeedsRenderScope(node, stable) || jsxValueChildrenNeedRenderScope(node, false)
 		? 'fragment'
 		: null;
 }

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

Reviewed by Cursor Bugbot for commit 7483954. Configure here.

Comment thread packages/octane/src/compiler/compile.js Resolved
Treating unreassigned parameters and top-level locals as static is sound
for the client record, which only stores hole values and interprets them
where the value mounts. The server's body call renders holes to HTML
immediately, so a direct call whose only hole was such a local, like
`Frame({ children: <ReadContext /> })`, rendered `children` in the caller's
scope while the client rendered it under the provider.

The server now classifies every non-literal hole as needing deferral again;
only the client's branch decision uses stable locals. Client output is
unchanged.

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