openapi: 3.1.0
info:
  title: Saci API
  version: 0.2.0
  description: |
    REST API of the Saci backend. Every route lives under `/api/v1/<problem-domain>`.
    Client routes (`/api/v1/runs`) require `Authorization: Bearer <token>` when the
    backend is configured with `backend.auth`; it must be when it listens beyond
    loopback. Worker routes (`/api/v1/worker/...`) require the run-scoped worker
    token issued at launch, which grants access to that one run only.
    See docs/reference/rest-api.md for semantics and examples.
servers:
  - url: http://127.0.0.1:8080
tags:
  - name: runs
    description: Submitting, inspecting and cancelling runs.
  - name: catalog
    description: What runs may be submitted with.
  - name: investigations
    description: Failure investigations of finished runs (docs/run-tests/investigate-failures.md).
  - name: actions
    description: Outcome actions of finished runs (docs/connect/outcome-actions.md).
  - name: worker
    description: Run-scoped reporting by a worker.
  - name: health
paths:
  /api/v1/health:
    get:
      tags: [health]
      summary: Liveness check. Never requires authentication.
      operationId: getHealth
      security: []
      responses:
        "200":
          description: The backend is serving.
          content:
            application/json:
              schema:
                type: object
                required: [status]
                properties:
                  status: { type: string, const: ok }
  /api/v1/runs:
    post:
      tags: [runs]
      summary: Submit a run. Execution starts immediately; there is no approval stage.
      operationId: submitRun
      security: [{ clientToken: [] }, {}]
      parameters:
        - $ref: "#/components/parameters/IdempotencyKey"
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: "#/components/schemas/SubmitRun" }
      responses:
        "202":
          description: |
            Accepted and stored. The worker launches asynchronously; follow progress through
            `/events`. A retry with the same `Idempotency-Key` and body returns the original
            run with status 200 instead.
          headers:
            Location:
              schema: { type: string }
              description: "`/api/v1/runs/{run_id}`"
          content:
            application/json:
              schema: { $ref: "#/components/schemas/RunSummary" }
        "200":
          description: Replay of an earlier submission with the same idempotency key.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/RunSummary" }
        "400": { $ref: "#/components/responses/Error" }
        "401": { $ref: "#/components/responses/Error" }
        "409": { $ref: "#/components/responses/Error" }
        "422": { $ref: "#/components/responses/Error" }
    get:
      tags: [runs]
      summary: List runs, newest first.
      operationId: listRuns
      security: [{ clientToken: [] }, {}]
      parameters:
        - $ref: "#/components/parameters/Limit"
        - name: cursor
          in: query
          description: "`next_cursor` of the previous page."
          schema: { type: string }
      responses:
        "200":
          description: One page of runs.
          content:
            application/json:
              schema:
                type: object
                required: [items]
                properties:
                  items:
                    type: array
                    items: { $ref: "#/components/schemas/RunSummary" }
                  next_cursor: { type: string }
        "400": { $ref: "#/components/responses/Error" }
        "401": { $ref: "#/components/responses/Error" }
  /api/v1/runs/{run_id}:
    parameters:
      - $ref: "#/components/parameters/RunId"
    get:
      tags: [runs]
      summary: Inspect a run.
      operationId: getRun
      security: [{ clientToken: [] }, {}]
      responses:
        "200":
          description: The run.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/RunSummary" }
        "401": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
  /api/v1/runs/{run_id}/events:
    parameters:
      - $ref: "#/components/parameters/RunId"
    get:
      tags: [runs]
      summary: Progress events after a sequence number.
      description: |
        Returns events with `seq > after`, in order. With `wait`, the request is held
        (long poll) until at least one new event exists, the run finishes, or `wait`
        seconds pass. Clients reconnect with `after = next_after` and never miss or
        duplicate an event.
      operationId: listRunEvents
      security: [{ clientToken: [] }, {}]
      parameters:
        - name: after
          in: query
          schema: { type: integer, minimum: 0, default: 0 }
        - $ref: "#/components/parameters/Limit"
        - name: wait
          in: query
          description: Seconds to wait for new events (0 to 60).
          schema: { type: integer, minimum: 0, maximum: 60, default: 0 }
      responses:
        "200":
          description: A page of events.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/EventPage" }
        "401": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
  /api/v1/runs/{run_id}/report:
    parameters:
      - $ref: "#/components/parameters/RunId"
    get:
      tags: [runs]
      summary: The run report. Final once `lifecycle` is `finished`; retained after the worker is removed.
      operationId: getRunReport
      security: [{ clientToken: [] }, {}]
      responses:
        "200":
          description: The report.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/RunReport" }
        "401": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
  /api/v1/runs/{run_id}/evidence/{evidence_id}:
    parameters:
      - $ref: "#/components/parameters/RunId"
      - name: evidence_id
        in: path
        required: true
        schema: { type: string, pattern: "^ev-[0-9]+$" }
    get:
      tags: [runs]
      summary: One recorded observation.
      operationId: getRunEvidence
      security: [{ clientToken: [] }, {}]
      responses:
        "200":
          description: The evidence.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Evidence" }
        "401": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
  /api/v1/runs/{run_id}/cancellation:
    parameters:
      - $ref: "#/components/parameters/RunId"
    post:
      tags: [runs]
      summary: Request cancellation.
      description: |
        Records the request and asks the worker to stop. Idempotent: repeating it returns
        the same `cancel_requested_at`. The run is finished only once its lifecycle is
        `finished` (outcome `cancelled` unless it ended for another reason first).
      operationId: cancelRun
      security: [{ clientToken: [] }, {}]
      responses:
        "202":
          description: Cancellation requested.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/RunSummary" }
        "401": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
        "409":
          description: The run already finished (`run_finished`).
          content:
            application/json:
              schema: { $ref: "#/components/schemas/ErrorBody" }
  /api/v1/runs/{run_id}/plan:
    parameters:
      - $ref: "#/components/parameters/RunId"
    get:
      tags: [runs]
      summary: The plan the run executes, from the documents retained with it.
      description: |
        The submitted bundle and its units in execution order, expanded by the backend's
        parser: authored action, criteria, verification, source locations and the
        run-unique keys reports and events use. It is read from the run's retained input,
        never from changed files or the current configuration. A run without a plan,
        such as an investigation, gives `404`.
      operationId: getRunPlan
      security: [{ clientToken: [] }, {}]
      responses:
        "200":
          description: The plan.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/RunPlan" }
        "401": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
  /api/v1/environments:
    get:
      tags: [catalog]
      summary: Environments runs may name, with their effective limits.
      description: |
        Names, descriptions, effective execution limits and the names of the MCP servers
        steps may use. Parameters, secrets and connection details are never included.
      operationId: listEnvironments
      security: [{ clientToken: [] }, {}]
      responses:
        "200":
          description: Every configured environment, by name.
          content:
            application/json:
              schema:
                type: object
                required: [items]
                properties:
                  items:
                    type: array
                    items: { $ref: "#/components/schemas/EnvironmentInfo" }
        "401": { $ref: "#/components/responses/Error" }
  /api/v1/policies:
    get:
      tags: [catalog]
      summary: Policies runs may name, including the implied `default`.
      description: |
        Failure behavior, retries and the outcome actions each policy considers (name,
        server, tool, triggering outcomes and approval; never their arguments).
      operationId: listPolicies
      security: [{ clientToken: [] }, {}]
      responses:
        "200":
          description: Every policy, by name.
          content:
            application/json:
              schema:
                type: object
                required: [items]
                properties:
                  items:
                    type: array
                    items: { $ref: "#/components/schemas/PolicyInfo" }
        "401": { $ref: "#/components/responses/Error" }
  /api/v1/investigations:
    get:
      tags: [investigations]
      summary: List the investigations of all runs, newest first.
      operationId: listInvestigations
      security: [{ clientToken: [] }, {}]
      parameters:
        - $ref: "#/components/parameters/Limit"
        - name: cursor
          in: query
          description: "`next_cursor` of the previous page."
          schema: { type: string }
      responses:
        "200":
          description: One page of investigations.
          content:
            application/json:
              schema:
                type: object
                required: [items]
                properties:
                  items:
                    type: array
                    items: { $ref: "#/components/schemas/Investigation" }
                  next_cursor: { type: string }
        "400": { $ref: "#/components/responses/Error" }
        "401": { $ref: "#/components/responses/Error" }
    post:
      tags: [investigations]
      summary: Investigate a failed unit of a finished run.
      description: |
        Creates an investigation and starts its worker (docs/run-tests/investigate-failures.md). The
        backend copies the failed unit and the evidence its attempts cite from the run's
        report. Without `unit`, the first unit with outcome `failed`, `error` or `blocked`
        is selected. Without `sources`, every configured repository is cloned at its
        configured ref. Each run and unit has one current investigation: a second request
        for the same unit returns it with status 200. When it finished with outcome `error`
        or `cancelled`, a request starts a new investigation.
      operationId: createInvestigation
      security: [{ clientToken: [] }, {}]
      parameters:
        - $ref: "#/components/parameters/IdempotencyKey"
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: "#/components/schemas/InvestigationRequest" }
      responses:
        "202":
          description: Accepted and stored. The worker launches asynchronously.
          headers:
            Location:
              schema: { type: string }
              description: "`/api/v1/investigations/{investigation_id}`"
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Investigation" }
        "200":
          description: The existing investigation of the unit, or a replay with the same idempotency key.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Investigation" }
        "400": { $ref: "#/components/responses/Error" }
        "401": { $ref: "#/components/responses/Error" }
        "404":
          description: The run does not exist, or investigations are not configured.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/ErrorBody" }
        "409": { $ref: "#/components/responses/Error" }
        "422":
          description: |
            The run has not finished or is an investigation, the unit did not fail, or a
            source is not configured.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/ErrorBody" }
  /api/v1/investigations/{investigation_id}:
    parameters:
      - name: investigation_id
        in: path
        required: true
        description: The ID of the run that executes the investigation.
        schema: { type: string, pattern: "^run-[a-z0-9]+$" }
    get:
      tags: [investigations]
      summary: Inspect an investigation.
      description: |
        Its events, evidence and report are those of the run with the same ID, under
        `/api/v1/runs/{run_id}`. Cancel it there too.
      operationId: getInvestigation
      security: [{ clientToken: [] }, {}]
      responses:
        "200":
          description: The investigation.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Investigation" }
        "401": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
  /api/v1/runs/{run_id}/investigations:
    parameters:
      - $ref: "#/components/parameters/RunId"
    get:
      tags: [investigations]
      summary: The investigations of a run, oldest first.
      operationId: listRunInvestigations
      security: [{ clientToken: [] }, {}]
      responses:
        "200":
          description: The investigations.
          content:
            application/json:
              schema:
                type: object
                required: [items]
                properties:
                  items:
                    type: array
                    items: { $ref: "#/components/schemas/Investigation" }
        "401": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
  /api/v1/runs/{run_id}/actions:
    parameters:
      - $ref: "#/components/parameters/RunId"
    get:
      tags: [actions]
      summary: The run's outcome actions and their delivery.
      description: |
        Actions are recorded when the report becomes final, with their arguments rendered
        from it. Delivery state is separate from the run's outcome, which it never changes.
      operationId: listRunActions
      security: [{ clientToken: [] }, {}]
      responses:
        "200":
          description: The actions, in name order (empty when none was triggered).
          content:
            application/json:
              schema:
                type: object
                required: [items]
                properties:
                  items:
                    type: array
                    items: { $ref: "#/components/schemas/Action" }
        "401": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
  /api/v1/runs/{run_id}/actions/{name}:
    parameters:
      - $ref: "#/components/parameters/RunId"
      - $ref: "#/components/parameters/ActionName"
    get:
      tags: [actions]
      summary: One action with its history.
      operationId: getAction
      security: [{ clientToken: [] }, {}]
      responses:
        "200":
          description: The action, with `history`.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Action" }
        "400": { $ref: "#/components/responses/Error" }
        "401": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
  /api/v1/runs/{run_id}/actions/{name}/redispatch:
    parameters:
      - $ref: "#/components/parameters/RunId"
      - $ref: "#/components/parameters/ActionName"
    post:
      tags: [actions]
      summary: Queue an unknown or failed action again with its recorded arguments.
      description: |
        Without an idempotency key, a tool that already acted on an earlier call acts again.
        Check the remote system first, or reconcile instead.
      operationId: redispatchAction
      security: [{ clientToken: [] }, {}]
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: "#/components/schemas/Decision" }
      responses:
        "200":
          description: Queued.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Action" }
        "400": { $ref: "#/components/responses/Error" }
        "401": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
        "409":
          description: "`digest_mismatch` or `action_not_resolvable`."
          content:
            application/json:
              schema: { $ref: "#/components/schemas/ErrorBody" }
  /api/v1/runs/{run_id}/actions/{name}/reconciliation:
    parameters:
      - $ref: "#/components/parameters/RunId"
      - $ref: "#/components/parameters/ActionName"
    post:
      tags: [actions]
      summary: Record what a person found for an unknown delivery; nothing is sent.
      operationId: reconcileAction
      security: [{ clientToken: [] }, {}]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [state]
              additionalProperties: false
              properties:
                state: { type: string, enum: [completed, error] }
                note: { type: string, description: What was checked. }
      responses:
        "200":
          description: Reconciled.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Action" }
        "400": { $ref: "#/components/responses/Error" }
        "401": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
        "409":
          description: "`action_not_resolvable`: the action is not unknown."
          content:
            application/json:
              schema: { $ref: "#/components/schemas/ErrorBody" }
  /api/v1/runs/{run_id}/actions/{name}/approval:
    parameters:
      - $ref: "#/components/parameters/RunId"
      - $ref: "#/components/parameters/ActionName"
    post:
      tags: [actions]
      summary: Approve an action waiting for approval; it is then dispatched.
      operationId: approveAction
      security: [{ clientToken: [] }, {}]
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: "#/components/schemas/Decision" }
      responses:
        "200":
          description: Approved and queued.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Action" }
        "400": { $ref: "#/components/responses/Error" }
        "401": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
        "409":
          description: "`digest_mismatch` or `action_not_pending`."
          content:
            application/json:
              schema: { $ref: "#/components/schemas/ErrorBody" }
  /api/v1/runs/{run_id}/actions/{name}/rejection:
    parameters:
      - $ref: "#/components/parameters/RunId"
      - $ref: "#/components/parameters/ActionName"
    post:
      tags: [actions]
      summary: Reject an action waiting for approval; it is never dispatched.
      operationId: rejectAction
      security: [{ clientToken: [] }, {}]
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: "#/components/schemas/Decision" }
      responses:
        "200":
          description: Rejected.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Action" }
        "400": { $ref: "#/components/responses/Error" }
        "401": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
        "409":
          description: "`digest_mismatch` or `action_not_pending`."
          content:
            application/json:
              schema: { $ref: "#/components/schemas/ErrorBody" }
  /api/v1/actions:
    get:
      tags: [actions]
      summary: Actions in one state across runs, oldest first, one page at a time.
      description: |
        Ordered by creation time, then insertion. Pass `next_cursor` as `cursor` to read the
        next page; it is absent on the last page. The order stays stable while actions change
        state, so an action is never listed twice in one state's pages.
      operationId: listActions
      security: [{ clientToken: [] }, {}]
      parameters:
        - name: state
          in: query
          schema: { $ref: "#/components/schemas/ActionState" }
        - $ref: "#/components/parameters/Limit"
        - name: cursor
          in: query
          description: "`next_cursor` of the previous page."
          schema: { type: string }
      responses:
        "200":
          description: One page of actions.
          content:
            application/json:
              schema:
                type: object
                required: [items]
                properties:
                  items:
                    type: array
                    items: { $ref: "#/components/schemas/Action" }
                  next_cursor: { type: string }
        "400": { $ref: "#/components/responses/Error" }
        "401": { $ref: "#/components/responses/Error" }
  /api/v1/worker/runs/{run_id}/specification:
    parameters:
      - $ref: "#/components/parameters/RunId"
    get:
      tags: [worker]
      summary: The immutable run specification assigned to this worker.
      operationId: getWorkerSpecification
      security: [{ workerToken: [] }]
      responses:
        "200":
          description: The specification. Secrets are references; values are injected into the worker environment.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/RunSpec" }
        "401": { $ref: "#/components/responses/Error" }
        "403": { $ref: "#/components/responses/Error" }
        "409": { $ref: "#/components/responses/Error" }
  /api/v1/worker/runs/{run_id}/events:
    parameters:
      - $ref: "#/components/parameters/RunId"
    post:
      tags: [worker]
      summary: Report events. Acknowledged only after they are persisted.
      description: |
        The batch holds consecutive events. Events at or below the acknowledged sequence are
        ignored (retransmissions), so a worker that did not receive an acknowledgment resends
        the same batch. A batch that would skip a sequence number is rejected with
        `sequence_gap` and the current `acknowledged_seq` in `details`.
      operationId: postWorkerEvents
      security: [{ workerToken: [] }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [events]
              properties:
                events:
                  type: array
                  items: { $ref: "#/components/schemas/RunEvent" }
      responses:
        "200":
          description: Persisted.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/EventAck" }
        "401": { $ref: "#/components/responses/Error" }
        "403": { $ref: "#/components/responses/Error" }
        "409": { $ref: "#/components/responses/Error" }
        "422": { $ref: "#/components/responses/Error" }
  /api/v1/worker/runs/{run_id}/control:
    parameters:
      - $ref: "#/components/parameters/RunId"
    get:
      tags: [worker]
      summary: Whether the worker must stop.
      operationId: getWorkerControl
      security: [{ workerToken: [] }]
      responses:
        "200":
          description: Control state.
          content:
            application/json:
              schema:
                type: object
                required: [cancel_requested]
                properties:
                  cancel_requested: { type: boolean }
        "401": { $ref: "#/components/responses/Error" }
        "403": { $ref: "#/components/responses/Error" }
components:
  securitySchemes:
    clientToken:
      type: http
      scheme: bearer
      description: Token from the environment variable named by `backend.auth.token_env`.
    workerToken:
      type: http
      scheme: bearer
      description: Run-scoped token generated by the backend for one worker.
  parameters:
    RunId:
      name: run_id
      in: path
      required: true
      schema: { type: string, pattern: "^run-[a-z0-9]+$" }
    ActionName:
      name: name
      in: path
      required: true
      schema: { type: string, pattern: "^[A-Za-z0-9_-]+$" }
    Limit:
      name: limit
      in: query
      schema: { type: integer, minimum: 1, maximum: 200, default: 50 }
    IdempotencyKey:
      name: Idempotency-Key
      in: header
      description: |
        Client-chosen key (1 to 200 visible ASCII characters). Retrying a submission with the
        same key and body returns the original run instead of creating another one; reusing it
        with a different body fails with `idempotency_conflict`.
      schema: { type: string, minLength: 1, maxLength: 200 }
  responses:
    Error:
      description: A structured error.
      content:
        application/json:
          schema: { $ref: "#/components/schemas/ErrorBody" }
  schemas:
    ErrorBody:
      type: object
      required: [error]
      properties:
        error:
          type: object
          required: [code, message]
          properties:
            code:
              type: string
              enum: [unauthorized, forbidden, not_found, validation_failed, idempotency_conflict, run_finished, sequence_gap, action_not_pending, digest_mismatch, action_not_resolvable, bad_request, internal]
            message: { type: string }
            details:
              type: array
              items: { type: string }
            diagnostics:
              type: array
              description: Plan problems by document and line (`validation_failed`); `details` holds the same text.
              items: { $ref: "#/components/schemas/PlanDiagnostic" }
    Lifecycle:
      type: string
      enum: [accepted, starting, running, stopping, finished]
    Outcome:
      type: string
      enum: [passed, failed, blocked, inconclusive, error, skipped, cancelled]
    PlanBundle:
      type: object
      required: [entry, documents]
      properties:
        entry: { type: string, description: Bundle-relative path of the entry document. }
        documents:
          type: object
          description: Markdown sources keyed by bundle-relative path (`/` separators, no `..`).
          additionalProperties: { type: string }
    SubmitRun:
      type: object
      additionalProperties: false
      required: [bundle, environment]
      properties:
        bundle: { $ref: "#/components/schemas/PlanBundle" }
        environment: { type: string }
        policy: { type: string, default: default }
        from_step: { type: string, description: First unit key to execute. Includes procedures invoked by that unit. }
        skip_cleanup: { type: boolean, description: Skip every cleanup unit in this run. }
    OutcomeCounts:
      type: object
      required: [passed, failed, blocked, inconclusive, error, skipped, cancelled]
      properties:
        passed: { type: integer }
        failed: { type: integer }
        blocked: { type: integer }
        inconclusive: { type: integer }
        error: { type: integer }
        skipped: { type: integer }
        cancelled: { type: integer }
    RunSummary:
      type: object
      required: [id, plan_title, environment, policy, lifecycle, counts, total_units, last_event_seq, created_at]
      properties:
        id: { type: string }
        plan_title: { type: string }
        environment: { type: string }
        policy: { type: string }
        lifecycle: { $ref: "#/components/schemas/Lifecycle" }
        outcome: { $ref: "#/components/schemas/Outcome" }
        reason: { type: string }
        current_unit: { type: string }
        counts: { $ref: "#/components/schemas/OutcomeCounts" }
        total_units: { type: integer }
        last_event_seq: { type: integer }
        created_at: { type: string, format: date-time }
        finished_at: { type: [string, "null"], format: date-time }
        cancel_requested_at: { type: [string, "null"], format: date-time }
    EventPage:
      type: object
      required: [items, next_after, finished]
      properties:
        items:
          type: array
          items: { $ref: "#/components/schemas/RunEvent" }
        next_after: { type: integer }
        finished: { type: boolean }
    EventAck:
      type: object
      required: [acknowledged_seq]
      properties:
        acknowledged_seq: { type: integer }
    SourceLocation:
      type: object
      required: [document, line]
      properties:
        document: { type: string, description: Bundle-relative path. }
        line: { type: integer, minimum: 1 }
    PlanDiagnostic:
      type: object
      required: [document, line, message]
      properties:
        document: { type: string }
        line: { type: integer, minimum: 1 }
        message: { type: string }
    UnitKind:
      type: string
      enum: [test, step]
    Criterion:
      type: object
      required: [index, text, location]
      properties:
        index: { type: integer, minimum: 0, description: Position within the unit's expected results. }
        text: { type: string }
        location: { $ref: "#/components/schemas/SourceLocation" }
    CodeBlock:
      type: object
      required: [language, code, location]
      properties:
        language: { type: string }
        code: { type: string }
        run:
          type: boolean
          description: The info string marks the block to run as written; the worker runs it before it asks the model.
        location: { $ref: "#/components/schemas/SourceLocation" }
    ProcedureRef:
      type: object
      required: [target, location]
      properties:
        target: { type: string, description: Link target as written. }
        location: { $ref: "#/components/schemas/SourceLocation" }
    ExecutionLimits:
      type: object
      required: [run_timeout_secs, step_timeout_secs, operation_timeout_secs, model_request_timeout_secs, model_request_retries, model_calls_per_step, tool_calls_per_step, output_limit_bytes, cleanup_timeout_secs]
      properties:
        run_timeout_secs: { type: integer }
        step_timeout_secs: { type: integer }
        operation_timeout_secs: { type: integer }
        model_request_timeout_secs: { type: integer }
        model_request_retries: { type: integer }
        model_calls_per_step: { type: integer }
        tool_calls_per_step: { type: integer }
        output_limit_bytes: { type: integer }
        cleanup_timeout_secs: { type: integer, description: Time for cleanup units after an early stop, a cancellation or the run deadline. }
    EnvironmentInfo:
      type: object
      required: [name, limits]
      properties:
        name: { type: string }
        description: { type: string }
        limits: { $ref: "#/components/schemas/ExecutionLimits" }
        mcp_servers:
          type: array
          description: Names of the MCP servers steps may use.
          items: { type: string }
    PolicyInfo:
      type: object
      required: [name, configured, on_failure, after_stop, retry]
      properties:
        name: { type: string }
        configured: { type: boolean, description: False for the implied `default` policy. }
        on_failure: { type: string, enum: [stop, continue] }
        after_stop:
          type: string
          enum: [cleanup, skip]
          description: Whether the remaining cleanup units run after an early stop, a cancellation or the run deadline.
        retry:
          type: object
          required: [max_attempts]
          properties:
            max_attempts: { type: integer, minimum: 1 }
            on:
              type: array
              items: { $ref: "#/components/schemas/Outcome" }
        actions:
          type: array
          items: { $ref: "#/components/schemas/ActionInfo" }
    ActionInfo:
      type: object
      required: [name, server, tool, on, approval]
      properties:
        name: { type: string }
        server: { type: string }
        tool: { type: string }
        on:
          type: array
          items: { $ref: "#/components/schemas/Outcome" }
        approval: { type: string, enum: [none, required] }
    RunPlan:
      type: object
      required: [run_id, title, entry, documents, units]
      properties:
        run_id: { type: string }
        title: { type: string }
        entry: { type: string }
        documents:
          type: object
          additionalProperties: { type: string }
        units:
          type: array
          items: { $ref: "#/components/schemas/PlannedUnit" }
        from_step: { type: string, description: Starting unit key selected when the run was submitted. }
        diagnostics:
          type: array
          description: Why the retained bundle no longer expands, when it does not; `units` is then empty.
          items: { $ref: "#/components/schemas/PlanDiagnostic" }
    PlannedUnit:
      type: object
      required: [key, id, kind, title, phase, location, cleanup, action, criteria]
      properties:
        key: { type: string, description: "Run-unique key, as in reports and events." }
        id: { type: string }
        kind: { $ref: "#/components/schemas/UnitKind" }
        title: { type: string }
        phase: { type: string }
        location: { $ref: "#/components/schemas/SourceLocation" }
        cleanup: { type: boolean }
        action: { type: string, description: The action as authored Markdown. }
        commands:
          type: array
          items: { $ref: "#/components/schemas/CodeBlock" }
        criteria:
          type: array
          items: { $ref: "#/components/schemas/Criterion" }
        verification: { type: string }
        procedures:
          type: array
          items: { $ref: "#/components/schemas/ProcedureRef" }
        invoked_by:
          type: array
          description: Keys of the units whose invocations led here, outermost first.
          items: { type: string }
    ExecutionError:
      type: object
      required: [kind, message]
      properties:
        kind:
          type: string
          enum: [provider, invalid_assessment, tool, limit_exceeded, launch_failed, worker_lost, invalid_specification]
        message: { type: string }
    CriterionResult:
      type: object
      required: [criterion, status, basis, evidence, explanation]
      properties:
        criterion: { type: integer, minimum: 0 }
        status: { type: string, enum: [satisfied, contradicted, undetermined] }
        basis: { type: string, enum: [mechanical, model_judgment] }
        evidence:
          type: array
          items: { type: string }
        explanation: { type: string }
    Assessment:
      type: object
      required: [outcome, criteria, explanation]
      properties:
        outcome: { $ref: "#/components/schemas/Outcome" }
        criteria:
          type: array
          items: { $ref: "#/components/schemas/CriterionResult" }
        explanation: { type: string }
    Attempt:
      type: object
      required: [number, started_at, evidence]
      properties:
        number: { type: integer, minimum: 1 }
        started_at: { type: string, format: date-time }
        finished_at: { type: [string, "null"], format: date-time }
        outcome: { $ref: "#/components/schemas/Outcome" }
        assessment: { $ref: "#/components/schemas/Assessment" }
        error: { $ref: "#/components/schemas/ExecutionError" }
        evidence:
          type: array
          items: { type: string }
        timing: { $ref: "#/components/schemas/AttemptTiming" }
    AttemptTiming:
      description: |
        Where an attempt spent its time. The rest of the attempt duration is
        worker overhead.
      type: object
      required: [model_requests, model_ms, tool_ms]
      properties:
        model_requests: { type: integer, minimum: 0, description: Model requests sent. }
        model_ms: { type: integer, minimum: 0, description: Time spent on model requests, retries included. }
        tool_ms: { type: integer, minimum: 0, description: Time spent in tool calls. }
        input_tokens: { type: integer, minimum: 0, description: Input tokens of the model requests, as the provider reported them. }
        cached_tokens: { type: integer, minimum: 0, description: "The part of input_tokens read from the provider's prompt cache. Absent when the provider does not report it." }
        output_tokens: { type: integer, minimum: 0, description: Output tokens of the model requests, reasoning included. }
    UnitReport:
      type: object
      required: [key, id, kind, title, phase, location, cleanup, attempts]
      properties:
        key: { type: string }
        id: { type: string }
        kind: { $ref: "#/components/schemas/UnitKind" }
        title: { type: string }
        phase: { type: string }
        location: { $ref: "#/components/schemas/SourceLocation" }
        cleanup: { type: boolean }
        criteria:
          type: array
          items: { $ref: "#/components/schemas/Criterion" }
        outcome: { $ref: "#/components/schemas/Outcome" }
        reason: { type: string, description: Why the unit was skipped or interrupted. }
        attempts:
          type: array
          items: { $ref: "#/components/schemas/Attempt" }
    RunEvent:
      description: |
        One progress event: the common fields plus the fields of its `type` (see
        docs/reference/run-contracts.md).
      allOf:
        - type: object
          required: [run_id, seq, at]
          properties:
            run_id: { type: string }
            seq: { type: integer, minimum: 1 }
            at: { type: string, format: date-time }
            unit: { type: string }
            attempt: { type: integer, minimum: 1 }
            source: { $ref: "#/components/schemas/SourceLocation" }
        - oneOf:
            - type: object
              required: [type, model]
              properties:
                type: { type: string, const: run_started }
                model: { type: string }
                environment: { $ref: "#/components/schemas/WorkerEnvironment" }
            - type: object
              required: [type]
              properties:
                type: { type: string, const: attempt_started }
            - type: object
              required: [type, evidence]
              properties:
                type: { type: string, const: observation }
                evidence: { $ref: "#/components/schemas/Evidence" }
            - type: object
              description: >-
                A shell command is still running. Sent at a fixed interval while
                it runs; the observation that follows carries its result.
              required: [type, command, elapsed_secs]
              properties:
                type: { type: string, const: command_running }
                command: { type: string, description: The redacted command. }
                elapsed_secs: { type: integer, minimum: 0 }
            - type: object
              required: [type, outcome]
              properties:
                type: { type: string, const: attempt_finished }
                outcome: { $ref: "#/components/schemas/Outcome" }
                assessment: { $ref: "#/components/schemas/Assessment" }
                error: { $ref: "#/components/schemas/ExecutionError" }
                timing: { $ref: "#/components/schemas/AttemptTiming" }
            - type: object
              required: [type, outcome]
              properties:
                type: { type: string, const: unit_finished }
                outcome: { $ref: "#/components/schemas/Outcome" }
            - type: object
              required: [type, reason]
              properties:
                type: { type: string, const: unit_skipped }
                reason: { type: string }
            - type: object
              required: [type]
              properties:
                type: { type: string, const: cancellation_requested }
            - type: object
              required: [type, report]
              properties:
                type: { type: string, const: investigation_concluded }
                report: { $ref: "#/components/schemas/InvestigationReport" }
            - type: object
              required: [type, outcome, reason]
              properties:
                type: { type: string, const: run_finished }
                outcome: { $ref: "#/components/schemas/Outcome" }
                reason: { type: string }
    InvestigationReport:
      type: object
      description: The result of an investigation run (see crates/saci-core/src/investigation.rs).
      required: [outcome, summary]
      properties:
        outcome: { type: string, enum: [concluded, inconclusive, error, cancelled] }
        summary: { type: string }
        sources:
          type: array
          items:
            type: object
            required: [name, commit]
            properties:
              name: { type: string }
              commit: { type: string }
        hypotheses:
          type: array
          items:
            type: object
            required: [statement, confidence]
            properties:
              statement: { type: string }
              confidence: { type: string, enum: [high, medium, low] }
              evidence: { type: array, items: { type: string } }
              code:
                type: array
                items:
                  type: object
                  required: [repository, commit, path, start_line, end_line]
                  properties:
                    repository: { type: string }
                    commit: { type: string }
                    path: { type: string }
                    start_line: { type: integer, minimum: 1 }
                    end_line: { type: integer, minimum: 1 }
        bug:
          type: object
          required: [url, title]
          properties:
            url: { type: string }
            title: { type: string }
        no_bug_reason: { type: string }
    InvestigationRequest:
      type: object
      additionalProperties: false
      required: [run_id]
      properties:
        run_id: { type: string }
        unit: { type: string, description: The failed unit's key; the first unsuccessful unit when omitted. }
        sources:
          type: array
          description: Configured repositories to clone; all of them when omitted.
          items:
            type: object
            additionalProperties: false
            required: [name]
            properties:
              name: { type: string }
              ref: { type: string, description: The branch, tag or commit to check out instead of the configured ref. }
    Repository:
      type: object
      required: [name, url, ref]
      properties:
        name: { type: string }
        url: { type: string }
        ref: { type: string }
        description: { type: string }
    Investigation:
      type: object
      required: [id, parent_run, unit, plan_title, unit_title, lifecycle, repositories, created_at]
      properties:
        id: { type: string, description: The investigation, and the run that executes it. }
        parent_run: { type: string }
        unit: { type: string }
        plan_title: { type: string, description: Title of the investigated run's plan. }
        unit_title: { type: string, description: Title of the investigated unit. }
        lifecycle: { $ref: "#/components/schemas/Lifecycle" }
        outcome:
          type: string
          enum: [concluded, inconclusive, error, cancelled]
          description: Present once finished.
        reason: { type: string }
        repositories:
          type: array
          items: { $ref: "#/components/schemas/Repository" }
        created_at: { type: string, format: date-time }
        finished_at: { type: string, format: date-time }
        result: { $ref: "#/components/schemas/InvestigationReport" }
    CapturedText:
      type: object
      required: [text, total_bytes]
      properties:
        text: { type: string, description: "The retained, redacted text." }
        truncated: { type: boolean, description: Bytes beyond the configured limit were discarded. }
        total_bytes: { type: integer, description: Size of the original output. }
    Finding:
      description: >-
        One thing the guard found in an observation. It never holds the text that it is about.
        `credential`: the redaction masked a credential of the `label` kind, for example
        `aws-access-key-id`, and the evidence holds `[REDACTED:credential:<label>]` in its place.
      type: object
      required: [check, label]
      properties:
        check: { type: string, enum: [credential] }
        label: { type: string }
    Evidence:
      description: >-
        A redacted tool observation: the common fields plus the fields of its `tool` (shell,
        http, mcp or rejected). MCP evidence names the tool `tool_name`; `is_error` means the
        tool reported an error, `protocol_error` that no result arrived.
      allOf:
        - type: object
          required: [id, unit, attempt, recorded_at, duration_ms]
          properties:
            id: { type: string }
            unit: { type: string }
            attempt: { type: integer }
            recorded_at: { type: string, format: date-time }
            duration_ms: { type: integer }
            findings:
              type: array
              description: What the guard found in the observation. Absent when it found nothing.
              items: { $ref: "#/components/schemas/Finding" }
        - oneOf:
            - type: object
              required: [tool, command, stdout, stderr, termination]
              properties:
                tool: { type: string, const: shell }
                command: { type: string }
                stdout: { $ref: "#/components/schemas/CapturedText" }
                stderr: { $ref: "#/components/schemas/CapturedText" }
                exit_code: { type: integer }
                termination:
                  type: string
                  enum: [exited, timed_out, cancelled, session_ended, session_lost]
                restarted: { type: boolean, description: Ran in a new shell session; earlier state is gone. }
                session_lost: { type: boolean, description: The shell session did not survive this command. }
            - type: object
              required: [tool, method, url]
              properties:
                tool: { type: string, const: http }
                method: { type: string }
                url: { type: string }
                request_headers:
                  type: object
                  additionalProperties: { type: string }
                request_body: { $ref: "#/components/schemas/CapturedText" }
                response:
                  type: object
                  required: [status, headers, body]
                  properties:
                    status: { type: integer }
                    headers:
                      type: object
                      additionalProperties: { type: string }
                    body: { $ref: "#/components/schemas/CapturedText" }
                    body_error: { type: string }
                transport_error: { type: string, description: "No response arrived (connection, TLS, timeout)." }
            - type: object
              required: [tool, server, tool_name, arguments, result, is_error]
              properties:
                tool: { type: string, const: mcp }
                server: { type: string }
                tool_name: { type: string }
                arguments: {}
                result: { $ref: "#/components/schemas/CapturedText" }
                is_error: { type: boolean }
                protocol_error: { type: string }
            - type: object
              required: [tool, name, arguments, reason]
              properties:
                tool: { type: string, const: rejected }
                name: { type: string }
                arguments: { type: string, description: The arguments as received. }
                reason: { type: string }
    RunReport:
      type: object
      description: The durable run report (see docs/reference/run-contracts.md).
      required: [schema_version, run_id, plan_title, entry, environment, policy, model, created_at, lifecycle, counts, units, cleanup_not_run, evidence, last_seq]
      properties:
        schema_version: { type: integer }
        run_id: { type: string }
        plan_title: { type: string }
        entry: { type: string }
        environment: { type: string }
        policy: { type: string }
        model: { type: string }
        created_at: { type: string, format: date-time }
        lifecycle: { $ref: "#/components/schemas/Lifecycle" }
        outcome: { $ref: "#/components/schemas/Outcome" }
        reason: { type: string }
        started_at: { type: [string, "null"], format: date-time }
        finished_at: { type: [string, "null"], format: date-time }
        counts: { $ref: "#/components/schemas/OutcomeCounts" }
        units:
          type: array
          items: { $ref: "#/components/schemas/UnitReport" }
        cleanup_not_run:
          type: array
          description: Cleanup units that did not run.
          items: { type: string }
        evidence:
          type: array
          items: { $ref: "#/components/schemas/Evidence" }
        backend_build: { $ref: "#/components/schemas/BuildInfo" }
        worker_environment: { $ref: "#/components/schemas/WorkerEnvironment" }
        worker_image: { $ref: "#/components/schemas/WorkerImage" }
        investigation: { $ref: "#/components/schemas/InvestigationReport" }
        last_seq: { type: integer }
    BuildInfo:
      type: object
      description: A Saci build (see docs/contributing/development.md#build-commit).
      required: [version]
      properties:
        version: { type: string, description: The package version. }
        commit: { type: string, description: The commit the binary was built from. Absent when the build did not know it. }
    WorkerEnvironment:
      type: object
      description: Where the worker executed (see docs/reference/worker.md#environment-details). Absent until the worker starts, and for workers that do not report it.
      required: [saci, arch, tools]
      properties:
        saci: { $ref: "#/components/schemas/BuildInfo" }
        os: { type: string, description: "The operating system name and version, for example `Fedora Linux 44`." }
        kernel: { type: string, description: "The kernel name and release, for example `Linux 6.12.0`." }
        arch: { type: string, description: "The CPU architecture, for example `x86_64`." }
        tools:
          type: array
          description: The tools found on the worker's PATH, with the first line of their version output.
          items:
            type: object
            required: [name, version]
            properties:
              name: { type: string }
              version: { type: string }
    WorkerImage:
      type: object
      description: The container image that ran the worker (see docs/operate/container-workers.md#worker-image). Absent until the backend launches the worker.
      required: [reference]
      properties:
        reference: { type: string, description: "The configured `worker.image`, with its tag." }
        digest: { type: string, description: "Registry digest, `sha256:<hex>`. Absent for an image that did not come from a registry." }
        id: { type: string, description: "Local image ID, `sha256:<hex>`, when the runtime reports one." }
    ActionState:
      type: string
      enum: [pending_approval, queued, executing, rejected, completed, error, unknown]
      default: pending_approval
    Decision:
      type: object
      required: [digest]
      properties:
        digest: { type: string, description: Digest of the reviewed arguments. }
        note: { type: string, description: Kept with the decision. }
    Action:
      type: object
      required: [run_id, name, server, tool, approval, state, arguments, digest, attempts, created_at, updated_at]
      properties:
        run_id: { type: string }
        name: { type: string, description: Configured action name. }
        server: { type: string, description: "Server under `actions.servers`." }
        tool: { type: string }
        approval: { type: string, enum: [none, required] }
        state: { $ref: "#/components/schemas/ActionState" }
        arguments:
          type: object
          description: The exact arguments sent, or to be sent, rendered from the final report.
        digest: { type: string, description: Hex SHA-256 of the serialized arguments. }
        idempotency_key:
          type: string
          description: The key placed in the arguments under the configured `idempotency_key` argument.
        decision:
          type: object
          required: [verdict, at]
          properties:
            verdict: { type: string, enum: [approved, rejected] }
            at: { type: string, format: date-time }
            note: { type: string }
        attempts: { type: integer, minimum: 0 }
        created_at: { type: string, format: date-time }
        updated_at: { type: string, format: date-time }
        result: { type: string, description: "The tool's result text, once it answered." }
        error: { type: string, description: Why delivery failed or is unknown. }
        history:
          type: array
          description: Served for a single action only.
          items:
            type: object
            required: [at, event, state, attempt]
            properties:
              at: { type: string, format: date-time }
              event:
                type: string
                enum: [recorded, approved, rejected, claimed, answered, failed, uncertain, replayed, redispatched, reconciled]
              state: { $ref: "#/components/schemas/ActionState" }
              attempt: { type: integer, minimum: 0 }
              detail: { type: string, description: The error or note. }
    RunSpec:
      type: object
      description: The immutable run specification (see crates/saci-core/src/spec.rs).
      required: [schema_version, run_id, created_at, environment, policy, limits, llm]
      properties:
        schema_version: { type: integer }
        run_id: { type: string }
        created_at: { type: string, format: date-time }
        bundle:
          $ref: "#/components/schemas/PlanBundle"
          description: The plan of a test run. Exactly one of `bundle` and `investigation` is set.
        investigation:
          type: object
          additionalProperties: true
          description: The failure to investigate, for an investigation run (see crates/saci-core/src/investigation.rs).
        environment: { type: object, additionalProperties: true }
        policy: { type: object, additionalProperties: true }
        limits: { type: object, additionalProperties: true }
        llm: { type: object, additionalProperties: true }
