---
issue: 156
title: Port Bot Spawning Logic
analyzed: 2025-10-21T13:48:23Z
estimated_hours: 12-16
parallelization_factor: 2.0
---

# Parallel Work Analysis: Issue #156

## Overview
Extract Python bot spawning logic from `slide-system` and port it to the `bot-spawner` Express service. This involves copying bot code, setting up Python environment, implementing Express API endpoints, and testing bot spawning both locally and on Railway.

## Parallel Streams

### Stream A: Python Bot Environment Setup
**Scope**: Copy bot code, create Dockerfile with Python dependencies, Playwright installation
**Files**:
- `apps/bot-spawner/bots/screenshare-bot/**` (new, copied from slide-system)
- `apps/bot-spawner/bots/tool-forwarder/**` (new, copied from slide-system)
- `apps/bot-spawner/Dockerfile` (new)
- `apps/bot-spawner/.dockerignore` (if needed)
**Agent Type**: backend-specialist
**Can Start**: immediately
**Estimated Hours**: 4-5
**Dependencies**: none

**Tasks**:
- Copy `apps/slide-system/bots/screenshare-bot` to `apps/bot-spawner/bots/`
- Copy `apps/slide-system/bots/tool-forwarder` to `apps/bot-spawner/bots/`
- Create Dockerfile with Ubuntu 24.04, Python 3.11, uv, Playwright
- Install bot dependencies via requirements.txt
- Install Playwright browsers with --with-deps

### Stream B: Express API Routes Implementation
**Scope**: Implement bot spawn/kill/status/logs Express routes
**Files**:
- `apps/bot-spawner/src/routes/spawn-livekit.ts` (new)
- `apps/bot-spawner/src/routes/spawn-tool-forwarder.ts` (new)
- `apps/bot-spawner/src/routes/kill.ts` (new)
- `apps/bot-spawner/src/routes/status.ts` (new)
- `apps/bot-spawner/src/routes/logs.ts` (new)
- `apps/bot-spawner/src/main.ts` (modify to register routes)
**Agent Type**: backend-specialist
**Can Start**: immediately
**Estimated Hours**: 6-8
**Dependencies**: none (can develop alongside Stream A)

**Tasks**:
- Extract spawn logic from `apps/slide-system/src/steps/bot-spawn-livekit.step.ts`
- Implement POST `/api/bots/screenshare/spawn-livekit` endpoint
- Implement POST `/api/bots/tool-forwarder/spawn` endpoint
- Implement POST `/api/bots/:id/kill` endpoint
- Implement GET `/api/bots/:id/status` endpoint
- Implement GET `/api/bots/:id/logs` endpoint
- Add detached process spawning with log capture
- Add Supabase integration for active_bots table

### Stream C: Testing & Deployment
**Scope**: Local testing, Railway deployment, diagnostic script testing
**Files**:
- Railway configuration (already exists)
- Test scripts (new)
**Agent Type**: backend-specialist
**Can Start**: after Streams A & B complete
**Estimated Hours**: 4-5
**Dependencies**: Stream A (Python env), Stream B (API routes)

**Tasks**:
- Test spawn endpoint locally with curl
- Verify bot joins LiveKit room locally
- Deploy diagnostic script to Railway first
- Verify asyncio import works on Railway
- Deploy real bots to Railway
- Test all endpoints on Railway
- Verify bot logs captured correctly
- Confirm bot survives service restart (detached process)

## Coordination Points

### Shared Files
- `apps/bot-spawner/src/main.ts` - Stream B modifies this to register routes
- `apps/bot-spawner/package.json` - May need uuid dependency (Stream B)

### Sequential Requirements
1. **Streams A & B can run in parallel** - They work on completely independent files
2. **Stream C must wait for A & B** - Requires both Python environment and API routes to be complete
3. **Dockerfile (Stream A) must be complete before Railway deployment (Stream C)**

## Conflict Risk Assessment
- **Low Risk**: Streams A & B work on completely different directories
- **Minimal Overlap**: Only `main.ts` is shared, but changes are isolated (just route registration)
- **Clean Separation**: Python bot code vs Express routes vs testing

## Parallelization Strategy

**Recommended Approach**: hybrid

**Execution Plan**:
1. Launch Streams A & B simultaneously (independent work)
2. Stream A focuses on Python environment (Dockerfile, bot code)
3. Stream B focuses on Express routes (TypeScript API implementation)
4. When both A & B complete, start Stream C (testing & deployment)

**Coordination**:
- Stream B can develop/test routes with mock subprocess spawning
- Stream A can validate Dockerfile builds independently
- Merge both streams before Stream C testing

## Expected Timeline

With parallel execution:
- Wall time: 8-10 hours (max of A & B, plus C)
  - Streams A & B: 6-8h (parallel, take longest stream time)
  - Stream C: 4-5h (sequential after A & B)
- Total work: 14-18 hours
- Efficiency gain: 40-45% time savings

Without parallel execution:
- Wall time: 14-18 hours (sequential sum)

## Notes

**Critical Considerations**:
- This is a **debugging strategy**, not a fix attempt
- Success = visibility into WHERE bots hang, not necessarily working bots
- Heavy instrumentation required in spawn endpoints
- Deploy diagnostic script BEFORE real bots on Railway
- Bot code remains unchanged (validates environment-agnostic assumption)

**Dockerfile Strategy**:
- We previously removed Dockerfile to use Railpack
- **Decision needed**: Do we add Dockerfile back for Python support, or can Railpack handle Python+Node hybrid?
- If Railpack can't handle Python, we need Dockerfile (Stream A blocks everything)

**Risk**: Railway internal networking must work for slide-system → bot-spawner HTTP calls (tested in Task #155/157)
