# Teaspoon v0 Output Contract

Status: current v0 CLI and structured output contract.

This document describes the observable command contract and the structured data
returned by the Go API for UI rendering. User interfaces should consume these
structured fields instead of parsing rendered stderr.

This CLI diagnostic contract is for local, trusted debugging. Server API
execution uses the smaller contract below; it does not copy CLI diagnostic
snapshots into HTTP responses or durable request results.

## Server API diagnostics

New API failure records contain only diagnostic code, location and description.
Runtime/decode descriptions come from a fixed code-to-message mapping rather
than the original diagnostic text, which may embed application values. No
globals, stack parameters/locals, source snippets or suggestions are persisted
in either `result_json` or `diagnostic_json`. Rollback still uses an in-memory
state snapshot; removing diagnostic capture does not remove rollback.

HTTP error envelopes omit location and all debug details, with a fixed
`Request failed.` message for ordinary runtime/decode failures. This applies
whether or not authentication is configured, and to revision-pinned routes.
Explicit TSP `error(value)` remains business output: its message and structured
value are retained. Successful results are unchanged.

Old UUID retries project saved failures to this minimal form after the usual
identity/argument checks. They do not reexecute requests or rewrite old database
rows. Historical raw diagnostics therefore remain sensitive at rest; the change
is not a scrub of old logs or backups. Upgrade checks normalize runtime/decode
descriptions on both sides while retaining error code/phase and business output
comparisons. See [revision compatibility](revision_compatibility.md).

Request arguments, explicit outputs and recorded external-call results can
still contain private data needed for recovery. Diagnostic minimization does
not make the request store public or replace its access/encryption controls.
Privileged local CLI debugging retains its detailed diagnostic contract.
Exact [historical version archival](historical_execution_versions.md) and
[offline debugging by UUID](request_replay_debugging.md) are documented future
work, not features implemented by this change.

## CLI Command

The only required v0 command is:

```bash
teaspoon run main.tsp
```

Rules:

- `say(value)` writes the value display text to stdout followed by a newline.
- `ask(prompt)` writes the prompt display text to stdout without adding a
  newline, then reads one line from stdin and trims surrounding whitespace.
- Syntax, validation, input, limit, and runtime errors are rendered to stderr.
- CLI argument errors are rendered to stderr.

Exit codes:

- `0`: success
- `1`: syntax, validation, input, limit, or runtime error
- `2`: CLI argument error

## Go API Result

`RunCLI` returns a `CLIResult` with this shape:

```json
{
  "ExitCode": 0,
  "Events": [],
  "Result": {},
  "Error": null
}
```

`Events`, `Result`, and `Error` are JSON-compatible maps and arrays. On
success, `Result` is populated and `Error` is empty. On error, `Error` is
populated. Runtime errors may also include events that happened before the
error.

## Events

Output event from `say`:

```json
{
  "type": "output",
  "value": {
    "kind": "text",
    "display": "hello"
  },
  "display": "hello"
}
```

Input request event from `ask`:

```json
{
  "type": "input_request",
  "id": "input-1",
  "prompt": {
    "kind": "text",
    "display": "Name: "
  },
  "display": "Name: "
}
```

Event values always include `kind` and `display`. List event values also include
`items`, whose entries use the same event value shape.

## Success Result

A successful run returns:

```json
{
  "status": "success",
  "globals": {},
  "stats": {
    "output_count": 0
  }
}
```

`globals` maps top-level variable names to captured values. `output_count` is
the number of `say` calls that completed.

## Structured Error

An error object contains:

```json
{
  "code": "NameError",
  "message": "Unknown name.",
  "location": {
    "file": "main.tsp",
    "line": 4,
    "column": 7
  },
  "snippet": "  say(Score)",
  "highlight": {
    "column": 7,
    "length": 5
  },
  "suggestion": "Did you mean `score`?",
  "stack": [],
  "globals": {}
}
```

Required fields are `code`, `message`, `location`, `stack`, and `globals`.
`snippet`, `highlight`, and `suggestion` appear when available.

Rendered stderr is derived from the structured error:

```text
NameError at main.tsp:4:7
  say(Score)
      ^^^^^
Unknown name.
Suggestion: Did you mean `score`?
```

## Stack Frames

Runtime errors include a structured stack. Each frame contains:

```json
{
  "function": "divide",
  "location": {
    "file": "main.tsp",
    "line": 3,
    "column": 14
  },
  "call_site": {
    "file": "main.tsp",
    "line": 8,
    "column": 7
  },
  "parameters": {},
  "locals": {}
}
```

`call_site` is `null` for `start()`. `parameters` and `locals` map names to
captured values. Recursive calls appear as separate frames.

## Captured Values

Captured values used in errors and success results contain:

```json
{
  "kind": "number",
  "display": "1/3",
  "truncated": false
}
```

The current implementation records `truncated: false`. The contract reserves
`truncated: true` and additional size or prefix fields for future payload-size
limits.

## Stability Rules

- Existing field names and meanings are stable for v0.
- New optional fields may be added.
- UIs should branch on `code`, `location`, `stack`, `globals`, and event
  `type`, not on rendered English text.
- Rendered messages default to English.
