Skip to content

feat(contextguru): Resolve the cache strategy from engine.cache and follow the wait plan - #2079

Closed
boazdavid wants to merge 3 commits into
rossoctl:cg/06-keepalivefrom
boazdavid:cg/06a-cache-strategy
Closed

boazdavid wants to merge 3 commits into
rossoctl:cg/06-keepalivefrom
boazdavid:cg/06a-cache-strategy

Conversation

@boazdavid

@boazdavid boazdavid commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Depends on #2059 (cg/06-keepalive)

This branches off cg/06-keepalive, not main: it extends that PR's keep-alive plugin, so it can't build or make sense without it. Rebase/merge order: #2059 first.

What

Two gaps against context-guru's own docs/how-to/cache-strategies.md:

  1. No strategy picker. keep_alive: had its own static policy (enabled/idle_seconds/max_pings/max_usd_per_ping/min_prefix_tokens) and never touched context-guru's named strategy table, so none of auto, none, 1-ping, 2-ping, 1-hour-head, 1-hour-on-wait, 1-hour-always or 30-min-ping could be picked.
  2. Pings didn't follow the wait. The per-wait ping plan (keepalive.Policy.WaitPlan) was never turned on, and recordFor filled none of the inputs it needs, so every wait got the same flat ping count instead of following what the thread actually waits for (its own tool, a person, background work, a teammate, done).

How

  • Bumps context-guru to v0.7.6, which carries the strategy table and WaitPlan.
  • keepAliveConfig.policy now also takes the engine's own resolved cache: block (cgconfig.CacheConfig, already parsed by cgconfig.LoadBytes from engine: in Configure — context-guru's own config.applyStrategy resolves the named strategy into it, so nothing here reimplements that table).
    • Set engine: {cache: {strategy: auto}} (or any other name) to pick a strategy.
    • keep_alive: {enabled: false} still always wins as a kill switch, independent of the strategy.
    • With no cache.strategy set, policy() returns exactly what it always did. The per-wait plan's docs: 📝 Plugin doc updates #858 "no ping before the first tool call" rule changes real scheduling, not just a tunable — it only turns on through an explicit strategy, never silently.
  • recordFor now carries Usage.StopReason (inf.FinishReason) and ToolUseNames (inf.ToolCalls) so the keeper's WaitingFor can classify the wait.

Known gap

OpenTasks (background-task/teammate markers in the transcript) is left unset. That scanner lives in context-guru's internal/waitcause, which the core does not export. A background/teammate wait here falls back to WaitingFor's unknown cause rather than a cortex-side approximation of a scanner we can't see. Left as a follow-up rather than reimplementing it locally.

Testing

  • go test ./plugins/contextguru/... — all green, including new tests for the strategy branch (auto, none, the enabled: false kill switch) and for recordFor's new fields.
  • go test ./... in core/ — clean except two pre-existing, unrelated DST fixture failures (cost/ledger, cost/usage, both tied to America/Havana's tzdata, reproducible on cg/06-keepalive before this change too).
  • go build ./... clean in core/, cmd/cortex/, cmd/cortex-envoy/.

🤖 Generated with Claude Code

boazdavid and others added 3 commits November 23, 2026 10:09
…at the keeper holds

Review fixes for rossoctl#2059:

- Pass the 1-hour cache-write count to the keeper. Without it every
  thread billed at the 1-hour tier ran on the 5-minute schedule: two
  pings that bought nothing, and no 59-minute ping.
- Do not track a request that inference-router translated or gave
  upstream headers to. Its body, path and headers in the pipeline are
  not what went upstream, so a ping could not replay it.
- Keep keep_alive on by default on every install, and add an upgrade
  note for clusters that already run context-guru: they start pinging
  on their callers' credentials with no config change.
- Close the window where a request that finishes during Shutdown could
  hold a credential after the keeper's sweep.
- Cap the accountant snapshot file at 20 MiB, not the raw state, which
  the file carries as base64.
- Fetch the LiteLLM map on the plugin's model transport, so on a local
  install it ignores HTTPS_PROXY.
- Show a subscription ping's API equivalent in agentop when its units
  round to zero.
- Docs: a keeper entry holds the masked credential and the last request
  body; its hold is 12 minutes on a 5-minute thread and 118 minutes (at
  most 236) on a 1-hour thread. The snapshot leaves out the oldest
  threads past 20 MiB. Fold the duplicated session-api.md paragraph.
- Pin context-guru to the v0.7.5 release, not v0.7.5-beta.2.
- Drop the development-log references from comments, and fix the
  accountant version in snapshot_trim.go.

Assisted-By: Claude (Anthropic AI) <noreply@anthropic.com>
Signed-off-by: DAVID BOAZ <DAVIDBO@il.ibm.com>
Brings the named cache-strategy table (config.LookupStrategy /
applyStrategy) and the per-wait ping plan (keepalive.Policy.WaitPlan),
both of which the next commit wires up. No behavior change by itself:
nothing here reads a cache: block yet.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Signed-off-by: DAVID BOAZ <DAVIDBO@il.ibm.com>
…ollow the wait plan

Two gaps against context-guru's own docs/how-to/cache-strategies.md:

1. keep_alive: had its own static policy (enabled/idle_seconds/max_pings/
   max_usd_per_ping/min_prefix_tokens) and never touched context-guru's
   named strategy table, so none of auto, none, 1-ping, 2-ping, 1-hour-head,
   1-hour-on-wait, 1-hour-always or 30-min-ping could be picked.

2. The per-wait ping plan (keepalive.Policy.WaitPlan) was never turned on,
   and recordFor filled none of the inputs it needs, so every wait got the
   same flat ping count instead of following what the thread actually waits
   for (its own tool, a person, background work, a teammate, done).

keepAliveConfig.policy now takes the engine's own resolved cache: block
(cgconfig.CacheConfig, already parsed by cgconfig.LoadBytes from engine:
in Configure -- context-guru's own config.applyStrategy resolves the named
strategy into it, so nothing here reimplements that table). Set
engine: {cache: {strategy: auto}} (or any other name) to pick one;
keep_alive: {enabled: false} still always wins as a kill switch. With no
cache.strategy set, policy() returns exactly what it always did -- the
per-wait plan's rossoctl#858 "no ping before the first tool call" rule changes
real scheduling, not just a tunable, so it only turns on through an
explicit strategy.

recordFor now carries Usage.StopReason (inf.FinishReason) and
ToolUseNames (inf.ToolCalls) so the keeper's WaitingFor can classify the
wait. ToolUseIncomplete is always false: that flag covers a host that
assembles ToolCalls from a spliced streaming-sniff window, which does not
describe this parser (inferenceparser fully assembles ToolCalls before
OnFinish runs). OpenTasks (background-task/teammate markers) is left
unset: that scanner lives in context-guru's internal/waitcause, which the
core does not export -- a background/teammate wait here falls back to
WaitingFor's unknown cause rather than a cortex-side approximation of a
scanner we cannot see.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Signed-off-by: DAVID BOAZ <DAVIDBO@il.ibm.com>
@boazdavid
boazdavid requested a review from a team as a code owner November 23, 2026 16:23
@coderabbitai

coderabbitai Bot commented Nov 23, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 186d6526-7dfb-42a7-82f4-ba7ddd7d2379

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions github-actions Bot added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 23, 2026
@boazdavid

Copy link
Copy Markdown
Contributor Author

Folded both commits onto cg/06-keepalive directly (now in #2059) instead of stacking a separate PR — they're one feature, not two, and #2059 is mine to amend.

@boazdavid boazdavid closed this Nov 23, 2026
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Nov 23, 2026
@boazdavid
boazdavid deleted the cg/06a-cache-strategy branch November 23, 2026 16:30

@clawgenti clawgenti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

The strategy picker is wired cleanly for the ping-only strategies. auto, none, 1-ping, 2-ping and 30-min-ping resolve through context-guru's own applyStrategy, the enabled: false kill switch holds, and the no-strategy path keeps its old behaviour. recordFor now passes the right inputs to WaitPlan. There is one blocker. The strategies that rewrite the request (1-hour-head, 1-hour-always, and the costly-route fallback of 1-hour-on-wait) are not delivered, because the host never passes the matching apply.Opts hooks. They are accepted without a warning and behave like none or auto. I checked this with a probe test on head. Details are inline.

Author: boazdavid (CONTRIBUTOR association, admin permission on the repo, so maintainer)
Areas reviewed: Go (core/plugins/contextguru, core/accounting, cmd/agentop/money), go.mod/go.sum bump, Docs, CLAUDE.md
Agent/IDE config (.claude/.vscode): none (only the top-level CLAUDE.md doc changed)
Commits: 3 commits, all signed off: yes
CI status: only the metadata checks ran (DCO, title, label, project). No Go job runs against the cg/06-keepalive base. I ran the checks locally on 1bb9ee4. go build ./... in core and go vet both pass. go test -race ./plugins/contextguru/... ./accounting/... and go test ./cmd/agentop/money/... all pass.

MaxUSDPerPing: resolved.KeepAliveMaxUSDPerPing,
MaxBodyBytes: resolved.KeepAliveMaxBodyBytes,
MinPrefixTokens: resolved.KeepAliveMinPrefixTokens,
HeadTTL1h: resolved.HeadTTL1h,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

must-fix: HeadTTL1h, HeadTTLMinBytes, OneHourAlways and OneHourRebuildTokens are copied into the keeper Policy, but nothing in this host acts on them. In context-guru v0.7.6, request-side relabelling is done by apply, and only when the host sets apply.Opts.HeadTTL1h/HeadTTLMinBytes (apply.go upgradeHeadTTL) and apply.Opts.OneHourFirst (the hook that calls Keeper.OneHourFirst; the context-guru proxy wires it in proxy/keepalive.go oneHourFirstFor). OnRequest (plugin.go:496) sets neither. So:

  • 1-hour-head resolves to KeepAlive: false, so no keeper is built and the head is never relabelled. It behaves like none, and it also turns off the default keep-alive.
  • 1-hour-always: no request is ever relabelled to 1h, and oneHourFirst[key] is never set, so the keeper pings exactly like auto.
  • 1-hour-on-wait on a costly route: oneHourMarkCostly puts the probing main thread in the 1-hour-first mode, so Record sets pol.KeepAlive = false for it. But its later requests are never relabelled to 1h, so that thread loses keep-alive without getting the 1-hour write that was meant to replace it. The D fallback (first cold rebuild ≥150k at 1h) never fires either.

I checked this with a probe on head. I configured engine.cache.strategy to each of these, then ran OnRequest on a 260 KB Anthropic body with cache_control on tools, system and the message. The forwarded body never contains "ttl":"1h". For 1-hour-head the keeper is nil. For 1-hour-always the keeper is on with OneHourAlways=true and nothing is relabelled.

The PR description and the policy() doc list all eight strategies as pickable, so this falls under what the PR claims to deliver. There are two ways to fix it:

  1. Wire HeadTTL1h/HeadTTLMinBytes from cfg.Cache.Resolved() into apply.Opts. Add an OneHourFirst closure that calls p.keeper.OneHourFirst(...) with the same endpoint, route and thread keys recordFor uses (watch the observe-mode path). Add a test that asserts the forwarded body carries 1h markers for 1-hour-head (body over the size gate) and 1-hour-always.
  2. Or refuse 1-hour-head, 1-hour-always and 1-hour-on-wait in Configure with a clear error until they are wired, and say so in the description and docs.

// docs/how-to/cache-strategies.md) decides every field, INCLUDING whether keep-alive runs at all
// ("none" sets no cache key, so cache.KeepAlive is false). The one override kept across both
// paths is c.Enabled == false: an explicit `keep_alive: {enabled: false}` always wins, as a kill
// switch independent of the chosen strategy. Mixing the two tuning surfaces (a strategy AND

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: "Mixing the two tuning surfaces is not supported", but nothing enforces or reports it. engine: {cache: {strategy: auto}} together with keep_alive: {max_pings: 5} drops max_pings without a word. A WARN in Configure would save an operator from a confusing debug session, for example: keep_alive.idle_seconds/max_pings/max_usd_per_ping/min_prefix_tokens are ignored while engine.cache.strategy is set. A config error would also work. Use cache: keys in the engine block instead, since applyStrategy already lets a key the file sets win over the strategy row.


**Credential and content retention:** for each tracked thread, Cortex keeps in memory, masked, the caller's credential and the thread's last request body. The body is the whole forwarded conversation, which each ping re-sends. Both are released on that thread's next request, on plugin `Shutdown` or a config reload, or at the thread's hold deadline, whichever comes first. The deadline depends on the cache tier the provider billed:
- A thread with a 5-minute cache: `(max_pings + 1) × idle_seconds`, 12 minutes at the defaults above.
- A thread whose request asks `ttl: "1h"` on every cache breakpoint, and whose cache the provider billed at the 1-hour tier: the engine's 1-hour schedule, one ping at 59 minutes, so 118 minutes. It is never more than 236 minutes (context-guru's `keepalive.MaxHold1h`). The `keep_alive` block has no setting for this schedule.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: The new user-facing knob, engine: {cache: {strategy: <name>}}, is not documented anywhere in this file. Please cover the names, the keep_alive.enabled: false kill switch winning, the fact that the other keep_alive knobs are ignored under a strategy, and the default with no strategy (no WaitPlan). Two statements are also now out of date. "The keep_alive block has no setting for this schedule" still holds for that block, but a strategy now does set it: 2-ping sets keepalive_max_pings_1h: 2, which makes the 1-hour hold 177 minutes, not 118. The same 118-minute figure is in CLAUDE.md, docs/laptop-service.md and docs/session-api.md. Saying "118 minutes with one 1-hour ping (auto), up to 236" would keep all four accurate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ai-review Request automated AI code review from clawgenti

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants