From 2b5ea4977f3138692d0132feb10e7f2832fc03d3 Mon Sep 17 00:00:00 2001 From: James Long Date: Tue, 17 Nov 2026 15:42:14 -0500 Subject: [PATCH] fix(codemode): reject an unknown tool's promise instead of throwing (#64510) --- .../codemode/src/codemode/tool-runtime.ts | 5 +- packages/codemode/test/tool-paths.test.ts | 46 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/packages/codemode/src/codemode/tool-runtime.ts b/packages/codemode/src/codemode/tool-runtime.ts index 9a0af41afe3..eabe07d080c 100644 --- a/packages/codemode/src/codemode/tool-runtime.ts +++ b/packages/codemode/src/codemode/tool-runtime.ts @@ -810,7 +810,10 @@ export const globals = ( return searchAlias as Host.Fn } const found = resolve(root, path, prepared.searchIndex) - return found instanceof HostFailure ? found : toolFunction(segments.join("."), found) + // Tools are async, so a call to a missing one rejects its promise rather than throwing at the call. + return found instanceof HostFailure + ? Host.fn(() => Effect.fail(found), { type: "async" }) + : toolFunction(segments.join("."), found) }, }), search: search as Host.Fn, diff --git a/packages/codemode/test/tool-paths.test.ts b/packages/codemode/test/tool-paths.test.ts index ed80e31e375..d827a7a5d91 100644 --- a/packages/codemode/test/tool-paths.test.ts +++ b/packages/codemode/test/tool-paths.test.ts @@ -481,3 +481,49 @@ describe("tool references under ==", () => { ).toEqual([false, false, false, 0]) }) }) + +describe("unknown tool calls reject their promise", () => { + const runtime = CodeMode.make({ tools: { shop: { show: echo("Show", "shown"), list: echo("List", "listed") } } }) + + test("try/await catches the rejection", async () => { + expect( + await value(runtime, `try { await tools.shop.nope({}) } catch (error) { return error.message }`), + ).toBe("Unknown tool 'shop.nope'.") + }) + + test(".catch on the call sees the rejection", async () => { + expect(await value(runtime, `return await tools.shop.nope({}).catch((error) => error.message)`)).toBe( + "Unknown tool 'shop.nope'.", + ) + }) + + test("Promise.allSettled settles an unknown tool as rejected", async () => { + expect( + await value( + runtime, + `const settled = await Promise.allSettled([tools.shop.show({}), tools.shop.nope({})]) + return settled.map((result) => result.status)`, + ), + ).toEqual(["fulfilled", "rejected"]) + }) + + test("calling a namespace rejects too", async () => { + expect(await value(runtime, `return await tools.shop({}).catch((error) => error.message)`)).toBe( + "Tool 'shop' is not callable, it is a namespace. Did you mean tools.shop.list or tools.shop.show?", + ) + }) + + test("an unhandled rejection keeps its diagnostic", async () => { + const unknown = await failure(runtime, `return await tools.shop.shows({})`) + expect(unknown.kind).toBe("UnknownTool") + expect(unknown.message).toBe("Unknown tool 'shop.shows'. Did you mean tools.shop.show?") + expect(unknown.suggestions).toEqual(["Use search to find available tools."]) + }) + + test("reading an unknown path is unchanged", async () => { + expect(await value(runtime, `return [typeof tools.shop.nope, Object.keys(tools.shop)]`)).toEqual([ + "function", + ["show", "list"], + ]) + }) +})