Skip to content

The agent that grows with you

fix(auxiliary): a hard stop closes a streamed compression summary at its next chunk

Teknium9870c38661f2
diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.py
index a2a26784c6b..5b541da88bb 100644
--- a/agent/auxiliary_client.py
+++ b/agent/auxiliary_client.py
@@ -448,8 +448,8 @@ def _run_protected_sync_provider_call(callback: Callable[[dict[str, Any]], Any],
Aux clients are process-shared and cannot be closed to wake one request, so the callback (incl.
stream aggregation) runs in a daemon while the owner polls cancellation; on cancel the owner
- unwinds at once and the daemon finishes under the provider timeout in ``kwargs`` (it owns no
- transcript/commit state, never holds the session lock). Unprotected / no cancel source: direct.
+ unwinds at once; the daemon stops at its next streamed chunk, else under the provider timeout in ``kwargs``
+ (it owns no transcript/commit state, never holds the session lock). Unprotected / no cancel source: direct.
"""
source_cancel_check = _capture_aux_cancel_check()
if not _aux_interrupt_protected() or not callable(source_cancel_check):
@@ -7176,11 +7176,8 @@ class _ChatStreamAccumulator:
# llama-server prompt reading doesn't count toward the ceiling (``local_prompt_progress``).
self._prompt_reading = PromptReadingClock(self._started)
self._total_ceiling = total_ceiling
- # Absolute instant the waiting host gives up; checked alongside (not instead of) the
- # ceiling, and unaffected by pre-construction dispatch/TTFT.
- # Checked as well as (not instead of) the ceiling above: the ceiling still bounds callers with no
- # host deadline, and the host deadline is absolute, so it is unaffected by however long dispatch and
- # TTFT took before this accumulator was constructed. See #99692.
+ # Absolute instant the waiting host gives up, checked as well as (not instead of) the ceiling, which
+ # still bounds callers with no host deadline; unaffected by pre-construction dispatch/TTFT (#99692).
self._host_deadline = host_deadline
self.content_parts: list[str] = []
self.reasoning_parts: list[str] = []
@@ -7191,7 +7188,10 @@ class _ChatStreamAccumulator:
self.resp_model = model or ""
def _check_deadlines(self) -> None:
- """Raise TimeoutError past the total ceiling or the host deadline."""
+ """Raise TimeoutError past the total ceiling or the host deadline; a host hard cancel (/stop, session
+ boundary) stops the stream at its next chunk instead of letting the daemon bill on to the deadline."""
+ if _aux_interrupt_cancel_requested():
+ raise AuxiliaryExplicitCancellation()
now = time.monotonic()
if self._total_ceiling is not None and (now - self._started - self._prompt_reading.seconds) >= self._total_ceiling:
raise TimeoutError(f"Auxiliary streamed call timed out after {self._total_ceiling:.0f}s "
diff --git a/tests/agent/test_aux_summary_stream_hard_cancel.py b/tests/agent/test_aux_summary_stream_hard_cancel.py
new file mode 100644
index 00000000000..65ae4195626
--- /dev/null
+++ b/tests/agent/test_aux_summary_stream_hard_cancel.py
@@ -0,0 +1,95 @@
+"""A hard cancel (/stop, session boundary) of a compression summary closes the provider stream.
+
+The owner unwinds at once, but the streamed summary runs in an isolated daemon whose chunk consumer only
+checked the total ceiling and the host deadline, so the provider stream kept generating (and billing) until
+that deadline, up to ten minutes later. It must close at the next chunk. Real HTTP path against a loopback
+server that trickles a chunk every 50 ms, with a temp ``HERMES_HOME``.
+"""
+
+import json
+import threading
+import time
+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
+
+import pytest
+
+import agent.auxiliary_client as aux
+
+
+def _chunk(text, finish=None):
+ payload = {"id": "c", "object": "chat.completion.chunk", "created": 1, "model": "m",
+ "choices": [{"index": 0, "delta": {"content": text} if text else {}, "finish_reason": finish}]}
+ return f"data: {json.dumps(payload)}\n\n".encode()
+
+
+def _serve(handler):
+ server = ThreadingHTTPServer(("127.0.0.1", 0), handler)
+ thread = threading.Thread(target=server.serve_forever, kwargs={"poll_interval": 0.01}, daemon=True)
+ thread.start()
+ return server, thread
+
+
+def _stop(server, thread):
+ aux.shutdown_cached_clients()
+ server.shutdown()
+ server.server_close()
+ thread.join(timeout=5)
+
+
+def _write_config(home, config):
+ (home / "config.yaml").write_text(json.dumps(config), encoding="utf-8")
+
+
+def trickle_endpoint():
+ """Streams one content chunk every 50 ms for up to 30 s; records when the client hangs up."""
+ sent_some, client_closed = threading.Event(), threading.Event()
+
+ class Handler(BaseHTTPRequestHandler):
+ protocol_version = "HTTP/1.1"
+
+ def log_message(self, *_args):
+ pass
+
+ def do_POST(self):
+ self.rfile.read(int(self.headers.get("Content-Length") or 0))
+ self.send_response(200)
+ self.send_header("Content-Type", "text/event-stream")
+ self.send_header("Connection", "close")
+ self.end_headers()
+ try:
+ for n in range(600):
+ self.wfile.write(_chunk("word "))
+ self.wfile.flush()
+ if n == 3:
+ sent_some.set()
+ time.sleep(0.05) # health: allow HX013 -- the trickle pace is the slow provider under test
+ except OSError:
+ client_closed.set()
+ self.close_connection = True
+
+ aux.shutdown_cached_clients()
+ aux._reset_aux_unhealthy_cache()
+ server, thread = _serve(Handler)
+ try:
+ yield f"http://127.0.0.1:{server.server_port}/v1", sent_some, client_closed
+ finally:
+ _stop(server, thread)
+
+
+def test_hard_cancel_closes_the_streamed_summary_at_its_next_chunk(tmp_path, monkeypatch, trickle_endpoint):
+ url, sent_some, client_closed = trickle_endpoint
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ monkeypatch.setenv("NO_PROXY", "127.0.0.1,localhost")
+ _write_config(tmp_path, {"auxiliary": {"compression": {
+ "provider": "custom", "base_url": url, "api_key": "dummy", "model": "slow-summarizer"}}})
+ stop = threading.Event()
+ threading.Thread(target=lambda: sent_some.wait(10) and stop.set(), daemon=True).start()
+
+ # The compression attempt's seams: a progress hook (streams the call) and the hard-cancel source.
+ with aux.aux_progress_hook(lambda: None), aux.aux_interrupt_protection(cancel_check=stop.is_set):
+ with pytest.raises(aux.AuxiliaryExplicitCancellation):
+ aux.call_llm(task="compression", messages=[{"role": "user", "content": "Summarize."}], timeout=20)
+
+ assert stop.is_set()
+ assert client_closed.wait(5), "the cancelled summary stream kept streaming after the hard stop"