工具循环检测

OpenClaw 可以防止 Agent 陷入重复的工具调用模式。 这个防护机制默认关闭

只在需要时启用,因为设置过严可能会误拦合理的重复调用。

为什么需要它

  • 检测没有进展的重复序列。
  • 检测高频无结果循环(相同工具、相同输入、反复报错)。
  • 检测已知轮询工具的特定重复调用模式。

配置块

全局默认值:

{
  tools: {
    loopDetection: {
      enabled: false,
      historySize: 30,
      warningThreshold: 10,
      criticalThreshold: 20,
      globalCircuitBreakerThreshold: 30,
      detectors: {
        genericRepeat: true,
        knownPollNoProgress: true,
        pingPong: true,
      },
    },
  },
}

按 Agent 覆盖(可选):

{
  agents: {
    list: [
      {
        id: "safe-runner",
        tools: {
          loopDetection: {
            enabled: true,
            warningThreshold: 8,
            criticalThreshold: 16,
          },
        },
      },
    ],
  },
}

字段说明

  • enabled:总开关。false 表示不执行循环检测。
  • historySize:保留用于分析的最近工具调用数量。
  • warningThreshold:将模式归类为警告的阈值。
  • criticalThreshold:阻止重复循环模式的阈值。
  • globalCircuitBreakerThreshold:全局无进展熔断阈值。
  • detectors.genericRepeat:检测相同工具 + 相同参数的重复模式。
  • detectors.knownPollNoProgress:检测已知轮询类模式且状态无变化。
  • detectors.pingPong:检测交替式 ping-pong 模式。

推荐配置

  • 先用 enabled: true,阈值保持默认。
  • 保持阈值顺序:warningThreshold < criticalThreshold < globalCircuitBreakerThreshold
  • 如果出现误报:
    • 调高 warningThreshold 和/或 criticalThreshold
    • (可选)调高 globalCircuitBreakerThreshold
    • 仅禁用造成问题的检测器
    • 减小 historySize 来降低历史上下文的严格程度

日志和预期行为

检测到循环时,OpenClaw 会报告循环事件,并根据严重程度阻止或抑制下一次工具调用。 这既能保护用户免受失控的 token 消耗和锁死,又不影响正常的工具使用。

  • 优先使用警告和临时抑制。
  • 只在证据反复累积时才升级。

说明

  • tools.loopDetection 与 Agent 级别的覆盖合并。
  • Agent 级配置完全覆盖或扩展全局值。
  • 如果没有配置,防护机制保持关闭。