Skip to content

fix(ralph-loop): keep LLM in smart zone with fresh context per iteration#1901

Open
edxeth wants to merge 7 commits intocode-yeongyu:devfrom
edxeth:pr-1348-reopen
Open

fix(ralph-loop): keep LLM in smart zone with fresh context per iteration#1901
edxeth wants to merge 7 commits intocode-yeongyu:devfrom
edxeth:pr-1348-reopen

Conversation

@edxeth
Copy link
Contributor

@edxeth edxeth commented Feb 16, 2026

The Problem: Context Window Overflow

The original ralph-loop implementation kept all iterations in a single session, causing the context window to fill up over time. This is exactly the problem described in "Why the Anthropic Ralph plugin sucks":

As LLMs receive more tokens, the relationships between tokens scale quadratically. Each additional token makes it harder for the model to process information and make good decisions.

Every LLM has a smart zone (first ~40% of context) and a dumb zone (remaining ~60%). The original implementation guaranteed you'd enter the dumb zone after just a few iterations:

Iteration Context Usage Zone
1 ~20% Smart
2 ~35% Smart
3 ~50% Dumb
4+ 60%+ Dumb

The Solution: Fresh Context Per Iteration

This PR adds a default_strategy config option with "reset" as the new default:

Strategy Behavior Use Case
reset Creates NEW session each iteration Long-running loops, production work
continue Keeps same session Short tasks, debugging

With reset, each iteration starts with a fresh context window. The LLM always operates in the smart zone:

Iteration 1: Fresh context → Smart zone → Commit
Iteration 2: Fresh context → Smart zone → Commit  
Iteration 3: Fresh context → Smart zone → Commit
...

This matches how the original bash-loop Ralph works:

while :; do cat PROMPT.md | claude-code ; done

Each invocation = fresh context.

Changes

Core Feature

  • New default_strategy config with reset (default) and continue options
  • --strategy flag for per-loop override: /ralph-loop "task" --strategy=continue
  • Strategy stored in loop state - persists across iterations

Bug Fixes

  • Skip todo-continuation-enforcer when ralph-loop is active - prevents hook conflicts
  • Conditional prompt prefix - reset allows keyword detection (new session needs mode injections), continue skips it (mode already applied)

Implementation

  • TUI session switch via raw API call to /tui/select-session
  • Strategy parsed from command arguments in both chat.message and PreToolUse handlers

Usage

Config (default for all loops):

{
  "ralph_loop": {
    "enabled": true,
    "default_strategy": "reset"
  }
}

Command (per-loop override):

/ralph-loop "Build feature X" --strategy=continue --max-iterations=10

Testing

  • 42 ralph-loop tests pass
  • 37 todo-continuation-enforcer tests pass
  • Manually verified both strategies work correctly

References


Reopen Note

This PR is a direct reopen of #1348 from a dedicated feature branch (edxeth:pr-1348-reopen) because the previous PR used dev as both head and base branch name context.

… context per iteration

- Add ContextStrategySchema with 'reset' and 'continue' options
- 'reset' (default): creates new session with fresh context each iteration
- 'continue': keeps same session, accumulates context
- Implement TUI session switch via raw API call to /tui/select-session
- Update tests and documentation

This addresses context overflow in long-running Ralph loops by starting
each iteration with a clean context window (smart zone).
- Rename context_strategy to default_strategy in config schema
- Add --strategy=reset|continue flag to /ralph-loop and /ulw-loop commands
- Store strategy in loop state for per-loop override
- Parse --strategy from command arguments in index.ts
- Skip todo-continuation-enforcer when ralph-loop is active
- Update tests for new naming and behavior
…ation 1

- Always write strategy field to state file with fallback to DEFAULT_STRATEGY
- Reset strategy now sends full command template (not continuation prompt)
- Preserve raw user task with flags instead of parsed prompt
- Skip startLoop re-initialization when loop already active (prevents infinite loop)
- Rename initial session to 'Ralph Loop - Iteration 1' using session.update API
- Only replace {{PROMISE}} placeholder for continue strategy

Fixes reset strategy creating endless iteration code-yeongyu#2 sessions and ensures
each iteration receives the exact same message format as iteration 1.
Remove hardcoded session rename to 'Ralph Loop - Iteration 1' for the
first iteration. Iteration 2+ already use dynamic naming via
ralph-loop/index.ts, so this was redundant. Let OpenCode generate
session names naturally for iteration 1.
Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 20 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Auto-approved: PR only adds new feature for context strategy with thorough tests and no regressions detected.

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