# Diagnostics

A diagnostic is one finding your command reports, such as a lint error or a config warning. You add diagnostics while the command runs, and readerly prints them once, at the end, in the shape the reader needs.

## The record

Every diagnostic is a `DiagnosticRecord` from [`src/types.ts`](https://github.com/loadsheet/readerly/blob/main/src/types.ts). It follows the nostics shape and adds a location.

| Field | Type | What it holds |
| --- | --- | --- |
| `severity` | `"error" \| "warn" \| "info"` | Required. Errors fail the run. |
| `code` | `string` | A stable rule or error code, such as `ACME001`. |
| `message` | `string` | Required. One sentence about what is wrong. |
| `why` | `string` | The reason it matters. |
| `fix` | `string` | What to change. |
| `docs` | `string` | A URL that explains the code. |
| `file` | `string` | Path of the file the finding is in. |
| `line`, `column` | `number` | Where it starts. |
| `endLine`, `endColumn` | `number` | Where it ends. Only GitHub annotations use them. |
| `sources` | `string[]` | Source locations, as nostics reports them. |
| `data` | `unknown` | Anything else. `json` and `jsonl` carry it, `text` and `agent` don't print it. |

## Adding diagnostics

`out.error`, `out.warn` and `out.info` take a message string or a record without `severity`. `out.diagnostic` takes a full record, which helps when the severity comes from your own rule table.

```ts [lint.ts]
import { createOut } from "readerly";

export async function lint(argv: string[]): Promise<number> {
  const out = createOut({ tool: "acme", command: "lint", argv });
  out.error({
    code: "ACME001",
    message: "Route handler reads process.env at module scope",
    why: "The value is fixed at build time",
    fix: "Read it inside the handler",
    docs: "https://acme.dev/rules/ACME001",
    file: "src/app.ts",
    line: 14,
    column: 3,
  });
  out.warn({ code: "ACME010", message: "Alias shadows a workspace package", file: "vite.config.ts", line: 8, column: 5 });
  out.info("Cache was cold");
  return out.end();
}
```

In `text`, `agent` and `json`, nothing prints when you call them. readerly collects the records, counts them by severity and renders them when you call `out.end()`, or earlier when a [prompt](https://readerly.loadsheet.dev/guide/prompts) needs the screen. `jsonl` writes each record as you add it. With `--quiet`, `text` drops infos and logs from the report but still counts them.

## How text prints them

A person at a terminal gets the `acme check` example from [`test/golden`](https://github.com/loadsheet/readerly/blob/main/test/golden) like this, without the color:

```text
✖ 2 errors, 1 warning in 212 files (0.8s)

error ACME001  src/app.ts:14:3  Route handler reads process.env at module scope
├▶ why: The value is fixed at build time
╰▶ fix: Read it inside the handler
error ACME004  src/db.ts:3:1  Top-level await in a server entry
╰▶ fix: Move the call into the plugin setup
warn  ACME010  vite.config.ts:8:5  Alias shadows a workspace package

next: acme explain <code>   (explains one code)
```

The first line is the verdict. Its glyph comes from the status: a red `✖` when the run failed, a yellow `⚠` for `action_required` or for errors and warnings in a run that didn't fail, and a green `✔` otherwise. A clean run prints `✔ no problems`.

Rows are sorted errors first, then warnings, then infos. Each row starts with the severity padded to five characters (`error`, `warn `, `info `), in red, yellow or cyan. Then come the code, the location as `file:line:column` and the message, separated by two spaces.

Below a row, `tree()` draws one branch per field: `why:`, `fix:`, and `see:` with the `docs` URL when you pass `--full`. The last branch gets `╰▶`, the others `├▶`. The glyphs are dim and the labels bold. See [Color and glyphs](https://readerly.loadsheet.dev/guide/color) for the full vocabulary.

## The row cap

A terminal shows at most 50 rows. Set `maxRows` in `createOut` to change it. The rows past the cap collapse into one dim line with the command that shows them all. With `maxRows: 3`, the person at the terminal gets:

```text
✖ 1 error, 4 warnings, 1 info

error ACME001  src/app.ts:14:3  Route handler reads process.env at module scope
├▶ why: The value is fixed at build time
╰▶ fix: Read it inside the handler
warn  ACME010  packages/p1/vite.config.ts:8:5  Alias shadows a workspace package
warn  ACME010  packages/p2/vite.config.ts:8:5  Alias shadows a workspace package
… 3 more (rerun: acme lint --full)
```

The cap applies only to the `terminal` reader on `text`. CI logs, pipes and `--full` get every row.

## How agent prints them

A coding agent gets each diagnostic on one line: severity, code, location, message, and the fix as a second sentence.

```text
acme check: failed. 2 errors, 1 warning in 212 files (0.8s).
error ACME001 src/app.ts:14:3 Route handler reads process.env at module scope. fix: Read it inside the handler
error ACME004 src/db.ts:3:1 Top-level await in a server entry. fix: Move the call into the plugin setup
warn ACME010 vite.config.ts:8:5 Alias shadows a workspace package
next: acme check --fix   (applies 2 safe fixes)
next: acme explain <code>   (explains one code)
```

`why:` and `docs:` join the line only with `--full`. With `--full`, the agent gets this line for the `lint` example above:

```text
error ACME001 src/app.ts:14:3 Route handler reads process.env at module scope. why: The value is fixed at build time. fix: Read it inside the handler. docs: https://acme.dev/rules/ACME001
```

There is no row cap in `agent`. The [budget](https://readerly.loadsheet.dev/guide/budget) decides how many lines fit.

## The verdict

The verdict is the counts, an optional summary and a status.

- **Counts.** `2 errors, 1 warning`. Severities with no records are left out, and a run with none says `no problems`.
- **Summary.** Text after the counts, such as `in 212 files (0.8s)`. Pass `summary: (result, counts) => string | undefined` to `createOut`, or a string to `out.end({ summary })` for one run.
- **Status.** `failed` when there is at least one error, else `ok`. Pass `out.end({ status })` to decide it yourself.

A tool that tolerates errors can end with `ok`:

```ts [lint.ts]
out.error("Unused export");
return out.end({ status: "ok", summary: "(errors tolerated)" });
```

The exit code is 0, and a coding agent gets:

```text
acme lint: ok. 1 error (errors tolerated).
error Unused export
```

In `text`, that verdict shows a yellow `⚠`, because the counts still hold an error. Only a `failed` status shows `✖`.

## Using tree on its own

`tree()` from `readerly/tree` draws the same shape for any headline and branches. It returns a string with no trailing newline and no color unless you pass a `Style`.

```ts [explain.ts]
import { tree } from "readerly/tree";

console.log(
  tree("ACME001 Route handler reads process.env", [
    ["why", "The value is fixed at build time"],
    ["fix", "Read it inside the handler"],
  ]),
);
```

It prints:

```text
ACME001 Route handler reads process.env
├▶ why: The value is fixed at build time
╰▶ fix: Read it inside the handler
```

Pass `createStyle(true)` from `readerly/color` as the third argument to make the headline and labels bold and the glyphs dim.

Next: [Budget](https://readerly.loadsheet.dev/guide/budget).

<!-- vitehub-docs:section-links -->
## In this section

- [Introduction](https://readerly.loadsheet.dev/raw/guide.md)
- [Installation](https://readerly.loadsheet.dev/raw/guide/installation.md)
- [Readers](https://readerly.loadsheet.dev/raw/guide/readers.md)
- [Coding agents](https://readerly.loadsheet.dev/raw/guide/agents.md)
- [Formats](https://readerly.loadsheet.dev/raw/guide/formats.md)
- [Shared flags](https://readerly.loadsheet.dev/raw/guide/flags.md)
- [Color and glyphs](https://readerly.loadsheet.dev/raw/guide/color.md)
- [Diagnostics](https://readerly.loadsheet.dev/raw/guide/diagnostics.md) (this page)
- [Budget](https://readerly.loadsheet.dev/raw/guide/budget.md)
- [Prompts and input](https://readerly.loadsheet.dev/raw/guide/prompts.md)
- [Failures and exit codes](https://readerly.loadsheet.dev/raw/guide/failures.md)
- [JSON and JSONL](https://readerly.loadsheet.dev/raw/guide/json.md)
- [Custom renderers](https://readerly.loadsheet.dev/raw/guide/renderers.md)
- [API](https://readerly.loadsheet.dev/raw/guide/api.md)

Next: [Budget](https://readerly.loadsheet.dev/raw/guide/budget.md)

All pages: [llms.txt](https://readerly.loadsheet.dev/llms.txt)
