Skip to content

The agent that grows with you

test: distinguish background notice replay from redelivery

ethernet7f2d1ee1062a
diff --git a/tests/tools/test_completed_process_results.py b/tests/tools/test_completed_process_results.py
index 3eea1798ea5..01d7bc81949 100644
--- a/tests/tools/test_completed_process_results.py
+++ b/tests/tools/test_completed_process_results.py
@@ -1,5 +1,6 @@
"""Completed work remains retrievable when its finite CLI owner exits."""
+from collections import Counter
import http.server
import json
import os
@@ -9,10 +10,36 @@ import subprocess
import sys
import textwrap
import threading
+import time
REPO_ROOT = Path(__file__).resolve().parents[2]
+def _new_background_notifications(messages, seen_counts):
+ """Count new history occurrences, not positions shifted by request assembly."""
+ counts = Counter(
+ m["content"] for m in messages
+ if m["role"] == "user"
+ and isinstance(m.get("content"), str)
+ and m["content"].startswith("[IMPORTANT: Background process ")
+ )
+ new = []
+ for content, count in counts.items():
+ new.extend([content] * max(0, count - seen_counts[content]))
+ seen_counts[content] = max(seen_counts[content], count)
+ return new
+
+
+def test_background_notification_history_replay_is_not_new_delivery():
+ notice = "[IMPORTANT: Background process proc_a exited (exit code 7).]"
+ seen_counts = Counter()
+ first = [{"role": "system", "content": "first"}, {"role": "user", "content": notice}]
+ shifted = [{"role": "system", "content": "second"}, {"role": "user", "content": "query"}, *first[1:]]
+ assert _new_background_notifications(first, seen_counts) == [notice]
+ assert _new_background_notifications(shifted, seen_counts) == []
+ assert _new_background_notifications([*shifted, first[1]], seen_counts) == [notice]
+
+
def test_headless_terminal_result_survives_cli_exit(tmp_path):
"""Real CLI, tool dispatch, shell child and fresh reader; only the LLM is local."""
home = tmp_path / "profile"
@@ -40,8 +67,9 @@ def test_headless_terminal_result_survives_cli_exit(tmp_path):
command = shlex.join(path.as_posix() for path in (Path(sys.executable), child, release))
observed = []
seen_tool = set()
- seen_follow_up_positions = set()
+ seen_follow_up_counts = Counter()
follow_ups = []
+ completed_during_provider_reply = threading.Event()
class Provider(http.server.BaseHTTPRequestHandler):
def do_GET(self):
@@ -53,13 +81,7 @@ def test_headless_terminal_result_survives_cli_exit(tmp_path):
self.send_error(404)
return
tool_results = [m for m in request["messages"] if m["role"] == "tool"]
- # Each request replays earlier messages. A second delivery of identical
- # text at a NEW history position is still a duplicate notification.
- for index, m in enumerate(request["messages"]):
- if (m["role"] == "user" and "Background process" in str(m.get("content") or "")
- and index not in seen_follow_up_positions):
- seen_follow_up_positions.add(index)
- follow_ups.append(m["content"])
+ follow_ups.extend(_new_background_notifications(request["messages"], seen_follow_up_counts))
has_terminal = any(t.get("function", {}).get("name") == "terminal"
for t in request.get("tools", []))
message = {"role": "assistant", "content": "Coordinator finished."}
@@ -80,6 +102,14 @@ def test_headless_terminal_result_survives_cli_exit(tmp_path):
seen_tool.add(key)
observed.append(json.loads(m["content"]))
release.touch()
+ # Hold the tool-result response until the real child has exited: on a
+ # loaded runner completion can race this provider request.
+ deadline = time.monotonic() + 25
+ receipt = home / "logs" / "process-results" / f"{observed[0]['session_id']}.json"
+ while not receipt.is_file() and time.monotonic() < deadline:
+ time.sleep(0.01)
+ if receipt.is_file():
+ completed_during_provider_reply.set()
response = {
"id": "chatcmpl-local", "object": "chat.completion", "created": 1,
"model": "test-model", "choices": [{
@@ -128,6 +158,7 @@ def test_headless_terminal_result_survives_cli_exit(tmp_path):
server.server_close()
thread.join(timeout=5)
assert producer.returncode == 0, producer.stdout + producer.stderr
+ assert completed_during_provider_reply.is_set(), producer.stdout + producer.stderr
assert "Coordinator finished." in producer.stdout
assert len(observed) == 1, (observed, producer.stdout, producer.stderr)
process_id = observed[0]["session_id"]