Spaces:
Sleeping
Sleeping
God Claude Opus 4.6 commited on
Commit Β·
16f7d4e
1
Parent(s): 5a4bb8f
Fix conversation-loop mechanism issues
Browse files1. Fix inverted discussion loop detection logic:
- The counter was resetting when CC was NOT busy (wrong!)
- Now only resets when task assigned or child not alive
- Properly detects when agents discuss without assigning tasks while CC is idle
2. Add stuck CC detection and termination:
- Track time since last NEW CC output (not just turn count)
- Show warning when CC has no new output for 180s+
- Add [ACTION: terminate_cc] to kill stuck CC processes
- Reset staleness tracking when CC gets new output
These fixes should prevent agents from getting stuck in discussion loops
and provide a way to recover from truly stuck Claude Code processes.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- scripts/conversation-loop.py +46 -5
scripts/conversation-loop.py
CHANGED
|
@@ -311,6 +311,25 @@ def action_send_bubble(text):
|
|
| 311 |
return f"Error: {e}"
|
| 312 |
|
| 313 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
# ββ Claude Code Action (THE STAR) βββββββββββββββββββββββββββββββββββββββββββββ
|
| 315 |
|
| 316 |
CLAUDE_WORK_DIR = "/tmp/claude-workspace"
|
|
@@ -433,6 +452,8 @@ cc_status = {"running": False, "task": "", "result": "", "assigned_by": "", "sta
|
|
| 433 |
cc_lock = threading.Lock()
|
| 434 |
_last_cc_snapshot = "" # tracks whether CC output changed between turns
|
| 435 |
_cc_stale_count = 0 # how many turns CC output hasn't changed
|
|
|
|
|
|
|
| 436 |
|
| 437 |
|
| 438 |
def cc_submit_task(task, assigned_by, ctx):
|
|
@@ -446,6 +467,8 @@ def cc_submit_task(task, assigned_by, ctx):
|
|
| 446 |
cc_status["assigned_by"] = assigned_by
|
| 447 |
cc_status["started"] = time.time()
|
| 448 |
cc_live_lines.clear()
|
|
|
|
|
|
|
| 449 |
|
| 450 |
enriched = enrich_task_with_context(task, ctx)
|
| 451 |
print(f"[TASK] {assigned_by} assigned to Claude Code ({len(enriched)} chars)...")
|
|
@@ -464,7 +487,7 @@ def cc_submit_task(task, assigned_by, ctx):
|
|
| 464 |
|
| 465 |
def cc_get_live_status():
|
| 466 |
"""Get CC's current status and recent output for agents to discuss."""
|
| 467 |
-
global _last_cc_snapshot, _cc_stale_count
|
| 468 |
with cc_lock:
|
| 469 |
if cc_status["running"]:
|
| 470 |
elapsed = int(time.time() - cc_status["started"])
|
|
@@ -477,10 +500,18 @@ def cc_get_live_status():
|
|
| 477 |
else:
|
| 478 |
_cc_stale_count = 0
|
| 479 |
_last_cc_snapshot = snapshot
|
|
|
|
| 480 |
stale_note = f"\n(No new output for {_cc_stale_count} turns β discuss other topics while waiting)" if _cc_stale_count >= 2 else ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 481 |
return (f"π¨ Claude Code is WORKING (assigned by {cc_status['assigned_by']}, {elapsed}s ago)\n"
|
| 482 |
f"Task: {cc_status['task']}\n"
|
| 483 |
-
f"Recent output:\n{recent}{stale_note}")
|
| 484 |
elif cc_status["result"]:
|
| 485 |
return (f"β
Claude Code FINISHED (assigned by {cc_status['assigned_by']})\n"
|
| 486 |
f"Result:\n{cc_status['result'][:1500]}")
|
|
@@ -800,6 +831,11 @@ def parse_and_execute_turn(raw_text, ctx):
|
|
| 800 |
result = action_send_bubble(bubble_match.group(1).strip())
|
| 801 |
results.append({"action": "send_bubble", "result": result})
|
| 802 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 803 |
# Activate deferred cooldown
|
| 804 |
if _pending_cooldown:
|
| 805 |
last_rebuild_trigger_at = time.time()
|
|
@@ -809,12 +845,16 @@ def parse_and_execute_turn(raw_text, ctx):
|
|
| 809 |
# Update discussion loop counter
|
| 810 |
cc_busy = cc_status["running"]
|
| 811 |
child_alive = child_state["alive"] or child_state["stage"] == "RUNNING"
|
| 812 |
-
|
| 813 |
-
|
|
|
|
|
|
|
|
|
|
| 814 |
if _discussion_loop_count > 0:
|
| 815 |
-
print(f"[LOOP-DISCUSS] Reset (task assigned or
|
| 816 |
_discussion_loop_count = 0
|
| 817 |
else:
|
|
|
|
| 818 |
_discussion_loop_count += 1
|
| 819 |
if _discussion_loop_count >= 2:
|
| 820 |
print(f"[LOOP-DISCUSS] WARNING: {_discussion_loop_count} consecutive discussion-only turns with CC IDLE and child alive!")
|
|
@@ -928,6 +968,7 @@ AVAILABLE ACTIONS:
|
|
| 928 |
[ACTION: delete_env:KEY] β Delete an environment variable
|
| 929 |
[ACTION: send_bubble:MESSAGE] β Send a message to {CHILD_NAME}
|
| 930 |
[ACTION: create_child] β Create {CHILD_NAME} (if not born)
|
|
|
|
| 931 |
|
| 932 |
HF SPACES TECHNICAL NOTES:
|
| 933 |
- Docker containers MUST bind port 7860.
|
|
|
|
| 311 |
return f"Error: {e}"
|
| 312 |
|
| 313 |
|
| 314 |
+
def action_terminate_cc():
|
| 315 |
+
"""Terminate a stuck Claude Code process. Use when CC has been running with no new output for too long."""
|
| 316 |
+
global cc_status, cc_live_lines, _cc_stale_count, _last_cc_snapshot, _last_cc_output_time
|
| 317 |
+
with cc_lock:
|
| 318 |
+
if not cc_status["running"]:
|
| 319 |
+
return "Claude Code is not running. Nothing to terminate."
|
| 320 |
+
# Mark as not running - the background thread will eventually finish
|
| 321 |
+
cc_status["running"] = False
|
| 322 |
+
cc_status["result"] = "(TERMINATED by agent - task was stuck)"
|
| 323 |
+
# Reset staleness tracking
|
| 324 |
+
_cc_stale_count = 0
|
| 325 |
+
_last_cc_snapshot = ""
|
| 326 |
+
_last_cc_output_time = 0
|
| 327 |
+
cc_live_lines.clear()
|
| 328 |
+
assigned_by = cc_status["assigned_by"]
|
| 329 |
+
task = cc_status["task"]
|
| 330 |
+
return f"Terminated stuck Claude Code task (assigned by {assigned_by}). The task was: {task[:100]}..."
|
| 331 |
+
|
| 332 |
+
|
| 333 |
# ββ Claude Code Action (THE STAR) βββββββββββββββββββββββββββββββββββββββββββββ
|
| 334 |
|
| 335 |
CLAUDE_WORK_DIR = "/tmp/claude-workspace"
|
|
|
|
| 452 |
cc_lock = threading.Lock()
|
| 453 |
_last_cc_snapshot = "" # tracks whether CC output changed between turns
|
| 454 |
_cc_stale_count = 0 # how many turns CC output hasn't changed
|
| 455 |
+
_last_cc_output_time = 0.0 # timestamp of last NEW CC output line
|
| 456 |
+
CC_STUCK_TIMEOUT = 180 # seconds with no new output before CC is considered STUCK
|
| 457 |
|
| 458 |
|
| 459 |
def cc_submit_task(task, assigned_by, ctx):
|
|
|
|
| 467 |
cc_status["assigned_by"] = assigned_by
|
| 468 |
cc_status["started"] = time.time()
|
| 469 |
cc_live_lines.clear()
|
| 470 |
+
global _last_cc_output_time
|
| 471 |
+
_last_cc_output_time = time.time() # Initialize to now, will update as we get output
|
| 472 |
|
| 473 |
enriched = enrich_task_with_context(task, ctx)
|
| 474 |
print(f"[TASK] {assigned_by} assigned to Claude Code ({len(enriched)} chars)...")
|
|
|
|
| 487 |
|
| 488 |
def cc_get_live_status():
|
| 489 |
"""Get CC's current status and recent output for agents to discuss."""
|
| 490 |
+
global _last_cc_snapshot, _cc_stale_count, _last_cc_output_time
|
| 491 |
with cc_lock:
|
| 492 |
if cc_status["running"]:
|
| 493 |
elapsed = int(time.time() - cc_status["started"])
|
|
|
|
| 500 |
else:
|
| 501 |
_cc_stale_count = 0
|
| 502 |
_last_cc_snapshot = snapshot
|
| 503 |
+
_last_cc_output_time = time.time() # Update when we see NEW output
|
| 504 |
stale_note = f"\n(No new output for {_cc_stale_count} turns β discuss other topics while waiting)" if _cc_stale_count >= 2 else ""
|
| 505 |
+
|
| 506 |
+
# Detect STUCK CC: been running with no new output for too long
|
| 507 |
+
time_since_new_output = int(time.time() - _last_cc_output_time) if _last_cc_output_time > 0 else elapsed
|
| 508 |
+
stuck_note = ""
|
| 509 |
+
if time_since_new_output > CC_STUCK_TIMEOUT and _cc_stale_count >= 4:
|
| 510 |
+
stuck_note = f"\nβ οΈ STUCK: No new output for {time_since_new_output}s! Consider terminating and re-assigning."
|
| 511 |
+
|
| 512 |
return (f"π¨ Claude Code is WORKING (assigned by {cc_status['assigned_by']}, {elapsed}s ago)\n"
|
| 513 |
f"Task: {cc_status['task']}\n"
|
| 514 |
+
f"Recent output:\n{recent}{stale_note}{stuck_note}")
|
| 515 |
elif cc_status["result"]:
|
| 516 |
return (f"β
Claude Code FINISHED (assigned by {cc_status['assigned_by']})\n"
|
| 517 |
f"Result:\n{cc_status['result'][:1500]}")
|
|
|
|
| 831 |
result = action_send_bubble(bubble_match.group(1).strip())
|
| 832 |
results.append({"action": "send_bubble", "result": result})
|
| 833 |
|
| 834 |
+
# 5. Handle [ACTION: terminate_cc] (terminate stuck Claude Code)
|
| 835 |
+
if re.search(r'\[ACTION:\s*terminate_cc\]', raw_text):
|
| 836 |
+
result = action_terminate_cc()
|
| 837 |
+
results.append({"action": "terminate_cc", "result": result})
|
| 838 |
+
|
| 839 |
# Activate deferred cooldown
|
| 840 |
if _pending_cooldown:
|
| 841 |
last_rebuild_trigger_at = time.time()
|
|
|
|
| 845 |
# Update discussion loop counter
|
| 846 |
cc_busy = cc_status["running"]
|
| 847 |
child_alive = child_state["alive"] or child_state["stage"] == "RUNNING"
|
| 848 |
+
# Reset counter when task assigned (progress!) or child not alive (can't work on dead child)
|
| 849 |
+
# DO NOT reset when CC is busy - that's when agents should be discussing while waiting
|
| 850 |
+
# DO NOT reset when CC is idle - that's exactly when we want to detect discussion loops
|
| 851 |
+
if task_assigned or not child_alive:
|
| 852 |
+
# Reset counter if task assigned or child not alive
|
| 853 |
if _discussion_loop_count > 0:
|
| 854 |
+
print(f"[LOOP-DISCUSS] Reset (task assigned or child not alive)")
|
| 855 |
_discussion_loop_count = 0
|
| 856 |
else:
|
| 857 |
+
# Increment when: CC is idle AND child is alive AND no task assigned (potential discussion loop)
|
| 858 |
_discussion_loop_count += 1
|
| 859 |
if _discussion_loop_count >= 2:
|
| 860 |
print(f"[LOOP-DISCUSS] WARNING: {_discussion_loop_count} consecutive discussion-only turns with CC IDLE and child alive!")
|
|
|
|
| 968 |
[ACTION: delete_env:KEY] β Delete an environment variable
|
| 969 |
[ACTION: send_bubble:MESSAGE] β Send a message to {CHILD_NAME}
|
| 970 |
[ACTION: create_child] β Create {CHILD_NAME} (if not born)
|
| 971 |
+
[ACTION: terminate_cc] β Terminate a STUCK Claude Code process (use when CC has no new output for 180s+)
|
| 972 |
|
| 973 |
HF SPACES TECHNICAL NOTES:
|
| 974 |
- Docker containers MUST bind port 7860.
|