Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(connectors): isolate tool-list fallback pages by cursor
Afloat315866ae1a50
diff --git a/server/connector-tools-list.test.ts b/server/connector-tools-list.test.tsindex fde2e60519..bc7bf0d6e4 100644--- a/server/connector-tools-list.test.ts+++ b/server/connector-tools-list.test.ts@@ -113,3 +113,64 @@ describe("tools/list fallback cache", () => {});});+++describe("paginated tools/list fallback", () => {+ const page = (id: string, cursor?: string) => ({ jsonrpc: "2.0", id, method: "tools/list", params: cursor === undefined ? {} : { cursor } });+ const reply = (id: string, name: string, nextCursor?: string) => Response.json({ jsonrpc: "2.0", id, result: { tools: [{ name }], ...(nextCursor === undefined ? {} : { nextCursor }) } });++ it("keeps first and continuation pages separate during a backend outage", async () => {+ const backend = "pages-outage";+ await withToolsListFallback(backend, page("seed-first"), async () => reply("seed-first", "read", "second"));+ await withToolsListFallback(backend, page("seed-second", "second"), async () => reply("seed-second", "write"));+ const first = await withToolsListFallback(backend, page("retry-first"), async () => new Response(null, { status: 503 }));+ expect(await first.json()).toEqual({ jsonrpc: "2.0", id: "retry-first", result: { tools: [{ name: "read" }], nextCursor: "second" } });+ const second = await withToolsListFallback(backend, page("retry-second", "second"), async () => new Response(null, { status: 429 }));+ expect(await second.json()).toEqual({ jsonrpc: "2.0", id: "retry-second", result: { tools: [{ name: "write" }] } });+ });++ it("does not substitute a cached first page for a never-seen cursor", async () => {+ const backend = "unseen-page";+ await withToolsListFallback(backend, page("seed"), async () => reply("seed", "read", "missing"));+ const response = await withToolsListFallback(backend, page("missing", "missing"), async () => new Response("unavailable", { status: 503 }));+ expect(response.status).toBe(503);+ expect(await response.text()).toBe("unavailable");+ });++ it("does not suppress a network failure using another page", async () => {+ const backend = "unseen-network-page";+ await withToolsListFallback(backend, page("seed"), async () => reply("seed", "read", "missing"));+ const error = new Error("network disconnected");+ await expect(withToolsListFallback(backend, page("missing", "missing"), async () => { throw error; })).rejects.toBe(error);+ });++ it("replays the matching continuation page after a transport failure", async () => {+ const backend = "matching-network-page";+ await withToolsListFallback(backend, page("seed", "second"), async () => reply("seed", "write"));+ const response = await withToolsListFallback(backend, page("retry", "second"), async () => { throw new Error("offline"); });+ expect(await response.json()).toEqual({ jsonrpc: "2.0", id: "retry", result: { tools: [{ name: "write" }] } });+ });++ it("treats an empty cursor as a distinct opaque continuation token", async () => {+ const backend = "empty-page-cursor";+ await withToolsListFallback(backend, page("first"), async () => reply("first", "read", ""));+ await withToolsListFallback(backend, page("empty", ""), async () => reply("empty", "write"));+ const response = await withToolsListFallback(backend, page("first-again"), async () => new Response(null, { status: 503 }));+ expect((await response.json()).result).toEqual({ tools: [{ name: "read" }], nextCursor: "" });+ });++ it("does not serve fallback pages for invalid cursor values", async () => {+ const backend = "invalid-page-cursor";+ await withToolsListFallback(backend, page("seed"), async () => reply("seed", "read"));+ for (const cursor of [null, 1, {}, []]) {+ const response = await withToolsListFallback(backend, { ...page("invalid"), params: { cursor } }, async () => new Response("bad cursor", { status: 503 }));+ expect(response.status).toBe(503);+ }+ });++ it("never shares cached pages between backend identities", async () => {+ await withToolsListFallback("page-owner-A", page("seed", "second"), async () => reply("seed", "private_tool"));+ const response = await withToolsListFallback("page-owner-B", page("read", "second"), async () => new Response(null, { status: 503 }));+ expect(response.status).toBe(503);+ });+});diff --git a/server/connector-tools-list.ts b/server/connector-tools-list.tsindex 28127c436e..9cb7a25ca2 100644--- a/server/connector-tools-list.ts+++ b/server/connector-tools-list.ts@@ -34,12 +34,16 @@ export function parseMcpResponse(text: string, id: unknown): Record<string, unknreturn frames.filter(candidate).at(-1) ?? frames.map(diagnostic).filter((frame) => frame !== null).at(-1) ?? null;}-const lastToolsLists = new Map<string, unknown>();+const lastToolsLists = new Map<string, Map<string | undefined, unknown>>();-function toolsListId(payload: unknown): { id: unknown } | null {+function toolsListId(payload: unknown): { id: unknown; cursor: string | undefined } | null {if (!payload || typeof payload !== "object" || Array.isArray(payload)) return null;- const frame = payload as { method?: unknown; id?: unknown };- return frame.method === "tools/list" && frame.id !== undefined ? { id: frame.id } : null;+ const frame = payload as { method?: unknown; id?: unknown; params?: unknown };+ if (frame.method !== "tools/list" || frame.id === undefined) return null;+ if (frame.params !== undefined && (!frame.params || typeof frame.params !== "object" || Array.isArray(frame.params))) return null;+ const cursor = (frame.params as { cursor?: unknown } | undefined)?.cursor;+ if (cursor !== undefined && typeof cursor !== "string") return null;+ return { id: frame.id, cursor };}function toolsResult(text: string, id: unknown): unknown {@@ -61,7 +65,7 @@ export async function withToolsListFallback(): Promise<Response> {const request = toolsListId(payload);if (!request) return send();- const cached = lastToolsLists.get(backend);+ const cached = lastToolsLists.get(backend)?.get(request.cursor);const fallback = () => Response.json({ jsonrpc: "2.0", id: request.id, result: cached });let response: Response;try {@@ -72,7 +76,11 @@ export async function withToolsListFallback(}if (response.ok) {const result = toolsResult(await response.clone().text(), request.id);- if (result !== undefined) lastToolsLists.set(backend, result);+ if (result !== undefined) {+ const pages = lastToolsLists.get(backend) ?? new Map<string | undefined, unknown>();+ pages.set(request.cursor, result);+ lastToolsLists.set(backend, pages);+ }} else if (cached !== undefined && (response.status >= 500 || response.status === 429)) {await response.body?.cancel();return fallback();