This commit is contained in:
ZacharyZhang-NY committed 2026-04-05 20:14:48 -04:00
1 parent 09971cabc2
commit 9145572422
163 files changed
+3348 -2874

No files matched your search

+74 -108
View File
@@ -1,147 +1,113 @@
import { describe, it, expect, vi } from "vitest";
import { checkBudgetThreshold } from "@main/automation/automation-run-completion";
/**
* Tests for budget alert notification logic.
* Tests for budget alert notification logic in automation-run-completion.ts.
*
* Budget alerts fire at two thresholds:
* 1. 80% utilization -> warning notification with agent name and percentage
* 2. 100% utilization -> paused notification indicating budget exhaustion
* 3. No notification when budgetMonthlyUsd is 0 (unlimited budget)
* 4. Percentage is calculated as Math.round((spentMonthlyUsd / budgetMonthlyUsd) * 100)
* 1. >= 80% utilization -> warning notification with agent name and percentage
* 2. >= 100% utilization -> exhaustion notification (both fire)
* 3. budgetMonthlyUsd <= 0 (unlimited) -> no notification
*/
/**
* Extract and test the budget notification logic that lives in automation-service.ts
* handleRunFinished -> budget threshold section.
*
* The actual code pattern in automation-service.ts:
* if (agent.budgetMonthlyUsd > 0) {
* const utilization = agent.spentMonthlyUsd / agent.budgetMonthlyUsd;
* if (utilization >= 0.8) {
* // 80% threshold warning
* const pct = Math.round(utilization * 100);
* notify("Budget Alert", `${agent.name} is at ${pct}% of monthly budget ...`);
* if (utilization >= 1.0) {
* // 100% exhaustion notification
* notify("Budget Alert", `${agent.name} paused -- monthly budget exhausted ...`);
* }
* }
* }
*/
function simulateBudgetNotification(
notify: (title: string, body: string) => void,
agent: { name: string; budgetMonthlyUsd: number; spentMonthlyUsd: number },
) {
if (agent.budgetMonthlyUsd > 0) {
const utilization = agent.spentMonthlyUsd / agent.budgetMonthlyUsd;
if (utilization >= 0.8) {
const pct = Math.round(utilization * 100);
notify(
"Budget Alert",
`${agent.name} is at ${pct}% of monthly budget ($${agent.spentMonthlyUsd.toFixed(2)} / $${agent.budgetMonthlyUsd.toFixed(2)})`,
);
if (utilization >= 1.0) {
notify(
"Budget Alert",
`${agent.name} paused -- monthly budget exhausted ($${agent.spentMonthlyUsd.toFixed(2)} / $${agent.budgetMonthlyUsd.toFixed(2)})`,
);
}
}
}
/** Identity localizer: returns the English string, as in the production default path. */
function en(enStr: string, _zh: string): string {
return enStr;
}
describe("Budget Alert Notifications", () => {
it("fires warning notification at 80% budget utilization", () => {
const notify = vi.fn();
simulateBudgetNotification(notify, {
name: "Alice",
budgetMonthlyUsd: 100,
spentMonthlyUsd: 85,
});
checkBudgetThreshold(
{ name: "Alice", budgetMonthlyUsd: 100, spentMonthlyUsd: 85 },
notify,
en,
);
expect(notify).toHaveBeenCalledTimes(1);
expect(notify).toHaveBeenCalledWith(
"Budget Alert",
expect.stringContaining("Alice"),
);
expect(notify).toHaveBeenCalledWith(
"Budget Alert",
expect.stringContaining("85%"),
);
expect(notify).toHaveBeenCalledWith(
"Budget Alert",
expect.stringContaining("% of monthly budget"),
);
const call = notify.mock.calls[0]![0] as { title: string; body: string };
expect(call.title).toContain("Budget");
expect(call.body).toContain("Alice");
expect(call.body).toContain("85%");
});
it("fires paused notification at 100% budget utilization", () => {
it("fires both notifications at >= 100% utilization", () => {
const notify = vi.fn();
simulateBudgetNotification(notify, {
name: "Bob",
budgetMonthlyUsd: 50,
spentMonthlyUsd: 55,
});
// Both the 80% and 100% notifications should fire
checkBudgetThreshold(
{ name: "Bob", budgetMonthlyUsd: 50, spentMonthlyUsd: 55 },
notify,
en,
);
expect(notify).toHaveBeenCalledTimes(2);
// Second call should be the exhaustion notification
expect(notify).toHaveBeenCalledWith(
"Budget Alert",
expect.stringContaining("paused"),
);
expect(notify).toHaveBeenCalledWith(
"Budget Alert",
expect.stringContaining("budget exhausted"),
);
const bodies = notify.mock.calls.map((c) => (c[0] as { body: string }).body);
// Second call is the exhaustion notification
expect(bodies[1]).toContain("100%");
});
it("correctly calculates percentage as Math.round(utilization * 100)", () => {
it("correctly rounds percentage as Math.round(utilization * 100)", () => {
const notify = vi.fn();
// 83.333...% should round to 83%
simulateBudgetNotification(notify, {
name: "Charlie",
budgetMonthlyUsd: 60,
spentMonthlyUsd: 50,
});
expect(notify).toHaveBeenCalledWith(
"Budget Alert",
expect.stringContaining("83%"),
checkBudgetThreshold(
{ name: "Charlie", budgetMonthlyUsd: 60, spentMonthlyUsd: 50 },
notify,
en,
);
const body = (notify.mock.calls[0]![0] as { body: string }).body;
expect(body).toContain("83%");
});
it("does not fire notification when budgetMonthlyUsd is 0 (unlimited)", () => {
const notify = vi.fn();
simulateBudgetNotification(notify, {
name: "Diana",
budgetMonthlyUsd: 0,
spentMonthlyUsd: 500,
});
checkBudgetThreshold(
{ name: "Diana", budgetMonthlyUsd: 0, spentMonthlyUsd: 500 },
notify,
en,
);
expect(notify).not.toHaveBeenCalled();
});
it("does not fire notification when utilization is below 80%", () => {
const notify = vi.fn();
simulateBudgetNotification(notify, {
name: "Eve",
budgetMonthlyUsd: 100,
spentMonthlyUsd: 50,
});
checkBudgetThreshold(
{ name: "Eve", budgetMonthlyUsd: 100, spentMonthlyUsd: 50 },
notify,
en,
);
expect(notify).not.toHaveBeenCalled();
});
it("fires both notifications at exactly 100%", () => {
const notify = vi.fn();
simulateBudgetNotification(notify, {
name: "Frank",
budgetMonthlyUsd: 100,
spentMonthlyUsd: 100,
});
checkBudgetThreshold(
{ name: "Frank", budgetMonthlyUsd: 100, spentMonthlyUsd: 100 },
notify,
en,
);
expect(notify).toHaveBeenCalledTimes(2);
// First call: 80% threshold warning
expect(notify.mock.calls[0]![0]).toBe("Budget Alert");
expect(notify.mock.calls[0]![1]).toContain("100%");
expect(notify.mock.calls[0]![1]).toContain("% of monthly budget");
// Second call: exhaustion notification
expect(notify.mock.calls[1]![0]).toBe("Budget Alert");
expect(notify.mock.calls[1]![1]).toContain("paused");
expect(notify.mock.calls[1]![1]).toContain("budget exhausted");
const first = notify.mock.calls[0]![0] as { title: string; body: string };
const second = notify.mock.calls[1]![0] as { title: string; body: string };
expect(first.body).toContain("100%");
expect(second.title).toContain("Exhausted");
});
it("does not fire notification when budgetMonthlyUsd is negative", () => {
const notify = vi.fn();
checkBudgetThreshold(
{ name: "Grace", budgetMonthlyUsd: -10, spentMonthlyUsd: 500 },
notify,
en,
);
expect(notify).not.toHaveBeenCalled();
});
it("fires only the warning (not exhaustion) at exactly 80%", () => {
const notify = vi.fn();
checkBudgetThreshold(
{ name: "Hank", budgetMonthlyUsd: 100, spentMonthlyUsd: 80 },
notify,
en,
);
expect(notify).toHaveBeenCalledTimes(1);
const body = (notify.mock.calls[0]![0] as { body: string }).body;
expect(body).toContain("80%");
});
});
-135
View File
@@ -1,17 +1,11 @@
import { describe, expect, it } from "vitest";
import {
matchConnector,
inferTaskType,
getConnectorStrengths,
TASK_TYPE_PREFERENCE,
CONNECTOR_STRENGTHS as _CONNECTOR_STRENGTHS,
ROLE_DEPARTMENT_HINTS as _ROLE_DEPARTMENT_HINTS,
} from "../../src/main/connector-matching";
import type { ConnectorId, ConnectorStatus } from "@shared/types";
function makeConnector(id: ConnectorId, status: ConnectorStatus = "ready", label?: string) {
return { id, status, label: label ?? id };
}
describe("connector-matching", () => {
describe("TASK_TYPE_PREFERENCE", () => {
@@ -129,133 +123,4 @@ describe("connector-matching", () => {
expect(result.length).toBeGreaterThan(10);
});
});
describe("matchConnector", () => {
const allReady = [
makeConnector("claude_local", "ready", "Claude Code"),
makeConnector("codex_local", "ready", "Codex"),
makeConnector("gemini_local", "ready", "Gemini CLI"),
];
it("recommends claude_local for code tasks when all ready", () => {
const result = matchConnector({
taskType: "code",
agentRole: "engineer",
agentDepartment: "engineering",
availableConnectors: allReady,
});
expect(result.recommendedConnectorId).toBe("claude_local");
});
it("recommends gemini_local for research tasks when all ready", () => {
const result = matchConnector({
taskType: "research",
agentRole: "researcher",
agentDepartment: "research",
availableConnectors: allReady,
});
expect(result.recommendedConnectorId).toBe("gemini_local");
});
it("recommends claude_local for content tasks when all ready", () => {
const result = matchConnector({
taskType: "content",
agentRole: "marketer",
agentDepartment: "marketing",
availableConnectors: [
makeConnector("claude_local", "ready", "Claude Code"),
],
});
expect(result.recommendedConnectorId).toBe("claude_local");
});
it("falls back when preferred connector is offline", () => {
const result = matchConnector({
taskType: "research",
agentRole: "researcher",
agentDepartment: "research",
availableConnectors: [
makeConnector("gemini_local", "not_installed", "Gemini CLI"),
makeConnector("claude_local", "ready", "Claude Code"),
makeConnector("codex_local", "ready", "Codex"),
],
});
// gemini is not ready, so should fallback to claude_local (next in research preference)
expect(result.recommendedConnectorId).toBe("claude_local");
});
it("uses first available connector when none are ready", () => {
const result = matchConnector({
taskType: "code",
agentRole: "engineer",
agentDepartment: "engineering",
availableConnectors: [
makeConnector("claude_local", "detected", "Claude Code"),
makeConnector("codex_local", "auth_required", "Codex"),
],
});
// None ready, should pick first available
expect(result.recommendedConnectorId).toBe("claude_local");
});
it("returns claude_local as ultimate fallback with empty connector list", () => {
const result = matchConnector({
taskType: "code",
agentRole: "engineer",
agentDepartment: "engineering",
availableConnectors: [],
});
expect(result.recommendedConnectorId).toBe("claude_local");
});
it("returns a different fallback when multiple connectors are ready", () => {
const result = matchConnector({
taskType: "code",
agentRole: "engineer",
agentDepartment: "engineering",
availableConnectors: allReady,
});
expect(result.fallbackConnectorId).not.toBeNull();
expect(result.fallbackConnectorId).not.toBe(result.recommendedConnectorId);
});
it("returns null fallback when only one connector is ready", () => {
const result = matchConnector({
taskType: "code",
agentRole: "engineer",
agentDepartment: "engineering",
availableConnectors: [
makeConnector("claude_local", "ready", "Claude Code"),
makeConnector("codex_local", "not_installed", "Codex"),
],
});
expect(result.fallbackConnectorId).toBeNull();
});
it("includes a descriptive reason string", () => {
const result = matchConnector({
taskType: "code",
agentRole: "engineer",
agentDepartment: "engineering",
availableConnectors: allReady,
});
expect(result.reason).toBeTruthy();
expect(typeof result.reason).toBe("string");
expect(result.reason.length).toBeGreaterThan(5);
});
it("handles degraded connectors as non-ready", () => {
const result = matchConnector({
taskType: "research",
agentRole: "analyst",
agentDepartment: "research",
availableConnectors: [
makeConnector("gemini_local", "degraded", "Gemini CLI"),
makeConnector("claude_local", "ready", "Claude Code"),
],
});
// gemini is degraded, should skip to claude_local
expect(result.recommendedConnectorId).toBe("claude_local");
});
});
});
+122 -180
View File
@@ -1,167 +1,118 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import {
interruptStaleRunsLogic,
retryInterruptedTaskLogic,
markInterruptedTaskFailedLogic,
type StaleRunDb,
type RetryTaskDb,
type FailTaskDb,
} from "@main/database/crash-recovery-helpers";
/**
* Tests for crash recovery detection and user notification.
*
* Covers:
* - interruptStaleRuns returns correct count and marks tasks as blocked
* - getInterruptedRunsForRecovery returns correct shape
* - retryInterruptedTask resets task to 'todo' and clears retry_count
* - markInterruptedTaskFailed sets task to 'cancelled'
* - interruptStaleRunsLogic returns correct count and marks tasks as blocked
* - retryInterruptedTaskLogic resets task to 'todo' and clears retry_count
* - markInterruptedTaskFailedLogic sets task to 'cancelled'
*
* getInterruptedRunsForRecovery is a pure SQL query with a well-defined JOIN
* shape; its contract is tested via the return-type assertions below.
*/
interface MockStatement {
run: (...args: unknown[]) => unknown;
get: (...args: unknown[]) => unknown;
all: (...args: unknown[]) => unknown[];
}
interface MockDatabase {
prepare: (key: string) => MockStatement;
}
/**
* Simulate interruptStaleRuns logic as implemented in database.ts
*/
function simulateInterruptStaleRuns(
db: MockDatabase,
releaseAllWorkspaceLocks: () => void,
transactionFn: (fn: () => void) => void,
runningCount: number,
): number {
const ts = new Date().toISOString();
let interruptedCount = 0;
transactionFn(() => {
const updateRunsResult = { changes: runningCount };
// Simulate: update runs set status = 'interrupted'
db.prepare("update-runs-interrupted").run(ts, ts);
interruptedCount = Number(updateRunsResult.changes);
if (interruptedCount > 0) {
// Mark associated tasks as blocked
db.prepare("update-tasks-blocked").run(ts, ts);
}
// Clear active_run_id
db.prepare("update-tasks-active-run-null").run();
// Reset agents to idle
db.prepare("update-agents-idle").run(ts);
releaseAllWorkspaceLocks();
});
return interruptedCount;
}
/**
* Simulate getInterruptedRunsForRecovery
*/
function simulateGetInterruptedRunsForRecovery(
queryResult: Array<{
runId: string;
taskId: string;
taskTitle: string;
agentId: string;
agentName: string;
connectorId: string;
interruptedAt: string;
}>,
): typeof queryResult {
return queryResult;
}
/**
* Simulate retryInterruptedTask
*/
function simulateRetryInterruptedTask(
db: MockDatabase,
taskId: string,
): void {
const ts = new Date().toISOString();
db.prepare("update-task-todo").run(ts, taskId);
db.prepare("update-task-retry-reset").run(ts, taskId);
}
/**
* Simulate markInterruptedTaskFailed
*/
function simulateMarkInterruptedTaskFailed(
db: MockDatabase,
taskId: string,
): void {
const ts = new Date().toISOString();
db.prepare("update-task-cancelled").run(ts, taskId);
function makeStaleRunDb(runningCount: number): StaleRunDb & { calls: string[] } {
const calls: string[] = [];
return {
calls,
interruptRunningRuns: vi.fn((_ts: string) => {
calls.push("interruptRunningRuns");
return runningCount;
}) as unknown as StaleRunDb["interruptRunningRuns"],
blockInterruptedTasks: vi.fn((_ts: string) => {
calls.push("blockInterruptedTasks");
}) as unknown as StaleRunDb["blockInterruptedTasks"],
clearActiveRunIds: vi.fn(() => {
calls.push("clearActiveRunIds");
}) as unknown as StaleRunDb["clearActiveRunIds"],
resetAgentsToIdle: vi.fn((_ts: string) => {
calls.push("resetAgentsToIdle");
}) as unknown as StaleRunDb["resetAgentsToIdle"],
releaseAllWorkspaceLocks: vi.fn(() => {
calls.push("releaseAllWorkspaceLocks");
}) as unknown as StaleRunDb["releaseAllWorkspaceLocks"],
};
}
describe("Crash Recovery Detection", () => {
let db: MockDatabase;
let releaseAllWorkspaceLocks: () => void;
const transaction = (fn: () => void) => fn();
const nowIso = () => new Date().toISOString();
beforeEach(() => {
db = {
prepare: vi.fn().mockReturnValue({
run: vi.fn().mockReturnValue({ changes: 0 }),
get: vi.fn().mockReturnValue(undefined),
all: vi.fn().mockReturnValue([]),
}),
} as unknown as MockDatabase;
releaseAllWorkspaceLocks = vi.fn() as unknown as () => void;
});
describe("interruptStaleRuns", () => {
describe("interruptStaleRunsLogic", () => {
it("returns 0 when no running runs exist", () => {
const transaction = (fn: () => void) => fn();
const count = simulateInterruptStaleRuns(db, releaseAllWorkspaceLocks, transaction, 0);
const db = makeStaleRunDb(0);
const count = interruptStaleRunsLogic(db, transaction, nowIso);
expect(count).toBe(0);
});
it("returns correct count when running runs exist", () => {
const transaction = (fn: () => void) => fn();
const count = simulateInterruptStaleRuns(db, releaseAllWorkspaceLocks, transaction, 3);
const db = makeStaleRunDb(3);
const count = interruptStaleRunsLogic(db, transaction, nowIso);
expect(count).toBe(3);
});
it("marks associated tasks as blocked when interrupted runs exist", () => {
const transaction = (fn: () => void) => fn();
const count = simulateInterruptStaleRuns(db, releaseAllWorkspaceLocks, transaction, 2);
expect(count).toBe(2);
// The blocked update should have been called (db.prepare was called with blocked query)
expect(db.prepare).toHaveBeenCalled();
const db = makeStaleRunDb(2);
interruptStaleRunsLogic(db, transaction, nowIso);
expect(db.calls).toContain("blockInterruptedTasks");
});
it("does NOT mark tasks as blocked when no runs were interrupted", () => {
const callLog: string[] = [];
db.prepare = vi.fn().mockImplementation((query: string) => {
callLog.push(query);
return {
run: vi.fn().mockReturnValue({ changes: 0 }),
get: vi.fn(),
all: vi.fn(),
};
});
const db = makeStaleRunDb(0);
interruptStaleRunsLogic(db, transaction, nowIso);
expect(db.calls).not.toContain("blockInterruptedTasks");
});
const transaction = (fn: () => void) => fn();
const count = simulateInterruptStaleRuns(db, releaseAllWorkspaceLocks, transaction, 0);
expect(count).toBe(0);
// The "update-tasks-blocked" query should NOT have been called
expect(callLog).not.toContain("update-tasks-blocked");
it("always clears active_run_id and resets agents to idle", () => {
const db = makeStaleRunDb(0);
interruptStaleRunsLogic(db, transaction, nowIso);
expect(db.calls).toContain("clearActiveRunIds");
expect(db.calls).toContain("resetAgentsToIdle");
});
it("releases all workspace locks", () => {
const transaction = (fn: () => void) => fn();
simulateInterruptStaleRuns(db, releaseAllWorkspaceLocks, transaction, 1);
const db = makeStaleRunDb(1);
interruptStaleRunsLogic(db, transaction, nowIso);
expect(db.calls).toContain("releaseAllWorkspaceLocks");
});
expect(releaseAllWorkspaceLocks).toHaveBeenCalled();
it("executes all steps inside the transaction", () => {
const db = makeStaleRunDb(1);
const txCalls: string[] = [];
const trackingTransaction = (fn: () => void) => {
txCalls.push("begin");
fn();
txCalls.push("end");
};
interruptStaleRunsLogic(db, trackingTransaction, nowIso);
expect(txCalls[0]).toBe("begin");
expect(txCalls[txCalls.length - 1]).toBe("end");
expect(db.calls.length).toBeGreaterThan(0);
});
});
describe("getInterruptedRunsForRecovery", () => {
it("returns run details for interrupted runs with blocked tasks", () => {
const mockResults = [
describe("getInterruptedRunsForRecovery (shape contract)", () => {
it("result entries have required fields", () => {
// This function is a pure SQL JOIN; we verify its return shape here
// by asserting the TypeScript type it must conform to.
const mockResult: Array<{
runId: string;
taskId: string;
taskTitle: string;
agentId: string;
agentName: string;
connectorId: string;
interruptedAt: string;
}> = [
{
runId: "run-1",
taskId: "task-1",
@@ -171,73 +122,64 @@ describe("Crash Recovery Detection", () => {
connectorId: "claude-code",
interruptedAt: "2026-03-22T07:00:00Z",
},
{
runId: "run-2",
taskId: "task-2",
taskTitle: "Write tests",
agentId: "agent-2",
agentName: "QA Engineer",
connectorId: "codex",
interruptedAt: "2026-03-22T07:00:00Z",
},
];
const result = simulateGetInterruptedRunsForRecovery(mockResults);
expect(result).toHaveLength(2);
expect(result[0]).toEqual(
expect(mockResult[0]).toEqual(
expect.objectContaining({
runId: "run-1",
taskId: "task-1",
taskTitle: "Build feature",
agentName: "Developer",
runId: expect.any(String),
taskId: expect.any(String),
taskTitle: expect.any(String),
agentName: expect.any(String),
interruptedAt: expect.any(String),
}),
);
});
it("returns empty array when no interrupted runs with blocked tasks exist", () => {
const result = simulateGetInterruptedRunsForRecovery([]);
const result: Array<unknown> = [];
expect(result).toHaveLength(0);
expect(result).toEqual([]);
});
});
describe("retryInterruptedTask", () => {
it("sets task to todo and resets retry_count", () => {
const runCalls: Array<{ query: string; args: unknown[] }> = [];
db.prepare = vi.fn().mockImplementation((query: string) => ({
run: vi.fn((...args: unknown[]) => {
runCalls.push({ query, args });
return { changes: 1 };
}),
}));
describe("retryInterruptedTaskLogic", () => {
let db: RetryTaskDb;
simulateRetryInterruptedTask(db, "task-42");
beforeEach(() => {
db = {
setTaskTodo: vi.fn(),
resetTaskRetryFields: vi.fn(),
};
});
expect(runCalls).toHaveLength(2);
// First call sets status to 'todo'
expect(runCalls[0]!.args).toContain("task-42");
// Second call resets retry count
expect(runCalls[1]!.args).toContain("task-42");
it("sets task to todo and resets retry fields", () => {
retryInterruptedTaskLogic(db, "task-42", nowIso);
expect(db.setTaskTodo).toHaveBeenCalledWith(expect.any(String), "task-42");
expect(db.resetTaskRetryFields).toHaveBeenCalledWith(expect.any(String), "task-42");
});
it("calls both statements with the same timestamp", () => {
const fixedTs = "2026-01-01T00:00:00.000Z";
retryInterruptedTaskLogic(db, "task-42", () => fixedTs);
expect(db.setTaskTodo).toHaveBeenCalledWith(fixedTs, "task-42");
expect(db.resetTaskRetryFields).toHaveBeenCalledWith(fixedTs, "task-42");
});
});
describe("markInterruptedTaskFailed", () => {
describe("markInterruptedTaskFailedLogic", () => {
let db: FailTaskDb;
beforeEach(() => {
db = { setTaskCancelled: vi.fn() };
});
it("sets task to cancelled", () => {
const runCalls: Array<{ query: string; args: unknown[] }> = [];
db.prepare = vi.fn().mockImplementation((query: string) => ({
run: vi.fn((...args: unknown[]) => {
runCalls.push({ query, args });
return { changes: 1 };
}),
}));
markInterruptedTaskFailedLogic(db, "task-99", nowIso);
expect(db.setTaskCancelled).toHaveBeenCalledWith(expect.any(String), "task-99");
});
simulateMarkInterruptedTaskFailed(db, "task-99");
expect(runCalls).toHaveLength(1);
expect(runCalls[0]!.args).toContain("task-99");
it("passes the taskId correctly", () => {
const fixedTs = "2026-01-01T00:00:00.000Z";
markInterruptedTaskFailedLogic(db, "task-99", () => fixedTs);
expect(db.setTaskCancelled).toHaveBeenCalledWith(fixedTs, "task-99");
});
});
});
+75 -154
View File
@@ -1,4 +1,9 @@
import { describe, it, expect, vi, beforeEach, afterEach, type MockInstance } from "vitest";
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import {
handleFailureEscalation,
handleSuccessReset,
type EscalationDeps,
} from "@main/worker/failure-escalation";
/**
* Tests for the retry-aware failure escalation pipeline.
@@ -11,125 +16,10 @@ import { describe, it, expect, vi, beforeEach, afterEach, type MockInstance } fr
* 5. Task succeeds -> reset retry count
*/
// We test the escalation logic by extracting the relevant handler from worker-service
// and verifying the calls it makes to the database and notification system.
interface MockDb {
getTaskRetryCount: MockInstance & ((taskId: string) => number);
incrementTaskRetryCount: MockInstance & ((taskId: string) => void);
resetTaskRetryCount: MockInstance & ((taskId: string) => void);
getChainOfCommand: MockInstance & ((agentId: string) => { id: string; name: string }[]);
getCeoForCompany: MockInstance & ((companyId: string) => { id: string; name: string } | null);
advanceTaskStatus: MockInstance & ((taskId: string, status: string, note: string) => { previousStatus: string });
}
interface MockDeps {
db: MockDb;
wakeAgentIfPossible: MockInstance & ((agentId: string, companyId: string, reason: string) => string);
notify: MockInstance & ((...args: unknown[]) => void);
logger: { info: MockInstance & ((...args: unknown[]) => void); warn: MockInstance & ((...args: unknown[]) => void); error: MockInstance & ((...args: unknown[]) => void) };
}
const MAX_RETRY_ATTEMPTS = 2;
const RETRY_COOLDOWN_MS = 30_000;
/**
* Simulate the escalation logic extracted from worker-service.ts.
* This mirrors the actual implementation to verify behavior.
*/
function simulateFailureEscalation(
deps: MockDeps,
run: { taskId: string; agentId: string; companyId: string },
task: { title: string } | null,
eventStatus: "failed" | "timed_out",
): { action: string; retryScheduled?: boolean } {
const { db, wakeAgentIfPossible, notify, logger } = deps;
if (eventStatus === "failed" || eventStatus === "timed_out") {
const retryCount = db.getTaskRetryCount(run.taskId);
if (retryCount < MAX_RETRY_ATTEMPTS) {
db.incrementTaskRetryCount(run.taskId);
db.advanceTaskStatus(
run.taskId,
"todo",
`Run ${eventStatus} (attempt ${retryCount + 1}/${MAX_RETRY_ATTEMPTS}). Auto-retrying after ${RETRY_COOLDOWN_MS / 1000}s cooldown.`,
);
logger.info(
`[escalation] Retrying task ${run.taskId} (attempt ${retryCount + 1}/${MAX_RETRY_ATTEMPTS}) for agent ${run.agentId}`,
);
// In real code this is setTimeout; we just mark it
wakeAgentIfPossible(run.agentId, run.companyId, "retry_failed");
return { action: "retry", retryScheduled: true };
} else {
db.resetTaskRetryCount(run.taskId);
logger.warn(
`[escalation] Task ${run.taskId} failed ${MAX_RETRY_ATTEMPTS} times. Escalating to manager.`,
);
let escalated = false;
try {
const chain = db.getChainOfCommand(run.agentId);
if (chain.length > 0) {
wakeAgentIfPossible(chain[0].id, run.companyId, "report_failed");
escalated = true;
logger.info(
`[escalation] Escalated task ${run.taskId} to manager ${chain[0].name} (${chain[0].id})`,
);
}
} catch {
// Chain lookup failed (agent deleted)
}
if (!escalated) {
try {
const ceo = db.getCeoForCompany(run.companyId);
if (ceo) {
wakeAgentIfPossible(ceo.id, run.companyId, "report_failed");
escalated = true;
logger.info(
`[escalation] Escalated task ${run.taskId} to CEO ${ceo.name} (${ceo.id})`,
);
}
} catch {
/* CEO lookup failed */
}
}
if (!escalated) {
const taskTitle = task?.title ?? run.taskId;
notify(
"Task failed -- manual intervention needed",
`"${taskTitle}" failed ${MAX_RETRY_ATTEMPTS} times and no manager is available to handle it.`,
);
logger.error(
`[escalation] Task ${run.taskId} failed with no escalation target. User notified.`,
);
return { action: "user_notified" };
}
return { action: "escalated" };
}
}
return { action: "none" };
}
function simulateSuccessReset(
deps: MockDeps,
run: { taskId: string },
eventStatus: "succeeded",
): void {
if (eventStatus === "succeeded") {
try {
deps.db.resetTaskRetryCount(run.taskId);
} catch {
/* best effort */
}
}
}
function createMockDeps(): MockDeps {
function createMockDeps(): EscalationDeps {
return {
db: {
getTaskRetryCount: vi.fn().mockReturnValue(0),
@@ -146,17 +36,19 @@ function createMockDeps(): MockDeps {
warn: vi.fn(),
error: vi.fn(),
},
} as unknown as MockDeps;
};
}
const RUN = { taskId: "task-1", agentId: "agent-1", companyId: "company-1" };
const TASK = { title: "Implement feature X" };
describe("Failure Escalation Pipeline", () => {
let deps: MockDeps;
let deps: EscalationDeps;
let scheduleRetry: ReturnType<typeof vi.fn<(fn: () => void, delayMs: number) => void>>;
beforeEach(() => {
deps = createMockDeps();
scheduleRetry = vi.fn<(fn: () => void, delayMs: number) => void>();
vi.useFakeTimers();
});
@@ -166,9 +58,11 @@ describe("Failure Escalation Pipeline", () => {
describe("Retry path (count < MAX_RETRY_ATTEMPTS)", () => {
it("retries on first failure (retry_count=0)", () => {
deps.db.getTaskRetryCount.mockReturnValue(0);
(deps.db.getTaskRetryCount as ReturnType<typeof vi.fn>).mockReturnValue(0);
const result = simulateFailureEscalation(deps, RUN, TASK, "failed");
const result = handleFailureEscalation(
deps, RUN, TASK, "failed", MAX_RETRY_ATTEMPTS, RETRY_COOLDOWN_MS, scheduleRetry,
);
expect(result.action).toBe("retry");
expect(deps.db.incrementTaskRetryCount).toHaveBeenCalledWith("task-1");
@@ -177,13 +71,15 @@ describe("Failure Escalation Pipeline", () => {
"todo",
expect.stringContaining("attempt 1/2"),
);
expect(deps.wakeAgentIfPossible).toHaveBeenCalledWith("agent-1", "company-1", "retry_failed");
expect(scheduleRetry).toHaveBeenCalled();
});
it("retries on second failure (retry_count=1)", () => {
deps.db.getTaskRetryCount.mockReturnValue(1);
(deps.db.getTaskRetryCount as ReturnType<typeof vi.fn>).mockReturnValue(1);
const result = simulateFailureEscalation(deps, RUN, TASK, "failed");
const result = handleFailureEscalation(
deps, RUN, TASK, "failed", MAX_RETRY_ATTEMPTS, RETRY_COOLDOWN_MS, scheduleRetry,
);
expect(result.action).toBe("retry");
expect(deps.db.incrementTaskRetryCount).toHaveBeenCalledWith("task-1");
@@ -192,18 +88,33 @@ describe("Failure Escalation Pipeline", () => {
"todo",
expect.stringContaining("attempt 2/2"),
);
expect(scheduleRetry).toHaveBeenCalled();
});
it("calls wakeAgentIfPossible via scheduleRetry callback", () => {
(deps.db.getTaskRetryCount as ReturnType<typeof vi.fn>).mockReturnValue(0);
handleFailureEscalation(
deps, RUN, TASK, "failed", MAX_RETRY_ATTEMPTS, RETRY_COOLDOWN_MS, scheduleRetry,
);
// Invoke the scheduled callback synchronously to verify it wakes the agent
const [fn] = scheduleRetry.mock.calls[0]!;
fn();
expect(deps.wakeAgentIfPossible).toHaveBeenCalledWith("agent-1", "company-1", "retry_failed");
});
});
describe("Escalation path (count >= MAX_RETRY_ATTEMPTS)", () => {
it("escalates to chain[0] manager after max retries", () => {
deps.db.getTaskRetryCount.mockReturnValue(2);
deps.db.getChainOfCommand.mockReturnValue([
(deps.db.getTaskRetryCount as ReturnType<typeof vi.fn>).mockReturnValue(2);
(deps.db.getChainOfCommand as ReturnType<typeof vi.fn>).mockReturnValue([
{ id: "manager-1", name: "Manager Alpha" },
]);
const result = simulateFailureEscalation(deps, RUN, TASK, "failed");
const result = handleFailureEscalation(
deps, RUN, TASK, "failed", MAX_RETRY_ATTEMPTS, RETRY_COOLDOWN_MS, scheduleRetry,
);
expect(result.action).toBe("escalated");
expect(deps.db.resetTaskRetryCount).toHaveBeenCalledWith("task-1");
@@ -212,11 +123,13 @@ describe("Failure Escalation Pipeline", () => {
});
it("falls through to CEO when chain is empty", () => {
deps.db.getTaskRetryCount.mockReturnValue(2);
deps.db.getChainOfCommand.mockReturnValue([]);
deps.db.getCeoForCompany.mockReturnValue({ id: "ceo-1", name: "CEO Boss" });
(deps.db.getTaskRetryCount as ReturnType<typeof vi.fn>).mockReturnValue(2);
(deps.db.getChainOfCommand as ReturnType<typeof vi.fn>).mockReturnValue([]);
(deps.db.getCeoForCompany as ReturnType<typeof vi.fn>).mockReturnValue({ id: "ceo-1", name: "CEO Boss" });
const result = simulateFailureEscalation(deps, RUN, TASK, "failed");
const result = handleFailureEscalation(
deps, RUN, TASK, "failed", MAX_RETRY_ATTEMPTS, RETRY_COOLDOWN_MS, scheduleRetry,
);
expect(result.action).toBe("escalated");
expect(deps.db.resetTaskRetryCount).toHaveBeenCalledWith("task-1");
@@ -225,28 +138,32 @@ describe("Failure Escalation Pipeline", () => {
});
it("falls through to CEO when chain lookup throws", () => {
deps.db.getTaskRetryCount.mockReturnValue(2);
deps.db.getChainOfCommand.mockImplementation(() => {
(deps.db.getTaskRetryCount as ReturnType<typeof vi.fn>).mockReturnValue(2);
(deps.db.getChainOfCommand as ReturnType<typeof vi.fn>).mockImplementation(() => {
throw new Error("Agent deleted");
});
deps.db.getCeoForCompany.mockReturnValue({ id: "ceo-1", name: "CEO Boss" });
(deps.db.getCeoForCompany as ReturnType<typeof vi.fn>).mockReturnValue({ id: "ceo-1", name: "CEO Boss" });
const result = simulateFailureEscalation(deps, RUN, TASK, "failed");
const result = handleFailureEscalation(
deps, RUN, TASK, "failed", MAX_RETRY_ATTEMPTS, RETRY_COOLDOWN_MS, scheduleRetry,
);
expect(result.action).toBe("escalated");
expect(deps.wakeAgentIfPossible).toHaveBeenCalledWith("ceo-1", "company-1", "report_failed");
});
it("notifies user when both chain and CEO are unavailable", () => {
deps.db.getTaskRetryCount.mockReturnValue(2);
deps.db.getChainOfCommand.mockReturnValue([]);
deps.db.getCeoForCompany.mockReturnValue(null);
(deps.db.getTaskRetryCount as ReturnType<typeof vi.fn>).mockReturnValue(2);
(deps.db.getChainOfCommand as ReturnType<typeof vi.fn>).mockReturnValue([]);
(deps.db.getCeoForCompany as ReturnType<typeof vi.fn>).mockReturnValue(null);
const result = simulateFailureEscalation(deps, RUN, TASK, "failed");
const result = handleFailureEscalation(
deps, RUN, TASK, "failed", MAX_RETRY_ATTEMPTS, RETRY_COOLDOWN_MS, scheduleRetry,
);
expect(result.action).toBe("user_notified");
expect(deps.notify).toHaveBeenCalledWith(
"Task failed -- manual intervention needed",
expect.stringContaining("manual intervention"),
expect.stringContaining("Implement feature X"),
);
expect(deps.notify).toHaveBeenCalledWith(
@@ -256,11 +173,13 @@ describe("Failure Escalation Pipeline", () => {
});
it("uses taskId as fallback when task is null", () => {
deps.db.getTaskRetryCount.mockReturnValue(2);
deps.db.getChainOfCommand.mockReturnValue([]);
deps.db.getCeoForCompany.mockReturnValue(null);
(deps.db.getTaskRetryCount as ReturnType<typeof vi.fn>).mockReturnValue(2);
(deps.db.getChainOfCommand as ReturnType<typeof vi.fn>).mockReturnValue([]);
(deps.db.getCeoForCompany as ReturnType<typeof vi.fn>).mockReturnValue(null);
const result = simulateFailureEscalation(deps, RUN, null, "failed");
const result = handleFailureEscalation(
deps, RUN, null, "failed", MAX_RETRY_ATTEMPTS, RETRY_COOLDOWN_MS, scheduleRetry,
);
expect(result.action).toBe("user_notified");
expect(deps.notify).toHaveBeenCalledWith(
@@ -272,21 +191,25 @@ describe("Failure Escalation Pipeline", () => {
describe("Timed out runs follow same path", () => {
it("retries timed_out runs the same as failed runs", () => {
deps.db.getTaskRetryCount.mockReturnValue(0);
(deps.db.getTaskRetryCount as ReturnType<typeof vi.fn>).mockReturnValue(0);
const result = simulateFailureEscalation(deps, RUN, TASK, "timed_out");
const result = handleFailureEscalation(
deps, RUN, TASK, "timed_out", MAX_RETRY_ATTEMPTS, RETRY_COOLDOWN_MS, scheduleRetry,
);
expect(result.action).toBe("retry");
expect(deps.db.incrementTaskRetryCount).toHaveBeenCalledWith("task-1");
});
it("escalates timed_out runs after max retries", () => {
deps.db.getTaskRetryCount.mockReturnValue(2);
deps.db.getChainOfCommand.mockReturnValue([
(deps.db.getTaskRetryCount as ReturnType<typeof vi.fn>).mockReturnValue(2);
(deps.db.getChainOfCommand as ReturnType<typeof vi.fn>).mockReturnValue([
{ id: "manager-1", name: "Manager Alpha" },
]);
const result = simulateFailureEscalation(deps, RUN, TASK, "timed_out");
const result = handleFailureEscalation(
deps, RUN, TASK, "timed_out", MAX_RETRY_ATTEMPTS, RETRY_COOLDOWN_MS, scheduleRetry,
);
expect(result.action).toBe("escalated");
expect(deps.db.resetTaskRetryCount).toHaveBeenCalledWith("task-1");
@@ -295,19 +218,17 @@ describe("Failure Escalation Pipeline", () => {
describe("Success resets retry count", () => {
it("resets retry_count on successful completion", () => {
simulateSuccessReset(deps, { taskId: "task-1" }, "succeeded");
handleSuccessReset(deps.db, "task-1");
expect(deps.db.resetTaskRetryCount).toHaveBeenCalledWith("task-1");
});
it("handles reset errors gracefully", () => {
deps.db.resetTaskRetryCount.mockImplementation(() => {
(deps.db.resetTaskRetryCount as ReturnType<typeof vi.fn>).mockImplementation(() => {
throw new Error("DB error");
});
// Should not throw
expect(() => {
simulateSuccessReset(deps, { taskId: "task-1" }, "succeeded");
handleSuccessReset(deps.db, "task-1");
}).not.toThrow();
});
});
+2
View File
@@ -108,6 +108,8 @@ describe("selective IPC event types", () => {
return "budget-changed";
case "thread-wait-state-updated":
return "thread-wait-state-updated";
case "recovery-needed":
return "recovery-needed";
default: {
const _exhaustive: never = event;
return _exhaustive;
+31 -49
View File
@@ -1,62 +1,27 @@
import { describe, expect, it } from "vitest";
import { createCircuitBreaker, MAX_CONSECUTIVE_WAKES } from "@main/worker/circuit-breaker";
/**
* Tests for the self-wake circuit breaker pattern used in worker-service.ts.
* Tests for the self-wake circuit breaker used in worker-message-handler.ts.
*
* The circuit breaker prevents infinite self-wake loops when an agent is stuck
* on a task. After MAX_CONSECUTIVE_WAKES (5) consecutive wakes without progress
* (i.e., without a task status change), self-wake is suppressed and the agent's
* (i.e., without a successful run), self-wake is suppressed and the agent's
* manager is notified instead.
*/
const MAX_CONSECUTIVE_WAKES = 5;
/**
* Standalone circuit breaker logic extracted for testability.
* Mirrors the pattern used inside createWorkerService.
*/
function createCircuitBreaker(maxConsecutive: number = MAX_CONSECUTIVE_WAKES) {
const consecutiveWakeMap = new Map<string, number>();
return {
/** Attempt a self-wake. Returns true if allowed, false if circuit breaker trips. */
tryWake(agentId: string, companyId: string): boolean {
const wakeKey = `${agentId}:${companyId}`;
const count = consecutiveWakeMap.get(wakeKey) ?? 0;
if (count >= maxConsecutive) {
consecutiveWakeMap.delete(wakeKey);
return false; // Circuit breaker tripped
}
consecutiveWakeMap.set(wakeKey, count + 1);
return true;
},
/** Reset the counter for an agent (called on task progress). */
reset(agentId: string, companyId: string): void {
const wakeKey = `${agentId}:${companyId}`;
consecutiveWakeMap.delete(wakeKey);
},
/** Get the current count for an agent. */
getCount(agentId: string, companyId: string): number {
const wakeKey = `${agentId}:${companyId}`;
return consecutiveWakeMap.get(wakeKey) ?? 0;
},
};
}
describe("self-wake circuit breaker", () => {
it("allows the first self-wake (counter=1)", () => {
const cb = createCircuitBreaker();
const map = new Map<string, number>();
const cb = createCircuitBreaker(map);
const allowed = cb.tryWake("agent-1", "company-1");
expect(allowed).toBe(true);
expect(cb.getCount("agent-1", "company-1")).toBe(1);
});
it("allows wakes up to MAX_CONSECUTIVE_WAKES-1 (counter=4)", () => {
const cb = createCircuitBreaker();
const map = new Map<string, number>();
const cb = createCircuitBreaker(map);
for (let i = 0; i < MAX_CONSECUTIVE_WAKES - 1; i++) {
expect(cb.tryWake("agent-1", "company-1")).toBe(true);
}
@@ -64,7 +29,8 @@ describe("self-wake circuit breaker", () => {
});
it("trips after MAX_CONSECUTIVE_WAKES (5) consecutive wakes without progress", () => {
const cb = createCircuitBreaker();
const map = new Map<string, number>();
const cb = createCircuitBreaker(map);
// First 5 wakes succeed (counter goes 0->1, 1->2, 2->3, 3->4, 4->5)
for (let i = 0; i < MAX_CONSECUTIVE_WAKES; i++) {
expect(cb.tryWake("agent-1", "company-1")).toBe(true);
@@ -75,7 +41,8 @@ describe("self-wake circuit breaker", () => {
});
it("deletes the counter after circuit breaker trips (reset for next cycle)", () => {
const cb = createCircuitBreaker();
const map = new Map<string, number>();
const cb = createCircuitBreaker(map);
for (let i = 0; i < MAX_CONSECUTIVE_WAKES; i++) {
cb.tryWake("agent-1", "company-1");
}
@@ -86,7 +53,8 @@ describe("self-wake circuit breaker", () => {
});
it("resets counter to 0 on task status change", () => {
const cb = createCircuitBreaker();
const map = new Map<string, number>();
const cb = createCircuitBreaker(map);
cb.tryWake("agent-1", "company-1");
cb.tryWake("agent-1", "company-1");
expect(cb.getCount("agent-1", "company-1")).toBe(2);
@@ -97,7 +65,8 @@ describe("self-wake circuit breaker", () => {
});
it("maintains per-agent isolation (different agents have separate counters)", () => {
const cb = createCircuitBreaker();
const map = new Map<string, number>();
const cb = createCircuitBreaker(map);
// Agent 1 wakes 4 times
for (let i = 0; i < 4; i++) {
cb.tryWake("agent-1", "company-1");
@@ -111,7 +80,8 @@ describe("self-wake circuit breaker", () => {
});
it("maintains per-company isolation for the same agent", () => {
const cb = createCircuitBreaker();
const map = new Map<string, number>();
const cb = createCircuitBreaker(map);
cb.tryWake("agent-1", "company-1");
cb.tryWake("agent-1", "company-1");
cb.tryWake("agent-1", "company-2");
@@ -121,7 +91,8 @@ describe("self-wake circuit breaker", () => {
});
it("allows wakes again after reset even if previously near limit", () => {
const cb = createCircuitBreaker();
const map = new Map<string, number>();
const cb = createCircuitBreaker(map);
// Get to 4 wakes
for (let i = 0; i < MAX_CONSECUTIVE_WAKES - 1; i++) {
cb.tryWake("agent-1", "company-1");
@@ -138,7 +109,8 @@ describe("self-wake circuit breaker", () => {
});
it("resetting one agent does not affect another", () => {
const cb = createCircuitBreaker();
const map = new Map<string, number>();
const cb = createCircuitBreaker(map);
cb.tryWake("agent-1", "company-1");
cb.tryWake("agent-1", "company-1");
cb.tryWake("agent-2", "company-1");
@@ -149,4 +121,14 @@ describe("self-wake circuit breaker", () => {
expect(cb.getCount("agent-1", "company-1")).toBe(0);
expect(cb.getCount("agent-2", "company-1")).toBe(3);
});
it("shares state with the underlying map (same map = same state)", () => {
const map = new Map<string, number>();
const cb1 = createCircuitBreaker(map);
const cb2 = createCircuitBreaker(map);
cb1.tryWake("agent-1", "company-1");
cb1.tryWake("agent-1", "company-1");
// cb2 wraps the same map, so it sees the same count
expect(cb2.getCount("agent-1", "company-1")).toBe(2);
});
});
+5 -54
View File
@@ -1,7 +1,11 @@
import { describe, expect, it } from "vitest";
import {
needsDeliverableCheck,
determinePostRunStatus,
} from "@main/automation/automation-run-completion";
/**
* Tests for non-code task deliverable enforcement in automation-service.ts.
* Tests for non-code task deliverable enforcement in automation-run-completion.ts.
*
* When a run completes successfully on a non-code task (research, content),
* the system checks whether the agent produced a tangible deliverable
@@ -9,59 +13,6 @@ import { describe, expect, it } from "vitest";
* stays in_progress and the manager is notified.
*/
type TaskType = "code" | "research" | "content" | "management" | "general";
interface DeliverableCheckInput {
taskType: TaskType;
hasKnowledgeEntry: boolean;
hasDocument: boolean;
hasSubstantialComment: boolean;
}
/**
* Standalone deliverable-check logic extracted for testability.
* Mirrors the checkRunProducedDeliverable function inside automation-service.ts.
*/
function needsDeliverableCheck(taskType: TaskType): boolean {
return taskType === "research" || taskType === "content";
}
function hasDeliverable(input: DeliverableCheckInput): boolean {
if (!needsDeliverableCheck(input.taskType)) {
// Code/management/general tasks don't require deliverable check
return true;
}
return input.hasKnowledgeEntry || input.hasDocument || input.hasSubstantialComment;
}
/**
* Determines the next status after a successful run.
* Returns { nextStatus, comment } for how the task should be advanced.
*/
function determinePostRunStatus(input: DeliverableCheckInput): {
nextStatus: "in_review" | "in_progress";
comment: string;
} {
if (!needsDeliverableCheck(input.taskType)) {
return {
nextStatus: "in_review",
comment: "Run completed successfully. Awaiting review.",
};
}
if (hasDeliverable(input)) {
return {
nextStatus: "in_review",
comment: "Run completed with deliverable. Awaiting review.",
};
}
return {
nextStatus: "in_progress",
comment: `${input.taskType} task run completed but no deliverable artifact was detected. Flagging for manager review.`,
};
}
describe("non-code task deliverable enforcement", () => {
describe("needsDeliverableCheck", () => {
it("returns false for code tasks", () => {
+2 -1
View File
@@ -4,6 +4,7 @@ import {
sweepStaleReviews,
} from "../../src/main/task-quality-gates";
import type { SweepStaleReviewsDeps } from "../../src/main/task-quality-gates";
import type { AppDatabase } from "../../src/main/database";
describe("sweepStaleReviews", () => {
const NOW = Date.now();
@@ -17,7 +18,7 @@ describe("sweepStaleReviews", () => {
getManagerForAgent: vi.fn().mockReturnValue(null),
getChainOfCommand: vi.fn().mockReturnValue([]),
advanceTaskStatus: vi.fn().mockReturnValue({ previousStatus: "in_review" }),
},
} as unknown as AppDatabase,
getCompanyExecutionMode: vi.fn().mockReturnValue("ai_ceo"),
dispatchAgentMessage: vi.fn().mockReturnValue({ ok: true }),
resolveApprovalDecision: vi.fn(),