# JSON output

> The --json contract of the workroom CLI, with its result envelope, streaming events, exit codes and error kinds.

Add `--json` to a `workroom` command to get output a program can read. The macOS app drives the CLI this way. You can too.

## What you get

With `--json`, a command prints exactly one JSON object on stdout, then exits with a [stable exit code](#exit-codes). Progress from setup and teardown scripts, and from a clone, streams on stderr as one JSON object per line (NDJSON), so stdout stays a single object. Errors are an object too.

In `--json` mode a command never prompts. `workroom delete` needs `--confirm NAME`, and anything that would ask a question fails instead.

Commands that run in `--json` mode: `create`, `list` (with `--warnings none|fast|full`), `delete`, `version`. `base set`, `base clear`, `trust`, `trust status` and `untrust` run in `--json` mode too. The app also uses hidden commands, such as `add-project` and `delete-project`, which [Internal APIs](/docs/reference/internal-apis.md#hidden-commands) describes.

## The result envelope

A successful command prints `ok: true`, the schema and CLI versions, the command name, and the command's own fields:

```json
{ "ok": true, "schema_version": 1, "cli_version": "v3.0.0",
  "command": "create", "name": "swift-meadow", "path": "/Users/you/workrooms/swift-meadow",
  "vcs": "git", "project": "/Users/you/dev/myapp" }
```

A failed command prints `ok: false` and an `error` object. `kind` is the stable machine code and `message` is for people, so branch on `kind`, never on `message`:

```json
{ "ok": false, "schema_version": 1, "cli_version": "v3.0.0",
  "command": "create", "error": { "kind": "WorkspaceExists", "message": "git worktree already exists: …" } }
```

`command` is left out when the failure happened before a command was known.

The schema is versioned with `schema_version`, currently `1`. Adding a field is not a change of version, so ignore keys you don't know. Only a breaking change bumps it.

### Fields by command

- `create` returns `name`, `path`, `vcs` and `project`. It adds a `warning` string when the new workroom starts from out-of-date refs or from a fallback base.
- `delete` returns `name`.
- `list` returns `projects`, `workrooms_dir` and `config_path`. Each project has `path`, `vcs` and `workrooms`, and each workroom has `name`, `path`, `vcs_name` and `warnings`. A workroom or project on another host also has a `host` object. `list` adds `base_branch` at the top level for the global default, and to each project that sets its own. Without a setting the field is absent. Each project also has `has_include`, true when its root has a `.worktreeinclude`. It is absent with `--warnings none`, which reads only the config.
- `base set` and `base clear` return `{"project": …, "base_branch": …}`, or `{"global": true, "base_branch": …}` with `--global`.

The `warnings` on a workroom have a `kind` and a `message`. The kinds are `DirectoryMissing` and `VCSWorkroomMissing`. There is a third, `HostDestroyed`, for a remote workroom whose host its provider destroyed. A remote workroom never gets the first two.

### A create that fails after the workroom exists

Creating a workroom is not transactional. When the setup script fails, the workroom already exists, so the error envelope carries a `created` object with its `name`, `path`, `vcs` and `project`:

```json
{ "ok": false, "schema_version": 1, "cli_version": "v3.0.0", "command": "create",
  "created": { "name": "calm-river", "path": "/Users/you/workrooms/calm-river",
               "project": "/Users/you/dev/myapp", "vcs": "git" },
  "error": { "kind": "SetupScriptFailed", "message": "setup script failed: … returned a non-zero exit code" } }
```

## Streaming events on stderr

Each line on stderr is one JSON object with a `type`. Read it line by line while the command runs.

A `log` event is one line of a script's output, or of git's output while `add-project --clone` clones. Before it clones, `add-project --clone` also logs `Removing staging a crashed clone left: <name>` for each leftover it removes. `phase` is `setup`, `teardown` or `clone`. Each time git redraws a progress counter in place, the new value arrives as an event of its own:

```json
{ "type": "log", "phase": "setup", "text": "npm install…" }
```

`create --json` also prints a `created` event the moment the workroom exists, before the setup script runs. `setup` says whether there is a setup script to run:

```json
{ "type": "created", "name": "swift-meadow", "path": "/Users/you/workrooms/swift-meadow", "project": "/Users/you/dev/myapp", "vcs": "git", "setup": true }
```

### Step events

`create --json` also reports its steps, in order: `fetch` (fetch the base branch), `worktree` (`git worktree add`), `changes` (copy your main checkout's uncommitted changes, only with `--start local-with-changes`), `include` (copy the files listed in `.worktreeinclude`, only when the project has that file) and `script` (the setup script). `state` is `running`, `done`, `warning`, `skipped` or `failed`. `detail` carries the warning text, the reason a step was skipped, or the failure message, and is left out when there is none:

```json
{ "type": "step", "id": "fetch", "state": "warning", "detail": "Could not fetch origin. …" }
```

Each `log` event then carries a `step`, the step that last started. It is left out before any step has started. The `fetch` and `worktree` logs hold each `$ git …` command line and its output. The `created` event gains a `warning` field, which is an empty string when there is no warning.

## Exit codes

Branch on the exit code when you don't need the details.

| Code | Meaning |
| ---- | ------- |
| `0` | Success. |
| `1` | Internal error, or a failed `git` command. Any error Workroom doesn't recognise lands here, including an invalid `--warnings` value. |
| `2` | Usage or validation, such as a `--confirm` value that doesn't match. |
| `3` | A precondition or not-found: unsupported VCS, already exists, invalid name, run from inside a workroom. |
| `4` | Cancelled, or nothing to do. |
| `5` | A setup or teardown script failed. |
| `6` | The config file could not be read, written or parsed. |
| `7` | A terminal API command could reach no daemon it can use: none answers, or the one that does is too old for the command. |
| `8` | A terminal API wait timed out. |

`workroom trust status` has its own answers: `0` when every script is approved or ignored by git, `1` when one is not, and `2` when it can't tell.

## Error kinds

`error.kind` is one of these. The tables are generated from the CLI's own registry of kinds, so every kind a command can end with is here.

<!-- The kinds every command may end with: generated from the kind registry by `make docs-cli`. Don't edit by hand. -->

| `kind` | Exit | Meaning |
| ------ | ---- | ------- |
| `InWorkroom` | 3 | The command ran from inside an existing workroom. |
| `UnsupportedVCS` | 3 | No Git repository was found. |
| `NotADirectory` | 3 | The path exists but is not a directory. |
| `InvalidName` | 3 | The workroom name failed validation. |
| `DirExists` | 3 | The target directory already exists. |
| `WorkspaceExists` | 3 | The Git worktree already exists. |
| `WorkspaceNotFound` | 3 | The Git worktree to delete doesn't exist, or the workroom's folder isn't a worktree Git registers for the project, so its teardown script won't run there. |
| `ConfirmationMismatch` | 2 | `--confirm` didn't match the workroom's name. |
| `UnsafeDeletePath` | 2 | Workroom refused to delete an unsafe or reserved path. |
| `Cancelled` | 4 | The user aborted, or there was nothing to do. |
| `SetupScriptFailed` | 5 | The setup script returned non-zero. |
| `TeardownScriptFailed` | 5 | The teardown script returned non-zero. |
| `ConfigReadFailed` | 6 | The config file could not be read or parsed. |
| `ConfigWriteFailed` | 6 | The config file could not be written. After `add-project --clone`, the message says the clone is at the path but not registered; the clone is kept. |
| `VCSCommandFailed` | 1 | An underlying `git` command failed. |
| `RemoteProjectUnsupported` | 3 | `add-project` was given a remote path such as `host:path` or `ssh://…`. |
| `RemoteWorkroomUnsupported` | 3 | `delete` or `delete-project` was asked to act on a remote workroom. |
| `ProjectNotFound` | 3 | The project is not registered. |
| `WorkroomNotFound` | 3 | The workroom is not registered. |
| `InvalidHostDescriptor` | 2 | A host descriptor must be a JSON object. |
| `WorkroomExists` | 3 | A workroom of that name is already registered. |
| `BaseBranchNotFound` | 3 | A base branch exists neither on its remote nor locally, or the fetch that checks the remote failed. |
| `ScriptNotTrusted` | 3 | A setup or teardown script this Mac has not approved. |
| `LocalWorkLeftOut` | 3 | `create --start ask` found unpushed commits on your local base branch, or uncommitted changes on it, that the new workroom would leave out. Nothing was created. The envelope's `local_work` carries `branch`, `remote`, `ahead` (the commits not pushed), `behind` (the remote's commits the local branch lacks), `uncommitted`, `summary` and `behind_note` (a sentence saying it is behind, or `""`); create again with `--start origin`, `local` or `local-with-changes`. `trust status` reports the same `local_work` when there is some. |
| `StartCommitUnusable` | 3 | A create answering an earlier one's question was given a `start_commit` that is no longer where it starts, as when the project's base branch changed in between. Nothing was created; create again without it. |
| `CloneFailed` | 1 | `add-project --clone` could not clone the repository. The message carries git's own error, such as `Permission denied (publickey).`; git never prompts, so missing credentials fail here. Nothing was registered. |
| `DestinationNotEmpty` | 3 | `add-project --clone` was given a folder that already has files in it, or the folder gained files while the clone ran. No clone is left behind. Hidden staging that a crashed clone on this machine left is removed first and doesn't count. When only clone staging (`.clone-*` folders and `.clone-*.lock` files) is in the way, the message names it. |
| `MacOnly` | 3 | A command that reads or writes the Mac's Workroom config, such as `create` or `list`, ran somewhere other than a Mac, such as a remote host. |
| `ManagedInstall` | 3 | `update` ran in an install something else keeps up to date: the copy inside the Workroom app, the daemon the app installed on a host, or the daemon's own copy that its terminals run. Update the app instead; a host gets each newer build when the app connects. |
| `NeedsTheUser` | 3 | `trust`, `delete` or `delete-project` ran under a coding agent, such as Claude Code or Codex: approving a script and deleting are the user's to do. Nothing was approved or deleted. The message says to ask the user to do it in the Workroom app, or to run the command in their own terminal. |
| `InternalError` | 1 | Anything else. |

<!-- End of the generated kinds. -->

### The terminal API's kinds

The commands that drive terminals (`sessions`, `capture`, `wait`, `send`, `command`, `prompt`, `events`, `new`, `close`, `run-control` and `api`) return these, and `NotADirectory` for a `new --cwd` that is no folder. A kind may carry more in `error.details`, and `TargetAmbiguous` lists the terminals it matched in `error.matches`. `workroom command` exits with the command's own code when it runs, so tell its failures from the command's by `ok` in its `--json` envelope. See [Drive terminals from agents and scripts](/docs/guides/drive-terminals-from-agents-and-scripts.md), and the [terminal API reference](/docs/reference/terminal-api.md) for the kinds each method can return.

<!-- The terminal API's kinds: generated from the kind registry by `make docs-cli`. Don't edit by hand. -->

| `kind` | Exit | Meaning |
| ------ | ---- | ------- |
| `TargetAmbiguous` | 3 | More than one terminal matches the target, so nothing was done. |
| `TargetNotFound` | 3 | No terminal matches the target. |
| `DaemonUnreachable` | 7 | No daemon answers: the command ran outside a Workroom terminal, or `WORKROOM_DAEMON` names a socket nothing listens on. |
| `DaemonTooOld` | 7 | The daemon is older than the command needs. Run `"$WORKROOM_DAEMON.bin/path/workroom"`, the one that matches it. If that says so too, the daemon needs updating, which updating the app does. |
| `WaitTimeout` | 8 | The `--timeout` passed before what was waited for happened. |
| `AgentBusy` | 3 | The coding agent is working or waiting on a question, so the prompt wasn't given. `error.details` says whether the text was typed but not submitted. |
| `NotAnAgent` | 3 | No coding agent runs in the terminal. |
| `NotARun` | 3 | `run-control` named a terminal that is no run tab. |
| `SessionEnded` | 3 | The terminal ended before or while it was asked. |
| `AlternateScreen` | 3 | A full-screen program is up in the terminal. |
| `NoShellIntegration` | 3 | The terminal's shell sends no shell integration marks, so a command's end can't be seen. |
| `ExitCodeMismatch` | 3 | `wait --run exited --exit-code N` saw the run exit with another code, which `error.details.exit_code` holds. |
| `WaitInterrupted` | 3 | A wait for output or a command's end may have missed it: the app updated the daemon in the middle, or the output came faster than the wait could read it. |
| `TooLarge` | 2 | The request, or what it asked for, is more than one request or reply carries. |
| `Busy` | 3 | The terminal or the daemon is busy, such as a shell running another command, so try again shortly. |
| `RefusedSelf` | 3 | The command named the terminal it runs in. Pass `--allow-self` to allow it. |
| `InvalidRequest` | 2 | The daemon couldn't read the request: an unknown method, or parameters it refuses. |

<!-- End of the generated kinds. -->

### The ScriptNotTrusted envelope

A `ScriptNotTrusted` error envelope has a `script` object, so a program can show the script and ask. `text` is the script to show, which is lossy for bytes that aren't UTF-8. `bytes` is its exact contents in base64, which is what to approve:

```json
{ "ok": false, "schema_version": 1, "cli_version": "v3.0.0", "command": "create",
  "error": { "kind": "ScriptNotTrusted", "message": "script is not trusted on this Mac: …" },
  "script": { "name": "workroom_setup", "origin": "root", "sha256": "0ce619c6…", "text": "#!/bin/sh\n…",
              "bytes": "IyEvYmluL3No…", "trusted": false, "exempt": false },
  "start_commit": "9b2f4905aeaf0dd0cfc829ad42745d5c1268236e" }
```

See [workroom trust](/docs/cli/workroom-trust.md) for how approval works.

### The start commit

A `create` that stops to ask something before it makes anything, a `ScriptNotTrusted` or `LocalWorkLeftOut` error, also has `start_commit`: the commit its start point resolved to after its fetch. The Workroom app passes it back when you answer, so the create continues from the commit you were asked about instead of fetching again. That create reports no `fetch` step, since the one whose question it answers already fetched.
