fix(codemode): reject an unknown tool's promise instead of throwing (#64510)

This commit is contained in:
James Long
2026-11-17 15:42:14 -05:00
committed by GitHub
Unverified
parent e40057ab84
commit 2b5ea4977f
2 changed files with 50 additions and 1 deletions
@@ -810,7 +810,10 @@ export const globals = <R>(
return searchAlias as Host.Fn<R>
}
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<R>,
+46
View File
@@ -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"],
])
})
})