The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(process): an exited no-stdin reader says why it got EOF
Teknium98b129f1cb05
diff --git a/tests/tools/test_process_stdin_unavailable_error.py b/tests/tools/test_process_stdin_unavailable_error.pyindex 5d2c65aa8b0..60daddfc5c5 100644--- a/tests/tools/test_process_stdin_unavailable_error.py+++ b/tests/tools/test_process_stdin_unavailable_error.py@@ -3,6 +3,8 @@A local ``terminal(background=true)`` spawn without ``pty=true`` connects stdin to /dev/null bydesign; the old error blamed a "non-local backend or stdin closed", so agents retried blind."""+import time+import pytestfrom tools.process_registry import ProcessRegistry@@ -49,3 +51,16 @@ def test_non_local_session_keeps_the_backend_explanation():error = stdin_unavailable_error(SimpleNamespace(process=None, output_log=""))assert "non-local terminal backend" in error and "pty=true" not in error++[email protected]("posix")+def test_a_reader_that_hit_eof_says_why_it_already_exited(registry):+ """``read x`` on /dev/null returns at once, so the write lands on an exited process."""+ session = registry.spawn_local("read x; echo got:$x", cwd=None, task_id="t-stdin-eof")+ for _ in range(100):+ if session.exited:+ break+ time.sleep(0.05)+ result = registry.write_stdin(session.id, "hello\n")+ assert result["status"] == "already_exited"+ assert "without stdin" in result["error"] and "pty=true" in result["error"]diff --git a/tools/process_registry.py b/tools/process_registry.pyindex a23e12fd7e5..c565ce3c26d 100644--- a/tools/process_registry.py+++ b/tools/process_registry.py@@ -2286,12 +2286,12 @@ class ProcessRegistry(ProcessTerminationMixin, ProcessCheckpointMixin, ProcessOudef _stdin_op(self, session_id: str, pty_op, pipe_op, ok: dict) -> dict:"""``pty_op(pty)`` under PTY mode, else ``pipe_op(stdin)`` on the Popen pipe; *ok* on success."""- from tools.process_registry_stdin import stdin_unavailable_error+ from tools.process_registry_stdin import already_exited_error, stdin_unavailable_errorsession = self.get(session_id)if session is None:return _not_found(session_id)if session.exited:- return {"status": "already_exited", "error": "Process has already finished"}+ return {"status": "already_exited", "error": already_exited_error(session)}try:if session._pty:pty_op(session._pty)diff --git a/tools/process_registry_stdin.py b/tools/process_registry_stdin.pyindex ce4daea3490..c2585801fbb 100644--- a/tools/process_registry_stdin.py+++ b/tools/process_registry_stdin.py@@ -7,6 +7,20 @@ def _pty_available() -> bool:return importlib.util.find_spec("winpty" if sys.platform == "win32" else "ptyprocess") is not None+def _pty_relaunch_helps(session) -> bool:+ """A local pipe-mode spawn (stdin is /dev/null) where ``spawn_local`` could give a PTY instead."""+ return session.process is not None and not session.output_log and _pty_available()+++def already_exited_error(session) -> str:+ """A pipe-mode process that read stdin got EOF at once and exited: say so, or the agent retries blind."""+ if session.process is None or session.process.stdin is not None or not _pty_relaunch_helps(session):+ return "Process has already finished"+ return ("Process has already finished. It was started without stdin (terminal background=true without "+ "pty=true), so anything it read got end-of-file at once. Relaunch it with "+ "terminal(background=true, pty=true) to send input.")++def stdin_unavailable_error(session) -> str:"""Error for process_manage write/submit on a session without a stdin pipe or PTY.@@ -17,7 +31,7 @@ def stdin_unavailable_error(session) -> str:return ("Process stdin not available: it runs on a non-local terminal backend (or was recovered ""after a restart), which has no live stdin. Pass input non-interactively instead ""(arguments, a here-doc, or `printf ... | cmd`).")- if session.output_log or not _pty_available():+ if not _pty_relaunch_helps(session):return ("This process was started without stdin, and this session cannot give background ""processes a pseudo-terminal, so pty=true would not help. Pass input non-interactively ""instead (arguments, a here-doc, or `printf ... | cmd`).")