Skip to content

Commit 91dc04c

Browse files
committed
Address review: reuse shared abort-signal helpers; warn on dropped API key
Replace the local raceWithAbortSignal/createAbortError pair with the shared utils/abort-signal exports (rejectOnAbort + createAbortError(providerName)), matching openai-codex.ts. The shared helpers are strictly better shaped: the already-aborted branch detaches a rejection handler from the pending promise, the listener registers with { once: true }, and the error carries the provider name (The Ollama request was aborted) per the series contract. Call sites that may see an undefined signal (no metadata signal; merged signal undefined when neither an external signal nor a positive timeout applies) keep the no-race path explicit. The local helpers and their dedicated unit block are removed; the shared helpers are covered by utils/__tests__/abort-signal.spec.ts. The plain-HTTP credential gate now console.warns when ollamaApiKey is set but the endpoint is neither https nor loopback, so a bare 401 behind an auth proxy is diagnosable; the warning never echoes the key. New tests pin the warning (content mentions ollamaApiKey, not the key) and its absence on secure endpoints. Negative controls: removing the warn fails exactly the remote-HTTP test (isolated); mutating the ported provider name fails exactly the already-aborted completePrompt test. Verification at this head: 87/87 green in native-ollama.spec.ts; tsc --noEmit 0 errors; eslint --max-warnings=0 clean with suppression counts unchanged; prettier clean.
1 parent fa64caf commit 91dc04c

2 files changed

Lines changed: 51 additions & 113 deletions

File tree

‎src/api/providers/__tests__/native-ollama.spec.ts‎

Lines changed: 27 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { Anthropic } from "@anthropic-ai/sdk"
44

5-
import { NativeOllamaHandler, raceWithAbortSignal } from "../native-ollama"
5+
import { NativeOllamaHandler } from "../native-ollama"
66
import { ApiHandlerOptions } from "../../../shared/api"
77
import { getOllamaModels } from "../fetchers/ollama"
88

@@ -973,7 +973,7 @@ describe("NativeOllamaHandler", () => {
973973

974974
const promise = handler.completePrompt("Test prompt", { abortSignal: controller.signal })
975975
controller.abort()
976-
await expect(promise).rejects.toThrow("This operation was aborted")
976+
await expect(promise).rejects.toThrow("The Ollama request was aborted")
977977

978978
expect(capturedInstanceAbort).toBeDefined()
979979
expect(capturedInstanceAbort).toHaveBeenCalledTimes(1)
@@ -1000,7 +1000,7 @@ describe("NativeOllamaHandler", () => {
10001000
})
10011001

10021002
await expect(handler.completePrompt("Test prompt", { abortSignal: controller.signal })).rejects.toThrow(
1003-
"This operation was aborted",
1003+
"The Ollama request was aborted",
10041004
)
10051005

10061006
expect(capturedInstanceAbort).toBeDefined()
@@ -2201,10 +2201,11 @@ describe("NativeOllamaHandler", () => {
22012201
)
22022202
})
22032203

2204-
it("should not attach the API key for a remote HTTP endpoint", async () => {
2204+
it("should not attach the API key for a remote HTTP endpoint and warn about the drop", async () => {
22052205
mockChat.mockResolvedValue({
22062206
message: { content: "Response" },
22072207
})
2208+
const warnSpy = vitest.spyOn(console, "warn").mockImplementation(() => {})
22082209

22092210
const handler = new NativeOllamaHandler({
22102211
apiModelId: "llama2",
@@ -2219,6 +2220,28 @@ describe("NativeOllamaHandler", () => {
22192220
// so the Authorization header must be omitted.
22202221
const callArgs = OllamaMock.mock.calls[0][0]
22212222
expect(callArgs.headers).toBeUndefined()
2223+
// The silent drop is a diagnosis trap (bare 401 behind an auth
2224+
// proxy), so the gate warns - without echoing the key itself.
2225+
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("ollamaApiKey"))
2226+
expect(warnSpy.mock.calls.flat().join(" ")).not.toContain("test-api-key-123")
2227+
})
2228+
2229+
it("should not warn when the API key is attached to a secure endpoint", async () => {
2230+
mockChat.mockResolvedValue({
2231+
message: { content: "Response" },
2232+
})
2233+
const warnSpy = vitest.spyOn(console, "warn").mockImplementation(() => {})
2234+
2235+
const handler = new NativeOllamaHandler({
2236+
apiModelId: "llama2",
2237+
ollamaModelId: "llama2",
2238+
ollamaBaseUrl: "https://ollama.example.com:11434",
2239+
ollamaApiKey: "test-api-key-123",
2240+
})
2241+
2242+
await handler.completePrompt("Test prompt")
2243+
2244+
expect(warnSpy).not.toHaveBeenCalledWith(expect.stringContaining("ollamaApiKey"))
22222245
})
22232246

22242247
it("should attach the API key for an HTTPS endpoint", async () => {
@@ -2280,65 +2303,6 @@ describe("NativeOllamaHandler", () => {
22802303
})
22812304
})
22822305

2283-
describe("raceWithAbortSignal", () => {
2284-
it("should resolve with the pending result when no signal is given", async () => {
2285-
const result = await raceWithAbortSignal(Promise.resolve("value"), undefined)
2286-
expect(result).toBe("value")
2287-
})
2288-
2289-
it("should reject with AbortError when the signal is already aborted", async () => {
2290-
const controller = new AbortController()
2291-
controller.abort()
2292-
const pending = new Promise<string>(() => {})
2293-
const settledPre = expect(raceWithAbortSignal(pending, controller.signal)).rejects.toMatchObject({
2294-
name: "AbortError",
2295-
})
2296-
await withDeadline(settledPre, 500)
2297-
})
2298-
2299-
it("should reject with AbortError when the signal aborts before the promise settles", async () => {
2300-
const controller = new AbortController()
2301-
const pending = new Promise<string>(() => {})
2302-
const raced = raceWithAbortSignal(pending, controller.signal)
2303-
controller.abort()
2304-
const settledAbort = expect(raced).rejects.toMatchObject({ name: "AbortError" })
2305-
await withDeadline(settledAbort, 500)
2306-
})
2307-
2308-
it("should resolve when the promise settles before the signal aborts", async () => {
2309-
const controller = new AbortController()
2310-
const raced = raceWithAbortSignal(Promise.resolve("done"), controller.signal)
2311-
const result = await withDeadline(raced, 500)
2312-
expect(result).toBe("done")
2313-
controller.abort() // a late abort must not disturb the settled result
2314-
})
2315-
2316-
it("should remove the abort listener when pending settles, on resolve and reject", async () => {
2317-
const resolveController = new AbortController()
2318-
const resolveAddSpy = vitest.spyOn(resolveController.signal, "addEventListener")
2319-
const resolveRemoveSpy = vitest.spyOn(resolveController.signal, "removeEventListener")
2320-
const resolved = await raceWithAbortSignal(Promise.resolve("done"), resolveController.signal)
2321-
expect(resolved).toBe("done")
2322-
// The listener detached must be the exact reference that was
2323-
// registered, so a reused caller signal cannot keep it.
2324-
expect(resolveRemoveSpy).toHaveBeenCalledWith("abort", resolveAddSpy.mock.calls[0]?.[1])
2325-
2326-
const rejectController = new AbortController()
2327-
const rejectAddSpy = vitest.spyOn(rejectController.signal, "addEventListener")
2328-
const rejectRemoveSpy = vitest.spyOn(rejectController.signal, "removeEventListener")
2329-
const rejected = expect(
2330-
raceWithAbortSignal(
2331-
new Promise<string>((_resolve, reject) => {
2332-
reject(new Error("boom"))
2333-
}),
2334-
rejectController.signal,
2335-
),
2336-
).rejects.toThrow("boom")
2337-
await withDeadline(rejected, 500)
2338-
expect(rejectRemoveSpy).toHaveBeenCalledWith("abort", rejectAddSpy.mock.calls[0]?.[1])
2339-
})
2340-
})
2341-
23422306
describe("per-request abortable transport", () => {
23432307
// Earlier tests in this file install a persistent global setTimeout spy
23442308
// (vi.spyOn; vi.clearAllMocks does not restore spy implementations), so

‎src/api/providers/native-ollama.ts‎

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ApiStream } from "../transform/stream"
66
import { BaseProvider } from "./base-provider"
77
import type { ApiHandlerOptions, GetModelsOptions } from "../../shared/api"
88
import { getOllamaModels, isSecureOllamaEndpoint } from "./fetchers/ollama"
9-
import { mergeAbortSignalAndTimeout } from "./utils/abort-signal"
9+
import { createAbortError, mergeAbortSignalAndTimeout, rejectOnAbort } from "./utils/abort-signal"
1010
import { TagMatcher } from "../../utils/tag-matcher"
1111
import type {
1212
SingleCompletionHandler,
@@ -29,47 +29,6 @@ type ReasoningContentBlock = { type: "reasoning"; text: string }
2929
type ThinkingContentBlock = { type: "thinking"; thinking: string }
3030
type AssistantContentBlock = Anthropic.ContentBlock | ReasoningContentBlock | ThinkingContentBlock
3131

32-
/**
33-
* Creates the canonical abort error shared by every cancellation path so
34-
* callers can identify cancellations by `name === "AbortError"`.
35-
*/
36-
function createAbortError(): Error {
37-
const abortError = new Error("This operation was aborted")
38-
abortError.name = "AbortError"
39-
return abortError
40-
}
41-
42-
/**
43-
* Races `pending` against `signal`: if the signal aborts before the promise
44-
* settles, the returned promise rejects with an AbortError instead of
45-
* resolving with a stale result. The abort listener is removed as soon as
46-
* `pending` settles (in either direction), so a reusable caller signal does
47-
* not accumulate one listener per race; a cancelled in-flight request still
48-
* settles in the background.
49-
*/
50-
export function raceWithAbortSignal<T>(pending: Promise<T>, signal: AbortSignal | undefined): Promise<T> {
51-
if (!signal) {
52-
return pending
53-
}
54-
if (signal.aborted) {
55-
return Promise.reject(createAbortError())
56-
}
57-
return new Promise<T>((resolve, reject) => {
58-
const onAbort = () => reject(createAbortError())
59-
signal.addEventListener("abort", onAbort)
60-
pending.then(
61-
(value) => {
62-
signal.removeEventListener("abort", onAbort)
63-
resolve(value)
64-
},
65-
(error) => {
66-
signal.removeEventListener("abort", onAbort)
67-
reject(error)
68-
},
69-
)
70-
})
71-
}
72-
7332
function convertToOllamaMessages(anthropicMessages: Anthropic.Messages.MessageParam[]): Message[] {
7433
const ollamaMessages: Message[] = []
7534
// Track tool use IDs to tool names so tool results can be sent with
@@ -304,9 +263,18 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
304263
// Use constructor `headers` option instead of mutating (request as any).config.
305264
// The credential is only attached when the endpoint is HTTPS or loopback;
306265
// sending it over plaintext HTTP to a remote host would leak it (CWE-319).
307-
if (this.options.ollamaApiKey && isSecureOllamaEndpoint(clientOptions.host)) {
308-
clientOptions.headers = {
309-
Authorization: `Bearer ${this.options.ollamaApiKey}`,
266+
if (this.options.ollamaApiKey) {
267+
if (isSecureOllamaEndpoint(clientOptions.host)) {
268+
clientOptions.headers = {
269+
Authorization: `Bearer ${this.options.ollamaApiKey}`,
270+
}
271+
} else {
272+
// The credential is dropped to avoid leaking it over plaintext
273+
// HTTP (CWE-319); surface the drop so a bare 401 from an auth
274+
// proxy is diagnosable. The key itself is never logged.
275+
console.warn(
276+
`[NativeOllama] ollamaApiKey is set but the endpoint "${clientOptions.host}" is not https or loopback; the Authorization header is not sent (the credential would leak over plaintext). Use an https:// or loopback endpoint to authenticate.`,
277+
)
310278
}
311279
}
312280

@@ -474,7 +442,7 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
474442
if (externalAbortSignal?.aborted) {
475443
// The request is already cancelled: reject before allocating the
476444
// per-request client or transport so nothing is left to clean up.
477-
throw createAbortError()
445+
throw createAbortError("Ollama")
478446
}
479447

480448
const requestController = new AbortController()
@@ -494,7 +462,9 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
494462
// fetch receives the signal so an aborted request is cancelled at
495463
// the network level, and the race rejects a request aborted during
496464
// discovery instead of proceeding to chat() with a stale model.
497-
const { id: modelId } = await raceWithAbortSignal(this.fetchModel(externalAbortSignal), externalAbortSignal)
465+
const { id: modelId } = externalAbortSignal
466+
? await rejectOnAbort(this.fetchModel(externalAbortSignal), externalAbortSignal, "Ollama")
467+
: await this.fetchModel(undefined)
498468
// Stryker disable next-line MethodExpression,StringLiteral: DEEP_SEEK_DEFAULT_TEMPERATURE (0.0) is numerically equal to the 0 fallback, so the R1 check cannot change the emitted temperature
499469
const useR1Format = modelId.toLowerCase().includes("deepseek-r1")
500470

@@ -537,7 +507,7 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
537507
// instead of being consumed after cancellation.
538508
if (externalAbortSignal?.aborted) {
539509
stream.abort()
540-
throw createAbortError()
510+
throw createAbortError("Ollama")
541511
}
542512

543513
let totalInputTokens = 0
@@ -715,7 +685,7 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
715685
if (abortSignal.aborted) {
716686
requestController.abort()
717687
client.abort()
718-
throw createAbortError()
688+
throw createAbortError("Ollama")
719689
} else {
720690
onAbort = () => {
721691
requestController.abort()
@@ -736,7 +706,11 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
736706
// remains the backstop that rejects a stalled discovery instead of
737707
// hanging past the deadline.
738708
const discoverySignal = mergeAbortSignalAndTimeout(requestController.signal, options?.timeoutMs)
739-
const { id: modelId } = await raceWithAbortSignal(this.fetchModel(discoverySignal), discoverySignal)
709+
// mergeAbortSignalAndTimeout yields undefined when neither an external
710+
// signal nor a positive timeout applies; no race is needed then.
711+
const { id: modelId } = discoverySignal
712+
? await rejectOnAbort(this.fetchModel(discoverySignal), discoverySignal, "Ollama")
713+
: await this.fetchModel(discoverySignal)
740714

741715
const useR1Format = modelId.toLowerCase().includes("deepseek-r1")
742716

0 commit comments

Comments
 (0)