Skip to content

fix(compiler): read only authored values in plain-hook use() dependencies - #1817

Merged
trueadm merged 1 commit into
mainfrom
fix/plain-hook-use-deps
Oct 6, 2026
Merged

trueadm merged 1 commit into
mainfrom
fix/plain-hook-use-deps

Conversation

@trueadm

@trueadm trueadm commented Oct 6, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Memoized use() arguments in plain .ts/.js hook modules now use the same dependency rules as .tsrx components. Their dependency arrays no longer read values the authored argument skips.
  • With options undefined, use(load(run?.(options.label) ?? 'idle')), use(load(() => options.label)) and use(load(ready ? options.label : 'idle')) now render instead of throwing. A typeof window !== 'undefined' ? window.location : … argument no longer reads a bare window during SSR.

Follow-up to #1813 (issue #1799). That PR fixed collectDependencies and collectDepPaths, and listed this collector as out of scope.

Why

slotHooks memoizes a plain module's use() argument as _$useMemo(() => (arg), [deps], slot) on the client and _$puMemo(...) on the server. Its collector, collectParallelUseDependencies, recorded every one-level root.prop read as an eager dependency, with no skip or defer policy. The array evaluates on every render, so it read values the argument never reads. On main:

Argument main This PR (same as .tsrx)
load(run?.(options.label) ?? 'idle') [load, run, options.label] [load, run, __methodDep(options, "label", true)]
load(() => options.label) [load, options.label] [load, options?.label]
load(ready ? options.label : 'idle') [load, ready, options.label] [load, ready, options?.label]
load(options.a ?? window.x) [load, options.a, window.x] [load, options.a]
load(typeof window !== 'undefined' ? window.location : options.label) [load, window, window.location, options.label] [load, typeof window, (typeof window !== 'undefined') ? window.location : void 0, !(typeof window !== 'undefined') ? options.label : void 0]

Changes

  • slot-hooks.js: collectParallelUseDependencies now calls the full compiler's collectDepPaths, so the two paths cannot drift apart again. It replaces about 260 lines of separate scope tracking.
    • Free names resolve through a createLexicalAnalysis of the module. It is built lazily, on the first non-trivial use() argument.
    • printParallelUseDependency prints the closed set of dependency shapes collectDepPaths synthesizes back to text, keeping this pass on its text-edit path. That set is identifiers, one-level members, typeof witnesses, __methodDep probes, and replayed typeof-guard conditionals.
    • The printer also collects the names each dependency reads. Those names still decide when a use() that reads an earlier result starts a new batch. Trivial arguments compute their root directly and never build the scope analysis.
  • compile.js: collectDepPaths is now exported. No other change.
  • docs/differences-from-react.md: the optional-chain paragraph now names plain .ts/.js hook modules.

Validation

  • pnpm format:check: not run repo-wide. pnpm format:files:check passes on every changed file.
  • pnpm typecheck: not run repo-wide. pnpm typecheck:files on slot-hooks.js, compile.js and the test file passes.
  • pnpm test: not run in full; CI runs it.
  • Owning file creation-dependencies.test.ts (--project octane): 894/894 pass.
  • Neighbouring plain-path use() suites: compile-profile, plain-hook-memo-codegen, strong-effects, external-hook-slot, ssr-parallel-use and strong-hook-cache pass, 709/709.
  • Pre-fix failures: on main, all 24 new plain-hook shape tests fail with TypeError: Cannot read properties of undefined (reading 'label'). The extended matrix adds another 128 ts-dialect failures, such as creationAbsentGlobal is not defined. No tsrx/tsx case changes.
  • Mutation checks:
    • Without collecting roots, the 16 batch-order tests fail with Cannot access 'first' before initialization.
    • With no scope resolver, 70 tests fail on stale values and TDZ errors.
    • With every name treated as bound, 24 tests fail with creationAbsentGlobal is not defined.
  • Corpus: slotHooks ran over all 1,601 tracked plain modules that import octane, in client and server with hmr on and off. All 6,404 outputs are byte-identical to main. The 4 errors are the deliberate Context.Provider typetest, which fails identically on main.
  • Native fallback parser path (const-generic function types): checked by hand. It produces the same probes. Its preserved parentheses coarsen a dependency to its bare root, which is safe.
  • Compile time: on the 13 corpus modules with use() statements, slotHooks new/old median is 1.004 (range 0.98–1.07, machine load about 5). This PR makes no performance claim.

New coverage in creation-dependencies.test.ts:

  • A plain .ts dialect for the whole creation matrix. That covers every case, the guarded-global, getter, missing-global, readiness, hydration-adoption, constructor and skipped-plus-read getter tests, in client and server, dev and prod.
  • plain hook use() dependencies, covering six reported shapes:
    • optional-call argument
    • optional computed key
    • callback body
    • conditional branch
    • || right side
    • ?? right side
  • Each shape runs with options undefined and null, a throwing getter in skipped positions, and a field change on the same object.
  • Batch-order tests check that a deferred, skipped, guarded or typeof read of an earlier use() result keeps the later use() out of its batch.

Risk / follow-ups

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
Compiler-only change to generated memo dependency arrays for plain modules with non-trivial use() arguments; behavior shifts for optional chains, guards, and SSR globals, though corpus checks report byte-identical output when arguments lack those patterns.

Overview
Plain .ts/.js hook modules now infer memoized use() dependency arrays with the same rules as .tsrx components, instead of a separate collector that treated every one-level member read as eager.

slot-hooks.js routes collectParallelUseDependencies through the shared collectDepPaths helper (exported from compile.js), with module lexical scope from createLexicalAnalysis. Dependency evaluation on each render therefore defers reads inside callbacks and short-circuit branches (options?.label), skips optional-chain argument/key reads via guarded __methodDep probes, and replays typeof guards so SSR does not touch bare globals like window. A small printer turns synthesized dep nodes back to source text; root name tracking still splits parallel use() batches when a later argument depends on an earlier result.

Docs note that plain hook modules follow the same optional-chain dependency rules. Tests extend the creation-dependency matrix to the ts dialect and add plain hook use() dependencies coverage for optional calls, conditionals, batch ordering, and throwing getters in skipped positions.

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

…cies

Plain `.ts`/`.js` hook modules memoize `use()` arguments through slot-hooks'
`collectParallelUseDependencies`. It recorded every one-level `root.prop` read
as an eager dependency, with no skip or defer policy. A dependency array runs
on every render, so `use(load(run?.(options.label) ?? 'idle'))`,
`use(load(() => options.label))` and `use(load(ready ? options.label : 'idle'))`
all threw when `options` was undefined. A `typeof window !== 'undefined'` guard
also emitted a bare `window` dependency, which throws during server rendering.

The collector now delegates to the full compiler's `collectDepPaths`, so both
paths share one dependency policy:

- Deferred reads become `root?.prop` for bound roots and drop ambient globals.
- Reads after an optional link become `__methodDep(root, 'prop', true)`.
- typeof guards are replayed.

Free names resolve through a module scope analysis that is built only for
non-trivial arguments. The synthesized dependencies are printed back to text,
and the names they read still decide whether a `use()` joins the current batch.

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 6, 2026
@trueadm
trueadm marked this pull request as ready for review October 6, 2026 12:53
@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Benchmark report

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

Compares ed57b1ea5, the merge commit's first parent with b098016e3 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 2.700 2.667 −0.0% −3.5% … +3.5% ⚪ within noise
octane-tsrx replace 5.983 6.017 −4.0% −6.7% … +0.9% ⚪ within noise
octane-tsrx add 2.467 2.475 ±0.0% −4.0% … +0.0% ⚪ within noise
octane-tsrx update 0.123 0.123 +0.5% −1.3% … +1.7% ⚪ within noise
octane-tsrx select 0.005 0.005 −1.3% −2.1% … −0.5% ⚪ within noise
octane-tsrx swap 0.036 0.035 +0.3% −0.7% … +2.9% ⚪ within noise
octane-tsrx remove 0.019 0.019 −0.8% −9.2% … +2.3% ⚪ within noise
octane-tsrx runlots 24.142 23.892 −2.5% −4.1% … −1.0% ⚪ within noise
octane-tsrx select_lots 0.006 0.006 −1.3% −1.9% … −0.6% ⚪ within noise
octane-tsrx clear 42.150 43.467 +0.8% −0.4% … +2.7% ⚪ within noise
octane-jsx run 3.650 3.742 −2.8% −8.1% … +1.1% ⚪ within noise
octane-jsx replace 7.450 6.850 −0.0% −7.1% … +7.1% ⚪ within noise
octane-jsx add 3.775 3.758 ±0.0% −7.7% … +3.0% ⚪ within noise
octane-jsx update 0.335 0.324 −3.3% −5.4% … +3.3% ⚪ within noise
octane-jsx select 0.140 0.148 −0.5% −3.7% … +3.2% ⚪ within noise
octane-jsx swap 0.252 0.260 +4.1% −1.2% … +6.6% ⚪ within noise
octane-jsx remove 0.188 0.185 +0.6% −1.2% … +3.0% ⚪ within noise
octane-jsx runlots 29.658 29.450 +1.2% −1.5% … +4.0% ⚪ within noise
octane-jsx select_lots 1.477 1.460 +1.6% +0.5% … +3.5% ⚪ within noise
octane-jsx clear 48.400 48.758 +0.2% −1.5% … +1.6% ⚪ within noise

All 64 work counters are unchanged.

Application bundle size (bytes)

No changes across 60 measured values.

Public-import reachability bundles (bytes)

No changes across 105 measured values.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

🚀 Deploying Preview to Cloudflare 🚀

Preview Deployments by commit

Status Deployment URL Commit Updated (UTC) See this deployment's details
  • Build: Failed ❌

View logs ↗
0524dbf 2026-10-06T13:17:17.174Z View logs ↗

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

🚀 Deploying Preview to Cloudflare 🚀

Preview Deployments by commit

Status Deployment URL Commit Updated (UTC) See this deployment's details
  • Build: Failed ❌

View logs ↗
0524dbf 2026-10-06T13:21:06.046Z View logs ↗

@trueadm
trueadm merged commit 262926c into main Oct 6, 2026
75 of 80 checks passed
@trueadm
trueadm deleted the fix/plain-hook-use-deps branch October 6, 2026 13:33
trueadm added a commit that referenced this pull request Oct 6, 2026
#1817 routes skipped `root.prop` reads in memoized use() arguments through
the guarded `__methodDep` probe. That probe now treats a guarded call's own
non-arrow function as receiver-dependent, so a skipped read of a stable
ordinary function would reload whenever its container is rebuilt. The
probe is a read, never a call, so it passes the `read` flag like the
other guarded reads.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
trueadm added a commit that referenced this pull request Oct 6, 2026
…pendencies (#1818)

* fix(octane): track the receiver of a shared own method in inferred dependencies

An inferred dependency for a one-level method call `source.read()` compared
the own function `source.read` alone. When two objects share one own
function, swapping the receiver kept that identity, so an inferred effect
stayed subscribed to the old object and an inferred memo kept its old value
(#1788).

`__methodDep` now keeps an own function's identity only when it is an arrow,
which cannot see `this`; any other own method tracks its receiver, as an
inherited one already did and as React Compiler does for every method call.
The arrow check reads the function's source text, skipping it for functions
that own a `prototype`. Guarded plain reads pass a new `read` flag, so a
guarded read of a function value still compares the value. Every emitter
(component hooks, plain hooks, inline hook memos, Strong caches, signal
captures, and binding-program refs) builds the flags from one helper.

Closes #1788.

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

* fix(octane): shrink the arrow check in method dependencies

Classify every own function with the source pattern alone. The `prototype`
pre-filter and the `(` fast path saved about 20 ns per call but cost 100 raw
bytes in every bundle that ships the helper. A callable proxy's source reads
as native code and fails the pattern before `name` is read, so the probe
needs no exception guard.

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

* fix(octane): keep a parenthesized arrow named async on its own identity

Folding the leading-`(` check into the arrow pattern applied the `async`
name guard to every arrow, so `{ async: () => … }` tracked its receiver.
Only source that may be a method named `async` needs the guard; a
parenthesized arrow cannot be one.

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

* fix(compiler): mark use() dependency probes as reads

#1817 routes skipped `root.prop` reads in memoized use() arguments through
the guarded `__methodDep` probe. That probe now treats a guarded call's own
non-arrow function as receiver-dependent, so a skipped read of a stable
ordinary function would reload whenever its container is rebuilt. The
probe is a read, never a call, so it passes the `read` flag like the
other guarded reads.

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

* fix(octane): fall back to the receiver when a method probe throws

The unguarded own-property path classified a method without the fallback
the guarded path has. A patched `Function.prototype.toString` or a throwing
`name` getter then failed the whole inferred dependency array. A probe that
throws now counts as a method, as a failed guarded probe already does, and
the authored call keeps any exception.

The bare-parameter arrow cases now build their source with the Function
constructor: the test transform re-prints `v => v` as `(v) => v`, so those
cases had not reached the identifier branch of the arrow pattern.

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

* test(evals): pass arrow callbacks in the inferred-hook-deps grader

The reference solution calls `props.onTotal(total)` from an inferred
effect. #1818 makes a call through an own non-arrow function track its
receiver, as React Compiler does, and a `vi.fn()` is an ordinary function
that records `this`. Passed directly, every rebuilt props object became a
real dependency and the grader's "no unrelated repeat" step saw a second
call. Each observer now reaches the component through a stable arrow, as a
parent passes a callback; the assertions still read the spies, so a
solution with a `null` or hand-written array still fails. The manifest's
grader digest is regenerated.

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

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
trueadm added a commit that referenced this pull request Oct 6, 2026
…endencies (#1821)

* fix(octane): track the receiver of a shared own method in inferred dependencies

An inferred dependency for a one-level method call `source.read()` compared
the own function `source.read` alone. When two objects share one own
function, swapping the receiver kept that identity, so an inferred effect
stayed subscribed to the old object and an inferred memo kept its old value
(#1788).

`__methodDep` now keeps an own function's identity only when it is an arrow,
which cannot see `this`; any other own method tracks its receiver, as an
inherited one already did and as React Compiler does for every method call.
The arrow check reads the function's source text, skipping it for functions
that own a `prototype`. Guarded plain reads pass a new `read` flag, so a
guarded read of a function value still compares the value. Every emitter
(component hooks, plain hooks, inline hook memos, Strong caches, signal
captures, and binding-program refs) builds the flags from one helper.

Closes #1788.

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

* fix(octane): shrink the arrow check in method dependencies

Classify every own function with the source pattern alone. The `prototype`
pre-filter and the `(` fast path saved about 20 ns per call but cost 100 raw
bytes in every bundle that ships the helper. A callable proxy's source reads
as native code and fails the pattern before `name` is read, so the probe
needs no exception guard.

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

* fix(octane): keep a parenthesized arrow named async on its own identity

Folding the leading-`(` check into the arrow pattern applied the `async`
name guard to every arrow, so `{ async: () => … }` tracked its receiver.
Only source that may be a method named `async` needs the guard; a
parenthesized arrow cannot be one.

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

* fix(compiler): mark use() dependency probes as reads

#1817 routes skipped `root.prop` reads in memoized use() arguments through
the guarded `__methodDep` probe. That probe now treats a guarded call's own
non-arrow function as receiver-dependent, so a skipped read of a stable
ordinary function would reload whenever its container is rebuilt. The
probe is a read, never a call, so it passes the `read` flag like the
other guarded reads.

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

* fix(octane): fall back to the receiver when a method probe throws

The unguarded own-property path classified a method without the fallback
the guarded path has. A patched `Function.prototype.toString` or a throwing
`name` getter then failed the whole inferred dependency array. A probe that
throws now counts as a method, as a failed guarded probe already does, and
the authored call keeps any exception.

The bare-parameter arrow cases now build their source with the Function
constructor: the test transform re-prints `v => v` as `(v) => v`, so those
cases had not reached the identifier branch of the arrow pattern.

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

* test(evals): pass arrow callbacks in the inferred-hook-deps grader

The reference solution calls `props.onTotal(total)` from an inferred
effect. #1818 makes a call through an own non-arrow function track its
receiver, as React Compiler does, and a `vi.fn()` is an ordinary function
that records `this`. Passed directly, every rebuilt props object became a
real dependency and the grader's "no unrelated repeat" step saw a second
call. Each observer now reaches the component through a stable arrow, as a
parent passes a callback; the assertions still read the spies, so a
solution with a `null` or hand-written array still fails. The manifest's
grader digest is regenerated.

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

* fix(compiler): track an inherited method's receiver in use() memo dependencies

A batched use() argument recorded a called one-level member by its value,
so `use(load(count.toFixed(1)))` depended on `Number.prototype.toFixed` and
never refreshed when `count` changed. collectDepPaths now records a called
member through the guarded `__methodDep` descriptor probe: an inherited
method, accessor, or primitive receiver tracks the receiver, while an own
function of any kind keeps its identity. Tracking the receiver of an own
non-arrow function, as inferred hook dependencies do, would key
`use(props.load(id))` on a rebuilt props object and start a fresh request on
every parent render and boundary retry.

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

* fix(compiler): read use() memo dependencies through erased TypeScript wrappers

`props!.load(id)`, `(props as T).load(id)`, and `props['load' as const](id)`
call the same member as `props.load(id)`, but collectDepPaths only unwrapped
the callee, so a wrapped receiver or key fell back to the whole `props`
object. A `function` loader then restarted on every parent render and
boundary retry. Unwrap the receiver and a computed key for reads and calls.

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

---------

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