9 Commits

Author SHA1 Message Date
kshitijk4poor
ed9b42ff42 fix(prompt-cache): zero-priced cache writes buy no markers; learned rules survive one noisy chat
Follow-ups found reviewing #156455, #156250 and #157377 after they merged.

- catalog_cache_markers counted "0.0000000000" (2 live Nous models), 0 and 0.0 as a priced
  cache write, so those routes were sent markers. Only a positive finite rate counts now.
- The learned-route store kept the last 8 miss samples, so one chat missing 8 more times
  pushed out the second chat's sample and a two-session rule vanished with no new evidence.
  The trim now drops the oldest sample of a session that has another.
- An unmarked keep-alive decision parsed prompt_cache_routes.json three times; warming_rule
  reads it once (marker_preference too).
- The sandbox cwd fallback stripped the recovered path, so a directory ending in a space
  was recorded without it and the next command fell back again.
- Skill docs said to "read the newest hits" of search_files, which returns the first 50;
  they now say to page with offset. The ast-grep pitfall row's escaped pipe is explained.
2026-11-21 15:42:00 -08:00
Teknium
cd7bbff04c fix(prompt-cache): keep-alives skip Claude models whose effort becomes a thinking budget
is_replayable only read extra_body.reasoning; the custom/OpenAI-compatible
profiles (Nous Portal via provider: custom) send top-level reasoning_effort, and
request_overrides can put it in extra_body. One helper now reads every shape, so
claude-haiku-4.5 / sonnet-4.5 with an effort are no longer replayed with a 1-token
cap that keys a different cache entry. Adaptive Claude and effort none stay warmed.
2026-11-19 10:35:54 -08:00
Teknium
ab5b40cabe fix(prompt-cache): a keep-alive is priced at the service tier the response was served at
PromptCacheWarmer._record normalized only message.usage; OpenAI reports the
served service_tier on the response, so an Ultrafast replay was booked at
Standard prices. Fold it in with with_served_service_tier, as
record_response_usage does.
2026-11-18 03:40:34 -08:00
Teknium
06f48b59e6 fix(caching): keep-alive accounting writes through the agent's storage handle
Main's persistence interface makes the agent's store a storage handle with no
queue_token_counts; the warmer's keep-alive accounting called it on
agent._session_db directly and silently lost every refresh's usage. It now goes
through turn_usage.queue_usage, which takes the handle's usage repository (and
the worker store otherwise) with the session id and surface the keep-alive
captured when it was scheduled.
2026-11-18 03:40:34 -08:00
Teknium
df2dea71fa fix(cache): a retried keep-alive miss keeps the lifetime learned at that miss
Greptile on 37f847a1: the retry deadline was rebuilt from run.ttl_s, the
tier the clock assumed. When this miss supplies the second session's
sample and learned_window returns a shorter learned lifetime (say 10 min
under a 1h tier), the retry still waited on the tier, so the next
keep-alive landed after the entry was gone, paid a second rewrite and
disabled warming. The retry now warms on the learned lifetime.
2026-11-18 03:40:34 -08:00
Teknium
fc085c44a3 fix(cache): a retried keep-alive miss waits a full window; report summarized results left without an archive
Greptile on 2a51baab:
- An automatic-cache miss reports no write, so the clock did not move and the retry was scheduled off the
  old deadline (about 8 min after the miss on a 30m window, at once near its end). The retry now waits a
  full window from the miss it follows.
- A summarized tool result whose archive write fails (disk full, read-only home) is now logged at WARNING;
  the compaction still commits (refusing would re-pay the summary every turn while the disk stays full).
  Docs say where the bytes remain (summary + state.db).
- The claim-race barrier waits up to 5 s (tests/AGENTS.md wall-clock floor).
2026-11-18 03:40:34 -08:00
Teknium
0cf82ca702 fix(cache): review round 2 — managed local unload expiry, malformed route store, CLI dead hook, scan/release handoff
alt-glitch's review of 893b54f9:
- Hermes' own managed llama-server unloads an idle model (and its prompt cache) after 15 min (+ two
  120 s sweeps): its clock now ends there, so /context and the cold offload see it; a user's own local
  server still never expires on the clock. The guarantee is capped at the estimated end.
- A malformed entry in cache/prompt_cache_routes.json (non-numeric timestamp, counters or gap lists) is
  dropped instead of raising into agent build and usage accounting.
- The route key keeps the port (normalized base URL): two local servers on one host learn apart.
- The classic CLI's idle cache hook never ran: `hermes chat --cli` drives a GatewayAgent and the gateway
  owns the warmer and its housekeeping. The hook, its test and its helper are removed (the two CLI
  files are main's again).
- A due-scan's claimed action is handed to a release job that registered for the same chat while the
  scan ran, instead of being dropped.
- Keep-alive log lines carry the chat's session tag (set on the timer thread).
- Tests that could not fail now can: the claim race pauses after the decision, the late-miss branch with
  no successor, and the compaction hand-off through carry_compression_automation (each red when its fix
  is reverted).
- Follow-ups folded in: a finite admission arms no idle keep-alive; delegate cost folding takes the
  session-totals lock; the docs say the gateway offload lands on its 5-minute tick.
2026-11-18 03:40:34 -08:00
Teknium
df702cab6b feat(cache): backtest rulings — upstream-aware misses, 5m->1h tier at the first pause, learning-store guards
Teknium approved every improvement the usage backtest found:

- A keep-alive miss served by another upstream than the reply that cached the prefix (Nous serves one
  Claude model from chatcmpl-* and gen-* upstreams; 70 switches broke a warm cache in the window), or
  one that still read the stable system prefix, is routing, not lifetime. Real misses end warming only
  on the second in a row (three misses of any kind end it too). The upstream key is data
  (prompt_cache_clock.response_upstream: a gateway's upstream field, else the response id family),
  stored on the clock event; a retried miss's rewrite is not charged to the money limit.
- cache_ttl auto: a person's chat starts on 5m and asks for 1h from the first message after an idle gap
  past five minutes, for the rest of the session (kept per session id, carried to the compaction child,
  re-derived from a stored clock that asked for 1h). The upgrade fires only when the clock holds no
  guarantee and the last cache touch is over 300 s old, so markers never change on a warm prefix. No
  idle keep-alives before the upgrade. /context names the pending tier. Explicit pins and machine
  sessions unchanged. Wire snapshot regenerated: fresh cli agents now send 5m markers.
- Route store: a downgrade needs samples from two different sessions, an upstream switch is never a
  sample, and a learned lifetime never undercuts the longest hit ever seen (kept outside the trimmed
  recent gaps).

Tests pinning the single-miss stop and the 1h start are rewritten to the new ruling (assertions only).
2026-11-18 03:40:34 -08:00
Teknium
58d6946da5 refactor(caching): prompt_cache_lifecycle becomes a package
agent/prompt_cache_lifecycle.py (1,200 lines) splits along its topics, per root AGENTS.md
§ Packages and Python shape: replay.py (which requests replay, with which output cap, through
which client), economics.py (a keep-alive's cost against the rewrite it prevents), edge.py (the
cache-edge gates claim_edge asks), warmer.py (PromptCacheWarmer and the agent-facing seams).
__init__ is the facade (__all__, no behaviour); topic modules import one another, never the
facade. Callers keep importing the public names from agent.prompt_cache_lifecycle; tests mirror
the folder under tests/agent/prompt_cache_lifecycle/ and patch where each name is looked up.

Co-authored-by: kshitijk4poor <82637225+kshitijk4poor@users.noreply.github.com>
2026-11-18 03:40:34 -08:00