{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://pinthread.dev/schemas/thread-v1.json",
  "title": "PinthreadThread",
  "description": "Versioned JSON Schema (v1) for pinthread thread, comment, and anchor payloads across API, CLI, and MCP surfaces.",
  "oneOf": [
    { "$ref": "#/definitions/Thread" },
    { "$ref": "#/definitions/CompactThread" }
  ],
  "definitions": {
    "Thread": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "page", "anchor", "resolved", "createdAt", "updatedAt", "comments"],
      "properties": {
        "id": { "type": "string" },
        "page": { "type": "string" },
        "anchor": { "$ref": "#/definitions/Anchor" },
        "resolved": { "type": "boolean" },
        "resolvedBy": {
          "type": ["object", "string", "null"],
          "additionalProperties": false,
          "required": ["id", "name"],
          "properties": {
            "id": { "type": "string" },
            "name": { "type": "string" },
            "verified": { "type": "boolean" },
            "agent": { "type": "boolean" }
          }
        },
        "status": {
          "type": "string",
          "enum": ["open", "in_progress", "needs_review", "resolved", "dismissed"]
        },
        "assignee": {
          "type": ["object", "null"],
          "additionalProperties": false,
          "required": ["id", "name"],
          "properties": {
            "id": { "type": "string" },
            "name": { "type": "string" }
          }
        },
        "priority": {
          "type": "string",
          "enum": ["none", "low", "medium", "high", "urgent"]
        },
        "labels": {
          "type": "array",
          "items": { "type": "string" }
        },
        "dueAt": { "type": ["number", "null"] },
        "dismissalReason": { "type": ["string", "null"] },
        "awaitingReviewer": { "type": "boolean" },
        "intent": {
          "type": ["string", "null"],
          "enum": ["fix", "change", "question", "approve", null]
        },
        "revision": { "type": ["string", "null"] },
        "currentRevision": { "type": ["string", "null"] },
        "stale": { "type": "boolean" },
        "createdAt": { "type": "number" },
        "updatedAt": { "type": "number" },
        "attachments": {
          "type": "array",
          "items": { "$ref": "#/definitions/ThreadAttachment" }
        },
        "suggestion": { "$ref": "#/definitions/ThreadSuggestion" },
        "ui_state": { "type": "string" },
        "Motion": { "type": "string" },
        "target": { "$ref": "#/definitions/TargetDetails" },
        "comments": {
          "type": "array",
          "items": { "$ref": "#/definitions/Comment" }
        }
      }
    },
    "CompactThread": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "page", "resolved", "status", "awaitingReviewer", "stale", "comments"],
      "properties": {
        "id": { "type": "string" },
        "page": { "type": "string" },
        "resolved": { "type": "boolean" },
        "status": {
          "type": "string",
          "enum": ["open", "in_progress", "needs_review", "resolved", "dismissed"]
        },
        "dismissalReason": { "type": ["string", "null"] },
        "awaitingReviewer": { "type": "boolean" },
        "intent": {
          "type": ["string", "null"],
          "enum": ["fix", "change", "question", "approve", null]
        },
        "stale": { "type": "boolean" },
        "selector": { "type": "string" },
        "text": { "type": "string" },
        "source": { "type": "string" },
        "suggestion": {
          "type": "object",
          "additionalProperties": false,
          "required": ["old", "new"],
          "properties": {
            "old": { "type": "string" },
            "new": { "type": "string" }
          }
        },
        "ui_state": { "type": "string" },
        "attachments": {
          "type": "array",
          "items": { "$ref": "#/definitions/ThreadAttachment" }
        },
        "Motion": { "type": "string" },
        "comments": {
          "type": "array",
          "items": { "$ref": "#/definitions/CompactComment" }
        }
      }
    },
    "Comment": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "body", "author", "createdAt"],
      "properties": {
        "id": { "type": "string" },
        "body": { "type": "string" },
        "author": {
          "oneOf": [
            { "type": "string" },
            {
              "type": "object",
              "additionalProperties": false,
              "required": ["id", "name"],
              "properties": {
                "id": { "type": "string" },
                "name": { "type": "string" },
                "verified": { "type": "boolean" },
                "agent": { "type": "boolean" },
                "avatarUrl": { "type": "string" },
                "accentColor": { "type": "string" }
              }
            }
          ]
        },
        "verified": { "type": "boolean" },
        "createdAt": { "type": "number" },
        "editedAt": { "type": ["number", "null"] },
        "reactions": {
          "type": "object",
          "additionalProperties": {
            "type": "array",
            "items": { "type": "string" }
          }
        }
      }
    },
    "CompactComment": {
      "type": "object",
      "additionalProperties": false,
      "required": ["author", "body"],
      "properties": {
        "author": { "type": "string" },
        "body": { "type": "string" }
      }
    },
    "Anchor": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "selector",
        "text",
        "x",
        "y",
        "width",
        "height",
        "pageX",
        "pageY",
        "viewportWidth"
      ],
      "properties": {
        "unstacked": { "type": "boolean" },
        "selector": { "type": "string" },
        "selectorVersion": { "type": "integer", "enum": [1] },
        "framePath": {
          "type": "array",
          "items": { "type": "string" },
          "maxItems": 16
        },
        "text": { "type": "string" },
        "x": { "type": "number", "minimum": 0, "maximum": 1 },
        "y": { "type": "number", "minimum": 0, "maximum": 1 },
        "width": { "type": "number", "minimum": 0, "maximum": 1 },
        "height": { "type": "number", "minimum": 0, "maximum": 1 },
        "pageX": { "type": "number" },
        "pageY": { "type": "number" },
        "viewportWidth": { "type": "number" },
        "source": { "type": "string" },
        "details": { "$ref": "#/definitions/AnchorDetails" },
        "context": { "$ref": "#/definitions/AnchorContext" },
        "capture": { "$ref": "#/definitions/Capture" },
        "captureFailure": { "$ref": "#/definitions/CaptureFailure" },
        "suggestion": { "$ref": "#/definitions/AnchorSuggestion" },
        "targets": {
          "type": "array",
          "items": { "$ref": "#/definitions/AnchorTarget" },
          "maxItems": 20
        },
        "state": { "$ref": "#/definitions/UiState" }
      }
    },
    "CaptureFailure": {
      "type": "object",
      "additionalProperties": false,
      "required": ["code", "message", "elapsedMs", "renderer"],
      "properties": {
        "code": { "type": "string", "enum": ["capture_failed", "capture_timeout"] },
        "message": { "type": "string", "const": "Screenshot capture failed." },
        "elapsedMs": { "type": "integer", "minimum": 0, "maximum": 3000 },
        "renderer": { "type": "string", "const": "modern-screenshot4.7.0" }
      }
    },
    "AnchorTarget": {
      "type": "object",
      "additionalProperties": false,
      "required": ["selector", "text"],
      "properties": {
        "selector": { "type": "string" },
        "selectorVersion": { "type": "integer", "enum": [1] },
        "framePath": {
          "type": "array",
          "items": { "type": "string" },
          "maxItems": 16
        },
        "text": { "type": "string" },
        "source": { "type": "string" },
        "context": { "$ref": "#/definitions/AnchorContext" }
      }
    },
    "AnchorDetails": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "component": { "type": "string" },
        "file": { "type": "string" },
        "line": { "type": "integer" },
        "column": { "type": "integer" },
        "attrs": {
          "type": "object",
          "additionalProperties": { "type": "string" }
        }
      }
    },
    "AnchorContext": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "tag": { "type": "string" },
        "role": { "type": "string" },
        "label": { "type": "string" },
        "nearby": { "type": "string" },
        "classes": { "type": "string" },
        "selectedText": { "type": "string" },
        "styles": { "type": "string" },
        "scope": { "type": "string" },
        "motion": { "$ref": "#/definitions/MotionContext" }
      }
    },
    "Capture": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "env": {
          "type": "object",
          "additionalProperties": { "type": ["string", "number", "null"] }
        },
        "console": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["level", "text", "at"],
            "properties": {
              "level": { "type": "string", "enum": ["error", "warn"] },
              "text": { "type": "string" },
              "at": { "type": "number" }
            }
          }
        },
        "network": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["method", "url", "status", "ms", "at"],
            "properties": {
              "method": { "type": "string" },
              "url": { "type": "string" },
              "status": { "type": "number" },
              "ms": { "type": "number" },
              "at": { "type": "number" }
            }
          }
        }
      }
    },
    "MotionContext": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "reducedMotion": { "type": "boolean" },
        "progress": { "type": "number" },
        "animations": {
          "type": "array",
          "items": { "$ref": "#/definitions/MotionAnimation" }
        },
        "truncated": { "type": "boolean" }
      }
    },
    "MotionAnimation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["kind"],
      "properties": {
        "kind": { "type": "string", "enum": ["css-animation", "css-transition", "web-animation"] },
        "name": { "type": "string" },
        "properties": { "type": "array", "items": { "type": "string" } },
        "duration": { "type": "number" },
        "delay": { "type": "number" },
        "iterations": { "oneOf": [{ "type": "number" }, { "type": "string", "enum": ["infinite"] }] },
        "easing": { "type": "string" },
        "playState": { "type": "string" },
        "progress": { "type": "number" },
        "keyframes": { "type": "array", "items": { "$ref": "#/definitions/MotionKeyframe" } },
        "truncated": { "type": "boolean" },
        "target": { "type": "string", "enum": ["target", "ancestor"] },
        "selector": { "type": "string" }
      }
    },
    "MotionKeyframe": {
      "type": "object",
      "additionalProperties": { "type": ["string", "number", "null"] },
      "properties": {
        "offset": { "type": ["number", "null"] },
        "easing": { "type": "string" }
      }
    },

    "UiState": {
      "type": "object",
      "additionalProperties": false,
      "required": ["layers"],
      "properties": {
        "layers": {
          "type": "array",
          "items": { "$ref": "#/definitions/UiLayer" },
          "maxItems": 4
        },
        "scroll": {
          "type": "object",
          "additionalProperties": false,
          "required": ["x", "y"],
          "properties": {
            "x": { "type": "number" },
            "y": { "type": "number" }
          }
        }
      }
    },
    "UiLayer": {
      "type": "object",
      "additionalProperties": false,
      "required": ["kind"],
      "properties": {
        "kind": {
          "type": "string",
          "enum": ["dialog", "menu", "listbox", "popover", "tooltip", "disclosure"]
        },
        "label": { "type": "string" },
        "layer": { "type": "string" },
        "owner": { "type": "string" },
        "open": { "type": "string" },
        "scrollTop": { "type": "number" }
      }
    },
    "AnchorSuggestion": {
      "type": "object",
      "additionalProperties": false,
      "required": ["old", "new"],
      "properties": {
        "old": { "type": "string" },
        "new": { "type": "string" },
        "applied": {
          "type": "object",
          "additionalProperties": false,
          "required": ["at", "by"],
          "properties": {
            "at": { "type": "number" },
            "by": { "type": "string" }
          }
        }
      }
    },
    "ThreadSuggestion": {
      "type": "object",
      "additionalProperties": false,
      "required": ["old", "new"],
      "properties": {
        "old": { "type": "string" },
        "new": { "type": "string" },
        "selector": { "type": "string" },
        "applied": {
          "type": "object",
          "additionalProperties": false,
          "required": ["at", "by"],
          "properties": {
            "at": { "type": "number" },
            "by": { "type": "string" }
          }
        }
      }
    },
    "TargetDetails": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "selector": { "type": "string" },
        "text": { "type": "string" },
        "role": { "type": "string" },
        "label": { "type": "string" },
        "classes": { "type": "string" },
        "nearby": { "type": "string" },
        "attributes": {
          "type": "object",
          "additionalProperties": { "type": "string" }
        },
        "component": { "type": "string" },
        "source": { "type": "string" },
        "sourceLocation": { "type": "string" },
        "editor": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "vscode": { "type": "string" },
            "cursor": { "type": "string" }
          }
        }
      }
    },
    "ThreadAttachment": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "kind", "name", "mime", "size", "createdAt"],
      "properties": {
        "id": { "type": "string" },
        "kind": {
          "type": "string",
          "enum": ["image", "screenshot", "log", "metadata", "video", "replay"]
        },
        "name": { "type": "string" },
        "mime": { "type": "string" },
        "size": { "type": "number" },
        "createdAt": { "type": "number" },
        "bytes": { "type": "number" },
        "width": { "type": "number" },
        "height": { "type": "number" },
        "commentId": { "type": "string" },
        "box": {
          "type": "object",
          "additionalProperties": false,
          "required": ["x", "y", "width", "height"],
          "properties": {
            "x": { "type": "number" },
            "y": { "type": "number" },
            "width": { "type": "number" },
            "height": { "type": "number" }
          }
        },
        "pageUrl": { "type": "string" },
        "viewport": {
          "type": "object",
          "additionalProperties": false,
          "required": ["width", "height"],
          "properties": {
            "width": { "type": "number" },
            "height": { "type": "number" }
          }
        },
        "text": { "type": "string" },
        "url": { "type": "string" },
        "expiresAt": { "type": "number" }
      }
    }
  }
}
