Repository navigation
Fix: set apply.Opts.CanKeepAlive in the contextguru plugin - #2171
Conversation
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>
|
Warning Review limit reachedYou'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. View limit details
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. |
clawgenti
left a comment
There was a problem hiding this comment.
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.canKeepAliveline removed,TestOnRequest_ExtractLLMSweepRegistersAPingCandidate_Issue2162fails withno_candidate. With the line in place, it passes. - The default config works too.
keepalive_wait_plandefaults to on (WaitPlanOn()returns true when the key is unset). I wrote a probe with the plan on andstrategy: auto. Before any tool turn there is no candidate, as intended (#858). After anOnFinishwhose response called a tool,ToolSeenis 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'sfireSweepSubstitutelooks up. - OpenAI and observe mode keep nothing. OpenAI routes are covered without a provider check, because
registerPingalready returns early whenPrefixAskis nil and the plugin setsPrefixAskonly for Anthropic. Underon_error: observe, no candidate is registered. go test ./plugins/contextguru/...andgo vetpass,gofmtis 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}}}`) |
There was a problem hiding this comment.
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)) okOne 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 == "" { |
There was a problem hiding this comment.
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.
Summary
apply.Opts.CanKeepAlive. Without it,extract_llm_sweepnever registers a ping candidate, so it never fires on Cortex.extract_llm_sweepmakes about 4% of the saving atultrahigh. On Cortex it made $0.What we found
ExtractSweep.registerPingandregisterWarmboth checkc.CanKeepAlive. When it isnil, they return at once ("no host hook means no keep-alive: keep nothing").c.CanKeepAlivecomes fromapply.Opts.CanKeepAlive, afunc(session, thread string) bool.h.keeper.canPing, which resolves akeepalive.Policyand callskeepalive.CanKeepAlive(pol, toolSeen).apply.Opts{...}literal inOnRequestdid not set this field at all.What changed
(*ContextGuru).canKeepAlive(session, thread string) boolincore/plugins/contextguru/keepalive.go. It mirrors the proxy'scanPing: it returnsfalsewhen the keeper is nil (keep-alive off) or the session is empty, and otherwise callskeepalive.CanKeepAlivewith the plugin's own resolved policy (p.kaPol) and the keeper'sToolSeenfor that thread's key.CanKeepAlive: p.canKeepAliveinto theapply.Opts{...}literal inOnRequest(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 throughOnRequestwithextract_llm_sweepconfigured, then checksoffload.SweepPingSubstitute— the same check context-guru's own tests use. I confirmed this test fails with the old code (noCanKeepAlive, reasonno_candidate) and passes with the fix.gofmt -l .,go vet ./...,go test ./...(core module) all pass. One pre-existing, unrelated failure incore/cost/ledgerandcore/cost/usage(date-fixture tests) is present onmaintoo and is not touched by this change.Open questions
apply.Optsfields the proxy sets and the plugin doesn't (CompactionPoint,SelfRates/RatesFor,FreePing,Tracker,Mode). This PR only fixesCanKeepAlive, 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: newcanKeepAlivemethod, next to the existingkeeperKeyhelper it reuses the samekeepalive.Keyshape for (noTenant, since Cortex is single-tenant).keepalive.Keeper.ToolSeenis already exported by thecontext-gurulibrary (keepalive/waitplan.go), so no library change was needed.Assisted-By: Claude (Anthropic AI) noreply@anthropic.com