fix(loop): retain discover_agents in context + enforce loop sleep#215
Open
uibeka wants to merge 1 commit intoConway-Research:mainfrom
Open
fix(loop): retain discover_agents in context + enforce loop sleep#215uibeka wants to merge 1 commit intoConway-Research:mainfrom
uibeka wants to merge 1 commit intoConway-Research:mainfrom
Conversation
Two related fixes to the agent loop: 1. Remove discover_agents from IDLE_ONLY_TOOLS — discovery results are external, actionable data that agents need in subsequent turns. Unlike status-check tools (check_credits, system_synopsis), discovery produces information the agent must process. Classifying it as idle strips results from context, causing immediate retry loops. 2. Add loop enforcement escalation — the existing loop detector correctly identifies repetitive patterns after 3 turns but only injects a text warning that agents ignore. Now, if the warning is ignored and the same pattern repeats 3 more times, the agent is forced to sleep. This breaks infinite loops while giving the agent a fair chance to self-correct. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
dfe7b39 to
9e38e71
Compare
Contributor
Author
|
Rebased on v0.2.0 (a6b2b11). Our loop.test.ts passes all 18 tests. Note: CI may show failures from v0.2.0's new test files (setTimeout mock issue in orchestration tests) - unrelated to this PR's changes. |
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.
Problem
Agents enter infinite tool-call loops, burning credits indefinitely. Observed across 4 production sessions with different tool types (discover_agents, exec, expose_port). The agent loops within 3-7 turns every session, with the loop detector firing but failing to break the cycle. Total credit waste: ~$3.50+ across sessions, 0 successful autonomous task completions.
Root Cause
1.
discover_agentsmisclassified inIDLE_ONLY_TOOLS(loop.ts)discover_agentsis included in the idle tools set alongside status-check tools likecheck_creditsandsystem_synopsis. However, discovery returns external, actionable data (other agents' identities, descriptions, capabilities) — fundamentally different from self-status checks that return unchanging internal state.When an agent calls
discover_agentsalone (the natural first action for any network-aware agent), the turn is classified as "idle" and stripped from conversational context before the next inference call. The agent has no memory of having performed discovery, so it calls it again. Infinitely.2. Loop detector warns but doesn't enforce (loop.ts)
The repetitive pattern detector correctly fires after 3 identical tool-call patterns, injecting a "DO SOMETHING DIFFERENT" system message. But it then resets the counter — the agent ignores the text warning, loops 3 more times, gets the same warning, forever. Detection without enforcement.
Fix
1. Remove
discover_agentsfromIDLE_ONLY_TOOLSOne-line change. Discovery produces external data the agent needs to act on in subsequent turns. Retaining it in context lets agents process discovery results normally.
2. Add loop enforcement escalation
After the existing warning fires (3 identical patterns), if the agent ignores the warning and repeats the same pattern 3 more times (6 total), it is forced to sleep. This breaks infinite loops while giving agents a fair chance to self-correct after the first warning.
Impact
Testing
pnpm build— zero errorspnpm test— 27/27 files, 1005/1005 tests passreview_memoryinstead of removeddiscover_agents