From 69ae3342dbf125c5d7dc5770ca34eae1b34a0db9 Mon Sep 17 00:00:00 2001 From: Codex Date: Mon, 28 Sep 2026 10:41:36 +0900 Subject: [PATCH] fix: stop the progress ticker when a run loses its paired turn attempt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit syncTrackedProgressMessage() logged "no longer owns the active paired turn attempt" and returned, but left the 5s progress ticker running. Losing the attempt is permanent — ownership only flips back to 'active' when a new attempt claims the turn under its own runId — so a superseded run kept waking every 5s for the entire life of the following turn (165 wakeups observed in one room) while editing nothing. The channel's progress message stayed frozen for the whole next turn, so a room that was actively working looked hung. Clear the ticker on ownership loss, matching the repeated edit-failure path. The regression test asserts on the ownership probe rather than editMessage: a leaked ticker and a stopped one both produce zero edits, so counting edits cannot distinguish them. Co-Authored-By: Claude Opus 4.7 --- src/message-turn-controller.test.ts | 58 +++++++++++++++++++++++++++++ src/message-turn-controller.ts | 9 +++++ 2 files changed, 67 insertions(+) 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; }