Skip to content

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

fix: GET /tabs marks the listed current tab as used (LastUsed, scope, current pointer), so the /health responsiveness probe shields the active tab from LRU and from session-end cleanup (PIN-370)

Luigi Agosti5eb13ee7442d
diff --git a/internal/bridge/tab_lookup.go b/internal/bridge/tab_lookup.go
index 6269c2d5..37591f77 100644
--- a/internal/bridge/tab_lookup.go
+++ b/internal/bridge/tab_lookup.go
@@ -16,6 +16,25 @@ func (tm *TabManager) markAccessed(tabID string) {
tm.mu.Unlock()
}
+func (tm *TabManager) CurrentTabID() string {
+ if tm == nil {
+ return ""
+ }
+ tm.mu.RLock()
+ defer tm.mu.RUnlock()
+ return tm.selectCurrentTrackedTab()
+}
+
+func (tm *TabManager) TabLastUsed(tabID string) (time.Time, bool) {
+ tm.mu.RLock()
+ defer tm.mu.RUnlock()
+ entry, ok := tm.tabs[tabID]
+ if !ok {
+ return time.Time{}, false
+ }
+ return entry.LastUsed, true
+}
+
// selectCurrentTrackedTab returns the current tab ID, falling back to the most
// recently used tab if the explicit pointer is stale or unset.
func (tm *TabManager) selectCurrentTrackedTab() string {
diff --git a/internal/handlers/health_tabs.go b/internal/handlers/health_tabs.go
index d60f08b0..43955a65 100644
--- a/internal/handlers/health_tabs.go
+++ b/internal/handlers/health_tabs.go
@@ -13,6 +13,28 @@ type tabHandoffReader interface {
TabHandoffState(tabID string) (bridge.TabHandoffState, bool)
}
+type currentTabReader interface {
+ CurrentTabID() string
+}
+
+var _ currentTabReader = (*bridge.Bridge)(nil)
+
+func (h *Handlers) listedCurrentTabID(r *http.Request, targets []bridge.TabTarget) string {
+ if !currentTabScopeFromRequest(r).IsGlobal() {
+ tabID, _ := h.scopedCurrentTabForRequest(r)
+ return tabID
+ }
+ if reader, ok := h.Bridge.(currentTabReader); ok {
+ if tabID := reader.CurrentTabID(); tabID != "" {
+ return tabID
+ }
+ }
+ if len(targets) > 0 {
+ return targets[0].TargetID
+ }
+ return ""
+}
+
func (h *Handlers) HandleHealth(w http.ResponseWriter, r *http.Request) {
if h.Bridge == nil {
writeUnavailable(w, 503, "bridge_unavailable", "bridge not initialized")
@@ -160,14 +182,10 @@ func (h *Handlers) HandleTabs(w http.ResponseWriter, r *http.Request) {
return
}
- currentTabID := ""
- if _, resolvedID, err := h.tabContext(r, ""); err == nil {
- currentTabID = resolvedID
- }
+ currentTabID := h.listedCurrentTabID(r, targets)
tabs := make([]map[string]any, 0, len(targets))
appendTab := func(t bridge.TabTarget) {
- // Skip the initial about:blank tab that Chrome creates on launch
if bridge.IsTransientURL(t.URL, h.Config.Port) {
return
}
diff --git a/internal/handlers/health_tabs_readonly_test.go b/internal/handlers/health_tabs_readonly_test.go
new file mode 100644
index 00000000..f88b1324
--- /dev/null
+++ b/internal/handlers/health_tabs_readonly_test.go
@@ -0,0 +1,157 @@
+package handlers
+
+import (
+ "context"
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "reflect"
+ "testing"
+ "time"
+
+ "github.com/chromedp/chromedp"
+ "github.com/pinchtab/pinchtab/internal/bridge"
+ "github.com/pinchtab/pinchtab/internal/config"
+ "github.com/pinchtab/pinchtab/internal/session"
+)
+
+type listedBridge struct {
+ *bridge.Bridge
+ targets []bridge.TabTarget
+}
+
+func (b listedBridge) ListTargets() ([]bridge.TabTarget, error) { return b.targets, nil }
+
+func (b listedBridge) EnsureBrowser(*config.RuntimeConfig) error { return nil }
+
+type tabsListingFixture struct {
+ b *bridge.Bridge
+ h *Handlers
+}
+
+const listingSession = "ses_listing"
+
+func newTabsListingFixture(t *testing.T) *tabsListingFixture {
+ t.Helper()
+ ctx, cancel := chromedp.NewContext(context.Background())
+ t.Cleanup(cancel)
+ cfg := &config.RuntimeConfig{StateDir: t.TempDir()}
+ b := bridge.New(context.Background(), ctx, cfg)
+ for _, id := range []string{"tabA", "tabB", "tabC"} {
+ b.RegisterTab(id, ctx)
+ time.Sleep(2 * time.Millisecond)
+ }
+ b.RecordTabScope("tabB", sessionScope().key, true)
+ lb := listedBridge{Bridge: b}
+ for _, id := range []string{"tabA", "tabB", "tabC"} {
+ lb.targets = append(lb.targets, bridge.TabTarget{TargetID: id, URL: "https://example.com/" + id, Type: "page"})
+ }
+ h := New(lb, cfg, nil, nil, nil)
+ h.CurrentTabs.Set(sessionScope(), "tabB")
+ return &tabsListingFixture{b: b, h: h}
+}
+
+func sessionScope() currentTabScope {
+ return scopedCurrentTab(currentTabScopeSession, listingSession)
+}
+
+func (f *tabsListingFixture) list(t *testing.T, scoped bool) []string {
+ t.Helper()
+ req := httptest.NewRequest(http.MethodGet, "/tabs", nil)
+ if scoped {
+ req = session.WithSession(req, &session.Session{ID: listingSession})
+ }
+ w := httptest.NewRecorder()
+ f.h.HandleTabs(w, req)
+ if w.Code != http.StatusOK {
+ t.Fatalf("GET /tabs = %d: %s", w.Code, w.Body.String())
+ }
+ var resp struct {
+ Tabs []struct {
+ ID string `json:"id"`
+ } `json:"tabs"`
+ }
+ if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
+ t.Fatal(err)
+ }
+ ids := make([]string, 0, len(resp.Tabs))
+ for _, tab := range resp.Tabs {
+ ids = append(ids, tab.ID)
+ }
+ return ids
+}
+
+type tabsListingState struct {
+ LastUsed map[string]time.Time
+ CurrentTab string
+ Accessed map[string]bool
+ OnlyByCreator []string
+ CurrentTabs map[string]string
+}
+
+func (f *tabsListingFixture) state() tabsListingState {
+ st := tabsListingState{LastUsed: map[string]time.Time{}, CurrentTabs: map[string]string{}}
+ for _, id := range []string{"tabA", "tabB", "tabC"} {
+ st.LastUsed[id], _ = f.b.TabLastUsed(id)
+ }
+ st.CurrentTab = f.b.CurrentTabID()
+ st.Accessed = f.b.AccessedTabIDs()
+ st.OnlyByCreator = f.b.TabsOnlyUsedByCreator(sessionScope().key)
+ f.h.CurrentTabs.mu.Lock()
+ for key, entry := range f.h.CurrentTabs.entries {
+ st.CurrentTabs[key] = entry.tabID
+ }
+ f.h.CurrentTabs.mu.Unlock()
+ return st
+}
+
+func TestHandleTabsListingChangesNoTabState(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ scoped bool
+ first string
+ }{
+ {"unidentified caller", false, "tabC"},
+ {"session-scoped caller", true, "tabB"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ f := newTabsListingFixture(t)
+ before := f.state()
+
+ ids := f.list(t, tc.scoped)
+
+ if after := f.state(); !reflect.DeepEqual(before, after) {
+ t.Fatalf("GET /tabs changed tab state:\nbefore %+v\nafter %+v", before, after)
+ }
+ if len(ids) != 3 || ids[0] != tc.first {
+ t.Fatalf("listing = %v, want %s first", ids, tc.first)
+ }
+ })
+ }
+}
+
+func TestHandleTabsUnidentifiedListingKeepsASessionTabOnlyUsedByItsCreator(t *testing.T) {
+ f := newTabsListingFixture(t)
+ f.b.RecordTabScope("tabC", sessionScope().key, true)
+
+ f.list(t, false)
+
+ got := f.b.TabsOnlyUsedByCreator(sessionScope().key)
+ if !reflect.DeepEqual(got, []string{"tabB", "tabC"}) {
+ t.Fatalf("tabs only used by the session = %v, want [tabB tabC]", got)
+ }
+}
+
+func TestHandleTabsSessionWhoseTabClosedGetsNoCurrentAndKeepsItsPointer(t *testing.T) {
+ f := newTabsListingFixture(t)
+ f.h.CurrentTabs.Set(sessionScope(), "gone")
+
+ ids := f.list(t, true)
+
+ if !reflect.DeepEqual(ids, []string{"tabA", "tabB", "tabC"}) {
+ t.Fatalf("listing = %v, want the unmarked target order", ids)
+ }
+ if got, ok := f.h.CurrentTabs.Get(sessionScope()); !ok || got != "gone" {
+ t.Fatalf("session pointer = %q %v, want gone kept", got, ok)
+ }
+}
diff --git a/internal/handlers/health_tabs_test.go b/internal/handlers/health_tabs_test.go
index 2f582a07..dd50b391 100644
--- a/internal/handlers/health_tabs_test.go
+++ b/internal/handlers/health_tabs_test.go
@@ -343,6 +343,10 @@ func (m *MockBridge) BrowserContext() context.Context {
return context.Background()
}
+func (m *MockBridge) CurrentTabID() string {
+ return m.currentTabID
+}
+
func (m *MockBridge) TabContext(tabID string) (*bridge.TabHandle, string, error) {
if tabID == "" && m.currentTabID != "" {
return bridge.NewTabHandle(context.Background()), m.currentTabID, nil