Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ToolFailures.ts

66 lines2.9 KB

1// The MCP result of a failed tool call keeps the failure's message and drops its code. So the
2// code travels beside the result: the handler leaves it in the call's cell for analytics, and the
3// latest one per thread and tool stays in memory for a feedback report to name.
4import { OrchestratorMcpFailure, type ThreadId } from "@t3tools/contracts";
5import * as Context from "effect/Context";
6import * as Layer from "effect/Layer";
7import * as Schema from "effect/Schema";
8​
9/** One tool call's declared failure code, written by its handler. */
10export const ToolCallFailure = Context.Reference<{ code?: string } | undefined>(
11 "t3/mcp/ToolCallFailure",
12 { defaultValue: () => undefined },
13);
14​
15const isOrchestratorFailure = Schema.is(OrchestratorMcpFailure);
16​
17/**
18 * Where an error whose tag covers several causes declares a finer closed code. The getter must
19 * pick from a fixed list in the source, never build the code from a value.
20 */
21export const FailureCode: unique symbol = Symbol.for("t3/mcp/FailureCode");
22​
23/**
24 * A declared failure's closed code: the orchestrator's `code`, else the code the error declares
25 * under `FailureCode`, else its tag. Any other field can hold a value, so none is read.
26 */
27export const declaredFailureCode = (error: unknown) => {
28 if (isOrchestratorFailure(error)) return error.code;
29 if (typeof error === "object" && error !== null && FailureCode in error) {
30 const code = error[FailureCode];
31 if (typeof code === "string") return code;
32 }
33 const tag = typeof error === "object" && error !== null && "_tag" in error ? error._tag : null;
34 return typeof tag === "string" ? tag : undefined;
35};
36​
37/** How long after a tool failed a feedback report about that tool is taken to mean that failure. */
38export const TOOL_FAILURE_REPORT_WINDOW_MS = 10 * 60_000;
39const MAX_THREADS = 256;
40​
41const makeToolFailures = () => {
42 // Insertion order is recency, so the first key is the thread that failed longest ago.
43 const byThread = new Map<ThreadId, Map<string, { readonly code: string; readonly at: number }>>();
44 return {
45 note: (threadId: ThreadId, tool: string, code: string, at: number) => {
46 const tools = byThread.get(threadId) ?? new Map();
47 byThread.delete(threadId);
48 byThread.set(threadId, tools.set(tool, { code, at }));
49 if (byThread.size > MAX_THREADS) byThread.delete(byThread.keys().next().value!);
50 },
51 latest: (threadId: ThreadId, tool: string, now: number) => {
52 const failure = byThread.get(threadId)?.get(tool);
53 return failure !== undefined && now - failure.at <= TOOL_FAILURE_REPORT_WINDOW_MS
54 ? failure.code
55 : undefined;
56 },
57 };
58};
59​
60/** The latest failed call of each tool in each thread; remembers nothing without `layer`. */
61export const ToolFailures = Context.Reference<ReturnType<typeof makeToolFailures>>(
62 "t3/mcp/ToolFailures",
63 { defaultValue: () => ({ note: () => {}, latest: () => undefined }) },
64);
65​
66export const layer = Layer.sync(ToolFailures, makeToolFailures);