{
  "openapi": "3.1.0",
  "info": {
    "title": "DiscoTrace Agentic AEO API",
    "version": "1.0.0",
    "description": "Workspace-scoped REST API for reading studies, graded AI buyer traces, coded reservations, Content Center drafts, deterministic credit estimates, and queueing study runs. Authenticate with a workspace Bearer API key (`dt_live_...` or `dt_test_...`).",
    "contact": {
      "name": "DiscoTrace",
      "url": "https://discotrace.com",
      "email": "hello@discotrace.com"
    }
  },
  "servers": [
    {
      "url": "https://app.discotrace.com/api/v1",
      "description": "DiscoTrace Portal API v1"
    }
  ],
  "tags": [
    {
      "name": "Workspace & Credits",
      "description": "Inspect authenticated workspace identity, API key scopes, live credit balances, reservation totals, and deterministic pre-flight quotes. All endpoints in this group require at least the `read` scope and never deduct credits. Use `GET /me` to verify your key prefix and auto-approve ceiling before automating runs, `GET /credits` to audit recent credit ledger entries and active job holds, and `POST /credits/estimate` to preview exact credit math for any combination of surfaces, scenarios, and experiment asks."
    },
    {
      "name": "Studies & Diagnostics",
      "description": "Create versioned study blueprints, read pass-over-pass KPI summaries, filter individual graded AI buyer traces, and inspect coded reservations across your authenticated workspace. Read endpoints (`GET /studies`, `GET /studies/{slug}`, `/runs`, `/traces`, `/reservations`) require the `read` scope and return structured JSON synced from published runs. Creating a new study (`POST /studies`) requires the `write_study` scope and supports `Idempotency-Key` headers for safe retries."
    },
    {
      "name": "Content Center",
      "description": "Read prioritized AEO content opportunities derived from coded reservations and missing-proof diagnostics, and create or update grounded Markdown drafts in the workspace Content Generation Center. Listing drafts requires the `read` scope; creating or updating drafts requires the `write_drafts` scope. Drafts created by agents are marked `draft` or `in_review` and never publish automatically to your live site without human editorial review."
    },
    {
      "name": "Run Queue",
      "description": "Queue baseline studies, post-ship rechecks, and controlled A/A/B experiments, and poll live job execution status with deterministic credit guardrails. Queueing runs (`POST /studies/{slug}/runs`) requires the `queue_runs` scope and enforces both your workspace credit balance and the API key auto-approve credit ceiling (`auto_approve_max_credits`). Runs above the ceiling enter `awaiting_approval` and return a `resolveUrl` for human confirmation; `dry_run: true` validates quotes with zero credit deduction."
    }
  ],
  "security": [
    {
      "BearerAuth": []
    }
  ],
  "components": {
    "securitySchemes": {
      "BearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "Workspace API key created in Portal -> API & MCP (`dt_live_...` or `dt_test_...`). Also accepted via `X-API-Key`."
      }
    },
    "schemas": {
      "ErrorResponse": {
        "type": "object",
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "type": "object",
            "required": [
              "code",
              "message",
              "retryable",
              "resolveUrl"
            ],
            "properties": {
              "code": {
                "type": "string",
                "example": "insufficient_scope"
              },
              "message": {
                "type": "string",
                "example": "API key is missing required scope 'run'."
              },
              "retryable": {
                "type": "boolean",
                "example": false
              },
              "resolveUrl": {
                "type": "string",
                "format": "uri",
                "example": "https://app.discotrace.com/w/acme/api"
              },
              "details": {
                "type": "object",
                "additionalProperties": true
              }
            }
          }
        }
      },
      "PlanSpec": {
        "type": "object",
        "description": "Study matrix, AI surface roster, precision preset, and scope configuration.",
        "properties": {
          "roster": {
            "type": "string",
            "enum": [
              "consumer",
              "enterprise_frontier",
              "enterprise_max"
            ],
            "default": "consumer"
          },
          "surfaces": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "example": [
              "openai",
              "claude",
              "gemini",
              "perplexity",
              "google_ai_overviews"
            ]
          },
          "personas": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "example": [
              "VP of Operations",
              "Head of IT"
            ]
          },
          "situations": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "example": [
              "Replacing legacy tool",
              "Evaluating compliance fit"
            ]
          },
          "active_cells": {
            "type": "integer",
            "minimum": 0,
            "example": 9
          },
          "precision": {
            "type": "string",
            "enum": [
              "standard",
              "high_precision"
            ],
            "default": "standard"
          },
          "scope": {
            "type": "string",
            "enum": [
              "pass1_baseline",
              "recheck_pass2",
              "free_discovery"
            ],
            "default": "pass1_baseline"
          },
          "cadence": {
            "type": "string",
            "enum": [
              "one_off",
              "monthly",
              "biweekly"
            ],
            "default": "monthly"
          },
          "include_preference": {
            "type": "boolean",
            "description": "Run supplied-page Preference diagnostics (default true on Pass 1, false on Pass 2+)."
          },
          "shown_cells": {
            "type": "integer",
            "minimum": 0,
            "example": 4
          },
          "edit_tests": {
            "type": "integer",
            "minimum": 0,
            "example": 1
          }
        }
      }
    }
  },
  "paths": {
    "/me": {
      "get": {
        "operationId": "getWorkspaceMe",
        "tags": [
          "Workspace & Credits"
        ],
        "summary": "Inspect authenticated workspace and API key guardrails",
        "description": "Requires `read` scope. Returns workspace metadata, plan tier, credit balance, and active API key scopes and auto-approve limit.",
        "responses": {
          "200": {
            "description": "Authenticated workspace and API key metadata."
          },
          "401": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "403": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    },
    "/credits": {
      "get": {
        "operationId": "getWorkspaceCredits",
        "tags": [
          "Workspace & Credits"
        ],
        "summary": "Get workspace credit balance and recent job credit usage",
        "description": "Requires `read` scope. Returns total balance, pending reserved credits, available credits, auto-approve limit, and the 10 most recent jobs.",
        "responses": {
          "200": {
            "description": "Credit balance, pending reservations, and recent job history."
          },
          "401": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "403": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    },
    "/credits/estimate": {
      "post": {
        "operationId": "estimateStudyCredits",
        "tags": [
          "Workspace & Credits"
        ],
        "summary": "Deterministic pre-flight credit quote for a study plan_spec",
        "description": "Requires `read` scope. Computes exact credits (`1 credit = 1 completed graded answer` on Consumer surfaces), stage breakdown, total AI responses, and whether the run fits inside `auto_approve_credits`.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "plan_spec": {
                    "$ref": "#/components/schemas/PlanSpec"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Deterministic credit and volume quote."
          },
          "401": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "403": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    },
    "/studies": {
      "get": {
        "operationId": "listStudies",
        "tags": [
          "Studies & Diagnostics"
        ],
        "summary": "List workspace studies and latest KPI metrics",
        "description": "Requires `read` scope. Returns all studies in the workspace along with latest pass number and percentage + sample-size KPIs.",
        "responses": {
          "200": {
            "description": "List of studies with latest pass KPIs."
          },
          "401": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "403": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      },
      "post": {
        "operationId": "createStudy",
        "tags": [
          "Studies & Diagnostics"
        ],
        "summary": "Create a new study revision v1 (supports Idempotency-Key and optional queue_run)",
        "description": "Requires `write` scope (plus `run` scope when `queue_run: true`). Creates a study and revision `v1` from a Persona x Buyer Situation `plan_spec`.",
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "description": "Optional 24-hour idempotency key to prevent duplicate creation or duplicate job queues on retry.",
            "schema": {
              "type": "string",
              "example": "run-2026-11-29-pass2"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "title",
                  "slug"
                ],
                "properties": {
                  "title": {
                    "type": "string",
                    "example": "B2B Mid-Market Discovery"
                  },
                  "slug": {
                    "type": "string",
                    "pattern": "^[a-z0-9][a-z0-9-]{0,63}$",
                    "example": "mid-market-discovery"
                  },
                  "mode": {
                    "type": "string",
                    "enum": [
                      "full",
                      "discovery_free"
                    ],
                    "default": "full"
                  },
                  "schedule_cron": {
                    "type": "string",
                    "example": "0 9 1 * *"
                  },
                  "change_summary": {
                    "type": "string",
                    "example": "Initial 3x3 persona x situation baseline"
                  },
                  "plan_spec": {
                    "$ref": "#/components/schemas/PlanSpec"
                  },
                  "queue_run": {
                    "type": "boolean",
                    "default": false
                  },
                  "job_kind": {
                    "type": "string",
                    "enum": [
                      "baseline_pass",
                      "recheck_pass",
                      "experiment_run",
                      "draft_refresh",
                      "free_discovery"
                    ],
                    "default": "baseline_pass"
                  },
                  "dry_run": {
                    "type": "boolean",
                    "default": false
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Study and revision v1 created (with optional queued job and credit estimate)."
          },
          "400": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "401": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "403": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "409": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    },
    "/studies/{slug}": {
      "get": {
        "operationId": "getStudy",
        "tags": [
          "Studies & Diagnostics"
        ],
        "summary": "Get study metadata, active revision plan_spec, and runs",
        "description": "Requires `read` scope. Returns study configuration, latest revision `plan_spec`, run history, and Content Center draft count.",
        "parameters": [
          {
            "name": "slug",
            "in": "path",
            "required": true,
            "description": "Lowercase study slug within the authenticated workspace.",
            "schema": {
              "type": "string",
              "example": "core-discovery"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Study details, active revision, and pass history."
          },
          "401": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "404": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    },
    "/studies/{slug}/runs": {
      "get": {
        "operationId": "listStudyRuns",
        "tags": [
          "Studies & Diagnostics"
        ],
        "summary": "List all historical runs/passes for a study",
        "description": "Requires `read` scope. Returns immutable pass records (`Pass 1` through `Pass N`) with shipped-change markers and summary KPIs.",
        "parameters": [
          {
            "name": "slug",
            "in": "path",
            "required": true,
            "description": "Lowercase study slug within the authenticated workspace.",
            "schema": {
              "type": "string",
              "example": "core-discovery"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Historical passes for the study."
          },
          "401": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "404": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    },
    "/studies/{slug}/traces": {
      "get": {
        "operationId": "queryStudyTraces",
        "tags": [
          "Studies & Diagnostics"
        ],
        "summary": "Query graded AI buyer answer traces from latest pass",
        "description": "Requires `read` scope. Filters saved session traces by buyer situation, AI surface/agent, or diagnostic stage (`named`, `live`, `shown`, `edit`, `article`).",
        "parameters": [
          {
            "name": "slug",
            "in": "path",
            "required": true,
            "description": "Lowercase study slug within the authenticated workspace.",
            "schema": {
              "type": "string",
              "example": "core-discovery"
            }
          },
          {
            "name": "situation",
            "in": "query",
            "required": false,
            "description": "Case-insensitive substring match on scenario/situation.",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "agent",
            "in": "query",
            "required": false,
            "description": "Case-insensitive substring match on AI surface/agent label.",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "mode",
            "in": "query",
            "required": false,
            "description": "Diagnostic stage filter (`named`, `live`, `shown`, `edit`, `article`).",
            "schema": {
              "type": "string",
              "enum": [
                "named",
                "live",
                "shown",
                "edit",
                "article"
              ]
            }
          },
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "description": "Maximum traces returned (default `50`, max `200`).",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 200,
              "default": 50
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Filtered session traces and match count."
          },
          "401": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "404": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    },
    "/studies/{slug}/reservations": {
      "get": {
        "operationId": "listStudyReservations",
        "tags": [
          "Studies & Diagnostics"
        ],
        "summary": "List coded buyer reservations from latest pass",
        "description": "Requires `read` scope. Returns verbatim-anchored buyer reservations coded against the frozen study codebook.",
        "parameters": [
          {
            "name": "slug",
            "in": "path",
            "required": true,
            "description": "Lowercase study slug within the authenticated workspace.",
            "schema": {
              "type": "string",
              "example": "core-discovery"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Coded buyer reservations for the latest pass."
          },
          "401": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "404": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    },
    "/studies/{slug}/opportunities": {
      "get": {
        "operationId": "listStudyOpportunities",
        "tags": [
          "Content Center"
        ],
        "summary": "List prioritized AEO opportunities and linked copy-ready drafts",
        "description": "Requires `read` scope. Pairs prioritized search/content opportunities with current Content Center draft statuses and `[VERIFY:]` checklists.",
        "parameters": [
          {
            "name": "slug",
            "in": "path",
            "required": true,
            "description": "Lowercase study slug within the authenticated workspace.",
            "schema": {
              "type": "string",
              "example": "core-discovery"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Prioritized opportunities and linked draft metadata."
          },
          "401": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "404": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    },
    "/studies/{slug}/drafts": {
      "get": {
        "operationId": "listStudyDrafts",
        "tags": [
          "Content Center"
        ],
        "summary": "List content drafts for a study",
        "description": "Requires `read` scope. Returns full Content Center drafts including Markdown copy, metadata, `facts_to_verify`, `source_queries`, and `competing_urls`.",
        "parameters": [
          {
            "name": "slug",
            "in": "path",
            "required": true,
            "description": "Lowercase study slug within the authenticated workspace.",
            "schema": {
              "type": "string",
              "example": "core-discovery"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Content Center drafts for the study."
          },
          "401": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "404": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      },
      "post": {
        "operationId": "upsertStudyDraft",
        "tags": [
          "Content Center"
        ],
        "summary": "Create or upsert a content draft for an opportunity",
        "description": "Requires `write` scope. Creates or updates a grounded Markdown draft linked to an `opportunity_id`.",
        "parameters": [
          {
            "name": "slug",
            "in": "path",
            "required": true,
            "description": "Lowercase study slug within the authenticated workspace.",
            "schema": {
              "type": "string",
              "example": "core-discovery"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "opportunity_id",
                  "title"
                ],
                "properties": {
                  "opportunity_id": {
                    "type": "string",
                    "example": "opp-pricing-clarity"
                  },
                  "title": {
                    "type": "string",
                    "example": "Transparent Enterprise Pricing & SLA Guide"
                  },
                  "target_url_slug": {
                    "type": "string",
                    "example": "/pricing"
                  },
                  "meta_title": {
                    "type": "string"
                  },
                  "meta_description": {
                    "type": "string"
                  },
                  "draft_markdown": {
                    "type": "string"
                  },
                  "status": {
                    "type": "string",
                    "enum": [
                      "brief_only",
                      "draft",
                      "review",
                      "approved",
                      "published"
                    ]
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Draft created or updated."
          },
          "400": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "401": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "403": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "404": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    },
    "/studies/{slug}/drafts/{id}": {
      "patch": {
        "operationId": "updateStudyDraft",
        "tags": [
          "Content Center"
        ],
        "summary": "Update draft markdown, metadata, or review status",
        "description": "Requires `write` scope. Partially updates an existing draft without wiping omitted fields.",
        "parameters": [
          {
            "name": "slug",
            "in": "path",
            "required": true,
            "description": "Lowercase study slug within the authenticated workspace.",
            "schema": {
              "type": "string",
              "example": "core-discovery"
            }
          },
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Draft ID.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "title": {
                    "type": "string"
                  },
                  "target_url_slug": {
                    "type": "string"
                  },
                  "meta_title": {
                    "type": "string"
                  },
                  "meta_description": {
                    "type": "string"
                  },
                  "draft_markdown": {
                    "type": "string"
                  },
                  "status": {
                    "type": "string",
                    "enum": [
                      "brief_only",
                      "draft",
                      "review",
                      "approved",
                      "published"
                    ]
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Updated content draft."
          },
          "400": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "401": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "403": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "404": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    },
    "/jobs": {
      "get": {
        "operationId": "listJobs",
        "tags": [
          "Run Queue"
        ],
        "summary": "List study execution jobs in this workspace",
        "description": "Requires `read` scope. Lists up to 50 recent jobs with status, quoted credits, billed credits, and progress.",
        "responses": {
          "200": {
            "description": "Recent workspace execution jobs."
          },
          "401": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "403": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      },
      "post": {
        "operationId": "queueStudyJob",
        "tags": [
          "Run Queue"
        ],
        "summary": "Queue a study run or experiment (enforces auto_approve_credits guardrail)",
        "description": "Requires `run` scope. Returns `201` when queued immediately (`dry_run: true`, `dt_test_` key, or within `auto_approve_credits` and available balance) or `202` (`status: awaiting_approval`) with a 1-click portal `resolveUrl`.",
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "description": "Optional 24-hour idempotency key to prevent duplicate creation or duplicate job queues on retry.",
            "schema": {
              "type": "string",
              "example": "run-2026-11-29-pass2"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "study_slug"
                ],
                "properties": {
                  "study_slug": {
                    "type": "string",
                    "example": "core-discovery"
                  },
                  "job_kind": {
                    "type": "string",
                    "enum": [
                      "baseline_pass",
                      "recheck_pass",
                      "experiment_run",
                      "draft_refresh",
                      "free_discovery"
                    ],
                    "default": "recheck_pass"
                  },
                  "plan_spec": {
                    "$ref": "#/components/schemas/PlanSpec"
                  },
                  "dry_run": {
                    "type": "boolean",
                    "default": false
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Job queued for execution."
          },
          "202": {
            "description": "Job created in `awaiting_approval` state; open `resolveUrl` for 1-click portal approval."
          },
          "400": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "401": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "403": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "404": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "409": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    },
    "/jobs/{id}": {
      "get": {
        "operationId": "getJobStatus",
        "tags": [
          "Run Queue"
        ],
        "summary": "Check status, progress, and credit billing of a job",
        "description": "Requires `read` scope. Polls execution state (`awaiting_approval`, `queued`, `running`, `completed`, `failed`), quoted credits, and billed credits.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Study job ID.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Job status and credit billing record."
          },
          "401": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "404": {
            "description": "Structured error payload with remediation link (`resolveUrl`).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    }
  }
}
