Ways out the checker didn't see: a call that throws past a release, std's q under another name, an error q passes on that the function doesn't declare #140

Merged
sionzee merged 5 commits from ways-out into main 2026-11-23 13:32:24 +01:00
Owner

Three programs that pass tsc strict, safe-ts check --idiomatic --deny-warnings and oxlint, and are wrong when they run. All three came out of an outside audit of main; I confirmed each on the binary before anything was built.

function tick(n: number): void {
  const id = setInterval(log, 1000);
  check(n);                    // throws a PropagateError that a caller catches: the interval is never cleared
  clearInterval(id);
}

import { q as ask } from "@evobug_com/safe-ts-std/result";
const v = ask(parse(n));       // reported as a way out when written `q(...)`, invisible under another name

export function load(text: string): Result<number, "other"> {
  const n = q(parse(text));    // parse returns Result<number, "bad-number">: `load` returns an error it doesn't declare
  return n + 1;
}

The first leaves a resource open on a path the rules took to be impossible. The second is one rule reading a name's text. The third is a type that lies: q gives TypeScript the value, so tsc never compares the two error types, and a caller's exhaustive match meets an error it has no arm for. Before this, only safe-ts emit followed by tsc found it, which is not in the loop an agent runs.

What changes

A call that throws is a way out for unreleased-resource and unbalanced-update. A call between the acquisition and the release (or between a change and its undo) counts when the whole-program pass knows it can throw where it stands: a function of the project that throws or propagates, a Throws / Throwing slot, a built-in or a library member the maps mark as throwing, or a built-in that runs a throwing callback right away (list.forEach(check)). The file lists the calls and the pass decides, the same split the rules already use for awaits. A call that cannot fail is not a way out, and neither is one the checker cannot resolve: taking those to throw too changes no finding anywhere measured.

std's names are std's however they are imported. The shared normalisation now reads import { q as ask } (and Mut as Lent, isErr as failed, a pack's safe version under another name) by the exported name, so every rule knows it; messages and fixes keep the name the file writes. R.q(...) through a namespace import is read the same way, also next to a renamed import.

New rule q-undeclared-error (error). It compares the error q(...) passes on with the error the enclosing function declares, wherever both can be read from what is written: a project function's declared return type (across files, methods, q(await f()), a const read once), std's helpers (unsafe gives unknown, parseJson a JsonError; a table a test checks against std's sources), a pack's safe version, a declared function type, a function with no declared return type, and what a mapper returns. Literals, primitives, Enums and classes are compared by identity; anything it cannot decide is silent, and the page says exactly what is compared.

Evidence

  • The systematic part. The spelling harness gained a transformation that imports every std name under another name. It found 205 sites across 42 rules where a rule read the name's text (21 silent, 60 reporting on another line, 124 reporting something new). All are fixed; five lines remain where only the wording of a message differs.
  • Held-out cases, written by the reviewer and not shown to the implementer.
    • Ways out: 46 leaking forms, all reported (a callee in another file, re-exported, a default export, through a namespace; a method through a parameter, a local, a field, an element, a map's value, a static field, a call's result; an abstract method and its override; callbacks by name and in place in forEach, map, reduce, some, sort; a throw inside an argument or a template). 13 fine forms, all silent (a call that cannot fail, a release in finally, a callee that catches its own throw, a callback that runs later).
    • Error types: 32 of 33 mismatches reported; the one missed is a mapper returning a member of the error. 24 fitting forms silent (a wider union, a subclass under its base, unknown and string declared, a mapper that fits, a type parameter).
  • Two rounds. The first version missed a thrower imported from another file, which is nearly every call in a real project: it asked the library table first and never read the function's body. It also skipped std's helpers in the new rule and quoted R.q(...) as R(...). All fixed in the second round.
  • On an adopter's project (about 760 files): five new findings, all q-undeclared-error, all real: functions declaring Result<..., string> that pass on the Error object of a safe file-system call. Nothing else changes. Of its 300 q(...) sites the rule compares both sides at 238; the page lists the other 62 by kind.
  • Soundness corpus: three classes added and closed (sync-throw-skips-release, sync-throw-skips-undo, q-passes-undeclared-error). 16 of 103 escape.
  • Cost: +0.6% instructions for a whole check of that project's largest entry (17.55 to 17.66 billion, one thread, callgrind). Wall time cannot show it: pass 426 ms before, 429 ms after, minimum of 20 interleaved runs. The work guard's allocation count rose 0.5%.

What turns red on upgrade

  • q-undeclared-error is an error. Declare the error in the return type, or turn it into a declared one with q(result, (error): Declared => ...).
  • unreleased-resource and unbalanced-update (warnings) may report a path they did not see before. None on the code measured. The fix is try / finally.

What stays unseen

  • A loan still pending when a function throws: aliased-mut models no way out by exception at all. Listed in the flow guide; a change to the loan analysis of its own.
  • A pack's safe version against a named error type: the checker does not read package declarations, so it tells such an error only from literals and primitives.
  • A mapper whose result is not plainly one thing.
  • R.q(...) in a file that also declares a name q of its own (as before this change).

Found and left

safe-ts emit writes an import line that does not parse when q is used through a namespace import (import * as R from "…/result";, Err as __qErr). main does the same; it gets its own fix with transform tests.

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

Three programs that pass `tsc` strict, `safe-ts check --idiomatic --deny-warnings` and oxlint, and are wrong when they run. All three came out of an outside audit of `main`; I confirmed each on the binary before anything was built. ```ts function tick(n: number): void { const id = setInterval(log, 1000); check(n); // throws a PropagateError that a caller catches: the interval is never cleared clearInterval(id); } import { q as ask } from "@evobug_com/safe-ts-std/result"; const v = ask(parse(n)); // reported as a way out when written `q(...)`, invisible under another name export function load(text: string): Result<number, "other"> { const n = q(parse(text)); // parse returns Result<number, "bad-number">: `load` returns an error it doesn't declare return n + 1; } ``` The first leaves a resource open on a path the rules took to be impossible. The second is one rule reading a name's text. The third is a type that lies: `q` gives TypeScript the value, so `tsc` never compares the two error types, and a caller's exhaustive `match` meets an error it has no arm for. Before this, only `safe-ts emit` followed by `tsc` found it, which is not in the loop an agent runs. ## What changes **A call that throws is a way out** for `unreleased-resource` and `unbalanced-update`. A call between the acquisition and the release (or between a change and its undo) counts when the whole-program pass knows it can throw where it stands: a function of the project that throws or propagates, a `Throws` / `Throwing` slot, a built-in or a library member the maps mark as throwing, or a built-in that runs a throwing callback right away (`list.forEach(check)`). The file lists the calls and the pass decides, the same split the rules already use for `await`s. A call that cannot fail is not a way out, and neither is one the checker cannot resolve: taking those to throw too changes no finding anywhere measured. **std's names are std's however they are imported.** The shared normalisation now reads `import { q as ask }` (and `Mut as Lent`, `isErr as failed`, a pack's safe version under another name) by the exported name, so every rule knows it; messages and fixes keep the name the file writes. `R.q(...)` through a namespace import is read the same way, also next to a renamed import. **New rule `q-undeclared-error`** (error). It compares the error `q(...)` passes on with the error the enclosing function declares, wherever both can be read from what is written: a project function's declared return type (across files, methods, `q(await f())`, a `const` read once), std's helpers (`unsafe` gives `unknown`, `parseJson` a `JsonError`; a table a test checks against std's sources), a pack's safe version, a declared function type, a function with no declared return type, and what a mapper returns. Literals, primitives, `Enum`s and classes are compared by identity; anything it cannot decide is silent, and the page says exactly what is compared. ## Evidence - **The systematic part.** The spelling harness gained a transformation that imports every std name under another name. It found **205 sites across 42 rules** where a rule read the name's text (21 silent, 60 reporting on another line, 124 reporting something new). All are fixed; five lines remain where only the wording of a message differs. - **Held-out cases, written by the reviewer and not shown to the implementer.** - Ways out: 46 leaking forms, all reported (a callee in another file, re-exported, a default export, through a namespace; a method through a parameter, a local, a field, an element, a map's value, a static field, a call's result; an abstract method and its override; callbacks by name and in place in `forEach`, `map`, `reduce`, `some`, `sort`; a throw inside an argument or a template). 13 fine forms, all silent (a call that cannot fail, a release in `finally`, a callee that catches its own throw, a callback that runs later). - Error types: 32 of 33 mismatches reported; the one missed is a mapper returning a member of the error. 24 fitting forms silent (a wider union, a subclass under its base, `unknown` and `string` declared, a mapper that fits, a type parameter). - **Two rounds.** The first version missed a thrower imported from another file, which is nearly every call in a real project: it asked the library table first and never read the function's body. It also skipped std's helpers in the new rule and quoted `R.q(...)` as `R(...)`. All fixed in the second round. - **On an adopter's project (about 760 files):** five new findings, all `q-undeclared-error`, all real: functions declaring `Result<..., string>` that pass on the `Error` object of a safe file-system call. Nothing else changes. Of its 300 `q(...)` sites the rule compares both sides at 238; the page lists the other 62 by kind. - **Soundness corpus:** three classes added and closed (`sync-throw-skips-release`, `sync-throw-skips-undo`, `q-passes-undeclared-error`). 16 of 103 escape. - **Cost:** +0.6% instructions for a whole check of that project's largest entry (17.55 to 17.66 billion, one thread, callgrind). Wall time cannot show it: pass 426 ms before, 429 ms after, minimum of 20 interleaved runs. The work guard's allocation count rose 0.5%. ## What turns red on upgrade - `q-undeclared-error` is an error. Declare the error in the return type, or turn it into a declared one with `q(result, (error): Declared => ...)`. - `unreleased-resource` and `unbalanced-update` (warnings) may report a path they did not see before. None on the code measured. The fix is `try` / `finally`. ## What stays unseen - A loan still pending when a function throws: `aliased-mut` models no way out by exception at all. Listed in the flow guide; a change to the loan analysis of its own. - A pack's safe version against a *named* error type: the checker does not read package declarations, so it tells such an error only from literals and primitives. - A mapper whose result is not plainly one thing. - `R.q(...)` in a file that also declares a name `q` of its own (as before this change). ## Found and left `safe-ts emit` writes an import line that does not parse when `q` is used through a namespace import (`import * as R from "…/result";, Err as __qErr`). `main` does the same; it gets its own fix with transform tests. Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review and acceptance tests) in Claude Code.
What was open: `unreleased-resource` and `unbalanced-update` took no synchronous call to be a
way out, so an interval set before `check(n)` (a function that throws a PropagateError) and
cleared after it leaked whenever a caller caught the throw; and four sites read std's `q` by its
text, so `const v = ask(parse(n))` with `q as ask` returned the error early unseen.

What closes it:
- The flow layer takes the calls an analysis names (`flow::Calls`): std's `q` by its import, and
  calls that may throw. `unreleased-resource` solves again with those calls throwing for what the
  first solve left open, and lists them as candidates; `unbalanced-update` gets an `Ev::Raise`
  after each such call. The whole-program pass decides (`Project::call_can_throw`): a project
  function that lets a failure out synchronously (through a `Throwing` slot too), a built-in that
  throws (`throwing_builtins`), a library member a pack marks as throwing. A call it can't resolve
  doesn't throw: measured on an adopter (1,128 of 5,813 calls asked about, nearly all built-in
  methods on receivers of no known class) the other choice changes no finding there, on the
  examples or on the fixtures.
- `spelling::renamed_imports`: a SAFE-TS export imported under another name is read by the name it
  is exported as; fixes and messages keep the written name. The harness's new transformation
  `import-rename` found 205 sites across 42 rules before, 0 after (5 `text` lines where the tree as
  parsed reads less). `superseded-task-write` and `failure-contract` no longer read `Mut<` and
  `PropagateError` from the source text.

On an adopter (entries and directories, --idiomatic): no finding changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
What was open: `q` gives TypeScript the value, so `const n = q(parse(text))` with `parse`
returning `Result<number, "bad-number">`, in a function declaring `Result<number, "other">`,
passed tsc and `safe-ts check` (which only asked that the function return some Result); a
caller's exhaustive match over the declared error met one it had no arm for. Only `safe-ts emit`
followed by tsc found it.

What closes it: the file's analysis records, per `q(...)` in a function with a declared return
type, the call its Result comes from (in place, awaited, a `const` read once, a `?.` chain) or the
mapper's declared return type, and the function's own declared type (`LocalAnalysis::q_errors`;
`FnInfo::declared_return`, and `err_returned` for a function with no declared return type). The
whole-program pass resolves the call as every rule does, opens both sides to their error types
through aliases and promises (`written::result_error_of`) and compares what can be decided
without guessing (`written::atoms`, `written::fits`): literals, primitives, Enums and classes by
identity (a subclass fits), other named types by identity, the language's error classes by name.
Silent on type parameters, generics, object types written in place, a pack's safe version.

Soundness corpus: `q-passes-undeclared-error` escapes on the starting commit and is rejected now
(16 of 101 classes escape). On an adopter: 300 `q` sites, 155 with both sides compared, no
finding; the examples unchanged. Spelling harness: two sites by design (`inline-const` makes the
`q` one `q-position` refuses; a `const` returned hides an undeclared function's errors), one
`text` line for the release fix around a copied callee; messages of `brand-constructor` and
`must-use-result` name a std export as the file writes it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
What was open (review of adb8cbbf): a callee in another file of the project didn't answer
whether it throws: `check(n)` imported, `new Reader(3).guard(n)`, `reader.guard(n)` on a local
made with `new`. A call of a function imported from a project file is also listed among the
package calls, and `call_can_throw` returned that list's "nothing known" before asking the
project's function. And a callback a built-in runs right away (`ns.forEach((n) => check(n))`,
`ns.forEach(check)`) let its throw out unseen.

What closes it: the package answer counts only when a pack describes the member; otherwise the
project's function is resolved (every written form of the receiver: a parameter, `new` in place
or in a local, a field, a field of a parameter, an element, a call's result, a static method, a
namespace import, a re-export, a default export, all through the shared resolution). A call that
runs callbacks right away (the analyzer's `sync_callbacks`) carries the calls written in them,
and a function passed by name, as its own (`left_behind::run_now_calls`); the first that can
throw is named. Both rules, fixtures for each form.

Soundness corpus: `sync-throw-skips-release` and `sync-throw-skips-undo` escape on the starting
commit (its CLI and its native addon for oxlint) and are rejected now (16 of 103 escape). On an
adopter: no finding changes. Work guard: `memo_questions` +144 (the imported callees resolved),
allocations +0.5%; ceilings regenerated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
What was open (review of 7b2d3f74's parent): with `import { q as ask }` and `import * as R` of
the same module in one file, `renamed_imports` made the first `q`, so the namespace member
`R.q` was no longer read as a name (a binding called `q` existed) and the leak past
`R.q(parse(n))` went silent, where the starting commit reported it. Findings also quoted the call
as `R(...)`.

What closes it: a namespace member of a SAFE-TS module that the file also imports by that name is
read as that binding (no import added), and a copy of it (`const take = R.q`) names that binding
too; messages quote the callee as written, dots included. The harness rewrites one site at a time
and can't write the two imports together: fixtures for it in `unreleased_resource` and
`q_undeclared_error`. Two by-design lines for `R?.q(...)` and `const RCopy1 = R; RCopy1.q(...)`,
which the transform doesn't rewrite (checked with `safe-ts emit`); `miss no-mutable-global
callee-const` closes as a side effect.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
q-undeclared-error reads std's helpers, safe versions, declared slots and mappers
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m50s
Tests / test (pull_request) Successful in 10m59s
622d764aab
What was open (review of adb8cbbf): the forms where the bug is most likely were silent.
`q(unsafe(...))` and `q(await unsafeAsync(...))` pass on `unknown`, `q(parseJson<T>(...))` a
`JsonError`, a pack's safe version an error object, `q(hooks.parse(text))` the error of a declared
function type, and a function with no declared return type with two returns gave nothing; most
mappers (by name, or an arrow written in place without a return type) weren't read.

What closes it: std's helpers by a table of their error types (`written::STD_ERRORS`), checked
against std's sources by a test so it can't drift; a pack's safe version as an error object of a
type not read (decided against literals and primitives only: the maps describe the failure, not
its type); a call through a declared function type by its contract's return type; an undeclared
function's errors from each `return` (a `const` read once followed, `.length` / `.size` a value);
a mapper by its declared return type, the function it names, or what an arrow written there
plainly returns; `unknown` passed on fits only `unknown`; std's types by name, installed or not.

On an adopter: 300 `q` sites, 238 compared (155 before), 5 findings, each a safe `fs` function's
error object passed on by a function declaring `Result<..., string>`: real, the declared type is
false for every caller. The rule's page says how far it reaches, by kind. Spelling harness: closes
`far q-undeclared-error hoist-const`, `new bounded-concurrency callee-const` and one `new
uncaught-propagation callee-const`; the new fixtures add sites of existing reasons, and two by-design
lines for `R?.q` and a copied namespace (not rewritten by the transform, checked with emit).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee deleted branch ways-out 2026-11-23 13:32:25 +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!140
No description provided.