midnight-saci

REST API and worker reporting protocol

The backend’s REST API is described by openapi.yaml. Every route lives under /api/v1/<problem-domain>: client routes under /api/v1/runs, worker routes under /api/v1/worker/runs. Bodies are JSON. Example payloads in api-examples/ are checked against the Rust types in saci_core::api by the test suite.

Authentication

Token comparison is constant-time. A backend whose backend.auth.token_env names an unset or empty variable refuses to start.

Errors

Errors use one body shape and stable codes:

{ "error": { "code": "validation_failed", "message": "the plan is invalid", "details": ["plan.md:5: ..."] } }
Code Status Meaning
bad_request 400 Malformed JSON, query or header.
unauthorized 401 Missing or wrong token.
forbidden 403 A worker token used for another run.
not_found 404 No such run or evidence.
idempotency_conflict 409 An Idempotency-Key reused with a different body.
run_finished 409 Cancellation or worker reporting after the run finished.
sequence_gap 409 A worker batch skipped a sequence number.
action_not_pending 409 The action is not awaiting approval: it was already decided, or never needed approval.
digest_mismatch 409 An approval, rejection or redispatch named a digest other than the recorded arguments’.
action_not_resolvable 409 A redispatch of an action that is not unknown or error, or a reconciliation of one that is not unknown.
validation_failed 422 Invalid plan bundle, unknown environment or policy; details lists every diagnostic with its file and line.
internal 500 The backend failed.

A plan validation_failed also carries the same problems as structured diagnostics, each with document, line and message (example), so a client can show them next to the source without parsing the text.

Submitting a run

POST /api/v1/runs with a SubmitRun body: the bundle (entry document plus every invoked procedure, keyed by bundle-relative path), an environment name, an optional policy name, an optional from_step unit key and an optional skip_cleanup boolean. A run with from_step includes that unit, its invoked procedures and all later units. Its report and outcome cover those units. Set skip_cleanup to true to skip all cleanup units, including after an early stop. The default is false.

The backend validates the bundle and the names, stores the run with a redacted snapshot of its effective configuration, and answers 202 Accepted with a RunSummary and a Location header. The worker launch happens afterwards, asynchronously; there is no approval stage. Invalid input is rejected with 422 before anything is stored or launched.

A body that is not valid JSON, or has fields other than bundle, environment, policy, from_step and skip_cleanup, is a 400 bad_request. An invalid plan, environment, policy or unit key is a 422 validation_failed.

Retries and idempotency. A submission is safe to retry only with an Idempotency-Key header. Repeating the request with the same key and an identical body returns the original run with 200 OK and starts nothing new; the same key with a different body fails with 409 idempotency_conflict. Without a key, a client that lost the response cannot tell whether the run was created and should list runs before resubmitting.

Listing and inspecting

Query parameters are validated: limit must be 1 to 200 (default 50), wait 0 to 60, and a cursor must come from an earlier page; anything else is a 400 bad_request. A malformed run or evidence ID is a 404.

Following progress

GET /api/v1/runs/{run_id}/events?after=<seq>&wait=<secs> returns events with seq > after in order (example). With wait, the backend holds the request until a new event exists, the run finishes, or the wait (at most 60 seconds) ends. A client keeps next_after and reconnects with it, so a dropped connection never loses or repeats an event. finished: true means no more events will be recorded, not that this page holds the rest: a finished run may still have more events than one page (limit, 50 by default). Keep reading from next_after until a page ends with run_finished or is empty.

Cancelling

POST /api/v1/runs/{run_id}/cancellation records the request (202) and the backend tells the worker to stop. Repeating it is harmless and returns the same cancel_requested_at. Cancellation is requested until the run’s lifecycle reaches finished; the outcome is then cancelled unless the run had already ended for another reason. Cancelling a finished run returns 409 run_finished.

Outcome actions

GET /api/v1/runs/{run_id}/actions lists the run’s outcome actions with their state (pending_approval, queued, executing, rejected, completed, error, unknown), the exact arguments, their digest, the idempotency key when the action has one, any decision, the attempts, and the result or error. The list is empty until the run finishes, and when no action was triggered. Delivery never changes the run’s outcome. See outcome actions.

GET /api/v1/runs/{run_id}/actions/{name} returns one action with its history: one entry per change, with at, event (recorded, approved, rejected, claimed, answered, failed, uncertain, replayed, redispatched or reconciled), the state and attempt count afterwards, and the error or note as detail.

GET /api/v1/actions?state=<state>&limit=<n>&cursor=<cursor> lists actions in one state across runs, oldest first (by creation time, then insertion). The default state is pending_approval. A page that is not the last has a next_cursor; pass it as cursor to read the next one. The order is stable while actions change state, so paging never lists an action twice in one state; an action that moves to another state while you page leaves this listing.

POST /api/v1/runs/{run_id}/actions/{name}/approval and POST /api/v1/runs/{run_id}/actions/{name}/rejection decide on an action waiting for approval. The body is {"digest": "<sha256 of the recorded arguments>", "note": "optional"}. They answer 200 with the action, 404 not_found without such an action, 409 digest_mismatch for another digest, and 409 action_not_pending when the action is not waiting for approval.

POST /api/v1/runs/{run_id}/actions/{name}/redispatch queues an unknown or error action again with its recorded arguments; the body is the same as an approval’s. POST /api/v1/runs/{run_id}/actions/{name}/reconciliation with {"state": "completed" | "error", "note": "optional"} records what a person found for an unknown action and sends nothing. Both answer 200 with the action, or 409 action_not_resolvable in another state; see retries and restarts.

Investigations

POST /api/v1/investigations with {"run_id": "...", "unit": "1.2", "sources": [{"name": "orders", "ref": "v2"}]} creates an investigation of a failed unit of a finished run and starts its worker (202). unit and sources are optional. The request accepts an Idempotency-Key. A second request for the same run and unit returns the current investigation with 200, unless it ended in error or was cancelled: then a new investigation starts. The backend answers 404 not_found when investigations are not configured or the run does not exist, and 422 validation_failed when the run has not finished, is an investigation, the unit did not fail, or a source is not configured.

GET /api/v1/investigations/{investigation_id} returns the investigation: its parent run and unit with their titles, lifecycle, outcome, repositories and result. GET /api/v1/investigations lists the investigations of all runs, newest first, with limit and cursor as for GET /api/v1/runs. GET /api/v1/runs/{run_id}/investigations lists a run’s investigations, oldest first. The investigation ID is a run ID: use the run routes for its events, evidence, report and cancellation. GET /api/v1/runs does not list investigations. See failure investigations.

The backend also serves investigations over A2A, outside /api/v1: GET /.well-known/agent-card.json and the JSON-RPC endpoint POST /a2a. They are not REST routes, so openapi.yaml does not describe them. See A2A.

Console reads

These client routes describe what a run may be submitted with and what a run executes, for clients such as the web console. They only read, and they are built from an allowlist of fields: secret values and references, environment parameters, MCP connection details, headers and action argument templates never appear.

Worker reporting protocol

A worker is started with the backend URL, its run ID and its run-scoped token. It then:

  1. Fetches its input with GET /api/v1/worker/runs/{run_id}/specification and validates it before doing anything else. The specification contains secret references only; secret values are injected into the worker’s process environment by the launcher.
  2. Reports events with POST /api/v1/worker/runs/{run_id}/events and a batch of consecutive events (example). The backend persists the batch in one transaction and only then answers with the highest persisted sequence number (EventAck). Events at or below that number are ignored, so a worker that did not receive an acknowledgment resends the same batch; deduplication guarantees each event is recorded once, not that target actions happened once. A batch that would skip a number fails with 409 sequence_gap and the acknowledged sequence in details (example); the worker resends from there. Events after run_finished are rejected with 409 run_finished.
  3. Polls control with GET /api/v1/worker/runs/{run_id}/control and stops when cancel_requested is true, reporting cancellation_requested and finally run_finished with outcome cancelled.

Evidence travels inside observation events, already redacted, so it stays readable from the backend after the worker disconnects or is removed.

Next steps