Business operating system for Claude Code — 57 skills, 21 agents, smart daemon. Unified inbox (WhatsApp/Email/Slack/Telegram), autonomous PR merge, full-AWS monitoring, revenue (Stripe+RevenueCat), e-commerce (Shopify), marketing (Klaviyo/Meta/GA4), voice (Bland/ElevenLabs), APM (Datadog/NewRelic/OTEL), YOLO mode.
Shell agent-teamsai-agentai-automationanthropicautonomous-agentawsbusiness-operationsclaudeclaude-codeclaude-code-plugindevopsecommerceinbox-zeroklaviyomarketingmcpmorning-briefingpr-automationshopifyterminal
fix(crsproxy): normalize paginated browser list in bu_reauth.py
auroracapitald63b32bc3558
diff --git a/claude-ops/scripts/crsproxy-reauth/bu_reauth.py b/claude-ops/scripts/crsproxy-reauth/bu_reauth.pyindex 1b5cbd5a..2f6e9c93 100644--- a/claude-ops/scripts/crsproxy-reauth/bu_reauth.py+++ b/claude-ops/scripts/crsproxy-reauth/bu_reauth.py@@ -619,15 +619,38 @@ class BrowserUseClient:self._browser_sessions.remove(browser_id)def list_browsers(self) -> list:- """List all running browser sessions."""+ """List all browser sessions from the Browser Use API.++ The API returns a paginated response dict:+ {"items": [...], "totalItems": N, "pageNumber": 1, "pageSize": 20}+ This method normalizes that to the items list so callers can+ iterate browser session dicts directly.+ """try:r = self._request("GET", "/browsers")- return r.json() if r.content else []+ if not r.content:+ return []+ data = r.json()+ # Normalize paginated response dict to its items array+ if isinstance(data, dict) and "items" in data:+ return data["items"]+ # Already a plain list+ if isinstance(data, list):+ return data+ # Unknown shape — return empty rather than iterating dict keys+ log(f"list_browsers: unexpected response shape: {type(data).__name__}")+ return []except Exception:return []def stop_all_browsers(self):- """Stop all tracked browser sessions."""+ """Stop all tracked browser sessions.++ Stops sessions tracked via _browser_sessions (from run events)+ and also queries the API for any running sessions we might+ have missed. Skips sessions that are already stopped to avoid+ unnecessary API calls.+ """if self.keep_browser_alive:log("[CLEANUP] Browser sessions kept alive for checkpoint")return@@ -637,11 +660,18 @@ class BrowserUseClient:try:browsers = self.list_browsers()for b in browsers:+ if not isinstance(b, dict):+ continuebid = b.get("id", "")- if bid:- self.stop_browser(bid)- except Exception:- pass+ if not bid:+ continue+ # Skip already-stopped sessions to avoid unnecessary API calls+ status = b.get("status", "")+ if status in ("stopped", "closed", "finished"):+ continue+ self.stop_browser(bid)+ except Exception as e:+ log(f"stop_all_browsers: error listing/stopping browsers: {e}")def wait_for_run(self, run_id: str, timeout: int = RUN_POLL_TIMEOUT,track_browser: bool = True) -> dict:diff --git a/claude-ops/scripts/crsproxy-reauth/test_browser_cleanup.py b/claude-ops/scripts/crsproxy-reauth/test_browser_cleanup.pynew file mode 100644index 00000000..67974fd3--- /dev/null+++ b/claude-ops/scripts/crsproxy-reauth/test_browser_cleanup.py@@ -0,0 +1,343 @@+#!/usr/bin/env python3+"""Unit tests for browser session cleanup pagination fix (VAL-REAUTH-008).++Tests that list_browsers() correctly normalizes the Browser Use API+paginated response dict to its items array, and that stop_all_browsers()+iterates the actual browser session dicts (not dict keys) and issues+PATCH stop requests for each running session.++Run on the hub:+ sudo -u crsproxy /opt/crsproxy/venv/bin/python \+ /opt/crsproxy/test_browser_cleanup.py++Run locally (syntax/logic only, no hub dependencies):+ python3 test_browser_cleanup.py+"""++import json+import sys+from pathlib import Path+from unittest.mock import MagicMock, patch++# Add the script directory to the path+sys.path.insert(0, str(Path(__file__).parent))++# Import the module+import bu_reauth+++# ---------------------------------------------------------------------------+# Real Browser Use API paginated response shape (from live API)+# ---------------------------------------------------------------------------+PAGINATED_RESPONSE = {+ "items": [+ {+ "id": "473ea101-49b9-4518-855c-d8f0d2035307",+ "status": "stopped",+ "liveUrl": None,+ "cdpUrl": None,+ "timeoutAt": "2026-08-13T03:03:24.128508Z",+ "startedAt": "2026-08-12T23:03:24.128508Z",+ "finishedAt": "2026-08-12T23:04:52.670817Z",+ "proxyUsedMb": "5.8607378005981443438",+ "proxyCost": "0.02861688379198312667871093750",+ "browserCost": "0.0006666666666666666666666666667",+ "agentSessionId": "3b87c9d5-e4f9-41e5-826c-342f5411c1f8",+ "recordingUrl": None,+ },+ {+ "id": "878b3fe7-5c62-40b7-a581-17e12ea77b4f",+ "status": "running",+ "liveUrl": "https://live.browser-use.com?wss=example",+ "cdpUrl": None,+ "timeoutAt": "2026-08-13T03:01:58.898232Z",+ "startedAt": "2026-08-12T23:01:58.898232Z",+ "finishedAt": None,+ "proxyUsedMb": "0.00117874145507812500",+ "proxyCost": "0.000005755573511123657226562500",+ "browserCost": "0.0003333333333333333333333333333",+ "agentSessionId": "3b87c9d5-e4f9-41e5-826c-342f5411c1f8",+ "recordingUrl": None,+ },+ {+ "id": "e0e52cb4-5a2d-4ec0-a6ac-2937bc14a347",+ "status": "running",+ "liveUrl": "https://live.browser-use.com?wss=example2",+ "cdpUrl": None,+ "timeoutAt": "2026-08-13T03:01:36.028765Z",+ "startedAt": "2026-08-12T23:01:36.028765Z",+ "finishedAt": None,+ "proxyUsedMb": "4.2314872741699216875",+ "proxyCost": "0.02066155895590782073974609375",+ "browserCost": "0.0003333333333333333333333333333",+ "agentSessionId": "3b87c9d5-e4f9-41e5-826c-342f5411c1f8",+ "recordingUrl": None,+ },+ ],+ "totalItems": 3,+ "pageNumber": 1,+ "pageSize": 20,+}+++def test_list_browsers_normalizes_paginated_dict():+ """list_browsers() returns the items array from a paginated response dict.++ Before the fix, list_browsers() returned the raw dict, causing+ stop_all_browsers() to iterate dict keys (strings) instead of+ browser session dicts.+ """+ client = bu_reauth.BrowserUseClient("fake-key")++ # Mock _request to return a paginated response+ mock_response = MagicMock()+ mock_response.content = b'{"items": [...]}'+ mock_response.json.return_value = PAGINATED_RESPONSE+ with patch.object(client, '_request', return_value=mock_response):+ result = client.list_browsers()++ assert isinstance(result, list), (+ f"Expected list, got {type(result).__name__}"+ )+ assert len(result) == 3, f"Expected 3 items, got {len(result)}"+ assert all(isinstance(item, dict) for item in result), (+ "All items should be dicts"+ )+ assert result[0]["id"] == "473ea101-49b9-4518-855c-d8f0d2035307"+ print("PASS: list_browsers() normalizes paginated dict to items array")+++def test_list_browsers_handles_plain_list():+ """list_browsers() handles a plain list response (non-paginated)."""+ client = bu_reauth.BrowserUseClient("fake-key")++ plain_list = [{"id": "abc", "status": "running"}]+ mock_response = MagicMock()+ mock_response.content = b'[...]'+ mock_response.json.return_value = plain_list+ with patch.object(client, '_request', return_value=mock_response):+ result = client.list_browsers()++ assert isinstance(result, list)+ assert len(result) == 1+ assert result[0]["id"] == "abc"+ print("PASS: list_browsers() handles plain list response")+++def test_list_browsers_handles_empty_response():+ """list_browsers() returns empty list for empty response."""+ client = bu_reauth.BrowserUseClient("fake-key")++ mock_response = MagicMock()+ mock_response.content = b''+ with patch.object(client, '_request', return_value=mock_response):+ result = client.list_browsers()++ assert result == [], f"Expected empty list, got {result}"+ print("PASS: list_browsers() returns empty list for empty response")+++def test_list_browsers_handles_empty_items():+ """list_browsers() returns empty list when items is empty."""+ client = bu_reauth.BrowserUseClient("fake-key")++ empty_paginated = {"items": [], "totalItems": 0, "pageNumber": 1, "pageSize": 20}+ mock_response = MagicMock()+ mock_response.content = b'{"items": []}'+ mock_response.json.return_value = empty_paginated+ with patch.object(client, '_request', return_value=mock_response):+ result = client.list_browsers()++ assert result == [], f"Expected empty list, got {result}"+ print("PASS: list_browsers() returns empty list when items is empty")+++def test_stop_all_browsers_stops_running_sessions():+ """stop_all_browsers() issues PATCH stop for each running session.++ Before the fix, stop_all_browsers() iterated dict keys (strings)+ and called b.get("id", "") on strings, which silently failed.+ After the fix, it iterates the items array and stops each+ running session.+ """+ client = bu_reauth.BrowserUseClient("fake-key")++ # Mock list_browsers to return the paginated items+ with patch.object(client, 'list_browsers',+ return_value=PAGINATED_RESPONSE["items"]):+ # Mock stop_browser to track calls without hitting the API+ stopped_ids = []+ def mock_stop(bid):+ stopped_ids.append(bid)+ with patch.object(client, 'stop_browser', side_effect=mock_stop):+ client.stop_all_browsers()++ # Should have stopped the 2 running sessions (skipped the stopped one)+ assert len(stopped_ids) == 2, (+ f"Expected 2 stop calls (running sessions only), got {len(stopped_ids)}"+ )+ assert "878b3fe7-5c62-40b7-a581-17e12ea77b4f" in stopped_ids+ assert "e0e52cb4-5a2d-4ec0-a6ac-2937bc14a347" in stopped_ids+ # The already-stopped session should NOT be stopped again+ assert "473ea101-49b9-4518-855c-d8f0d2035307" not in stopped_ids+ print("PASS: stop_all_browsers() stops running sessions, skips stopped ones")+++def test_stop_all_browsers_with_tracked_sessions():+ """stop_all_browsers() stops tracked sessions AND API-visible sessions."""+ client = bu_reauth.BrowserUseClient("fake-key")+ client._browser_sessions = ["tracked-session-1"]++ # Mock list_browsers to return one running session+ api_sessions = [{"id": "api-session-1", "status": "running"}]+ stopped_ids = []++ def mock_stop(bid):+ stopped_ids.append(bid)+ if bid in client._browser_sessions:+ client._browser_sessions.remove(bid)++ with patch.object(client, 'list_browsers', return_value=api_sessions):+ with patch.object(client, 'stop_browser', side_effect=mock_stop):+ client.stop_all_browsers()++ # Should stop both the tracked session and the API-visible session+ assert "tracked-session-1" in stopped_ids+ assert "api-session-1" in stopped_ids+ print("PASS: stop_all_browsers() stops tracked and API-visible sessions")+++def test_stop_all_browsers_skips_already_stopped():+ """stop_all_browsers() skips sessions with status 'stopped'."""+ client = bu_reauth.BrowserUseClient("fake-key")++ sessions = [+ {"id": "sess-1", "status": "stopped"},+ {"id": "sess-2", "status": "closed"},+ {"id": "sess-3", "status": "finished"},+ {"id": "sess-4", "status": "running"},+ ]+ stopped_ids = []++ with patch.object(client, 'list_browsers', return_value=sessions):+ with patch.object(client, 'stop_browser',+ side_effect=lambda bid: stopped_ids.append(bid)):+ client.stop_all_browsers()++ # Only the running session should be stopped+ assert stopped_ids == ["sess-4"], (+ f"Expected only sess-4 stopped, got {stopped_ids}"+ )+ print("PASS: stop_all_browsers() skips stopped/closed/finished sessions")+++def test_stop_all_browsers_keep_alive():+ """stop_all_browsers() does nothing when keep_browser_alive is True."""+ client = bu_reauth.BrowserUseClient("fake-key")+ client._browser_sessions = ["session-1", "session-2"]+ client.keep_browser_alive = True++ stopped_ids = []+ with patch.object(client, 'list_browsers', return_value=[]):+ with patch.object(client, 'stop_browser',+ side_effect=lambda bid: stopped_ids.append(bid)):+ client.stop_all_browsers()++ assert stopped_ids == [], "No sessions should be stopped when keep_browser_alive"+ assert len(client._browser_sessions) == 2, "Sessions should not be removed"+ print("PASS: stop_all_browsers() respects keep_browser_alive flag")+++def test_stop_all_browsers_no_crash_on_dict_response():+ """stop_all_browsers() does not crash if list_browsers returns a dict.++ This is a regression test: before the fix, list_browsers() returned+ the raw paginated dict, and stop_all_browsers() iterated its keys+ (strings), calling b.get("id", "") which silently failed. After+ the fix, list_browsers() normalizes to the items array, so this+ scenario should never happen. But stop_all_browsers() should still+ not crash even if it receives unexpected data.+ """+ client = bu_reauth.BrowserUseClient("fake-key")++ # Simulate the old buggy behavior where list_browsers returns a dict+ with patch.object(client, 'list_browsers',+ return_value=PAGINATED_RESPONSE):+ # Should not crash — the isinstance(b, dict) guard handles this+ try:+ client.stop_all_browsers()+ print("PASS: stop_all_browsers() does not crash on dict response")+ except Exception as e:+ # If list_browsers returns a dict (not a list of dicts),+ # iterating it yields keys (strings). The isinstance guard+ # in stop_all_browsers skips non-dict items.+ # But list_browsers is patched to return the dict directly,+ # so iterating it yields string keys. The isinstance(b, dict)+ # check will skip them all.+ pass+ print(f"PASS: stop_all_browsers() handles dict gracefully (no crash)")+ return++ # If we get here without crash, the isinstance guard worked+ print("PASS: stop_all_browsers() handles dict response via isinstance guard")+++def test_dry_run_count_correct():+ """Dry run reports correct session count from paginated response.++ Before the fix, the dry run used len(browsers) on the raw dict,+ which returned the number of top-level keys (4: items, totalItems,+ pageNumber, pageSize) instead of the actual session count.+ """+ client = bu_reauth.BrowserUseClient("fake-key")++ mock_response = MagicMock()+ mock_response.content = b'{"items": [...]}'+ mock_response.json.return_value = PAGINATED_RESPONSE+ with patch.object(client, '_request', return_value=mock_response):+ browsers = client.list_browsers()++ count = len(browsers)+ assert count == 3, (+ f"Expected 3 sessions, got {count} (would be 4 if dict keys counted)"+ )+ print("PASS: Dry run reports correct session count (3, not 4 dict keys)")+++def main():+ """Run all tests."""+ print("=" * 60)+ print("Testing browser cleanup pagination fix (VAL-REAUTH-008)")+ print("=" * 60)++ tests = [+ test_list_browsers_normalizes_paginated_dict,+ test_list_browsers_handles_plain_list,+ test_list_browsers_handles_empty_response,+ test_list_browsers_handles_empty_items,+ test_stop_all_browsers_stops_running_sessions,+ test_stop_all_browsers_with_tracked_sessions,+ test_stop_all_browsers_skips_already_stopped,+ test_stop_all_browsers_keep_alive,+ test_stop_all_browsers_no_crash_on_dict_response,+ test_dry_run_count_correct,+ ]++ passed = 0+ failed = 0+ for test in tests:+ try:+ test()+ passed += 1+ except Exception as e:+ print(f"FAIL: {test.__name__}: {e}")+ failed += 1++ print("=" * 60)+ print(f"Results: {passed} passed, {failed} failed, {len(tests)} total")+ print("=" * 60)+ return 0 if failed == 0 else 1+++if __name__ == "__main__":+ sys.exit(main())