fix: restore implicit follow-up continuation
This commit is contained in:
@@ -314,6 +314,99 @@ describe('createMessageRuntime', () => {
|
|||||||
expect(saveState).toHaveBeenCalled();
|
expect(saveState).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('allows follow-up messages without a trigger after a visible reply in non-main groups', async () => {
|
||||||
|
const chatJid = 'group@test';
|
||||||
|
const group: RegisteredGroup = {
|
||||||
|
...makeGroup('codex'),
|
||||||
|
requiresTrigger: true,
|
||||||
|
};
|
||||||
|
const channel = makeChannel(chatJid);
|
||||||
|
const saveState = vi.fn();
|
||||||
|
const lastAgentTimestamps: Record<string, string> = {};
|
||||||
|
|
||||||
|
vi.mocked(db.getMessagesSince)
|
||||||
|
.mockReturnValueOnce([
|
||||||
|
{
|
||||||
|
id: 'msg-1',
|
||||||
|
chat_jid: chatJid,
|
||||||
|
sender: 'user@test',
|
||||||
|
sender_name: 'User',
|
||||||
|
content: '@Andy 첫 요청',
|
||||||
|
timestamp: '2026-03-18T09:00:00.000Z',
|
||||||
|
},
|
||||||
|
])
|
||||||
|
.mockReturnValueOnce([
|
||||||
|
{
|
||||||
|
id: 'msg-2',
|
||||||
|
chat_jid: chatJid,
|
||||||
|
sender: 'user@test',
|
||||||
|
sender_name: 'User',
|
||||||
|
content: '두 번째 말은 멘션 없이 이어서',
|
||||||
|
timestamp: '2026-03-18T09:00:10.000Z',
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
vi.mocked(agentRunner.runAgentProcess).mockImplementation(
|
||||||
|
async (_group, _input, _onProcess, onOutput) => {
|
||||||
|
await onOutput?.({
|
||||||
|
status: 'success',
|
||||||
|
result: '응답했습니다.',
|
||||||
|
phase: 'final',
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
status: 'success',
|
||||||
|
result: '응답했습니다.',
|
||||||
|
phase: 'final',
|
||||||
|
};
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
const runtime = createMessageRuntime({
|
||||||
|
assistantName: 'Andy',
|
||||||
|
idleTimeout: 60_000,
|
||||||
|
pollInterval: 1_000,
|
||||||
|
timezone: 'UTC',
|
||||||
|
triggerPattern: /^@Andy\b/i,
|
||||||
|
channels: [channel],
|
||||||
|
queue: {
|
||||||
|
registerProcess: vi.fn(),
|
||||||
|
closeStdin: vi.fn(),
|
||||||
|
notifyIdle: vi.fn(),
|
||||||
|
} as any,
|
||||||
|
getRegisteredGroups: () => ({ [chatJid]: group }),
|
||||||
|
getSessions: () => ({}),
|
||||||
|
getLastTimestamp: () => '',
|
||||||
|
setLastTimestamp: vi.fn(),
|
||||||
|
getLastAgentTimestamps: () => lastAgentTimestamps,
|
||||||
|
saveState,
|
||||||
|
persistSession: vi.fn(),
|
||||||
|
clearSession: vi.fn(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const first = await runtime.processGroupMessages(chatJid, {
|
||||||
|
runId: 'run-triggered-first-turn',
|
||||||
|
reason: 'messages',
|
||||||
|
});
|
||||||
|
const second = await runtime.processGroupMessages(chatJid, {
|
||||||
|
runId: 'run-triggerless-follow-up',
|
||||||
|
reason: 'messages',
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(first).toBe(true);
|
||||||
|
expect(second).toBe(true);
|
||||||
|
expect(agentRunner.runAgentProcess).toHaveBeenCalledTimes(2);
|
||||||
|
expect(channel.sendMessage).toHaveBeenNthCalledWith(
|
||||||
|
1,
|
||||||
|
chatJid,
|
||||||
|
'응답했습니다.',
|
||||||
|
);
|
||||||
|
expect(channel.sendMessage).toHaveBeenNthCalledWith(
|
||||||
|
2,
|
||||||
|
chatJid,
|
||||||
|
'응답했습니다.',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
it('clears Claude sessions and closes stdin immediately on poisoned output', async () => {
|
it('clears Claude sessions and closes stdin immediately on poisoned output', async () => {
|
||||||
const chatJid = 'group@test';
|
const chatJid = 'group@test';
|
||||||
const group = makeGroup('claude-code');
|
const group = makeGroup('claude-code');
|
||||||
|
|||||||
@@ -86,6 +86,7 @@ export function createMessageRuntime(deps: MessageRuntimeDeps): {
|
|||||||
startMessageLoop: () => Promise<void>;
|
startMessageLoop: () => Promise<void>;
|
||||||
} {
|
} {
|
||||||
let messageLoopRunning = false;
|
let messageLoopRunning = false;
|
||||||
|
const implicitContinuationUntil = new Map<string, number>();
|
||||||
|
|
||||||
const getCurrentAvailableGroups = (): AvailableGroup[] =>
|
const getCurrentAvailableGroups = (): AvailableGroup[] =>
|
||||||
getAvailableGroups(deps.getRegisteredGroups());
|
getAvailableGroups(deps.getRegisteredGroups());
|
||||||
@@ -115,6 +116,24 @@ export function createMessageRuntime(deps: MessageRuntimeDeps): {
|
|||||||
deps.saveState();
|
deps.saveState();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const openImplicitContinuationWindow = (chatJid: string): void => {
|
||||||
|
if (deps.idleTimeout <= 0) return;
|
||||||
|
implicitContinuationUntil.set(chatJid, Date.now() + deps.idleTimeout);
|
||||||
|
};
|
||||||
|
|
||||||
|
const hasImplicitContinuationWindow = (
|
||||||
|
chatJid: string,
|
||||||
|
messages: NewMessage[],
|
||||||
|
): boolean => {
|
||||||
|
const until = implicitContinuationUntil.get(chatJid);
|
||||||
|
if (!until) return false;
|
||||||
|
if (Date.now() > until) {
|
||||||
|
implicitContinuationUntil.delete(chatJid);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return messages.some((message) => message.is_from_me !== true);
|
||||||
|
};
|
||||||
|
|
||||||
const getProcessableMessages = (
|
const getProcessableMessages = (
|
||||||
chatJid: string,
|
chatJid: string,
|
||||||
messages: Parameters<typeof filterProcessableMessages>[0],
|
messages: Parameters<typeof filterProcessableMessages>[0],
|
||||||
@@ -138,10 +157,7 @@ export function createMessageRuntime(deps: MessageRuntimeDeps): {
|
|||||||
// into a reply loop.
|
// into a reply loop.
|
||||||
return messages.filter(
|
return messages.filter(
|
||||||
(message) =>
|
(message) =>
|
||||||
!(
|
!(message.is_bot_message && message.content.trim() === failureText),
|
||||||
message.is_bot_message &&
|
|
||||||
message.content.trim() === failureText
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -161,6 +177,7 @@ export function createMessageRuntime(deps: MessageRuntimeDeps): {
|
|||||||
item.result_payload,
|
item.result_payload,
|
||||||
);
|
);
|
||||||
markWorkItemDelivered(item.id, replaceMessageId);
|
markWorkItemDelivered(item.id, replaceMessageId);
|
||||||
|
openImplicitContinuationWindow(item.chat_jid);
|
||||||
logger.info(
|
logger.info(
|
||||||
{
|
{
|
||||||
chatJid: item.chat_jid,
|
chatJid: item.chat_jid,
|
||||||
@@ -188,6 +205,7 @@ export function createMessageRuntime(deps: MessageRuntimeDeps): {
|
|||||||
try {
|
try {
|
||||||
await channel.sendMessage(item.chat_jid, item.result_payload);
|
await channel.sendMessage(item.chat_jid, item.result_payload);
|
||||||
markWorkItemDelivered(item.id);
|
markWorkItemDelivered(item.id);
|
||||||
|
openImplicitContinuationWindow(item.chat_jid);
|
||||||
logger.info(
|
logger.info(
|
||||||
{
|
{
|
||||||
chatJid: item.chat_jid,
|
chatJid: item.chat_jid,
|
||||||
@@ -655,7 +673,10 @@ export function createMessageRuntime(deps: MessageRuntimeDeps): {
|
|||||||
(msg.is_from_me ||
|
(msg.is_from_me ||
|
||||||
isTriggerAllowed(chatJid, msg.sender, allowlistCfg)),
|
isTriggerAllowed(chatJid, msg.sender, allowlistCfg)),
|
||||||
);
|
);
|
||||||
if (!hasTrigger) {
|
if (
|
||||||
|
!hasTrigger &&
|
||||||
|
!hasImplicitContinuationWindow(chatJid, missedMessages)
|
||||||
|
) {
|
||||||
logger.info(
|
logger.info(
|
||||||
{ chatJid, group: group.name, groupFolder: group.folder, runId },
|
{ chatJid, group: group.name, groupFolder: group.folder, runId },
|
||||||
'Skipping queued run because no allowed trigger was found',
|
'Skipping queued run because no allowed trigger was found',
|
||||||
@@ -1203,7 +1224,12 @@ export function createMessageRuntime(deps: MessageRuntimeDeps): {
|
|||||||
(msg.is_from_me ||
|
(msg.is_from_me ||
|
||||||
isTriggerAllowed(chatJid, msg.sender, allowlistCfg)),
|
isTriggerAllowed(chatJid, msg.sender, allowlistCfg)),
|
||||||
);
|
);
|
||||||
if (!hasTrigger) continue;
|
if (
|
||||||
|
!hasTrigger &&
|
||||||
|
!hasImplicitContinuationWindow(chatJid, processableGroupMessages)
|
||||||
|
) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
deps.queue.enqueueMessageCheck(chatJid, group.folder);
|
deps.queue.enqueueMessageCheck(chatJid, group.folder);
|
||||||
|
|||||||
Reference in New Issue
Block a user