A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
[RSI, bug] Fix stale async-bash completion notice race at the turn boundary (#2372)
Seth Karten66df06fc12d7
diff --git a/prime-agent-runtime/src/rlm/bash.py b/prime-agent-runtime/src/rlm/bash.pyindex 82b9e93b29..c8eab3db14 100644--- a/prime-agent-runtime/src/rlm/bash.py+++ b/prime-agent-runtime/src/rlm/bash.py@@ -11,6 +11,7 @@ import subprocessimport sysimport threadingimport time+import uuidfrom collections import dequefrom collections.abc import Callable, Generatorfrom dataclasses import dataclass@@ -746,17 +747,7 @@ class BashHandle:def _arm_consumed_notice(self, command: str) -> None:# Armed only post-acceptance: the withdrawal can never overtake its notice.- loop = asyncio.get_running_loop()-- def dispatch() -> None:- def start() -> None:- task = loop.create_task(self._notify_result_consumed(command))- task.add_done_callback(_consume_notice_task)-- try:- loop.call_soon_threadsafe(start)- except RuntimeError:- pass # notifying loop already closed+ dispatch = functools.partial(self._notify_result_consumed, command)with self._callback_lock:if not self._result_consumed:@@ -764,17 +755,29 @@ class BashHandle:returndispatch()- async def _notify_result_consumed(self, command: str) -> None:+ def _notify_result_consumed(self, command: str) -> None:+ """Ship the withdrawal inside the read, ahead of the cell's done event.++ The host delivers a queued notice at the reading cell's turn boundary,+ which begins when that cell's done event is processed: a withdrawal+ frame that leaves the kernel after done arrives too late, and the stale+ notice wakes the model anyway. Reads happen inside a live cell, so+ writing the frame right here puts it ahead of done on the wire, where+ the host must withdraw before it can dispatch. The reply never matters+ (unknown reply ids are dropped), so the request is fire-and-forget:+ no future to await, no event-loop hop that could run after the cell.+ """from . import replif not repl.is_active():return- try:- await repl.host_request(- {"type": "bash.consumed", "pid": self._pid, "command": command}- )- except (OSError, RuntimeError):- return # bridge closed at teardown; old hosts error-reply — both fine+ repl._send(+ {+ "event": "host_request",+ "id": uuid.uuid4().hex,+ "data": {"type": "bash.consumed", "pid": self._pid, "command": command},+ }+ )async def _wait_reaped(self) -> None:loop = asyncio.get_running_loop()diff --git a/prime-agent-runtime/test/test_repl.py b/prime-agent-runtime/test/test_repl.pyindex 0c19e19f46..a5c914e94c 100644--- a/prime-agent-runtime/test/test_repl.py+++ b/prime-agent-runtime/test/test_repl.py@@ -798,6 +798,10 @@ class ReplTest(unittest.TestCase):reply_ok(self.repl, notice)read = self.repl.execute(f"withdraw-{label}-read", read_code)self.assertIn(f"withdrawn-{label}", one(read, "result")["text"])+ # The withdrawal must ship ahead of the reading cell's done, or the+ # notice is delivered as a stale turn-boundary notice instead.+ kinds = [event.get("event") for event in read]+ self.assertLess(kinds.index("host_request"), kinds.index("done"))request = wait_for_host_request(self.repl, read)self.assertEqual(request["data"], {"type": "bash.consumed", "pid": pid, "command": command}