# JSON output, exit codes, and schemas

Scripts can rely on four things from `osmates`: `--json` output, exit statuses, error objects, and the published schemas.

## Read the output

1. Add `--json` to the command, such as `osmates vm info dev --json`.
2. Read standard output as JSON. Messages and failures go to standard error.
3. Check the exit status before you use the result.

Most commands print one JSON value, with sorted keys and ISO 8601 dates. Two print JSON Lines, one object per line: `vm events --json` and `audit show --json`.

Three options change what the command prints:

| Command | Output |
|---|---|
| `vm exec --detach --json` | The job it started |
| `vm import --check --json` | What the import would bring in |
| `diagnostics --list --json` | The parts the archive would hold |

## Read the JSON shapes

Commands and MCP tools write the same shapes:

| What | Shape |
|---|---|
| Keys | camelCase, such as `diskSize` and `frameId`. MCP tool arguments keep their own names, such as `frame_id` |
| A choice | A string, such as `"running"` |
| A choice with values | An object with one key, its name, such as `{"volumeFull": {"path": "/Volumes/Work"}}` |
| Durations, such as `elapsed` | Whole milliseconds |
| A message | One string. Commands escape the guest's words in it, and MCP tools mark them with `<guest-data>` |
| A failure inside a result, such as a job's `failure` | The fields of the error object that command or tool prints when it fails |
| `configuration` in `vm info` | The machine's settings, without the records OSMates keeps for itself |

Guest strings stay inside their JSON values. Control and invisible characters use `\u` escapes, U+2028 and U+2029 included, so a reader such as Python's `str.splitlines` sees one line per value.

## Read the exit status

| Status | Meaning |
|---|---|
| `0` | The command succeeded |
| `1` | The command failed |
| `64` | The command line was wrong, and nothing ran |
| `65` | The input was wrong |
| `75` | The same command may work later |
| `130` | Control-C cancelled the work |

A third macOS machine started without `--queue` gives 75. An unsuitable Windows installation file gives 65.

A check that fails prints its result, then exits 1: `vm resize`, `vm sign-in`, `vm copy`, `vm wait`, `audit verify`, and `recordings export`.

### Statuses from a program

`vm exec` and `osmates run` return the program's own status, except:

| Status | Meaning |
|---|---|
| `1` | The program's status was outside 0 to 255 |
| `124` | The program ran out of time |
| `125` | The command couldn't be completed |
| `128` plus the signal number | A signal ended the program |

`vm exec --json` exits 0 once the program ends, and the program's status is in `exitCode`. A script with `set -e` can still read a failed program's output.

Windows programs can return statuses above 255. The shell sees 8 bits, and `vm exec --json` keeps the full value.

## Handle an error

With `--json`, a failed command leaves standard output empty. The last line of standard error is an `error` object:

| Field | Use |
|---|---|
| `code` | Identifies the failure, such as `machineNotRunning`. Not always present |
| `kind` | `failed`, `temporary`, or `wrongInput` |
| `message` | Show it to a person |
| `actions` | Steps that may fix it |

For `temporary`, run the command again later. For `wrongInput`, change the input first.

Each action has a `purpose` and a `destructive` flag, and either a `command` with `<placeholders>` or a `url`. Check `destructive` before you run one.

A wrong command line exits 64 with a usage error in text, even with `--json`.

A failed MCP call sets `isError`. Its structured content holds the same `error` object, and its `actions` are tool calls. To run one, use `osmates agent call <tool> '<arguments>'`.

## Validate results with schemas

The [API index](https://osmates.com/reference/api.json) lists every output schema, exit status, and error code. Schemas use JSON Schema 2020-12 and live under `https://osmates.com/schemas/0/`.

| What you need | Path |
|---|---|
| A command's output | `cli/vm/info.json` |
| A command's other output | `cli/vm/exec.detach.json` |
| A command error | `cli/error.json` |
| An MCP tool's arguments | `mcp/tools/vm_exec/input.json` |
| An MCP tool's result | `mcp/tools/vm_exec/output.json` |
| An MCP error | `mcp/error.json` |
| A network access log line | `files/egress-log.json` |
| An audit export line | `files/audit-export.json` |
| A trajectory's `entry.json` | `files/trajectory-entry.json` |
| A line of a trajectory's `events.jsonl` | `files/trajectory-events.json` |
| A trajectory export's `manifest.json` | `files/trajectory-manifest.json` |

Each schema is self-contained, and a JSON Lines schema describes one line.

Objects can gain fields. Ignore fields you don't know, and don't depend on key order. Values are never `null`.

MCP results with images have no structured content. The text holds the same information.

## Check compatibility before you update

Read the version with `osmates --version` or `osmates status --json`. The API follows the OSMates version and [Semantic Versioning](https://semver.org).

Before 1.0.0, any release can change the API, so read the release notes before you update a script. From 1.0.0:

- A breaking change needs a major version.
- New commands, options, tools, and fields arrive in minor versions.
- Patch versions don't change the API.
- A removal is marked deprecated one minor version before the next major removes it.
- What a beta adds can change before the release that follows it.

Within a major version, output can gain fields and values. Treat a value you don't know as unknown.

The promise covers commands, options, JSON output, exit statuses, error objects, MCP tools, profiles, tool arguments, and structured results. It doesn't cover help text, output without `--json`, messages on standard error, tool-result text, JSON key order, or diagnostics archives.
