{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://raw.githubusercontent.com/realpkuasule/harness-automation/main/docs/api/harness-policy-v2.schema.json",
  "title": "Harness Engineering Policy v2",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "project",
    "sources",
    "agents",
    "policies"
  ],
  "properties": {
    "schemaVersion": {
      "const": "2.0"
    },
    "compiler": {
      "$ref": "#/$defs/compilerIdentity"
    },
    "typescriptNamingBaseline": {
      "$ref": "#/$defs/typescriptNamingBaseline"
    },
    "evaluations": {
      "$ref": "#/$defs/evaluationSnapshot"
    },
    "project": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "owner", "phase", "stacks"],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1
        },
        "owner": {
          "type": "string",
          "minLength": 1
        },
        "phase": {
          "enum": ["design-approved", "development", "maintenance"]
        },
        "profile": {
          "enum": ["full-typescript", "python-data-ai", "go-performance", "custom"]
        },
        "stacks": {
          "type": "array",
          "minItems": 1,
          "maxItems": 16,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "minLength": 1,
            "maxLength": 64,
            "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$"
          }
        },
        "deliveryProfiles": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "enum": ["worktree-delivery"]
          }
        },
        "domainProfiles": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "enum": ["game-development"]
          }
        },
        "qualityProfiles": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "enum": ["eval-driven-development"]
          }
        }
      }
    },
    "sources": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["id", "kind", "path", "sha256", "approved"],
        "properties": {
          "id": {
            "type": "string",
            "pattern": "^[a-z][a-z0-9-]*$"
          },
          "kind": {
            "enum": ["prd", "design", "research", "eval", "existing-policy"]
          },
          "path": {
            "type": "string",
            "minLength": 1
          },
          "sha256": {
            "type": "string",
            "pattern": "^[a-f0-9]{64}$"
          },
          "approved": {
            "type": "boolean"
          }
        }
      }
    },
    "agents": {
      "type": "object",
      "additionalProperties": false,
      "required": ["portableInstructionFile", "adapters"],
      "properties": {
        "portableInstructionFile": {
          "const": "AGENTS.md"
        },
        "adapters": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["id", "capabilities"],
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^[a-z][a-z0-9-]*$"
              },
              "capabilities": {
                "type": "array",
                "uniqueItems": true,
                "items": {
                  "enum": [
                    "root-instructions",
                    "scoped-instructions",
                    "instruction-imports",
                    "session-hooks",
                    "mcp",
                    "structured-output"
                  ]
                }
              }
            }
          }
        }
      }
    },
    "policies": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/policy"
      }
    }
  },
  "$defs": {
    "compilerIdentity": {
      "type": "object",
      "additionalProperties": false,
      "required": ["package", "version"],
      "properties": {
        "package": {
          "const": "@realpkuasule/harness-automation"
        },
        "version": {
          "type": "string",
          "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+(?:-[0-9A-Za-z.-]+)?(?:\\+[0-9A-Za-z.-]+)?$"
        }
      }
    },
    "typescriptNamingBaseline": {
      "type": "object",
      "additionalProperties": false,
      "required": ["ruleId", "approvedIntakeHash", "fingerprints"],
      "properties": {
        "ruleId": {
          "const": "typescript-naming"
        },
        "approvedIntakeHash": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$"
        },
        "fingerprints": {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^[a-f0-9]{64}$"
          }
        }
      }
    },
    "evaluationSnapshot": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schemaVersion", "suites"],
      "properties": {
        "schemaVersion": {
          "enum": ["1.0", "1.1", "1.2"]
        },
        "suites": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/evaluationSuiteSnapshot"
          }
        }
      }
    },
    "evaluationSuiteSnapshot": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "kind", "command", "tasks", "baseline", "target", "graders"],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$"
        },
        "kind": {
          "enum": ["capability", "regression"]
        },
        "command": {
          "$ref": "#/$defs/argv"
        },
        "tasks": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "minLength": 1
          }
        },
        "baseline": {
          "type": "object",
          "additionalProperties": false,
          "required": ["origin", "score", "trials", "evidence"],
          "properties": {
            "origin": {
              "enum": ["pre-implementation", "adoption", "legacy-unknown"]
            },
            "score": {
              "type": "number",
              "minimum": 0,
              "maximum": 1
            },
            "trials": {
              "type": "integer",
              "minimum": 1
            },
            "evidence": {
              "type": "string",
              "minLength": 1
            }
          }
        },
        "target": {
          "type": "object",
          "additionalProperties": false,
          "required": ["metric", "threshold", "trials"],
          "properties": {
            "metric": {
              "enum": ["pass-rate", "pass-at-1", "pass-all-trials"]
            },
            "threshold": {
              "type": "number",
              "minimum": 0,
              "maximum": 1
            },
            "trials": {
              "type": "integer",
              "minimum": 1
            }
          }
        },
        "graders": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["id", "kind", "role"],
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$"
              },
              "kind": {
                "enum": ["code", "model", "human"]
              },
              "role": {
                "enum": ["gate", "guidance"]
              },
              "calibrationEvidence": {
                "type": "string",
                "minLength": 1
              }
            }
          }
        },
        "runnerSources": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "minLength": 1
          }
        },
        "traceability": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["requirementId", "ruleIds"],
            "properties": {
              "requirementId": {
                "type": "string",
                "minLength": 1
              },
              "ruleIds": {
                "type": "array",
                "minItems": 1,
                "uniqueItems": true,
                "items": {
                  "type": "string",
                  "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$"
                }
              }
            }
          }
        },
        "negativeControl": {
          "type": "object",
          "additionalProperties": false,
          "required": ["command", "fixture", "expectedExitCode"],
          "properties": {
            "command": {
              "$ref": "#/$defs/argv"
            },
            "fixture": {
              "type": "string",
              "minLength": 1
            },
            "expectedExitCode": {
              "type": "integer",
              "minimum": 1,
              "maximum": 255
            },
            "expectedReport": {
              "type": "object",
              "additionalProperties": false,
              "required": ["testId", "assertionId", "category"],
              "properties": {
                "testId": { "type": "string", "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$" },
                "assertionId": { "type": "string", "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$" },
                "category": { "type": "string", "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$" }
              }
            }
          }
        }
      }
    },
    "argv": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "string",
        "minLength": 1
      }
    },
    "policy": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "title",
        "statement",
        "rationale",
        "owner",
        "sourceRefs",
        "scope",
        "formalization",
        "severity",
        "status",
        "targets",
        "verification",
        "changeControl"
      ],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$"
        },
        "title": {
          "type": "string",
          "minLength": 1
        },
        "statement": {
          "type": "string",
          "minLength": 1
        },
        "rationale": {
          "type": "string",
          "minLength": 1
        },
        "owner": {
          "type": "string",
          "minLength": 1
        },
        "sourceRefs": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "pattern": "^[a-z][a-z0-9-]*$"
          }
        },
        "scope": {
          "type": "object",
          "additionalProperties": false,
          "required": ["include", "exclude", "boundaries"],
          "properties": {
            "include": {
              "type": "array",
              "minItems": 1,
              "items": {
                "type": "string"
              }
            },
            "exclude": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "boundaries": {
              "type": "array",
              "items": {
                "enum": [
                  "code",
                  "api",
                  "rpc",
                  "database",
                  "queue",
                  "generated-code",
                  "deployment",
                  "evaluation"
                ]
              }
            }
          }
        },
        "formalization": {
          "enum": ["deterministic", "procedural", "cognitive"]
        },
        "severity": {
          "enum": ["info", "warn", "error"]
        },
        "status": {
          "enum": ["proposed", "active", "disabled"]
        },
        "targets": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/target"
          }
        },
        "verification": {
          "$ref": "#/$defs/verification"
        },
        "examples": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "valid": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "invalid": {
              "type": "array",
              "items": {
                "type": "string"
              }
            }
          }
        },
        "changeControl": {
          "type": "object",
          "additionalProperties": false,
          "required": ["approvalRequired", "migrationRequired"],
          "properties": {
            "approvalRequired": {
              "type": "boolean"
            },
            "migrationRequired": {
              "type": "boolean"
            }
          }
        }
      }
    },
    "target": {
      "type": "object",
      "additionalProperties": false,
      "required": ["kind", "adapter"],
      "properties": {
        "kind": {
          "enum": [
            "agent-instruction",
            "formatter",
            "linter",
            "type-checker",
            "schema-validator",
            "contract-test",
            "git-hook",
            "ci",
            "repository-setting",
            "custom-command",
            "evaluation"
          ]
        },
        "adapter": {
          "type": "string",
          "minLength": 1
        },
        "configPath": {
          "type": "string"
        },
        "command": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "string"
          }
        }
      }
    },
    "verification": {
      "type": "object",
      "additionalProperties": false,
      "required": ["commands", "passCriteria"],
      "properties": {
        "commands": {
          "type": "array",
          "items": {
            "type": "array",
            "minItems": 1,
            "items": {
              "type": "string"
            }
          }
        },
        "passCriteria": {
          "type": "string",
          "minLength": 1
        },
        "timeoutSeconds": {
          "type": "integer",
          "minimum": 1,
          "maximum": 3600
        }
      }
    }
  }
}
