@@ -79,7 +79,8 @@ async function lastAssistantText(tx: DbOrTx, chatId: string, streamId: string):
7979 * under a row lock, so the turn's own end and a reconciler pass settle it exactly once: one
8080 * outcome, one change to the job's failure streak. An outcome the agent reported is kept; a
8181 * turn that ended well without one is `not_met`, summarized by its last reply, and is not a
82- * failure. Returns false while the turn is still live.
82+ * failure. A run whose row is gone (its chat was deleted) ends `interrupted`. Returns false while
83+ * the turn is still live.
8384 */
8485export async function finalizeJobRun ( executionId : string ) : Promise < boolean > {
8586 let disabledScheduleId : string | null = null
@@ -108,13 +109,17 @@ export async function finalizeJobRun(executionId: string): Promise<boolean> {
108109 . from ( copilotRuns )
109110 . where ( eq ( copilotRuns . streamId , executionId ) )
110111 . limit ( 1 )
111- if ( ! run || ! TERMINAL_RUN_STATUSES . includes ( run . status ) ) return false
112+ if ( run && ! TERMINAL_RUN_STATUSES . includes ( run . status ) ) return false
112113
113114 const reported = REPORTED_JOB_OUTCOMES . find ( ( outcome ) => outcome === record . outcome )
114115 const deadlineStopped = toRecord ( record . executionData ) . deadlineStopRequestedAt !== undefined
115116 let outcome : JobRunOutcome
116117 let summary : string
117- if ( run . status === 'complete' && reported ) {
118+ if ( ! run ) {
119+ /** A started run's row is gone (its chat was deleted): nothing will ever end it otherwise. */
120+ outcome = 'interrupted'
121+ summary = 'The run was removed before it finished.'
122+ } else if ( run . status === 'complete' && reported ) {
118123 outcome = reported
119124 summary = record . summary ?? ''
120125 } else if ( run . status === 'complete' ) {
@@ -127,7 +132,7 @@ export async function finalizeJobRun(executionId: string): Promise<boolean> {
127132 outcome = 'failed'
128133 summary = run . error ?? 'The run failed.'
129134 }
130- const endedAt = run . completedAt ?? new Date ( )
135+ const endedAt = run ? .completedAt ?? new Date ( )
131136 await tx
132137 . update ( jobExecutionLogs )
133138 . set ( {
0 commit comments