Skip to content

fix(javascript): bound value evaluation through nested closure aliases - #1941

Merged
baron merged 2 commits into
morluto:mainfrom
ranmori:fix/javascript-closure-alias-evaluation
Oct 12, 2026
Merged

baron merged 2 commits into
morluto:mainfrom
ranmori:fix/javascript-closure-alias-evaluation

Conversation

@ranmori

@ranmori ranmori commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Problem

A closure that aliases a binding from its enclosing scope makes semantic value
evaluation exponential in nesting depth:

function f0(seed){ var s0 = seed;
  function f1(){ var s1 = s0;
    function f2(){ var s2 = s1; /* ... */ } } }

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:

Case Before After
analyzeJavaScriptSemantics, 20 nested closures (~3 KB) ~14 s 8 ms
analyze-javascript-application on one 443 KB generated wrapper (24-deep closures) provider_timeout at 300 s, exit 1 exit 0 in 23 s

The 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

  • New cases in 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:fast passes.
  • Differential check: serialized analyzeJavaScriptSemantics output is
    byte-identical before and after on 600 JavaScript files sampled from
    node_modules plus 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

  • Linux and macOS; all runs were on one Windows host, using the published 6.4.0
    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.
  • Alias chains longer than the 256-level expression depth budget. Values that
    reach that budget are deliberately not kept, so such a chain evaluated from
    its deep end first is not bounded by this change.
  • Four tests in JavaScriptModuleSpecifierPaths.test.ts and
    JavaScriptFileUrlPaths.test.ts fail on this Windows host with and without
    the 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.

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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-12T08:20:53.819873Z 72a88db New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 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 ||

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@CassieFox23

Copy link
Copy Markdown
Contributor

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

Check Node 22.23.1 Node 24.18.0 Node 26.11.1
Changed test file (4 tests) pass pass pass
vitest run --project domain (2,002 tests, 3 skipped) pass pass pass
The 4 ? and # path tests, with their 2 files (48 tests) pass pass pass
npm run test:fast (4,878 tests, 3 skipped) not run pass not run
  • The 3 new tests fail on base d802304 and pass on head. The object case reads 589,823 ancestry lookups against a bound of 18,360, the same figures as in the description.
  • The path tests that failed on Windows pass here on Linux. They pass on head and on base. Linux allows ? and # in file names.

Time for analyze-javascript-application, from each build's own CLI. Median of 3 runs, in seconds, for the unknown-parameter shape from the description:

Depth Base Head
15 1.41 1.33
20 4.37 1.33
24 49.44 1.40
  • About 1.3 seconds of every run is CLI start-up, so depths 5 to 15 look flat on both builds.
  • Three other shapes behave the same: an object, a self-referential s0 || {}, and a member read. At depth 24, base took 42.6, 82.0 and 86.1 seconds, and head took 1.3 seconds each.
  • Every run on both builds exited 0. Full CLI JSON was byte-identical between base and head for all 24 fixtures.
  • My fixtures are under 1 KB each, not the 443 KB wrapper from the description. Base finished at 24 levels here, so this does not dispute the 300-second timeout on that wrapper.

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 analyzeJavaScriptSemantics gave identical results, with 16, 46, 94 and 14 "Alias cycle" reasons. I also made 2,300 random programs. They had nested aliases, cycles, mutation and escapes. All 2,300 gave identical results on base and head, and 806 of them contained an alias cycle.

Not run

  • The MCP form of analyze_javascript_application. Only the CLI was run.
  • npm run check and npm run check:pr.
  • Alias chains past the 256-level depth budget. The description lists this as open.
  • The 443 KB generated wrapper from the description.
  • npm run test:fast on Node 22 and 26.
  • The base build on Node 22 and 26.
  • macOS.
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> --json

Fixtures came from a small script: depth 5, 10, 15, 20 and 24, each closure doing var sN = sN-1, rooted at seed, { value: 1 }, s0 || {} or seed.inner. The new test file was copied onto base to show it fails there.

Test setup
  • Box: Nobara 44 (Fedora 44 base), Linux 7.2.9
  • CPU: AMD Ryzen 9 5900X, 12 cores / 24 threads
  • RAM: 32 GB DDR4-3600
  • Node: 22.23.1, 24.18.0, 26.11.1
  • Head / base: ec28a5f / d802304 (fix(install): describe automatic setup and guard truncated downloads #1902)
  • Shared box: other test jobs ran at the same time. The load average (runnable tasks, out of 24 threads) was 3.5 to 5 during timing. Treat the seconds as rough. The ratios are the point.
  • The in-process base time for 20 levels was about 3 seconds here. The description reports 14 seconds on Windows.

🤖 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.
@ranmori

ranmori commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

@CassieFox23 Thanks for the independent run, especially the cyclic fixtures and the 2,300 random programs — and for confirming the four ?/# path failures are a Windows file-name limit.

One update: your results are for ec28a5f. The Codex review found that commit let a chain of composite wrappers (var s1 = {x: s0}; var s2 = {x: s1}; …) nest past the expression depth budget. 72a88db fixes that by reusing a retained value only at the depth it was derived at or shallower. Closure-chain timings are unchanged on my host, and the 612-input comparison against base is still byte-identical. If you have the harness handy, a re-run on 72a88db with long wrapper chains added to the generator would be welcome.

@CassieFox23

Copy link
Copy Markdown
Contributor

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.

Results

A chain is var s0 = {}; var s1 = {x: s0}; … var sN = {x: s(N-1)}. Each cell is how many wrappers deep the result nests. All runs used Node 24, one analysis per process.

Wrappers (N) Base d802304 Old ec28a5f New 72a88db
10 10 10 10
100 86, partial 100, complete 86, partial
1,000 86 1,000 (63 MB of JSON) 86
3,000 86 JSON.stringify overflow 86
10,000 86 JSON.stringify overflow 86
  • Codex was right about the old commit. JSON.stringify of the result threw "Maximum call stack size exceeded" at 3,000 wrappers. Arrays, nested braces and comma assignments did the same.
  • The new commit never overflowed. Its result is byte-identical to base on all 81 wrapper inputs where both finished. The same holds for the cyclic fixtures and 2,300 random programs.
  • The CLI never crashed on any build. With --json, 3,000 and 10,000 wrappers exited 0, and the output nested 11 levels deep. At 3,000 the old commit printed 89 MB, against 31 MB on base and new. At 10,000 every build printed about 20 MB, less than at 3,000. All 10,001 bindings are in that output. I did not find out why the size drops.
  • The new commit is much faster than base on nested closures, but only up to a size. Past it, it is as slow as base. The old commit stayed fast to the sizes I tried.
Shape Base Old head New head
Closures with var a = s; var s = {x: a} 47 s at 21 levels, over 60 s at 24 0.2 s at 90 0.2 s at 50, over 120 s at 60
Closures with var s = {x: s} 12 s at 21, over 60 s at 24 0.2 s at 90 0.2 s at 80, over 120 s at 90
Closures with var s = s_prev 8.6 s at 21, over 60 s at 24 0.2 s at 200 0.2 s at 100, over 120 s at 200
Flat mix of objects, arrays and comma assignments 3.5 s at 60, 113 s at 100 0.2 s at 100 0.2 s at 60, 4.8 s at 70, 113 s at 100

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.

  • Timing: the CLI is unchanged, as you said. A cold in-process run is 4 to 8 percent slower on the new commit, about 1 ms of 21 ms. Load average stayed under 2 on 24 threads.

Still not run

  • The MCP form of analyze_javascript_application.
  • npm run check and npm run check:pr.
  • The 443 KB wrapper from the description.
  • npm run test:fast on Node 22 and 26.
  • The base build on Node 22 and 26.
  • macOS.
  • Chains past 10,000.

Thanks, ranmori, for the quick fix and the clear note.

All harness results, base vs new
Check Base vs new 72a88db Old ec28a5f vs new
CLI --json, 24 fixtures (20 nested closures, 4 cyclic), exit codes all 0, 24 of 24 byte-identical all 0, 24 of 24 byte-identical
In-process IR, 20 fixtures (4 cyclic, 16 nested) 20 of 20 identical 20 of 20 identical
Random programs, seeds 1, 2 and 3 (300, 1,000, 1,000) 0 of 2,300 differ 0 of 2,300 differ
Alias-cycle reasons, cyclic fixtures 3, 8, 16, mix 16, 46, 94, 14 on both 16, 46, 94, 14 on both
  • The old and new commits differ only on long wrapper chains. On the random programs they gave the same output. The counts of programs that contained an alias cycle (103, 356, 347) match my first run.
  • One shape, module.exports = sN after 10,000 wrappers, ran out of memory on all three builds. That includes base, so it is not this PR.
  • Wrapper chains, old vs new: 72 of 91 comparable inputs are identical. The rest are chains past the budget, where old nests deeper.
  • On base and new, the depth stops growing at a fixed value. It is the same at N of 100, 1,000, 3,000 and 10,000, and under 256.
  • Objects, arrays and comma assignments stop at 86. So does a leaf that is a parameter.
  • They are 52 for an alias plus a wrapper, and 154 for three-level braces.
  • Closure chains from the description (24 levels): CLI 1.3 s on both commits.
  • Base took 41 to 83 s on those in this run. My first comment had 43 to 86 s.
Tests
Check Node 22.23.1 Node 24.18.0 Node 26.11.1
Changed test file (7 tests) pass pass pass
vitest run --project domain (2,002 tests, 3 skipped) pass pass pass
The 2 path test files (48 tests) pass pass pass
npm run test:fast (4,878 tests, 3 skipped) not run pass not run
  • The new test file on the old commit: 3 of 7 fail (the object, array and sequence-assigned wrapper tests). On base the 3 closure-alias tests fail instead.
  • Node 22 and 26 ran node_modules/vitest/vitest.mjs under that node binary. I did not separately check the worker runtime.
Timing

Cold 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.

Shape, depth Old ec28a5f New 72a88db
unknown parameter, 20 20.1 (19.6 to 20.6) 21.1 (20.9 to 24.9)
unknown parameter, 24 21.1 (20.9 to 25.2) 22.9 (22.1 to 23.8)
object, 20 20.7 (20.1 to 23.8) 21.5 (20.9 to 23.2)
object, 24 22.0 (21.4 to 23.2) 23.3 (22.6 to 24.8)
self-referential, 24 22.0 (21.3 to 23.2) 23.7 (23.0 to 24.2)
member read, 24 22.0 (21.2 to 23.2) 23.4 (22.9 to 26.0)

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.

Commands
git -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:fast

Wrapper 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 baron left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: domain Domain logic, errors, and pure functions bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants