Add hybrid memory retrieval and escalation history

This commit is contained in:
dahyeon committed 2026-05-18 23:04:34 -07:00
1 parent 1a86bded0b
commit 211de18476
38 files changed
+1602 -37

No files matched your search

+42 -3
View File
@@ -8,6 +8,10 @@ import type { HandoffPacket } from "@agent/handoff-packet";
import type { GarnetClient } from "@common/garnet";
import type { NatsClient } from "@common/nats";
import type { LlmClient } from "@llm/llm-client";
import type {
EscalationHistoryService,
UserEscalationHistory,
} from "../../memory/escalation-history.js";
import { AlertService } from "@obs/alert";
import { metrics } from "@obs/metrics";
import { createSlackNotifierFromEnv } from "@obs/slack-notifier";
@@ -87,6 +91,7 @@ export class SupervisorSkill implements Skill {
natsClient: Pick<NatsClient, "publish">,
private readonly llmClient?: Pick<LlmClient, "chatCompletion"> | undefined,
private readonly skillRegistry?: SkillRegistry | undefined,
private readonly escalationHistoryService?: EscalationHistoryService | undefined,
) {
this.alertService = new AlertService(
garnetClient,
@@ -108,7 +113,8 @@ export class SupervisorSkill implements Skill {
_conversationHistory: ConversationMessage[],
): Promise<AgentResponse> {
const packet = readHandoffPacket(request);
const llmDecision = await this.routeByLlm(packet);
const escalationHistory = await this.loadEscalationHistory(request.userId);
const llmDecision = await this.routeByLlm(packet, escalationHistory);
const useLlmRoute =
llmDecision !== null &&
llmDecision.confidence >= getSupervisorLlmConfidenceThreshold();
@@ -117,14 +123,26 @@ export class SupervisorSkill implements Skill {
: routeSupervisorTarget(packet);
const reason = packet?.handoffReason ?? "out-of-scope";
await appendSupervisorReview(this.garnetClient, {
const queuedAt = new Date().toISOString();
const reviewId = await appendSupervisorReview(this.garnetClient, {
chatroomId: request.chatroomId,
packet: JSON.stringify(packet ?? null),
queuedAt,
reason,
requestId: request.requestId,
target: target ?? "human-review",
userId: request.userId,
});
await this.escalationHistoryService?.recordEscalation({
chatroomId: request.chatroomId,
packetSummary: packet?.userGoal ?? packet?.summary ?? "",
queuedAt,
reason,
reviewId,
sourceChain: packet?.sourceChain ?? [],
target: target ?? "human-review",
userId: request.userId,
});
metrics.humanOverrideTotal.inc({ kind: reason, skill: this.name });
await this.alertService.sendAlert(
@@ -154,6 +172,7 @@ export class SupervisorSkill implements Skill {
routedBy: useLlmRoute ? "llm" : "rules",
source: userMessage.source,
supervisorReason: useLlmRoute ? llmDecision.reason : reason,
supervisorEscalationTotal: escalationHistory?.totalEscalations ?? 0,
suggestedTarget: target,
},
requestId: request.requestId,
@@ -164,6 +183,7 @@ export class SupervisorSkill implements Skill {
private async routeByLlm(
packet?: HandoffPacket,
escalationHistory?: UserEscalationHistory | null,
): Promise<SupervisorRoutingDecision | null> {
if (!getSupervisorLlmEnabled() || !this.llmClient || !this.skillRegistry) {
return null;
@@ -173,7 +193,11 @@ export class SupervisorSkill implements Skill {
const result = await this.llmClient.chatCompletion(
[
{
content: buildSupervisorSystemPrompt(this.skillRegistry, packet),
content: buildSupervisorSystemPrompt(
this.skillRegistry,
packet,
escalationHistory,
),
role: "system",
},
{
@@ -213,4 +237,19 @@ export class SupervisorSkill implements Skill {
target === "human-review" || Boolean(this.skillRegistry?.get(target))
);
}
private async loadEscalationHistory(userId: string) {
if (!this.escalationHistoryService) {
return null;
}
try {
return await this.escalationHistoryService.load(userId);
} catch (error) {
getLogger().warn(
{ err: error },
"[Supervisor] Failed to load escalation history",
);
return null;
}
}
}