codemod/host. The workflow’s body, function steps and forEachFile callbacks then run in your process, so you can:
- set breakpoints in them and step through them,
- measure their coverage,
- mock the modules they import,
- start them from your own scripts and tools.
codemod/testing uses this host for TypeScript workflows. See Testing workflows.
Run a workflow
codemod/host ships with the codemod package:
scripts/migrate.ts
codemod.yaml (pick one by name with workflowName), or is workflow.ts.
runWorkflow resolves for every outcome. Check report.status: "succeeded", "failed", "canceled", "paused", "rejected", "awaiting-approval" or "stopped". It rejects only when the workflow cannot be loaded.
Options
Import workflows yourself
Workflows importcodemod/workflow, codemod/ast-grep, codemod/runtime and codemod/llm. These are exports of the codemod package, so Node.js, Bun, Vite and Vitest resolve them like any other import, once the package is installed:
codemod:workflow, codemod:ast-grep, and so on. The CLI still accepts that spelling. Node.js and Bun need the resolver for it: codemod/host and codemod/testing register it themselves, and anywhere else, import codemod/register first. With Vite and Vitest, add the codemod/vite plugin instead:
vitest.config.ts
runWorkflow, with path set to the workflow file so relative task paths resolve:
runWorkflow(path) applies on Node.js as it loads the file. That is fine for most workflows, with two differences: imported forEachFile callbacks are loaded with the workflow and run as plain functions (a step is then identified by the function’s text, not by its file and the files it imports), and shard’s method.function and filter fail with “was not extracted by the build”, as the engine runs them from the build’s modules. For workflows that give shard functions, use runWorkflow(path) on Node.js, or the CLI. Bun loads workflow files without the build’s changes too, even through runWorkflow(path).
How it differs from the CLI
Both hosts run the same orchestrator:step(), replay on resume, until and given work the same way. Tasks the engine owns run in the engine on both: astGrep, shell, shard (with its function and filter, in the engine’s isolates), codemod, agent and the git steps, and so do selecting files for forEachFile and writing what callbacks change. forEachFile callbacks, inline or imported, run in Node.js; codemod/host applies the build’s changes to the workflow file as it loads it, when it loads it by path on Node.js (not for a module you imported yourself, nor on Bun: see Import workflows yourself).
A workflow file is checked as the CLI checks it before it runs: a callback that assigns to an outer variable, fs in the workflow body, and the other things the CLI refuses are errors here too, so a workflow that passes its tests also runs with the CLI. codemod/runtime logs and failures behave the same in steps and callbacks on both.
On the Node.js host:
- Pure callbacks are checked as in the CLI. While the workflow’s code runs, in the body or a callback,
fetch,XMLHttpRequest,WebSocket, timers (also fromnode:timers),child_process,fswrites (writeFile,mkdir,rm,rename,openfor writing, …) and settingprocess.envthrow the CLI’s errors, and so does importing a built-in module the CLI gives no workflow code (node:http,node:net,node:worker_threads, …), statically or withimport(). Callbacks see an emptyprocess.envbut keepprocess.cwd()andprocess.argv. Reading files is not limited to the target here. Code outside the run, such as your test, is not affected. These checks are not a sandbox: references to host APIs saved beforecodemod/hostloaded can escape them. - Semantic analysis (
semanticAnalysis) is not available to callbacks on this host:definition()andreferences()find nothing. Run the workflow with the CLI for it. - The workflow body’s clock and randomness are replayed, as in the CLI. In the body,
Date,performance.now(),Math.random()and the globalcrypto.getRandomValues()andcrypto.randomUUID()start from the run’s start time and seed, and give the same answers when the run resumes; in callbacks they are real.node:cryptoimported in the body is not seeded. codemod/llmis not available yet.codemod(...)steps run local packages only; run workflows that use registry packages with the CLI.definition()andreferences()find nothing in roots thatforEachFilecallbacks parse, as in the CLI.- Callbacks run one at a time, in this process. The CLI runs
forEachFilecallbacks on several threads, so a callback that relies on running after another file’s callback works here and not there; return values instead. - A step’s
timeoutcannot stop JavaScript that is running. When a function step or aforEachFilecallback runs out of time here, the step fails and its file changes are not written, but its code runs on until it returns; code that never waits is not interrupted at all. The CLI stops them. Tasks such asshellare stopped on both. - Events are sent with the CLI.
waitForEventworks here for runs kept withstore: "local"; send to them withcodemod workflow send. - The workflow body may wait on a promise no step will settle, which the CLI stops.
- A run is resumed where it started. Runs started here are resumed with
runWorkflow(..., { resume }), and runs the CLI started withcodemod workflow resume: the two record steps differently. jssgTransformdoes not run here, not even in a 1.x transform an upgraded workflow runs withjssgFilefromcodemod/compat: that transform fails. Other transformsjssgFileruns work as in the CLI. Run the workflow with the CLI for it.- YAML workflows do not run here. Run them with the CLI, or convert them with
codemod workflow upgrade.