Repository navigation
fix(javascript): bound value evaluation through nested closure aliases - #1941
Conversation
A closure that aliases a binding from its enclosing scope evaluates that binding once live and once at its own capture point. The capture cannot order a binding declared in another function, so it fell back to a second live evaluation. Only primitive results were kept, so every level of nesting doubled the work: 20 nested closures over a non-constant value took about 14 s, and a 443 KB generated wrapper reached the 300 s analysis timeout. Keep a live binding value when no caller could change it: no alias cycle closes on or above the binding and the expression depth budget was not reached. Bindings on a cycle are still re-evaluated, so their values and reasons are unchanged. Found while building a source-owned reproduction for morluto#1847; that report's worker heap exhaustion is a separate, already closed defect.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ec28a5fcec
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| // it doubles the work at every level of nesting. | ||
| if ( | ||
| context.capturePoint !== undefined || | ||
| callerIndependent || |
There was a problem hiding this comment.
Cap composite values before caching them
For sequence-expression assignments such as let s0, s1; 0, s0 = {}; 0, s1 = {x: s0}; ..., the RHSs have no capture position, so every wrapper is caller-independent and this branch caches it. Each subsequent binding then embeds the prior cached object without incrementally traversing its depth, allowing a linear-size source to build arbitrarily deep values while bypassing SEMANTIC_EXPRESSION_DEPTH_LIMIT; semanticResourceLimitsIn subsequently re-walks every shared prefix in O(N²), and transport serialization can overflow on the deepest value. Track expanded composite depth/size before caching, or restrict this cache to values whose retained representation is bounded.
AGENTS.md reference: AGENTS.md:L42-L42
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Confirmed, and it reproduces with plain declarations too (var s1 = {x: s0}; var s2 = {x: s1}; …), not only the sequence form. At 3,000 wrappers ec28a5fc produced a 2,999-level value and overflowed the stack in JSON.stringify; base stops at 86 levels with an expression-depth limit.
Fixed in 72a88db: a caller-independent value now records the expression depth it was derived at and is reused only at that depth or shallower. A deeper reader derives it again and reaches the budget as on base. For 50 / 200 / 800 / 3,000 wrappers (object, array, sequence-assigned) the last value's nesting, coverage status and serialized IR size now match base exactly. Three regression cases added to javascriptSemanticAnalysis.scope_depth.test.ts; they fail on ec28a5fc (expected 299 to be less than 256).
Cost: the 3,000-wrapper chain is about 0–15% slower than base on my Windows host over three alternating runs. Closure chains are unchanged from the first commit (20 levels in ~9 ms).
|
TL;DR: On Linux, the fix works. Deep closure chains now take about 1.3 seconds, down from 43 to 86 seconds at 24 levels on the base, d802304. Output is byte-for-byte the same, and cyclic bindings still give the same results. Thanks, ranmori, for the fix and for the clear write-up. I tested head ec28a5f against base d802304 (#1902). That base is still the merge base with current main. Results
Time for
Cycles still work. Three cyclic fixtures (3, 8 and 16 levels) and one mixed fixture gave the same CLI output on both builds. A direct call to Not run
Commands# build (Node 24), once per tree
npm ci && npm run build && npm run test:prepare
# tests
npx vitest run --project domain src/domain/javascript/javascriptSemanticAnalysis.scope_depth.test.ts
npx vitest run --project domain
npm run test:fast
npx vitest run --project services src/application/javascript/JavaScriptModuleSpecifierPaths.test.ts src/application/javascript/JavaScriptFileUrlPaths.test.ts
# timing, 3 runs per fixture, each build's own CLI
node scripts/rea.mjs analyze-javascript-application <fixture-dir> --jsonFixtures came from a small script: depth 5, 10, 15, 20 and 24, each closure doing Test setup
🤖 Generated with Claude Code (Sonnet 5.5, verified by Opus 5.5) 🦊 |
Reusing a retained composite value from any depth let a chain of wrappers
(s1 = {x: s0}, s2 = {x: s1}, ...) nest past the expression depth budget:
300 wrappers produced a 299-level value where the budget stops at 86, the
serialized IR grew quadratically, and 3,000 wrappers overflowed the stack
in JSON.stringify.
Record the expression depth at which each caller-independent value was
derived and reuse it only at that depth or shallower. A deeper reader
derives the value again and reaches the budget exactly as before. Closure
aliases read their outer binding at one depth on both paths, so nested
closures stay linear.
|
@CassieFox23 Thanks for the independent run, especially the cyclic fixtures and the 2,300 random programs — and for confirming the four One update: your results are for ec28a5f. The Codex review found that commit let a chain of composite wrappers ( |
|
TL;DR: The new commit 72a88db fixes the wrapper chains. Nesting stops at the same depth as on base, even at 10,000 wrappers, and the overflow is gone. Still slow: very long chains of nested closures. The slow point moved from about 20 levels on base to 50 to 200 levels. The old commit had no slow point up to 200 levels. I re-ran my whole harness on 72a88db. I added long wrapper chains to my generator. I ran them on the old commit ec28a5f and on the base d802304 (#1902) too. ResultsA chain is
At 100 levels, a closure chain timed out at 300 s on base and on the new commit. I did not look into why the gain stops where it does. Chains of 1,000 or more nested functions fail to parse on all three builds, with the same result, so this table stops at 200.
Still not run
Thanks, ranmori, for the quick fix and the clear note. All harness results, base vs new
Tests
TimingCold in-process analysis, one fresh process per run, 10 runs per cell. Median in ms, with min to max. The table is the old-first order. I also ran 10 more with new first. Load average (runnable tasks, out of 24 threads) was 1.2 to 1.8 the whole time. I did not check what else was running.
Order did not change the gap (new about 4 to 8 percent higher both ways). Treat it as a small, repeatable cost, not noise. CLI, depth 24, 6 runs each, median seconds: old 1.26 to 1.27, new 1.27 to 1.29 across the four shapes. All exit 0, and the ranges overlap. That is within noise. Commandsgit -C rea.git fetch origin '+refs/pull/1941/head:refs/pull/1941/head'
git -C rea.git archive <sha> | tar -x -C <dir> # new, old, base
npm ci && npm run build && npm run test:prepare # new; old and base reuse the identical lockfile's node_modules
# wrapper chains, one process per build and fixture
timeout 300 node wrap-run.mjs <build>/dist <fixture>/app.js <N>
# CLI
node scripts/rea.mjs analyze-javascript-application <fixture-dir> --json
# random programs and IR compare
node fuzz-ir-diff.mjs <distA> <distB> <count> <seed>
node ir-diff.mjs <distA> <distB> <fixtures> <names...>
# tests
node node_modules/vitest/vitest.mjs run --project domain src/domain/javascript/javascriptSemanticAnalysis.scope_depth.test.ts
node node_modules/vitest/vitest.mjs run --project domain
npm run test:fastWrapper timeouts were 300 s for N up to 1,000 and 120 s for 3,000 and 10,000. Test setup
🤖 Generated with Claude Code (Sonnet 5.5, verified by Opus 5.5) 🦊 |
baron
left a comment
There was a problem hiding this comment.
Reviewed exact head 72a88db and current-main integration. No confirmed source blocker.
Caller-independent values are analysis-state-owned, excluded from capture-context reuse, cycle-sensitive, and reusable only with at least the derivation budget under which they were established. Producer-side mutation invalidation remains intact; primitive/capture-point/independent caches serve distinct semantics and should not be collapsed merely to reduce lines. Object/array/sequence depth controls and closure tests meaningfully preserve capacity/unknown behavior.
Pinned validation: 301 focused head tests, 305 current-main integration tests, check:fast/build prerequisites, and a 32-level inert closure CLI/MCP full-Evidence-parity/cleanup witness passed. The old composite-depth review lead is addressed by the depth-keyed current source. Exact-head hosted CI is green; no real-provider or performance-enforcement claim.
Problem
A closure that aliases a binding from its enclosing scope makes semantic value
evaluation exponential in nesting depth:
Each closure evaluates the outer binding once live and once at its own capture
point. The capture cannot order a binding declared in another function, so it
falls back to a second live evaluation. Only primitive results were kept, so
every level doubled the work.
Observed on Windows 11 x64, Node 24.13.0, at main
d802304c:analyzeJavaScriptSemantics, 20 nested closures (~3 KB)analyze-javascript-applicationon one 443 KB generated wrapper (24-deep closures)provider_timeoutat 300 s, exit 1The same doubling occurs when the aliased value is an object, a member read,
a logical fallback, or an unknown parameter. A chain ending in a literal was
already fast because primitives were cached.
Change
Keep a live binding value when no caller could change it: no alias cycle closes
on or above the binding, and the expression depth budget was not reached while
evaluating it. Bindings that sit on a cycle are still re-evaluated, so their
values and reasons are unchanged. The existing cache-clearing on mutation and
escape effects covers the new entries.
Validation
javascriptSemanticAnalysis.scope_depth.test.ts(object root,self-referential namespace root, unknown parameter root) fail without the fix
(589,823 ancestry reads against a bound of 18,360) and pass with it.
vitest run --project domain: 136 files, 2,002 tests pass.npm run check:fastpasses.analyzeJavaScriptSemanticsoutput isbyte-identical before and after on 600 JavaScript files sampled from
node_modulesplus 12 inline alias-cycle, closure, mutation and escape cases.Total analysis time over that corpus showed no consistent difference across
three alternating runs.
Not verified
Windows native addon with a source-built runtime.
npm run check,check:pr, and the MCP workflow. Only the CLI was run end to end.reach that budget are deliberately not kept, so such a chain evaluated from
its deep end first is not bounded by this change.
JavaScriptModuleSpecifierPaths.test.tsandJavaScriptFileUrlPaths.test.tsfail on this Windows host with and withoutthe change (they create file names containing
?and#).Found while building a source-owned reproduction for #1847. That report's worker
heap exhaustion is a separate, already closed defect; this PR does not claim to
address it.