Skip to content

finalizeInterruptedMessages

const finalizeInterruptedMessages: (staleMs) => Promise<number> = sqliteRepo.finalizeInterruptedMessages

Defined in: lib/repositories/chat-history.ts:35

Finalize assistant turns left in-flight (done=0) by a worker/sidepanel death mid-stream. Marks each done and flags metrics.interrupted so the UI can surface the partial answer as interrupted and offer a retry.

Ownership is enforced inside the query, so there is no separate liveness check to race:

  • Staleness: a row is finalized only if it has not been written within staleMs. Streaming partial-content writes bump updatedAt ~every second, so a turn actively streaming in ANY window is never selected. A NULL updatedAt (rows predating the column) is treated as stale.
  • Tool-loop ownership: a turn awaiting tool approval can legitimately go minutes without a message write while its durable tool_loop_runs checkpoint is live. Excluding sessions that have any checkpoint row keeps those live waits from being finalized (rows are deleted on completion and pruned when abandoned).

Returns the count fixed.

number = 20_000

Promise<number>