{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/multi-agent-ecosystem/multi-agent-pipeline/pipeline/schemas/ledger.schema.json",
  "title": "Office ledger",
  "description": "What GET /v1/ledger returns and `office-ledger.mjs --json` prints: tokens and estimated USD for today, this week (from Monday) and this month, in local time, broken down by run, repository, model and phase. A run's numbers come from its tracker-state.json when the tracker carries tokens, else from the usage records of the transcripts tied to it; a run with neither is not-measured, and its usd and tokens are null, never 0. Sessions no run claims are counted apart as outside the pipeline. Prices come from cost-table.json; a cache write is priced at 1.25 times input. Every amount names its source.",
  "type": "object",
  "additionalProperties": false,
  "required": ["contractVersion", "generatedAt", "prices", "windows"],
  "properties": {
    "contractVersion": {
      "type": "string",
      "pattern": "^1\\.[0-9]+\\.[0-9]+$",
      "x-sensitivity": "none"
    },
    "generatedAt": {
      "type": "string",
      "format": "date-time",
      "x-sensitivity": "none"
    },
    "prices": {
      "type": "object",
      "additionalProperties": false,
      "required": ["table", "verifiedAt", "ageDays", "stale", "warning"],
      "x-sensitivity": "none",
      "properties": {
        "table": {
          "const": "cost-table.json"
        },
        "verifiedAt": {
          "type": ["string", "null"]
        },
        "ageDays": {
          "type": ["integer", "null"]
        },
        "stale": {
          "type": "boolean"
        },
        "warning": {
          "type": ["string", "null"]
        }
      }
    },
    "windows": {
      "type": "object",
      "additionalProperties": false,
      "required": ["today", "week", "month"],
      "x-sensitivity": "none",
      "properties": {
        "today": {
          "$ref": "#/$defs/window"
        },
        "week": {
          "$ref": "#/$defs/window"
        },
        "month": {
          "$ref": "#/$defs/window"
        }
      }
    }
  },
  "$defs": {
    "source": {
      "enum": ["tracker-state", "transcript", "mixed", "not-measured"],
      "description": "tracker-state: the pipeline's own ledger; transcript: derived from the host's usage records; mixed: a sum of both kinds; not-measured: nothing could be read, so the numbers are null."
    },
    "tokens": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["in", "out", "cacheRead", "cacheWrite"],
          "properties": {
            "in": {
              "type": "integer",
              "minimum": 0
            },
            "out": {
              "type": "integer",
              "minimum": 0
            },
            "cacheRead": {
              "type": "integer",
              "minimum": 0
            },
            "cacheWrite": {
              "type": "integer",
              "minimum": 0
            }
          }
        },
        {
          "type": "null"
        }
      ]
    },
    "amount": {
      "type": "object",
      "additionalProperties": false,
      "required": ["tokens", "usd", "source", "unpriced"],
      "properties": {
        "tokens": {
          "$ref": "#/$defs/tokens"
        },
        "usd": {
          "type": ["number", "null"]
        },
        "source": {
          "$ref": "#/$defs/source"
        },
        "unpriced": {
          "type": "integer",
          "minimum": 0,
          "description": "Usage records whose model has no price; their tokens are counted, their cost is not."
        }
      }
    },
    "row": {
      "type": "object",
      "additionalProperties": false,
      "required": ["key", "tokens", "usd", "source", "unpriced"],
      "properties": {
        "key": {
          "type": "string"
        },
        "repo": {
          "type": ["string", "null"],
          "description": "On byRun rows: the run repository."
        },
        "tokens": {
          "$ref": "#/$defs/tokens"
        },
        "usd": {
          "type": ["number", "null"]
        },
        "source": {
          "$ref": "#/$defs/source"
        },
        "unpriced": {
          "type": "integer",
          "minimum": 0
        }
      }
    },
    "window": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "since",
        "total",
        "outsidePipeline",
        "notMeasured",
        "byRun",
        "byRepo",
        "byModel",
        "byPhase"
      ],
      "properties": {
        "since": {
          "type": "string",
          "format": "date-time"
        },
        "total": {
          "$ref": "#/$defs/amount"
        },
        "outsidePipeline": {
          "$ref": "#/$defs/amount"
        },
        "notMeasured": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "description": "Task ids of runs active in the window whose cost could not be measured."
        },
        "byRun": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/row"
          }
        },
        "byRepo": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/row"
          }
        },
        "byModel": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/row"
          }
        },
        "byPhase": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/row"
          }
        }
      }
    }
  }
}
