Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

docs: update memory - Phase 3 complete, detail Phases 4-7

Marioee8f2c5fe26a
diff --git a/memory/2026-02-28.md b/memory/2026-02-28.md
index dbbb8aca..5ac284af 100644
--- a/memory/2026-02-28.md
+++ b/memory/2026-02-28.md
@@ -77,13 +77,14 @@ Dashboard (9867) manages:
## Branch Status
- **Branch**: `feat/make-cli-useful`
+- **Latest commit**: `01d601e` (rebased on top of b116bf4)
- **Recent commits**:
+ - `5256b8a` - feat: integrate PortAllocator into orchestrator Launch() (Phase 3) ✅ JUST PUSHED
+ - `b116bf4` - chore: disable fenced-code-language lint rule
+ - `ddf8fff` - docs: consolidate roadmap - Phase 2.5 (port allocation) complete
- `a0b527e` - feat: add configurable port ranges for instance allocation
- - `ddfa0ed` - docs: update memory with Phase 2 completion and Phase 3-5 planning
- `a4ec3ee` - feat: update handlers to ensure Chrome initialization (Phase 2 Part 2)
- `56491d2` - feat: implement Chrome initialization (Phase 2 Part 1)
- - `1130a3f` - docs: update to reflect Chrome initialization on instance creation
- - `bdccfbe` - feat: add lazy browser initialization infrastructure to Bridge
## Implementation Progress
@@ -152,83 +153,153 @@ Dashboard (9867) manages:
- ✅ All 195+ tests passing
- ✅ All pre-commit checks passing
-### 📋 Phase 3: Auto-Port Allocation in Orchestrator (NEXT - Estimated 1-2 commits)
+### ✅ Phase 3: Auto-Port Allocation in Orchestrator (COMPLETED - Commit 5256b8a)
-**Goal**: Use PortAllocator when launching instances instead of requiring manual port specification
+**What was done:**
+1. **Updated Launch()** to auto-allocate ports
+ - If port is empty string or "0", calls `o.portAllocator.AllocatePort()`
+ - Proper lock management for concurrent allocations
+ - Returns Instance with allocated port
+
+2. **Updated handlers** to support optional port
+ - `handleStartByID()` - port is optional, auto-allocates if not provided
+ - `handleLaunchByName()` - port is optional, auto-allocates if not provided
+ - Both handlers pass empty port to Launch() for auto-allocation
+
+3. **Port release on shutdown**
+ - `markStopped()` calls `portAllocator.ReleasePort(port)`
+ - Converts port string to int and releases back to allocator
+ - Debug logging tracks port lifecycle
+ - Enables port reuse for new instances
+
+4. **Fixed auto-launch in dashboard**
+ - Removed hardcoded defaultPort="9867" (was colliding with orchestrator)
+ - Now allows auto-allocation for default instance
+ - Uses `inst.Port` in logs instead of variable (accurate after allocation)
+
+**Behavior Flow:**
+```
+POST /instances/launch {}
+ → Launch("profile", "", headless=true)
+ → Allocates port 9868
+ → Returns {id, port: 9868, ...}
+
+POST /instances/launch {}
+ → Launch("profile2", "", headless=true)
+ → Allocates port 9869
+ → Returns {id, port: 9869, ...}
+
+POST /instances/9869/stop
+ → markStopped() → ReleasePort(9869)
+
+POST /instances/launch {}
+ → Launch("profile3", "", headless=true)
+ → Allocates port 9869 (re-used)
+ → Returns {id, port: 9869, ...}
+```
+
+**Testing:**
+- All 195+ tests passing
+- Orchestrator tests verify port tracking
+- No regressions in existing functionality
+- Thread-safety validated via concurrent tests
+
+## What's Complete (Ready for Production Use)
+
+✅ **Infrastructure Phase** (Phases 1-2.5 + 3)
+- Bridge lazy initialization infrastructure
+- Chrome initialization functions in bridge/init.go
+- Handler integration with ensureChrome()
+- PortAllocator system with env var configuration
+- Auto-allocation of ports when creating instances
+- Port release on instance shutdown
+
+✅ **Current Capability**
+Users can now:
+```bash
+# Create 3 instances with auto-allocated ports
+curl -X POST http://localhost:9867/instances/launch -d '{"name":"work"}'
+# Returns: {id: "work-9868", port: "9868", ...}
+
+curl -X POST http://localhost:9867/instances/launch -d '{"name":"scrape"}'
+# Returns: {id: "scrape-9869", port: "9869", ...}
+
+curl -X POST http://localhost:9867/instances/launch -d '{"name":"test"}'
+# Returns: {id: "test-9870", port: "9870", ...}
+
+# Each instance has its own port and Bridge
+# Navigate on port 9868: POST http://localhost:9868/navigate?url=...
+# Navigate on port 9869: POST http://localhost:9869/navigate?url=...
+# Navigate on port 9870: POST http://localhost:9870/navigate?url=...
+```
+
+---
+
+### 📋 Phase 4: Instance-Scoped Endpoints & Orchestrator Routes (NEXT - Estimated 2-3 commits)
-1. **Update orchestrator.Launch()**
- - If port parameter is empty, call `o.portAllocator.AllocatePort()`
- - Return allocated port to caller
- - On instance shutdown/failure, call `o.portAllocator.ReleasePort(port)`
+**Goal**: Provide endpoints on orchestrator to talk to specific instances
-2. **Update orchestrator handlers** (/instances POST endpoint)
- - Accept optional port in request body
- - If port provided, use it (manual override)
- - If not provided, auto-allocate
- - Return allocated port in response
+**Current State:**
+- Instances run on separate ports (9868, 9869, etc.)
+- Each port is its own Bridge with own handlers
+- Users must know which instance's port to target
+- Orchestrator (9867) knows about all instances but doesn't proxy to them
-3. **Wire handler to use allocator**
- - orchestrator/handlers.go CreateInstance() should:
- a. Extract port from request (optional)
- b. Call orchestrator.Launch(name, port, headless)
- c. If Launch succeeds, return instance with port
+**Proposed Phase 4 Approach:**
+1. **Add instance proxy routes on orchestrator**
+ - `POST /instances/{id}/navigate?url=...` → forward to instance:port/navigate
+ - `GET /instances/{id}/snapshot?tabId=...` → forward to instance:port/snapshot
+ - `POST /instances/{id}/action` → forward to instance:port/action
+ - This gives clients a single port to talk to (9867 orchestrator)
+
+2. **Alternative: Let clients discover then target directly**
+ - GET /instances → returns [{id, port, ...}]
+ - Client calls POST http://localhost:{port}/navigate
+ - No proxy layer needed
+
+3. **Testing**
+ - Create instances → get list with ports
+ - Either proxy through orchestrator or direct to instance port
+ - Both approaches work, users pick their preference
+
+**Note**: Current architecture already supports direct-to-instance approach.
+Proxy approach (Phase 4) is nice-to-have for simplification but not required.
+
+### 📋 Phase 5: Auto-Initialize Chrome on Instance Startup (NEXT - Estimated 2-3 commits)
+
+**Goal**: Make Chrome start automatically when instances are created
+
+**Current State:**
+- Instances create on ports 9868-9968
+- Bridge.EnsureChrome() exists but is only called on first navigation request
+- Handlers call ensureChrome() before operations
+
+**Phase 5 Work:**
+1. **Call EnsureChrome() during instance startup**
+ - After instance process starts and passes health check (already running handlers)
+ - Orchestrator calls POST http://localhost:{port}/ensure-chrome
+ - New handler EnsureChrome() endpoint that calls bridge.EnsureChrome()
+ - Wait for completion before returning from POST /instances
+
+2. **Add /ensure-chrome handler endpoint**
+ - Simple POST /ensure-chrome handler
+ - Calls bridge.EnsureChrome(config)
+ - Returns error if Chrome init fails
+ - Allows orchestrator to verify Chrome is ready
+
+3. **Update orchestrator Launch() flow**
+ - After instance becomes "running"
+ - POST /ensure-chrome to instance port
+ - If fails, mark instance as "error" and release port
+ - If succeeds, instance is ready for navigation
4. **Testing**
- - POST /instances with no port → should auto-allocate (9868)
- - POST /instances 5 times → should get 9868, 9869, 9870, 9871, 9872
- - Verify PortAllocator tracks them
- - Clean shutdown → ports released and reusable
-
-### 📋 Phase 4: Instance-Scoped Endpoints (After Phase 3 - Estimated 3-4 commits)
-
-**Key insight**: Each instance already runs on its own port (9868, 9869, etc.) with its own Bridge.
-The handler routes on that port don't need `/instances/{id}` prefix - just register at `/navigate`, `/snapshot`, etc.
-Requests to port 9868 automatically go to that instance's Bridge.
-
-1. **Verify instance-port mapping works**
- - Each spawned instance already on separate port ✓
- - Each port gets its own Bridge instance ✓
- - Routes on that Bridge's handlers.go are isolated ✓
-
-2. **Update CLI to use specific instance port**
- - CLI commands should target instance port directly
- - Or query orchestrator for first running instance
- - Dashboard can proxy to instance ports if needed for UI
-
-3. **Testing approach**
- - Create instance via POST http://localhost:9867/instances → returns port (e.g., 9868)
- - Navigate via POST http://localhost:9868/navigate
- - Port already targets instance (no path prefix needed)
-
-### 📋 Phase 5: Orchestrator Chrome Initialization (After Phase 4 - Estimated 2-3 commits)
-
-**Goal**: Initialize Chrome when instances are created (POST /instances)
-
-1. **Update orchestrator instance launch flow**
- - After instance process starts and passes health check
- - Call bridge.InitChrome() via HTTP request to instance
- - Instance calls bridge.EnsureChrome() which runs InitChrome()
- - Wait for Chrome to fully initialize
- - Return instance with Chrome ready
-
-2. **Test POST /instances endpoint**
- - Create instance with {profile: "work", headless: false}
- - Verify Chrome window opens (headless=false)
- - Verify returned instance has port, status="running", tabs=[]
- - Verify subsequent POST /navigate works immediately (Chrome already running)
-
-3. **Test headless instances**
- - Create instance with {headless: true}
- - Verify no Chrome window visible
- - Verify POST /navigate still works (Chrome running in background)
-
-4. **Test auto-port + Chrome init flow**
- - POST /instances (no port) → auto-allocate 9868, start Chrome
- - POST /instances (no port) → auto-allocate 9869, start Chrome
- - Both instances ready immediately
- - No race conditions or port conflicts
-
-### 📋 Phase 6: Testing & Validation (After Phase 5)
+ - Create instance → Chrome immediately available
+ - Headed instances → verify window opens
+ - Headless instances → verify background Chrome working
+ - Immediate navigation should work without EnsureChrome() calls in handlers
+
+### 📋 Phase 6: Comprehensive End-to-End Testing (After Phase 5)
1. **Test headed vs headless**
- Create instance {headless: false} → Chrome window appears
- Create instance {headless: true} → No window, runs in background