{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://dnncha.github.io/dotmatch/agent-tools.schema.json",
  "title": "DotMatch local research-agent tool contract",
  "type": "object",
  "additionalProperties": false,
  "required": ["$schema", "schema_version", "tool_contract_version", "generated_for_version", "transport", "safety", "intents", "envelope", "tools"],
  "properties": {
    "$schema": {"const": "https://dnncha.github.io/dotmatch/agent-tools.schema.json"},
    "schema_version": {"const": "1.0"},
    "tool_contract_version": {"const": "1.0"},
    "generated_for_version": {"type": "string", "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+"},
    "transport": {
      "type": "object",
      "additionalProperties": false,
      "required": ["input", "output", "diagnostics", "network"],
      "properties": {
        "input": {"type": "string"},
        "output": {"type": "string"},
        "diagnostics": {"type": "string"},
        "network": {"type": "string"}
      }
    },
    "safety": {"type": "object"},
    "intents": {"type": "array", "minItems": 2, "items": {"type": "object"}},
    "envelope": {"type": "object", "required": ["$ref"]},
    "tools": {
      "type": "array",
      "minItems": 6,
      "maxItems": 6,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["name", "purpose", "input_schema"],
        "properties": {
          "name": {"enum": ["discover", "prepare_assay", "inspect_assay", "run_assay", "review_assay", "handoff_assay"]},
          "purpose": {"type": "string", "minLength": 20},
          "input_schema": {"type": "object"}
        }
      }
    }
  },
  "$defs": {
    "spec_input": {
      "type": "object",
      "additionalProperties": false,
      "required": ["spec"],
      "properties": {
        "spec": {"type": "string", "minLength": 1},
        "minimum_free_bytes": {"type": "integer", "minimum": 0, "default": 1073741824}
      }
    },
    "finding": {
      "type": "object",
      "required": ["finding_id", "severity", "message", "source_artifact"],
      "properties": {
        "finding_id": {"type": "string"},
        "severity": {"enum": ["info", "warning", "error", "blocked"]},
        "message": {"type": "string"},
        "source_artifact": {"type": "string"}
      }
    },
    "artifact": {
      "type": "object",
      "required": ["role", "path", "exists", "safe_to_share"],
      "properties": {
        "role": {"type": "string"},
        "path": {"type": "string"},
        "exists": {"type": "boolean"},
        "safe_to_share": {"type": "boolean"},
        "sha256": {"type": "string", "pattern": "^[0-9a-f]{64}$"},
        "bytes": {"type": "integer", "minimum": 0}
      }
    },
    "envelope": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schema_version", "tool_contract_version", "dotmatch_version", "tool", "status", "exit_code", "spec", "artifacts", "findings", "resource_use", "next_actions"],
      "properties": {
        "schema_version": {"const": "1.0"},
        "tool_contract_version": {"const": "1.0"},
        "dotmatch_version": {"type": "string"},
        "tool": {"type": "string"},
        "status": {"enum": ["passed", "needs_review", "failed", "blocked", "invalid_input", "interrupted"]},
        "exit_code": {"enum": [0, 1, 2, 130]},
        "spec": {
          "type": "object",
          "required": ["revision", "path", "sha256"],
          "properties": {
            "revision": {"type": "integer", "minimum": 0},
            "path": {"type": "string"},
            "sha256": {"type": "string", "pattern": "^$|^[0-9a-f]{64}$"}
          }
        },
        "artifacts": {"type": "array", "items": {"$ref": "#/$defs/artifact"}},
        "findings": {"type": "array", "items": {"$ref": "#/$defs/finding"}},
        "resource_use": {"type": "object"},
        "next_actions": {"type": "array", "items": {"type": "object"}},
        "result": {"type": "object"}
      },
      "allOf": [
        {
          "if": {"properties": {"status": {"const": "passed"}}, "required": ["status"]},
          "then": {"properties": {"exit_code": {"const": 0}}}
        },
        {
          "if": {"properties": {"status": {"const": "needs_review"}}, "required": ["status"]},
          "then": {"properties": {"exit_code": {"const": 1}}}
        },
        {
          "if": {
            "properties": {"status": {"enum": ["failed", "blocked", "invalid_input"]}},
            "required": ["status"]
          },
          "then": {"properties": {"exit_code": {"const": 2}}}
        },
        {
          "if": {"properties": {"status": {"const": "interrupted"}}, "required": ["status"]},
          "then": {"properties": {"exit_code": {"const": 130}}}
        }
      ]
    }
  }
}
