Skip to content

fix(javascript): attribute writes through a simple thunk's call result - #1468

Closed
akushonkamen wants to merge 1 commit into
morluto:mainfrom
akushonkamen:fix/1449-call-expr-referenced-values
Closed

akushonkamen wants to merge 1 commit into
morluto:mainfrom
akushonkamen:fix/1449-call-expr-referenced-values

Conversation

@akushonkamen

Copy link
Copy Markdown
Contributor

Fixes #1449

Root cause

referencedValues in src/domain/javascript/javascriptSemanticMemberMutations.ts handled Object/Array/Assignment/Await/Sequence/Conditional/Logical bases, but had no CallExpression branch: a call base such as get() fell through to the empty return [] fallback. For get().x = 2 with const get = () => shared, the write therefore never attributed to shared, binding.mutatedPaths stayed empty, the evaluation side never invalidated the cache (evaluateBinding walks mutatedPaths in javascriptSemanticValues.ts), and static_return_shapes kept reporting literal 1 while coverage claimed complete — a fact real JS cannot produce.

Reproduction

The issue's fixture (get().x = 2 through a thunk) reported literal 1; a return shape of literal 2/unknown is the truth. Equivalent forms const t = get(); t.x = 2, function get() { return shared; }, IIFE, member-chain thunks (() => shared.inner), delete and ++ targets all reproduced. Reproduced as 8 failing unit cases before the fix (red), all passing after (green).

Fix

referencedValues gains a CallExpression/OptionalCallExpression branch that resolves the call through simple local thunks:

  • The callee is a function expression (IIFE) or an identifier whose binding's initializer is a plain function.
  • The function must be synchronous and non-generator — async/generator callees hand back wrappers (promise/iterator), not the returned object, so writes through them do not reach the captured binding.
  • Each return at the function's own top level (skipping nested functions/classes) must be an identifier or a member chain with exact static keys rooted in a captured binding.

Resolved roots re-enter the existing alias walk as exact paths ([...chainKeys, ...initializerProjection, ...path]), where the per-origin binding lifetime already closes cycles — no fixed-cap pruning, consistent with the exact-path semantics established in #1390/#1445. A returned call is deliberately left unexpanded, so thunk chains cannot loop the pending queue.

Tests

src/domain/javascript/javascriptSemanticAnalysis.member_mutations.test.ts:

  • 8 new cases assert the write-through result becomes unknown: arrow thunk, stored call result, function declaration, function expression, IIFE, member-chain thunk, delete, ++.
  • 2 new guards assert no over-invalidation: a thunk returning a fresh object literal and an async thunk keep shared known.
  • Targeted file: 163 passed (was 8 failed / 155 passed before).
  • JavaScript domain project: 39 files, 739 passed.
  • Fast projects (domain, services, adapters, composition, conformance, evaluation): 330 files, 3634 passed, 7 skipped, 0 failed.
  • tsc --noEmit clean; prettier/oxfmt/oxlint clean.

Known remaining gap (not a regression)

Forms that park the call result in a property slot first (o.a = get(); o.a.x = 2) still need slot-level value flow to attribute the later write; this change does not alter their behavior. They are listed under the issue's own "Not run" scope and need a slot-carried-reference mechanism, which I'd propose as a follow-up.

Disclosure: prepared with AI assistance (ZCode agent, GLM), reviewed and tested by a human.

A write through a function's returned object, such as `get().x = 2`
where `get` is a local thunk returning `shared`, never reached the
underlying binding. The mutation collector's referencedValues had no
CallExpression branch, so call bases fell through to the empty fallback,
mutatedPaths stayed empty, and the return shape kept reporting the stale
literal while coverage claimed to be complete.

Call results now resolve through simple local thunks: the callee's
initializer must be a synchronous, non-generator function whose returns
are identifiers or member chains with exact static keys rooted in a
captured binding. Each resolved root re-enters the alias walk, where the
existing per-origin lifetime closes cycles; returned calls stay
unexpanded. Async and generator callees hand back wrappers rather than
the returned object, so they are skipped and unrelated results keep
their known literals.

Fixes morluto#1449

Co-authored-by: Claude Opus 5.5 <[email protected]>
@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

Copy link
Copy Markdown
Collaborator

Thanks for investigating this. A parallel maintainer fix is now in #1471, including the property-slot gap noted here and method/async/generator cases, with 34 compiled CLI/stdio MCP variants plus Node 22/24 runtime checks. Its final-head CI is running; I am tracking that broader fix for #1449.

@CassieFox23

CassieFox23 commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR: On 64 small cases, plain thunks now give an honest unknown instead of the stale literal 1. Methods, async, generators and a few thunk shapes still give literal 1. No control turned wrong.

Every row returns 2 in real JS.

Call shape main this PR
get().x = 2, with an arrow, function or function expression literal 1 unknown
const t = get(); t.x = 2 literal 1 unknown
(() => shared)().x = 2 literal 1 unknown
o.a = get(); o.a.x = 2, plain or IIFE literal 1 unknown
get().x += 1, Object.assign(get(), …) literal 1 unknown
() => o.inner, () => o.a.b, () => arr[0] literal 1 unknown
Two returns, let get reassigned, top-level call literal 1 unknown
box.get().x = 2, plain or through o.a literal 1 literal 1
new B().get().x = 2 literal 1 literal 1
const get2 = () => get(); get2().x = 2 literal 1 literal 1
() => (f ? shared : other) literal 1 literal 1
(() => shared).bind(null) literal 1 literal 1
(await get()).x = 2, await get().then(…) literal 1 literal 1
g().next().value.x = 2, spread, for…of literal 1 literal 1

The PR body says slot forms stay as they were. Here the plain slot went to unknown too.

The async skip fits get().x = 2, which writes the promise. But await and .next().value hand back shared itself.

Controls: 19 forms where 1 is right. They cover fresh objects, copies and primitives. Some write another field. They also cover writes to a promise or iterator. 16 keep literal 1. Two read an import and give unknown on main too. One moves to unknown here: let get = () => shared; get = () => other. That is safe, just less precise.

Tests: the PR's test file passes 163 of 163 on head. On main it fails 8 of 163. Those are the 8 new write-through cases. Both guards pass on main and head. Node 22, 24 and 26 agree. CLI and MCP agree on every case.

Not run

Edit: optional calls and async generators ran on main and #1471, not on this PR, in the #1471 results. Both give the stale literal 1 on main. Still not run: the full suite here, CommonJS, macOS and Windows.

Thanks, akushonkamen, for the quick fix and the clear write-up.

Full matrix (64 cases)

Each case is export function result() in app.js, unless files are named. In the cross-module rows, real JS loaded b.js before calling result(). With a.js alone, real JS gives 1. Code drops the const shared = { x: 1 } line and the final return shared.x. Main is 4e3d3354. 6.3.0 matched main on every row. CLI on Node 22 shown; Node 24, Node 26 and MCP gave the same.

Form Real JS 6.3.0 main #1468 #1468 verdict
const get = () => shared; get().x = 2; 2 literal 1 literal 1 unknown honest
const get = () => shared; const t = get(); t.x = 2; 2 literal 1 literal 1 unknown honest
function get() { return shared; } const o = {}; o.a = get(); o.a.x = 2; 2 literal 1 literal 1 unknown honest
const o = {}; o.a = (() => shared)(); o.a.x = 2; 2 literal 1 literal 1 unknown honest
const box = { get() { return shared; } }; const o = {}; o.a = box.get(); o.a.x = 2; 2 literal 1 literal 1 literal 1 WRONG
const o = {}; o.a = Object(shared); o.a.x = 2; 2 unknown unknown unknown honest
function get() { return shared; } get().x = 2; 2 literal 1 literal 1 unknown honest
const get = function () { return shared; }; get().x = 2; 2 literal 1 literal 1 unknown honest
(() => shared)().x = 2; 2 literal 1 literal 1 unknown honest
const box = { get() { return shared; } }; box.get().x = 2; 2 literal 1 literal 1 literal 1 WRONG
class B { get() { return shared; } } new B().get().x = 2; 2 literal 1 literal 1 literal 1 WRONG
const id = (v) => v; id(shared).x = 2; 2 unknown unknown unknown honest
const o = { inner: { y: 1 } }; const pick = (p) => p.inner; pick(o).y = 2; return o.inner.y; 2 unknown unknown unknown honest
const get = () => shared; const get2 = () => get(); get2().x = 2; 2 literal 1 literal 1 literal 1 WRONG
const other = { x: 1 }; const f = true; const get = () => (f ? shared : other); get().x = 2; 2 literal 1 literal 1 literal 1 WRONG
const other = { x: 1 }; const get = (f) => { if (f) return shared; return other; }; get(true).x = 2; 2 literal 1 literal 1 unknown honest
const other = { x: 1 }; let get = () => other; get = () => shared; get().x = 2; 2 literal 1 literal 1 unknown honest
const get = () => shared; get().x += 1; 2 literal 1 literal 1 unknown honest
const get = (() => shared).bind(null); get().x = 2; 2 literal 1 literal 1 literal 1 WRONG
const get = () => shared; Object.assign(get(), { x: 2 }); 2 literal 1 literal 1 unknown honest
const o = { inner: { y: 1 } }; const get = () => o.inner; get().y = 2; return o.inner.y; 2 literal 1 literal 1 unknown honest
const shared = { y: 1 }; const o = { inner: shared }; const get = () => o.inner; get().y = 2; return shared.y; 2 literal 1 literal 1 unknown honest
const o = { a: { b: { z: 1 } } }; const get = () => o.a.b; get().z = 2; return o.a.b.z; 2 literal 1 literal 1 unknown honest
const arr = [{ x: 1 }]; const get = () => arr[0]; get().x = 2; return arr[0].x; 2 literal 1 literal 1 unknown honest
const o = { x: { y: 1 }, get() { return this.x; } }; o.get().y = 2; return o.x.y; 2 unknown unknown unknown honest
async result: async function get() { return shared; } (await get()).x = 2; 2 literal 1 literal 1 literal 1 WRONG
async result: async function get() { return shared; } await get().then((t) => { t.x = 2; }); 2 literal 1 literal 1 literal 1 WRONG
function* g() { yield shared; } g().next().value.x = 2; 2 literal 1 literal 1 literal 1 WRONG
function* g() { yield shared; } [...g()][0].x = 2; 2 literal 1 literal 1 literal 1 WRONG
function* g() { yield shared; } for (const t of g()) t.x = 2; 2 literal 1 literal 1 literal 1 WRONG
a.js exports shared, get = () => shared / b.js result: get().x = 2; return shared.x 2 unknown unknown unknown honest
a.js exports shared, get / b.js top level: get().x = 2; result returns shared.x 2 unknown unknown unknown honest
a.js exports shared, get, result returns shared.x / b.js top level: get().x = 2 2 literal 1 literal 1 literal 1 WRONG
a.js exports shared, get / mid.js: export { shared, get } from './a.js' / b.js imports mid.js, result: get().x = 2; return shared.x 2 unknown unknown unknown honest
a.js exports shared, get / mid.js: export * from './a.js' / b.js imports mid.js, result: get().x = 2; return shared.x 2 unknown unknown unknown honest
CJS: a.js module.exports = { shared, get } / b.js result: get().x = 2; return shared.x 2 no shape no shape no shape not reached
CJS: a.js exports shared, get, result / b.js: require('./a.js').get().x = 2 2 no shape no shape no shape not reached
CJS: mid.js: module.exports = require('./a.js') / b.js requires mid.js, result: get().x = 2; return shared.x 2 no shape no shape no shape not reached
module-level shared, get; result returns shared.x; top level: get().x = 2 2 literal 1 literal 1 unknown honest
shared.x = 2; 2 unknown unknown unknown honest
const o = { inner: { y: 1 } }; o.inner.y = 2; return o.inner.y; 2 unknown unknown unknown honest
a.js exports shared / b.js result: shared.x = 2; return shared.x 2 unknown unknown unknown honest
CJS: a.js exports shared / b.js result: shared.x = 2; return shared.x 2 no shape no shape no shape not reached
a.js exports shared, result returns shared.x / b.js top level: shared.x = 2 2 literal 1 literal 1 literal 1 WRONG
module-level shared; result returns shared.x; top level: shared.x = 2 2 unknown unknown unknown honest
const get = () => ({ x: 1 }); get().x = 2; 1 literal 1 literal 1 literal 1 correct
const get = () => ({ ...shared }); get().x = 2; 1 literal 1 literal 1 literal 1 correct
const get = () => shared.x; let v = get(); v = 2; 1 literal 1 literal 1 literal 1 correct
const get = () => shared; const t = get(); const y = t.x; 1 literal 1 literal 1 literal 1 correct
const get = () => shared; get().y = 2; 1 literal 1 literal 1 literal 1 correct
const other = { x: 1 }; const get = () => other; get().x = 2; 1 literal 1 literal 1 literal 1 correct
const o = { inner: { y: 1 }, other: { y: 1 } }; const get = () => o.other; get().y = 2; return o.inner.y; 1 literal 1 literal 1 literal 1 correct
function* g() { yield { x: 1 }; } g().next().value.x = 2; 1 literal 1 literal 1 literal 1 correct
async result: async function get() { return { x: 1 }; } (await get()).x = 2; 1 literal 1 literal 1 literal 1 correct
async function get() { return shared; } get().then((t) => { t.x = 2; }); 1 literal 1 literal 1 literal 1 correct
a.js exports shared, make = () => ({ x: 1 }) / b.js result: make().x = 2; return shared.x 1 unknown unknown unknown lost precision
a.js exports shared / b.js result: return shared.x (no write anywhere) 1 unknown unknown unknown lost precision
a.js exports shared, get, result returns shared.x / b.js: export const y = get().x 1 literal 1 literal 1 literal 1 correct
const shared = { x: 1, inner: { y: 1 } }; const get = () => shared.inner; get().y = 2; 1 literal 1 literal 1 literal 1 correct
const make = (v) => ({ x: v }); make(1).x = 2; 1 literal 1 literal 1 literal 1 correct
(() => ({ x: 1 }))().x = 2; 1 literal 1 literal 1 literal 1 correct
const get = async () => shared; get().x = 2; 1 literal 1 literal 1 literal 1 correct
function* g() { yield shared; } g().x = 2; 1 literal 1 literal 1 literal 1 correct
const other = { x: 1 }; let get = () => shared; get = () => other; get().x = 2; 1 literal 1 literal 1 unknown lost precision
Commands
  • Build: git archive 9c559f3e, then npm ci and npm run build on Node 24. Main got the same steps.
  • CLI: rea analyze-javascript-application <dir> --artifact-format directory --json.
  • MCP: one stdio server per tree and Node, calling analyze_javascript_application.
  • Tests: vitest run src/domain/javascript/javascriptSemanticAnalysis.member_mutations.test.ts. For main I copied in the head's test file.
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: 9c559f3e / main 4e3d3354 (same src/domain/javascript as b118f044)
  • REA: 6.3.0 (released package) as well

🤖 Generated with Claude Code (Opus 5.5) 🦊

@N0zoM1z0

Copy link
Copy Markdown
Collaborator

Thanks for investigating this. #1471 has now merged with final-head CI green and compiled CLI/MCP coverage for these cases plus property-slot, method, async and generator references. Closing this as superseded; #1449 is resolved.

@N0zoM1z0 N0zoM1z0 closed this Oct 10, 2026
@morluto morluto added bug Something isn't working area: domain Domain logic, errors, and pure functions labels Oct 14, 2026
@akushonkamen
akushonkamen deleted the fix/1449-call-expr-referenced-values branch October 25, 2026 16:23
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] Writing through a function's returned object reports a stale literal

4 participants