The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(process_registry): decide a killed PTY chunk atomically with the kill
unsupportedpastelsaa45d6c9fbb8
diff --git a/tests/tools/test_process_registry_pty_kill.py b/tests/tools/test_process_registry_pty_kill.pyindex 8fad7cc58f8..132b17e4896 100644--- a/tests/tools/test_process_registry_pty_kill.py+++ b/tests/tools/test_process_registry_pty_kill.py@@ -60,8 +60,12 @@ def _spawn_with_escapee(registry, tmp_path, escapee_lifetime, late_output=""):session = registry.spawn_local(f"setsid sh -c 'sh -c \"echo \\$PPID\" > {pidfile}; {late}exec sleep {escapee_lifetime}' & sleep 120",cwd=str(tmp_path), use_pty=True)- assert _wait_for(lambda: pidfile.exists() and pidfile.read_text().strip(), 5)- return session, int(pidfile.read_text())+ try:+ assert _wait_for(lambda: pidfile.exists() and pidfile.read_text().strip(), 5)+ return session, int(pidfile.read_text())+ except BaseException:+ _terminate_owned(session)+ raisedef _kill_within_deadline(registry, session):@@ -109,6 +113,43 @@ def test_kill_returns_while_escaped_descendant_holds_the_pty(tmp_path, monkeypatassert "LATE-ESCAPEE-OUTPUT" not in session.output_buffer+@_POSIX_PTY+def test_chunk_read_before_the_kill_is_not_added_after_it(tmp_path, monkeypatch):+ # The reader has read a chunk but not yet buffered it when the kill snapshots the output+ # and sets ``exited``. The chunk must not land in the killed session afterwards.+ pytest.importorskip("ptyprocess")+ monkeypatch.setattr(module, "_is_supervised_gateway_process", lambda: False)+ monkeypatch.setattr(module, "_is_supervised_backend_host", lambda: False, raising=False)+ registry = ProcessRegistry()+ entered, release = threading.Event(), threading.Event()+ ingest, emit, emitted = registry._ingest_output, registry._emit_output, []++ def paused_ingest(session, text, **kwargs):+ if "RACE-MARKER" in text:+ entered.set()+ release.wait(10)+ ingest(session, text, **kwargs)++ def recording_emit(session, text):+ emitted.append(text)+ emit(session, text)++ registry._ingest_output = paused_ingest+ registry._emit_output = recording_emit+ session = registry.spawn_local(+ "sleep 0.2; echo RACE-MARKER; exec sleep 15", cwd=str(tmp_path), use_pty=True)+ try:+ assert entered.wait(5), "reader never read the marker"+ result = registry.kill_process(session.id)+ finally:+ release.set()+ assert result["status"] == "killed"+ assert _wait_for(lambda: not session._reader_thread.is_alive(), 5)+ assert "RACE-MARKER" not in result.get("output", "")+ assert "RACE-MARKER" not in session.output_buffer+ assert not any("RACE-MARKER" in text for text in emitted)++@pytest.mark.parametrize("marked, supervised, scoped", [(True, True, True), # dashboard / serve run by a service manager(True, False, False), # dashboard / serve started from a shelldiff --git a/tools/process_registry.py b/tools/process_registry.pyindex 24bdf1bb721..63cff8b2f54 100644--- a/tools/process_registry.py+++ b/tools/process_registry.py@@ -621,10 +621,23 @@ class ProcessSession:def append_output(self, text: str) -> None:"""Append to the rolling output buffer under the session lock, keeping the tail."""with self._lock:- self.output_buffer += text- self.total_output_chars += len(text)- if len(self.output_buffer) > self.max_output_chars:- self.output_buffer = self.output_buffer[-self.max_output_chars:]+ self._append_locked(text)++ def append_output_if_running(self, text: str) -> bool:+ """Append unless the session has exited. Decided under the lock a kill holds while it+ snapshots the output and sets ``exited``, so a chunk is either in the kill's receipt or+ dropped, never added after it."""+ with self._lock:+ if self.exited:+ return False+ self._append_locked(text)+ return True++ def _append_locked(self, text: str) -> None:+ self.output_buffer += text+ self.total_output_chars += len(text)+ if len(self.output_buffer) > self.max_output_chars:+ self.output_buffer = self.output_buffer[-self.max_output_chars:]def mark_exited(self, exit_code, reason: str = "exited", source: str = "") -> None:"""Record an exit. A kill that raced the observer already recorded its own@@ -1717,8 +1730,7 @@ class ProcessRegistry(ProcessCheckpointMixin):# A kill can leave this reader running while a detached descendant holds the# slave open (_release_finished_handles defers the close to it). Keep draining,# but leave the killed session's output as the kill reported it.- if not session.exited:- self._ingest_output(session, text)+ self._ingest_output(session, text, unless_exited=True)try:while pty.isalive():@@ -1752,9 +1764,13 @@ class ProcessRegistry(ProcessCheckpointMixin):session, decoder, ingest, "PTY",pty.wait, lambda: pty.exitstatus if hasattr(pty, 'exitstatus') else -1)- def _ingest_output(self, session: ProcessSession, text: str) -> None:- """Buffer a freshly-read chunk, then scan watch patterns and stream it live."""- session.append_output(text)+ def _ingest_output(self, session: ProcessSession, text: str, *, unless_exited: bool = False) -> None:+ """Buffer a freshly-read chunk, then scan watch patterns and stream it live.+ ``unless_exited`` drops the chunk once the session has exited (atomically with a kill)."""+ if not unless_exited:+ session.append_output(text)+ elif not session.append_output_if_running(text):+ returnself._check_watch_patterns(session, text)self._emit_output(session, text)