fix(sync-continuation): improve error handling and toast cleanup
- Add proper error handling in executeSyncContinuation with try-catch blocks - Ensure toast cleanup happens in all error paths via finally block - Add anchorMessageCount tracking for accurate result fetching after continuation - Improve fetchSyncResult to filter messages after anchor point - Add silent failure detection when no new response is generated
This commit is contained in:
@@ -30,6 +30,7 @@ export async function pollSyncSession(
|
||||
agentToUse: string
|
||||
toastManager: { removeTask: (id: string) => void } | null | undefined
|
||||
taskId: string | undefined
|
||||
anchorMessageCount?: number
|
||||
}
|
||||
): Promise<string | null> {
|
||||
const syncTiming = getTimingConfig()
|
||||
@@ -48,7 +49,13 @@ export async function pollSyncSession(
|
||||
await new Promise(resolve => setTimeout(resolve, syncTiming.POLL_INTERVAL_MS))
|
||||
pollCount++
|
||||
|
||||
const statusResult = await client.session.status()
|
||||
let statusResult: { data?: Record<string, { type: string }> }
|
||||
try {
|
||||
statusResult = await client.session.status()
|
||||
} catch (error) {
|
||||
log("[task] Poll status fetch failed, retrying", { sessionID: input.sessionID, error: String(error) })
|
||||
continue
|
||||
}
|
||||
const allStatuses = (statusResult.data ?? {}) as Record<string, { type: string }>
|
||||
const sessionStatus = allStatuses[input.sessionID]
|
||||
|
||||
@@ -65,8 +72,19 @@ export async function pollSyncSession(
|
||||
continue
|
||||
}
|
||||
|
||||
const messagesResult = await client.session.messages({ path: { id: input.sessionID } })
|
||||
const msgs = ((messagesResult as { data?: unknown }).data ?? messagesResult) as SessionMessage[]
|
||||
let messagesResult: { data?: unknown } | SessionMessage[]
|
||||
try {
|
||||
messagesResult = await client.session.messages({ path: { id: input.sessionID } })
|
||||
} catch (error) {
|
||||
log("[task] Poll messages fetch failed, retrying", { sessionID: input.sessionID, error: String(error) })
|
||||
continue
|
||||
}
|
||||
const rawData = (messagesResult as { data?: unknown })?.data ?? messagesResult
|
||||
const msgs = Array.isArray(rawData) ? (rawData as SessionMessage[]) : []
|
||||
|
||||
if (input.anchorMessageCount !== undefined && msgs.length <= input.anchorMessageCount) {
|
||||
continue
|
||||
}
|
||||
|
||||
if (isSessionComplete(msgs)) {
|
||||
log("[task] Poll complete - terminal finish detected", { sessionID: input.sessionID, pollCount })
|
||||
|
||||
Reference in New Issue
Block a user