Skip to content

fix(octane): give each custom-hook call its own signal declarations - #1553

Merged
trueadm merged 7 commits into
mainfrom
fix/custom-hook-signal-declarations
Oct 2, 2026
Merged

trueadm merged 7 commits into
mainfrom
fix/custom-hook-signal-declarations

Conversation

@trueadm

@trueadm trueadm commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • A signal$, derived$ or query$ declared inside a custom hook now belongs to each call of that hook. Two calls in one component (useUser$(a), useUser$(b)) previously shared one cell: the second call rendered the first call's data (X|X) and never ran its own loader.
  • Each call now owns its cells, a call skipped by a condition keeps its own, and server and browser builds key them identically for SSR seeds and streamed results.

Why

  • Instance (i:) declarations were keyed by (component instance, declaration site) only, so ScopeImpl.declaredNode returned one node for every call of the hook. Octane keys hooks by compiler-assigned call site, so the declaration's identity should include the enclosing custom-hook calls rather than become a compile-time diagnostic. A diagnostic also cannot see calls that cross modules.
  • The runtime hook-slot path cannot be the identity. hookSlots() numbers follow module evaluation order and differ between server and browser, while declaration keys are wire identity (native seeds, streamed frames, early values). S8 in docs/async-signals-implementation.md requires compiler IDs that never depend on process counters.

Changes

  • Compiler (compile.js, slot-hooks.js, plain-hook-memo.js): each custom-hook call's slot Symbol is registered with a position-hashed h: site (signalHookCallSite in signal-declarations.js). It applies in modules with a runtime signals import, and for hooks whose local or imported name ends in $. It is limited to DOM targets: universal/valdi output and universal runtime units get nothing, so native bundles never import the DOM runtime.
  • Plain modules that import only signals (slot-hooks.js): a runtime octane/signals import now makes the plain pass walk the module's imported or declared use* hooks. A hooks.ts composing its own declaring hooks otherwise got no call boundary at all. Modules that only declare signals are byte-identical; no current workspace module is affected.
  • Runtime: new renderer-free signals/declaration-path.ts; client and server runtimes export an internal signalHookSite bound to their own slot stack. The public octane entry is unchanged.
  • Facades: declarationKey() appends the enclosing call sites to i: keys (explicit { key } too) in signal$, derived$ and query$.
  • Docs paragraph in docs/signals.md; patch changeset.

Validation

  • pnpm format:check
  • pnpm typecheck
  • pnpm sync (no generated changes)
  • pnpm test: not run in full. The machine was running ~150 concurrent vitest processes from other sessions; the targeted runs are below.
  • targeted: new signals-hook-declarations.test.ts (81 cases: dev, prod and strong compiles, each in the octane-signals, -prod and -strong projects). It covers query/signal/derived per call, nested hooks, conditional skip, hooks composed in plain .ts (surgical and inline-memo paths, including a type-wrapped callee) and in .tsrx without a signals import, plus SSR→hydrate adoption with shifted server slot numbering, including the project's HMR client build.
  • targeted: compiler/signal-declarations.test.ts (universal output stays off octane/internal/client).
  • broad non-browser subset (signals-, hydration/, compiler/, ssr-, hook suites, universal): 18,284 of 18,288 passed. The 4 failures were load timeouts or timing assertions and each file passes alone; hmr.test.ts times out on base main under the same load. After rebasing onto current main, hydration/signal-instance-identity.test.ts failed only at load average ~60 (re-imports per test); its failing groups pass when run alone.
  • node --test scoped-signals bundle-boundaries, bundle-size minimal gates, initial-document-signals: 37/37.
  • Pre-fix evidence: all 18 cases in one project fail on the original source. Separate sabotage runs (runtime-slot keys, each compiler path, the $ gate, the HMR branch, the DOM-target gate) each turn the matching tests red.

Performance

Minified esbuild client bundles, same script before and after:

App raw gzip
Custom hooks, no signals 0 B 0 B
Signals, no custom-hook calls +107 B +24 B
Signals, 4 custom-hook calls +407 B +136 B

Per render: one prefix check per instance declaration, plus an O(hook depth) path join only for declarations inside custom hooks. Wall-clock was not measured: machine load made timings untrustworthy.

Risk / follow-ups

  • A caller compiled without a signals import that calls a hook not named with $ keeps the previous shared cell; it adds no path segment.
  • Two calls of one hook with an identical selection now issue two requests, matching how two separate declarations already behave.
  • Separate pre-existing defect, filed as its own task: plain .ts modules do not slot calls to locally declared custom hooks, so even useState collides across two calls.

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
Touches instance signal identity, compiler hook lowering, and SSR/hydration key agreement—core reactive state paths—but behavior is gated to DOM signal-aware/$ hooks with broad targeted test coverage.

Overview
Fixes per-call isolation for signal$ / derived$ / query$ declared inside custom hooks. Two invocations of the same hook in one component (e.g. useUser$(a) and useUser$(b)) no longer share one instance cell, so loaders and state stay independent; conditionally skipped calls keep their own cells for the instance lifetime.

The compiler (TSRX compile.js, plain-module slot-hooks.js / plain-hook-memo.js) registers DOM-only, position-hashed h: sites via signalHookCallSite and wraps eligible custom-hook slot symbols with runtime signalHookSite. That applies in signal-importing modules and for hooks whose local or imported name ends in $; universal/non-DOM builds skip it.

Runtime adds declaration-path.ts and client/server signalHookSite helpers that fold the enclosing custom-hook path into keys. declarationKey() appends that path to instance (i:) identities in the signal facades so SSR seeds, hydration, and streamed results match across server and client without relying on hook slot numbering.

Docs and a patch changeset describe the behavior; new integration tests cover queries, cells, composition across modules, and SSR→hydrate adoption.

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

A `signal$`, `derived$` or `query$` declared inside a custom hook was keyed
only by its component instance and declaration site, so two calls of one
hook in one component aliased a single cell: `useUser$(a)`/`useUser$(b)`
rendered `X|X` and ran one loader.

Instance declarations now fold in the compiler-assigned call sites of the
enclosing custom-hook calls. Runtime hook-slot numbers follow module
evaluation order and differ between server and browser, while declaration
keys are wire identity for seeds, streamed results and early values, so
each signal-aware custom-hook call gets a position-hashed `h:` site
registered on its slot Symbol (tsrx, plain-module surgical and inline-memo
paths). Sites are emitted only for DOM targets, in modules with a runtime
signals import or for `$` hooks.

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 08:43
@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

  • 🔴 bundle-reachability: 2 value(s) increased

Compares acb4e0361 with 7e54c6350 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)

All 20 timed operations (20 within noise)
target operation base head Δ noise
octane-tsrx run 4.07 4.62 +13.5% ±84% ⚪ within noise
octane-tsrx replace 7.57 10.80 +42.7% ±74% ⚪ within noise
octane-tsrx add 3.65 3.69 +1.1% ±14% ⚪ within noise
octane-tsrx update 0.33 0.37 +12.1% ±48% ⚪ within noise
octane-tsrx select 0.14 0.13 −7.1% ±67% ⚪ within noise
octane-tsrx swap 0.35 0.32 −8.6% ±41% ⚪ within noise
octane-tsrx remove 0.30 0.28 −6.7% ±35% ⚪ within noise
octane-tsrx runlots 34.61 31.38 −9.3% ±23% ⚪ within noise
octane-tsrx select_lots 0.21 0.18 −14.3% ±72% ⚪ within noise
octane-tsrx clear 35.72 35.00 −2.0% ±19% ⚪ within noise
octane-jsx run 3.83 3.73 −2.6% ±5% ⚪ within noise
octane-jsx replace 7.27 7.10 −2.3% ±13% ⚪ within noise
octane-jsx add 3.34 3.40 +1.8% ±11% ⚪ within noise
octane-jsx update 0.50 0.51 +2.0% ±28% ⚪ within noise
octane-jsx select 0.34 0.35 +2.9% ±34% ⚪ within noise
octane-jsx swap 0.65 0.66 +1.5% ±46% ⚪ within noise
octane-jsx remove 0.52 0.58 +11.5% ±67% ⚪ within noise
octane-jsx runlots 30.54 33.36 +9.2% ±12% ⚪ within noise
octane-jsx select_lots 2.37 3.27 +38.0% ±83% ⚪ within noise
octane-jsx clear 32.72 37.91 +15.9% ±21% ⚪ within noise

Application bundle size (bytes)

No changes across 45 measured values.

Public-import reachability bundles (bytes)

target metric base head Δ
binding-aria-esbuild brotli 3,009 3,013 +4 (+0.1%) 🔴
binding-radix-esbuild gzip 67,773 67,772 −1 (−0.0%) 🟢
binding-radix-esbuild brotli 58,791 58,824 +33 (+0.1%) 🔴

102 other measured values are unchanged.

CI job durations

  • 🟡 2 job(s) slower than every recent main run: React parity shard (1/4), example apps (2/3)

Compares the CI run for 07371d957 (run) with the last 5 successful main push runs. A job is only slower or faster outside the whole main range, by at least 10% and 15s. Job time excludes runner setup.

job main median main range this PR Δ
wall clock (first job start to last job end) 12m 39s 11m 55s–15m 07s 14m 33s +1m 54s (+15%) ⚪ within noise
total job time 140m 36s 134m 18s–156m 28s 144m 19s +3m 43s (+3%) ⚪ within noise
React parity shard (1/4) 10m 51s 10m 23s–11m 04s 13m 42s +2m 51s (+26%) 🟡 slower
example apps (2/3) 3m 43s 3m 38s–3m 49s 4m 13s +30s (+13%) 🟡 slower
All 26 timed jobs
job main median main range this PR Δ
React parity shard (1/4) 10m 51s 10m 23s–11m 04s 13m 42s +2m 51s (+26%) 🟡 slower
test shard (Node 24, 2/4) 11m 17s 10m 38s–14m 29s 12m 32s +1m 15s (+11%) ⚪ within noise
test shard (Node 24, 1/4) 11m 23s 10m 36s–12m 46s 12m 11s +48s (+7%) ⚪ within noise
website integration suite 5m 46s 5m 43s–7m 00s 6m 32s +46s (+13%) ⚪ within noise
React parity shard (3/4) 10m 25s 10m 08s–12m 02s 11m 04s +39s (+6%) ⚪ within noise
example apps (2/3) 3m 43s 3m 38s–3m 49s 4m 13s +30s (+13%) 🟡 slower
lint checks 7m 52s 7m 43s–9m 37s 8m 08s +16s (+3%) ⚪ within noise
heavy integration (browser - chromium) 11m 08s 10m 58s–12m 54s 11m 19s +11s (+2%) ⚪ within noise
validate publishable packages 3m 58s 3m 51s–4m 29s 4m 08s +10s (+4%) ⚪ within noise
Three compatibility 2m 42s 2m 38s–2m 56s 2m 47s +5s (+3%) ⚪ within noise
test shard (Node 24, 4/4) 11m 03s 10m 21s–13m 56s 11m 07s +4s (+1%) ⚪ within noise
Lynx compatibility 1m 32s 1m 29s–2m 04s 1m 34s +2s (+2%) ⚪ within noise
example apps (1/3) 3m 10s 3m 04s–3m 14s 3m 12s +2s (+1%) ⚪ within noise
lint 3s 3s–4s 4s +1s (+33%) ⚪ within noise
React parity checks 11s 10s–12s 12s +1s (+9%) ⚪ within noise
typecheck 3s 3s–3s 3s ±0s (±0%) ⚪ within noise
examples 3s 3s–4s 3s ±0s (±0%) ⚪ within noise
test (24) 3s 2s–4s 3s ±0s (±0%) ⚪ within noise
CI provenance 3s 3s–4s 3s ±0s (±0%) ⚪ within noise
classify changeset release 13s 12s–13s 11s −2s (−15%) ⚪ within noise
example apps (3/3) 3m 37s 3m 19s–3m 41s 3m 34s −3s (−1%) ⚪ within noise
test shard (Node 24, 3/4) 9m 31s 9m 24s–10m 50s 9m 28s −3s (−1%) ⚪ within noise
heavy integration (package builds, eval corpus, runtime loaders, Astro) 5m 43s 5m 21s–5m 45s 5m 39s −4s (−1%) ⚪ within noise
typecheck checks 2m 13s 1m 56s–9m 11s 2m 03s −10s (−8%) ⚪ within noise
React parity shard (4/4) 10m 52s 10m 08s–12m 06s 10m 27s −25s (−4%) ⚪ within noise
React parity shard (2/4) 10m 30s 9m 52s–11m 18s 10m 00s −30s (−5%) ⚪ within noise

@trueadm

trueadm commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator Author

On the 🔴 binding-radix-esbuild +28 B brotli: no new code is reachable. I rebuilt that scenario locally on this head and on its parent (the same esbuild config as run-minimal.mjs). Raw size is identical (210,554 B both). With minified identifiers of ≤3 characters normalized, the two prettified bundles diff to zero lines, and neither signalHookSite nor declaration-path appears in the output. The brotli delta comes only from esbuild assigning different short names (bi↔ki, ka↔ba) now that the runtime module graph imports one more (tree-shaken) module. The scenario stays under its committed budget: 58,714 against 59,104 B brotli.

@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: Aliased dollar hooks drop sites
    • Changed compile.js to use n._octaneCustomHookCall ?? localName instead of just localName when calling customHookSignalSite, matching the slot-hooks.js path that correctly uses the original imported name for the $ suffix check.

Create PR

Or push these changes by commenting:

@cursor push 502c867ecd
Preview (502c867ecd)
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
@@ -20181,7 +20181,7 @@
 						},
 						forceSymbol,
 						null,
-						isCustom ? customHookSignalSite(ctx, n, localName) : null,
+						isCustom ? customHookSignalSite(ctx, n, n._octaneCustomHookCall ?? localName) : null,
 					);
 					slot = symVar;
 				}

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

Reviewed by Cursor Bugbot for commit 05aa67c. Configure here.

Comment thread packages/octane/src/compiler/compile.js Resolved
trueadm and others added 5 commits October 2, 2026 12:55
…e kind

A `.tsrx` module checked only the local callee for a trailing `$`, while
plain modules checked only the imported name. `import { useUser$ as
useUser }` in a component module without a signals import therefore still
shared one cell across calls. All three emitters now register a call site
when either the local or the imported hook name ends in `$`; the `.tsrx`
path reads the exported name from the authored imports that its server
and client compiles share.

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

# Conflicts:
#	packages/octane/src/signals/derived-facade.ts
#	packages/octane/src/signals/facade.ts
#	packages/octane/src/signals/query-facade.ts
Pins the combination with #1550: each custom-hook call owns its own query,
so a new captured argument re-selects that call's query and leaves the
other call's result in place.

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

#1546 gave module-local custom-hook calls a withSlot boundary, but the plain
pass still walks only modules that import from `octane`. A `hooks.ts` that
imports `query$` from `octane/signals` and composes its own hooks
(`useUsers$` calling `useUser$` twice) therefore got no boundary, and both
calls shared one cell. A runtime signals import makes the module Octane's,
so its imported or declared `use*` hooks are now walked too. A module that
only declares signals is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@trueadm
trueadm merged commit 95421bb into main Oct 2, 2026
31 checks passed
@trueadm
trueadm deleted the fix/custom-hook-signal-declarations branch October 2, 2026 12:53
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