Skip to content

Comments

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
uibeka:fix/context-degradation-fallback
Open

fix(context): improve maintenance loop resilience with deep fallback, memory filtering, and enhanced warnings#189
uibeka wants to merge 1 commit intoConway-Research:mainfrom
uibeka:fix/context-degradation-fallback

Conversation

@uibeka
Copy link
Contributor

@uibeka uibeka commented Feb 22, 2026

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:

  1. The IDLE_ONLY_TOOLS filter's fallback shows only 2 idle turns when all 20 are idle — zero productive context
  2. Episodic memory reinforces the loop — memory block injection and review_memory surface 20 maintenance entries
  3. Anti-repetition warning is too generic — "Move on to BUILDING something" is ignored because it competes with 20 turns of concrete behavioral evidence

Root 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_memory creates stronger behavioral
signal 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_memory tool 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

  • Agents retain productive context even during deep maintenance loops
  • Memory channel stops reinforcing maintenance patterns
  • Anti-repetition warning provides actionable direction, not vague suggestion
  • All three fixes are additive — if they don't help, they don't hurt
  • Normal productive operation completely unaffected (fixes only activate during degradation)
  • Zero extra inference calls — all fixes are pure logic
  • Zero changes to loop detection, idle detection, sleep/wake lifecycle, policy engine, or financial rules

Testing

  • 1002 existing tests pass (zero regressions, 1 updated with explicit classification)
  • Added: Deep fallback retrieves productive turns when all 20 are idle
  • Added: Deep fallback falls back to current behavior when no productive turns in 100
  • Added: Normal productive path unaffected (deep fallback not triggered)
  • Added: Memory injection filters out maintenance/idle entries
  • Added: Memory injection handles all-maintenance case (empty block, no crash)
  • Added: review_memory filters maintenance/idle entries
  • Added: Enhanced warning includes specific tool names and avoid-list
  • Added: Warning not triggered during diverse productive tool usage
  • Total: 1004 tests passing across 27 test files

… 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]>
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