You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
refactor(api): move hand-written abort bridges to the shared helper #2486
Seven providers on main bridge the caller's abortSignal with their own AbortController and abort listener. bridgeExternalAbortSignal in src/api/providers/utils/abort-signal.ts does the same work. anthropic.ts, anthropic-vertex.ts, minimax.ts and xai.ts already use it. Each hand-written copy adds one more place for a listener leak or a missed pre-aborted check.
Sites at origin/main66b07e0e65:
bedrock.ts (~593): its 10-minute timer also aborts the request controller.
lm-studio.ts (~137) and qwen-code.ts (~293): both need a signal even with no caller signal, and abort the controller in finally.
openai-native.ts (~471, attachExternalAbort): the controller is exposed to the stop-button path.
openai-codex.ts (~524): both transports abort through the controller.
native-ollama.ts (~481 and ~736): the abort callback also calls client.abort().
vscode-lm.ts (~1198 and ~1684): the callback cancels a token source and records abortedByCaller.
fetchers/modelCache.ts has two abort listeners. They are not request bridges, so leave them as they are.
The helper returns only a signal, and returns undefined when there is no caller signal. Most sites above need a controller or an extra callback. Extend the helper first, for example with an optional controller and an onAbort hook. Then migrate each site and keep its provider tests green.
Also in scope:
main has two createTimeoutError helpers: one in utils/abort-signal.ts and one in utils/request-timeout.ts with a phase argument. Keep one. Move the callers.
After the migration, add a lint selector in packages/config-eslint/api-provider-hygiene.js that flags addEventListener("abort", …) outside the helper. Add one that flags a hand-built TimeoutError. Both need zero violations when they land.
Context: review of #1535 and the open abort PRs #1545 and #1311.
Shared-symbol owner.src/api/providers/utils/abort-signal.ts and src/api/providers/utils/request-timeout.ts belong to the chain owner (@easonLiangWorldedtech). Split PRs may touch them only to resolve a merge conflict with main; every helper API change lands in U0a/U0b first and the provider units rebase onto them.
Per-unit budget, measured against each unit's own base: <= 400 a+d (hard ceiling 1000), <= 500 changed executable lines, <= 400 valid mutants. Every new branch gets a test plus a negative control. src/eslint-suppressions.json counts never increase.
Remaining hand-written bridges at main75a93a9d9acc (git grep -c 'addEventListener("abort")' src/api/providers — production files only): bedrock.ts 1, lm-studio.ts 1, native-ollama.ts 2, openai-codex.ts 1, openai-native.ts 1, qwen-code.ts 1, vscode-lm.ts 2 — seven provider files, matching this issue's scope. fetchers/modelCache.ts (2) is the shared fetch layer and stays out of these units.
Unit
Scope
Files
Lands
U0a
Give bridgeExternalAbortSignal an optional controller/onAbort hook so the abandoned-generator teardown keeps a controller without hand-writing addEventListener("abort"). Additive: default behaviour unchanged. Driver: the openrouter.ts teardown controller pinned by openrouter.spec.ts:1463 (raised on #1545).
Collapse the duplicate createTimeoutError definitions (abort-signal.ts:304 single-arg vs request-timeout.ts:23(providerName, phase)) into the phase-aware one, and add the optional cause the timeout call sites need (raised on #1311 and #1535). Call-site switches in lm-studio.ts / qwen-code.ts are import-line + one-argument edits only, so U2 can rebase cleanly.
utils/abort-signal.ts, utils/request-timeout.ts, both specs, friendli.ts (+ import/call lines in lm-studio.ts, qwen-code.ts)
2nd
U1
bedrock.ts: replace the hand-written bridge with bridgeExternalAbortSignal + dispose in finally.
bedrock.ts, __tests__/bedrock.spec.ts
after U0b
U2
lm-studio.ts + qwen-code.ts: same migration, keep their timeout semantics.
those 2 providers + lm-studio*.spec.ts, qwen-code*.spec.ts
after U0b
U3
openai-native.ts: same migration (3 controllers today, 1 bridge + teardown).
vscode-lm.ts: same migration (2 bridges) + github-copilot inherits it.
vscode-lm.ts, vscode-lm.spec.ts
after U0b
U7
Add the two hygiene selectors that fail a hand-written bridge at lint time, so new providers cannot reintroduce one. Suppression counts may only fall.
packages/config-eslint/api-provider-hygiene.js + its test, src/eslint-suppressions.json
last, once U1-U6 are merged
Merge order: U0a -> U0b -> U1 -> U2 -> U3 -> U4 -> U5 -> U6 -> U7. U1-U6 are independent of each other; they only share the helper base.
Two notes for the series, not units here:
Timeout vs abort verdict: gemini/lite-llm/mistral/openai-native report TimeoutError via the phase-aware helper, while the gateway family (zoo-gateway, requesty, opencode-go, unbound, vercel-ai-gateway) reports AbortError for a timeout-only expiry, which is what the merged specs pin. U0b should decide one contract and the units follow it; until then no unit should change a provider's verdict as drive-by cleanup.
#2500 landed the RouterProvider/OpenAI-SDK streaming contract in a form later units should copy. Beyond the promise-shaped completePrompt wiring, review rounds required three stream-side pieces that U1-U6 should treat as part of the convention, not optional polish:
Bridge the caller signal for streams too. The pinned openai SDK does signal.addEventListener('abort', () => controller.abort()) in client.mjs and never removes it, so handing the SDK a live caller signal leaves one listener and one request controller behind per request. createMessage now bridges and disposes on every exit path: the generator finally after the request starts, and the model lookup's rejection handler before it.
Race create(...) with the signal.await rejectOnAbort(pendingCompletion, bridgedCallerSignal, providerName) so a request that never returns settles as the contract AbortError on cancel; keep the one-argument call and plain await when no signal exists.
Guard the emit loop, not just the tail.if (signal?.aborted) break at the top of the for await body (a tail check then turns it into the AbortError) plus a check before emitting items still buffered inside the chunk - the pair openai-codex.ts:567-586 already ships. A tail-only check cannot stop a second tool_call_partial from the same SDK chunk.
Budget note: those three pieces pushed #2500 to 483 a+d (kenari.ts 109/-37, kenari.spec.ts 335/-2), 83 past the 400-line unit budget, because each arrived with the regression test its finding demanded (five tests, ~90 of the lines are spec). Units that start from this shape should budget for them up front instead of discovering them in review, and U7 should make the emit-loop guard and the bridge enforceable so the next provider does not have to learn them one review round at a time.
Activity
edelauna commentedon Nov 18, 2026
@easonLiangWorldedtech - could you look at redoing some of the previously merged prs.
easonLiangWorldedtech commentedon Nov 18, 2026
Ok, I will check it.
easonLiangWorldedtech commentedon Nov 18, 2026
Split plan (posted before any split PR opens)
Shared-symbol owner.
src/api/providers/utils/abort-signal.tsandsrc/api/providers/utils/request-timeout.tsbelong to the chain owner (@easonLiangWorldedtech). Split PRs may touch them only to resolve a merge conflict withmain; every helper API change lands in U0a/U0b first and the provider units rebase onto them.Per-unit budget, measured against each unit's own base: <= 400 a+d (hard ceiling 1000), <= 500 changed executable lines, <= 400 valid mutants. Every new branch gets a test plus a negative control.
src/eslint-suppressions.jsoncounts never increase.Remaining hand-written bridges at
main75a93a9d9acc(git grep -c 'addEventListener("abort")' src/api/providers— production files only):bedrock.ts1,lm-studio.ts1,native-ollama.ts2,openai-codex.ts1,openai-native.ts1,qwen-code.ts1,vscode-lm.ts2 — seven provider files, matching this issue's scope.fetchers/modelCache.ts(2) is the shared fetch layer and stays out of these units.bridgeExternalAbortSignalan optional controller/onAbort hook so the abandoned-generator teardown keeps a controller without hand-writingaddEventListener("abort"). Additive: default behaviour unchanged. Driver: theopenrouter.tsteardown controller pinned byopenrouter.spec.ts:1463(raised on #1545).utils/abort-signal.ts,utils/__tests__/abort-signal.spec.tscreateTimeoutErrordefinitions (abort-signal.ts:304single-arg vsrequest-timeout.ts:23(providerName, phase)) into the phase-aware one, and add the optionalcausethe timeout call sites need (raised on #1311 and #1535). Call-site switches inlm-studio.ts/qwen-code.tsare import-line + one-argument edits only, so U2 can rebase cleanly.utils/abort-signal.ts,utils/request-timeout.ts, both specs,friendli.ts(+ import/call lines inlm-studio.ts,qwen-code.ts)bedrock.ts: replace the hand-written bridge withbridgeExternalAbortSignal+disposeinfinally.bedrock.ts,__tests__/bedrock.spec.tslm-studio.ts+qwen-code.ts: same migration, keep their timeout semantics.lm-studio*.spec.ts,qwen-code*.spec.tsopenai-native.ts: same migration (3 controllers today, 1 bridge + teardown).openai-native.ts,openai-native.spec.tsopenai-codex.ts: same migration.openai-codex.ts,openai-codex.spec.ts,openai-codex-native-tool-calls.spec.tsnative-ollama.ts: same migration (2 bridges).native-ollama.ts,native-ollama.spec.tsvscode-lm.ts: same migration (2 bridges) +github-copilotinherits it.vscode-lm.ts,vscode-lm.spec.tspackages/config-eslint/api-provider-hygiene.js+ its test,src/eslint-suppressions.jsonMerge order: U0a -> U0b -> U1 -> U2 -> U3 -> U4 -> U5 -> U6 -> U7. U1-U6 are independent of each other; they only share the helper base.
Two notes for the series, not units here:
gemini/lite-llm/mistral/openai-nativereportTimeoutErrorvia the phase-aware helper, while the gateway family (zoo-gateway,requesty,opencode-go,unbound,vercel-ai-gateway) reportsAbortErrorfor a timeout-only expiry, which is what the merged specs pin. U0b should decide one contract and the units follow it; until then no unit should change a provider's verdict as drive-by cleanup.fetchModel()takes no signal (router-provider.ts:59;zoo-gateway.tsand the gateway siblings do not even thread it intofetchModels), so the feat(api): abort signal support for requesty (createMessage + kill tests) #1538 fix - passing the per-request scope into model discovery - cannot be ported additively. That belongs to RouterProvider: signal-aware shared-flight model discovery so a cancelled waiter can stop the fetch #2202.feat(api): cancel Kenari requests through the shared abort helpers
feat(api): cancel Kenari requests through the shared abort helpers
feat(api): cancel Kenari requests through the shared abort helpers
feat(api): cancel Kenari requests through the shared abort helpers
feat(api): cancel Kenari requests through the shared abort helpers
feat(api): cancel Kenari requests through the shared abort helpers
feat(api): cancel Kenari requests through the shared abort helpers
feat(api): cancel Kenari requests through the shared abort helpers
feat(api): cancel Kenari requests through the shared abort helpers
feat(api): cancel Kenari requests through the shared abort helpers
easonLiangWorldedtech commentedon Nov 18, 2026
U6 (Kenari, #2500) realized shape
#2500 landed the RouterProvider/OpenAI-SDK streaming contract in a form later units should copy. Beyond the promise-shaped
completePromptwiring, review rounds required three stream-side pieces that U1-U6 should treat as part of the convention, not optional polish:openaiSDK doessignal.addEventListener('abort', () => controller.abort())inclient.mjsand never removes it, so handing the SDK a live caller signal leaves one listener and one request controller behind per request.createMessagenow bridges and disposes on every exit path: the generatorfinallyafter the request starts, and the model lookup's rejection handler before it.create(...)with the signal.await rejectOnAbort(pendingCompletion, bridgedCallerSignal, providerName)so a request that never returns settles as the contractAbortErroron cancel; keep the one-argument call and plain await when no signal exists.if (signal?.aborted) breakat the top of thefor awaitbody (a tail check then turns it into theAbortError) plus a check before emitting items still buffered inside the chunk - the pairopenai-codex.ts:567-586already ships. A tail-only check cannot stop a secondtool_call_partialfrom the same SDK chunk.Budget note: those three pieces pushed #2500 to 483 a+d (
kenari.ts109/-37,kenari.spec.ts335/-2), 83 past the 400-line unit budget, because each arrived with the regression test its finding demanded (five tests, ~90 of the lines are spec). Units that start from this shape should budget for them up front instead of discovering them in review, and U7 should make the emit-loop guard and the bridge enforceable so the next provider does not have to learn them one review round at a time.feat(api): cancel Kenari requests through the shared abort helpers
feat(api): cancel Kenari requests through the shared abort helpers