> ## Documentation Index
> Fetch the complete documentation index at: https://docs.codemod.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Drive codemod from a program

> Run a workflow from a TUI, an agent or a script: read its events as JSON lines, answer its approvals on stdin, attach to runs started elsewhere.

A program that runs codemod for someone (a terminal UI, a coding agent, a CI wrapper) starts it as a child process with `--format jsonl --control stdio`. The run prints one JSON object per line on stdout, and reads commands, one JSON object per line, on stdin. When the workflow asks something (an `approval(...)` step, a shell command, a coding agent to pick), the run waits for the answer instead of exiting.

```bash theme={null}
npx codemod@next workflow run -w workflow.ts --target . --format jsonl --control stdio
npx codemod@next workflow resume --id <ID> --format jsonl --control stdio
npx codemod@next @org/my-package --format jsonl --control stdio
```

The TypeScript types for everything below are in the `codemod` npm package:

```ts theme={null}
import type { Command, Event, Hello, Line } from "codemod/protocol";
```

## What the run prints

The first line is `hello`. It names the run before anything happens, so a client can attach, cancel or resume it later:

```json theme={null}
{"event":"hello","protocol":1,"cli":"2.0.0","run_id":"8c7000b1-…","mode":"run","dry_run":false,
 "workflow":"/work/codemod/workflow.ts","target":"/work/app",
 "settings":{"capabilities":["fs"],"managed_git":false,"agent":null,"decision_model":"systemone/jev-latest","approve_all":false,"approved":[]},
 "commands":["answer","approve","reject","stop"]}
```

Then come the run's events, the same as `--format jsonl` prints without a control channel (see [`--format`](https://docs.codemod.com/community/cli#codemod-workflow)). Each has `seq`, which counts up through the whole run, also across resumes, and `ts`. Step events name their step with `step`, a key that stays the same across resumes (`"3"`; `"3/0"` for step 0 of the codemod that step 3 runs, whose `parent` is `"3"`).

When a workflow first uses an experimental API, an `experimental` event names it in `api`. A run emits at most one notice per API, even if it resumes. This event is part of the event stream, not the workflow result.

A question looks like this:

```json theme={null}
{"event":"question","id":"approval:1","kind":"approval","step":"1","message":"Go on to b?",
 "approval":{"kind":"manual","message":"Go on to b?"},"seq":7,"ts":"…"}
```

| `id` | Asked when |
| - | - |
| `approval:<step>` | an `approval(...)` step runs |
| `interrupted-step:<step>` | a resumed side-effect step started but has no saved outcome |
| `shell:<step>` | a `shell` step is about to run a command |
| `agent-run:<step>` | an `agent` step is about to run a coding agent; `approval` has the agent, the prompt and its autonomy |
| `http:<step>` | an `http` step is about to send a request; `approval` has the method, the URL and the environment variables its headers read |
| `pull-request:<step>` | a `pullRequest` step is about to push and open a pull request |
| `capabilities:<step>` | a step's code asks for capabilities the run was not given |
| `uncommitted-changes` | the run is about to change a target with uncommitted changes (unless `--allow-dirty`) |
| `agent:<step>` | an `agent(...)` step needs a coding agent; `kind` is `"agent"` and `choices` lists them |

Questions come one at a time. When one is answered, or closed because the run stops, `question_closed` says how: `answer` is `"approved"`, `"rejected"` or `"later"` (an agent's id, or `null`, for agent questions), and `by` is `"answer"`, `"stop"` or `"eof"`. `--approve` still gives approvals up front, without asking.

## Commands

```json theme={null}
{"command":"approve","question_id":"approval:1"}
{"command":"reject","question_id":"approval:1"}
{"command":"answer","question_id":"approval:1","value":"later"}
{"command":"answer","question_id":"agent:3","value":"codex"}
{"command":"stop","mode":"pause"}
```

* `approve` and `reject` answer an approval. `answer` takes `"approve"`, `"reject"` or `"later"` (only for manual approvals and interrupted steps) for approvals, and an available agent's id, or `null` for the built-in agent, for agent questions. `questionId` is accepted for `question_id`.
* `stop` stops the run: `"pause"` (the default) leaves it `paused`, to resume; `"cancel"` ends it as `canceled`.
* A command that cannot be followed (not JSON, not UTF-8, no such open question, a wrong answer) gets a `control_error` event with the `message` and the `line`; the run goes on, and so does reading commands.
* Closing stdin pauses the run, as `stop` does, and closes the open question. Keep stdin open for as long as the run should go on.

An interrupted-step question comes before `run_started`. Answer `"approve"` to rerun that step, `"reject"` to skip it without rejecting the run, or `"later"` to leave the run as it was and exit `3`. `--approve` does not answer these questions; use `--rerun-step` or `--skip-step` to decide up front.

## How a run ends

| Status | How | Resume |
| - | - | - |
| `succeeded` | | nothing left to run |
| `failed` | a step failed, or the workflow called `fail()` | `workflow resume` |
| `awaiting-approval` | answered `later` (or no control channel and nobody to ask) | `workflow resume --approve <id>` |
| `paused` | `stop` with `"pause"`, stdin closed, Ctrl-C | `workflow resume` (paused at a question: `workflow resume --approve <id>`) |
| `canceled` | `stop` with `"cancel"`, `workflow cancel` | over: `--force` only |
| `rejected` | an approval was rejected | over: `--force` only |

**A run paused at a question still waits for it.** When you `stop` or close stdin while a `question` is open, the run is `paused`, and `run_finished` (and `workflow status --format json`) keep that question's `question` id and `awaiting` message. The approval step is `awaiting-approval`, not `canceled`. Resume it with `workflow resume --id <ID> --approve <question id>` to give that approval, or without `--approve` to be asked again.

**A rejected approval ends the run.** Nothing after it runs, steps running alongside stop, and what earlier steps changed stays changed. The process exits with `1`, as it does for `paused` and `canceled`, and with `3` for `awaiting-approval` (see [exit codes](https://docs.codemod.com/community/cli.md#exit-codes)); `run_finished` has the status.

A dry run with a control channel waits at its questions like a real run, and `approve` lets it go on and report its diff. Without one, a dry run that reaches an approval exits with `1`: it is not saved, so it cannot wait to be resumed.

## Attach to a run

> [!WARNING]
>
> **Experimental.** This can change in a minor release of the CLI, but not in a patch. Pin the CLI version (`npx codemod@<version>`) if you build on it.

Every saved run keeps its events in the run store. `workflow attach` prints them, after a `hello` (with `"mode":"attach"` and no commands), and follows the run while another process executes it:

```bash theme={null}
npx codemod@next workflow attach --id <ID> --format jsonl [--from-seq <SEQ>]
```

It ends when the run stops executing. It only watches: the questions are answered by the process that has the control channel.

## Read runs and workflows

* `workflow list --format json` and `workflow status --id <ID> --format json` print runs; `status` has `question`, the approval a waiting run needs. If the run pauses while a question is open (for example, `stop` or closed stdin), it keeps that question.
* **Experimental:** `workflow inspect -w <file|dir> --format json` describes a workflow for a setup form: declared params with their types, doc comments and `@default`s, imports, which declared steps reach outside the target (`http`, `shell`, `agent`, ...), and its `step(...)` calls. See [`workflow inspect`](https://docs.codemod.com/community/cli#codemod-workflow).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.