Skip to content

feat(keepalive): recommended policy as default (270 s, K=2, 80k floor, $0.50 ceiling) (#435) - #444

Merged
OsherElhadad merged 3 commits into
mainfrom
feat/keepalive-recommended-defaults
Oct 11, 2026
Merged

OsherElhadad merged 3 commits into
mainfrom
feat/keepalive-recommended-defaults

Conversation

@OsherElhadad

@OsherElhadad OsherElhadad commented Oct 11, 2026 •

Copy link
Copy Markdown
Collaborator

TL;DR

Make the policy recommended by the held-out re-analysis (#435, workstream B) the default: keepalive_idle_seconds 270 (was 280), keepalive_max_pings 2 (unchanged), keepalive_min_prefix_tokens 80000 (was 20000), keepalive_max_usd_per_ping 0.50 (was 0.25). keepalive itself stays off by default (consent decision, unchanged).

Independent verification (VERIFY-B-policy-r2), a red-team pass and a live paired run (B-live-validate) have landed for K=2, the 80k floor and the $0.50 ceiling. The 270-vs-280 choice is NOT confirmed by any of them (see Caveats). Not deployed; deployment is for owners.

What changed

  • config/config.go: the four defaults; the "X=280 wastes 53 pings, +$125.08" rationale (W1 clock) replaced by the W3 one.
  • proxy/keepalive.go: DefaultMaxUSDPerPing mirror (pinned to config by an existing test).
  • proxy/campaign.go: keepalive-5m arm idle 270 (campaignKeepAlive5mIdle); campaignDefaultMinPrefixTokens 80000 (shared by all campaign arms). keepalive-5m-once and stop-reason-gated stay at 280: the 270 evidence covers K>=2 only, K=1 prefers 280, and the gate loses.
  • dash/keepalive.go: replay gate kaGateMinPrefix and recIdleSeconds (the shipped policy they mirror).
  • Tests: defaults pinned, campaign arms pinned, replay fixtures moved above the 80k floor. CGO_ENABLED=1 go test passes for config, proxy, dash, kvcache, tenant, cmd, components, plugin; gofmt clean.
  • Not changed: the kvcache simulator/suggester keeps its 280 s schedule (its hazard windows are fixed at the W1 clock; it cannot represent the W3 cliff), so suggest cells are still scored at 280 while keepalive-5m now runs at 270.

Evidence (counterfactual, model dollars at deployed gateway prices, same denominator: never-ping model total over the 54-day union window)

Savings vs never-ping, 95% CI by session bootstrap.

arm temporal, ex-poller leave-one-tenant-out, all LOTO, ex-poller pings (LOTO)
deployed policy (observed pings) 171 294 261 3,717
ping-everyone K=2, X=280, no floor 803 1,799 1,090 76,836
K=2, X=270, floor 80k (this PR) 1,084 (K<=2 grid-selected) 2,123 [1,288, 3,356] 1,582 [1,119, 2,161] 16.2k
K=3, X=270, floor 80k (if pollers) 2,859 (grid, any K) 1,692 21.7k
oracle (upper bound, not deployable) 4,554 3,433 12.4k

X=270 vs 280 at K=2, floor 80k: the earlier "+273 [-21, +488]" is WITHDRAWN. Independent re-derivation: all -19 [-462, +326] (a tie), ex-poller +299 [225, 365]; red team: between about -$425 and +$780 depending on the unknown ping cliff (295-305 s). 270 is kept as the cliff-robust (minimax) choice, not as a demonstrated gain. The recommended rule captures about 47-50% of the oracle.

Caveats

  • Counterfactual from a fitted time law (W3: response-end clock, hard ~295 s cliff). 270 vs 280 is a tie on the full window and depends on the ping cliff: if the cliff is at 290 s, 280 loses about $420; if it is at 300 s, 280 wins about $240. K=1 prefers 280. The live run (low load, fire lateness <= 2 s) could not test the 270/280 margin.
  • Ex-poller levels fall 40-47% (one periodic 10-15 min poller episode); if pollers are expected use K=3.
  • Reconciliation to the bill is -0.7% to -1.1% overall but two cells exceed 2%; only deltas are quoted. Ping side validated only against the 3,717 deployed pings.
  • Operational ping cost (rate limits, 429s, credential hold) is not in these dollars. The 0.25 ceiling forfeited money (K=2/80k/270: 1,390 with it vs 2,123 without).
  • A live A/B of 270 vs 280 on one opted-in tenant under production load would settle it.

@OsherElhadad

Copy link
Copy Markdown
Collaborator Author

Verification update (#446): the recommended policy's dollars are confirmed independently ($2,144 [1,294, 3,068] vs never-ping over 54 days, counterfactual). The claim in this PR body that 270 s beats 280 s by +$273 is withdrawn: over the full window the two tie (−$19 [−462, 326]). We keep 270 s for robustness — if the TTL cliff sits at 290 s, 280 s loses about $420 and 270 s loses nothing material. The PR stays a draft until the live paired validation lands.

@OsherElhadad
OsherElhadad marked this pull request as ready for review October 11, 2026 05:23
@OsherElhadad

Copy link
Copy Markdown
Collaborator Author

Live paired validation landed (see #446): hits and ping counts match the simulator; observed saving is ~15–25% below the model, so the honest estimate is ~$1.6–1.8K over 54 days (3.5–3.9% of the bill). Ready for review. Not deployed; deployment is for owners.

@OsherElhadad OsherElhadad changed the title feat(keepalive): recommended policy as default (270 s, K=2, 80k floor, $0.50 ceiling) [draft, #435] feat(keepalive): recommended policy as default (270 s, K=2, 80k floor, $0.50 ceiling) (#435) Oct 11, 2026
@OsherElhadad

Copy link
Copy Markdown
Collaborator Author

Reviewer: CHANGES REQUESTED. Tests are fine (go test config, kvcache, tenant, proxy keep-alive/campaign, dash keep-alive green; gofmt clean; CI pass). The PR body repeated the withdrawn '270 beats 280 by +273' claim, so I rewrote that part myself (VERIFY: tie -19 [-462, 326] all, +299 ex-poller; REDTEAM: -425..+780 depending on the cliff; 270 kept as the minimax choice; live run did not test the margin) and dropped the 'draft' wording. Remaining defects are in the code:

Must fix

  1. config/config.go (DefaultKeepAliveIdle comment) still says 'for K>=2 it beats 280 by $273..$534 over 54 days'. That is the withdrawn claim, now committed as a code comment. Replace with: tie on the full window, chosen as the cliff-robust choice, K=1 prefers 280.
  2. Old values still encoded in keep-alive paths, so the 'default' differs by entry point:
    • proxy/keepalivectl.go:156 recIdleSecondsDefault = 280 (arming a session with no idle in the body gets 280, not 270).
    • dash/keepalive.go:1292 KeepAliveCalc fallback idleSeconds = 280, and :548 coverageSeconds = 2*280 + TTL (the 'shipped default's 860 s' becomes 810 s). :648 'prefix >= 20000' and prefixEdges' 20000 are analytic buckets (AboveTwentyK); fine to keep, but the comments at ~495-509 and ~1207 call 20k 'the shipped gate', which is now false.
    • context-guru-plugin/scripts/settings.py:3230-3237 writes keepalive_idle_seconds 280, max_usd 0.25, min_prefix 20000 into config and describes '280s ... >=20k ... <=$0.25' (comment there says explicit values exist precisely so defaults don't re-tune installs; so either update it or document that it intentionally stays on the old policy).
    • dash/ui/app.js:7263 (fallback 280), :8616/:8618 (form defaults 280 / 20000), :10337 ('default (280 s idle, 2 pings)'); docs/how-to/cache-keepalive.md:60-77 ('280-second interval is the measured optimum', 20000, 0.25, +$125.08).
  3. Deliberate exceptions are documented only for campaign.go (once / stop-reason-gated at 280) and the PR body (kvcache suggester scores at 280); add the one-line note at the kvcache suggester's schedule constant so the divergence is visible where someone will look. Note also that campaignDefaultMinPrefixTokens = 80000 is shared by the once and gated arms, whose K=1 evidence is separate; say so in the arm comment or keep their floor at 20000.

Heads-up (no action needed): the new campaign tests pin keepalive-5m (270) and once (280) only; add a stop-reason-gated assertion too.

@OsherElhadad

Copy link
Copy Markdown
Collaborator Author

Addressed review: config comment now states the tie (-19 [-462, +326]) and cliff-robustness rationale; remaining 280/20k/0.25 sites updated (keepalivectl fallback, KeepAliveCalc/behaviour fallbacks, plugin settings.py, dashboard form defaults and fallbacks, how-to doc); once/stop-reason-gated/1h campaign arms keep the 20k floor via a per-arm field, with a test; note added at the kvcache schedule constant. Tests and gofmt pass (one unrelated SQLITE_BUSY flake under load passed on rerun).

@OsherElhadad

Copy link
Copy Markdown
Collaborator Author

Reviewer: approved after re-review. Full runs on this branch: go test ./dash -count=1 (137 s) and ./proxy (96 s) pass, plus config, kvcache, tenant; gofmt clean; CI all pass. Under heavy box load (load avg ~11, other agents running) one earlier dash run failed in the timing-sensitive TestJanitorPassCheckpointsWAL (WAL size) and a concurrent proxy run failed; both pass on rerun at lower load, the WAL test passes 3/3 in isolation on this branch and on origin/main, and none of it touches keep-alive code. Treat as the existing load flake.

Checked: the config.go comment now states the robustness rationale (tie -19 [-462, +326], not a gain; K=1 prefers 280); proxy/keepalivectl.go, dash/keepalive.go (calc fallback, coverage), settings.py preset, app.js and docs/how-to/cache-keepalive.md moved to 270 / 80k / $0.50; deliberate exceptions are documented in code: keepalive-5m-once, stop-reason-gated and 1h stay at their old idle and a 20k floor (campaignLegacyMinPrefixTokens, new per-arm test), and the kvcache suggester carries a NOTE that it still scores at 280 s / 20k. PR body no longer claims 270 beats 280.

Non-blocking leftovers: docs/how-to/kv-cache-keepalive-budget.md and docs/how-to/test-subscription-sandbox.md still show 280 / 20000 / 0.25 as example config (harmless, but they read as the defaults); dash/keepalive.go comments around the 20k analytic bucket still call 20k the shipped gate.

@OsherElhadad OsherElhadad added the ready-to-merge Program PR approved by the reviewer, CI green; needs maintainer approval label Oct 11, 2026
Osher-Elhadad added 3 commits October 11, 2026 18:36
… 80k floor, $0.50 ceiling)

Defaults move from 280 s / 20k / $0.25 to 270 s / 80k / $0.50 (K stays 2): config, the proxy
ceiling mirror, the keepalive-5m campaign arm (idle 270; shared 80k floor), the dashboard
replay gate and recommender. The 280 rationale in config.go is superseded. keepalive-5m-once
and stop-reason-gated stay at 280 (K=1 prefers 280; the gate loses). The kvcache simulator
keeps its 280 s schedule (W1 clock). Counterfactual; pending verification (#435).

Signed-off-by: Osher-Elhadad <[email protected]>
Replace the withdrawn 270-beats-280 comment with the cliff-robustness rationale; arm and
calc fallbacks, plugin settings, dashboard form defaults and the how-to now use 270 s /
80k / $0.50. once, stop-reason-gated and 1h campaign arms keep a 20k floor (80k evidence is
K=2 only). Note at the kvcache schedule constant that the simulator still scores at 280 s.

Signed-off-by: Osher-Elhadad <[email protected]>
@OsherElhadad
OsherElhadad force-pushed the feat/keepalive-recommended-defaults branch from 7c6595e to 4d29023 Compare October 11, 2026 18:38
@OsherElhadad

Copy link
Copy Markdown
Collaborator Author

Results: recommended keep-alive defaults

What it does. Makes the keep-alive policy that won the replay the default: ping when idle 270 s, at most 2 pings per idle span, only for prefixes of 80,000 tokens or more, and never above $0.50 per ping. Accounts that set their own values are unaffected.

Evidence (issue #446, counterfactual replay on 54 days of real traffic, independently verified):

  • Counterfactual net saving about $2.1K by the model, about $1.6-1.8K after live validation: 3.5-3.9% of the bill, against $0.33K for the deployed policy.
  • Live paired validation on 41 pauses matched the simulator: 31 vs 32 hits, 91 vs 92 pings.
  • 270 s vs 280 s is a tie on the full window; 270 is chosen because it is more robust to where the cache-TTL cliff actually falls.
  • The old $0.25 ceiling cost about $780 of savings in the replay.

What changed since review. Rebased on main (one test-file conflict, both tests kept). The two how-to docs now show 270 / 80000 / 0.50 as example config, and the comments in dash/keepalive.go no longer call 20k "the shipped gate". go test ./config ./kvcache ./proxy ./dash pass, gofmt clean.

Not deployed; deployment is for the owners.

@OsherElhadad

Copy link
Copy Markdown
Collaborator Author

Reviewer: APPROVAL UPDATED for the rebased head 4d29023 (earlier approval was for the old head). CI all pass; go test ./config ./proxy ./kvcache ./tenant and the full ./dash -count=1 (66 s) green; gofmt clean. Both TestKeepAliveRecommendedDefaults and the existing config test survived the rebase. The 280 / 20000 / 0.25 leftovers I listed are gone (docs examples and dash comments aligned; only the documented exceptions remain: once, stop-reason-gated and 1h arms, and the kvcache suggester with its NOTE). The PR body keeps the corrected 270-vs-280 statement (the +273 claim appears only as withdrawn).

@OsherElhadad
OsherElhadad merged commit 2f6bd96 into main Oct 11, 2026
6 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Oct 11, 2026
@OsherElhadad
OsherElhadad deleted the feat/keepalive-recommended-defaults branch October 11, 2026 20:53
amiddavid added a commit that referenced this pull request Oct 12, 2026
Lets turn_gate.py run against the production dashboard DB, which has no
transcripts and no exact per-thread turn index. Mirrors wait_causes.py's
own DB-only approach:

- Group real request rows (keepalive=0, status=200) by `thread_id` where
  the DB has that column, else by (session_id, model) as an approximate
  thread proxy. Pings never form a turn boundary.
- Turn index is 1-based row order within a group; also switched transcript
  mode to the same 1-based convention, so both modes report into the same
  turn buckets.
- Gaps use the previous request's own response-arrival time (ts +
  upstream_ms, wait_causes.py's approx_response_ts), not its raw request
  start, to the next request's start.
- Simulated ping cost is now gated by the keeper's 3 live checks (issue
  #444's defaults): a prefix floor (80,000 tokens), a minimum idle time
  before the first ping (270s), and a per-ping USD ceiling ($0.50) --
  configurable via --prefix-floor/--idle-sec/--ping-ceiling-usd. A pause
  that fails a gate is still counted, with zero simulated cost.
- Added real keep-alive ping attribution (keepalive=1 rows, priced from
  their own cost_usd) per turn bucket, reusing wait_causes.py's DBIndex,
  plus a USD column alongside the existing x-unit cost/avoided/net, and a
  gate-at-5/gate-at-10 net-change comparison against today's baseline.
- Extended --self-test with DB-only fixtures and gate coverage.

Assisted-By: Claude Opus 5.5 (1M context) <[email protected]>
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

ready-to-merge Program PR approved by the reviewer, CI green; needs maintainer approval

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant