turn_context_compaction.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. """Turn-start compaction for ``build_turn_context`` plus the small compression-attempt
  2. helpers shared with the pre-API / post-tool sites in ``turn_preflight``.
  3. Three passes, in order: idle-triggered compaction (opt-in, wall-clock gap), preflight
  4. context compression (token threshold), and the uncompressed-session overflow-warning
  5. re-arm. ``run_turn_start_compaction`` mutates ``agent`` exactly as the inline prologue
  6. did and returns a ``CompactionOutcome``. Predicates/estimators that tests patch on
  7. ``agent.turn_context`` are imported lazily through that module so patches intercept."""
  8. from __future__ import annotations
  9. import logging
  10. import time
  11. from dataclasses import dataclass
  12. from typing import Any, Dict, List, Optional
  13. from agent.context_engine import automatic_compaction_status_message
  14. from agent.conversation_compression import (
  15. IDLE_COMPACTION_STATUS_TEMPLATE, PREFLIGHT_COMPRESSION_STATUS_TEMPLATE,
  16. compression_skipped_due_to_lock, conversation_history_after_compression,
  17. )
  18. logger = logging.getLogger("agent.turn_context")
  19. @dataclass
  20. class CompactionOutcome:
  21. """Locals rebuilt by turn-start compaction (``build_turn_context`` reads them back)."""
  22. messages: list[dict[str, Any]]
  23. active_system_prompt: Optional[str]
  24. conversation_history: Optional[list[dict[str, Any]]]
  25. current_turn_user_idx: int
  26. # A preflight pass (threshold or engine-driven) actually rebuilt ``messages``.
  27. compressed: bool = False
  28. # Preflight proved an immediate retry ineffective (no progress / insufficient).
  29. blocked: bool = False
  30. # ── Helpers shared by every compression-attempt site ──
  31. def _clear_overflow_warn(agent: Any) -> None:
  32. """Re-arm the context-overflow warning dedup (test doubles may lack the method)."""
  33. # Compression is actually running (block cleared / was never blocked) — reset the blocked-overflow
  34. # warning dedup so a future blocked-over-threshold turn can warn again. Mirrors the turn-context
  35. # preflight reset (silent-overflow fix #62625). getattr guard: test doubles built via object.__new__
  36. # lack the method (gateway test-double pitfall) — treat absence as no-op.
  37. # Compression is actually running (block cleared / was never blocked) — reset the blocked-overflow
  38. # warning dedup so a future blocked-over-threshold turn can warn again (silent-overflow fix #62625).
  39. # getattr guard: test doubles built via object.__new__ lack the method (gateway test-double pitfall) —
  40. # treat absence as no-op.
  41. _clear_warn = getattr(agent, "_clear_context_overflow_warn", None)
  42. if callable(_clear_warn):
  43. _clear_warn()
  44. def _reset_retry_state_after_compaction(agent: Any) -> None:
  45. """Give the compacted request a fresh chance: clear retry/empty-response state."""
  46. agent._empty_content_retries = 0
  47. agent._thinking_prefill_retries = 0
  48. agent._last_content_with_tools = None
  49. agent._last_content_tools_all_housekeeping = False
  50. agent._mute_post_response = False
  51. def _blocked_compress_reason(
  52. compressor: Any, tokens: int, attempts_spent: Optional[int] = None
  53. ) -> Optional[str]:
  54. """Why an over-threshold request is blocked (``None`` below threshold or when the
  55. engine lacks ``should_compress_info`` / raises).
  56. ``attempts_spent``: when given and the engine says compression SHOULD run
  57. (``(True, None)``) yet the caller skipped it, the per-turn attempt budget is
  58. spent — name it ``attempts_exhausted:<n>`` instead of dropping the
  59. ``(True, None)`` on the floor (silent-lockout case, #101889)."""
  60. _info = getattr(compressor, "should_compress_info", None)
  61. if not callable(_info):
  62. return None
  63. try:
  64. _should_now, _reason = _info(tokens)
  65. except Exception:
  66. return None
  67. if attempts_spent is not None and _should_now and not _reason:
  68. return f"attempts_exhausted:{attempts_spent}"
  69. return _reason
  70. def _apply_grown_window(agent: Any, compressor: Any, grown: int) -> None:
  71. """A managed local runtime granted a bigger window: recalibrate the compressor."""
  72. compressor.update_model(
  73. agent.model, grown, base_url=getattr(agent, "base_url", "") or "",
  74. api_key=getattr(agent, "api_key", "") or "",
  75. provider=getattr(agent, "provider", "") or "",
  76. api_mode=getattr(agent, "api_mode", "") or "",
  77. )
  78. agent._buffer_status(
  79. f"📈 Context window grown to {grown // 1024}K "
  80. f"(local model; conversation continues uncompressed)"
  81. )
  82. def _refund_api_call(agent: Any, api_call_count: int) -> int:
  83. """Refund the call count and iteration budget for a pass that should not consume it:
  84. one that never reached the provider (preflight) or a provider-switch fallback hop."""
  85. # Host progress-aware timeout (#98722, salvaged from #98741): this preflight iteration never reached the
  86. # provider. Refund its provisional call/budget exactly like a successful pre-API compaction, then stop
  87. # before the unchanged oversized request reaches the provider — its overflow error would only invoke
  88. # compression again on the same transcript with the wait budget already spent.
  89. api_call_count -= 1
  90. agent._api_call_count = api_call_count
  91. agent.iteration_budget.refund()
  92. return api_call_count
  93. def _reanchor(agent: Any, messages: list[Any], user_message: Any) -> int:
  94. """Compaction rebuilt ``messages``: re-anchor this turn's user index so the
  95. api_content stamp, injection site and persist-override row hit the same dict."""
  96. from agent.turn_context import reanchor_current_turn_user_idx
  97. idx = reanchor_current_turn_user_idx(messages, user_message)
  98. agent._persist_user_message_idx = idx
  99. return idx
  100. # ── Turn-start passes ──
  101. def run_turn_start_compaction(
  102. agent: Any, *, messages: list[dict[str, Any]], system_message: Optional[str],
  103. active_system_prompt: Optional[str], conversation_history: Optional[list[dict[str, Any]]],
  104. current_turn_user_idx: int, user_message: Any, effective_task_id: str,
  105. ) -> CompactionOutcome:
  106. """Idle compaction, then preflight compression (or the uncompressed guard)."""
  107. out = CompactionOutcome(
  108. messages=messages, active_system_prompt=active_system_prompt,
  109. conversation_history=conversation_history, current_turn_user_idx=current_turn_user_idx,
  110. )
  111. _idle_compaction(agent, out, system_message, user_message, effective_task_id)
  112. _preflight_compression(agent, out, system_message, user_message, effective_task_id)
  113. return out
  114. def _idle_compaction(
  115. agent: Any, out: CompactionOutcome, system_message: Optional[str], user_message: Any,
  116. effective_task_id: str,
  117. ) -> None:
  118. """Idle-triggered compaction (opt-in; ``idle_compact_after_seconds``): fires on the
  119. wall-clock gap since ``_last_activity_ts``; a cheap gap check gates the estimate."""
  120. from agent import turn_context as _tc
  121. messages = out.messages
  122. _idle_after = getattr(agent, "compression_idle_compact_after_seconds", 0)
  123. if not (agent.compression_enabled and _idle_after > 0 and messages):
  124. return
  125. _idle_gap = time.time() - getattr(agent, "_last_activity_ts", time.time())
  126. if _idle_gap < _idle_after:
  127. return
  128. _compressor = agent.context_compressor
  129. # A live or restored native checkpoint must reach its issuer once so real usage,
  130. # rather than an opaque ciphertext estimate, decides whether local compression is
  131. # still needed. Threshold and post-tool preflight honor the same latch.
  132. if bool(getattr(_compressor, "awaiting_real_usage_after_compression", False)):
  133. return
  134. # Route-aware pressure: on compacted native-Codex sessions the durable figure
  135. # overstates the wire, so reuse the preflight estimator.
  136. _idle_tokens = _tc._preflight_request_tokens(
  137. agent, messages, out.active_system_prompt or ""
  138. )
  139. # Don't summarise a thread already below the post-compression target size.
  140. _idle_floor = int(_compressor.threshold_tokens * _compressor.summary_target_ratio)
  141. _idle_cooldown = getattr(
  142. _compressor, "get_active_compression_failure_cooldown", lambda: None
  143. )()
  144. # What the previous pass actually produced — the honest floor versus the theoretical
  145. # ``_idle_floor``. Type pin: compressor doubles expose truthy non-ints here; only a real
  146. # int may raise the floor, anything else falls back to 0 (original semantics).
  147. _idle_last_compaction = getattr(_compressor, "last_compression_rough_tokens", 0)
  148. if not isinstance(_idle_last_compaction, int) or isinstance(_idle_last_compaction, bool):
  149. _idle_last_compaction = 0
  150. if not _tc._should_idle_compact(
  151. enabled=agent.compression_enabled, idle_after_seconds=_idle_after,
  152. idle_gap_seconds=_idle_gap, tokens=_idle_tokens, floor_tokens=_idle_floor,
  153. cooldown_active=bool(_idle_cooldown), last_compaction_tokens=_idle_last_compaction,
  154. ):
  155. return
  156. logger.info(
  157. "Idle compaction: %ss idle >= %ss, ~%s tokens > %s floor (last compaction produced ~%s) (session %s)",
  158. int(_idle_gap), _idle_after, f"{_idle_tokens:,}", f"{_idle_floor:,}",
  159. f"{_idle_last_compaction:,}" if _idle_last_compaction > 0 else "n/a",
  160. agent.session_id or "none",
  161. )
  162. _idle_status = automatic_compaction_status_message(
  163. _compressor,
  164. phase="idle",
  165. default_message=IDLE_COMPACTION_STATUS_TEMPLATE.format(
  166. idle_seconds=int(_idle_gap), tokens=_idle_tokens
  167. ),
  168. approx_tokens=_idle_tokens,
  169. idle_seconds=int(_idle_gap),
  170. model=agent.model,
  171. )
  172. if _idle_status:
  173. agent._emit_status(_idle_status)
  174. out.messages, out.active_system_prompt = agent._compress_context(
  175. messages, system_message, approx_tokens=_idle_tokens, task_id=effective_task_id, trigger="idle",
  176. )
  177. # ``_compress_context`` returns the INPUT list object when it skips; only
  178. # re-baseline and re-anchor after a real compaction.
  179. if out.messages is not messages:
  180. out.conversation_history = conversation_history_after_compression(
  181. agent, out.messages, out.conversation_history
  182. )
  183. out.current_turn_user_idx = _reanchor(agent, out.messages, user_message)
  184. def _codex_native_auto_compaction(agent: Any) -> bool:
  185. """Codex app-server threads are compacted by the codex agent itself; Hermes only
  186. initiates compaction in "hermes" mode."""
  187. return (
  188. # See #36801.
  189. getattr(agent, "api_mode", None) == "codex_app_server"
  190. and str(
  191. getattr(agent, "codex_app_server_auto_compaction", "native") or "native"
  192. ).lower()
  193. in {"native", "off"}
  194. )
  195. def _preflight_compression(
  196. agent: Any, out: CompactionOutcome, system_message: Optional[str], user_message: Any,
  197. effective_task_id: str,
  198. ) -> None:
  199. """Preflight context compression; the cheap pre-check gates the full estimate
  200. (see ``_should_run_preflight_estimate`` for the OR semantics)."""
  201. from agent import turn_context as _tc
  202. agent._turn_received_provider_response = False
  203. agent._turn_preflight_display_snapshot = None
  204. if not agent.compression_enabled:
  205. _rearm_uncompressed_overflow_warn(agent, out.messages, out.active_system_prompt)
  206. return
  207. _compressor = agent.context_compressor
  208. # A structural no-op ("nothing eligible among N messages") is a verdict about that transcript;
  209. # the gateway daemon keeps this compressor across turns, so a grown transcript re-arms it.
  210. _lift = getattr(_compressor, "lift_structural_backoff_if_grown", None)
  211. if callable(_lift):
  212. _lift(len(out.messages))
  213. if _tc._review_fork_first_request_pending(agent) or not _tc._should_run_preflight_estimate(
  214. out.messages, _compressor.protect_first_n, _compressor.protect_last_n,
  215. _compressor.threshold_tokens,
  216. ):
  217. return
  218. _preflight_tokens = _tc._preflight_request_tokens(
  219. agent, out.messages, out.active_system_prompt or ""
  220. )
  221. # getattr guard: compressor doubles and plugin engines lack this method — absence
  222. # means no snapshot and the finalizer's rollback stays disarmed.
  223. _snapshot_fn = getattr(_compressor, "snapshot_preflight_display_tokens", None)
  224. if callable(_snapshot_fn):
  225. _snapshot_val = _snapshot_fn()
  226. # Type pin: MagicMock compressors return truthy Mock objects — only a real int
  227. # snapshot may arm the interrupted-turn rollback.
  228. if isinstance(_snapshot_val, int) and not isinstance(_snapshot_val, bool):
  229. agent._turn_preflight_display_snapshot = _snapshot_val
  230. # An anchored figure is real usage + delta: never deferred.
  231. _preflight_deferred = not getattr(agent, "_request_pressure_anchored", False) and getattr(
  232. _compressor, "should_defer_preflight_to_real_usage", lambda _tokens: False
  233. )(_preflight_tokens)
  234. _codex_native_auto = _codex_native_auto_compaction(agent)
  235. if not _preflight_deferred:
  236. # Display-only seed: a real provider reading wins and the -1 sentinel stays
  237. # protected. Also feeds the tool-loop gate on usage-less responses.
  238. _maybe_seed = getattr(_compressor, "maybe_seed_preflight_display_tokens", None)
  239. if callable(_maybe_seed):
  240. _maybe_seed(_preflight_tokens)
  241. _compression_cooldown = getattr(
  242. _compressor, "get_active_compression_failure_cooldown", lambda: None
  243. )()
  244. _should_compress_now = False
  245. _compress_block_reason = None
  246. if _preflight_deferred:
  247. logger.info(
  248. "Skipping preflight compression: rough estimate ~%s >= %s is not anchored on "
  249. "real usage (last real provider prompt %s); deferring to the next response",
  250. f"{_preflight_tokens:,}", f"{_compressor.threshold_tokens:,}",
  251. f"{_compressor.last_real_prompt_tokens:,}",
  252. )
  253. elif _compression_cooldown:
  254. logger.info(
  255. "Skipping preflight compression: same-session cooldown active "
  256. "(~%s seconds remaining, session %s)",
  257. int(_compression_cooldown.get("remaining_seconds", 0.0)),
  258. agent.session_id or "none",
  259. )
  260. if _preflight_tokens >= _compressor.threshold_tokens:
  261. # Over threshold but blocked by the summary-LLM cooldown — surface a warning.
  262. _cooldown_secs = _compression_cooldown.get("remaining_seconds", 0.0)
  263. _compress_block_reason = f"cooldown:{_cooldown_secs:.0f}"
  264. elif _codex_native_auto:
  265. logger.info(
  266. "Skipping Hermes preflight compression for codex app-server "
  267. "(mode=%s); Hermes will not start thread compaction here.",
  268. getattr(agent, "codex_app_server_auto_compaction", "native"),
  269. )
  270. else:
  271. _should_compress_now = _compressor.should_compress(_preflight_tokens)
  272. if not _should_compress_now:
  273. _compress_block_reason = _blocked_compress_reason(_compressor, _preflight_tokens)
  274. if _should_compress_now:
  275. # Managed local runtime: growing the window beats compressing (ladder order;
  276. # same seam as _maybe_grow_local_window in the loop).
  277. try:
  278. from agent.conversation_loop import _maybe_grow_local_window
  279. _grown = _maybe_grow_local_window(agent, _compressor, _preflight_tokens)
  280. except Exception:
  281. _grown = None
  282. if _grown:
  283. _apply_grown_window(agent, _compressor, _grown)
  284. _should_compress_now = _compressor.should_compress(_preflight_tokens)
  285. if _should_compress_now:
  286. _run_preflight_passes(
  287. agent, out, _compressor, _preflight_tokens, system_message, effective_task_id
  288. )
  289. elif _compress_block_reason:
  290. # Over threshold but compression blocked: surface a deduped warning so the
  291. # user can /new or /compress instead of a silent provider limit.
  292. agent._warn_context_overflow_blocked(
  293. _compress_block_reason, _preflight_tokens, _compressor.threshold_tokens
  294. )
  295. else:
  296. # Sub-threshold and unblocked — re-arm the overflow warning.
  297. _clear_overflow_warn(agent)
  298. # Engine maintenance only when NO skip-branch fired: cooldown, deferred
  299. # estimate, or codex-native route keep the engine hook unconsulted.
  300. if not (_compression_cooldown or _preflight_deferred or _codex_native_auto):
  301. _engine_preflight_maintenance(
  302. agent, out, _compressor, _preflight_tokens, system_message, effective_task_id
  303. )
  304. if out.compressed:
  305. # Compression rebuilt the list, so the pre-compression user index is stale.
  306. # Exact-content match first so a todo-snapshot can't steal it.
  307. out.current_turn_user_idx = _reanchor(agent, out.messages, user_message)
  308. def _run_preflight_passes(
  309. agent: Any, out: CompactionOutcome, _compressor: Any, _preflight_tokens: int,
  310. system_message: Optional[str], effective_task_id: str,
  311. ) -> None:
  312. """Threshold-triggered preflight passes (honor ``compression.max_attempts`` like
  313. the loop's sites, default 3)."""
  314. from agent import turn_context as _tc
  315. out.compressed = True
  316. # Compression is actually running — reset the dedup so a future blocked turn can
  317. # warn again.
  318. _clear_overflow_warn(agent)
  319. logger.info(
  320. "Preflight compression: ~%s tokens >= %s threshold (model %s, ctx %s)",
  321. f"{_preflight_tokens:,}", f"{_compressor.threshold_tokens:,}", agent.model,
  322. f"{_compressor.context_length:,}",
  323. )
  324. _preflight_status = automatic_compaction_status_message(
  325. _compressor,
  326. phase="preflight",
  327. default_message=PREFLIGHT_COMPRESSION_STATUS_TEMPLATE.format(
  328. tokens=_preflight_tokens, threshold=_compressor.threshold_tokens
  329. ),
  330. approx_tokens=_preflight_tokens,
  331. threshold_tokens=_compressor.threshold_tokens,
  332. context_length=_compressor.context_length,
  333. model=agent.model,
  334. )
  335. if _preflight_status:
  336. agent._emit_status(_preflight_status)
  337. _max_preflight_passes = max(1, int(getattr(agent, "max_compression_attempts", 3) or 3))
  338. for _pass in range(_max_preflight_passes):
  339. _preflight_input = out.messages
  340. _orig_len = len(_preflight_input)
  341. _orig_tokens = _preflight_tokens
  342. out.messages, out.active_system_prompt = agent._compress_context(
  343. _preflight_input, system_message, approx_tokens=_preflight_tokens,
  344. task_id=effective_task_id, trigger="turn_start_threshold",
  345. )
  346. if out.messages is _preflight_input and compression_skipped_due_to_lock(agent):
  347. # Lock-skip: another path holds the lock, so this is a DEFER, not proof of
  348. # incompressibility — don't arm the blocker; stop passes this turn.
  349. logger.info(
  350. # That is a temporary DEFER, not proof the transcript cannot compress — do NOT arm the
  351. # insufficient-progress blocker (the loop's error handlers must keep their provider-proven
  352. # retry budget) and stop preflight passes for this turn; the lock winner is shrinking the
  353. # same session concurrently. See #69870.
  354. "Preflight compression deferred: compression lock "
  355. "held by another path (session %s)",
  356. agent.session_id or "none",
  357. )
  358. break
  359. # Re-estimate so size-only compression (same rows, fewer tokens) counts as
  360. # progress.
  361. _preflight_tokens = _tc._preflight_request_tokens(
  362. agent, out.messages, out.active_system_prompt or ""
  363. )
  364. if not _tc.compression_made_progress(
  365. _orig_len, len(out.messages), _orig_tokens, _preflight_tokens
  366. ):
  367. _tc._fail_closed_after_preflight_timeout(agent, _preflight_tokens)
  368. _tc._fail_closed_on_insufficient_progress(agent, _preflight_tokens)
  369. out.blocked = True
  370. break # Cannot compress further: neither rows nor tokens moved
  371. out.conversation_history = conversation_history_after_compression(
  372. agent, out.messages, out.conversation_history
  373. )
  374. _reset_retry_state_after_compaction(agent)
  375. if not _compressor.should_compress(_preflight_tokens):
  376. break
  377. if not _tc._compression_warrants_another_preflight_pass(
  378. _orig_tokens, _preflight_tokens, _compressor.threshold_tokens
  379. ):
  380. out.blocked = True
  381. logger.warning(
  382. "Preflight compression made insufficient progress: "
  383. "~%s -> ~%s request tokens; skipping additional passes",
  384. f"{_orig_tokens:,}", f"{_preflight_tokens:,}",
  385. )
  386. # Sub-5% progress on a request still above the window: no further pass will get under it.
  387. _tc._fail_closed_on_insufficient_progress(agent, _preflight_tokens)
  388. break
  389. def _engine_preflight_maintenance(
  390. agent: Any, out: CompactionOutcome, _compressor: Any, _preflight_tokens: int,
  391. system_message: Optional[str], effective_task_id: str,
  392. ) -> None:
  393. """Engine-driven sub-threshold preflight maintenance: engines overriding
  394. ``should_compress_preflight()`` get exactly ONE ``compress()`` pass; a no-op never
  395. touches ``blocked``."""
  396. _engine_preflight = getattr(_compressor, "should_compress_preflight", None)
  397. if not callable(_engine_preflight):
  398. return
  399. try:
  400. _wants_engine_preflight = bool(_engine_preflight(out.messages))
  401. except Exception as _preflight_exc:
  402. # A buggy engine must never break an otherwise-healthy turn.
  403. logger.debug(
  404. "should_compress_preflight raised %s; skipping "
  405. "engine-driven preflight maintenance",
  406. _preflight_exc,
  407. )
  408. return
  409. if not _wants_engine_preflight:
  410. return
  411. logger.info(
  412. "Engine-driven preflight maintenance: %s requested "
  413. "compress() at ~%s tokens (below %s threshold)",
  414. getattr(_compressor, "name", type(_compressor).__name__),
  415. f"{_preflight_tokens:,}", f"{getattr(_compressor, 'threshold_tokens', 0):,}",
  416. )
  417. _engine_input = out.messages
  418. out.messages, out.active_system_prompt = agent._compress_context(
  419. _engine_input, system_message, approx_tokens=_preflight_tokens, task_id=effective_task_id,
  420. trigger="engine_preflight",
  421. )
  422. # ``_compress_context`` returns the INPUT list on every skip path and an engine
  423. # may no-op; re-baseline/re-anchor only after a REAL compaction.
  424. if out.messages is not _engine_input:
  425. out.compressed = True
  426. out.conversation_history = conversation_history_after_compression(
  427. agent, out.messages
  428. )
  429. _reset_retry_state_after_compaction(agent)
  430. def _rearm_uncompressed_overflow_warn(
  431. agent: Any, messages: list[Any], active_system_prompt: Optional[str]
  432. ) -> None:
  433. """Uncompressed session guard: the warning fires from the loop's pre-API site;
  434. here we only RE-ARM the dedup once back under the window."""
  435. from agent import turn_context as _tc
  436. _ctx_len = getattr(getattr(agent, "context_compressor", None), "context_length", None)
  437. if not (isinstance(_ctx_len, int) and _ctx_len > 0):
  438. return
  439. _raw_chars = 0
  440. for _m in messages:
  441. if not isinstance(_m, dict):
  442. continue
  443. _c = _m.get("content")
  444. if isinstance(_c, str):
  445. _raw_chars += len(_c)
  446. elif _c:
  447. # Non-string, non-empty (multimodal) content defeats a char count — force
  448. # the real estimate. None/"" contribute nothing.
  449. _raw_chars = _ctx_len + 1
  450. break
  451. # Cheap gate: raw text under ~1/4 of the window (4 chars/token) cannot be over it.
  452. if _raw_chars <= _ctx_len:
  453. _clear_overflow_warn(agent)
  454. return
  455. # Re-arm with the same route-aware (checkpoint-pruned wire) figure the warn site
  456. # measures, else a compacted session never clears the dedup and genuine overflow
  457. # warnings stay suppressed.
  458. _uncompressed_tokens = _tc._preflight_request_tokens(
  459. agent, messages, active_system_prompt or ""
  460. )
  461. if _uncompressed_tokens <= _ctx_len:
  462. _clear_overflow_warn(agent)