Files
EvoScientist-Multi/EvoScientist/middleware/background.py
T
dinos 01845f4311 refactor: route middleware display events through an injected event sink (#343)
* chore: add pytest-asyncio in auto mode

* test: migrate channel and stream tests to native async

Convert run_async() wrapper tests to plain 'async def test_*' under
pytest-asyncio auto mode. collect_events() in stream_v3_fakes becomes a
coroutine awaited at every call site.

* test: migrate command and model/middleware tests to native async

Convert run_async() wrappers (import, alias, and fixture forms) to plain
'async def test_*'. Multi-call tests merge onto one loop as sequential
awaits; none asserted on loop identity.

* test: migrate TUI, notifier, gateway, and session tests to native async

TUI/notifier/gateway files convert run_async wrappers to plain async
tests. test_sessions.py's unittest.TestCase classes move to
unittest.IsolatedAsyncioTestCase (pytest-asyncio does not await async
methods on plain TestCase; converting blindly would have made ~70 tests
silently vacuous). Its setUpClass keeps a one-shot asyncio.run() since
IsolatedAsyncioTestCase has no async class-level hook. TestLoadingWidget
in test_tui_widgets.py drops its TestCase base for the same reason.

* test: replace direct asyncio.run() calls with native async tests

Convert tests that called asyncio.run() (directly or via a local _run
helper) to plain 'async def test_*'; delete the local helpers.

* test: drop undeclared anyio markers and delete run_async helper

The @pytest.mark.anyio tests relied on anyio being a transitive dep of
httpx; auto-mode pytest-asyncio collects them natively. run_async() and
its fixture are unreferenced after the migration, so remove them —
pytest-asyncio's per-test loop teardown covers the pending-task
cancellation the helper existed for (verified: full suite runs with no
'Event loop is closed' errors or destroyed-task warnings).

* test: add autouse fixture for watcher cleanup

* refactor: remove redundant hasattr calls

* refactor: add typed middleware event sink and thread through assembly

Add MiddlewareEventSink protocol + NoOpSink in middleware/events.py
with a documented any-thread non-blocking contract (contract test uses a
deliberately-slow fake sink). Thread an optional `events` parameter
through create_cli_agent -> _get_default_middleware -> tool selector /
model fallback constructors; subagent stacks are always forced to
NoOpSink.

* refactor: inject a notifier port into async-watcher and background middleware

Add public pre_cancel_watcher() and enqueue_task_notification() to
cli/async_notifier.py and a small NotifierPort protocol
(middleware/notifier.py) that the module satisfies structurally.
AsyncWatcherMiddleware and BackgroundExecutionMiddleware now receive the
port by constructor injection at the composition root, deleting the lazy
'from ..cli import async_notifier' imports and the private
_watcher_by_thread / _enqueue pokes.

* refactor: invert tool-selection ownership onto a frontend event sink

The adaptive tool selector now reports on_tool_selection_started /
on_tool_selection / on_tool_selection_ended to the injected sink instead
of writing four process-global module variables. The frontend sink
(stream/sink.py FrontendEventSink) owns the selected/total/active state
with consume-once + dedup-vs-last-emitted semantics;
stream/tool_selection.py reads that sink object (a ToolSelectionView)
rather than reaching into tool_selector's globals.

Deleted: the 4 module globals, the cross-module mutations in
tool_selection.py, the track_stream_selection flag, the now-vestigial
_ToolSelectionTrackerMiddleware, reset_tool_selection_state_for_tests,
and the autouse conftest fixture. The sink is threaded from the two
interactive frontends through create_runtime_gateways ->
LocalGraphGateway (read side) and _load_agent -> create_cli_agent (write
side); subagent / headless stacks get NoOpSink.

* refactor: route model-fallback narration through the injected event sink

Delete the _ui_emit_fn / set_ui_emit module global and the
..stream.console import from model_fallback.py. The fallback middleware
now reports through its injected sink: the fallback transition via the
structured on_model_fallback (the frontend formats the '-> Falling back
to ...' line), and the surrounding narration (primary-failure header,
per-attempt outcome, exhaustion, non-fallbackable rejection) via
emit_fallback_notice, preserving the exact user-facing text. The TUI
binds its _append_system as the sink's fallback display where it used to
call set_ui_emit (cleared on exit); the Rich CLI's sink prints to the
console. _try_fallbacks / _guard_and_fallback take the sink.

* refactor: declare events on the GraphGateway protocol

Both gateway implementations now carry an explicit events attribute
(LangGraphServerGateway holds None — no frontend renders middleware
events across the HTTP boundary), so the four call sites use plain
attribute access instead of getattr probing an implicit contract.

* refactor: bind fallback display via the closure-scoped concrete sink

The App methods used gateway.events (typed as the read-side view) and
hasattr-probed for the concrete FrontendEventSink API. The enclosing
factory creates that sink two hundred lines up — close over it directly:
no probing, fully typed, and it becomes a constructor parameter
naturally when the App class is hoisted out of the factory.

* fix: end tool selection before fallback handler

* fix: keep fallback display errors non-fatal

* fix: preserve selector suppression for default streams

* fix: restore fallback notice console display

* refactor: consolidate fallback narration events

* refactor: clean middleware event sink plumbing

* fix: type gateway session events

* refactor: make all event protocols runtime-checkable

MiddlewareEventSink already carried @runtime_checkable (the stream
binding guard isinstance-checks it); ToolSelectionView and SessionEvents
now match, so mirroring that pattern against any of the three protocols
works instead of raising TypeError.

* fix(cli): close QuickJS workers after one-shot failures

* fix(cli): honor no-thinking in final output

* fix(channels): report failed startup accurately

* fix(channels): make Telegram cleanup idempotent

* fix(tui): skip command sync during exit

* fix(channels): preserve startup state during retries

* refactor(channels): share pending startup status

* refactor(cli): expose channel startup snapshot

* fix(tui): move channel startup off event loop

* test(channels): release retry gate on assertion failure

---------

Co-authored-by: Xi Zhang <106144707+X-iZhang@users.noreply.github.com>
2026-07-14 22:34:17 +00:00

164 lines
5.9 KiB
Python

"""``BackgroundExecutionMiddleware`` — background-process tools for the main agent.
Mirrors deepagents' ``AsyncSubAgentMiddleware`` shape (a middleware that owns a set of
tools). The tools are stateless wrappers over :mod:`EvoScientist.background`, which holds
the live, process-level registry. They reuse the sandbox's ``validate_command`` so a
background launch cannot bypass the same safety checks as ``execute``.
Naming: these manage OS *processes* (never "job" — that word is reserved-free; async
sub-agents are *tasks*, future cron is *schedules*).
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from langchain.agents.middleware import AgentMiddleware
from langchain.tools import ToolRuntime
from langchain_core.tools import tool
from .. import background, paths
from ..backends import prepare_sandbox_command
if TYPE_CHECKING:
from .notifier import NotifierPort
def _origin_thread_id(runtime: ToolRuntime | None) -> str | None:
"""Best-effort current CLI thread_id, used to route the completion notification."""
try:
return (runtime.config or {}).get("configurable", {}).get("thread_id")
except Exception:
return None
def _notify_done(
proc: background.BgProcess,
origin_thread_id: str | None,
notifier: NotifierPort,
) -> None:
"""Watcher ``on_exit`` hook: enqueue a completion notification via the port.
Skipped for user-stopped processes (the user already knows). The notifier
port owns the notification type, so this module never imports the CLI layer.
"""
if proc.stopped:
return
rc = proc.returncode
if rc == 0:
status = "success"
elif rc is not None and rc < 0:
status = "interrupted" # terminated by a signal
else:
status = "error"
notifier.enqueue_bg_process_notification(
task_id=proc.process_id,
agent_name=proc.name,
status=status,
prompt=proc.command,
origin_cli_thread_id=origin_thread_id,
)
def _make_run_in_background(notifier: NotifierPort, dangerous: bool):
"""Build the ``run_in_background`` tool bound to an injected notifier + policy.
``dangerous`` is captured from ``cfg.dangerous_mode`` at assembly (the agent
is rebuilt when config changes, so the captured value never goes stale), and
the notifier is the injected port used for the completion notification.
"""
@tool(parse_docstring=True)
def run_in_background(
command: str, name: str | None = None, runtime: ToolRuntime = None
) -> str:
"""Launch a long-running shell command in the background and return immediately.
Use for unbounded or very long tasks (model training, large downloads, servers)
that should not block the conversation. Output streams to a log file; poll it with
check_process and stop it with stop_process. For a bounded command that just needs
more time, prefer execute(..., timeout=N) instead of backgrounding.
Args:
command: The shell command to run in the background.
name: Optional short label to recognize the process later.
"""
cwd = str(paths.resolve_virtual_path("/"))
# Same path-rewriting + validation as execute (shared helper) so virtual paths
# resolve to the workspace and the command can't bypass the sandbox checks.
command, error = prepare_sandbox_command(
command, cwd, virtual_mode=not dangerous, dangerous=dangerous
)
if error:
return error
tid = _origin_thread_id(runtime)
process_id = background.launch(
command,
cwd,
name,
origin_thread_id=tid,
on_exit=lambda p: _notify_done(p, tid, notifier),
)
label = f" (name={name!r})" if name else ""
# In dangerous mode `/` is the real root, so advertise the real log path;
# in virtual mode `/.bg_processes/...` correctly maps to the workspace.
log_path = (
f"{cwd}/.bg_processes/{process_id}.log"
if dangerous
else f"/.bg_processes/{process_id}.log"
)
return (
f"Started background process {process_id}{label}. "
f"Output -> {log_path}. "
f"Poll with check_process('{process_id}'), stop with stop_process('{process_id}')."
)
return run_in_background
@tool(parse_docstring=True)
def check_process(process_id: str, runtime: ToolRuntime = None) -> str:
"""Check a background process's status and recent output.
Args:
process_id: The id returned by run_in_background.
"""
return background.status(process_id, thread_id=_origin_thread_id(runtime))
@tool(parse_docstring=True)
def stop_process(process_id: str) -> str:
"""Stop (kill) a running background process and its child process group.
Args:
process_id: The id returned by run_in_background.
"""
return background.stop(process_id)
@tool(parse_docstring=True)
def list_processes(all_threads: bool = False, runtime: ToolRuntime = None) -> str:
"""List background processes launched this session with their live statuses.
Args:
all_threads: List processes from every session, not just the current one.
"""
return background.list_all(_origin_thread_id(runtime), include_all=all_threads)
class BackgroundExecutionMiddleware(AgentMiddleware):
"""Adds run_in_background / check_process / stop_process / list_processes.
Modelled on ``AsyncSubAgentMiddleware``: the middleware simply exposes the tool set.
Attached to the main agent only (async sub-agents must not spawn local processes).
"""
def __init__(self, notifier: NotifierPort, *, dangerous: bool = False) -> None:
super().__init__()
self.tools = [
_make_run_in_background(notifier, dangerous),
check_process,
stop_process,
list_processes,
]