fix(ralph-loop): finalize continue/reset loop strategies and docs#2116
Open
edxeth wants to merge 12 commits intocode-yeongyu:devfrom
Open
fix(ralph-loop): finalize continue/reset loop strategies and docs#2116edxeth wants to merge 12 commits intocode-yeongyu:devfrom
edxeth wants to merge 12 commits intocode-yeongyu:devfrom
Conversation
…ant behavior Make reset loop iterations inherit agent/model/variant correctly while opening as top-level sessions and selecting them reliably across SDK/TUI API shapes. Persist per-session chat variant across turns, clear it on session deletion, and avoid re-injecting variant when model fallback explicitly removes it.
Make omitted --strategy always resolve to continue, remove hardcoded reset session titles, and stop continuation/background descendant activity when completion promises are reached. This follows up code-yeongyu#2108 and restores the intended reset/continue behavior established in code-yeongyu#1901.
There was a problem hiding this comment.
2 issues found across 16 files
Confidence score: 3/5
- There is a concrete user-facing risk in
src/hooks/ralph-loop/command-arguments.ts: slicingrawArgumentsby a trimmedpromptCandidatecan misalign flags parsing when leading/trailing spaces exist. - Dead code in
src/hooks/ralph-loop/ralph-loop-event-handler.tsforsession.stopsuggests some behavior is unreachable; likely low functional impact but indicates mismatch with Opencode SDK events. - Pay close attention to
src/hooks/ralph-loop/command-arguments.tsandsrc/hooks/ralph-loop/ralph-loop-event-handler.ts- correct the slice alignment and remove the unusedsession.stophandler.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/hooks/ralph-loop/command-arguments.ts">
<violation number="1" location="src/hooks/ralph-loop/command-arguments.ts:21">
P1: Incorrect string slicing logic for `flagsSource` when `promptCandidate` has leading/trailing spaces. The code uses `rawArguments.slice(promptCandidate.length)` but `promptCandidate` is trimmed, causing the slice to start inside the original string rather than after the prompt. This leaks prompt content into flag parsing and can incorrectly parse flags that appear inside the prompt text.</violation>
</file>
<file name="src/hooks/ralph-loop/ralph-loop-event-handler.ts">
<violation number="1" location="src/hooks/ralph-loop/ralph-loop-event-handler.ts:189">
P2: Custom agent: **Opencode Compatibility**
The Opencode SDK does not emit a `session.stop` event. This block is dead code and should be removed. Session aborts are correctly handled through your existing `session.error` and `message.updated` event handlers.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
Fix unquoted prompt flag slicing so leading/trailing spaces cannot leak prompt text into flag parsing, and remove unsupported session.stop handling in favor of session.error/message.updated abort paths. Add coverage for leading-space prompt parsing.
There was a problem hiding this comment.
2 issues found across 16 files
Confidence score: 3/5
- There’s some regression risk:
src/hooks/ralph-loop/ralph-loop-event-handler.tsassumes error messages are on the error object, but the Opencode SDK nests them under.data.message, which could break error handling. - Test reliability risk in
src/hooks/ralph-loop/session-reset-strategy.test.ts: assigning undefined toprocess.envbecomes the string "undefined", potentially polluting subsequent tests. - Pay close attention to
src/hooks/ralph-loop/ralph-loop-event-handler.tsandsrc/hooks/ralph-loop/session-reset-strategy.test.ts- error handling compatibility and environment variable cleanup.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/hooks/ralph-loop/ralph-loop-event-handler.ts">
<violation number="1" location="src/hooks/ralph-loop/ralph-loop-event-handler.ts:216">
P1: Custom agent: **Opencode Compatibility**
Error messages in Opencode SDK are nested inside `.data.message`, not directly on the error object.</violation>
</file>
<file name="src/hooks/ralph-loop/session-reset-strategy.test.ts">
<violation number="1" location="src/hooks/ralph-loop/session-reset-strategy.test.ts:11">
P1: Assigning potentially undefined values to process.env causes test pollution - Node.js converts undefined to string "undefined". Use conditional assignment: if undefined, use `delete process.env[key]` instead.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
Prevent duplicate or premature Ralph loop continuation by deferring idle-driven iterations while descendant tasks or active todos remain. Harden reset-session continuation and prompt replay so reset loops stay stable across session handoff and abort edge cases.
Use ralph_loop.default_strategy when --strategy is omitted so command behavior follows configuration instead of always defaulting to continue.
There was a problem hiding this comment.
1 issue found across 3 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/hooks/ralph-loop/reset-iteration-prompt-builder.ts">
<violation number="1" location="src/hooks/ralph-loop/reset-iteration-prompt-builder.ts:23">
P1: Prompt injection vulnerability: Unescaped closing tag in XML-style prompt block</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
There was a problem hiding this comment.
1 issue found across 10 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/hooks/ralph-loop/continuation-prompt-builder.ts">
<violation number="1" location="src/hooks/ralph-loop/continuation-prompt-builder.ts:24">
P1: Unsafe string replacement with user input containing $ characters can corrupt prompts. Use a replacement function: `.replace("{{PROMPT}}", () => state.raw_task_arguments ?? state.prompt)` to prevent JavaScript from interpreting $$, $&, and $` as special substitution patterns.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
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.
Summary
--strategyfollowsralph_loop.default_strategy(with config fallback tocontinue).docs/*:--strategy=continue|reset,default_strategy, precedence rules, and practical guidance for when to use each loop style.Why this change
The Ralph loop strategy switch is not cosmetic:
continueandresetrepresent opposing operating philosophies.continue= hot-context continuity in one thread (great for HITL and debugging continuity).reset= fresh-context iteration isolation (great for longer AFK runs and reducing context-rot pressure).Given that these modes are meaningfully different, docs now explicitly call out behavioral and philosophical differences so users can intentionally choose the right loop style.
What changed
Runtime / hook behavior
--strategynow resolves via config (ralph_loop.default_strategy) rather than hardcoded behavior.Documentation
Updated:
docs/reference/features.md/ralph-loopflag docs including--strategy.continuevsresetcomparison table and usage guidance.default_strategy.docs/reference/configuration.mdralph_loopsubsection includingdefault_strategy.--strategyflag > config > fallback).Validation and real-world testing
I validated this branch against real loop usage patterns and edge paths, including both strategies:
Executed:
bun test src/hooks/ralph-loop/index.test.ts src/hooks/ralph-loop/session-reset-strategy.test.ts src/hooks/ralph-loop/reset-strategy-race-condition.test.ts src/plugin/chat-message.test.tsbun run typecheckbun run buildReferences