Skip to content

The open-source app everyone uses to manage agents at work

146 KB
Raw Blame
// Generated by scripts/generate-protocol-schema-module.mjs. Do not edit.

export const authCredentialsSchema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v1/auth-credentials.schema.json",
  "title": "PRP credential update notification",
  "description": "Content-free evidence of changed credentials for the harness-auth binding in this normalized session. Credential-store detection does not prove an OAuth refresh succeeded.",
  "type": "object",
  "required": [
    "schema",
    "provider",
    "bindingId",
    "revision",
    "detection"
  ],
  "properties": {
    "schema": {
      "const": "paperclip.auth.credentials.updated.v1"
    },
    "provider": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
    },
    "bindingId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
    },
    "revision": {
      "type": "integer",
      "minimum": 1,
      "maximum": 9007199254740991
    },
    "detection": {
      "enum": [
        "credential_store",
        "native_hook"
      ]
    }
  },
  "additionalProperties": false
} as const;

export const identitySchema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v1/identity.schema.json",
  "title": "PRP identity graph",
  "type": "object",
  "required": [
    "schema",
    "companyId",
    "issueId",
    "runId",
    "environmentLeaseId",
    "runnerInstanceId",
    "normalizedSessionId",
    "driverSessionId"
  ],
  "properties": {
    "schema": {
      "const": "paperclip.prp.identity.v1"
    },
    "companyId": {
      "$ref": "#/$defs/stableId"
    },
    "issueId": {
      "$ref": "#/$defs/stableId"
    },
    "runId": {
      "$ref": "#/$defs/stableId"
    },
    "environmentLeaseId": {
      "$ref": "#/$defs/stableId"
    },
    "runnerInstanceId": {
      "$ref": "#/$defs/stableId"
    },
    "normalizedSessionId": {
      "$ref": "#/$defs/stableId"
    },
    "driverSessionId": {
      "$ref": "#/$defs/stableId"
    },
    "providerSessionId": {
      "$ref": "#/$defs/stableId"
    }
  },
  "additionalProperties": true,
  "$defs": {
    "stableId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
    }
  }
} as const;

export const capabilitiesSchema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v1/capabilities.schema.json",
  "title": "PRP negotiated capabilities",
  "type": "object",
  "required": [
    "schema",
    "sessionReusePolicy",
    "driver",
    "steer",
    "interrupt",
    "resume",
    "runtimeRequests",
    "structuredResult",
    "typedEvents"
  ],
  "properties": {
    "schema": {
      "const": "paperclip.prp.capabilities.v1"
    },
    "sessionReusePolicy": {
      "enum": [
        "new_per_run",
        "reuse_per_issue",
        "reuse_per_workspace"
      ]
    },
    "driver": {
      "type": "object",
      "required": [
        "kind",
        "version"
      ],
      "properties": {
        "kind": {
          "type": "string",
          "minLength": 1,
          "maxLength": 80
        },
        "version": {
          "type": "string",
          "minLength": 1,
          "maxLength": 80
        }
      },
      "additionalProperties": true
    },
    "steer": {
      "type": "boolean"
    },
    "interrupt": {
      "type": "boolean"
    },
    "resume": {
      "type": "boolean"
    },
    "runtimeRequests": {
      "type": "boolean"
    },
    "structuredResult": {
      "type": "boolean"
    },
    "typedEvents": {
      "type": "boolean"
    },
    "typedEventFamilies": {
      "type": "array",
      "maxItems": 64,
      "items": {
        "type": "object",
        "required": [
          "family",
          "version",
          "availability",
          "detailLevel"
        ],
        "properties": {
          "family": {
            "type": "string",
            "minLength": 1,
            "maxLength": 160,
            "pattern": "^[a-z][a-z0-9_.-]*$"
          },
          "version": {
            "const": 1
          },
          "availability": {
            "enum": [
              "available",
              "unsupported",
              "policy_disabled"
            ]
          },
          "detailLevel": {
            "enum": [
              "summary",
              "structured"
            ]
          }
        },
        "additionalProperties": false
      }
    },
    "semanticTools": {
      "type": "object",
      "required": [
        "schema",
        "schemaVersion",
        "operations"
      ],
      "properties": {
        "schema": {
          "const": "paperclip.prp.semantic_tools.v1"
        },
        "schemaVersion": {
          "const": 1
        },
        "operations": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "operationId",
              "version",
              "availability",
              "redactionDisposition"
            ],
            "properties": {
              "operationId": {
                "type": "string",
                "minLength": 1,
                "maxLength": 160,
                "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
              },
              "version": {
                "const": 1
              },
              "availability": {
                "enum": [
                  "available",
                  "denied",
                  "unsupported"
                ]
              },
              "redactionDisposition": {
                "const": "digest_only"
              },
              "requiredClaims": {
                "type": "array",
                "items": {
                  "type": "string",
                  "minLength": 1,
                  "maxLength": 160
                },
                "uniqueItems": true
              },
              "reasonCode": {
                "type": "string",
                "minLength": 1,
                "maxLength": 160,
                "pattern": "^[a-z][a-z0-9_.:-]*$"
              }
            },
            "additionalProperties": true
          }
        }
      },
      "additionalProperties": true
    },
    "unsupported": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1
      },
      "uniqueItems": true
    }
  },
  "additionalProperties": true
} as const;

export const capabilitiesV2Schema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v2/capabilities.schema.json",
  "title": "PRP v2 negotiated capabilities",
  "type": "object",
  "required": [
    "schema",
    "sessionReusePolicy",
    "driver",
    "steer",
    "interrupt",
    "resume",
    "runtimeRequests",
    "structuredResult",
    "typedEvents",
    "sessionGoals"
  ],
  "properties": {
    "schema": {
      "const": "paperclip.prp.capabilities.v2"
    },
    "sessionReusePolicy": {
      "enum": [
        "new_per_run",
        "reuse_per_issue",
        "reuse_per_workspace"
      ]
    },
    "driver": {
      "type": "object",
      "required": [
        "kind",
        "version"
      ],
      "properties": {
        "kind": {
          "type": "string",
          "minLength": 1,
          "maxLength": 80
        },
        "version": {
          "type": "string",
          "minLength": 1,
          "maxLength": 80
        }
      },
      "additionalProperties": true
    },
    "steer": {
      "type": "boolean"
    },
    "interrupt": {
      "type": "boolean"
    },
    "resume": {
      "type": "boolean"
    },
    "runtimeRequests": {
      "type": "boolean"
    },
    "structuredResult": {
      "type": "boolean"
    },
    "typedEvents": {
      "type": "boolean"
    },
    "sessionGoals": {
      "$ref": "https://paperclip.dev/schemas/prp/v2/session-goal.schema.json#/$defs/capability"
    },
    "unsupported": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1
      },
      "uniqueItems": true
    }
  },
  "additionalProperties": true
} as const;

export const capabilitiesV3Schema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v3/capabilities.schema.json",
  "title": "PRP v3 negotiated capabilities",
  "type": "object",
  "required": [
    "schema",
    "sessionReusePolicy",
    "driver",
    "steer",
    "interrupt",
    "resume",
    "runtimeRequests",
    "structuredResult",
    "typedEvents",
    "sessionGoals"
  ],
  "properties": {
    "schema": {
      "const": "paperclip.prp.capabilities.v3"
    },
    "sessionReusePolicy": {
      "enum": [
        "new_per_run",
        "reuse_per_issue",
        "reuse_per_workspace"
      ]
    },
    "driver": {
      "type": "object",
      "required": [
        "kind",
        "version"
      ],
      "properties": {
        "kind": {
          "type": "string",
          "minLength": 1,
          "maxLength": 80
        },
        "version": {
          "type": "string",
          "minLength": 1,
          "maxLength": 80
        }
      },
      "additionalProperties": true
    },
    "steer": {
      "type": "boolean"
    },
    "interrupt": {
      "type": "boolean"
    },
    "resume": {
      "type": "boolean"
    },
    "runtimeRequests": {
      "type": "boolean"
    },
    "structuredResult": {
      "type": "boolean"
    },
    "typedEvents": {
      "type": "boolean"
    },
    "sessionGoals": {
      "$ref": "https://paperclip.dev/schemas/prp/v2/session-goal.schema.json#/$defs/capability"
    },
    "unsupported": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1
      },
      "uniqueItems": true
    },
    "externalProvider": {
      "type": "boolean"
    }
  },
  "additionalProperties": true
} as const;

export const commandSchema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v1/command.schema.json",
  "title": "PRP runner command",
  "type": "object",
  "required": [
    "schema",
    "commandId",
    "controllerSeq",
    "type",
    "issuedAt",
    "payload"
  ],
  "properties": {
    "schema": {
      "const": "paperclip.prp.command.v1"
    },
    "commandId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "controllerSeq": {
      "type": "integer",
      "minimum": 1
    },
    "type": {
      "enum": [
        "run.prepare",
        "run.attach",
        "session.open",
        "turn.start",
        "turn.steer",
        "turn.interrupt",
        "turn.stop",
        "request.resolve",
        "interaction.receipt",
        "semantic_tool.result",
        "session.snapshot",
        "session.close",
        "session.budget.increase",
        "session.destroy",
        "run.cancel",
        "runner.drain",
        "runner.suspend",
        "runner.shutdown"
      ]
    },
    "issuedAt": {
      "type": "string",
      "format": "date-time"
    },
    "deadlineAt": {
      "type": "string",
      "format": "date-time"
    },
    "precondition": {
      "type": "object",
      "properties": {
        "runnerState": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "runState": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "sessionState": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "activeTurnId": {
          "type": [
            "string",
            "null"
          ]
        }
      },
      "additionalProperties": true
    },
    "payload": {
      "type": "object",
      "additionalProperties": true
    }
  },
  "additionalProperties": true
} as const;

export const commandV2Schema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v2/command.schema.json",
  "title": "PRP v2 runner command",
  "type": "object",
  "required": [
    "schema",
    "commandId",
    "controllerSeq",
    "type",
    "issuedAt",
    "payload"
  ],
  "properties": {
    "schema": {
      "const": "paperclip.prp.command.v2"
    },
    "commandId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "controllerSeq": {
      "type": "integer",
      "minimum": 1
    },
    "type": {
      "enum": [
        "run.prepare",
        "run.attach",
        "session.open",
        "turn.start",
        "turn.steer",
        "turn.interrupt",
        "turn.stop",
        "request.resolve",
        "interaction.receipt",
        "semantic_tool.result",
        "session.snapshot",
        "session.close",
        "session.budget.increase",
        "session.destroy",
        "run.cancel",
        "runner.drain",
        "runner.suspend",
        "runner.shutdown",
        "session.goal.get",
        "session.goal.set",
        "session.goal.clear"
      ]
    },
    "issuedAt": {
      "type": "string",
      "format": "date-time"
    },
    "deadlineAt": {
      "type": "string",
      "format": "date-time"
    },
    "precondition": {
      "type": "object",
      "properties": {
        "runnerState": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "runState": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "sessionState": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "activeTurnId": {
          "type": [
            "string",
            "null"
          ]
        }
      },
      "additionalProperties": true
    },
    "payload": {
      "type": "object",
      "additionalProperties": true
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "type": {
            "const": "session.goal.set"
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "type": "object",
            "properties": {
              "requestId": {
                "type": "string",
                "minLength": 1,
                "maxLength": 160
              },
              "objective": {
                "type": "string",
                "minLength": 1,
                "maxLength": 4000
              },
              "status": {
                "enum": [
                  "active",
                  "paused"
                ]
              },
              "tokenBudget": {
                "type": [
                  "integer",
                  "null"
                ],
                "minimum": 1
              }
            },
            "additionalProperties": true
          }
        }
      }
    }
  ],
  "additionalProperties": true
} as const;

export const commandV3Schema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v3/command.schema.json",
  "title": "PRP v3 runner command",
  "type": "object",
  "required": [
    "schema",
    "commandId",
    "controllerSeq",
    "type",
    "issuedAt",
    "payload"
  ],
  "properties": {
    "schema": {
      "const": "paperclip.prp.command.v3"
    },
    "commandId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "controllerSeq": {
      "type": "integer",
      "minimum": 1
    },
    "type": {
      "enum": [
        "run.prepare",
        "run.attach",
        "session.open",
        "turn.start",
        "turn.steer",
        "turn.interrupt",
        "turn.stop",
        "request.resolve",
        "interaction.receipt",
        "semantic_tool.result",
        "session.snapshot",
        "session.close",
        "session.budget.increase",
        "session.destroy",
        "run.cancel",
        "runner.drain",
        "runner.suspend",
        "runner.shutdown",
        "session.goal.get",
        "session.goal.set",
        "session.goal.clear",
        "external_provider.operation"
      ]
    },
    "issuedAt": {
      "type": "string",
      "format": "date-time"
    },
    "deadlineAt": {
      "type": "string",
      "format": "date-time"
    },
    "precondition": {
      "type": "object",
      "properties": {
        "runnerState": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "runState": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "sessionState": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "activeTurnId": {
          "type": [
            "string",
            "null"
          ]
        }
      },
      "additionalProperties": true
    },
    "payload": {
      "type": "object",
      "additionalProperties": true
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "type": {
            "const": "session.goal.set"
          }
        },
        "type": "object"
      },
      "then": {
        "properties": {
          "payload": {
            "type": "object",
            "properties": {
              "requestId": {
                "type": "string",
                "minLength": 1,
                "maxLength": 160
              },
              "objective": {
                "type": "string",
                "minLength": 1,
                "maxLength": 4000
              },
              "status": {
                "enum": [
                  "active",
                  "paused"
                ]
              },
              "tokenBudget": {
                "type": [
                  "integer",
                  "null"
                ],
                "minimum": 1
              }
            },
            "additionalProperties": true
          }
        },
        "type": "object"
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "external_provider.operation"
          }
        },
        "type": "object"
      },
      "then": {
        "properties": {
          "payload": {
            "type": "object",
            "required": [
              "requestId",
              "bindingId",
              "runId",
              "normalizedSessionId",
              "turnId",
              "bindingGeneration",
              "assignmentRevision",
              "digest",
              "action",
              "input"
            ],
            "properties": {
              "requestId": {
                "type": "string",
                "minLength": 1,
                "maxLength": 240
              },
              "bindingId": {
                "type": "string",
                "minLength": 1,
                "maxLength": 240
              },
              "runId": {
                "type": "string",
                "minLength": 1,
                "maxLength": 240
              },
              "normalizedSessionId": {
                "type": "string",
                "minLength": 1,
                "maxLength": 240
              },
              "turnId": {
                "type": "string",
                "minLength": 1,
                "maxLength": 240
              },
              "bindingGeneration": {
                "type": "integer",
                "minimum": 1
              },
              "assignmentRevision": {
                "type": "integer",
                "minimum": 1
              },
              "digest": {
                "type": "string",
                "pattern": "^sha256:[0-9a-f]{64}$"
              },
              "action": {
                "enum": [
                  "accept",
                  "tool",
                  "progress",
                  "finish",
                  "renew",
                  "request_user_input",
                  "consume_input"
                ]
              },
              "input": {
                "type": "object"
              }
            },
            "additionalProperties": false
          }
        },
        "type": "object"
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "external_provider.operation"
          },
          "payload": {
            "properties": {
              "action": {
                "const": "accept"
              }
            },
            "type": "object"
          }
        },
        "type": "object"
      },
      "then": {
        "properties": {
          "payload": {
            "properties": {
              "input": {
                "type": "object",
                "properties": {},
                "required": [],
                "additionalProperties": false
              }
            },
            "type": "object"
          }
        },
        "type": "object"
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "external_provider.operation"
          },
          "payload": {
            "properties": {
              "action": {
                "const": "tool"
              }
            },
            "type": "object"
          }
        },
        "type": "object"
      },
      "then": {
        "properties": {
          "payload": {
            "properties": {
              "input": {
                "type": "object",
                "properties": {
                  "name": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 160
                  },
                  "arguments": {
                    "type": "object"
                  }
                },
                "required": [
                  "name",
                  "arguments"
                ],
                "additionalProperties": false
              }
            },
            "type": "object"
          }
        },
        "type": "object"
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "external_provider.operation"
          },
          "payload": {
            "properties": {
              "action": {
                "const": "progress"
              }
            },
            "type": "object"
          }
        },
        "type": "object"
      },
      "then": {
        "properties": {
          "payload": {
            "properties": {
              "input": {
                "type": "object",
                "properties": {
                  "text": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 12000
                  }
                },
                "required": [
                  "text"
                ],
                "additionalProperties": false
              }
            },
            "type": "object"
          }
        },
        "type": "object"
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "external_provider.operation"
          },
          "payload": {
            "properties": {
              "action": {
                "const": "finish"
              }
            },
            "type": "object"
          }
        },
        "type": "object"
      },
      "then": {
        "properties": {
          "payload": {
            "properties": {
              "input": {
                "type": "object",
                "properties": {
                  "result": {
                    "type": "object"
                  }
                },
                "required": [
                  "result"
                ],
                "additionalProperties": false
              }
            },
            "type": "object"
          }
        },
        "type": "object"
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "external_provider.operation"
          },
          "payload": {
            "properties": {
              "action": {
                "const": "renew"
              }
            },
            "type": "object"
          }
        },
        "type": "object"
      },
      "then": {
        "properties": {
          "payload": {
            "properties": {
              "input": {
                "type": "object",
                "properties": {
                  "expiresAtUnixMs": {
                    "type": "integer",
                    "minimum": 1
                  }
                },
                "required": [
                  "expiresAtUnixMs"
                ],
                "additionalProperties": false
              }
            },
            "type": "object"
          }
        },
        "type": "object"
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "external_provider.operation"
          },
          "payload": {
            "properties": {
              "action": {
                "const": "request_user_input"
              }
            },
            "type": "object"
          }
        },
        "type": "object"
      },
      "then": {
        "properties": {
          "payload": {
            "properties": {
              "input": {
                "type": "object",
                "properties": {
                  "requestId": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 240
                  },
                  "questionSet": {
                    "$ref": "https://paperclip.dev/schemas/prp/v1/question-set.schema.json"
                  }
                },
                "required": [
                  "requestId",
                  "questionSet"
                ],
                "additionalProperties": false
              }
            },
            "type": "object"
          }
        },
        "type": "object"
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "external_provider.operation"
          },
          "payload": {
            "properties": {
              "action": {
                "const": "consume_input"
              }
            },
            "type": "object"
          }
        },
        "type": "object"
      },
      "then": {
        "properties": {
          "payload": {
            "properties": {
              "input": {
                "type": "object",
                "properties": {
                  "requestId": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 240
                  },
                  "inputDigest": {
                    "type": "string",
                    "pattern": "^sha256:[0-9a-f]{64}$"
                  }
                },
                "required": [
                  "requestId",
                  "inputDigest"
                ],
                "additionalProperties": false
              }
            },
            "type": "object"
          }
        },
        "type": "object"
      }
    }
  ],
  "additionalProperties": true
} as const;

export const providerDescriptorSchema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v1/provider-descriptor.schema.json",
  "title": "PRP provider runtime descriptor",
  "type": "object",
  "required": [
    "provider",
    "driver",
    "model",
    "executionKind",
    "providerVersion"
  ],
  "properties": {
    "provider": {
      "enum": [
        "codex",
        "opencode",
        "claude_managed",
        "aws_agentcore",
        "acpx",
        "openai_dot",
        "grok_bot",
        "muse"
      ]
    },
    "driver": {
      "enum": [
        "codex_app_server",
        "opencode_server",
        "claude_managed_agents_api",
        "aws_agentcore_harness_api",
        "acpx_runtime",
        "openai_dot_mcp",
        "grok_bot_mcp",
        "muse_external"
      ]
    },
    "model": {
      "type": [
        "string",
        "null"
      ],
      "maxLength": 240
    },
    "executionKind": {
      "enum": [
        "local_process",
        "remote_service"
      ]
    },
    "providerVersion": {
      "type": "string",
      "minLength": 1,
      "maxLength": 120
    },
    "service": {
      "enum": [
        "anthropic_managed_agents",
        "aws_bedrock_agentcore_harness",
        "openai_dot",
        "grok_bot",
        "muse"
      ]
    },
    "providerSessionId": {
      "type": [
        "string",
        "null"
      ],
      "maxLength": 240
    },
    "processId": {
      "type": [
        "integer",
        "null"
      ],
      "minimum": 1
    },
    "agentProcessId": {
      "type": [
        "integer",
        "null"
      ],
      "minimum": 1
    },
    "endpointArn": {
      "type": "string",
      "minLength": 1,
      "maxLength": 2048
    },
    "endpointQualifier": {
      "type": "string",
      "minLength": 1,
      "maxLength": 240
    },
    "memoryId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 240
    },
    "eventExpiryDays": {
      "const": 90
    },
    "agent": {
      "enum": [
        "pi",
        "claude",
        "codex",
        "cursor",
        "copilot"
      ]
    },
    "requestedModel": {
      "type": "string",
      "minLength": 1,
      "maxLength": 240
    },
    "acpProtocolVersion": {
      "const": 1
    },
    "agentServerPackage": {
      "type": "string",
      "minLength": 1,
      "maxLength": 240
    },
    "agentServerVersion": {
      "type": "string",
      "minLength": 1,
      "maxLength": 120
    },
    "agentRuntimePackage": {
      "type": [
        "string",
        "null"
      ],
      "maxLength": 240
    },
    "agentRuntimeVersion": {
      "type": [
        "string",
        "null"
      ],
      "maxLength": 120
    },
    "acpxRecordId": {
      "type": [
        "string",
        "null"
      ],
      "maxLength": 240
    },
    "turnControls": {
      "$ref": "#/$defs/turnControls"
    },
    "piThinkingLevel": {
      "enum": [
        "off",
        "low",
        "high",
        "max"
      ]
    },
    "claudeEffort": {
      "enum": [
        "low",
        "medium",
        "high",
        "xhigh",
        "max"
      ]
    }
  },
  "allOf": [
    {
      "if": {
        "required": [
          "piThinkingLevel"
        ]
      },
      "then": {
        "properties": {
          "provider": {
            "const": "acpx"
          },
          "agent": {
            "const": "pi"
          }
        },
        "required": [
          "agent"
        ]
      }
    },
    {
      "if": {
        "required": [
          "claudeEffort"
        ]
      },
      "then": {
        "properties": {
          "provider": {
            "const": "acpx"
          },
          "agent": {
            "const": "claude"
          }
        },
        "required": [
          "agent"
        ]
      }
    },
    {
      "oneOf": [
        {
          "properties": {
            "provider": {
              "const": "codex"
            },
            "driver": {
              "const": "codex_app_server"
            },
            "executionKind": {
              "const": "local_process"
            }
          }
        },
        {
          "properties": {
            "provider": {
              "const": "opencode"
            },
            "driver": {
              "const": "opencode_server"
            },
            "executionKind": {
              "const": "local_process"
            }
          }
        },
        {
          "properties": {
            "provider": {
              "const": "claude_managed"
            },
            "driver": {
              "const": "claude_managed_agents_api"
            },
            "executionKind": {
              "const": "remote_service"
            }
          }
        },
        {
          "properties": {
            "provider": {
              "const": "aws_agentcore"
            },
            "driver": {
              "const": "aws_agentcore_harness_api"
            },
            "executionKind": {
              "const": "remote_service"
            }
          }
        },
        {
          "properties": {
            "provider": {
              "const": "acpx"
            },
            "driver": {
              "const": "acpx_runtime"
            },
            "executionKind": {
              "const": "local_process"
            }
          }
        },
        {
          "properties": {
            "provider": {
              "const": "openai_dot"
            },
            "driver": {
              "const": "openai_dot_mcp"
            },
            "executionKind": {
              "const": "remote_service"
            }
          }
        },
        {
          "properties": {
            "provider": {
              "const": "grok_bot"
            },
            "driver": {
              "const": "grok_bot_mcp"
            },
            "executionKind": {
              "const": "remote_service"
            }
          }
        },
        {
          "properties": {
            "provider": {
              "const": "muse"
            },
            "driver": {
              "const": "muse_external"
            },
            "executionKind": {
              "const": "remote_service"
            }
          }
        }
      ]
    },
    {
      "if": {
        "properties": {
          "executionKind": {
            "const": "remote_service"
          }
        }
      },
      "then": {
        "required": [
          "service"
        ],
        "properties": {
          "processId": {
            "const": null
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "executionKind": {
            "const": "local_process"
          }
        }
      },
      "then": {
        "properties": {
          "service": false
        }
      }
    },
    {
      "if": {
        "properties": {
          "provider": {
            "const": "claude_managed"
          }
        },
        "required": [
          "provider"
        ]
      },
      "then": {
        "properties": {
          "service": {
            "const": "anthropic_managed_agents"
          }
        },
        "required": [
          "service"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "provider": {
            "const": "aws_agentcore"
          }
        },
        "required": [
          "provider"
        ]
      },
      "then": {
        "properties": {
          "service": {
            "const": "aws_bedrock_agentcore_harness"
          }
        },
        "required": [
          "service",
          "endpointArn",
          "endpointQualifier",
          "memoryId",
          "eventExpiryDays"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "provider": {
            "const": "acpx"
          }
        },
        "required": [
          "provider"
        ]
      },
      "then": {
        "required": [
          "agent",
          "requestedModel",
          "acpProtocolVersion",
          "agentServerPackage",
          "agentServerVersion",
          "acpxRecordId",
          "agentProcessId"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "provider": {
            "const": "openai_dot"
          }
        },
        "required": [
          "provider"
        ]
      },
      "then": {
        "required": [
          "service",
          "providerSessionId"
        ],
        "properties": {
          "service": {
            "const": "openai_dot"
          },
          "model": {
            "const": null
          },
          "providerSessionId": {
            "const": null
          },
          "agentProcessId": {
            "const": null
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "provider": {
            "const": "grok_bot"
          }
        },
        "required": [
          "provider"
        ]
      },
      "then": {
        "required": [
          "service",
          "providerSessionId"
        ],
        "properties": {
          "service": {
            "const": "grok_bot"
          },
          "model": {
            "const": null
          },
          "providerSessionId": {
            "const": null
          },
          "agentProcessId": {
            "const": null
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "provider": {
            "const": "muse"
          }
        },
        "required": [
          "provider"
        ]
      },
      "then": {
        "required": [
          "service",
          "providerSessionId"
        ],
        "properties": {
          "service": {
            "const": "muse"
          },
          "model": {
            "const": null
          },
          "providerSessionId": {
            "const": null
          },
          "agentProcessId": {
            "const": null
          }
        }
      }
    }
  ],
  "$defs": {
    "turnControls": {
      "type": "object",
      "required": [
        "steering",
        "queuedFollowUp"
      ],
      "properties": {
        "steering": {
          "type": "boolean"
        },
        "queuedFollowUp": {
          "type": "boolean"
        }
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": false
} as const;

export const providerEventSchema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v1/provider-event.schema.json",
  "title": "Provider-neutral runner activity payload",
  "oneOf": [
    {
      "$ref": "#/$defs/plan"
    },
    {
      "$ref": "#/$defs/toolExecution"
    },
    {
      "$ref": "#/$defs/research"
    },
    {
      "$ref": "#/$defs/delegation"
    },
    {
      "$ref": "#/$defs/modelRoute"
    },
    {
      "$ref": "#/$defs/modelVerification"
    },
    {
      "$ref": "#/$defs/contextCompacted"
    },
    {
      "$ref": "#/$defs/artifactViewed"
    },
    {
      "$ref": "#/$defs/artifactGenerated"
    },
    {
      "$ref": "#/$defs/reviewMode"
    },
    {
      "$ref": "#/$defs/hook"
    },
    {
      "$ref": "#/$defs/memoryCitation"
    },
    {
      "$ref": "#/$defs/safetyReview"
    },
    {
      "$ref": "#/$defs/terminalInput"
    },
    {
      "$ref": "#/$defs/wait"
    },
    {
      "$ref": "#/$defs/providerNotice"
    }
  ],
  "$defs": {
    "id": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
    },
    "shortText": {
      "type": "string",
      "maxLength": 4000
    },
    "nullableShortText": {
      "type": [
        "string",
        "null"
      ],
      "maxLength": 4000
    },
    "safePath": {
      "type": [
        "string",
        "null"
      ],
      "maxLength": 4096,
      "pattern": "^(?!/)(?!.*(?:^|/)\\.\\.(?:/|$)).*$"
    },
    "safeUrl": {
      "type": [
        "string",
        "null"
      ],
      "maxLength": 8192,
      "pattern": "^https?://"
    },
    "plan": {
      "type": "object",
      "description": "A complete provider-authored within-turn checklist snapshot. Consumers replace the prior snapshot with the same planId in PRP sourceSeq order; this is not Paperclip's durable Plan document.",
      "required": [
        "schema",
        "planId",
        "revision",
        "steps",
        "complete",
        "syncStatus"
      ],
      "properties": {
        "schema": {
          "const": "paperclip.plan.updated.v1"
        },
        "planId": {
          "$ref": "#/$defs/id"
        },
        "revision": {
          "type": "integer",
          "minimum": 1
        },
        "explanation": {
          "$ref": "#/$defs/nullableShortText"
        },
        "steps": {
          "type": "array",
          "maxItems": 256,
          "items": {
            "type": "object",
            "required": [
              "stepId",
              "body",
              "status"
            ],
            "properties": {
              "stepId": {
                "$ref": "#/$defs/id"
              },
              "body": {
                "$ref": "#/$defs/shortText"
              },
              "status": {
                "enum": [
                  "pending",
                  "in_progress",
                  "completed",
                  "blocked"
                ]
              }
            },
            "additionalProperties": false
          }
        },
        "complete": {
          "type": "boolean"
        },
        "syncStatus": {
          "description": "Legacy compatibility field. Within-turn checklist snapshots use not_applicable.",
          "enum": [
            "streaming",
            "pending",
            "synchronized",
            "conflict",
            "not_applicable"
          ]
        },
        "documentRevision": {
          "description": "Legacy compatibility field. Within-turn checklist snapshots use null.",
          "type": [
            "integer",
            "null"
          ],
          "minimum": 1
        }
      },
      "additionalProperties": false
    },
    "toolExecution": {
      "type": "object",
      "required": [
        "schema",
        "executionId",
        "transport",
        "operation",
        "status",
        "output",
        "outputBytes",
        "outputTruncated",
        "outputDigest"
      ],
      "properties": {
        "schema": {
          "const": "paperclip.tool.execution.v1"
        },
        "executionId": {
          "$ref": "#/$defs/id"
        },
        "transport": {
          "enum": [
            "process",
            "mcp",
            "dynamic",
            "builtin"
          ]
        },
        "operation": {
          "enum": [
            "read",
            "search",
            "list",
            "execute",
            "edit",
            "unknown"
          ]
        },
        "name": {
          "$ref": "#/$defs/nullableShortText"
        },
        "target": {
          "$ref": "#/$defs/safePath"
        },
        "namespace": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 240
        },
        "readOnly": {
          "type": [
            "boolean",
            "null"
          ]
        },
        "inputUpdated": {
          "type": "boolean"
        },
        "status": {
          "enum": [
            "running",
            "completed",
            "failed",
            "cancelled",
            "interrupted"
          ]
        },
        "durationMs": {
          "type": [
            "integer",
            "null"
          ],
          "minimum": 0
        },
        "exitCode": {
          "type": [
            "integer",
            "null"
          ]
        },
        "progress": {
          "$ref": "#/$defs/nullableShortText"
        },
        "output": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 65536
        },
        "outputBytes": {
          "type": "integer",
          "minimum": 0
        },
        "outputTruncated": {
          "type": "boolean"
        },
        "outputDigest": {
          "type": [
            "string",
            "null"
          ],
          "pattern": "^sha256:[a-f0-9]{64}$"
        }
      },
      "additionalProperties": false
    },
    "research": {
      "type": "object",
      "required": [
        "schema",
        "researchId",
        "action",
        "status",
        "sources"
      ],
      "properties": {
        "schema": {
          "const": "paperclip.research.v1"
        },
        "researchId": {
          "$ref": "#/$defs/id"
        },
        "action": {
          "enum": [
            "search",
            "open_page",
            "find_in_page",
            "other"
          ]
        },
        "status": {
          "enum": [
            "running",
            "completed",
            "failed",
            "cancelled"
          ]
        },
        "query": {
          "$ref": "#/$defs/nullableShortText"
        },
        "url": {
          "$ref": "#/$defs/safeUrl"
        },
        "pattern": {
          "$ref": "#/$defs/nullableShortText"
        },
        "sources": {
          "type": "array",
          "maxItems": 64,
          "items": {
            "type": "object",
            "required": [
              "sourceId",
              "title",
              "url",
              "snippet"
            ],
            "properties": {
              "sourceId": {
                "$ref": "#/$defs/id"
              },
              "title": {
                "$ref": "#/$defs/shortText"
              },
              "url": {
                "$ref": "#/$defs/safeUrl"
              },
              "snippet": {
                "$ref": "#/$defs/nullableShortText"
              }
            },
            "additionalProperties": false
          }
        }
      },
      "additionalProperties": false
    },
    "delegation": {
      "type": "object",
      "required": [
        "schema",
        "delegationId",
        "action",
        "status",
        "children"
      ],
      "properties": {
        "schema": {
          "const": "paperclip.delegation.v1"
        },
        "delegationId": {
          "$ref": "#/$defs/id"
        },
        "action": {
          "enum": [
            "spawn",
            "message",
            "resume",
            "wait",
            "close"
          ]
        },
        "status": {
          "enum": [
            "running",
            "waiting",
            "completed",
            "failed",
            "interrupted",
            "closed"
          ]
        },
        "children": {
          "type": "array",
          "maxItems": 64,
          "items": {
            "type": "object",
            "required": [
              "childId",
              "role",
              "model",
              "status",
              "summary",
              "activitySummary"
            ],
            "properties": {
              "childId": {
                "$ref": "#/$defs/id"
              },
              "role": {
                "type": [
                  "string",
                  "null"
                ],
                "maxLength": 160
              },
              "model": {
                "type": [
                  "string",
                  "null"
                ],
                "maxLength": 240
              },
              "status": {
                "enum": [
                  "running",
                  "waiting",
                  "completed",
                  "failed",
                  "interrupted",
                  "closed"
                ]
              },
              "summary": {
                "$ref": "#/$defs/nullableShortText"
              },
              "activitySummary": {
                "$ref": "#/$defs/nullableShortText"
              }
            },
            "additionalProperties": false
          }
        }
      },
      "additionalProperties": false
    },
    "modelRoute": {
      "type": "object",
      "required": [
        "schema",
        "routeId",
        "provider",
        "requestedModel",
        "fromModel",
        "effectiveModel",
        "reason"
      ],
      "properties": {
        "schema": {
          "const": "paperclip.model.route_changed.v1"
        },
        "routeId": {
          "$ref": "#/$defs/id"
        },
        "provider": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160
        },
        "requestedModel": {
          "type": "string",
          "minLength": 1,
          "maxLength": 240
        },
        "fromModel": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 240
        },
        "effectiveModel": {
          "type": "string",
          "minLength": 1,
          "maxLength": 240
        },
        "reason": {
          "$ref": "#/$defs/shortText"
        }
      },
      "additionalProperties": false
    },
    "modelVerification": {
      "type": "object",
      "required": [
        "schema",
        "verificationId",
        "status",
        "classes",
        "buffering",
        "summary"
      ],
      "properties": {
        "schema": {
          "const": "paperclip.model.verification.v1"
        },
        "verificationId": {
          "$ref": "#/$defs/id"
        },
        "status": {
          "enum": [
            "running",
            "completed",
            "failed"
          ]
        },
        "classes": {
          "type": "array",
          "maxItems": 32,
          "items": {
            "type": "string",
            "maxLength": 160
          }
        },
        "buffering": {
          "type": "boolean"
        },
        "summary": {
          "$ref": "#/$defs/nullableShortText"
        }
      },
      "additionalProperties": false
    },
    "contextCompacted": {
      "type": "object",
      "required": [
        "schema",
        "compactionId",
        "reason",
        "preTokens",
        "postTokens",
        "sameSession"
      ],
      "properties": {
        "schema": {
          "const": "paperclip.context.compacted.v1"
        },
        "compactionId": {
          "$ref": "#/$defs/id"
        },
        "reason": {
          "enum": [
            "context_window",
            "manual",
            "provider",
            "unknown"
          ]
        },
        "preTokens": {
          "type": [
            "integer",
            "null"
          ],
          "minimum": 0
        },
        "postTokens": {
          "type": [
            "integer",
            "null"
          ],
          "minimum": 0
        },
        "sameSession": {
          "type": "boolean"
        }
      },
      "additionalProperties": false
    },
    "artifactViewed": {
      "type": "object",
      "required": [
        "schema",
        "artifactId",
        "reference",
        "mediaType",
        "title"
      ],
      "properties": {
        "schema": {
          "const": "paperclip.artifact.viewed.v1"
        },
        "artifactId": {
          "$ref": "#/$defs/id"
        },
        "reference": {
          "$ref": "#/$defs/safePath"
        },
        "mediaType": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 160
        },
        "title": {
          "$ref": "#/$defs/nullableShortText"
        }
      },
      "additionalProperties": false
    },
    "artifactGenerated": {
      "type": "object",
      "required": [
        "schema",
        "artifactId",
        "status",
        "reference",
        "mediaType",
        "registered",
        "failure"
      ],
      "properties": {
        "schema": {
          "const": "paperclip.artifact.generated.v1"
        },
        "artifactId": {
          "$ref": "#/$defs/id"
        },
        "status": {
          "enum": [
            "running",
            "completed",
            "failed"
          ]
        },
        "reference": {
          "$ref": "#/$defs/safePath"
        },
        "mediaType": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 160
        },
        "registered": {
          "type": "boolean"
        },
        "transparentBackground": {
          "type": [
            "boolean",
            "null"
          ]
        },
        "failure": {
          "$ref": "#/$defs/nullableShortText"
        }
      },
      "additionalProperties": false
    },
    "reviewMode": {
      "type": "object",
      "required": [
        "schema",
        "reviewId",
        "state",
        "scope"
      ],
      "properties": {
        "schema": {
          "const": "paperclip.review.mode_changed.v1"
        },
        "reviewId": {
          "$ref": "#/$defs/id"
        },
        "state": {
          "enum": [
            "entered",
            "exited"
          ]
        },
        "scope": {
          "$ref": "#/$defs/nullableShortText"
        }
      },
      "additionalProperties": false
    },
    "hook": {
      "type": "object",
      "required": [
        "schema",
        "hookId",
        "event",
        "scope",
        "status",
        "blocking",
        "durationMs",
        "summary"
      ],
      "properties": {
        "schema": {
          "const": "paperclip.hook.v1"
        },
        "hookId": {
          "$ref": "#/$defs/id"
        },
        "event": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160
        },
        "scope": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160
        },
        "status": {
          "enum": [
            "running",
            "completed",
            "failed",
            "cancelled"
          ]
        },
        "blocking": {
          "type": "boolean"
        },
        "durationMs": {
          "type": [
            "integer",
            "null"
          ],
          "minimum": 0
        },
        "summary": {
          "$ref": "#/$defs/nullableShortText"
        }
      },
      "additionalProperties": false
    },
    "memoryCitation": {
      "type": "object",
      "required": [
        "schema",
        "citationId",
        "messageItemId",
        "label",
        "available",
        "reference"
      ],
      "properties": {
        "schema": {
          "const": "paperclip.memory.citation.v1"
        },
        "citationId": {
          "$ref": "#/$defs/id"
        },
        "messageItemId": {
          "$ref": "#/$defs/id"
        },
        "label": {
          "$ref": "#/$defs/shortText"
        },
        "available": {
          "type": "boolean"
        },
        "reference": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 4096
        }
      },
      "additionalProperties": false
    },
    "safetyReview": {
      "type": "object",
      "required": [
        "schema",
        "reviewId",
        "targetExecutionId",
        "status",
        "decision",
        "summary"
      ],
      "properties": {
        "schema": {
          "const": "paperclip.safety.review.v1"
        },
        "reviewId": {
          "$ref": "#/$defs/id"
        },
        "targetExecutionId": {
          "anyOf": [
            {
              "$ref": "#/$defs/id"
            },
            {
              "type": "null"
            }
          ]
        },
        "status": {
          "enum": [
            "running",
            "completed",
            "failed"
          ]
        },
        "decision": {
          "enum": [
            "pending",
            "allowed",
            "denied",
            "user_required",
            "unknown"
          ]
        },
        "summary": {
          "$ref": "#/$defs/nullableShortText"
        }
      },
      "additionalProperties": false
    },
    "terminalInput": {
      "type": "object",
      "required": [
        "schema",
        "executionId",
        "origin",
        "inputClass",
        "byteCount"
      ],
      "properties": {
        "schema": {
          "const": "paperclip.terminal.input_sent.v1"
        },
        "executionId": {
          "$ref": "#/$defs/id"
        },
        "origin": {
          "enum": [
            "agent",
            "user",
            "system"
          ]
        },
        "inputClass": {
          "enum": [
            "text",
            "control",
            "eof"
          ]
        },
        "byteCount": {
          "type": "integer",
          "minimum": 0
        }
      },
      "additionalProperties": false
    },
    "wait": {
      "type": "object",
      "required": [
        "schema",
        "waitId",
        "reason",
        "status",
        "plannedDurationMs",
        "elapsedDurationMs"
      ],
      "properties": {
        "schema": {
          "const": "paperclip.wait.v1"
        },
        "waitId": {
          "$ref": "#/$defs/id"
        },
        "reason": {
          "enum": [
            "timer",
            "provider",
            "rate_limit",
            "unknown"
          ]
        },
        "status": {
          "enum": [
            "running",
            "completed",
            "interrupted",
            "cancelled"
          ]
        },
        "plannedDurationMs": {
          "type": [
            "integer",
            "null"
          ],
          "minimum": 0
        },
        "elapsedDurationMs": {
          "type": [
            "integer",
            "null"
          ],
          "minimum": 0
        }
      },
      "additionalProperties": false
    },
    "providerNotice": {
      "type": "object",
      "required": [
        "schema",
        "noticeId",
        "severity",
        "category",
        "scope",
        "recoverable",
        "userActionable",
        "summary"
      ],
      "properties": {
        "schema": {
          "const": "paperclip.provider.notice.v1"
        },
        "noticeId": {
          "$ref": "#/$defs/id"
        },
        "severity": {
          "enum": [
            "info",
            "warning",
            "error"
          ]
        },
        "category": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160
        },
        "scope": {
          "enum": [
            "turn",
            "session",
            "environment",
            "account"
          ]
        },
        "recoverable": {
          "type": "boolean"
        },
        "userActionable": {
          "type": "boolean"
        },
        "summary": {
          "$ref": "#/$defs/shortText"
        },
        "provenance": {
          "type": "object",
          "required": [
            "method",
            "eventType",
            "sessionId",
            "turnId"
          ],
          "properties": {
            "method": {
              "type": "string",
              "maxLength": 160
            },
            "eventType": {
              "type": "string",
              "maxLength": 160
            },
            "sessionId": {
              "type": "string",
              "maxLength": 240
            },
            "turnId": {
              "type": "string",
              "maxLength": 240
            },
            "agentId": {
              "type": "string",
              "maxLength": 240
            },
            "timestamp": {
              "type": "string",
              "maxLength": 80
            }
          },
          "additionalProperties": false
        },
        "details": {
          "type": "array",
          "maxItems": 64,
          "items": {
            "type": "object",
            "required": [
              "name",
              "value"
            ],
            "properties": {
              "name": {
                "type": "string",
                "minLength": 1,
                "maxLength": 160
              },
              "value": {
                "type": "string",
                "maxLength": 4000
              }
            },
            "additionalProperties": false
          }
        }
      },
      "additionalProperties": false
    }
  }
} as const;

export const workspaceDiffSchema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v1/workspace-diff.schema.json",
  "title": "Paperclip workspace diff",
  "type": "object",
  "required": [
    "schema",
    "changeSetId",
    "revision",
    "source",
    "complete",
    "files",
    "totals",
    "patchArtifactRef"
  ],
  "properties": {
    "schema": {
      "const": "paperclip.workspace.diff.v1"
    },
    "changeSetId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 200
    },
    "revision": {
      "type": "integer",
      "minimum": 1
    },
    "source": {
      "enum": [
        "harness_reported",
        "runner_verified"
      ]
    },
    "complete": {
      "type": "boolean"
    },
    "files": {
      "type": "array",
      "maxItems": 2000,
      "items": {
        "type": "object",
        "required": [
          "path",
          "operation",
          "previousPath",
          "additions",
          "deletions",
          "binary",
          "diff"
        ],
        "properties": {
          "path": {
            "type": "string",
            "minLength": 1,
            "maxLength": 1024,
            "pattern": "^(?!/)(?!.*(?:^|/)\\.\\.(?:/|$)).+$"
          },
          "operation": {
            "enum": [
              "create",
              "modify",
              "delete",
              "rename",
              "mode_change"
            ]
          },
          "previousPath": {
            "oneOf": [
              {
                "type": "null"
              },
              {
                "type": "string",
                "minLength": 1,
                "maxLength": 1024,
                "pattern": "^(?!/)(?!.*(?:^|/)\\.\\.(?:/|$)).+$"
              }
            ]
          },
          "additions": {
            "type": [
              "integer",
              "null"
            ],
            "minimum": 0
          },
          "deletions": {
            "type": [
              "integer",
              "null"
            ],
            "minimum": 0
          },
          "binary": {
            "type": "boolean"
          },
          "diff": {
            "type": [
              "string",
              "null"
            ],
            "maxLength": 262144
          }
        },
        "additionalProperties": false
      }
    },
    "totals": {
      "type": "object",
      "required": [
        "files",
        "additions",
        "deletions"
      ],
      "properties": {
        "files": {
          "type": "integer",
          "minimum": 0
        },
        "additions": {
          "type": [
            "integer",
            "null"
          ],
          "minimum": 0
        },
        "deletions": {
          "type": [
            "integer",
            "null"
          ],
          "minimum": 0
        }
      },
      "additionalProperties": false
    },
    "patchArtifactRef": {
      "type": [
        "string",
        "null"
      ],
      "maxLength": 2048
    }
  },
  "additionalProperties": false
} as const;

export const workspaceFileReferenceSchema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v1/workspace-file-reference.schema.json",
  "title": "Paperclip workspace file reference",
  "type": "object",
  "required": [
    "schema",
    "referenceId",
    "source",
    "path",
    "displayName",
    "mediaType",
    "presentation",
    "line",
    "preview",
    "previewTruncated",
    "contentDigest"
  ],
  "properties": {
    "schema": {
      "const": "paperclip.workspace.file_reference.v1"
    },
    "referenceId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 240
    },
    "source": {
      "enum": [
        "harness_reported",
        "runner_verified"
      ]
    },
    "path": {
      "type": "string",
      "minLength": 1,
      "maxLength": 1024,
      "pattern": "^(?!/)(?!.*(?:^|/)\\.\\.(?:/|$)).+$"
    },
    "displayName": {
      "type": "string",
      "minLength": 1,
      "maxLength": 255
    },
    "mediaType": {
      "type": [
        "string",
        "null"
      ],
      "maxLength": 160
    },
    "presentation": {
      "enum": [
        "document",
        "code",
        "image",
        "generic"
      ]
    },
    "line": {
      "type": [
        "integer",
        "null"
      ],
      "minimum": 1
    },
    "preview": {
      "type": [
        "string",
        "null"
      ],
      "maxLength": 262144
    },
    "previewTruncated": {
      "type": "boolean"
    },
    "contentDigest": {
      "oneOf": [
        {
          "type": "null"
        },
        {
          "type": "string",
          "pattern": "^sha256:[a-f0-9]{64}$"
        }
      ]
    }
  },
  "additionalProperties": false
} as const;

export const semanticToolSchema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v1/semantic-tool.schema.json",
  "title": "PRP provider-neutral semantic tool envelope",
  "type": "object",
  "required": [
    "schema",
    "schemaVersion",
    "phase",
    "operationId",
    "callId",
    "correlation",
    "idempotencyKey",
    "content"
  ],
  "properties": {
    "schema": {
      "const": "paperclip.prp.semantic_tool.v1"
    },
    "schemaVersion": {
      "const": 1
    },
    "phase": {
      "enum": [
        "input",
        "result",
        "reconciled"
      ]
    },
    "operationId": {
      "$ref": "#/$defs/stableId"
    },
    "callId": {
      "$ref": "#/$defs/stableId"
    },
    "correlation": {
      "type": "object",
      "required": [
        "runId",
        "normalizedSessionId",
        "turnId",
        "itemId"
      ],
      "properties": {
        "runId": {
          "$ref": "#/$defs/stableId"
        },
        "normalizedSessionId": {
          "$ref": "#/$defs/stableId"
        },
        "turnId": {
          "$ref": "#/$defs/stableId"
        },
        "itemId": {
          "$ref": "#/$defs/stableId"
        },
        "requestId": {
          "$ref": "#/$defs/stableId"
        }
      },
      "additionalProperties": true
    },
    "idempotencyKey": {
      "type": [
        "string",
        "null"
      ],
      "minLength": 1,
      "maxLength": 240
    },
    "content": {
      "$ref": "#/$defs/safeContent"
    },
    "outcome": {
      "enum": [
        "succeeded",
        "denied",
        "conflict",
        "duplicate",
        "unavailable",
        "failed"
      ]
    },
    "code": {
      "$ref": "#/$defs/stableCode"
    },
    "retryable": {
      "type": "boolean"
    },
    "authorizationBoundary": {
      "enum": [
        "company",
        "actor",
        "active_task",
        "grant",
        "governed_action",
        "lock",
        "revision"
      ]
    },
    "operationReceiptId": {
      "$ref": "#/$defs/stableId"
    },
    "auditReceiptId": {
      "$ref": "#/$defs/stableId"
    },
    "currentRevision": {
      "oneOf": [
        {
          "type": "integer",
          "minimum": 0
        },
        {
          "$ref": "#/$defs/stableId"
        }
      ]
    },
    "duplicateOfReceiptId": {
      "$ref": "#/$defs/stableId"
    },
    "artifactRefs": {
      "type": "array",
      "items": {
        "allOf": [
          {
            "$ref": "#/$defs/safeReference"
          },
          {
            "type": "object",
            "properties": {
              "kind": {
                "enum": [
                  "artifact",
                  "work_product"
                ]
              }
            }
          }
        ]
      },
      "uniqueItems": true
    },
    "targets": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/immutableTarget"
      },
      "uniqueItems": true
    },
    "causalRefs": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/safeReference"
      },
      "uniqueItems": true
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "phase": {
            "enum": [
              "result",
              "reconciled"
            ]
          }
        },
        "required": [
          "phase"
        ]
      },
      "then": {
        "required": [
          "outcome",
          "code",
          "retryable",
          "authorizationBoundary",
          "operationReceiptId"
        ]
      }
    }
  ],
  "additionalProperties": true,
  "$defs": {
    "stableId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 240,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
    },
    "stableCode": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160,
      "pattern": "^[a-z][a-z0-9_.:-]*$"
    },
    "safeContent": {
      "type": "object",
      "required": [
        "digest",
        "redactionDisposition",
        "references"
      ],
      "properties": {
        "digest": {
          "type": "string",
          "pattern": "^sha256:[a-f0-9]{64}$"
        },
        "redactionDisposition": {
          "enum": [
            "digest_only",
            "allowlisted_references",
            "redacted"
          ]
        },
        "references": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/safeReference"
          },
          "uniqueItems": true
        }
      },
      "additionalProperties": true
    },
    "safeReference": {
      "type": "object",
      "required": [
        "kind",
        "id"
      ],
      "properties": {
        "kind": {
          "enum": [
            "task",
            "document_revision",
            "interaction",
            "approval",
            "decision",
            "artifact",
            "work_product",
            "wake",
            "monitor",
            "audit",
            "operation"
          ]
        },
        "id": {
          "$ref": "#/$defs/stableId"
        }
      },
      "additionalProperties": true
    },
    "immutableTarget": {
      "type": "object",
      "required": [
        "kind",
        "id",
        "immutable"
      ],
      "properties": {
        "kind": {
          "enum": [
            "document_revision",
            "interaction",
            "approval",
            "decision"
          ]
        },
        "id": {
          "$ref": "#/$defs/stableId"
        },
        "immutable": {
          "const": true
        },
        "revisionId": {
          "$ref": "#/$defs/stableId"
        },
        "decisionId": {
          "$ref": "#/$defs/stableId"
        }
      },
      "additionalProperties": true
    }
  }
} as const;

export const usageSchema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v1/usage.schema.json",
  "title": "PRP provider usage receipt",
  "type": "object",
  "required": [
    "provider",
    "model",
    "cumulative",
    "runDelta"
  ],
  "properties": {
    "provider": {
      "type": "string",
      "minLength": 1,
      "maxLength": 80
    },
    "model": {
      "type": [
        "string",
        "null"
      ],
      "maxLength": 240
    },
    "providerSessionId": {
      "type": [
        "string",
        "null"
      ],
      "maxLength": 240
    },
    "providerRequestId": {
      "type": [
        "string",
        "null"
      ],
      "maxLength": 240
    },
    "cumulative": {
      "$ref": "#/$defs/measurement"
    },
    "runDeltaAvailable": {
      "type": "boolean"
    },
    "runDelta": {
      "$ref": "#/$defs/measurement"
    }
  },
  "additionalProperties": false,
  "$defs": {
    "measurement": {
      "type": "object",
      "required": [
        "inputTokens",
        "outputTokens",
        "cacheReadTokens",
        "cacheWriteTokens",
        "activeSeconds",
        "requests",
        "providerCostUsd"
      ],
      "properties": {
        "inputTokens": {
          "type": "integer",
          "minimum": 0
        },
        "outputTokens": {
          "type": "integer",
          "minimum": 0
        },
        "cacheReadTokens": {
          "type": "integer",
          "minimum": 0
        },
        "cacheWriteTokens": {
          "type": "integer",
          "minimum": 0
        },
        "activeSeconds": {
          "type": "number",
          "minimum": 0
        },
        "requests": {
          "type": "integer",
          "minimum": 0
        },
        "providerCostUsd": {
          "type": "number",
          "minimum": 0
        }
      },
      "additionalProperties": false
    }
  }
} as const;

export const stopReasonSchema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v1/stop-reason.schema.json",
  "title": "PRP terminal stop reason receipt",
  "type": "object",
  "required": [
    "schema",
    "schemaVersion",
    "receiptId",
    "kind",
    "code",
    "retryable",
    "decisionId",
    "limitClass",
    "aggregate"
  ],
  "properties": {
    "schema": {
      "const": "paperclip.prp.stop_reason.v1"
    },
    "schemaVersion": {
      "const": 1
    },
    "receiptId": {
      "$ref": "#/$defs/stableId"
    },
    "kind": {
      "enum": [
        "budget",
        "cost"
      ]
    },
    "code": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160,
      "pattern": "^[a-z][a-z0-9_.:-]*$"
    },
    "retryable": {
      "type": "boolean"
    },
    "decisionId": {
      "$ref": "#/$defs/stableId"
    },
    "limitClass": {
      "enum": [
        "company_monthly",
        "actor_monthly",
        "project_monthly",
        "run_cost",
        "provider_quota"
      ]
    },
    "aggregate": {
      "type": "object",
      "required": [
        "unit",
        "observed",
        "limit",
        "window"
      ],
      "properties": {
        "unit": {
          "enum": [
            "cents",
            "usd_micros",
            "tokens"
          ]
        },
        "observed": {
          "type": "integer",
          "minimum": 0
        },
        "limit": {
          "type": "integer",
          "minimum": 0
        },
        "window": {
          "enum": [
            "run",
            "monthly_utc",
            "provider_window"
          ]
        }
      },
      "additionalProperties": true
    },
    "relatedReceiptIds": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/stableId"
      },
      "uniqueItems": true
    }
  },
  "additionalProperties": true,
  "$defs": {
    "stableId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 240,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
    }
  }
} as const;

export const terminalSchema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v1/terminal.schema.json",
  "title": "PRP run terminal state",
  "type": "object",
  "required": [
    "schema",
    "turnTerminalState",
    "runTerminalState",
    "reportedWorkDisposition"
  ],
  "properties": {
    "schema": {
      "const": "paperclip.prp.terminal.v1"
    },
    "turnTerminalState": {
      "enum": [
        "completed",
        "failed",
        "interrupted",
        "cancelled"
      ]
    },
    "runTerminalState": {
      "enum": [
        "succeeded",
        "failed",
        "cancelled"
      ]
    },
    "reportedWorkDisposition": {
      "enum": [
        "done",
        "blocked",
        "needs_review",
        "yielded"
      ]
    },
    "workAssessmentId": {
      "type": "string",
      "minLength": 1
    },
    "statusDecisionId": {
      "type": "string",
      "minLength": 1
    },
    "stopReason": {
      "$ref": "https://paperclip.dev/schemas/prp/v1/stop-reason.schema.json"
    }
  },
  "additionalProperties": true
} as const;

export const questionSetSchema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v1/question-set.schema.json",
  "title": "Paperclip question set",
  "type": "object",
  "required": [
    "schema",
    "questions"
  ],
  "properties": {
    "schema": {
      "const": "paperclip.question_set.v1"
    },
    "title": {
      "type": "string",
      "maxLength": 1000
    },
    "description": {
      "type": "string",
      "maxLength": 100000
    },
    "submitLabel": {
      "type": "string",
      "maxLength": 200
    },
    "questions": {
      "type": "array",
      "minItems": 1,
      "maxItems": 64,
      "items": {
        "$ref": "#/$defs/question"
      }
    }
  },
  "$defs": {
    "option": {
      "type": "object",
      "required": [
        "id",
        "label"
      ],
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160
        },
        "label": {
          "type": "string",
          "minLength": 1,
          "maxLength": 1000
        },
        "description": {
          "type": "string",
          "maxLength": 4000
        },
        "recommended": {
          "type": "boolean"
        }
      },
      "additionalProperties": false
    },
    "customAnswer": {
      "type": "object",
      "required": [
        "enabled"
      ],
      "properties": {
        "enabled": {
          "const": true
        },
        "label": {
          "type": "string",
          "maxLength": 1000
        },
        "placeholder": {
          "type": "string",
          "maxLength": 1000
        }
      },
      "additionalProperties": false
    },
    "textValidation": {
      "type": "object",
      "properties": {
        "minLength": {
          "type": "integer",
          "minimum": 0,
          "maximum": 100000
        },
        "maxLength": {
          "type": "integer",
          "minimum": 0,
          "maximum": 100000
        },
        "pattern": {
          "type": "string",
          "maxLength": 1000
        },
        "inputType": {
          "enum": [
            "text",
            "number",
            "integer"
          ]
        },
        "minimum": {
          "type": "number"
        },
        "maximum": {
          "type": "number"
        }
      },
      "additionalProperties": false
    },
    "question": {
      "type": "object",
      "required": [
        "id",
        "prompt",
        "required",
        "answerMode"
      ],
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160
        },
        "header": {
          "type": "string",
          "maxLength": 1000
        },
        "prompt": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4000
        },
        "helpText": {
          "type": "string",
          "maxLength": 4000
        },
        "required": {
          "type": "boolean"
        },
        "answerMode": {
          "enum": [
            "single_select",
            "multi_select",
            "text"
          ]
        },
        "initialText": {
          "type": "string",
          "maxLength": 100000,
          "description": "Editable draft of at most 100000 Unicode code points. Never an implicit answer; submitted text still uses the existing response bounds."
        },
        "options": {
          "type": "array",
          "maxItems": 128,
          "items": {
            "$ref": "#/$defs/option"
          }
        },
        "customAnswer": {
          "$ref": "#/$defs/customAnswer"
        },
        "textValidation": {
          "$ref": "#/$defs/textValidation"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "answerMode": {
                "const": "text"
              }
            },
            "required": [
              "answerMode"
            ]
          },
          "then": {
            "properties": {
              "options": {
                "type": "array",
                "maxItems": 0
              }
            },
            "not": {
              "required": [
                "customAnswer"
              ]
            }
          }
        },
        {
          "if": {
            "properties": {
              "answerMode": {
                "enum": [
                  "single_select",
                  "multi_select"
                ]
              }
            },
            "required": [
              "answerMode"
            ]
          },
          "then": {
            "required": [
              "options"
            ],
            "not": {
              "required": [
                "initialText"
              ]
            },
            "properties": {
              "options": {
                "type": "array",
                "minItems": 1
              }
            }
          }
        }
      ],
      "additionalProperties": false
    }
  },
  "additionalProperties": false
} as const;

export const questionResponseSchema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v1/question-response.schema.json",
  "title": "Paperclip question response",
  "type": "object",
  "required": [
    "schema",
    "answers"
  ],
  "properties": {
    "schema": {
      "const": "paperclip.question_response.v1"
    },
    "answers": {
      "type": "object",
      "propertyNames": {
        "minLength": 1,
        "maxLength": 160
      },
      "additionalProperties": {
        "type": "object",
        "properties": {
          "selectedOptionIds": {
            "type": "array",
            "uniqueItems": true,
            "items": {
              "type": "string",
              "minLength": 1,
              "maxLength": 160
            }
          },
          "text": {
            "type": "string",
            "maxLength": 100000
          },
          "customText": {
            "type": "string",
            "maxLength": 100000
          }
        },
        "additionalProperties": false
      }
    }
  },
  "additionalProperties": false
} as const;

export const questionAdapterFixtureSchema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v1/question-adapter-fixture.schema.json",
  "title": "Harness question adapter conformance fixture",
  "type": "object",
  "required": [
    "schema",
    "adapter",
    "nativeRequest",
    "canonicalQuestionSet",
    "canonicalResponse",
    "nativeResponse"
  ],
  "properties": {
    "schema": {
      "const": "paperclip.question_adapter_fixture.v1"
    },
    "adapter": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "nativeRequest": {},
    "canonicalQuestionSet": {
      "$ref": "https://paperclip.dev/schemas/prp/v1/question-set.schema.json"
    },
    "canonicalResponse": {
      "$ref": "https://paperclip.dev/schemas/prp/v1/question-response.schema.json"
    },
    "nativeResponse": {}
  },
  "additionalProperties": false
} as const;

export const requestSchema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v1/request.schema.json",
  "title": "PRP request",
  "oneOf": [
    {
      "type": "object",
      "required": [
        "schema",
        "requestKind",
        "requestId",
        "type",
        "status",
        "prompt",
        "choices",
        "origin",
        "turnId"
      ],
      "properties": {
        "schema": {
          "const": "paperclip.runtime_request.v2"
        },
        "requestKind": {
          "const": "permission_approval"
        },
        "requestId": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160
        },
        "type": {
          "const": "permission"
        },
        "status": {
          "enum": [
            "pending",
            "resolved",
            "expired",
            "cancelled"
          ]
        },
        "prompt": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4000
        },
        "choices": {
          "type": "array",
          "minItems": 1,
          "maxItems": 4,
          "items": {
            "type": "object",
            "required": [
              "key",
              "label"
            ],
            "properties": {
              "key": {
                "enum": [
                  "accept",
                  "accept_for_session",
                  "decline",
                  "cancel"
                ]
              },
              "label": {
                "type": "string",
                "minLength": 1,
                "maxLength": 500
              }
            },
            "additionalProperties": false
          },
          "allOf": [
            {
              "contains": {
                "type": "object",
                "required": [
                  "key"
                ],
                "properties": {
                  "key": {
                    "const": "accept"
                  }
                }
              },
              "minContains": 0,
              "maxContains": 1
            },
            {
              "contains": {
                "type": "object",
                "required": [
                  "key"
                ],
                "properties": {
                  "key": {
                    "const": "accept_for_session"
                  }
                }
              },
              "minContains": 0,
              "maxContains": 1
            },
            {
              "contains": {
                "type": "object",
                "required": [
                  "key"
                ],
                "properties": {
                  "key": {
                    "const": "decline"
                  }
                }
              },
              "minContains": 0,
              "maxContains": 1
            },
            {
              "contains": {
                "type": "object",
                "required": [
                  "key"
                ],
                "properties": {
                  "key": {
                    "const": "cancel"
                  }
                }
              },
              "minContains": 0,
              "maxContains": 1
            }
          ]
        },
        "details": {
          "type": "object",
          "additionalProperties": true
        },
        "origin": {
          "type": "object",
          "required": [
            "adapter"
          ],
          "properties": {
            "adapter": {
              "type": "string",
              "minLength": 1,
              "maxLength": 160
            },
            "provider": {
              "type": "string",
              "minLength": 1,
              "maxLength": 160
            },
            "method": {
              "type": "string",
              "minLength": 1,
              "maxLength": 500
            }
          },
          "additionalProperties": false
        },
        "turnId": {
          "type": "string",
          "minLength": 1,
          "maxLength": 240
        },
        "itemId": {
          "type": [
            "string",
            "null"
          ],
          "minLength": 1,
          "maxLength": 240
        }
      },
      "additionalProperties": false
    },
    {
      "type": "object",
      "required": [
        "schema",
        "requestKind",
        "requestId",
        "type",
        "status",
        "prompt",
        "input"
      ],
      "properties": {
        "schema": {
          "const": "paperclip.runtime_request.v2"
        },
        "requestKind": {
          "const": "runtime"
        },
        "requestId": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160
        },
        "type": {
          "const": "input"
        },
        "status": {
          "enum": [
            "pending",
            "resolved",
            "expired",
            "cancelled"
          ]
        },
        "prompt": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4000
        },
        "input": {
          "$ref": "https://paperclip.dev/schemas/prp/v1/question-set.schema.json"
        },
        "origin": {
          "type": "object",
          "required": [
            "adapter"
          ],
          "properties": {
            "adapter": {
              "type": "string",
              "minLength": 1,
              "maxLength": 160
            },
            "provider": {
              "type": "string",
              "minLength": 1,
              "maxLength": 160
            },
            "method": {
              "type": "string",
              "minLength": 1,
              "maxLength": 500
            }
          },
          "additionalProperties": false
        },
        "turnId": {
          "type": "string",
          "minLength": 1,
          "maxLength": 240
        },
        "itemId": {
          "type": "string",
          "minLength": 1,
          "maxLength": 240
        }
      },
      "additionalProperties": false
    },
    {
      "type": "object",
      "required": [
        "schema",
        "requestKind",
        "requestId",
        "type",
        "status",
        "prompt"
      ],
      "properties": {
        "schema": {
          "const": "paperclip.runtime_request.v1"
        },
        "requestKind": {
          "const": "runtime"
        },
        "requestId": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160
        },
        "type": {
          "enum": [
            "permission",
            "input"
          ]
        },
        "status": {
          "enum": [
            "pending",
            "resolved",
            "expired",
            "cancelled"
          ]
        },
        "prompt": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4000
        },
        "choices": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "key",
              "label"
            ],
            "properties": {
              "key": {
                "type": "string",
                "minLength": 1
              },
              "label": {
                "type": "string",
                "minLength": 1
              }
            },
            "additionalProperties": true
          }
        }
      },
      "additionalProperties": true
    },
    {
      "type": "object",
      "required": [
        "schema",
        "requestKind",
        "requestId",
        "kind",
        "blocking",
        "payload"
      ],
      "properties": {
        "schema": {
          "const": "paperclip.interaction_request.v1"
        },
        "requestKind": {
          "const": "issue_thread"
        },
        "requestId": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160
        },
        "kind": {
          "enum": [
            "request_confirmation",
            "request_checkbox_confirmation",
            "request_item_verdicts",
            "ask_user_questions",
            "suggest_tasks"
          ]
        },
        "blocking": {
          "type": "boolean"
        },
        "payload": {
          "type": "object",
          "additionalProperties": true
        }
      },
      "additionalProperties": true
    }
  ]
} as const;

export const resultSchema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v1/result.schema.json",
  "title": "PRP structured run result",
  "type": "object",
  "required": [
    "schema",
    "reportedWorkDisposition",
    "summary",
    "completionClaim",
    "evidence",
    "verification",
    "attentionRequests",
    "artifacts"
  ],
  "properties": {
    "schema": {
      "const": "paperclip.run_result.v1"
    },
    "reportedWorkDisposition": {
      "enum": [
        "done",
        "blocked",
        "needs_review",
        "yielded"
      ]
    },
    "summary": {
      "type": "string",
      "minLength": 1,
      "maxLength": 12000
    },
    "completionClaim": {
      "type": "object",
      "required": [
        "contractRevision",
        "objectiveSatisfied",
        "criteria",
        "remainingWork"
      ],
      "properties": {
        "contractRevision": {
          "type": "string",
          "minLength": 1
        },
        "objectiveSatisfied": {
          "type": "boolean"
        },
        "criteria": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "criterionId",
              "status",
              "evidenceRefs"
            ],
            "properties": {
              "criterionId": {
                "type": "string",
                "minLength": 1
              },
              "status": {
                "enum": [
                  "satisfied",
                  "not_satisfied",
                  "unknown"
                ]
              },
              "evidenceRefs": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "explanation": {
                "type": "string"
              }
            },
            "additionalProperties": true
          }
        },
        "remainingWork": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "description",
              "blocksCompletion"
            ],
            "properties": {
              "description": {
                "type": "string",
                "minLength": 1
              },
              "blocksCompletion": {
                "type": "boolean"
              }
            },
            "additionalProperties": true
          }
        }
      },
      "additionalProperties": true
    },
    "evidence": {
      "type": "array",
      "items": {
        "type": "object"
      }
    },
    "verification": {
      "type": "array",
      "description": "Checks used to verify the work. Use failed only when the check actually ran and found a defect in the work. If a check could not run or complete because the environment, tool, dependency, permission, credential, policy, external service, or budget was unavailable, use not_run with reasonCode even when the attempted command exited non-zero.",
      "items": {
        "type": "object",
        "required": [
          "commandOrCheck",
          "status"
        ],
        "properties": {
          "commandOrCheck": {
            "type": "string",
            "minLength": 1
          },
          "status": {
            "enum": [
              "passed",
              "failed",
              "not_run"
            ],
            "description": "passed means the check ran and succeeded; failed means it ran to a meaningful verdict and found the work incorrect; not_run means no meaningful verdict was possible because the check was not attempted or could not complete."
          },
          "reasonCode": {
            "description": "Required for not_run; identify why the check had no meaningful verdict.",
            "enum": [
              "environment_unavailable",
              "tool_unavailable",
              "dependency_missing",
              "permission_denied",
              "credential_missing",
              "policy_restricted",
              "external_service_unavailable",
              "budget_exhausted",
              "other"
            ]
          },
          "detail": {
            "type": "string"
          },
          "artifactRef": {
            "type": "string"
          }
        },
        "additionalProperties": true
      }
    },
    "blocker": {
      "type": "object",
      "required": [
        "reasonCode",
        "owner",
        "unblockAction",
        "scope"
      ],
      "properties": {
        "reasonCode": {
          "type": "string",
          "minLength": 1
        },
        "owner": {
          "type": "object",
          "required": [
            "kind",
            "name"
          ],
          "properties": {
            "kind": {
              "enum": [
                "agent",
                "user",
                "system",
                "external"
              ]
            },
            "name": {
              "type": "string",
              "minLength": 1
            }
          },
          "additionalProperties": true
        },
        "unblockAction": {
          "type": "string",
          "minLength": 1
        },
        "scope": {
          "enum": [
            "current_track",
            "task_wide"
          ]
        }
      },
      "additionalProperties": true
    },
    "attentionRequests": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "kind": {
            "type": "string",
            "minLength": 1
          },
          "summary": {
            "type": "string",
            "minLength": 1
          },
          "ownerClass": {
            "enum": [
              "human",
              "agent",
              "external_system"
            ]
          },
          "targetAgentId": {
            "type": "string",
            "minLength": 1
          }
        },
        "additionalProperties": true
      }
    },
    "artifacts": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "kind",
          "ref"
        ],
        "properties": {
          "kind": {
            "type": "string",
            "minLength": 1
          },
          "ref": {
            "type": "string",
            "minLength": 1
          },
          "title": {
            "type": "string"
          }
        },
        "additionalProperties": true
      }
    },
    "continuation": {
      "type": "object",
      "required": [
        "kind",
        "summary",
        "idempotencyKey"
      ],
      "properties": {
        "kind": {
          "enum": [
            "same_agent",
            "retry",
            "delegated_issue",
            "response_wake",
            "monitor",
            "case_handoff"
          ]
        },
        "summary": {
          "type": "string",
          "minLength": 1
        },
        "idempotencyKey": {
          "type": "string",
          "minLength": 1
        },
        "handoffId": {
          "type": "string",
          "minLength": 1
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "kind": {
                "const": "case_handoff"
              }
            },
            "required": [
              "kind"
            ]
          },
          "then": {
            "required": [
              "handoffId"
            ]
          }
        }
      ],
      "additionalProperties": true
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "reportedWorkDisposition": {
            "const": "blocked"
          }
        }
      },
      "then": {
        "required": [
          "blocker"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "reportedWorkDisposition": {
            "const": "yielded"
          }
        }
      },
      "then": {
        "required": [
          "continuation"
        ]
      }
    }
  ],
  "additionalProperties": true
} as const;

export const eventSchema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v1/event.schema.json",
  "title": "PRP native event",
  "type": "object",
  "required": [
    "schema",
    "sourceEventId",
    "sourceSeq",
    "sourceInstanceId",
    "sourceKind",
    "runId",
    "eventType",
    "schemaVersion",
    "priority",
    "emittedAt",
    "payload"
  ],
  "properties": {
    "schema": {
      "const": "paperclip.prp.event.v1"
    },
    "sourceEventId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "sourceSeq": {
      "type": "integer",
      "minimum": 1,
      "maximum": 9007199254740991
    },
    "sourceInstanceId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "sourceKind": {
      "enum": [
        "runner",
        "control_plane"
      ]
    },
    "runId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "normalizedSessionId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "turnId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 240
    },
    "itemId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 240
    },
    "eventType": {
      "enum": [
        "runner.connected",
        "runner.reconnected",
        "runner.reconciled",
        "runner.disconnected",
        "runner.draining",
        "runner.backpressure",
        "runner.suspending",
        "runner.suspended",
        "runner.stopped",
        "runner.diagnostic",
        "runtime.phase.changed",
        "sandbox.metric",
        "workspace.ready",
        "workspace.change.updated",
        "workspace.diff.recorded",
        "workspace.file.referenced",
        "auth.credentials.updated",
        "harness.starting",
        "harness.ready",
        "harness.exited",
        "harness.diagnostic",
        "plan.updated",
        "tool.execution.started",
        "tool.execution.progressed",
        "tool.execution.completed",
        "research.started",
        "research.progressed",
        "research.completed",
        "delegation.started",
        "delegation.updated",
        "delegation.completed",
        "model.route.changed",
        "model.verification.updated",
        "context.compacted",
        "artifact.viewed",
        "artifact.generated",
        "review.mode.changed",
        "hook.started",
        "hook.completed",
        "memory.citation.referenced",
        "safety.review.started",
        "safety.review.completed",
        "terminal.input.sent",
        "wait.started",
        "wait.completed",
        "provider.notice.recorded",
        "session.starting",
        "session.started",
        "session.resuming",
        "session.resumed",
        "session.reconciled",
        "session.updated",
        "session.closed",
        "session.failed",
        "turn.submitted",
        "turn.accepted",
        "turn.started",
        "turn.completed",
        "turn.failed",
        "turn.interrupted",
        "turn.cancelled",
        "item.started",
        "item.delta",
        "item.completed",
        "item.failed",
        "usage.reported",
        "semantic_tool.input",
        "semantic_tool.result",
        "semantic_tool.reconciled",
        "mcp_app.discovered",
        "mcp_app.resource.resolved",
        "mcp_app.initializing",
        "mcp_app.ready",
        "mcp_app.tool_input",
        "mcp_app.tool_result",
        "mcp_app.action.requested",
        "mcp_app.action.resolved",
        "mcp_app.host_context.changed",
        "mcp_app.failed",
        "mcp_app.teardown",
        "runtime_request.created",
        "runtime_request.resolved",
        "runtime_request.expired",
        "runtime_request.cancelled",
        "interaction.request.proposed",
        "interaction.request.materialized",
        "interaction.request.rejected",
        "interaction.response.progressed",
        "interaction.response.resolved",
        "interaction.response.delivered",
        "run.attached",
        "run.detached",
        "run.result.proposed",
        "run.result.accepted",
        "run.result.rejected",
        "attention.request.proposed",
        "attention.request.routed",
        "attention.request.resolved",
        "attention.request.expired",
        "attention.request.superseded",
        "work.assessment.recorded",
        "issue.status.decision.recorded",
        "issue.status.decision.applied",
        "issue.status.decision.rejected",
        "issue.status.decision.superseded",
        "run.terminal"
      ]
    },
    "schemaVersion": {
      "const": 1
    },
    "priority": {
      "enum": [
        0,
        1,
        2
      ]
    },
    "emittedAt": {
      "type": "string",
      "format": "date-time"
    },
    "observedAt": {
      "type": "string",
      "format": "date-time"
    },
    "payload": {
      "type": "object",
      "properties": {
        "channel": {
          "enum": [
            "progress",
            "final",
            "summary",
            "detail",
            "unknown"
          ]
        },
        "providerPhase": {
          "type": "string"
        },
        "providerMethod": {
          "type": "string"
        }
      },
      "additionalProperties": true
    },
    "debug": {
      "type": "object",
      "additionalProperties": true
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "auth.credentials.updated"
          }
        }
      },
      "then": {
        "properties": {
          "sourceKind": {
            "const": "runner"
          },
          "priority": {
            "const": 0
          },
          "payload": {
            "$ref": "https://paperclip.dev/schemas/prp/v1/auth-credentials.schema.json"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "plan.updated"
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "$ref": "https://paperclip.dev/schemas/prp/v1/provider-event.schema.json#/$defs/plan"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "enum": [
              "tool.execution.started",
              "tool.execution.progressed",
              "tool.execution.completed"
            ]
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "$ref": "https://paperclip.dev/schemas/prp/v1/provider-event.schema.json#/$defs/toolExecution"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "enum": [
              "research.started",
              "research.progressed",
              "research.completed"
            ]
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "$ref": "https://paperclip.dev/schemas/prp/v1/provider-event.schema.json#/$defs/research"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "enum": [
              "delegation.started",
              "delegation.updated",
              "delegation.completed"
            ]
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "$ref": "https://paperclip.dev/schemas/prp/v1/provider-event.schema.json#/$defs/delegation"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "model.route.changed"
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "$ref": "https://paperclip.dev/schemas/prp/v1/provider-event.schema.json#/$defs/modelRoute"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "model.verification.updated"
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "$ref": "https://paperclip.dev/schemas/prp/v1/provider-event.schema.json#/$defs/modelVerification"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "context.compacted"
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "$ref": "https://paperclip.dev/schemas/prp/v1/provider-event.schema.json#/$defs/contextCompacted"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "artifact.viewed"
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "$ref": "https://paperclip.dev/schemas/prp/v1/provider-event.schema.json#/$defs/artifactViewed"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "artifact.generated"
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "$ref": "https://paperclip.dev/schemas/prp/v1/provider-event.schema.json#/$defs/artifactGenerated"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "review.mode.changed"
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "$ref": "https://paperclip.dev/schemas/prp/v1/provider-event.schema.json#/$defs/reviewMode"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "enum": [
              "hook.started",
              "hook.completed"
            ]
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "$ref": "https://paperclip.dev/schemas/prp/v1/provider-event.schema.json#/$defs/hook"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "memory.citation.referenced"
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "$ref": "https://paperclip.dev/schemas/prp/v1/provider-event.schema.json#/$defs/memoryCitation"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "enum": [
              "safety.review.started",
              "safety.review.completed"
            ]
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "$ref": "https://paperclip.dev/schemas/prp/v1/provider-event.schema.json#/$defs/safetyReview"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "terminal.input.sent"
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "$ref": "https://paperclip.dev/schemas/prp/v1/provider-event.schema.json#/$defs/terminalInput"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "enum": [
              "wait.started",
              "wait.completed"
            ]
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "$ref": "https://paperclip.dev/schemas/prp/v1/provider-event.schema.json#/$defs/wait"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "provider.notice.recorded"
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "$ref": "https://paperclip.dev/schemas/prp/v1/provider-event.schema.json#/$defs/providerNotice"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "workspace.file.referenced"
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "$ref": "https://paperclip.dev/schemas/prp/v1/workspace-file-reference.schema.json"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "enum": [
              "workspace.change.updated",
              "workspace.diff.recorded"
            ]
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "$ref": "https://paperclip.dev/schemas/prp/v1/workspace-diff.schema.json"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "workspace.diff.recorded"
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "allOf": [
              {
                "$ref": "https://paperclip.dev/schemas/prp/v1/workspace-diff.schema.json"
              },
              {
                "type": "object",
                "properties": {
                  "complete": {
                    "const": true
                  }
                }
              }
            ]
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "enum": [
              "semantic_tool.input",
              "mcp_app.tool_input"
            ]
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "type": "object",
            "properties": {
              "semantic_tool": {
                "allOf": [
                  {
                    "$ref": "https://paperclip.dev/schemas/prp/v1/semantic-tool.schema.json"
                  },
                  {
                    "type": "object",
                    "properties": {
                      "phase": {
                        "const": "input"
                      }
                    }
                  }
                ]
              }
            },
            "additionalProperties": true
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "enum": [
              "semantic_tool.result",
              "mcp_app.tool_result"
            ]
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "type": "object",
            "properties": {
              "semantic_tool": {
                "allOf": [
                  {
                    "$ref": "https://paperclip.dev/schemas/prp/v1/semantic-tool.schema.json"
                  },
                  {
                    "type": "object",
                    "properties": {
                      "phase": {
                        "const": "result"
                      }
                    }
                  }
                ]
              }
            },
            "additionalProperties": true
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "semantic_tool.reconciled"
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "type": "object",
            "properties": {
              "semantic_tool": {
                "allOf": [
                  {
                    "$ref": "https://paperclip.dev/schemas/prp/v1/semantic-tool.schema.json"
                  },
                  {
                    "type": "object",
                    "properties": {
                      "phase": {
                        "const": "reconciled"
                      }
                    }
                  }
                ]
              }
            },
            "additionalProperties": true
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "run.terminal"
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "$ref": "https://paperclip.dev/schemas/prp/v1/terminal.schema.json"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "run.result.proposed"
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "$ref": "https://paperclip.dev/schemas/prp/v1/result.schema.json"
          }
        }
      }
    }
  ],
  "additionalProperties": true
} as const;

export const eventV2Schema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v2/event.schema.json",
  "title": "PRP v2 native event",
  "type": "object",
  "required": [
    "schema",
    "sourceEventId",
    "sourceSeq",
    "sourceInstanceId",
    "sourceKind",
    "runId",
    "eventType",
    "schemaVersion",
    "priority",
    "emittedAt",
    "payload"
  ],
  "properties": {
    "schema": {
      "const": "paperclip.prp.event.v2"
    },
    "sourceEventId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "sourceSeq": {
      "type": "integer",
      "minimum": 1,
      "maximum": 9007199254740991
    },
    "sourceInstanceId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "sourceKind": {
      "enum": [
        "runner",
        "control_plane"
      ]
    },
    "runId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "normalizedSessionId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "turnId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "itemId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "eventType": {
      "enum": [
        "runner.connected",
        "runner.reconnected",
        "runner.reconciled",
        "runner.disconnected",
        "runner.draining",
        "runner.suspending",
        "runner.suspended",
        "runner.stopped",
        "runner.diagnostic",
        "runtime.phase.changed",
        "sandbox.metric",
        "workspace.ready",
        "workspace.change.updated",
        "workspace.diff.recorded",
        "workspace.file.referenced",
        "auth.credentials.updated",
        "harness.starting",
        "harness.ready",
        "harness.exited",
        "harness.diagnostic",
        "plan.updated",
        "tool.execution.started",
        "tool.execution.progressed",
        "tool.execution.completed",
        "research.started",
        "research.progressed",
        "research.completed",
        "delegation.started",
        "delegation.updated",
        "delegation.completed",
        "model.route.changed",
        "model.verification.updated",
        "context.compacted",
        "artifact.viewed",
        "artifact.generated",
        "review.mode.changed",
        "hook.started",
        "hook.completed",
        "memory.citation.referenced",
        "safety.review.started",
        "safety.review.completed",
        "terminal.input.sent",
        "wait.started",
        "wait.completed",
        "provider.notice.recorded",
        "session.starting",
        "session.started",
        "session.resuming",
        "session.resumed",
        "session.reconciled",
        "session.updated",
        "session.closed",
        "session.failed",
        "session.capabilities.updated",
        "session.goal.snapshot",
        "session.goal.updated",
        "session.goal.cleared",
        "turn.submitted",
        "turn.accepted",
        "turn.started",
        "turn.completed",
        "turn.failed",
        "turn.interrupted",
        "turn.cancelled",
        "item.started",
        "item.delta",
        "item.completed",
        "item.failed",
        "usage.reported",
        "semantic_tool.input",
        "semantic_tool.result",
        "mcp_app.discovered",
        "mcp_app.resource.resolved",
        "mcp_app.initializing",
        "mcp_app.ready",
        "mcp_app.tool_input",
        "mcp_app.tool_result",
        "mcp_app.action.requested",
        "mcp_app.action.resolved",
        "mcp_app.host_context.changed",
        "mcp_app.failed",
        "mcp_app.teardown",
        "runtime_request.created",
        "runtime_request.resolved",
        "runtime_request.expired",
        "runtime_request.cancelled",
        "interaction.request.proposed",
        "interaction.request.materialized",
        "interaction.request.rejected",
        "interaction.response.progressed",
        "interaction.response.resolved",
        "interaction.response.delivered",
        "run.attached",
        "run.detached",
        "run.result.proposed",
        "run.result.accepted",
        "run.result.rejected",
        "attention.request.proposed",
        "attention.request.routed",
        "attention.request.resolved",
        "attention.request.expired",
        "attention.request.superseded",
        "work.assessment.recorded",
        "issue.status.decision.recorded",
        "issue.status.decision.applied",
        "issue.status.decision.rejected",
        "issue.status.decision.superseded",
        "run.terminal"
      ]
    },
    "schemaVersion": {
      "const": 2
    },
    "priority": {
      "enum": [
        0,
        1,
        2
      ]
    },
    "emittedAt": {
      "type": "string",
      "format": "date-time"
    },
    "observedAt": {
      "type": "string",
      "format": "date-time"
    },
    "payload": {
      "type": "object",
      "additionalProperties": true
    },
    "debug": {
      "type": "object",
      "additionalProperties": true
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "auth.credentials.updated"
          }
        }
      },
      "then": {
        "properties": {
          "sourceKind": {
            "const": "runner"
          },
          "priority": {
            "const": 0
          },
          "payload": {
            "$ref": "https://paperclip.dev/schemas/prp/v1/auth-credentials.schema.json"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "enum": [
              "session.goal.snapshot",
              "session.goal.updated"
            ]
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "type": "object",
            "required": [
              "goal"
            ],
            "properties": {
              "goal": {
                "oneOf": [
                  {
                    "$ref": "https://paperclip.dev/schemas/prp/v2/session-goal.schema.json#/$defs/snapshot"
                  },
                  {
                    "type": "null"
                  }
                ]
              }
            },
            "additionalProperties": true
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "session.capabilities.updated"
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "type": "object",
            "required": [
              "sessionGoals"
            ],
            "properties": {
              "sessionGoals": {
                "$ref": "https://paperclip.dev/schemas/prp/v2/session-goal.schema.json#/$defs/capability"
              },
              "turnControls": {
                "$ref": "https://paperclip.dev/schemas/prp/v1/provider-descriptor.schema.json#/$defs/turnControls"
              }
            },
            "additionalProperties": true
          }
        }
      }
    }
  ],
  "additionalProperties": true
} as const;

export const eventV3Schema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v3/event.schema.json",
  "title": "PRP v3 native event",
  "type": "object",
  "required": [
    "schema",
    "sourceEventId",
    "sourceSeq",
    "sourceInstanceId",
    "sourceKind",
    "runId",
    "eventType",
    "schemaVersion",
    "priority",
    "emittedAt",
    "payload"
  ],
  "properties": {
    "schema": {
      "const": "paperclip.prp.event.v3"
    },
    "sourceEventId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "sourceSeq": {
      "type": "integer",
      "minimum": 1,
      "maximum": 9007199254740991
    },
    "sourceInstanceId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "sourceKind": {
      "enum": [
        "runner",
        "control_plane"
      ]
    },
    "runId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "normalizedSessionId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "turnId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "itemId": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "eventType": {
      "enum": [
        "runner.connected",
        "runner.reconnected",
        "runner.reconciled",
        "runner.disconnected",
        "runner.draining",
        "runner.suspending",
        "runner.suspended",
        "runner.stopped",
        "runner.diagnostic",
        "runtime.phase.changed",
        "sandbox.metric",
        "workspace.ready",
        "workspace.change.updated",
        "workspace.diff.recorded",
        "workspace.file.referenced",
        "harness.starting",
        "harness.ready",
        "harness.exited",
        "harness.diagnostic",
        "plan.updated",
        "tool.execution.started",
        "tool.execution.progressed",
        "tool.execution.completed",
        "research.started",
        "research.progressed",
        "research.completed",
        "delegation.started",
        "delegation.updated",
        "delegation.completed",
        "model.route.changed",
        "model.verification.updated",
        "context.compacted",
        "artifact.viewed",
        "artifact.generated",
        "review.mode.changed",
        "hook.started",
        "hook.completed",
        "memory.citation.referenced",
        "safety.review.started",
        "safety.review.completed",
        "terminal.input.sent",
        "wait.started",
        "wait.completed",
        "provider.notice.recorded",
        "session.starting",
        "session.started",
        "session.resuming",
        "session.resumed",
        "session.reconciled",
        "session.updated",
        "session.closed",
        "session.failed",
        "session.capabilities.updated",
        "session.goal.snapshot",
        "session.goal.updated",
        "session.goal.cleared",
        "turn.submitted",
        "turn.accepted",
        "turn.started",
        "turn.completed",
        "turn.failed",
        "turn.interrupted",
        "turn.cancelled",
        "item.started",
        "item.delta",
        "item.completed",
        "item.failed",
        "usage.reported",
        "semantic_tool.input",
        "semantic_tool.result",
        "mcp_app.discovered",
        "mcp_app.resource.resolved",
        "mcp_app.initializing",
        "mcp_app.ready",
        "mcp_app.tool_input",
        "mcp_app.tool_result",
        "mcp_app.action.requested",
        "mcp_app.action.resolved",
        "mcp_app.host_context.changed",
        "mcp_app.failed",
        "mcp_app.teardown",
        "runtime_request.created",
        "runtime_request.resolved",
        "runtime_request.expired",
        "runtime_request.cancelled",
        "interaction.request.proposed",
        "interaction.request.materialized",
        "interaction.request.rejected",
        "interaction.response.progressed",
        "interaction.response.resolved",
        "interaction.response.delivered",
        "run.attached",
        "run.detached",
        "run.result.proposed",
        "run.result.accepted",
        "run.result.rejected",
        "attention.request.proposed",
        "attention.request.routed",
        "attention.request.resolved",
        "attention.request.expired",
        "attention.request.superseded",
        "work.assessment.recorded",
        "issue.status.decision.recorded",
        "issue.status.decision.applied",
        "issue.status.decision.rejected",
        "issue.status.decision.superseded",
        "run.terminal",
        "external_provider.dispatch_requested",
        "external_provider.operation_settled",
        "external_provider.input_available",
        "auth.credentials.updated"
      ]
    },
    "schemaVersion": {
      "const": 3
    },
    "priority": {
      "enum": [
        0,
        1,
        2
      ]
    },
    "emittedAt": {
      "type": "string",
      "format": "date-time"
    },
    "observedAt": {
      "type": "string",
      "format": "date-time"
    },
    "payload": {
      "type": "object",
      "additionalProperties": true
    },
    "debug": {
      "type": "object",
      "additionalProperties": true
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "eventType": {
            "enum": [
              "session.goal.snapshot",
              "session.goal.updated"
            ]
          }
        },
        "type": "object"
      },
      "then": {
        "properties": {
          "payload": {
            "type": "object",
            "required": [
              "goal"
            ],
            "properties": {
              "goal": {
                "oneOf": [
                  {
                    "$ref": "https://paperclip.dev/schemas/prp/v2/session-goal.schema.json#/$defs/snapshot"
                  },
                  {
                    "type": "null"
                  }
                ]
              }
            },
            "additionalProperties": true
          }
        },
        "type": "object"
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "session.capabilities.updated"
          }
        },
        "type": "object"
      },
      "then": {
        "properties": {
          "payload": {
            "type": "object",
            "required": [
              "sessionGoals"
            ],
            "properties": {
              "sessionGoals": {
                "$ref": "https://paperclip.dev/schemas/prp/v2/session-goal.schema.json#/$defs/capability"
              },
              "turnControls": {
                "$ref": "https://paperclip.dev/schemas/prp/v1/provider-descriptor.schema.json#/$defs/turnControls"
              }
            },
            "additionalProperties": true
          }
        },
        "type": "object"
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "external_provider.dispatch_requested"
          }
        },
        "type": "object"
      },
      "then": {
        "properties": {
          "payload": {
            "oneOf": [
              {
                "type": "object",
                "properties": {
                  "binding": {
                    "type": "object",
                    "properties": {
                      "companyId": {
                        "type": "string",
                        "minLength": 1,
                        "maxLength": 240
                      },
                      "agentId": {
                        "type": "string",
                        "minLength": 1,
                        "maxLength": 240
                      },
                      "bindingId": {
                        "type": "string",
                        "minLength": 1,
                        "maxLength": 240
                      },
                      "runId": {
                        "type": "string",
                        "minLength": 1,
                        "maxLength": 240
                      },
                      "normalizedSessionId": {
                        "type": "string",
                        "minLength": 1,
                        "maxLength": 240
                      },
                      "turnId": {
                        "type": "string",
                        "minLength": 1,
                        "maxLength": 240
                      },
                      "bindingGeneration": {
                        "type": "integer",
                        "minimum": 1
                      },
                      "assignmentRevision": {
                        "type": "integer",
                        "minimum": 1
                      }
                    },
                    "additionalProperties": false,
                    "required": [
                      "companyId",
                      "agentId",
                      "bindingId",
                      "runId",
                      "normalizedSessionId",
                      "turnId",
                      "bindingGeneration",
                      "assignmentRevision"
                    ]
                  },
                  "text": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 1048576
                  },
                  "instructions": {
                    "type": "string",
                    "maxLength": 1048576
                  },
                  "acceptByUnixMs": {
                    "type": "integer",
                    "minimum": 1
                  },
                  "expiresAtUnixMs": {
                    "type": "integer",
                    "minimum": 1
                  },
                  "completionContract": {
                    "type": "object",
                    "properties": {
                      "revision": {
                        "type": "string",
                        "minLength": 1
                      },
                      "criterionIds": {
                        "type": "array",
                        "minItems": 1,
                        "maxItems": 256,
                        "uniqueItems": true,
                        "items": {
                          "type": "string",
                          "minLength": 1
                        }
                      }
                    },
                    "required": [
                      "revision",
                      "criterionIds"
                    ],
                    "additionalProperties": false
                  },
                  "tools": {
                    "type": "array",
                    "items": {
                      "type": "object"
                    },
                    "maxItems": 512
                  }
                },
                "additionalProperties": false,
                "required": [
                  "binding",
                  "text",
                  "instructions",
                  "acceptByUnixMs",
                  "expiresAtUnixMs",
                  "completionContract",
                  "tools"
                ]
              },
              {
                "type": "object",
                "properties": {
                  "binding": {
                    "type": "object",
                    "properties": {
                      "companyId": {
                        "type": "string",
                        "minLength": 1,
                        "maxLength": 240
                      },
                      "agentId": {
                        "type": "string",
                        "minLength": 1,
                        "maxLength": 240
                      },
                      "bindingId": {
                        "type": "string",
                        "minLength": 1,
                        "maxLength": 240
                      },
                      "runId": {
                        "type": "string",
                        "minLength": 1,
                        "maxLength": 240
                      },
                      "normalizedSessionId": {
                        "type": "string",
                        "minLength": 1,
                        "maxLength": 240
                      },
                      "turnId": {
                        "type": "string",
                        "minLength": 1,
                        "maxLength": 240
                      },
                      "bindingGeneration": {
                        "type": "integer",
                        "minimum": 1
                      },
                      "assignmentRevision": {
                        "type": "integer",
                        "minimum": 1
                      }
                    },
                    "additionalProperties": false,
                    "required": [
                      "companyId",
                      "agentId",
                      "bindingId",
                      "runId",
                      "normalizedSessionId",
                      "turnId",
                      "bindingGeneration",
                      "assignmentRevision"
                    ]
                  },
                  "kind": {
                    "const": "authority_revoked"
                  },
                  "reason": {
                    "type": "string"
                  },
                  "externalStopConfirmed": {
                    "const": false
                  }
                },
                "required": [
                  "binding",
                  "kind",
                  "externalStopConfirmed"
                ],
                "additionalProperties": false
              }
            ]
          }
        },
        "type": "object"
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "external_provider.operation_settled"
          }
        },
        "type": "object"
      },
      "then": {
        "properties": {
          "payload": {
            "type": "object",
            "properties": {
              "binding": {
                "type": "object",
                "properties": {
                  "companyId": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 240
                  },
                  "agentId": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 240
                  },
                  "bindingId": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 240
                  },
                  "runId": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 240
                  },
                  "normalizedSessionId": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 240
                  },
                  "turnId": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 240
                  },
                  "bindingGeneration": {
                    "type": "integer",
                    "minimum": 1
                  },
                  "assignmentRevision": {
                    "type": "integer",
                    "minimum": 1
                  }
                },
                "additionalProperties": false,
                "required": [
                  "companyId",
                  "agentId",
                  "bindingId",
                  "runId",
                  "normalizedSessionId",
                  "turnId",
                  "bindingGeneration",
                  "assignmentRevision"
                ]
              },
              "requestId": {
                "type": "string",
                "minLength": 1,
                "maxLength": 240
              },
              "outcome": {
                "type": "object"
              }
            },
            "required": [
              "binding",
              "requestId",
              "outcome"
            ],
            "additionalProperties": false
          }
        },
        "type": "object"
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "external_provider.input_available"
          }
        },
        "type": "object"
      },
      "then": {
        "properties": {
          "payload": {
            "type": "object",
            "properties": {
              "binding": {
                "type": "object",
                "properties": {
                  "companyId": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 240
                  },
                  "agentId": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 240
                  },
                  "bindingId": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 240
                  },
                  "runId": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 240
                  },
                  "normalizedSessionId": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 240
                  },
                  "turnId": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 240
                  },
                  "bindingGeneration": {
                    "type": "integer",
                    "minimum": 1
                  },
                  "assignmentRevision": {
                    "type": "integer",
                    "minimum": 1
                  }
                },
                "additionalProperties": false,
                "required": [
                  "companyId",
                  "agentId",
                  "bindingId",
                  "runId",
                  "normalizedSessionId",
                  "turnId",
                  "bindingGeneration",
                  "assignmentRevision"
                ]
              },
              "requestId": {
                "type": "string",
                "minLength": 1,
                "maxLength": 240
              },
              "turnId": {
                "type": "string",
                "minLength": 1,
                "maxLength": 240
              },
              "inputDigest": {
                "type": "string",
                "pattern": "^sha256:[0-9a-f]{64}$"
              },
              "response": {
                "$ref": "https://paperclip.dev/schemas/prp/v1/question-response.schema.json"
              }
            },
            "required": [
              "binding",
              "requestId",
              "turnId",
              "inputDigest",
              "response"
            ],
            "additionalProperties": false
          }
        },
        "type": "object"
      }
    },
    {
      "if": {
        "properties": {
          "eventType": {
            "const": "auth.credentials.updated"
          }
        }
      },
      "then": {
        "properties": {
          "sourceKind": {
            "const": "runner"
          },
          "priority": {
            "const": 0
          },
          "payload": {
            "$ref": "https://paperclip.dev/schemas/prp/v1/auth-credentials.schema.json"
          }
        }
      }
    }
  ],
  "additionalProperties": true
} as const;

export const sessionGoalSchema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v2/session-goal.schema.json",
  "title": "PRP v2 session goal capability and snapshot",
  "$defs": {
    "capability": {
      "type": "object",
      "required": [
        "availability",
        "actions",
        "autonomousUpdates",
        "persistentAcrossResume",
        "maxObjectiveChars",
        "tokenBudgetControl",
        "usageReporting"
      ],
      "properties": {
        "availability": {
          "enum": [
            "available",
            "unsupported",
            "policy_disabled"
          ]
        },
        "actions": {
          "type": "array",
          "items": {
            "enum": [
              "set",
              "pause",
              "resume",
              "clear"
            ]
          },
          "uniqueItems": true
        },
        "autonomousUpdates": {
          "type": "boolean"
        },
        "persistentAcrossResume": {
          "type": "boolean"
        },
        "maxObjectiveChars": {
          "type": "integer",
          "minimum": 1,
          "maximum": 4000
        },
        "tokenBudgetControl": {
          "type": "boolean"
        },
        "usageReporting": {
          "type": "boolean"
        },
        "reasonCode": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160
        },
        "reason": {
          "type": "string",
          "minLength": 1,
          "maxLength": 1000
        }
      },
      "additionalProperties": true
    },
    "snapshot": {
      "type": "object",
      "required": [
        "objective",
        "status",
        "tokenBudget",
        "tokensUsed",
        "elapsedSeconds",
        "iterations",
        "lastReason",
        "createdAt",
        "updatedAt",
        "completedAt",
        "workingNow"
      ],
      "properties": {
        "objective": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4000
        },
        "status": {
          "enum": [
            "active",
            "paused",
            "blocked",
            "limited",
            "usage_limited",
            "budget_limited",
            "complete"
          ]
        },
        "tokenBudget": {
          "type": [
            "integer",
            "null"
          ],
          "minimum": 1
        },
        "tokensUsed": {
          "type": [
            "integer",
            "null"
          ],
          "minimum": 0
        },
        "elapsedSeconds": {
          "type": [
            "number",
            "null"
          ],
          "minimum": 0
        },
        "iterations": {
          "type": [
            "integer",
            "null"
          ],
          "minimum": 0
        },
        "lastReason": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 4000
        },
        "createdAt": {
          "type": [
            "string",
            "null"
          ],
          "format": "date-time"
        },
        "updatedAt": {
          "type": [
            "string",
            "null"
          ],
          "format": "date-time"
        },
        "completedAt": {
          "type": [
            "string",
            "null"
          ],
          "format": "date-time"
        },
        "workingNow": {
          "type": "boolean"
        }
      },
      "additionalProperties": true
    }
  }
} as const;

export const fixtureSchema = {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://paperclip.dev/schemas/prp/v1/fixture.schema.json",
  "title": "PRP scripted replay fixture",
  "type": "object",
  "required": [
    "schema",
    "fixtureVersion",
    "protocolVersion",
    "name",
    "description",
    "identity",
    "capabilities",
    "commands",
    "events",
    "result"
  ],
  "properties": {
    "schema": {
      "const": "paperclip.prp.fixture.v1"
    },
    "fixtureVersion": {
      "const": 1
    },
    "protocolVersion": {
      "enum": [
        1,
        2,
        3
      ]
    },
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 120
    },
    "description": {
      "type": "string",
      "minLength": 1,
      "maxLength": 1000
    },
    "identity": {
      "$ref": "https://paperclip.dev/schemas/prp/v1/identity.schema.json"
    },
    "capabilities": {
      "oneOf": [
        {
          "$ref": "https://paperclip.dev/schemas/prp/v1/capabilities.schema.json"
        },
        {
          "$ref": "https://paperclip.dev/schemas/prp/v2/capabilities.schema.json"
        },
        {
          "$ref": "https://paperclip.dev/schemas/prp/v3/capabilities.schema.json"
        }
      ]
    },
    "commands": {
      "type": "array",
      "items": {
        "oneOf": [
          {
            "$ref": "https://paperclip.dev/schemas/prp/v1/command.schema.json"
          },
          {
            "$ref": "https://paperclip.dev/schemas/prp/v2/command.schema.json"
          },
          {
            "$ref": "https://paperclip.dev/schemas/prp/v3/command.schema.json"
          }
        ]
      }
    },
    "events": {
      "type": "array",
      "minItems": 1,
      "items": {
        "oneOf": [
          {
            "$ref": "https://paperclip.dev/schemas/prp/v1/event.schema.json"
          },
          {
            "$ref": "https://paperclip.dev/schemas/prp/v2/event.schema.json"
          },
          {
            "$ref": "https://paperclip.dev/schemas/prp/v3/event.schema.json"
          }
        ]
      }
    },
    "requests": {
      "type": "array",
      "items": {
        "$ref": "https://paperclip.dev/schemas/prp/v1/request.schema.json"
      }
    },
    "result": {
      "$ref": "https://paperclip.dev/schemas/prp/v1/result.schema.json"
    }
  },
  "additionalProperties": true
} as const;

export const prpSchemaBundle = {
  "auth-credentials": authCredentialsSchema,
  "identity": identitySchema,
  "capabilities": capabilitiesSchema,
  "capabilities-v2": capabilitiesV2Schema,
  "capabilities-v3": capabilitiesV3Schema,
  "command": commandSchema,
  "command-v2": commandV2Schema,
  "command-v3": commandV3Schema,
  "provider-descriptor": providerDescriptorSchema,
  "provider-event": providerEventSchema,
  "workspace-diff": workspaceDiffSchema,
  "workspace-file-reference": workspaceFileReferenceSchema,
  "semantic-tool": semanticToolSchema,
  "usage": usageSchema,
  "stop-reason": stopReasonSchema,
  "terminal": terminalSchema,
  "question-set": questionSetSchema,
  "question-response": questionResponseSchema,
  "question-adapter-fixture": questionAdapterFixtureSchema,
  "request": requestSchema,
  "result": resultSchema,
  "event": eventSchema,
  "event-v2": eventV2Schema,
  "event-v3": eventV3Schema,
  "session-goal": sessionGoalSchema,
  "fixture": fixtureSchema,
} as const;