{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.anguskit.com/schema/anguscopilot/1.0.0/reasoning.json",
  "title": "Reasoning Configuration",
  "description": "推理模式与执行预算配置。v2.0 支持 ask / toolCall / react / plan / reflect 五种模式，去掉自动切换。",
  "type": "object",
  "required": ["mode"],
  "properties": {
    "mode": {
      "description": "推理模式。每种模式语义见技术方案 §6。 [stable]",
      "type": "string",
      "enum": ["ask", "toolCall", "react", "plan", "reflect"]
    },
    "allowedModes": {
      "description": "允许的推理模式白名单（ADR-004 / SEM-008）。声明后 mode 必须 ∈ allowedModes；空数组或省略表示全部允许。 [stable]",
      "type": "array",
      "items": {
        "type": "string",
        "enum": ["ask", "toolCall", "react", "plan", "reflect"]
      },
      "uniqueItems": true
    },
    "maxSteps": {
      "description": "ReAct/Plan/Reflect 模式下的最大步数。 [stable]",
      "type": "integer",
      "minimum": 1,
      "maximum": 200,
      "default": 20
    },
    "maxTokens": {
      "description": "整个推理过程的 Token 预算上限（用于探索性预算控制）。 [stable]",
      "type": "integer",
      "minimum": 1
    },
    "maxExternalCalls": {
      "description": "整个推理过程对外部 API 的最大调用次数。 [stable]",
      "type": "integer",
      "minimum": 1
    },
    "thinkingBudget": {
      "description": "思考预算等级，影响 Prompt 中 chain-of-thought 的展开程度。 [reserved] —— schema 已定义但引擎尚未消费（v2.0 未接入 Reasoner 构造链路）；保留字段以便后续迭代，当前配置不生效。",
      "type": "string",
      "enum": ["low", "medium", "high"]
    },

    "loopDetection": {
      "description": "ReAct 循环检测策略（v2.0 三重检测）。 [beta]",
      "type": "object",
      "properties": {
        "layers": {
          "description": "启用的检测层（W10 新增）。任一命中即终止；空数组等价于关闭循环检测。 [beta]",
          "type": "array",
          "items": {
            "type": "string",
            "enum": ["structural", "semantic", "info-gain"]
          },
          "uniqueItems": true,
          "default": ["structural", "semantic", "info-gain"]
        },
        "structuralRepeatThreshold": {
          "description": "Action 结构指纹重复阈值（仅 layers 含 structural 时生效）。 [beta]",
          "type": "integer",
          "minimum": 2,
          "default": 3
        },
        "semanticSimilarityThreshold": {
          "description": "语义指纹相似度阈值（Embedding 余弦），超过即视为重复（仅 layers 含 semantic 时生效）。 [beta]",
          "type": "number",
          "minimum": 0,
          "maximum": 1,
          "default": 0.92
        },
        "embeddingRef": {
          "description": "语义指纹层使用的 Embedding 引用。layers 含 semantic 时必填（W10 新增 A2 约束）。 [beta]",
          "type": "string",
          "minLength": 1
        },
        "noProgressSteps": {
          "description": "累计无信息增益步数上限。layers 含 info-gain 时必须 ≥ 1（W10 新增 A3 约束）。 [beta]",
          "type": "integer",
          "minimum": 1,
          "default": 5
        }
      },
      "patternProperties": { "^x-": true },
      "$comment": "A2/A3 跨字段约束：layers 含 semantic 强制 embeddingRef；layers 含 info-gain 时 noProgressSteps ≥ 1（minimum 已保证）。",
      "allOf": [
        {
          "if": {
            "properties": {
              "layers": {
                "type": "array",
                "contains": { "const": "semantic" }
              }
            },
            "required": ["layers"]
          },
          "then": {
            "required": ["embeddingRef"]
          }
        }
      ],
      "unevaluatedProperties": false
    },

    "plan": {
      "description": "Plan 模式专用配置。 [beta]",
      "type": "object",
      "properties": {
        "humanReview": {
          "description": "Plan 生成后是否强制人工审核。 [beta]",
          "type": "boolean",
          "default": false
        },
        "subTaskMode": {
          "description": "子任务可独立声明推理子模式。 [beta]",
          "type": "string",
          "enum": ["ask", "toolCall", "react", "reflect"]
        },
        "subContext": {
          "description": "Plan 子模式上下文传递层级（与 §6.5.3 / §7.4.3 同源）。 [beta]",
          "type": "object",
          "properties": {
            "mode": {
              "description": "上下文传递模式。 [beta]",
              "type": "string",
              "enum": ["inherit", "scoped", "isolated"],
              "default": "scoped"
            },
            "whitelist": {
              "description": "scoped 模式下允许传递给子任务的字段名白名单（W10 新增 A4 约束：scoped 时必须非空）。 [beta]",
              "type": "array",
              "items": { "type": "string", "minLength": 1 },
              "uniqueItems": true
            }
          },
          "patternProperties": { "^x-": true },
          "$comment": "A4 跨字段约束：subContext.mode=scoped 时 whitelist 必须非空。",
          "if": {
            "properties": { "mode": { "const": "scoped" } },
            "required": ["mode"]
          },
          "then": {
            "required": ["whitelist"],
            "properties": {
              "whitelist": { "minItems": 1 }
            }
          },
          "unevaluatedProperties": false
        },
        "replan": {
          "description": "Replan 触发条件与上限。 [beta]",
          "type": "object",
          "properties": {
            "maxAttempts": {
              "description": "全局 replan 最大尝试次数（与 §6.5.4 对齐）。 [beta]",
              "type": "integer",
              "minimum": 0,
              "maximum": 10,
              "default": 2
            },
            "triggers": {
              "description": "允许触发 replan 的事件类型。 [beta]",
              "type": "array",
              "items": {
                "type": "string",
                "enum": ["loop-detected", "budget-exceeded", "fact-conflict", "user-clarification"]
              },
              "uniqueItems": true
            }
          },
          "patternProperties": { "^x-": true },
          "unevaluatedProperties": false
        }
      },
      "patternProperties": { "^x-": true },
      "unevaluatedProperties": false
    },

    "reflect": {
      "description": "Reflect 模式专用配置（Generator + Critic）。 [beta]",
      "type": "object",
      "properties": {
        "maxIterations": {
          "type": "integer",
          "minimum": 1,
          "default": 3
        },
        "criticPrompt": { "type": "string" },
        "stopWhen": {
          "description": "停止条件 SpEL 表达式（如 'score > 0.9'）。 [beta]",
          "type": "string"
        }
      },
      "patternProperties": { "^x-": true },
      "unevaluatedProperties": false
    },

    "observability": {
      "description": "推理可观测配置（W10 新增）。 [beta]",
      "type": "object",
      "properties": {
        "recording": {
          "description": "录制策略（与 §14.3.1 对齐）。 [beta]",
          "type": "object",
          "properties": {
            "enabled": { "type": "boolean", "default": false },
            "sampleRate": {
              "description": "采样率（0..1）；错误强制 100%。 [beta]",
              "type": "number",
              "minimum": 0,
              "maximum": 1,
              "default": 0.01
            },
            "retentionDays": {
              "type": "integer",
              "minimum": 1,
              "maximum": 3650,
              "default": 30
            },
            "backend": {
              "type": "string",
              "enum": ["postgres+oss", "postgres", "elastic"],
              "default": "postgres+oss"
            }
          },
          "patternProperties": { "^x-": true },
          "unevaluatedProperties": false
        },
        "detailedLlmIo": {
          "description": "是否在 Span 中记录 LLM Prompt/Completion 详细内容。开启时必须声明 piiRedactor（W10 新增 A5 约束）。 [beta]",
          "type": "boolean",
          "default": false
        },
        "piiRedactor": {
          "description": "PII 脱敏器引用（detailedLlmIo=true 时必填）。 [beta]",
          "type": "string",
          "minLength": 1
        }
      },
      "patternProperties": { "^x-": true },
      "$comment": "A5 跨字段约束：detailedLlmIo=true 时必须 piiRedactor。",
      "if": {
        "properties": { "detailedLlmIo": { "const": true } },
        "required": ["detailedLlmIo"]
      },
      "then": {
        "required": ["piiRedactor"]
      },
      "unevaluatedProperties": false
    }
  },
  "patternProperties": {
    "^x-": true
  },
  "allOf": [
    {
      "$comment": "A1：Ask 模式禁止配置 plan / reflect 段。",
      "if": { "properties": { "mode": { "const": "ask" } }, "required": ["mode"] },
      "then": {
        "not": {
          "anyOf": [
            { "required": ["plan"] },
            { "required": ["reflect"] }
          ]
        }
      }
    },
    {
      "$comment": "A1：ToolCall 模式禁止 plan / reflect。",
      "if": { "properties": { "mode": { "const": "toolCall" } }, "required": ["mode"] },
      "then": {
        "not": {
          "anyOf": [
            { "required": ["plan"] },
            { "required": ["reflect"] }
          ]
        }
      }
    },
    {
      "$comment": "Plan 模式可声明 plan 段；同时禁止 reflect 段（如需 reflect 子任务请用 subTaskMode）。",
      "if": { "properties": { "mode": { "const": "plan" } }, "required": ["mode"] },
      "then": {
        "not": { "required": ["reflect"] }
      }
    },
    {
      "if": { "properties": { "mode": { "const": "reflect" } }, "required": ["mode"] },
      "then": {
        "not": { "required": ["plan"] }
      }
    }
  ],
  "unevaluatedProperties": false
}
