feat(pflash): adaptive keep_ratio bandit MVP

Merged
DU
6mo ago
+1050-13

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/)

FileLoCWhat
server/src/server/adaptive_keep_ratio.h+116Bandit state machine (step_adaptive_keep_ratio + LRU-bounded HttpServerSessions, cap 1024)
server/src/server/http_server.{cpp,h}+54Parse session_id, call bandit, inject accept_rate into chat/anthropic/responses usage objects
server/src/qwen35/qwen35_backend.{cpp,h}+13Plumb accept_rate + spec_decode_ran out of do_spec_decode
server/src/common/model_backend.h+5accept_rate / spec_decode_ran fields on GenerateResult
server/test/test_adaptive_keep_ratio.cpp+239State machine: convergence, clamping, EMA, LRU eviction
server/test/test_bandit_integration.cpp+200HTTP-shaped lifecycle, non-string session_id guards
server/test/test_server_unit.cpp+82ParsedRequest::session_id + accept_rate usage-object contract
server/CMakeLists.txt+12Register the 3 new test targets
.github/workflows/ci.yml+1Submodule PAT fix for fork PRs
harness/clients/run_claude_code.sh+41/-2Wire PFLASH_SESSION_ID env into request body
harness/clients/session_inject_proxy.py+144Local proxy that injects extra_body.session_id into requests
harness/clients/prompts/{logic,math}_check.txt+10Multi-turn test prompts
thoughts/2026-05-21_pflash_mvp_plan.md+129Design doc (mrciffa-scope MVP plan)

All bandit logic lives in the C++ HTTP server. The Python server is deprecated and untouched.

Behavior matrix

Requestmainthis PR
no session_idfixed keep_ratioidentical
extra_body.session_id="…"unknown field, ignoredper-session bandit keep_ratio
pflash_mode=offno compressionno 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

  1. extra_body.session_id vs top-level session_id — kept both, prefer one?
  2. kBanditStepLarge = 0.01 — too slow for bad priors?
  3. 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.