{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://madd.sh/schemas/draft/0.2.0/contract.schema.json",
  "title": "MADD Contract Schema",
  "description": "MADD 0.2.0 candidate. Complete contracts require meta, intention, functional and tasks; fragment validation omits root required only. Status is not delivery evidence.",
  "type": "object",
  "properties": {
    "meta": {
      "type": "object",
      "description": "Contract identity and governance",
      "properties": {
        "id": {
          "type": "string",
          "format": "uuid"
        },
        "name": {
          "type": "string"
        },
        "version": {
          "type": "string",
          "pattern": "^\\d+\\.\\d+\\.\\d+$"
        },
        "status": {
          "enum": [
            "draft",
            "approved",
            "implemented",
            "verified"
          ]
        },
        "created": {
          "type": "string",
          "format": "date-time"
        },
        "updated": {
          "type": "string",
          "format": "date-time"
        },
        "authors": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "approvers": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "tags": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "cycle": {
          "type": "object",
          "description": "MADD cycle tracking \u2014 links this cycle to previous retro-specification",
          "properties": {
            "id": {
              "type": "integer",
              "minimum": 1,
              "description": "Current cycle number, starts at 1"
            },
            "previous_retro_ref": {
              "type": [
                "string",
                "null"
              ],
              "description": "Git tag or commit of the retro that started this cycle"
            }
          },
          "additionalProperties": false
        },
        "format_version": {
          "const": "0.2.0"
        },
        "owner": {
          "type": "string",
          "minLength": 1
        },
        "source_ids": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          },
          "uniqueItems": true
        },
        "sources": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "minLength": 1
              },
              "repository": {
                "type": "string",
                "format": "uri",
                "pattern": "^https://"
              },
              "revision": {
                "type": "string",
                "pattern": "^[a-f0-9]{40}([a-f0-9]{24})?$"
              }
            },
            "required": [
              "id",
              "repository",
              "revision"
            ],
            "additionalProperties": false
          }
        },
        "maddVersion": {
          "type": "string"
        },
        "release": {
          "type": "object",
          "properties": {
            "type": {
              "enum": [
                "major",
                "minor",
                "patch",
                null
              ]
            },
            "baseline_ref": {
              "type": [
                "string",
                "null"
              ]
            },
            "baseline_version": {
              "type": [
                "string",
                "null"
              ]
            }
          },
          "additionalProperties": false
        }
      },
      "required": [
        "name",
        "version",
        "status",
        "format_version",
        "owner"
      ],
      "additionalProperties": false
    },
    "intention": {
      "type": "object",
      "description": "The WHY - Business context and objectives",
      "properties": {
        "context": {
          "type": "string",
          "minLength": 1
        },
        "objectives": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^OBJ-\\d+$"
              },
              "description": {
                "type": "string",
                "minLength": 1
              },
              "success_metrics": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              }
            },
            "required": [
              "id",
              "description"
            ],
            "additionalProperties": false
          },
          "minItems": 1
        },
        "stakeholders": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "constraints": {
          "type": "object",
          "properties": {
            "budget": {
              "type": [
                "string",
                "null"
              ]
            },
            "deadline": {
              "type": [
                "string",
                "null"
              ],
              "format": "date"
            },
            "regulations": {
              "type": "array",
              "items": {
                "type": "string"
              }
            }
          },
          "additionalProperties": false
        },
        "out_of_scope": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "assumptions": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "risks": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^RISK-\\d+$"
              },
              "description": {
                "type": "string"
              },
              "probability": {
                "enum": [
                  "low",
                  "medium",
                  "high"
                ]
              },
              "impact": {
                "enum": [
                  "low",
                  "medium",
                  "high"
                ]
              },
              "mitigation": {
                "type": "string"
              }
            },
            "required": [
              "id",
              "description"
            ],
            "additionalProperties": false
          }
        }
      },
      "required": [
        "context",
        "objectives"
      ],
      "additionalProperties": false
    },
    "functional": {
      "type": "object",
      "description": "The WHAT - User-facing specifications",
      "properties": {
        "actors": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string"
              },
              "description": {
                "type": "string"
              }
            },
            "required": [
              "id",
              "description"
            ],
            "additionalProperties": false
          }
        },
        "features": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^FEAT-\\d+$"
              },
              "name": {
                "type": "string"
              },
              "description": {
                "type": "string"
              },
              "actor": {
                "type": "string"
              },
              "priority": {
                "enum": [
                  "must-have",
                  "should-have",
                  "nice-to-have"
                ]
              },
              "requirements": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              }
            },
            "required": [
              "id",
              "name",
              "description"
            ],
            "additionalProperties": false
          }
        },
        "requirements": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^REQ-F-\\d+$"
              },
              "feature": {
                "type": "string"
              },
              "description": {
                "type": "string",
                "minLength": 1
              },
              "acceptance_criteria": {
                "type": "array",
                "items": {
                  "type": "string",
                  "minLength": 1
                },
                "minItems": 1
              },
              "domains": {
                "type": "array",
                "items": {
                  "enum": [
                    "database",
                    "api",
                    "frontend",
                    "security",
                    "infrastructure"
                  ]
                },
                "description": "Technical domains this requirement touches."
              },
              "validation": {
                "type": "object",
                "properties": {
                  "method": {
                    "enum": [
                      "test",
                      "manual",
                      "automated"
                    ]
                  },
                  "test_id": {
                    "type": "string"
                  }
                },
                "required": [
                  "method",
                  "test_id"
                ],
                "additionalProperties": false
              },
              "source_ids": {
                "type": "array",
                "items": {
                  "type": "string",
                  "minLength": 1
                },
                "uniqueItems": true
              },
              "owner": {
                "type": "string",
                "minLength": 1
              },
              "scenarios": {
                "type": "array",
                "minItems": 1,
                "items": {
                  "type": "object",
                  "properties": {
                    "given": {
                      "type": "string",
                      "minLength": 1
                    },
                    "when": {
                      "type": "string",
                      "minLength": 1
                    },
                    "then": {
                      "type": "string",
                      "minLength": 1
                    }
                  },
                  "required": [
                    "given",
                    "when",
                    "then"
                  ],
                  "additionalProperties": false
                }
              }
            },
            "required": [
              "id",
              "description",
              "acceptance_criteria",
              "validation"
            ],
            "additionalProperties": false
          },
          "minItems": 1
        },
        "workflows": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^WF-\\d+$"
              },
              "name": {
                "type": "string"
              },
              "steps": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "diagram": {
                "type": "string"
              }
            },
            "required": [
              "id",
              "name",
              "steps"
            ],
            "additionalProperties": false
          }
        },
        "business_rules": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^BR-\\d+$"
              },
              "description": {
                "type": "string"
              },
              "enforcement": {
                "type": "string"
              }
            },
            "required": [
              "id",
              "description"
            ],
            "additionalProperties": false
          }
        },
        "ui_specs": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^UI-\\d+$"
              },
              "screen": {
                "type": "string"
              },
              "mockup": {
                "type": "string"
              },
              "components": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              }
            },
            "additionalProperties": false
          }
        }
      },
      "required": [
        "requirements"
      ],
      "additionalProperties": false
    },
    "technical": {
      "type": "object",
      "description": "The HOW - System specifications",
      "properties": {
        "architecture": {
          "type": "object",
          "properties": {
            "style": {
              "enum": [
                "monolith",
                "microservices",
                "serverless",
                "hybrid"
              ]
            },
            "diagram": {
              "type": "string"
            },
            "components": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "id": {
                    "type": "string",
                    "pattern": "^COMP-\\d+$"
                  },
                  "name": {
                    "type": "string"
                  },
                  "type": {
                    "enum": [
                      "service",
                      "library",
                      "database",
                      "queue",
                      "cache",
                      "external"
                    ]
                  },
                  "responsibility": {
                    "type": "string"
                  },
                  "dependencies": {
                    "type": "array",
                    "items": {
                      "type": "string"
                    }
                  }
                },
                "required": [
                  "id",
                  "name",
                  "type"
                ],
                "additionalProperties": false
              }
            },
            "decisions": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "id": {
                    "type": "string",
                    "pattern": "^ADR-\\d+$"
                  },
                  "title": {
                    "type": "string"
                  },
                  "status": {
                    "enum": [
                      "proposed",
                      "accepted",
                      "deprecated",
                      "superseded"
                    ]
                  },
                  "context": {
                    "type": "string"
                  },
                  "decision": {
                    "type": "string"
                  },
                  "consequences": {
                    "type": "string"
                  }
                },
                "required": [
                  "id",
                  "title",
                  "status",
                  "decision"
                ],
                "additionalProperties": false
              }
            }
          },
          "additionalProperties": false
        },
        "stack": {
          "type": "object",
          "properties": {
            "language": {
              "type": "string"
            },
            "runtime": {
              "type": "string"
            },
            "framework": {
              "type": "string"
            },
            "database": {
              "type": "string"
            },
            "cache": {
              "type": "string"
            },
            "packages": {
              "type": "object",
              "properties": {
                "required": {
                  "type": "array",
                  "items": {
                    "type": "string"
                  }
                },
                "forbidden": {
                  "type": "array",
                  "items": {
                    "type": "string"
                  }
                },
                "preferred": {
                  "type": "object",
                  "additionalProperties": {
                    "type": "string"
                  }
                }
              },
              "additionalProperties": false
            }
          },
          "additionalProperties": false
        },
        "data_model": {
          "type": "object",
          "properties": {
            "entities": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "name": {
                    "type": "string"
                  },
                  "table": {
                    "type": "string"
                  },
                  "fields": {
                    "type": "array",
                    "items": {
                      "type": "object",
                      "properties": {
                        "name": {
                          "type": "string"
                        },
                        "type": {
                          "type": "string"
                        },
                        "primary": {
                          "type": "boolean"
                        },
                        "unique": {
                          "type": "boolean"
                        },
                        "nullable": {
                          "type": "boolean"
                        },
                        "index": {
                          "type": "boolean"
                        },
                        "default": {
                          "type": [
                            "string",
                            "number",
                            "boolean",
                            "null"
                          ]
                        }
                      },
                      "required": [
                        "name",
                        "type"
                      ],
                      "additionalProperties": false
                    }
                  },
                  "indexes": {
                    "type": "array",
                    "items": {
                      "type": "string"
                    }
                  },
                  "constraints": {
                    "type": "array",
                    "items": {
                      "type": "string"
                    }
                  }
                },
                "required": [
                  "name",
                  "fields"
                ],
                "additionalProperties": false
              }
            },
            "relationships": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "from": {
                    "type": "string"
                  },
                  "to": {
                    "type": "string"
                  },
                  "type": {
                    "enum": [
                      "one-to-one",
                      "one-to-many",
                      "many-to-many"
                    ]
                  }
                },
                "additionalProperties": false
              }
            }
          },
          "additionalProperties": false
        },
        "api": {
          "type": "object",
          "properties": {
            "style": {
              "enum": [
                "rest",
                "graphql",
                "grpc",
                "websocket"
              ]
            },
            "base_path": {
              "type": "string"
            },
            "auth_method": {
              "type": "string"
            },
            "endpoints": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "id": {
                    "type": "string",
                    "pattern": "^API-\\d+$"
                  },
                  "method": {
                    "enum": [
                      "GET",
                      "POST",
                      "PUT",
                      "PATCH",
                      "DELETE"
                    ]
                  },
                  "path": {
                    "type": "string"
                  },
                  "description": {
                    "type": "string"
                  },
                  "request": {
                    "type": "object",
                    "properties": {
                      "params": {
                        "type": "object",
                        "additionalProperties": {
                          "$ref": "#/$defs/jsonValue"
                        }
                      },
                      "query": {
                        "type": "object",
                        "additionalProperties": {
                          "$ref": "#/$defs/jsonValue"
                        }
                      },
                      "body": {
                        "type": "object",
                        "additionalProperties": {
                          "$ref": "#/$defs/jsonValue"
                        }
                      }
                    },
                    "additionalProperties": false
                  },
                  "responses": {
                    "type": "object",
                    "additionalProperties": {
                      "$ref": "#/$defs/jsonValue"
                    }
                  },
                  "requirements": {
                    "type": "array",
                    "items": {
                      "type": "string"
                    }
                  }
                },
                "required": [
                  "id",
                  "method",
                  "path"
                ],
                "additionalProperties": false
              }
            },
            "conventions": {
              "type": "object",
              "properties": {
                "naming": {
                  "type": "string"
                },
                "errors": {
                  "type": "object",
                  "additionalProperties": {
                    "$ref": "#/$defs/jsonValue"
                  }
                },
                "pagination": {
                  "type": "object",
                  "additionalProperties": {
                    "$ref": "#/$defs/jsonValue"
                  }
                }
              },
              "additionalProperties": false
            }
          },
          "additionalProperties": false
        },
        "nfr": {
          "type": "object",
          "description": "Non-functional requirements",
          "properties": {
            "performance": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "id": {
                    "type": "string",
                    "pattern": "^REQ-NF-\\d+$"
                  },
                  "metric": {
                    "type": "string",
                    "minLength": 1
                  },
                  "target": {
                    "type": "string",
                    "minLength": 1
                  },
                  "measurement": {
                    "type": "string"
                  },
                  "validation": {
                    "type": "object",
                    "properties": {
                      "method": {
                        "enum": [
                          "test",
                          "manual",
                          "automated"
                        ]
                      },
                      "test_id": {
                        "type": "string"
                      }
                    },
                    "required": [
                      "method",
                      "test_id"
                    ],
                    "additionalProperties": false
                  },
                  "source_ids": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "minLength": 1
                    },
                    "uniqueItems": true
                  },
                  "scenarios": {
                    "type": "array",
                    "minItems": 1,
                    "items": {
                      "type": "object",
                      "properties": {
                        "given": {
                          "type": "string",
                          "minLength": 1
                        },
                        "when": {
                          "type": "string",
                          "minLength": 1
                        },
                        "then": {
                          "type": "string",
                          "minLength": 1
                        }
                      },
                      "required": [
                        "given",
                        "when",
                        "then"
                      ],
                      "additionalProperties": false
                    }
                  }
                },
                "required": [
                  "id",
                  "validation",
                  "metric",
                  "target"
                ],
                "additionalProperties": false
              }
            },
            "security": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "id": {
                    "type": "string",
                    "pattern": "^REQ-NF-\\d+$"
                  },
                  "requirement": {
                    "type": "string",
                    "minLength": 1
                  },
                  "owasp": {
                    "type": "string"
                  },
                  "validation": {
                    "type": "object",
                    "properties": {
                      "method": {
                        "enum": [
                          "test",
                          "manual",
                          "automated"
                        ]
                      },
                      "test_id": {
                        "type": "string"
                      }
                    },
                    "required": [
                      "method",
                      "test_id"
                    ],
                    "additionalProperties": false
                  },
                  "source_ids": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "minLength": 1
                    },
                    "uniqueItems": true
                  },
                  "scenarios": {
                    "type": "array",
                    "minItems": 1,
                    "items": {
                      "type": "object",
                      "properties": {
                        "given": {
                          "type": "string",
                          "minLength": 1
                        },
                        "when": {
                          "type": "string",
                          "minLength": 1
                        },
                        "then": {
                          "type": "string",
                          "minLength": 1
                        }
                      },
                      "required": [
                        "given",
                        "when",
                        "then"
                      ],
                      "additionalProperties": false
                    }
                  }
                },
                "required": [
                  "id",
                  "validation",
                  "requirement"
                ],
                "additionalProperties": false
              }
            },
            "reliability": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "id": {
                    "type": "string",
                    "pattern": "^REQ-NF-\\d+$"
                  },
                  "metric": {
                    "type": "string",
                    "minLength": 1
                  },
                  "target": {
                    "type": "string",
                    "minLength": 1
                  },
                  "validation": {
                    "type": "object",
                    "properties": {
                      "method": {
                        "enum": [
                          "test",
                          "manual",
                          "automated"
                        ]
                      },
                      "test_id": {
                        "type": "string"
                      }
                    },
                    "required": [
                      "method",
                      "test_id"
                    ],
                    "additionalProperties": false
                  },
                  "source_ids": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "minLength": 1
                    },
                    "uniqueItems": true
                  },
                  "scenarios": {
                    "type": "array",
                    "minItems": 1,
                    "items": {
                      "type": "object",
                      "properties": {
                        "given": {
                          "type": "string",
                          "minLength": 1
                        },
                        "when": {
                          "type": "string",
                          "minLength": 1
                        },
                        "then": {
                          "type": "string",
                          "minLength": 1
                        }
                      },
                      "required": [
                        "given",
                        "when",
                        "then"
                      ],
                      "additionalProperties": false
                    }
                  }
                },
                "required": [
                  "id",
                  "validation",
                  "metric",
                  "target"
                ],
                "additionalProperties": false
              }
            },
            "scalability": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "id": {
                    "type": "string",
                    "pattern": "^REQ-NF-\\d+$"
                  },
                  "requirement": {
                    "type": "string",
                    "minLength": 1
                  },
                  "constraints": {
                    "type": "string"
                  },
                  "validation": {
                    "type": "object",
                    "properties": {
                      "method": {
                        "enum": [
                          "test",
                          "manual",
                          "automated"
                        ]
                      },
                      "test_id": {
                        "type": "string"
                      }
                    },
                    "required": [
                      "method",
                      "test_id"
                    ],
                    "additionalProperties": false
                  },
                  "source_ids": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "minLength": 1
                    },
                    "uniqueItems": true
                  },
                  "scenarios": {
                    "type": "array",
                    "minItems": 1,
                    "items": {
                      "type": "object",
                      "properties": {
                        "given": {
                          "type": "string",
                          "minLength": 1
                        },
                        "when": {
                          "type": "string",
                          "minLength": 1
                        },
                        "then": {
                          "type": "string",
                          "minLength": 1
                        }
                      },
                      "required": [
                        "given",
                        "when",
                        "then"
                      ],
                      "additionalProperties": false
                    }
                  }
                },
                "required": [
                  "id",
                  "validation",
                  "requirement"
                ],
                "additionalProperties": false
              }
            }
          },
          "additionalProperties": false
        }
      },
      "additionalProperties": false
    },
    "tasks": {
      "type": "object",
      "description": "BUILD - Execution plan",
      "properties": {
        "phases": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^PHASE-\\d+$"
              },
              "name": {
                "type": "string"
              },
              "description": {
                "type": "string"
              },
              "order": {
                "type": "integer"
              },
              "category": {
                "enum": [
                  "foundation",
                  "core",
                  "feature",
                  "polish"
                ],
                "description": "Phase type \u2014 Principle 5: foundations must complete before features"
              },
              "tasks": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              }
            },
            "required": [
              "id",
              "name",
              "order",
              "category"
            ],
            "additionalProperties": false
          }
        },
        "items": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^TASK-\\d+$"
              },
              "title": {
                "type": "string"
              },
              "description": {
                "type": "string"
              },
              "phase": {
                "type": "string"
              },
              "requirements": {
                "type": "array",
                "items": {
                  "type": "string"
                },
                "minItems": 1
              },
              "dependencies": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "acceptance": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "fraction_hint": {
                "type": "string",
                "description": "Label grouping related tasks into a fraction for incremental dev\u2192ci\u2192audit cycles"
              },
              "domains": {
                "type": "array",
                "items": {
                  "enum": [
                    "database",
                    "api",
                    "frontend",
                    "security",
                    "infrastructure"
                  ]
                },
                "description": "Technical domains this task involves. Used by Conductor to instantiate domain-specific Maker/Breaker pairs."
              },
              "status": {
                "enum": [
                  "pending",
                  "in_progress",
                  "done",
                  "blocked"
                ]
              },
              "assignee": {
                "type": "string"
              },
              "source_ids": {
                "type": "array",
                "items": {
                  "type": "string",
                  "minLength": 1
                },
                "uniqueItems": true
              },
              "repository": {
                "type": "string",
                "minLength": 1
              },
              "owner": {
                "type": "string",
                "minLength": 1
              },
              "deliverable": {
                "type": "string",
                "minLength": 1
              },
              "done_when": {
                "type": "string",
                "minLength": 1
              },
              "risk": {
                "enum": [
                  "low",
                  "medium",
                  "high"
                ]
              }
            },
            "required": [
              "id",
              "title",
              "status",
              "requirements"
            ],
            "additionalProperties": false
          },
          "minItems": 1
        },
        "tests": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^TEST-\\d+$"
              },
              "type": {
                "enum": [
                  "unit",
                  "integration",
                  "e2e",
                  "performance",
                  "security",
                  "contract",
                  "build",
                  "recovery",
                  "manual"
                ]
              },
              "requirement": {
                "type": "string"
              },
              "description": {
                "type": "string"
              },
              "steps": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "status": {
                "enum": [
                  "pending",
                  "passing",
                  "failing",
                  "skipped"
                ]
              },
              "binding": {
                "enum": [
                  "planned",
                  "bound"
                ]
              },
              "ref": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "source_ids": {
                "type": "array",
                "items": {
                  "type": "string",
                  "minLength": 1
                },
                "uniqueItems": true
              }
            },
            "required": [
              "id",
              "type",
              "description",
              "requirement",
              "binding",
              "ref"
            ],
            "allOf": [
              {
                "if": {
                  "properties": {
                    "binding": {
                      "const": "bound"
                    }
                  }
                },
                "then": {
                  "properties": {
                    "ref": {
                      "type": "string",
                      "minLength": 1
                    }
                  }
                },
                "else": {
                  "properties": {
                    "ref": {
                      "type": "null"
                    }
                  }
                }
              }
            ],
            "additionalProperties": false
          },
          "minItems": 1
        }
      },
      "required": [
        "items",
        "tests"
      ],
      "additionalProperties": false
    },
    "operations": {
      "type": "object",
      "description": "RUN - Deployment and operations",
      "properties": {
        "environments": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "name": {
                "type": "string"
              },
              "url": {
                "type": "string",
                "format": "uri"
              },
              "region": {
                "type": "string"
              }
            },
            "required": [
              "name"
            ],
            "additionalProperties": false
          }
        },
        "infrastructure": {
          "type": "object",
          "properties": {
            "provider": {
              "enum": [
                "aws",
                "gcp",
                "azure",
                "self-hosted",
                "hybrid"
              ]
            },
            "compute": {
              "type": "object",
              "additionalProperties": {
                "$ref": "#/$defs/jsonValue"
              }
            },
            "database": {
              "type": "object",
              "additionalProperties": {
                "$ref": "#/$defs/jsonValue"
              }
            },
            "networking": {
              "type": "object",
              "additionalProperties": {
                "$ref": "#/$defs/jsonValue"
              }
            }
          },
          "additionalProperties": false
        },
        "deployment": {
          "type": "object",
          "properties": {
            "strategy": {
              "enum": [
                "rolling",
                "blue-green",
                "canary",
                "recreate"
              ]
            },
            "pipeline": {
              "type": "string"
            },
            "stages": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "rollback": {
              "type": "object",
              "additionalProperties": {
                "$ref": "#/$defs/jsonValue"
              }
            }
          },
          "additionalProperties": false
        },
        "configuration": {
          "type": "object",
          "properties": {
            "secrets": {
              "type": "array",
              "items": {
                "type": "object",
                "additionalProperties": {
                  "$ref": "#/$defs/jsonValue"
                }
              }
            },
            "env_vars": {
              "type": "array",
              "items": {
                "type": "object",
                "additionalProperties": {
                  "$ref": "#/$defs/jsonValue"
                }
              }
            }
          },
          "additionalProperties": false
        },
        "monitoring": {
          "type": "object",
          "properties": {
            "logging": {
              "type": "object",
              "additionalProperties": {
                "$ref": "#/$defs/jsonValue"
              }
            },
            "metrics": {
              "type": "object",
              "additionalProperties": {
                "$ref": "#/$defs/jsonValue"
              }
            },
            "alerting": {
              "type": "array",
              "items": {
                "type": "object",
                "additionalProperties": {
                  "$ref": "#/$defs/jsonValue"
                }
              }
            },
            "slo": {
              "type": "array",
              "items": {
                "type": "object",
                "additionalProperties": {
                  "$ref": "#/$defs/jsonValue"
                }
              }
            }
          },
          "additionalProperties": false
        },
        "runbooks": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^RB-\\d+$"
              },
              "title": {
                "type": "string"
              },
              "symptoms": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "steps": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              }
            },
            "required": [
              "id",
              "title",
              "steps"
            ],
            "additionalProperties": false
          }
        }
      },
      "additionalProperties": false
    },
    "retro": {
      "type": "object",
      "description": "POST - Reality documentation by SCRIBE",
      "properties": {
        "generated_at": {
          "type": "string",
          "format": "date-time"
        },
        "generated_by": {
          "type": "string"
        },
        "implementation": {
          "type": "object",
          "properties": {
            "components": {
              "type": "array",
              "items": {
                "type": "object",
                "additionalProperties": {
                  "$ref": "#/$defs/jsonValue"
                }
              }
            },
            "endpoints": {
              "type": "array",
              "items": {
                "type": "object",
                "additionalProperties": {
                  "$ref": "#/$defs/jsonValue"
                }
              }
            }
          },
          "additionalProperties": false
        },
        "coverage": {
          "type": "object",
          "properties": {
            "requirements": {
              "type": "object",
              "additionalProperties": {
                "$ref": "#/$defs/jsonValue"
              }
            },
            "tests": {
              "type": "object",
              "additionalProperties": {
                "$ref": "#/$defs/jsonValue"
              }
            }
          },
          "additionalProperties": false
        },
        "debt": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^DEBT-\\d+$"
              },
              "type": {
                "enum": [
                  "todo",
                  "fixme",
                  "hack",
                  "workaround",
                  "deprecated"
                ]
              },
              "location": {
                "type": "string"
              },
              "description": {
                "type": "string"
              },
              "impact": {
                "enum": [
                  "low",
                  "medium",
                  "high"
                ]
              },
              "effort": {
                "enum": [
                  "small",
                  "medium",
                  "large"
                ]
              },
              "source_rec": {
                "type": [
                  "string",
                  "null"
                ],
                "pattern": "^REC-\\d+$",
                "description": "Original recommendation ID if converted from audit finding"
              }
            },
            "required": [
              "id",
              "type",
              "location",
              "description"
            ],
            "additionalProperties": false
          }
        },
        "gaps": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "intended": {
                "type": "string"
              },
              "actual": {
                "type": "string"
              },
              "reason": {
                "type": "string"
              }
            },
            "additionalProperties": false
          }
        },
        "changelog": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "version": {
                "type": "string"
              },
              "date": {
                "type": "string",
                "format": "date"
              },
              "added": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "changed": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "fixed": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "removed": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "security": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              }
            },
            "required": [
              "version",
              "date"
            ],
            "additionalProperties": false
          }
        },
        "fractions": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "minLength": 1
              },
              "status": {
                "enum": [
                  "approved",
                  "conditional",
                  "rejected"
                ]
              },
              "iterations_used": {
                "type": "integer",
                "minimum": 1
              },
              "tasks": {
                "type": "array",
                "items": {
                  "type": "string",
                  "minLength": 1
                },
                "uniqueItems": true
              },
              "requirements_covered": {
                "type": "array",
                "items": {
                  "type": "string",
                  "minLength": 1
                },
                "uniqueItems": true
              },
              "debt": {
                "type": "array",
                "items": {
                  "type": "string",
                  "minLength": 1
                },
                "uniqueItems": true
              }
            },
            "required": [
              "id",
              "status"
            ],
            "additionalProperties": false
          }
        }
      },
      "additionalProperties": false
    },
    "audit_cycle": {
      "type": "object",
      "description": "AUDIT - Iteration tracking and prioritized recommendations",
      "properties": {
        "current_iteration": {
          "type": "integer",
          "minimum": 0
        },
        "max_iterations": {
          "type": "integer",
          "minimum": 1,
          "default": 3
        },
        "release_type": {
          "type": [
            "string",
            "null"
          ],
          "enum": [
            "major",
            "minor",
            "patch",
            null
          ]
        },
        "scope": {
          "type": [
            "string",
            "null"
          ],
          "enum": [
            "full",
            "feature",
            "targeted",
            null
          ]
        },
        "baseline_ref": {
          "type": [
            "string",
            "null"
          ]
        },
        "started_at": {
          "type": [
            "string",
            "null"
          ],
          "format": "date-time"
        },
        "status": {
          "enum": [
            "pending",
            "in_progress",
            "complete"
          ]
        },
        "scope_details": {
          "type": "object",
          "properties": {
            "changed_files": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "impacted_requirements": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "impacted_components": {
              "type": "array",
              "items": {
                "type": "string"
              }
            }
          },
          "additionalProperties": false
        },
        "history": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "iteration": {
                "type": "integer"
              },
              "verdict": {
                "enum": [
                  "APPROVED",
                  "CHANGES_REQUIRED",
                  "REJECTED"
                ]
              },
              "date": {
                "type": "string",
                "format": "date-time"
              },
              "summary": {
                "type": "string"
              }
            },
            "required": [
              "iteration",
              "verdict",
              "date"
            ],
            "additionalProperties": false
          }
        },
        "fractions": {
          "type": "array",
          "description": "Fraction-based decomposition for incremental dev\u2192ci\u2192audit cycles",
          "items": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^FRAC-\\d+$"
              },
              "name": {
                "type": "string"
              },
              "tasks": {
                "type": "array",
                "items": {
                  "type": "string"
                },
                "description": "TASK-xxx IDs in this fraction"
              },
              "requirements": {
                "type": "array",
                "items": {
                  "type": "string"
                },
                "description": "REQ-F-xxx IDs covered by this fraction"
              },
              "phase_category": {
                "enum": [
                  "foundation",
                  "core",
                  "feature",
                  "polish"
                ]
              },
              "max_iterations": {
                "type": "integer",
                "minimum": 1,
                "maximum": 5
              },
              "current_iteration": {
                "type": "integer",
                "minimum": 0
              },
              "status": {
                "enum": [
                  "pending",
                  "in_progress",
                  "approved",
                  "conditional",
                  "rejected"
                ]
              },
              "history": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "iteration": {
                      "type": "integer"
                    },
                    "verdict": {
                      "enum": [
                        "CI_PASS",
                        "CI_FAIL",
                        "APPROVED",
                        "CHANGES_REQUIRED",
                        "REJECTED"
                      ]
                    },
                    "date": {
                      "type": "string",
                      "format": "date-time"
                    }
                  },
                  "additionalProperties": false
                }
              }
            },
            "required": [
              "id",
              "tasks",
              "status"
            ],
            "additionalProperties": false
          }
        },
        "recommendations": {
          "type": "array",
          "description": "Prioritized audit recommendations tracked across iterations",
          "items": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^REC-\\d+$"
              },
              "severity": {
                "enum": [
                  "blocker",
                  "major",
                  "minor",
                  "observation"
                ]
              },
              "priority": {
                "type": "integer",
                "minimum": 1,
                "description": "Computed priority (1=highest)"
              },
              "category": {
                "enum": [
                  "security",
                  "compliance",
                  "quality",
                  "performance",
                  "architecture"
                ]
              },
              "status": {
                "enum": [
                  "open",
                  "in_progress",
                  "resolved",
                  "deferred",
                  "wont_fix"
                ]
              },
              "title": {
                "type": "string"
              },
              "description": {
                "type": "string"
              },
              "location": {
                "type": "string",
                "description": "File path and line number"
              },
              "related_requirements": {
                "type": "array",
                "items": {
                  "type": "string"
                },
                "description": "REQ-F-xxx or REQ-NF-xxx IDs"
              },
              "related_components": {
                "type": "array",
                "items": {
                  "type": "string"
                },
                "description": "COMP-xxx IDs"
              },
              "confidence": {
                "type": "integer",
                "minimum": 0,
                "maximum": 100,
                "description": "Audit confidence score (0-100)"
              },
              "fraction": {
                "type": "string",
                "pattern": "^FRAC-\\d+$",
                "description": "Fraction this recommendation belongs to"
              },
              "introduced_in_iteration": {
                "type": "integer"
              },
              "resolved_in_iteration": {
                "type": [
                  "integer",
                  "null"
                ]
              },
              "resolution": {
                "type": [
                  "string",
                  "null"
                ],
                "description": "How the recommendation was addressed"
              },
              "created_at": {
                "type": "string",
                "format": "date-time"
              },
              "updated_at": {
                "type": "string",
                "format": "date-time"
              }
            },
            "required": [
              "id",
              "severity",
              "priority",
              "category",
              "status",
              "title",
              "description",
              "introduced_in_iteration",
              "created_at"
            ],
            "additionalProperties": false
          }
        }
      },
      "required": [
        "current_iteration",
        "max_iterations",
        "status",
        "history",
        "recommendations"
      ],
      "additionalProperties": false
    },
    "$schema": {
      "enum": [
        "../contract.schema.json",
        "https://madd.sh/schemas/draft/0.2.0/contract.schema.json"
      ]
    }
  },
  "required": [
    "meta",
    "intention",
    "functional",
    "tasks"
  ],
  "additionalProperties": false,
  "$defs": {
    "jsonValue": {
      "anyOf": [
        {
          "type": [
            "string",
            "number",
            "boolean",
            "null"
          ]
        },
        {
          "type": "array",
          "items": {
            "$ref": "#/$defs/jsonValue"
          }
        },
        {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/$defs/jsonValue"
          }
        }
      ]
    }
  }
}
