diff --git a/agent/chat_completion_nonstream.py b/agent/chat_completion_nonstream.py index 87d277b2b7..2c42f80e38 100644 --- a/agent/chat_completion_nonstream.py +++ b/agent/chat_completion_nonstream.py @@ -39,6 +39,7 @@ class _NonStreamRequest: else None ) self.call_start = h.time.time() + self.wait_notice_started_ts = None self.thread = None def _install_codex_request_token(self) -> None: @@ -122,10 +123,31 @@ class _NonStreamRequest: with state.lock: return state.last_event_ts, state.last_progress_ts, state.retry_started_ts - def _emit_wait_notice(self, elapsed: float) -> None: + def _emit_wait_notice(self, elapsed: float, *, heartbeat: bool = True) -> None: wd = self.wd try: last_event_ts, last_progress_ts, retry_started_ts = self._codex_watchdog_snapshot() + activity_ts = retry_started_ts if retry_started_ts is not None else last_event_ts + # Only undo a notice this request owns, promptly rather than at the + # next heartbeat: reasoning callbacks do not reset the CLI spinner. + if (self.wait_notice_started_ts is not None and activity_ts is not None + and activity_ts > self.wait_notice_started_ts): + self.agent._emit_wait_notice("Thinking...") + self.wait_notice_started_ts = None + if not heartbeat: + return + silence = self.call_start + elapsed - ( + activity_ts if activity_ts is not None else self.call_start) + if activity_ts is not None and silence < 30.0: + self.agent._touch_activity( + "waiting for first stream event after reconnect" + if retry_started_ts is not None else "receiving stream response") + return + status = "no response yet" + if retry_started_ts is not None: + status = "no response after reconnect" + elif last_event_ts is not None: + status = "no stream events" recovery = h._codex_wait_notice_recovery(stale_timeout=wd.stale_timeout, ttfb_enabled=wd.ttfb_enabled, ttfb_timeout=wd.ttfb_timeout, last_event_ts=last_event_ts, last_progress_ts=last_progress_ts, @@ -133,9 +155,12 @@ class _NonStreamRequest: call_start=self.call_start, idle_enabled=wd.idle_enabled, idle_timeout=wd.idle_timeout, idle_requires_progress=wd.idle_requires_progress, elapsed=elapsed) + if recovery and activity_ts is not None: + recovery += " total elapsed" self.agent._emit_wait_notice( f"⏳ waiting on {self.api_kwargs.get('model', 'the provider')} — " - f"{int(elapsed)}s with no response yet (provider may be slow or overloaded{recovery})") + f"{int(silence)}s with {status} (provider may be slow or overloaded{recovery})") + self.wait_notice_started_ts = self.call_start + elapsed except Exception: h.logger.debug("wait-notice construction failed", exc_info=True) @@ -227,12 +252,11 @@ class _NonStreamRequest: while t.is_alive(): t.join(timeout=0.3) poll_count += 1 - # Every ~30s: gateway inactivity heartbeat + rewrite the status line - # so users see WHAT the wait is (the "infinite thinking" complaint). + # Keep the quiet gateway heartbeat; only silence warrants a notice. + # Resumed events clear our notice on the next poll, not 30s later. now = h.time.time() elapsed = now - self.call_start - if poll_count % 100 == 0: # 100 × 0.3s = 30s - self._emit_wait_notice(elapsed) + self._emit_wait_notice(elapsed, heartbeat=poll_count % 100 == 0) last_event_ts, last_progress_ts, retry_started_ts = self._codex_watchdog_snapshot() retry_ttfb_elapsed = now - retry_started_ts if retry_started_ts is not None else None if wd.ttfb_enabled and retry_ttfb_elapsed is not None and retry_ttfb_elapsed > wd.ttfb_timeout: diff --git a/tests/agent/test_nonstream_wait_notice.py b/tests/agent/test_nonstream_wait_notice.py new file mode 100644 index 0000000000..cc5ec004c3 --- /dev/null +++ b/tests/agent/test_nonstream_wait_notice.py @@ -0,0 +1,93 @@ +"""Wait status describes request-local silence, never active generation.""" + +from types import SimpleNamespace +import threading + +import pytest + +from agent import chat_completion_helpers as h +from agent.chat_completion_nonstream import _NonStreamRequest + + +def _request(): + request = _NonStreamRequest.__new__(_NonStreamRequest) + notices, touches = [], [] + request.agent = SimpleNamespace( + _emit_wait_notice=notices.append, + _touch_activity=touches.append, + _interrupt_requested=False, + _codex_stream_last_event_ts=9999.0, # Another request cannot hide this one's stall. + ) + request.api_kwargs = {"model": "test-model"} + request.call_start = 1000.0 + request.wd = SimpleNamespace( + codex=True, stale_timeout=600.0, ttfb_enabled=True, ttfb_timeout=120.0, + idle_enabled=True, idle_timeout=180.0, idle_requires_progress=False, + ) + request.codex_watchdog_state = SimpleNamespace( + lock=threading.Lock(), last_event_ts=None, last_progress_ts=None, + retry_started_ts=None, + ) + request.wait_notice_started_ts = None + request.result = {"error": None, "response": None} + return request, notices, touches + + +@pytest.mark.parametrize( + "event,progress,retry,expected", + [ + (59.0, 59.0, None, None), # Reasoning/text/tool arguments still arriving. + (59.0, None, None, None), # Lifecycle traffic is not transport silence either. + (10.0, 10.0, None, "50s with no stream events"), + (10.0, None, None, "50s with no stream events"), + (None, None, None, "60s with no response yet"), + (10.0, 10.0, 59.0, None), # Internal reconnect gets a fresh first-event wait. + (10.0, 10.0, 20.0, "40s with no response after reconnect"), + ], +) +def test_wait_notice_tracks_current_attempt_silence(event, progress, retry, expected): + request, notices, touches = _request() + state = request.codex_watchdog_state + state.last_event_ts = None if event is None else request.call_start + event + state.last_progress_ts = None if progress is None else request.call_start + progress + state.retry_started_ts = None if retry is None else request.call_start + retry + request._emit_wait_notice(60.0) + if expected is None: + assert notices == [] + assert touches, "Quiet heartbeat must survive suppressing a visible warning" + else: + assert len(notices) == 1 + assert expected in notices[0] + assert "auto-reconnect at" in notices[0] + + +def test_resumed_events_clear_only_this_requests_wait_notice(monkeypatch): + request, notices, _ = _request() + sentinel = object() + ticks = [0] + + class Worker: + def __init__(self, **kwargs): + pass + + def start(self): + pass + + def is_alive(self): + return ticks[0] < 104 + + def join(self, timeout): + ticks[0] += 1 + if ticks[0] >= 101: + request.codex_watchdog_state.last_event_ts = 1000.0 + ticks[0] * 0.3 + if ticks[0] == 104: + request.result["response"] = sentinel + + monkeypatch.setattr(h.threading, "Thread", Worker) + monkeypatch.setattr(h.time, "time", lambda: 1000.0 + ticks[0] * 0.3) + assert request.run() is sentinel + assert len(notices) == 2 + assert "no response yet" in notices[0] + assert notices[1] + assert "waiting on" not in notices[1] + assert "no response" not in notices[1] diff --git a/website/docs/user-guide/configuration.md b/website/docs/user-guide/configuration.md index cccec83cfb..f7e3928dd1 100644 --- a/website/docs/user-guide/configuration.md +++ b/website/docs/user-guide/configuration.md @@ -1190,6 +1190,8 @@ The **stale non-stream detection** kills non-streaming calls that produce no res This budget bounds every non-streaming call. A provider that accepts a request and then goes silent — connection held open, no bytes, no error — is aborted at the stale timeout and retried, rather than hanging until the much longer socket read timeout (or, for an unattended cron run, until something external kills the process). +The Codex Responses **waiting status** describes silence, not total generation time: active stream events (including reasoning) keep it quiet. If events stop, it reports time without stream events instead of claiming no response has arrived; the notice clears when events resume. When a reconnect starts a fresh first-event watchdog phase, the waiting status follows that phase. This display behavior does not extend the separate wall-clock stale-call budget or change watchdog timeouts. + Cron jobs and delegated subagents stream too. They run the request inline on their own thread (the interrupt worker other sessions use wedges inside the gateway's nested thread pools), but the wire request is still `stream: true`, so the **stale stream detection** budget above governs them — every token counts as liveness, so a reasoning model that thinks for minutes is not mistaken for a hung provider, and edge proxies that kill silent connections keep seeing bytes. ### Disabling API streaming diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/configuration.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/configuration.md index f72df7043a..8868006a8a 100644 --- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/configuration.md +++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/configuration.md @@ -738,6 +738,8 @@ Hermes 对流式传输有单独的超时层,以及用于非流式调用的陈 **陈旧非流检测**终止长时间没有响应的非流式调用。默认情况下,Hermes 在本地端点上禁用此功能,以避免长时间预填充期间的误报。如果您显式设置 `providers..stale_timeout_seconds`、`providers..models..stale_timeout_seconds` 或 `HERMES_API_CALL_STALE_TIMEOUT`,即使在本地端点上也会遵守该显式值。 +Codex Responses 的**等待状态**描述的是没有流事件的时间,而不是生成的总时长:持续收到流事件(包括推理内容)时不会显示等待警告。如果事件停止,提示会显示多久没有收到流事件,而不会声称尚未收到任何响应;事件恢复后提示会清除。当重连进入新的首事件看门狗阶段时,等待状态也会跟随该阶段。这只影响状态显示,不会延长独立的调用总时长限制,也不会更改看门狗超时。 + ## 上下文压力警告 与迭代预算压力分开,上下文压力跟踪对话距**压缩阈值**有多近 —— 即上下文压缩触发以摘要旧消息的点。这有助于您和 agent 了解对话何时变长。