Skip to content

Commit 9a3db65

Browse files
authored
fix(voyage): keep embedding batch waits within timeout (#111673)
1 parent 6cb8333 commit 9a3db65

2 files changed

Lines changed: 94 additions & 11 deletions

File tree

‎extensions/voyage/embedding-batch.test.ts‎

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Voyage batch tests cover bounded status/error response reads.
2-
import { describe, expect, it } from "vitest";
2+
import { describe, expect, it, vi } from "vitest";
33
import { runVoyageEmbeddingBatches } from "./embedding-batch.js";
44
import type { VoyageEmbeddingClient } from "./embedding-provider.js";
55
import { voyageEmbeddingBatchTesting as testing } from "./test-support.js";
@@ -76,6 +76,64 @@ function streamingResponse(params: { chunkCount: number; chunkSize: number; stat
7676
}
7777

7878
describe("voyage batch bounded reads", () => {
79+
it("clamps polling to the remaining batch timeout", async () => {
80+
const sleeps: number[] = [];
81+
82+
await expect(
83+
runVoyageEmbeddingBatches({
84+
client: buildClient(),
85+
agentId: "main",
86+
requests: [{ custom_id: "req-0", body: { input: "hello" } }],
87+
wait: true,
88+
pollIntervalMs: 2_000,
89+
timeoutMs: 1_000,
90+
concurrency: 1,
91+
deps: {
92+
now: (() => {
93+
const times = [0, 500];
94+
return () => times.shift() ?? 500;
95+
})(),
96+
sleep: async (ms) => {
97+
sleeps.push(ms);
98+
throw new Error("stop after first wait");
99+
},
100+
uploadBatchJsonlFile: async () => "input-0",
101+
postJsonWithRetry: async () => ({ id: "batch-0", status: "in_progress" }),
102+
},
103+
}),
104+
).rejects.toThrow("stop after first wait");
105+
106+
expect(sleeps).toEqual([500]);
107+
});
108+
109+
it("does not poll status after the batch timeout expires", async () => {
110+
let now = 0;
111+
const statusFetch = vi.fn();
112+
113+
await expect(
114+
runVoyageEmbeddingBatches({
115+
client: buildClient(),
116+
agentId: "main",
117+
requests: [{ custom_id: "req-0", body: { input: "hello" } }],
118+
wait: true,
119+
pollIntervalMs: 1_000,
120+
timeoutMs: 1_000,
121+
concurrency: 1,
122+
deps: {
123+
now: () => now,
124+
sleep: async (ms) => {
125+
now += ms;
126+
},
127+
uploadBatchJsonlFile: async () => "input-0",
128+
postJsonWithRetry: async () => ({ id: "batch-0", status: "in_progress" }),
129+
withRemoteHttpResponse: statusFetch as never,
130+
},
131+
}),
132+
).rejects.toThrow("voyage batch batch-0 timed out after 1000ms");
133+
134+
expect(statusFetch).not.toHaveBeenCalled();
135+
});
136+
79137
it("uses a 16 MiB cap for successful status/error-file responses", () => {
80138
expect(VOYAGE_BATCH_RESPONSE_MAX_BYTES).toBe(16 * 1024 * 1024);
81139
});

‎extensions/voyage/embedding-batch.ts‎

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,14 @@ function resolveVoyageBatchDeps(overrides: Partial<VoyageBatchDeps> | undefined)
7777
function buildVoyageBatchRequest<T>(params: {
7878
client: VoyageEmbeddingClient;
7979
path: string;
80+
signal?: AbortSignal;
8081
onResponse: (res: Response) => Promise<T>;
8182
}) {
8283
const baseUrl = normalizeBatchBaseUrl(params.client);
8384
return {
8485
url: `${baseUrl}/${params.path}`,
8586
ssrfPolicy: params.client.ssrfPolicy,
87+
signal: params.signal,
8688
init: {
8789
headers: buildBatchHeaders(params.client, { json: true }),
8890
},
@@ -130,12 +132,14 @@ async function fetchVoyageBatchStatus(params: {
130132
batchId: string;
131133
deps: VoyageBatchDeps;
132134
maxResponseBytes?: number;
135+
signal?: AbortSignal;
133136
}): Promise<VoyageBatchStatus> {
134137
const maxBytes = params.maxResponseBytes ?? VOYAGE_BATCH_RESPONSE_MAX_BYTES;
135138
return await params.deps.withRemoteHttpResponse(
136139
buildVoyageBatchRequest({
137140
client: params.client,
138141
path: `batches/${params.batchId}`,
142+
signal: params.signal,
139143
onResponse: async (res) => {
140144
await assertOkOrThrowProviderError(res, "voyage.batch-status");
141145
return await readProviderJsonResponse<VoyageBatchStatus>(res, "voyage-batch-status", {
@@ -191,15 +195,34 @@ async function waitForVoyageBatch(params: {
191195
deps: VoyageBatchDeps;
192196
}): Promise<BatchCompletionResult> {
193197
const start = params.deps.now();
198+
const deadline = start + params.timeoutMs;
194199
let current: VoyageBatchStatus | undefined = params.initial;
195200
while (true) {
196-
const status =
197-
current ??
198-
(await fetchVoyageBatchStatus({
199-
client: params.client,
200-
batchId: params.batchId,
201-
deps: params.deps,
202-
}));
201+
let status: VoyageBatchStatus;
202+
if (current) {
203+
status = current;
204+
} else {
205+
const remainingRequestMs = deadline - params.deps.now();
206+
if (remainingRequestMs <= 0) {
207+
throw new Error(`voyage batch ${params.batchId} timed out after ${params.timeoutMs}ms`);
208+
}
209+
const signal = AbortSignal.timeout(Math.max(1, remainingRequestMs));
210+
try {
211+
status = await fetchVoyageBatchStatus({
212+
client: params.client,
213+
batchId: params.batchId,
214+
deps: params.deps,
215+
signal,
216+
});
217+
} catch (error) {
218+
if (signal.aborted) {
219+
throw new Error(`voyage batch ${params.batchId} timed out after ${params.timeoutMs}ms`, {
220+
cause: error,
221+
});
222+
}
223+
throw error;
224+
}
225+
}
203226
const state = status.status ?? "unknown";
204227
await throwIfBatchCompletionError({
205228
provider: "voyage",
@@ -231,11 +254,13 @@ async function waitForVoyageBatch(params: {
231254
if (!params.wait) {
232255
throw new Error(`voyage batch ${params.batchId} still ${state}; wait disabled`);
233256
}
234-
if (params.deps.now() - start > params.timeoutMs) {
257+
const remainingMs = deadline - params.deps.now();
258+
if (remainingMs <= 0) {
235259
throw new Error(`voyage batch ${params.batchId} timed out after ${params.timeoutMs}ms`);
236260
}
237-
params.debug?.(`voyage batch ${params.batchId} ${state}; waiting ${params.pollIntervalMs}ms`);
238-
await params.deps.sleep(params.pollIntervalMs);
261+
const waitMs = Math.min(params.pollIntervalMs, remainingMs);
262+
params.debug?.(`voyage batch ${params.batchId} ${state}; waiting ${waitMs}ms`);
263+
await params.deps.sleep(waitMs);
239264
current = undefined;
240265
}
241266
}

0 commit comments

Comments
 (0)