Skip to content

fix(check): a failed package leaves no shared TypeScript state behind - #32

Merged
lucasbraum merged 2 commits into
mainfrom
fix/isolate-analysis-state
Oct 6, 2026
Merged

lucasbraum merged 2 commits into
mainfrom
fix/isolate-analysis-state

Conversation

@lucasbraum

@lucasbraum lucasbraum commented Oct 6, 2026 •

Copy link
Copy Markdown
Collaborator

Follow-up to #31.

Problem

In the regression run for #31, main checked @types/react right after @types/node overflowed the stack in the same process. One @types/react change came out with a signature that began with Node's assert types (AssertOptions & { strict?: …). Run alone, @types/react was correct.

TypeScript's printer clears its writer only when a print finishes. A print that throws, a stack overflow included, leaves its partial text in front of every later print. Uptide shares one printer across all packages. Reproduced in isolation: after a print of Partial<X> fails on X, printing Logger returns "Partial<Logger".

Fix

  • serialize.ts: a print that throws replaces the shared printer before rethrowing, and the transformation result is always disposed. This fixes the exact corruption seen, wherever a print fails.

  • shared-state.ts: a small registry. Each module registers how to discard what it shares across packages:

    • the printer;
    • the consumer repository's program and checker (repo.ts; the per-repository WeakMaps in compile.ts and usages.ts go with it);
    • cached detailed surfaces (usages.ts);
    • the zod pack's parsed files.

    check calls resetSharedState() after any per-package failure, before the next package. A stack overflow can leave a checker mid-resolution too, not just the printer, so everything is discarded.

I chose the reset over a fresh worker per package because of cost (below).

fix too

fix takes one package per run; there is no fix --group (--group exists on check only). But one process can still run several packages: the Action fixes each detected upgrade in turn, then checks the next. Two boundaries now reset:

  • fix() itself discards shared state when it fails, before its caller goes on.
  • A failed assisted attempt. fix catches it and retries; the state is discarded before the next attempt or site.

Guard against new caches

shared-state.test.ts scans packages/core/src and fails on any module-level new Map, new WeakMap, ts-morph new Project or createPrinter(, unless the file registers with onReset or the line is marked // shared-state: not TS-dependent. A planted cache (const programs = new Map<string, ts.Program>()) is reported by file and line. Current state:

  • Now registered: compile.ts's three WeakMaps of programs and diagnostics, and usages.ts's file index. They were keyed by the repository a reset discards; they're now dropped explicitly.
  • Marked: the workspace-glob cache and the package.json cache, which hold no TypeScript state.

Tests

  • serialize.test.ts: a print that fails halfway leaves the next print clean. On main it returns Partial<Logger.
  • check.test.ts: widget fails mid-print before logkit, with concurrency 1 as the CLI's workers run. logkit's report must be byte-identical to check on logkit alone, ignoring timings and temp paths. On main it differs: logkit's before signature starts with Partial<.
  • shared-state.test.ts: after a reset, the consumer repository gets a fresh program and checker, with the same files.
  • fix, two tests that fail without the wiring (no reset happens):
    • a fix that fails inside the printer, followed in the same process by a check of logkit that is byte-identical to logkit checked alone;
    • an agent attempt that fails inside the printer: the state is reset, and the retry migrates the site.

Time cost

Cost
No failure none measurable. zod 3.25.76 → 4.6.5 declaration diff (68,483 changes), 4 runs each, interleaved: 54.6–60.3 s on this branch, 56.0–66.6 s on main, identical results
After a failure one reload of the consumer project: ~4.0 s on documenso's packages/lib (1,251 source files; first load 5.6 s), ~0.1 s on storefront
Fresh worker per package (not chosen) a full load for every package, 5.6 s each on documenso

Tests: core 840 passed, cli 322 passed; typecheck and biome clean. Changeset: patch.

🤖 Generated with Claude Code

lucasbraum and others added 2 commits October 5, 2026 22:54
On main, @types/react checked after @types/node's stack overflow in the same
process got a signature beginning with Node's assert types: TypeScript's
printer clears its writer only when a print finishes, so the overflowing
print left its partial text in front of every later print.

- serialize.ts: a print that throws replaces the printer before rethrowing,
  and the transformation result is always disposed.
- shared-state.ts: modules register how to discard what they share across
  packages (the printer, the consumer repository's program and checker, the
  detailed surfaces, the zod pack's parsed files); check calls
  resetSharedState() after any per-package failure, before the next package.

Tests: a print that fails halfway leaves the next print clean; with widget
failing mid-print before logkit (concurrency 1, as the CLI's workers run),
logkit's report is byte-identical to checking logkit alone (on main it began
with 'Partial<'); a reset gives the next package a fresh program.

Cost: nothing without a failure (zod 3 -> 4 declaration diff, four runs each:
54.6-60.3 s on this branch, 56.0-66.6 s on main, identical results). After a
failure, one reload of the consumer project: about 4 s on documenso's
packages/lib (1,251 files), 0.1 s on storefront. A fresh worker per package
would pay a full load (5.6 s there) for every package.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Lucas Braum <[email protected]>
…against unregistered caches

fix takes one package per run (there is no fix --group), but a process can
still run several: the Action fixes each detected upgrade in turn, then
checks the next. fix() now discards shared TypeScript state when it fails,
before its caller goes on, and so does a failed assisted attempt (which fix
catches and retries) before the next attempt or site.

Tests, which fail without the wiring (no reset happens): a fix that fails
inside TypeScript's printer, followed in the same process by a check of
logkit that is byte-identical to logkit checked alone; an agent attempt that
fails inside the printer, after which the state is reset and the retry
migrates the site.

shared-state.test.ts scans packages/core/src: a module-level new Map,
new WeakMap, ts-morph new Project or createPrinter( fails the test unless its
file registers with onReset or the line is marked
'// shared-state: not TS-dependent'. compile.ts's three program/diagnostic
WeakMaps and usages.ts's file index now register (they were keyed by a
repository a reset discards, now dropped explicitly); the workspace-glob and
package.json caches are marked.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Lucas Braum <[email protected]>
@lucasbraum
lucasbraum merged commit 36871e6 into main Oct 6, 2026
5 checks passed
@lucasbraum
lucasbraum deleted the fix/isolate-analysis-state branch October 6, 2026 06:21
lucasbraum added a commit that referenced this pull request Oct 7, 2026
…t a default's ` = ` (#44)

## What and why

**Fixes:** `check` (and `diffPackage`) died with `Manipulation error: A
syntax error was inserted.` on `i18next` 23 → 26 and `type-fest` 4 → 5,
so those upgrades could not be analyzed at all. It was found while
ranking `packs/queue.json` (#43). No issue was filed.

**Root cause.** Both packages contain the same construct: a type alias
that changed between versions and whose type parameters have defaults.
The compat program in `packages/core/src/adapters/typescript/compat.ts`
declares each changed alias's old and new body as `type __a_N<params> =
body` and adds it with `addStatements`. `bodyFor` (case `'type'`) took
the body from the first `' = '` in the signature. With a default, that
`' = '` sits inside the type-parameter list, so the generated alias
repeated the tail of its own parameter list:

- type-fest: `type __a_1<Array_ extends readonly unknown[], Start
extends number = never, End extends number = never> = never, End extends
number = never> = ...`
- i18next: `type __a_0<TOptions = object> = object> =
i18nextMod.BackendModule<TOptions>;`

ts-morph throws only when the broken alias's body contains a template
literal type: type-fest's `CamelCase<Type, Options extends
CamelCaseOptions = {}>`, i18next's `TFunctionReturn<…, ActualNS extends
Namespace = NsByTOptions<Ns, TOpt>, ActualKey = KeyWithContext<Key,
TOpt>>`. Without one, the broken alias didn't crash. It compiled to an
error type and its comparison was silently lost. That second effect is
fixed too.

The recursion cut from #31 and the shared-state reset
from #32 are not involved; the mis-split predates both.

**Fix** (`compat.ts` only, no try/catch around it): a new `aliasBody()`
finds the `>` that closes the type-parameter list and takes the alias's
own `' = '` after it. The bracket-depth scan is shared with
`headerTypeParams` and `countParams`, and it now ignores the `>` of `=>`
and text inside string and template literals. The old scanners
miscounted a `=>` inside a parameter's constraint or default.

## How it was verified

- Reproduced with `diffPackage` before the fix: `type-fest ERROR
Manipulation error: A syntax error was inserted.` (at `index.d.ts:233`)
and `i18next ERROR …` (at `index.d.mts:64`). After: type-fest 4.41.0 →
5.10.0 gives 194 changes, and i18next 23.16.8 → 26.4.2 gives 115.
- Two regression fixtures, no network, following the `recursion.test.ts`
/ `fixtures/deps/*-vN` convention. Both fail on main's `compat.ts` with
the manipulation error and pass now:
- `fixtures/deps/slice-v1`/`-v2` model type-fest: a re-exporting entry,
`CamelCase<Type, Options extends CamelCaseOptions = {}>` over a template
literal, and `Slice<Array_, Start extends number = never, End extends
number = never>`. Result: `type breaking CamelCase` and `widened
additive Slice`.
- `fixtures/deps/backend-v1`/`-v2` model i18next: aliases over a
namespace import, `BackendModule<TOptions = object>` → `<Options =
object>`, and a `Lookup<…>` with defaulted parameters over a template
literal. Result: `type breaking Lookup`. The checker finds
`BackendModule`'s parameter rename equivalent.
- `pnpm lint` (601 files, no fixes), `pnpm typecheck` (3/3), `pnpm test`
(uptide 345/345; core 876 passed, 15 skipped, the existing env-gated
ones).
- Pack tests are unchanged. Each line is predicted/expected/false
positives/false negatives, identical to main:

  | Pack | Repository | Result |
  | --- | --- | --- |
  | zod (`PASS zod: verified`) | storefront | 38/38/0/0 |
  | | AwesomeIntune | 12/15/0/3 |
  | | PATH | 39/51/0/12 |
  | | band-sdk | 29/37/0/8 |
  | stripe (`PASS stripe: verified`) | storefront | 4/4/0/0 |
  | | unkey | 13/15/0/2 |
  | | saas-starter | 1/1/0/0 |
  | ai (`PASS ai: verified`) | chatbot | 27/27/0/0 |
  | | morphic | 20/34/0/14 |

## Checklist

- [x] Tests for the change (a bug fix starts with a failing test):
`alias-defaults.test.ts`, both cases failing before the fix
- [x] `pnpm lint && pnpm typecheck && pnpm test` pass
- [x] README and docs updated if behavior changed: no doc describes this
internal step
- [x] Changeset added (`.changeset/alias-default-params.md`, patch)
- [x] No new dependency
- [x] No private code, names or paths in fixtures, tests or docs: the
fixtures are minimal models, not copies of the packages
- [x] The rules in CONTRIBUTING.md ("Rules that do not bend") still hold

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01B2T8GmEVnZwfNrugajEMqi

---
_Generated by [Claude
Code](https://claude.ai/code/session_01B2T8GmEVnZwfNrugajEMqi)_

Signed-off-by: Claude <[email protected]>
Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant