{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/homeofe/AAHP/schema/aahp-manifest.schema.json",
  "title": "AAHP Manifest",
  "description": "Schema for the AI-to-AI Handoff Protocol MANIFEST.json file (v2/v3)",
  "type": "object",
  "required": [
    "aahp_version",
    "project",
    "last_session",
    "files",
    "quick_context"
  ],
  "properties": {
    "aahp_version": {
      "type": "string",
      "pattern": "^\\d+\\.\\d+$",
      "description": "AAHP protocol version"
    },
    "project": {
      "type": "string",
      "minLength": 1,
      "description": "Project name"
    },
    "last_session": {
      "type": "object",
      "required": [
        "agent",
        "timestamp",
        "phase"
      ],
      "properties": {
        "agent": {
          "type": "string",
          "description": "Model/agent identifier (e.g., claude-sonnet-4.5)"
        },
        "session_id": {
          "type": "string",
          "description": "Unique session identifier"
        },
        "timestamp": {
          "type": "string",
          "format": "date-time",
          "description": "ISO 8601 timestamp of session end"
        },
        "commit": {
          "type": "string",
          "description": "Git commit hash at session end"
        },
        "phase": {
          "type": "string",
          "enum": [
            "research",
            "architecture",
            "implementation",
            "review",
            "fix",
            "idle",
            "documentation"
          ],
          "description": "Pipeline phase at session end"
        },
        "duration_minutes": {
          "type": "integer",
          "minimum": 0,
          "description": "Approximate session duration"
        }
      }
    },
    "files": {
      "type": "object",
      "patternProperties": {
        "^.+\\.md$": {
          "$ref": "#/$defs/manifestFile"
        },
        "^(pii-allowlist|LOG-ARCHIVE\\.index)\\.json$": {
          "$ref": "#/$defs/manifestFile"
        }
      },
      "additionalProperties": false
    },
    "quick_context": {
      "type": "string",
      "maxLength": 500,
      "description": "2-3 sentences: what just happened, what's next, any blockers"
    },
    "token_budget": {
      "type": "object",
      "properties": {
        "manifest_only": {
          "type": "integer",
          "minimum": 0
        },
        "manifest_plus_core": {
          "type": "integer",
          "minimum": 0
        },
        "full_read": {
          "type": "integer",
          "minimum": 0
        }
      }
    },
    "next_task_id": {
      "type": "integer",
      "minimum": 1,
      "description": "v3: Next available task ID counter (e.g., 6 means T-001 through T-005 are assigned)"
    },
    "tasks": {
      "type": "object",
      "description": "v3: Task registry with dependency graph. Keys are task IDs (T-001, T-002, etc.)",
      "patternProperties": {
        "^T-\\d{3,}$": {
          "type": "object",
          "required": [
            "title",
            "status"
          ],
          "properties": {
            "title": {
              "type": "string",
              "maxLength": 200,
              "description": "Short task title"
            },
            "status": {
              "type": "string",
              "enum": [
                "ready",
                "in_progress",
                "blocked",
                "done",
                "cancelled"
              ],
              "description": "Current task status"
            },
            "priority": {
              "type": "string",
              "enum": [
                "critical",
                "high",
                "medium",
                "low"
              ],
              "description": "Task priority"
            },
            "depends_on": {
              "type": "array",
              "items": {
                "type": "string",
                "pattern": "^T-\\d{3,}$"
              },
              "description": "Task IDs that must complete before this task can start"
            },
            "blocked_by": {
              "type": "string",
              "maxLength": 200,
              "description": "External blocker description (not a task dependency)"
            },
            "assigned_to": {
              "type": "string",
              "description": "Agent or role currently working on this task"
            },
            "created": {
              "type": "string",
              "format": "date-time",
              "description": "When the task was created"
            },
            "completed": {
              "type": "string",
              "format": "date-time",
              "description": "When the task was completed"
            }
          }
        }
      },
      "additionalProperties": false
    },
    "cross_repo_ref": {
      "type": "object",
      "description": "v3.4 (optional, additive): links this handoff to a related repo's handoff. Backward compatible; projects without it validate unchanged.",
      "required": [
        "repo",
        "commit",
        "relation"
      ],
      "properties": {
        "repo": {
          "type": "string",
          "minLength": 1,
          "description": "owner/name of the referenced repository (e.g., homeofe/improvements)"
        },
        "commit": {
          "type": "string",
          "description": "Commit hash in the referenced repository this handoff relates to"
        },
        "handoff_file": {
          "type": "string",
          "description": "Path to the referenced handoff file (default .ai/handoff/MANIFEST.json)"
        },
        "relation": {
          "type": "string",
          "enum": [
            "implements",
            "extends",
            "consumes"
          ],
          "description": "How this repo relates to the referenced repo"
        }
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": false,
  "$defs": {
    "manifestFile": {
      "type": "object",
      "required": [
        "checksum",
        "updated",
        "lines",
        "summary"
      ],
      "properties": {
        "checksum": {
          "type": "string",
          "pattern": "^sha256:[a-f0-9]{64}$",
          "description": "SHA-256 hash of file contents"
        },
        "updated": {
          "type": "string",
          "format": "date-time",
          "description": "Last modification timestamp"
        },
        "lines": {
          "type": "integer",
          "minimum": 0,
          "description": "Line count"
        },
        "summary": {
          "type": "string",
          "maxLength": 200,
          "description": "One-line human-readable summary"
        }
      }
    }
  }
}
