Skip to content

feat: add three-layer agent loop protection#3

Merged
Jonnytoshen merged 4 commits into
mainfrom
feat/agent-fuse
May 21, 2026
Merged

feat: add three-layer agent loop protection#3
Jonnytoshen merged 4 commits into
mainfrom
feat/agent-fuse

Conversation

@Jonnytoshen

Copy link
Copy Markdown
Owner

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

  • Added SafeAny type alias to src/types/any.ts with a barrel export from src/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

  • Loop detection (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).
  • Step-level retry (src/retry.ts): Exponential backoff with up to MAX_RETRIES=3 for retryable API errors (rate limit, timeout, network failures).
  • Token budget: Accumulates input + output tokens across steps and force-stops when the limit is exceeded (default 15,000 tokens).
  • Updated src/agent-loop.ts and src/index.ts to integrate all three layers.

chore: Add @ai-sdk/provider dependency

  • Required peer dependency for the provider abstraction used by the mock model.

Testing

  • Lint: ✅ passed
  • Format check: ✅ passed

- 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)
Copilot AI review requested due to automatic review settings May 21, 2026 07:08
@Jonnytoshen Jonnytoshen merged commit c9c63c2 into main May 21, 2026
1 check failed
@Jonnytoshen Jonnytoshen deleted the feat/agent-fuse branch May 21, 2026 07:10
@Jonnytoshen Jonnytoshen removed the request for review from Copilot May 21, 2026 07:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant