Skip to content

Commit b5ed58c

Browse files
authored
refactor(agents): use fixed tool-loop defaults directly (#130681)
1 parent 345b25e commit b5ed58c

1 file changed

Lines changed: 16 additions & 80 deletions

File tree

‎src/agents/tool-loop-detection.ts‎

Lines changed: 16 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -50,33 +50,6 @@ const TOOL_CALL_HISTORY_SIZE = 30;
5050
export const UNKNOWN_TOOL_THRESHOLD = 10;
5151
const CRITICAL_THRESHOLD = 20;
5252
const GLOBAL_CIRCUIT_BREAKER_THRESHOLD = 30;
53-
const DEFAULT_LOOP_DETECTION_CONFIG = {
54-
enabled: false,
55-
historySize: TOOL_CALL_HISTORY_SIZE,
56-
warningThreshold: TOOL_LOOP_WARNING_THRESHOLD,
57-
unknownToolThreshold: UNKNOWN_TOOL_THRESHOLD,
58-
criticalThreshold: CRITICAL_THRESHOLD,
59-
globalCircuitBreakerThreshold: GLOBAL_CIRCUIT_BREAKER_THRESHOLD,
60-
detectors: {
61-
genericRepeat: true,
62-
knownPollNoProgress: true,
63-
pingPong: true,
64-
},
65-
};
66-
67-
type ResolvedLoopDetectionConfig = {
68-
enabled: boolean;
69-
historySize: number;
70-
warningThreshold: number;
71-
unknownToolThreshold: number;
72-
criticalThreshold: number;
73-
globalCircuitBreakerThreshold: number;
74-
detectors: {
75-
genericRepeat: boolean;
76-
knownPollNoProgress: boolean;
77-
pingPong: boolean;
78-
};
79-
};
8053

8154
type ToolLoopDetectionScope = {
8255
runId?: string;
@@ -90,18 +63,6 @@ function selectHistoryForScope(
9063
return history.filter((record) => normalizeRunId(record.runId) === runId);
9164
}
9265

93-
function resolveLoopDetectionConfig(config?: ToolLoopDetectionConfig): ResolvedLoopDetectionConfig {
94-
return {
95-
enabled: config?.enabled ?? DEFAULT_LOOP_DETECTION_CONFIG.enabled,
96-
historySize: DEFAULT_LOOP_DETECTION_CONFIG.historySize,
97-
warningThreshold: DEFAULT_LOOP_DETECTION_CONFIG.warningThreshold,
98-
unknownToolThreshold: DEFAULT_LOOP_DETECTION_CONFIG.unknownToolThreshold,
99-
criticalThreshold: DEFAULT_LOOP_DETECTION_CONFIG.criticalThreshold,
100-
globalCircuitBreakerThreshold: DEFAULT_LOOP_DETECTION_CONFIG.globalCircuitBreakerThreshold,
101-
detectors: DEFAULT_LOOP_DETECTION_CONFIG.detectors,
102-
};
103-
}
104-
10566
/**
10667
* Hash a tool call for pattern matching.
10768
* Uses tool name + deterministic JSON serialization digest of params.
@@ -530,8 +491,7 @@ export function detectToolCallLoop(
530491
config?: ToolLoopDetectionConfig,
531492
scope?: ToolLoopDetectionScope,
532493
): LoopDetectionResult {
533-
const resolvedConfig = resolveLoopDetectionConfig(config);
534-
if (!resolvedConfig.enabled) {
494+
if (!config?.enabled) {
535495
return { stuck: false };
536496
}
537497
const history = selectHistoryForScope(state.toolCallHistory ?? [], scope);
@@ -543,11 +503,9 @@ export function detectToolCallLoop(
543503
const knownPollTool = isKnownPollToolCall(toolName, params);
544504
const pingPong = getPingPongStreak(history, currentHash);
545505
const argumentChurnLivenessSignal =
546-
argumentChurn.count >= resolvedConfig.warningThreshold
547-
? ("argument_churn" as const)
548-
: undefined;
506+
argumentChurn.count >= TOOL_LOOP_WARNING_THRESHOLD ? ("argument_churn" as const) : undefined;
549507

550-
if (unknownToolStreak.count >= resolvedConfig.unknownToolThreshold) {
508+
if (unknownToolStreak.count >= UNKNOWN_TOOL_THRESHOLD) {
551509
return {
552510
stuck: true,
553511
level: "critical",
@@ -558,7 +516,7 @@ export function detectToolCallLoop(
558516
};
559517
}
560518

561-
if (noProgressStreak >= resolvedConfig.globalCircuitBreakerThreshold) {
519+
if (noProgressStreak >= GLOBAL_CIRCUIT_BREAKER_THRESHOLD) {
562520
log.error(
563521
`Global circuit breaker triggered: ${toolName} repeated ${noProgressStreak} times with no progress`,
564522
);
@@ -572,11 +530,7 @@ export function detectToolCallLoop(
572530
};
573531
}
574532

575-
if (
576-
knownPollTool &&
577-
resolvedConfig.detectors.knownPollNoProgress &&
578-
noProgressStreak >= resolvedConfig.criticalThreshold
579-
) {
533+
if (knownPollTool && noProgressStreak >= CRITICAL_THRESHOLD) {
580534
log.error(`Critical polling loop detected: ${toolName} repeated ${noProgressStreak} times`);
581535
return {
582536
stuck: true,
@@ -588,11 +542,7 @@ export function detectToolCallLoop(
588542
};
589543
}
590544

591-
if (
592-
knownPollTool &&
593-
resolvedConfig.detectors.knownPollNoProgress &&
594-
noProgressStreak >= resolvedConfig.warningThreshold
595-
) {
545+
if (knownPollTool && noProgressStreak >= TOOL_LOOP_WARNING_THRESHOLD) {
596546
log.warn(`Polling loop warning: ${toolName} repeated ${noProgressStreak} times`);
597547
return {
598548
stuck: true,
@@ -609,11 +559,7 @@ export function detectToolCallLoop(
609559
? `pingpong:${canonicalPairKey(currentHash, pingPong.pairedSignature)}`
610560
: `pingpong:${toolName}:${currentHash}`;
611561

612-
if (
613-
resolvedConfig.detectors.pingPong &&
614-
pingPong.count >= resolvedConfig.criticalThreshold &&
615-
pingPong.noProgressEvidence
616-
) {
562+
if (pingPong.count >= CRITICAL_THRESHOLD && pingPong.noProgressEvidence) {
617563
log.error(
618564
`Critical ping-pong loop detected: alternating calls count=${pingPong.count} currentTool=${toolName}`,
619565
);
@@ -628,7 +574,7 @@ export function detectToolCallLoop(
628574
};
629575
}
630576

631-
if (resolvedConfig.detectors.pingPong && pingPong.count >= resolvedConfig.warningThreshold) {
577+
if (pingPong.count >= TOOL_LOOP_WARNING_THRESHOLD) {
632578
log.warn(
633579
`Ping-pong loop warning: alternating calls count=${pingPong.count} currentTool=${toolName}`,
634580
);
@@ -649,11 +595,7 @@ export function detectToolCallLoop(
649595
const recentCount = history.filter(
650596
(h) => h.toolName === toolName && h.argsHash === currentHash,
651597
).length;
652-
if (
653-
!knownPollTool &&
654-
resolvedConfig.detectors.genericRepeat &&
655-
noProgressStreak >= resolvedConfig.criticalThreshold
656-
) {
598+
if (!knownPollTool && noProgressStreak >= CRITICAL_THRESHOLD) {
657599
log.error(`Critical generic loop detected: ${toolName} repeated ${noProgressStreak} times`);
658600
return {
659601
stuck: true,
@@ -665,16 +607,12 @@ export function detectToolCallLoop(
665607
};
666608
}
667609

668-
if (argumentChurn.count >= resolvedConfig.warningThreshold) {
610+
if (argumentChurn.count >= TOOL_LOOP_WARNING_THRESHOLD) {
669611
log.warn(`Argument churn warning: ${toolName} cycled through stable argument patterns`);
670612
return buildArgumentChurnWarning(toolName, argumentChurn);
671613
}
672614

673-
if (
674-
!knownPollTool &&
675-
resolvedConfig.detectors.genericRepeat &&
676-
recentCount >= resolvedConfig.warningThreshold
677-
) {
615+
if (!knownPollTool && recentCount >= TOOL_LOOP_WARNING_THRESHOLD) {
678616
log.warn(`Loop warning: ${toolName} called ${recentCount} times with identical arguments`);
679617
return {
680618
stuck: true,
@@ -698,10 +636,9 @@ export function recordToolCall(
698636
toolName: string,
699637
params: unknown,
700638
toolCallId?: string,
701-
config?: ToolLoopDetectionConfig,
639+
_config?: ToolLoopDetectionConfig,
702640
scope?: ToolLoopDetectionScope,
703641
): void {
704-
const resolvedConfig = resolveLoopDetectionConfig(config);
705642
const runId = normalizeRunId(scope?.runId);
706643
if (!state.toolCallHistory) {
707644
state.toolCallHistory = [];
@@ -715,8 +652,8 @@ export function recordToolCall(
715652
timestamp: Date.now(),
716653
});
717654

718-
if (state.toolCallHistory.length > resolvedConfig.historySize) {
719-
state.toolCallHistory.splice(0, state.toolCallHistory.length - resolvedConfig.historySize);
655+
if (state.toolCallHistory.length > TOOL_CALL_HISTORY_SIZE) {
656+
state.toolCallHistory.splice(0, state.toolCallHistory.length - TOOL_CALL_HISTORY_SIZE);
720657
}
721658
}
722659

@@ -735,7 +672,6 @@ export function recordToolCallOutcome(
735672
runId?: string;
736673
},
737674
): ToolCallRecord | undefined {
738-
const resolvedConfig = resolveLoopDetectionConfig(params.config);
739675
const runId = normalizeRunId(params.runId);
740676
const outcome = hashToolOutcome(params.toolName, params.toolParams, params.result, params.error);
741677
if (!outcome.resultHash && !outcome.outcomeKind) {
@@ -795,8 +731,8 @@ export function recordToolCallOutcome(
795731
recordedOutcome = record;
796732
}
797733

798-
if (state.toolCallHistory.length > resolvedConfig.historySize) {
799-
state.toolCallHistory.splice(0, state.toolCallHistory.length - resolvedConfig.historySize);
734+
if (state.toolCallHistory.length > TOOL_CALL_HISTORY_SIZE) {
735+
state.toolCallHistory.splice(0, state.toolCallHistory.length - TOOL_CALL_HISTORY_SIZE);
800736
}
801737
return recordedOutcome;
802738
}

0 commit comments

Comments
 (0)