-
Notifications
You must be signed in to change notification settings - Fork 213
feat(pflash): adaptive keep_ratio bandit MVP #264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
davide221
merged 2 commits into
Luce-Org:main
from
dusterbloom:feat/pflash-mvp-adaptive-keep
May 27, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| Answer these logic puzzles. End your answer with OK_DONE. | ||
|
|
||
| 1. If all roses are flowers and some flowers fade quickly, can we conclude that some roses fade quickly? | ||
| 2. A bat and a ball cost $1.10 in total. The bat costs $1.00 more than the ball. How much does the ball cost? | ||
| 3. If you have a 3-litre jug and a 5-litre jug, how can you measure exactly 4 litres of water? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| Solve the following math problems. End your answer with OK_DONE. | ||
|
|
||
| 1. What is 17 * 23? | ||
| 2. What is the sum of the first 10 prime numbers? | ||
| 3. If a rectangle has width 7 and height 11, what is its area? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| #!/usr/bin/env python3 | ||
| """Thin proxy that injects extra_body.session_id into /v1/messages requests. | ||
|
|
||
| Run between the claude CLI and the dflash server when PFLASH_SESSION_ID is set. | ||
| All other paths and methods are forwarded verbatim. | ||
|
|
||
| Usage: | ||
| python3 session_inject_proxy.py \\ | ||
| --host 127.0.0.1 --port 18081 \\ | ||
| --upstream http://127.0.0.1:18080 \\ | ||
| --session-id <id> | ||
|
|
||
| The proxy listens on --port and forwards to --upstream, injecting | ||
| extra_body.session_id on every POST /v1/messages request. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import json | ||
| import os | ||
| import socket | ||
| import threading | ||
| from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer | ||
| from urllib.parse import urlparse | ||
| import http.client | ||
|
|
||
|
|
||
| class Handler(BaseHTTPRequestHandler): | ||
| upstream: str = "" | ||
| session_id: str = "" | ||
|
|
||
| def log_message(self, fmt, *args): | ||
| print("[session-proxy] %s" % (fmt % args), flush=True) | ||
|
|
||
| def _upstream_conn(self) -> tuple[http.client.HTTPConnection, str]: | ||
| url = urlparse(self.upstream) | ||
| port = url.port or (443 if url.scheme == "https" else 80) | ||
| cls = http.client.HTTPSConnection if url.scheme == "https" else http.client.HTTPConnection | ||
| return cls(url.hostname, port, timeout=900), url.path.rstrip("/") | ||
|
|
||
| def _forward_raw(self, body: bytes): | ||
| """Forward request verbatim (no injection needed).""" | ||
| conn, base = self._upstream_conn() | ||
| headers = { | ||
| k: v for k, v in self.headers.items() | ||
| if k.lower() not in ("host", "content-length", "transfer-encoding") | ||
| } | ||
| headers["Content-Length"] = str(len(body)) | ||
| conn.request(self.command, base + self.path, body, headers) | ||
| resp = conn.getresponse() | ||
| self._relay_response(resp) | ||
|
|
||
| def _relay_response(self, resp: http.client.HTTPResponse): | ||
| """Relay upstream response back to client, handling SSE streaming.""" | ||
| content_type = resp.getheader("Content-Type", "") | ||
| is_sse = "text/event-stream" in content_type | ||
|
|
||
| self.send_response(resp.status) | ||
| skip_headers = {"transfer-encoding", "content-length"} | ||
| for k, v in resp.getheaders(): | ||
| if k.lower() not in skip_headers: | ||
| self.send_header(k, v) | ||
|
|
||
| if is_sse: | ||
| self.send_header("Transfer-Encoding", "chunked") | ||
| self.end_headers() | ||
| # Stream chunk by chunk | ||
| while True: | ||
| chunk = resp.read(4096) | ||
| if not chunk: | ||
| # Write terminal chunk | ||
| self.wfile.write(b"0\r\n\r\n") | ||
| self.wfile.flush() | ||
| break | ||
| size = "%X\r\n" % len(chunk) | ||
| self.wfile.write(size.encode("ascii")) | ||
| self.wfile.write(chunk) | ||
| self.wfile.write(b"\r\n") | ||
| self.wfile.flush() | ||
| else: | ||
| data = resp.read() | ||
| self.send_header("Content-Length", str(len(data))) | ||
| self.end_headers() | ||
| self.wfile.write(data) | ||
|
|
||
| def _read_body(self) -> bytes: | ||
| n = int(self.headers.get("Content-Length", "0")) | ||
| if n <= 0: | ||
| return b"" | ||
| return self.rfile.read(n) | ||
|
|
||
| def do_GET(self): | ||
| conn, base = self._upstream_conn() | ||
| headers = {k: v for k, v in self.headers.items() if k.lower() != "host"} | ||
| conn.request("GET", base + self.path, None, headers) | ||
| resp = conn.getresponse() | ||
| self._relay_response(resp) | ||
|
|
||
| def do_POST(self): | ||
| body = self._read_body() | ||
| path = self.path | ||
|
|
||
| # Inject session_id only on /v1/messages | ||
| if self.session_id and path.startswith("/v1/messages"): | ||
| try: | ||
| obj = json.loads(body.decode("utf-8")) | ||
| if "extra_body" not in obj: | ||
| obj["extra_body"] = {} | ||
| if "session_id" not in obj["extra_body"]: | ||
| obj["extra_body"]["session_id"] = self.session_id | ||
| body = json.dumps(obj).encode("utf-8") | ||
| except Exception as exc: | ||
| print(f"[session-proxy] JSON parse error, forwarding raw: {exc}", flush=True) | ||
|
|
||
| self._forward_raw(body) | ||
|
|
||
|
|
||
| def main(): | ||
| ap = argparse.ArgumentParser() | ||
| ap.add_argument("--host", default="127.0.0.1") | ||
| ap.add_argument("--port", type=int, default=18081) | ||
| ap.add_argument("--upstream", default="http://127.0.0.1:18080") | ||
| ap.add_argument("--session-id", default=os.environ.get("PFLASH_SESSION_ID", "")) | ||
| args = ap.parse_args() | ||
|
|
||
| if not args.session_id: | ||
| print("[session-proxy] WARNING: no session_id set; proxy is pass-through only", flush=True) | ||
|
|
||
| Handler.upstream = args.upstream.rstrip("/") | ||
| Handler.session_id = args.session_id | ||
|
|
||
| srv = ThreadingHTTPServer((args.host, args.port), Handler) | ||
| print( | ||
| f"[session-proxy] listening on http://{args.host}:{args.port} " | ||
| f"-> {Handler.upstream} " | ||
| f"(session_id={Handler.session_id!r})", | ||
| flush=True, | ||
| ) | ||
| srv.serve_forever() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.