Something went wrong. Try again.
MCP based ACP agent
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263"""A persistent IPython kernel driven over ZMQ via jupyter_client.
Adapted from the jupyter-kernel-tool gist. The kernel is a subprocesslaunched with crow-cli's own interpreter (``sys.executable``), so executedcode runs with crow's dependencies available and can interrogate crow.db.State persists across :meth:`CrowKernel.execute` calls — this is a REPL,not a fresh subprocess per call.
Unlike the terminal tool, there is no ``!`` shell escape: bash belongs to``terminal``. This tool runs Python only."""
import queueimport reimport threadingimport timefrom logging import getLogger
from jupyter_client import KernelManager
logger = getLogger(__name__)
# Strip ANSI color codes from tracebacks so the agent reads clean text._ANSI_RE = re.compile(r"\x1b\[[0-9;]*m")
#: A frame jupyter_client cannot parse — a missing DELIM raises ValueError out#: of feed_identities. Junk bytes, healthy socket: drop the frame, read on._GARBAGE = (ValueError, KeyError)
#: How long to block on either channel before looking at the cancel flag.POLL_S = 0.05
#: Returned when a cell already owns the kernel. Two readers on one ZMQ socket#: make libzmq abort the process ("Bad address", src/fq.cpp).OCCUPIED = ( "(kernel busy: an earlier cell is still running in this kernel. Re-call" " execute with a longer timeout to collect its output, or pass reset=True" " to abandon it and start a fresh kernel.)")
class KernelDead(Exception): """The kernel's channels are unusable. The caller builds a new one."""
class CrowKernel: """A small wrapper around jupyter_client for crow's ``execute`` tool."""
def __init__(self, python_path: str | None = None, cwd: str | None = None): self._lock = threading.Lock() self._cancelled = threading.Event() self.dead = False
self.km = KernelManager()
# Point the kernel at crow-cli's own interpreter so the REPL has # crow's deps (sqlalchemy/httpx/rich/yaml) and can read crow.db. if python_path: self.km.kernel_spec.argv[0] = python_path
if cwd: self.km.start_kernel(cwd=cwd) else: self.km.start_kernel()
self.client = self.km.client() self.client.start_channels() self._wait_ready()
def _wait_ready(self) -> None: """Block until the kernel is ready to execute.
``wait_for_ready`` is the correct signal, but in some environments it can hang (the gist's note); bound it and fall back to a short sleep so startup is never unbounded. """ try: self.client.wait_for_ready(timeout=15) except _GARBAGE as exc: # Junk frame, not a dead kernel: settle and go. logger.warning("wait_for_ready read a junk frame (%s); settling", exc) time.sleep(2) except Exception: logger.warning("kernel wait_for_ready timed out; sleeping to settle") time.sleep(2)
def execute(self, code: str, timeout: float | None = 30) -> str: """Execute Python code and return what the cell PRINTED.
stdout + stderr, or the ANSI-stripped traceback on error. The REPL's display of the last expression (execute_result / Out[n]) is drained off iopub and DISCARDED: it is a notebook affordance, not program output, and the LLM's view of execute is the program's output — plus image blocks the server prepends when vision ran. Code talks to the model with print(); values talk to later code by being values.
``timeout`` bounds the wait for THIS cell (seconds); ``None`` waits forever, for a long-running process the caller is deliberately blocking on. On timeout the cell is NOT killed — IPython keeps running it — so this returns a "kernel busy" notice rather than raising, and the cell's output stays collectable on a later call.
Every message is filtered on ``parent_header.msg_id == msg_id`` for this cell. A previous cell that timed out and kept running leaves its own shell reply and iopub messages queued; without the filter the drain consumes those STALE messages and breaks on the stale idle, returning the previous cell's output and desyncing attribution by one until a reset. With it, stale messages are discarded and the stream re-syncs on its own once the busy cell finishes. """ if self.dead: raise KernelDead("the kernel's channels are gone") if not self._lock.acquire(timeout=2 * POLL_S): return OCCUPIED try: return self._execute_locked(code, timeout) except KernelDead: self.dead = True raise finally: self._lock.release()
def _execute_locked(self, code: str, timeout: float | None) -> str: msg_id = self.client.execute(code)
reply = self._await_shell_reply(msg_id, timeout) if reply is None: return self._busy(timeout)
stdout: list[str] = [] stderr: list[str] = [] error = None
# The cell is done (we hold its execute_reply), so its iopub messages # are already buffered — drain them, skipping any stale ones, and stop # at THIS cell's idle. while True: if self._cancelled.is_set(): break try: msg = self.client.get_iopub_msg(timeout=timeout) except queue.Empty: break except _GARBAGE as exc: logger.warning("dropped an unparseable iopub frame: %s", exc) continue except Exception as exc: raise KernelDead(f"iopub read failed: {exc}") from exc if msg.get("parent_header", {}).get("msg_id") != msg_id: continue
msg_type = msg["msg_type"] content = msg["content"] if msg_type == "stream": if content["name"] == "stdout": stdout.append(content["text"]) else: stderr.append(content["text"]) elif msg_type == "error": error = content elif msg_type == "status" and content["execution_state"] == "idle": break
return self._format_output( stdout="".join(stdout), stderr="".join(stderr), error=error, )
def _await_shell_reply(self, msg_id: str, timeout: float | None): """This cell's execute_reply, or None on timeout.
Skips stale replies left by a previous cell that timed out and kept running, so the wait is bounded by ``timeout`` overall rather than restarting it per discarded message. ``timeout=None`` blocks until the cell finishes. """ deadline = None if timeout is None else time.monotonic() + timeout while True: remaining = None if deadline is None else deadline - time.monotonic() if remaining is not None and remaining <= 0: return None try: reply = self.client.get_shell_msg(timeout=remaining) except queue.Empty: return None except _GARBAGE as exc: logger.warning("dropped an unparseable shell frame: %s", exc) continue except Exception as exc: raise KernelDead(f"shell read failed: {exc}") from exc if reply.get("parent_header", {}).get("msg_id") == msg_id: return reply # A previous cell's reply — discard and keep waiting for ours.
@staticmethod def _busy(timeout: float | None) -> str: """The honest signal for a cell still running past its timeout.
Replaces the bare ``Error:`` an unhandled ``queue.Empty`` used to surface as (its ``str()`` is empty), which read as "broken" and sent the caller to reset a kernel that was merely busy. """ return ( f"(kernel busy: the cell did not finish within {timeout}s and is" " still running — its output was not captured this call. Re-call" " execute to collect it once it completes, and pass a longer" " timeout= — or timeout=None to wait — for a long-running cell.)" )
def _format_output( self, stdout: str, stderr: str, error: dict | None, ) -> str: """The program's output, nothing else.""" # Error first — that's what matters when it breaks. if error: traceback_lines = [ _ANSI_RE.sub("", line) for line in error.get("traceback", []) ] return "\n".join(traceback_lines)
output = [] if stdout: output.append(stdout.rstrip()) if stderr: output.append(stderr.rstrip()) return "\n".join(output)
def shutdown(self) -> None: """Shut the kernel down. Never blocks, and never needs a live channel.
Reset is the one thing that has to work when everything else does not, so this refuses to wait on a cell: the cancelled flag ends the shell and iopub waits, and the teardown runs off-thread when the lock says a cell still holds the kernel. Closing sockets underneath a thread blocked reading them is the libzmq abort this guards against, so ``stop_channels`` only runs when no reader is inside. """ self._cancelled.set() self.dead = True if self._lock.acquire(blocking=False): try: self._teardown(stop_channels=True) finally: self._lock.release() return threading.Thread(target=self._teardown, args=(False,), daemon=True).start()
def _teardown(self, stop_channels: bool) -> None: if stop_channels: try: self.client.stop_channels() except Exception: logger.warning("stop_channels raised", exc_info=True) try: self.km.shutdown_kernel(now=True) except Exception: logger.warning("kernel shutdown raised", exc_info=True)