components:
    headers:
        Location:
            description: The execution's path, `/v1/executions/{id}`.
            schema:
                type: string
    parameters:
        ExecutionID:
            in: path
            name: id
            required: true
            schema:
                examples:
                    - exe_01JD3K7Q2M8V5T9W4X6Y0Z1A2B
                type: string
        IdempotencyKey:
            description: The client's key, 16 to 128 characters of `[A-Za-z0-9_-]`, required when any step changes state. Sent as a structured-field string (`"…"`) or bare. May be sent in the body as `idempotency_key` instead, or in both if equal. A read intent takes no key.
            in: header
            name: Idempotency-Key
            required: false
            schema:
                type: string
    responses:
        IdentityUnavailable:
            content:
                application/problem+json:
                    schema:
                        $ref: '#/components/schemas/Problem'
            description: The identity provider is unavailable. No code; `Retry-After` set.
            headers:
                Retry-After:
                    description: Seconds to wait (RFC 9110).
                    schema:
                        minimum: 1
                        type: integer
        Internal:
            content:
                application/problem+json:
                    schema:
                        $ref: '#/components/schemas/Problem'
            description: Sefra could not process the request; nothing was accepted. No code.
        Unauthorized:
            content:
                application/problem+json:
                    schema:
                        $ref: '#/components/schemas/Problem'
            description: No bearer token, or one that is not valid. No code.
            headers:
                WWW-Authenticate:
                    schema:
                        type: string
        UnknownExecution:
            content:
                application/problem+json:
                    schema:
                        $ref: '#/components/schemas/Problem'
            description: '`UNKNOWN_EXECUTION`: no such execution, or another principal''s. The envelope is the reduced one.'
    schemas:
        Envelope:
            properties:
                approval:
                    properties:
                        approval_id:
                            type: string
                        expires_at:
                            type: string
                        required:
                            items:
                                type: string
                            type: array
                    required:
                        - approval_id
                        - expires_at
                        - required
                    type: object
                error:
                    properties:
                        code:
                            enum:
                                - LICENSE_EXPIRED
                                - PAUSED
                                - PRINCIPAL_SUSPENDED
                                - RATE_LIMITED
                                - UNKNOWN_INTENT
                                - INTENT_DISABLED
                                - IDEMPOTENCY_KEY_MISSING
                                - IDEMPOTENCY_KEY_REUSED
                                - SCHEMA_VIOLATION
                                - PLAN_INVALID
                                - UNKNOWN_EXECUTION
                                - NOT_CANCELLABLE
                                - FACT_UNAVAILABLE
                                - POLICY_ERROR
                                - POLICY_DENIED
                                - APPROVAL_DENIED
                                - APPROVAL_EXPIRED
                                - APPROVAL_SCOPE_EXCEEDED
                                - CANCELLED
                                - DOWNSTREAM_REJECTED
                                - DOWNSTREAM_UNAVAILABLE
                                - DOWNSTREAM_OUTCOME_UNKNOWN
                        message:
                            type: string
                        retry_after_s:
                            minimum: 0
                            type: integer
                        retryable:
                            type: boolean
                        stage:
                            enum:
                                - admission
                                - validation
                                - policy
                                - approval
                                - execution
                        step:
                            type: string
                        violations:
                            items:
                                properties:
                                    actual: {}
                                    hint:
                                        type: string
                                    limit: {}
                                    message:
                                        type: string
                                    path:
                                        type: string
                                    rule:
                                        type: string
                                required:
                                    - message
                                type: object
                            type: array
                    required:
                        - code
                        - stage
                        - message
                        - retryable
                    type: object
                execution_id:
                    type:
                        - string
                        - "null"
                poll_after_s:
                    minimum: 0
                    type: integer
                replayed:
                    type: boolean
                status:
                    enum:
                        - pending_approval
                        - executing
                        - compensating
                        - succeeded
                        - rejected
                        - failed
                        - rolled_back
                        - needs_intervention
                        - resolved_manually
                steps:
                    items:
                        properties:
                            id:
                                type: string
                            intent:
                                type: string
                            output:
                                type: object
                            status:
                                enum:
                                    - pending
                                    - executing
                                    - succeeded
                                    - failed
                                    - unknown
                                    - compensating
                                    - compensated
                                    - compensation_failed
                                    - skipped
                        required:
                            - id
                            - intent
                            - status
                        type: object
                    type: array
            required:
                - execution_id
            title: Sefra result envelope
            type: object
        IdempotencyKey:
            pattern: ^[A-Za-z0-9_-]{16,128}$
            type: string
        PlanSubmission:
            additionalProperties: false
            properties:
                idempotency_key:
                    $ref: '#/components/schemas/IdempotencyKey'
                rationale:
                    $ref: '#/components/schemas/Rationale'
                steps:
                    items:
                        additionalProperties: false
                        properties:
                            id:
                                pattern: ^[a-z0-9_]{1,32}$
                                type: string
                            input:
                                description: 'A value may be {"$ref": "steps.<step id>.output.<field>"}, an output field of an earlier step.'
                                type: object
                            intent:
                                type: string
                        required:
                            - id
                            - intent
                            - input
                        type: object
                    maxItems: 16
                    minItems: 2
                    type: array
            required:
                - steps
            type: object
        Problem:
            properties:
                code:
                    description: The error code. Absent for a malformed request, 401, 413, 500 and 503 without an operation refused.
                    type: string
                detail:
                    description: The refusal's fixed message, or what was malformed.
                    type: string
                envelope:
                    $ref: '#/components/schemas/Envelope'
                    description: The full result envelope, present whenever `code` is.
                status:
                    type: integer
                title:
                    description: The HTTP status phrase.
                    type: string
                type:
                    const: about:blank
                    type: string
            required:
                - type
                - title
                - status
            type: object
        Rationale:
            description: Optional, agent-supplied and unverified. Shown to approvers; never influences a decision.
            maxLength: 2000
            type: string
        SingleIntentSubmission:
            additionalProperties: false
            properties:
                idempotency_key:
                    $ref: '#/components/schemas/IdempotencyKey'
                input:
                    description: The intent's input, as its JSON Schema in the bundle says (the `input` of its MCP tool).
                    type: object
                intent:
                    description: An intent type granted to the caller, for example `procurement.create_purchase_order`.
                    type: string
                rationale:
                    $ref: '#/components/schemas/Rationale'
            required:
                - intent
                - input
            type: object
    securitySchemes:
        bearer:
            bearerFormat: JWT
            description: An OAuth access token for the agent listener's resource. A 401 names the protected resource metadata (RFC 9728) in `WWW-Authenticate`.
            scheme: bearer
            type: http
info:
    description: The same submissions, reads and cancellations as the MCP tools on `/mcp`. Both run one pipeline, so grants, policy, limits and the audit trail behave identically. A refused request is a problem document (RFC 9457) with `code` and the full result in `envelope`. An accepted execution is answered with its result, whatever its later outcome. Generate one idempotency key per business action and reuse it when retrying that action. If the result is not final, read it again after `poll_after_s` seconds.
    license:
        name: Proprietary
    summary: Submit, read and cancel governed executions over REST.
    title: Sefra agent API
    version: "1"
openapi: 3.1.0
paths:
    /v1/executions:
        post:
            description: A body with `intent` is a single intent, the MCP tool input of that intent with the intent type named beside it. A body with `steps` is a plan, the input of `sefra-submit_plan`. Only this route takes a rate-limit token.
            operationId: submitExecution
            parameters:
                - $ref: '#/components/parameters/IdempotencyKey'
            requestBody:
                content:
                    application/json:
                        schema:
                            oneOf:
                                - $ref: '#/components/schemas/SingleIntentSubmission'
                                - $ref: '#/components/schemas/PlanSubmission'
                required: true
            responses:
                "200":
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/Envelope'
                    description: Replay. The key is bound to an execution with the same payload; its current envelope, `replayed` true.
                    headers:
                        Location:
                            $ref: '#/components/headers/Location'
                "201":
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/Envelope'
                    description: Accepted, and the execution reached a final status within the synchronous wait.
                    headers:
                        Location:
                            $ref: '#/components/headers/Location'
                "202":
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/Envelope'
                    description: Accepted, and the execution is not final yet (`executing`, `compensating`, `pending_approval`, `needs_intervention`). Poll its Location after `poll_after_s`.
                    headers:
                        Location:
                            $ref: '#/components/headers/Location'
                "400":
                    content:
                        application/problem+json:
                            schema:
                                $ref: '#/components/schemas/Problem'
                    description: '`IDEMPOTENCY_KEY_MISSING`, or a malformed request without a code: not a JSON object, both or neither of `intent` and `steps`, `intent` not a string, the key header twice, or header and body keys that differ.'
                "401":
                    $ref: '#/components/responses/Unauthorized'
                "403":
                    content:
                        application/problem+json:
                            schema:
                                $ref: '#/components/schemas/Problem'
                    description: '`POLICY_DENIED`, `POLICY_ERROR` or `PRINCIPAL_SUSPENDED`.'
                "413":
                    content:
                        application/problem+json:
                            schema:
                                $ref: '#/components/schemas/Problem'
                    description: The body is larger than 2 MiB. No code, no execution.
                "422":
                    content:
                        application/problem+json:
                            schema:
                                $ref: '#/components/schemas/Problem'
                    description: '`SCHEMA_VIOLATION`, `PLAN_INVALID`, `UNKNOWN_INTENT` or `IDEMPOTENCY_KEY_REUSED`.'
                "429":
                    content:
                        application/problem+json:
                            schema:
                                $ref: '#/components/schemas/Problem'
                    description: '`RATE_LIMITED`. Retry after `Retry-After` seconds, equal to `envelope.error.retry_after_s`; the key is still free.'
                    headers:
                        Retry-After:
                            description: Seconds to wait (RFC 9110).
                            schema:
                                minimum: 1
                                type: integer
                "500":
                    $ref: '#/components/responses/Internal'
                "503":
                    content:
                        application/problem+json:
                            schema:
                                $ref: '#/components/schemas/Problem'
                    description: '`PAUSED`, `INTENT_DISABLED`, `FACT_UNAVAILABLE` or `LICENSE_EXPIRED`, retryable later; or, without a code, no active bundle or the identity provider unavailable (then with `Retry-After`).'
                    headers:
                        Retry-After:
                            description: Seconds to wait; set only when the identity provider is unavailable.
                            schema:
                                minimum: 1
                                type: integer
            summary: Submit a single intent or a plan
    /v1/executions/{id}:
        get:
            operationId: getExecution
            parameters:
                - $ref: '#/components/parameters/ExecutionID'
            responses:
                "200":
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/Envelope'
                    description: The current envelope, whatever the status.
                "401":
                    $ref: '#/components/responses/Unauthorized'
                "404":
                    $ref: '#/components/responses/UnknownExecution'
                "500":
                    $ref: '#/components/responses/Internal'
                "503":
                    $ref: '#/components/responses/IdentityUnavailable'
            summary: Read one of your executions
    /v1/executions/{id}/cancel:
        post:
            operationId: cancelExecution
            parameters:
                - $ref: '#/components/parameters/ExecutionID'
            responses:
                "200":
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/Envelope'
                    description: Cancelled. The envelope, `rejected` with `CANCELLED`.
                "401":
                    $ref: '#/components/responses/Unauthorized'
                "404":
                    $ref: '#/components/responses/UnknownExecution'
                "409":
                    content:
                        application/problem+json:
                            schema:
                                $ref: '#/components/schemas/Problem'
                    description: '`NOT_CANCELLABLE`: the execution is not `pending_approval`.'
                "500":
                    $ref: '#/components/responses/Internal'
                "503":
                    $ref: '#/components/responses/IdentityUnavailable'
            summary: Cancel one of your executions while it is pending approval
security:
    - bearer: []
servers:
    - description: Your Sefra deployment
      url: https://sefra.example.com
