chore(debug): log parsed sender/channel fields in hook decision recompute

This commit is contained in:
2026-02-26 11:47:23 +00:00
parent 6b3d89634a
commit 211a94233f

View File

@@ -69,30 +69,29 @@ function extractUntrustedConversationInfo(text: string): Record<string, unknown>
} }
} }
function deriveDecisionInputFromAgentCtx( function deriveDecisionInputFromPrompt(
ctx: Record<string, unknown>, prompt: string,
): { channel: string; channelId?: string; senderId?: string; content: string } { messageProvider?: string,
const channel = normalizeChannel(ctx); ): {
const content = typeof ctx.input === "string" ? ctx.input : ""; channel: string;
const conv = extractUntrustedConversationInfo(content) || {}; channelId?: string;
const channelIdRaw = senderId?: string;
(typeof ctx.channelId === "string" && ctx.channelId) || content: string;
conv: Record<string, unknown>;
} {
const conv = extractUntrustedConversationInfo(prompt) || {};
const channel = (messageProvider || "").toLowerCase();
const channelId =
(typeof conv.channel_id === "string" && conv.channel_id) || (typeof conv.channel_id === "string" && conv.channel_id) ||
(typeof conv.chat_id === "string" && conv.chat_id.startsWith("channel:") (typeof conv.chat_id === "string" && conv.chat_id.startsWith("channel:")
? conv.chat_id.slice("channel:".length) ? conv.chat_id.slice("channel:".length)
: undefined); : undefined);
const senderIdRaw = const senderId =
(typeof ctx.senderId === "string" && ctx.senderId) ||
(typeof conv.sender_id === "string" && conv.sender_id) || (typeof conv.sender_id === "string" && conv.sender_id) ||
(typeof conv.sender === "string" && conv.sender) || (typeof conv.sender === "string" && conv.sender) ||
undefined; undefined;
return { return { channel, channelId, senderId, content: prompt, conv };
channel,
channelId: channelIdRaw,
senderId: senderIdRaw,
content,
};
} }
function pruneDecisionMap(now = Date.now()) { function pruneDecisionMap(now = Date.now()) {
@@ -363,41 +362,32 @@ export default {
const live = getLivePluginConfig(api, baseConfig as WhisperGateConfig) as WhisperGateConfig & DebugConfig; const live = getLivePluginConfig(api, baseConfig as WhisperGateConfig) as WhisperGateConfig & DebugConfig;
ensurePolicyStateLoaded(api, live); ensurePolicyStateLoaded(api, live);
// In before_model_resolve, ctx only has: agentId, sessionKey, sessionId, workspaceDir, messageProvider.
// senderId/channelId/input are NOT available. Use event.prompt (user message incl. untrusted metadata).
const channel = (ctx.messageProvider || "").toLowerCase();
if (live.discordOnly !== false && channel !== "discord") return;
const prompt = ((event as Record<string, unknown>).prompt as string) || ""; const prompt = ((event as Record<string, unknown>).prompt as string) || "";
const conv = extractUntrustedConversationInfo(prompt) || {}; const derived = deriveDecisionInputFromPrompt(prompt, ctx.messageProvider);
const senderId = if (live.discordOnly !== false && derived.channel !== "discord") return;
(typeof conv.sender_id === "string" && conv.sender_id) ||
(typeof conv.sender === "string" && conv.sender) ||
undefined;
const channelId =
(typeof conv.channel_id === "string" && conv.channel_id) ||
(typeof (conv as Record<string, unknown>).conversation_label === "string"
? undefined
: undefined);
let rec = sessionDecision.get(key); let rec = sessionDecision.get(key);
if (!rec || Date.now() - rec.createdAt > DECISION_TTL_MS) { if (!rec || Date.now() - rec.createdAt > DECISION_TTL_MS) {
if (rec) sessionDecision.delete(key); if (rec) sessionDecision.delete(key);
const decision = evaluateDecision({ const decision = evaluateDecision({
config: live, config: live,
channel, channel: derived.channel,
channelId, channelId: derived.channelId,
channelPolicies: policyState.channelPolicies, channelPolicies: policyState.channelPolicies,
senderId, senderId: derived.senderId,
content: prompt, content: derived.content,
}); });
rec = { decision, createdAt: Date.now() }; rec = { decision, createdAt: Date.now() };
sessionDecision.set(key, rec); sessionDecision.set(key, rec);
pruneDecisionMap(); pruneDecisionMap();
if (shouldDebugLog(live, channelId)) { if (shouldDebugLog(live, derived.channelId)) {
api.logger.info( api.logger.info(
`whispergate: debug before_model_resolve recompute session=${key} senderId=${senderId} decision=${decision.reason} ` + `whispergate: debug before_model_resolve recompute session=${key} ` +
`shouldNoReply=${decision.shouldUseNoReply} shouldInject=${decision.shouldInjectEndMarkerPrompt}`, `channel=${derived.channel} channelId=${derived.channelId ?? ""} senderId=${derived.senderId ?? ""} ` +
`convSenderId=${String((derived.conv as Record<string, unknown>).sender_id ?? "")} ` +
`convSender=${String((derived.conv as Record<string, unknown>).sender ?? "")} ` +
`convChannelId=${String((derived.conv as Record<string, unknown>).channel_id ?? "")} ` +
`decision=${decision.reason} shouldNoReply=${decision.shouldUseNoReply} shouldInject=${decision.shouldInjectEndMarkerPrompt}`,
); );
} }
} }
@@ -425,31 +415,26 @@ export default {
if (!rec || Date.now() - rec.createdAt > DECISION_TTL_MS) { if (!rec || Date.now() - rec.createdAt > DECISION_TTL_MS) {
if (rec) sessionDecision.delete(key); if (rec) sessionDecision.delete(key);
// before_prompt_build has event.prompt and event.messages available.
const channel = (ctx.messageProvider || "").toLowerCase();
const prompt = ((event as Record<string, unknown>).prompt as string) || ""; const prompt = ((event as Record<string, unknown>).prompt as string) || "";
const conv = extractUntrustedConversationInfo(prompt) || {}; const derived = deriveDecisionInputFromPrompt(prompt, ctx.messageProvider);
const senderId =
(typeof conv.sender_id === "string" && conv.sender_id) ||
(typeof conv.sender === "string" && conv.sender) ||
undefined;
const channelId =
(typeof conv.channel_id === "string" && conv.channel_id) ||
undefined;
const decision = evaluateDecision({ const decision = evaluateDecision({
config: live, config: live,
channel, channel: derived.channel,
channelId, channelId: derived.channelId,
channelPolicies: policyState.channelPolicies, channelPolicies: policyState.channelPolicies,
senderId, senderId: derived.senderId,
content: prompt, content: derived.content,
}); });
rec = { decision, createdAt: Date.now() }; rec = { decision, createdAt: Date.now() };
if (shouldDebugLog(live, channelId)) { if (shouldDebugLog(live, derived.channelId)) {
api.logger.info( api.logger.info(
`whispergate: debug before_prompt_build recompute session=${key} senderId=${senderId} decision=${decision.reason} ` + `whispergate: debug before_prompt_build recompute session=${key} ` +
`shouldNoReply=${decision.shouldUseNoReply} shouldInject=${decision.shouldInjectEndMarkerPrompt}`, `channel=${derived.channel} channelId=${derived.channelId ?? ""} senderId=${derived.senderId ?? ""} ` +
`convSenderId=${String((derived.conv as Record<string, unknown>).sender_id ?? "")} ` +
`convSender=${String((derived.conv as Record<string, unknown>).sender ?? "")} ` +
`convChannelId=${String((derived.conv as Record<string, unknown>).channel_id ?? "")} ` +
`decision=${decision.reason} shouldNoReply=${decision.shouldUseNoReply} shouldInject=${decision.shouldInjectEndMarkerPrompt}`,
); );
} }
} }