Skip to content

The agent that grows with you

fix: polled process results carry the reply-sized output and cut marker (review follow-up)

teknium1cf54dcfd06c7
diff --git a/tests/tools/test_notify_on_complete.py b/tests/tools/test_notify_on_complete.py
index 9caafda0535..1b90ab3b57f 100644
--- a/tests/tools/test_notify_on_complete.py
+++ b/tests/tools/test_notify_on_complete.py
@@ -126,6 +126,26 @@ class TestCompletionQueue:
assert len(completion["output"]) == 5000
assert "output_cut" not in completion
+ def test_polled_result_carries_the_same_reply_as_the_notification(self, registry):
+ """api_server / one-shot senders cannot receive completion notifications and poll with
+ process(action='wait') instead (bot-mode.md): the polled result must carry the reply
+ whole up to the per-process size and the same ``output_cut`` marker, not a silent
+ 2000-char tail."""
+ s = _make_session(sid="proc_polled", output="Reply from @b:\n" + "x" * 4000)
+ s.completion_output_chars = 6000
+ s.exited, s.exit_code = True, 0
+ registry._finished[s.id] = s
+ with patch.object(registry, "_reconcile_local_exit"), patch.object(registry, "_write_checkpoint"):
+ result = registry.wait(s.id, timeout=1)
+ assert result["status"] == "exited"
+ assert result["output"].startswith("Reply from @b:")
+ assert "output_cut" not in result
+
+ s.completion_output_chars = 1000
+ result = registry.wait(s.id, timeout=1)
+ assert len(result["output"]) == 1000
+ assert result["output_cut"] == len(s.output_buffer) - 1000
+
def test_multiple_completions_queued(self, registry):
"""Multiple notify processes all push to the same queue."""
for i in range(3):
diff --git a/tools/process_registry.py b/tools/process_registry.py
index ed2f88a2eef..dae13056bd9 100644
--- a/tools/process_registry.py
+++ b/tools/process_registry.py
@@ -482,6 +482,17 @@ def _output_tail(session: "ProcessSession", n: int) -> str:
return strip_ansi(session.output_buffer[-n:])
+def _completion_output(session: "ProcessSession") -> dict:
+ """``output`` sized by the session's ``completion_output_chars`` plus ``output_cut`` when trimmed.
+
+ Shared by the completion notification AND the wait/poll/kill snapshots: a bot in an api_server
+ or one-shot session cannot receive notifications and polls instead, so the polled result must
+ carry the same whole reply and the same cut marker (#115334)."""
+ limit = session.completion_output_chars or COMPLETION_OUTPUT_CHARS
+ cut = len(session.output_buffer) - limit
+ return {"output": _output_tail(session, limit), **({"output_cut": cut} if cut > 0 else {})}
+
+
@dataclass
class ProcessSession:
"""A tracked background process with output buffering."""
@@ -1511,8 +1522,6 @@ class ProcessRegistry(ProcessCheckpointMixin):
self._release_finished_handles(session)
self._write_checkpoint()
if was_running and session.notify_on_complete:
- limit = session.completion_output_chars or COMPLETION_OUTPUT_CHARS
- cut = len(session.output_buffer) - limit
notification = {
"type": "completion",
"session_id": session.id,
@@ -1522,9 +1531,8 @@ class ProcessRegistry(ProcessCheckpointMixin):
"command": session.command,
**({"handoff_note": session.handoff_note} if session.handoff_note else {}),
**self._exit_fields(session),
- "output": _output_tail(session, limit),
# A consumer that relays the output (a bot DM's reply) must know it is not whole.
- **({"output_cut": cut} if cut > 0 else {}),
+ **_completion_output(session),
# Stable producer identity across checkpoint recovery (unlike a
# consumer-observed completion timestamp).
"started_at": session.started_at,
@@ -1972,10 +1980,10 @@ class ProcessRegistry(ProcessCheckpointMixin):
@staticmethod
def _exit_snapshot(session: ProcessSession, status: str) -> dict:
- """Result dict for an exited session: exit metadata + last 2000 chars of output."""
+ """Result dict for an exited session: exit metadata + the completion-sized output tail."""
return {
"status": status, "command": session.command,
- **ProcessRegistry._exit_fields(session), "output": _output_tail(session, 2000)}
+ **ProcessRegistry._exit_fields(session), **_completion_output(session)}
def kill_process(
self, session_id: str, *, source: str = "process.kill", consume_output: bool = True,
@@ -2039,7 +2047,7 @@ class ProcessRegistry(ProcessCheckpointMixin):
# Capture output, mark consumed, THEN expose ``exited`` to watcher tasks —
# closes the delayed-notification race without losing the transcript.
with session._lock:
- output = _output_tail(session, 2000)
+ output = _completion_output(session)
if consume_output:
self._completion_consumed.add(session_id)
session.exited = True
@@ -2055,7 +2063,7 @@ class ProcessRegistry(ProcessCheckpointMixin):
self._write_checkpoint()
return {
"status": "killed", "session_id": session.id, "completion_reason": session.completion_reason,
- "termination_source": session.termination_source, "output": output}
+ "termination_source": session.termination_source, **output}
except Exception as e:
return {"status": "error", "error": str(e)}
@@ -2088,11 +2096,11 @@ class ProcessRegistry(ProcessCheckpointMixin):
with session._lock:
session.exited = True
session.exit_code = None
- output = _output_tail(session, 2000)
+ output = _completion_output(session)
if consume_output:
self._completion_consumed.add(session_id)
self._move_to_finished(session)
- return {"status": "already_exited", "exit_code": session.exit_code, "output": output}
+ return {"status": "already_exited", "exit_code": session.exit_code, **output}
self._terminate_host_pid(session.pid, session.host_start_time)
else:
return {