Escape hatches, round two: markers need permission, every spelling of a hatch is counted, declare is a cast #119

Merged
sionzee merged 39 commits from escape-hatches-2 into main 2026-11-21 23:28:33 +01:00
Owner

Based on sentinel-values (#118). The open pieces above #118 form one chain, each branch containing the ones below it: #119, #120, #121, #123, then #116 and #122. Merge them in that order. This is the bottom of the chain.

An agent aims at "the checker passes", so every way of making it pass without fixing the code gets used. The check already counts its escape hatches and ratchets them against a baseline. Four ways around that were still open. Each passed tsc strict, safe-ts check --deny-warnings and oxlint with the preset:

// @safe-ts std                              one comment turns every rule off for the file,
export function c(x: unknown): string {      and nothing says who may write it
  throw new Error(String(x as string));
}

// oxlint-disable-next-line safe-ts/no-any   silences the lint plugin, counted nowhere

declare const injected: Config;              a value and a type asserted to exist:
declare function lookup(key: string): Config;   a cast with no `as`

import * as r from "@evobug_com/safe-ts-std/result";
export const forged = r.trust<{ admin: boolean }>("no");   not counted, not reported: the baseline gate passed

What changes

File markers need permission. // @safe-ts std and // @safe-ts legacy are honoured only in files the project lists under "trustedFiles" / "legacyFiles" in package.json. An unlisted marker is an error (unlisted-marker), and the file is checked, transformed and followed as if the marker were not there, so writing the comment gains nothing. The CLI, editor, oxlint plugin, Vite plugin and Node loader decide through one function. Markers inside node_modules stay honoured.

Lint suppressions are counted, and the ones that silence SAFE-TS are reported (no-lint-suppression). Every eslint-disable* / oxlint-disable* form, in .ts, .tsx and Vue scripts and templates. Reported: a blanket one, one naming a safe-ts/ rule, and one naming a rule the preset sets to error (no-eval, no-new-func, ...) without -- reason. vue-tsc's template comments (@vue-ignore, @vue-skip, @vue-expect-error) get the treatment @ts-ignore and @ts-expect-error already have.

The same at configuration level. An .oxlintrc.json that does not extend the preset, turns one of its rules off or down, or leaves files out is listed under "weakened below the defaults" and ratcheted like package.json's settings.

Ambient declarations (no-unchecked-declare). Every declare form and the project's own .d.ts files are counted. Reported: the forms that assert a value nobody checks, such as declare const x: Config, declare function, declare class, a required member added to a built-in or to a package's interface, a bodiless declare module "x";, and declare value: string on a class field. Only counted: a declaration typed unknown or admitting undefined, optional members, wildcard asset modules with a body, and new types.

Every spelling of a hatch is counted. trust, unsafe, panic and defineSanitizer were counted only when called by their imported name. Now a namespace import, a copy, .call, a computed member, a hatch stored in an object or a field, and a re-export all count; a use the checker cannot follow counts once, so the count can only be too high. A package linked into the workspace is recognised by its package.json name.

The ratchet covers all of it. The new counts are in the summary line, --json and the baseline, and a test holds the three together.

What turns red on upgrade

  • Marked files. safe-ts check prints, per package.json, the exact "trustedFiles" / "legacyFiles" lines to add, with every marked file it found.
  • Old baseline files. A baseline written before these counts existed has no keys for them; a missing key means 0, so the first gate run fails and says why. Skipping the key would let the count rise unseen. Write the baseline again after reviewing.
  • Asserting declarations. On an adopter's project: 5 findings in 4 .d.ts files and 12 source files with declarations. A bundler-injected constant typed string, a virtual: module with a typed default, declare module '*.css';, and two required globals in test support code. The 12 others (optional members, globals typed Option<T>) are counted only.

That project gets 8 new findings in all: those 5, and one file whose std marker is no longer honoured until listed, with the cast and the object dictionary it was hiding. Nothing is lost. Its hatch counts are unchanged; it uses no other spelling of a hatch.

Review

The implementer's first round counted hatches by imported name only, which my held-out cases showed was the piece's own hole: r.trust<T>(x) passed the baseline gate with no finding. The second round replaced the per-name counters with one pass over every reference. I also checked mismatched lists (a legacy marker in a file listed as trusted is refused), markers below the header, block and JSDoc forms, and that // safe-ts-ignore unlisted-marker above a marker gains nothing.

Four classes were added to the soundness corpus (a cast behind an unlisted std or legacy marker, behind a declare, behind a required member on a built-in). All escaped at the starting commit and are rejected now.

What stays unseen

  • Members a library namespace gets in declare global, and members brought in by extends.
  • A bare read of a global declared Option<T> before it is set.
  • For .oxlintrc.json: the files its extends names, nested configs, oxlint -c, and JavaScript configs (one next to the package.json is a setup warning).
  • A list pattern broader than the files that use it. The file counts still ratchet.
  • The checker prints the list lines; it does not write them into package.json.

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

Based on `sentinel-values` (#118). The open pieces above #118 form one chain, each branch containing the ones below it: #119, #120, #121, #123, then #116 and #122. Merge them in that order. This is the bottom of the chain. An agent aims at "the checker passes", so every way of making it pass without fixing the code gets used. The check already counts its escape hatches and ratchets them against a baseline. Four ways around that were still open. Each passed `tsc` strict, `safe-ts check --deny-warnings` and oxlint with the preset: ```ts // @safe-ts std one comment turns every rule off for the file, export function c(x: unknown): string { and nothing says who may write it throw new Error(String(x as string)); } // oxlint-disable-next-line safe-ts/no-any silences the lint plugin, counted nowhere declare const injected: Config; a value and a type asserted to exist: declare function lookup(key: string): Config; a cast with no `as` import * as r from "@evobug_com/safe-ts-std/result"; export const forged = r.trust<{ admin: boolean }>("no"); not counted, not reported: the baseline gate passed ``` ## What changes **File markers need permission.** `// @safe-ts std` and `// @safe-ts legacy` are honoured only in files the project lists under `"trustedFiles"` / `"legacyFiles"` in package.json. An unlisted marker is an error (`unlisted-marker`), and the file is checked, transformed and followed as if the marker were not there, so writing the comment gains nothing. The CLI, editor, oxlint plugin, Vite plugin and Node loader decide through one function. Markers inside `node_modules` stay honoured. **Lint suppressions are counted, and the ones that silence SAFE-TS are reported** (`no-lint-suppression`). Every `eslint-disable*` / `oxlint-disable*` form, in `.ts`, `.tsx` and Vue scripts and templates. Reported: a blanket one, one naming a `safe-ts/` rule, and one naming a rule the preset sets to error (`no-eval`, `no-new-func`, ...) without `-- reason`. vue-tsc's template comments (`@vue-ignore`, `@vue-skip`, `@vue-expect-error`) get the treatment `@ts-ignore` and `@ts-expect-error` already have. **The same at configuration level.** An `.oxlintrc.json` that does not extend the preset, turns one of its rules off or down, or leaves files out is listed under "weakened below the defaults" and ratcheted like package.json's settings. **Ambient declarations** (`no-unchecked-declare`). Every `declare` form and the project's own `.d.ts` files are counted. Reported: the forms that assert a value nobody checks, such as `declare const x: Config`, `declare function`, `declare class`, a required member added to a built-in or to a package's interface, a bodiless `declare module "x";`, and `declare value: string` on a class field. Only counted: a declaration typed `unknown` or admitting `undefined`, optional members, wildcard asset modules with a body, and new types. **Every spelling of a hatch is counted.** `trust`, `unsafe`, `panic` and `defineSanitizer` were counted only when called by their imported name. Now a namespace import, a copy, `.call`, a computed member, a hatch stored in an object or a field, and a re-export all count; a use the checker cannot follow counts once, so the count can only be too high. A package linked into the workspace is recognised by its package.json name. **The ratchet covers all of it.** The new counts are in the summary line, `--json` and the baseline, and a test holds the three together. ## What turns red on upgrade - **Marked files.** `safe-ts check` prints, per package.json, the exact `"trustedFiles"` / `"legacyFiles"` lines to add, with every marked file it found. - **Old baseline files.** A baseline written before these counts existed has no keys for them; a missing key means 0, so the first gate run fails and says why. Skipping the key would let the count rise unseen. Write the baseline again after reviewing. - **Asserting declarations.** On an adopter's project: 5 findings in 4 `.d.ts` files and 12 source files with declarations. A bundler-injected constant typed `string`, a `virtual:` module with a typed default, `declare module '*.css';`, and two required globals in test support code. The 12 others (optional members, globals typed `Option<T>`) are counted only. That project gets 8 new findings in all: those 5, and one file whose `std` marker is no longer honoured until listed, with the cast and the object dictionary it was hiding. Nothing is lost. Its hatch counts are unchanged; it uses no other spelling of a hatch. ## Review The implementer's first round counted hatches by imported name only, which my held-out cases showed was the piece's own hole: `r.trust<T>(x)` passed the baseline gate with no finding. The second round replaced the per-name counters with one pass over every reference. I also checked mismatched lists (a `legacy` marker in a file listed as trusted is refused), markers below the header, block and JSDoc forms, and that `// safe-ts-ignore unlisted-marker` above a marker gains nothing. Four classes were added to the soundness corpus (a cast behind an unlisted `std` or `legacy` marker, behind a `declare`, behind a required member on a built-in). All escaped at the starting commit and are rejected now. ## What stays unseen - Members a library namespace gets in `declare global`, and members brought in by `extends`. - A bare read of a global declared `Option<T>` before it is set. - For `.oxlintrc.json`: the files its `extends` names, nested configs, `oxlint -c`, and JavaScript configs (one next to the package.json is a setup warning). - A list pattern broader than the files that use it. The file counts still ratchet. - The checker prints the list lines; it does not write them into package.json. Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review, acceptance tests and one fix) in Claude Code.
What was open: each of these passed tsc strict, `safe-ts check --idiomatic --deny-warnings`
and oxlint with the preset, and none was counted or ratcheted.

- `// @safe-ts std` / `// @safe-ts legacy`: one comment turned every rule off for a file (and
  `std` made its functions count as never failing for callers), and nothing said who may write
  it. Now a marker counts only in files the nearest "safe-ts" package.json lists under
  "trustedFiles" / "legacyFiles" (patterns as in "overrides", relative to it). Anywhere else it
  is an `unlisted-marker` error at the marker and the file is checked, transformed and followed
  as if it weren't there: check, the editor, the oxlint plugin, the Vite plugin and the loader
  all go through `Project::markers_allowed` (part of the analysis cache key). Library code in
  node_modules keeps its markers. `safe-ts check` prints, per package.json, the lines that list
  every marked file it found. The repository's packages, the demo, the fixtures and the docs
  examples list theirs.
- `eslint-disable*` / `oxlint-disable*` comments were counted nowhere, and silenced the oxlint
  plugin. Now all are counted; a blanket one or one naming a `safe-ts/` rule is reported by the
  new `no-lint-suppression`, on the directive's name inside the comment, where oxlint's own
  directive ranges don't reach (measured: a block directive applies from its comment's end, a
  `-line` one to the code before the comment). A named rule of another linter is only counted.
- `declare const | let | var | function | class | enum | namespace`, `declare global`,
  `declare module "..."` and the project's own .d.ts files were casts with no `as`. Now they
  are counted, the CLI checks the .d.ts files under the entries' directories (as the editor and
  oxlint already did), and the new `no-unchecked-declare` reports what asserts a value nothing
  checks (a type other than `unknown` or one admitting `undefined`, functions, classes, enums,
  required members added to a built-in or in a package augmentation, values added to a project
  module, `declare module "x";` with no body).
- `defineSanitizer(...)` was not counted; now it is, also through `export *` and renamed
  re-exports in project modules (and so are re-exported `trust()` and `panic()`).

The three counts are in the summary line, `--json --summary` and the baseline, and a test holds
the three together. An old baseline without the new keys loads; a missing count is 0, so the
gate fails once with a note until it is written again.

Soundness corpus: four new classes (unlisted std marker, unlisted legacy marker, a declared
value, a member declared on Array) that escaped before and are rejected now; the corpus project
lists no marker files.

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

- A hatch was counted only when called by its imported name: `r.trust(x)`, `const t = trust;
  t(x)`, `(0, trust)(x)`, `trust.call(...)`, `r.unsafe(...)`, `stop = panic` counted nothing, so
  `export const forged = r.trust<T>("no")` passed the baseline uncounted. Now one pass over the
  semantic references of every binding that may hold one (named and namespace imports,
  destructuring, dynamic `import()`, prelude names; hatch_refs.rs) classifies each use: a call,
  `.call` / `.apply` / `.bind`, a copy followed to its own uses (an exported copy counts once),
  and any other use counted (handed on, re-exported, a std namespace used whole: fail closed).
  Types (`typeof trust`) don't count. The whole program decides which imports are hatches,
  through re-exports, and counts `export { x as y } from` of one.
- With std or the Vue package linked into a workspace, `defineSanitizer` counted 0: the
  resolver calls those files the project's. Now the package.json that owns the defining file
  says what it is, and a SAFE-TS package's own files aren't counted as uses.
- vue-tsc's `<!-- @vue-ignore -->`, `<!-- @vue-skip -->` and `<!-- @vue-expect-error -->` were
  neither reported nor counted. Now `no-ts-suppression` reports ignore, skip and a reasonless
  expect-error; ignore counts with @ts-ignore, expect-error with @ts-expect-error, skip as the
  new `vueSkips`.
- A disable of an oxlint rule the preset makes an error (`no-eval`, `no-proto`, ...) passed
  silently: those rules are how SAFE-TS bans the constructs. Now one without `-- reason` is
  reported by `no-lint-suppression`. The same at config level: `.oxlintrc.json` next to the
  package.json (JSON with comments) is read, and a config not extending the preset, a preset
  rule turned off or down in "rules" or "overrides", and "ignorePatterns" are named as weakened
  settings, which the baseline ratchets.
- `declare value: string` in a class was silent while `value!: string` is a cast. Now it is
  reported by `no-unchecked-declare` and counted with the ambient declarations.
- Docs that teach a `declare` form are checked with the rule on (`declares` fence word), and
  say what the rule asks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Weakened lint settings are named by .oxlintrc.json, the file that sets them
All checks were successful
Benchmarks / bench (pull_request) Successful in 3m54s
Tests / test (pull_request) Successful in 7m48s
6577df1a9a
The check listed `oxlint.rules no-eval off (package.json)`: the setting lives in the
`.oxlintrc.json` beside it, and that is where a reader has to look.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Ambient declarations test: a neutral member name in the sample
All checks were successful
Benchmarks / bench (pull_request) Successful in 4m9s
Tests / test (pull_request) Successful in 9m13s
6e1cc600bc
Co-Authored-By: Claude Fable 5.2 <[email protected]>
sionzee changed target branch from sentinel-values to main 2026-11-21 23:27:20 +01:00
Merge main
All checks were successful
Benchmarks / bench (pull_request) Successful in 3m10s
Tests / test (pull_request) Successful in 8m54s
af956cb06e
Co-Authored-By: Claude Fable 5.2 <[email protected]>
sionzee deleted branch escape-hatches-2 2026-11-21 23:28:33 +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!119
No description provided.