Skip to content

The agent that grows with you

fix(process): a tick's omitted count is its own field, never parsed from output

Teknium70c734ea41e0
diff --git a/gateway/session_automation.py b/gateway/session_automation.py
index 3aa5419bc90..af9333cdf08 100644
--- a/gateway/session_automation.py
+++ b/gateway/session_automation.py
@@ -80,7 +80,7 @@ async def prepare_heartbeat(runner, event):
if earlier is None or not await _withdraw(authority, waiting):
return event
from tools.process_registry_notifications import join_heartbeat_outputs
- return {**event, 'output': join_heartbeat_outputs(earlier.get('output', ''), event.get('output', ''))}
+ return {**event, **join_heartbeat_outputs(earlier, event)}
async def withdraw_process_ticks(runner, event):
diff --git a/tests/gateway/test_local_producer_admission.py b/tests/gateway/test_local_producer_admission.py
index aeb6a1ecc7c..9267901f155 100644
--- a/tests/gateway/test_local_producer_admission.py
+++ b/tests/gateway/test_local_producer_admission.py
@@ -187,9 +187,9 @@ async def test_waiting_ticks_fold_into_one_turn_and_none_outlives_its_process(ga
submitted = await viewer.dispatch({'id': count, 'method': 'prompt.submit', 'params': {
'session_id': sid, 'input_id': lookalike, 'text': 'typed'}})
assert 'result' in submitted, submitted
- first_cut = '...(300 earlier characters omitted)\n' + 'a' * 2000
- for seq, output in ((2, first_cut), (3, 'b' * 1000)):
- assert await gateway.drain(gateway.tick(primary, sid, seq, process='proc_1', output=output)) == []
+ for seq, output, omitted in ((2, 'a' * 2000, 300), (3, 'b' * 1000, 0)):
+ tick = dict(gateway.tick(primary, sid, seq, process='proc_1', output=output), omitted=omitted)
+ assert await gateway.drain(tick) == []
# proc_12 exits: its waiting tick is withdrawn, and a late tick is dropped.
exited = gateway.process_event(primary, sid, 'completion', process='proc_12', exit_code=0)
assert await gateway.runner._deliver_completion_notification('[proc_12 finished]', exited) is True
@@ -278,6 +278,17 @@ def _automation_rows(messaging):
return {row['request_id']: row for row in rows if row['principal_id'] == 'automation:' + messaging.route}
+async def test_job_output_that_looks_like_a_cut_marker_keeps_the_earlier_output(messaging):
+ """The omitted count rides the tick as its own field; text the job printed is never read as one."""
+ literal = '...(10 earlier characters omitted)\nprinted by the job\n'
+ for seq, output in ((1, 'proc_job/1\n'), (2, literal)):
+ assert await _drain_one(messaging.runner, messaging.event(
+ 'heartbeat', **_tick_fields('proc_job', seq, output))) == 0
+ folded = _automation_rows(messaging)['["heartbeat","proc_job","proc_job-tick-2"]']['payload']['text']
+ assert _tick_output(folded) == 'proc_job/1\n' + literal
+
+
@pytest.mark.asyncio
async def test_a_late_tick_of_a_settled_batch_member_is_dropped(messaging):
"""A completion batch names only its primary in ``request_id``; the siblings live in the payload.
diff --git a/tests/tools/test_process_heartbeat.py b/tests/tools/test_process_heartbeat.py
index c199fe53234..cad41f7c703 100644
--- a/tests/tools/test_process_heartbeat.py
+++ b/tests/tools/test_process_heartbeat.py
@@ -158,3 +158,16 @@ def test_terminal_dispatch_heartbeat_implies_notify_and_foreground_runs_without_
bg = json.loads(dispatch({"command": "sleep 1", "background": True, "heartbeat": 120}))
assert "error" not in bg or not bg["error"]
assert captured["heartbeat"] == 120 and captured["notify_on_complete"] is True
+
+
+def test_a_cut_tick_carries_its_omitted_count_as_a_field_and_the_marker_is_rendered_once():
+ from tools.process_registry_notifications import (
+ HEARTBEAT_OUTPUT_CHARS, format_process_notification, heartbeat_output_tail)
+ output, omitted = heartbeat_output_tail("x" * 5 + "y" * HEARTBEAT_OUTPUT_CHARS)
+ assert (output, omitted) == ("y" * HEARTBEAT_OUTPUT_CHARS, 5)
+ beat = {"type": "heartbeat", "session_id": "proc_1", "command": "make", "seq": 1, "elapsed": 60,
+ "interval": 60, "output": output, "omitted": omitted}
+ assert f"heartbeat:\n...(5 earlier characters omitted)\n{output}]" in format_process_notification(beat)
+ # Job text that looks like a marker is output, never a count.
+ literal = "...(10 earlier characters omitted)\n"
+ assert heartbeat_output_tail(literal) == (literal, 0)
diff --git a/tools/process_registry.py b/tools/process_registry.py
index 0673116efc9..436f00b8f8a 100644
--- a/tools/process_registry.py
+++ b/tools/process_registry.py
@@ -765,7 +765,7 @@ class ProcessRegistry(ProcessTerminationMixin, ProcessCheckpointMixin, ProcessOu
session._heartbeat_total_at_last = session.total_output_chars
if not output:
return
- output = heartbeat_output_tail(output)
+ output, omitted = heartbeat_output_tail(output)
session._heartbeat_seq += 1
notification = {
**self._watch_event_base(session),
@@ -774,6 +774,7 @@ class ProcessRegistry(ProcessTerminationMixin, ProcessCheckpointMixin, ProcessOu
"interval": session.heartbeat_seconds,
"elapsed": int(now - session.started_at) if session.started_at else 0,
"output": output,
+ "omitted": omitted,
"started_at": session.started_at,
}
_redact_process_result(notification)
diff --git a/tools/process_registry_notifications.py b/tools/process_registry_notifications.py
index bdad4080af4..27ab6006742 100644
--- a/tools/process_registry_notifications.py
+++ b/tools/process_registry_notifications.py
@@ -3,7 +3,6 @@ watch_match, watch_disabled, watch_overflow_*, async_delegation) into the
``[IMPORTANT: ...]`` / ``[ASYNC DELEGATION ...]`` text the CLI drain loop, gateway and
TUI inject into the agent conversation."""
-import re
import time
from dataclasses import dataclass
from contextlib import suppress
@@ -376,31 +375,33 @@ HEARTBEAT_DISPLAY_KIND = "hidden" # a wake, not a message: no surface paints th
HEARTBEAT_OUTPUT_CHARS = 2000
-_OMITTED_PREFIX = re.compile(r"\.\.\.\((\d+) earlier characters omitted\)\n")
-
-
-def heartbeat_output_tail(output: str, omitted: int = 0) -> str:
- """A tick's output, capped to its newest characters: a long job's latest lines are what matter.
- ``omitted`` counts characters already cut before ``output``."""
+def heartbeat_output_tail(output: str, omitted: int = 0) -> tuple[str, int]:
+ """A tick's output capped to its newest characters (a long job's latest lines are what matter),
+ and the count of characters cut before it. ``omitted`` counts characters already cut before
+ ``output``. The count rides the tick as its own field (``omitted``): text a job printed is never
+ read back as a cut marker."""
if len(output) > HEARTBEAT_OUTPUT_CHARS:
omitted += len(output) - HEARTBEAT_OUTPUT_CHARS
output = output[-HEARTBEAT_OUTPUT_CHARS:]
- return f"...({omitted} earlier characters omitted)\n{output}" if omitted else output
+ return output, omitted
-def join_heartbeat_outputs(earlier: str, later: str) -> str:
- """Two consecutive ticks' output as one tick's, capped the same way, counting every character
- either cut. A later tick that was itself cut leaves none of the earlier output."""
- earlier_cut, earlier_text = _omitted_prefix(earlier)
- later_cut, later_text = _omitted_prefix(later)
+def join_heartbeat_outputs(earlier: dict, later: dict) -> dict:
+ """Two consecutive ticks' ``output``/``omitted`` as one tick's, capped the same way, counting
+ every character either cut. A later tick that was itself cut leaves none of the earlier output."""
+ earlier_cut, earlier_text = int(earlier.get("omitted") or 0), str(earlier.get("output") or "")
+ later_cut, later_text = int(later.get("omitted") or 0), str(later.get("output") or "")
if later_cut:
- return heartbeat_output_tail(later_text, earlier_cut + len(earlier_text) + later_cut)
- return heartbeat_output_tail(earlier_text + later_text, earlier_cut)
+ output, omitted = heartbeat_output_tail(later_text, earlier_cut + len(earlier_text) + later_cut)
+ else:
+ output, omitted = heartbeat_output_tail(earlier_text + later_text, earlier_cut)
+ return {"output": output, "omitted": omitted}
-def _omitted_prefix(output: str) -> tuple[int, str]:
- match = _OMITTED_PREFIX.match(output)
- return (int(match.group(1)), output[match.end():]) if match else (0, output)
+def heartbeat_output_text(evt: dict) -> str:
+ """A tick's output as the agent reads it: the cut marker, rendered here only, then the tail."""
+ omitted, output = int(evt.get("omitted") or 0), str(evt.get("output") or "")
+ return f"...({omitted} earlier characters omitted)\n{output}" if omitted else output
def heartbeat_display_text(evt: dict) -> str:
@@ -479,7 +480,7 @@ def format_process_notification(evt: dict) -> str | None:
f"[Background process {_sid} heartbeat #{evt.get('seq', '?')} — still running after "
f"{_format_age(float(evt.get('elapsed') or 0))} (next in {evt.get('interval', '?')}s when there "
f"is new output; you will also be told when it exits).\n"
- f"{attribution}Command: {_cmd}\nOutput since last heartbeat:\n{evt.get('output', '')}]")
+ f"{attribution}Command: {_cmd}\nOutput since last heartbeat:\n{heartbeat_output_text(evt)}]")
if evt_type == "watch_match":
_sup = evt.get("suppressed", 0)
return (