{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://replygirl.github.io/kuru/configuration.v1.schema.json",
  "title": "Kuru configuration v1",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "mode": {
      "type": "string",
      "enum": ["ifs", "polyvagal", "freudian", "jungian"],
      "default": "ifs"
    },
    "provider": { "type": "string", "enum": ["codex", "responses", "demo"], "default": "codex" },
    "model": { "type": "string", "minLength": 1, "maxLength": 256, "default": "auto" },
    "effort": { "type": ["string", "null"], "minLength": 1, "maxLength": 128, "default": null },
    "assumed_context_window_tokens": {
      "type": ["integer", "null"],
      "minimum": 1,
      "maximum": 2000000,
      "default": null
    },
    "context_output_reserve_tokens": {
      "type": ["integer", "null"],
      "minimum": 1,
      "maximum": 2000000,
      "default": null
    },
    "context_compaction_threshold_percent": {
      "type": "integer",
      "minimum": 50,
      "maximum": 95,
      "default": 75
    },
    "context_compaction_output_reserve_tokens": {
      "type": "integer",
      "minimum": 1,
      "maximum": 2000000,
      "default": 1024
    },
    "max_rounds": { "type": "integer", "minimum": 1, "maximum": 64, "default": 3 },
    "max_tool_calls": { "type": "integer", "minimum": 1, "maximum": 1024, "default": 12 },
    "max_parallel": { "type": "integer", "minimum": 1, "maximum": 64, "default": 4 },
    "dream_every": { "type": "integer", "minimum": 0, "default": 8 },
    "dream_on_exit": { "type": "boolean", "default": true },
    "max_parts": { "type": "integer", "minimum": 1, "maximum": 128, "default": 16 },
    "allow_shell": { "type": "boolean", "default": false },
    "allow_write": { "type": "boolean", "default": false },
    "permissions": {
      "type": "array",
      "maxItems": 128,
      "items": { "$ref": "#/$defs/permission" },
      "default": []
    },
    "hooks": {
      "$ref": "#/$defs/hooks",
      "default": {
        "max_invocations": 256,
        "max_total_ms": 120000,
        "max_annotation_bytes": 262144,
        "pre_turn": [],
        "post_turn": [],
        "pre_tool": [],
        "post_tool": [],
        "speaker_selected": []
      }
    },
    "api_base": { "type": "string", "format": "uri", "default": "https://api.openai.com/v1" },
    "api_key_env": {
      "type": "string",
      "pattern": "^[A-Za-z_][A-Za-z0-9_]{0,255}$",
      "default": "OPENAI_API_KEY"
    },
    "mcp": {
      "type": "object",
      "maxProperties": 64,
      "propertyNames": { "pattern": "^[A-Za-z0-9_-]{1,64}$" },
      "additionalProperties": { "$ref": "#/$defs/mcp" },
      "default": {}
    },
    "external_agents": {
      "type": "object",
      "maxProperties": 64,
      "propertyNames": { "pattern": "^[A-Za-z0-9_-]{1,64}$" },
      "additionalProperties": { "type": "string", "format": "uri" },
      "default": {}
    },
    "update": { "$ref": "#/$defs/update" },
    "memory": {
      "$ref": "#/$defs/memory",
      "default": { "offline": false, "startup_timeout_secs": 30, "service_idle_timeout_secs": 30 }
    },
    "ui": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "theme": { "type": "string", "enum": ["dark", "light"], "default": "dark" },
        "palette": {
          "type": "object",
          "maxProperties": 13,
          "propertyNames": {
            "enum": [
              "background",
              "surface",
              "raised",
              "border",
              "text",
              "muted",
              "accent",
              "secondary",
              "warning",
              "info",
              "error",
              "scene_ghost",
              "scene_trace"
            ]
          },
          "additionalProperties": { "type": "string", "pattern": "^#[0-9A-Fa-f]{6}$" },
          "default": {}
        }
      },
      "default": { "theme": "dark", "palette": {} }
    }
  },
  "$defs": {
    "update": {
      "type": "object",
      "additionalProperties": false,
      "properties": { "notice": { "type": "boolean", "default": false } }
    },
    "configValues": { "$ref": "#" },
    "managed": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "defaults": { "$ref": "#/$defs/configValues" },
        "constraints": { "$ref": "#/$defs/configValues" }
      }
    },
    "permission": {
      "type": "object",
      "additionalProperties": false,
      "required": ["action", "selector"],
      "properties": {
        "action": { "type": "string", "enum": ["allow", "ask", "deny"] },
        "selector": { "$ref": "#/$defs/permissionSelector" },
        "path": {
          "type": "string",
          "minLength": 1,
          "maxLength": 512,
          "pattern": "^(?:\\.|(?![A-Za-z]:)(?!.*(?:^|/)\\.\\.?($|/))(?:\\*\\*|(?:[^/\\\\\\[\\]{}*?\\x00-\\x1F\\x7F-\\x9F]|\\?|\\*(?!\\*))+)(?:/(?:\\*\\*|(?:[^/\\\\\\[\\]{}*?\\x00-\\x1F\\x7F-\\x9F]|\\?|\\*(?!\\*))+))*)$"
        }
      },
      "allOf": [
        {
          "if": { "required": ["path"] },
          "then": {
            "properties": {
              "selector": {
                "properties": {
                  "kind": { "const": "native" },
                  "name": {
                    "enum": ["file_read", "file_list", "grep", "glob", "file_write", "file_delete"]
                  }
                }
              }
            }
          }
        }
      ]
    },
    "hooks": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "max_invocations": { "type": "integer", "minimum": 1, "maximum": 1024, "default": 256 },
        "max_total_ms": { "type": "integer", "minimum": 1, "maximum": 600000, "default": 120000 },
        "max_annotation_bytes": {
          "type": "integer",
          "minimum": 1,
          "maximum": 1048576,
          "default": 262144
        },
        "pre_turn": { "$ref": "#/$defs/hookList" },
        "post_turn": { "$ref": "#/$defs/hookList" },
        "pre_tool": { "$ref": "#/$defs/hookList" },
        "post_tool": { "$ref": "#/$defs/hookList" },
        "speaker_selected": { "$ref": "#/$defs/hookList" }
      }
    },
    "hookList": {
      "type": "array",
      "maxItems": 16,
      "items": { "$ref": "#/$defs/hookCommand" },
      "default": []
    },
    "hookCommand": {
      "type": "object",
      "additionalProperties": false,
      "required": ["command"],
      "properties": {
        "command": { "type": "string", "minLength": 1, "maxLength": 4096 },
        "args": {
          "type": "array",
          "maxItems": 64,
          "items": { "type": "string", "maxLength": 4096 },
          "default": []
        },
        "timeout_ms": {
          "type": "integer",
          "minimum": 1,
          "maximum": 120000,
          "default": 5000
        },
        "max_output_bytes": {
          "type": "integer",
          "minimum": 1,
          "maximum": 262144,
          "default": 65536
        }
      }
    },
    "permissionSelector": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["kind", "name"],
          "properties": {
            "kind": { "const": "native" },
            "name": {
              "enum": [
                "file_read",
                "file_list",
                "grep",
                "glob",
                "file_write",
                "file_delete",
                "shell",
                "web_fetch"
              ]
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["kind", "alias", "tool"],
          "properties": {
            "kind": { "const": "mcp" },
            "alias": { "type": "string", "pattern": "^[A-Za-z0-9_-]{1,64}$" },
            "tool": {
              "type": "string",
              "minLength": 1,
              "maxLength": 256,
              "pattern": "^[^\\x00-\\x1F\\x7F-\\x9F]+$"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["kind", "alias"],
          "properties": {
            "kind": { "const": "a2a" },
            "alias": { "type": "string", "pattern": "^[A-Za-z0-9_-]{1,64}$" }
          }
        }
      ]
    },
    "memory": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "dolt_binary": { "type": ["string", "null"], "default": null },
        "cache_dir": { "type": ["string", "null"], "default": null },
        "offline": { "type": "boolean", "default": false },
        "startup_timeout_secs": { "type": "integer", "minimum": 1, "maximum": 300, "default": 30 },
        "service_idle_timeout_secs": {
          "type": "integer",
          "minimum": 0,
          "maximum": 300,
          "default": 30
        }
      }
    },
    "mcp": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "enabled": { "type": "boolean", "default": true },
        "command": {
          "type": ["string", "null"],
          "minLength": 1,
          "maxLength": 4096,
          "default": null
        },
        "args": { "type": "array", "items": { "type": "string" }, "maxItems": 256, "default": [] },
        "url": { "type": ["string", "null"], "format": "uri", "default": null },
        "env": {
          "type": "object",
          "propertyNames": { "pattern": "^[A-Za-z_][A-Za-z0-9_]{0,255}$" },
          "additionalProperties": { "type": "string" },
          "default": {}
        },
        "allow_tools": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1,
            "maxLength": 256,
            "pattern": "^[^\\[\\]\\{\\}\\u0000-\\u001F\\u007F-\\u009F]+$"
          },
          "maxItems": 128,
          "default": []
        },
        "deny_tools": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1,
            "maxLength": 256,
            "pattern": "^[^\\[\\]\\{\\}\\u0000-\\u001F\\u007F-\\u009F]+$"
          },
          "maxItems": 128,
          "default": []
        },
        "header_env": {
          "type": "object",
          "maxProperties": 32,
          "propertyNames": {
            "allOf": [
              { "pattern": "^[!#$%&'*+.^_`|~0-9A-Za-z-]{1,128}$" },
              {
                "not": {
                  "pattern": "(?i)^(accept|connection|content-length|content-type|host|mcp-protocol-version|mcp-session-id|te|trailer|transfer-encoding|upgrade)$"
                }
              }
            ]
          },
          "additionalProperties": {
            "type": "string",
            "pattern": "^[A-Za-z_][A-Za-z0-9_]{0,255}$"
          },
          "default": {}
        },
        "oauth": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "enabled": { "type": "boolean", "default": false },
            "client_id": {
              "type": ["string", "null"],
              "minLength": 1,
              "maxLength": 2048,
              "pattern": "^[^\\u0000-\\u001F\\u007F-\\u009F]+$",
              "default": null
            },
            "client_secret_env": {
              "type": ["string", "null"],
              "pattern": "^[A-Za-z_][A-Za-z0-9_]{0,255}$",
              "default": null
            },
            "client_metadata_url": {
              "type": ["string", "null"],
              "format": "uri",
              "pattern": "(?i)^https://",
              "default": null
            },
            "scopes": {
              "type": "array",
              "items": {
                "type": "string",
                "minLength": 1,
                "maxLength": 256,
                "pattern": "^[\\u0021\\u0023-\\u005B\\u005D-\\u007E]+$"
              },
              "maxItems": 64,
              "uniqueItems": true,
              "default": []
            }
          },
          "allOf": [
            {
              "not": {
                "required": ["client_id", "client_metadata_url"]
              }
            },
            {
              "if": { "required": ["client_secret_env"] },
              "then": { "required": ["client_id"] }
            }
          ]
        }
      },
      "oneOf": [
        {
          "required": ["command"],
          "not": { "required": ["url"] },
          "properties": { "header_env": { "maxProperties": 0 } },
          "allOf": [{ "not": { "required": ["oauth"] } }]
        },
        {
          "required": ["url"],
          "not": { "required": ["command"] },
          "properties": {
            "args": { "maxItems": 0 },
            "env": { "maxProperties": 0 }
          },
          "allOf": [
            {
              "if": {
                "required": ["oauth"],
                "properties": {
                  "oauth": {
                    "required": ["enabled"],
                    "properties": { "enabled": { "const": true } }
                  }
                }
              },
              "then": {
                "properties": {
                  "url": { "pattern": "(?i)^https://" },
                  "header_env": {
                    "propertyNames": {
                      "not": { "pattern": "(?i)^(authorization|proxy-authorization)$" }
                    }
                  }
                }
              }
            }
          ]
        }
      ]
    }
  }
}
