Skip to content

The agent that grows with you

fix(process): first heartbeat carries output produced before arming

teknium1e94fc4e26a2a
diff --git a/tests/tools/test_process_heartbeat.py b/tests/tools/test_process_heartbeat.py
index b708a2bc457..2faef8d5821 100644
--- a/tests/tools/test_process_heartbeat.py
+++ b/tests/tools/test_process_heartbeat.py
@@ -114,6 +114,24 @@ def test_schema_minimum_heartbeat_is_disabled_for_foreground(monkeypatch):
assert captured["notify_on_complete"] is False
+def test_first_heartbeat_carries_output_produced_before_arming(tmp_path, monkeypatch):
+ """The terminal tool arms the heartbeat after its spawn bookkeeping; whatever the process
+ printed in that gap belongs to the first heartbeat, not to nobody (CI: 'first' vanished)."""
+ monkeypatch.setattr(pr, "HEARTBEAT_MIN_SECONDS", 1)
+ monkeypatch.setattr(pr, "HEARTBEAT_TICK_SECONDS", 0.1)
+ registry = ProcessRegistry()
+ session = registry.spawn_local("echo first; sleep 3", cwd=str(tmp_path))
+ session.notify_on_complete = True
+ assert _wait_until(lambda: "first" in registry.poll(session.id).get("output_preview", ""), timeout=10)
+ registry.arm_heartbeat(session, 1)
+
+ assert _wait_until(lambda: any(e.get("type") == "heartbeat" for e in list(registry.completion_queue.queue)),
+ timeout=10)
+ first_beat = next(e for e in _drain(registry.completion_queue) if e["type"] == "heartbeat")
+ assert "first" in first_beat["output"], first_beat
+
+
def test_terminal_dispatch_heartbeat_implies_notify_and_refuses_foreground(monkeypatch):
from tools import terminal_tool as tt
diff --git a/tools/process_registry.py b/tools/process_registry.py
index 207a66e5a89..7e7780695ef 100644
--- a/tools/process_registry.py
+++ b/tools/process_registry.py
@@ -713,7 +713,9 @@ class ProcessRegistry(ProcessCheckpointMixin):
seconds = max(int(seconds), HEARTBEAT_MIN_SECONDS)
session.heartbeat_seconds = seconds
session._heartbeat_last = time.time()
- session._heartbeat_total_at_last = session.total_output_chars
+ # The output baseline stays at spawn (field default 0), never here: the spawn call
+ # arms the heartbeat only after its bookkeeping, and a fast-starting process has
+ # already written its first lines by then. Those lines belong to the first heartbeat.
self._ensure_heartbeat_thread()
return seconds