{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://a.skh.am/pi-daemon/dashboard-session-draft.schema.json",
  "title": "Pi Daemon Dash lazy session draft contract v1",
  "description": "Browser-safe lazy session draft resources and durable first-send tickets. Private stored prompt content is intentionally absent.",
  "oneOf": [
    {
      "$ref": "#/$defs/createRequest"
    },
    {
      "$ref": "#/$defs/cancelRequest"
    },
    {
      "$ref": "#/$defs/sendRequest"
    },
    {
      "$ref": "#/$defs/resource"
    },
    {
      "$ref": "#/$defs/sendTicket"
    }
  ],
  "$defs": {
    "id": {
      "type": "string",
      "minLength": 1,
      "maxLength": 128,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
    },
    "idempotencyKey": {
      "type": "string",
      "minLength": 1,
      "maxLength": 512,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
    },
    "timestamp": {
      "type": "string",
      "minLength": 20,
      "maxLength": 64,
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\\.[0-9]{1,9})?Z$"
    },
    "error": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "code",
        "message",
        "retryable"
      ],
      "properties": {
        "code": {
          "$ref": "#/$defs/id"
        },
        "message": {
          "type": "string",
          "minLength": 1,
          "maxLength": 1024
        },
        "retryable": {
          "type": "boolean"
        }
      }
    },
    "model": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "provider",
        "id"
      ],
      "properties": {
        "provider": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128
        },
        "id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 256
        },
        "thinkingLevel": {
          "enum": [
            "off",
            "minimal",
            "low",
            "medium",
            "high",
            "xhigh",
            "max"
          ]
        }
      }
    },
    "tools": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "mode"
      ],
      "properties": {
        "mode": {
          "enum": [
            "default",
            "none",
            "no-builtin",
            "allowlist"
          ]
        },
        "include": {
          "type": "array",
          "maxItems": 32,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "minLength": 1,
            "maxLength": 128,
            "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
          }
        },
        "exclude": {
          "type": "array",
          "maxItems": 32,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "minLength": 1,
            "maxLength": 128,
            "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
          }
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "mode": {
                "const": "none"
              }
            }
          },
          "then": {
            "properties": {
              "include": {
                "type": "array",
                "maxItems": 0
              },
              "exclude": {
                "type": "array",
                "maxItems": 0
              }
            }
          }
        }
      ]
    },
    "resources": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "noExtensions",
        "noSkills",
        "noPromptTemplates",
        "noThemes",
        "noContextFiles",
        "projectTrust"
      ],
      "properties": {
        "noExtensions": {
          "type": "boolean"
        },
        "noSkills": {
          "type": "boolean"
        },
        "noPromptTemplates": {
          "type": "boolean"
        },
        "noThemes": {
          "type": "boolean"
        },
        "noContextFiles": {
          "type": "boolean"
        },
        "projectTrust": {
          "enum": [
            "default",
            "deny",
            "approve"
          ]
        }
      }
    },
    "spec": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "cwd",
        "persistence",
        "tools",
        "resources",
        "isolation"
      ],
      "properties": {
        "cwd": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4096
        },
        "name": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128
        },
        "persistence": {
          "enum": [
            "persistent",
            "memory"
          ]
        },
        "model": {
          "$ref": "#/$defs/model"
        },
        "tools": {
          "$ref": "#/$defs/tools"
        },
        "resources": {
          "$ref": "#/$defs/resources"
        },
        "isolation": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "mode"
          ],
          "properties": {
            "mode": {
              "const": "unisolated"
            }
          }
        }
      }
    },
    "sessionIdentity": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "sessionId",
        "generation"
      ],
      "properties": {
        "sessionId": {
          "$ref": "#/$defs/id"
        },
        "generation": {
          "type": "integer",
          "minimum": 0
        }
      }
    },
    "materialization": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "ticketId",
        "state"
      ],
      "properties": {
        "ticketId": {
          "$ref": "#/$defs/id"
        },
        "state": {
          "enum": [
            "queued",
            "running",
            "succeeded",
            "failed",
            "indeterminate"
          ]
        },
        "session": {
          "$ref": "#/$defs/sessionIdentity"
        },
        "error": {
          "$ref": "#/$defs/error"
        }
      }
    },
    "resource": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "contractVersion",
        "draftId",
        "revision",
        "state",
        "createdAt",
        "updatedAt",
        "spec",
        "firstMessageStartsSession"
      ],
      "properties": {
        "contractVersion": {
          "const": "1.0"
        },
        "draftId": {
          "$ref": "#/$defs/id"
        },
        "revision": {
          "type": "integer",
          "minimum": 1
        },
        "state": {
          "enum": [
            "draft",
            "materializing",
            "live",
            "failed",
            "indeterminate",
            "cancelled"
          ]
        },
        "createdAt": {
          "$ref": "#/$defs/timestamp"
        },
        "updatedAt": {
          "$ref": "#/$defs/timestamp"
        },
        "spec": {
          "$ref": "#/$defs/spec"
        },
        "firstMessageStartsSession": {
          "const": true
        },
        "materialization": {
          "$ref": "#/$defs/materialization"
        }
      }
    },
    "createRequest": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "requestId",
        "idempotencyKey",
        "spec"
      ],
      "properties": {
        "requestId": {
          "$ref": "#/$defs/id"
        },
        "idempotencyKey": {
          "$ref": "#/$defs/idempotencyKey"
        },
        "draftId": {
          "$ref": "#/$defs/id"
        },
        "spec": {
          "$ref": "#/$defs/spec"
        }
      }
    },
    "cancelRequest": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "requestId",
        "idempotencyKey",
        "expectedRevision"
      ],
      "properties": {
        "requestId": {
          "$ref": "#/$defs/id"
        },
        "idempotencyKey": {
          "$ref": "#/$defs/idempotencyKey"
        },
        "expectedRevision": {
          "type": "integer",
          "minimum": 1
        }
      }
    },
    "sendRequest": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "requestId",
        "idempotencyKey",
        "expectedRevision",
        "message"
      ],
      "properties": {
        "requestId": {
          "$ref": "#/$defs/id"
        },
        "idempotencyKey": {
          "$ref": "#/$defs/idempotencyKey"
        },
        "expectedRevision": {
          "type": "integer",
          "minimum": 1
        },
        "message": {
          "type": "string",
          "minLength": 1,
          "maxLength": 262144
        }
      }
    },
    "sendTicket": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "ticketId",
        "draftId",
        "draftRevision",
        "requestId",
        "idempotencyKey",
        "state",
        "submittedAt",
        "updatedAt"
      ],
      "properties": {
        "ticketId": {
          "$ref": "#/$defs/id"
        },
        "draftId": {
          "$ref": "#/$defs/id"
        },
        "draftRevision": {
          "type": "integer",
          "minimum": 1
        },
        "requestId": {
          "$ref": "#/$defs/id"
        },
        "idempotencyKey": {
          "$ref": "#/$defs/idempotencyKey"
        },
        "state": {
          "enum": [
            "queued",
            "running",
            "succeeded",
            "failed",
            "indeterminate"
          ]
        },
        "submittedAt": {
          "$ref": "#/$defs/timestamp"
        },
        "updatedAt": {
          "$ref": "#/$defs/timestamp"
        },
        "session": {
          "$ref": "#/$defs/sessionIdentity"
        },
        "error": {
          "$ref": "#/$defs/error"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "state": {
                "const": "succeeded"
              }
            }
          },
          "then": {
            "type": "object",
            "required": [
              "session"
            ],
            "properties": {
              "session": {
                "$ref": "#/$defs/sessionIdentity"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "state": {
                "const": "failed"
              }
            }
          },
          "then": {
            "type": "object",
            "required": [
              "error"
            ],
            "properties": {
              "error": {
                "$ref": "#/$defs/error"
              }
            }
          }
        }
      ]
    }
  }
}
