@@ -50,33 +50,6 @@ const TOOL_CALL_HISTORY_SIZE = 30;
5050export const UNKNOWN_TOOL_THRESHOLD = 10 ;
5151const CRITICAL_THRESHOLD = 20 ;
5252const 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
8154type 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.
@@ -514,8 +475,7 @@ export function detectToolCallLoop(
514475 config ?: ToolLoopDetectionConfig ,
515476 scope ?: ToolLoopDetectionScope ,
516477) : LoopDetectionResult {
517- const resolvedConfig = resolveLoopDetectionConfig ( config ) ;
518- if ( ! resolvedConfig . enabled ) {
478+ if ( ! config ?. enabled ) {
519479 return { stuck : false } ;
520480 }
521481 const history = selectHistoryForScope ( state . toolCallHistory ?? [ ] , scope ) ;
@@ -527,11 +487,9 @@ export function detectToolCallLoop(
527487 const knownPollTool = isKnownPollToolCall ( toolName , params ) ;
528488 const pingPong = getPingPongStreak ( history , currentHash ) ;
529489 const argumentChurnLivenessSignal =
530- argumentChurn . count >= resolvedConfig . warningThreshold
531- ? ( "argument_churn" as const )
532- : undefined ;
490+ argumentChurn . count >= TOOL_LOOP_WARNING_THRESHOLD ? ( "argument_churn" as const ) : undefined ;
533491
534- if ( unknownToolStreak . count >= resolvedConfig . unknownToolThreshold ) {
492+ if ( unknownToolStreak . count >= UNKNOWN_TOOL_THRESHOLD ) {
535493 return {
536494 stuck : true ,
537495 level : "critical" ,
@@ -542,7 +500,7 @@ export function detectToolCallLoop(
542500 } ;
543501 }
544502
545- if ( noProgressStreak >= resolvedConfig . globalCircuitBreakerThreshold ) {
503+ if ( noProgressStreak >= GLOBAL_CIRCUIT_BREAKER_THRESHOLD ) {
546504 log . error (
547505 `Global circuit breaker triggered: ${ toolName } repeated ${ noProgressStreak } times with no progress` ,
548506 ) ;
@@ -556,11 +514,7 @@ export function detectToolCallLoop(
556514 } ;
557515 }
558516
559- if (
560- knownPollTool &&
561- resolvedConfig . detectors . knownPollNoProgress &&
562- noProgressStreak >= resolvedConfig . criticalThreshold
563- ) {
517+ if ( knownPollTool && noProgressStreak >= CRITICAL_THRESHOLD ) {
564518 log . error ( `Critical polling loop detected: ${ toolName } repeated ${ noProgressStreak } times` ) ;
565519 return {
566520 stuck : true ,
@@ -572,11 +526,7 @@ export function detectToolCallLoop(
572526 } ;
573527 }
574528
575- if (
576- knownPollTool &&
577- resolvedConfig . detectors . knownPollNoProgress &&
578- noProgressStreak >= resolvedConfig . warningThreshold
579- ) {
529+ if ( knownPollTool && noProgressStreak >= TOOL_LOOP_WARNING_THRESHOLD ) {
580530 log . warn ( `Polling loop warning: ${ toolName } repeated ${ noProgressStreak } times` ) ;
581531 return {
582532 stuck : true ,
@@ -593,11 +543,7 @@ export function detectToolCallLoop(
593543 ? `pingpong:${ canonicalPairKey ( currentHash , pingPong . pairedSignature ) } `
594544 : `pingpong:${ toolName } :${ currentHash } ` ;
595545
596- if (
597- resolvedConfig . detectors . pingPong &&
598- pingPong . count >= resolvedConfig . criticalThreshold &&
599- pingPong . noProgressEvidence
600- ) {
546+ if ( pingPong . count >= CRITICAL_THRESHOLD && pingPong . noProgressEvidence ) {
601547 log . error (
602548 `Critical ping-pong loop detected: alternating calls count=${ pingPong . count } currentTool=${ toolName } ` ,
603549 ) ;
@@ -612,7 +558,7 @@ export function detectToolCallLoop(
612558 } ;
613559 }
614560
615- if ( resolvedConfig . detectors . pingPong && pingPong . count >= resolvedConfig . warningThreshold ) {
561+ if ( pingPong . count >= TOOL_LOOP_WARNING_THRESHOLD ) {
616562 log . warn (
617563 `Ping-pong loop warning: alternating calls count=${ pingPong . count } currentTool=${ toolName } ` ,
618564 ) ;
@@ -633,11 +579,7 @@ export function detectToolCallLoop(
633579 const recentCount = history . filter (
634580 ( h ) => h . toolName === toolName && h . argsHash === currentHash ,
635581 ) . length ;
636- if (
637- ! knownPollTool &&
638- resolvedConfig . detectors . genericRepeat &&
639- noProgressStreak >= resolvedConfig . criticalThreshold
640- ) {
582+ if ( ! knownPollTool && noProgressStreak >= CRITICAL_THRESHOLD ) {
641583 log . error ( `Critical generic loop detected: ${ toolName } repeated ${ noProgressStreak } times` ) ;
642584 return {
643585 stuck : true ,
@@ -649,16 +591,12 @@ export function detectToolCallLoop(
649591 } ;
650592 }
651593
652- if ( argumentChurn . count >= resolvedConfig . warningThreshold ) {
594+ if ( argumentChurn . count >= TOOL_LOOP_WARNING_THRESHOLD ) {
653595 log . warn ( `Argument churn warning: ${ toolName } cycled through stable argument patterns` ) ;
654596 return buildArgumentChurnWarning ( toolName , argumentChurn ) ;
655597 }
656598
657- if (
658- ! knownPollTool &&
659- resolvedConfig . detectors . genericRepeat &&
660- recentCount >= resolvedConfig . warningThreshold
661- ) {
599+ if ( ! knownPollTool && recentCount >= TOOL_LOOP_WARNING_THRESHOLD ) {
662600 log . warn ( `Loop warning: ${ toolName } called ${ recentCount } times with identical arguments` ) ;
663601 return {
664602 stuck : true ,
@@ -682,10 +620,9 @@ export function recordToolCall(
682620 toolName : string ,
683621 params : unknown ,
684622 toolCallId ?: string ,
685- config ?: ToolLoopDetectionConfig ,
623+ _config ?: ToolLoopDetectionConfig ,
686624 scope ?: ToolLoopDetectionScope ,
687625) : void {
688- const resolvedConfig = resolveLoopDetectionConfig ( config ) ;
689626 const runId = normalizeRunId ( scope ?. runId ) ;
690627 if ( ! state . toolCallHistory ) {
691628 state . toolCallHistory = [ ] ;
@@ -699,8 +636,8 @@ export function recordToolCall(
699636 timestamp : Date . now ( ) ,
700637 } ) ;
701638
702- if ( state . toolCallHistory . length > resolvedConfig . historySize ) {
703- state . toolCallHistory . splice ( 0 , state . toolCallHistory . length - resolvedConfig . historySize ) ;
639+ if ( state . toolCallHistory . length > TOOL_CALL_HISTORY_SIZE ) {
640+ state . toolCallHistory . splice ( 0 , state . toolCallHistory . length - TOOL_CALL_HISTORY_SIZE ) ;
704641 }
705642}
706643
@@ -719,7 +656,6 @@ export function recordToolCallOutcome(
719656 runId ?: string ;
720657 } ,
721658) : ToolCallRecord | undefined {
722- const resolvedConfig = resolveLoopDetectionConfig ( params . config ) ;
723659 const runId = normalizeRunId ( params . runId ) ;
724660 const outcome = hashToolOutcome ( params . toolName , params . toolParams , params . result , params . error ) ;
725661 if ( ! outcome . resultHash && ! outcome . outcomeKind ) {
@@ -779,8 +715,8 @@ export function recordToolCallOutcome(
779715 recordedOutcome = record ;
780716 }
781717
782- if ( state . toolCallHistory . length > resolvedConfig . historySize ) {
783- state . toolCallHistory . splice ( 0 , state . toolCallHistory . length - resolvedConfig . historySize ) ;
718+ if ( state . toolCallHistory . length > TOOL_CALL_HISTORY_SIZE ) {
719+ state . toolCallHistory . splice ( 0 , state . toolCallHistory . length - TOOL_CALL_HISTORY_SIZE ) ;
784720 }
785721 return recordedOutcome ;
786722}
0 commit comments