{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://cortex-agent.local/schemas/dispatch/daemon-state.schema.json",
  "title": "Cortex Agent Dispatch Daemon State",
  "description": "Recoverable local state for a future opt-in user-space daemon. Phase 0 does not create this file.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "enabled",
    "status",
    "agent_root",
    "pid",
    "poll_interval_ms",
    "started_at",
    "last_heartbeat_at",
    "stopped_at",
    "last_trigger_id",
    "last_dispatch_run_id",
    "last_error"
  ],
  "properties": {
    "schema_version": { "const": 1 },
    "enabled": { "type": "boolean", "default": false },
    "status": { "enum": ["disabled", "starting", "running", "stopping", "stopped", "degraded"] },
    "agent_root": { "type": "string", "minLength": 1 },
    "pid": {
      "anyOf": [
        { "type": "null" },
        { "type": "integer", "minimum": 1 }
      ]
    },
    "poll_interval_ms": { "type": "integer", "minimum": 1000 },
    "started_at": { "$ref": "#/$defs/nullableDateTime" },
    "last_heartbeat_at": { "$ref": "#/$defs/nullableDateTime" },
    "stopped_at": { "$ref": "#/$defs/nullableDateTime" },
    "last_trigger_id": { "type": ["string", "null"] },
    "last_dispatch_run_id": { "type": ["string", "null"] },
    "last_error": {
      "anyOf": [
        { "type": "null" },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["code", "message", "at"],
          "properties": {
            "code": { "type": "string", "minLength": 1 },
            "message": { "type": "string", "minLength": 1 },
            "at": { "type": "string", "format": "date-time" }
          }
        }
      ]
    }
  },
  "$defs": {
    "nullableDateTime": {
      "anyOf": [
        { "type": "null" },
        { "type": "string", "format": "date-time" }
      ]
    }
  }
}
