High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: fail fast with the browser cause on the api proxy path, not an opaque 503
Boschca9e99be1c02
diff --git a/internal/orchestrator/browser_preflight.go b/internal/orchestrator/browser_preflight.gonew file mode 100644index 00000000..391b31fc--- /dev/null+++ b/internal/orchestrator/browser_preflight.go@@ -0,0 +1,53 @@+package orchestrator++import (+ "fmt"+ "net/http"+ "os"+ "strings"++ "github.com/pinchtab/pinchtab/internal/browsers"+ "github.com/pinchtab/pinchtab/internal/browsers/runtimekit"+ "github.com/pinchtab/pinchtab/internal/config"+)++// BrowserUnavailableReason reports whether the request's target browser has no+// resolvable binary, with a user-facing reason. It lets the proxy fail fast and+// clearly instead of polling into a generic "instance not ready after 10s"+// timeout when the real problem is simply that no browser is installed — the+// opaque-503 cold start surfaced by install-UX testing, on the API path as well+// as the CLI.+//+// It deliberately speaks only to the simple default/single-provider case. An+// explicit per-request browser, a multi-target config, or a CDP attach all fall+// through (return false) so existing behavior and HTTP error mapping are+// unchanged. It mirrors the launch-time resolution in bridge/runtime.InitBrowser+// (explicit browser.binary wins over discovery) so it can't diverge from what+// the instance would actually try to launch.+func (o *Orchestrator) BrowserUnavailableReason(r *http.Request) (string, bool) {+ if o == nil || o.runtimeCfg == nil {+ return "", false+ }+ if ExtractRequestedBrowser(r) != "" || len(o.runtimeCfg.Targets) > 0 {+ return "", false+ }+ if strings.TrimSpace(o.runtimeCfg.CDPAttachURL) != "" {+ return "", false // attaching to an external CDP endpoint; no local binary needed+ }+ provider := config.NormalizeBrowser(o.runtimeCfg.DefaultBrowser)+ if _, ok := browsers.Get(provider); !ok {+ return "", false+ }+ if override := strings.TrimSpace(o.runtimeCfg.BrowserBinary); override != "" {+ if info, err := os.Stat(override); err != nil || info.IsDir() {+ return fmt.Sprintf("configured browser.binary does not point at a usable executable: %s — "++ "set it to a real browser or unset it for auto-discovery (run `pinchtab doctor`)", override), true+ }+ return "", false+ }+ if runtimekit.FindBrowserBinary(provider) == "" {+ return fmt.Sprintf("no %s browser found on this host — install Chrome/Chromium "++ "(e.g. `apt-get install -y chromium`) or set browser.binary (run `pinchtab doctor`)", provider), true+ }+ return "", false+}diff --git a/internal/orchestrator/browser_preflight_test.go b/internal/orchestrator/browser_preflight_test.gonew file mode 100644index 00000000..89c83e5a--- /dev/null+++ b/internal/orchestrator/browser_preflight_test.go@@ -0,0 +1,74 @@+package orchestrator++import (+ "net/http"+ "net/http/httptest"+ "os"+ "path/filepath"+ "testing"++ "github.com/pinchtab/pinchtab/internal/config"+)++func TestBrowserUnavailableReason(t *testing.T) {+ req := func(target string) *http.Request {+ return httptest.NewRequest(http.MethodGet, target, nil)+ }+ existing := filepath.Join(t.TempDir(), "chrome")+ if err := os.WriteFile(existing, []byte("#!/bin/sh\n"), 0o755); err != nil {+ t.Fatal(err)+ }++ t.Run("missing browser.binary override is reported", func(t *testing.T) {+ o := &Orchestrator{runtimeCfg: &config.RuntimeConfig{+ DefaultBrowser: "chrome",+ BrowserBinary: filepath.Join(t.TempDir(), "missing"),+ }}+ reason, unavailable := o.BrowserUnavailableReason(req("/text?url=http://x/"))+ if !unavailable {+ t.Fatal("expected a missing override to be reported unavailable")+ }+ if reason == "" {+ t.Error("expected a non-empty reason")+ }+ })++ t.Run("existing override is available", func(t *testing.T) {+ o := &Orchestrator{runtimeCfg: &config.RuntimeConfig{+ DefaultBrowser: "chrome",+ BrowserBinary: existing,+ }}+ if _, unavailable := o.BrowserUnavailableReason(req("/text?url=http://x/")); unavailable {+ t.Error("an existing override binary should be available")+ }+ })++ t.Run("explicit per-request browser falls through", func(t *testing.T) {+ o := &Orchestrator{runtimeCfg: &config.RuntimeConfig{+ DefaultBrowser: "chrome",+ BrowserBinary: filepath.Join(t.TempDir(), "missing"),+ }}+ // An explicit ?browser= override must not be short-circuited here.+ if _, unavailable := o.BrowserUnavailableReason(req("/text?browser=chrome&url=http://x/")); unavailable {+ t.Error("explicit per-request browser should fall through, not fast-fail")+ }+ })++ t.Run("external CDP attach falls through", func(t *testing.T) {+ o := &Orchestrator{runtimeCfg: &config.RuntimeConfig{+ DefaultBrowser: "chrome",+ BrowserBinary: filepath.Join(t.TempDir(), "missing"),+ CDPAttachURL: "http://127.0.0.1:9222",+ }}+ if _, unavailable := o.BrowserUnavailableReason(req("/text?url=http://x/")); unavailable {+ t.Error("CDP attach needs no local binary; should fall through")+ }+ })++ t.Run("nil config is safe", func(t *testing.T) {+ o := &Orchestrator{}+ if _, unavailable := o.BrowserUnavailableReason(req("/text")); unavailable {+ t.Error("nil runtimeCfg must not report unavailable")+ }+ })+}diff --git a/internal/strategy/autorestart/handlers.go b/internal/strategy/autorestart/handlers.goindex 1f662f90..27777c0d 100644--- a/internal/strategy/autorestart/handlers.go+++ b/internal/strategy/autorestart/handlers.go@@ -44,6 +44,19 @@ func (s *Strategy) ensureRunning(r *http.Request) (string, int, error) {if s.orch == nil {return "", 503, fmt.Errorf("no orchestrator configured")}+ // Fail fast when nothing is running yet *because* no browser is resolvable:+ // return the real cause now instead of polling for 10s into a generic+ // "instance not ready" timeout. This generalizes the CLI nav preflight to the+ // HTTP API and any command. The browser probe only runs when the instance+ // isn't already up, so steady-state proxied requests are untouched.+ if t, _, err := s.orch.FirstRunningURLForRequest(r); err == nil && t != "" {+ return t, 0, nil+ } else if err == nil {+ if reason, unavailable := s.orch.BrowserUnavailableReason(r); unavailable {+ return "", http.StatusServiceUnavailable, errors.New(reason)+ }+ }+var lastStatus inttarget, err := readiness.WaitUntil(context.Background(), instanceReadyWait, 200*time.Millisecond,func() (string, bool, error) {