Skip to content

The agent that grows with you

fix(compression): preserve fallback before worker start

fangliquanflq0bcad4519e5b
diff --git a/agent/conversation_compression.py b/agent/conversation_compression.py
index cd3b574e0ee..4d4f086a591 100644
--- a/agent/conversation_compression.py
+++ b/agent/conversation_compression.py
@@ -1316,6 +1316,10 @@ def run_compress_context_with_progress_timeout(
# (worker slot freed late). Check the fence BEFORE any expensive
# summary work so a stale job never burns an LLM call; its return
# value is discarded by the already-departed host.
+ if worker_fence.deadline_exceeded:
+ raise concurrent.futures.TimeoutError(
+ "compression deadline expired before worker start"
+ )
if worker_fence.is_cancelled:
logger.info(
"Skipping stale compression job: fence cancelled before start"
@@ -1362,7 +1366,11 @@ def run_compress_context_with_progress_timeout(
except concurrent.futures.TimeoutError:
waited = time.monotonic() - wait_started
since_progress = fence.seconds_since_progress()
- if since_progress < idle and waited < ceiling:
+ if (
+ not fence.deadline_exceeded
+ and since_progress < idle
+ and waited < ceiling
+ ):
logger.info(
"Context compression still streaming after %.0fs "
"(last progress %.1fs ago) — extending wait "
diff --git a/tests/agent/test_compress_context_progress_timeout.py b/tests/agent/test_compress_context_progress_timeout.py
index 1b83f38773a..1a81a1d495c 100644
--- a/tests/agent/test_compress_context_progress_timeout.py
+++ b/tests/agent/test_compress_context_progress_timeout.py
@@ -8,12 +8,14 @@ for the owned wrapper used when callers do not pass a ``commit_fence``.
from __future__ import annotations
+import concurrent.futures
import threading
import time
from unittest.mock import MagicMock
import pytest
+import agent.conversation_compression as cc
from agent.conversation_compression import (
CompressionCommitFence,
resolve_context_compression_timeouts,
@@ -46,6 +48,46 @@ class TestResolveContextCompressionTimeouts:
class TestRunCompressContextWithProgressTimeout:
+ def test_deadline_before_worker_start_uses_timeout_fallback(self, monkeypatch):
+ original = [{"role": "user", "content": "keep-me"}]
+ worker = MagicMock()
+ fallback = MagicMock(return_value="fallback-prompt")
+ timeouts = []
+
+ class _ExpiredBeforeStartExecutor:
+ def submit(self, fn, fence):
+ fence._deadline = time.monotonic() - 1.0
+ future = concurrent.futures.Future()
+ try:
+ future.set_result(fn(fence))
+ except BaseException as exc:
+ future.set_exception(exc)
+ return future
+
+ monkeypatch.setattr(
+ cc,
+ "_get_compress_timeout_executor",
+ _ExpiredBeforeStartExecutor,
+ )
+
+ result_msgs, result_prompt = run_compress_context_with_progress_timeout(
+ worker=worker,
+ messages=original,
+ system_prompt_fallback=fallback,
+ idle_timeout_seconds=1.0,
+ total_ceiling_seconds=1.0,
+ on_timeout=lambda idle, waited, since: timeouts.append(
+ (idle, waited, since)
+ ),
+ stall_fallback=False,
+ )
+
+ worker.assert_not_called()
+ fallback.assert_called_once_with()
+ assert result_msgs is original
+ assert result_prompt == "fallback-prompt"
+ assert len(timeouts) == 1
+
def test_silent_worker_times_out_and_preserves_messages(self):
original = [{"role": "user", "content": "keep-me"}]
started = threading.Event()