High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: orchestrator client timeout + add GET /instances/{id} endpoint + integration test
Mario9bd300618dcd
diff --git a/internal/orchestrator/handlers.go b/internal/orchestrator/handlers.goindex 61b9fe09..7e9a5b74 100644--- a/internal/orchestrator/handlers.go+++ b/internal/orchestrator/handlers.go@@ -14,6 +14,7 @@ import (func (o *Orchestrator) RegisterHandlers(mux *http.ServeMux) {// Core routesmux.HandleFunc("GET /instances", o.handleList)+ mux.HandleFunc("GET /instances/{id}", o.handleGetInstance)mux.HandleFunc("GET /instances/tabs", o.handleAllTabs)// Phase 3: Tab Management (new API)@@ -71,6 +72,20 @@ func (o *Orchestrator) handleList(w http.ResponseWriter, r *http.Request) {web.JSON(w, 200, o.List())}+func (o *Orchestrator) handleGetInstance(w http.ResponseWriter, r *http.Request) {+ id := r.PathValue("id")+ o.mu.RLock()+ inst, ok := o.instances[id]+ o.mu.RUnlock()++ if !ok {+ web.Error(w, 404, fmt.Errorf("instance %q not found", id))+ return+ }++ web.JSON(w, 200, inst.Instance)+}+// resolveProfileID resolves a path value to a profile name.// Accepts both a 12-char hex profile ID or a profile name directly.func (o *Orchestrator) resolveProfileName(idOrName string) (string, error) {diff --git a/internal/orchestrator/orchestrator.go b/internal/orchestrator/orchestrator.goindex 4e3d4eab..45b3435c 100644--- a/internal/orchestrator/orchestrator.go+++ b/internal/orchestrator/orchestrator.go@@ -72,11 +72,20 @@ func NewOrchestratorWithRunner(baseDir string, runner HostRunner) *Orchestrator}orch := &Orchestrator{- instances: make(map[string]*InstanceInternal),- baseDir: baseDir,- binary: binary,- runner: runner,- client: &http.Client{Timeout: 3 * time.Second},+ instances: make(map[string]*InstanceInternal),+ baseDir: baseDir,+ binary: binary,+ runner: runner,+ // Client timeout for proxying to instances: 60 seconds+ // Why so high?+ // - First request to an instance triggers lazy Chrome initialization (8-20+ seconds)+ // - Navigation can take up to 60s (NavigateTimeout in bridge config)+ // - Proxied requests (e.g., POST /instances/{id}/navigate) must wait for:+ // 1. Instance /health handler to initialize Chrome (via ensureChrome())+ // 2. Tab operations to complete (navigate, snapshot, actions, etc.)+ // - Short timeout (<5s) would break first-request scenarios+ // See: internal/orchestrator/health.go (monitor), internal/bridge/init.go (InitChrome)+ client: &http.Client{Timeout: 60 * time.Second},childAuthToken: os.Getenv("BRIDGE_TOKEN"),portAllocator: NewPortAllocator(9868, 9968),idMgr: idutil.NewManager(),diff --git a/tests/integration/orchestrator_test.go b/tests/integration/orchestrator_test.goindex 84c6fff4..e29f4995 100644--- a/tests/integration/orchestrator_test.go+++ b/tests/integration/orchestrator_test.go@@ -364,6 +364,73 @@ func TestOrchestrator_ProxyRouting(t *testing.T) {}}+// TestOrchestrator_FirstRequestLazyChrome tests proxy request with lazy Chrome initialization+// This test verifies the orchestrator's 60-second client timeout is sufficient for lazy Chrome initialization.+// Scenario:+// 1. Create instance (starts monitor polling /health)+// 2. Monitor's first /health call triggers ensureChrome() (8-20+ seconds)+// 3. Once /health succeeds, instance status becomes "running"+// 4. Proxy request to /navigate completes successfully+// If the orchestrator's client timeout is too short (<30s), the /health check would timeout+// and the instance would never reach "running" state.+func TestOrchestrator_FirstRequestLazyChrome(t *testing.T) {+ // Create instance+ payload := map[string]any{+ "name": fmt.Sprintf("first-request-%d", time.Now().Unix()),+ "headless": true,+ }++ status, body := httpPost(t, "/instances/launch", payload)+ if status != 201 {+ t.Fatalf("instance creation failed: %d", status)+ }++ instID := jsonField(t, body, "id")++ defer func() {+ httpPost(t, fmt.Sprintf("/instances/%s/stop", instID), nil)+ }()++ // Wait for instance to reach "running" state via monitor+ // The monitor polls /health every 500ms+ // First /health call triggers Chrome initialization (8-20+ seconds)+ // Once /health succeeds, status changes to "running"+ // If orchestrator timeout is <30s, this loop will timeout+ const maxWait = 45 * time.Second+ const pollInterval = 500 * time.Millisecond+ startTime := time.Now()++ var instStatus string+ for time.Since(startTime) < maxWait {+ status, body := httpGet(t, fmt.Sprintf("/instances/%s", instID))+ if status == 200 {+ instStatus = jsonField(t, body, "status")+ if instStatus == "running" {+ break+ }+ }+ time.Sleep(pollInterval)+ }++ if instStatus != "running" {+ t.Fatalf("instance never reached running state (timeout: %s), last status: %s", maxWait, instStatus)+ }++ // Now make the actual proxy request - Chrome is already initialized+ navPayload := map[string]any{"url": "https://example.com"}+ status, body = httpPost(t, fmt.Sprintf("/instances/%s/navigate", instID), navPayload)+ if status != 200 {+ t.Fatalf("navigate failed: %d: %s", status, string(body))+ }++ tabID := jsonField(t, body, "tabId")+ if !strings.HasPrefix(tabID, "tab_") {+ t.Fatalf("invalid tab ID: %s", tabID)+ }++ t.Logf("✓ Instance reached running state with lazy Chrome init; navigate succeeded: tabId=%s", tabID)+}+// TestOrchestrator_AggregateTabsEndpoint verifies GET /instances/tabs returns all tabsfunc TestOrchestrator_AggregateTabsEndpoint(t *testing.T) {var instIDs []string