import { describe, expect, it } from "vitest"; import type { HandoffReason } from "@agent/types"; import { decide } from "./trigger.js"; describe("escalation trigger", () => { it("turns explicit handoff reasons into structured requests", () => { const reasons: HandoffReason[] = [ "out-of-scope", "missing-tool", "low-confidence", "validation-failed", "same-agent-revisit", "max-hop", "high-risk-action", "policy-risk", ]; for (const reason of reasons) { const decision = decide({ classified: { confidence: 0.8 }, reason, targetAgent: "supervisor", taskSpec: "review", }); expect(decision).toMatchObject({ reason, shouldHandoff: true, handoff: { reason, targetAgent: "supervisor", taskSpec: "review", }, }); } }); it("escalates exhausted budgets and validation rejects", () => { expect( decide({ budget: { estimatedCostUsd: 0, handoffs: 4, maxCostUsd: 0.5, maxHandoffs: 4, maxSameAgentVisits: 2, maxToolCalls: 20, toolCalls: 0, visits: {}, }, }).handoff?.reason, ).toBe("max-hop"); expect(decide({ validation: { status: "reject" } }).handoff?.reason).toBe( "validation-failed", ); }); it("escalates low-confidence and uncertain high-risk responses", () => { expect( decide({ classified: { confidence: 0.2 } }).handoff?.reason, ).toBe("low-confidence"); expect( decide({ capabilityCard: { canHandle: ["policy"], cannotHandle: [], domain: "moderation", handoffTargets: ["supervisor"], outputContract: { format: "text", maxChars: "500" }, riskTier: "high", }, classified: { confidence: 0.6 }, }).handoff?.reason, ).toBe("high-risk-action"); }); it("does not escalate ordinary confident responses", () => { expect( decide({ capabilityCard: { canHandle: ["help"], cannotHandle: [], domain: "onboarding", handoffTargets: ["supervisor"], outputContract: { format: "text", maxChars: "500" }, riskTier: "medium", }, classified: { confidence: 0.9 }, }), ).toEqual({ shouldHandoff: false }); }); });