diff --git a/src/agents/pi-embedded-runner/run.compaction-loop-guard.test.ts b/src/agents/pi-embedded-runner/run.compaction-loop-guard.test.ts index 4bfc0c373d8..83b9780416a 100644 --- a/src/agents/pi-embedded-runner/run.compaction-loop-guard.test.ts +++ b/src/agents/pi-embedded-runner/run.compaction-loop-guard.test.ts @@ -285,6 +285,66 @@ describe("post-compaction loop guard wired into runEmbeddedPiAgent", () => { expect(mockedRunEmbeddedAttempt).toHaveBeenCalledTimes(2); }); + it("uses the active agent post-compaction guard window over the global default", async () => { + const overflowError = makeOverflowError(); + + mockedRunEmbeddedAttempt.mockImplementationOnce(async () => + makeAttemptResult({ promptError: overflowError }), + ); + mockedRunEmbeddedAttempt.mockImplementationOnce(async (attemptParams: unknown) => { + const onToolOutcome = (attemptParams as { onToolOutcome?: ToolOutcomeObserver }) + .onToolOutcome; + for (let i = 0; i < 3; i += 1) { + await executeWrappedToolOutcome( + "gateway", + { action: "lookup", path: "x" }, + "identical-result", + onToolOutcome, + ); + } + return makeAttemptResult({ + promptError: null, + toolMetas: [{ toolName: "gateway" }, { toolName: "gateway" }, { toolName: "gateway" }], + }); + }); + + mockedCompactDirect.mockResolvedValueOnce( + makeCompactionSuccess({ + summary: "Compacted session", + firstKeptEntryId: "entry-5", + tokensBefore: 150000, + }), + ); + + const result = await runEmbeddedPiAgent({ + ...baseParams, + agentId: "agent-a", + config: { + tools: { + loopDetection: { + postCompactionGuard: { enabled: true, windowSize: 2 }, + }, + }, + agents: { + list: [ + { + id: "agent-a", + tools: { + loopDetection: { + postCompactionGuard: { windowSize: 4 }, + }, + }, + }, + ], + }, + } as never, + }); + + expect(result.meta.error).toBeUndefined(); + expect(mockedCompactDirect).toHaveBeenCalledTimes(1); + expect(mockedRunEmbeddedAttempt).toHaveBeenCalledTimes(2); + }); + it("aborts post-compaction loop from the live tool path even when toolCallHistory is at its trim cap", async () => { // Long-running sessions accumulate up to historySize (default 30) records // in toolCallHistory. The live observer must still see the new outcome diff --git a/src/agents/pi-embedded-runner/run.ts b/src/agents/pi-embedded-runner/run.ts index 314b2dc9abf..f3e99919081 100644 --- a/src/agents/pi-embedded-runner/run.ts +++ b/src/agents/pi-embedded-runner/run.ts @@ -82,6 +82,7 @@ import { runAgentCleanupStep } from "../run-cleanup-timeout.js"; import { buildAgentRuntimeAuthPlan } from "../runtime-plan/auth.js"; import { buildAgentRuntimePlan } from "../runtime-plan/build.js"; import { ensureRuntimePluginsLoaded } from "../runtime-plugins.js"; +import { resolveToolLoopDetectionConfig } from "../tool-loop-detection-config.js"; import { derivePromptTokens, normalizeUsage, type UsageLike } from "../usage.js"; import { redactRunIdentifier, resolveRunWorkspaceDir } from "../workspace-run.js"; import { runPostCompactionSideEffects } from "./compaction-hooks.js"; @@ -790,8 +791,12 @@ export async function runEmbeddedPiAgent( // Post-compaction loop guard for #77474. Armed at each compaction-success // site below; observed from the live tool-outcome path so it can abort // while the post-compaction prompt is still running. + const resolvedLoopDetectionConfig = resolveToolLoopDetectionConfig({ + cfg: params.config, + agentId: sessionAgentId, + }); const postCompactionGuard = createPostCompactionLoopGuard( - params.config?.tools?.loopDetection?.postCompactionGuard, + resolvedLoopDetectionConfig?.postCompactionGuard, ); const observePostCompactionToolOutcome = ( observation: PostCompactionGuardObservation, diff --git a/src/agents/pi-tools.ts b/src/agents/pi-tools.ts index 80d78201aa6..89cd59827f8 100644 --- a/src/agents/pi-tools.ts +++ b/src/agents/pi-tools.ts @@ -2,7 +2,6 @@ import { createCodingTools, createReadTool } from "@mariozechner/pi-coding-agent import { HEARTBEAT_RESPONSE_TOOL_NAME } from "../auto-reply/heartbeat-tool-response.js"; import type { ModelCompatConfig } from "../config/types.models.js"; import type { OpenClawConfig } from "../config/types.openclaw.js"; -import type { ToolLoopDetectionConfig } from "../config/types.tools.js"; import type { DiagnosticTraceContext } from "../infra/diagnostic-trace-context.js"; import { resolveMergedSafeBinProfileFixtures } from "../infra/exec-safe-bin-runtime-policy.js"; import { logWarn } from "../logger.js"; @@ -65,6 +64,7 @@ import { PROCESS_TOOL_DISPLAY_SUMMARY, } from "./tool-description-presets.js"; import { createToolFsPolicy, resolveToolFsConfig } from "./tool-fs-policy.js"; +import { resolveToolLoopDetectionConfig } from "./tool-loop-detection-config.js"; import { applyToolPolicyPipeline, buildDefaultToolPolicyPipelineSteps, @@ -233,32 +233,7 @@ function resolveExecConfig(params: { cfg?: OpenClawConfig; agentId?: string }) { }; } -export function resolveToolLoopDetectionConfig(params: { - cfg?: OpenClawConfig; - agentId?: string; -}): ToolLoopDetectionConfig | undefined { - const global = params.cfg?.tools?.loopDetection; - const agent = - params.agentId && params.cfg - ? resolveAgentConfig(params.cfg, params.agentId)?.tools?.loopDetection - : undefined; - - if (!agent) { - return global; - } - if (!global) { - return agent; - } - - return { - ...global, - ...agent, - detectors: { - ...global.detectors, - ...agent.detectors, - }, - }; -} +export { resolveToolLoopDetectionConfig } from "./tool-loop-detection-config.js"; export const __testing = { cleanToolSchemaForGemini, diff --git a/src/agents/tool-loop-detection-config.ts b/src/agents/tool-loop-detection-config.ts new file mode 100644 index 00000000000..5da91231bcb --- /dev/null +++ b/src/agents/tool-loop-detection-config.ts @@ -0,0 +1,34 @@ +import type { OpenClawConfig } from "../config/types.openclaw.js"; +import type { ToolLoopDetectionConfig } from "../config/types.tools.js"; +import { resolveAgentConfig } from "./agent-scope.js"; + +export function resolveToolLoopDetectionConfig(params: { + cfg?: OpenClawConfig; + agentId?: string; +}): ToolLoopDetectionConfig | undefined { + const global = params.cfg?.tools?.loopDetection; + const agent = + params.agentId && params.cfg + ? resolveAgentConfig(params.cfg, params.agentId)?.tools?.loopDetection + : undefined; + + if (!agent) { + return global; + } + if (!global) { + return agent; + } + + return { + ...global, + ...agent, + detectors: { + ...global.detectors, + ...agent.detectors, + }, + postCompactionGuard: { + ...global.postCompactionGuard, + ...agent.postCompactionGuard, + }, + }; +} diff --git a/src/config/schema.labels.ts b/src/config/schema.labels.ts index 641c3a9847d..f73243b4f3f 100644 --- a/src/config/schema.labels.ts +++ b/src/config/schema.labels.ts @@ -215,6 +215,8 @@ export const FIELD_LABELS: Record = { "tools.loopDetection.unknownToolThreshold": "Unknown-tool Loop Threshold", "tools.loopDetection.criticalThreshold": "Tool-loop Critical Threshold", "tools.loopDetection.globalCircuitBreakerThreshold": "Tool-loop Global Circuit Breaker Threshold", + "tools.loopDetection.postCompactionGuard.enabled": "Post-compaction Loop Guard", + "tools.loopDetection.postCompactionGuard.windowSize": "Post-compaction Loop Guard Window Size", "tools.loopDetection.detectors.genericRepeat": "Tool-loop Generic Repeat Detection", "tools.loopDetection.detectors.knownPollNoProgress": "Tool-loop Poll No-Progress Detection", "tools.loopDetection.detectors.pingPong": "Tool-loop Ping-Pong Detection",