Read the opening of an agent's answer, not all of it, when testing a connection - #471
Open
kevin9327 wants to merge 1 commit into
Open
Read the opening of an agent's answer, not all of it, when testing a connection#471kevin9327 wants to merge 1 commit into
kevin9327 wants to merge 1 commit into
Conversation
…connection The cap on how much of the stream the check reads was applied to a string `response.text()` had already taken in full, so the check took as long as the agent's run did. An agent that streams past the timeout came back as `The agent started answering and the connection broke`, about an agent that had answered correctly in its first two events. The body is read to the cap and the rest is cancelled, which is what the cap was for. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
kevin9327
requested review from
MikeRyanDev,
davidmckayv,
guidovizoso and
tylerslaton
as code owners
September 9, 2026 22:58
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
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.
Register an agent that streams its answer — a Bot working through a document, a model writing slowly
— and press Test connection. The form sits there for the full fifteen seconds and then says:
Nothing broke. The agent had already answered correctly:
RUN_STARTEDwas the first thing it wrote.The person registering it is now looking at their own service, which is fine.
Measured, against a real
Bun.serveagent on loopback that writes its events and then keepsstreaming (a kilobyte every 20ms, the way a model streams), with the timeout cut to 3s so the probe
is quick:
Why
server/src/agents/connection-test.tshas the right idea written down beside the constant:and then reads it all:
response.text()reads a body to its end, so the cap was applied to a string this process hadalready taken in full. The check therefore took as long as the agent's run, not as long as its
answer — and when that outlasted
AbortSignal.timeout, the abort came back throughtext()as arejected read, into the catch written for a connection that really did drop.
That catch is why the sentence is the one above rather than the timeout one: the request itself had
succeeded, so it is the body read that aborts, and 200 is already on the result.
The fix
Read from
response.bodyuntilMAX_BYTESis reached, then cancel the rest. Counted in bytes, whichis what the constant is named in and what a chunk off the wire is measured in. The chunk that crosses
the cap is kept whole rather than cut at it — one read past the cap at most, and a cut through a
multi-byte character would put a replacement character in the middle of a line the scan is about to
read.
The cancel is in a
finally, because the agent is very likely still writing and nothing is going toread the rest: leaving it open holds a socket into somebody's agent for as long as that run cares to
go on.
Measured
bun test server/tests/agent-connection-live.test.tsorigin/mainwith only the new test applied: 10 pass, 1 fail. The new case(
is reported as working, without waiting for it to finish) takes 3008ms and returnsok: false. Whole file: 3.28s.The ten cases that were already in that file are the pin, and they pass before and after. They are
the ones that would catch an over-correction, because they are all about reading the body correctly:
a real
@copilotkit/aimockagent reportingRUN_STARTED/RUN_FINISHED, a run that starts and neverfinishes, an agent answering
RUN_ERROR, a stream carrying a tool call, a redirect that is followed,a redirect that is refused, a redirect loop, and three unreachable addresses.
Also run, all green:
bun test server/tests/agent-endpoint.test.ts server/tests/agent-test-connection-headers.test.ts server/tests/agent-routes.test.ts— 111 pass, 0 failbun run --filter server typecheck— exit 0bunx biome checkon both changed files — cleanNote on the CHANGELOG
A deployment behaves differently afterwards, so there is an entry. It goes at the top of
## Unreleased, the same one line every entry goes at, so it will conflict with any other PR openagainst that anchor. Happy to rebase whenever it suits you.