Skip to content

fix(javascript): preserve effects through yielded array references - #1437

Merged
N0zoM1z0 merged 1 commit into
mainfrom
fix/javascript-array-iteration-aliases
Oct 10, 2026
Merged

N0zoM1z0 merged 1 commit into
mainfrom
fix/javascript-array-iteration-aliases

Conversation

@N0zoM1z0

Copy link
Copy Markdown
Collaborator

for...of lost the reference from its loop binding to a yielded array element, so a later member write left exported return shapes reporting a stale literal. Preserve yielded references through existing destructuring/copy handling and the iterable fallback boundary. Resolve var and existing assignment targets in their actual lexical scope; treat yielded values stored in member-expression heads as escaped references.

For example:

const shared = { x: 1, keep: 7 };
for (const item of [shared]) item.x = 2;
return { x: shared.x, keep: shared.keep };

Before: x was reported as literal 1. After: x is unknown and keep remains literal 7. Actual source-owned Node 22/24 execution returns x: 2. The static analyzer does not pretend to execute the loop.

Validation:

  • A compiled public CLI regression failed against unmodified main, then passed through both CLI and actual SDK-connected stdio MCP. Covers 24 source variants: inline/named/spread arrays, var and existing/destructured/member targets, copies/defaults/rest, update/delete/escape, for await...of, indexed loops, and unaffected sibling/read-only/empty/for-in/shadow controls. MCP text and structured results preserve the same Evidence; the session remains usable.
  • All 343 adjacent semantic tests pass across six suites.
  • npm run check:fast and npm run verify:test-discovery pass.
  • Real Node 22.22.2 and 24.18.0 stdio MCP probes preserve the corrected values and controls.

No tool inventory or public schema change. No native provider changed; real Hopper/Ghidra verification was not performed. No private application or Node 26 coverage is claimed.

Closes #1428.

@N0zoM1z0 N0zoM1z0 added bug Something isn't working area: domain Domain logic, errors, and pure functions labels Oct 10, 2026

@N0zoM1z0 N0zoM1z0 left a comment •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Reviewed 62bffde: yielded references retain their actual lexical owner and reuse existing iterable/copy semantics; member-expression loop heads preserve reference escapes. The original stale-literal CLI regression fails on unmodified main, and the corrected CLI/stdio MCP workflow covers 24 source variants while retaining unaffected literals. All 343 adjacent semantic tests, static checks, discovery and real Node 22/24 MCP probes pass. No schema or provider change. Final-head CI 38025036702 and Real JavaScript recovery 38025036687 completed successfully; all 20 checks passed and there are no unresolved review threads.

@N0zoM1z0

Copy link
Copy Markdown
Collaborator Author

Independently rebuilt frozen main 0b9815b6 and reviewed head 62bffde3, then ran the same 338-byte inert source-owned target through the public CLI. Main reports stale literal 1 for both inline and named arrays, while the candidate reports unknown and retains the forEach control. The compiled CLI/stdin MCP acceptance workflow and six adjacent semantic suites pass under the local two-CPU / 2 GiB family guard.

An additional 780-byte control target retains object-rest and array-rest exclusions, undefined defaults, and mutation provenance when the source variable is reassigned in the loop. Those controls match main. There is also a pre-existing precision gap, rather than a new regression:

export function result() {
  const shared = {x: 1}, other = {x: 2};
  for (const {child: item = shared} of [{child: other}]) item.x = 3;
  return shared.x;
}

Both main and this head report unknown; actual source-owned Node execution returns 1. The default is unreachable for the observed yielded object. This can be tracked independently of the stale-literal correction; do not treat it as an introduced regression.

The new acceptance child's explicitly selected environment also omits NODE_OPTIONS, so a caller's test heap setting does not reach its CLI/MCP subprocesses. The guarded small-fixture run succeeds, but an explicit heap setting in those owned child environments would retain the intended local resource control. No native-provider certification is claimed from these JavaScript workflows.

@N0zoM1z0
N0zoM1z0 marked this pull request as ready for review October 10, 2026 05:00
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@N0zoM1z0
N0zoM1z0 merged commit 0a14ca4 into main Oct 10, 2026
20 checks passed
@N0zoM1z0
N0zoM1z0 deleted the fix/javascript-array-iteration-aliases branch October 10, 2026 05:00
@CassieFox23

CassieFox23 commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR: This merged while I was testing, so this is a post-merge check of 62bffde3. It fixes my #1428 repro and 15 more loop shapes. It adds no wrong answers and loses no facts I track. Tests and the full suite pass. Speed is the same.

Loop soundness

I ran 38 small loop cases on base 0b9815b6 and on 62bffde3. Each ran on Node 22, 24 and 26. All three Nodes agreed. I checked each case against real JS.

case real JS base 62bffde3
#1428 repro: inline, named, with keep 2 1, wrong unknown
[o.child] and [o, o.child] 2 1, wrong unknown
destructured heads {inner} and [a] 2 1, wrong unknown
9 more for...of shapes 2 1, wrong unknown
controls 1 right literal right literal
while loop that walks m0 twice 2 1, wrong 1, wrong
arr[0] = shared, then for...of 2 1, wrong 1, wrong
for...of over a generator 2 1, wrong 1, wrong
t = other before t.x = 2 1 1 unknown

The untouched field stays literal 7, as the PR says. The three wrong rows are wrong on base too. The arr[0] = shared one needs no loop at all. That is #1442 now.

The last row is safe, just careful. Base only got 1 because it skipped the loop write.

Regression, tests and speed

check Node 22 Node 24 Node 26
JS semantic suites + new test (794 tests) pass pass pass
new test on base - fails, as it should -
full suite - 6,337 pass, 89 skip -
tsc --noEmit and oxlint - pass -

I reran the 59 cases from my #1390 review too. 55 give the same result and the same IR. The other 4 are for...of cases. Each went from a wrong 1 to unknown. No new wrong value, no lost fact, no timeout.

Loop timings and the Hermes Desktop (MIT) chunk match base within noise. Numbers are in the fold.

Not run

Thanks, N0zoM1z0, for such a quick fix. And thanks, akushonkamen, for finding the exact lines.

The 9 shapes and the controls

The 9 more for...of shapes:

  • a write under if (flag)
  • a copy of t in the body
  • t saved to an outer let
  • for (t of ...), then a write after the loop
  • nested loops
  • a write to t.child.x
  • a two-item array
  • for await
  • for (holder.item of ...)

The controls:

  • a loop that only reads
  • a write to another object
  • another object in the array
  • an empty array
  • two shadowed names
  • a field the loop never writes

for...in, indexed arr[i].x = 2 and forEach were unknown on both trees.

Speed

Median of 3 runs on Node 24, in ms:

loop k base 62bffde3
#1401 loop 6 / 9 / 12 22.0 / 23.3 / 25.1 21.8 / 23.2 / 23.7
write loop 6 / 9 / 12 23.4 / 23.9 / 26.0 21.9 / 23.6 / 27.2

Hermes Desktop (MIT) chunk, 3 runs each, analysis ms: base 1,521 / 1,577 / 1,520; 62bffde3 1,690 / 1,521 / 1,551. Peak memory about 393 MiB on both. Same IR hash on all six. Other jobs shared the box.

Commands
  • Each case body is wrapped in export function result() { ... }.
  • It goes through analyzeJavaScriptSemantics from the built dist/.
  • Each run: ( ulimit -c 0; timeout 120 node --max-old-space-size=3000 run.mjs <tree> file <case.js> ).
  • Real JS: each body runs in a fresh vm context.
  • 228 loop runs and 118 corpus runs, all exit 0.
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: 62bffde3 / 0b9815b6 (merged as 0a14ca41)

🤖 Generated with Claude Code (Opus 5.5) 🦊

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.

[Bug] for...of over an array reports a stale literal after a member write

2 participants