Refine Codex progress message tracking

This commit is contained in:
Eyejoker
2026-03-19 03:22:31 +09:00
parent 779d57c392
commit 29b78fc286
2 changed files with 245 additions and 53 deletions

View File

@@ -60,10 +60,12 @@ function makeChannel(chatJid: string): Channel {
name: 'discord',
connect: vi.fn().mockResolvedValue(undefined),
sendMessage: vi.fn().mockResolvedValue(undefined),
sendAndTrack: vi.fn().mockResolvedValue('progress-1'),
isConnected: vi.fn(() => true),
ownsJid: vi.fn((jid: string) => jid === chatJid),
disconnect: vi.fn().mockResolvedValue(undefined),
setTyping: vi.fn().mockResolvedValue(undefined),
editMessage: vi.fn().mockResolvedValue(undefined),
};
}
@@ -221,7 +223,8 @@ describe('createMessageRuntime', () => {
expect(notifyIdle).toHaveBeenCalledWith(chatJid, 'run-2');
});
it('streams Codex progress as a normal room message and only marks idle after the turn settles', async () => {
it('tracks Codex progress in one editable message and updates elapsed time every 10 seconds', async () => {
vi.useFakeTimers();
const chatJid = 'group@test';
const group = makeGroup('codex');
const channel = makeChannel(chatJid);
@@ -248,6 +251,7 @@ describe('createMessageRuntime', () => {
newSessionId: 'session-progress',
});
expect(notifyIdle).not.toHaveBeenCalled();
await vi.advanceTimersByTimeAsync(10_000);
await onOutput?.({
status: 'success',
result: null,
@@ -283,25 +287,124 @@ describe('createMessageRuntime', () => {
clearSession: vi.fn(),
});
const result = await runtime.processGroupMessages(chatJid, {
runId: 'run-progress',
reason: 'messages',
try {
const result = await runtime.processGroupMessages(chatJid, {
runId: 'run-progress',
reason: 'messages',
});
expect(result).toBe(true);
expect(channel.sendAndTrack).toHaveBeenCalledWith(
chatJid,
'CI 상태 확인 중입니다.\n\n0초',
);
expect(channel.editMessage).toHaveBeenCalledWith(
chatJid,
'progress-1',
'CI 상태 확인 중입니다.\n\n10초',
);
expect(channel.sendMessage).not.toHaveBeenCalled();
expect(notifyIdle).toHaveBeenCalledTimes(1);
expect(notifyIdle).toHaveBeenCalledWith(chatJid, 'run-progress');
expect(persistSession).toHaveBeenCalledWith(
group.folder,
'session-progress',
);
} finally {
vi.useRealTimers();
}
});
it('formats longer Codex progress durations with minutes and hours', async () => {
vi.useFakeTimers();
const chatJid = 'group@test';
const group = makeGroup('codex');
const channel = makeChannel(chatJid);
vi.mocked(db.getMessagesSince).mockReturnValue([
{
id: 'msg-1',
chat_jid: chatJid,
sender: 'user@test',
sender_name: 'User',
content: 'hello',
timestamp: '2026-03-19T00:00:00.000Z',
},
]);
vi.mocked(agentRunner.runAgentProcess).mockImplementation(
async (_group, _input, _onProcess, onOutput) => {
await onOutput?.({
status: 'success',
phase: 'progress',
result: '오래 걸리는 작업입니다.',
newSessionId: 'session-long-progress',
});
await vi.advanceTimersByTimeAsync(70_000);
expect(channel.editMessage).toHaveBeenLastCalledWith(
chatJid,
'progress-1',
'오래 걸리는 작업입니다.\n\n1분 10초',
);
await vi.advanceTimersByTimeAsync(3_600_000);
await onOutput?.({
status: 'success',
result: null,
newSessionId: 'session-long-progress',
});
return {
status: 'success',
result: null,
newSessionId: 'session-long-progress',
};
},
);
const runtime = createMessageRuntime({
assistantName: 'Andy',
idleTimeout: 1_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: () => ({}),
saveState: vi.fn(),
persistSession: vi.fn(),
clearSession: vi.fn(),
});
expect(result).toBe(true);
expect(channel.sendMessage).toHaveBeenCalledWith(
chatJid,
'CI 상태 확인 중입니다.',
);
expect(notifyIdle).toHaveBeenCalledTimes(1);
expect(notifyIdle).toHaveBeenCalledWith(chatJid, 'run-progress');
expect(persistSession).toHaveBeenCalledWith(
group.folder,
'session-progress',
);
try {
const result = await runtime.processGroupMessages(chatJid, {
runId: 'run-long-progress',
reason: 'messages',
});
expect(result).toBe(true);
expect(channel.sendAndTrack).toHaveBeenCalledWith(
chatJid,
'오래 걸리는 작업입니다.\n\n0초',
);
expect(channel.editMessage).toHaveBeenLastCalledWith(
chatJid,
'progress-1',
'오래 걸리는 작업입니다.\n\n1시간 1분 10초',
);
} finally {
vi.useRealTimers();
}
});
it('keeps progress separate from the final Codex answer', async () => {
vi.useFakeTimers();
const chatJid = 'group@test';
const group = makeGroup('codex');
const channel = makeChannel(chatJid);
@@ -326,6 +429,7 @@ describe('createMessageRuntime', () => {
result: '테스트를 돌리는 중입니다.',
newSessionId: 'session-final',
});
await vi.advanceTimersByTimeAsync(10_000);
await onOutput?.({
status: 'success',
phase: 'final',
@@ -362,24 +466,31 @@ describe('createMessageRuntime', () => {
clearSession: vi.fn(),
});
const result = await runtime.processGroupMessages(chatJid, {
runId: 'run-final',
reason: 'messages',
});
try {
const result = await runtime.processGroupMessages(chatJid, {
runId: 'run-final',
reason: 'messages',
});
expect(result).toBe(true);
expect(channel.sendMessage).toHaveBeenNthCalledWith(
1,
chatJid,
'테스트를 돌리는 중입니다.',
);
expect(channel.sendMessage).toHaveBeenNthCalledWith(
2,
chatJid,
'테스트가 끝났습니다.',
);
expect(notifyIdle).toHaveBeenCalledTimes(1);
expect(notifyIdle).toHaveBeenCalledWith(chatJid, 'run-final');
expect(result).toBe(true);
expect(channel.sendAndTrack).toHaveBeenCalledWith(
chatJid,
'테스트를 돌리는 중입니다.\n\n0초',
);
expect(channel.editMessage).toHaveBeenCalledWith(
chatJid,
'progress-1',
'테스트를 돌리는 중입니다.\n\n10초',
);
expect(channel.sendMessage).toHaveBeenCalledWith(
chatJid,
'테스트가 끝났습니다.',
);
expect(notifyIdle).toHaveBeenCalledTimes(1);
expect(notifyIdle).toHaveBeenCalledWith(chatJid, 'run-final');
} finally {
vi.useRealTimers();
}
});
it('does not roll back when a streamed progress message was already posted before an error', async () => {
@@ -451,9 +562,9 @@ describe('createMessageRuntime', () => {
});
expect(result).toBe(true);
expect(channel.sendMessage).toHaveBeenCalledWith(
expect(channel.sendAndTrack).toHaveBeenCalledWith(
chatJid,
'중간 진행상황입니다.',
'중간 진행상황입니다.\n\n0초',
);
expect(lastAgentTimestamps[chatJid]).toBe('2026-03-19T00:00:00.000Z');
expect(saveState).toHaveBeenCalled();