Skip to content

feat(keepalive): record the subscription limit headers on ping rows - #1012

Merged
amiddavid merged 2 commits into
mainfrom
feat/ping-limit-headers
Nov 10, 2026
Merged

amiddavid merged 2 commits into
mainfrom
feat/ping-limit-headers

Conversation

@amiddavid

@amiddavid amiddavid commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Behavior change (requested by the owner in #995). Who: subscription users with keep-alive on. Old: GET /api/me/limits shows the 5-hour share from the user's last own request. New: a ping's answer also updates it, so the share stays current while the user is idle. How to turn it off: turn keep-alive off (cache strategy none); then no ping rows exist. The dashboard must be on for any of this to be stored.

Summary

Keep-alive pings now record the subscription limit headers. A ping is a real request, and its answer carries the anthropic-ratelimit-unified-5h-* and -7d-* headers. The proxy stores them on the ping row. GET /api/me/limits reads the newest row with values, so the 5-hour share stays current while the user is idle.

What changed

  • components.KeepAliveReport has a new field, Header: the ping response's headers. nil when there was no response to read.
  • keepalive: scheduled pings (record1) and manual pings (PingNow) put the response headers on the report. The keeper reads nothing from them.
  • proxy.PingLimits reads the report's headers with the parser from feat(proxy): record the subscription limit headers and serve GET /api/me/limits #991 (readLimitHeaders). No second parser.
  • cmd/context-guru-proxy (dashEmitter) sets Event.Limits on the ping row from proxy.PingLimits.
  • auth_subscription stays 0 on ping rows. The auth kind still comes from the user's own newest request (LatestAuthSubscription already skips ping rows). So an account that moves to an API key still gets {} at once.
  • A ping that gets a 429 or 5xx is still booked with its row, as before. Its row now also has the headers, as on the request path.
  • Not covered: a ping replaced by a summary or sweep call (a substitute). That call goes through the cheap-model path and has no ping response. Its report has no Header.

Data dependence

Metadata only: numbers and short status words from response headers. The same at every storage level. No change in kind. The row in docs/data-dependence.md now also names ping rows.

How it was tested

New tests:

  • keepalive/pingheader_test.go: a scheduled ping, a 429 ping and a manual ping carry the response headers on the report. A ping with no headers carries none.
  • proxy/pinglimits_test.go: PingLimits gives the same result as the request parser, and nil for no header or a bad header.
  • cmd/context-guru-proxy/dashemitter_limits_test.go: a ping row stores the limits. LatestLimits returns the ping's values (newer than the user's request). LatestAuthSubscription is still true from the user's request. A ping without headers stores no limits.

On the eval box:

  • go build ./... and go vet ./...: clean.
  • go test -race on ./keepalive/..., ./components/..., ./cmd/...: all pass.
  • go test -race ./proxy/...: all pass (531 s).
  • ./dash/... is not changed.

Closes #995

🤖 Generated with Claude Code

A keep-alive ping is a real request, so its answer carries the
anthropic-ratelimit-unified-5h-* and -7d-* headers. The ping row now
stores them, so GET /api/me/limits stays current while the user is idle.

- components.KeepAliveReport gets the ping response's Header.
- The keeper passes it from scheduled and manual pings.
- The proxy host reads it with the same parser as the request path
  (proxy.PingLimits wraps readLimitHeaders) and sets Event.Limits.
- auth_subscription stays 0 on ping rows; the user's request decides it.

Closes #995

Assisted-By: Claude Opus 5.5
Signed-off-by: DAVID AMID <[email protected]>

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.322 offload-review skill, @amiddavid's session fresh 1 of 2 1 of 2 74db21ccaf7d approve

Verdict: approve

Findings

None. I found no defect that blocks this PR.

Non-blocking

  • components/component.go:1240 — the field doc says Header is nil "when there was no response to read (a substitute call...)". A substitute call does get an answer with headers; the summary result type (offload.KeepAliveSummaryResult) just does not carry them. The behavior is right and the PR names the gap. Only the wording is loose.
  • proxy/pinglimits_test.go:16 — the test compares PingLimits with readLimitHeaders on the same input. That part cannot fail while the delegation stays. The nil and bad-header cases are the real checks. Fine as is.
  • dash/limits.go:88 — LatestLimits, unlike LatestAuthSubscription, has no provider = 'anthropic' filter. Pre-existing, and only Anthropic sends these headers, so ping rows change nothing here.

What I checked

  • The full diff, one commit, and the PR description.
  • Every emitter call site: record1 (keeper.go:1690), PingNow (manual.go:184), and recordSubstitutePing (substitute.go:273). All report paths that have a response now pass resp.Header; the substitute path leaves it nil on purpose, and the PR says so.
  • All four record1 call sites in fire (transient, failed-recorded, missed, normal) pass the header. The transport-error path emits no report, which is right: no response exists.
  • Header ownership and races. The proxy dispatcher clones the upstream header before returning it (proxy/dispatcher.go:322), so the keeper holds the only reference. dashEmitter reads it inside PingLimits before Record, synchronously. The fake test clones under its lock. No sink mutates the map.
  • The parser. PingLimits reuses readLimitHeaders; no second parser. Status words are allow-listed (limitStatusOK), utilization is bounded, so no free text reaches a row.
  • The dash side. limitArgs writes limit_seen and the seven columns for any row, ping rows included (dash/store.go:682). LatestLimits reads the newest limit_seen = 1 row with no keep-alive filter, so a ping row now wins while the user is idle — the feature. LatestAuthSubscription keeps its keepalive = 0 filter, so the auth kind still comes from the user's own request; the test asserts this.
  • Fail open. PingLimits is a pure parse and cannot fail. dashEmitter.KeepAlivePing keeps its panic recover (issue #918). A ping without headers records limit_seen = 0, which stays apart from a share of 0.
  • The 429 and 5xx path. noteOutcome maps them to outcomeTransient, which still books the row, so the provider's refusal headers are recorded — same as the request path.
  • Tests. I read all three new test files and the helpers they use (manualKeeper, reportSink, fakeOf, the 281-second sweep pattern from the existing tests). The dashEmitter test leaves pings nil, but PingSavings returns before st.put because Window is zero. I did not run the tests; live runs were not approved.
  • Data dependence. docs/data-dependence.md row #984 now names ping rows. Same kind: numbers, short status words, a flag. No direction change, so no new "Decided by" confirmation is needed.
  • The behavior change is named at the top of the PR: /api/me/limits returns fresher values while the user is idle. Who it affects, the old and new behavior, and the scope are all stated.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.322 offload-review skill, @amiddavid's session fresh 2 of 2 1 of 2 74db21ccaf7d approve

Verdict: approve

Findings

None. I could not prove a defect in the changed lines.

Non-blocking

  • PR body, What changed — the turn-off path is not named. RELEASING.md asks the author to name each behavior change with "how to turn it off". The Summary names the change and the new behavior, but not the off switch. It exists: the feature needs the dashboard, and ping rows stop when keep-alive is off. One line in the PR body would close the gap. The behavior itself is what issue #995 (filed by @amiddavid) asks for, so the human's request is on record.
  • keepalive/pingheader_test.go:41 — the comment names a case the test does not run. The comment says a refused ping's headers carry the status "rejected". limitHeader() sets "allowed", and the test asserts "allowed". A "rejected" status word never runs through this path in the tests. Cosmetic: the parser's status handling is covered in proxy/pinglimits_test.go and proxy request-path tests.
  • components/component.go:1240 — the core report type now imports net/http. dispatch already speaks http.Header, so this adds no new dependency direction. Noted only, because the repo keeps the core API surface small.

What I checked

  • Read CLAUDE.md, docs/design.md sections on keep-alive and the dispatch design, and the full diff (1 commit, 10 files).
  • Header flow, keeper side. fire() passes resp.Header on all four record1 outcomes that book a row (transient, failed-recorded, missed, read); PingNow passes it on its success path. The transport-error path calls no record1, so a zero-value resp is never read. All record1 call sites got the new argument. dispatch.Response has the Header field, and the proxy's Dispatcher.Do returns a private clone (proxy/dispatcher.go:324), so no other holder can mutate the map the report carries.
  • Substitute paths. substitute.go's report and fireExtra set no Header, so PingLimits returns nil there. This matches the field's doc and the PR's "Not covered" note.
  • Host side. dashEmitter.KeepAlivePing sets ev.Limits from proxy.PingLimits, which calls the same readLimitHeaders as the request path (proxy.go:2228, noteLimits). No second parser. A nil or bad header yields nil limits, and limitArgs then stores limit_seen = 0, so the row stays clean.
  • The auth-kind gate. LatestAuthSubscription filters keepalive = 0, so an account that moves to an API key still gets {} from GET /api/me/limits even while old ping rows carry limit values. LatestLimits has no keep-alive filter, so a newer ping row wins — which is the feature. Both are asserted in dashemitter_limits_test.go.
  • Credential consistency. A ping dispatches with the credential the hold took from the client's own request (heldAuth, proxy/dispatcher.go:133), so the ping's limit headers describe the same account the client's requests do. A server-held key (up.setKey) affects the request path the same way, so the two paths cannot disagree.
  • Tests. All four new keeper tests use existing helpers (manualKeeper, fakeOf, sess1); the fake's change clones under the lock and is nil-safe. The dash test's polling loop, filter flags (Requests forces WithKeepAlive = true), and PingSavings early-return on a zero Window all check out against the current code.
  • Docs and rules. The data-dependence row is updated in plain words and the kind stays metadata. No new component, so datadep_test.go needs no row. Fail-open is untouched: this change only adds recorded values, and a parse failure stores nothing.

The Header field doc now says why a substitute call has no headers. The
429 test comment no longer names a "rejected" status that it does not run.

Assisted-By: Claude Opus 5.5
Signed-off-by: DAVID AMID <[email protected]>
@amiddavid

Copy link
Copy Markdown
Collaborator Author

Both round-1 reviews approve 74db21c. The non-blocking notes are fixed:

  • Reviewer 1, components/component.go: the Header doc now says a substitute call has no headers because its summary result does not carry them. Fixed in 14c23ef.
  • Reviewer 2, keepalive/pingheader_test.go: the comment no longer names a "rejected" status that the test does not run. Fixed in 14c23ef.
  • Reviewer 2, PR body: the top now names the behavior change and how to turn it off (keep-alive off).
  • Reviewer 2, net/http in components: kept. dispatch already uses http.Header.
  • Reviewer 1, LatestLimits has no provider filter: this was there before the PR, and only Anthropic sends these headers. No change.

scripts/release/approval-still-valid.sh 1012 74db21cc…:

COMMENTS ONLY: 74db21ccaf7d..14c23efccdba changes only comment lines in .go and .sh files. The approval of 74db21ccaf7d still counts.

@amiddavid

Copy link
Copy Markdown
Collaborator Author

Owner decisions on the two open questions:

  1. Ping rows do not decide the auth kind. The subscription check reads only the user's own newest request. No change.
  2. Substitute pings: a follow-up issue tracks it, and work starts now on a branch on top of this PR.

@amiddavid
amiddavid merged commit fee8a48 into main Nov 10, 2026
12 of 13 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Nov 10, 2026
amiddavid added a commit that referenced this pull request Nov 10, 2026
Conflict in docs/data-dependence.md: kept the Pi row and main's updated limit-headers row.

Assisted-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
amiddavid added a commit that referenced this pull request Nov 10, 2026
#1012 is merged. Keep this branch's text for the KeepAliveReport.Header
comment and the data-dependence row.

Assisted-By: Claude Opus 5.5
Signed-off-by: DAVID AMID <[email protected]>
amiddavid added a commit that referenced this pull request Nov 10, 2026
At each Pi session start, the Pi extension now checks the local
proxy with one GET /healthz (5-second limit). If the proxy does not
answer, it restarts the proxy's user service once and checks again.
If the proxy is still down, Pi shows one warning, not only a
connection error on every routed request.

Behavior change: Pi users who installed context-guru with
/context-guru setup, and only when a Pi model goes to the proxy. Old:
nothing checked the proxy, and a down proxy showed only connection
errors. New: the check, one restart, and one warning. No switch;
pi --no-extensions turns off the whole extension, and
/context-guru uninstall removes the install record and so the check.
Owner's yes, 2026-11-10, in the integrator session: "1011 behavior change approved".

Data dependence: metadata only. A bare GET /healthz to localhost with
no body. It reads the install record and the model URLs Pi knows. No
change to stored data.

Reviews: 2 offload reviews (GLM), round 3 (owner granted round 3), both approve at 655a6a9. Round 3 reviewed the merge of main after #1010, which changed the same 5 pi-package files.
Two later merges of main (1077d80 after #1012, d4124cd after #1014)
change no #1011 code: their remerge-diff outside
docs/data-dependence.md is 0 lines; the one doc conflict kept both
rows.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
amiddavid added a commit that referenced this pull request Nov 10, 2026
…rs (#1021)

When a keep-alive ping is replaced by a cache_aware_summarizer summary
or an extract_llm_sweep question, the substitute's dashboard row now
records the subscription limit headers of its own answer, as an
ordinary ping row does (#1012). So GET /api/me/limits stays current
while the user is idle. Only an answer sent with the user's own
credential counts; a summary on a config-sourced model does not.

Behavior change: users on a Claude subscription whose keep-alive pings
are replaced by a summary or a sweep question. Old: a substitute's row
had no limit values, so the share could be older. New: it has them.
Off: turn keep-alive off (no pings and no substitutes), or the
dashboard off (no rows kept).
Owner's yes, 2026-11-10, in the integrator session: "#1021 is ready to merge" and "1021 and 988 in this order are also ready".

Data dependence: metadata only (numbers and short status words from
response headers). No change in kind.

Reviews: 2 offload reviews (GLM), round 3, both approve at f320374.
After it: 2 merges of main with no hand resolution (remerge-diff 0),
each tested on the merged tree (./keepalive, ./components,
./cheapmodel, ./dash, ./proxy).

Assisted-By: Claude Opus 5.5
Signed-off-by: DAVID AMID <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

keepalive: record the subscription limit headers on ping responses

1 participant