Skip to content

Comments

fix(loop): retain discover_agents in context + enforce loop sleep#215

Open
uibeka wants to merge 1 commit intoConway-Research:mainfrom
uibeka:fix/loop-discovery-context-and-enforcement
Open

fix(loop): retain discover_agents in context + enforce loop sleep#215
uibeka wants to merge 1 commit intoConway-Research:mainfrom
uibeka:fix/loop-discovery-context-and-enforcement

Conversation

@uibeka
Copy link
Contributor

@uibeka uibeka commented Feb 24, 2026

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_agents misclassified in IDLE_ONLY_TOOLS (loop.ts)

discover_agents is included in the idle tools set alongside status-check tools like check_credits and system_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_agents alone (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_agents from IDLE_ONLY_TOOLS

One-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

  • Agents can now discover other agents and act on results without context-stripping retry loops
  • Infinite loops of ALL types (not just discovery) are bounded to ~6 turns maximum instead of unbounded
  • No cost increase — enforcement reduces credit waste
  • Backward compatible — agents that don't loop see zero behavioral change

Testing

  • pnpm build — zero errors
  • pnpm test — 27/27 files, 1005/1005 tests pass
  • 3 new tests: enforcement at 6 identical patterns, enforcement reset on behavior change, discover_agents context retention
  • 1 existing test updated: maintenance loop test uses review_memory instead of removed discover_agents

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]>
@uibeka uibeka force-pushed the fix/loop-discovery-context-and-enforcement branch from dfe7b39 to 9e38e71 Compare February 24, 2026 17:53
@uibeka
Copy link
Contributor Author

uibeka commented Feb 24, 2026

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.

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