Skip to content

fix(octane): resolve a body's signal declarations from the functions it creates - #1706

Merged
trueadm merged 3 commits into
mainfrom
fix/forwarded-callback-declaration-owner
Oct 4, 2026
Merged

trueadm merged 3 commits into
mainfrom
fix/forwarded-callback-declaration-owner

Conversation

@trueadm

@trueadm trueadm commented Oct 4, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #1680.

A component passed a Retry callback that closes over its own query$ to a child button. Clicking it reset a cell owned by the child, not the component's. Native events run under the host's signal owner. Descriptor resolution only falls back to the declaration owner when that owner is on the reader's enclosingOwner chain, and a child component isn't on it. The declaring query stayed in @catch and the loader ran again for nothing (3 calls instead of 2). Two related cases had the same cause:

  • a child calling a parent getter (props.read() → record$.get()) during render started a second query in the child's cell;
  • code after await, even in a direct native button, had no ambient owner and resolved a document-level cell.

Design (chosen with the maintainer, extends the #1530 reader-owned contract)

Use lexical binding at the use site. Inside a function that a component or hook body creates, a method call on one of that body's const signal$/derived$/query$ declarations (result$.reset(), .get(), .set(), .subscribe()…) is lowered to __declared(result$).reset(). __declared returns a lazily created lexical copy of the descriptor. That copy shares the descriptor's cell cache and resolves against its recorded declaration owner (instance, arm or row) instead of the ambient reader. Resolution happens when the call runs, so it still works after await and in timers.

Unchanged:

Once the declaring owner retires, a lexical call throws ScopeDisposedError, the same fence an unmount cleanup already hits (choice confirmed with the maintainer). resolveOwner now refuses a retired instance before re-registering it with its document, so a kept callback can't grow the document's instance set.

The alternative was compiler-wrapped callbacks (__owned(fn)) running under the creating owner. It was rejected: it allocates per render per closure, changes function identity, loses the owner after await, and runs every handle in the callback (including props) under the parent.

Changes

  • packages/octane/src/compiler/signal-declarations.js: declaredUses uses lexical scope to find calls on scope-proven local declarations from nested functions, skipping producer arguments. It is wired into both the .tsrx AST lowering (copy-on-write, origins inherited) and the plain .ts/.js source edits, and is skipped when a module has no in-function declarations.
  • packages/octane/src/signals/facade.ts: Descriptor gains a lexical field, reader() and static declared. The constructor can create the lexical copy that shares cells/owner. __declared is exported from octane/signals.
  • Docs: docs/signals.md contract paragraph; website signals page gains a component-declaration paragraph with the Retry example. docs/differences-from-react.md carries no ownership wording, so it is unchanged.
  • Patch changeset for octane.

Validation

  • pnpm format:check (not run; scoped pnpm format:files:check on all changed files passes)
  • pnpm typecheck (not run; scoped pnpm typecheck:files on the changed source/test files passes)
  • pnpm test (not run locally per the local test-load rule; CI runs it)
  • targeted tests: pnpm vitest run --project octane packages/octane/tests/hydration/signal-declaration-owner.test.ts, 86/86 passing (dev compile; octane-prod covers prod + Strong in CI)
  • pnpm sync: no generated changes

New compiled .tsrx + DOM regressions in signal-declaration-owner.test.ts. Before the fix, 8 of the 10 new tests failed for the intended reason:

  • forwarded Retry recovers the declaring query: one initial load and one retry; also after await, for both a forwarded callback and a direct native button (previously stuck in @catch with 3 loads);
  • direct native-button control (passed before, still passes);
  • custom hook in a plain .ts module returning the retry callback (source-edit lowering path);
  • shadowing: a child-local result$ with the same name hits the child's cell, the handle prop keeps the child's cell, a shadowing const result$ = props.handle$ alias stays reader-owned, and the parent's forwarded write hits the parent;
  • a child rendering a parent getter starts one query, not two (client) and resumes the server's single query during hydration with DOM adoption (SSR: previously 2 server loads);
  • derived producers stay reader-owned beside a forwarded callback (2 requests: the parent's and the child's own derived cell). This one passed before too; it pins the unchanged producer contract;
  • a kept callback reads the declaring cell with no ambient owner (previously the document cell) and throws ScopeDisposedError after unmount.

Mutation checks: allowing rewrites inside producer arguments fails only the producer test, and matching by name instead of by binding fails only the shadowing test.

Follow-up (Bugbot): a factory callee wrapped in parentheses or a TS assertion, e.g. (derived$ as typeof derived$)(…), now also counts as a producer. Covered by a wrapped-producer variant of the producer test, which failed before the fix.

Risk / follow-ups

  • Contract extension: a nested component defined inside another component that calls methods on the outer component's declaration now resolves the outer cell (lexical) instead of its own. This is consistent with the new rule, and the pattern is unusual.
  • Hot path: Descriptor.resolve() gains one own-field compare (lexical, initialized on every descriptor so the shape stays stable). Calls from nested functions add one __declared call, plus one lazy copy per descriptor per render on first use. No benchmark was run locally, per the load rule. The bundle-size scenarios are all signal-free, so the facade change doesn't touch their budgets.
  • fix(octane): cancel a component's superseded query$ request read only by its pending @try arm #1703 (Superseded local query stays pending when read inside a pending boundary #1697) landed first and is merged in. The lexical copy inherits the declaring descriptor's invocation, so a redeclaration it presents stages with the declaring render. Normal descriptors behave exactly as on main.

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
Extends core signal ownership and compiler lowering for all nested method calls on instance declarations; behavior change for unusual nested-component patterns, with targeted hydration coverage.

Overview
Fixes incorrect signal cell resolution when a component or hook body passes closures that call methods on locally declared handles (result$.reset(), .get(), etc.) into children, effects, or code after await. Those calls now resolve through the declaring instance, arm, or row via compiler lowering to __declared(handle$) and a lazy lexical Descriptor copy that shares the cell cache but pins declaration-owner resolution.

Unchanged: handles passed as props stay reader-owned; signal$/derived$/query$ producer arguments keep reader ownership. Retired declaring owners fence lexical calls with ScopeDisposedError; resolveOwner refuses re-registering retired instances from kept callbacks.

Docs and the website Retry example document the closure vs prop contract. Hydration tests cover forwarded retry, post-await, plain-hook source edits, shadowing, single-query child getters, and producer boundaries.

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

…it creates

A Retry callback a component passes to a child button reset a query cell owned
by the child, because native events run under the host's signal owner and
descriptor resolution follows the reader's enclosing chain, which a child
component does not share. The declaring component's query stayed in its @catch
arm and its loader ran again for nothing. A child calling a parent getter during
render started a second query, and code after `await` resolved a document cell.

The compiler now routes method calls on a body's const signal$/derived$/query$
declarations, inside functions that body creates, through __declared. That
returns a lexical handle that resolves against the descriptor's recorded
declaration owner (instance, arm or row) instead of the ambient reader. Handles
passed as values keep reader ownership, so a child given a handle prop still
owns its cell, and producer closures (factory and Scope producer arguments) are
unchanged. Both the .tsrx AST lowering and the plain-module source edits apply
it. A retired declaring owner fences such calls with ScopeDisposedError, and
owner resolution now refuses a retired instance before re-registering it.

Fixes #1680

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 4, 2026
@trueadm
trueadm marked this pull request as ready for review October 4, 2026 15:23

@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: Wrapped factory callees lose producer skip
    • Applied unwrapExpression to the callee in both trustedFactory copies and in the second branch of the producer skip in declaredUses, so parenthesized or TypeScript-wrapped callees are now correctly recognized as factories and their arguments marked as producer closures.

Create PR

Or push these changes by commenting:

@cursor push 813ceb3a39
Preview (813ceb3a39)
diff --git a/packages/octane/src/compiler/signal-declarations.js b/packages/octane/src/compiler/signal-declarations.js
--- a/packages/octane/src/compiler/signal-declarations.js
+++ b/packages/octane/src/compiler/signal-declarations.js
@@ -527,16 +527,17 @@
 			for (const child of node) visit(child, producer);
 			return;
 		}
-		if (
-			(node.type === 'CallExpression' || node.type === 'OptionalCallExpression') &&
-			(trusted(node) !== null ||
-				((node.callee?.type === 'MemberExpression' ||
-					node.callee?.type === 'OptionalMemberExpression') &&
-					SIGNAL_FACTORIES.has(propertyName(node.callee))))
-		) {
-			visit(node.callee, producer);
-			visit(node.arguments, true);
-			return;
+		if (node.type === 'CallExpression' || node.type === 'OptionalCallExpression') {
+			const callee = unwrapExpression(node.callee);
+			if (
+				trusted(node) !== null ||
+				((callee?.type === 'MemberExpression' || callee?.type === 'OptionalMemberExpression') &&
+					SIGNAL_FACTORIES.has(propertyName(callee)))
+			) {
+				visit(node.callee, producer);
+				visit(node.arguments, true);
+				return;
+			}
 		}
 		const callee =
 			node.type === 'CallExpression' || node.type === 'OptionalCallExpression'
@@ -620,7 +621,7 @@
 	// lowered helpers, or null for any other call.
 	function trustedFactory(node) {
 		if (node.type !== 'CallExpression' && node.type !== 'OptionalCallExpression') return null;
-		const callee = node.callee;
+		const callee = unwrapExpression(node.callee);
 		const scope = lexical.nodeScopes.get(callee) ?? lexical.rootScope;
 		if (callee?.type === 'Identifier') {
 			const record = namedImports.get(callee.name);
@@ -792,7 +793,7 @@
 	// lowered helpers, or null for any other call.
 	const trustedFactory = (node) => {
 		if (node.type !== 'CallExpression' && node.type !== 'OptionalCallExpression') return null;
-		const callee = node.callee;
+		const callee = unwrapExpression(node.callee);
 		const scope = lexical.nodeScopes.get(callee) ?? lexical.rootScope;
 		if (callee?.type === 'Identifier') {
 			const record = namedImports.get(callee.name);

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

Reviewed by Cursor Bugbot for commit babfb44. Configure here.

@github-actions

github-actions Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

🟢 No budget breaches, no added work, and no timing changes beyond ±3%.

Compares 77504c160, the merge commit's first parent with cae373ea7 on the same runner. A byte value over its committed budget or any increase in a js-framework work counter fails this check. 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 3.033 2.883 −1.4% −3.3% … ±0.0% ⚪ within noise
octane-tsrx replace 5.333 5.333 ±0.0% −1.9% … +1.9% ⚪ within noise
octane-tsrx add 2.517 2.492 ±0.0% ±0.0% … ±0.0% ⚪ within noise
octane-tsrx update 0.132 0.137 +0.2% ±0.0% … +2.4% ⚪ within noise
octane-tsrx select 0.006 0.006 +3.4% +1.3% … +4.6% ⚪ within noise
octane-tsrx swap 0.039 0.041 −0.8% −2.2% … +3.9% ⚪ within noise
octane-tsrx remove 0.018 0.017 +0.8% −0.8% … +3.5% ⚪ within noise
octane-tsrx runlots 24.825 24.067 −0.6% −2.8% … +5.0% ⚪ within noise
octane-tsrx select_lots 0.006 0.006 +5.1% +2.2% … +5.9% ⚪ within noise
octane-tsrx clear 35.875 35.642 +1.0% −3.7% … +2.2% ⚪ within noise
octane-jsx run 3.592 3.592 +0.0% −0.0% … +2.6% ⚪ within noise
octane-jsx replace 6.267 6.258 −0.7% −1.6% … ±0.0% ⚪ within noise
octane-jsx add 3.225 3.225 ±0.0% −0.0% … +0.0% ⚪ within noise
octane-jsx update 0.252 0.246 −0.4% −1.5% … +0.4% ⚪ within noise
octane-jsx select 0.138 0.138 +0.5% −2.0% … +2.7% ⚪ within noise
octane-jsx swap 0.243 0.246 +0.8% −0.0% … +1.8% ⚪ within noise
octane-jsx remove 0.190 0.184 −0.9% −3.1% … +0.9% ⚪ within noise
octane-jsx runlots 30.733 29.750 −1.6% −2.9% … +0.3% ⚪ within noise
octane-jsx select_lots 1.668 1.646 −0.3% −1.8% … +0.9% ⚪ within noise
octane-jsx clear 37.550 38.100 +0.4% −4.2% … +4.9% ⚪ within noise

All 64 work counters are unchanged.

Application bundle size (bytes)

target metric base head Δ
octane-tsrx bindings_js_gzip 240,870 240,859 −11 (−0.0%) 🟢
octane-tsrx bindings_js_brotli 205,198 205,189 −9 (−0.0%) 🟢
octane-tsrx bindings_app_gzip 121,727 121,716 −11 (−0.0%) 🟢
octane-tsrx bindings_app_brotli 101,835 101,826 −9 (−0.0%) 🟢

50 other measured values are unchanged.

Public-import reachability bundles (bytes)

No changes across 105 measured values.

trueadm and others added 2 commits October 4, 2026 16:31
A factory called through parentheses or a type assertion, such as
`(derived$ as typeof derived$)(compute, { key })`, was not recognized as a
producer, so method calls on local declarations inside its compute were routed
through __declared and read the declaring cell instead of the reader's. Unwrap
the callee before the producer check, for trusted imports and Scope factories.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…k-declaration-owner

# Conflicts:
#	packages/octane/src/signals/facade.ts
@trueadm

trueadm commented Oct 4, 2026

Copy link
Copy Markdown
Collaborator Author

Re the benchmark report: the 🟡 select_lots wall-time row (0.005 → 0.006 ms, report-only) can't come from this diff. None of the js-framework Octane apps import octane/signals. lowerSignalDeclarations returns the AST untouched before any new logic when a module has no signal factory call, and the plain-module path only runs declaredUses when there are in-function declarations. The signals facade isn't in those bundles. The report also confirms that all 64 work counters and every byte budget are unchanged, so this is timer noise on a ~5 µs operation.

🤖 Addressed by Claude Code

@trueadm
trueadm merged commit 4601dd3 into main Oct 4, 2026
32 checks passed
@trueadm
trueadm deleted the fix/forwarded-callback-declaration-owner branch October 4, 2026 16:15
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.

Captured local query reset targets a child cell when a callback is forwarded

1 participant