Repository navigation
feat(api): honour caller cancellation in the openai family (#1311 split u1) - #2801
easonLiangWorldedtech wants to merge 2 commits into
Conversation
…-Org#1311 split u0) Unit u0 of the split plan in Zoo-Code-Org#2799. It carries the shared helpers that the provider units of Zoo-Code-Org#1311 consume, and nothing provider-specific. - abort-signal.ts: mergeAbortSignalAndTimeout, rejectOnAbort, and the timeout classification helpers (isTimeoutAbort, createTimeoutError). - error-handler.ts: handleOpenAIRequestError keeps an HTTP failure's identity over a racing abort and reports a deadline as TimeoutError. - request-timeout.ts: getRequestTimeoutMs for the per-request SDK timeout. - test-utils/errors.ts: captureError, used by the cancellation rows of the provider units. Evidence: 643 rows across the utils and test-utils specs pass; tsc --noEmit clean; eslint --prune-suppressions --max-warnings=0 clean; prettier clean.
…Org#1311 split u1) Unit u1 of the split plan in Zoo-Code-Org#2799. It carries openai.ts and the OpenAI-compatible base, with their cancellation rows. - The two abort contracts stay apart: the caller's signal goes to the request each route issues, and a mid-flight abort or a raw SDK abort becomes the provider's contract error with the failure kept as the cause. Each request site normalizes its own failure, including the Responses completion. - A stream failure is a flag beside its value, so a rejection carrying `undefined` still reaches the handler, and the recorded usage is yielded before the error. A usage frame the SDK already returned is recorded before the abort break. - The Responses completion forwards the merged caller and deadline signal, so the deadline stays armed while the SDK reads the JSON body. - Request creation is raced against the request signal, so a 429 backoff sleep cannot outlast a deadline. Evidence: openai, base-provider and cancellation rows 725 pass; tsc --noEmit clean; eslint --prune-suppressions --max-warnings=0 clean; prettier clean.
Review statusThanks for contributing. This comment tracks the review sequence and the next action. Current step: Fix the failing required CI checks; awaiting-maintainer requires CI and automated review completion. Review-state labels are managed by this workflow; do not edit them manually. |
Pre-merge checks:
|
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Regression Evidence | Unit u1 (HEAD 91c6af4 vs u0 head c6f18d3) changes OpenAiHandler.completePrompt on the Responses route. The new code merges the caller signal with the deadline and classifies timeouts (src/api/provid… |
Add a provider-level test in src/api/providers/tests/openai.spec.ts for the Responses route. Set openAiUseResponsesApi: true, then call completePrompt("...", { timeoutMs: <small value> }) with no caller signal. Park the `mockRespons… |

Passed checks: 3 passed
-
Check name Status Explanation Security Boundaries The Security boundaries check fails only for a concrete defect in a changed path: an exposed secret or personal data, unvalidated input that is trusted or executed, or a bypassed approval or allowlist… Persistence Integrity The check applies only to changed persistence paths. PR #2801(head split/1311-u1-openai-family, base main, author easonLiangWorldedtech) changes OpenAI-family provider request and stream handling. Th…Lifecycle Resource Cleanup Checked the lifecycle paths changed in this unit (commit 91c6af4 against base c6f18d3) in openai.tsandbase-openai-compatible-provider.ts. No changed path leaks a listener, watcher, provider, tim…Full details: Security Boundaries
-
Explanation
The Security boundaries check fails only for a concrete defect in a changed path: an exposed secret or personal data, unvalidated input that is trusted or executed, or a bypassed approval or allowlist control. I found no such defect. Scope reviewed: - PR
#2801(Zoo-Code-Org/Zoo-Code, head split/1311-u1-openai-family, base main). Author: easonLiangWorldedtech. - The u1 commit (c6f18d3..91c6af4) changes openai.ts, base-openai-compatible-provider.ts, and their spec files. - The u0 commit (dd30249..c6f18d3), which the PR diff against main also includes, adds error-handler.ts, abort-signal.ts, request-timeout.ts, and config-builder helpers. What the changes do: - They add caller-cancellation and deadline plumbing. They use AbortSignal checks, a per-request SDK timeout, and error normalization to an AbortError or TimeoutError. - The Azure AI Inference path option is still set under the same condition. It is now built through setOption, which omits undefined values, so the request options are equivalent. - No new logging of request bodies, headers, or API keys appears in the diff. The only new key-like value is the placeholder "test-api-key" in the spec files. - No approval, allowlist, or permission gate is removed or skipped. Abort checks only stop work earlier. - The eslint-suppressions.json change removes one suppression and lowers another count. It does not add a suppression. - Error text from handleOpenAIRequestError reuses the existing handleOpenAIError path. Pre-existing logging in handleProviderError is unchanged. The check does not match these changes to any explicit failure condition.
Full details: Persistence Integrity
-
Explanation
The check applies only to changed persistence paths. PR
#2801(head split/1311-u1-openai-family, base main, author easonLiangWorldedtech) changes OpenAI-family provider request and stream handling. The 19 changed files cover openai.ts, base-openai-compatible-provider.ts, the utils abort/timeout/error helpers, test-utils, eslint-suppressions.json, and provider specs. Evidence gathered: - Added lines were scanned for disk, settings, secret, globalState/workspaceState, database, and flush or commit operations (writeFile, appendFile, rename, unlink, globalState, secrets, persist, save). No matches. The only.update(hits are in-memory stream parsers (matcher.update, signatureReader.update). - No fs or fs/promises calls were added, so no unawaited disk write was introduced. - ThoughtSignatureReader is an in-memory per-instance helper (openai.ts:167-168). The diff reorders its update call inside the stream loop, but it does not write to storage. - The utils changes (error-handler.ts, abort-signal.ts, request-timeout.ts) change error classification and timeout value computation. They do not touch stored state. No changed path writes persistent state, so no path can lose or corrupt persisted state under this check's criteria. The PR fails none of the listed conditions: un-awaited writes, non-atomic writes, missing rollback, or defaults that do not reach consumers.
Full details: Lifecycle Resource Cleanup
-
Explanation
Checked the lifecycle paths changed in this unit (commit 91c6af4 against base c6f18d3) in
openai.tsandbase-openai-compatible-provider.ts. No changed path leaks a listener, watcher, provider, timer, or task, and none repeats work after cancellation. - Listeners: The only listeners the changed code creates go throughrejectOnAbort/raceAbort(utils/abort-signal.ts:180-215). That helper detaches the abort listener when the pending promise settles or the abort fires, whichever comes first. The already-aborted branch attaches no listener.openai.tscompletePromptandbase-openai-compatible-provider.tscreateMessageboth use it. - Timers:mergeAbortSignalAndTimeout(openai.tsbuildCompletePromptRequestConfig,base-openai-compatible-provider.tscompletePrompt) usesAbortSignal.timeout(), which the code comment says manages its own timer.AbortSignal.anyis used for merging. NosetTimeout/setIntervalappears in the changed non-test code. - Early cancellation:throwIfAbortedruns before the merged signal or timeout is built in bothcompletePromptmethods, so a pre-aborted call creates no timer. - Streams: Abort checks usebreakinsidefor awaitloops (openai.tscreateMessageandhandleStreamResponse; basecreateMessage). Leaving the loop calls the SDK iteratorreturn(), which closes the response. Generator yields are re-checked against the signal, so a cancelled caller does not receive further content. - Abandoned in-flight requests: WhenrejectOnAbortwins the race, the SDK call still receives the same signal (requestConfig.signal), so the SDK aborts the request.friendli.tscreateStreamalso forwardsrequestOptions. No orphaned stream was found. - Duplicate work: No retry or re-issue path was added. The non-streaming branches only add checks before each yield. - Pre-existing code: The Responses-routebridgeExternalAbortSignal(openai.ts~481-526) is unchanged by this unit and disposes infinally. Residual risk:rejectOnAbortleaves the underlying SDK promise running. Its settlement is handled, so this does not cause an unhandled rejection. The SDK receives the same signal, so the request still ends.
-
Full details: Regression Evidence
-
Explanation
Unit u1 (HEAD 91c6af4 vs u0 head c6f18d3) changes
OpenAiHandler.completePrompton the Responses route. The new code merges the caller signal with the deadline and classifies timeouts (src/api/providers/openai.ts, Responses branch, themergeAbortSignalAndTimeoutcall and the catch that testsisTimeoutAbort(requestSignal)). The code comment says the deadline "only stays armed if it is forwarded as this request's signal." Test coverage for this changed behavior is incomplete: - Covered: the chat-path deadline (openai.spec.ts ~1511-1555 expectsTimeoutErrorand the message "The OpenAI completion request timed out"). - Covered: the Responses route forwardstimeout: 1234, and the merged signal is not aborted before the caller aborts (openai.spec.ts ~3375-3394). - Covered: caller-cancel on the Responses route (openai.spec.ts ~1793-1828). - Missing: no ResponsescompletePrompttest letstimeoutMselapse. Nothing asserts that the merged signal aborts on the deadline. Nothing asserts that the Responses catch returns theTimeoutErrorcontract instead of a raw or user-cancel abort. ThetimeoutMs: 1234test never fires a timer. A regression that drops the timeout from the merged signal, or removes|| isTimeoutAbort(requestSignal), would leave the suite green. The timeout test for the chat route would not catch it, because it covers a different code path. The lowest valid layer for this gap is the provider specopenai.spec.ts, next to the existing chat-path timeout tests. Other changed behavior in this unit has direct provider-level tests (abort pre-check, mid-stream abort, usage-before-verdict, o3 family, base provider, and the timeout spec). I found no test copying the implementation.Resolution
Add a provider-level test in src/api/providers/tests/openai.spec.ts for the Responses route. Set
openAiUseResponsesApi: true, then callcompletePrompt("...", { timeoutMs: <small value> })with no caller signal. Park themockResponsesCreatemock untiloptions.signalaborts, following the chat-path test at ~1524-1555. Assert that the merged signal aborts. Assert that the result hasname"TimeoutError" and message "The OpenAI completion request timed out". Optionally add a case where the caller signal stays live, and assert the same classification.
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Changed-code mutation testingReport for PR head
Details: mutants, guidance, and reproductionChanged-code mutation testing
Focused tests
Reproduce locallyFrom a full checkout containing both commits: Mutation reports
The workflow uploads generated reports in the Advisory findings
|
Part of #2799 (split plan for #1311). Unit u1 of 4. Base for review: u0 (#2800).
This unit's own diff (provider-only)
openai.ts,base-openai-compatible-provider.tsand their rows:openai.spec.ts,base-openai-compatible-provider.spec.ts,base-openai-compatible-provider-timeout.spec.ts,openai.cancellation.spec.ts,mimo.spec.ts,friendli.spec.ts,sambanova.spec.ts,fireworks.spec.ts, plus the suppression prune.GitHub currently shows the ancestor's files too, because this branch is based on u0's head. Once #2800 merges, the diff above is all this pull shows.
Why not independent right now
Two compile-time dependencies block a provider-only branch today:
handleOpenAIRequestErroris defined in u0, and every provider request site calls it. A branch that carries only this provider fails the repository's pre-push typecheck (tsc), so it cannot be pushed, let alone be green. Once #2800 lands, this branch rebases to a provider-only, independently mergeable pull.Budget (own delta vs u0's head
c6f18d3e15)Changed executable lines: 228 (cap 500): openai.ts 140, base 88. a+d 2346/132 - the soft 400 screen is exceeded because the focused specs travel with the unit.
Verification
725 rows pass (openai, base, cancellation);
tscclean;eslint --prune-suppressions --max-warnings=0clean; prettier clean.Cross-unit contract points
Consumes u0's helpers; adds no copy. u2 and u3 extend these classes.
Merge order
u0 (#2800) -> u1 -> u2 (#2802) -> u3 (#2803).