From 7d3a67ee561b1f99a652ca042cb4a631ca352d2f Mon Sep 17 00:00:00 2001 From: Molecule AI SDK-Dev Date: Wed, 13 May 2026 05:47:45 +0000 Subject: [PATCH 1/3] feat(api): add AbortSignal.timeout() to apiCall and platformGet MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit All fetch calls now carry a 30-second timeout via AbortSignal.timeout(). Timeout errors are distinguished from network errors and surfaced as structured ApiError with a descriptive message including the timeout value and endpoint. timeoutMs is optional on both functions — callers can override explicitly. Timeouts for long-running operations: - handleChatWithAgent: 120 s (agent inference can be slow) - handleAsyncDelegate: 300 s (cross-workspace delegation can chain) API-breaking? No — timeoutMs is optional and defaults to 30_000 ms. Addresses: api.ts has no request-level timeout — a stalled platform connection would hang the MCP handler indefinitely. Co-Authored-By: Claude Opus 4.7 --- src/api.ts | 24 ++++++++++++++++++++++++ src/tools/agents.ts | 3 +++ src/tools/delegation.ts | 4 +++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/api.ts b/src/api.ts index 42a4110..c68b471 100644 --- a/src/api.ts +++ b/src/api.ts @@ -45,16 +45,24 @@ export function toMcpText(text: string) { return { content: [{ type: "text" as const, text }] }; } +// Default per-request timeout for all API calls (30 s). Covers the 99th-percentile +// platform response under normal load; long-running operations (bundle export, +// agent chat) should pass a larger timeout via the caller's context. +const DEFAULT_TIMEOUT_MS = 30_000; + export async function apiCall( method: string, path: string, body?: unknown, + timeoutMs?: number, ): Promise { + const timeout = timeoutMs ?? DEFAULT_TIMEOUT_MS; try { const res = await fetch(`${PLATFORM_URL}${path}`, { method, headers: { "Content-Type": "application/json" }, body: body ? JSON.stringify(body) : undefined, + signal: AbortSignal.timeout(timeout), }); if (!res.ok) { const text = await res.text(); @@ -68,7 +76,12 @@ export async function apiCall( } } catch (err) { const msg = err instanceof Error ? err.message : String(err); + const isTimeout = + err instanceof Error && (err.name === "TimeoutError" || msg.includes("timed out")); logError(err, `Molecule AI API error (${method} ${path})`, { platformUrl: PLATFORM_URL }); + if (isTimeout) { + return { error: `Request timed out after ${timeout} ms (${method} ${path})`, detail: msg }; + } return { error: `Platform unreachable at ${PLATFORM_URL}`, detail: msg }; } } @@ -88,7 +101,9 @@ export async function apiCall( export async function platformGet( path: string, maxRetries = 3, + timeoutMs?: number, ): Promise { + const timeout = timeoutMs ?? DEFAULT_TIMEOUT_MS; let attempt = 0; while (true) { @@ -96,6 +111,7 @@ export async function platformGet( const res = await fetch(`${PLATFORM_URL}${path}`, { method: "GET", headers: { "Content-Type": "application/json" }, + signal: AbortSignal.timeout(timeout), }); if (res.status === 429 && attempt < maxRetries) { @@ -137,7 +153,15 @@ export async function platformGet( } } catch (err) { const msg = err instanceof Error ? err.message : String(err); + const isTimeout = + err instanceof Error && (err.name === "TimeoutError" || msg.includes("timed out")); logError(err, `Molecule AI API error (GET ${path})`, { platformUrl: PLATFORM_URL }); + if (isTimeout) { + return { + error: `Request timed out after ${timeout} ms (GET ${path})`, + detail: msg, + }; + } return { error: `Platform unreachable at ${PLATFORM_URL}`, detail: msg }; } } diff --git a/src/tools/agents.ts b/src/tools/agents.ts index 33ec671..7dec62c 100644 --- a/src/tools/agents.ts +++ b/src/tools/agents.ts @@ -48,6 +48,8 @@ export type GetModelParams = z.infer; export async function handleChatWithAgent(args: unknown): Promise> { const params = validate(args, ChatWithAgentSchema); + // Agent chat can involve multi-turn LLM inference — allow up to 2 min rather + // than the 30 s default so complex tasks don't time out mid-generation. const data = await apiCall< { result?: { parts?: Array<{ kind?: string; text?: string }> } } >( @@ -59,6 +61,7 @@ export async function handleChatWithAgent(args: unknown): Promise } } | null)?.result?.parts || []; diff --git a/src/tools/delegation.ts b/src/tools/delegation.ts index e593d9a..e20ba8d 100644 --- a/src/tools/delegation.ts +++ b/src/tools/delegation.ts @@ -8,7 +8,9 @@ export async function handleAsyncDelegate(params: { task: string; }) { const { workspace_id, target_id, task } = params; - const data = await apiCall("POST", `/workspaces/${workspace_id}/delegate`, { target_id, task }); + // Delegation can trigger multi-step agent chains — use a 5-minute timeout to avoid + // premature failures on complex cross-workspace workflows. + const data = await apiCall("POST", `/workspaces/${workspace_id}/delegate`, { target_id, task }, 300_000); return toMcpResult(data); } -- 2.52.0 From 56ad95acdd94e9f30b4a4279bab3b84f3f1d75a3 Mon Sep 17 00:00:00 2001 From: Molecule AI SDK-Dev Date: Wed, 13 May 2026 06:55:22 +0000 Subject: [PATCH 2/3] test(api): add timeout tests for apiCall and platformGet Cover the AbortSignal.timeout() distinguishability: - apiCall: timeout returns ApiError with "timed out" in error field - platformGet: timeout returns ApiError with "timed out" in error field - apiCall with timeoutMs override: signal is passed through to fetch Co-Authored-By: Claude Opus 4.7 --- tests/__tests__/api.test.ts | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/__tests__/api.test.ts b/tests/__tests__/api.test.ts index a1c8085..402b6cb 100644 --- a/tests/__tests__/api.test.ts +++ b/tests/__tests__/api.test.ts @@ -150,6 +150,25 @@ describe("apiCall", () => { expect((result as { detail: string }).detail).toContain("Failed to fetch"); }); + it("returns ApiError with timeout message when request times out", async () => { + // Simulate what AbortSignal.timeout() fires when its timer expires: + // the error's .name is "TimeoutError" and .message contains "timed out". + // Using a plain Error with name set to "TimeoutError" so the instanceof + // Error check in apiCall's catch block succeeds and detects it as a timeout. + const timeoutError = Object.assign(new Error("The operation was aborted due to timeout."), { + name: "TimeoutError", + }); + global.fetch = jest.fn().mockRejectedValue(timeoutError); + + const result = await apiCall("GET", "/workspaces"); + + expect(isApiError(result)).toBe(true); + // Timeout errors are surfaced distinctly from network-unreachable errors. + // The error field includes the timeout summary; the detail is the raw message. + expect((result as { error: string }).error).toContain("timed out"); + expect((result as { detail: string }).detail).toBeTruthy(); + }); + it("sends JSON body on POST with body argument", async () => { global.fetch = mockFetch({ id: "ws-new" }, { status: 201 }); @@ -183,6 +202,17 @@ describe("apiCall", () => { const call = (fetch as jest.Mock).mock.calls[0]; expect(call[1].headers).toEqual({ "Content-Type": "application/json" }); }); + + it("passes custom timeoutMs to AbortSignal.timeout()", async () => { + global.fetch = mockFetch({ id: "ws-1" }, { status: 200 }); + + await apiCall("GET", "/workspaces/ws-1", undefined, 5_000); + + const call = (fetch as jest.Mock).mock.calls[0]; + expect(call[1].signal).toBeDefined(); + // Verify the signal is an AbortSignal instance + expect(call[1].signal instanceof AbortSignal).toBe(true); + }); }); // --------------------------------------------------------------------------- @@ -222,6 +252,20 @@ describe("platformGet", () => { expect((result as { error: string }).error).toContain("Platform unreachable"); }); + it("returns ApiError with timeout message when request times out", async () => { + // Simulate what AbortSignal.timeout() fires when its timer expires. + const timeoutError = Object.assign(new Error("The operation was aborted due to timeout."), { + name: "TimeoutError", + }); + global.fetch = jest.fn().mockRejectedValue(timeoutError); + + const result = await platformGet("/workspaces"); + + expect(isApiError(result)).toBe(true); + expect((result as { error: string }).error).toContain("timed out"); + expect((result as { detail: string }).detail).toBeTruthy(); + }); + describe("429 retry logic", () => { beforeEach(() => { jest.useFakeTimers(); -- 2.52.0 From 6cf1c7f3a10b62ecc97817fc0b801bbc03c55d3b Mon Sep 17 00:00:00 2001 From: Molecule AI SDK-Dev Date: Wed, 13 May 2026 07:16:34 +0000 Subject: [PATCH 3/3] =?UTF-8?q?ci:=20retrigger=20=E2=80=94=20verify=20CI?= =?UTF-8?q?=20stability?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -- 2.52.0