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 // An error whose tag covers several causes declares the finer code itself.
21 expect(
22 ToolFailures.declaredFailureCode({
23 _tag: "PullRequestForgeError",
24 [ToolFailures.FailureCode]: "PullRequestForgeRateLimited",
25 }),
26 ).toBe("PullRequestForgeRateLimited");
27 expect(
28 ToolFailures.declaredFailureCode(new Error("/home/ana/app is not a repository")),
29 ).toBeUndefined();
30});
31
32it.effect("keeps each tool's latest failure per thread, for as long as a report can mean it", () =>
33 Effect.gen(function* () {
34 const failures = yield* ToolFailures.ToolFailures;
35 const thread = ThreadId.make("thread:a");
36 failures.note(thread, "link_issue", "IssueUnavailableError", 0);
37 failures.note(thread, "link_issue", "IssueThreadRequiredError", 1_000);
38 expect(failures.latest(thread, "link_issue", 2_000)).toBe("IssueThreadRequiredError");
39 expect(failures.latest(thread, "read_issue", 2_000)).toBeUndefined();
40 expect(failures.latest(ThreadId.make("thread:b"), "link_issue", 2_000)).toBeUndefined();
41 expect(
42 failures.latest(thread, "link_issue", 1_000 + ToolFailures.TOOL_FAILURE_REPORT_WINDOW_MS + 1),
43 ).toBeUndefined();
44
45 // Memory is bounded: the thread that failed longest ago goes first.
46 for (let index = 0; index < 256; index++) {
47 failures.note(ThreadId.make(`thread:${index}`), "link_issue", "IssueUnavailableError", 2_000);
48 }
49 expect(failures.latest(thread, "link_issue", 2_000)).toBeUndefined();
50 expect(failures.latest(ThreadId.make("thread:0"), "link_issue", 2_000)).toBe(
51 "IssueUnavailableError",
52 );
53 }).pipe(Effect.provide(ToolFailures.layer)),
54);