High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: bound the tab-instance cache by wiring the existing reconciler into maintenance
Luigi Agosti0519bb3668af
diff --git a/internal/instance/locator.go b/internal/instance/locator.goindex a0eca3ee..984779b6 100644--- a/internal/instance/locator.go+++ b/internal/instance/locator.go@@ -55,17 +55,23 @@ func (l *Locator) FindInstanceByTabID(tabID string) (*bridge.Instance, error) {slog.Debug("locator: failed to fetch tabs", "instance", inst.ID, "err", err)continue}+ // Cache the tabs scanned so far in one critical section rather than+ // taking the write lock per tab, matching RefreshAll.+ l.mu.Lock()+ var match boolfor _, tab := range tabs {- // Cache every tab we discover for future lookups.- l.mu.Lock()l.cache[tab.ID] = inst.ID- l.mu.Unlock()-if tab.ID == tabID {- result := inst // copy- return &result, nil+ match = true+ break}}+ l.mu.Unlock()++ if match {+ result := inst // copy+ return &result, nil+ }}return nil, fmt.Errorf("tab %q not found in any running instance", tabID)diff --git a/internal/instance/locator_reconcile_test.go b/internal/instance/locator_reconcile_test.gonew file mode 100644index 00000000..f3692a1e--- /dev/null+++ b/internal/instance/locator_reconcile_test.go@@ -0,0 +1,53 @@+package instance_test++import (+ "testing"++ bridgepkg "github.com/pinchtab/pinchtab/internal/bridge"+ "github.com/pinchtab/pinchtab/internal/instance"+)++// FindInstanceByTabID caches every tab it discovers, but tabs also close+// without passing through the orchestrator proxy — the bridge evicts idle tabs+// on its own, and pages call window.close(). Those entries are never+// invalidated, so the cache only ever grows. RefreshAll is the reconciler.+func TestLocatorRefreshAllDropsTabsClosedOutOfBand(t *testing.T) {+ launcher := newMockLauncher()+ repo := instance.NewRepository(launcher)+ fetcher := newMockFetcher()++ inst, err := launcher.Launch("p", "9001", true)+ if err != nil {+ t.Fatalf("Launch: %v", err)+ }+ repo.Add(inst)++ for _, tabID := range []string{"tab-1", "tab-2", "tab-3"} {+ fetcher.AddTab("9001", tabID, "https://example.com")+ }++ // Looking up the last tab caches the ones scanned before it too; the scan+ // returns early on a match, so everything up to the hit lands in the cache.+ locator := instance.NewLocator(repo, fetcher)+ if _, err := locator.FindInstanceByTabID("tab-3"); err != nil {+ t.Fatalf("FindInstanceByTabID: %v", err)+ }+ if got := locator.CacheSize(); got != 3 {+ t.Fatalf("cache size after discovery = %d, want 3", got)+ }++ // Two tabs go away without the orchestrator ever seeing a /close.+ fetcher.tabsByURL["http://localhost:9001"] = []bridgepkg.InstanceTab{+ {ID: "tab-2", URL: "https://example.com"},+ }++ if got := locator.CacheSize(); got != 3 {+ t.Fatalf("cache size = %d, want 3 — entries linger until reconciled", got)+ }++ locator.RefreshAll()++ if got := locator.CacheSize(); got != 1 {+ t.Errorf("cache size after RefreshAll = %d, want 1 — closed tabs must be dropped", got)+ }+}diff --git a/internal/orchestrator/maintenance_test.go b/internal/orchestrator/maintenance_test.gonew file mode 100644index 00000000..3ac25cbc--- /dev/null+++ b/internal/orchestrator/maintenance_test.go@@ -0,0 +1,31 @@+package orchestrator++import (+ "testing"+ "time"+)++// The maintenance tick is the only thing that bounds the tab→instance cache:+// tabs closed outside the proxy (bridge idle eviction, window.close) are never+// invalidated, so without this the cache only grows.+func TestRunMaintenanceOnceReconcilesTabCache(t *testing.T) {+ o := NewOrchestrator(t.TempDir())++ o.instanceMgr.Locator.Register("tab-gone", "inst-gone")+ if got := o.instanceMgr.Locator.CacheSize(); got != 1 {+ t.Fatalf("cache size = %d, want 1", got)+ }++ o.runMaintenanceOnce(time.Hour, 10000)++ if got := o.instanceMgr.Locator.CacheSize(); got != 0 {+ t.Errorf("cache size after maintenance = %d, want 0 — no instances are running, so nothing should remain cached", got)+ }+}++// Maintenance must stay safe when the instance manager was never wired.+func TestRunMaintenanceOnceWithoutInstanceManager(t *testing.T) {+ o := NewOrchestrator(t.TempDir())+ o.instanceMgr = nil+ o.runMaintenanceOnce(time.Hour, 10000)+}diff --git a/internal/orchestrator/orchestrator.go b/internal/orchestrator/orchestrator.goindex 2e7f3f13..d5fa2ea0 100644--- a/internal/orchestrator/orchestrator.go+++ b/internal/orchestrator/orchestrator.go@@ -248,11 +248,22 @@ func (o *Orchestrator) RunMaintenance(ctx context.Context) {case <-ctx.Done():returncase <-t.C:- o.bindings.PruneAgents(idleTTL, maxAgent)+ o.runMaintenanceOnce(idleTTL, maxAgent)}}}+// runMaintenanceOnce bounds the caches that nothing else prunes. Agent bindings+// have no lifecycle signal, and the tab→instance cache misses every tab that+// closes without passing through the proxy — bridge-side idle eviction and+// window.close() among them — so both need periodic reconciliation.+func (o *Orchestrator) runMaintenanceOnce(idleTTL time.Duration, maxAgent int) {+ o.bindings.PruneAgents(idleTTL, maxAgent)+ if o.instanceMgr != nil {+ o.instanceMgr.Locator.RefreshAll()+ }+}+func (o *Orchestrator) Bindings() *Bindings {if o == nil {return nil