diff --git a/swarm_framework/orchestrator.py b/swarm_framework/orchestrator.py index ab9546f..1aa70e2 100644 --- a/swarm_framework/orchestrator.py +++ b/swarm_framework/orchestrator.py @@ -51,55 +51,57 @@ "last_output": "" }) + # Capture output for prompt detection and hanging check + current_output = "" + try: + cap_cmd = f"tmux capture-pane -p -t {SESSION_NAME}:{agent}" + result = subprocess.run(cap_cmd, shell=True, capture_output=True, text=True, check=True) + current_output = result.stdout + + # Check for manual prompts in Orchestrator (always) + if agent == "grid.0" or agent == "orchestrator": + lines = current_output.split('\n') + last_snippet = "\n".join(lines[-10:]) if len(lines) > 10 else current_output + + if "1." in last_snippet and "2." in last_snippet: + if not state.get("prompted"): + print(f"Detected prompt in Orchestrator.") + notify_master_prompt("orchestrator", current_output) + state["prompted"] = True + except Exception as e: + print(f"Error capturing output for {agent}: {e}") + continue + # Check if we recently sent a command is_busy = (now - state["last_cmd_time"]) < BUSY_THRESHOLD print(f"Debug [check_hanging]: Agent: {agent}, is_busy: {is_busy}, now: {now}, last_cmd_time: {state['last_cmd_time']}") if is_busy: - # Capture output - try: - cap_cmd = f"tmux capture-pane -p -t {SESSION_NAME}:{agent}" - result = subprocess.run(cap_cmd, shell=True, capture_output=True, text=True, check=True) - current_output = result.stdout - - # Check for manual prompts in Orchestrator - if agent == "grid.0" or agent == "orchestrator": - lines = current_output.split('\n') - last_snippet = "\n".join(lines[-5:]) if len(lines) > 5 else current_output - - if "1." in last_snippet and "2." in last_snippet: - if not state.get("prompted"): - print(f"Detected prompt in Orchestrator.") - notify_master_prompt("orchestrator", current_output) - state["prompted"] = True - - print(f"Debug [check_hanging]: Agent: {agent}, current_output len: {len(current_output)}, prev_output len: {len(state['last_output'])}") - - if current_output != state["last_output"]: - state["last_output"] = current_output - state["last_output_time"] = now - state["prompted"] = False # Reset prompt flag on change - print(f"Debug [check_hanging]: Agent: {agent}, output changed.") + print(f"Debug [check_hanging]: Agent: {agent}, current_output len: {len(current_output)}, prev_output len: {len(state['last_output'])}") + + if current_output != state["last_output"]: + state["last_output"] = current_output + state["last_output_time"] = now + state["prompted"] = False # Reset prompt flag on change + print(f"Debug [check_hanging]: Agent: {agent}, output changed.") + else: + # No change + silence_duration = now - state["last_output_time"] + print(f"Debug [check_hanging]: Agent: {agent}, silence_duration: {silence_duration}") + if silence_duration > HANGING_THRESHOLD: + print(f"Detected hanging agent: {agent}") + notify_master(agent, current_output) + # Reset to avoid spamming + state["last_cmd_time"] = 0 - else: - # No change - silence_duration = now - state["last_output_time"] - print(f"Debug [check_hanging]: Agent: {agent}, silence_duration: {silence_duration}") - if silence_duration > HANGING_THRESHOLD: - print(f"Detected hanging agent: {agent}") - notify_master(agent, current_output) - # Reset to avoid spamming - state["last_cmd_time"] = 0 - - except Exception as e: - print(f"Error checking agent {agent}: {e}") def notify_master(agent, last_output): print(f"Notifying master about hanging agent {agent}...") # Extract last few lines of output lines = last_output.split('\n') - last_snippet = "\n".join(lines[-5:]) if len(lines) > 5 else last_output + last_snippet = "\n".join(lines[-10:]) if len(lines) > 10 else last_output + message = f"\n[System] Warning: Agent {agent} seems to be hanging.\nLast output snippet:\n---\n{last_snippet}\n---\nWhat should we do?" @@ -115,7 +117,8 @@ def notify_master_prompt(agent, last_output): print(f"Notifying master about prompt in agent {agent}...") lines = last_output.split('\n') - last_snippet = "\n".join(lines[-5:]) if len(lines) > 5 else last_output + last_snippet = "\n".join(lines[-10:]) if len(lines) > 10 else last_output + message = f"\n[System] Orchestrator Brain needs input.\nPrompt snippet:\n---\n{last_snippet}\n---\nPlease reply to continue."