Repository navigation
The prompt cache stays warm through long tool runs (always on; Nous Portal, OpenRouter, Anthropic) - #122094
The prompt cache stays warm through long tool runs (always on; Nous Portal, OpenRouter, Anthropic)#122094teknium1 wants to merge 11 commits into
Conversation
૮ >ﻌ< ა ci reviewran on 613cfd7 — fix(agent): keep-alives fire the auxiliary call events, neve ℹ️ InfoCI caches · View report · View job27 / 27 cache hits (0 misses) per-job cache resultshits
missesnone Compressed archive bytes. Not-restored bytes use the main-size reference; missing sizes remain unknown. debug infoCI timingsCI timings · View report · View jobWall time 10m26s vs 9m13s (+13.2%). 4 job(s) slower, 8 faster, 1 unchanged.
|
SummarySolid feature port — Anthropic prompt-cache warming with sensible economics gating. Positives
Suggestions
Questions
|
Scope of the
|
|
@teknium1 I'd like to help move this forward rather than start a competing implementation. We have a native-Anthropic provider result that may help close the vendor-validation gap described above. In an isolated synthetic-prefix test on Opus 5.5 with medium effort and a one-hour cache TTL:
This validates zero-output retention beyond the original hour on the tested native Anthropic route. It does not validate this PR's implementation, other providers, repeated overnight refreshes, or subscription-quota savings. No real conversation was replayed. Our follow-up use case is opt-in, bounded idle warming for selected/pinned conversations across overnight breaks, with refresh usage accounted separately and a time/budget limit measured from real activity. I see this PR currently caps idle warming at 30 minutes and excludes subscription-included routes; those would need separate agreement and evidence, not a silent relaxation. Would you prefer help updating and landing this PR first, with a linked issue/follow-up for longer idle retention, or a separate proposal before implementation? Happy to share the minimal synthetic repro and sanitized usage results. |
3021afd to
55ec460
Compare
|
Independent review of P1: a streaming-mode timer outlives turns that end by early return. P2: in idle mode, duplicate warmers survive on forks and evicted agents. P2: OpenRouter's response cache makes the 2nd and later refreshes no-ops. The replay goes through the shared P2: P2: the warmer passes
Fix: use P3: race between the timer thread and the loop. In P3: the replay doesn't use the client the real request used. The real chat request uses
The MoA guard is also keyed on P3: P3: non-Claude routes are warmed too. Markers on Qwen (Portal, DashScope, OpenCode) and Kimi (OpenRouter, Portal) pass Verdict: request changes. Fix the P1 (settle on every turn exit) plus the OpenRouter response-cache and fork/eviction P2s before merge. Accounting and thread-safety are minor. |
…, and bypasses the OpenRouter response cache Review findings on the widened warmer (#122094): - Settle the warmer in run_conversation's finally (every turn exit), not only in finalize_turn: early returns (tool-validation exits, repetition stop, truncation) left a streaming run replaying every 270 s for up to an hour. - Cancel it in release_clients (gateway eviction, review forks) and force it off on cache-parity forks, which share the parent's prefix. - Send X-OpenRouter-Cache: false on the replay: a byte-identical request inside OpenRouter's 300 s response-cache TTL is answered from that cache and never refreshes the prompt cache upstream. - Skip a Chat Completions replay when reasoning is on for a Claude model without adaptive thinking (the gateway turns it into a budget a one-token replay changes), and warm only Claude on that wire; refuse MoA by provider too. - Cap the token key the request actually carried; the timer thread never stops a newer run the loop armed; accounting rows keep the session's real source and billing mode and are tagged task=cache_warm.
|
Review findings addressed in
Tests: warmer, agent-init, finalizer, background-review and |
[Medium impact] The PR appears safe to merge; all four numbered findings are addressed, and no new actionable issue was established.
|
…ownership checks are atomic Greptile review on #122094: - The first refresh was scheduled delay_s after the RESPONSE finished, so a response that took over 30 s on a 5-minute entry sent its keep-alive after expiry (a paid write, not a warm). It now counts from the request's send time (api_start_time), each later refresh from its own send time, and an entry already past its lifetime arms nothing. - _validate / _stop_if_current / start check and replace _run under one RLock, so an old timer callback can never cancel a run the loop armed meanwhile. - New invariant test reads cache_warming from a real config.yaml in HERMES_HOME and pins the send-time anchor.
…, and bypasses the OpenRouter response cache Review findings on the widened warmer (#122094): - Settle the warmer in run_conversation's finally (every turn exit), not only in finalize_turn: early returns (tool-validation exits, repetition stop, truncation) left a streaming run replaying every 270 s for up to an hour. - Cancel it in release_clients (gateway eviction, review forks) and force it off on cache-parity forks, which share the parent's prefix. - Send X-OpenRouter-Cache: false on the replay: a byte-identical request inside OpenRouter's 300 s response-cache TTL is answered from that cache and never refreshes the prompt cache upstream. - Skip a Chat Completions replay when reasoning is on for a Claude model without adaptive thinking (the gateway turns it into a budget a one-token replay changes), and warm only Claude on that wire; refuse MoA by provider too. - Cap the token key the request actually carried; the timer thread never stops a newer run the loop armed; accounting rows keep the session's real source and billing mode and are tagged task=cache_warm.
…ownership checks are atomic Greptile review on #122094: - The first refresh was scheduled delay_s after the RESPONSE finished, so a response that took over 30 s on a 5-minute entry sent its keep-alive after expiry (a paid write, not a warm). It now counts from the request's send time (api_start_time), each later refresh from its own send time, and an entry already past its lifetime arms nothing. - _validate / _stop_if_current / start check and replace _run under one RLock, so an old timer callback can never cancel a run the loop armed meanwhile. - New invariant test reads cache_warming from a real config.yaml in HERMES_HOME and pins the send-time anchor.
b3b8ffc to
8eab506
Compare
…, and bypasses the OpenRouter response cache Review findings on the widened warmer (#122094): - Settle the warmer in run_conversation's finally (every turn exit), not only in finalize_turn: early returns (tool-validation exits, repetition stop, truncation) left a streaming run replaying every 270 s for up to an hour. - Cancel it in release_clients (gateway eviction, review forks) and force it off on cache-parity forks, which share the parent's prefix. - Send X-OpenRouter-Cache: false on the replay: a byte-identical request inside OpenRouter's 300 s response-cache TTL is answered from that cache and never refreshes the prompt cache upstream. - Skip a Chat Completions replay when reasoning is on for a Claude model without adaptive thinking (the gateway turns it into a budget a one-token replay changes), and warm only Claude on that wire; refuse MoA by provider too. - Cap the token key the request actually carried; the timer thread never stops a newer run the loop armed; accounting rows keep the session's real source and billing mode and are tagged task=cache_warm.
…ownership checks are atomic Greptile review on #122094: - The first refresh was scheduled delay_s after the RESPONSE finished, so a response that took over 30 s on a 5-minute entry sent its keep-alive after expiry (a paid write, not a warm). It now counts from the request's send time (api_start_time), each later refresh from its own send time, and an entry already past its lifetime arms nothing. - _validate / _stop_if_current / start check and replace _run under one RLock, so an old timer callback can never cancel a run the loop armed meanwhile. - New invariant test reads cache_warming from a real config.yaml in HERMES_HOME and pins the send-time anchor.
…ownership checks are atomic Greptile review on #122094: - The first refresh was scheduled delay_s after the RESPONSE finished, so a response that took over 30 s on a 5-minute entry sent its keep-alive after expiry (a paid write, not a warm). It now counts from the request's send time (api_start_time), each later refresh from its own send time, and an entry already past its lifetime arms nothing. - _validate / _stop_if_current / start check and replace _run under one RLock, so an old timer callback can never cancel a run the loop armed meanwhile. - New invariant test reads cache_warming from a real config.yaml in HERMES_HOME and pins the send-time anchor.
…t, warm x3, expire agent/prompt_cache_lifecycle.py owns the per-chat decision at each pre-expiry point, replacing agent/prompt_cache_warmer.py's scheduler and agent/cache_expiry_compaction.py's decision (both deleted). A real request opens a budget of 3 keep-alives; chats a person types into keep warming while idle, machine sessions (subagents, side agents, forks, machine-paced sources) only while a tool runs. A spent budget on a proven/declared window >= 30m with context >= half the compaction threshold and growth since the last compaction is claimed once by the surface owner (CLI idle tick, TUI ticker, gateway watcher); CLI and TUI then prime the compacted chat for one fresh budget. Any miss ends warming; an unknown clock arms nothing. Every #122094 guard (witness, single admission under lock, reset epoch, aux hooks, unknown-price refusal, spend cap, $0.05 floor) and every #156253 owner guard (claim_gate, profile scope, bounded jobs, detached chats) is kept. The one-hour age cap is replaced by the budget.
…, and bypasses the OpenRouter response cache Review findings on the widened warmer (#122094): - Settle the warmer in run_conversation's finally (every turn exit), not only in finalize_turn: early returns (tool-validation exits, repetition stop, truncation) left a streaming run replaying every 270 s for up to an hour. - Cancel it in release_clients (gateway eviction, review forks) and force it off on cache-parity forks, which share the parent's prefix. - Send X-OpenRouter-Cache: false on the replay: a byte-identical request inside OpenRouter's 300 s response-cache TTL is answered from that cache and never refreshes the prompt cache upstream. - Skip a Chat Completions replay when reasoning is on for a Claude model without adaptive thinking (the gateway turns it into a budget a one-token replay changes), and warm only Claude on that wire; refuse MoA by provider too. - Cap the token key the request actually carried; the timer thread never stops a newer run the loop armed; accounting rows keep the session's real source and billing mode and are tagged task=cache_warm.
…ownership checks are atomic Greptile review on #122094: - The first refresh was scheduled delay_s after the RESPONSE finished, so a response that took over 30 s on a 5-minute entry sent its keep-alive after expiry (a paid write, not a warm). It now counts from the request's send time (api_start_time), each later refresh from its own send time, and an entry already past its lifetime arms nothing. - _validate / _stop_if_current / start check and replace _run under one RLock, so an old timer callback can never cancel a run the loop armed meanwhile. - New invariant test reads cache_warming from a real config.yaml in HERMES_HOME and pins the send-time anchor.
…t, warm x3, expire agent/prompt_cache_lifecycle.py owns the per-chat decision at each pre-expiry point, replacing agent/prompt_cache_warmer.py's scheduler and agent/cache_expiry_compaction.py's decision (both deleted). A real request opens a budget of 3 keep-alives; chats a person types into keep warming while idle, machine sessions (subagents, side agents, forks, machine-paced sources) only while a tool runs. A spent budget on a proven/declared window >= 30m with context >= half the compaction threshold and growth since the last compaction is claimed once by the surface owner (CLI idle tick, TUI ticker, gateway watcher); CLI and TUI then prime the compacted chat for one fresh budget. Any miss ends warming; an unknown clock arms nothing. Every #122094 guard (witness, single admission under lock, reset epoch, aux hooks, unknown-price refusal, spend cap, $0.05 floor) and every #156253 owner guard (claim_gate, profile scope, bounded jobs, detached chats) is kept. The one-hour age cap is replaced by the budget.
…, and bypasses the OpenRouter response cache Review findings on the widened warmer (#122094): - Settle the warmer in run_conversation's finally (every turn exit), not only in finalize_turn: early returns (tool-validation exits, repetition stop, truncation) left a streaming run replaying every 270 s for up to an hour. - Cancel it in release_clients (gateway eviction, review forks) and force it off on cache-parity forks, which share the parent's prefix. - Send X-OpenRouter-Cache: false on the replay: a byte-identical request inside OpenRouter's 300 s response-cache TTL is answered from that cache and never refreshes the prompt cache upstream. - Skip a Chat Completions replay when reasoning is on for a Claude model without adaptive thinking (the gateway turns it into a budget a one-token replay changes), and warm only Claude on that wire; refuse MoA by provider too. - Cap the token key the request actually carried; the timer thread never stops a newer run the loop armed; accounting rows keep the session's real source and billing mode and are tagged task=cache_warm.
…ownership checks are atomic Greptile review on #122094: - The first refresh was scheduled delay_s after the RESPONSE finished, so a response that took over 30 s on a 5-minute entry sent its keep-alive after expiry (a paid write, not a warm). It now counts from the request's send time (api_start_time), each later refresh from its own send time, and an entry already past its lifetime arms nothing. - _validate / _stop_if_current / start check and replace _run under one RLock, so an old timer callback can never cancel a run the loop armed meanwhile. - New invariant test reads cache_warming from a real config.yaml in HERMES_HOME and pins the send-time anchor.
…t, warm x3, expire agent/prompt_cache_lifecycle.py owns the per-chat decision at each pre-expiry point, replacing agent/prompt_cache_warmer.py's scheduler and agent/cache_expiry_compaction.py's decision (both deleted). A real request opens a budget of 3 keep-alives; chats a person types into keep warming while idle, machine sessions (subagents, side agents, forks, machine-paced sources) only while a tool runs. A spent budget on a proven/declared window >= 30m with context >= half the compaction threshold and growth since the last compaction is claimed once by the surface owner (CLI idle tick, TUI ticker, gateway watcher); CLI and TUI then prime the compacted chat for one fresh budget. Any miss ends warming; an unknown clock arms nothing. Every #122094 guard (witness, single admission under lock, reset epoch, aux hooks, unknown-price refusal, spend cap, $0.05 floor) and every #156253 owner guard (claim_gate, profile scope, bounded jobs, detached chats) is kept. The one-hour age cap is replaced by the budget.
…, and bypasses the OpenRouter response cache Review findings on the widened warmer (#122094): - Settle the warmer in run_conversation's finally (every turn exit), not only in finalize_turn: early returns (tool-validation exits, repetition stop, truncation) left a streaming run replaying every 270 s for up to an hour. - Cancel it in release_clients (gateway eviction, review forks) and force it off on cache-parity forks, which share the parent's prefix. - Send X-OpenRouter-Cache: false on the replay: a byte-identical request inside OpenRouter's 300 s response-cache TTL is answered from that cache and never refreshes the prompt cache upstream. - Skip a Chat Completions replay when reasoning is on for a Claude model without adaptive thinking (the gateway turns it into a budget a one-token replay changes), and warm only Claude on that wire; refuse MoA by provider too. - Cap the token key the request actually carried; the timer thread never stops a newer run the loop armed; accounting rows keep the session's real source and billing mode and are tagged task=cache_warm.
…ownership checks are atomic Greptile review on #122094: - The first refresh was scheduled delay_s after the RESPONSE finished, so a response that took over 30 s on a 5-minute entry sent its keep-alive after expiry (a paid write, not a warm). It now counts from the request's send time (api_start_time), each later refresh from its own send time, and an entry already past its lifetime arms nothing. - _validate / _stop_if_current / start check and replace _run under one RLock, so an old timer callback can never cancel a run the loop armed meanwhile. - New invariant test reads cache_warming from a real config.yaml in HERMES_HOME and pins the send-time anchor.
…t, warm x3, expire agent/prompt_cache_lifecycle.py owns the per-chat decision at each pre-expiry point, replacing agent/prompt_cache_warmer.py's scheduler and agent/cache_expiry_compaction.py's decision (both deleted). A real request opens a budget of 3 keep-alives; chats a person types into keep warming while idle, machine sessions (subagents, side agents, forks, machine-paced sources) only while a tool runs. A spent budget on a proven/declared window >= 30m with context >= half the compaction threshold and growth since the last compaction is claimed once by the surface owner (CLI idle tick, TUI ticker, gateway watcher); CLI and TUI then prime the compacted chat for one fresh budget. Any miss ends warming; an unknown clock arms nothing. Every #122094 guard (witness, single admission under lock, reset epoch, aux hooks, unknown-price refusal, spend cap, $0.05 floor) and every #156253 owner guard (claim_gate, profile scope, bounded jobs, detached chats) is kept. The one-hour age cap is replaced by the budget.
…, and bypasses the OpenRouter response cache Review findings on the widened warmer (#122094): - Settle the warmer in run_conversation's finally (every turn exit), not only in finalize_turn: early returns (tool-validation exits, repetition stop, truncation) left a streaming run replaying every 270 s for up to an hour. - Cancel it in release_clients (gateway eviction, review forks) and force it off on cache-parity forks, which share the parent's prefix. - Send X-OpenRouter-Cache: false on the replay: a byte-identical request inside OpenRouter's 300 s response-cache TTL is answered from that cache and never refreshes the prompt cache upstream. - Skip a Chat Completions replay when reasoning is on for a Claude model without adaptive thinking (the gateway turns it into a budget a one-token replay changes), and warm only Claude on that wire; refuse MoA by provider too. - Cap the token key the request actually carried; the timer thread never stops a newer run the loop armed; accounting rows keep the session's real source and billing mode and are tagged task=cache_warm.
…ownership checks are atomic Greptile review on #122094: - The first refresh was scheduled delay_s after the RESPONSE finished, so a response that took over 30 s on a 5-minute entry sent its keep-alive after expiry (a paid write, not a warm). It now counts from the request's send time (api_start_time), each later refresh from its own send time, and an entry already past its lifetime arms nothing. - _validate / _stop_if_current / start check and replace _run under one RLock, so an old timer callback can never cancel a run the loop armed meanwhile. - New invariant test reads cache_warming from a real config.yaml in HERMES_HOME and pins the send-time anchor.
…t, warm x3, expire agent/prompt_cache_lifecycle.py owns the per-chat decision at each pre-expiry point, replacing agent/prompt_cache_warmer.py's scheduler and agent/cache_expiry_compaction.py's decision (both deleted). A real request opens a budget of 3 keep-alives; chats a person types into keep warming while idle, machine sessions (subagents, side agents, forks, machine-paced sources) only while a tool runs. A spent budget on a proven/declared window >= 30m with context >= half the compaction threshold and growth since the last compaction is claimed once by the surface owner (CLI idle tick, TUI ticker, gateway watcher); CLI and TUI then prime the compacted chat for one fresh budget. Any miss ends warming; an unknown clock arms nothing. Every #122094 guard (witness, single admission under lock, reset epoch, aux hooks, unknown-price refusal, spend cap, $0.05 floor) and every #156253 owner guard (claim_gate, profile scope, bounded jobs, detached chats) is kept. The one-hour age cap is replaced by the budget.
…, and bypasses the OpenRouter response cache Review findings on the widened warmer (#122094): - Settle the warmer in run_conversation's finally (every turn exit), not only in finalize_turn: early returns (tool-validation exits, repetition stop, truncation) left a streaming run replaying every 270 s for up to an hour. - Cancel it in release_clients (gateway eviction, review forks) and force it off on cache-parity forks, which share the parent's prefix. - Send X-OpenRouter-Cache: false on the replay: a byte-identical request inside OpenRouter's 300 s response-cache TTL is answered from that cache and never refreshes the prompt cache upstream. - Skip a Chat Completions replay when reasoning is on for a Claude model without adaptive thinking (the gateway turns it into a budget a one-token replay changes), and warm only Claude on that wire; refuse MoA by provider too. - Cap the token key the request actually carried; the timer thread never stops a newer run the loop armed; accounting rows keep the session's real source and billing mode and are tagged task=cache_warm.
…ownership checks are atomic Greptile review on #122094: - The first refresh was scheduled delay_s after the RESPONSE finished, so a response that took over 30 s on a 5-minute entry sent its keep-alive after expiry (a paid write, not a warm). It now counts from the request's send time (api_start_time), each later refresh from its own send time, and an entry already past its lifetime arms nothing. - _validate / _stop_if_current / start check and replace _run under one RLock, so an old timer callback can never cancel a run the loop armed meanwhile. - New invariant test reads cache_warming from a real config.yaml in HERMES_HOME and pins the send-time anchor.
…t, warm x3, expire agent/prompt_cache_lifecycle.py owns the per-chat decision at each pre-expiry point, replacing agent/prompt_cache_warmer.py's scheduler and agent/cache_expiry_compaction.py's decision (both deleted). A real request opens a budget of 3 keep-alives; chats a person types into keep warming while idle, machine sessions (subagents, side agents, forks, machine-paced sources) only while a tool runs. A spent budget on a proven/declared window >= 30m with context >= half the compaction threshold and growth since the last compaction is claimed once by the surface owner (CLI idle tick, TUI ticker, gateway watcher); CLI and TUI then prime the compacted chat for one fresh budget. Any miss ends warming; an unknown clock arms nothing. Every #122094 guard (witness, single admission under lock, reset epoch, aux hooks, unknown-price refusal, spend cap, $0.05 floor) and every #156253 owner guard (claim_gate, profile scope, bounded jobs, detached chats) is kept. The one-hour age cap is replaced by the budget.
…, and bypasses the OpenRouter response cache Review findings on the widened warmer (#122094): - Settle the warmer in run_conversation's finally (every turn exit), not only in finalize_turn: early returns (tool-validation exits, repetition stop, truncation) left a streaming run replaying every 270 s for up to an hour. - Cancel it in release_clients (gateway eviction, review forks) and force it off on cache-parity forks, which share the parent's prefix. - Send X-OpenRouter-Cache: false on the replay: a byte-identical request inside OpenRouter's 300 s response-cache TTL is answered from that cache and never refreshes the prompt cache upstream. - Skip a Chat Completions replay when reasoning is on for a Claude model without adaptive thinking (the gateway turns it into a budget a one-token replay changes), and warm only Claude on that wire; refuse MoA by provider too. - Cap the token key the request actually carried; the timer thread never stops a newer run the loop armed; accounting rows keep the session's real source and billing mode and are tagged task=cache_warm.
…ownership checks are atomic Greptile review on #122094: - The first refresh was scheduled delay_s after the RESPONSE finished, so a response that took over 30 s on a 5-minute entry sent its keep-alive after expiry (a paid write, not a warm). It now counts from the request's send time (api_start_time), each later refresh from its own send time, and an entry already past its lifetime arms nothing. - _validate / _stop_if_current / start check and replace _run under one RLock, so an old timer callback can never cancel a run the loop armed meanwhile. - New invariant test reads cache_warming from a real config.yaml in HERMES_HOME and pins the send-time anchor.
…t, warm x3, expire agent/prompt_cache_lifecycle.py owns the per-chat decision at each pre-expiry point, replacing agent/prompt_cache_warmer.py's scheduler and agent/cache_expiry_compaction.py's decision (both deleted). A real request opens a budget of 3 keep-alives; chats a person types into keep warming while idle, machine sessions (subagents, side agents, forks, machine-paced sources) only while a tool runs. A spent budget on a proven/declared window >= 30m with context >= half the compaction threshold and growth since the last compaction is claimed once by the surface owner (CLI idle tick, TUI ticker, gateway watcher); CLI and TUI then prime the compacted chat for one fresh budget. Any miss ends warming; an unknown clock arms nothing. Every #122094 guard (witness, single admission under lock, reset epoch, aux hooks, unknown-price refusal, spend cap, $0.05 floor) and every #156253 owner guard (claim_gate, profile scope, bounded jobs, detached chats) is kept. The one-hour age cap is replaced by the budget.
…, and bypasses the OpenRouter response cache Review findings on the widened warmer (#122094): - Settle the warmer in run_conversation's finally (every turn exit), not only in finalize_turn: early returns (tool-validation exits, repetition stop, truncation) left a streaming run replaying every 270 s for up to an hour. - Cancel it in release_clients (gateway eviction, review forks) and force it off on cache-parity forks, which share the parent's prefix. - Send X-OpenRouter-Cache: false on the replay: a byte-identical request inside OpenRouter's 300 s response-cache TTL is answered from that cache and never refreshes the prompt cache upstream. - Skip a Chat Completions replay when reasoning is on for a Claude model without adaptive thinking (the gateway turns it into a budget a one-token replay changes), and warm only Claude on that wire; refuse MoA by provider too. - Cap the token key the request actually carried; the timer thread never stops a newer run the loop armed; accounting rows keep the session's real source and billing mode and are tagged task=cache_warm.
…ownership checks are atomic Greptile review on #122094: - The first refresh was scheduled delay_s after the RESPONSE finished, so a response that took over 30 s on a 5-minute entry sent its keep-alive after expiry (a paid write, not a warm). It now counts from the request's send time (api_start_time), each later refresh from its own send time, and an entry already past its lifetime arms nothing. - _validate / _stop_if_current / start check and replace _run under one RLock, so an old timer callback can never cancel a run the loop armed meanwhile. - New invariant test reads cache_warming from a real config.yaml in HERMES_HOME and pins the send-time anchor.
…t, warm x3, expire agent/prompt_cache_lifecycle.py owns the per-chat decision at each pre-expiry point, replacing agent/prompt_cache_warmer.py's scheduler and agent/cache_expiry_compaction.py's decision (both deleted). A real request opens a budget of 3 keep-alives; chats a person types into keep warming while idle, machine sessions (subagents, side agents, forks, machine-paced sources) only while a tool runs. A spent budget on a proven/declared window >= 30m with context >= half the compaction threshold and growth since the last compaction is claimed once by the surface owner (CLI idle tick, TUI ticker, gateway watcher); CLI and TUI then prime the compacted chat for one fresh budget. Any miss ends warming; an unknown clock arms nothing. Every #122094 guard (witness, single admission under lock, reset epoch, aux hooks, unknown-price refusal, spend cap, $0.05 floor) and every #156253 owner guard (claim_gate, profile scope, bounded jobs, detached chats) is kept. The one-hour age cap is replaced by the budget.
…, and bypasses the OpenRouter response cache Review findings on the widened warmer (#122094): - Settle the warmer in run_conversation's finally (every turn exit), not only in finalize_turn: early returns (tool-validation exits, repetition stop, truncation) left a streaming run replaying every 270 s for up to an hour. - Cancel it in release_clients (gateway eviction, review forks) and force it off on cache-parity forks, which share the parent's prefix. - Send X-OpenRouter-Cache: false on the replay: a byte-identical request inside OpenRouter's 300 s response-cache TTL is answered from that cache and never refreshes the prompt cache upstream. - Skip a Chat Completions replay when reasoning is on for a Claude model without adaptive thinking (the gateway turns it into a budget a one-token replay changes), and warm only Claude on that wire; refuse MoA by provider too. - Cap the token key the request actually carried; the timer thread never stops a newer run the loop armed; accounting rows keep the session's real source and billing mode and are tagged task=cache_warm.
…ownership checks are atomic Greptile review on #122094: - The first refresh was scheduled delay_s after the RESPONSE finished, so a response that took over 30 s on a 5-minute entry sent its keep-alive after expiry (a paid write, not a warm). It now counts from the request's send time (api_start_time), each later refresh from its own send time, and an entry already past its lifetime arms nothing. - _validate / _stop_if_current / start check and replace _run under one RLock, so an old timer callback can never cancel a run the loop armed meanwhile. - New invariant test reads cache_warming from a real config.yaml in HERMES_HOME and pins the send-time anchor.
…t, warm x3, expire agent/prompt_cache_lifecycle.py owns the per-chat decision at each pre-expiry point, replacing agent/prompt_cache_warmer.py's scheduler and agent/cache_expiry_compaction.py's decision (both deleted). A real request opens a budget of 3 keep-alives; chats a person types into keep warming while idle, machine sessions (subagents, side agents, forks, machine-paced sources) only while a tool runs. A spent budget on a proven/declared window >= 30m with context >= half the compaction threshold and growth since the last compaction is claimed once by the surface owner (CLI idle tick, TUI ticker, gateway watcher); CLI and TUI then prime the compacted chat for one fresh budget. Any miss ends warming; an unknown clock arms nothing. Every #122094 guard (witness, single admission under lock, reset epoch, aux hooks, unknown-price refusal, spend cap, $0.05 floor) and every #156253 owner guard (claim_gate, profile scope, bounded jobs, detached chats) is kept. The one-hour age cap is replaced by the budget.
|
Superseded by #156455 (merged as 0e8e421). This PR's warming (keep-alives while a long tool runs, on Anthropic Messages and Chat Completions for Nous Portal, OpenRouter and Anthropic) is now the tool-wait half of the merged lifecycle, in The review fixes made here carried over:
#156455 adds idle warming, the 2-miss stop, compaction or offload at the cache edge, and per-route learning on top. Credit: @mitsuhiko for the original design in earendil-works/pi#9668, which this PR ported. Closing as superseded. |
When a tool runs longer than the prompt cache lasts (a build, a test suite,
sleep 330), the next request today pays to write the whole conversation into the cache again. Hermes now sends one tiny keep-alive copy of the last request just before the cache expires, so the next request reads the cache instead. It is always on and there is nothing to configure.Reads the shared cache clock from #156101.
Outcome: a long tool round no longer turns the next Claude request into a full-price cache write, on Nous Portal, OpenRouter and native Anthropic.
Before / after (live: real
hermes chat -q, Opus 5.5 on Nous Portal, onesleep 330tool call,cache_ttl: 5m, no warming config)maind1b46d718ff(earlier round)c4e4054e891(pre-clock)afcd7793812(reads the clock, onmain4162ecf11f)warm_until 12:44:50(5m,route_ceiling_5m, declared); keep-alive sent 12:44:22task=cache_warmrow, carries the clock column)new request in flight (refreshes=1, spent=$0.0038)new request in flight (refreshes=1, spent=$0.0025)state.db)Same control as before (Haiku 5.5
Reply with just: ok): the warmer refused,expected savings below threshold.Miss case (stub-driven, not live): a refresh can't be forced to miss on a real provider from outside, so the miss row comes from a local Chat Completions stub, called through the real OpenAI client, that bills every refresh as a 120,000-token cache write.
d46c2c21371scheduledinactiveprompt cache warmed (#2): cache_read=0 write=120000prompt cache warming stopped: cache refresh missed (read=0, write=120000, cost=$0.7500) (refreshes=1, spent=$0.7500)Control: on the earlier head, the same prompt with
sleep 5sent no keep-alive. Live spend on the clock round: $0.070 (onesleep 330Opus run $0.068 plus the Haiku control $0.0015).Why the gateway clamp: on Nous Portal a
ttl: "1h"entry was measured gone after 7 minutes, the same as a 5-minute entry. So off native Anthropic the keep-alive runs on the 5-minute schedule whatevercache_ttlasks for. Native Anthropic keeps its configured tier.What was removed, and why
prompt_caching.cache_warmingis gone. Warming during a running turn has no real tradeoff: the next request is certain, a keep-alive costs about 1/12 of the write it prevents, and the economics gate already refuses small prompts, unpriced routes and subscription-included routes. Warming follows prompt caching itself: a request with nocache_controlmarkers (caching off, non-Claude) is never warmed.Spend guards (fix round)
cache_read == 0orcache_write > 0means the entry was already gone or re-written, so warming stops for that turn and says why. Missing usage or an unknown cost also stops it.estimate_usage_coston its usage), not(n+1) ×an assumed read cost. Each refresh is priced as a cache read of the cached prefix plus the uncached tail at the full input rate, and is reserved at no less than what the last one was billed.state.dbrow to the old session, and still counts in the agent's running totals (compression keeps them; only a session reset zeroes them).prompt_cache_clock.cache_windowfor the session's guaranteed-warm deadline and refreshes shortly before it. A window the clock does not call certainly warm (unknown, a gateway asked for an untrusted1h, the cache moved, already past) arms nothing. Each refresh is folded back into the clock (its row carriesprompt_cache_clock), so/contextshows the window it bought and the next refresh is due from that deadline. The warmer's own TTL math (warm_ttl_seconds, itsTTL_SECONDS, the send-time anchor) is deleted. Route ceilings now decide the gateway clamp (Nous ships5m), so lifting a ceiling needs no warmer change.Changes
agent/prompt_cache_warmer.py::PromptCacheWarmer: a timer at 90% of the lifetime that replays the exact request (deep-copied) withmax_tokens=1, no stream,max_retries=0and a 60 s timeout. It uses the agent's Anthropic client or its Chat Completions client, sendsX-OpenRouter-Cache: false, and times its first refresh from the attempt's own send time.miss − warm ≥ $0.05fromusage_pricing.get_pricing_entry, the billed-cost cap and miss stop above, and a one-hour age limit. Budget-based thinking is skipped.turn_response_check), cancel before each real request (turn_api_request.build_request_payload), cancel on every turn exit (conversation_loop.run_conversation'sfinally), onrelease_clientsand onclose(). Cache-parity forks opt out (_skip_prompt_cache_warming).state.db, never into the compressor, usage anchors orsession_api_calls.configuration.md, no key) and developer guide.Validation
tests/agent/test_prompt_cache_warmer.pytest_warming_follows_the_shared_clock_and_never_warms_without_a_guarantee,test_a_refresh_moves_the_clock_and_the_next_one_is_due_from_its_new_deadline. Earlier rounds: New and each red on the commit before its fix:test_a_refresh_that_misses_the_cache_stops_warming_and_says_whyandtest_the_spend_cap_counts_what_refreshes_were_billed_not_an_assumed_read(both use a local HTTP stub through the real OpenAI client),test_every_stop_reason_is_logged_once_at_info,test_an_uncached_tail_is_priced_at_the_input_rate,test_a_refresh_in_flight_when_the_session_rotates_is_booked_where_it_was_sent[_compress/_new_session]tests/agent/+tests/hermes_state/(1,431 files)test_vault_key_storage.py:keyringnot installed in the local env)scripts/check --only health,ruffNot covered: native Anthropic (
api.anthropic.com; this install has no Anthropic API key), an OpenRouter leg (the key is expired; an earlier OpenRouter run on this PR showed 68% → 100%), and a live miss (it can't be forced from outside, so the stub covers it).Infographic
Rebuilt onto current
mainby cherry-picking the original 4 commits; streaming warming made unconditional per the maintainer's call. A fix round then added the miss stop, the billed-cost cap and the stop logging. Awaiting review — not merged.