refactor: centralize channel selection with resolveChannel()

Replace all manual channel selection (useReviewerChannel,
useArbiterChannel, isReviewerWorkItem, isArbiterWorkItem, 3-way
ternary chains) with a single resolveChannel(taskStatus) function
that maps task status to the correct Discord bot channel.
This commit is contained in:
Eyejoker
2026-03-30 22:56:40 +09:00
parent 7ad40276d9
commit a91776b067

View File

@@ -535,6 +535,20 @@ export function createMessageRuntime(deps: MessageRuntimeDeps): {
); );
const arbiterChannel = foundArbiterChannel || channel; const arbiterChannel = foundArbiterChannel || channel;
// Resolve the correct Discord channel for a given task status.
const resolveChannel = (taskStatus?: string | null): Channel => {
switch (taskStatus) {
case 'review_ready':
case 'in_review':
return reviewerChannel;
case 'arbiter_requested':
case 'in_arbitration':
return arbiterChannel;
default:
return channel;
}
};
if (isPairedRoomJid(chatJid)) { if (isPairedRoomJid(chatJid)) {
logger.info( logger.info(
{ {
@@ -558,23 +572,10 @@ export function createMessageRuntime(deps: MessageRuntimeDeps): {
(group.agentType || 'claude-code') as 'claude-code' | 'codex', (group.agentType || 'claude-code') as 'claude-code' | 'codex',
); );
if (openWorkItem) { if (openWorkItem) {
// Use reviewer channel if the pending task is in review state
const pendingTask = isPairedRoomJid(chatJid) const pendingTask = isPairedRoomJid(chatJid)
? getLatestOpenPairedTaskForChat(chatJid) ? getLatestOpenPairedTaskForChat(chatJid)
: null; : null;
const isReviewerWorkItem = const deliveryChannel = resolveChannel(pendingTask?.status);
pendingTask &&
(pendingTask.status === 'review_ready' ||
pendingTask.status === 'in_review');
const isArbiterWorkItem =
pendingTask &&
(pendingTask.status === 'arbiter_requested' ||
pendingTask.status === 'in_arbitration');
const deliveryChannel = isArbiterWorkItem
? arbiterChannel
: isReviewerWorkItem
? reviewerChannel
: channel;
const delivered = await deliverOpenWorkItem( const delivered = await deliverOpenWorkItem(
deliveryChannel, deliveryChannel,
openWorkItem, openWorkItem,
@@ -643,7 +644,7 @@ export function createMessageRuntime(deps: MessageRuntimeDeps): {
prompt: reviewPrompt, prompt: reviewPrompt,
chatJid, chatJid,
runId, runId,
channel: reviewerChannel, channel: resolveChannel(pendingReviewTask?.status),
startSeq: null, startSeq: null,
endSeq: null, endSeq: null,
}); });
@@ -656,7 +657,6 @@ export function createMessageRuntime(deps: MessageRuntimeDeps): {
(pendingReviewTask.status === 'arbiter_requested' || (pendingReviewTask.status === 'arbiter_requested' ||
pendingReviewTask.status === 'in_arbitration') pendingReviewTask.status === 'in_arbitration')
) { ) {
// Advance only the arbiter cursor — do NOT touch the owner cursor
const lastRaw = rawMissedMessages[rawMissedMessages.length - 1]; const lastRaw = rawMissedMessages[rawMissedMessages.length - 1];
const cursor = lastRaw?.seq ?? lastRaw?.timestamp; const cursor = lastRaw?.seq ?? lastRaw?.timestamp;
if (cursor != null) { if (cursor != null) {
@@ -686,7 +686,7 @@ export function createMessageRuntime(deps: MessageRuntimeDeps): {
prompt: arbiterPrompt, prompt: arbiterPrompt,
chatJid, chatJid,
runId, runId,
channel: arbiterChannel, channel: resolveChannel(pendingReviewTask?.status),
startSeq: null, startSeq: null,
endSeq: null, endSeq: null,
}); });
@@ -826,24 +826,16 @@ export function createMessageRuntime(deps: MessageRuntimeDeps): {
const pendingTaskForChannel = isPairedRoomJid(chatJid) const pendingTaskForChannel = isPairedRoomJid(chatJid)
? getLatestOpenPairedTaskForChat(chatJid) ? getLatestOpenPairedTaskForChat(chatJid)
: null; : null;
const useReviewerChannel = const taskStatus = pendingTaskForChannel?.status;
pendingTaskForChannel && const turnChannel = resolveChannel(taskStatus);
(pendingTaskForChannel.status === 'review_ready' || const cursorKey = resolveCursorKey(chatJid, taskStatus);
pendingTaskForChannel.status === 'in_review');
const useArbiterChannel =
pendingTaskForChannel &&
(pendingTaskForChannel.status === 'arbiter_requested' ||
pendingTaskForChannel.status === 'in_arbitration');
const turnChannel = useArbiterChannel
? arbiterChannel
: useReviewerChannel
? reviewerChannel
: channel;
const cursorKey = resolveCursorKey(chatJid, pendingTaskForChannel?.status);
// Arbiter turns use a dedicated context prompt; regular turns use formatted messages. // Arbiter turns use a dedicated context prompt; regular turns use formatted messages.
const isArbiterTurn =
taskStatus === 'arbiter_requested' ||
taskStatus === 'in_arbitration';
let prompt: string; let prompt: string;
if (useArbiterChannel && pendingTaskForChannel) { if (isArbiterTurn && pendingTaskForChannel) {
const arbiterMsgs = labelPairedSenders( const arbiterMsgs = labelPairedSenders(
chatJid, chatJid,
getRecentChatMessages(chatJid, 20), getRecentChatMessages(chatJid, 20),
@@ -1047,7 +1039,10 @@ export function createMessageRuntime(deps: MessageRuntimeDeps): {
const loopPendingTask = isPairedRoomJid(chatJid) const loopPendingTask = isPairedRoomJid(chatJid)
? getLatestOpenPairedTaskForChat(chatJid) ? getLatestOpenPairedTaskForChat(chatJid)
: null; : null;
const loopCursorKey = resolveCursorKey(chatJid, loopPendingTask?.status); const loopCursorKey = resolveCursorKey(
chatJid,
loopPendingTask?.status,
);
const rawPendingMessages = getMessagesSinceSeq( const rawPendingMessages = getMessagesSinceSeq(
chatJid, chatJid,