Skip to content

serve: send plain steps before the round's drafted passes - #731

Merged
juliankang4 merged 3 commits into
midagedev:mainfrom
juliankang4:fix-serve-step-token-order
Oct 23, 2026
Merged

juliankang4 merged 3 commits into
midagedev:mainfrom
juliankang4:fix-serve-step-token-order

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

여러 요청이 함께 돌 때, 일반 step을 마친 요청의 토큰과 최종 응답이 다른 요청의 draft 호출을 기다리지 않고 바로 클라이언트에 전달되도록 고치는 수정입니다.

Part of #565. Tracked in #699.

What and why

When a round mixes plain steps with drafted advances, a plain request's new token did not reach its client until every drafted advance of the round had finished. A plain request that finished in that step had its final frame held for the same time. The plain slot's answer was already computed, so the wait was pure delay for that client.

Worker::run built the step rows and the advance rows together, called the engine for the steps, then for the drafted rows, and only after both calls applied the results (Gen::stepped, Gen::advanced) and pumped them out. This order came in with 3c5a133. Nothing in the drafted advance depends on the step's delivery, so the order was not needed.

This PR applies and delivers the step results right after the step call, and then calls the drafted advances:

  • The step tokens go through Gen::stepped and the existing pump_ready before the advance call. A pending Advance is kept in the request's existing Active.need, so the next call a Fill saved with hold_need is untouched.
  • Delivering can end a request and remove it from the active list. The advance rows and the index of their results are therefore built after delivery, from the requests that remain. The call order and the row order stay as before.
  • If an orderly stop arrives while delivering, the remaining advances are not called and the loop takes its existing halt path.
  • The engine call count, the token order and the number of result buffers are unchanged. A single batched engine call still returns all its rows together, so no per-row callback was added.

If a drafted request errors after a plain request has already sent its final frame, that final frame stays sent. The requests still running end through the existing fatal path.

No public type, function signature, GPU code, kernel, launch or checkpoint policy changes. The change is in the request loop, above any rank split, so the rank count is not involved.

Release notes: Fixed: a plain request in a round that also drafts gets its token and final response without waiting for the other slots' drafted advances.

Code shape

  • No second copy. The change reorders the existing Worker::step path around pump_ready; it adds no helper. The new test reuses DraftGated, EntryLog, read_stream and start_n.
  • Model code holds only architecture facts. No model code changes.
  • Dead code is gone. Nothing was made unused.
  • No silent failure. No new Result, .ok() or fallback. Engine call errors still go to the existing die path.
  • Comments state what is true now. The one new comment says that delivering can move the remaining entries.
  • 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. cargo fmt --check -p bloomery-serve is clean. Clippy has not been run on this head.

Duplication found or removed (path:line), or "none": none; the duplicate scan found no candidate.

What I ran

Hardware: a Linux host without a GPU. The new test and the serve tests use the in-process DraftMock engine (4,096 positions), so no model file and no GPU are involved. The change does not touch GPU code, so there is no nvidia-smi output to show.

FAIL-first: the new test hw_plain_steps_reach_clients_before_drafted_passes (crates/serve/tests/slots/mod.rs) against the base worker.rs, with the new test file kept:

bash tools/gate.sh -p bloomery-serve --test serve slots::hw_plain_steps_reach_clients_before_drafted_passes -- --ignored --nocapture
test slots::hw_plain_steps_reach_clients_before_drafted_passes ... FAILED
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 101 filtered out; finished in 10.11s
FAIL-FIRST PASS: main test rc=101, expected delivery assertion failed

On the base, at N = 2, finish = false, the plain request's token waited behind the held draft advance, which is the delivery assertion the test makes.

With the fix, the serve library and integration tests, including ignored ones:

bash tools/gate.sh -p bloomery-serve --lib --test serve -- --include-ignored --nocapture
test result: ok. 345 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 6.12s
test result: ok. 102 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 3.26s

The new test covers four cases: two and four slots, each with a plain request that continues and one that finishes. Half the requests are plain and half draft. The draft advance is held once every prompt has entered the round. A continuing plain request must report its new predicted_n, and a finishing one must send its final stream frame, before the advance is released. After the release, every request's final tokens and its joined stream content must equal those of the same request run alone on a fresh server.

Both runs were on the same head and base: head 81b4d9a397ea62ab43293dc546cfadc0d60c44f9 on e14cd4643fcd210fb55c33e1c1984ae9af9be4dc.

On the Mac: cargo fmt --check -p bloomery-serve and git diff --check upstream/main...HEAD returned 0, and the working tree was clean.

Not run: just gate-* recipes beyond the serve gate above, clippy, and the other static checks.

Reproduction

bash tools/gate.sh -p bloomery-serve --test serve slots::hw_plain_steps_reach_clients_before_drafted_passes -- --ignored --nocapture
bash tools/gate.sh -p bloomery-serve --lib --test serve -- --include-ignored --nocapture

To see the base behaviour, run the first command with crates/serve/src/worker.rs taken from the base commit.

What I ran into

This does not block the PR.

  • This is an ordering fix. I did not measure throughput or time per token on a real model, and I do not claim a speed gain. The delay it removes is the remaining time of the drafted advance in a mixed round.
  • Other call sites in Worker already deliver before the next slot's call: the start and the last prompt call pump before the next slot call, and a Fill saves its next call with hold_need after each call. The single-slot and turns paths deliver after one call. Those paths needed no change.
  • A cache seal still begins after the final event, which is a separate contract and is not reordered here.

AI help

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

@github-actions github-actions Bot added the serve The HTTP server: crates/serve label Oct 23, 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: CLEAN: No behavior or proven quality defect remains in the changed code.

Non-blocking

⚠ Refresh the gate evidence: crates/serve/tests/slots/mod.rs:1177 adds a second regression test, but the PR's run logs name head 81b4d9a, not the reviewed head 1d2c67f.
Run bash tools/gate.sh -p bloomery-serve --lib --test serve -- --include-ignored --nocapture on this head and attach the output.
Also attach FAIL-first evidence for the reasoning-round test. This verifies the final ordering guard before merge.

⚠ Supply the dispatch timing comparison: crates/serve/src/worker.rs:881 adds a pump traversal in each mixed round, and line 891 adds a scan visiting at most N active entries per round, where N is the active request count.
Engine calls and result-buffer counts stay unchanged. This is a host dispatch reorder; AGENTS.md's dispatch rule asks for a same-lease A/B.
Attach a base/head comparison that exercises mixed plain and drafted requests, so the host work before drafted calls is measured.

Checked

Duplicates: 3 new functions/methods checked; no duplicated owner. Dryer reported 0 pairs.
Complexity: crapper flagged Worker::step, 26 → 30; branches added to an existing function for early delivery and result handling. Pending calls reuse Active.need.
Silent failure: 2 engine-result guards followed to die; errors remain visible to running requests.
Tests: static mutation check—dropping worker.rs:881 fails the first delivery assertion; admitting newly eligible advances at worker.rs:889 fails the second test's call-order assertions.
Gates: two ignored hw_ tests added; no pins, thresholds or existing cases moved.
Adversarial checks: removal of a finishing plain request preserves advance mapping; saved Fill needs survive pumping; resumed reasoning waits until the next round; stop skips the pending advances; draft errors reach the remaining requests.
Performance: decode dispatch; the additional traversals are quantified above. Engine calls and result-buffer counts remain unchanged.
Escape catalog: C1–C4 do not match.

Notes

Static review only; tests were not executed here. The PR carries no qualifying GPU evidence.

🔨 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 24b4d57 into midagedev:main Oct 23, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

serve The HTTP server: crates/serve

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant