Repository navigation
feat(keepalive): client lifecycle signals retire a session's cache on exit and compaction - #520
Conversation
… exit and compaction The Claude Code plugin can tell the proxy that a session ended (SessionEnd) or was compacted (SessionStart source=compact). The proxy retires the keep-alive entries that hold the dead prefix, so it stops pinging a cache no later request will read. - POST /signal: authenticated, metered, content-free body (session, event, kind from a closed set), 204 on accept. 404 unless CONTEXT_GURU_CLIENT_SIGNALS=on. - SessionEnd retires the session and keeps a per-tenant end mark (12 h, 2,000 per tenant) so a request in flight at exit is not held afterwards. - SessionStart compact retires only entries whose last request started 2 s before the event, so a late event cannot retire the post-compaction entry. - Other SessionStart sources and Notification are recorded only. - Plugin: SessionEnd, SessionStart compact|resume|clear and Notification hooks run scripts/signal.sh, gated by the client_signals option (default off). Never blocks; every exit is 0. - Docs: cache-keepalive how-to section and the plugin option row. Signed-off-by: Osher-Elhadad <[email protected]>
OsherElhadad
left a comment
There was a problem hiding this comment.
Review: APPROVE at ae84df7. (Posted as a comment: GitHub does not let the author account approve.)
- The code matches what was reviewed and merged in the private mirror. The only difference is one comment in
proxy/clientsignal.go, which now gives no figures. go vetis clean onproxyandcontext-guru-plugin. The proxySignal|Compact|InFlight|Lifecycle|SessionEndtests and the pluginSignaltests pass.- The commit is by Osher-Elhadad, signed off, with no AI attribution. The PR text has no identifiers or results.
Approved.
…ith #520 Signed-off-by: Osher-Elhadad <[email protected]>
amiddavid
left a comment
There was a problem hiding this comment.
Summary
One concern about how this interacts with #519 (thread identity across compaction). It is not a blocker for #520 alone. The two PRs merge with no conflict.
What we found
On SessionStart source=compact, keeper.signal calls retireSessionStartedBefore(tenant, session, now-2s). That loop matches base and every base+"\x00"<thread> key, so it retires the held entry of every thread in the session: the main thread, each subagent (a:<id>), and each side thread.
Only one thread was compacted: the one whose request carried the compaction prompt (the main agent, for /compact and auto-compact). A subagent or teammate that is idle with a live cache loses its keep-alive pings until its next request.
How #519 and #520 work together
We ran a live check of #519 (Claude Code 2.1.296, auto mode, /compact):
- Released v0.5.0: the compaction call got a new thread id. The old main entry stayed held, and keep-alive pinged it 2 times after the compaction, for a prefix nothing reads again. #520 removes exactly that waste.
- #519: the compaction call stays on the main thread, so its arrival already clears the old main entry. What is left for #520 is the compaction call's own entry (the old conversation plus the prompt). Retiring it is correct.
Proposal
- On
compact, retire only the compacted thread, not the whole session. After #519, the thread tracker marks that thread (compacting,internal/thread). The keeper could read it, or retire only header-less (main-agent) keys. - Optional, later: use this hook as the proxy's compaction signal for thread identity, in place of matching the prompt text. That would remove the false positive in #533. The hook arrives after the compaction call and before the next turn, so the timing works.
Open questions
- Does
SessionStart source=compactalso fire when a subagent compacts its own context? If it does, the payload would need the agent id to pick the right thread.
Reviewed by an AI reviewer (Claude), for the release integrator.
SummaryThe concern in my earlier comment is now a confirmed defect, so it needs a fix in this PR before release v0.5.1. #524 makes these signals the default, so the fix here reaches every user once #524 rebases. What we foundThe review of #524 tested it (#524 (review)). The main thread and an idle subagent Proposal
Posted by an AI assistant (Claude), for the release integrator. |
…l takes JSON only Only the main agent compacts, and its compaction call stays on the main thread, so SessionStart compact retires that thread's entry and leaves an idle subagent's or teammate's held cache pinging. SessionEnd still retires every thread. The route refuses a non-JSON body (415): a web page can POST text/plain to a loopback proxy without a CORS preflight, and the local proxy takes no credential. Signed-off-by: Osher-Elhadad <[email protected]>
|
Thanks. Here is a point-by-point reply to the review. The fix is in d221800. 1. Compact retired every thread. Fixed. On the compact path (a non-zero cutoff),
Open question: subagent compaction. The SessionStart payload carries no agent id. In every run we have seen, compact fired only for the main agent's Question 1 (loopback proxy needs no credential). Agreed that it is acceptable. I also closed the browser case: Question 2 ( Checks: |
OsherElhadad
left a comment
There was a problem hiding this comment.
Re-review: APPROVE at d221800. This covers the delta since ae84df7. (Posted as a comment: GitHub does not let the author account approve.)
- Compact retires the main thread only.
retireSessionStartedBeforewith a non-zero cutoff now skipse.thread != "", and SessionEnd (zero cutoff) still retires every thread. The main agent is the primary thread"":newIDgives the primary to the tool-declaring thread, and the post-compaction request inherits the compacting thread's id. That holds for Claude Code and Codex. Removing the thread skip makes TestCompactRetiresOnlyTheMainThread fail. - JSON only.
mime.ParseMediaTypeacceptsapplication/jsonwith parameters and in any case, and refuses missing,text/plainand form types with 415. TestSignalTakesJSONOnly covers all five, and disabling the check fails it. The route isPOST /signalonly, and nothing in proxy or dash setsAccess-Control-Allow-*, so a cross-origin JSON preflight is never granted.signal.shsendsContent-Type: application/json, and so do the Codex hook and the hosted snippet in #524. gofmtandgo vetare clean, and the proxy signal tests pass. CI is green. The commit is signed off with no AI attribution.
Non-blocking:
- A
p:side thread is kept on compact as well. Those are tool-less auxiliary requests, rarely large enough to be held, and their pings are bounded by MaxPings. - If the primary was evicted (restart or idle) before a compact, the main thread can come back under a
p:id. A compact then retires nothing, which is the no-signal behaviour and fails safe.
Approved.
…gin to 0.2.1 Both plugins changed in v0.5.1: the Claude Code plugin gained the lifecycle signal hooks (rossoctl#520, rossoctl#524) and the per-agent money status line (rossoctl#525); the Codex plugin gained its SessionEnd and SessionStart hooks (rossoctl#524). A new version number is what makes an installed plugin update. Assisted-By: Claude Opus 5.5 (1M context) <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
What
The proxy cannot see from the request stream that a user quit Claude Code or that the conversation was compacted, so the keep-alive keeps pinging a cache no later request will read. With this change the Claude Code plugin tells the proxy, and the proxy retires the held entries.
SessionEnd(any reason)SessionStartsource=compactSessionStartsources,NotificationPOST /signal: authenticated and metered like any request; body{"session","event","kind"}from a closed set, unknown fields refused, 1 KiB cap,204on accept.SessionEnd,SessionStart(compact|resume|clear) andNotificationhooks runscripts/signal.sh. It reads only those three fields from the hook payload, sends the credential to curl on stdin, has a 2 s timeout, retries a not-yet-started proxy in the background, and exits 0 on every path.How to turn it on
Off by default on both sides.
CONTEXT_GURU_CLIENT_SIGNALS=on(otherwise/signalis 404).client_signalsoptionon(/plugin configure), orCONTEXT_GURU_CLIENT_SIGNALS=onin the shell.How it fails safe
A signal only removes spend: it retires a held entry and never extends a hold or arms a ping. A lost or late event leaves the no-signal behaviour; a forged one can only affect the sender's own sessions (the key is tenant-scoped); end marks are capped per tenant. If
ANTHROPIC_BASE_URLis not a context-guru proxy, that host seesPOST /signalwith the token it already receives, so leave the option off there (documented).Docs
docs/how-to/cache-keepalive.md(new section) and the plugin options table indocs/how-to/install-plugin.md.Tests
gofmt -lclean,go vet ./proxy ./context-guru-pluginclean.go test -race -run 'Signal|Lifecycle|Compact|Retire' ./proxy ./context-guru-pluginpass.go test ./proxy ./context-guru-plugin ./sessionpass.SessionEnd, the hook script's payload filtering and retries (race-free).