feat: add three-layer agent loop protection#3
Merged
Conversation
- Loop detection: track tool call history with a sliding window, inject warning messages at 5 repeats, halt at 8 (critical) or 10 (global circuit breaker) - Step-level retry: exponential backoff up to MAX_RETRIES=3 for retryable API errors (rate limit, timeout, network) - Token budget: accumulate input+output tokens across steps and force-stop when the limit is exceeded (default 15,000)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a three-layer agent loop protection system to prevent infinite loops, along with supporting infrastructure (mock model, utility types, and new dependency).
Changes
feat(types): SafeAny type
SafeAnytype alias tosrc/types/any.tswith a barrel export fromsrc/types/index.ts.feat: Mock model implementation
src/mock-model.ts— a fully configurable in-process model stub that supports tool calling and scripted response sequences, enabling deterministic loop testing without live API calls.feat: Three-layer agent loop protection
src/loop-detection.ts): Tracks tool call history with a sliding window. Injects a warning message after 5 repeated calls and halts execution at 8 (critical threshold) or 10 (global circuit breaker).src/retry.ts): Exponential backoff with up toMAX_RETRIES=3for retryable API errors (rate limit, timeout, network failures).input + outputtokens across steps and force-stops when the limit is exceeded (default 15,000 tokens).src/agent-loop.tsandsrc/index.tsto integrate all three layers.chore: Add
@ai-sdk/providerdependencyTesting