Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6cf1c7f3a1 | |||
| 56ad95acdd | |||
| 7d3a67ee56 |
+24
@@ -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 };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 || [];
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -90,14 +90,13 @@ export async function handleGetRemoteAgentSetupCommand(params: {
|
||||
`WORKSPACE_ID=${w.id} \\`,
|
||||
`PLATFORM_URL=${targetUrl} \\`,
|
||||
`python3 -c "from molecule_agent import RemoteAgentClient; \\`,
|
||||
` c = RemoteAgentClient(workspace_id='${w.id}', platform_url='${targetUrl}'); \\`,
|
||||
` if c.load_token() is None: c.register(); \\`,
|
||||
` c = RemoteAgentClient.register_from_env(); \\`,
|
||||
` c.pull_secrets(); \\`,
|
||||
` c.run_heartbeat_loop()"`,
|
||||
``,
|
||||
`# For a richer demo (logging, graceful shutdown) see`,
|
||||
`# examples/remote-agent/run.py in the molecule-sdk-python checkout.`,
|
||||
`# The agent will register (mint + cache bearer token at`,
|
||||
`# The agent will register, mint its bearer token (cached at`,
|
||||
`# ~/.molecule/${w.id}/.auth_token), pull secrets, then heartbeat.`,
|
||||
].join("\n");
|
||||
return toMcpResult({
|
||||
|
||||
+44
-96
@@ -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();
|
||||
@@ -290,99 +334,3 @@ describe("platformGet", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// remote_agents — handleGetRemoteAgentSetupCommand
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// remote_agents.ts reads PLATFORM_URL at module-load time from process.env.
|
||||
// We use jest.isolateModules so each test gets a fresh module context with
|
||||
// the right env var set before the module is loaded.
|
||||
const originalEnv = process.env.MOLECULE_API_URL;
|
||||
|
||||
describe("handleGetRemoteAgentSetupCommand", () => {
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
process.env.MOLECULE_API_URL = "http://localhost:8080";
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (originalEnv === undefined) {
|
||||
delete process.env.MOLECULE_API_URL;
|
||||
} else {
|
||||
process.env.MOLECULE_API_URL = originalEnv;
|
||||
}
|
||||
});
|
||||
|
||||
async function loadHandlerAndMock(workspace: Record<string, unknown>) {
|
||||
let handler!: typeof import("../../src/tools/remote_agents").handleGetRemoteAgentSetupCommand;
|
||||
let mockGet!: jest.Mock;
|
||||
await new Promise<void>((resolve) => {
|
||||
jest.isolateModules(() => {
|
||||
mockGet = jest.fn().mockResolvedValue(workspace);
|
||||
jest.mock("../../src/api", () => ({
|
||||
...jest.requireActual("../../src/api"),
|
||||
platformGet: mockGet,
|
||||
}));
|
||||
const mod = require("../../src/tools/remote_agents");
|
||||
handler = mod.handleGetRemoteAgentSetupCommand;
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
return { handler, mockGet };
|
||||
}
|
||||
|
||||
it("generates valid Python command with constructor + register pattern", async () => {
|
||||
const { handler } = await loadHandlerAndMock({
|
||||
id: "ws-abc123",
|
||||
name: "my-agent",
|
||||
runtime: "external",
|
||||
});
|
||||
const result = await handler({ workspace_id: "ws-abc123" });
|
||||
const parsed = JSON.parse((result.content[0] as { text: string }).text);
|
||||
expect(parsed.workspace_id).toBe("ws-abc123");
|
||||
expect(parsed.workspace_name).toBe("my-agent");
|
||||
expect(parsed.setup_command).toContain("RemoteAgentClient(workspace_id='ws-abc123'");
|
||||
expect(parsed.setup_command).not.toContain("register_from_env");
|
||||
expect(parsed.setup_command).toContain("register()");
|
||||
});
|
||||
|
||||
it("warns when PLATFORM_URL is localhost and no override is given", async () => {
|
||||
const { handler } = await loadHandlerAndMock({
|
||||
id: "ws-abc123",
|
||||
name: "my-agent",
|
||||
runtime: "external",
|
||||
});
|
||||
const result = await handler({ workspace_id: "ws-abc123" });
|
||||
const parsed = JSON.parse((result.content[0] as { text: string }).text);
|
||||
expect(parsed.warnings).toBeDefined();
|
||||
expect(parsed.warnings![0]).toContain("localhost");
|
||||
});
|
||||
|
||||
it("uses platform_url_override when provided", async () => {
|
||||
const { handler } = await loadHandlerAndMock({
|
||||
id: "ws-abc123",
|
||||
name: "my-agent",
|
||||
runtime: "external",
|
||||
});
|
||||
const result = await handler({
|
||||
workspace_id: "ws-abc123",
|
||||
platform_url_override: "https://platform.example.com",
|
||||
});
|
||||
const parsed = JSON.parse((result.content[0] as { text: string }).text);
|
||||
expect(parsed.setup_command).toContain("platform_url='https://platform.example.com'");
|
||||
expect(parsed.warnings).toBeUndefined();
|
||||
});
|
||||
|
||||
it("returns error when workspace is not runtime=external", async () => {
|
||||
const { handler } = await loadHandlerAndMock({
|
||||
id: "ws-abc123",
|
||||
name: "my-agent",
|
||||
runtime: "docker",
|
||||
});
|
||||
const result = await handler({ workspace_id: "ws-abc123" });
|
||||
const parsed = JSON.parse((result.content[0] as { text: string }).text);
|
||||
expect(parsed.error).toContain("not external");
|
||||
expect(parsed.setup_command).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,164 +0,0 @@
|
||||
/**
|
||||
* Unit tests for src/tools/remote_agents.ts
|
||||
*
|
||||
* Tests handleGetRemoteAgentSetupCommand which generates a Python bootstrap
|
||||
* command for remote agents. Key edge cases:
|
||||
* - localhost warning when PLATFORM_URL is localhost and no override given
|
||||
* - platform_url_override bypasses localhost warning
|
||||
* - non-external runtime returns error
|
||||
* - workspace not found returns error
|
||||
*/
|
||||
|
||||
import { toMcpResult } from "../../src/api";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Factory so each fetch call gets a fresh Response (bodies can only be read once). */
|
||||
function makeFetchResponse(body: unknown, init: ResponseInit = {}): Response {
|
||||
const text = typeof body === "string" ? body : JSON.stringify(body);
|
||||
return new Response(text, {
|
||||
status: init.status ?? 200,
|
||||
statusText: init.statusText,
|
||||
headers: init.headers as HeadersInit,
|
||||
});
|
||||
}
|
||||
|
||||
type RemoteAgentsHandler = {
|
||||
handleGetRemoteAgentSetupCommand: (
|
||||
params: { workspace_id: string; platform_url_override?: string }
|
||||
) => Promise<ReturnType<typeof toMcpResult>>;
|
||||
};
|
||||
|
||||
/**
|
||||
* Dynamically import the remote_agents module with a mocked platformGet.
|
||||
* Must be called inside jest.isolateModules() with MOLECULE_API_URL set.
|
||||
*/
|
||||
async function loadHandlerWithMock(
|
||||
mockPlatformGet: jest.Mock,
|
||||
): Promise<RemoteAgentsHandler> {
|
||||
let handler!: RemoteAgentsHandler;
|
||||
await new Promise<void>((resolve) => {
|
||||
jest.isolateModules(() => {
|
||||
jest.mock("../../src/api", () => ({
|
||||
...jest.requireActual("../../src/api"),
|
||||
platformGet: mockPlatformGet,
|
||||
}));
|
||||
const mod = require("../../src/tools/remote_agents") as RemoteAgentsHandler;
|
||||
handler = mod;
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
return handler;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// handleGetRemoteAgentSetupCommand tests
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe("handleGetRemoteAgentSetupCommand", () => {
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
});
|
||||
|
||||
it("returns a setup command with correct RemoteAgentClient API call", async () => {
|
||||
const mockGet = jest.fn().mockResolvedValue({
|
||||
id: "ws-abc123",
|
||||
name: "test-agent",
|
||||
runtime: "external",
|
||||
});
|
||||
|
||||
const handler = await loadHandlerWithMock(mockGet);
|
||||
const result = await handler.handleGetRemoteAgentSetupCommand({
|
||||
workspace_id: "ws-abc123",
|
||||
});
|
||||
|
||||
expect(result.content[0].text).toContain("ws-abc123");
|
||||
expect(result.content[0].text).toContain("molecule_agent import RemoteAgentClient");
|
||||
// Must use constructor + load_token pattern, NOT the non-existent register_from_env()
|
||||
expect(result.content[0].text).not.toContain("register_from_env()");
|
||||
expect(result.content[0].text).toContain("load_token()");
|
||||
expect(result.content[0].text).toContain("pull_secrets()");
|
||||
expect(result.content[0].text).toContain("run_heartbeat_loop()");
|
||||
});
|
||||
|
||||
it("returns a localhost warning when PLATFORM_URL is localhost and no override given", async () => {
|
||||
// Set localhost as the platform URL before loading the module
|
||||
process.env.MOLECULE_API_URL = "http://localhost:8080";
|
||||
|
||||
const mockGet = jest.fn().mockResolvedValue({
|
||||
id: "ws-abc123",
|
||||
name: "test-agent",
|
||||
runtime: "external",
|
||||
});
|
||||
|
||||
const handler = await loadHandlerWithMock(mockGet);
|
||||
const result = await handler.handleGetRemoteAgentSetupCommand({
|
||||
workspace_id: "ws-abc123",
|
||||
});
|
||||
|
||||
const parsed = JSON.parse(result.content[0].text);
|
||||
expect(parsed.warnings).toBeDefined();
|
||||
expect(parsed.warnings[0]).toContain("localhost");
|
||||
expect(parsed.warnings[0]).toContain("platform_url_override");
|
||||
|
||||
delete process.env.MOLECULE_API_URL;
|
||||
});
|
||||
|
||||
it("platform_url_override bypasses the localhost warning", async () => {
|
||||
// Even with localhost as the base URL, passing an override suppresses the warning
|
||||
process.env.MOLECULE_API_URL = "http://localhost:8080";
|
||||
|
||||
const mockGet = jest.fn().mockResolvedValue({
|
||||
id: "ws-abc123",
|
||||
name: "test-agent",
|
||||
runtime: "external",
|
||||
});
|
||||
|
||||
const handler = await loadHandlerWithMock(mockGet);
|
||||
const result = await handler.handleGetRemoteAgentSetupCommand({
|
||||
workspace_id: "ws-abc123",
|
||||
platform_url_override: "https://platform.example.com",
|
||||
});
|
||||
|
||||
const parsed = JSON.parse(result.content[0].text);
|
||||
expect(parsed.warnings).toBeUndefined();
|
||||
expect(parsed.platform_url).toBe("https://platform.example.com");
|
||||
|
||||
delete process.env.MOLECULE_API_URL;
|
||||
});
|
||||
|
||||
it("returns error when workspace runtime is not 'external'", async () => {
|
||||
const mockGet = jest.fn().mockResolvedValue({
|
||||
id: "ws-abc123",
|
||||
name: "docker-agent",
|
||||
runtime: "docker",
|
||||
});
|
||||
|
||||
const handler = await loadHandlerWithMock(mockGet);
|
||||
const result = await handler.handleGetRemoteAgentSetupCommand({
|
||||
workspace_id: "ws-abc123",
|
||||
});
|
||||
|
||||
const parsed = JSON.parse(result.content[0].text);
|
||||
expect(parsed.error).toContain("not external");
|
||||
expect(parsed.error).toContain("runtime='external'");
|
||||
expect(parsed.actual_runtime).toBe("docker");
|
||||
});
|
||||
|
||||
it("returns error when workspace is not found", async () => {
|
||||
const mockGet = jest.fn().mockResolvedValue({
|
||||
error: "not found",
|
||||
detail: "workspace ws-missing does not exist",
|
||||
});
|
||||
|
||||
const handler = await loadHandlerWithMock(mockGet);
|
||||
const result = await handler.handleGetRemoteAgentSetupCommand({
|
||||
workspace_id: "ws-missing",
|
||||
});
|
||||
|
||||
const parsed = JSON.parse(result.content[0].text);
|
||||
expect(parsed.error).toBeDefined();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user