Repository navigation
fix(octane): resolve a body's signal declarations from the functions it creates - #1706
Conversation
…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]>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
Bugbot Autofix prepared a fix for the issue found in the latest run.
- ✅ Fixed: Wrapped factory callees lose producer skip
- Applied
unwrapExpressionto the callee in bothtrustedFactorycopies and in the second branch of the producer skip indeclaredUses, so parenthesized or TypeScript-wrapped callees are now correctly recognized as factories and their arguments marked as producer closures.
- Applied
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.
Benchmark report🟢 No budget breaches, no added work, and no timing changes beyond ±3%. Compares js-framework: 1k/10k row operationsAll 20 timed operations (20 without a verdict)
All 64 work counters are unchanged. Application bundle size (bytes)
50 other measured values are unchanged. Public-import reachability bundles (bytes)No changes across 105 measured values. |
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
|
Re the benchmark report: the 🟡 🤖 Addressed by Claude Code |


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'senclosingOwnerchain, and a child component isn't on it. The declaring query stayed in@catchand the loader ran again for nothing (3 calls instead of 2). Two related cases had the same cause:props.read()→record$.get()) during render started a second query in the child's cell;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
constsignal$/derived$/query$declarations (result$.reset(),.get(),.set(),.subscribe()…) is lowered to__declared(result$).reset().__declaredreturns 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 afterawaitand in timers.Unchanged:
result$as a prop still owns its own cell (fix(octane): isolate signals in keyed SSR rows #1281, the server query-attempt observer, the streamed activator and fix: retain pending query owners for unchanged parent inputs #1221 rely on this).derived$(() => record$.get())), keep reader ownership. A derived handle a child receives still computes from the child's cells.Once the declaring owner retires, a lexical call throws
ScopeDisposedError, the same fence an unmount cleanup already hits (choice confirmed with the maintainer).resolveOwnernow 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 afterawait, and runs every handle in the callback (including props) under the parent.Changes
packages/octane/src/compiler/signal-declarations.js:declaredUsesuses lexical scope to find calls on scope-proven local declarations from nested functions, skipping producer arguments. It is wired into both the.tsrxAST lowering (copy-on-write, origins inherited) and the plain.ts/.jssource edits, and is skipped when a module has no in-function declarations.packages/octane/src/signals/facade.ts:Descriptorgains alexicalfield,reader()andstatic declared. The constructor can create the lexical copy that sharescells/owner.__declaredis exported fromoctane/signals.docs/signals.mdcontract paragraph; website signals page gains a component-declaration paragraph with the Retry example.docs/differences-from-react.mdcarries no ownership wording, so it is unchanged.octane.Validation
pnpm format:check(not run; scopedpnpm format:files:checkon all changed files passes)pnpm typecheck(not run; scopedpnpm typecheck:fileson the changed source/test files passes)pnpm test(not run locally per the local test-load rule; CI runs it)pnpm vitest run --project octane packages/octane/tests/hydration/signal-declaration-owner.test.ts, 86/86 passing (dev compile;octane-prodcovers prod + Strong in CI)pnpm sync: no generated changesNew compiled
.tsrx+ DOM regressions insignal-declaration-owner.test.ts. Before the fix, 8 of the 10 new tests failed for the intended reason:await, for both a forwarded callback and a direct native button (previously stuck in@catchwith 3 loads);.tsmodule returning the retry callback (source-edit lowering path);result$with the same name hits the child's cell, the handle prop keeps the child's cell, a shadowingconst result$ = props.handle$alias stays reader-owned, and the parent's forwarded write hits the parent;ScopeDisposedErrorafter 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
Descriptor.resolve()gains one own-field compare (lexical, initialized on every descriptor so the shape stays stable). Calls from nested functions add one__declaredcall, 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.invocation, so a redeclaration it presents stages with the declaring render. Normal descriptors behave exactly as on main.Provenance
agent-authored)🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith 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 afterawait. Those calls now resolve through the declaring instance, arm, or row via compiler lowering to__declared(handle$)and a lazy lexicalDescriptorcopy 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 withScopeDisposedError;resolveOwnerrefuses 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.