fix: correct channelId extraction and dormant check in hooks

message-received.ts:
- message_received ctx has channelId/accountId/conversationId (not
  sessionKey). Add extraction from ctx.channelId and metadata.to
  ("channel:ID" format) before the conversation_info fallback.

agent-end.ts:
- When tail-match is interrupted, only call wakeFromDormant() if the
  channel is actually dormant. For non-dormant interrupts (e.g. the
  moderator bot's own trigger messages firing message_received on
  other agents), fall through to normal advanceSpeaker() so the turn
  cycle continues correctly instead of re-triggering the same speaker.
- Import isDormant from turn-manager.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
h z
2026-04-09 06:24:05 +01:00
parent b9cbb7e895
commit 74e6d61d4d
2 changed files with 28 additions and 7 deletions

View File

@@ -7,6 +7,7 @@ import {
getAnchor,
advanceSpeaker,
wakeFromDormant,
isDormant,
hasSpeakers,
getDebugInfo,
isTurnPending,
@@ -175,11 +176,17 @@ export function registerAgentEndHook(deps: AgentEndDeps): InterruptFn {
});
if (interrupted) {
api.logger.info(`dirigent: tail-match interrupted channel=${channelId} — wake-from-dormant`);
if (isDormant(channelId)) {
// Channel is dormant: a new external message woke it — restart from first speaker
api.logger.info(`dirigent: tail-match interrupted (dormant) channel=${channelId} — waking`);
const first = wakeFromDormant(channelId);
if (first) await triggerNextSpeaker(channelId, first);
return;
}
// Not dormant: interrupt was a spurious trigger (e.g. moderator bot message).
// Fall through to normal advance so the turn cycle continues correctly.
api.logger.info(`dirigent: tail-match interrupted (non-dormant) channel=${channelId} — advancing normally`);
}
if (!matched) {
api.logger.warn(`dirigent: tail-match timeout channel=${channelId} agentId=${agentId} — advancing anyway`);
}

View File

@@ -24,13 +24,27 @@ export function registerMessageReceivedHook(deps: Deps): void {
const e = event as Record<string, unknown>;
const c = ctx as Record<string, unknown>;
// Extract Discord channel ID from session key or event metadata
// Extract Discord channel ID from ctx or event metadata
let channelId: string | undefined;
if (typeof c.sessionKey === "string") {
// ctx.channelId may be bare "1234567890" or "channel:1234567890"
if (typeof c.channelId === "string") {
const bare = c.channelId.match(/^(\d+)$/)?.[1] ?? c.channelId.match(/:(\d+)$/)?.[1];
if (bare) channelId = bare;
}
// fallback: sessionKey (per-session api instances)
if (!channelId && typeof c.sessionKey === "string") {
channelId = parseDiscordChannelId(c.sessionKey);
}
// fallback: metadata.to / originatingTo = "channel:1234567890"
if (!channelId) {
const metadata = e.metadata as Record<string, unknown> | undefined;
const to = String(metadata?.to ?? metadata?.originatingTo ?? "");
const toMatch = to.match(/:(\d+)$/);
if (toMatch) channelId = toMatch[1];
}
// fallback: conversation_info
if (!channelId) {
// Try from event metadata (conversation_info channel_id field)
const metadata = e.metadata as Record<string, unknown> | undefined;
const convInfo = metadata?.conversation_info as Record<string, unknown> | undefined;
const raw = String(convInfo?.channel_id ?? metadata?.channelId ?? "");