fix: bypass no-reply for moderator bot handoff messages #7

Closed
zhi wants to merge 4 commits from fix/moderator-bypass into feat/turn-based-speaking
2 changed files with 34 additions and 42 deletions

View File

@@ -22,8 +22,7 @@
"debugLogChannelIds": [], "debugLogChannelIds": [],
"discordControlApiBaseUrl": "http://127.0.0.1:8790", "discordControlApiBaseUrl": "http://127.0.0.1:8790",
"discordControlApiToken": "<DISCORD_CONTROL_AUTH_TOKEN>", "discordControlApiToken": "<DISCORD_CONTROL_AUTH_TOKEN>",
"discordControlCallerId": "agent-main", "discordControlCallerId": "agent-main"
"moderatorBotToken": "<MODERATOR_BOT_TOKEN>"
} }
} }
} }

View File

@@ -24,7 +24,6 @@ type DebugConfig = {
}; };
const sessionDecision = new Map<string, DecisionRecord>(); const sessionDecision = new Map<string, DecisionRecord>();
const sessionInjected = new Set<string>(); // Track which sessions have already injected the end marker
const MAX_SESSION_DECISIONS = 2000; const MAX_SESSION_DECISIONS = 2000;
const DECISION_TTL_MS = 5 * 60 * 1000; const DECISION_TTL_MS = 5 * 60 * 1000;
function buildEndMarkerInstruction(endSymbols: string[], isGroupChat: boolean): string { function buildEndMarkerInstruction(endSymbols: string[], isGroupChat: boolean): string {
@@ -83,6 +82,7 @@ function extractUntrustedConversationInfo(text: string): Record<string, unknown>
function deriveDecisionInputFromPrompt( function deriveDecisionInputFromPrompt(
prompt: string, prompt: string,
messageProvider?: string, messageProvider?: string,
channelIdFromCtx?: string,
): { ): {
channel: string; channel: string;
channelId?: string; channelId?: string;
@@ -92,11 +92,17 @@ function deriveDecisionInputFromPrompt(
} { } {
const conv = extractUntrustedConversationInfo(prompt) || {}; const conv = extractUntrustedConversationInfo(prompt) || {};
const channel = (messageProvider || "").toLowerCase(); const channel = (messageProvider || "").toLowerCase();
const channelId =
(typeof conv.channel_id === "string" && conv.channel_id) || // Priority: ctx.channelId > conv.chat_id > conv.channel_id
(typeof conv.chat_id === "string" && conv.chat_id.startsWith("channel:") let channelId = channelIdFromCtx;
? conv.chat_id.slice("channel:".length) if (!channelId) {
: undefined); channelId =
(typeof conv.chat_id === "string" && conv.chat_id.startsWith("channel:")
? conv.chat_id.slice("channel:".length)
: typeof conv.channel_id === "string" && conv.channel_id) ||
undefined;
}
const senderId = const 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) ||
@@ -262,20 +268,8 @@ function resolveDiscordUserId(api: OpenClawPluginApi, accountId: string): string
const discord = (channels.discord as Record<string, unknown>) || {}; const discord = (channels.discord as Record<string, unknown>) || {};
const accounts = (discord.accounts as Record<string, Record<string, unknown>>) || {}; const accounts = (discord.accounts as Record<string, Record<string, unknown>>) || {};
const acct = accounts[accountId]; const acct = accounts[accountId];
if (!acct?.token || typeof acct.token !== "string") return undefined;
if (!acct?.token || typeof acct.token !== "string") { return userIdFromToken(acct.token);
api.logger.warn(`whispergate: resolveDiscordUserId failed for accountId=${accountId}: no token found in config`);
return undefined;
}
const userId = userIdFromToken(acct.token);
if (!userId) {
api.logger.warn(`whispergate: resolveDiscordUserId failed for accountId=${accountId}: could not parse userId from token`);
return undefined;
}
api.logger.info(`whispergate: resolveDiscordUserId success accountId=${accountId} userId=${userId}`);
return userId;
} }
/** Get the moderator bot's Discord user ID from its token */ /** Get the moderator bot's Discord user ID from its token */
@@ -589,11 +583,23 @@ export default {
); );
} }
const derived = deriveDecisionInputFromPrompt(prompt, ctx.messageProvider); const derived = deriveDecisionInputFromPrompt(prompt, ctx.messageProvider, ctx.channelId);
// Only proceed if: discord channel AND prompt contains untrusted metadata // Only proceed if: discord channel AND prompt contains untrusted metadata
const hasConvMarker = prompt.includes("Conversation info (untrusted metadata):"); const hasConvMarker = prompt.includes("Conversation info (untrusted metadata):");
if (live.discordOnly !== false && (!hasConvMarker || derived.channel !== "discord")) return; if (live.discordOnly !== false && (!hasConvMarker || derived.channel !== "discord")) return;
// Moderator bypass: if sender is the moderator bot, don't trigger no-reply
// This prevents moderator handoff messages from being treated as regular messages without 🔚
const moderatorUserId = getModeratorUserId(live);
if (moderatorUserId && derived.senderId === moderatorUserId) {
if (shouldDebugLog(live, derived.channelId)) {
api.logger.info(
`whispergate: moderator bypass for senderId=${derived.senderId}, skipping no-reply`,
);
}
return;
}
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);
@@ -628,11 +634,12 @@ export default {
// Try resolveAccountId first, fall back to ctx.accountId if not found // Try resolveAccountId first, fall back to ctx.accountId if not found
let accountId = resolveAccountId(api, ctx.agentId || ""); let accountId = resolveAccountId(api, ctx.agentId || "");
// Debug log for turn check // Debug log for turn check - log all available identifiers
if (shouldDebugLog(live, derived.channelId)) { if (shouldDebugLog(live, derived.channelId)) {
const turnDebug = getTurnDebugInfo(derived.channelId); const turnDebug = getTurnDebugInfo(derived.channelId);
api.logger.info( api.logger.info(
`whispergate: turn check preflight agentId=${ctx.agentId ?? "undefined"} ` + `whispergate: turn check preflight ` +
`agentId=${ctx.agentId ?? "undefined"} ` +
`resolvedAccountId=${accountId ?? "undefined"} ` + `resolvedAccountId=${accountId ?? "undefined"} ` +
`ctxAccountId=${ctx.accountId ?? "undefined"} ` + `ctxAccountId=${ctx.accountId ?? "undefined"} ` +
`turnOrderLen=${turnDebug.turnOrder?.length ?? 0} ` + `turnOrderLen=${turnDebug.turnOrder?.length ?? 0} ` +
@@ -712,7 +719,7 @@ export default {
if (rec) sessionDecision.delete(key); if (rec) sessionDecision.delete(key);
const prompt = ((event as Record<string, unknown>).prompt as string) || ""; const prompt = ((event as Record<string, unknown>).prompt as string) || "";
const derived = deriveDecisionInputFromPrompt(prompt, ctx.messageProvider); const derived = deriveDecisionInputFromPrompt(prompt, ctx.messageProvider, ctx.channelId);
const decision = evaluateDecision({ const decision = evaluateDecision({
config: live, config: live,
@@ -736,17 +743,6 @@ export default {
} }
sessionDecision.delete(key); sessionDecision.delete(key);
// Only inject once per session (one-time injection)
if (sessionInjected.has(key)) {
if (shouldDebugLog(live, undefined)) {
api.logger.info(
`whispergate: debug before_prompt_build session=${key} inject skipped (already injected)`,
);
}
return;
}
if (!rec.decision.shouldInjectEndMarkerPrompt) { if (!rec.decision.shouldInjectEndMarkerPrompt) {
if (shouldDebugLog(live, undefined)) { if (shouldDebugLog(live, undefined)) {
api.logger.info( api.logger.info(
@@ -758,7 +754,7 @@ export default {
// Resolve end symbols from config/policy for dynamic instruction // Resolve end symbols from config/policy for dynamic instruction
const prompt = ((event as Record<string, unknown>).prompt as string) || ""; const prompt = ((event as Record<string, unknown>).prompt as string) || "";
const derived = deriveDecisionInputFromPrompt(prompt, ctx.messageProvider); const derived = deriveDecisionInputFromPrompt(prompt, ctx.messageProvider, ctx.channelId);
const policy = resolvePolicy(live, derived.channelId, policyState.channelPolicies); const policy = resolvePolicy(live, derived.channelId, policyState.channelPolicies);
const isGroupChat = derived.conv.is_group_chat === true || derived.conv.is_group_chat === "true"; const isGroupChat = derived.conv.is_group_chat === true || derived.conv.is_group_chat === "true";
const instruction = buildEndMarkerInstruction(policy.endSymbols, isGroupChat); const instruction = buildEndMarkerInstruction(policy.endSymbols, isGroupChat);
@@ -770,10 +766,7 @@ export default {
if (idStr) identity = idStr + "\n\n"; if (idStr) identity = idStr + "\n\n";
} }
// Mark session as injected (one-time injection) api.logger.info(`whispergate: prepend end marker instruction for session=${key}, reason=${rec.decision.reason} isGroupChat=${isGroupChat}`);
sessionInjected.add(key);
api.logger.info(`whispergate: one-time inject end marker for session=${key}, reason=${rec.decision.reason} isGroupChat=${isGroupChat}`);
return { prependContext: identity + instruction }; return { prependContext: identity + instruction };
}); });