{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "devon.item_contract.schema.v1",
  "title": "DEVON Item Contract Schema",
  "description": "Schema for execution and monitoring items derived from a DH category contract and consumed by the Execution and Operational Verification Panel.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "item_contract_id",
    "version",
    "status",
    "item_id",
    "item_type",
    "source",
    "execution",
    "dependency",
    "evidence",
    "output_capture",
    "validation",
    "acceptance",
    "monitoring",
    "refresh",
    "progress",
    "risk",
    "recovery",
    "artifacts",
    "schedule",
    "status_model"
  ],
  "properties": {
    "item_contract_id": {
      "type": "string",
      "minLength": 1
    },
    "version": {
      "type": "string",
      "const": "v1"
    },
    "status": {
      "type": "string",
      "enum": ["DRAFT", "ACTIVE", "FAIL", "MISSING"]
    },
    "item_id": {
      "type": "string",
      "minLength": 1
    },
    "item_type": {
      "type": "string",
      "enum": [
        "task",
        "technology",
        "service",
        "container",
        "port",
        "directory",
        "file",
        "package",
        "config",
        "validation",
        "backup",
        "security_rule",
        "runtime_check",
        "healthcheck",
        "job",
        "queue",
        "model",
        "memory_store",
        "evidence_store"
      ]
    },
    "source": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "source_doc_id",
        "source_phase_id",
        "source_category_id",
        "source_category_contract_path",
        "source_path",
        "source_hash_sha256"
      ],
      "properties": {
        "source_doc_id": {
          "type": "string",
          "minLength": 1
        },
        "source_phase_id": {
          "type": "string",
          "pattern": "^phase-[0-9]{2}$"
        },
        "source_category_id": {
          "type": "string",
          "minLength": 1
        },
        "source_bucket": {
          "type": "string"
        },
        "source_category_contract_path": {
          "type": "string",
          "pattern": "^/home/yeff/public_html/devon/canon/execution/categories/.+\\.contract\\.json$"
        },
        "source_path": {
          "type": "string",
          "pattern": "^/home/yeff/public_html/devon/docs/json/phase-[0-9]{2}/.+\\.json$"
        },
        "source_hash_sha256": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$"
        }
      }
    },
    "execution": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "order",
        "title",
        "objective",
        "command_mode",
        "command",
        "environment",
        "working_directory",
        "requires_contract_before_patch"
      ],
      "properties": {
        "order": {
          "type": "integer",
          "minimum": 1
        },
        "title": {
          "type": "string",
          "minLength": 1
        },
        "objective": {
          "type": "string"
        },
        "command_mode": {
          "type": "string",
          "enum": ["manual_copy", "assisted", "automated", "not_applicable"]
        },
        "command": {
          "type": "string"
        },
        "script_path": {
          "type": "string"
        },
        "environment": {
          "type": "string",
          "enum": [
            "bootstrap",
            "sandbox",
            "production",
            "recovery",
            "monitoring",
            "build",
            "preview",
            "canon",
            "not_applicable"
          ]
        },
        "working_directory": {
          "type": "string"
        },
        "expected_change": {
          "type": "string"
        },
        "requires_contract_before_patch": {
          "type": "boolean",
          "const": true
        },
        "next_on_pass": {
          "type": "string"
        },
        "next_on_fail": {
          "type": "string"
        }
      }
    },
    "dependency": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "depends_on",
        "dependency_type",
        "unlock_condition",
        "blocks"
      ],
      "properties": {
        "depends_on": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "dependency_type": {
          "type": "string",
          "enum": ["hard", "soft", "optional", "blocking", "none"]
        },
        "unlock_condition": {
          "type": "string"
        },
        "blocks": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      }
    },
    "evidence": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "evidence_required",
        "evidence_type",
        "evidence_command",
        "expected_evidence",
        "status_source"
      ],
      "properties": {
        "evidence_required": {
          "type": "boolean",
          "const": true
        },
        "evidence_type": {
          "type": "string",
          "enum": [
            "command_output",
            "file_exists",
            "service_status",
            "container_status",
            "port_status",
            "endpoint_response",
            "log_excerpt",
            "json_value",
            "hash",
            "backup_path",
            "validation_report",
            "manual_evidence",
            "not_applicable"
          ]
        },
        "evidence_command": {
          "type": "string"
        },
        "expected_evidence": {
          "type": "string"
        },
        "evidence_path": {
          "type": "string"
        },
        "status_source": {
          "type": "string"
        },
        "accepted_values": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      }
    },
    "output_capture": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "capture_stdout",
        "capture_stderr",
        "max_output_size_bytes",
        "redact_secrets",
        "store_output_as_evidence"
      ],
      "properties": {
        "capture_stdout": {
          "type": "boolean"
        },
        "capture_stderr": {
          "type": "boolean"
        },
        "max_output_size_bytes": {
          "type": "integer",
          "minimum": 0
        },
        "redact_secrets": {
          "type": "boolean",
          "const": true
        },
        "store_output_as_evidence": {
          "type": "boolean"
        },
        "stdout_excerpt": {
          "type": "string"
        },
        "stderr_excerpt": {
          "type": "string"
        },
        "output_hash": {
          "type": "string"
        }
      }
    },
    "validation": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "validation_command",
        "expected_result",
        "blocking_if_fail",
        "validation_timeout_seconds"
      ],
      "properties": {
        "validation_command": {
          "type": "string"
        },
        "expected_result": {
          "type": "string"
        },
        "blocking_if_fail": {
          "type": "boolean"
        },
        "validation_timeout_seconds": {
          "type": "integer",
          "minimum": 0
        },
        "validation_status": {
          "type": "string",
          "enum": ["PASS", "FAIL", "MISSING", "STALE", "DEGRADED"]
        }
      }
    },
    "acceptance": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "acceptance_criteria",
        "done_definition",
        "promotion_condition"
      ],
      "properties": {
        "acceptance_criteria": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "done_definition": {
          "type": "string"
        },
        "promotion_condition": {
          "type": "string"
        }
      }
    },
    "monitoring": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "scan_enabled",
        "scan_interval_seconds",
        "pre_checks",
        "post_checks",
        "watch_services",
        "watch_ports",
        "watch_containers",
        "watch_resources",
        "impact_window_seconds"
      ],
      "properties": {
        "scan_enabled": {
          "type": "boolean"
        },
        "scan_interval_seconds": {
          "type": "integer",
          "minimum": 1
        },
        "pre_checks": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "post_checks": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "watch_services": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "watch_ports": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "watch_containers": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "watch_resources": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "impact_window_seconds": {
          "type": "integer",
          "minimum": 0
        },
        "expected_delta": {
          "type": "string"
        },
        "unexpected_delta_blocking": {
          "type": "boolean"
        }
      }
    },
    "refresh": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "refresh_on_execution",
        "refresh_on_validation",
        "manual_refresh_allowed",
        "impact_refresh_required",
        "checks_to_run_after_execution"
      ],
      "properties": {
        "refresh_on_execution": {
          "type": "boolean"
        },
        "refresh_on_validation": {
          "type": "boolean"
        },
        "manual_refresh_allowed": {
          "type": "boolean"
        },
        "impact_refresh_required": {
          "type": "boolean"
        },
        "checks_to_run_after_execution": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "refresh_priority": {
          "type": "string",
          "enum": ["low", "normal", "high", "critical"]
        }
      }
    },
    "progress": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "progress_mode",
        "checkpoints",
        "current_percent",
        "missing_to_100"
      ],
      "properties": {
        "progress_mode": {
          "type": "string",
          "const": "contracted_checks"
        },
        "checkpoints": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/progress_checkpoint"
          }
        },
        "current_percent": {
          "type": "integer",
          "minimum": 0,
          "maximum": 100
        },
        "missing_to_100": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/missing_record"
          }
        }
      }
    },
    "risk": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "risk_level",
        "can_affect_network",
        "can_affect_ssh",
        "can_affect_containers",
        "can_interrupt_service",
        "requires_backup",
        "requires_approval",
        "safe_to_retry"
      ],
      "properties": {
        "risk_level": {
          "type": "string",
          "enum": ["LOW", "MEDIUM", "HIGH", "CRITICAL", "BLOCKED"]
        },
        "can_affect_network": {
          "type": "boolean"
        },
        "can_affect_ssh": {
          "type": "boolean"
        },
        "can_affect_containers": {
          "type": "boolean"
        },
        "can_interrupt_service": {
          "type": "boolean"
        },
        "requires_backup": {
          "type": "boolean"
        },
        "requires_approval": {
          "type": "boolean"
        },
        "safe_to_retry": {
          "type": "boolean"
        }
      }
    },
    "recovery": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "rollback_available",
        "rollback_type",
        "rollback_command",
        "rollback_evidence",
        "recovery_task_id",
        "fallback_state"
      ],
      "properties": {
        "rollback_available": {
          "type": "boolean"
        },
        "rollback_type": {
          "type": "string"
        },
        "rollback_command": {
          "type": "string"
        },
        "rollback_evidence": {
          "type": "string"
        },
        "recovery_task_id": {
          "type": "string"
        },
        "fallback_state": {
          "type": "string"
        }
      }
    },
    "artifacts": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "created_files",
        "modified_files",
        "created_directories",
        "created_services",
        "created_containers",
        "created_ports",
        "created_logs",
        "created_backups",
        "generated_reports"
      ],
      "properties": {
        "created_files": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "modified_files": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "created_directories": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "created_services": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "created_containers": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "created_ports": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "created_logs": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "created_backups": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "generated_reports": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      }
    },
    "schedule": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "planned_start",
        "planned_finish",
        "estimated_duration_minutes",
        "execution_day",
        "execution_batch",
        "milestone_id",
        "critical_path",
        "started_at",
        "executed_at",
        "validated_at",
        "completed_at",
        "failed_at",
        "blocked_at",
        "actual_duration_minutes",
        "delay_reason",
        "schedule_status"
      ],
      "properties": {
        "planned_start": {
          "type": "string"
        },
        "planned_finish": {
          "type": "string"
        },
        "estimated_duration_minutes": {
          "type": "integer",
          "minimum": 0
        },
        "execution_day": {
          "type": "integer",
          "minimum": 0
        },
        "execution_batch": {
          "type": "string"
        },
        "milestone_id": {
          "type": "string"
        },
        "sprint_id": {
          "type": "string"
        },
        "day_plan_id": {
          "type": "string"
        },
        "critical_path": {
          "type": "boolean"
        },
        "started_at": {
          "type": "string"
        },
        "executed_at": {
          "type": "string"
        },
        "validated_at": {
          "type": "string"
        },
        "completed_at": {
          "type": "string"
        },
        "failed_at": {
          "type": "string"
        },
        "blocked_at": {
          "type": "string"
        },
        "actual_duration_minutes": {
          "type": "integer",
          "minimum": 0
        },
        "delay_reason": {
          "type": "string"
        },
        "schedule_status": {
          "type": "string",
          "enum": [
            "MISSING",
            "PLANNED",
            "READY",
            "ON_TIME",
            "DELAYED",
            "BLOCKED",
            "DONE",
            "RESCHEDULED"
          ]
        }
      }
    },
    "status_model": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "execution_status",
        "observed_status",
        "validation_status",
        "final_status",
        "contract_progress_status"
      ],
      "properties": {
        "execution_status": {
          "type": "string",
          "enum": [
            "MISSING",
            "READY",
            "RUNNING",
            "WAITING_EVIDENCE",
            "VALIDATING",
            "PASS",
            "FAIL",
            "BLOCKED",
            "ROLLBACK_REQUIRED",
            "RESCHEDULED"
          ]
        },
        "observed_status": {
          "type": "string",
          "enum": ["PASS", "FAIL", "MISSING", "STALE", "DEGRADED"]
        },
        "validation_status": {
          "type": "string",
          "enum": ["PASS", "FAIL", "MISSING", "STALE", "DEGRADED"]
        },
        "final_status": {
          "type": "string",
          "enum": [
            "MISSING",
            "READY",
            "RUNNING",
            "WAITING_EVIDENCE",
            "VALIDATING",
            "PASS",
            "FAIL",
            "BLOCKED",
            "ROLLBACK_REQUIRED",
            "RESCHEDULED",
            "STALE",
            "DEGRADED"
          ]
        },
        "contract_progress_status": {
          "type": "string",
          "enum": ["PASS", "FAIL", "MISSING"]
        }
      }
    }
  },
  "$defs": {
    "progress_checkpoint": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "percent",
        "condition",
        "evidence_command",
        "evidence_type"
      ],
      "properties": {
        "percent": {
          "type": "integer",
          "minimum": 0,
          "maximum": 100
        },
        "condition": {
          "type": "string"
        },
        "evidence_command": {
          "type": "string"
        },
        "evidence_type": {
          "type": "string"
        }
      }
    },
    "missing_record": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "missing_id",
        "description",
        "required_evidence",
        "blocking",
        "next_action"
      ],
      "properties": {
        "missing_id": {
          "type": "string"
        },
        "description": {
          "type": "string"
        },
        "required_evidence": {
          "type": "string"
        },
        "blocking": {
          "type": "boolean"
        },
        "next_action": {
          "type": "string"
        }
      }
    }
  }
}
