{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://aslice.sh/schematics/json/operation-outcome.schema.json",
  "title": "Operation outcome",
  "description": "Version 1 structural contract. Normative field semantics: docs/STATE-AND-RECOVERY.md section 10.2 and docs/DATABASE.md section 9. Cryptography, canonical encoding, chain continuity, ordering, and filesystem safety require semantic validation. Version 2: explicit source-work consent, namespace selections, and security/restart decisions. Old versions are rejected; regenerate from authenticated inputs, never infer consent. Field semantics: docs/DESIGN.md vulnerability-and-sbom-pipeline and docs/PACKAGE-FORMAT.md Security dependency and selection records.",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "format": {
      "const": 2
    },
    "command": {
      "type": "string",
      "minLength": 1
    },
    "role": {
      "anyOf": [
        {
          "enum": [
            "client-state",
            "client-cache",
            "system-state",
            "coordinator",
            "publisher",
            "release-signer"
          ]
        },
        {
          "type": "null"
        }
      ]
    },
    "instance_id": {
      "anyOf": [
        {
          "type": "string",
          "pattern": "^[0-9a-f]{32}$"
        },
        {
          "type": "null"
        }
      ]
    },
    "status": {
      "enum": [
        "ok",
        "refused",
        "needs-attention",
        "error",
        "incomplete-remediation"
      ]
    },
    "errors": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "code": {
            "type": "string",
            "minLength": 1
          },
          "message": {
            "type": "string",
            "minLength": 1
          },
          "remedy": {
            "type": "string",
            "minLength": 1
          },
          "path": {
            "type": "string",
            "minLength": 1
          }
        },
        "required": [
          "code",
          "message",
          "remedy"
        ]
      },
      "uniqueItems": true
    },
    "data": {
      "type": [
        "object",
        "null"
      ],
      "additionalProperties": false,
      "properties": {
        "format": {
          "const": 1
        },
        "operation_id": {
          "anyOf": [
            {
              "type": "string",
              "pattern": "^[0-9a-f]{32}$"
            },
            {
              "type": "null"
            }
          ]
        },
        "plan_digest": {
          "anyOf": [
            {
              "type": "string",
              "pattern": "^sha256:[0-9a-f]{64}$"
            },
            {
              "type": "null"
            }
          ]
        },
        "owner_uid": {
          "anyOf": [
            {
              "type": "integer",
              "minimum": 0,
              "maximum": 9007199254740991
            },
            {
              "type": "null"
            }
          ]
        },
        "phase": {
          "enum": [
            "unprepared",
            "prepared",
            "effects",
            "committed",
            "complete"
          ]
        },
        "helpers_active": {
          "type": "boolean"
        },
        "waiting": {
          "type": "boolean"
        },
        "outcome": {
          "anyOf": [
            {
              "enum": [
                "repaired",
                "usable_with_unresolved_repairs",
                "replacement_prepared_activation_blocked"
              ]
            },
            {
              "type": "null"
            }
          ]
        },
        "committed": {
          "type": "boolean"
        },
        "verification_complete": {
          "type": "boolean"
        },
        "activation_allowed": {
          "type": "boolean"
        },
        "recovery_required": {
          "type": "boolean"
        },
        "retry_safe": {
          "type": "boolean"
        },
        "usable_closures": {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^sha256:[0-9a-f]{64}$"
          },
          "uniqueItems": true
        },
        "unresolved_repairs": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "group": {
                "type": "string",
                "minLength": 1
              },
              "reason": {
                "type": "string",
                "minLength": 1
              },
              "packages": {
                "type": "array",
                "items": {
                  "type": "string",
                  "minLength": 1
                },
                "uniqueItems": true
              },
              "services": {
                "type": "array",
                "items": {
                  "type": "string",
                  "minLength": 1
                },
                "uniqueItems": true
              },
              "paths": {
                "type": "array",
                "items": {
                  "type": "string",
                  "minLength": 1
                },
                "uniqueItems": true
              },
              "next_actions": {
                "type": "array",
                "items": {
                  "type": "string",
                  "minLength": 1
                },
                "uniqueItems": true
              },
              "evidence": {
                "type": "array",
                "items": {
                  "type": "object",
                  "additionalProperties": false,
                  "properties": {
                    "digest": {
                      "type": "string",
                      "pattern": "^sha256:[0-9a-f]{64}$"
                    },
                    "size": {
                      "type": "integer",
                      "minimum": 0,
                      "maximum": 9007199254740991
                    }
                  },
                  "required": [
                    "digest",
                    "size"
                  ]
                },
                "uniqueItems": true
              }
            },
            "required": [
              "group",
              "reason",
              "packages",
              "services",
              "paths",
              "next_actions",
              "evidence"
            ]
          },
          "uniqueItems": true
        },
        "next_actions": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          },
          "uniqueItems": true
        },
        "plan": {
          "anyOf": [
            {
              "type": "object",
              "additionalProperties": false,
              "properties": {
                "format": {
                  "const": 1
                },
                "action": {
                  "enum": [
                    "continue",
                    "manual",
                    "salvage",
                    "activate"
                  ]
                },
                "instance_id": {
                  "type": "string",
                  "pattern": "^[0-9a-f]{32}$"
                },
                "interrupted_operation": {
                  "anyOf": [
                    {
                      "type": "string",
                      "pattern": "^[0-9a-f]{32}$"
                    },
                    {
                      "type": "null"
                    }
                  ]
                },
                "base_head": {
                  "type": "string",
                  "pattern": "^sha256:[0-9a-f]{64}$"
                },
                "base_generation": {
                  "anyOf": [
                    {
                      "type": "string",
                      "pattern": "^sha256:[0-9a-f]{64}$"
                    },
                    {
                      "type": "null"
                    }
                  ]
                },
                "replacement_path": {
                  "anyOf": [
                    {
                      "type": "string",
                      "minLength": 1
                    },
                    {
                      "type": "null"
                    }
                  ]
                },
                "evidence": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "additionalProperties": false,
                    "properties": {
                      "digest": {
                        "type": "string",
                        "pattern": "^sha256:[0-9a-f]{64}$"
                      },
                      "size": {
                        "type": "integer",
                        "minimum": 0,
                        "maximum": 9007199254740991
                      }
                    },
                    "required": [
                      "digest",
                      "size"
                    ]
                  },
                  "uniqueItems": true
                },
                "artifacts": {
                  "type": "array",
                  "items": {
                    "type": "string",
                    "pattern": "^sha256:[0-9a-f]{64}$"
                  },
                  "uniqueItems": true
                },
                "omissions": {
                  "type": "array",
                  "items": {
                    "type": "string",
                    "minLength": 1
                  },
                  "uniqueItems": true
                },
                "required_consents": {
                  "type": "array",
                  "items": {
                    "enum": [
                      "system-changes",
                      "grafts"
                    ]
                  },
                  "uniqueItems": true
                },
                "effects": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "additionalProperties": false,
                    "properties": {
                      "owner": {
                        "enum": [
                          "user",
                          "protected"
                        ]
                      },
                      "resource": {
                        "type": "string",
                        "minLength": 1
                      },
                      "operation": {
                        "enum": [
                          "restore",
                          "retain",
                          "replace",
                          "register",
                          "unregister",
                          "relocate",
                          "select"
                        ]
                      },
                      "before": {
                        "anyOf": [
                          {
                            "type": "object",
                            "additionalProperties": false,
                            "properties": {
                              "digest": {
                                "type": "string",
                                "pattern": "^sha256:[0-9a-f]{64}$"
                              },
                              "size": {
                                "type": "integer",
                                "minimum": 0,
                                "maximum": 9007199254740991
                              }
                            },
                            "required": [
                              "digest",
                              "size"
                            ]
                          },
                          {
                            "type": "null"
                          }
                        ]
                      },
                      "after": {
                        "anyOf": [
                          {
                            "type": "object",
                            "additionalProperties": false,
                            "properties": {
                              "digest": {
                                "type": "string",
                                "pattern": "^sha256:[0-9a-f]{64}$"
                              },
                              "size": {
                                "type": "integer",
                                "minimum": 0,
                                "maximum": 9007199254740991
                              }
                            },
                            "required": [
                              "digest",
                              "size"
                            ]
                          },
                          {
                            "type": "null"
                          }
                        ]
                      },
                      "backup": {
                        "anyOf": [
                          {
                            "type": "object",
                            "additionalProperties": false,
                            "properties": {
                              "digest": {
                                "type": "string",
                                "pattern": "^sha256:[0-9a-f]{64}$"
                              },
                              "size": {
                                "type": "integer",
                                "minimum": 0,
                                "maximum": 9007199254740991
                              }
                            },
                            "required": [
                              "digest",
                              "size"
                            ]
                          },
                          {
                            "type": "null"
                          }
                        ]
                      }
                    },
                    "required": [
                      "owner",
                      "resource",
                      "operation",
                      "before",
                      "after",
                      "backup"
                    ]
                  },
                  "uniqueItems": true
                },
                "choices": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "additionalProperties": false,
                    "properties": {
                      "group": {
                        "type": "string",
                        "minLength": 1
                      },
                      "group_digest": {
                        "type": "string",
                        "pattern": "^sha256:[0-9a-f]{64}$"
                      },
                      "choice": {
                        "enum": [
                          "restore",
                          "keep",
                          "manual"
                        ]
                      },
                      "evidence": {
                        "type": "array",
                        "items": {
                          "type": "object",
                          "additionalProperties": false,
                          "properties": {
                            "digest": {
                              "type": "string",
                              "pattern": "^sha256:[0-9a-f]{64}$"
                            },
                            "size": {
                              "type": "integer",
                              "minimum": 0,
                              "maximum": 9007199254740991
                            }
                          },
                          "required": [
                            "digest",
                            "size"
                          ]
                        },
                        "uniqueItems": true
                      }
                    },
                    "required": [
                      "group",
                      "group_digest",
                      "choice",
                      "evidence"
                    ]
                  },
                  "uniqueItems": true
                }
              },
              "required": [
                "format",
                "action",
                "instance_id",
                "interrupted_operation",
                "base_head",
                "base_generation",
                "replacement_path",
                "evidence",
                "artifacts",
                "omissions",
                "required_consents",
                "effects",
                "choices"
              ],
              "allOf": [
                {
                  "if": {
                    "properties": {
                      "action": {
                        "const": "continue"
                      }
                    }
                  },
                  "then": {
                    "properties": {
                      "interrupted_operation": {
                        "type": "string",
                        "pattern": "^[0-9a-f]{32}$"
                      }
                    }
                  }
                },
                {
                  "if": {
                    "properties": {
                      "action": {
                        "const": "activate"
                      }
                    }
                  },
                  "then": {
                    "properties": {
                      "replacement_path": {
                        "type": "string",
                        "minLength": 1
                      }
                    }
                  }
                }
              ]
            },
            {
              "type": "null"
            }
          ]
        }
      },
      "required": [
        "format",
        "operation_id",
        "plan_digest",
        "owner_uid",
        "phase",
        "helpers_active",
        "waiting",
        "outcome",
        "committed",
        "verification_complete",
        "activation_allowed",
        "recovery_required",
        "retry_safe",
        "usable_closures",
        "unresolved_repairs",
        "next_actions",
        "plan"
      ],
      "allOf": [
        {
          "if": {
            "properties": {
              "outcome": {
                "const": "repaired"
              }
            }
          },
          "then": {
            "properties": {
              "unresolved_repairs": {
                "maxItems": 0
              },
              "recovery_required": {
                "const": false
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "outcome": {
                "const": "replacement_prepared_activation_blocked"
              }
            }
          },
          "then": {
            "properties": {
              "activation_allowed": {
                "const": false
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "committed": {
                "const": true
              }
            }
          },
          "then": {
            "properties": {
              "retry_safe": {
                "const": false
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "phase": {
                "const": "committed"
              }
            }
          },
          "then": {
            "properties": {
              "committed": {
                "const": true
              }
            }
          }
        }
      ]
    },
    "security_findings": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "advisory_id": {
            "type": "string",
            "minLength": 1
          },
          "repository_identity": {
            "type": "string",
            "minLength": 1
          },
          "artifact_id": {
            "type": "string",
            "pattern": "^sha256:[0-9a-f]{64}$"
          },
          "component": {
            "anyOf": [
              {
                "type": "string",
                "minLength": 1
              },
              {
                "type": "null"
              }
            ]
          },
          "vulnerability": {
            "enum": [
              "vulnerable",
              "fixed",
              "not-affected",
              "unknown"
            ]
          },
          "remediation": {
            "enum": [
              "fix-available",
              "held",
              "unavailable"
            ]
          },
          "reason": {
            "type": "string",
            "minLength": 1
          },
          "dependency_path": {
            "type": "array",
            "items": {
              "type": "string",
              "pattern": "^[a-z0-9][a-z0-9-]*:[a-z0-9][a-z0-9-]*$"
            },
            "minItems": 0,
            "uniqueItems": true
          },
          "retained_generation": {
            "anyOf": [
              {
                "type": "string",
                "pattern": "^sha256:[0-9a-f]{64}$"
              },
              {
                "type": "null"
              }
            ]
          }
        },
        "required": [
          "advisory_id",
          "repository_identity",
          "artifact_id",
          "component",
          "vulnerability",
          "remediation",
          "reason",
          "dependency_path",
          "retained_generation"
        ]
      },
      "minItems": 0,
      "uniqueItems": true
    },
    "restart_findings": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "action": {
            "enum": [
              "restart",
              "consumer-rebuild",
              "reboot",
              "unknown"
            ]
          },
          "process_id": {
            "anyOf": [
              {
                "type": "integer",
                "minimum": 0,
                "maximum": 9007199254740991
              },
              {
                "type": "null"
              }
            ]
          },
          "service": {
            "anyOf": [
              {
                "type": "string",
                "minLength": 1
              },
              {
                "type": "null"
              }
            ]
          },
          "artifact_id": {
            "anyOf": [
              {
                "type": "string",
                "pattern": "^sha256:[0-9a-f]{64}$"
              },
              {
                "type": "null"
              }
            ]
          },
          "advisory_ids": {
            "type": "array",
            "items": {
              "type": "string",
              "minLength": 1
            },
            "minItems": 0,
            "uniqueItems": true
          },
          "reason": {
            "type": "string",
            "minLength": 1
          }
        },
        "required": [
          "action",
          "process_id",
          "service",
          "artifact_id",
          "advisory_ids",
          "reason"
        ]
      },
      "minItems": 0,
      "uniqueItems": true
    },
    "inspection_coverage": {
      "enum": [
        "not-requested",
        "complete",
        "partial",
        "unknown"
      ]
    },
    "exit_code": {
      "enum": [
        0,
        1,
        2,
        3
      ]
    }
  },
  "required": [
    "format",
    "command",
    "role",
    "instance_id",
    "status",
    "errors",
    "data",
    "security_findings",
    "restart_findings",
    "inspection_coverage",
    "exit_code"
  ],
  "allOf": [
    {
      "if": {
        "properties": {
          "status": {
            "const": "ok"
          }
        }
      },
      "then": {
        "properties": {
          "errors": {
            "maxItems": 0
          }
        }
      },
      "else": {
        "properties": {
          "errors": {
            "minItems": 1
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "status": {
            "const": "incomplete-remediation"
          }
        }
      },
      "then": {
        "properties": {
          "exit_code": {
            "const": 3
          },
          "security_findings": {
            "minItems": 1
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "status": {
            "const": "ok"
          }
        }
      },
      "then": {
        "properties": {
          "exit_code": {
            "const": 0
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "status": {
            "const": "refused"
          }
        }
      },
      "then": {
        "properties": {
          "exit_code": {
            "const": 2
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "status": {
            "const": "error"
          }
        }
      },
      "then": {
        "properties": {
          "exit_code": {
            "const": 1
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "status": {
            "const": "needs-attention"
          }
        }
      },
      "then": {
        "properties": {
          "exit_code": {
            "const": 3
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "command": {
            "const": "needs-restarting"
          },
          "status": {
            "const": "ok"
          }
        }
      },
      "then": {
        "properties": {
          "inspection_coverage": {
            "const": "complete"
          },
          "restart_findings": {
            "maxItems": 0
          }
        }
      }
    }
  ]
}
