{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://workspai.dev/contracts/doctor-remediation-plan.v1.json",
  "title": "Workspai Doctor Remediation Plan v2 Legacy Path Alias",
  "description": "Deprecated compatibility path. New consumers must resolve contracts/doctor-remediation-plan.v2.json.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "generatedAt",
    "policyProfile",
    "fixableProjects",
    "totalSteps",
    "executableSteps",
    "risk",
    "steps"
  ],
  "properties": {
    "schemaVersion": {
      "const": "doctor-remediation-plan-v2"
    },
    "generatedAt": {
      "type": "string"
    },
    "policyProfile": {
      "enum": ["local", "ci", "release", "enterprise-strict"]
    },
    "fixableProjects": {
      "type": "integer",
      "minimum": 0
    },
    "totalSteps": {
      "type": "integer",
      "minimum": 0
    },
    "executableSteps": {
      "type": "integer",
      "minimum": 0
    },
    "risk": {
      "$ref": "#/$defs/riskSummary"
    },
    "steps": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/step"
      }
    }
  },
  "$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
        }
      }
    },
    "preview": {
      "type": "object",
      "additionalProperties": false,
      "required": ["title", "summary", "changes"],
      "properties": {
        "title": {
          "type": "string"
        },
        "summary": {
          "type": "string"
        },
        "changes": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      }
    },
    "rollback": {
      "type": "object",
      "additionalProperties": false,
      "required": ["available", "strategy"],
      "properties": {
        "available": {
          "type": "boolean"
        },
        "strategy": {
          "enum": ["snapshot", "idempotent", "manual", "none"]
        }
      }
    },
    "diffPreview": {
      "type": "object",
      "additionalProperties": false,
      "required": ["available", "format", "summary", "hunks"],
      "properties": {
        "available": {
          "type": "boolean"
        },
        "format": {
          "enum": ["unified", "summary", "none"]
        },
        "summary": {
          "type": "string"
        },
        "hunks": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "limitations": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      }
    },
    "studioStatus": {
      "type": "object",
      "additionalProperties": false,
      "required": ["state", "reason"],
      "properties": {
        "state": {
          "enum": ["ready", "blocked", "review-required", "guidance-only"]
        },
        "reason": {
          "type": "string"
        }
      }
    },
    "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"]
              }
            }
          }
        }
      ]
    },
    "repairIntent": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "mode",
        "confidence",
        "primaryActionLabel",
        "requiresApproval",
        "requiresFreshEvidence",
        "reason",
        "relatedCommands"
      ],
      "properties": {
        "mode": {
          "enum": [
            "none",
            "run-command",
            "edit-file",
            "review-required",
            "verify-before-fix",
            "refresh-evidence",
            "manual-guidance"
          ]
        },
        "primaryActionLabel": {
          "type": "string"
        },
        "requiresApproval": {
          "type": "boolean"
        },
        "confidence": {
          "enum": ["high", "medium", "low"]
        },
        "requiresFreshEvidence": {
          "type": "boolean"
        },
        "reason": {
          "type": "string"
        },
        "relatedCommands": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      }
    },
    "repairOperation": {
      "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"
            }
          }
        }
      ]
    },
    "step": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "phase",
        "order",
        "dependsOn",
        "projectName",
        "projectPath",
        "originalCommand",
        "kind",
        "risk",
        "executable",
        "findingStatus",
        "files",
        "preview",
        "diffPreview",
        "refreshCommands",
        "rollback",
        "studioStatus",
        "executableInCurrentEnvironment"
      ],
      "properties": {
        "id": {
          "type": "string"
        },
        "phase": {
          "enum": [
            "dependency-baseline",
            "local-environment",
            "source-hygiene",
            "command-contract",
            "runtime-governance",
            "manual-review",
            "generic-execution"
          ]
        },
        "order": {
          "type": "integer",
          "minimum": 1
        },
        "dependsOn": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "projectName": {
          "type": "string"
        },
        "projectPath": {
          "type": "string"
        },
        "originalCommand": {
          "type": "string"
        },
        "kind": {
          "enum": [
            "env-copy",
            "package-json-script",
            "file-create",
            "file-append",
            "json-edit",
            "env-key-add",
            "makefile-target",
            "rapidkit-init",
            "go-mod-tidy",
            "dependency-sync",
            "manual-url",
            "shell"
          ]
        },
        "risk": {
          "enum": ["safe", "guarded", "invasive"]
        },
        "executable": {
          "type": "boolean"
        },
        "reason": {
          "type": "string"
        },
        "issueId": {
          "type": "string"
        },
        "diagnosisFindingId": {
          "type": "string",
          "minLength": 1
        },
        "causalKey": {
          "type": "string",
          "minLength": 1
        },
        "findingStatus": {
          "enum": ["blocking", "advisory", "informational", "unknown"]
        },
        "issueClass": {
          "enum": [
            "dependency",
            "environment",
            "security",
            "test",
            "quality",
            "container",
            "deployment",
            "runtime",
            "workspace-contract",
            "source-tree",
            "framework",
            "configuration",
            "custom",
            "unknown"
          ]
        },
        "operationalImpact": {
          "enum": [
            "none",
            "developer-friction",
            "ci-risk",
            "release-risk",
            "security-risk",
            "runtime-risk",
            "customer-risk"
          ]
        },
        "repairIntent": {
          "$ref": "#/$defs/repairIntent"
        },
        "files": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "operation": {
          "$ref": "#/$defs/repairOperation"
        },
        "invocation": {
          "$ref": "#/$defs/commandInvocation"
        },
        "strategy": {
          "type": "array",
          "items": { "$ref": "#/$defs/repairStrategyStage" }
        },
        "transaction": {
          "$ref": "#/$defs/dependencyRepairTransaction"
        },
        "preview": {
          "$ref": "#/$defs/preview"
        },
        "diffPreview": {
          "$ref": "#/$defs/diffPreview"
        },
        "verifyCommand": {
          "type": "string"
        },
        "refreshCommands": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "rollback": {
          "$ref": "#/$defs/rollback"
        },
        "studioStatus": {
          "$ref": "#/$defs/studioStatus"
        },
        "executableInCurrentEnvironment": {
          "type": "boolean"
        },
        "blockedReason": {
          "type": "string"
        }
      }
    }
  }
}
