{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Grid Budget",
  "description": "Schema for .grid/budget.json - tracks spending and enforces budget limits for Grid missions.",
  "type": "object",
  "properties": {
    "limit": {
      "type": "number",
      "minimum": 0,
      "description": "Budget limit in USD. 0 means unlimited."
    },
    "total_spent": {
      "type": "number",
      "minimum": 0,
      "description": "Total amount spent in USD across all spawns"
    },
    "remaining": {
      "type": "number",
      "description": "Remaining budget in USD (limit - total_spent). Null if unlimited."
    },
    "currency": {
      "type": "string",
      "default": "USD",
      "description": "Currency for budget tracking"
    },
    "mission": {
      "type": "string",
      "description": "Mission this budget applies to"
    },
    "started_at": {
      "type": "string",
      "format": "date-time",
      "description": "When budget tracking started"
    },
    "last_updated": {
      "type": "string",
      "format": "date-time",
      "description": "Last time budget was updated"
    },
    "spawns": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["agent", "timestamp"],
        "properties": {
          "id": {
            "type": "string",
            "description": "Unique spawn identifier"
          },
          "agent": {
            "type": "string",
            "description": "Agent type that was spawned (e.g., 'grid-executor')"
          },
          "model": {
            "type": "string",
            "description": "Model used for this spawn (e.g., 'claude-sonnet-4-20250514')"
          },
          "cost": {
            "type": "number",
            "minimum": 0,
            "description": "Cost of this spawn in USD"
          },
          "input_tokens": {
            "type": "integer",
            "minimum": 0,
            "description": "Number of input tokens used"
          },
          "output_tokens": {
            "type": "integer",
            "minimum": 0,
            "description": "Number of output tokens generated"
          },
          "timestamp": {
            "type": "string",
            "format": "date-time",
            "description": "When the spawn occurred"
          },
          "duration_ms": {
            "type": "integer",
            "minimum": 0,
            "description": "Duration of the spawn in milliseconds"
          },
          "block": {
            "type": "string",
            "description": "Block ID this spawn was for"
          },
          "thread": {
            "type": "string",
            "description": "Thread ID this spawn was for"
          },
          "status": {
            "type": "string",
            "enum": ["completed", "failed", "active"],
            "description": "Status of this spawn"
          }
        }
      },
      "description": "List of all program spawns with cost tracking"
    },
    "summary": {
      "type": "object",
      "properties": {
        "total_spawns": {
          "type": "integer",
          "minimum": 0
        },
        "completed_spawns": {
          "type": "integer",
          "minimum": 0
        },
        "failed_spawns": {
          "type": "integer",
          "minimum": 0
        },
        "total_input_tokens": {
          "type": "integer",
          "minimum": 0
        },
        "total_output_tokens": {
          "type": "integer",
          "minimum": 0
        },
        "average_cost_per_spawn": {
          "type": "number",
          "minimum": 0
        },
        "cost_by_agent": {
          "type": "object",
          "additionalProperties": {
            "type": "number"
          },
          "description": "Cost breakdown by agent type"
        },
        "cost_by_model": {
          "type": "object",
          "additionalProperties": {
            "type": "number"
          },
          "description": "Cost breakdown by model"
        }
      },
      "description": "Summary statistics for budget reporting"
    },
    "alerts": {
      "type": "object",
      "properties": {
        "warn_at_percentage": {
          "type": "number",
          "minimum": 0,
          "maximum": 100,
          "default": 80,
          "description": "Percentage of budget to trigger warning"
        },
        "pause_at_percentage": {
          "type": "number",
          "minimum": 0,
          "maximum": 100,
          "default": 100,
          "description": "Percentage of budget to pause execution"
        },
        "warnings_sent": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "percentage": { "type": "number" },
              "timestamp": { "type": "string", "format": "date-time" },
              "message": { "type": "string" }
            }
          }
        }
      },
      "description": "Budget alert configuration and history"
    }
  }
}
