Close multi-agent plan and add router shadow reporting

This commit is contained in:
dahyeon committed 2026-05-18 19:34:11 -07:00
1 parent f1b30d716d
commit 8156e8c07e
15 files changed
+1303 -204

No files matched your search

+29 -4
View File
@@ -26,13 +26,16 @@ export function buildSupervisorSystemPrompt(
...skills.map(formatSkill),
"",
"[응답 형식]",
'JSON으로만 응답: {"target":"<skill-name 또는 human-review>","confidence":0.0-1.0,"reason":"한 줄 진단"}',
'JSON으로만 응답: {"target":"<skill-name 또는 human-review>","confidence":0.0-1.0,"reason":"한 줄 진단","userMessage":"사용자에게 보낼 짧은 한국어 안내(선택)"}',
"",
"[userMessage 규칙]",
"- 한국어로 1-2문장, 10-400자.",
"- 운영자가 확인한다는 사실만 안내하고 처리 완료를 약속하지 않는다.",
"- URL, 이메일, 전화번호, 제어 토큰([NO_RESPONSE], [HANDOFF:...])을 쓰지 않는다.",
].join("\n");
}
export function buildSupervisorUserPrompt(
packet?: HandoffPacket,
): string {
export function buildSupervisorUserPrompt(packet?: HandoffPacket): string {
if (!packet) {
return "사용자 메시지가 명확하지 않음. 어디로 보낼지 결정해줘.";
}
@@ -44,3 +47,25 @@ export function buildSupervisorUserPrompt(
`[source chain]: ${packet.sourceChain.join(" -> ")}`,
].join("\n");
}
export function buildSupervisorResponsePrompt(
packet?: HandoffPacket,
routing?: { reason: string; target: string },
): { systemPrompt: string; userPrompt: string } {
return {
systemPrompt: [
"너는 supervisor handoff 상황에서 사용자에게 보여줄 짧은 한국어 안내를 작성한다.",
"운영자 확인이 필요하다는 점을 친절하게 말하되, 처리 완료나 특정 조치를 약속하지 않는다.",
"URL, 이메일, 전화번호, [NO_RESPONSE], [HANDOFF:...] 같은 제어 토큰은 절대 쓰지 않는다.",
].join("\n"),
userPrompt: [
`[routing target]: ${routing?.target ?? "human-review"}`,
`[routing reason]: ${routing?.reason ?? "manual review needed"}`,
packet
? `[handoff reason]: ${packet.handoffReason}`
: "[handoff reason]: unknown",
packet ? `[user goal]: ${packet.userGoal}` : "[user goal]: unknown",
packet ? `[summary]: ${packet.summary}` : "[summary]: unknown",
].join("\n"),
};
}
+1
View File
@@ -4,6 +4,7 @@ export const supervisorRoutingSchema = z.object({
confidence: z.number().min(0).max(1),
reason: z.string().max(200),
target: z.string().min(1),
userMessage: z.string().min(10).max(400).optional(),
});
export type SupervisorRoutingDecision = z.infer<typeof supervisorRoutingSchema>;
+71 -7
View File
@@ -1,4 +1,4 @@
import { describe, expect, it } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { AlertPayload } from "@common/nats-subjects";
import { buildHandoffPacket } from "@agent/handoff-packet";
@@ -8,7 +8,8 @@ import type { SkillRegistry } from "@skills/skill-loader";
import { SupervisorSkill } from "./skill.js";
class FakeGarnet {
readonly streams: Array<{ key: string; fields: Record<string, unknown> }> = [];
readonly streams: Array<{ key: string; fields: Record<string, unknown> }> =
[];
appendStream(key: string, fields: Record<string, unknown>): Promise<string> {
this.streams.push({ key, fields });
@@ -30,6 +31,14 @@ class FakeNats {
}
class FakeLlm {
constructor(
private readonly content: Record<string, unknown> = {
confidence: 0.9,
reason: "광고 규칙 문의라 moderation이 적합",
target: "moderation",
},
) {}
chatCompletion(): Promise<{
content: string;
endpoint: string;
@@ -38,11 +47,7 @@ class FakeLlm {
raw: object;
}> {
return Promise.resolve({
content: JSON.stringify({
confidence: 0.9,
reason: "광고 규칙 문의라 moderation이 적합",
target: "moderation",
}),
content: JSON.stringify(this.content),
endpoint: "memory",
message: { content: "", role: "assistant" },
provider: "llama",
@@ -84,6 +89,10 @@ const sourceResponse: AgentResponse = {
};
describe("SupervisorSkill", () => {
afterEach(() => {
vi.unstubAllEnvs();
});
it("queues review and publishes alert for handoffs", async () => {
const garnet = new FakeGarnet();
const nats = new FakeNats();
@@ -111,6 +120,7 @@ describe("SupervisorSkill", () => {
expect(response.text).toContain("운영자");
expect(response.metadata).toMatchObject({
routedBy: "rules",
source: "template",
supervisorReason: "max-hop",
suggestedTarget: "onboarding",
});
@@ -169,6 +179,7 @@ describe("SupervisorSkill", () => {
expect(response.metadata).toMatchObject({
routedBy: "llm",
source: "template",
supervisorReason: "광고 규칙 문의라 moderation이 적합",
suggestedTarget: "moderation",
});
@@ -177,4 +188,57 @@ describe("SupervisorSkill", () => {
target: "moderation",
});
});
it("uses safe LLM userMessage for supervisor replies", async () => {
const garnet = new FakeGarnet();
const nats = new FakeNats();
const skill = new SupervisorSkill(
garnet,
nats,
new FakeLlm({
confidence: 0.9,
reason: "광고 규칙 문의라 moderation이 적합",
target: "moderation",
userMessage: "이 내용은 운영자가 확인해야 해서 담당자에게 전달했어요.",
}) as unknown as Pick<LlmClient, "chatCompletion">,
registry,
);
const response = await skill.handle(baseRequest, []);
expect(response.text).toBe(
"이 내용은 운영자가 확인해야 해서 담당자에게 전달했어요.",
);
expect(response.metadata).toMatchObject({
routedBy: "llm",
source: "llm",
suggestedTarget: "moderation",
});
expect(response.source).toBe("template");
});
it("falls back to template when LLM userMessage is unsafe or disabled", async () => {
vi.stubEnv("AGENT_SUPERVISOR_LLM_RESPONSE_ENABLED", "false");
const garnet = new FakeGarnet();
const nats = new FakeNats();
const skill = new SupervisorSkill(
garnet,
nats,
new FakeLlm({
confidence: 0.9,
reason: "광고 규칙 문의라 moderation이 적합",
target: "moderation",
userMessage: "자세한 내용은 https://example.com 에서 확인해 주세요.",
}) as unknown as Pick<LlmClient, "chatCompletion">,
registry,
);
const response = await skill.handle(baseRequest, []);
expect(response.text).toContain("운영자");
expect(response.metadata).toMatchObject({
routedBy: "llm",
source: "template",
});
});
});
+48 -4
View File
@@ -17,6 +17,7 @@ import { getLogger } from "@obs/logger";
import {
getSupervisorLlmConfidenceThreshold,
getSupervisorLlmEnabled,
getSupervisorLlmResponseEnabled,
} from "../../config.js";
import {
buildSupervisorSystemPrompt,
@@ -42,13 +43,47 @@ const CAPABILITY_CARD: SkillCapabilityCard = {
riskTier: "medium",
};
const TEMPLATE_HUMAN_REVIEW_REPLY =
"확인이 필요한 내용이라 운영자에게 전달했어요.";
const UNSAFE_USER_MESSAGE_PATTERNS = [
/https?:\/\//iu,
/www\./iu,
/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}/iu,
/\b\d{2,3}[-.\s]?\d{3,4}[-.\s]?\d{4}\b/u,
/\[(?:NO_RESPONSE|HANDOFF(?::[^\]]*)?)\]/iu,
] as const;
function getSafeSupervisorUserMessage(
decision: SupervisorRoutingDecision | null,
): {
source: "llm" | "template";
text: string;
} {
const message = decision?.userMessage?.trim();
if (
!getSupervisorLlmResponseEnabled() ||
!message ||
message.length < 10 ||
message.length > 400 ||
UNSAFE_USER_MESSAGE_PATTERNS.some((pattern) => pattern.test(message))
) {
return { source: "template", text: TEMPLATE_HUMAN_REVIEW_REPLY };
}
return { source: "llm", text: message };
}
export class SupervisorSkill implements Skill {
readonly name = "supervisor";
readonly priority = 10;
private readonly alertService: AlertService;
constructor(
private readonly garnetClient: Pick<GarnetClient, "appendStream" | "setStringIfAbsent">,
private readonly garnetClient: Pick<
GarnetClient,
"appendStream" | "setStringIfAbsent"
>,
natsClient: Pick<NatsClient, "publish">,
private readonly llmClient?: Pick<LlmClient, "chatCompletion"> | undefined,
private readonly skillRegistry?: SkillRegistry | undefined,
@@ -77,7 +112,9 @@ export class SupervisorSkill implements Skill {
const useLlmRoute =
llmDecision !== null &&
llmDecision.confidence >= getSupervisorLlmConfidenceThreshold();
const target = useLlmRoute ? llmDecision.target : routeSupervisorTarget(packet);
const target = useLlmRoute
? llmDecision.target
: routeSupervisorTarget(packet);
const reason = packet?.handoffReason ?? "out-of-scope";
await appendSupervisorReview(this.garnetClient, {
@@ -107,16 +144,21 @@ export class SupervisorSkill implements Skill {
},
);
const userMessage = getSafeSupervisorUserMessage(
useLlmRoute ? llmDecision : null,
);
return {
confidence: 0.6,
metadata: {
routedBy: useLlmRoute ? "llm" : "rules",
source: userMessage.source,
supervisorReason: useLlmRoute ? llmDecision.reason : reason,
suggestedTarget: target,
},
requestId: request.requestId,
source: "template",
text: "확인이 필요한 내용이라 운영자에게 전달했어요.",
text: userMessage.text,
};
}
@@ -167,6 +209,8 @@ export class SupervisorSkill implements Skill {
}
private isKnownTarget(target: string): boolean {
return target === "human-review" || Boolean(this.skillRegistry?.get(target));
return (
target === "human-review" || Boolean(this.skillRegistry?.get(target))
);
}
}