MCPcopy
hub / github.com/codeaashu/claude-code / completeMainSessionTask

Function completeMainSessionTask

src/tasks/LocalMainSessionTask.ts:168–219  ·  view source on GitHub ↗
(
  taskId: string,
  success: boolean,
  setAppState: SetAppState,
)

Source from the content-addressed store, hash-verified

166 * Called when the backgrounded query finishes.
167 */
168export function completeMainSessionTask(
169 taskId: string,
170 success: boolean,
171 setAppState: SetAppState,
172): void {
173 let wasBackgrounded = true
174 let toolUseId: string | undefined
175
176 updateTaskState<LocalMainSessionTaskState>(taskId, setAppState, task => {
177 if (task.status !== 'running') {
178 return task
179 }
180
181 // Track if task was backgrounded (for notification decision)
182 wasBackgrounded = task.isBackgrounded ?? true
183 toolUseId = task.toolUseId
184
185 task.unregisterCleanup?.()
186
187 return {
188 ...task,
189 status: success ? 'completed' : 'failed',
190 endTime: Date.now(),
191 messages: task.messages?.length ? [task.messages.at(-1)!] : undefined,
192 }
193 })
194
195 void evictTaskOutput(taskId)
196
197 // Only send notification if task is still backgrounded (not foregrounded)
198 // If foregrounded, user is watching it directly - no notification needed
199 if (wasBackgrounded) {
200 enqueueMainSessionNotification(
201 taskId,
202 'Background session',
203 success ? 'completed' : 'failed',
204 setAppState,
205 toolUseId,
206 )
207 } else {
208 // Foregrounded: no XML notification (TUI user is watching), but SDK
209 // consumers still need to see the task_started bookend close.
210 // Set notified so evictTerminalTask/generateTaskAttachments eviction
211 // guards pass; the backgrounded path sets this inside
212 // enqueueMainSessionNotification's check-and-set.
213 updateTaskState(taskId, setAppState, task => ({ ...task, notified: true }))
214 emitTaskTerminatedSdk(taskId, success ? 'completed' : 'failed', {
215 toolUseId,
216 summary: 'Background session',
217 })
218 }
219}
220
221/**
222 * Enqueue a notification about the backgrounded session completing.

Callers 1

startBackgroundSessionFunction · 0.85

Calls 4

evictTaskOutputFunction · 0.85
emitTaskTerminatedSdkFunction · 0.85
updateTaskStateFunction · 0.50

Tested by

no test coverage detected