Keep repetition guard internal (#144)
Browse files* Clarify repetition guard messaging
The loop breaker is intentional recovery behavior, but the user-facing 'doom loop' phrasing reads like a crash. Rename the visible log and system hint to a repetition guard while preserving the existing detector behavior and historical SFT tag compatibility.
Constraint: Existing trajectories may still contain the old wording, so the tagger must recognize both labels.
Rejected: Remove the event entirely | users would lose visibility into why the agent changed strategy.
Confidence: high
Scope-risk: narrow
Directive: Keep user-facing recovery logs operational and non-alarming; reserve internal jargon for code, not UI events.
Tested: UV_CACHE_DIR=/tmp/uv-cache uv run --extra dev pytest tests/unit/test_doom_loop_polling.py tests/unit/test_sft_tagger.py
* Hide repetition guard from user surfaces
The repetition guard is an internal control-flow intervention, not a status event users need to act on. Remove the CLI/frontend tool_log emissions while keeping the internal corrective prompt and logger warnings.
Constraint: Historical trajectories can still contain the old doom-loop log text, so the SFT tagger test keeps compatibility for existing data.
Rejected: Show a renamed repetition-guard event | the requested behavior is that this remains fully internal and invisible in CLI/frontend surfaces.
Confidence: high
Scope-risk: narrow
Tested: UV_CACHE_DIR=/tmp/uv-cache uv run --extra dev pytest tests/unit/test_doom_loop_polling.py tests/unit/test_sft_tagger.py
* Remove dead repetition tag branch
Review caught that no user-visible tool_log will contain the new repetition-guard text after this PR. Keep historical doom-loop log compatibility only and avoid a dead future-facing branch.
Confidence: high
Scope-risk: narrow
Tested: UV_CACHE_DIR=/tmp/uv-cache uv run --extra dev pytest tests/unit/test_doom_loop_polling.py tests/unit/test_sft_tagger.py
Files changed (5)
680 unmodified lines681682683684685686687688689690691692693694695680 unmodified linessession.context_manager.add_message(Message(role="user", content=doom_prompt))await session.send_event(Event(event_type="tool_log",data={"tool": "system","log": "Doom loop detected β injecting corrective prompt",},))malformed_tool = _detect_repeated_malformed(session.context_manager.items)if malformed_tool:680 unmodified lines681682683684685686680 unmodified linessession.context_manager.add_message(Message(role="user", content=doom_prompt))malformed_tool = _detect_repeated_malformed(session.context_manager.items)if malformed_tool:
155 unmodified lines1561571581591601611621631645 unmodified lines170171172173174175176177178155 unmodified lines# Check for identical consecutive callstool_name = detect_identical_consecutive(signatures, threshold=3)if tool_name:logger.warning("Doom loop detected: %d+ identical consecutive calls to '%s'", 3, tool_name)return (f"[SYSTEM: DOOM LOOP DETECTED] You have called '{tool_name}' with the same "f"arguments multiple times in a row, getting the same result each time. "f"STOP repeating this approach β it is not working. "f"Step back and try a fundamentally different strategy. "5 unmodified linespattern = detect_repeating_sequence(signatures)if pattern:pattern_desc = " β ".join(s.name for s in pattern)logger.warning("Doom loop detected: repeating sequence [%s]", pattern_desc)return (f"[SYSTEM: DOOM LOOP DETECTED] You are stuck in a repeating cycle of tool calls: "f"[{pattern_desc}]. This pattern has repeated multiple times without progress. "f"STOP this cycle and try a fundamentally different approach. "f"Consider: breaking down the problem differently, using alternative tools, "155 unmodified lines1561571581591601611621631641651661671685 unmodified lines174175176177178179180181182155 unmodified lines# Check for identical consecutive callstool_name = detect_identical_consecutive(signatures, threshold=3)if tool_name:logger.warning("Repetition guard activated: %d+ identical consecutive calls to '%s'",3,tool_name,)return (f"[SYSTEM: REPETITION GUARD] You have called '{tool_name}' with the same "f"arguments multiple times in a row, getting the same result each time. "f"STOP repeating this approach β it is not working. "f"Step back and try a fundamentally different strategy. "5 unmodified linespattern = detect_repeating_sequence(signatures)if pattern:pattern_desc = " β ".join(s.name for s in pattern)logger.warning("Repetition guard activated: repeating sequence [%s]", pattern_desc)return (f"[SYSTEM: REPETITION GUARD] You are stuck in a repeating cycle of tool calls: "f"[{pattern_desc}]. This pattern has repeated multiple times without progress. "f"STOP this cycle and try a fundamentally different approach. "f"Consider: breaking down the problem differently, using alternative tools, "
305 unmodified lines306307308309310311312313305 unmodified lines# ββ Doom-loop detection ββdoom_prompt = check_for_doom_loop(messages)if doom_prompt:logger.warning("Research sub-agent doom loop detected at iteration %d", _iteration)await _log("Doom loop detected β injecting corrective prompt")messages.append(Message(role="user", content=doom_prompt))# ββ Context budget: warn at 75%, hard-stop at 95% ββ305 unmodified lines306307308309310311312313314315305 unmodified lines# ββ Doom-loop detection ββdoom_prompt = check_for_doom_loop(messages)if doom_prompt:logger.warning("Research sub-agent repetition guard activated at iteration %d",_iteration,)messages.append(Message(role="user", content=doom_prompt))# ββ Context budget: warn at 75%, hard-stop at 95% ββ
4 unmodified lines56789101154 unmodified lines666768697071727 unmodified lines808182838485864 unmodified lineslong-running job with `bash sleep 300 && wc -l output` four times in arow. The arguments were byte-identical, but the results moved (27210 β36454 β 45770 β 55138 β actual progress). The detector hashed args onlyand false-fired DOOM LOOP, which made the agent abandon perfectly validpolling.After the fix the signature includes the tool result hash, so identical54 unmodified lines]prompt = check_for_doom_loop(msgs)assert prompt is not Noneassert "DOOM LOOP" in promptassert "bash" in prompt7 unmodified lines]prompt = check_for_doom_loop(msgs)assert prompt is not Noneassert "DOOM LOOP" in promptassert "write" in prompt4 unmodified lines56789101154 unmodified lines666768697071727 unmodified lines808182838485864 unmodified lineslong-running job with `bash sleep 300 && wc -l output` four times in arow. The arguments were byte-identical, but the results moved (27210 β36454 β 45770 β 55138 β actual progress). The detector hashed args onlyand false-fired the repetition guard, which made the agent abandon perfectly validpolling.After the fix the signature includes the tool result hash, so identical54 unmodified lines]prompt = check_for_doom_loop(msgs)assert prompt is not Noneassert "REPETITION GUARD" in promptassert "bash" in prompt7 unmodified lines]prompt = check_for_doom_loop(msgs)assert prompt is not Noneassert "REPETITION GUARD" in promptassert "write" in prompt
78 unmodified lines7980818283848578 unmodified linesdef test_outcome_doom_loop_and_context():events = [_ev("tool_log", {"tool": "system", "log": "Doom loop detected β injecting corrective prompt"}),_ev("compacted", {"old_tokens": 100, "new_tokens": 50}),_ev("turn_complete", {"history_size": 10}),]78 unmodified lines7980818283848578 unmodified linesdef test_outcome_doom_loop_and_context():events = [_ev("tool_log", {"tool": "system", "log": "Doom loop detected"}),_ev("compacted", {"old_tokens": 100, "new_tokens": 50}),_ev("turn_complete", {"history_size": 10}),]