Skip to content

serve: a key file line sets its key's scope and its in-flight and rate limits (403, 429 with Retry-After) - #1003

Merged
juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-usab-key-admission
Oct 30, 2026
Merged

juliankang4 merged 1 commit into
midagedev:mainfrom
juliankang4:dgx-spark-q38-usab-key-admission

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

API 키 파일의 한 줄에서 키별 접근 범위(scope)와 동시 요청 수, 요청 속도 제한을 정할 수 있게 했습니다.

What and why

--api-key-file keys all reach every route and run unlimited. A key file line can now narrow its key with fields after it: sk-app scope=inference inflight=2 rate=30/m burst=5.

  • scope= takes inference, read or admin (comma-separated). A route outside the key's scope answers 403 (permission_error).
  • inflight=N caps the requests the key has answered at once. A request keeps its place until its answer is fully written and the engine has left its generation, also after a cancel or a disconnect.
  • rate=N/s or rate=N/m with burst=B is a per-key request bucket.
  • A request past either limit answers 429 (rate_limit_error) with Retry-After in whole seconds and starts nothing.
  • A bad field, a repeated field or a key repeated with other fields refuses the start, naming the file, the line and the field, never the key.

A key with no fields behaves as before. docs/serving.md documents the fields.

Release notes: Added: a key file line can set a key's scope and its in-flight and rate limits; a request past a limit answers 429 with Retry-After, and a route outside the scope answers 403.

Code shape

  • No second copy. The limit counters live in the new crates/serve/src/admit.rs; the HTTP writer, the SSE stream and the background path share one lease on it instead of counting separately.
  • Model code holds only architecture facts. No model file changed.
  • Dead code is gone. Nothing is left unused.
  • No silent failure. Undefined key file fields are named errors that refuse the start.
  • Comments state what is true now.
  • No relaxed gate. No band, tolerance or lint moved.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up.

Duplication found or removed (path:line): none.

What I ran

On a DGX Spark (GB10), a real bloomery-serve built with cargo oxide build --arch sm_121a -- -p bloomery-gpu-gates --release --bin bloomery-serve served nvidia/Qwen3.8-Flash-Next-FP4-Local with a key file that sets scope=, inflight= and rate= on separate keys:

./target/release/bloomery-serve -m <model> --host 127.0.0.1 --port 18743 --ctx-size 2048 --api-key-file <keys>
HTTP sk-look POST /v1/chat/completions -> 403
SCOPE PASS
HTTP sk-gen POST /v1/chat/completions -> 429 Retry-After 1
INFLIGHT PASS
HTTP sk-slow POST /v1/chat/completions -> 429 Retry-After 60
RATE PASS
KEY-ADMISSION PASS

A key file with a field the line does not take exits with a usage error that names the line and the field. The scope=read key reads GET /props (200) and the scope=inference key gets 403 there and on POST /shutdown.

The whole serve library passes:

cargo test --locked -p bloomery-serve --lib -- --nocapture --test-threads=1
test result: ok. 451 passed; 0 failed

With the fix reverted, a route the decision server seats never answered 429:

cargo test --locked -p bloomery-serve --lib decide::tests::a_seated_route_takes_its_key_place -- --nocapture --test-threads=1
decide.rs:1075 /v1/responses/job/cancel: no answer within the bound

AI help

AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.

@github-actions github-actions Bot added documentation Improvements or additions to documentation serve The HTTP server: crates/serve labels Oct 30, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: MUST_FIX: the new socket test duplicates an existing HTTP response reader.

Must fix

1. Reuse the HTTP test-response reader

crates/serve/src/api.rs:6935 adds HTTP head/body separation and status parsing already owned by crates/serve/src/http.rs:469. This is a kind B duplication defect under R14.
Reusing that owner removes the second parser without adding an abstraction. Replace lines 6935–6943 with the following; the socket timeout remains, and the EOF assertion preserves the connection-close check:

let mut read = BufReader::new(&mut s);
let (status, head, body) = crate::http::read_test_response(&mut read);
assert_eq!(read.read(&mut [0]).expect("the connection closes"), 0);
(status, head, String::from_utf8(body).expect("a UTF-8 response"))

Checked

Duplication: both cited readers were inspected; the new parser is on added lines. Dryer reported no pairs.
Fix: BufReader and Read are already in scope. The requests use HTTP/1.0, and the response writer declares Content-Length. The existing helper reads that body; the final read checks EOF with the original timeout.
Diagnostics: Limits::parse is new at complexity 17; ApiKeys::add rose 7→12; keep_alive rose 20→24. These measurements do not establish a separate finding.
Scope: this is a full review, not a revert or a no-merge mirror. No ledger lessons apply.

Notes

Tests were not executed during validation. The PR's card report lacks the nvidia-smi line, both commit hashes and just gate-* commands with output, so GPU evidence is incomplete.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

…e limits (403, 429 with Retry-After)

A key file line may carry fields after its key: scope=inference|read|admin limits the routes the key reaches (a route outside them is a 403 permission_error), inflight=N caps the key's requests answered at once, and rate=N/s or N/m with burst=B is a bucket each request takes one from. A request past a limit is a 429 rate_limit_error with Retry-After in whole seconds, and the connection carries the next request. Each key counts apart; a background response holds its key's place until its generation ends. A key with no fields reaches every route unlimited, as before, and a field the line does not take refuses the start by line and place, never echoing the key.
@juliankang4
juliankang4 force-pushed the dgx-spark-q38-usab-key-admission branch from 7a14f5f to b2e4bef Compare October 30, 2026 06:23

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since the last review: the response-parser duplication is fixed at crates/serve/src/api.rs:6935. The decision-server test also reuses its existing response reader.

Verdict: CLEAN: the interdiff resolves the previous finding and introduces no proven defect.

Checked

Duplicates: 0 new definitions in the interdiff; both replacements reuse existing owners. Dryer reported 0 pairs.
Complexity: full-PR diagnostics report new Limits::parse (17), and branch growth in ApiKeys::add (7→12) and keep_alive (20→24). These functions are unchanged by the interdiff.
Silent failure: 0 added suppression sites. Unparseable status codes, invalid UTF-8 bodies and truncated bodies still fail the tests.
Tests: static inspection of flag.rs:274 and api.rs:1495 confirms that bypassing scope refusal contradicts the 403 assertion, and dropping the seated-route condition sends the second request into the held decider instead of returning 429. The revised readers preserve these checks.
Gates: no thresholds or explicit gate assertions removed or relaxed in the interdiff.
Adversarial checks: truncated bodies fail read_exact; a missing API connection close fails the bounded EOF check. The tested HTTP/1.0 responses declare Content-Length and Connection: close, as the reused readers require.
Escape catalog: no patterns match. No production hot path changes in the interdiff.

Notes

Tests were not executed during review or validation. The PR report lacks the nvidia-smi line, both commit hashes and just gate-* commands with output; GPU evidence is incomplete.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

@juliankang4

Copy link
Copy Markdown
Collaborator Author

The review is done and the contribution check passed, so I'm merging this.

@juliankang4
juliankang4 merged commit 556e99e into midagedev:main Oct 30, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation serve The HTTP server: crates/serve

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant