Add bounded skill handoff graph

This commit is contained in:
dahyeon committed 2026-05-18 17:22:12 -07:00
1 parent 54bc295ead
commit c14adac162
42 files changed
+2243 -31

No files matched your search

+95
View File
@@ -0,0 +1,95 @@
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 });
});
});
+56
View File
@@ -0,0 +1,56 @@
import type { HandoffReason, HandoffRequest } from "@agent/types";
import type { BudgetSnapshot } from "@agent/budget";
import type { SkillCapabilityCard } from "@skills/skill-loader";
export interface EscalationDecision {
handoff?: HandoffRequest | undefined;
reason?: HandoffReason | undefined;
shouldHandoff: boolean;
}
export function decide(input: {
budget?: BudgetSnapshot | undefined;
capabilityCard?: SkillCapabilityCard | undefined;
classified?: { confidence?: number | undefined } | undefined;
reason?: HandoffReason | undefined;
targetAgent?: string | undefined;
taskSpec?: string | undefined;
validation?: { status?: string | undefined } | undefined;
}): EscalationDecision {
const confidence = input.classified?.confidence;
let reason: HandoffReason | undefined = input.reason;
if (!reason) {
if (input.budget && input.budget.handoffs >= input.budget.maxHandoffs) {
reason = "max-hop";
} else if (input.validation?.status === "reject") {
reason = "validation-failed";
} else if (typeof confidence === "number" && confidence < 0.35) {
reason = "low-confidence";
} else if (
input.capabilityCard?.riskTier === "high" &&
typeof confidence === "number" &&
confidence < 0.7
) {
reason = "high-risk-action";
}
}
if (!reason) {
return { shouldHandoff: false };
}
return {
handoff: {
confidence: confidence ?? 0.5,
evidenceRefs: [],
expectedOutput: "text",
reason,
risk: input.capabilityCard?.riskTier ?? "medium",
targetAgent: input.targetAgent ?? "supervisor",
taskSpec: input.taskSpec ?? "Escalate this request to the appropriate skill.",
},
reason,
shouldHandoff: true,
};
}