mirror of
https://github.com/Comfy-Org/comfy-typescript-sdk.git
synced 2026-11-04 23:47:27 +00:00
[PR #204] fix(jobs): end job.events() with a typed error on the terminal SSE error frame #203
Labels
No labels
audit:ai-readiness
audit:community
audit:github
audit:health-scoring
audit:policy
audit:release
audit:supply-chain
bug
cursor-review-pin
effort:medium
effort:small
effort:trivial
priority:nice-to-have
priority:recommended
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Comfy-Org/comfy-typescript-sdk#203
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Comfy-Org/comfy-typescript-sdk/pull/204
Author: @mattmillerai
Created: 10/9/2026
Status: 🔄 Open
Base:
main← Head:mattmiller/be-19941-sse-error-frame📝 Commits (3)
d3d2a74fix(jobs): end job.events() with a typed error on the terminal SSE error frame642dc54fix(jobs): harden the SSE error-frame path against prototype codes and abort races2229088fix(jobs): keep the SSE error frame through a rejected iterator close, and keep its reason📊 Changes
12 files changed (+334 additions, -5 deletions)
View changed files
📝
CHANGELOG.md(+3 -0)📝
src/low/errors.test.ts(+85 -0)📝
src/low/errors.ts(+75 -1)📝
src/low/spec-coverage.test.ts(+26 -0)📝
src/low/sse.test.ts(+5 -0)📝
src/low/sse.ts(+3 -1)📝
src/sdk/events.ts(+1 -0)📝
src/sdk/exceptions.test.ts(+10 -0)📝
src/sdk/exceptions.ts(+5 -1)📝
src/sdk/jobs.test.ts(+88 -1)📝
src/sdk/jobs.ts(+21 -1)📝
test/support/stub-server.ts(+12 -0)📄 Description
Summary
GET /api/v2/jobs/{id}/eventsnow ends a stream withevent: errorplus an error envelope (credential_expired,forbidden,job_not_found) when the server's periodic re-poll permanently fails.eventFromRawhad no case for that frame, soJob.events()dropped it, never saw a terminal status, and ranrefresh()→ 100 ms pause → reconnect. Forjob_not_found, whileGET /jobs/{id}still answers non-terminal, that loop never ends and keeps using the caller's concurrent-stream quota. In all three cases the caller never saw the server's explanation.Change
src/sdk/jobs.ts: inside thefor awaitframe loop ofJob.events(), and beforeeventFromRaw, anerrorframe is turned into a typed error viasseErrorFromFrame(raw.data)and the loop breaks. The error is thrown astoSdkError(...)after thetry, so the reconnect dispatch (501/429 handling, and the abort rethrow that would leak the untranslated low error) never sees it. Thecatchalso checks for the frame first, becausebreakcloses the iterator inside thetry, and a rejection from that close (an already-errored body) would otherwise land there and drop or replace the frame. There is no refresh, no pause and no reconnect. Frames that arrived before theerrorframe are still yielded. The JSDoc now names the three rejections.src/low/errors.ts: adds the exportedsseErrorFromFrame(data). It mapsunauthorized/credential_expired→ 401,forbidden→ 403, andnot_found/job_not_found/asset_not_found→ 404, then callserrorFromEnvelope, keeping thecodeverbatim. An unknown code gives a bareApiErrorwithhttpStatus: 0, which still ends the iterator. A frame with no envelope message keeps what the server did say: a stringerror, a non-JSON line, a non-object JSON value, or the payload itself. If there is none, it names the frame rather than reportingHTTP 0. That reason has its line breaks flattened and is capped at 500 characters before it goes intoError.message.credential_expiredis added toBY_CODEasUnauthorized. Code lookups here and inerrorFromEnvelopecheck for an own property (Object.hasOwn), so a server code likeconstructorcan't resolve throughObject.prototype.src/low/sse.ts:iterateSseno longer drops anerrorframe whosedata:line is empty (other empty frames are still skipped). A bareevent: errorwith nodata:line at all is never dispatched byeventsource-parser, as the SSE spec requires, so that shape still falls through to the poll-and-reconnect backstop.src/sdk/exceptions.ts:credential_expired→Unauthorized, sotoSdkErrorkeeps the type. Its code lookup also checks for an own property now.src/low/spec-coverage.test.ts: a rot guard that fails once the vendored spec'sgetJobEventsx-sse-eventsdeclareserror. That is the signal to check the frame's declared shape and replace the guard with a real comparison.src/sdk/events.ts: adds a comment noting thaterroris handled upstream and is not aComfyEventkind.test/support/stub-server.ts: addssseErrorFrameCodestate. When it is set, the stub sends arunningstatus frame, then theerrorframe, then closes.Judgment calls
server.state.pollCount. The stub's counter is actually calledjobPollCount, so the tests use that.pollsToSucceed = 1_000_000, so the poll backstop never reports terminal. That reproduces the unboundedjob_not_foundcycle. It also assertsabortableSleepwas never called (no reconnect pause), on top of the ticket'seventsConnectCount === 1and unchanged poll count.Residual
Job.events()likely has the same drop-and-reconnect behaviour for the terminalerrorframe. This PR only fixes the TypeScript SDK. The Python twin needs the same change (an SSE error-frame → typed error helper, pluscredential_expired→Unauthorizedin both code tables).surface-parity.test.tsdoes not flag it, because it does not cover the low-layer error helpers.errorevent's entry inspec/openapi.yamlarrives with the pending v2 spec sync, which is a separate open PR. This change deliberately does not depend on it, because the server already emits the frame. The rot guard insrc/low/spec-coverage.test.tsfails when that sync lands, which is the cue to pin the declared codes againstSSE_STATUS_BY_CODE.event: errorwith an{error: {code, message}}envelope.catchblock ofJob.events()and the stub's 429 state. This PR edits the frame loop and adds a separate stub field, so the two should rebase cleanly. Whichever lands second should re-runsrc/sdk/jobs.test.ts.Provenance
pnpm test818 passed / 5 skipped. This includes prototype-key, generic-code, message-fallback and sanitizing cases, an empty-dataerrorframe, and a rejected iterator close (that case fails without the catch re-check)tsc --noEmitexcludes tests)pnpm lint,pnpm format:check,pnpm typecheckcleanpnpm check:spec-driftOK (generated + Router route)pnpm buildOKmainlocally: lint, format, typecheck, spec-drift andpnpm test(822 passed / 5 skipped) all cleansdk-parityis red on a shared drift, not on this branch: the Python SDK addedQueueBacklogFull, which open PR #209 refreshes the snapshot forpollCount; the stub's counter isjobPollCount. All other acceptance criteria are met as written.🤖 Generated with Claude Code
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.