High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
feat(cache): add cache routes to all strategies and per-instance orchestrator
Bosch144d0d45b2dc
diff --git a/internal/orchestrator/handlers.go b/internal/orchestrator/handlers.goindex 0e667576..1ea4030e 100644--- a/internal/orchestrator/handlers.go+++ b/internal/orchestrator/handlers.go@@ -90,9 +90,9 @@ func (o *Orchestrator) registerHandlers(mux *http.ServeMux, skipLaunch bool) {registerCapabilityRoute(mux, "GET /tabs/{id}/download", o.AllowsDownload(), "download", "security.allowDownload", "download_disabled", o.proxyTabRequest)registerCapabilityRoute(mux, "POST /tabs/{id}/upload", o.AllowsUpload(), "upload", "security.allowUpload", "upload_disabled", o.proxyTabRequest)- // Cache operations - proxy to first running instance- mux.HandleFunc("POST /cache/clear", o.proxyToFirstInstance)- mux.HandleFunc("GET /cache/status", o.proxyToFirstInstance)+ // Cache operations - per-instance (browser-wide shorthands are in strategy routes)+ mux.HandleFunc("POST /instances/{id}/cache/clear", o.proxyToInstance)+ mux.HandleFunc("GET /instances/{id}/cache/status", o.proxyToInstance)}func (o *Orchestrator) handleList(w http.ResponseWriter, r *http.Request) {diff --git a/internal/orchestrator/orchestrator_test.go b/internal/orchestrator/orchestrator_test.goindex 2286949c..1aa1b705 100644--- a/internal/orchestrator/orchestrator_test.go+++ b/internal/orchestrator/orchestrator_test.go@@ -632,6 +632,31 @@ func TestValidateAttachURL_WildcardHost(t *testing.T) {}}+func TestOrchestrator_RegisterHandlers_CacheRoutes(t *testing.T) {+ o := NewOrchestratorWithRunner(t.TempDir(), &mockRunner{portAvail: true})+ o.ApplyRuntimeConfig(&config.RuntimeConfig{})++ mux := http.NewServeMux()+ o.RegisterHandlers(mux)++ routes := []struct {+ method string+ path string+ route string+ }{+ {"POST", "/instances/inst1/cache/clear", "POST /instances/{id}/cache/clear"},+ {"GET", "/instances/inst1/cache/status", "GET /instances/{id}/cache/status"},+ }++ for _, rt := range routes {+ req := httptest.NewRequest(rt.method, rt.path, nil)+ _, pattern := mux.Handler(req)+ if pattern != rt.route {+ t.Errorf("expected route %q for %s %s, got %q", rt.route, rt.method, rt.path, pattern)+ }+ }+}+func TestOrchestrator_RegisterHandlers_LocksSensitiveRoutes(t *testing.T) {o := NewOrchestratorWithRunner(t.TempDir(), &mockRunner{portAvail: true})o.ApplyRuntimeConfig(&config.RuntimeConfig{})diff --git a/internal/orchestrator/proxy.go b/internal/orchestrator/proxy.goindex 41e70dd4..859d4e06 100644--- a/internal/orchestrator/proxy.go+++ b/internal/orchestrator/proxy.go@@ -335,39 +335,3 @@ func (o *Orchestrator) singleRunningInstance() *InstanceInternal {}return only}--// proxyToFirstInstance proxies requests to the first running instance.-// Used for browser-wide operations like cache clearing that aren't tab-specific.-func (o *Orchestrator) proxyToFirstInstance(w http.ResponseWriter, r *http.Request) {- inst := o.firstRunningInstance()- if inst == nil {- httpx.Error(w, 503, fmt.Errorf("no running instance available"))- return- }-- activity.EnrichRequest(r, activity.Update{- InstanceID: inst.ID,- ProfileID: inst.ProfileID,- ProfileName: inst.ProfileName,- })-- targetURL, err := o.instancePathURL(inst, r.URL.Path, r.URL.RawQuery)- if err != nil {- httpx.Error(w, 502, err)- return- }- o.proxyToURL(w, r, targetURL)-}--// firstRunningInstance returns any running instance (used for browser-wide ops).-func (o *Orchestrator) firstRunningInstance() *InstanceInternal {- o.mu.RLock()- defer o.mu.RUnlock()-- for _, inst := range o.instances {- if inst.Status == "running" && instanceIsActive(inst) {- return inst- }- }- return nil-}diff --git a/internal/strategy/autorestart/autorestart.go b/internal/strategy/autorestart/autorestart.goindex 81bcd532..d008b48b 100644--- a/internal/strategy/autorestart/autorestart.go+++ b/internal/strategy/autorestart/autorestart.go@@ -188,6 +188,7 @@ func (s *Strategy) RegisterRoutes(mux *http.ServeMux) {"GET /cookies", "POST /cookies","GET /stealth/status", "POST /fingerprint/rotate","POST /find",+ "POST /cache/clear", "GET /cache/status","GET /solvers","POST /solve", "POST /solve/{name}",}diff --git a/internal/strategy/explicit/explicit.go b/internal/strategy/explicit/explicit.goindex 4e5db42c..68c8598f 100644--- a/internal/strategy/explicit/explicit.go+++ b/internal/strategy/explicit/explicit.go@@ -51,6 +51,7 @@ func (s *Strategy) RegisterRoutes(mux *http.ServeMux) {"GET /cookies", "POST /cookies","GET /stealth/status", "POST /fingerprint/rotate","POST /find",+ "POST /cache/clear", "GET /cache/status","GET /solvers","POST /solve", "POST /solve/{name}","GET /network", "GET /network/stream", "GET /network/export", "GET /network/export/stream", "GET /network/{requestId}", "POST /network/clear",diff --git a/internal/strategy/noinstance/noinstance.go b/internal/strategy/noinstance/noinstance.goindex 996c7da8..04545c6b 100644--- a/internal/strategy/noinstance/noinstance.go+++ b/internal/strategy/noinstance/noinstance.go@@ -47,6 +47,7 @@ func (s *Strategy) RegisterRoutes(mux *http.ServeMux) {"GET /cookies", "POST /cookies","GET /stealth/status", "POST /fingerprint/rotate","POST /find",+ "POST /cache/clear", "GET /cache/status","GET /solvers","POST /solve", "POST /solve/{name}","GET /network", "GET /network/stream", "GET /network/export", "GET /network/export/stream", "GET /network/{requestId}", "POST /network/clear",diff --git a/internal/strategy/simple/simple.go b/internal/strategy/simple/simple.goindex 8439a0df..3c1ca928 100644--- a/internal/strategy/simple/simple.go+++ b/internal/strategy/simple/simple.go@@ -62,6 +62,7 @@ func (s *Strategy) RegisterRoutes(mux *http.ServeMux) {"GET /cookies", "POST /cookies","GET /stealth/status", "POST /fingerprint/rotate","POST /find",+ "POST /cache/clear", "GET /cache/status","GET /solvers","POST /solve", "POST /solve/{name}",}diff --git a/internal/strategy/strategy_test.go b/internal/strategy/strategy_test.goindex dce8d031..11e4d9c4 100644--- a/internal/strategy/strategy_test.go+++ b/internal/strategy/strategy_test.go@@ -1,14 +1,21 @@package strategy_testimport (+ "context"+ "io"+ "net/http"+ "net/http/httptest""testing"+ "github.com/pinchtab/pinchtab/internal/config"+ "github.com/pinchtab/pinchtab/internal/orchestrator""github.com/pinchtab/pinchtab/internal/strategy"// Register strategies via init()_ "github.com/pinchtab/pinchtab/internal/strategy/alwayson"_ "github.com/pinchtab/pinchtab/internal/strategy/autorestart"_ "github.com/pinchtab/pinchtab/internal/strategy/explicit"+ _ "github.com/pinchtab/pinchtab/internal/strategy/noinstance"_ "github.com/pinchtab/pinchtab/internal/strategy/simple")@@ -79,6 +86,52 @@ func TestRegistry_Names(t *testing.T) {}}+type mockRunner struct{}++func (m *mockRunner) Run(_ context.Context, _ string, _ []string, _ []string, _ io.Writer, _ io.Writer) (orchestrator.Cmd, error) {+ return nil, nil+}+func (m *mockRunner) IsPortAvailable(_ string) bool { return true }++func TestCacheRoutes_RegisteredAcrossStrategies(t *testing.T) {+ strategies := []string{"simple", "explicit", "no-instance", "simple-autorestart"}+ cacheRoutes := []struct {+ method string+ path string+ route string+ }{+ {"POST", "/cache/clear", "POST /cache/clear"},+ {"GET", "/cache/status", "GET /cache/status"},+ }++ for _, name := range strategies {+ t.Run(name, func(t *testing.T) {+ s, err := strategy.New(name)+ if err != nil {+ t.Fatalf("strategy.New(%q): %v", name, err)+ }++ orch := orchestrator.NewOrchestratorWithRunner(t.TempDir(), &mockRunner{})+ orch.ApplyRuntimeConfig(&config.RuntimeConfig{})++ if oa, ok := s.(strategy.OrchestratorAware); ok {+ oa.SetOrchestrator(orch)+ }++ mux := http.NewServeMux()+ s.RegisterRoutes(mux)++ for _, rt := range cacheRoutes {+ req := httptest.NewRequest(rt.method, rt.path, nil)+ _, pattern := mux.Handler(req)+ if pattern != rt.route {+ t.Errorf("strategy %q: expected route %q for %s %s, got %q", name, rt.route, rt.method, rt.path, pattern)+ }+ }+ })+ }+}+func TestOrchestratorAware_AllStrategies(t *testing.T) {for _, name := range []string{"explicit", "simple", "simple-autorestart", "always-on"} {s, err := strategy.New(name)