# pipe/v1 — pipelines

Runs a short ordered chain of existing apished operations in one request,
so an agent doesn't need a network round-trip per step. Each step is
dispatched in-process against the exact same handler its own REST endpoint
uses — same validation, same errors, same response shape — there is no
separate "pipeline dialect" to learn.

### `POST /v1/pipe/run`

**Request**

```json
{
  "steps": [
    { "path": "/v1/encode/decode", "args": { "format": "base64", "input": "SGVsbG8=" } },
    { "path": "/v1/text/reverse", "from": { "field": "output", "as": "text" } },
    { "path": "/v1/hash/digest", "args": { "algo": "sha256" }, "from": { "field": "reversed", "as": "input" } }
  ]
}
```

- `path` is the same canonical path the module's own README documents (e.g.
  `/v1/hash/digest`) — not a made-up pipeline-only name.
- `args` is that operation's normal JSON request body. Omit it if the step
  needs nothing but the value carried over by `from`.
- `from` optionally overwrites one field of `args` with a field read out of
  the *previous* step's response. It names both ends explicitly —
  `field` (the key in the previous response) and `as` (the key to set in
  this step's `args`) — because there's no single field name every
  endpoint agrees on: `encode.decode` returns `output`, `text.reverse`
  returns `reversed`, `hash.digest` returns `digest`. A step can only carry
  over one field this way; build the rest of `args` normally.

**Response**

```json
{
  "results": [
    { "step": 0, "path": "/v1/encode/decode", "result": { "format": "base64", "output": "Hello", "encoding": "text" } },
    { "step": 1, "path": "/v1/text/reverse", "result": { "reversed": "olleH" } },
    { "step": 2, "path": "/v1/hash/digest", "result": { "algo": "sha256", "digest": "c89dbc885161e7c62a81d7f6466bb81c4c0105d60da4d82f68b01dfb436e4729" } }
  ]
}
```

`result` is the target endpoint's raw JSON body — exactly what calling it
directly would have returned.

**Error response** (stops at the first failing step; no later step runs,
no partial "successes past the failure")

```json
{ "error": { "step": 1, "path": "/v1/encode/decode", "message": "invalid base64 input" } }
```

## Semantics

- **Byte/text boundary**: nothing here re-encodes anything. A value crossing
  step boundaries is whatever JSON value the producing endpoint returned —
  usually a string. If that string is base64 text, it stays base64 text
  until a step explicitly decodes it; there's no implicit bytes-vs-text
  conversion happening behind your back.
- **Stop-on-first-error only.** There is no `continueOnError`.
- **Steps are not individually rate-limited or recorded in call stats.**
  Only the outer `/v1/pipe/run` call is — the same one rate-limit slot and
  one stats entry regardless of how many steps it contains. Rate-limiting
  or counting each internal step would double-charge callers for using
  pipelines instead of separate requests, working against the entire point
  of the feature.
- A step can target any apished operation *except* `/v1/pipe/run` itself —
  pipe cannot call itself, so a pipeline can't recurse into another
  pipeline.
- Limit: 20 steps per request (`maxSteps` in `run.go`). This is meant for a
  short, hand-written composition, not a batch engine — ask if a real case
  needs more.

## MCP

Exposed as the `pipe_run` tool, taking the same `steps` shape as the REST
body. It's included even though an MCP client could already chain tool
calls itself turn-by-turn, for two reasons: this repo enforces 1:1 parity
between every module's REST endpoints and its MCP tools (`TestMCPToolCoverage`,
`TestScopedMCPServerCoverage` — see `modules/registry.go`'s comment on why),
and it still saves round-trips within a single MCP session the same way it
does over plain REST.

## Out of scope

Parallel step execution, `continueOnError`/partial results, and any
templating language beyond the single `from` field-carry — all deliberately
left out of this first version; ask if a real pipeline needs one of them.

## Files

- `pipe.go` — module wiring (routes, HTTP handler)
- `run.go` — the dispatch loop (`Run`), step/result/error types
- `run_test.go` — dispatch-loop tests, including the worked multi-step example
- `http_test.go` — HTTP handler tests
