The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(agent): move the compression banner and Codex autoraise notice into a sibling
kshitijk4poorfb4cc3bd683e
diff --git a/agent/agent_init.py b/agent/agent_init.pyindex 04c8a0abe3e..d7dfcba708d 100644--- a/agent/agent_init.py+++ b/agent/agent_init.py@@ -34,6 +34,7 @@ from agent.model_metadata import (MINIMUM_CONTEXT_LENGTH, fetch_model_metadata, is_local_endpoint)from agent.agent_init_ollama import _clamp_compressor_to_ollama_num_ctx, _configure_ollama_num_ctx+from agent.agent_init_compression_notice import _emit_compression_summaryfrom agent.primary_runtime_snapshot import _snapshot_primary_runtimefrom agent.process_bootstrap import _install_safe_stdiofrom agent.subdirectory_hints import SubdirectoryHintTracker@@ -138,31 +139,6 @@ def _custom_provider_runtime_ids(value: Any) -> set[str]:return {normalized, f"custom:{normalized}"}-def _build_codex_gpt5_autoraise_notice(- autoraise: dict[str, Any], context_length: Optional[int] = None-) -> str:- """One-time notice when Codex gpt-5.x raises compaction (``autoraise``: model/from/to).-- ``context_length`` is the live-resolved window (Codex's catalog shifts server-side) so the- banner reports what this session got. Printed for CLI and replayed via status_callback for- gateway users, so it must be self-contained and include the exact opt-back-out command.- """- model = str(autoraise.get("model") or "gpt-5.4/5.5").strip().lower().rsplit("/", 1)[-1]- if isinstance(context_length, int) and context_length > 0:- cap = f"{round(context_length / 1000)}K"- else:- # Static fallback: codex-spark is natively 128K; gpt-5.4/5.5/5.6 are capped at 272K.- cap = "128K" if model.startswith("gpt-5.3-codex-spark") else "272K"- from_pct = round(autoraise["from"] * 100)- to_pct = round(autoraise["to"] * 100)- return (- f"ℹ Codex {model} caps context at {cap}, so auto-compaction was raised "- f"to {to_pct}% (from {from_pct}%) to use more of the window before "- f"summarizing.\n"- f" Opt back out: hermes config set compression.codex_gpt55_autoraise false"- )--def _resolve_compression_threshold(global_threshold: float, model_cthresh: Optional[float], *, model: Optional[str] = None,is_codex_autoraise: bool,@@ -182,42 +158,6 @@ def _resolve_compression_threshold(return model_cthresh, None-def _codex_gpt55_autoraise_notice_marker():- """Per-profile marker path (``$HERMES_HOME`` is profile-scoped; not a config key)."""- return get_hermes_home() / ".codex_gpt55_autoraise_notice"---def _codex_gpt55_autoraise_notice_state(autoraise: dict[str, Any]) -> str:- """Notice identity keyed on what it displays (model + from→to percentages).-- An unchanged threshold stays silent across restarts; a changed global threshold or a- different autoraised Codex model re-notifies once.- """- model = str(autoraise.get("model") or "").strip().lower().rsplit("/", 1)[-1]- from_pct = round(float(autoraise["from"]) * 100)- to_pct = round(float(autoraise["to"]) * 100)- return f"{model}:{from_pct}:{to_pct}"---def _codex_gpt55_autoraise_notice_seen(autoraise: dict[str, Any]) -> bool:- """True if this exact notice was already shown for this profile (unreadable = unseen)."""- try:- current = _codex_gpt55_autoraise_notice_state(autoraise)- return _codex_gpt55_autoraise_notice_marker().read_text(- encoding="utf-8"- ).strip() == current- except (OSError, KeyError, TypeError, ValueError):- return False---def _record_codex_gpt55_autoraise_notice(autoraise: dict[str, Any]) -> None:- """Persist that the notice was shown. Best-effort: a failure only re-shows it later."""- with suppress(OSError, KeyError, TypeError, ValueError):- marker = _codex_gpt55_autoraise_notice_marker()- marker.parent.mkdir(parents=True, exist_ok=True)- marker.write_text(_codex_gpt55_autoraise_notice_state(autoraise), encoding="utf-8")--def _normalized_custom_base_url(value: Any) -> str:if not isinstance(value, str):return ""@@ -2125,50 +2065,6 @@ def _inject_context_engine_tools(agent):_ra().logger.debug("Context engine on_session_start: %s", _ce_err)-def _emit_compression_summary(agent, cs):- # Codex autoraise notice: once per profile/config state (persisted marker; the gateway- # rebuilds the agent per message). The display gate hides the banner, not the autoraise.- _autoraise = agent._compression_threshold_autoraised or {}- _autoraise_notice = None- if (- # A change in the raised threshold (or the autoraised model) updates the marker state and- # re-notifies once. The config display gate (compression.codex_gpt55_autoraise_notice) still- # suppresses the banner entirely without disabling the threshold autoraise. See #54432.- bool(_autoraise)- and cs.enabled- and cs.autoraise_notice_enabled- and not _codex_gpt55_autoraise_notice_seen(_autoraise)- ):- _autoraise_notice = _build_codex_gpt5_autoraise_notice(- _autoraise, context_length=getattr(agent.context_compressor, "context_length", None)- )-- if not agent.quiet_mode:- _cc = agent.context_compressor- if cs.enabled:- # The active engine's own threshold — a plugin's differs from cs.threshold.- _pct = getattr(_cc, "threshold_percent", cs.threshold)- _cap = getattr(_cc, "threshold_tokens_cap", None)- # Name the cap only when it is what set the trigger; on small windows the ratio already sits below it.- _eff_cap = getattr(_cc, "_effective_threshold_cap", lambda _ctx: None)(_cc.context_length)- _cap_binds = _eff_cap is not None and _cc.threshold_tokens == _eff_cap- _cap_note = f" (capped at {_cap:,} tokens)" if _cap_binds else ""- print(f"📊 Context limit: {_cc.context_length:,} tokens (compress at {int(_pct*100)}% = {_cc.threshold_tokens:,}{_cap_note})")- else:- print(f"📊 Context limit: {_cc.context_length:,} tokens (auto-compression disabled)")- # Gateway users get the same text via _compression_warning on turn 1.- if _autoraise_notice:- agent._safe_print(_autoraise_notice, diagnostic=True)-- # status_callback isn't wired yet: stash for replay on the first turn; mark shown so- # repeated inits stay silent.- agent._compression_warning = _autoraise_notice- if _autoraise_notice:- _record_codex_gpt55_autoraise_notice(_autoraise)- # Feasibility check deferred to the first turn near threshold (eager costs ~400ms cold).- agent._compression_feasibility_checked = False--def _init_usage_state(agent):from agent.runtime_cwd import scope_terminal_cwd# Prefer the session's explicitly adopted workspace (a Desktop session created under thediff --git a/agent/agent_init_compression_notice.py b/agent/agent_init_compression_notice.pynew file mode 100644index 00000000000..6f4d23f7dc9--- /dev/null+++ b/agent/agent_init_compression_notice.py@@ -0,0 +1,113 @@+"""Init-time compression banner and the once-per-profile Codex gpt-5.x autoraise notice."""++from __future__ import annotations++from contextlib import suppress+from typing import Any, Optional++from hermes_constants import get_hermes_home+++def _build_codex_gpt5_autoraise_notice(+ autoraise: dict[str, Any], context_length: Optional[int] = None+) -> str:+ """One-time notice when Codex gpt-5.x raises compaction (``autoraise``: model/from/to).++ ``context_length`` is the live-resolved window (Codex's catalog shifts server-side) so the+ banner reports what this session got. Printed for CLI and replayed via status_callback for+ gateway users, so it must be self-contained and include the exact opt-back-out command.+ """+ model = str(autoraise.get("model") or "gpt-5.4/5.5").strip().lower().rsplit("/", 1)[-1]+ if isinstance(context_length, int) and context_length > 0:+ cap = f"{round(context_length / 1000)}K"+ else:+ # Static fallback: codex-spark is natively 128K; gpt-5.4/5.5/5.6 are capped at 272K.+ cap = "128K" if model.startswith("gpt-5.3-codex-spark") else "272K"+ from_pct = round(autoraise["from"] * 100)+ to_pct = round(autoraise["to"] * 100)+ return (+ f"ℹ Codex {model} caps context at {cap}, so auto-compaction was raised "+ f"to {to_pct}% (from {from_pct}%) to use more of the window before "+ f"summarizing.\n"+ f" Opt back out: hermes config set compression.codex_gpt55_autoraise false"+ )+++def _codex_gpt55_autoraise_notice_marker():+ """Per-profile marker path (``$HERMES_HOME`` is profile-scoped; not a config key)."""+ return get_hermes_home() / ".codex_gpt55_autoraise_notice"+++def _codex_gpt55_autoraise_notice_state(autoraise: dict[str, Any]) -> str:+ """Notice identity keyed on what it displays (model + from→to percentages).++ An unchanged threshold stays silent across restarts; a changed global threshold or a+ different autoraised Codex model re-notifies once.+ """+ model = str(autoraise.get("model") or "").strip().lower().rsplit("/", 1)[-1]+ from_pct = round(float(autoraise["from"]) * 100)+ to_pct = round(float(autoraise["to"]) * 100)+ return f"{model}:{from_pct}:{to_pct}"+++def _codex_gpt55_autoraise_notice_seen(autoraise: dict[str, Any]) -> bool:+ """True if this exact notice was already shown for this profile (unreadable = unseen)."""+ try:+ current = _codex_gpt55_autoraise_notice_state(autoraise)+ return _codex_gpt55_autoraise_notice_marker().read_text(+ encoding="utf-8"+ ).strip() == current+ except (OSError, KeyError, TypeError, ValueError):+ return False+++def _record_codex_gpt55_autoraise_notice(autoraise: dict[str, Any]) -> None:+ """Persist that the notice was shown. Best-effort: a failure only re-shows it later."""+ with suppress(OSError, KeyError, TypeError, ValueError):+ marker = _codex_gpt55_autoraise_notice_marker()+ marker.parent.mkdir(parents=True, exist_ok=True)+ marker.write_text(_codex_gpt55_autoraise_notice_state(autoraise), encoding="utf-8")+++def _emit_compression_summary(agent, cs):+ # Codex autoraise notice: once per profile/config state (persisted marker; the gateway+ # rebuilds the agent per message). The display gate hides the banner, not the autoraise.+ _autoraise = agent._compression_threshold_autoraised or {}+ _autoraise_notice = None+ if (+ # A change in the raised threshold (or the autoraised model) updates the marker state and+ # re-notifies once. The config display gate (compression.codex_gpt55_autoraise_notice) still+ # suppresses the banner entirely without disabling the threshold autoraise. See #54432.+ bool(_autoraise)+ and cs.enabled+ and cs.autoraise_notice_enabled+ and not _codex_gpt55_autoraise_notice_seen(_autoraise)+ ):+ _autoraise_notice = _build_codex_gpt5_autoraise_notice(+ _autoraise, context_length=getattr(agent.context_compressor, "context_length", None)+ )++ if not agent.quiet_mode:+ _cc = agent.context_compressor+ if cs.enabled:+ # The active engine's own threshold — a plugin's differs from cs.threshold.+ _pct = getattr(_cc, "threshold_percent", cs.threshold)+ _cap = getattr(_cc, "threshold_tokens_cap", None)+ # Name the cap only when it is what set the trigger; on small windows the ratio already sits below it.+ _eff_cap = getattr(_cc, "_effective_threshold_cap", lambda _ctx: None)(_cc.context_length)+ _cap_binds = _eff_cap is not None and _cc.threshold_tokens == _eff_cap+ _cap_note = f" (capped at {_cap:,} tokens)" if _cap_binds else ""+ print(f"📊 Context limit: {_cc.context_length:,} tokens (compress at {int(_pct*100)}% = {_cc.threshold_tokens:,}{_cap_note})")+ else:+ print(f"📊 Context limit: {_cc.context_length:,} tokens (auto-compression disabled)")+ # Gateway users get the same text via _compression_warning on turn 1.+ if _autoraise_notice:+ agent._safe_print(_autoraise_notice, diagnostic=True)++ # status_callback isn't wired yet: stash for replay on the first turn; mark shown so+ # repeated inits stay silent.+ agent._compression_warning = _autoraise_notice+ if _autoraise_notice:+ _record_codex_gpt55_autoraise_notice(_autoraise)+ # Feasibility check deferred to the first turn near threshold (eager costs ~400ms cold).+ agent._compression_feasibility_checked = Falsediff --git a/tests/agent/test_codex_gpt55_autoraise_notice.py b/tests/agent/test_codex_gpt55_autoraise_notice.pyindex 2dd0f71b4e3..7424dcf4fe5 100644--- a/tests/agent/test_codex_gpt55_autoraise_notice.py+++ b/tests/agent/test_codex_gpt55_autoraise_notice.py@@ -24,7 +24,7 @@ from hermes_constants import get_hermes_homefrom hermes_state import SessionDBfrom run_agent import AIAgent-from agent.agent_init import (+from agent.agent_init_compression_notice import (_codex_gpt55_autoraise_notice_marker,_codex_gpt55_autoraise_notice_seen,_record_codex_gpt55_autoraise_notice,