Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Releases of TandemEngine, TandemLLM's inference engine (TandemLLM up to 0.15.4;

## Unreleased

### Server

- A completion response carries the answer's own id on its headers: `X-Tandem-Request-Id: chatcmpl-...`, the same id the body carries (SRV-733). The id is made when the request is accepted, but on a stream it reached the client only with the first frame, after prefill, so TandemApp's Live view could not join its prefill-phase rows to the engine's live rows while the answer was still prefilling. The header is additive: every response is otherwise the one it was, and only responses to completion requests carry it.

### Fixes

- The 27B no longer stops answering after a short chat follows a long one (SRV-885). The end of a request could lower the draft window's floor and leave the rows between the new floor and the old one unmapped. The next save of that conversation to the disk read them, the GPU faulted (an illegal memory access) and every request after it was a 503 `engine_failed` until a restart. A lowered floor now leaves those rows on the window's shared page until they are mapped again, and a trim drops only what lies past the rows it keeps.
Expand Down
10 changes: 10 additions & 0 deletions server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8405,6 +8405,7 @@ class Handler(BaseHTTPRequestHandler):
disable_nagle_algorithm = True
_status: int | None = None
_tandem: str | None = None # the conductor mode of the completion in hand
_request_id: str | None = None # the answer id of the completion in hand (SRV-733)
_images_left_out = 0 # the completion's earlier images left out (server/images.py)

def send_response(self, code, message=None):
Expand All @@ -8415,6 +8416,12 @@ def send_response(self, code, message=None):
# every completion response says which mode it ran in, so a bench client can
# assert it was served raw (docs/server.md, "The conductor's session monitor")
self.send_header("X-Tandem", self._tandem)
if self._request_id is not None:
# SRV-733: the answer's own id, made at accept time, on the headers of every
# response to its request -- a stream's first frame carries it only after
# prefill, and the TandemApp controller joins its prefill-phase rows to this
# request's live rows by it (the engine row's request_id)
self.send_header("X-Tandem-Request-Id", self._request_id)
if self._images_left_out:
self.send_header("X-Tandem-Images-Left-Out", str(self._images_left_out))

Expand Down Expand Up @@ -9513,6 +9520,7 @@ def _strip_session(self) -> bool:

def do_GET(self):
self._tandem = None
self._request_id = None # a keep-alive socket's handler is reused
if self._strip_session():
return
if self._chat("GET"):
Expand Down Expand Up @@ -9794,6 +9802,7 @@ def _not_here(self, path: str, why: str = "this engine runs without an extension

def do_POST(self):
self._tandem = None
self._request_id = None # a keep-alive socket's handler is reused
if self._strip_session():
return
if self._chat("POST"): # before the JSON read: uploads are multipart
Expand Down Expand Up @@ -10070,6 +10079,7 @@ def _complete(self, body: dict, chat: bool) -> None:
self._early, self._early_body, self._bar = None, False, None
t_req = time.perf_counter()
cid = ("chatcmpl-" if chat else "cmpl-") + uuid.uuid4().hex[:24]
self._request_id = cid # SRV-733: on the response headers from here on
rec = usage_mod.RequestRecord(cid, "chat" if chat else "completions",
bool(body.get("stream")), t_arrival=t_req)
rec.usage_context = STATE.get("usage_context", "client")
Expand Down
44 changes: 44 additions & 0 deletions tests/test_app_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* nor does the role chunk, thinking on or off: it goes out with the first text;
* a streamed stop string never reaches the client, not even the part of it that arrives before
the match, and a streamed answer with `stop` is the non-streamed one;
* the completion response carries the answer's id at stream open: `X-Tandem-Request-Id`,
the same id the body carries, and nothing that is not a completion carries it;
* the `both` format closes the `<think>` it opens in `content`;
* the `[req]` line carries the loop's own block count, decode time and first-miss histogram,
for the chain and the tree, and a request that never generated carries none;
Expand Down Expand Up @@ -314,6 +316,48 @@ def test_the_stop_header_is_non_streaming_only_and_the_stream_says_it_in_band():
app.PatternStop = real


def _request_id_of(head):
got = [v for k, v in (line.split(": ", 1) for line in head.splitlines()[1:])
if k.lower() == "x-tandem-request-id"]
assert len(got) == 1, head
return got[0]


def test_the_completion_response_carries_the_answer_id_at_stream_open():
"""SRV-733: the TandemApp controller joins its prefill-phase rows to the engine's live
rows by the answer's id -- the engine row's request_id -- and on a stream that id only
reached the client with the first frame, after prefill. Every response to a completion
request now carries it at stream open: `X-Tandem-Request-Id`, the same id the body
carries; a response to anything else carries no answer id."""
def first_frame_id(raw):
return next(json.loads(line[6:]) for line in raw.splitlines()
if line.startswith("data: {"))["id"]
serve()
app.STATE["tok"] = CharTok()
real = app.generate_stream
app.generate_stream = lambda *a, **k: iter([ord(c) for c in "done"])
try:
chat = {"messages": [{"role": "user", "content": "tidy up"}], "max_tokens": 8}
head, out = Req("/v1/chat/completions", dict(chat, stream=True)).response()
cid = _request_id_of(head)
assert cid.startswith("chatcmpl-") and len(cid) == 9 + 24, cid
assert first_frame_id(out) == cid, (out[:200], cid)
head, out = Req("/v1/chat/completions", chat).response()
assert json.loads(out)["id"] == _request_id_of(head)
finally:
app.generate_stream = real
serve()
comp = {"prompt": "hello", "max_tokens": 8}
head, out = Req("/v1/completions", dict(comp, stream=True)).response()
cid = _request_id_of(head)
assert cid.startswith("cmpl-") and len(cid) == 5 + 24, cid
assert first_frame_id(out) == cid, (out[:200], cid)
head, out = Req("/v1/completions", comp).response()
assert json.loads(out)["id"] == _request_id_of(head)
head, _ = Req("/v1/not-a-completion", {"x": 1}).response()
assert "X-Tandem-Request-Id" not in head, head



# ------------------------------------------------------------------

Expand Down
Loading