{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$defs": {
    "modelOrdering": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "generation",
        "releaseDate",
        "capabilityRank",
        "checkedAt",
        "status",
        "sources",
        "note",
        "capabilityNote"
      ],
      "properties": {
        "generation": {
          "type": "array",
          "items": { "type": "integer", "minimum": 0 }
        },
        "releaseDate": {
          "type": ["string", "null"],
          "pattern": "^[0-9]{4}-(?:0[1-9]|1[0-2])(?:-(?:0[1-9]|[12][0-9]|3[01]))?$"
        },
        "capabilityRank": { "type": ["integer", "null"], "minimum": 0 },
        "checkedAt": {
          "type": "string",
          "pattern": "^[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])$"
        },
        "status": {
          "enum": ["verified", "partial", "unresolved", "not-applicable"]
        },
        "sources": {
          "type": "array",
          "uniqueItems": true,
          "items": { "type": "string", "pattern": "^https://[^\\s]+$" }
        },
        "note": { "type": "string", "minLength": 1 },
        "capabilityNote": { "type": "string", "minLength": 1 }
      },
      "allOf": [
        {
          "if": {
            "properties": { "status": { "enum": ["verified", "partial"] } }
          },
          "then": {
            "properties": {
              "generation": { "type": "array", "minItems": 1 },
              "releaseDate": { "type": "string" },
              "sources": { "type": "array", "minItems": 1 }
            }
          }
        },
        {
          "if": {
            "properties": {
              "status": { "enum": ["unresolved", "not-applicable"] }
            }
          },
          "then": {
            "properties": {
              "generation": { "type": "array", "maxItems": 0 },
              "releaseDate": { "type": "null" },
              "capabilityRank": { "type": "null" }
            }
          }
        },
        {
          "if": {
            "properties": { "capabilityRank": { "type": "integer" } },
            "required": ["capabilityRank"]
          },
          "then": {
            "properties": { "sources": { "type": "array", "minItems": 1 } }
          }
        }
      ]
    },
    "slug": {
      "type": "string",
      "pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*$"
    },
    "path": {
      "type": "string",
      "minLength": 1,
      "pattern": "^(?!/)(?!.*(?:^|/)\\.\\.(?:/|$))(?!.*\\\\).+$"
    },
    "billing": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "provider",
        "plan",
        "amount",
        "unit",
        "batchId",
        "caseCount",
        "note"
      ],
      "properties": {
        "type": {
          "enum": ["subscription", "batch", "run"]
        },
        "provider": {
          "type": ["string", "null"]
        },
        "plan": {
          "type": ["string", "null"]
        },
        "amount": {
          "type": ["number", "null"],
          "minimum": 0
        },
        "unit": {
          "type": ["string", "null"]
        },
        "sourceAmount": {
          "type": ["number", "null"],
          "minimum": 0
        },
        "sourceUnit": {
          "type": ["string", "null"]
        },
        "conversionRate": {
          "type": ["object", "null"],
          "additionalProperties": false,
          "required": ["amount", "unit", "perAmount", "perUnit"],
          "properties": {
            "amount": {
              "type": "number",
              "exclusiveMinimum": 0
            },
            "unit": {
              "type": "string"
            },
            "perAmount": {
              "type": "number",
              "exclusiveMinimum": 0
            },
            "perUnit": {
              "type": "string"
            }
          }
        },
        "batchId": {
          "type": ["string", "null"],
          "pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*$"
        },
        "caseCount": {
          "type": ["integer", "null"],
          "minimum": 1
        },
        "note": {
          "type": "string"
        },
        "duration": {
          "type": ["object", "null"],
          "additionalProperties": false,
          "required": ["seconds", "text"],
          "properties": {
            "seconds": {
              "type": ["integer", "null"],
              "minimum": 0
            },
            "text": {
              "type": "string",
              "minLength": 1
            }
          }
        }
      }
    },
    "scoreDimensions": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "taskCoverage",
        "composition",
        "readability",
        "interactionClarity",
        "craft"
      ],
      "properties": {
        "taskCoverage": { "type": "integer", "minimum": 0, "maximum": 40 },
        "composition": { "type": "integer", "minimum": 0, "maximum": 20 },
        "readability": { "type": "integer", "minimum": 0, "maximum": 15 },
        "interactionClarity": {
          "type": "integer",
          "minimum": 0,
          "maximum": 10
        },
        "craft": { "type": "integer", "minimum": 0, "maximum": 15 }
      }
    },
    "scoreTaskChecks": {
      "type": "object",
      "minProperties": 8,
      "maxProperties": 8,
      "additionalProperties": {
        "type": "integer",
        "minimum": 0,
        "maximum": 5
      }
    },
    "caseScore": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "rubricVersion",
        "status",
        "total",
        "dimensions",
        "taskChecks",
        "captureState",
        "ceiling",
        "assessedAt",
        "inputHash",
        "criteriaHash",
        "screenshotHash",
        "evidence",
        "visualChecks",
        "observations",
        "confidence",
        "limitations",
        "captureManifest",
        "evidenceSetHash",
        "captureInventoryHash",
        "rubricHash",
        "reviewedFrames"
      ],
      "properties": {
        "rubricVersion": {
          "const": 3
        },
        "status": {
          "enum": ["pending", "rated", "insufficient", "unavailable"]
        },
        "total": {
          "type": ["integer", "null"],
          "minimum": 0,
          "maximum": 100
        },
        "dimensions": {
          "anyOf": [
            {
              "$ref": "#/$defs/scoreDimensions"
            },
            {
              "type": "null"
            }
          ]
        },
        "taskChecks": {
          "anyOf": [
            {
              "$ref": "#/$defs/scoreTaskChecks"
            },
            {
              "type": "null"
            }
          ]
        },
        "captureState": {
          "enum": [
            "result",
            "initial",
            "partial",
            "blocked",
            "undemonstrated",
            null
          ]
        },
        "ceiling": {
          "type": ["integer", "null"],
          "minimum": 0,
          "maximum": 100
        },
        "inputHash": {
          "type": ["string", "null"],
          "pattern": "^[a-f0-9]{64}$"
        },
        "criteriaHash": {
          "type": ["string", "null"],
          "pattern": "^[a-f0-9]{64}$"
        },
        "assessedAt": {
          "type": ["string", "null"],
          "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
        },
        "screenshotHash": {
          "type": ["string", "null"],
          "pattern": "^[a-f0-9]{64}$"
        },
        "evidence": {
          "type": "string",
          "minLength": 12
        },
        "visualChecks": {
          "anyOf": [
            {
              "$ref": "#/$defs/scoreVisualChecks"
            },
            {
              "type": "null"
            }
          ]
        },
        "observations": {
          "anyOf": [
            {
              "$ref": "#/$defs/scoreObservations"
            },
            {
              "type": "null"
            }
          ]
        },
        "confidence": {
          "enum": ["high", "medium", "low", null]
        },
        "limitations": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 8
          }
        },
        "captureManifest": {
          "type": ["string", "null"],
          "pattern": "^catalog/screenshot-captures/[^/]+/[^/]+\\.json$"
        },
        "evidenceSetHash": {
          "type": ["string", "null"],
          "pattern": "^[a-f0-9]{64}$"
        },
        "captureInventoryHash": {
          "type": ["string", "null"],
          "pattern": "^[a-f0-9]{64}$"
        },
        "rubricHash": {
          "type": ["string", "null"],
          "pattern": "^[a-f0-9]{64}$"
        },
        "reviewedFrames": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "minLength": 1
          }
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "status": {
                "const": "rated"
              }
            }
          },
          "then": {
            "properties": {
              "total": {
                "type": "integer",
                "minimum": 0,
                "maximum": 100
              },
              "dimensions": {
                "$ref": "#/$defs/scoreDimensions"
              },
              "taskChecks": {
                "$ref": "#/$defs/scoreTaskChecks"
              },
              "captureState": {
                "enum": ["result", "initial", "partial", "blocked"]
              },
              "ceiling": {
                "type": "integer"
              },
              "inputHash": {
                "type": "string"
              },
              "criteriaHash": {
                "type": "string"
              },
              "assessedAt": {
                "type": "string",
                "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
              },
              "screenshotHash": {
                "type": "string",
                "pattern": "^[a-f0-9]{64}$"
              },
              "visualChecks": {
                "$ref": "#/$defs/scoreVisualChecks"
              },
              "rubricHash": {
                "type": "string"
              },
              "evidenceSetHash": {
                "type": "string"
              },
              "captureInventoryHash": {
                "type": "string"
              },
              "captureManifest": {
                "type": "string"
              },
              "observations": {
                "$ref": "#/$defs/scoreObservations"
              },
              "confidence": {
                "enum": ["high", "medium", "low"]
              },
              "limitations": {
                "type": "array",
                "minItems": 1
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "status": {
                "const": "insufficient"
              }
            }
          },
          "then": {
            "properties": {
              "total": {
                "type": "null"
              },
              "dimensions": {
                "type": "null"
              },
              "taskChecks": {
                "type": "null"
              },
              "captureState": {
                "const": "undemonstrated"
              },
              "ceiling": {
                "type": "null"
              },
              "assessedAt": {
                "type": "string"
              },
              "inputHash": {
                "type": "string"
              },
              "screenshotHash": {
                "type": "string"
              },
              "criteriaHash": {
                "type": "string"
              },
              "visualChecks": {
                "type": "null"
              },
              "rubricHash": {
                "type": "string"
              },
              "evidenceSetHash": {
                "type": "string"
              },
              "captureInventoryHash": {
                "type": "string"
              },
              "captureManifest": {
                "type": "string"
              },
              "observations": {
                "$ref": "#/$defs/scoreObservations"
              },
              "confidence": {
                "enum": ["high", "medium", "low"]
              },
              "limitations": {
                "type": "array",
                "minItems": 1
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "status": {
                "const": "unavailable"
              }
            }
          },
          "then": {
            "properties": {
              "total": {
                "type": "null"
              },
              "dimensions": {
                "type": "null"
              },
              "taskChecks": {
                "type": "null"
              },
              "captureState": {
                "type": "null"
              },
              "ceiling": {
                "type": "null"
              },
              "inputHash": {
                "type": "string"
              },
              "criteriaHash": {
                "type": "string"
              },
              "assessedAt": {
                "type": "string",
                "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
              },
              "screenshotHash": {
                "type": "null"
              },
              "visualChecks": {
                "type": "null"
              },
              "rubricHash": {
                "type": "string"
              },
              "evidenceSetHash": {
                "type": "string"
              },
              "captureInventoryHash": {
                "type": "string"
              },
              "captureManifest": {
                "type": "string"
              },
              "observations": {
                "$ref": "#/$defs/scoreObservations"
              },
              "confidence": {
                "enum": ["high", "medium", "low"]
              },
              "limitations": {
                "type": "array",
                "minItems": 1
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "status": {
                "const": "pending"
              }
            }
          },
          "then": {
            "properties": {
              "total": {
                "type": "null"
              },
              "dimensions": {
                "type": "null"
              },
              "taskChecks": {
                "type": "null"
              },
              "captureState": {
                "type": "null"
              },
              "ceiling": {
                "type": "null"
              },
              "inputHash": {
                "type": "null"
              },
              "criteriaHash": {
                "type": "null"
              },
              "assessedAt": {
                "type": "null"
              },
              "screenshotHash": {
                "type": "null"
              },
              "visualChecks": {
                "type": "null"
              }
            }
          }
        }
      ]
    },
    "topic": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "schemaVersion",
        "id",
        "title",
        "description",
        "category",
        "tags"
      ],
      "properties": {
        "schemaVersion": {
          "const": 1
        },
        "id": {
          "$ref": "#/$defs/slug"
        },
        "title": {
          "type": "string",
          "minLength": 1
        },
        "description": {
          "type": "string"
        },
        "category": {
          "$ref": "#/$defs/slug"
        },
        "thumbnail": {
          "type": "object",
          "additionalProperties": false,
          "required": ["path", "sourceRunId"],
          "properties": {
            "path": {
              "$ref": "#/$defs/path"
            },
            "sourceRunId": {
              "type": "string",
              "minLength": 1
            }
          }
        },
        "testingStatus": {
          "enum": ["active", "archived"]
        },
        "testingHistory": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["status", "date", "reason"],
            "properties": {
              "status": {
                "enum": ["active", "archived"]
              },
              "date": {
                "type": "string",
                "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
              },
              "reason": {
                "type": "string",
                "minLength": 1,
                "maxLength": 1000
              }
            }
          }
        },
        "tags": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "uniqueItems": true
        },
        "complexity": {
          "type": ["object", "null"],
          "additionalProperties": false,
          "required": ["level", "reason"],
          "properties": {
            "level": {
              "type": "integer",
              "minimum": 1,
              "maximum": 5
            },
            "reason": {
              "type": "string",
              "minLength": 1,
              "maxLength": 1000
            }
          }
        }
      },
      "allOf": [
        {
          "if": {
            "required": ["testingStatus"],
            "properties": {
              "testingStatus": {
                "const": "archived"
              }
            }
          },
          "then": {
            "required": ["testingHistory"],
            "properties": {
              "testingHistory": {
                "type": "array",
                "minItems": 1
              }
            }
          }
        }
      ]
    },
    "prompt": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "schemaVersion",
        "id",
        "topicId",
        "title",
        "previousId",
        "source",
        "extraction",
        "hash"
      ],
      "properties": {
        "schemaVersion": {
          "const": 1
        },
        "id": {
          "$ref": "#/$defs/slug"
        },
        "topicId": {
          "$ref": "#/$defs/slug"
        },
        "title": {
          "type": "string"
        },
        "previousId": {
          "type": ["string", "null"]
        },
        "source": {
          "$ref": "#/$defs/path"
        },
        "extraction": {
          "type": "string"
        },
        "hash": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$"
        }
      }
    },
    "run": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "schemaVersion",
        "id",
        "runId",
        "topicId",
        "promptId",
        "title",
        "modelId",
        "effort",
        "date",
        "description",
        "type",
        "status",
        "stack",
        "tags",
        "input",
        "environment",
        "changes",
        "preview",
        "score",
        "build",
        "links"
      ],
      "properties": {
        "schemaVersion": {
          "const": 1
        },
        "id": {
          "type": "string",
          "pattern": "^[a-z0-9-]+--[a-z0-9-]+$"
        },
        "runId": {
          "$ref": "#/$defs/slug"
        },
        "topicId": {
          "$ref": "#/$defs/slug"
        },
        "promptId": {
          "$ref": "#/$defs/slug"
        },
        "title": {
          "type": "string",
          "minLength": 1
        },
        "modelId": {
          "$ref": "#/$defs/slug"
        },
        "effort": {
          "$ref": "#/$defs/slug"
        },
        "date": {
          "anyOf": [
            {
              "type": "null"
            },
            {
              "type": "string",
              "pattern": "^\\d{4}(-\\d{2}(-\\d{2})?)?$"
            }
          ]
        },
        "description": {
          "type": "string"
        },
        "type": {
          "enum": [
            "single-html",
            "prototype",
            "app",
            "fullstack",
            "prompt",
            "other"
          ]
        },
        "status": {
          "enum": ["draft", "ready", "archived"]
        },
        "stack": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "tags": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "changes": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "verification": {
          "type": "object",
          "additionalProperties": false,
          "required": ["reviewedAt", "environment", "checks", "unverified"],
          "properties": {
            "reviewedAt": {
              "type": "string",
              "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
            },
            "environment": { "type": "string", "minLength": 1 },
            "unverified": { "type": "string", "minLength": 1 },
            "checks": {
              "type": "array",
              "minItems": 1,
              "items": {
                "type": "object",
                "additionalProperties": false,
                "required": ["label", "status"],
                "properties": {
                  "label": { "type": "string", "minLength": 1 },
                  "status": { "enum": ["passed", "failed", "untested"] },
                  "evidence": {
                    "type": "string",
                    "pattern": "^(?!.*(?:^|/)\\.\\.(?:/|$))[a-zA-Z0-9_.-]+(?:/[a-zA-Z0-9_.-]+)+$"
                  }
                },
                "allOf": [
                  {
                    "if": {
                      "properties": {
                        "status": { "enum": ["passed", "failed"] }
                      }
                    },
                    "then": {
                      "properties": { "evidence": {} },
                      "required": ["evidence"]
                    }
                  }
                ]
              }
            }
          }
        },
        "input": {
          "type": "object",
          "additionalProperties": false,
          "required": ["file", "hash", "completeness", "notes"],
          "properties": {
            "file": {
              "$ref": "#/$defs/path"
            },
            "hash": {
              "type": "string",
              "pattern": "^[a-f0-9]{64}$"
            },
            "completeness": {
              "enum": ["partial", "recorded", "unknown"]
            },
            "executionMode": {
              "enum": ["design-enhanced", "strict", "unknown"]
            },
            "executionModeNote": {
              "type": "string"
            },
            "authorization": {
              "type": "string"
            },
            "notes": {
              "type": "string"
            }
          }
        },
        "environment": {
          "type": "object",
          "additionalProperties": false,
          "required": ["tool", "notes"],
          "properties": {
            "toolId": {
              "type": ["string", "null"],
              "pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*$"
            },
            "tool": {
              "type": ["string", "null"]
            },
            "notes": {
              "type": "string"
            }
          }
        },
        "preview": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "kind",
            "pages",
            "defaultPage",
            "network",
            "embed",
            "externalUrl"
          ],
          "properties": {
            "kind": {
              "enum": ["static", "build", "external", "none"]
            },
            "network": {
              "enum": ["offline", "required", "unknown"]
            },
            "embed": {
              "type": "boolean"
            },
            "externalUrl": {
              "type": ["string", "null"]
            },
            "defaultPage": {
              "type": ["string", "null"]
            },
            "pages": {
              "type": "array",
              "items": {
                "type": "object",
                "additionalProperties": false,
                "required": ["id", "label", "path"],
                "properties": {
                  "id": {
                    "$ref": "#/$defs/slug"
                  },
                  "label": {
                    "type": "string"
                  },
                  "path": {
                    "$ref": "#/$defs/path"
                  }
                }
              }
            }
          }
        },
        "screenshot": {
          "$ref": "#/$defs/path"
        },
        "score": {
          "$ref": "#/$defs/caseScore"
        },
        "screenshotCapturedAt": {
          "type": ["string", "null"],
          "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
        },
        "archivedAt": {
          "type": ["string", "null"],
          "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
        },
        "sourceRunId": { "type": ["string", "null"], "minLength": 1 },
        "billing": {
          "anyOf": [
            { "$ref": "#/$defs/billing" },
            {
              "type": "object",
              "additionalProperties": false,
              "required": ["type", "batchId"],
              "properties": {
                "type": { "const": "batch" },
                "batchId": { "$ref": "#/$defs/slug" }
              }
            }
          ]
        },
        "build": {
          "anyOf": [
            {
              "type": "null"
            },
            {
              "type": "object",
              "additionalProperties": false,
              "required": ["command", "output"],
              "properties": {
                "command": {
                  "type": "array",
                  "minItems": 2,
                  "items": {
                    "type": "string"
                  }
                },
                "output": {
                  "$ref": "#/$defs/path"
                }
              }
            }
          ]
        },
        "links": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["label", "href"],
            "properties": {
              "label": {
                "type": "string"
              },
              "href": {
                "type": "string",
                "pattern": "^https://"
              }
            }
          }
        }
      }
    },
    "scoreVisualChecks": {
      "type": "object",
      "minProperties": 12,
      "maxProperties": 12,
      "additionalProperties": {
        "type": "integer",
        "minimum": 0,
        "maximum": 5
      }
    },
    "scoreObservations": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "taskCoverage",
        "composition",
        "readability",
        "interactionClarity",
        "craft"
      ],
      "properties": {
        "taskCoverage": {
          "type": "string",
          "minLength": 8
        },
        "composition": {
          "type": "string",
          "minLength": 8
        },
        "readability": {
          "type": "string",
          "minLength": 8
        },
        "interactionClarity": {
          "type": "string",
          "minLength": 8
        },
        "craft": {
          "type": "string",
          "minLength": 8
        }
      }
    }
  }
}
