Skip to content

The agent that grows with you

refactor(agent): derive the Codex attempt origin and pre-progress phase once

kshitijk4poor8b9de5592d92
diff --git a/agent/chat_completion_nonstream.py b/agent/chat_completion_nonstream.py
index f43fa87d17e..62d5bd7af51 100644
--- a/agent/chat_completion_nonstream.py
+++ b/agent/chat_completion_nonstream.py
@@ -119,20 +119,29 @@ class _NonStreamRequest:
return self.api_kwargs.get("model", "unknown")
def _codex_watchdog_snapshot(self):
+ """``(last_event_ts, last_progress_ts, retry_started_ts, attempt_started_ts)`` under one lock.
+
+ ``attempt_started_ts`` is the physical attempt's origin (the reconnect marker, else
+ ``call_start``); the notice and the kill loop must anchor to the same value."""
state = self.codex_watchdog_state
if state is None: # non-codex request: no watchdog reads these
- return (None, None, None)
+ return (None, None, None, self.call_start)
with state.lock:
- return state.last_event_ts, state.last_progress_ts, state.retry_started_ts
+ retry_started_ts = state.retry_started_ts
+ return (state.last_event_ts, state.last_progress_ts, retry_started_ts,
+ retry_started_ts if retry_started_ts is not None else self.call_start)
+
+ def _pre_progress(self, last_event_ts, last_progress_ts) -> bool:
+ """Stream open on this attempt, but no substantive model progress yet."""
+ return self.wd.progress_timeout > 0 and last_event_ts is not None and last_progress_ts is 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()
- pre_progress = bool(wd.progress_timeout and last_event_ts is not None and last_progress_ts is None)
- attempt_started_ts = retry_started_ts if retry_started_ts is not None else self.call_start
+ last_event_ts, last_progress_ts, retry_started_ts, attempt_started_ts = self._codex_watchdog_snapshot()
+ pre_progress = self._pre_progress(last_event_ts, last_progress_ts)
activity_ts = attempt_started_ts if pre_progress else (
- last_event_ts if last_event_ts is not None else retry_started_ts)
+ retry_started_ts if retry_started_ts is not None else last_event_ts)
# Lifecycle chatter is not progress: once pre-progress begins it must not
# clear/reset this notice. Real progress changes phase and clears it.
if (self.wait_notice_started_ts is not None and activity_ts is not None
@@ -246,7 +255,7 @@ class _NonStreamRequest:
def _interrupt(self, elapsed: float) -> None:
agent = self.agent
- last_event_ts, _, _ = self._codex_watchdog_snapshot()
+ last_event_ts, _, _, _ = self._codex_watchdog_snapshot()
h._record_interrupted_provider_wait(agent, elapsed,
response_started=self.wd.codex and last_event_ts is not None
)
@@ -282,13 +291,12 @@ class _NonStreamRequest:
now = h.time.time()
elapsed = now - self.call_start
self._emit_wait_notice(elapsed, heartbeat=poll_count % 100 == 0)
- last_event_ts, last_progress_ts, retry_started_ts = self._codex_watchdog_snapshot()
- attempt_started_ts = retry_started_ts if retry_started_ts is not None else self.call_start
+ last_event_ts, last_progress_ts, retry_started_ts, attempt_started_ts = self._codex_watchdog_snapshot()
attempt_elapsed = now - attempt_started_ts
if (wd.ttfb_enabled and last_event_ts is None and attempt_elapsed > wd.ttfb_timeout):
self._ttfb_kill(attempt_elapsed)
break
- if (wd.progress_timeout > 0 and last_event_ts is not None and last_progress_ts is None
+ if (self._pre_progress(last_event_ts, last_progress_ts)
and attempt_elapsed > wd.progress_timeout):
self._progress_kill(attempt_elapsed)
break