From a995b7d6bf7f4e7c8e21ae550b2a6f8398b30a7e Mon Sep 17 00:00:00 2001 From: orion Date: Sun, 8 Mar 2026 07:38:33 +0000 Subject: [PATCH] debug(turn): add mention-override and ensureTurnOrder state transition logs --- plugin/core/turn-bootstrap.ts | 10 ++++++++++ plugin/turn-manager.ts | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/plugin/core/turn-bootstrap.ts b/plugin/core/turn-bootstrap.ts index d561cf6..3743882 100644 --- a/plugin/core/turn-bootstrap.ts +++ b/plugin/core/turn-bootstrap.ts @@ -104,14 +104,24 @@ export async function ensureTurnOrder(api: OpenClawPluginApi, channelId: string) loadCache(api); let botAccounts = getChannelBotAccountIds(api, channelId); + api.logger.info( + `dirigent: turn-debug ensureTurnOrder enter channel=${channelId} cached=${JSON.stringify(botAccounts)} bootstrapTried=${channelBootstrapTried.has(channelId)}`, + ); + if (botAccounts.length === 0 && !channelBootstrapTried.has(channelId)) { channelBootstrapTried.add(channelId); const discovered = await fetchVisibleChannelBotAccountIds(api, channelId).catch(() => [] as string[]); + api.logger.info( + `dirigent: turn-debug ensureTurnOrder bootstrap-discovered channel=${channelId} discovered=${JSON.stringify(discovered)}`, + ); for (const aid of discovered) recordChannelAccount(api, channelId, aid); botAccounts = getChannelBotAccountIds(api, channelId); } if (botAccounts.length > 0) { + api.logger.info( + `dirigent: turn-debug ensureTurnOrder initTurnOrder channel=${channelId} members=${JSON.stringify(botAccounts)}`, + ); initTurnOrder(channelId, botAccounts); } } diff --git a/plugin/turn-manager.ts b/plugin/turn-manager.ts index 0fc9410..d00015d 100644 --- a/plugin/turn-manager.ts +++ b/plugin/turn-manager.ts @@ -60,15 +60,30 @@ export function initTurnOrder(channelId: string, botAccountIds: string[]): void const newSet = new Set(botAccountIds); const same = oldSet.size === newSet.size && [...oldSet].every(id => newSet.has(id)); if (same) return; // no change + + console.log( + `[dirigent][turn-debug] initTurnOrder membership-changed channel=${channelId} ` + + `oldOrder=${JSON.stringify(existing.turnOrder)} oldCurrent=${existing.currentSpeaker} ` + + `oldOverride=${JSON.stringify(existing.savedTurnOrder || null)} newMembers=${JSON.stringify(botAccountIds)}`, + ); + } else { + console.log( + `[dirigent][turn-debug] initTurnOrder first-init channel=${channelId} members=${JSON.stringify(botAccountIds)}`, + ); } + const nextOrder = shuffleArray(botAccountIds); channelTurns.set(channelId, { - turnOrder: shuffleArray(botAccountIds), + turnOrder: nextOrder, currentSpeaker: null, // start dormant noRepliedThisCycle: new Set(), lastChangedAt: Date.now(), waitingForHuman: false, }); + + console.log( + `[dirigent][turn-debug] initTurnOrder applied channel=${channelId} newOrder=${JSON.stringify(nextOrder)} newCurrent=null`, + ); } /** @@ -190,12 +205,21 @@ export function setMentionOverride(channelId: string, mentionedAccountIds: strin const state = channelTurns.get(channelId); if (!state || mentionedAccountIds.length === 0) return false; + console.log( + `[dirigent][turn-debug] setMentionOverride start channel=${channelId} ` + + `mentioned=${JSON.stringify(mentionedAccountIds)} current=${state.currentSpeaker} ` + + `order=${JSON.stringify(state.turnOrder)} saved=${JSON.stringify(state.savedTurnOrder || null)}`, + ); + // Restore any existing override first restoreOriginalOrder(state); // Filter to agents actually in the turn order const validIds = mentionedAccountIds.filter(id => state.turnOrder.includes(id)); - if (validIds.length === 0) return false; + if (validIds.length === 0) { + console.log(`[dirigent][turn-debug] setMentionOverride ignored channel=${channelId} reason=no-valid-mentioned`); + return false; + } // Order by their position in the current turn order validIds.sort((a, b) => state.turnOrder.indexOf(a) - state.turnOrder.indexOf(b)); @@ -208,6 +232,12 @@ export function setMentionOverride(channelId: string, mentionedAccountIds: strin state.noRepliedThisCycle = new Set(); state.lastChangedAt = Date.now(); + console.log( + `[dirigent][turn-debug] setMentionOverride applied channel=${channelId} ` + + `overrideOrder=${JSON.stringify(state.turnOrder)} current=${state.currentSpeaker} ` + + `savedOriginal=${JSON.stringify(state.savedTurnOrder || null)}`, + ); + return true; }