Repository navigation
fix(octane): track the receiver of a shared own method in inferred dependencies - #1818
Conversation
…pendencies 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]>
Benchmark report
Compares js-framework: 1k/10k row operationsAll 20 timed operations (20 without a verdict)
All 64 work counters are unchanged. Application bundle size (bytes)
45 other measured values are unchanged. Public-import reachability bundles (bytes)No changes across 105 measured values. |
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]>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
Autofix Details
Bugbot Autofix prepared a fix for the issue found in the latest run.
- ✅ Fixed: Arrow named async tracks receiver
- Changed ARROW_HEAD regex from
\s*to\s+after theasyncprefix so thatasync((method) is rejected at the regex level, and removed the now-unnecessarymethod.name !== 'async'guard that was incorrectly rejecting parenthesized arrows with an inferred name ofasync.
- Changed ARROW_HEAD regex from
Or push these changes by commenting:
@cursor push 2061afd1bf
Preview (2061afd1bf)
diff --git a/packages/octane/src/method-dep.ts b/packages/octane/src/method-dep.ts
--- a/packages/octane/src/method-dep.ts
+++ b/packages/octane/src/method-dep.ts
@@ -80,17 +80,17 @@
}
// An arrow's source text starts with its parameters, after an optional
-// `async`: a parenthesized list, or one identifier followed by `=>`. Every
-// other function starts with `function`, `class`, or a method's property key,
-// and a bound, native, or proxied function reads as `function … [native code]`.
-// Anything this does not recognize counts as a method, which only costs a
-// spurious recompute. A method named `async` also reads as `async(`, so the
-// name check runs only after the pattern matched.
-const ARROW_HEAD = /^(?:async\s*)?(?:\(|[\w$]+\s*=>)/;
+// `async ` (with whitespace): a parenthesized list, or one identifier followed
+// by `=>`. Every other function starts with `function`, `class`, or a method's
+// property key, and a bound, native, or proxied function reads as
+// `function … [native code]`. A method named `async` stringifies as `async(`
+// with no whitespace, so requiring `\s+` after `async` excludes it without a
+// name check. Anything this does not recognize counts as a method, which only
+// costs a spurious recompute.
+const ARROW_HEAD = /^(?:async\s+)?(?:\(|[\w$]+\s*=>)/;
function ownMethodDep(receiver: object, method: unknown): unknown {
- return typeof method === 'function' &&
- !(ARROW_HEAD.test(Function.prototype.toString.call(method)) && method.name !== 'async')
+ return typeof method === 'function' && !ARROW_HEAD.test(Function.prototype.toString.call(method))
? receiver
: method;
}
You can send follow-ups to the cloud agent here.
🚀 Deploying Preview to Cloudflare 🚀Preview Deployments by commit
|
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]>
🚀 Deploying Preview to Cloudflare 🚀Preview Deployments by commit
|
#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]>
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: Unguarded method probe can throw
- Wrapped the Function.prototype.toString.call and ARROW_HEAD.test inside ownMethodDep in a try/catch that returns the receiver on failure, matching the documented fail-closed contract.
Or push these changes by commenting:
@cursor push 1bd19ba278
Preview (1bd19ba278)
diff --git a/packages/octane/src/method-dep.ts b/packages/octane/src/method-dep.ts
--- a/packages/octane/src/method-dep.ts
+++ b/packages/octane/src/method-dep.ts
@@ -90,8 +90,12 @@
function ownMethodDep(receiver: object, method: unknown): unknown {
if (typeof method !== 'function') return method;
- const source = Function.prototype.toString.call(method);
- return ARROW_HEAD.test(source) && (source[0] === '(' || method.name !== 'async')
- ? method
- : receiver;
+ try {
+ const source = Function.prototype.toString.call(method);
+ return ARROW_HEAD.test(source) && (source[0] === '(' || method.name !== 'async')
+ ? method
+ : receiver;
+ } catch {
+ return receiver;
+ }
}
You can send follow-ups to the cloud agent here.
Reviewed by Cursor Bugbot for commit 527c9a4. Configure here.
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]>
…main (#1819) - weather framework (raw 132,546 -> 132,761, gzip 42,262 -> 42,381) and total (raw 154,697 -> 154,915, gzip 50,131 -> 50,253): #1818 makes inferred method-call dependencies track the receiver of a shared own method (#1788). `__methodDep` now reads an own function's source text to tell an arrow, which cannot see `this`, from a method; the weather app ships that helper. Measured: framework 132,729 raw / 42,349 gzip, total 154,883 raw / 50,221 gzip. Brotli and app budgets still fit. - deferred-hydration (raw 230,898 -> 230,880, gzip 72,349 -> 72,342): main measures 230,848 raw / 72,310 gzip, so #1771's downward ratchet fails test shard 1/4 on main and on every pull request based on it. Values are measured + 32. Co-authored-by: Claude Opus 5.5 <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub. 2 Skipped Deployments
|
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]>
…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]>


Summary
Closes #1788.
An inferred dependency for a one-level method call such as
source.subscribe()compared only the own functionsource.subscribe. When two objects share one own function, replacing the receiver kept that identity. The inferred effect stayed subscribed to the first object, and the inferred memo kept showingfirst. The explicit[source]controls behaved correctly.__methodDepnow chooses the dependency value from the method itself:function, method shorthand, async, bound, native, proxyprops.onReady?.())undefinedAn arrow binds
thislexically, so no receiver reaches its body. That keeps the existing optimization the issue asks to preserve:props.onChange(...)with a stable arrow callback (inline arrows,useCallback(() => …), state setters) stays quiet when the props container is rebuilt. Any other function can readthis. React Compiler 1.0 also tracks the receiver for these calls: it compiles() => props.onChange(1)to$[0] !== props, andsource.read()to$[0] !== source.How the arrow check works
Function.prototype.toStringand matches it against an anchored pattern: a leading(,x =>,async (orasync x =>. Ordinary functions start withfunction, classes withclass, and methods with their property key. Bound, native and proxied functions read asfunction … [native code].async. Their source also starts withasync(, so source that doesn't start with(must also have anameother thanasync. A parenthesized arrow such as{ async: () => … }skips that check, per Bugbot.Guarded plain reads get a
readflagThe helper also serves guarded plain reads, such as
if (x) use(props.handler). A read passes no receiver, so it keeps comparing the function value. The compiler now marks those reads with a fourthreadargument.All seven emitters take their flags from one
methodDepFlagshelper:hook-deps)slot-hooks)plain-hook-memo)signal-declarations)use()argument probes fix(compiler): read only authored values in plain-hook use() dependencies #1817 added tocollectDepPaths(compile.js), which are readsThe binding-program ref emitter previously dropped the guarded flag entirely. It now passes the same flags as the other emitters.
Behavior change for
vi.fn()callback propsA
vi.fn()/jest.fn()mock is an ordinary function that recordsthis. When a test passes one as a prop and a component calls it asprops.log(...)from an inferred hook, the hook now re-runs whenever the props container is rebuilt. That is sound: the mock does observe the new receiver. It is also what React Compiler or[props]deps would do.14 existing tests asserted the old behavior with
vi.fn()props: 9 inauto-hook-deps-behavior, 3 inhooks, and 1 inline-ref step inbehavior-root, in each of the dev and prod projects. They now wrap their spy in one stable arrow, which keeps each test's intent and its call-count assertions. The changeset calls this out.The
octane.inferred-hook-depseval grader hit the same case. Its reference solution callsprops.onTotal(total)from an inferred effect, and the grader passedvi.fn()observers. After discussion, the call keeps the React-parity rule. The grader now passes each spy through a stable arrow, as a parent component passes a callback, and itsgraderDigestis regenerated. Its assertions still read the spies, so a solution with anullor hand-written array still fails the "unrelated repeat" step.Validation
pnpm format:check— not run repo-wide;pnpm format:files:checkon every changed file passespnpm typecheck— not run repo-wide;pnpm typecheck:fileson the six changed source files passes. Tests are outside the package's typecheck program, and the new fixtures use untypedpropslike their neighbours.pnpm test— not run in full. Before the test edits,--project octane --project octane-prodran in full with the fix: the only failures were the 14vi.fn()cases above.--project octane):auto-hook-deps-behavior,signal-local-selectors. Earlier in the change, alsohooks,behavior-rootandguarded-hook-dependencies, in both octane and octane-prod.pnpm sync— no generated changesNew regression coverage (each new case was seen failing first):
functionmethod and over a method shorthand copied by spread. Before the fix, the effect stayed onfirstand the memo showedfirst. A Strongconstdeclaration is pinned as a neighbour: it already keyed on the receiver.method captures in signals and effectsscenario now runs a "shared own methods" receiver in all four dev/prod × Strong modes, for bothderived$and plain-hook declarations. That shape fails on main's helper.function, asyncfunction, method shorthand, async method and a method namedasyncmust re-run with the new props asthis.Mutation checks (each turned exactly its test red):
asyncname guardreadin the runtimereadinslot-hooksreadinplain-hook-memoPerformance
__methodDepmicro-benchmark, 4M calls on Node 24. Only own functions pay for the source probe:functionThat is one
toStringslice plus an anchored regex, per one-level method-call dependency per render. AWeakMapcache measured about 8 ns for stable arrows but about 155 ns for a fresh arrow each render, and it adds ephemeron churn, so it was left out. An earlier version also had aprototypepre-filter and a(fast path, each worth about 20 ns. They cost about 100 raw bytes, so they were dropped.Emitted code grows only for guarded plain reads (
, true, true).Bundle size: needs #1819 first
The weather app ships the helper: framework +215 raw / +119 gzip, which exceeds its 32-byte headroom. #1819 is the budget-only raise required by CONTRIBUTING, and it lists the exact bytes. #1819 also ratchets
main'sdeferred-hydrationbudget down: #1771's ratchet currently fails test shard 1/4 onmainitself, so shard 1 fails here too. Merge #1819 first, then mergemaininto this branch.Risk / follow-ups
container.fn(...)from an inferred hook or inline ref. That includes test mocks. Such hooks now re-run when the container changes, which matches React Compiler. Arrow callbacks, the common case, are unchanged.use()memos record a called member by its value incollectDepPaths, e.g.[load, count.toFixed]. Souse(load(count.toFixed(1)))stays at1.0aftercountchanges. This predates this PR and is the same bug class in a separate compiler path; it's tracked as a separate task.Provenance
agent-authored)🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.Note
Medium Risk
Changes core compiler/runtime inferred dependency semantics; non-arrow callbacks and mocks called as
props.fn(...)may re-run hooks more often when props objects are recreated, though arrow callbacks remain unchanged.Overview
Inferred hook dependencies for one-level method calls (
source.subscribe(),source.read()) no longer key only on the function identity when several objects share the same own method. The runtime__methodDephelper now treats own arrow functions as tracking themselves (unchanged for stableprops.onChangearrows) and any other own function as tracking the receiver, matching React Compiler and fixing cases where effects and memos stayed pinned to the first object.The compiler threads dependency metadata through a shared
methodDepFlagshelper, including a newreadflag for guarded plain reads (e.g.if (props.enabled) props.log(props.handler)) so those still compare the function value, not the props bag. Emitters updated include hook-deps, slot-hooks, plain-hook-memo, signal captures, DOM binding inline refs, anduse()probe paths.Docs and a patch changeset describe the semantics; eval grader digest for
octane.inferred-hook-depsis refreshed. Tests add receiver-sharing coverage and wrapvi.fn()callback props in stable arrows where the old “track function only” behavior would have made mocks look like spurious deps on container rebuilds.Reviewed by Cursor Bugbot for commit 337de87. Bugbot is set up for automated code reviews on this repo. Configure here.