diff --git a/src/message-turn-controller.test.ts b/src/message-turn-controller.test.ts index 19be428..362fccf 100644 --- a/src/message-turn-controller.test.ts +++ b/src/message-turn-controller.test.ts @@ -218,6 +218,64 @@ describe('MessageTurnController outbound audit logging', () => { } }); + it('stops the progress ticker once this run loses the paired turn attempt', async () => { + vi.useFakeTimers(); + try { + const channel = makeChannel(); + let ownsAttempt = true; + // Spy on the ownership probe itself: every surviving tick calls it, so a + // leaked ticker keeps incrementing even though it edits nothing. Counting + // editMessage alone cannot tell "ticker stopped" from "ticker still + // running but skipping", which is exactly the bug. + const canDeliverFinalText = vi.fn(() => ownsAttempt); + const controller = new MessageTurnController({ + chatJid: 'dc:test-room', + group: makeGroup(), + runId: 'run-superseded-1', + channel, + idleTimeout: 1_000, + failureFinalText: '실패', + isClaudeCodeAgent: true, + clearSession: vi.fn(), + requestClose: vi.fn(), + deliverFinalText: vi.fn().mockResolvedValue(true), + deliveryRole: 'owner', + deliveryServiceId: 'claude', + pairedTurnIdentity: makeTurnIdentity(), + canDeliverFinalText, + }); + + await controller.start(); + await controller.handleOutput({ + status: 'success', + phase: 'tool-activity', + agentId: 'sub-1', + result: '작업 중', + } as any); + await vi.advanceTimersByTimeAsync(0); + + await vi.advanceTimersByTimeAsync(5_000); + expect(vi.mocked(channel.editMessage)).toHaveBeenCalled(); + + // A newer turn claims the attempt: this run is superseded for good. + ownsAttempt = false; + await vi.advanceTimersByTimeAsync(5_000); + const editsAfterLoss = vi.mocked(channel.editMessage!).mock.calls.length; + const probesAfterLoss = canDeliverFinalText.mock.calls.length; + + // The ticker must be gone, not merely skipping: without clearing it the + // superseded run kept waking every 5s for the whole next turn (165 times + // in the incident) while the live run's progress sat frozen. + await vi.advanceTimersByTimeAsync(60_000); + expect(canDeliverFinalText.mock.calls.length).toBe(probesAfterLoss); + expect(vi.mocked(channel.editMessage!).mock.calls.length).toBe( + editsAfterLoss, + ); + } finally { + vi.useRealTimers(); + } + }); + it('logs fallback progress audit when tracked message creation fails', async () => { const channel = makeChannel(); const deliverFinalText = vi.fn().mockResolvedValue(true); diff --git a/src/message-turn-controller.ts b/src/message-turn-controller.ts index 6fb9f27..2eb55f8 100644 --- a/src/message-turn-controller.ts +++ b/src/message-turn-controller.ts @@ -675,6 +675,15 @@ export class MessageTurnController { }, 'Skipped editing tracked progress because this run no longer owns the active paired turn attempt', ); + // Losing the attempt is permanent: ownership only flips back to 'active' + // when a *new* attempt claims the turn under its own runId, which this + // run can never be. Leaving the 5s ticker alive made a superseded run + // retry-and-skip forever — it logged every 5s for the entire life of the + // following turn while never editing anything, so the channel's progress + // message sat frozen and the room looked hung even though the current + // run was working. Stop the ticker, like the repeated edit-failure path + // below already does. + this.clearProgressTicker(); return; }