fix(service): reassemble streamed assistant replies - #1107
Merged
Conversation
A service session's reply was read as the last assistant Message event, but harnesses stream a turn as one Message per token delta. Every multi-token answer was therefore truncated to its final fragment: a session that replied "Red, green, blue." returned "." over HTTP. Single-token answers masked this, so it survived manual testing. Concatenate the trailing assistant run instead, skipping transcript bookkeeping and stopping at the user's message or a tool call, so a tool-using turn reports its answer rather than the narration before it.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The bug
GET /svc/<name>/sessions/<id>read the reply as the last assistantMessageevent. But harnesses stream a turn as oneMessageevent per token delta, so the "reply" was the final fragment rather than the answer.Verified against a live daemon with an
openai:gpt-5service session:The transcript for "what is 2 plus 2" arrives as eight separate events:
"2"," plus"," ","2"," is"," ","4",".".This survived testing because single-token answers mask it — a service asked "17×3" returns
51correctly, since the last delta is the whole reply. Every real multi-word answer was silently truncated.The fix
Concatenate the trailing assistant run instead. Walking backwards, transcript bookkeeping (status, cost, usage, reasoning) is skipped, and collection stops at the first boundary that ends the final answer — the user's message, or a tool call whose narration precedes it. A tool-using turn therefore reports its answer rather than the commentary leading up to it.
Extracted into a testable helper; there was previously no coverage of reply extraction.
Verification
Same live-daemon setup, after the fix:
Also re-checked: keyed routing still reuses sessions with context intact, a turn parked on tool approval correctly reports
ready:false/reply:null.Tests: 4 new unit tests (streaming deltas, turn boundary, tool-use narration, no-reply-yet).
construct-daemon --lib483 pass,construct-cli --bins1314 pass, workspace builds clean.This PR only touches
crates/daemon→ the relevant binary isconstruct.