diff --git a/tests/app/test_recorded_transcript_reader.py b/tests/app/test_recorded_transcript_reader.py index 617a0ed3f..a751a44dd 100644 --- a/tests/app/test_recorded_transcript_reader.py +++ b/tests/app/test_recorded_transcript_reader.py @@ -8,7 +8,7 @@ TranscriptSnapshotUpdate, decode_updates, ) -from agent_comms.text_route_update import encode_updates +from agent_comms.execution.session_updates import encode_updates from agent_comms.coordination.errors import HistoricalSourceChanged, ReloadRequired from agent_comms.execution.session_file import RegistrySessionLocation from agent_comms.execution.transcript_sources import ( diff --git a/tests/app/test_transcript_recipient.py b/tests/app/test_transcript_recipient.py index cdcba9592..96b23015d 100644 --- a/tests/app/test_transcript_recipient.py +++ b/tests/app/test_transcript_recipient.py @@ -3,7 +3,7 @@ from dataclasses import replace from agent_comms.acp_extension import TranscriptSnapshotUpdate -from agent_comms.text_route_update import encode_updates +from agent_comms.execution.session_updates import encode_updates from agent_comms.execution.session_file import RegistrySessionLocation from agent_comms.projects.checkouts import Checkout from agent_comms.registry.threads import NativeThread diff --git a/tests/model/test_session_updates_after_binding_change.py b/tests/model/test_session_updates_after_binding_change.py index 89e5432fe..76f87c8fa 100644 --- a/tests/model/test_session_updates_after_binding_change.py +++ b/tests/model/test_session_updates_after_binding_change.py @@ -1,9 +1,7 @@ """A session update that was being validated when its session or process was replaced publishes nothing.""" from acp import schema -from agent_comms.text_route_update import ( - TextRouteUpdate, - encode_updates, -) +from agent_comms.execution.session_updates import encode_updates +from agent_comms.text_route_update import TextRouteUpdate from agent_comms.messaging.routing import MessageRoute from support.agents import RecordedEvents, deliver diff --git a/tests/support/agents.py b/tests/support/agents.py index 267e62c9c..1104f3066 100644 --- a/tests/support/agents.py +++ b/tests/support/agents.py @@ -148,7 +148,7 @@ def queued_session_response(session: str, *, next_owner: bool = False): from agent_comms.execution.child_process import ProcessIdentity from agent_comms.nominal.field_codec import InputId from agent_comms.registry.thread_identity import ClaimedIdentity, RecordedThreadIncarnation - from agent_comms.text_route_update import encode_updates + from agent_comms.execution.session_updates import encode_updates generation = 2 if next_owner else 1 scope = LiveQueueScope( diff --git a/tests/support/coordination.py b/tests/support/coordination.py index a7af504e5..95622b190 100644 --- a/tests/support/coordination.py +++ b/tests/support/coordination.py @@ -5,7 +5,7 @@ from pathlib import Path from agent_comms.acp_extension import CoordinationChangedUpdate -from agent_comms.text_route_update import encode_updates +from agent_comms.execution.session_updates import encode_updates from agent_comms.execution.child_process import ProcessIdentity from agent_comms.execution.context_reading import ContextReading, UnmeasuredContext from agent_comms.projects.checkouts import Checkout diff --git a/tests/support/core_adapters/mcp_turn_boundaries.py b/tests/support/core_adapters/mcp_turn_boundaries.py index a323d594e..ba363425c 100644 --- a/tests/support/core_adapters/mcp_turn_boundaries.py +++ b/tests/support/core_adapters/mcp_turn_boundaries.py @@ -20,7 +20,7 @@ McpClientAcknowledgementUpdate, TurnChangedUpdate, ) -from agent_comms.text_route_update import encode_updates +from agent_comms.execution.session_updates import encode_updates from agent_comms.comms import wire from agent_comms.coordination.coordinator import Coordination from agent_comms.execution.launch_runtime import ThreadLaunch diff --git a/tests/support/core_adapters/typed_turn_events.py b/tests/support/core_adapters/typed_turn_events.py index 1eca92169..8c32a8c7d 100644 --- a/tests/support/core_adapters/typed_turn_events.py +++ b/tests/support/core_adapters/typed_turn_events.py @@ -15,14 +15,14 @@ import agent_comms.agent_events as agent_events from agent_comms.agent_event_updates import AcpEventConsumer from agent_comms.coordination.coordinator import Coordination -from agent_comms.execution.controller import SessionUpdateSink from agent_comms.nominal.field_codec import AcpSessionId, TurnId +from agent_comms.transport.acp_update_sink import AcpUpdateSink from mcp_turn_boundaries import turn_source SESSION = AcpSessionId("pilot") -class Pipe(SessionUpdateSink): +class Pipe(AcpUpdateSink): async def publish_update(self, *, session_id, update): print(json.dumps({"jsonrpc": "2.0", "method": "session/update", "params": { "sessionId": session_id, diff --git a/tests/support/peers/delayed_load.py b/tests/support/peers/delayed_load.py index 67745ff31..00454f5d4 100644 --- a/tests/support/peers/delayed_load.py +++ b/tests/support/peers/delayed_load.py @@ -23,7 +23,7 @@ CoordinationChangedUpdate, TranscriptSnapshotUpdate, ) -from agent_comms.text_route_update import encode_updates +from agent_comms.execution.session_updates import encode_updates from agent_comms.execution.session_file import RegistrySessionLocation from base import Peer diff --git a/tests/support/ui.py b/tests/support/ui.py index 25f141dee..7c6085c0b 100644 --- a/tests/support/ui.py +++ b/tests/support/ui.py @@ -10,8 +10,8 @@ import asyncio import inspect from abc import ABC, abstractmethod -from collections.abc import Callable, Iterator -from contextlib import contextmanager +from collections.abc import AsyncIterator, Callable +from contextlib import asynccontextmanager from dataclasses import dataclass from pathlib import Path from typing import TYPE_CHECKING @@ -509,8 +509,8 @@ def core_service(self): def failure(self) -> BaseException | None: return self.app._exception or self.errors.unexpected(self) - @contextmanager - def reporting_error(self) -> Iterator[None]: + @asynccontextmanager + async def reporting_error(self) -> AsyncIterator[None]: """The enclosed steps show exactly one error on purpose; it is acknowledged when they end, and any second error saved in the same steps fails the test instead of hiding behind it.""" if unexpected := self.errors.unexpected(self): @@ -519,6 +519,10 @@ def reporting_error(self) -> Iterator[None]: self.errors = ExpectsErrors() try: yield + await self.until( + lambda: frozenset(self.reported_errors()) - before, + why="the expected error reported", + ) finally: saved = frozenset(self.reported_errors()) self.errors = RefusesErrors(saved) diff --git a/tests/support/warm_helpers.py b/tests/support/warm_helpers.py index a24b4802d..c604c1da1 100644 --- a/tests/support/warm_helpers.py +++ b/tests/support/warm_helpers.py @@ -156,7 +156,8 @@ def become_helper(numbers: list[int], received: list[int], *, pipe: int, and one that does starts its own tracker on demand.""" os.environ.clear() os.environ.update(environment) - moved = [fcntl.fcntl(fd, fcntl.F_DUPFD_CLOEXEC, 1024) for fd in received] + # Preserve every source before restoring targets, without assuming a file limit. + moved = [fcntl.fcntl(fd, fcntl.F_DUPFD_CLOEXEC, max(numbers) + 1) for fd in received] for fd in received: os.close(fd) for fd, number in zip(moved, numbers): diff --git a/tests/textual/test_application_actions_in_the_running_app.py b/tests/textual/test_application_actions_in_the_running_app.py index 4449a257d..3233ce0ce 100644 --- a/tests/textual/test_application_actions_in_the_running_app.py +++ b/tests/textual/test_application_actions_in_the_running_app.py @@ -224,7 +224,7 @@ async def test_a_settings_file_that_cannot_be_written_is_reported_kept_dirty_and original = path.read_bytes() path.unlink() path.mkdir() - with ui.reporting_error(): + async with ui.reporting_error(): app.saved_settings.set(SETTINGS.ui.footer, False) await app.saved_settings.save() assert app.saved_settings.unsaved and path.is_dir() diff --git a/tests/textual/test_cursor_request_results.py b/tests/textual/test_cursor_request_results.py index 97e3a752a..65c401c6e 100644 --- a/tests/textual/test_cursor_request_results.py +++ b/tests/textual/test_cursor_request_results.py @@ -10,7 +10,7 @@ CursorEnvelope, VerifiedCursorObservation, ) -from agent_comms.text_route_update import encode_updates +from agent_comms.execution.session_updates import encode_updates from agent_comms.execution.child_process import ProcessIdentity from agent_comms.nominal.field_codec import AcpSessionId, RootId from agent_comms.execution.native_input_record import ( diff --git a/tests/textual/test_error_notifier.py b/tests/textual/test_error_notifier.py index e6667619d..b9e15e85c 100644 --- a/tests/textual/test_error_notifier.py +++ b/tests/textual/test_error_notifier.py @@ -9,7 +9,7 @@ async def test_an_error_with_markup_brackets_is_shown_verbatim(workspace): async with workspace.app(size=(110, 30)) as ui: app, pilot = ui.textual record_notifications(app) - with ui.reporting_error(): + async with ui.reporting_error(): app.show_error(ValueError("bad [bold] value"), "Settings") await pilot.pause() # Toad presents notifications through TerminalAttention, which parsed markup and crashed on brackets. diff --git a/tests/textual/test_initial_session_title.py b/tests/textual/test_initial_session_title.py index a3739c017..a5674adc2 100644 --- a/tests/textual/test_initial_session_title.py +++ b/tests/textual/test_initial_session_title.py @@ -5,7 +5,7 @@ import pytest from acp.schema import AgentCapabilities, InitializeResponse, LoadSessionResponse, NewSessionResponse -from agent_comms.text_route_update import encode_updates +from agent_comms.execution.session_updates import encode_updates from support.agents import command from support.coordination import coordination_fact diff --git a/tests/textual/test_input_failure_draft.py b/tests/textual/test_input_failure_draft.py index 454212f30..7f8e074cf 100644 --- a/tests/textual/test_input_failure_draft.py +++ b/tests/textual/test_input_failure_draft.py @@ -5,13 +5,15 @@ from acp.exceptions import RequestError from agent_comms.execution.acp_failure import ( + ACPFailure, InputFailedUpdate, RequestFailedUpdate, ) -from agent_comms.text_route_update import encode_updates +from agent_comms.execution.session_updates import encode_updates from agent_comms.execution.acp_failure import BackendDeliveryFailure from support.agents import deliver +from toad.command_catalog import CommandCatalog from toad.conversation_submission import ImmediateInputSubmission from toad.core.input_events import UserInputSubmitted @@ -38,9 +40,10 @@ async def test_a_prompt_whose_request_fails_is_restored_to_the_composer(workspac receiver = ui.detached_agent("fixture") ui.attach_agent(receiver) await ui.write_draft("") - with patch.object(receiver.process, "prompt", side_effect=error): - await receiver.controller.submit("rpc prompt") - await ui.until(lambda: ui.draft() == "rpc prompt", why="the failed request's text back in the draft") + async with ui.reporting_error(): + with patch.object(receiver.process, "prompt", side_effect=error): + await receiver.controller.submit("rpc prompt") + await ui.until(lambda: ui.draft() == "rpc prompt", why="the failed request's text back in the draft") if isinstance(error, ConnectionError): failure = receiver.session.disposition.failure assert "Unconfirmed" in failure.details @@ -64,18 +67,22 @@ async def test_a_provider_error_fails_its_turn_and_keeps_the_thread_and_model_se ui.attach_agent(receiver) receiver.configuration.receive([schema.SessionConfigOptionSelect.model_validate(MODEL_ADVERTISEMENT)]) disposition = receiver.session.disposition - assert ui.conversation.model_selection_available + assert receiver.configuration.model.available await ui.write_draft("") - with patch.object(receiver.process, "prompt", - side_effect=RequestError.internal_error({"details": "WebSocket error"})): - await receiver.controller.submit("rpc prompt") - await ui.until(lambda: ui.draft() == "rpc prompt", why="the failed request's text back in the draft") - await deliver(receiver, { - "sessionUpdate": "session_info_update", - "_meta": encode_updates(RequestFailedUpdate(BackendDeliveryFailure("provider refused the model")))}) - await ui.step() + async with ui.reporting_error(): + with patch.object(receiver.process, "prompt", + side_effect=RequestError.internal_error({"details": "WebSocket error"})): + await receiver.controller.submit("rpc prompt") + await ui.until(lambda: ui.draft() == "rpc prompt", why="the failed request's text back in the draft") + async with ui.reporting_error(): + await deliver(receiver, { + "sessionUpdate": "session_info_update", + "_meta": encode_updates(RequestFailedUpdate(ACPFailure.from_detail(None, "provider refused the model")))}) + await ui.step() assert receiver.session.disposition is disposition - assert ui.conversation.model_selection_available + assert receiver.configuration.model.available + await CommandCatalog.execute("/model", ui.conversation) + await ui.until(lambda: ui.conversation.prompt.model_switcher.is_open, why="model selection after provider failure") await receiver.stop() @@ -85,10 +92,11 @@ async def test_a_prompt_the_agent_cannot_be_reached_for_is_restored_to_the_compo receiver = ui.detached_agent("fixture") ui.attach_agent(receiver) await ui.write_draft("") - with patch.object(receiver.process, "prompt", side_effect=FileNotFoundError(2, "agent socket disappeared")): - worker = await ImmediateInputSubmission("the local failure prompt", ()).execute(conversation.submissions) - await worker.wait() - await ui.until(lambda: ui.draft() == "the local failure prompt", why="the failed prompt back in the draft") + async with ui.reporting_error(): + with patch.object(receiver.process, "prompt", side_effect=FileNotFoundError(2, "agent socket disappeared")): + worker = await ImmediateInputSubmission("the local failure prompt", ()).execute(conversation.submissions) + await worker.wait() + await ui.until(lambda: ui.draft() == "the local failure prompt", why="the failed prompt back in the draft") await receiver.stop() @@ -193,9 +201,10 @@ async def test_a_send_whose_reply_cannot_be_read_returns_the_draft(workspace): await ui.open_direct("peer", actor=workspace.viewer) chat = ui.app.selected_session.query_one(CommsChatView) unreadable = UnsupportedFieldType("Unsupported JSON type: ToolResult") - with patch.object(Observing, "send_user_message", side_effect=unreadable), ui.reporting_error(): - worker = await chat.submissions.submit(UserInputSubmitted("UNREADABLE_REPLY_SEND")) - with pytest.raises(WorkerFailed): - await worker.wait() + with patch.object(Observing, "send_user_message", side_effect=unreadable): + async with ui.reporting_error(): + worker = await chat.submissions.submit(UserInputSubmitted("UNREADABLE_REPLY_SEND")) + with pytest.raises(WorkerFailed): + await worker.wait() assert not chat.submissions.delivering assert chat.prompt.text == "UNREADABLE_REPLY_SEND" diff --git a/tests/textual/test_observation_shutdown.py b/tests/textual/test_observation_shutdown.py index e9cc7b5f5..2f389b0f7 100644 --- a/tests/textual/test_observation_shutdown.py +++ b/tests/textual/test_observation_shutdown.py @@ -68,7 +68,7 @@ async def test_an_observer_dying_during_shutdown_still_releases_its_resources(wo process = access.observation.observed().process assert not (await asyncio.wait_for(process.read(empty_read(workspace)), 30)).outcomes reading = None - with ui.reporting_error(): + async with ui.reporting_error(): try: async with held(process) as pid: reading = asyncio.create_task(process.read(empty_read(workspace))) diff --git a/tests/textual/test_quit_with_unsaved_settings.py b/tests/textual/test_quit_with_unsaved_settings.py index 73941bba1..f3e2fb7e4 100644 --- a/tests/textual/test_quit_with_unsaved_settings.py +++ b/tests/textual/test_quit_with_unsaved_settings.py @@ -37,7 +37,7 @@ async def test_quit_that_cannot_save_stays_open_and_a_retry_saves_the_edit_and_e original = path.read_bytes() path.unlink() path.mkdir() - with ui.reporting_error(): + async with ui.reporting_error(): await ui.press('home', 'shift+end', '1', '3', '7', 'ctrl+q') await ui.until_shows('Failed to write') assert app.is_running and not app._exit diff --git a/tests/textual/test_sent_message_drawn_once.py b/tests/textual/test_sent_message_drawn_once.py index 44b53dc55..bbd645418 100644 --- a/tests/textual/test_sent_message_drawn_once.py +++ b/tests/textual/test_sent_message_drawn_once.py @@ -7,7 +7,7 @@ NoQueueScope, UnidentifiedInputStarted, ) -from agent_comms.text_route_update import encode_updates +from agent_comms.execution.session_updates import encode_updates from agent_comms.execution.session_file import RegistrySessionLocation from agent_comms.nominal.field_codec import InputId from agent_comms.projects.checkouts import Checkout diff --git a/tests/textual/test_session_agent_selection.py b/tests/textual/test_session_agent_selection.py index 46d467192..cd60b7aa2 100644 --- a/tests/textual/test_session_agent_selection.py +++ b/tests/textual/test_session_agent_selection.py @@ -3,7 +3,7 @@ import pytest from agent_comms.acp_extension import CoordinationChangedUpdate -from agent_comms.text_route_update import encode_updates +from agent_comms.execution.session_updates import encode_updates from agent_comms.execution.child_process import ProcessIdentity from agent_comms.ui_model.delivery import DeliveryView diff --git a/tests/textual/test_session_start_failure.py b/tests/textual/test_session_start_failure.py index ca7ff809a..0a144b7eb 100644 --- a/tests/textual/test_session_start_failure.py +++ b/tests/textual/test_session_start_failure.py @@ -21,7 +21,8 @@ async def test_a_failed_mount_names_the_session_and_leaves_no_dead_view(workspac async with workspace.app(size=(110, 35)) as ui: app, _ = ui.textual before = set(app.workspace_sessions.views) - with ui.reporting_error(), pytest.raises(SessionStartFailed): - await app.session_navigation.new( - await saved_tab(app), lambda: FailsToStart(Checkout(workspace.project))) + async with ui.reporting_error(): + with pytest.raises(SessionStartFailed): + await app.session_navigation.new( + await saved_tab(app), lambda: FailsToStart(Checkout(workspace.project))) assert set(app.workspace_sessions.views) == before diff --git a/tests/textual/test_transcript_lifecycle.py b/tests/textual/test_transcript_lifecycle.py index 52ad8271b..6abc83cef 100644 --- a/tests/textual/test_transcript_lifecycle.py +++ b/tests/textual/test_transcript_lifecycle.py @@ -124,7 +124,7 @@ def record_unhandled(loop: asyncio.AbstractEventLoop, context: dict[str, object] async def failed_read(): raise error - with ui.reporting_error(): + async with ui.reporting_error(): worker = history.reserve_source_work().schedule(history, failed_read) await ui.until(lambda: worker.is_finished, why="the failed pager finishes") logs = set(get_errors().glob("*.txt")) - before