Compare commits

...

2 Commits

Author SHA1 Message Date
Alexander Whitestone
ec3cd2081b fix(poka-yoke): add tool fixation detection (#310)
Some checks failed
Forge CI / smoke-and-build (pull_request) Failing after 26s
Detect when the same tool is called 5+ times consecutively and inject
a nudge advising the agent to diversify its approach.

Evidence from empirical audit:
- Top marathon session (qwen, 1643 msgs): execute_code streak of 20
- Opus session (1472 msgs): terminal streak of 10

The nudge fires every 5 consecutive calls (5, 10, 15...) so it
persists without being spammy. Tracks independently in both
sequential and concurrent execution paths.
2026-04-13 10:16:11 -04:00
Alexander Whitestone
110642d86a fix(poka-yoke): add circuit breaker for error cascading (#309)
Some checks failed
Forge CI / smoke-and-build (pull_request) Failing after 28s
After 3 consecutive tool errors, inject a warning into the tool result
advising the agent to switch strategies. Escalates at 6 and 9+ errors.

Empirical data from audit:
- P(error | prev error) = 58.6% vs P(error | prev success) = 25.2%
- 2.33x cascade amplification factor
- Max observed streak: 31 consecutive errors

Intervention tiers:
- 3 errors: advisory warning (try different tool, use terminal, simplify)
- 6 errors: urgent stop (halt retries, investigate or switch)
- 9+ errors: terminal-only recovery path

Tracks errors in both sequential and concurrent execution paths.
2026-04-13 10:12:24 -04:00

View File

@@ -721,6 +721,19 @@ class AIAgent:
self._current_tool: str | None = None
self._api_call_count: int = 0
# Poka-yoke #309: Circuit breaker for error cascading
# P(error | prev was error) = 58.6% vs P(error | prev was success) = 25.2%
# After 3+ consecutive errors, inject guidance to break the cascade.
self._consecutive_tool_errors: int = 0
self._error_streak_tool_names: list = [] # track which tools are in the streak
# Poka-yoke #310: Tool fixation detection
# Marathon sessions show tool fixation - same tool called 8-25 times in a row.
# After 5 consecutive calls to the same tool, nudge the agent to diversify.
self._last_tool_name: str | None = None
self._same_tool_streak: int = 0
self._tool_fixation_threshold: int = 5
# Centralized logging — agent.log (INFO+) and errors.log (WARNING+)
# both live under ~/.hermes/logs/. Idempotent, so gateway mode
# (which creates a new AIAgent per message) won't duplicate handlers.
@@ -6238,6 +6251,12 @@ class AIAgent:
def _run_tool(index, tool_call, function_name, function_args):
"""Worker function executed in a thread."""
start = time.time()
# Poka-yoke #310: Tool fixation detection (concurrent path)
if function_name == self._last_tool_name:
self._same_tool_streak += 1
else:
self._last_tool_name = function_name
self._same_tool_streak = 1
try:
result = self._invoke_tool(function_name, function_args, effective_task_id, tool_call.id)
except Exception as tool_error:
@@ -6288,6 +6307,13 @@ class AIAgent:
if is_error:
result_preview = function_result[:200] if len(function_result) > 200 else function_result
logger.warning("Tool %s returned error (%.2fs): %s", function_name, tool_duration, result_preview)
# Circuit breaker: track consecutive errors
self._consecutive_tool_errors += 1
self._error_streak_tool_names.append(function_name)
else:
# Reset circuit breaker on success
self._consecutive_tool_errors = 0
self._error_streak_tool_names = []
if self.tool_progress_callback:
try:
@@ -6331,6 +6357,41 @@ class AIAgent:
if subdir_hints:
function_result += subdir_hints
# Circuit breaker: inject warning after 3+ consecutive errors
if self._consecutive_tool_errors >= 3:
streak_info = self._error_streak_tool_names[-self._consecutive_tool_errors:]
unique_tools = list(dict.fromkeys(streak_info))
if self._consecutive_tool_errors == 3:
cb_msg = (
f"\n\n⚠️ CIRCUIT BREAKER: You have had {self._consecutive_tool_errors} consecutive tool errors "
f"({', '.join(unique_tools)}). Errors cascade — P(error|error) is 2.33x higher than normal. "
f"Consider: (1) trying a different tool type, (2) using terminal to debug, "
f"(3) simplifying your approach, or (4) asking the user for guidance."
)
function_result += cb_msg
elif self._consecutive_tool_errors == 6:
cb_msg = (
f"\n\n🛑 CIRCUIT BREAKER: {self._consecutive_tool_errors} consecutive errors. "
f"The error cascade is severe. STOP retrying the same approach. "
f"Use terminal to investigate, or switch strategies entirely."
)
function_result += cb_msg
elif self._consecutive_tool_errors >= 9 and self._consecutive_tool_errors % 3 == 0:
cb_msg = (
f"\n\n🔴 CIRCUIT BREAKER: {self._consecutive_tool_errors} consecutive errors. "
f"Terminal is your only reliable recovery path. Use it now."
)
function_result += cb_msg
# Poka-yoke #310: Tool fixation nudge
if self._same_tool_streak >= self._tool_fixation_threshold and self._same_tool_streak % self._tool_fixation_threshold == 0:
fixation_msg = (
f"\n\n🔄 TOOL FIXATION: You have called `{function_name}` {self._same_tool_streak} times consecutively. "
f"Consider: (1) trying a different tool, (2) using `terminal` to verify your approach, "
f"(3) stepping back to reassess the task."
)
function_result += fixation_msg
# Append tool result message in order
tool_msg = {
"role": "tool",
@@ -6416,6 +6477,13 @@ class AIAgent:
self._current_tool = function_name
self._touch_activity(f"executing tool: {function_name}")
# Poka-yoke #310: Tool fixation detection
if function_name == self._last_tool_name:
self._same_tool_streak += 1
else:
self._last_tool_name = function_name
self._same_tool_streak = 1
if self.tool_progress_callback:
try:
preview = _build_tool_preview(function_name, function_args)
@@ -6609,8 +6677,14 @@ class AIAgent:
_is_error_result, _ = _detect_tool_failure(function_name, function_result)
if _is_error_result:
logger.warning("Tool %s returned error (%.2fs): %s", function_name, tool_duration, result_preview)
# Circuit breaker: track consecutive errors
self._consecutive_tool_errors += 1
self._error_streak_tool_names.append(function_name)
else:
logger.info("tool %s completed (%.2fs, %d chars)", function_name, tool_duration, len(function_result))
# Reset circuit breaker on success
self._consecutive_tool_errors = 0
self._error_streak_tool_names = []
if self.tool_progress_callback:
try:
@@ -6642,6 +6716,41 @@ class AIAgent:
if subdir_hints:
function_result += subdir_hints
# Circuit breaker: inject warning after 3+ consecutive errors
if self._consecutive_tool_errors >= 3:
streak_info = self._error_streak_tool_names[-self._consecutive_tool_errors:]
unique_tools = list(dict.fromkeys(streak_info)) # preserve order, deduplicate
if self._consecutive_tool_errors == 3:
cb_msg = (
f"\n\n⚠️ CIRCUIT BREAKER: You have had {self._consecutive_tool_errors} consecutive tool errors "
f"({', '.join(unique_tools)}). Errors cascade — P(error|error) is 2.33x higher than normal. "
f"Consider: (1) trying a different tool type, (2) using terminal to debug, "
f"(3) simplifying your approach, or (4) asking the user for guidance."
)
function_result += cb_msg
elif self._consecutive_tool_errors == 6:
cb_msg = (
f"\n\n🛑 CIRCUIT BREAKER: {self._consecutive_tool_errors} consecutive errors. "
f"The error cascade is severe. STOP retrying the same approach. "
f"Use terminal to investigate, or switch strategies entirely."
)
function_result += cb_msg
elif self._consecutive_tool_errors >= 9 and self._consecutive_tool_errors % 3 == 0:
cb_msg = (
f"\n\n🔴 CIRCUIT BREAKER: {self._consecutive_tool_errors} consecutive errors. "
f"Terminal is your only reliable recovery path. Use it now."
)
function_result += cb_msg
# Poka-yoke #310: Tool fixation nudge
if self._same_tool_streak >= self._tool_fixation_threshold and self._same_tool_streak % self._tool_fixation_threshold == 0:
fixation_msg = (
f"\n\n🔄 TOOL FIXATION: You have called `{function_name}` {self._same_tool_streak} times consecutively. "
f"Consider: (1) trying a different tool, (2) using `terminal` to verify your approach, "
f"(3) stepping back to reassess the task."
)
function_result += fixation_msg
tool_msg = {
"role": "tool",
"content": function_result,