Skip to content

Fix: set apply.Opts.CanKeepAlive in the contextguru plugin - #2171

Merged
boazdavid merged 1 commit into
rossoctl:mainfrom
boazdavid:fix/2162-cankeepalive
Nov 29, 2026
Merged

boazdavid merged 1 commit into
rossoctl:mainfrom
boazdavid:fix/2162-cankeepalive

Conversation

@boazdavid

Copy link
Copy Markdown
Contributor

Summary

What we found

  • ExtractSweep.registerPing and registerWarm both check c.CanKeepAlive. When it is nil, they return at once ("no host hook means no keep-alive: keep nothing").
  • c.CanKeepAlive comes from apply.Opts.CanKeepAlive, a func(session, thread string) bool.
  • The context-guru proxy sets this field from h.keeper.canPing, which resolves a keepalive.Policy and calls keepalive.CanKeepAlive(pol, toolSeen).
  • The contextguru plugin's apply.Opts{...} literal in OnRequest did not set this field at all.

What changed

  • Added (*ContextGuru).canKeepAlive(session, thread string) bool in core/plugins/contextguru/keepalive.go. It mirrors the proxy's canPing: it returns false when the keeper is nil (keep-alive off) or the session is empty, and otherwise calls keepalive.CanKeepAlive with the plugin's own resolved policy (p.kaPol) and the keeper's ToolSeen for that thread's key.
  • Wired CanKeepAlive: p.canKeepAlive into the apply.Opts{...} literal in OnRequest (core/plugins/contextguru/plugin.go).

How it was tested

  • TestCanKeepAlive: unit test of the new method — off when keep-alive is off, off with no session, on when keep-alive is on and the wait plan is off.
  • TestOnRequest_ExtractLLMSweepRegistersAPingCandidate_Issue2162: drives a real Anthropic request with a large tool output through OnRequest with extract_llm_sweep configured, then checks offload.SweepPingSubstitute — the same check context-guru's own tests use. I confirmed this test fails with the old code (no CanKeepAlive, reason no_candidate) and passes with the fix.
  • gofmt -l ., go vet ./..., go test ./... (core module) all pass. One pre-existing, unrelated failure in core/cost/ledger and core/cost/usage (date-fixture tests) is present on main too and is not touched by this change.

Open questions

  • The issue also lists other apply.Opts fields the proxy sets and the plugin doesn't (CompactionPoint, SelfRates/RatesFor, FreePing, Tracker, Mode). This PR only fixes CanKeepAlive, the field the issue's title and measured $0 regression are about. The others are a separate, broader review and are left for a follow-up.

Technical notes

  • core/plugins/contextguru/keepalive.go: new canKeepAlive method, next to the existing keeperKey helper it reuses the same keepalive.Key shape for (no Tenant, since Cortex is single-tenant).
  • keepalive.Keeper.ToolSeen is already exported by the context-guru library (keepalive/waitplan.go), so no library change was needed.

Assisted-By: Claude (Anthropic AI) noreply@anthropic.com

Without CanKeepAlive, ExtractSweep.registerPing and registerWarm always see
"no host hook means no keep-alive: keep nothing" and never register a
candidate, so extract_llm_sweep never fires on Cortex. On the context-guru
proxy it makes about 4% of the saving at ultrahigh; on Cortex it makes $0
(cortex#2162).

Add canKeepAlive, mirroring the proxy's h.keeper.canPing: resolve the
thread's policy via keepalive.CanKeepAlive, with the keeper's ToolSeen as
the per-wait-plan gate. Wire it into the apply.Opts literal in OnRequest.

Assisted-By: Claude (Anthropic AI) <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 29, 2026 07:36
@coderabbitai

coderabbitai Bot commented Nov 29, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 49 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

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: 24ae7cab-4d97-44d0-b299-e6a4c545d30e
Commits

Reviewing files that changed from the base of the PR and between 7715238 and af8d452.

Files selected for processing: 3

  • core/plugins/contextguru/issue2162_test.go
  • core/plugins/contextguru/keepalive.go
  • core/plugins/contextguru/plugin.go
  • 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 29, 2026

@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 fix is correct and does what the PR says. canKeepAlive matches the proxy's canPing, and wiring it into apply.Opts makes extract_llm_sweep register ping candidates on Cortex. I checked this locally at af8d452:

  • The regression test is real. With the CanKeepAlive: p.canKeepAlive line removed, TestOnRequest_ExtractLLMSweepRegistersAPingCandidate_Issue2162 fails with no_candidate. With the line in place, it passes.
  • The default config works too. keepalive_wait_plan defaults to on (WaitPlanOn() returns true when the key is unset). I wrote a probe with the plan on and strategy: auto. Before any tool turn there is no candidate, as intended (#858). After an OnFinish whose response called a tool, ToolSeen is set under the keeper's key, and the next request registers a candidate. The session in that candidate's key (thread.Key(sess, "")) is the same one the keeper's fireSweepSubstitute looks up.
  • OpenAI and observe mode keep nothing. OpenAI routes are covered without a provider check, because registerPing already returns early when PrefixAsk is nil and the plugin sets PrefixAsk only for Anthropic. Under on_error: observe, no candidate is registered.
  • go test ./plugins/contextguru/... and go vet pass, gofmt is clean, and CI is green with no annotations on Go CI (core).

FYI, nothing to change: canKeepAlive receives the engine's session id from session.Scoped. That equals sessionID(pctx) only when the id passes the explicit-id charset (at most 128 characters from [A-Za-z0-9._:-]). A fallback Identity.Subject() such as an email address or a SPIFFE URI is hashed. ToolSeen then misses and canKeepAlive returns false. That is the safe answer, since the keeper's substitute looks up by the raw sid and would never find a candidate keyed by the hash anyway.

Nit: issue2162_test.go is the only issue-named test file in the repo. These two tests would fit in keepalive_test.go.

Author: boazdavid (CONTRIBUTOR association, admin permission on the repo, so maintainer)
Areas reviewed: Go (contextguru plugin, keep-alive wiring against context-guru v0.7.9), tests
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, all signed-off: yes
CI status: passing

// keepalive_wait_plan: false, so this isolates #2162's wiring from the per-wait plan's own
// "no tool call seen yet" gate (#858), which TestOnRequest_ExtractLLMSweepRegistersAPingCandidate_Issue2162
// does not drive a tool call either.
on := configured(t, `{"engine": {"cache": {"strategy": "auto", "keepalive_wait_plan": false}}}`)

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: Both new tests set keepalive_wait_plan: false. The shipped default is the plan on (CacheConfig.WaitPlanOn() is true when the key is unset), so production always goes through the ToolSeen branch, which neither test covers. That branch also depends on the engine's session id and the keeper's keeperKey agreeing.

I checked it with a probe and it works. Still, a case like this would guard it:

p := configured(t, `{"context_window":{"model_info":"off"},"engine":{"pipeline":["extract_llm_sweep"],` +
	`"cache":{"strategy":"auto"},"components":{"extract_llm_sweep":{"min_tokens":50}}}}`)
// 1st request: no tool turn seen yet -> SweepPingSubstitute reports no_candidate
// finish a turn: f.StatusCode=200, f.StartedAt=time.Now(), CacheReadTokens set,
//   FinishReason="tool_use", ToolCalls=[{Name:"Read"}]; p.OnFinish(...)
// 2nd request -> SweepPingSubstitute(engineKey(pctx)) ok

One gotcha: set StartedAt on the finished pctx. Without it, Keeper.Record returns before it records toolSeen.

// rule, with the keeper's record of whether the thread has seen a tool call yet (ToolSeen, #858).
// p.keeper is nil when keep_alive.enabled is false, so a thread then never keeps anything either.
func (p *ContextGuru) canKeepAlive(session, thread string) bool {
if p.keeper == nil || session == "" {

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.

nit: Consider also returning false once p.stopped.Load() is set. After Shutdown, while requests drain on the old pipeline during a hot-reload, OnFinish returns before Record. So a candidate registered then is never pinged and only sits in memory until maxSweepPingAge. The Arrive branch in OnRequest (plugin.go:686) already gates on !p.stopped.Load(), and this would match it. It's bounded, so this is not blocking.

@boazdavid
boazdavid merged commit 7e7fc6b into rossoctl:main Nov 29, 2026
43 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Nov 29, 2026
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.

contextguru plugin: set apply.Opts.CanKeepAlive, so extract_llm_sweep can run (about 4% of baseline at ultrahigh)

2 participants