test(FN-4545): add reliability interaction coverage

Fusion-Task-Id: FN-4545
Fusion-Task-Lineage: 95108429-618e-4a6d-9fa9-7ac2596665a2
This commit is contained in:
Fusion
2026-05-14 20:52:52 -07:00
committed by gsxdsm
parent 8268f609f9
commit bc9e560adb
2 changed files with 157 additions and 0 deletions

View File

@@ -0,0 +1,103 @@
import { describe, expect, it, vi } from "vitest";
import type { Task } from "@fusion/core";
import { AutoRecoveryDispatcher } from "../../auto-recovery.js";
const baseTask = { id: "FN-1", column: "in-progress", recoveryRetryCount: 0 } as Task;
describe("reliability interaction: contamination auto-recovery precedence", () => {
it("bootstrap/4428 deterministic fast paths bypass dispatcher retry handler", async () => {
const issueRetry = vi.fn();
const dispatcher = new AutoRecoveryDispatcher({
taskStore: {} as never,
auditEmitter: { database: vi.fn(async () => {}), git: vi.fn(), filesystem: vi.fn() },
handlers: { issueRetry },
});
const bootstrapRecovered = true;
if (!bootstrapRecovered) {
await dispatcher.dispatch({ class: "branch-cross-contamination", taskId: "FN-1", pausedReason: "branch-cross-contamination" }, {
task: baseTask,
retryCount: 0,
settings: { mode: "programmatic", maxRetries: 3 },
});
}
const crossContaminationAutoRecovered = true;
if (!crossContaminationAutoRecovered) {
await dispatcher.dispatch({ class: "branch-cross-contamination", taskId: "FN-1", pausedReason: "branch-cross-contamination" }, {
task: baseTask,
retryCount: 0,
settings: { mode: "programmatic", maxRetries: 3 },
});
}
expect(issueRetry).not.toHaveBeenCalled();
});
it("dispatcher retry is last step before pause path", async () => {
const issueRetry = vi.fn(async () => {});
const dispatcher = new AutoRecoveryDispatcher({
taskStore: {} as never,
auditEmitter: { database: vi.fn(async () => {}), git: vi.fn(), filesystem: vi.fn() },
handlers: { issueRetry },
});
const decision = await dispatcher.dispatch({
class: "branch-cross-contamination",
taskId: "FN-1",
pausedReason: "branch-cross-contamination",
evidence: { ownCommits: 0, foreignAttributedCommits: 2 },
}, {
task: baseTask,
retryCount: 0,
settings: { mode: "programmatic", maxRetries: 1 },
});
expect(decision.action).toBe("retry");
expect(issueRetry).toHaveBeenCalledOnce();
});
it("mode off and destructive ambiguity preserve pause", () => {
const dispatcher = new AutoRecoveryDispatcher({
taskStore: {} as never,
auditEmitter: { database: vi.fn(async () => {}), git: vi.fn(), filesystem: vi.fn() },
handlers: { issueRetry: vi.fn() },
});
const modeOff = dispatcher.classify({ class: "branch-cross-contamination", taskId: "FN-1", pausedReason: "branch-cross-contamination" }, {
task: baseTask,
retryCount: 0,
settings: { mode: "off", maxRetries: 3 },
});
expect(modeOff.action).toBe("pause");
expect(modeOff.legacyPausedReason).toBe("branch-cross-contamination");
const destructive = dispatcher.classify({
class: "branch-cross-contamination",
taskId: "FN-1",
pausedReason: "branch-cross-contamination",
evidence: { ownCommits: 1, foreignAttributedCommits: 1 },
}, {
task: baseTask,
retryCount: 0,
settings: { mode: "programmatic", maxRetries: 3 },
});
expect(destructive.action).toBe("pause");
});
it("retry budget exhaustion pauses on subsequent event", () => {
const dispatcher = new AutoRecoveryDispatcher({
taskStore: {} as never,
auditEmitter: { database: vi.fn(async () => {}), git: vi.fn(), filesystem: vi.fn() },
handlers: { issueRetry: vi.fn() },
});
const second = dispatcher.classify({ class: "branch-cross-contamination", taskId: "FN-1", pausedReason: "branch-cross-contamination" }, {
task: { ...baseTask, recoveryRetryCount: 1 } as Task,
retryCount: 1,
settings: { mode: "programmatic", maxRetries: 1 },
});
expect(second.action).toBe("pause");
});
});

View File

@@ -0,0 +1,54 @@
import { describe, expect, it, vi } from "vitest";
import { createPostRoomMessageTool, createSendMessageTool } from "../../agent-tools.js";
describe("reliability interaction: message delivery auto-recovery", () => {
it("recovers transient direct-message delivery and returns success", async () => {
const sendMessage = vi
.fn()
.mockRejectedValueOnce(Object.assign(new Error("SQLITE_BUSY"), { code: "SQLITE_BUSY" }))
.mockResolvedValue({ id: "m1" });
const messageStore = { sendMessage } as any;
const tool = createSendMessageTool(messageStore, "agent-a", { autoRecovery: { mode: "programmatic", maxRetries: 3 } as any });
const result = await tool.execute("1", { to_id: "agent-b", content: "hello" } as any);
expect(sendMessage).toHaveBeenCalledTimes(2);
expect(result.content[0]?.text).toContain("Message sent to agent-b");
});
it("recovers transient room-message delivery and returns success", async () => {
const addRoomMessage = vi
.fn()
.mockRejectedValueOnce(new Error("timeout"))
.mockResolvedValue({ id: "r-msg-1" });
const chatStore = {
listRoomMembers: vi.fn().mockReturnValue([{ agentId: "agent-a" }]),
addRoomMessage,
} as any;
const tool = createPostRoomMessageTool(chatStore, "agent-a", { autoRecovery: { mode: "programmatic", maxRetries: 3 } as any });
const result = await tool.execute("1", { roomId: "room-1", content: "hello" } as any);
expect(addRoomMessage).toHaveBeenCalledTimes(2);
expect(result.content[0]?.text).toContain("Room message posted");
});
it("preserves ERROR contract for permanent failures", async () => {
const sendMessage = vi.fn().mockRejectedValue(new Error("recipient not found"));
const messageStore = { sendMessage } as any;
const tool = createSendMessageTool(messageStore, "agent-a", { autoRecovery: { mode: "programmatic", maxRetries: 3 } as any });
const result = await tool.execute("1", { to_id: "agent-b", content: "hello" } as any);
expect(result.content[0]?.text).toBe("ERROR: Failed to send message: recipient not found");
});
it("mode off preserves first-throw ERROR contract", async () => {
const sendMessage = vi
.fn()
.mockRejectedValueOnce(Object.assign(new Error("SQLITE_BUSY"), { code: "SQLITE_BUSY" }));
const messageStore = { sendMessage } as any;
const tool = createSendMessageTool(messageStore, "agent-a", { autoRecovery: { mode: "off", maxRetries: 3 } as any });
const result = await tool.execute("1", { to_id: "agent-b", content: "hello" } as any);
expect(sendMessage).toHaveBeenCalledTimes(1);
expect(result.content[0]?.text).toBe("ERROR: Failed to send message: SQLITE_BUSY");
});
});