Compare commits

..

3 Commits

Author SHA1 Message Date
sdk-dev 6cf1c7f3a1 ci: retrigger — verify CI stability
CI / test (pull_request) Successful in 1m28s
[Do] Manual ack
sop-checklist / all-items-acked SOP checklist acknowledged
2026-05-13 07:16:34 +00:00
sdk-dev 56ad95acdd test(api): add timeout tests for apiCall and platformGet
CI / test (pull_request) Failing after 10m45s
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 <noreply@anthropic.com>
2026-05-13 06:55:22 +00:00
sdk-dev 7d3a67ee56 feat(api): add AbortSignal.timeout() to apiCall and platformGet
CI / test (pull_request) Successful in 58s
sop-checklist / all-items-acked SOP checklist acknowledged by sdk-dev
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 <noreply@anthropic.com>
2026-05-13 06:19:13 +00:00
5 changed files with 74 additions and 41 deletions
-40
View File
@@ -1,40 +0,0 @@
name: gitea-merge-queue
on:
schedule:
- cron: '*/5 * * * *'
workflow_dispatch:
permissions:
contents: read
concurrency:
group: gitea-merge-queue-${{ github.repository }}
cancel-in-progress: false
jobs:
queue:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Check out queue script from main
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
ref: ${{ github.event.repository.default_branch }}
- name: Process one queued PR
env:
GITEA_TOKEN: ${{ secrets.AUTO_SYNC_TOKEN }}
GITEA_HOST: git.moleculesai.app
REPO: ${{ github.repository }}
WATCH_BRANCH: ${{ github.event.repository.default_branch }}
QUEUE_LABEL: merge-queue
HOLD_LABEL: merge-queue-hold
UPDATE_STYLE: merge
# Context names: CI workflow is "CI", job is "test" → "CI / test".
# The CI workflow (.gitea/workflows/ci.yml) triggers on pull_request events,
# so Gitea posts statuses with the "(pull_request)" suffix.
# The queue script also checks the SOP gate: sop-checklist / all-items-acked (pull_request).
REQUIRED_CONTEXTS: >-
CI / test (pull_request), sop-checklist / all-items-acked (pull_request)
run: python3 .gitea/scripts/gitea-merge-queue.py
+24
View File
@@ -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<T = unknown>(
method: string,
path: string,
body?: unknown,
timeoutMs?: number,
): Promise<T | ApiError> {
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<T = unknown>(
}
} 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<T = unknown>(
export async function platformGet<T = unknown>(
path: string,
maxRetries = 3,
timeoutMs?: number,
): Promise<T | ApiError> {
const timeout = timeoutMs ?? DEFAULT_TIMEOUT_MS;
let attempt = 0;
while (true) {
@@ -96,6 +111,7 @@ export async function platformGet<T = unknown>(
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<T = unknown>(
}
} 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 };
}
}
+3
View File
@@ -48,6 +48,8 @@ export type GetModelParams = z.infer<typeof GetModelSchema>;
export async function handleChatWithAgent(args: unknown): Promise<ReturnType<typeof toMcpResult>> {
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<ReturnType<typ
message: { role: "user", parts: [{ type: "text", text: params.message }] },
},
},
120_000, // 2-minute timeout for agent chat
);
const parts =
(data as { result?: { parts?: Array<{ kind?: string; text?: string }> } } | null)?.result?.parts || [];
+3 -1
View File
@@ -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);
}
+44
View File
@@ -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();