/ scripts / run_day1_with_agents.sh
run_day1_with_agents.sh
  1  #!/bin/bash
  2  
  3  # ECHO Day 1 - Full Multi-Process Simulation
  4  #
  5  # This script runs all 9 agents as separate background processes
  6  # and executes a Day 1 company introduction scenario with real
  7  # agent coordination via Redis pub/sub.
  8  #
  9  # Each agent's logs are saved to separate files.
 10  
 11  set -e
 12  
 13  ECHO_ROOT="/Users/pranav/Documents/echo"
 14  LOGS_DIR="$ECHO_ROOT/logs/day1_$(date +%Y%m%d_%H%M%S)"
 15  PIDS_FILE="$ECHO_ROOT/.day1_pids"
 16  
 17  # Colors
 18  GREEN='\033[0;32m'
 19  BLUE='\033[0;34m'
 20  YELLOW='\033[1;33m'
 21  RED='\033[0;31m'
 22  NC='\033[0m'
 23  
 24  echo "================================================================================"
 25  echo "ECHO Day 1 - Company Introduction Simulation"
 26  echo "================================================================================"
 27  echo ""
 28  
 29  # Create logs directory
 30  mkdir -p "$LOGS_DIR"
 31  echo -e "${GREEN}✓ Created logs directory: $LOGS_DIR${NC}"
 32  
 33  # Clean up function
 34  cleanup() {
 35      echo ""
 36      echo "================================================================================"
 37      echo "Stopping all agents..."
 38      echo "================================================================================"
 39  
 40      if [ -f "$PIDS_FILE" ]; then
 41          while read pid; do
 42              if kill -0 "$pid" 2>/dev/null; then
 43                  echo "Stopping process $pid"
 44                  kill "$pid" 2>/dev/null || true
 45              fi
 46          done < "$PIDS_FILE"
 47          rm "$PIDS_FILE"
 48      fi
 49  
 50      echo ""
 51      echo -e "${GREEN}All agents stopped. Logs saved to: $LOGS_DIR${NC}"
 52      echo ""
 53      echo "View logs:"
 54      echo "  tail -f $LOGS_DIR/ceo.log"
 55      echo "  tail -f $LOGS_DIR/cto.log"
 56      echo "  ... etc"
 57      echo ""
 58  }
 59  
 60  trap cleanup EXIT INT TERM
 61  
 62  # Check prerequisites
 63  echo "Checking prerequisites..."
 64  if ! pg_isready -h localhost >/dev/null 2>&1; then
 65      echo -e "${RED}✗ PostgreSQL not running${NC}"
 66      exit 1
 67  fi
 68  echo -e "${GREEN}✓ PostgreSQL running${NC}"
 69  
 70  if ! redis-cli ping >/dev/null 2>&1; then
 71      echo -e "${RED}✗ Redis not running${NC}"
 72      exit 1
 73  fi
 74  echo -e "${GREEN}✓ Redis running${NC}"
 75  
 76  echo ""
 77  echo "================================================================================"
 78  echo "Starting ECHO Agents (9 separate processes)"
 79  echo "================================================================================"
 80  echo ""
 81  
 82  # Agent list - simple arrays (bash 3.2 compatible)
 83  AGENT_NAMES=(ceo cto chro operations_head product_manager senior_architect uiux_engineer senior_developer test_lead)
 84  AGENT_TITLES=(
 85      "Chief Executive Officer"
 86      "Chief Technology Officer"
 87      "Chief Human Resources Officer"
 88      "Operations Head"
 89      "Product Manager"
 90      "Senior Architect"
 91      "UI/UX Engineer"
 92      "Senior Developer"
 93      "Test Lead"
 94  )
 95  
 96  # Clean previous PID file
 97  rm -f "$PIDS_FILE"
 98  
 99  # Start each agent as background process with daemon mode simulation
100  for i in "${!AGENT_NAMES[@]}"; do
101      agent="${AGENT_NAMES[$i]}"
102      title="${AGENT_TITLES[$i]}"
103      AGENT_DIR="$ECHO_ROOT/apps/$agent"
104      AGENT_BIN="$AGENT_DIR/$agent"
105      LOG_FILE="$LOGS_DIR/${agent}.log"
106  
107      echo -e "${BLUE}Starting $agent ($title)...${NC}"
108  
109      # Start agent with continuous input stream (keeps it alive)
110      # This is a workaround - agents expect stdin, so we pipe yes output to keep them running
111      (
112          cd "$AGENT_DIR"
113          # Send initialize message first, then keep sending empty lines
114          {
115              echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"day1-simulation","version":"1.0"}}}'
116              sleep 1
117              # Keep process alive with periodic pings
118              while true; do
119                  echo '{"jsonrpc":"2.0","method":"ping"}'
120                  sleep 5
121              done
122          } | "$AGENT_BIN" > "$LOG_FILE" 2>&1
123      ) &
124  
125      PID=$!
126      echo "$PID" >> "$PIDS_FILE"
127      echo -e "${GREEN}  ✓ Started with PID: $PID${NC}"
128      echo -e "     Log: $LOG_FILE"
129      echo ""
130  
131      # Wait a bit before starting next agent
132      sleep 0.5
133  done
134  
135  echo "================================================================================"
136  echo -e "${GREEN}All 9 agents started successfully!${NC}"
137  echo "================================================================================"
138  echo ""
139  
140  # Wait for agents to fully initialize
141  echo "Waiting for agents to initialize..."
142  sleep 3
143  echo ""
144  
145  # Verify agents are running
146  echo "================================================================================"
147  echo "Agent Status Check"
148  echo "================================================================================"
149  echo ""
150  
151  RUNNING=0
152  while read pid; do
153      if kill -0 "$pid" 2>/dev/null; then
154          RUNNING=$((RUNNING + 1))
155      fi
156  done < "$PIDS_FILE"
157  
158  echo -e "${GREEN}✓ $RUNNING/9 agents running${NC}"
159  echo ""
160  
161  if [ $RUNNING -lt 9 ]; then
162      echo -e "${YELLOW}⚠ Some agents failed to start. Check logs.${NC}"
163      echo ""
164  fi
165  
166  # Show live logs
167  echo "================================================================================"
168  echo "Live Agent Logs (Press Ctrl+C to stop)"
169  echo "================================================================================"
170  echo ""
171  echo "Opening log viewer in 3 seconds..."
172  echo "You can also manually view logs:"
173  echo ""
174  for agent in "${AGENT_NAMES[@]}"; do
175      echo "  tail -f $LOGS_DIR/${agent}.log"
176  done
177  echo ""
178  
179  sleep 3
180  
181  # Show logs from all agents using multitail if available, otherwise use tail
182  if command -v multitail >/dev/null 2>&1; then
183      multitail \
184          -l "tail -f $LOGS_DIR/ceo.log" \
185          -l "tail -f $LOGS_DIR/cto.log" \
186          -l "tail -f $LOGS_DIR/chro.log" \
187          -l "tail -f $LOGS_DIR/operations_head.log" \
188          -l "tail -f $LOGS_DIR/product_manager.log" \
189          -l "tail -f $LOGS_DIR/senior_architect.log" \
190          -l "tail -f $LOGS_DIR/uiux_engineer.log" \
191          -l "tail -f $LOGS_DIR/senior_developer.log" \
192          -l "tail -f $LOGS_DIR/test_lead.log"
193  else
194      # Fallback: show one log file
195      echo -e "${YELLOW}Note: Install 'multitail' to view all logs simultaneously${NC}"
196      echo "      brew install multitail"
197      echo ""
198      echo "Showing CEO log (use other terminals to view other agents):"
199      echo ""
200      tail -f "$LOGS_DIR/ceo.log"
201  fi