Newer
Older
cortex-hub / tmp / swarm / scripts / swarm_aligner.py
import os
import subprocess
import time

SESSION_NAME = "jetski_swarm"

def get_windows():
    cmd = f"tmux list-windows -t {SESSION_NAME} -F '#I:#W'"
    result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
    if result.returncode != 0:
        print("Error listing windows. Is tmux running?")
        return []
    return result.stdout.strip().split('\n')

def capture_pane(window_id):
    cmd = f"tmux capture-pane -p -t {SESSION_NAME}:{window_id}"
    result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
    return result.stdout

def send_keys(window_id, keys):
    cmd = f"tmux send-keys -t {SESSION_NAME}:{window_id} {keys}"
    subprocess.run(cmd, shell=True)

def main():
    windows = get_windows()
    if not windows:
        return
        
    for win in windows:
        if not win:
            continue
        try:
            win_id, win_name = win.split(':')
        except ValueError:
            continue
            
        # Skip master and orchestrator if we want to focus on agents
        if win_name in ["master", "orchestrator"]:
            continue
            
        print(f"\n========================================")
        print(f"Targeting Window: {win_id} ({win_name})")
        print(f"========================================")
        
        while True:
            output = capture_pane(win_id)
            lines = output.split('\n')
            # Show last 10 lines
            print("\n".join(lines[-10:]))
            print("----------------------------------------")
            
            print("Commands: [u] Up, [d] Down, [e] Enter, [n] Next Agent, [q] Quit")
            print("Or type a string to send as keys.")
            
            try:
                choice = input("Choice: ").strip()
            except EOFError:
                print("No interactive terminal. Exiting.")
                return
                
            if choice == 'u':
                send_keys(win_id, "Up")
            elif choice == 'd':
                send_keys(win_id, "Down")
            elif choice == 'e':
                send_keys(win_id, "Enter")
            elif choice == 'n':
                print("Moving to next agent.")
                break
            elif choice == 'q':
                print("Quitting.")
                return
            elif choice:
                # Send text
                send_keys(win_id, f"\"{choice}\"")
                
            time.sleep(1) # Give time for screen to update

if __name__ == "__main__":
    main()