feat(pflash): adaptive keep_ratio bandit MVP
Summary
Replaces the fixed keep_ratio PFlash knob with a per-session ε-greedy bandit that adjusts compression based on observed accept_rate. Opt-in — clients without session_id get the existing fixed-keep path, byte-identical to main.
Source changes (1,043 LoC, 17 files, all under server/ + harness/ + thoughts/)
| File | LoC | What |
|---|---|---|
server/src/server/adaptive_keep_ratio.h | +116 | Bandit state machine (step_adaptive_keep_ratio + LRU-bounded HttpServerSessions, cap 1024) |
server/src/server/http_server.{cpp,h} | +54 | Parse session_id, call bandit, inject accept_rate into chat/anthropic/responses usage objects |
server/src/qwen35/qwen35_backend.{cpp,h} | +13 | Plumb accept_rate + spec_decode_ran out of do_spec_decode |
server/src/common/model_backend.h | +5 | accept_rate / spec_decode_ran fields on GenerateResult |
server/test/test_adaptive_keep_ratio.cpp | +239 | State machine: convergence, clamping, EMA, LRU eviction |
server/test/test_bandit_integration.cpp | +200 | HTTP-shaped lifecycle, non-string session_id guards |
server/test/test_server_unit.cpp | +82 | ParsedRequest::session_id + accept_rate usage-object contract |
server/CMakeLists.txt | +12 | Register the 3 new test targets |
.github/workflows/ci.yml | +1 | Submodule PAT fix for fork PRs |
harness/clients/run_claude_code.sh | +41/-2 | Wire PFLASH_SESSION_ID env into request body |
harness/clients/session_inject_proxy.py | +144 | Local proxy that injects extra_body.session_id into requests |
harness/clients/prompts/{logic,math}_check.txt | +10 | Multi-turn test prompts |
thoughts/2026-05-21_pflash_mvp_plan.md | +129 | Design doc (mrciffa-scope MVP plan) |
All bandit logic lives in the C++ HTTP server. The Python server is deprecated and untouched.
Behavior matrix
| Request | main | this PR |
|---|---|---|
no session_id | fixed keep_ratio | identical |
extra_body.session_id="…" | unknown field, ignored | per-session bandit keep_ratio |
pflash_mode=off | no compression | no compression (bandit not consulted) |
Evidence
5-turn adaptive trajectory (keep moves 0.10 → 0.15 as bandit responds to 6.2% accept), NIAH @ 16K/32K at 5/5 with ~1.9× faster prefill at keep=0.10, and Day-5 A/B/C (claude_code single-shot) showing the bandit beating both fixed arms (16s wall, 31.9% accept, OK_DONE). Bench scripts + result artifacts moved to a follow-up PR to keep this one focused on the C++ feature.
Open questions for reviewers
extra_body.session_idvs top-levelsession_id— kept both, prefer one?kBanditStepLarge = 0.01— too slow for bad priors?- EMA α direction (slow-prior weighting) — confirm intent.
History
This PR was rebased onto the dflash → server rename on main and squashed from 19 commits into one feature commit. Dropped:
- The Bug #42 tail-capture guard commit (
58a9bef) — moved to PR #274 - The ee3 accept_rate bench commit (
861c5e2) — separate concern - ~3,000 LoC of bench result artifacts (
bench/results/...) and bench scripts — split to a follow-up PR
Backup of the pre-rebase head: 861c5e2 (saved locally for rollback if needed).
CI note
Both CI jobs on this PR fail at the final uv sync --frozen step with Distribution not found at .../dflash (or .../pflash). Root cause is on main: the uv.lock was not regenerated after the rename commit. C++ build, pytest server suite (67/67), and ctest all pass. Not a PR-side issue.