Skip to content

server: deadline_s ends a reply at its deadline with every token and usage - #3438

Merged
ashhart merged 3 commits into
ashhart:mainfrom
jayleaton:lanes-up/01-request-deadline
Nov 21, 2026
Merged

ashhart merged 3 commits into
ashhart:mainfrom
jayleaton:lanes-up/01-request-deadline

Conversation

@jayleaton

@jayleaton jayleaton commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Before: a client that wants a reply bounded in time has to time out and disconnect (or call the cancel route from a
second connection); a disconnect loses the reply's usage and token ids. After: a request may carry deadline_s
(seconds, more than 0, at most 1,000,000) on chat and legacy completions. At that time the server cancels the engine
itself, keeps every token the engine gives before its finished event, and ends the reply with
finish_reason: "deadline", usage and token_ids/token_sha256. A reply that ends first, or a
cancel that came first, keeps its own ending: the deadline is marked only while the engine
has not finished, checked under the mailbox's mutex, so a slow client still draining a finished reply keeps stop or
length. Without the field nothing changes.

Checks on this head

  • Head commit: 79d08ea32 on base 6b493a3ef; every check below ran on this tree.
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 341 and 341, 0 new
    (tools/zig/lean_new.py; the 341 are inherited). python3 tools/pr_check.py --base <main> --pr 3438: 0 errors, 0 warnings.
  • Tests run, with the command and pass, fail and skip counts (aarch64 Linux, Zig 0.17.0):
    • zig build test-request-deadline-host: 30 pass, 0 fail, 0 skip as Zig counts the filtered run; 10 tests are new
      (5 through chat.run, 5 over HTTP through openai.run). The two slow-writer tests (an engine that ended before the
      50 ms deadline, read 100 ms late) fail on b14bde22a with the reply labelled deadline.
    • zig build test-server-cpu: 1470 of 1478 pass, 8 skip (checkpoints and libtfgrammar.so absent), 0 fail, with
      umask 022. With umask 002 one test on main fails the same way without this change:
      cli_config_test "serve --config: expand reads the file …" writes its file under the umask, and --config
      refuses a group-writable file (0664). This PR does not touch cli.zig or cli_config*.zig.
    • zig build check-native-cuda-host -Dsm=121: success.
    • zig build test-golden: 322 of 335 equal to the server goldens, 13 listed as known (main's own KNOWN-DIFFER
      list: tool streams, required tool choice and format without libtfgrammar.so), 0 unexpected; 55 of 55 equal.
    • Planted controls, each must fail: without the "deadline" label in finish, 3 tests fail; without the
      cancel-came-first guard, the new cancel test fails (deadline instead of cancelled); without the completion check
      (ended_first = false), both slow-writer tests fail (deadline instead of stop). All three fail as planted.
    • zig fmt --check build.zig zig and git diff --check: clean.
    • macOS: the macOS server test binary (metal_targets.zig's server_tests, which zig build test runs on a Mac)
      cross-built with -Dtarget=aarch64-macos from Linux through a local, uncommitted step: semantic analysis passes on
      the base and on this head, and the emitted build links to a Mach-O arm64 executable that holds the 10 new tests. It
      was not run. See "Not run".
  • CI: green on 79d08ea32: zig, zig-windows, zig-macos-dev-tools, pytest, release-paths, benchmark-clients
    (ubuntu and windows) and fmt pass; host-nightly-linux and host-nightly-macos skip on pull requests.
  • Not run, and why: no Mac here, so the server tests did not run on macOS (CI's zig-macos-dev-tools built the tf-*
    tools; this change touches no API they call); Windows ran only in CI (zig-windows). The diff adds no platform calls (the clock is the
    existing server/clock.zig).

Receipt

  • Main against branch: n/a: no engine, kernel, scheduler or sampling change; the field only decides when the server
    calls the engine's existing cancel. The host tests compare main's behaviour (no field) with a deadline the reply never
    meets: same reply, token sha and stream chunks, byte for byte.
  • Boxes it reaches: every server that serves chat or legacy completions, through chat.zig. The Responses and Ollama
    routes build their own chat body from listed fields and do not pass deadline_s on.
  • New switch? A request field, not a setting: deadline_s, default absent (no deadline), a number in (0, 1e6];
    anything else is refused with HTTP 400 before the engine. No profile row.
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: base
    6b493a3ef, head 79d08ea32, Zig 0.17.0, aarch64 Linux (GB10 host, CPU only, no GPU used), no checkpoint (fake
    engines in the tests).
  • Paths and platforms the change reaches, which I ran and which I could not: chat.run and openai.run, chat and
    completions, whole and streamed: ran on aarch64 Linux; macOS compiled and linked, not run; Windows through CI's zig-windows.
  • Exactness: tokens before the deadline are the engine's own, unchanged; a request without the field, or with a deadline
    it never meets, is byte-identical to main (test above). No token is changed or added.
  • Main against this branch, same machine and checkpoint: n/a, as above: nothing reaches inference.
  • Decode speed before and after: n/a: one null check per read-loop pass when the field is absent.
  • Prompt speed before and after: n/a: the change cannot reach prompt processing.
  • Licence of any new weights or drafter: n/a.

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

@jayleaton
jayleaton force-pushed the lanes-up/01-request-deadline branch from aac2b3f to f500b25 Compare November 15, 2026 15:21
@jayleaton
jayleaton force-pushed the lanes-up/01-request-deadline branch from f500b25 to b14bde2 Compare November 15, 2026 15:29
@jayleaton
jayleaton marked this pull request as ready for review November 15, 2026 15:53
@ashhart

ashhart commented Nov 15, 2026

Copy link
Copy Markdown
Owner

Thanks @jayleaton. The existing deadline tests pass, but there is one finish-reason race to fix.

At zig/src/server/chat_generation.zig:251, the deadline can fire while draining buffered tokens from an engine that already finished naturally. In a host reproducer, the engine ends before 50 ms, the first stream write takes 100 ms, and the complete abcde reply is labelled deadline instead of stop. Please check completion with the mailbox's synchronization before marking a deadline cancellation, preserve the earlier natural finish, and add this slow-writer case to the tests.

  • Synthena (Ash's Agent)

@jayleaton

Copy link
Copy Markdown
Contributor Author

Thanks, confirmed and fixed in 79d08ea32 (a new commit on top; no force-push).

  • The read loop now checks the mailbox's finished flag under the mailbox's own mutex before it marks a deadline. An
    engine that has already ended keeps its own finish reason (stop or length), and the server sends no cancel. A
    deadline is still marked only while the engine is running.
  • New tests in test-request-deadline-host:
    • request deadline: a slow writer draining a reply the engine already ended keeps finish stop and sends no cancel:
      your case through chat.run. The engine ends inside submit, the deadline is 50 ms and the first stream write takes
      100 ms. The reply is abcde, stop, and no cancel reaches the engine.
    • request deadline: HTTP a reply the engine ended before the deadline keeps finish stop when the server reads it late:
      through openai.run, for chat and completions, whole and streamed.
    • On the previous head (b14bde22a) both fail, with the reply labelled deadline.
  • Negative control: with the completion check dropped (ended_first = false), both new tests fail. The two earlier
    controls (no cancel-came-first guard, no "deadline" label) still fail as planted.
  • Same checks as before on the new head: test-request-deadline-host 30/30, test-server-cpu 1470/1478 (8 skip),
    test-golden 0 unexpected, check-native-cuda-host ok, lean 341 vs 341 (0 new), zig fmt --check clean, and the
    macOS server tests still cross-build and link for aarch64-macos.

— Jay Leaton

@ashhart

ashhart commented Nov 16, 2026

Copy link
Copy Markdown
Owner

@jayleaton, thank you. we applied the series to main and read it end to end: the field parses and refuses bad values before the
engine, the read loop cancels through the engine's existing cancel, and the finish reason is only overridden
when the deadline actually fired. The slow-writer race from the review is fixed the way you described - the
mailbox's finished flag is checked under its own mutex before a deadline is marked - and the new tests cover
it through both the chat path and HTTP, whole and streamed. We ran the request-deadline host suite here on an
x86 box and it is green, including those cases.

Nothing to change from us. One note for the record: the macOS server tests were compiled and linked but not
run, which you already flagged; the change reaches no API they call.

No hardware run is needed on our side - the change reaches no engine, kernel or backend, and the request-level
behaviour is exercised by the host suite. CI is green at this head. READY: it goes in the landing queue.

  • Synthena (Ash's Agent)

@jayleaton
jayleaton force-pushed the lanes-up/01-request-deadline branch 2 times, most recently from caed33c to c7c06c7 Compare November 17, 2026 21:48
@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thank you @jayleaton. The finish-reason race is fixed as described: the read loop checks the mailbox's finished flag under its own mutex, so a reply the engine already ended keeps stop or length and sends no cancel, while a deadline that really fired still ends the reply with every token, usage and ids. The slow-writer and late-read tests cover both the chat and HTTP paths.

No device run is needed. One thing before it lands: the zig-b job on this head fails three of the pinned serving hashes, which usually means a pinned source file changed and needs its re-pin in this PR. Please re-pin them, or tell us if you think the pins moved on main instead.

  • Synthena

@jayleaton

Copy link
Copy Markdown
Contributor Author

Thanks. I checked the three hashes, and I don't think any pin moved, in this PR or on main.

  • The three blocks in the zig-b log are namespaces.jsonl's recorded_prompt_sha256 pins for records 6, 67 and 97
    (b4cfdbdc…, d8e86eef…, 8b5aad89…). They are printed by DeepSeek chat: namespace pins reject unrelated prompt drift, which passes. That test appends "unrelated drift" to each record's qualified prompt and expects
    checkNamespacePin to fail, so expectEqualStrings prints one expected/found diff a record. The "found" values
    (97e8b46f…, 38139122…, 460ace0b…) are the SHA-256 of those drifted prompts. Zig shows that stderr under the
    binary's one failing test.
  • Recomputed from the fixtures: each pin equals the SHA-256 of its record's prompt in cases.jsonl.gz, and of the
    qualified prompt with the namespace undone. namespaces.jsonl, cases.jsonl.gz and deepseek_fixture_test.zig are
    the same at this PR's base (af9777b49), this head and current main; this PR doesn't touch them.
  • The job's one failure is benchmark_run_test "--drafter auto passes the family's drafter only when it is cached…",
    line 200: the --serial run exited 2 instead of 0. This PR doesn't touch the benchmark command, and the benchmark never
    sends deadline_s.
  • On this head (c7c06c7cb), on an M-series Mac (aarch64-macos, Zig 0.17.0), I built the server test binary
    filtered to --drafter auto, DeepSeek chat and request deadline:: 51 of 51 pass in each of 5 runs. Every run still
    prints the same three expected/found blocks, which shows they come from the passing drift test.

So I haven't re-pinned anything: re-pinning to the "found" values would make the drift check pass on drift. The head
stays c7c06c7cb. If you see the drafter test fail elsewhere, I'm happy to look at it in its own PR.

— Jay Leaton

@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thank you @jayleaton for tracing those three blocks. The pins are right, and re-pinning to the drifted values would break the drift check, so no change is needed there. The deadline finish-reason fix and tests are accepted.

One item remains: this head conflicts with current main in zig/src/server/chat.zig. We will rebase it onto current main ourselves; nothing is needed from you.

  • Synthena

@ashhart
ashhart force-pushed the lanes-up/01-request-deadline branch from c7c06c7 to 24c862e Compare November 19, 2026 07:53
@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thanks @jayleaton. As promised, we rebased this onto current main ourselves, keeping your commits as they were; nothing else changed. It is in the landing queue now, and nothing is needed from you.

  • Synthena

@AIFlowML

Copy link
Copy Markdown
Contributor

On exact head 9b13364b, zig build test-request-deadline-host -Dcpu=apple_m1 -j5 --summary all passed 33/33 on M5 Max (Zig 0.17.0). Current main 6fb427eb has one conflict at the Generation initializer in chat.zig. The scratch resolution kept main's .required initializer and appended .deadline_ns = prepared.deadline_ns. I tested against 328a566e; the two trial trees differ only in the changelog.

After that resolution, test-request-deadline-host passed 34/34, test-tool-required-host passed 33/33, and test-server-cpu reported 1,684 passes, 15 skips and zero failures using the same build flags. The focused suites cover the two sides of the merge, including the slow-writer deadline case and required calls. I ran these on the host without a model or a GPU workload.

Igor Lessio - AIFLOW LABS

@AIFlowML

AIFlowML commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

I reran the deadline checks on the exact #4276 train tip 59f88df6a, with #3718 stacked after #3438. M5 Max, Zig 0.17.0.

zig build test-request-deadline-host test-tool-required-host test-server-cpu -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j3 --summary all

Exit 0. The deadline target passed 34/34, required tools passed 33/33, and server CPU passed 1,723 with 15 skipped. The host test log includes warnings for the optional grammar dylib. This covers the combined train, whose automatic PR jobs were skipped.

The manually dispatched No GPU workflow has since completed successfully on the same train SHA.

I also trial-merged the same train into 37fa51d23. The only conflict was CHANGELOG.md; the scratch build kept main's copy. All 195 focused tests passed, including the five H3 resident-server tests, and the H3 server executable built.

zig build test-request-deadline-host test-tool-required-host test-h3-image-prompt test-h3-geometry test-h3-boundaries test-h3-image-server tf-h3-image-server -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j3 --summary all

I also merged the published deadline head 9c91113d9 into the tested #3718 restack on 5f53c445e. That combination was clean and added only the deadline changes. The seven focused targets above passed all 195 tests, and a separate zig build test-server-cpu -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j4 --summary all passed 1,723 with 15 skips on M5 Max. The combined tree also trial-merges cleanly into the newer 3025ce9d2. Its server source files are unchanged by that trial.

Igor Lessio - AIFLOW LABS

jayleaton and others added 3 commits November 21, 2026 06:23
…usage

A request may name "deadline_s" (seconds, more than 0, at most 1000000), for
chat and legacy completions. At that time the server cancels the engine
itself, still commits the tokens the engine gives before its finished event,
and labels the reply finish_reason "deadline". The final response keeps
usage and token_ids/token_sha256. A token the engine gives after the
deadline cancel may be EOS, hit a stop string or reach max_tokens; the
reply still ends "deadline". A deadline that passes after the reply already
ended (a stop string's own cancel) is not counted. Without the field
nothing changes.

A cancel from POST /v1/requests/{id}/cancel that came first keeps the
reply "cancelled": the deadline then sends no second cancel and never
relabels it. docs/api.md lists the field.

test-request-deadline-host: through chat.run and through openai.run (chat
and completions, whole and streamed): a forced deadline ends the reply with
finish deadline, the late token, usage and ids; no field and a deadline the
reply never meets give the same reply, token sha and stream choices; 0, -1,
"300", true and 1e7 are refused before the engine. A cancel ask before the
deadline, with an engine that ends late, still ends "cancelled".
…adline passes while it drains

A slow client could still be writing out tokens the engine had already
finished with (stop or length) when deadline_s passed; the server then sent
a cancel to the finished request and labelled the whole reply "deadline".
Before marking a deadline, the read loop now checks the mailbox's finished
flag under the mailbox's own mutex: an engine that ended first keeps its
finish reason and gets no cancel.

test-request-deadline-host: the engine ends inside submit, before the
50 ms deadline, and the first stream write takes 100 ms: the reply ends
"stop" with abcde and no cancel is sent (chat.run); over HTTP, for chat and
completions, whole and streamed, a reply the engine ended before the
deadline and the server first reads after it keeps "stop".
@ashhart
ashhart force-pushed the lanes-up/01-request-deadline branch from 9c91113 to 2e79a2c Compare November 21, 2026 06:23
@ashhart
ashhart merged commit 0f30268 into ashhart:main Nov 21, 2026
2 of 11 checks passed
@ashhart

ashhart commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Thank you @jayleaton. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants