fix(context): improve maintenance loop resilience with deep fallback, memory filtering, and enhanced warnings#189
Open
uibeka wants to merge 1 commit intoConway-Research:mainfrom
Conversation
… memory filtering, and enhanced warnings Three complementary fixes targeting context degradation during maintenance loops: 1. IDLE filter deep fallback (loop.ts): When all 20 recent turns are idle-only, scans back up to 100 turns for productive context. Includes up to 5 historical productive turns alongside the 2 most recent for continuity. 2. Episodic memory filtering (retrieval.ts + loop.ts): Filters maintenance and idle classified entries from both auto-injected memory blocks and review_memory tool results. Breaks the memory -> maintenance feedback loop. 3. Enhanced anti-repetition warning (context.ts): Replaces generic "Move on to BUILDING something" with specific directive naming productive tools to use, tools to avoid, and referencing the genesis prompt. All fixes are additive, zero-cost during normal operation, and invisible when agents are productive. 14 new tests covering positive and negative cases. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
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 degrade from productive work into maintenance loops after ~10-20 productive
turns. The 20-turn context window fills with status-check calls, and the model
pattern-matches on recent history rather than following genesis prompt instructions.
Three weaknesses in the context pipeline allow this:
review_memorysurface 20 maintenance entriesRoot Cause
LLMs weight recent conversational history more heavily than system prompt
instructions. When maintenance turns dominate the context window, that behavioral
pattern overwhelms the genesis prompt. The system prompt (genesis + SOUL.md)
is present every turn — the agent's mission is always visible. But 20 turns of
check_credits → system_synopsis → review_memorycreates stronger behavioralsignal than the mission directive.
Evidence: wiping state.db while preserving identical genesis prompt and SOUL.md
immediately restores productive behavior. The only variable is conversational
history content.
Fix
Three complementary fixes targeting different points in the context pipeline:
1. IDLE filter deep fallback (loop.ts)
When all 20 recent turns are idle-only, scan back up to 100 turns for the 5 most
recent productive turns. Include these alongside the 2 most recent turns so the
model has productive context to reference. Falls back to current behavior if no
productive turns found in 100. Only triggers in the all-idle case — zero cost
during normal productive operation.
2. Episodic memory filtering (retrieval.ts + loop.ts)
Filter maintenance and idle classified entries from both the auto-injected memory
block and
review_memorytool results. Only productive, strategic, communication,and error memories pass through. Breaks the memory → maintenance feedback loop
where agents check memory → see maintenance history → continue maintenance.
3. Enhanced anti-repetition warning (context.ts)
Replace generic "Move on to BUILDING something" with specific directive that names
concrete productive tools (exec, write_file, expose_port, register_erc8004), names
tools to avoid (check_credits, system_synopsis, review_memory, discover_agents),
references the genesis prompt, and includes the consecutive idle turn count.
Impact
Testing