chat_completion_wait_notice.py 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """Operator-facing wait notice for long provider silences (#92550).
  2. The 30s heartbeat is gateway liveness, not a warning. Once a request has been
  3. silent past the notice threshold this module decides WHAT the status line says
  4. (neutral: which phase of the wait we are in, which watchdog would reconnect and
  5. when) and WHETHER to rewrite it: once when the silence starts, again only when
  6. the wait phase changes or a watchdog deadline is near. Watchdog thresholds and
  7. retry policy live with the watchdogs; this is presentation only.
  8. """
  9. import math
  10. from typing import Optional
  11. NEAR_DEADLINE_SECS = 15.0
  12. def _near_deadline(watchdog: Optional[tuple[str, float]]) -> bool:
  13. return watchdog is not None and watchdog[1] <= NEAR_DEADLINE_SECS
  14. _PHASE_TEXT = {
  15. # Codex Responses (non-stream request path)
  16. "first_event": "{n}s waiting for the first provider event",
  17. "reconnect": "{n}s waiting for the first provider event after reconnect",
  18. "pre_progress": "provider stream open; {n}s without substantive model progress",
  19. "post_event": "provider stream active; {n}s without stream events",
  20. # Chat-completions streaming path
  21. "first_chunk": "{n}s waiting for the first stream chunk",
  22. "post_chunk": "stream open; {n}s without stream output",
  23. }
  24. def wait_notice_text(model: str, silence_secs: float, phase: str,
  25. watchdog: Optional[tuple[str, float]] = None) -> str:
  26. """One neutral status line. ``watchdog`` is ``(label, seconds_until_it_fires)``."""
  27. lead = "still waiting on" if _near_deadline(watchdog) else "waiting on"
  28. text = f"⏳ {lead} {model} — " + _PHASE_TEXT[phase].format(n=int(silence_secs))
  29. if watchdog is not None:
  30. label, remaining = watchdog
  31. text += f" (auto-reconnect: {label} watchdog in {max(0, int(remaining))}s)"
  32. return text
  33. def codex_watchdog_deadline(*, stale_timeout: float, ttfb_enabled: bool, ttfb_timeout: float,
  34. last_event_ts: Optional[float], last_progress_ts: Optional[float],
  35. retry_started_ts: Optional[float], call_start: float, idle_enabled: bool,
  36. idle_timeout: float, idle_requires_progress: bool, elapsed: float,
  37. progress_timeout: float = 0.0) -> Optional[tuple[str, float]]:
  38. """Earliest enabled Codex watchdog as ``(label, seconds_until_it_fires)``; None when
  39. none applies (disabled/infinite, or its deadline already passed)."""
  40. deadlines: list[tuple[str, float]] = []
  41. if math.isfinite(stale_timeout):
  42. deadlines.append(("wall-clock stale", stale_timeout))
  43. attempt_offset = max(0.0, retry_started_ts - call_start) if retry_started_ts is not None else 0.0
  44. if last_event_ts is None:
  45. if ttfb_enabled and math.isfinite(ttfb_timeout):
  46. deadlines.append(("TTFB", attempt_offset + ttfb_timeout))
  47. elif progress_timeout > 0 and last_progress_ts is None:
  48. deadlines.append(("first progress", attempt_offset + progress_timeout))
  49. elif (not idle_requires_progress or last_progress_ts is not None) and idle_enabled and math.isfinite(idle_timeout):
  50. deadlines.append(("stream idle", max(0.0, last_event_ts - call_start) + idle_timeout))
  51. if not deadlines:
  52. return None
  53. label, deadline = min(deadlines, key=lambda d: d[1])
  54. if deadline <= elapsed:
  55. return None
  56. return label, deadline - elapsed
  57. class WaitNoticeState:
  58. """Per-request memory of what the status line currently shows.
  59. ``should_emit`` is True for the first notice of a silence, for a phase or
  60. watchdog change, and once when the applicable deadline comes within
  61. ``NEAR_DEADLINE_SECS``; every other heartbeat only touches liveness.
  62. ``reset`` when activity resumes so the next silence gets a fresh notice.
  63. """
  64. def __init__(self) -> None:
  65. self.reset()
  66. def reset(self) -> None:
  67. self.phase: Optional[str] = None
  68. self.watchdog_label: Optional[str] = None
  69. self.near_shown = False
  70. def should_emit(self, phase: str, watchdog: Optional[tuple[str, float]]) -> bool:
  71. label = watchdog[0] if watchdog is not None else None
  72. near = _near_deadline(watchdog)
  73. emit = self.phase != phase or self.watchdog_label != label or (near and not self.near_shown)
  74. self.phase, self.watchdog_label = phase, label
  75. if near:
  76. self.near_shown = True
  77. return emit