fix: resolve session race condition and follow-up turn output loss
Two bugs fixed: 1. agent-runner error path race condition: when agent process exited with error, the promise resolved immediately without waiting for outputChain, allowing a late wrappedOnOutput to re-persist a stale session ID after clearSession had already run. 2. follow-up turn state not resetting: when a follow-up IPC message joined an active run, turn-level flags (finalOutputSentToUser, sawNonProgressOutput) from the first turn prevented the last progress from being promoted to a final message. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -223,7 +223,7 @@ describe('createMessageRuntime', () => {
|
||||
expect(notifyIdle).toHaveBeenCalledWith(chatJid, 'run-2');
|
||||
});
|
||||
|
||||
it('tracks Codex progress in one editable message and updates elapsed time every 10 seconds', async () => {
|
||||
it('tracks Codex progress in one editable message and promotes the last progress when the run ends without a final phase', async () => {
|
||||
vi.useFakeTimers();
|
||||
const chatJid = 'group@test';
|
||||
const group = makeGroup('codex');
|
||||
@@ -303,7 +303,10 @@ describe('createMessageRuntime', () => {
|
||||
'progress-1',
|
||||
'CI 상태 확인 중입니다.\n\n10초',
|
||||
);
|
||||
expect(channel.sendMessage).not.toHaveBeenCalled();
|
||||
expect(channel.sendMessage).toHaveBeenCalledWith(
|
||||
chatJid,
|
||||
'CI 상태 확인 중입니다.',
|
||||
);
|
||||
expect(notifyIdle).toHaveBeenCalledTimes(1);
|
||||
expect(notifyIdle).toHaveBeenCalledWith(chatJid, 'run-progress');
|
||||
expect(persistSession).toHaveBeenCalledWith(
|
||||
@@ -628,6 +631,316 @@ describe('createMessageRuntime', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('resets tracked progress after a final output that becomes empty after formatting', 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(channel.sendAndTrack!)
|
||||
.mockResolvedValueOnce('progress-1')
|
||||
.mockResolvedValueOnce('progress-2');
|
||||
|
||||
vi.mocked(agentRunner.runAgentProcess).mockImplementation(
|
||||
async (_group, _input, _onProcess, onOutput) => {
|
||||
await onOutput?.({
|
||||
status: 'success',
|
||||
phase: 'progress',
|
||||
result: '첫 번째 진행상황입니다.',
|
||||
newSessionId: 'session-empty-final',
|
||||
});
|
||||
await vi.advanceTimersByTimeAsync(10_000);
|
||||
await onOutput?.({
|
||||
status: 'success',
|
||||
phase: 'final',
|
||||
result: '<internal>hidden final</internal>',
|
||||
newSessionId: 'session-empty-final',
|
||||
});
|
||||
await onOutput?.({
|
||||
status: 'success',
|
||||
phase: 'progress',
|
||||
result: '두 번째 진행상황입니다.',
|
||||
newSessionId: 'session-empty-final',
|
||||
});
|
||||
await vi.advanceTimersByTimeAsync(10_000);
|
||||
await onOutput?.({
|
||||
status: 'success',
|
||||
result: null,
|
||||
newSessionId: 'session-empty-final',
|
||||
});
|
||||
return {
|
||||
status: 'success',
|
||||
result: null,
|
||||
newSessionId: 'session-empty-final',
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
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(),
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await runtime.processGroupMessages(chatJid, {
|
||||
runId: 'run-empty-final',
|
||||
reason: 'messages',
|
||||
});
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(channel.sendAndTrack).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
chatJid,
|
||||
'첫 번째 진행상황입니다.\n\n0초',
|
||||
);
|
||||
expect(channel.sendAndTrack).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
chatJid,
|
||||
'두 번째 진행상황입니다.\n\n0초',
|
||||
);
|
||||
expect(channel.editMessage).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
chatJid,
|
||||
'progress-1',
|
||||
'첫 번째 진행상황입니다.\n\n10초',
|
||||
);
|
||||
expect(channel.editMessage).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
chatJid,
|
||||
'progress-2',
|
||||
'두 번째 진행상황입니다.\n\n10초',
|
||||
);
|
||||
expect(channel.sendMessage).toHaveBeenCalledWith(
|
||||
chatJid,
|
||||
'두 번째 진행상황입니다.',
|
||||
);
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it('promotes the last progress output to a final message when the agent completes without a final phase', 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(channel.sendAndTrack!).mockResolvedValueOnce('progress-1');
|
||||
|
||||
vi.mocked(agentRunner.runAgentProcess).mockImplementation(
|
||||
async (_group, _input, _onProcess, onOutput) => {
|
||||
await onOutput?.({
|
||||
status: 'success',
|
||||
phase: 'progress',
|
||||
result: '검증 중입니다.',
|
||||
newSessionId: 'session-progress-only',
|
||||
});
|
||||
await vi.advanceTimersByTimeAsync(10_000);
|
||||
await onOutput?.({
|
||||
status: 'success',
|
||||
phase: 'progress',
|
||||
result: '커밋은 정상 들어갔고 pre-commit도 통과했습니다.',
|
||||
newSessionId: 'session-progress-only',
|
||||
});
|
||||
await onOutput?.({
|
||||
status: 'success',
|
||||
result: null,
|
||||
newSessionId: 'session-progress-only',
|
||||
});
|
||||
return {
|
||||
status: 'success',
|
||||
result: null,
|
||||
newSessionId: 'session-progress-only',
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
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(),
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await runtime.processGroupMessages(chatJid, {
|
||||
runId: 'run-progress-only',
|
||||
reason: 'messages',
|
||||
});
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(channel.sendAndTrack).toHaveBeenCalledWith(
|
||||
chatJid,
|
||||
'검증 중입니다.\n\n0초',
|
||||
);
|
||||
expect(channel.editMessage).toHaveBeenCalledWith(
|
||||
chatJid,
|
||||
'progress-1',
|
||||
'커밋은 정상 들어갔고 pre-commit도 통과했습니다.\n\n10초',
|
||||
);
|
||||
expect(channel.sendMessage).toHaveBeenCalledWith(
|
||||
chatJid,
|
||||
'커밋은 정상 들어갔고 pre-commit도 통과했습니다.',
|
||||
);
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it('promotes progress-only output for a follow-up turn after a prior final in the same run', 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(channel.sendAndTrack!).mockResolvedValueOnce('progress-follow-up');
|
||||
|
||||
vi.mocked(agentRunner.runAgentProcess).mockImplementation(
|
||||
async (_group, _input, _onProcess, onOutput) => {
|
||||
await onOutput?.({
|
||||
status: 'success',
|
||||
phase: 'final',
|
||||
result: '첫 번째 턴 최종 답변',
|
||||
newSessionId: 'session-follow-up',
|
||||
});
|
||||
await onOutput?.({
|
||||
status: 'success',
|
||||
phase: 'progress',
|
||||
result: '두 번째 턴 진행상황입니다.',
|
||||
newSessionId: 'session-follow-up',
|
||||
});
|
||||
await vi.advanceTimersByTimeAsync(10_000);
|
||||
await onOutput?.({
|
||||
status: 'success',
|
||||
result: null,
|
||||
newSessionId: 'session-follow-up',
|
||||
});
|
||||
return {
|
||||
status: 'success',
|
||||
result: null,
|
||||
newSessionId: 'session-follow-up',
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
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(),
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await runtime.processGroupMessages(chatJid, {
|
||||
runId: 'run-follow-up-progress-only',
|
||||
reason: 'messages',
|
||||
});
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(channel.sendMessage).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
chatJid,
|
||||
'첫 번째 턴 최종 답변',
|
||||
);
|
||||
expect(channel.sendAndTrack).toHaveBeenCalledWith(
|
||||
chatJid,
|
||||
'두 번째 턴 진행상황입니다.\n\n0초',
|
||||
);
|
||||
expect(channel.editMessage).toHaveBeenCalledWith(
|
||||
chatJid,
|
||||
'progress-follow-up',
|
||||
'두 번째 턴 진행상황입니다.\n\n10초',
|
||||
);
|
||||
expect(channel.sendMessage).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
chatJid,
|
||||
'두 번째 턴 진행상황입니다.',
|
||||
);
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it('does not roll back when a streamed progress message was already posted before an error', async () => {
|
||||
const chatJid = 'group@test';
|
||||
const group = makeGroup('codex');
|
||||
|
||||
Reference in New Issue
Block a user