Migrating with an agent
@evobug_com/safe-ts-skill packages a repeatable migration workflow in the Agent Skills format: a SKILL.md playbook, on-demand reference catalogues, and census/gate scripts that keep coding agents fixing architecture instead of patching individual diagnostics.
In short
- Install into your repo: run
npx @evobug_com/safe-ts-skillto copy it into.claude/skills/safe-ts-migrate(match your@evobug_compackage version). - Architecture first, checker as a gate: the agent drafts a target architecture document, then enforces
safe-ts check --deny-warningswith idiomatic rules alongside types, tests, and behavior checks. - Ratcheted census:
scripts/census.mjscounts both checker findings and hand-rolled anti-patterns per directory so counts can only decrease. - Useful without an agent too: the census, pattern catalogue, and checklists are plain files you can run and follow manually.
Why a skill
An agent asked to "make safe-ts check pass" does exactly that. It converts null to undefined, wraps throwing calls in unsafe, adds Result return types, and stops. The checker accepts what is left: a loading flag beside an error = ref(''), a '' that means "no error", if chains on strings, x.tag === "Ready". On one large Vue project migrated this way, AsyncState, Enum and <Match> ended up used nowhere, and 24 hand-made loading flags survived.
The skill makes the target architecture the goal (Result for failures, Option for absence, useTask for async work, Enum and match for closed sets, validated boundaries, no module state) and the checker one gate among types, lint, tests, the build, an architecture gate and behaviour checks.
Install
sh
npx @evobug_com/[email protected] # into .claude/skills/safe-ts-migrate
npx @evobug_com/safe-ts-skill --to <dir> # into <dir>/safe-ts-migrate, for agents that look elsewhere- Claude Code loads a skill from
.claude/skills/when a request matches its description: adopting SAFE-TS, migrating an area, removing// @safe-ts legacymarkers, taking a census, reviewing a migration change. - Commit the folder, so everyone working on the project (and CI, for the census) has the same copy.
- The skill describes one version's APIs and names it in
SKILL.md. After upgrading the packages, install the matching version with--force(it refuses to replace a copy that differs, so local edits aren't lost by accident). The whole upgrade is below. - Its scripts pass oxlint with the SAFE-TS preset, so a project that lints every file needn't exclude
.claude/. - An agent without skill support can still follow it: point it at
SKILL.mdfrom yourAGENTS.md.
The same files are in the repository, under skills/safe-ts-migrate.
What the agent does
- Reads the project: package manager, framework, build, tests, tsconfig aliases, the HTTP layer, the state library.
- Takes a census before anything is marked legacy.
- Writes the target architecture document with the census as evidence, and asks you to approve it: the absence policy (where
nullfrom the server becomesundefined, which request fields clear withnull), errors and requests, state, async work, closed sets, boundaries, tooling, the escape-hatch budget, sanctioned helpers, exceptions, and the areas in order. This is the one point where it asks. - Sets up the packages, both tsconfig flags, the oxlint preset, the Vite plugin (or the Bun plugin), the Node loader, the idiomatic rules,
requestFunctions, the gates and a CI job, and a conventions section forCLAUDE.md/AGENTS.md. Then it marks the files that don't pass yet and records the census baseline. - Migrates in order: the shared foundation (transport, requests, error types, stores), then utilities and the component library, then feature areas in parallel by dependency, then the bridges and the last markers. Each change groups the findings by cause and fixes the design behind each group (a type that can't hold the bad value, one definition of a closed set, a boundary that parses once, one owner of the state) rather than patching the reported lines, the way a Rust borrow error is fixed by changing ownership. It uses the pattern catalogue, passes every gate, runs the behaviour checks, and ends with a hand-off report that lists the causes removed and the changed signatures.
- Runs a final pass for places where
useTask,AsyncState,Enum,match,Optionandqwould replace hand-rolled code. - Reviews and hands off with the checklist and the escape-hatch counts.
What's in it
| File | Holds |
|---|---|
SKILL.md | the workflow and the rules for the whole migration |
references/architecture.md | the target architecture document, section by section, with SAFE-TS's defaults |
references/fix-the-design.md | fixing the cause of a group of findings instead of each line: the procedure, how to tell a patch from a design fix, examples |
references/census.md | running and reading the census, and using it as a gate |
references/patterns-core.md, patterns-vue.md, patterns-react.md | before and after for each common idiom: throws, requests, sentinels, string unions, boolean flags, casts and guards, null on the wire, optional keys, Patch bodies, Open server values, JSON and storage, base64, trust and LegacyNull, parseArgs, IndexedDB, the loading trio, <Match :value>, template refs, stores, props, v-html and URLs |
references/order-and-gates.md | the order of work, legacy markers as scaffolding, parallel builders, the gates per step |
references/behaviour.md | contract checks, comparing request bodies, a browser smoke list, and regressions a real migration produced |
references/tool-gaps.md | the tools' known limits in this version and their workarounds |
references/escape-hatches.md | when unsafe, panic, trust, LegacyNull, safe-ts-ignore, rule levels, void, casts and legacy markers are allowed, the reason, the budget |
references/review-checklist.md | for reviewers: correctness and architecture |
references/templates.md | tsconfig, package.json, oxlint, Vite, CI, the conventions section, the hand-off report |
references/final-pass.md | the "use every feature" pass |
scripts/census.mjs | the census and the architecture gate |
scripts/safe-ts.mjs | finds and runs the safe-ts binary in node_modules, for the census |
The catalogue's examples are checked with the rest of the documentation: each "before" must produce exactly the rules it names (none, for the patterns the checker doesn't see), and each "after" must pass safe-ts check and compile with tsc in strict mode.
The census
sh
node .claude/skills/safe-ts-migrate/scripts/census.mjs # the report
node .claude/skills/safe-ts-migrate/scripts/census.mjs --show trio-files
node .claude/skills/safe-ts-migrate/scripts/census.mjs --json > census.jsonIt runs safe-ts check <dirs> --json --idiomatic once over the directories it scans (every source file in them is an entry, so tests and scripts nothing imports count too; one run, so the rules that decide from the whole program see all of it and the counts equal that command's), and groups the findings, errors and warnings, by rule and directory, with the root causes. The idiomatic rules report several hand-rolled forms on their own: raw .tag reads, if (isErr(x)) return x, 'key' in on a union, unwrapOr(x, undefined), JsonNull as app state, casts in tests. Then it searches the source for what no rule reports: loading, error and data states, '', -1 and 0 sentinels, string comparisons in if chains, sets of boolean flags, raw fetch and HTTP clients, try/catch and .catch, as casts, JSON.parse and direct storage, and the escape hatches (unsafe without a reason comment, trust, LegacyNull, safe-ts-ignore comments, legacy markers). Last, how often the code uses each SAFE-TS feature. The patterns are regular expressions, so they are a map of where to look, not findings. Renames aren't counted as casts (a as b in an import or export list, on one line or several, and a mapped type's [K in Keys as NewKey]), nor is English in a string, a template or a comment ("rejected as DUPLICATE_SEQ"); a fetch the file imports by name (a feature pack's) isn't a raw fetch; a top-level let in a test file isn't module state, a named sentinel (const NONE = -1) counts once, at its declaration, and the skill's own folder isn't scanned.
With a baseline, the census is a gate: it fails when any file's count of a pattern or a rule rose, so the old patterns can be burned down area by area while no new ones get in.
sh
node scripts/census.mjs --write-baseline safe-ts-census.json
node scripts/census.mjs --baseline safe-ts-census.json # exit 1 if a count roseUpgrading between versions
For a project already on SAFE-TS, moving to a newer release. SKILL.md has the same steps for the agent; the CHANGELOG sections in between list the new rules (Added) and the rules that report more (Changed). <version> is the new version. Run each command over all of the project's source directories at once (apps packages scripts), so calls and imports across packages resolve.
Bump every
@evobug_compackage together, then take the census. All of them are released with one version, and each package's peer range is its own minor (^0.5.0is>=0.5.0 <0.6.0for a 0.x version), so a mix doesn't install. Changing every pin and runningnpm installstops withERESOLVEwhile the lockfile still holds the oldsafe-ts-vue, whose peer range is the oldsafe-ts-std. Either install them all in one command (npm keeps each in the field it is already in):shnpm install @evobug_com/safe-ts-std@<version> @evobug_com/safe-ts-vue@<version> \ @evobug_com/safe-ts-transform@<version> @evobug_com/oxlint-plugin-safe-ts@<version> ...or change the pins, delete the
node_modules/@evobug_com/...entries frompackage-lock.json, and runnpm install. Then run the census against the baseline: the new rules' findings show as rises, by file.Moving to 0.5 or later from an earlier version: lend every
Mut.Mut<T>is a type of its own since 0.5.0, andtscrefuses a value passed to aMutparameter withoutmut(value).npx safe-ts migrate mut <dirs> --writewrites them over the whole program (without--writeit prints the diff); review the diff and its "Needs a decision" list.Apply the new rules' fixes one rule at a time, and review each.
census.mjs --show <rule>lists a rule's places.npx safe-ts fix <rule> <dirs>prints the diff of the rule's quick fixes and--writeapplies them: read the diff, run the tests, then go to the next rule. Take most care withstrict-truthinesson values that may beundefined:if (x)on anx?: stringmeant "set and not empty", and a 0.4.0 quick fix wrotex !== '', which is true forundefined. Writex !== undefined && x !== '', or the half the code means.Audit the comparisons with
''or0whose operand can beundefined(x !== '',x === 0), from the fixes and from before.--show empty-string-sentinellists the''ones. Where the type includesundefined, the comparison also decides what happens to it.Reinstall the skill:
npx @evobug_com/safe-ts-skill@<version> --force.
When the new findings are fixed or recorded as exceptions, write the census baseline again, in a commit that names the upgrade.