mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-16 19:04:08 +03:00
58886fc81e
The clock modelled every route as "expires = last touch + tier", which is wrong for OpenAI GPT-5.6+: a read refreshes coverage to at least 30 minutes after it and never restarts a 12h write. It also treated documented minimums as expiries and lost or revived saved state. - Coverage deadlines per tier with per-route refresh rules: Anthropic family restarts the tier on read; OpenAI keeps max(existing, read + 30m) and a 12h deadline stays at write + 12h. - Windows carry guaranteed_until (earliest deadline, so mixed tiers in one response are only as warm as the shorter) and estimated_until (OpenAI: last touch + 24h retention ceiling; pre-5.6 in_memory 1h). cache_certainly_warm / cache_certainly_cold are the two consumer helpers. /context: warm until / may still be warm until / expired / unknown. - Events bind to the session's durable context generation (rows a compaction or prune archived, SessionDB.prompt_cache_generation), kept as saved on restore; a resumed agent restores and validates the saved event before folding its first response. - Route identity is provider + normalize_route_base_url (scheme, host, port, path) + model. - Agentless session.context_breakdown reads the saved clock (stored_cache_window). - Codex app-server activity is recorded as an explicit unknown (it sets its own cache options). - MoA records the resolved aggregator route and the payload the MoA client sent. - effective_cache_ttl applies the route ceiling on the measured-provider path too.
42 lines
2.3 KiB
Python
42 lines
2.3 KiB
Python
"""Codex app-server usage keeps the cache-write bucket the protocol reports.
|
|
|
|
codex-rs ``TokenUsageBreakdown`` carries ``cacheWriteInputTokens`` (from the Responses
|
|
``input_tokens_details.cache_write_tokens``); like ``cachedInputTokens`` it is inside
|
|
``inputTokens``. GPT-5.6+ bills those writes at 1.25x, so dropping them under-reports cost.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import patch
|
|
|
|
import run_agent
|
|
from agent.transports.codex_app_server_session import CodexAppServerSession, TurnResult
|
|
|
|
|
|
def test_cache_write_tokens_are_read_and_not_double_counted(monkeypatch):
|
|
def fake_run_turn(self, user_input: str, **kwargs):
|
|
return TurnResult(
|
|
final_text="done", projected_messages=[{"role": "assistant", "content": "done"}],
|
|
turn_id="turn-w-1", thread_id="thread-w-1",
|
|
token_usage_last={"totalTokens": 130, "inputTokens": 100, "cachedInputTokens": 20,
|
|
"cacheWriteInputTokens": 50, "outputTokens": 30, "reasoningOutputTokens": 0},
|
|
model_context_window=200000,
|
|
)
|
|
|
|
monkeypatch.setattr(CodexAppServerSession, "run_turn", fake_run_turn)
|
|
monkeypatch.setattr(CodexAppServerSession, "ensure_started", lambda self: "thread-w-1")
|
|
agent = run_agent.AIAgent(api_key="stub", base_url="https://stub.invalid", provider="openai",
|
|
api_mode="codex_app_server", quiet_mode=True, skip_context_files=True, skip_memory=True)
|
|
with patch.object(agent, "_spawn_background_review", return_value=None):
|
|
result = agent.run_conversation("hello")
|
|
|
|
assert (result["input_tokens"], result["cache_read_tokens"], result["cache_write_tokens"]) == (30, 20, 50)
|
|
assert result["prompt_tokens"] == 100 # uncached + read + write == inputTokens, never more
|
|
assert agent.session_cache_write_tokens == 50
|
|
# The app-server sets its own cache options: the clock records the activity as an explicit
|
|
# unknown, so no older window outlives it and /context names the reason.
|
|
from agent.prompt_cache_clock import cache_window, render_prompt_cache_line
|
|
window = cache_window(agent)
|
|
assert (window.confidence, window.reason) == ("unknown", "codex_app_server")
|
|
assert "runtime sets its own cache options" in render_prompt_cache_line(window.__dict__)
|