fix(agents): honor scoped post-compaction guard config

This commit is contained in:
Peter Steinberger
2026-05-05 00:49:57 +01:00
parent 1af6855bb0
commit ed4b223cf2
5 changed files with 104 additions and 28 deletions

View File

@@ -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

View File

@@ -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,

View File

@@ -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,

View File

@@ -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,
},
};
}

View File

@@ -215,6 +215,8 @@ export const FIELD_LABELS: Record<string, string> = {
"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",