# Rows from outside

## Rows from outside Legato

An agent or a script can add rows to a history whose owner says `"accepts_rows": true`. The rows go through the owner's rules, exactly as fetched ones do: its key, `fields`, `remove_after` and `date`.

### A history that only receives rows

A source of `collect` alone, with no `url` and no `kind`, keeps a history with nothing to fetch, for data that reaches the Mac some other way, such as a health export:

```json
"sources": {
  "daily": { "collect": { "dataset": "health-daily", "each": "days", "key": "{date}", "fields": ["date", "steps"],
                          "date": "date", "keep": { "for": "400d", "clock": "date" }, "accepts_rows": true,
                          "readers": ["health-steps"] } },
  "kept": { "dataset": "health-daily" }
}
```

- It needs `"accepts_rows": true`, and nothing beside `collect`.
- Cards read it through a dataset source, as here. A card that uses it directly says it "only receives rows; read them with a source of { "dataset": "health-daily" }".
- Details lists it as "Rows added from outside Legato", "Nothing fetched", "Keeps history on this Mac (health-daily), added with legato rows add".

### `legato rows`

The `legato` tool is in `/Applications/Legato.app/Contents/Helpers/`. It asks the app, which opens if it isn't running; only Legato itself opens the history's database.

```bash
legato rows add parcel-deliveries old-deliveries.json   # or from stdin
legato rows list parcel-deliveries --limit 20
legato rows delete parcel-deliveries --key ups:EX10000001 --key usps:EX20000002
legato rows delete parcel-deliveries --epoch 1
legato rows delete parcel-deliveries --all
```

**`rows add <dataset> [file] [--request-id id]`** reads a JSON array of items (or `{ "rows": [ … ] }`) shaped like the items of the owner source's answer: up to 2,000 items and 8 MB of them, in a file of up to 32 MB. Before anything is written to disk, the tool keeps only the owner's `fields`, with text removed after its markers; Legato then checks and cuts each row again itself, and dates it with the owner's `date` rule. It answers:

```json
{ "dataset": "parcel-deliveries", "accepted": 30, "rejected": 2, "expired": 0, "pruned": 0, "epoch": 1,
  "request_id": "5b0c6d1e-…" }
```

- `accepted` rows are in the history; `rejected` ones had no usable key or were over 16 KB; `expired` ones were already past retention; `pruned` rows made way for them.
- Rows land in the current history. They don't change `recording_since`, and they're listed in `coverage.imported`.
- **If Legato doesn't answer in time** (30 seconds, more while it opens), the request is withdrawn, but it may have been carried out. Run the same command again with `--request-id` and the id it named: a request that was carried out answers the same again and adds nothing. Use the id for the same operation on the same dataset within 7 days; reusing it for another dataset or operation is refused (`invalid_rows`, "Request id belongs to another history"). The original result, rejected counts included, comes back even if the owner's rules have changed since. After 7 days the id is forgotten.
- If the page's rules or history changed while the rows were being prepared, the tool prepares them again from your items, once.

**`rows list <dataset> [--limit n]`** gives up to 500 rows of the current history, the newest dated rows first, then undated ones: each with `key`, `date`, `date_kind`, `time`, `first_seen`, `last_seen` and `row`, with `epoch`, `total_count`, `undated_count` and `truncated`.

**`rows delete <dataset>`** takes one of:
- `--key k`, one or more: those rows of the current history;
- `--epoch n`: an earlier history, whole, or the current one's rows (the history itself stays);
- `--all`: every row. Earlier histories are removed and the current one stays, empty. The dataset, its owner and its readers stay.

Given more than one, `--all` wins, then `--epoch`.

It answers `{ "dataset", "deleted" }`. Deleting can't be undone, and the page goes on collecting. If the app commits a deletion but cannot publish its answer, retrying that same queued request returns its original count; it does not delete rows collected afterward.

### Over MCP

An agent connected with `legato mcp` has the same three as tools: `add_rows` (`dataset`, `rows`, and `request_id` to run again after a timeout), `list_rows` (`dataset`, `limit` up to 500) and `delete_rows` (`dataset` and one of `keys`, `epoch` or `all`). They answer as the commands do.

`get_history` (or `legato history`) says what Settings → History says about every history: its status, whether it needs you, why, and what to do in Settings. Use it when a history page is empty. It never returns rows. See [Lists and History](https://legatobar.com/docs/mcp-tools.md#lists-and-history).

### Refusals

A refusal is `{ "error": { "code", "message", "details" } }`, with exit status 1. `details` can hold the `request_id` to run again with, and `"retryable": "true"` when trying again may work:

| `code` | Message, or cause |
| --- | --- |
| `not_found` | "No page keeps the history parcel-deliveries. Its page declares it with collect." For `list` and `delete`: "This Mac keeps no history named parcel-deliveries." |
| `rows_not_accepted` | "The page that keeps parcel-deliveries doesn't take rows from outside: its collect needs "accepts_rows": true." |
| `claimed` | "Another page keeps parcel-deliveries; Settings → History can transfer it." |
| `stale` | "The page's history changed while the rows were prepared; prepare them again." Retryable; the tool tries once more by itself. |
| `paused` | A changed secret or key needs a choice in Settings; a migration in progress asks to try again shortly (retryable); a failed migration asks to resolve it in Settings |
| `storage_full` | "History storage is full. Delete old history in Settings → History, then try again." |
| `too_large` | More than 2,000 items or 8 MB in one request: "Batch exceeds 2,000 items", "Batch exceeds 8 MB", "At most 8 MB of rows at a time." or "That's more than one request can carry: send at most 2000 rows and 8 MB at a time."; from stdin, "The rows file is larger than 32 MB." |
| `invalid_rows` | "Rows are a JSON array of objects, like the items the page's source answers with." (or "Rows are a JSON array of objects."), "Batch alone exceeds dataset capacity" for a batch larger than a history can hold, or "Request id belongs to another history" |
| `invalid_arguments` | "Name the history by its dataset, like parcel-deliveries."; a request id that isn't 1–64 letters, digits and `-`; "delete takes --key k (one or more), --epoch n or --all."; a file named on the command line that can't be read or is over 32 MB: "Couldn't read old-deliveries.json, or it's larger than 32 MB." |
| `not_answered` | For `add`: "Legato didn't answer in time, so whether the rows were added is unknown. Run it again with request id … to find out: if they were, nothing is added twice." For `delete`: "Legato didn't answer in time, so whether the rows were deleted is unknown. Inspect the history before making another delete request." For `list`: "Legato didn't answer in time. Try again." |
| `failed` | "History couldn't do that." |
| `write_failed` | The request couldn't be written to the Pages folder |

