Skip to content

refactor(api): move hand-written abort bridges to the shared helper #2486

Open

Description

@edelauna

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/main 66b07e0e65:

  • 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.

Activity

  1. edelauna commented on Nov 18, 2026

    @edelauna
    ContributorAuthor

    @easonLiangWorldedtech - could you look at redoing some of the previously merged prs.

  2. easonLiangWorldedtech commented on Nov 18, 2026

    @easonLiangWorldedtech
    Contributor

    Ok, I will check it.

  3. easonLiangWorldedtech commented on Nov 18, 2026

    @easonLiangWorldedtech
    Contributor

    Split plan (posted before any split PR opens)

    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 main 75a93a9d9acc (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). utils/abort-signal.ts, utils/__tests__/abort-signal.spec.ts 1st
    U0b 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). openai-native.ts, openai-native.spec.ts after U0b
    U4 openai-codex.ts: same migration. openai-codex.ts, openai-codex.spec.ts, openai-codex-native-tool-calls.spec.ts after U0b
    U5 native-ollama.ts: same migration (2 bridges). native-ollama.ts, native-ollama.spec.ts after U0b
    U6 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:

  4. easonLiangWorldedtech commented on Nov 18, 2026

    @easonLiangWorldedtech
    Contributor

    U6 (Kenari, #2500) realized shape

    #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:

    1. 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.
    2. 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.
    3. 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.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions