{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://artifacts.apis.io/shapelessai/json-schema/shapelessai-identity-graph-event-schema.json",
  "title": "IdentityGraphEvent",
  "description": "One line of the NDJSON stream a resolve, an expand or a post writes, in this order: status lines and pulls as they run, graph patches as the graph grows, then exactly one done (the bill) or one error. A pull event repeats per pull id as it moves from running to ok; the last one wins. On error or a cut stream a key is charged only what the pulls it saw cost.",
  "x-generated": "2026-11-11",
  "x-method": "derived",
  "x-generator": "derive-json-schema.py",
  "x-source": "openapi/shapelessai-openapi.json#/components/schemas/IdentityGraphEvent",
  "oneOf": [
    {
      "type": "object",
      "properties": {
        "type": {
          "const": "status"
        },
        "label": {
          "type": "string"
        }
      },
      "required": [
        "type",
        "label"
      ]
    },
    {
      "type": "object",
      "properties": {
        "type": {
          "const": "pull"
        },
        "pull": {
          "$ref": "#/$defs/PullRecord"
        }
      },
      "required": [
        "type",
        "pull"
      ]
    },
    {
      "type": "object",
      "properties": {
        "type": {
          "const": "graph"
        },
        "nodes": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": true
          }
        },
        "edges": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": true
          }
        }
      },
      "required": [
        "type",
        "nodes",
        "edges"
      ]
    },
    {
      "type": "object",
      "properties": {
        "type": {
          "const": "done"
        },
        "anchor": {
          "type": "string",
          "description": "The node the run ended on: open it with GET /api/identity/node/{id}."
        },
        "costUsd": {
          "type": "number",
          "description": "What the run cost at engine price; the charge is this at the plan's rate."
        },
        "summary": {
          "type": "string"
        },
        "breakdown": {
          "$ref": "#/$defs/CostBreakdown"
        }
      },
      "required": [
        "type",
        "anchor",
        "costUsd",
        "summary"
      ]
    },
    {
      "type": "object",
      "properties": {
        "type": {
          "const": "error"
        },
        "message": {
          "type": "string"
        },
        "costUsd": {
          "type": "number",
          "description": "resolve: what the failed run spent at engine price. The key is charged only the pulls it saw."
        },
        "breakdown": {
          "$ref": "#/$defs/CostBreakdown"
        },
        "stoppedBy": {
          "type": "string",
          "enum": [
            "budget",
            "steps",
            "refusal",
            "stuck",
            "abort",
            "error",
            "undelivered"
          ],
          "description": "What ended a resolve that landed nothing."
        }
      },
      "required": [
        "type",
        "message"
      ]
    }
  ],
  "$defs": {
    "CostBreakdown": {
      "type": "object",
      "description": "Where a stream's engine cost went: the model's turns and the vendors' calls (probes, search, pages).",
      "properties": {
        "modelUsd": {
          "type": "number"
        },
        "vendorUsd": {
          "type": "number"
        }
      },
      "required": [
        "modelUsd",
        "vendorUsd"
      ]
    },
    "PullRecord": {
      "type": "object",
      "description": "One vendor call: what it asked, what it cost, what came back.",
      "properties": {
        "id": {
          "type": "string"
        },
        "vendor": {
          "type": "string"
        },
        "endpoint": {
          "type": "string"
        },
        "node": {
          "type": [
            "string",
            "null"
          ]
        },
        "expansion": {
          "type": [
            "string",
            "null"
          ],
          "enum": [
            "profile",
            "posts",
            "sponsors",
            "ads",
            "creators",
            "metrics",
            "labels",
            null
          ]
        },
        "status": {
          "type": "string",
          "enum": [
            "running",
            "ok",
            "empty",
            "failed",
            "cached"
          ],
          "description": "cached is a day-cache hit: $0."
        },
        "items": {
          "type": "integer"
        },
        "costUsd": {
          "type": "number"
        },
        "error": {
          "type": [
            "string",
            "null"
          ]
        },
        "startedAt": {
          "type": "string",
          "format": "date-time"
        },
        "finishedAt": {
          "type": [
            "string",
            "null"
          ],
          "format": "date-time"
        }
      },
      "required": [
        "id",
        "vendor",
        "status",
        "costUsd"
      ],
      "additionalProperties": true
    }
  }
}