What a path leaves behind: a change undone on only some ways out, a test the code has just decided #138

Merged
sionzee merged 148 commits from left-behind into main 2026-11-22 17:41:01 +01:00
Owner

Based on unread-answers (#137), and contains it: the diff shown is this piece alone.

Two rules about what a function's writes leave behind on each of its paths. Each of these passed tsc strict, safe-ts check --deny-warnings and oxlint:

async function reload(panel: Mut<Panel>): Promise<Result<void, string>> {
  panel.busy = true;
  panel.text = q(await load());       // `q` returns the error: `busy` stays true, the button stays disabled
  panel.busy = false;
  return undefined;
}

function visit(room: Mut<Room>, calledOff: boolean): string {
  room.users += 1;
  if (calledOff) return "called off"; // leaves without the decrement
  work(room);
  room.users -= 1;
  return "done";
}

stop(mut(worker));                                          // `stop` sets worker.task = undefined
if (worker.task !== undefined) taken.delete(worker.task);   // always false: the slot is never given back

Two rules

unbalanced-update (warning). A change and the change that undoes it, as two statements of one statement list, and a way out between them that carries the first without the second. Pairs the code shows by itself: a boolean set and cleared; += and -= by the same step; add / delete, set / delete with the same key, push / pop; a place given back what a const saved of it; and the same made by callees (enter(mut(room)) / leave(mut(room))) where a summary shows each makes that one change on every path. Ways out: a return, a throw, an await whose call can reject, a q(...) returning the error, a continue or break ending a loop's round. A call between the two halves is the work the pair brackets, whatever it may change. A flag left set on a written return is reported only where the function tested the flag first, as a lock: without that, "hide the old error, return on success" is reported for nothing. The fix is try / finally.

decided-test (error). A place given a literal by an assignment, or by a callee that always leaves that literal there, and tested against a literal before anything could change it. The message names the line that set it. Anything that could change the place ends what is known: a write of it or of what holds it, a call given its object (a function with no body to read, a method a subclass may override, a function value, a getter, Object.assign), an await for anything but the function's own locals, a join where paths disagree. The page has a table of every form of call and what it ends, each with a reported twin in the fixture.

Both stand on the control-flow layer, which gained graphs reduced to what one analysis asks of them.

Evidence

  • Held-out cases. Pairs: 15 of mine that leave something behind are reported (an early return past a decrement, a delete, a restore and a callee's undo; a throw; a round ended by continue; a flag, a counter and a map entry around a rejecting call, fetch, a Throwing callback, q(...)), and the 12 correct forms are silent (a finally, both outcomes handled, a function that only sets, a key kept on purpose, a failed state). Decided tests: 8 reported, 20 forms where something may have changed the place silent.
  • Three rounds. The first reported a test as decided after a function with no body had been lent the object, and after a method a subclass overrides: both fixed. The fix then silenced the pair rule whenever an unreadable call stood between the halves: fixed, the two rules now treat such a call differently. A stuck flag written with q(...) and a loop's round were added.
  • Soundness corpus: a class for a flag stuck on a rejection, added and closed. The two field classes this piece was started for stay open, and the list says so: their real fixes are spread over functions (the take in one, the give-back in another keyed on a state one path never reaches; a callee reading what its caller already cleared), which neither rule sees. Each class now has that shape as a second program, escaping. Trying the cross-function test on the real code gave 11 candidates with one real: not something a rule that must not guess can report. 16 of 100 escape.

Yield and cost

  • It finds nothing in real code. No finding on an adopter's project (760 files), on about a thousand files of four open-source code bases, or on this repository. The only candidates on the adopter were 20 flags in test files (state of a fixture made per test), which the rule now leaves alone.
  • It costs more than any other piece. On that project's largest entry, minimum and median of 31 interleaved runs: per-file analysis 212 / 224 ms before, 217 / 239 ms after; whole-program pass 532 / 542 ms before, 552 / 563 ms after. That is 3 to 5% of a whole check. The implementer cut it from 3,815 graphs and 250 seconds on one directory in the first version to 97 graphs and 140 full walks of 10,454 functions; what is left is a look at each function and a summary of what callees with a Mut parameter change.
  • For it: no false positive anywhere measured, and the same-function forms are the commonest way these two mistakes are written in new code.
  • Against it: the evidence that they occur is the three examples above, not a bug anyone shipped.

What turns red on upgrade

decided-test is an error and unbalanced-update a warning. Neither reports anything on the code measured, so nothing should.

What stays unseen

  • A pair whose halves are in different functions, or have nothing in common the code shows (lock(name) / unlock(name) on a table elsewhere).
  • A change a callee makes only on some paths, or below the callee: a summary describes a callee's own changes.
  • A synchronous throw from a callee between the halves.
  • A second reference to a parameter that is not passed to the call, which the loan rules assume away.

Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review, acceptance tests, the summary depth) in Claude Code.

Based on `unread-answers` (#137), and contains it: the diff shown is this piece alone. Two rules about what a function's writes leave behind on each of its paths. Each of these passed `tsc` strict, `safe-ts check --deny-warnings` and oxlint: ```ts async function reload(panel: Mut<Panel>): Promise<Result<void, string>> { panel.busy = true; panel.text = q(await load()); // `q` returns the error: `busy` stays true, the button stays disabled panel.busy = false; return undefined; } function visit(room: Mut<Room>, calledOff: boolean): string { room.users += 1; if (calledOff) return "called off"; // leaves without the decrement work(room); room.users -= 1; return "done"; } stop(mut(worker)); // `stop` sets worker.task = undefined if (worker.task !== undefined) taken.delete(worker.task); // always false: the slot is never given back ``` ## Two rules **`unbalanced-update`** (warning). A change and the change that undoes it, as two statements of one statement list, and a way out between them that carries the first without the second. Pairs the code shows by itself: a boolean set and cleared; `+=` and `-=` by the same step; `add` / `delete`, `set` / `delete` with the same key, `push` / `pop`; a place given back what a `const` saved of it; and the same made by callees (`enter(mut(room))` / `leave(mut(room))`) where a summary shows each makes that one change on every path. Ways out: a `return`, a `throw`, an `await` whose call can reject, a `q(...)` returning the error, a `continue` or `break` ending a loop's round. A call between the two halves is the work the pair brackets, whatever it may change. A flag left set on a written `return` is reported only where the function tested the flag first, as a lock: without that, "hide the old error, return on success" is reported for nothing. The fix is `try` / `finally`. **`decided-test`** (error). A place given a literal by an assignment, or by a callee that always leaves that literal there, and tested against a literal before anything could change it. The message names the line that set it. Anything that could change the place ends what is known: a write of it or of what holds it, a call given its object (a function with no body to read, a method a subclass may override, a function value, a getter, `Object.assign`), an `await` for anything but the function's own locals, a join where paths disagree. The page has a table of every form of call and what it ends, each with a reported twin in the fixture. Both stand on the control-flow layer, which gained graphs reduced to what one analysis asks of them. ## Evidence - **Held-out cases.** Pairs: 15 of mine that leave something behind are reported (an early return past a decrement, a `delete`, a restore and a callee's undo; a `throw`; a round ended by `continue`; a flag, a counter and a map entry around a rejecting call, `fetch`, a `Throwing` callback, `q(...)`), and the 12 correct forms are silent (a `finally`, both outcomes handled, a function that only sets, a key kept on purpose, a failed state). Decided tests: 8 reported, 20 forms where something may have changed the place silent. - **Three rounds.** The first reported a test as decided after a function with no body had been lent the object, and after a method a subclass overrides: both fixed. The fix then silenced the pair rule whenever an unreadable call stood between the halves: fixed, the two rules now treat such a call differently. A stuck flag written with `q(...)` and a loop's round were added. - **Soundness corpus:** a class for a flag stuck on a rejection, added and closed. **The two field classes this piece was started for stay open**, and the list says so: their real fixes are spread over functions (the take in one, the give-back in another keyed on a state one path never reaches; a callee reading what its caller already cleared), which neither rule sees. Each class now has that shape as a second program, escaping. Trying the cross-function test on the real code gave 11 candidates with one real: not something a rule that must not guess can report. 16 of 100 escape. ## Yield and cost - **It finds nothing in real code.** No finding on an adopter's project (760 files), on about a thousand files of four open-source code bases, or on this repository. The only candidates on the adopter were 20 flags in test files (state of a fixture made per test), which the rule now leaves alone. - **It costs more than any other piece.** On that project's largest entry, minimum and median of 31 interleaved runs: per-file analysis 212 / 224 ms before, 217 / 239 ms after; whole-program pass 532 / 542 ms before, 552 / 563 ms after. That is 3 to 5% of a whole check. The implementer cut it from 3,815 graphs and 250 seconds on one directory in the first version to 97 graphs and 140 full walks of 10,454 functions; what is left is a look at each function and a summary of what callees with a `Mut` parameter change. - **For it:** no false positive anywhere measured, and the same-function forms are the commonest way these two mistakes are written in new code. - **Against it:** the evidence that they occur is the three examples above, not a bug anyone shipped. ## What turns red on upgrade `decided-test` is an error and `unbalanced-update` a warning. Neither reports anything on the code measured, so nothing should. ## What stays unseen - A pair whose halves are in different functions, or have nothing in common the code shows (`lock(name)` / `unlock(name)` on a table elsewhere). - A change a callee makes only on some paths, or below the callee: a summary describes a callee's own changes. - A synchronous `throw` from a callee between the halves. - A second reference to a parameter that is not passed to the call, which the loan rules assume away. Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review, acceptance tests, the summary depth) in Claude Code.
Open: an analysis whose facts depend on what calls do could only be solved in the file's own
analysis, where a callee in another file is unknown, or by parsing the file again in the
whole-program pass.

Closes it: `Graph::reduce` replaces each point by what one analysis says it does (owned events,
the block structure and edges as they are, the events of each block's last point marked for its
exception edge), and `solve_reduced` runs the same worklist driver over it. A file's analysis
keeps the reduced graph; the whole-program pass solves it once it knows the calls. A unit test
checks that a reduced graph solves as the graph does, both meets, out by an exception too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Open: two classes of the field corpus escaped every gate. A change and its undo with a way out
between that skips the undo (`exit-path-skips-release`: a count taken by `enter(mut(room))` and
given back by `leave(mut(room))`, not on the early `return`), and a test the code has just decided
(`state-cleared-before-read`: `stop(mut(worker))` clears `worker.task`, the next line asks whether
it is set). A flag set before an `await` and cleared after it, stuck when the call rejects, had
no class.

Closes them:
- `unbalanced-update` (warning): pairs written in one statement list (a boolean set and cleared,
  `+=` / `-=` with one step, `add` / `delete` and `set` / `delete` with one key, `push` / `pop`, a
  place given back what a `const` saved of it), also made by callees; ways out: an `await` whose
  call can fail (as `unreleased-resource` decides it; a promise of a `Result` doesn't reject), a
  `q(...)`, a `throw`, a `return` (for a flag only where the function tested it first). Not in
  test files. Quick fix *Undo it in a finally*.
- `decided-test` (error): a truthiness test or a comparison with a literal of a place that holds
  a literal the function gave it, by an assignment or a callee that always leaves it, with no
  write, no call that may change it, no `await` and no disagreeing join between.
- The whole-program pass sums up each function once per run (`project/left_behind.rs`): what it
  may change in each parameter and its receiver, what it always does to one on every path;
  memoised, counted as `writes_summarised` in the work guard (2 on its project: `enter`, `leave`).
- Corpus: `flag-stuck-on-rejection` added (escaping at the starting commit), and the three classes
  rejected, each by its rule alone; two lines off `escaping.txt`.
- On an adopter (both ways) and five outside code bases: no new finding outside the adopter's
  test files. Docs: two rule pages, the flow guide, README, quick fixes, async guide, changelog,
  the migration skill, the oxlint preset. The `vue-split-state` example of a lone `saving` flag
  gains the new rule: its `await saveOrder()` can reject there.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Open, from review:
- decided-test reported tests that were not decided: a `declare function` lent the place
  counted as changing nothing (a key without a body summarised as empty), and a method's summary
  stood for overrides a subclass may run.
- unbalanced-update missed the stuck flag in its idiomatic spelling (`q(await load())` between
  the set and the clear): a `q(...)` exit needed the lock test meant for written returns.
- exit-path-skips-release and state-cleared-before-read came off escaping.txt though the fixes
  they were reduced from are not reported.
- The cost on the adopter's largest entry, and names from its code in comments and fixtures.

Closes them:
- No summary for a callee without a body; a method's only where its class can't be extended
  (not exported, not extended in its file, not abstract). Accessors the receiver resolution ties
  a member to, destructuring, tagged templates, `new`, `import()`, `yield`, `o[k]()`,
  `.call` / `.apply` / `.bind` are calls; a built-in is idle only where the file shows it
  (`Analyzer::idle_call`). A fixture (decided_test/calls.ts) has each form beside a twin.
- `q(...)` exits count for a flag as a rejection does; fixture beside its `finally` twin.
- Each of the two classes gains a program in its fix's shape (take and give-back in different
  functions; a callee reading what its caller cleared), which escapes; both are listed again
  (16 of 100 escape) and their `source` says what the rule sees; the corpus README says a class
  stays open while any written form of its fix escapes.
- Cost: the loan rules make a non-`Mut` parameter's summary empty; summaries are per parameter
  and asked only for an argument holding something a graph asks about (578 on the largest
  entry, 1,502 before); candidacy counts only lent arguments (102 graphs, 120 before); a light
  first look at every function, the whole walk only for a candidate or a function a summary may
  be asked of (1,297 of 10,454); functions found from the node list; each point's events built
  once for its graph.
- Adopter names replaced by names of our own in comments, docs and fixtures.
- The vue-split-state example of a lone flag reads its `Result`, so both docs harnesses see the
  same function; its fence is back to `vue-split-state` alone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
What was open, and what closes it:

- An unknown call between the two halves silenced unbalanced-update: the
  shared "any call ends what is known" of decided-test ended the pair too.
  The two rules now treat it apart. For unbalanced-update a call between
  (a function with no body here, a package's call, a function value, a
  method that may be overridden, a place handed on) is the work the pair
  brackets: the pair stays open across it and only an undo (written, or
  made by a callee that always makes it) or the function's own write it
  can't follow closes it. decided-test keeps ending what is known there.
  Fixture unbalanced_update/between.ts: the three calls between a change
  and its undo, each reported; the same three between a write and a test
  of the place, each silent.

- A round of a loop is a statement list: a pair in a loop's body with a
  `continue` or `break` between its halves is reported, naming the line
  that ends the round (flow::Options::jump_points makes the jump a point
  of its block). A flag needs the lock test first, as for a written
  return. Fixture unbalanced_update/rounds.ts: two reported, three silent
  (balanced round, a break of an inner loop, two loops). No new finding on
  an adopter or five outside code bases.

- Cost: callee summaries are keyed by depth and follow a callee's own
  calls one level down (DEPTH = 1); deeper calls may change anything they
  are given. Depths 0, 1 and 2 report the same on the fixtures, the
  corpus, the rewrite ratchet, the adopter and the outside code; no
  summaries at all loses the callee fixtures and the first programs of
  two corpus classes. Functions of a checked file with no Mut parameter
  and no change followed by a way out are passed over from a look at the
  file's nodes; a Mut-parameter function that is no candidate keeps a
  light look that records only what touches its Mut parameters.

Ratchet: two `?.` lines by design (a change or an undo behind `?.` runs on
some paths only), the destructuring line off (fixed by the first item),
two more hoisted loans in the new fixture.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A callee's summary is its own changes
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m24s
Tests / test (pull_request) Successful in 12m40s
7e6e7d41a4
The summaries behind unbalanced-update and decided-test followed a callee's calls one level
down. Nothing needs it: the fixtures, the corpus, the rewrite list, an adopter's project and
twenty held-out projects report the same with the callee's own changes alone, which is the
simpler thing to say on the rule pages and the bounded thing to compute. A call the callee
makes may change anything it is given.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
sionzee changed target branch from unread-answers to main 2026-11-22 17:32:28 +01:00
sionzee changed title from What a path leaves behind: a change undone on only some ways out, a test the code has just decided (optional: no finding on real code yet) to What a path leaves behind: a change undone on only some ways out, a test the code has just decided 2026-11-22 17:32:37 +01:00
Merge main: the piece below has landed
Some checks failed
Tests / test (pull_request) Has been cancelled
Benchmarks / bench (pull_request) Has been cancelled
88635b8c90
Co-Authored-By: Claude Fable 5.2 <[email protected]>
sionzee deleted branch left-behind 2026-11-22 17:41:01 +01:00
Sign in to join this conversation.
No reviewers
No labels
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
Evobug/Safe-Ts!138
No description provided.