{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://workspai.dev/contracts/artifact-remediation-plan.v1.json",
  "title": "Workspai Artifact Remediation Plan",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "generatedAt", "workspace", "source", "summary", "actions"],
  "properties": {
    "schemaVersion": {
      "const": "artifact-remediation-plan-v1"
    },
    "generatedAt": {
      "type": "string"
    },
    "workspace": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name"],
      "properties": {
        "name": { "type": "string" },
        "path": { "type": "string" }
      }
    },
    "source": {
      "type": "object",
      "additionalProperties": false,
      "required": ["command", "reportsDir", "includeAbsolutePaths", "ciMode"],
      "properties": {
        "command": { "const": "workspace remediation-plan" },
        "reportsDir": { "type": "string" },
        "includeAbsolutePaths": { "type": "boolean" },
        "ciMode": { "type": "boolean" }
      }
    },
    "summary": {
      "type": "object",
      "additionalProperties": false,
      "required": ["artifactsScanned", "cardsCovered", "totalActions", "executableActions", "risk"],
      "properties": {
        "artifactsScanned": { "type": "integer", "minimum": 0 },
        "cardsCovered": { "type": "integer", "minimum": 0 },
        "totalActions": { "type": "integer", "minimum": 0 },
        "executableActions": { "type": "integer", "minimum": 0 },
        "blockedActions": { "type": "integer", "minimum": 0 },
        "guidanceActions": { "type": "integer", "minimum": 0 },
        "risk": { "$ref": "#/$defs/riskSummary" }
      }
    },
    "execution": {
      "type": "object",
      "additionalProperties": false,
      "required": ["nextActionId", "eligibleActionIds", "blockedActionIds"],
      "properties": {
        "nextActionId": { "type": ["string", "null"] },
        "eligibleActionIds": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 },
          "uniqueItems": true
        },
        "blockedActionIds": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 },
          "uniqueItems": true
        }
      }
    },
    "actions": {
      "type": "array",
      "items": { "$ref": "#/$defs/action" }
    }
  },
  "$defs": {
    "riskSummary": {
      "type": "object",
      "additionalProperties": false,
      "required": ["safe", "guarded", "invasive"],
      "properties": {
        "safe": { "type": "integer", "minimum": 0 },
        "guarded": { "type": "integer", "minimum": 0 },
        "invasive": { "type": "integer", "minimum": 0 }
      }
    },
    "operation": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "path", "content", "overwrite"],
          "properties": {
            "type": { "const": "file-create" },
            "path": { "type": "string" },
            "content": { "type": "string" },
            "overwrite": { "const": false }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "path", "lines", "ensureNewline"],
          "properties": {
            "type": { "const": "file-append" },
            "path": { "type": "string" },
            "lines": { "type": "array", "items": { "type": "string" } },
            "ensureNewline": { "type": "boolean" }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "sourcePath", "path", "overwrite"],
          "properties": {
            "type": { "const": "file-copy" },
            "sourcePath": { "type": "string" },
            "path": { "type": "string" },
            "overwrite": { "const": false }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "path", "scriptName", "scriptValue"],
          "properties": {
            "type": { "const": "package-json-script" },
            "path": { "type": "string" },
            "scriptName": { "type": "string" },
            "scriptValue": { "type": "string" }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "path", "edits"],
          "properties": {
            "type": { "const": "json-edit" },
            "path": { "type": "string" },
            "edits": {
              "type": "array",
              "items": {
                "type": "object",
                "additionalProperties": false,
                "required": ["pointer", "value"],
                "properties": {
                  "pointer": { "type": "string" },
                  "value": { "type": ["string", "number", "boolean", "null"] }
                }
              }
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "path", "keys"],
          "properties": {
            "type": { "const": "env-key-add" },
            "path": { "type": "string" },
            "keys": {
              "type": "array",
              "items": {
                "type": "object",
                "additionalProperties": false,
                "required": ["name", "value"],
                "properties": {
                  "name": { "type": "string" },
                  "value": { "type": "string" },
                  "comment": { "type": "string" }
                }
              }
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "path", "target", "command", "phony"],
          "properties": {
            "type": { "const": "makefile-target" },
            "path": { "type": "string" },
            "target": { "type": "string" },
            "command": { "type": "string" },
            "phony": { "type": "boolean" }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "command", "cwd"],
          "properties": {
            "type": { "const": "run-command" },
            "command": { "type": "string" },
            "cwd": { "enum": ["workspace", "project"] }
          }
        }
      ]
    },
    "commandInvocation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["cwd", "executable", "args"],
      "properties": {
        "cwd": { "type": "string", "minLength": 1 },
        "executable": { "type": "string", "minLength": 1 },
        "args": {
          "type": "array",
          "items": { "type": "string" }
        }
      }
    },
    "repairStrategyStage": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "kind", "description", "risk", "continueWhen"],
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "kind": {
          "enum": ["diagnose", "safe-fix", "targeted-upgrade", "verify", "exception-review"]
        },
        "description": { "type": "string", "minLength": 1 },
        "risk": { "enum": ["safe", "guarded", "invasive"] },
        "invocation": { "$ref": "#/$defs/commandInvocation" },
        "continueWhen": {
          "enum": ["always", "previous-passed", "blocker-remains", "manual-decision"]
        }
      }
    },
    "dependencyRepairTransaction": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "schemaVersion",
        "kind",
        "state",
        "projectPath",
        "ecosystem",
        "requiredStages",
        "completion"
      ],
      "properties": {
        "schemaVersion": {
          "const": "workspai.doctor-dependency-repair-transaction.v1"
        },
        "kind": { "enum": ["dependency-security", "dependency-materialization"] },
        "state": { "const": "planned" },
        "projectPath": { "type": "string", "minLength": 1 },
        "ecosystem": { "type": "string", "minLength": 1 },
        "sourceMutationRequired": { "type": "boolean" },
        "observableState": { "enum": ["runtime-dependency-tree"] },
        "requiredStages": {
          "type": "array",
          "items": { "enum": ["reconcile", "audit", "test", "build"] },
          "uniqueItems": true
        },
        "completion": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "manifestLockConsistent",
            "declaredTestsPass",
            "declaredBuildPass",
            "canonicalVerificationRequired"
          ],
          "properties": {
            "manifestLockConsistent": { "const": true },
            "auditClean": { "const": true },
            "installedTreePresent": { "const": true },
            "declaredTestsPass": { "const": true },
            "declaredBuildPass": { "const": true },
            "canonicalVerificationRequired": { "const": true }
          }
        }
      },
      "allOf": [
        {
          "if": { "properties": { "kind": { "const": "dependency-security" } } },
          "then": {
            "properties": {
              "completion": {
                "type": "object",
                "properties": { "auditClean": { "const": true } },
                "required": ["auditClean"]
              }
            }
          }
        },
        {
          "if": { "properties": { "kind": { "const": "dependency-materialization" } } },
          "then": {
            "required": ["sourceMutationRequired", "observableState"],
            "properties": {
              "sourceMutationRequired": { "const": false },
              "observableState": { "const": "runtime-dependency-tree" },
              "completion": {
                "type": "object",
                "properties": { "installedTreePresent": { "const": true } },
                "required": ["installedTreePresent"]
              }
            }
          }
        }
      ]
    },
    "action": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "artifactKind",
        "cardId",
        "title",
        "order",
        "phase",
        "scope",
        "findingId",
        "findingStatus",
        "causalKey",
        "status",
        "mode",
        "risk",
        "requiresApproval",
        "blocker",
        "summary",
        "verifyCommand",
        "cwd",
        "files",
        "rollback",
        "notes"
      ],
      "properties": {
        "id": { "type": "string" },
        "artifactKind": { "type": "string" },
        "cardId": { "type": "string" },
        "title": { "type": "string" },
        "order": { "type": "integer", "minimum": 0 },
        "phase": { "type": "string" },
        "scope": { "enum": ["workspace", "project"] },
        "projectName": { "type": "string", "minLength": 1 },
        "projectPath": { "type": "string", "minLength": 1 },
        "sourceStepId": { "type": "string", "minLength": 1 },
        "findingId": { "type": "string", "minLength": 1 },
        "findingStatus": {
          "enum": ["blocking", "advisory", "informational", "unknown"]
        },
        "causalKey": { "type": "string", "minLength": 1 },
        "dependsOn": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 }
        },
        "strategy": {
          "type": "array",
          "items": { "$ref": "#/$defs/repairStrategyStage" }
        },
        "transaction": { "$ref": "#/$defs/dependencyRepairTransaction" },
        "requirements": {
          "type": "array",
          "items": { "$ref": "#/$defs/actionRequirement" }
        },
        "retryPolicy": { "$ref": "#/$defs/retryPolicy" },
        "status": { "enum": ["ready", "review-required", "blocked", "guidance-only"] },
        "mode": {
          "enum": [
            "edit-file",
            "run-command",
            "refresh-evidence",
            "verify-before-fix",
            "manual-guidance"
          ]
        },
        "risk": { "enum": ["safe", "guarded", "invasive"] },
        "requiresApproval": { "type": "boolean" },
        "blocker": { "type": "string" },
        "summary": { "type": "string" },
        "command": { "type": "string" },
        "invocation": { "$ref": "#/$defs/commandInvocation" },
        "verifyCommand": { "type": "string" },
        "cwd": { "enum": ["workspace", "project"] },
        "files": {
          "type": "array",
          "items": { "type": "string" }
        },
        "operation": { "$ref": "#/$defs/operation" },
        "rollback": {
          "type": "object",
          "additionalProperties": false,
          "required": ["available", "strategy"],
          "properties": {
            "available": { "type": "boolean" },
            "strategy": { "enum": ["idempotent", "manual", "none"] }
          }
        },
        "notes": {
          "type": "array",
          "items": { "type": "string" }
        }
      }
    },
    "actionRequirement": {
      "type": "object",
      "additionalProperties": false,
      "required": ["kind", "id", "status", "message"],
      "properties": {
        "kind": { "enum": ["executable", "action"] },
        "id": { "type": "string", "minLength": 1 },
        "status": { "enum": ["satisfied", "missing", "pending"] },
        "executable": { "type": "string", "minLength": 1 },
        "actionId": { "type": "string", "minLength": 1 },
        "message": { "type": "string", "minLength": 1 }
      }
    },
    "retryPolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["sameGeneration", "resumeWhen"],
      "properties": {
        "sameGeneration": { "enum": ["allowed", "forbidden"] },
        "resumeWhen": {
          "enum": ["immediate", "dependencies-complete", "environment-changed"]
        }
      }
    }
  }
}
