{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Fontaine headless contract, version 1",
  "description": "Every object `fontaine -p` prints with --output-format json or stream-json, and every line it reads with --input-format stream-json. Additive fields keep schema_version 1; a rename or a removal bumps it. Consumers must ignore fields they do not know.",
  "oneOf": [
    { "$ref": "#/$defs/init" },
    { "$ref": "#/$defs/assistant" },
    { "$ref": "#/$defs/assistant_delta" },
    { "$ref": "#/$defs/model_call" },
    { "$ref": "#/$defs/tool_call" },
    { "$ref": "#/$defs/gate_decision" },
    { "$ref": "#/$defs/tool_result" },
    { "$ref": "#/$defs/goal" },
    { "$ref": "#/$defs/usage_paused" },
    { "$ref": "#/$defs/usage_resumed" },
    { "$ref": "#/$defs/afk_report" },
    { "$ref": "#/$defs/result" },
    { "$ref": "#/$defs/user_input" }
  ],
  "$defs": {
    "envelope": {
      "type": "object",
      "required": ["type", "schema_version", "session_id"],
      "properties": {
        "schema_version": { "const": 1 },
        "session_id": { "type": "string", "minLength": 1 }
      }
    },
    "tier": {
      "enum": [
        "deny_rule",
        "ask_rule",
        "protected_path",
        "hook",
        "read_only_fast_path",
        "tracked_edit",
        "scratch_write",
        "shell_edit",
        "tier05",
        "spawn",
        "coordination",
        "allow_rule",
        "mode",
        "screen",
        "guardian",
        "dont_ask",
        "human",
        "guardian_off",
        "afk",
        "bug_draft",
        "bug_report"
      ]
    },
    "usage_bucket": {
      "type": "object",
      "required": [
        "input_tokens",
        "output_tokens",
        "cache_read_input_tokens",
        "cache_creation_input_tokens",
        "cost_usd"
      ],
      "properties": {
        "input_tokens": { "type": "integer", "minimum": 0 },
        "output_tokens": { "type": "integer", "minimum": 0 },
        "cache_read_input_tokens": { "type": "integer", "minimum": 0 },
        "cache_creation_input_tokens": { "type": "integer", "minimum": 0 },
        "cost_usd": { "type": ["number", "null"], "minimum": 0 }
      }
    },
    "init": {
      "allOf": [
        { "$ref": "#/$defs/envelope" },
        {
          "type": "object",
          "required": [
            "type",
            "model",
            "guardian_model",
            "guardian_profile",
            "permission_mode",
            "workspace_roots",
            "config_sources",
            "workspace_trust",
            "suspended_allow_rules"
          ],
          "properties": {
            "type": { "const": "init" },
            "model": { "type": "string", "pattern": "^[^/]+/.+$" },
            "guardian_model": { "type": ["string", "null"] },
            "guardian_available": { "type": ["boolean", "null"] },
            "guardian_profile": { "enum": ["full", "minimal"] },
            "permission_mode": {
              "enum": ["manual", "acceptEdits", "auto", "dontAsk", "afk"]
            },
            "workspace_roots": {
              "type": "array",
              "items": { "type": "string" },
              "minItems": 1
            },
            "config_sources": {
              "type": "array",
              "items": {
                "type": "object",
                "required": ["scope", "path", "loaded"],
                "properties": {
                  "scope": {
                    "enum": ["managed", "cli", "project-local", "project", "user"]
                  },
                  "path": { "type": ["string", "null"] },
                  "loaded": { "type": "boolean" }
                }
              }
            },
            "workspace_trust": { "enum": ["trusted", "untrusted"] },
            "project_executables": { "type": "boolean" },
            "suspended_allow_rules": {
              "type": "array",
              "items": { "type": "string" }
            },
            "tools": { "type": "array", "items": { "type": "string" } }
          }
        }
      ]
    },
    "assistant": {
      "allOf": [
        { "$ref": "#/$defs/envelope" },
        {
          "type": "object",
          "required": ["type", "text"],
          "properties": {
            "type": { "const": "assistant" },
            "text": { "type": "string" }
          }
        }
      ]
    },
    "assistant_delta": {
      "allOf": [
        { "$ref": "#/$defs/envelope" },
        {
          "type": "object",
          "required": ["type", "text"],
          "properties": {
            "type": { "const": "assistant_delta" },
            "text": { "type": "string" }
          }
        }
      ]
    },
    "model_call": {
      "allOf": [
        { "$ref": "#/$defs/envelope" },
        {
          "type": "object",
          "required": [
            "type",
            "input_tokens",
            "output_tokens",
            "cache_read_input_tokens",
            "cache_creation_input_tokens",
            "calls_tool"
          ],
          "properties": {
            "type": { "const": "model_call" },
            "input_tokens": { "type": "integer", "minimum": 0 },
            "output_tokens": { "type": "integer", "minimum": 0 },
            "cache_read_input_tokens": { "type": "integer", "minimum": 0 },
            "cache_creation_input_tokens": { "type": "integer", "minimum": 0 },
            "calls_tool": { "type": "boolean" }
          }
        }
      ]
    },
    "tool_call": {
      "allOf": [
        { "$ref": "#/$defs/envelope" },
        {
          "type": "object",
          "required": ["type", "tool_use_id", "tool", "args"],
          "properties": {
            "type": { "const": "tool_call" },
            "tool_use_id": { "type": "string", "minLength": 1 },
            "tool": { "type": "string", "minLength": 1 },
            "args": { "type": "object" }
          }
        }
      ]
    },
    "gate_decision": {
      "allOf": [
        { "$ref": "#/$defs/envelope" },
        {
          "type": "object",
          "required": ["type", "tool_use_id", "tier", "decision", "rule"],
          "properties": {
            "type": { "const": "gate_decision" },
            "tool_use_id": { "type": "string", "minLength": 1 },
            "agent_id": { "type": "string", "minLength": 1 },
            "agent_name": { "type": ["string", "null"] },
            "tier": { "$ref": "#/$defs/tier" },
            "decision": { "enum": ["allow", "deny", "prompt"] },
            "rule": { "type": ["string", "null"] },
            "explanation": { "type": ["string", "null"] },
            "confidence": { "type": ["number", "null"], "minimum": 0, "maximum": 1 },
            "guardian_model": { "type": ["string", "null"] },
            "screen_risk": { "type": ["number", "null"], "minimum": 0, "maximum": 1 },
            "screen_allow_risk": { "type": ["number", "null"], "minimum": 0, "maximum": 1 },
            "screen_steering": { "type": ["number", "null"], "minimum": 0, "maximum": 1 },
            "screen_scores": {
              "type": ["object", "null"],
              "additionalProperties": { "type": "number", "minimum": 0, "maximum": 1 }
            },
            "screen_outcome": { "enum": ["stop", "allow", "deny", "pass", "unavailable", null] },
            "fail_closed_reason": {
              "enum": [
                "error",
                "timeout",
                "unparseable",
                "low_confidence",
                "unavailable",
                "not_signed_in",
                "usage_pause_ended",
                null
              ]
            },
            "afk": { "type": "boolean" },
            "converted": {
              "enum": [
                "ask_rule",
                "floor",
                "mode",
                "escalate",
                "screen_escalate",
                "breaker_paused",
                "guardian_unavailable",
                "hook",
                null
              ]
            }
          }
        }
      ]
    },
    "tool_result": {
      "allOf": [
        { "$ref": "#/$defs/envelope" },
        {
          "type": "object",
          "required": ["type", "tool_use_id", "success", "text"],
          "properties": {
            "type": { "const": "tool_result" },
            "tool_use_id": { "type": "string", "minLength": 1 },
            "tool": { "type": "string" },
            "success": { "type": "boolean" },
            "text": { "type": "string" }
          }
        }
      ]
    },
    "goal": {
      "allOf": [
        { "$ref": "#/$defs/envelope" },
        {
          "type": "object",
          "required": ["type", "verdict", "reason", "evaluations"],
          "properties": {
            "type": { "const": "goal" },
            "verdict": { "enum": ["met", "not_met", "impossible", "failed"] },
            "reason": { "type": "string" },
            "evaluations": { "type": "integer", "minimum": 0 }
          }
        }
      ]
    },
    "usage_paused": {
      "allOf": [
        { "$ref": "#/$defs/envelope" },
        {
          "type": "object",
          "required": ["type", "account", "role"],
          "properties": {
            "type": { "const": "usage_paused" },
            "account": { "type": "string", "minLength": 1 },
            "role": { "enum": ["main", "guardian"] },
            "reset_at": {
              "type": ["integer", "null"],
              "description": "Unix epoch seconds when the provider said the window resets; null when it did not say."
            }
          }
        }
      ]
    },
    "afk_report": {
      "allOf": [
        { "$ref": "#/$defs/envelope" },
        {
          "type": "object",
          "required": ["type", "afk"],
          "properties": {
            "type": { "const": "afk_report" },
            "afk": { "$ref": "#/$defs/afk" }
          }
        }
      ]
    },
    "usage_resumed": {
      "allOf": [
        { "$ref": "#/$defs/envelope" },
        {
          "type": "object",
          "required": ["type", "account", "role", "end", "waited_secs"],
          "properties": {
            "type": { "const": "usage_resumed" },
            "account": { "type": "string", "minLength": 1 },
            "role": { "enum": ["main", "guardian"] },
            "end": { "enum": ["reset", "probed", "resumed", "cancelled", "cap_reached"] },
            "waited_secs": { "type": "integer", "minimum": 0 }
          }
        }
      ]
    },
    "result": {
      "allOf": [
        { "$ref": "#/$defs/envelope" },
        {
          "type": "object",
          "required": [
            "type",
            "subtype",
            "is_error",
            "result",
            "num_turns",
            "duration_ms",
            "total_cost_usd",
            "usage",
            "denials"
          ],
          "properties": {
            "type": { "const": "result" },
            "subtype": {
              "enum": [
                "success",
                "error_max_turns",
                "error_max_budget_usd",
                "error_during_execution",
                "error_usage_limit"
              ]
            },
            "usage_limit": {
              "type": "object",
              "description": "Present with error_usage_limit: the account whose usage window stayed spent.",
              "required": ["account", "role", "end"],
              "properties": {
                "account": { "type": "string", "minLength": 1 },
                "role": { "enum": ["main", "guardian"] },
                "reset_at": { "type": ["integer", "null"] },
                "end": { "enum": ["cancelled", "cap_reached"] }
              }
            },
            "is_error": { "type": "boolean" },
            "result": { "type": "string" },
            "error": { "type": ["string", "null"] },
            "structured_output": {},
            "num_turns": { "type": "integer", "minimum": 0 },
            "duration_ms": { "type": "integer", "minimum": 0 },
            "total_cost_usd": { "type": ["number", "null"], "minimum": 0 },
            "usage": {
              "type": "object",
              "required": ["main", "guardian", "subagents"],
              "properties": {
                "main": { "$ref": "#/$defs/usage_bucket" },
                "guardian": { "$ref": "#/$defs/usage_bucket" },
                "subagents": { "$ref": "#/$defs/usage_bucket" },
                "goal": { "$ref": "#/$defs/usage_bucket" }
              }
            },
            "account_usage": {
              "type": "array",
              "items": {
                "type": "object",
                "required": ["role", "account", "label"],
                "properties": {
                  "role": { "enum": ["main", "guardian"] },
                  "account": { "type": "string" },
                  "label": { "type": "string" },
                  "windows": {
                    "type": "array",
                    "items": {
                      "type": "object",
                      "required": ["label", "used_percentage"],
                      "properties": {
                        "label": { "type": "string" },
                        "used_percentage": { "type": "integer", "minimum": 0, "maximum": 100 },
                        "resets_at": { "type": "integer", "minimum": 0 }
                      }
                    }
                  }
                }
              }
            },
            "goal": {
              "type": "object",
              "required": ["condition", "status", "evaluations", "reason"],
              "properties": {
                "condition": { "type": "string" },
                "status": { "enum": ["active", "met", "impossible", "paused", "cleared"] },
                "evaluations": { "type": "integer", "minimum": 0 },
                "reason": { "type": ["string", "null"] }
              }
            },
            "afk": { "$ref": "#/$defs/afk" },
            "denials": {
              "type": "array",
              "items": {
                "type": "object",
                "required": ["tool", "tier", "rule", "explanation"],
                "properties": {
                  "tool": { "type": "string" },
                  "tier": { "$ref": "#/$defs/tier" },
                  "rule": { "type": ["string", "null"] },
                  "explanation": { "type": ["string", "null"] }
                }
              }
            }
          }
        }
      ]
    },
    "afk": {
      "type": "object",
      "description": "Present when the run used --afk or the session is stopped: the report of the latest AFK stretch.",
      "required": ["outcome", "counts", "temperature", "review", "timeline"],
      "properties": {
        "outcome": { "enum": ["active", "ended", "blocked", "stopped"] },
        "counts": {
          "type": "object",
          "required": [
            "allowed",
            "denied",
            "converted",
            "succeeded",
            "failed",
            "cancelled",
            "unknown"
          ],
          "properties": {
            "allowed": { "type": "integer", "minimum": 0 },
            "denied": { "type": "integer", "minimum": 0 },
            "converted": { "type": "integer", "minimum": 0 },
            "succeeded": { "type": "integer", "minimum": 0 },
            "failed": { "type": "integer", "minimum": 0 },
            "cancelled": { "type": "integer", "minimum": 0 },
            "unknown": { "type": "integer", "minimum": 0 }
          }
        },
        "temperature": {
          "type": "object",
          "required": ["current", "peak", "ceiling", "contributing"],
          "properties": {
            "current": { "type": "number", "minimum": 0 },
            "peak": { "type": "number", "minimum": 0 },
            "ceiling": { "type": "number", "exclusiveMinimum": 0 },
            "contributing": {
              "type": "array",
              "items": {
                "type": "object",
                "required": ["seq", "weight", "remaining"],
                "properties": {
                  "seq": { "type": "integer", "minimum": 0 },
                  "weight": { "type": "number", "minimum": 0 },
                  "remaining": { "type": "number", "minimum": 0 }
                }
              }
            }
          }
        },
        "review": {
          "type": "object",
          "required": ["links", "assessment"],
          "properties": {
            "links": {
              "type": "array",
              "items": {
                "type": "object",
                "required": ["denial_seq", "attempt_seq", "kind", "reason"],
                "properties": {
                  "denial_seq": { "type": "integer", "minimum": 0 },
                  "attempt_seq": { "type": "integer", "minimum": 0 },
                  "kind": { "enum": ["exact_retry", "same_target_other_tool", "suspected"] },
                  "reason": { "type": "string" }
                }
              }
            },
            "assessment": {
              "type": "object",
              "required": ["status"],
              "properties": {
                "status": {
                  "enum": [
                    "no_denials",
                    "pending",
                    "no_suspected_evasion",
                    "found",
                    "unavailable"
                  ]
                },
                "reason": { "type": "string" }
              }
            }
          }
        },
        "timeline": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["seq", "agent", "agent_name", "tool", "summary", "decision"],
            "properties": {
              "seq": { "type": "integer", "minimum": 0 },
              "ts_ms": { "type": "integer", "minimum": 0 },
              "agent": { "type": "string" },
              "agent_name": { "type": "string" },
              "tool": { "type": "string" },
              "summary": { "type": "string" },
              "decision": { "enum": ["allowed", "denied", "converted"] },
              "tier": { "oneOf": [{ "$ref": "#/$defs/tier" }, { "type": "null" }] },
              "rule": { "type": ["string", "null"] },
              "explanation": { "type": ["string", "null"] },
              "outcome": { "enum": ["ok", "error", "cancelled", null] }
            }
          }
        }
      }
    },
    "user_input": {
      "type": "object",
      "description": "One line of --input-format stream-json: Claude Code's user message shape. It carries no schema_version or session_id.",
      "required": ["type", "message"],
      "properties": {
        "type": { "const": "user" },
        "message": {
          "type": "object",
          "required": ["role", "content"],
          "properties": {
            "role": { "const": "user" },
            "content": {
              "oneOf": [
                { "type": "string" },
                {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "required": ["type"],
                    "properties": {
                      "type": { "type": "string" },
                      "text": { "type": "string" }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}
