{
  "openapi": "3.1.0",
  "info": {
    "title": "Duscode API",
    "version": "v1",
    "summary": "Hand Dus an engineering job, follow it, and fetch its Proof of Work.",
    "description": "The local Duscode server's versioned API. Every call except the version and this document needs an API token created in Settings → API & MCP, sent as `Authorization: Bearer dsk_…`. A token is scoped to named projects, named job profiles and named actions; it expires, can be revoked at once, and every use is audited. A job started through the API always runs in its own isolated session under one of the token's job profiles."
  },
  "servers": [
    {
      "url": "http://localhost:4317",
      "description": "The Duscode server on this computer (its port may differ: see `duscode status`)."
    }
  ],
  "tags": [
    {
      "name": "jobs",
      "description": "Engineering jobs"
    },
    {
      "name": "meta",
      "description": "Version and projects"
    },
    {
      "name": "insights",
      "description": "Outcome insights"
    }
  ],
  "paths": {
    "/api/v1": {
      "get": {
        "operationId": "getVersion",
        "tags": [
          "meta"
        ],
        "summary": "The API version",
        "responses": {
          "200": {
            "description": "The version and where this document is.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "version",
                    "openapi"
                  ],
                  "properties": {
                    "version": {
                      "type": "string"
                    },
                    "openapi": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/openapi.json": {
      "get": {
        "operationId": "getOpenApi",
        "tags": [
          "meta"
        ],
        "summary": "This document",
        "responses": {
          "200": {
            "description": "The OpenAPI document.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/projects": {
      "get": {
        "operationId": "listProjects",
        "tags": [
          "meta"
        ],
        "summary": "Projects this token may work on",
        "security": [
          {
            "apiToken": []
          }
        ],
        "x-duscode-scope": "any",
        "responses": {
          "200": {
            "description": "The projects.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/Project"
                  }
                }
              }
            }
          },
          "401": {
            "description": "No token, or the token is invalid, revoked or expired.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "The token may not do this, or not on this project or profile.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/jobs": {
      "get": {
        "operationId": "listJobs",
        "tags": [
          "jobs"
        ],
        "summary": "Engineering jobs, newest first",
        "security": [
          {
            "apiToken": []
          }
        ],
        "x-duscode-scope": "jobs:read",
        "parameters": [
          {
            "name": "project",
            "in": "query",
            "required": false,
            "description": "A project id this token may work on. Without it, every project the token may see.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The jobs.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/Job"
                  }
                }
              }
            }
          },
          "401": {
            "description": "No token, or the token is invalid, revoked or expired.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "The token may not do this, or not on this project or profile.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "post": {
        "operationId": "startJob",
        "tags": [
          "jobs"
        ],
        "summary": "Start an engineering job",
        "security": [
          {
            "apiToken": []
          }
        ],
        "x-duscode-scope": "jobs:start",
        "description": "Dus builds what the objective asks, commits, pushes, opens the pull request, verifies it with Local CI and merges under the project's merge rule, within the budget. The job runs in a new isolated session under the named job profile.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/JobStart"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "The job, queued or running.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Job"
                }
              }
            }
          },
          "400": {
            "description": "The request is incomplete or invalid.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "No token, or the token is invalid, revoked or expired.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "The token may not do this, or not on this project or profile.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/jobs/{id}": {
      "get": {
        "operationId": "getJob",
        "tags": [
          "jobs"
        ],
        "summary": "One job",
        "security": [
          {
            "apiToken": []
          }
        ],
        "x-duscode-scope": "jobs:read",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "The job id (job-…).",
            "schema": {
              "type": "string",
              "pattern": "^[a-zA-Z0-9][a-zA-Z0-9_-]{0,119}$"
            }
          },
          {
            "name": "project",
            "in": "query",
            "required": false,
            "description": "A project id this token may work on. Without it, every project the token may see.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The job.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Job"
                }
              }
            }
          },
          "401": {
            "description": "No token, or the token is invalid, revoked or expired.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "The token may not do this, or not on this project or profile.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "No such job in the projects this token may see.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/jobs/{id}/events": {
      "get": {
        "operationId": "streamJobEvents",
        "tags": [
          "jobs"
        ],
        "summary": "Follow a job (Server-Sent Events)",
        "security": [
          {
            "apiToken": []
          }
        ],
        "x-duscode-scope": "jobs:read",
        "description": "A `job.snapshot` event first, then `job.updated` and `job.effect` events as the job moves, and `job.finished` when it merges or is cancelled; then the stream ends. Each event's `id` is its sequence number: reconnect with `?after=<id>` to skip what you have.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "The job id (job-…).",
            "schema": {
              "type": "string",
              "pattern": "^[a-zA-Z0-9][a-zA-Z0-9_-]{0,119}$"
            }
          },
          {
            "name": "project",
            "in": "query",
            "required": false,
            "description": "A project id this token may work on. Without it, every project the token may see.",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "after",
            "in": "query",
            "required": false,
            "description": "Skip events up to this sequence number.",
            "schema": {
              "type": "integer",
              "minimum": 0
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The event stream; each `data:` line is a JobEvent.",
            "content": {
              "text/event-stream": {
                "schema": {
                  "$ref": "#/components/schemas/JobEvent"
                }
              }
            }
          },
          "401": {
            "description": "No token, or the token is invalid, revoked or expired.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "The token may not do this, or not on this project or profile.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "No such job.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/jobs/{id}/proof": {
      "get": {
        "operationId": "getJobProof",
        "tags": [
          "jobs"
        ],
        "summary": "A job's Proof of Work",
        "security": [
          {
            "apiToken": []
          }
        ],
        "x-duscode-scope": "jobs:read",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "The job id (job-…).",
            "schema": {
              "type": "string",
              "pattern": "^[a-zA-Z0-9][a-zA-Z0-9_-]{0,119}$"
            }
          },
          {
            "name": "project",
            "in": "query",
            "required": false,
            "description": "A project id this token may work on. Without it, every project the token may see.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "What the engine itself recorded for the job.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/JobProof"
                }
              }
            }
          },
          "401": {
            "description": "No token, or the token is invalid, revoked or expired.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "The token may not do this, or not on this project or profile.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "No such job.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/jobs/{id}/cancel": {
      "post": {
        "operationId": "cancelJob",
        "tags": [
          "jobs"
        ],
        "summary": "Cancel a job",
        "security": [
          {
            "apiToken": []
          }
        ],
        "x-duscode-scope": "jobs:cancel",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "The job id (job-…).",
            "schema": {
              "type": "string",
              "pattern": "^[a-zA-Z0-9][a-zA-Z0-9_-]{0,119}$"
            }
          },
          {
            "name": "project",
            "in": "query",
            "required": false,
            "description": "A project id this token may work on. Without it, every project the token may see.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The cancelled job.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Job"
                }
              }
            }
          },
          "401": {
            "description": "No token, or the token is invalid, revoked or expired.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "The token may not do this, or not on this project or profile.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "No such job.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "The job already merged, or another window runs it.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/insights": {
      "get": {
        "operationId": "getInsights",
        "tags": [
          "insights"
        ],
        "summary": "Outcome insights",
        "security": [
          {
            "apiToken": []
          }
        ],
        "x-duscode-scope": "insights:read",
        "description": "Cost per verified merged change and per healthy deploy, merge, rollback and revert rates, time to merge and owner attention, by project, member and job profile, computed only from the engine's receipts.",
        "parameters": [
          {
            "name": "project",
            "in": "query",
            "required": false,
            "description": "A project id this token may work on. Without it, every project the token may see.",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "days",
            "in": "query",
            "required": false,
            "description": "The period, in days (default 30).",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 365
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The insights.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "400": {
            "description": "Bad period.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "No token, or the token is invalid, revoked or expired.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "The token may not do this, or not on this project or profile.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "501": {
            "description": "Insights are not available on this server.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "apiToken": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "dsk_<id>_<secret>",
        "description": "Scopes: `jobs:start` (Start engineering jobs); `jobs:read` (See jobs, their progress and their Proof of Work); `jobs:cancel` (Cancel jobs); `insights:read` (Read outcome insights); `metrics:read` (Read Prometheus metrics (/metrics))."
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "required": [
          "error",
          "code"
        ],
        "properties": {
          "error": {
            "type": "string"
          },
          "code": {
            "type": "string",
            "enum": [
              "unauthorized",
              "forbidden",
              "not_found",
              "bad_request",
              "conflict",
              "method_not_allowed",
              "not_available"
            ]
          }
        }
      },
      "Project": {
        "type": "object",
        "required": [
          "id",
          "name"
        ],
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          }
        }
      },
      "JobStart": {
        "type": "object",
        "required": [
          "objective"
        ],
        "additionalProperties": false,
        "properties": {
          "objective": {
            "type": "string",
            "minLength": 1,
            "maxLength": 4000,
            "description": "What the job should achieve, in plain words."
          },
          "project": {
            "type": "string",
            "description": "Optional when the token has exactly one project."
          },
          "profile": {
            "type": "string",
            "description": "A job profile the token allows; optional when it allows exactly one."
          },
          "budgetUsd": {
            "type": "number",
            "exclusiveMinimum": 0,
            "maximum": 100,
            "description": "A spending cap; the profile's own cap still applies."
          },
          "title": {
            "type": "string",
            "maxLength": 60
          }
        }
      },
      "Job": {
        "type": "object",
        "required": [
          "id",
          "project",
          "objective",
          "status",
          "step",
          "reason",
          "updatedAt",
          "running",
          "needsOwner"
        ],
        "properties": {
          "id": {
            "type": "string"
          },
          "project": {
            "type": "string"
          },
          "objective": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "enum": [
              "working",
              "waiting",
              "blocked",
              "merged",
              "cancelled"
            ]
          },
          "step": {
            "type": "string"
          },
          "reason": {
            "type": "string"
          },
          "profile": {
            "type": "object",
            "required": [
              "id",
              "name"
            ],
            "properties": {
              "id": {
                "type": "string"
              },
              "name": {
                "type": "string"
              }
            }
          },
          "budgetUsd": {
            "type": "number"
          },
          "spentUsd": {
            "type": "number"
          },
          "mergedSha": {
            "type": "string"
          },
          "createdAt": {
            "type": "string",
            "format": "date-time"
          },
          "updatedAt": {
            "type": "string",
            "format": "date-time"
          },
          "running": {
            "type": "boolean"
          },
          "needsOwner": {
            "type": "boolean",
            "description": "It waits on the owner (an Owner's Call), not on CI or a reviewer."
          }
        }
      },
      "JobEffect": {
        "type": "object",
        "required": [
          "step",
          "outcome",
          "evidence",
          "at"
        ],
        "properties": {
          "step": {
            "type": "string"
          },
          "outcome": {
            "type": "string",
            "enum": [
              "done",
              "failed",
              "uncertain"
            ]
          },
          "evidence": {
            "type": "string"
          },
          "at": {
            "type": "string",
            "format": "date-time"
          },
          "costUsd": {
            "type": "number"
          },
          "receipt": {
            "type": "string",
            "enum": [
              "reserved",
              "confirmed"
            ]
          }
        }
      },
      "JobEvent": {
        "type": "object",
        "required": [
          "seq",
          "at",
          "type",
          "job"
        ],
        "properties": {
          "seq": {
            "type": "integer"
          },
          "at": {
            "type": "string",
            "format": "date-time"
          },
          "type": {
            "type": "string",
            "enum": [
              "job.snapshot",
              "job.updated",
              "job.effect",
              "job.finished"
            ]
          },
          "job": {
            "$ref": "#/components/schemas/Job"
          },
          "effect": {
            "$ref": "#/components/schemas/JobEffect"
          }
        }
      },
      "JobProof": {
        "type": "object",
        "required": [
          "job",
          "effects",
          "proofs",
          "attribution"
        ],
        "properties": {
          "job": {
            "$ref": "#/components/schemas/Job"
          },
          "effects": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/JobEffect"
            }
          },
          "proofs": {
            "type": "array",
            "items": {
              "type": "object"
            },
            "description": "Proof of Work records of the tasks that did the work."
          },
          "attribution": {
            "type": "string",
            "enum": [
              "session",
              "time-window",
              "none"
            ],
            "description": "How the proofs were tied to the job."
          },
          "releaseEvidence": {
            "type": "object",
            "description": "What is proven from objective to release, when known."
          }
        }
      }
    }
  }
}
