Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions backend/pkg/providers/performer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ import (
)

const (
maxRetriesToCallSimpleChain = 3
maxRetriesToCallAgentChain = 3
maxRetriesToCallFunction = 3
maxReflectorCallsPerChain = 3
delayBetweenRetries = 5 * time.Second
maxRetriesToCallSimpleChain = 3
maxRetriesToCallAgentChain = 3
maxRetriesToCallFunction = 3
maxReflectorCallsPerChain = 3
maxAgentChainIterations = 100
maxSoftDetectionsBeforeAbort = 4
delayBetweenRetries = 5 * time.Second
Comment on lines +33 to +35
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constant name maxRepeatingDetectionsBeforeErr is a bit ambiguous because the code escalates on the Nth repeating detection (via the -1 in the threshold math), not after N detections. Consider renaming it to something like “...ToAbort/ToError” or adjusting the condition so the name matches the behavior.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point on the naming ambiguity. Renamed to maxSoftDetectionsBeforeAbort = 4 and removed the -1 from the condition. Now the math is straightforward: RepeatingToolCallThreshold(3) + maxSoftDetectionsBeforeAbort(4) = 7, and the name accurately describes the behavior (4 soft detection responses before abort). Fixed in 917b371.

)

type callResult struct {
Expand Down Expand Up @@ -91,7 +93,13 @@ func (fp *flowProvider) performAgentChain(
groupID := fmt.Sprintf("flow-%d", fp.flowID)
toolTypeMapping := tools.GetToolTypeMapping()

for {
for iteration := 0; ; iteration++ {
if iteration >= maxAgentChainIterations {
msg := fmt.Sprintf("agent chain exceeded maximum iterations (%d)", maxAgentChainIterations)
logger.WithField("iteration", iteration).Error(msg)
return errors.New(msg)
}

result, err := fp.callWithRetries(ctx, chain, optAgentType, executor)
if err != nil {
logger.WithError(err).Error("failed to call agent chain")
Expand Down Expand Up @@ -256,6 +264,12 @@ func (fp *flowProvider) execToolCall(
})

if detector.detect(toolCall) {
if len(detector.funcCalls) >= RepeatingToolCallThreshold+maxSoftDetectionsBeforeAbort {
errMsg := fmt.Sprintf("tool '%s' repeated %d times consecutively, aborting chain", funcName, len(detector.funcCalls))
logger.WithField("repeat_count", len(detector.funcCalls)).Error(errMsg)
return "", errors.New(errMsg)
}

response := fmt.Sprintf("tool call '%s' is repeating, please try another tool", funcName)

_, observation := obs.Observer.NewObservation(ctx)
Expand Down