{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://standards.rootblocks.com/schemas/recipes/structured-project-output.v1.schema.json",
  "title": "RootBlocks Structured Project Output v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "recipeId",
    "recipeVersion",
    "profile",
    "language",
    "app",
    "applicationIr",
    "outputStatus",
    "projects",
    "projectReferences",
    "generatedFiles",
    "workflowBindings",
    "packages",
    "tests",
    "sourceMaps",
    "evidence",
    "delegations",
    "agentFallback",
    "validationGates"
  ],
  "properties": {
    "schemaVersion": {
      "const": "rootblocks.structured-project-output/v1"
    },
    "recipeId": {
      "$ref": "#/$defs/id"
    },
    "recipeVersion": {
      "$ref": "#/$defs/semver"
    },
    "profile": {
      "$ref": "#/$defs/id"
    },
    "language": {
      "enum": ["csharp", "typescript", "javascript", "python", "java", "kotlin", "go", "rust", "php", "ruby", "custom"]
    },
    "app": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "targetRuntime", "targetFramework", "host"],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1
        },
        "targetRuntime": {
          "type": "string",
          "minLength": 1
        },
        "targetFramework": {
          "type": "string",
          "minLength": 1
        },
        "host": {
          "enum": ["api", "worker", "console", "library", "custom"]
        }
      }
    },
    "outputStatus": {
      "enum": ["valid", "invalid"]
    },
    "applicationIr": {
      "$ref": "#/$defs/applicationIr"
    },
    "projects": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/project"
      }
    },
    "projectReferences": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/projectReference"
      }
    },
    "generatedFiles": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/generatedFile"
      }
    },
    "workflowBindings": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/workflowBinding"
      }
    },
    "packages": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/packageReference"
      }
    },
    "tests": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/test"
      }
    },
    "sourceMaps": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/sourceMap"
      }
    },
    "evidence": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/evidence"
      }
    },
    "delegations": {
      "type": "array",
      "minItems": 6,
      "items": {
        "$ref": "#/$defs/delegation"
      }
    },
    "agentFallback": {
      "$ref": "#/$defs/agentFallback"
    },
    "validationGates": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/validationGate"
      }
    }
  },
  "$defs": {
    "id": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
    },
    "semver": {
      "type": "string",
      "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(-[A-Za-z0-9.-]+)?(\\+[A-Za-z0-9.-]+)?$"
    },
    "path": {
      "type": "string",
      "minLength": 1,
      "pattern": "^[A-Za-z0-9_./<>*-]+$"
    },
    "layer": {
      "enum": ["solution", "domain", "application", "infrastructure", "workflows", "host", "test", "builderx", "docs", "infra", "cicd", "evidence", "artifact"]
    },
    "applicationIr": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "sections", "sourceArtifacts"],
      "properties": {
        "path": {
          "$ref": "#/$defs/path"
        },
        "sections": {
          "type": "array",
          "minItems": 16,
          "items": {
            "enum": [
              "appIdentity",
              "languageRuntime",
              "architectureProfile",
              "boundedContexts",
              "modules",
              "domainModel",
              "useCases",
              "ports",
              "adapters",
              "workflowBindings",
              "workflowNodeOperations",
              "applicationOperations",
              "domainPolicies",
              "hostEntrypoints",
              "tests",
              "infrastructureLinks",
              "deliveryLinks",
              "sourceMapPlan",
              "evidencePlan"
            ]
          },
          "uniqueItems": true,
          "allOf": [
            { "contains": { "const": "appIdentity" } },
            { "contains": { "const": "languageRuntime" } },
            { "contains": { "const": "architectureProfile" } },
            { "contains": { "const": "domainModel" } },
            { "contains": { "const": "useCases" } },
            { "contains": { "const": "ports" } },
            { "contains": { "const": "adapters" } },
            { "contains": { "const": "workflowBindings" } },
            { "contains": { "const": "workflowNodeOperations" } },
            { "contains": { "const": "applicationOperations" } },
            { "contains": { "const": "domainPolicies" } },
            { "contains": { "const": "tests" } },
            { "contains": { "const": "sourceMapPlan" } },
            { "contains": { "const": "evidencePlan" } }
          ]
        },
        "sourceArtifacts": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/path"
          },
          "uniqueItems": true
        }
      }
    },
    "project": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "layer", "kind", "path", "allowedReferences", "forbiddenReferences", "packagePolicy"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "layer": {
          "$ref": "#/$defs/layer"
        },
        "kind": {
          "enum": ["production", "test"]
        },
        "path": {
          "$ref": "#/$defs/path"
        },
        "allowedReferences": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        },
        "forbiddenReferences": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        },
        "packagePolicy": {
          "type": "object",
          "additionalProperties": false,
          "required": ["allowedFamilies", "forbiddenFamilies"],
          "properties": {
            "allowedFamilies": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/id"
              },
              "uniqueItems": true
            },
            "forbiddenFamilies": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/id"
              },
              "uniqueItems": true
            }
          }
        }
      }
    },
    "projectReference": {
      "type": "object",
      "additionalProperties": false,
      "required": ["from", "to", "reason"],
      "properties": {
        "from": {
          "$ref": "#/$defs/id"
        },
        "to": {
          "$ref": "#/$defs/id"
        },
        "reason": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "generatedFile": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "layer", "purpose", "generatedByPhase", "sourceMapRequired", "protectedZone"],
      "properties": {
        "path": {
          "$ref": "#/$defs/path"
        },
        "layer": {
          "$ref": "#/$defs/layer"
        },
        "purpose": {
          "type": "string",
          "minLength": 1
        },
        "generatedByPhase": {
          "$ref": "#/$defs/id"
        },
        "sourceMapRequired": {
          "type": "boolean"
        },
        "protectedZone": {
          "type": "boolean"
        }
      }
    },
    "test": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "type", "path", "covers", "required"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "type": {
          "enum": [
            "unit",
            "application",
            "contract",
            "integration",
            "e2e",
            "workflow-node",
            "workflow-regression",
            "architecture",
            "security",
            "static-analysis",
            "performance-load",
            "smoke"
          ]
        },
        "path": {
          "$ref": "#/$defs/path"
        },
        "covers": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        },
        "required": {
          "type": "boolean"
        },
        "risk": {
          "enum": ["low", "medium", "high", "critical"]
        },
        "publicContract": {
          "type": "boolean"
        },
        "sourceMapId": {
          "$ref": "#/$defs/id"
        },
        "evidencePath": {
          "$ref": "#/$defs/path"
        },
        "assertionTypes": {
          "type": "array",
          "items": {
            "enum": ["state", "path", "output", "contract", "dependency", "security", "performance", "evidence"]
          },
          "uniqueItems": true
        }
      }
    },
    "sourceMap": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "path", "covers"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "path": {
          "$ref": "#/$defs/path"
        },
        "covers": {
          "type": "array",
          "items": {
            "enum": ["spec", "construction", "workflow-node", "workflow-binding", "application-operation", "use-case", "domain-policy", "adapter", "endpoint", "generated-source", "test", "evidence", "validation-gate"]
          },
          "uniqueItems": true
        }
      }
    },
    "workflowBinding": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "workflowId",
        "flowPath",
        "definitionPath",
        "nodeOperationsPath",
        "variablesPath",
        "contextPath",
        "sourceMapPath",
        "applicationOperations",
        "domainPolicies",
        "infrastructureAccess",
        "sourceMapRequired"
      ],
      "properties": {
        "workflowId": {
          "$ref": "#/$defs/id"
        },
        "flowPath": {
          "$ref": "#/$defs/path"
        },
        "definitionPath": {
          "$ref": "#/$defs/path"
        },
        "nodeOperationsPath": {
          "$ref": "#/$defs/path"
        },
        "variablesPath": {
          "$ref": "#/$defs/path"
        },
        "contextPath": {
          "$ref": "#/$defs/path"
        },
        "sourceMapPath": {
          "$ref": "#/$defs/path"
        },
        "applicationOperations": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        },
        "domainPolicies": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        },
        "infrastructureAccess": {
          "enum": ["none", "through-application-ports", "direct"]
        },
        "sourceMapRequired": {
          "type": "boolean"
        }
      }
    },
    "packageReference": {
      "type": "object",
      "additionalProperties": false,
      "required": ["projectId", "family", "packageId", "ownerLayer"],
      "properties": {
        "projectId": {
          "$ref": "#/$defs/id"
        },
        "family": {
          "$ref": "#/$defs/id"
        },
        "packageId": {
          "type": "string",
          "minLength": 1
        },
        "ownerLayer": {
          "$ref": "#/$defs/layer"
        }
      }
    },
    "evidence": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "events"],
      "properties": {
        "path": {
          "$ref": "#/$defs/path"
        },
        "events": {
          "type": "array",
          "items": {
            "enum": ["validated", "generated", "compiled", "tested", "regression-run", "packaged", "failed"]
          },
          "uniqueItems": true
        }
      }
    },
    "validationGate": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "status", "blocksRelease", "details"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "status": {
          "enum": ["pass", "fail", "warn"]
        },
        "blocksRelease": {
          "type": "boolean"
        },
        "details": {
          "type": "string"
        }
      }
    },
    "delegation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["domain", "targetRef", "required"],
      "properties": {
        "domain": {
          "enum": ["iac", "cicd", "test", "component-pack", "source-map", "evidence"]
        },
        "targetRef": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9./-]{2,127}$"
        },
        "required": {
          "type": "boolean"
        }
      }
    },
    "agentFallback": {
      "type": "object",
      "additionalProperties": false,
      "required": ["generationModes", "protectedZones", "customZones", "builderxValidationRequired", "missingCriticalBehavior"],
      "properties": {
        "generationModes": {
          "type": "array",
          "minItems": 1,
          "items": {
            "enum": ["builderx-compiler", "agent-codegen-governed", "hybrid"]
          },
          "uniqueItems": true
        },
        "protectedZones": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/path"
          },
          "uniqueItems": true
        },
        "customZones": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/path"
          },
          "uniqueItems": true
        },
        "builderxValidationRequired": {
          "type": "boolean"
        },
        "missingCriticalBehavior": {
          "enum": ["block", "manual-review", "warn"]
        }
      }
    }
  }
}
