Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(sdk-typescript): distinguish cancellation from timeout (#3369)

Yu Zhang93162b2fd3c1
diff --git a/sdk/typescript/src/transport.ts b/sdk/typescript/src/transport.ts
index 2848cd8e0..253b46799 100644
--- a/sdk/typescript/src/transport.ts
+++ b/sdk/typescript/src/transport.ts
@@ -89,25 +89,35 @@ export class OpenVikingTransport {
body = JSON.stringify(options.body);
}
const controller = new AbortController();
- const abort = () => controller.abort(options.signal?.reason);
+ let abortSource: "caller" | "timeout" | undefined;
+ const abort = () => {
+ if (controller.signal.aborted) return;
+ abortSource = "caller";
+ controller.abort(options.signal?.reason);
+ };
options.signal?.addEventListener("abort", abort, { once: true });
if (options.signal?.aborted) abort();
- const timer = setTimeout(
- () =>
- controller.abort(new DOMException("Request timed out", "TimeoutError")),
- this.timeout,
- );
+ const timer = setTimeout(() => {
+ if (controller.signal.aborted) return;
+ abortSource = "timeout";
+ controller.abort(new DOMException("Request timed out", "TimeoutError"));
+ }, this.timeout);
try {
const init: RequestInit = { method, headers, signal: controller.signal };
if (body !== undefined) init.body = body;
return await consume(await this.fetcher(url, init));
} catch (error) {
- if (error instanceof OpenVikingError) throw error;
- if (controller.signal.aborted)
- throw new OpenVikingError("Request timed out or was aborted", {
+ if (abortSource === "timeout")
+ throw new OpenVikingError("Request timed out", {
code: "DEADLINE_EXCEEDED",
cause: error,
});
+ if (abortSource === "caller")
+ throw new OpenVikingError("Request was aborted by the caller", {
+ code: "ABORTED",
+ cause: error,
+ });
+ if (error instanceof OpenVikingError) throw error;
throw new OpenVikingError(
error instanceof Error ? error.message : "Network request failed",
{ code: "UNAVAILABLE", cause: error },
diff --git a/sdk/typescript/tests/client.test.ts b/sdk/typescript/tests/client.test.ts
index 58acadc9b..f68b94c47 100644
--- a/sdk/typescript/tests/client.test.ts
+++ b/sdk/typescript/tests/client.test.ts
@@ -64,7 +64,9 @@ describe("OpenVikingClient", () => {
});
it("sends dry_run for prune_orphans reindex requests", async () => {
- const fetcher = vi.fn<typeof fetch>().mockResolvedValue(ok({ status: "completed" }));
+ const fetcher = vi
+ .fn<typeof fetch>()
+ .mockResolvedValue(ok({ status: "completed" }));
const client = new OpenVikingClient({
baseUrl: "https://example.com",
fetch: fetcher,
@@ -484,7 +486,62 @@ describe("OpenVikingClient", () => {
await expect(
client.backupOVPack("backup", false, { signal: controller.signal }),
- ).rejects.toMatchObject({ code: "DEADLINE_EXCEEDED" });
+ ).rejects.toMatchObject({
+ code: "ABORTED",
+ message: "Request was aborted by the caller",
+ });
+ });
+
+ it("maps OpenVikingError abort reasons to caller cancellation", async () => {
+ const fetcher = vi
+ .fn<typeof fetch>()
+ .mockImplementation(async (_input, init) => {
+ if (init?.signal?.aborted) throw init.signal.reason;
+ return new Response(new Uint8Array([1]));
+ });
+ const client = new OpenVikingClient({
+ baseUrl: "https://example.com",
+ fetch: fetcher,
+ });
+ const controller = new AbortController();
+ controller.abort(new OpenVikingError("cancelled", { code: "UNAVAILABLE" }));
+
+ await expect(
+ client.backupOVPack("backup", false, { signal: controller.signal }),
+ ).rejects.toMatchObject({
+ code: "ABORTED",
+ message: "Request was aborted by the caller",
+ });
+ });
+
+ it("keeps in-flight caller cancellation distinct from timeouts", async () => {
+ let markStarted: (() => void) | undefined;
+ const started = new Promise<void>((resolve) => {
+ markStarted = resolve;
+ });
+ const fetcher = vi.fn<typeof fetch>().mockImplementation(
+ async (_input, init) =>
+ new Promise<Response>((_resolve, reject) => {
+ markStarted?.();
+ init?.signal?.addEventListener("abort", () =>
+ reject(init.signal?.reason),
+ );
+ }),
+ );
+ const client = new OpenVikingClient({
+ baseUrl: "https://example.com",
+ fetch: fetcher,
+ timeout: 10_000,
+ });
+ const controller = new AbortController();
+
+ const request = client.backupOVPack("backup", false, {
+ signal: controller.signal,
+ });
+ await started;
+ controller.abort(new Error("cancelled"));
+
+ await expect(request).rejects.toMatchObject({ code: "ABORTED" });
});
it("rejects directories before importing or restoring OVPack files", async () => {