Open History to see the last commit
1import { expect, it } from "@effect/vitest";
2import { OrchestratorMcpFailure, ThreadId } from "@t3tools/contracts";
3import * as Effect from "effect/Effect";
4
5import * as ToolFailures from "./ToolFailures.ts";
6
7it("reads a failure's closed code and never a value it carries", () => {
8 expect(
9 ToolFailures.declaredFailureCode(
10 new OrchestratorMcpFailure({ code: "thread_not_found", message: "No." }),
11 ),
12 ).toBe("thread_not_found");
13 // Only the orchestrator's `code` is a closed list; another error's may be a value.
14 expect(
15 ToolFailures.declaredFailureCode({
16 _tag: "PullRequestUnavailableError",
17 code: "/home/ana/app",
18 }),
19 ).toBe("PullRequestUnavailableError");
20 expect(
21 ToolFailures.declaredFailureCode(new Error("/home/ana/app is not a repository")),
22 ).toBeUndefined();
23});
24
25it.effect("keeps each tool's latest failure per thread, for as long as a report can mean it", () =>
26 Effect.gen(function* () {
27 const failures = yield* ToolFailures.ToolFailures;
28 const thread = ThreadId.make("thread:a");
29 failures.note(thread, "link_issue", "IssueUnavailableError", 0);
30 failures.note(thread, "link_issue", "IssueThreadRequiredError", 1_000);
31 expect(failures.latest(thread, "link_issue", 2_000)).toBe("IssueThreadRequiredError");
32 expect(failures.latest(thread, "read_issue", 2_000)).toBeUndefined();
33 expect(failures.latest(ThreadId.make("thread:b"), "link_issue", 2_000)).toBeUndefined();
34 expect(
35 failures.latest(thread, "link_issue", 1_000 + ToolFailures.TOOL_FAILURE_REPORT_WINDOW_MS + 1),
36 ).toBeUndefined();
37
38 // Memory is bounded: the thread that failed longest ago goes first.
39 for (let index = 0; index < 256; index++) {
40 failures.note(ThreadId.make(`thread:${index}`), "link_issue", "IssueUnavailableError", 2_000);
41 }
42 expect(failures.latest(thread, "link_issue", 2_000)).toBeUndefined();
43 expect(failures.latest(ThreadId.make("thread:0"), "link_issue", 2_000)).toBe(
44 "IssueUnavailableError",
45 );
46 }).pipe(Effect.provide(ToolFailures.layer)),
47);