fix: stop the progress ticker when a run loses its paired turn attempt

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 <noreply@anthropic.com>
This commit is contained in:
Codex
2026-09-28 10:41:36 +09:00
parent c7a8a4339c
commit 69ae3342db
2 changed files with 67 additions and 0 deletions

View File

@@ -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);

View File

@@ -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;
}