{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "additionalProperties": false,
  "definitions": {
    "CommandMatch": {
      "additionalProperties": false,
      "description": "命令匹配条件：command 与 commandAny 二选一必填；subcommand 与 subcommandAny 互斥。",
      "properties": {
        "argsAll": {
          "items": {
            "type": "string"
          },
          "type": "array"
        },
        "argsAny": {
          "items": {
            "type": "string"
          },
          "type": "array"
        },
        "argsContainAll": {
          "items": {
            "type": "string"
          },
          "type": "array"
        },
        "argsContainAny": {
          "items": {
            "type": "string"
          },
          "type": "array"
        },
        "argsNone": {
          "items": {
            "type": "string"
          },
          "type": "array"
        },
        "command": {
          "type": "string"
        },
        "commandAny": {
          "items": {
            "type": "string"
          },
          "type": "array"
        },
        "dynamic": {
          "type": "boolean"
        },
        "flags": {
          "$ref": "#/definitions/FlagMatcher"
        },
        "operandCount": {
          "$ref": "#/definitions/OperandCount"
        },
        "optionsBeforeSubcommand": {
          "$ref": "#/definitions/OptionsBeforeSubcommand"
        },
        "subcommand": {
          "type": "string"
        },
        "subcommandAny": {
          "items": {
            "type": "string"
          },
          "type": "array"
        },
        "visibleTextAll": {
          "items": {
            "type": "string"
          },
          "type": "array"
        },
        "visibleTextAny": {
          "items": {
            "type": "string"
          },
          "type": "array"
        },
        "visibleTextNone": {
          "items": {
            "type": "string"
          },
          "type": "array"
        }
      },
      "type": "object"
    },
    "CommandRule": {
      "additionalProperties": false,
      "description": "命令规则。",
      "properties": {
        "action": {
          "$ref": "#/definitions/PolicyAction"
        },
        "id": {
          "type": "string"
        },
        "label": {
          "type": "string"
        },
        "match": {
          "$ref": "#/definitions/CommandMatch"
        },
        "priority": {
          "description": "优先级：值越大越优先（缺省 0）。项目规则 0 / home -0.3 / 默认 -0.6。",
          "type": "number"
        },
        "reason": {
          "type": "string"
        },
        "type": {
          "const": "command",
          "type": "string"
        }
      },
      "required": [
        "id",
        "match",
        "reason",
        "type"
      ],
      "type": "object"
    },
    "DnaAction": {
      "description": "DNA 模式（Do Not Ask）下的自动回答动作（ask 无意义，仅 allow/block）。",
      "enum": [
        "allow",
        "block"
      ],
      "type": "string"
    },
    "DnaSettings": {
      "additionalProperties": false,
      "description": "DNA 模式（Do Not Ask）配置。",
      "properties": {
        "allowTools": {
          "description": "工具白名单（非空时启用白名单模式，仅允许名单中的工具）。",
          "items": {
            "type": "string"
          },
          "type": "array"
        },
        "blockTools": {
          "description": "工具黑名单（白名单为空时启用黑名单模式，禁止名单中的工具）。",
          "items": {
            "type": "string"
          },
          "type": "array"
        },
        "cancelledNote": {
          "description": "交互被拦截时追加给 AI 的结果说明（不配置时使用内置默认提示）。",
          "type": "string"
        },
        "extraPrompt": {
          "description": "附加提示词：DNA 模式下附加给 AI 的提示，让 AI 自动选择方案（如 select/confirm 交互）。不配置时使用内置默认提示。",
          "type": "string"
        },
        "maxNudges": {
          "description": "格式报告 nudge 重试预算（任务结束后未按 REPORT: SUCCESS|FAILURE 收尾时提醒次数，缺省 2，达到后放弃）。",
          "type": "number"
        },
        "maxViolations": {
          "description": "本次 DNA 模式下累计自动拒绝的总上限（所有规则含工具黑白名单，缺省 3，达到后强制中断会话并清零计数）。",
          "type": "number"
        },
        "parseFailure": {
          "$ref": "#/definitions/PolicyAction",
          "description": "Bash/PowerShell 解析失败时 DNA 模式的默认动作（缺省 block）。"
        },
        "readInside": {
          "$ref": "#/definitions/DnaAction",
          "description": "工作区内读的默认动作（缺省 allow）。"
        },
        "readOutside": {
          "$ref": "#/definitions/DnaAction",
          "description": "工作区外读的默认动作（缺省 allow）。"
        },
        "writeInside": {
          "$ref": "#/definitions/DnaAction",
          "description": "工作区内写/删/移动的默认动作（缺省 allow）。"
        },
        "writeOutside": {
          "$ref": "#/definitions/DnaAction",
          "description": "工作区外写/删/移动的默认动作（缺省 block）。"
        }
      },
      "type": "object"
    },
    "FlagMatcher": {
      "additionalProperties": false,
      "description": "语义化标志匹配（any/all/none 至少一个）。",
      "properties": {
        "all": {
          "items": {
            "type": "string"
          },
          "type": "array"
        },
        "any": {
          "items": {
            "type": "string"
          },
          "type": "array"
        },
        "none": {
          "items": {
            "type": "string"
          },
          "type": "array"
        }
      },
      "type": "object"
    },
    "LanguageSetting": {
      "description": "UI 语言设置：auto 跟随系统。",
      "enum": [
        "auto",
        "zh",
        "en"
      ],
      "type": "string"
    },
    "OperandCount": {
      "additionalProperties": false,
      "description": "操作数数量范围（min/max 至少一个）。",
      "properties": {
        "max": {
          "type": "number"
        },
        "min": {
          "type": "number"
        }
      },
      "type": "object"
    },
    "OptionsBeforeSubcommand": {
      "additionalProperties": false,
      "description": "子命令检测前的全局选项（如 git -C repo ...，boolean/value 至少一个）。",
      "properties": {
        "boolean": {
          "items": {
            "type": "string"
          },
          "type": "array"
        },
        "value": {
          "items": {
            "type": "string"
          },
          "type": "array"
        }
      },
      "type": "object"
    },
    "PathMatch": {
      "additionalProperties": false,
      "description": "路径规则 match（仅 path 字段）。",
      "properties": {
        "path": {
          "$ref": "#/definitions/PathPattern"
        }
      },
      "required": [
        "path"
      ],
      "type": "object"
    },
    "PathPattern": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "items": {
            "type": "string"
          },
          "type": "array"
        },
        {
          "$ref": "#/definitions/PathPatternObject"
        }
      ],
      "description": "路径模式：字符串/字符串数组（简写）或对象。"
    },
    "PathPatternObject": {
      "additionalProperties": false,
      "description": "路径模式对象：any 与 outsideWorkdir 二选一。",
      "properties": {
        "any": {
          "anyOf": [
            {
              "type": "string"
            },
            {
              "items": {
                "type": "string"
              },
              "type": "array"
            }
          ]
        },
        "except": {
          "items": {
            "type": "string"
          },
          "type": "array"
        },
        "outsideWorkdir": {
          "description": "true 时匹配工作区目录列表之外的所有路径（工作区 = cwd + extraDirs）。",
          "type": "boolean"
        }
      },
      "type": "object"
    },
    "PathRule": {
      "additionalProperties": false,
      "description": "路径规则。",
      "properties": {
        "action": {
          "$ref": "#/definitions/PolicyAction"
        },
        "id": {
          "type": "string"
        },
        "label": {
          "type": "string"
        },
        "match": {
          "$ref": "#/definitions/PathMatch"
        },
        "priority": {
          "description": "优先级：值越大越优先（缺省 0）。项目规则 0 / home -0.3 / 默认 -0.6。",
          "type": "number"
        },
        "reason": {
          "type": "string"
        },
        "type": {
          "$ref": "#/definitions/PathRuleType"
        }
      },
      "required": [
        "id",
        "match",
        "reason",
        "type"
      ],
      "type": "object"
    },
    "PathRuleType": {
      "description": "路径规则类型。",
      "enum": [
        "path:zeroAccess",
        "path:readOnly",
        "path:noDelete"
      ],
      "type": "string"
    },
    "PolicyAction": {
      "description": "动作：放行 / 询问 / 拦截。",
      "enum": [
        "allow",
        "ask",
        "block"
      ],
      "type": "string"
    },
    "PolicySettings": {
      "additionalProperties": false,
      "description": "策略设置。",
      "properties": {
        "dna": {
          "$ref": "#/definitions/DnaSettings",
          "description": "DNA 模式配置。"
        },
        "extraDirs": {
          "description": "额外工作区目录列表；与 cwd 共同构成「完整的工作区」，outsideWorkdir: true 的语义变为「在工作区目录列表之外」。",
          "items": {
            "type": "string"
          },
          "type": "array"
        },
        "language": {
          "$ref": "#/definitions/LanguageSetting",
          "description": "UI 提示语言（zh/en/auto），默认 auto 跟随系统。"
        },
        "parseFailure": {
          "$ref": "#/definitions/PolicyAction",
          "description": "Bash 解析失败时的动作。"
        },
        "showStatus": {
          "description": "是否在状态栏显示 🛡/⚠️ 图标。",
          "type": "boolean"
        }
      },
      "type": "object"
    },
    "Rule": {
      "anyOf": [
        {
          "$ref": "#/definitions/CommandRule"
        },
        {
          "$ref": "#/definitions/PathRule"
        }
      ],
      "description": "规则：命令规则或路径规则。"
    }
  },
  "description": "配置文件顶层结构。",
  "properties": {
    "rules": {
      "items": {
        "$ref": "#/definitions/Rule"
      },
      "type": "array"
    },
    "settings": {
      "$ref": "#/definitions/PolicySettings"
    },
    "version": {
      "description": "历史版本字段，已不区分、可省略。",
      "type": "number"
    }
  },
  "required": [
    "rules",
    "settings"
  ],
  "type": "object"
}