High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: use managed tab count for eviction decisions
Boscheea1150ba125
diff --git a/internal/bridge/tab_manager.go b/internal/bridge/tab_manager.goindex f7fef892..814e6158 100644--- a/internal/bridge/tab_manager.go+++ b/internal/bridge/tab_manager.go@@ -213,22 +213,21 @@ func (tm *TabManager) CreateTab(url string) (string, context.Context, context.Ca}if tm.config.MaxTabs > 0 {- // Use a short timeout for tab count check to avoid hanging under load- checkCtx, checkCancel := context.WithTimeout(tm.browserCtx, 3*time.Second)- targets, err := tm.ListTargetsWithContext(checkCtx)- checkCancel()+ // Count managed tabs for eviction decisions. Using Chrome's target list+ // would include unmanaged targets (e.g. the initial about:blank tab),+ // causing premature eviction of managed tabs.+ tm.mu.RLock()+ managedCount := len(tm.tabs)+ tm.mu.RUnlock()- if err != nil {- // If check fails due to timeout, log warning but allow creation to proceed- slog.Warn("tab count check timed out, proceeding with creation", "error", err)- } else if len(targets) >= tm.config.MaxTabs {+ if managedCount >= tm.config.MaxTabs {switch tm.config.TabEvictionPolicy {case "close_oldest":if evictErr := tm.closeOldestTab(); evictErr != nil {return "", nil, nil, fmt.Errorf("eviction failed: %w", evictErr)}case "reject":- return "", nil, nil, &TabLimitError{Current: len(targets), Max: tm.config.MaxTabs}+ return "", nil, nil, &TabLimitError{Current: managedCount, Max: tm.config.MaxTabs}default: // "close_lru" (default)if evictErr := tm.closeLRUTab(); evictErr != nil {return "", nil, nil, fmt.Errorf("eviction failed: %w", evictErr)diff --git a/tests/e2e/scenarios/24-tab-eviction-lru.sh b/tests/e2e/scenarios/24-tab-eviction-lru.shindex 61203da4..d5bf408e 100755--- a/tests/e2e/scenarios/24-tab-eviction-lru.sh+++ b/tests/e2e/scenarios/24-tab-eviction-lru.sh@@ -2,136 +2,90 @@# 24-tab-eviction-lru.sh — LRU tab eviction (maxTabs=2 on secure instance)## The secure pinchtab instance is configured with maxTabs=2 and close_lru.-# Tests that opening a 3rd tab evicts the least recently used tab.+# Tests that opening a 3rd managed tab evicts the least recently used one.+#+# Note: Chrome keeps an initial about:blank target that is unmanaged.+# Eviction is based on managed tab count, not Chrome target count.source "$(dirname "$0")/common.sh"# Use the secure instance (maxTabs=2)PINCHTAB_URL="$PINCHTAB_SECURE_URL"-# Close any existing tabs from previous tests or Chrome startup-for tab_id in $(curl -s "$PINCHTAB_URL/tabs" | jq -r '.tabs[].id // empty' 2>/dev/null); do- curl -sf -X POST "$PINCHTAB_URL/tabs/$tab_id/close" > /dev/null 2>&1-done-# ─────────────────────────────────────────────────────────────────start_test "LRU eviction: open 2 tabs (at limit)"-# Tab 1: index page+# Tab 1pt_post /navigate -d "{\"url\":\"${FIXTURES_URL}/index.html\"}"TAB1=$(echo "$RESULT" | jq -r '.tabId')assert_ok "open tab 1 (index)"echo -e " ${MUTED}tab1: ${TAB1:0:12}...${NC}"-sleep 0.5+sleep 1-# Tab 2: form page+# Tab 2pt_post /navigate -d "{\"url\":\"${FIXTURES_URL}/form.html\"}"TAB2=$(echo "$RESULT" | jq -r '.tabId')assert_ok "open tab 2 (form)"echo -e " ${MUTED}tab2: ${TAB2:0:12}...${NC}"-# Verify we have 2 tabs-pt_get /tabs > /dev/null-TAB_COUNT=$(echo "$RESULT" | jq '.tabs | length')-if [ "$TAB_COUNT" -eq 2 ]; then- echo -e " ${GREEN}✓${NC} 2 tabs open (at limit)"- ((ASSERTIONS_PASSED++)) || true-else- echo -e " ${RED}✗${NC} expected 2 tabs, got $TAB_COUNT"- ((ASSERTIONS_FAILED++)) || true-fi+# Both tabs should be accessible+pt_get "/tabs/$TAB1/snapshot" > /dev/null+assert_ok "tab1 accessible"+pt_get "/tabs/$TAB2/snapshot" > /dev/null+assert_ok "tab2 accessible"end_test# ─────────────────────────────────────────────────────────────────start_test "LRU eviction: 3rd tab evicts least recently used"-# Touch tab2 (make it recently used) by taking a snapshot.-# Sleep ensures LastUsed timestamps are clearly separated.+# Touch tab2 to make it recently used (clear time separation)sleep 1pt_get "/tabs/$TAB2/snapshot" > /dev/nullsleep 1-# Tab 3: buttons page — should evict tab1 (LRU)+# Tab 3 — should evict tab1 (LRU, not touched since creation)pt_post /navigate -d "{\"url\":\"${FIXTURES_URL}/buttons.html\"}"TAB3=$(echo "$RESULT" | jq -r '.tabId')-assert_ok "open tab 3 (buttons) — triggers LRU eviction"+assert_ok "open tab 3 (triggers eviction)"echo -e " ${MUTED}tab3: ${TAB3:0:12}...${NC}"-# Still 2 tabs (one was evicted)-pt_get /tabs > /dev/null-TAB_COUNT=$(echo "$RESULT" | jq '.tabs | length')-if [ "$TAB_COUNT" -eq 2 ]; then- echo -e " ${GREEN}✓${NC} still 2 tabs (eviction worked)"- ((ASSERTIONS_PASSED++)) || true-else- echo -e " ${RED}✗${NC} expected 2 tabs after eviction, got $TAB_COUNT"- ((ASSERTIONS_FAILED++)) || true-fi-# Tab1 should be gone (evicted as LRU)-TAB1_EXISTS=$(echo "$RESULT" | jq --arg id "$TAB1" '[.tabs[] | select(.id == $id)] | length')-if [ "$TAB1_EXISTS" -eq 0 ]; then- echo -e " ${GREEN}✓${NC} tab1 was evicted (LRU)"- ((ASSERTIONS_PASSED++)) || true-else- echo -e " ${RED}✗${NC} tab1 should have been evicted"- ((ASSERTIONS_FAILED++)) || true-fi--# Tab2 should still exist (it was recently used)-TAB2_EXISTS=$(echo "$RESULT" | jq --arg id "$TAB2" '[.tabs[] | select(.id == $id)] | length')-if [ "$TAB2_EXISTS" -eq 1 ]; then- echo -e " ${GREEN}✓${NC} tab2 survived (recently used)"- ((ASSERTIONS_PASSED++)) || true-else- echo -e " ${RED}✗${NC} tab2 should still exist"- ((ASSERTIONS_FAILED++)) || true-fi--end_test+pt_get "/tabs/$TAB1/snapshot"+assert_http_error 404 "tab1 evicted (LRU)"-# ─────────────────────────────────────────────────────────────────-start_test "LRU eviction: evicted tab returns 404"+# Tab2 should still be accessible (recently used)+pt_get "/tabs/$TAB2/snapshot" > /dev/null+assert_ok "tab2 survived (recently used)"-pt_get "/tabs/$TAB1/snapshot"-assert_http_error 404 "evicted tab snapshot"+# Tab3 should be accessible (just created)+pt_get "/tabs/$TAB3/snapshot" > /dev/null+assert_ok "tab3 accessible"end_test# ─────────────────────────────────────────────────────────────────start_test "LRU eviction: continuous eviction works"-# Touch tab3 to make it recently used, then open tab4+# Touch tab3 so tab2 becomes LRUsleep 1pt_get "/tabs/$TAB3/snapshot" > /dev/nullsleep 1-# Open tab 4 — should evict tab2 (LRU, not touched since creation)+# Tab 4 — should evict tab2 (LRU)pt_post /navigate -d "{\"url\":\"${FIXTURES_URL}/table.html\"}"TAB4=$(echo "$RESULT" | jq -r '.tabId')-assert_ok "open tab 4 (table) — triggers another eviction"--pt_get /tabs > /dev/null-TAB_COUNT=$(echo "$RESULT" | jq '.tabs | length')-if [ "$TAB_COUNT" -eq 2 ]; then- echo -e " ${GREEN}✓${NC} still 2 tabs after second eviction"- ((ASSERTIONS_PASSED++)) || true-else- echo -e " ${RED}✗${NC} expected 2 tabs, got $TAB_COUNT"- ((ASSERTIONS_FAILED++)) || true-fi--# Tab3 and tab4 should be the survivors-TAB3_EXISTS=$(echo "$RESULT" | jq --arg id "$TAB3" '[.tabs[] | select(.id == $id)] | length')-TAB4_EXISTS=$(echo "$RESULT" | jq --arg id "$TAB4" '[.tabs[] | select(.id == $id)] | length')-if [ "$TAB3_EXISTS" -eq 1 ] && [ "$TAB4_EXISTS" -eq 1 ]; then- echo -e " ${GREEN}✓${NC} tab3 and tab4 survived"- ((ASSERTIONS_PASSED++)) || true-else- echo -e " ${RED}✗${NC} expected tab3 and tab4 to survive"- ((ASSERTIONS_FAILED++)) || true-fi+assert_ok "open tab 4 (triggers second eviction)"++# Tab2 should be gone now+pt_get "/tabs/$TAB2/snapshot"+assert_http_error 404 "tab2 evicted (LRU)"++# Tab3 and tab4 should be accessible+pt_get "/tabs/$TAB3/snapshot" > /dev/null+assert_ok "tab3 survived"+pt_get "/tabs/$TAB4/snapshot" > /dev/null+assert_ok "tab4 accessible"end_test