[PR #5303] fix(javascript): keep var reads before their declarator unknown #6865

Open
opened 2026-11-01 21:43:39 +00:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/morluto/rea/pull/5303
Author: @darshjoshi
Created: 10/19/2026
Status: 🔄 Open

Base: main ← Head: fix/5302-var-read-before-initializer


📝 Commits (3)

  • ab8578e fix(javascript): keep var reads before their declarator unknown
  • db88f85 test(javascript): cover var reads before their declarator in export shapes
  • 9ae2561 test(javascript): pin var reads inside nested functions keeping their literal

📊 Changes

2 files changed (+63 additions, -3 deletions)

View changed files

📝 src/domain/javascript/javascriptSemanticValues.ts (+8 -3)
📝 tests/boundary/filesystem/javascriptExportShapeComparison.test.ts (+55 -0)

📄 Description

Summary

Fixes #5302.

compare_javascript_export_shapes reported a definite literal for a field that reads a var before its declarator runs. At runtime the field is undefined: the declaration is hoisted, but the initializer has not executed yet.

Problem and expected behavior

Smallest trigger: return { kind: "k", a: v }; var v = 1; reports /a as literal 1, while Node returns a: undefined. The same happens with an alias, as in var w = v; var v = 1; return { kind: "k", a: w };.

The broken invariant comes from the export return-shape docs: "Rebound values are evaluated at their read position."

Change and scope

In evaluateExpression, an identifier was checked at its read position through semanticBindingAtRead only when its binding had an assignment definition. A var whose only write is its declarator initializer skipped that check, so evaluateBinding used the initializer no matter where the read was.

var bindings now go through semanticBindingAtRead too. When the order is proven, it drops initializers that do not come before the read, and the read becomes unknown. When the order cannot be proven, for example in reads from nested functions, the declarator value is kept as before. Treating those reads as unknown would make common bundle patterns such as var x = require(...) read inside functions lose precision. Assignment-bearing bindings behave exactly as before.

Not included: modelling the read as a definite undefined. It stays conservative and unknown.

The open PR #4173 edits the same identifier check to add read-order checks for let/const bindings, and it excludes var. This change is independent of it, and whichever lands second needs a small merge.

Follow the contribution scope. Unresolved scope, authority, side effects, or required verification block integration. Link the required maintainer scope decision for new providers or capabilities involving target execution, injection, mutation, or protection recovery; ordinary fixes within existing boundaries may state "not applicable". Draft PRs may leave the scope decision pending and the readiness and merge checklists unchecked. Maintainers can help resolve open questions.

Contract and boundary impact

  • Semantic owner and earliest changed stage: JavaScript semantic value lattice (src/domain/javascript/javascriptSemanticValues.ts), identifier evaluation
  • Contribution scope fit and linked maintainer decision: not applicable, an ordinary fix within existing static-analysis boundaries
  • CLI and MCP/tool-catalog contract: no schema change. Affected reads now report unknown instead of a wrong literal.
  • Provider, bridge, target-format, or platform compatibility: none
  • Evidence, artifact, provenance, or reconstruction contract: none beyond the corrected value
  • Target selection, authority, upstream behavior, and cleanup details (if applicable): not applicable
  • Generated metadata (docs/public/product-catalog.json), package, or installation impact (CI generates and validates ignored catalogs and skills/; edit authored instructions in .agents/skills/reverse-engineer-anything/ and never commit build outputs): none

Expected effects (check known effects; use documentation-only on its own).
Unchecked items do not establish absence; leave uncertainties visible.

  • Read existing files or captured data.
  • Launch or attach to a process.
  • Inject instrumentation or run scripts/code.
  • Change process memory, registers, files, or configuration.
  • Contact network services.
  • Recover protected code or work around protections.
  • Documentation or usability only; no target interaction.
  • Not sure yet; investigation or maintainer guidance needed.

Evidence and regression coverage

  • Tests added or updated: tests/boundary/filesystem/javascriptExportShapeComparison.test.ts, new block "JavaScript export hoisted var reads". It has the two bug shapes plus a control showing that a read after the declarator still reports literal 1.
  • Base reproduction or other evidence: executed. With only the source file restored to b4938aed, both new cases fail (expected [ { …(11) } ] to deeply equal [ ObjectContaining{…} ]; the control passes). With the fix, all 51 tests in the file pass (52 with the nested-function test added in 9ae25617). The CLI reproduction from #5302 printed added /a {"availability":"literal","value":1} before the fix and no literal for /a after it.
  • Differential check: 93 straight-line probes run through the CLI, with each literal claim compared against calling the export in Node. Before: 3 wrong literals, all of them this bug (2 distinct shapes, one of which appears in both probe sets). After: 0 wrong literals, and every previously correct literal is still reported. Two redeclaration cases now resolve correctly (var a = 1; var a = 2; gives 2, matching Node).
  • User-visible CLI/MCP output (if applicable): the change for /a reports "right": {"availability": "unknown", ...} instead of {"availability": "literal", "value": 1}.
  • Remaining proof gaps: the differential covers straight-line function bodies. Reads from closures called before the declarator keep the existing behavior.

For evidence-bearing changes:

  • Observed, derived, and inferred claims remain distinguishable.
  • Artifact identity, source provenance, and failed attempts remain preserved.
  • Unsupported, incomplete, unavailable, or uncertain outcomes remain visible.

Validation performed

Node 24.18.0, npm 11.16.0, macOS arm64:

  • npm run test:focused -- tests/boundary/filesystem/javascriptExportShapeComparison.test.ts: 52 passed (after 9ae25617)
  • node scripts/run-development-tests.mjs changed --base upstream/main (this is test:changed with origin pointing at my fork): 135 files, 3314 tests passed
  • npm run check: passed. That covers typecheck, lint:built (0 errors), format:check, knip, metadata:check and architecture guards. javascriptSemanticValues.ts has the same lint warnings as on main.

Compatibility, safety, and release

  • Breaking changes or migration steps: none
  • Real Hopper/Ghidra, browser, or OS coverage: not applicable (pure static analysis); macOS only
  • Package or release metadata impact: none
  • Security, privacy, process, or containment review: no new effects

Author readiness

Human owner: @darshjoshi

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/morluto/rea/pull/5303 **Author:** [@darshjoshi](https://github.com/darshjoshi) **Created:** 10/19/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/5302-var-read-before-initializer` --- ### 📝 Commits (3) - [`ab8578e`](https://github.com/morluto/rea/commit/ab8578e895fd3180c9fa54ae91796556d0067926) fix(javascript): keep var reads before their declarator unknown - [`db88f85`](https://github.com/morluto/rea/commit/db88f851e047d9a0e146f744a0091d40065a76fe) test(javascript): cover var reads before their declarator in export shapes - [`9ae2561`](https://github.com/morluto/rea/commit/9ae256176ce75d63ab96701d7e41690010f58ff5) test(javascript): pin var reads inside nested functions keeping their literal ### 📊 Changes **2 files changed** (+63 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `src/domain/javascript/javascriptSemanticValues.ts` (+8 -3) 📝 `tests/boundary/filesystem/javascriptExportShapeComparison.test.ts` (+55 -0) </details> ### 📄 Description ## Summary Fixes #5302. `compare_javascript_export_shapes` reported a definite literal for a field that reads a `var` before its declarator runs. At runtime the field is `undefined`: the declaration is hoisted, but the initializer has not executed yet. ## Problem and expected behavior Smallest trigger: `return { kind: "k", a: v }; var v = 1;` reports `/a` as literal `1`, while Node returns `a: undefined`. The same happens with an alias, as in `var w = v; var v = 1; return { kind: "k", a: w };`. The broken invariant comes from the [export return-shape docs](https://github.com/morluto/rea/blob/main/docs/javascript-application-workflows.md#export-return-shape-comparison): "Rebound values are evaluated at their read position." ## Change and scope In `evaluateExpression`, an identifier was checked at its read position through `semanticBindingAtRead` only when its binding had an `assignment` definition. A `var` whose only write is its declarator initializer skipped that check, so `evaluateBinding` used the initializer no matter where the read was. `var` bindings now go through `semanticBindingAtRead` too. When the order is proven, it drops initializers that do not come before the read, and the read becomes unknown. When the order cannot be proven, for example in reads from nested functions, the declarator value is kept as before. Treating those reads as unknown would make common bundle patterns such as `var x = require(...)` read inside functions lose precision. Assignment-bearing bindings behave exactly as before. Not included: modelling the read as a definite `undefined`. It stays conservative and unknown. The open PR #4173 edits the same identifier check to add read-order checks for `let`/`const` bindings, and it excludes `var`. This change is independent of it, and whichever lands second needs a small merge. Follow the [contribution scope](https://github.com/morluto/rea/blob/main/CONTRIBUTING.md#contribution-scope). Unresolved scope, authority, side effects, or required verification block integration. Link the required maintainer scope decision for new providers or capabilities involving target execution, injection, mutation, or protection recovery; ordinary fixes within existing boundaries may state "not applicable". Draft PRs may leave the scope decision pending and the readiness and merge checklists unchecked. Maintainers can help resolve open questions. ## Contract and boundary impact - Semantic owner and earliest changed stage: JavaScript semantic value lattice (`src/domain/javascript/javascriptSemanticValues.ts`), identifier evaluation - Contribution scope fit and linked maintainer decision: not applicable, an ordinary fix within existing static-analysis boundaries - CLI and MCP/tool-catalog contract: no schema change. Affected reads now report unknown instead of a wrong literal. - Provider, bridge, target-format, or platform compatibility: none - Evidence, artifact, provenance, or reconstruction contract: none beyond the corrected value - Target selection, authority, upstream behavior, and cleanup details (if applicable): not applicable - Generated metadata (`docs/public/product-catalog.json`), package, or installation impact (CI generates and validates ignored catalogs and `skills/`; edit authored instructions in `.agents/skills/reverse-engineer-anything/` and never commit build outputs): none Expected effects (check known effects; use documentation-only on its own). Unchecked items do not establish absence; leave uncertainties visible. - [x] Read existing files or captured data. - [ ] Launch or attach to a process. - [ ] Inject instrumentation or run scripts/code. - [ ] Change process memory, registers, files, or configuration. - [ ] Contact network services. - [ ] Recover protected code or work around protections. - [ ] Documentation or usability only; no target interaction. - [ ] Not sure yet; investigation or maintainer guidance needed. ## Evidence and regression coverage - Tests added or updated: `tests/boundary/filesystem/javascriptExportShapeComparison.test.ts`, new block "JavaScript export hoisted var reads". It has the two bug shapes plus a control showing that a read after the declarator still reports literal `1`. - Base reproduction or other evidence: executed. With only the source file restored to `b4938aed`, both new cases fail (`expected [ { …(11) } ] to deeply equal [ ObjectContaining{…} ]`; the control passes). With the fix, all 51 tests in the file pass (52 with the nested-function test added in 9ae25617). The CLI reproduction from #5302 printed `added /a {"availability":"literal","value":1}` before the fix and no literal for `/a` after it. - Differential check: 93 straight-line probes run through the CLI, with each literal claim compared against calling the export in Node. Before: 3 wrong literals, all of them this bug (2 distinct shapes, one of which appears in both probe sets). After: 0 wrong literals, and every previously correct literal is still reported. Two redeclaration cases now resolve correctly (`var a = 1; var a = 2;` gives `2`, matching Node). - User-visible CLI/MCP output (if applicable): the change for `/a` reports `"right": {"availability": "unknown", ...}` instead of `{"availability": "literal", "value": 1}`. - Remaining proof gaps: the differential covers straight-line function bodies. Reads from closures called before the declarator keep the existing behavior. For evidence-bearing changes: - [ ] Observed, derived, and inferred claims remain distinguishable. - [ ] Artifact identity, source provenance, and failed attempts remain preserved. - [ ] Unsupported, incomplete, unavailable, or uncertain outcomes remain visible. ## Validation performed Node 24.18.0, npm 11.16.0, macOS arm64: - `npm run test:focused -- tests/boundary/filesystem/javascriptExportShapeComparison.test.ts`: 52 passed (after 9ae25617) - `node scripts/run-development-tests.mjs changed --base upstream/main` (this is `test:changed` with `origin` pointing at my fork): 135 files, 3314 tests passed - `npm run check`: passed. That covers typecheck, lint:built (0 errors), format:check, knip, metadata:check and architecture guards. `javascriptSemanticValues.ts` has the same lint warnings as on main. ## Compatibility, safety, and release - Breaking changes or migration steps: none - Real Hopper/Ghidra, browser, or OS coverage: not applicable (pure static analysis); macOS only - Package or release metadata impact: none - Security, privacy, process, or containment review: no new effects ## Author readiness <!-- The human owner completes these declarations before requesting review. The PR author is the owner by default. A different owner must explicitly accept ownership on this PR. AI agents must leave these declarations to a human to dictate to them or there could be catastrophic consequences for many users of REA. Draft PRs may leave them unchecked. These declarations record initial readiness when opening the PR for review; later pushes do not require a new human sign-off. Keep the description and validation evidence current as the work changes. See CONTRIBUTING.md#pull-request-ownership-and-readiness. --> Human owner: @darshjoshi - [x] I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work. - [x] I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps. - [x] I declare this PR ready for initial maintainer review. ## Before merge <!-- Draft PRs may leave pending items unchecked. Resolve material scope and authority questions before integration. --> - [ ] This PR addresses a concrete problem or an agreed enhancement. - [ ] Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff. - [ ] The PR has one focused outcome and the title follows `type(scope): outcome`. - [ ] Related issue is linked, or the reason for not linking one is stated above. - [ ] Tests cover changed observable behavior and meaningful failure paths. - [ ] Owning docs, contracts, and generated metadata are updated where needed. - [ ] User-visible CLI/MCP changes include representative output. - [ ] I checked the final diff for secrets, unrelated cleanup, and unsupported claims. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
morluto/rea#6865
No description provided.