fix(turn): keep mention override speaker/order when membership refresh runs
This commit is contained in:
@@ -55,8 +55,10 @@ function shuffleArray<T>(arr: T[]): T[] {
|
|||||||
export function initTurnOrder(channelId: string, botAccountIds: string[]): void {
|
export function initTurnOrder(channelId: string, botAccountIds: string[]): void {
|
||||||
const existing = channelTurns.get(channelId);
|
const existing = channelTurns.get(channelId);
|
||||||
if (existing) {
|
if (existing) {
|
||||||
// Check if membership changed
|
// Compare membership against base order.
|
||||||
const oldSet = new Set(existing.turnOrder);
|
// If mention override is active, turnOrder is temporary; use savedTurnOrder for stable comparison.
|
||||||
|
const baseOrder = existing.savedTurnOrder || existing.turnOrder;
|
||||||
|
const oldSet = new Set(baseOrder);
|
||||||
const newSet = new Set(botAccountIds);
|
const newSet = new Set(botAccountIds);
|
||||||
const same = oldSet.size === newSet.size && [...oldSet].every(id => newSet.has(id));
|
const same = oldSet.size === newSet.size && [...oldSet].every(id => newSet.has(id));
|
||||||
if (same) return; // no change
|
if (same) return; // no change
|
||||||
@@ -66,11 +68,40 @@ export function initTurnOrder(channelId: string, botAccountIds: string[]): void
|
|||||||
`oldOrder=${JSON.stringify(existing.turnOrder)} oldCurrent=${existing.currentSpeaker} ` +
|
`oldOrder=${JSON.stringify(existing.turnOrder)} oldCurrent=${existing.currentSpeaker} ` +
|
||||||
`oldOverride=${JSON.stringify(existing.savedTurnOrder || null)} newMembers=${JSON.stringify(botAccountIds)}`,
|
`oldOverride=${JSON.stringify(existing.savedTurnOrder || null)} newMembers=${JSON.stringify(botAccountIds)}`,
|
||||||
);
|
);
|
||||||
} else {
|
|
||||||
|
const nextOrder = shuffleArray(botAccountIds);
|
||||||
|
|
||||||
|
// Mention override active: update only the saved base order.
|
||||||
|
// Keep temporary turnOrder/currentSpeaker intact so @mention routing is not clobbered.
|
||||||
|
if (existing.savedTurnOrder) {
|
||||||
|
existing.savedTurnOrder = nextOrder;
|
||||||
|
existing.lastChangedAt = Date.now();
|
||||||
|
console.log(
|
||||||
|
`[dirigent][turn-debug] initTurnOrder applied-base-only channel=${channelId} ` +
|
||||||
|
`savedOrder=${JSON.stringify(nextOrder)} keptOverrideOrder=${JSON.stringify(existing.turnOrder)} ` +
|
||||||
|
`keptCurrent=${existing.currentSpeaker}`,
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Non-mention flow: preserve previous behavior (re-init to dormant).
|
||||||
|
channelTurns.set(channelId, {
|
||||||
|
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`,
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
`[dirigent][turn-debug] initTurnOrder first-init channel=${channelId} members=${JSON.stringify(botAccountIds)}`,
|
`[dirigent][turn-debug] initTurnOrder first-init channel=${channelId} members=${JSON.stringify(botAccountIds)}`,
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
const nextOrder = shuffleArray(botAccountIds);
|
const nextOrder = shuffleArray(botAccountIds);
|
||||||
channelTurns.set(channelId, {
|
channelTurns.set(channelId, {
|
||||||
|
|||||||
Reference in New Issue
Block a user