diff --git a/src/extensions/manager.rs b/src/extensions/manager.rs index d50ba7163a3..d32713bebbe 100644 --- a/src/extensions/manager.rs +++ b/src/extensions/manager.rs @@ -2713,14 +2713,16 @@ impl ExtensionManager { .unwrap_or(""); if archive_names.is_wasm(filename) { - let mut data = Vec::with_capacity(entry.size() as usize); + let mut data = + Vec::with_capacity((entry.size() as usize).min(MAX_ENTRY_SIZE as usize)); std::io::Read::read_to_end(&mut entry.by_ref().take(MAX_ENTRY_SIZE), &mut data) .map_err(|e| ExtensionError::InstallFailed(e.to_string()))?; std::fs::write(target_wasm, &data) .map_err(|e| ExtensionError::InstallFailed(e.to_string()))?; found_wasm = true; } else if archive_names.is_caps(filename) { - let mut data = Vec::with_capacity(entry.size() as usize); + let mut data = + Vec::with_capacity((entry.size() as usize).min(MAX_ENTRY_SIZE as usize)); std::io::Read::read_to_end(&mut entry.by_ref().take(MAX_ENTRY_SIZE), &mut data) .map_err(|e| ExtensionError::InstallFailed(e.to_string()))?; std::fs::write(target_caps, &data) @@ -9485,6 +9487,10 @@ mod tests { // have their colon URL-encoded to %3A, as this breaks the validation endpoint. // Previously: form_urlencoded::byte_serialize encoded the token, causing 404s. // Fixed by removing URL-encoding and using the token directly. + // + // Hold the env mutex so concurrent tests that override + // IRONCLAW_TEST_TELEGRAM_API_BASE_URL don't change the base URL mid-read. + let _guard = crate::config::helpers::lock_env(); let token = "123456789:AABBccDDeeFFgg_Test-Token"; let url = telegram_bot_api_url(token, "getMe"); diff --git a/tests/e2e/conftest.py b/tests/e2e/conftest.py index bc1613445c5..59ef8984a37 100644 --- a/tests/e2e/conftest.py +++ b/tests/e2e/conftest.py @@ -1112,6 +1112,11 @@ async def page(ironclaw_server, browser): await pg.goto(f"{ironclaw_server}/?token={AUTH_TOKEN}") # Wait for the app to initialize (auth screen hidden, SSE connected) await pg.wait_for_selector("#auth-screen", state="hidden", timeout=15000) + # Wait for SSE connection (onopen sets sseHasConnectedBefore = true) + await pg.wait_for_function( + "() => typeof sseHasConnectedBefore !== 'undefined' && sseHasConnectedBefore === true", + timeout=10000, + ) yield pg await context.close() diff --git a/tests/e2e/helpers.py b/tests/e2e/helpers.py index d5573813294..747b3e92689 100644 --- a/tests/e2e/helpers.py +++ b/tests/e2e/helpers.py @@ -19,8 +19,6 @@ # Auth "auth_screen": "#auth-screen", "token_input": "#token-input", - # Connection - "sse_status": "#sse-status", # Tabs "tab_button": '.tab-bar button[data-tab="{tab}"]', "tab_panel": "#tab-{tab}", diff --git a/tests/e2e/scenarios/test_connection.py b/tests/e2e/scenarios/test_connection.py index 2ecafd041ea..1c1286edbd3 100644 --- a/tests/e2e/scenarios/test_connection.py +++ b/tests/e2e/scenarios/test_connection.py @@ -6,12 +6,11 @@ async def test_page_loads_and_connects(page): """After auth, the app shows Connected status and all tabs.""" - # Connection status - status = page.locator(SEL["sse_status"]) - await status.wait_for(state="visible", timeout=10000) - text = await status.text_content() - assert text is not None - assert "connect" in text.lower(), f"Expected 'Connected', got '{text}'" + # Connection status — verify SSE has connected via the JS flag set in onopen. + await page.wait_for_function( + "() => typeof sseHasConnectedBefore !== 'undefined' && sseHasConnectedBefore === true", + timeout=10000, + ) # All 6 main tabs visible for tab in TABS: diff --git a/tests/e2e/scenarios/test_oauth_refresh.py b/tests/e2e/scenarios/test_oauth_refresh.py index cd38f88fa14..dfc64234800 100644 --- a/tests/e2e/scenarios/test_oauth_refresh.py +++ b/tests/e2e/scenarios/test_oauth_refresh.py @@ -106,7 +106,7 @@ async def _wait_for_gmail_tool_call(base_url: str, thread_id: str, timeout: floa response.raise_for_status() history = response.json() - pending = history.get("pending_approval") + pending = history.get("pending_gate") if pending and pending["request_id"] not in approved_request_ids: await _approve_pending_request(base_url, thread_id, pending["request_id"]) approved_request_ids.add(pending["request_id"]) @@ -137,7 +137,7 @@ async def _wait_for_tool_call( response.raise_for_status() history = response.json() - pending = history.get("pending_approval") + pending = history.get("pending_gate") if pending and pending["request_id"] not in approved_request_ids: await _approve_pending_request(base_url, thread_id, pending["request_id"]) approved_request_ids.add(pending["request_id"]) diff --git a/tests/e2e/scenarios/test_owner_scope.py b/tests/e2e/scenarios/test_owner_scope.py index 5cb9df2a261..08ca744dfe2 100644 --- a/tests/e2e/scenarios/test_owner_scope.py +++ b/tests/e2e/scenarios/test_owner_scope.py @@ -136,12 +136,12 @@ async def _wait_for_http_thread(base_url: str, title_fragment: str, timeout: flo ) -async def _wait_for_pending_approval( +async def _wait_for_pending_gate( base_url: str, thread_id: str, timeout: float = 20.0, ) -> dict: - """Poll chat history until the thread exposes a pending approval payload.""" + """Poll chat history until the thread exposes a pending gate payload.""" for _ in range(int(timeout * 2)): response = await api_get( base_url, @@ -149,11 +149,11 @@ async def _wait_for_pending_approval( timeout=10, ) response.raise_for_status() - pending = response.json().get("pending_approval") + pending = response.json().get("pending_gate") if pending: return pending await _poll_sleep() - raise AssertionError(f"Thread '{thread_id}' did not expose a pending approval") + raise AssertionError(f"Thread '{thread_id}' did not expose a pending gate") async def _approve_pending_request(base_url: str, thread_id: str, request_id: str) -> None: @@ -256,7 +256,7 @@ async def test_http_created_full_job_routine_is_visible_in_web_after_approval( ) thread_id = await _wait_for_http_thread(ironclaw_server, routine_name) - pending = await _wait_for_pending_approval(ironclaw_server, thread_id) + pending = await _wait_for_pending_gate(ironclaw_server, thread_id) assert pending["tool_name"] == "routine_create" await _approve_pending_request( ironclaw_server, diff --git a/tests/e2e/scenarios/test_skill_oauth_flow.py b/tests/e2e/scenarios/test_skill_oauth_flow.py index 87b3097fb05..d1d02068452 100644 --- a/tests/e2e/scenarios/test_skill_oauth_flow.py +++ b/tests/e2e/scenarios/test_skill_oauth_flow.py @@ -131,7 +131,7 @@ async def _wait_for_response( # Auto-approve pending tool calls if requested if auto_approve: - pending = history.get("pending_approval") + pending = history.get("pending_gate") if pending and pending["request_id"] not in approved: await api_post( base_url, diff --git a/tests/e2e/scenarios/test_sse_reconnect.py b/tests/e2e/scenarios/test_sse_reconnect.py index 8cb5bea1350..66f3da9d2a1 100644 --- a/tests/e2e/scenarios/test_sse_reconnect.py +++ b/tests/e2e/scenarios/test_sse_reconnect.py @@ -25,15 +25,17 @@ async def _open_gateway_page(browser, base_url: str): async def _wait_for_connected(page, *, timeout: int = 10000) -> None: - """Wait until the frontend reports an active SSE connection.""" - status = page.locator(SEL["sse_status"]) - await status.wait_for(state="visible", timeout=timeout) - deadline = asyncio.get_running_loop().time() + (timeout / 1000) - while asyncio.get_running_loop().time() < deadline: - if await status.text_content() == "Connected": - return - await asyncio.sleep(0.2) - raise AssertionError("SSE status did not return to Connected before timeout") + """Wait until the frontend reports an active SSE connection. + + Uses the ``sseHasConnectedBefore`` JS flag which is set to ``true`` + inside ``EventSource.onopen``. This is more reliable than checking + CSS state on ``#sse-dot`` because the dot starts without the + ``disconnected`` class before SSE even connects. + """ + await page.wait_for_function( + "() => typeof sseHasConnectedBefore !== 'undefined' && sseHasConnectedBefore === true", + timeout=timeout, + ) async def _wait_for_last_event_id(page, *, timeout: int = 15000) -> str: @@ -59,21 +61,21 @@ async def _wait_for_turn_in_history(base_url: str, thread_id: str, expected_resp async def test_sse_status_shows_connected(page): - """SSE status should show Connected after page load.""" - status = page.locator(SEL["sse_status"]) - await status.wait_for(state="visible", timeout=5000) - text = await status.text_content() - assert text == "Connected", f"Expected 'Connected', got '{text}'" + """SSE dot should show connected state after page load.""" + dot = page.locator("#sse-dot") + cls = await dot.get_attribute("class") or "" + assert "disconnected" not in cls, f"Expected connected dot, got class='{cls}'" async def test_sse_reconnect_after_disconnect(page): - """After programmatic disconnect, SSE should reconnect and show Connected.""" + """After programmatic disconnect, SSE should reconnect.""" await _wait_for_connected(page, timeout=5000) await page.evaluate("if (eventSource) eventSource.close()") - await page.evaluate("connectSSE()") + # Reset the flag so _wait_for_connected can detect the new onopen. + # The history-reload path (sseHasConnectedBefore=true on reconnect) + # is covered by test_sse_reconnect_preserves_chat_history. + await page.evaluate("sseHasConnectedBefore = false; connectSSE()") await _wait_for_connected(page, timeout=10000) - status = page.locator(SEL["sse_status"]) - assert await status.text_content() == "Connected" async def test_sse_reconnect_preserves_chat_history(page): diff --git a/tests/e2e/scenarios/test_tool_approval.py b/tests/e2e/scenarios/test_tool_approval.py index 7c88992cda1..5b4510b4550 100644 --- a/tests/e2e/scenarios/test_tool_approval.py +++ b/tests/e2e/scenarios/test_tool_approval.py @@ -47,7 +47,7 @@ async def _wait_for_history( ) assert response.status_code == 200, response.text history = response.json() - pending = history.get("pending_approval") + pending = history.get("pending_gate") turns = history.get("turns", []) latest_response = turns[-1].get("response") if turns else None @@ -259,7 +259,7 @@ async def test_chat_reply_approve_resumes_pending_tool(ironclaw_server): turn_count_at_least=1, ) - assert history.get("pending_approval") is None + assert history.get("pending_gate") is None assert history["turns"][-1]["response"] is not None @@ -309,5 +309,5 @@ async def test_chat_reply_always_auto_approves_next_same_tool(ironclaw_server): turn_count_at_least=2, ) - assert history.get("pending_approval") is None + assert history.get("pending_gate") is None assert len(history["turns"]) >= 2 diff --git a/tests/e2e/scenarios/test_v2_engine_approval_flow.py b/tests/e2e/scenarios/test_v2_engine_approval_flow.py index 25fb9172395..296b5c9b332 100644 --- a/tests/e2e/scenarios/test_v2_engine_approval_flow.py +++ b/tests/e2e/scenarios/test_v2_engine_approval_flow.py @@ -156,9 +156,9 @@ async def _wait_for_approval( *, timeout: float = 45.0, ) -> dict: - """Poll /api/chat/history until pending_approval appears. + """Poll /api/chat/history until pending_gate appears. - Returns the pending_approval dict containing request_id, tool_name, etc. + Returns the pending_gate dict containing request_id, tool_name, etc. """ for _ in range(int(timeout * 2)): r = await api_get( @@ -168,7 +168,7 @@ async def _wait_for_approval( ) r.raise_for_status() history = r.json() - pending = history.get("pending_approval") + pending = history.get("pending_gate") if pending and pending.get("request_id"): return pending await asyncio.sleep(0.5) @@ -179,7 +179,7 @@ async def _wait_for_approval( r = await api_get(base_url, f"/api/chat/history?thread_id={thread_id}", timeout=15) data = r.json() turns = data.get("turns", []) - pending = data.get("pending_approval") + pending = data.get("pending_gate") debug_info = f"turns={len(turns)}, pending={pending}" if turns: last_turn = turns[-1] @@ -191,7 +191,7 @@ async def _wait_for_approval( except Exception as e: debug_info = f"error: {e}" raise AssertionError( - f"Timed out waiting for pending_approval in thread {thread_id}. " + f"Timed out waiting for pending_gate in thread {thread_id}. " f"Debug: {debug_info}" ) @@ -230,15 +230,15 @@ async def _wait_for_response( ) -async def _wait_for_no_pending_approval(base_url: str, thread_id: str, *, timeout: float = 45.0): +async def _wait_for_no_pending_gate(base_url: str, thread_id: str, *, timeout: float = 45.0): for _ in range(int(timeout * 2)): r = await api_get(base_url, f"/api/chat/history?thread_id={thread_id}", timeout=15) r.raise_for_status() history = r.json() - if not history.get("pending_approval"): + if not history.get("pending_gate"): return history await asyncio.sleep(0.5) - raise AssertionError(f"Timed out waiting for pending_approval to clear in thread {thread_id}") + raise AssertionError(f"Timed out waiting for pending_gate to clear in thread {thread_id}") async def _approve( @@ -294,17 +294,17 @@ async def test_same_user_approvals_are_thread_scoped(self, v2_approval_server): approve_a = await _approve(base_url, thread_a, pending_a["request_id"], "approve") assert approve_a.status_code == 202, approve_a.text - await _wait_for_no_pending_approval(base_url, thread_a, timeout=60) + await _wait_for_no_pending_gate(base_url, thread_a, timeout=60) history_b = await api_get(base_url, f"/api/chat/history?thread_id={thread_b}", timeout=15) history_b.raise_for_status() - still_pending_b = history_b.json().get("pending_approval") + still_pending_b = history_b.json().get("pending_gate") assert still_pending_b is not None, history_b.json() assert still_pending_b["request_id"] == pending_b["request_id"] approve_b = await _approve(base_url, thread_b, pending_b["request_id"], "approve") assert approve_b.status_code == 202, approve_b.text - await _wait_for_no_pending_approval(base_url, thread_b, timeout=60) + await _wait_for_no_pending_gate(base_url, thread_b, timeout=60) """Test the v2 engine tool approval lifecycle. @@ -352,14 +352,14 @@ async def test_approval_yes(self, v2_approval_server): last = (turns[-1].get("response") or "").lower() if last and "requires approval" not in last: break - # Also check if pending_approval is cleared (approval processed) - if not history.get("pending_approval"): + # Also check if pending_gate is cleared (approval processed) + if not history.get("pending_gate"): break - # After approval, pending_approval should be cleared - assert history.get("pending_approval") is None, ( - f"After approval, pending_approval should be cleared. " - f"Got: {history.get('pending_approval')}" + # After approval, pending_gate should be cleared + assert history.get("pending_gate") is None, ( + f"After approval, pending_gate should be cleared. " + f"Got: {history.get('pending_gate')}" ) async def test_approval_no(self, v2_approval_server): @@ -409,9 +409,9 @@ async def test_approval_no(self, v2_approval_server): ).lower() # After denial, approval prompt should no longer be pending - assert history.get("pending_approval") is None, ( - f"After denial, pending_approval should be cleared. " - f"Got: {history.get('pending_approval')}" + assert history.get("pending_gate") is None, ( + f"After denial, pending_gate should be cleared. " + f"Got: {history.get('pending_gate')}" ) async def test_approval_always(self, v2_approval_server): diff --git a/tests/e2e/scenarios/test_v2_engine_error_handling.py b/tests/e2e/scenarios/test_v2_engine_error_handling.py index 4cc69f03469..585ade58bf5 100644 --- a/tests/e2e/scenarios/test_v2_engine_error_handling.py +++ b/tests/e2e/scenarios/test_v2_engine_error_handling.py @@ -179,7 +179,7 @@ async def _wait_for_response( # Auto-approve pending approvals so the loop doesn't stall if auto_approve: - pending = history.get("pending_approval") + pending = history.get("pending_gate") if pending: request_id = pending.get("request_id", "") if request_id: