Skip to content

Wait helpers ignore event: error and duplicate the stream parser #20

Open

Description

@burgaard

What's wrong

runStreamingAgentAndWait() has its own copy of the fetch, reader and SSE parser pipeline, separate from runStreamingAgent(). The copies have drifted, and the wait helper mishandles several stream endings:

  • event: error and event: timeout are ignored. The server sends error when the stream fails internally, and timeout after 600s, then closes the stream. The wait helper treats that as an early end, and the server's message is lost.
  • A final line without a trailing newline is dropped. Text left in the parser's buffer at the end of the stream is never parsed.
  • The response body stays open after done. The reader is cancelled only on the returnOnResult path.
  • Abort listeners leak. anySignal adds a listener to the caller's signal and never removes it, so a caller that reuses one AbortSignal collects one listener per call.
  • A timeout produces two different errors, depending on whether it fires during the fetch or during the read. The incomplete-run message is also built in two places.

Why it matters

A run that failed on the server comes back as a generic error, without the server's message. Every fix to the stream handling has to land in two places.

Done when

  • runStreamingAgentAndWait() is built on runStreamingAgent() (for await, tracking the latest snapshot and returning early).
  • event: error and event: timeout throw SeclaiStreamingError with the server's message (for error) and the latest snapshot in run.
  • A final unterminated line is parsed.
  • Every exit cancels the reader.
  • No listener is left on the caller's signal after a call.
  • Each case above has a test.

Out of scope

Changing what the default mode returns for a stream that ends without done. That stays as released until throwOnIncomplete becomes the default in the next major version.

Found in the review of #19.

Activity

  1. burgaard commented on Nov 6, 2026

    @burgaard
    ContributorAuthor

    More from the review of #19 (round 5), for the same rework:

    • Abort handling: runAgentAndPoll checks signal only between polls, and never passes it to runAgent(), getAgentRun() or the sleep. So an already-aborted signal still starts a (billable) run, and an abort during the sleep still sends one more GET. Both were in 1.8.0.
    • Timeout overshoot: the timeout is checked only before each sleep, so timeoutMs can overshoot by up to one pollIntervalMs.

    The fix for both is to pass signal through the requests and an abortable sleep, and to cap the sleep at the time left before the deadline.

  2. burgaard commented on Nov 6, 2026

    @burgaard
    ContributorAuthor

    More from the review of #19 (round 6): make runAgentAndPoll's abort surface as the abort error, as the streaming helpers do. Use signal.throwIfAborted() instead of throwing SeclaiError("Polling aborted."), so callers get an AbortError carrying signal.reason. That changes the type a caller catches, so it needs a changelog entry.

  3. burgaard commented on Nov 6, 2026

    @burgaard
    ContributorAuthor

    More from the review of #19 (round 9): with includeStepOutputs, the step-outputs fetch for a start run that already ended isn't bounded by timeoutMs. A slow or failing GET after the deadline delays the call or throws, though a finished run was in hand. Also untested today: that err.run is the runAgent() response when timeoutMs elapses before any poll.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions