Skip to content

Fix: Set keepalive.Record.PrefixRewritten in the contextguru plugin - #2169

Closed
boazdavid wants to merge 1 commit into
rossoctl:mainfrom
boazdavid:fix/2163-prefix-rewritten
Closed

boazdavid wants to merge 1 commit into
rossoctl:mainfrom
boazdavid:fix/2163-prefix-rewritten

Conversation

@boazdavid

@boazdavid boazdavid commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

What changed

  • Add prefixRewritten(pctx): true when the resolved thread.Result says this request is the first one after the main agent's own compaction (AfterCompaction), or a thread that took over another thread's content (InheritsFrom).
  • Set keepalive.Record.PrefixRewritten from it in recordFor.
  • This reuses the same thread.Result keeperKey already reads through requestThread; no extra call to the thread tracker.

Why it matters

  • context-guru's resolveOneHourProbe treats a cold return with PrefixRewritten unset the same as a real downgrade, and switches 1-hour keep-alive off for that provider/route/model for the life of the process.
  • Seen in production on 2026-11-27: a compaction/fork right after a 1-hour probe switched off the Opus route, even though the context-guru proxy proved the same route still grants the tier.

How it was tested

  • Added core/plugins/contextguru/keepalive_prefix_rewritten_test.go:
    • TestRecordForSetsPrefixRewrittenAfterCompaction — fails without the fix, passes with it.
    • TestRecordForLeavesPrefixRewrittenFalseOnAnOrdinaryTurn — control case.
  • GOWORK=off go vet ./... and gofmt -l . are both clean.
  • GOWORK=off go test ./core/plugins/contextguru/... passes.

Open questions

  • The issue's proposal also asks to (a) move the shared rule into a context-guru library function both hosts call, and (b) log prefix_rewritten on the keeper's WARN/INFO lines. Both of those are context-guru-repo changes, out of scope here.

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

Summary by CodeRabbit

  • Bug Fixes
    • Keepalive records now indicate when a thread’s prompt prefix was rewritten after compaction.

recordFor never set PrefixRewritten, so a thread's return after a client
compaction or a fork read as a cold cache miss with no other explanation.
The keeper's 1-hour probe (resolveOneHourProbe) counted that as a failed
grant and switched 1-hour keep-alive off for the route for the life of the
process (cortex#2163).

Derive it from the same thread.Result keeperKey already reads
(resolveThread's AfterCompaction and InheritsFrom), so no new call to the
thread tracker is added.

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:26
@github-actions github-actions Bot added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 29, 2026
@coderabbitai

coderabbitai Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The contextguru plugin now sets keepalive.Record.PrefixRewritten based on whether the request thread was compacted or inherited. Tests cover a compacted thread and an ordinary thread.

Changes: Keepalive Prefix Rewrite Tracking

Layer / File(s) Summary
Derive and record prefix rewrite status
core/plugins/contextguru/keepalive.go, core/plugins/contextguru/keepalive_prefix_rewritten_test.go
The new helper returns true for a thread marked AfterCompaction or one that inherits from another thread. Record construction uses this result. Tests check the compacted and ordinary cases.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: clawgenti, huang195, rubambiza

Merge Risk: Minimal · up to e5c4d

The change marks keep-alive records as prefix-rewritten after a compaction or a fork, so a cold return after either is no longer counted as a failed 1-hour grant. The tests cover both a compacted thread and an ordinary thread, and no merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the change: setting keepalive.Record.PrefixRewritten in the contextguru plugin. The raw summary says recordFor now sets PrefixRewritten through a new prefixRewritten helper, which retu…
Linked Issues check Passed Directly linked issue #2163 asks that the contextguru plugin set keepalive.Record.PrefixRewritten from the request's thread.Result, so that a compaction or fork does not switch the 1-hour route off. T…
Out of Scope Changes check Passed The whole-PR diff against the merge base touches core/plugins/contextguru/keepalive.go and adds keepalive_prefix_rewritten_test.go. The keepalive.go change is limited to the PrefixRewritten field assi…
Full details: Title check

Explanation

The title names the change: setting keepalive.Record.PrefixRewritten in the contextguru plugin. The raw summary says recordFor now sets PrefixRewritten through a new prefixRewritten helper, which returns true for AfterCompaction or InheritsFrom threads. The title is specific and matches the main change. It uses the conventional 'Fix:' prefix, which is acceptable.

Full details: Linked Issues check

Explanation

Directly linked issue #2163 asks that the contextguru plugin set keepalive.Record.PrefixRewritten from the request's thread.Result, so that a compaction or fork does not switch the 1-hour route off. The diff at head (core/plugins/contextguru/keepalive.go) adds prefixRewritten(pctx), which returns true when res.AfterCompaction is set or res.InheritsFrom() reports a source thread. recordFor sets PrefixRewritten from this helper. The helper reuses requestThread, the same resolution keeperKey already uses, so no second thread-tracker call is added. The PR also adds two tests in keepalive_prefix_rewritten_test.go: a compaction case and an ordinary-turn control case. I did not execute these tests. The author reports they pass. The issue's proposal items 2 (move the rule into a shared context-guru library function) and 3 (log prefix_rewritten on keeper WARN/INFO lines) are not implemented here. The PR states that these belong to the context-guru repository. The issue's stated check, a client compaction right after a 1-hour probe, is covered by the new code path.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against the merge base touches core/plugins/contextguru/keepalive.go and adds keepalive_prefix_rewritten_test.go. The keepalive.go change is limited to the PrefixRewritten field assignment in recordFor and the new prefixRewritten helper. Both directly implement the objective in #2163. The added test file exercises that helper. No unrelated change is visible in the reviewed evidence.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • 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

Recent review info

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: cc973aab-5828-4de4-af14-350abf59ee5a
Commits

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

Files selected for processing: 2

  • core/plugins/contextguru/keepalive.go
  • core/plugins/contextguru/keepalive_prefix_rewritten_test.go

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@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 mapping matches the context-guru proxy's rule (capture.prefixRewritten: AfterCompaction || InheritsFrom), the new test fails on main and passes here, and go vet / gofmt / go test ./core/plugins/contextguru/... are clean locally. The blocker: in a Claude Code compaction (the flow in the #2163 log), the request that resolves the 1-hour probe is the compaction call itself. This change leaves PrefixRewritten false on that call, so it would not have stopped the 2026-11-27 switch-off, and the issue's acceptance check ("a client compaction right after a 1-hour probe gives 1-hour probe released") still fails. Details are inline.

Author: boazdavid (CONTRIBUTOR association, admin repo permission — maintainer)
Areas reviewed: Go (contextguru plugin keepalive mapping, context-guru v0.7.9 thread + keepalive consumers), tests
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, all signed-off: yes
CI status: passing. I read the check annotations: they are only cache, Node 20, yamllint and hadolint warnings that were already there, and no tests marked "does not block". I could not fetch the Go CI (core) job log from here, so I ran the package tests locally.

Not applicable here: Record is an in-process struct, so no JSON decoder is involved.

if !ok {
return false
}
if res.AfterCompaction {

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: this does not cover the request that resolves the probe in the #2163 scenario.

In context-guru v0.7.9, resolveOneHourProbe runs on the first accepted Record for the probe's key and deletes the probe unconditionally (delete(k.oneHourProbes, key), onehour.go:281). In a Claude Code compaction the requests go:

  1. the compaction call (agentCompaction=true): compactionParent keeps it on the main thread, with the same key as the probe and AfterCompaction=false;
  2. the post-compaction request, which has AfterCompaction=true and also the same key.

I checked this with the real tracker (thread.New().ResolveSession) on turn1, turn2, the compaction call and the post-compaction request: all four resolve to thread "", and only #4 has afterCompaction=true. The thread.go comment in this package says the same: "The tracker marks the request that holds the summary, not the compaction request itself."

So #1 resolves and deletes the probe with PrefixRewritten=false, and by the time #2 carries the flag there is nothing left to release. The log in the issue fits this: the return starts at 01:35:16, the WARN fires at 01:35:27 when it finishes, and the 55k post-compaction request comes at 01:35:28, after the decision was made.

The InheritsFrom half does not reach the probe either. Inherits is only set when the thread id is new (th == nil in Resolve), so keeperKey gives that request a key that holds no probe. It is harmless and matches the proxy, but it does not address the issue.

Suggested fix: keep the agentCompaction verdict that resolveThread already computes on reqThread, for example reqThread{res, compactionCall bool}. Do not re-run IsClientCompaction on pctx.Body at finish time, because the engine may have rewritten the body by then. Then treat the compaction call as rewritten too. At worst this releases a probe that would have proved the tier, which is the safe direction. It goes beyond the proxy's rule, so context-guru should get the same change, ideally as the shared library function from point 2 of the issue.

// main agent's own compaction must carry PrefixRewritten, or the keeper's 1-hour probe
// (resolveOneHourProbe) reads its cold return as a downgrade and switches the route off for the
// life of the process.
func TestRecordForSetsPrefixRewrittenAfterCompaction(t *testing.T) {

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: this test builds the thread.Result by hand, so it checks the field mapping but not the scenario from #2163.

  • A test that drives the real tracker (p.threads = thread.New() and resolveThread) and a real keeper would have caught the ordering problem above: a 1-hour probe, then the compaction call, then the post-compaction request, then assert that the route is still on.
  • The InheritsFrom branch has no test at all. Result.inherits is unexported, so it can only be reached through the tracker.

@boazdavid

Copy link
Copy Markdown
Contributor Author

Closing in favor of #2170, which fixes the same PrefixRewritten gap together with #2154 and #2160 in one PR against the same function.

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 keepalive.Record.PrefixRewritten, so a client compaction does not switch the 1-hour route off

2 participants