{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://genesiscode.dev/schemas/roadmap-execution-manifest-v0.1.json",
  "title": "GenesisCode Roadmap Execution Manifest v0.1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "kind",
    "version",
    "audit_date",
    "authority",
    "input_identities",
    "summary",
    "ready_task_ids",
    "tasks"
  ],
  "properties": {
    "kind": {
      "const": "genesis/roadmap-execution-manifest-v0.1"
    },
    "version": {
      "const": "0.1"
    },
    "audit_date": {
      "type": "string",
      "format": "date"
    },
    "authority": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "roadmap",
        "policy",
        "schema",
        "completion_rule",
        "manifest_can_authorize_completion"
      ],
      "properties": {
        "roadmap": {"const": "ROADMAP.md"},
        "policy": {"const": "policies/roadmap_execution_v0.1.json"},
        "schema": {"const": "docs/spec/ROADMAP_EXECUTION_MANIFEST_v0.1.schema.json"},
        "completion_rule": {"type": "string", "minLength": 1},
        "manifest_can_authorize_completion": {"const": false}
      }
    },
    "input_identities": {
      "type": "object",
      "additionalProperties": false,
      "required": ["roadmap_sha256", "policy_sha256", "schema_sha256"],
      "properties": {
        "roadmap_sha256": {"$ref": "#/$defs/sha256"},
        "policy_sha256": {"$ref": "#/$defs/sha256"},
        "schema_sha256": {"$ref": "#/$defs/sha256"}
      }
    },
    "summary": {
      "type": "object",
      "additionalProperties": false,
      "required": ["task_count", "completed_count", "open_count", "ready_count"],
      "properties": {
        "task_count": {"type": "integer", "minimum": 1},
        "completed_count": {"type": "integer", "minimum": 0},
        "open_count": {"type": "integer", "minimum": 0},
        "ready_count": {"type": "integer", "minimum": 0}
      }
    },
    "ready_task_ids": {
      "$ref": "#/$defs/taskIdArray"
    },
    "tasks": {
      "type": "array",
      "minItems": 1,
      "items": {"$ref": "#/$defs/task"}
    }
  },
  "$defs": {
    "sha256": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "taskId": {
      "type": "string",
      "pattern": "^(R[0-9]+\\.[0-9]+\\.[a-z]|F[0-9]+\\.[a-z])$"
    },
    "taskIdArray": {
      "type": "array",
      "uniqueItems": true,
      "items": {"$ref": "#/$defs/taskId"}
    },
    "nonEmptyStringArray": {
      "type": "array",
      "minItems": 1,
      "uniqueItems": true,
      "items": {"type": "string", "minLength": 1}
    },
    "stringArray": {
      "type": "array",
      "uniqueItems": true,
      "items": {"type": "string", "minLength": 1}
    },
    "evidence": {
      "type": "object",
      "additionalProperties": false,
      "required": ["completed_date", "commands", "input_identity"],
      "properties": {
        "completed_date": {"type": "string", "format": "date"},
        "commands": {"$ref": "#/$defs/nonEmptyStringArray"},
        "input_identity": {
          "type": "string",
          "pattern": "^[a-z0-9-]+-sha256:[0-9a-f]{64}$"
        }
      }
    },
    "task": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "phase",
        "workstream",
        "source",
        "state",
        "title",
        "objective",
        "prerequisites",
        "unsatisfied_prerequisites",
        "start_ready",
        "risk_class",
        "resource_class",
        "owner_paths",
        "guard_checks",
        "parallel_safe_with",
        "negative_controls",
        "expected_inputs",
        "expected_outputs",
        "rollback",
        "acceptance"
      ],
      "properties": {
        "id": {"$ref": "#/$defs/taskId"},
        "phase": {
          "type": "string",
          "pattern": "^(R[0-9]+|F[0-9]+)$"
        },
        "workstream": {
          "type": "string",
          "pattern": "^(R[0-9]+\\.[0-9]+|F[0-9]+)$"
        },
        "source": {
          "type": "object",
          "additionalProperties": false,
          "required": ["path", "line"],
          "properties": {
            "path": {"const": "ROADMAP.md"},
            "line": {"type": "integer", "minimum": 1}
          }
        },
        "state": {"enum": ["open", "done"]},
        "title": {"type": "string", "minLength": 1},
        "objective": {"type": "string", "minLength": 1},
        "prerequisites": {"$ref": "#/$defs/taskIdArray"},
        "unsatisfied_prerequisites": {"$ref": "#/$defs/taskIdArray"},
        "start_ready": {"type": "boolean"},
        "risk_class": {"enum": ["low", "medium", "high", "critical"]},
        "resource_class": {
          "enum": ["static", "build", "benchmark", "proof", "release", "research"]
        },
        "owner_paths": {"$ref": "#/$defs/nonEmptyStringArray"},
        "guard_checks": {"$ref": "#/$defs/nonEmptyStringArray"},
        "parallel_safe_with": {"$ref": "#/$defs/stringArray"},
        "negative_controls": {"$ref": "#/$defs/nonEmptyStringArray"},
        "expected_inputs": {
          "type": "object",
          "additionalProperties": false,
          "required": ["prerequisite_task_ids", "owner_paths"],
          "properties": {
            "prerequisite_task_ids": {"$ref": "#/$defs/taskIdArray"},
            "owner_paths": {"$ref": "#/$defs/nonEmptyStringArray"}
          }
        },
        "expected_outputs": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "deliverable",
            "declared_artifacts",
            "durable_evidence_required",
            "mutable_e0_sufficient"
          ],
          "properties": {
            "deliverable": {"type": "string", "minLength": 1},
            "declared_artifacts": {"$ref": "#/$defs/stringArray"},
            "durable_evidence_required": {"const": true},
            "mutable_e0_sufficient": {"const": false}
          }
        },
        "rollback": {
          "type": "object",
          "additionalProperties": false,
          "required": ["automatic", "strategy", "preserve_failed_evidence"],
          "properties": {
            "automatic": {"const": false},
            "strategy": {"type": "string", "minLength": 1},
            "preserve_failed_evidence": {"type": "boolean"}
          }
        },
        "acceptance": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "status",
            "evidence",
            "independent_verification_required",
            "manifest_can_authorize_completion"
          ],
          "properties": {
            "status": {"enum": ["required", "satisfied"]},
            "evidence": {
              "oneOf": [
                {"type": "null"},
                {"$ref": "#/$defs/evidence"}
              ]
            },
            "independent_verification_required": {"type": "boolean"},
            "manifest_can_authorize_completion": {"const": false}
          }
        }
      },
      "allOf": [
        {
          "if": {"properties": {"state": {"const": "done"}}},
          "then": {
            "properties": {
              "start_ready": {"const": false},
              "unsatisfied_prerequisites": {"maxItems": 0},
              "acceptance": {
                "properties": {
                  "status": {"const": "satisfied"},
                  "evidence": {"$ref": "#/$defs/evidence"}
                }
              }
            }
          },
          "else": {
            "properties": {
              "acceptance": {
                "properties": {
                  "status": {"const": "required"},
                  "evidence": {"type": "null"}
                }
              }
            }
          }
        }
      ]
    }
  }
}
