{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "dd-flow/flow-run@3",
  "title": "dd-flow portable RUN projection",
  "description": "Compact current projection. SQLite owns runtime state and timeline.jsonl owns history.",
  "type": "object",
  "additionalProperties": true,
  "required": ["schema_id", "run_id", "project", "subject", "status", "started_at", "workspace"],
  "properties": {
    "schema_id": {"const": "dd-flow/flow-run@3"},
    "run_id": {"type": "string", "pattern": "^RUN-[0-9]+-[a-z0-9][a-z0-9-]*$"},
    "project": {"$ref": "#/definitions/identity"},
    "subject": {"$ref": "#/definitions/identity"},
    "flow_kind": {"type": "string", "minLength": 1},
    "status": {"enum": ["created", "running", "paused", "waiting_for_user", "done", "failed", "blocked", "cancelled", "discarded"]},
    "verdict": {"type": ["string", "null"]},
    "started_at": {"type": "string", "format": "date-time"},
    "finished_at": {"type": ["string", "null"], "format": "date-time"},
    "wall_clock_ms": {"type": ["integer", "null"], "minimum": 0},
    "timing_status": {"enum": ["measured", "partial", "unavailable"]},
    "workspace": {"$ref": "#/definitions/workspace"},
    "root_work_id": {"type": ["string", "null"]},
    "active_stages": {"type": "array", "items": {"type": "string", "minLength": 1}, "uniqueItems": true},
    "next_actions": {"type": "array", "items": {"type": "string", "minLength": 1}, "uniqueItems": true},
    "works": {"type": "array", "items": {"$ref": "#/definitions/work"}},
    "sessions": {"type": "array", "items": {"$ref": "#/definitions/session"}},
    "variables": {"type": "object"},
    "timeline_path": {"const": "timeline.jsonl"}
  },
  "definitions": {
    "identity": {"type": "object", "additionalProperties": false, "required": ["id", "title"], "properties": {"id": {"type": "string", "minLength": 1}, "title": {"type": "string", "minLength": 1}}},
    "workspace": {"type": "object", "additionalProperties": true, "required": ["project_root", "workspace_path", "branch", "base"], "properties": {"project_root": {"type": "string", "minLength": 1}, "workspace_path": {"type": "string", "minLength": 1}, "branch": {"type": "string", "minLength": 1}, "base": {"type": "string", "minLength": 1}}},
    "work": {"type": "object", "additionalProperties": false, "required": ["work_id", "status", "depends_on"], "properties": {"work_id": {"type": "string"}, "parent_work_id": {"type": ["string", "null"]}, "status": {"enum": ["created", "running", "completed", "failed", "cancelled"]}, "depends_on": {"type": "array", "items": {"type": "string"}}, "launch_policy": {"enum": ["reuse_allowed", "fresh_agent_required"]}, "started_at": {"type": ["string", "null"]}, "completed_at": {"type": ["string", "null"]}}},
    "session": {"type": "object", "additionalProperties": true, "required": ["session_id", "status"], "properties": {"session_id": {"type": "string", "minLength": 1}, "parent_session_id": {"type": ["string", "null"]}, "work_id": {"type": "string"}, "agent_id": {"type": ["string", "null"]}, "status": {"type": "string"}}}
  }
}
