diff --git a/src/toad/acp/agent_process.py b/src/toad/acp/agent_process.py index e1ce7831a..b985f69b4 100644 --- a/src/toad/acp/agent_process.py +++ b/src/toad/acp/agent_process.py @@ -5,8 +5,8 @@ import json import os from collections.abc import Callable -from contextlib import ExitStack from contextvars import ContextVar +from functools import partial from pathlib import Path from typing import Optional @@ -70,8 +70,7 @@ async def start(self, agent, processes: tuple[AgentProcess, ...]=()): env = os.environ.copy() cwd = str(agent.project_root_path.resolve()) selection = RouteSelection.capture(agent.controller.transcripts.access.launch_root.path) - with ExitStack() as acquisition: - acquisition.enter_context(selection.route.hold_client_route()) + with selection.route.hold_client_route(): if not agent.session.accepts_updates: return self for connection in dict.fromkeys((self, *processes)): @@ -95,6 +94,8 @@ async def start(self, agent, processes: tuple[AgentProcess, ...]=()): async def prompt(self, agent, content, metadata: dict[str, JsonValue], require: Callable[[], None]): """The agent-comms-acp child writes the prompt under Core's maintenance gate.""" + access = agent.controller.transcripts.access + await access.preparation.run_thread(partial(access.require, self.route_selection)) require() return await self.rpc.prompt(agent.session_id, content, **metadata) diff --git a/src/toad/comms_root.py b/src/toad/comms_root.py index 75198f65a..fdf00ee28 100644 --- a/src/toad/comms_root.py +++ b/src/toad/comms_root.py @@ -14,7 +14,6 @@ import asyncio import threading from collections.abc import Callable, Mapping -from contextlib import ExitStack from dataclasses import dataclass, field from functools import partial from pathlib import Path @@ -372,8 +371,7 @@ def require(self, selected: RouteSelection) -> Observing: def open_selected(self, selected: RouteSelection) -> Observing: def attach() -> Observing: previous = self.observation - with ExitStack() as acquisition: - acquisition.enter_context(selected.route.hold_client_route()) + with selected.route.hold_client_route(): service = Comms(selected.route) if service.root.resolve() != selected.root or RouteSelection.capture() != selected: raise RouteChangedError("Comms route changed while opening the service")