tenseleyflow/loader / e2158c6

Browse files

Adopt runtime entrypoint helpers in agent facade

Authored by espadonne
SHA
e2158c6c4f70e47f42f812d55c4c4a3867e2a4d8
Parents
15d4f6b
Tree
7717944

1 changed file

StatusFile+-
M src/loader/agent/loop.py 16 60
src/loader/agent/loop.pymodified
@@ -1,17 +1,13 @@
11
 """The main agent loop."""
22
 
3
-import asyncio
4
-import contextlib
53
 from collections.abc import AsyncIterator, Awaitable, Callable
64
 from dataclasses import dataclass
75
 from pathlib import Path
86
 
97
 from ..context.project import ProjectContext, detect_project
108
 from ..llm.base import LLMBackend, Message
11
-from ..runtime.bootstrap import build_runtime_bootstrap_source
129
 from ..runtime.capabilities import resolve_backend_capability_profile
1310
 from ..runtime.events import AgentEvent, TurnSummary
14
-from ..runtime.launcher import build_runtime_launcher
1511
 from ..runtime.permissions import (
1612
     PermissionMode,
1713
     build_permission_policy,
@@ -19,13 +15,15 @@ from ..runtime.permissions import (
1915
 )
2016
 from ..runtime.public_shell import (
2117
     SteeringMailbox,
22
-    build_event_emitter,
2318
     build_fresh_runtime_session_install,
2419
     build_runtime_few_shot_examples,
2520
     build_runtime_system_message,
2621
     clear_runtime_shell_history,
2722
     refresh_runtime_shell_capability_profile,
2823
     resume_runtime_shell_session,
24
+    run_runtime_shell,
25
+    run_runtime_shell_explore,
26
+    stream_runtime_shell,
2927
 )
3028
 from ..runtime.safeguards import RuntimeSafeguards
3129
 from ..runtime.workflow import WorkflowMode
@@ -228,11 +226,6 @@ class Agent:
228226
 
229227
         self.steering.queue(message)
230228
 
231
-    def build_runtime_source(self):
232
-        """Build the explicit runtime bootstrap source for public entrypoints."""
233
-
234
-        return build_runtime_bootstrap_source(self)
235
-
236229
     def drain_steering_messages(self) -> list[str]:
237230
         """Drain queued runtime steering messages."""
238231
 
@@ -262,56 +255,22 @@ class Agent:
262255
         Returns:
263256
             The final response text
264257
         """
265
-        emit = build_event_emitter(on_event)
266
-
267
-        # Mark agent as running (enables steering)
268
-        self.steering.mark_running()
269
-        try:
270
-            launcher = build_runtime_launcher(self.build_runtime_source())
271
-            return await launcher.run_user_message(
272
-                user_message,
273
-                emit,
274
-                on_confirmation=on_confirmation,
275
-                on_user_question=on_user_question,
276
-                use_plan=use_plan,
277
-            )
278
-        finally:
279
-            self.steering.mark_idle()
258
+        return await run_runtime_shell(
259
+            self,
260
+            user_message,
261
+            on_event=on_event,
262
+            on_confirmation=on_confirmation,
263
+            on_user_question=on_user_question,
264
+            use_plan=use_plan,
265
+        )
280266
 
281267
     async def run_streaming(
282268
         self,
283269
         user_message: str,
284270
     ) -> AsyncIterator[AgentEvent]:
285271
         """Run the agent with streaming output from the primary runtime path."""
286
-
287
-        queue: asyncio.Queue[AgentEvent | BaseException | None] = asyncio.Queue()
288
-
289
-        async def on_event(event: AgentEvent) -> None:
290
-            await queue.put(event)
291
-
292
-        async def run_agent() -> None:
293
-            try:
294
-                await self.run(user_message, on_event=on_event)
295
-            except BaseException as exc:  # pragma: no cover - propagated below
296
-                await queue.put(exc)
297
-            finally:
298
-                await queue.put(None)
299
-
300
-        task = asyncio.create_task(run_agent())
301
-        try:
302
-            while True:
303
-                item = await queue.get()
304
-                if item is None:
305
-                    break
306
-                if isinstance(item, BaseException):
307
-                    raise item
308
-                yield item
309
-            await task
310
-        finally:
311
-            if not task.done():
312
-                task.cancel()
313
-                with contextlib.suppress(asyncio.CancelledError):
314
-                    await task
272
+        async for event in stream_runtime_shell(self, user_message):
273
+            yield event
315274
 
316275
     async def run_explore(
317276
         self,
@@ -321,15 +280,12 @@ class Agent:
321280
         fresh: bool = False,
322281
     ) -> str:
323282
         """Run one read-only explore query outside the main workflow runtime."""
324
-        emit = build_event_emitter(on_event)
325
-
326
-        launcher = build_runtime_launcher(self.build_runtime_source())
327
-        self.last_turn_summary = await launcher.run_explore(
283
+        return await run_runtime_shell_explore(
284
+            self,
328285
             user_message,
329
-            emit,
286
+            on_event=on_event,
330287
             fresh=fresh,
331288
         )
332
-        return self.last_turn_summary.final_response
333289
 
334290
     def clear_history(self) -> None:
335291
         """Clear conversation history."""