forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConversationFinalizationService.swift
More file actions
924 lines (859 loc) · 36.2 KB
/
Copy pathConversationFinalizationService.swift
File metadata and controls
924 lines (859 loc) · 36.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
import Foundation
actor ConversationFinalizationService {
static let shared = ConversationFinalizationService()
private let maxRetries = 5
private let maxLocalFallbackRetries = 3
private var apiClient = APIClient.shared
private var meetingCompletionNotificationTask: Task<Void, Never>?
private var pendingMeetingCompletionConversationIDs = Set<String>()
private var pendingFinalizationProjectionPolls = Set<String>()
private var localProjectionHooks: LocalProjectionTestHooks?
private init() {}
deinit {
meetingCompletionNotificationTask?.cancel()
}
func setAPIClientForTesting(_ client: APIClient?) {
apiClient = client ?? APIClient.shared
}
func setLocalProjectionHooksForTesting(_ hooks: LocalProjectionTestHooks?) {
localProjectionHooks = hooks
}
func finalizeSession(
id sessionId: Int64,
reason: TranscriptionFinalizationReason,
allowCloudForceProcess: Bool = false
) async {
do {
guard let session = try await TranscriptionStorage.shared.getSession(id: sessionId) else {
return
}
await finalizeSession(session, reason: reason, allowCloudForceProcess: allowCloudForceProcess)
} catch {
logError("ConversationFinalization: Failed to load session \(sessionId)", error: error)
}
}
func recoverPendingFinalizations() async {
do {
let sessions = try await TranscriptionStorage.shared.getSessionsNeedingFinalization(maxRetries: maxRetries)
let exhaustedLocalFallbackSessions = try await TranscriptionStorage.shared
.getExhaustedCloudSessionsWithLocalSegments(
maxRetries: maxRetries,
maxLocalFallbackRetries: maxLocalFallbackRetries
)
let sessionsById = Dictionary(
grouping: sessions + exhaustedLocalFallbackSessions,
by: { $0.id ?? -1 }
).compactMap { $0.value.first }
if !sessionsById.isEmpty {
log(
"ConversationFinalization: Recovering \(sessionsById.count) pending sessions (\(exhaustedLocalFallbackSessions.count) exhausted cloud sessions have local fallback data)"
)
}
let exhaustedLocalFallbackIds = Set(exhaustedLocalFallbackSessions.compactMap(\.id))
for session in sessionsById
where session.isReadyForRetry() || session.status != .failed || session.retryCount >= maxRetries {
if let sessionId = session.id, exhaustedLocalFallbackIds.contains(sessionId) {
await finalizeExhaustedCloudSessionFromLocalSegments(session)
continue
}
await finalizeSession(
session,
reason: .retry,
allowCloudForceProcess: session.backendId?.isEmpty == false
)
}
} catch {
logError("ConversationFinalization: Recovery failed", error: error)
}
}
private func finalizeExhaustedCloudSessionFromLocalSegments(_ session: TranscriptionSessionRecord) async {
guard let sessionId = session.id else { return }
guard session.status != .completed && !session.backendSynced else { return }
log("ConversationFinalization: Retrying exhausted cloud session \(sessionId) from saved local segments")
do {
guard try await TranscriptionStorage.shared.markSessionUploading(id: sessionId) else {
return
}
guard let latestSession = try await TranscriptionStorage.shared.getSession(id: sessionId) else {
throw TranscriptionStorageError.sessionNotFound
}
let outcome = try await resolveExhaustedCloudReconciliation(session: latestSession, sessionId: sessionId)
guard outcome.handled else {
throw TranscriptionStorageError.invalidState("Exhausted cloud session has no local fallback")
}
await postMeetingCompletionIfReady(
session: latestSession,
reason: .retry,
meetingTreatmentEligible: outcome.meetingTreatmentEligible
)
} catch {
await markRetryableFailure(sessionId: sessionId, error: error)
}
}
private func finalizeSession(
_ session: TranscriptionSessionRecord,
reason: TranscriptionFinalizationReason,
allowCloudForceProcess: Bool
) async {
guard let sessionId = session.id else { return }
guard session.status != .completed && !session.backendSynced else { return }
let strategy = session.finalizationStrategy ?? defaultStrategy(for: session)
log(
"ConversationFinalization: Finalizing session \(sessionId) strategy=\(strategy.rawValue) reason=\(reason.rawValue)"
)
do {
await storeMeetingContextIfEnabled(for: session)
guard try await TranscriptionStorage.shared.markSessionUploading(id: sessionId) else {
return
}
let meetingTreatmentEligible: Bool?
switch strategy {
case .localSegments:
meetingTreatmentEligible = try await uploadLocalSegments(sessionId: sessionId)
case .cloudReconcile:
guard let latestSession = try await TranscriptionStorage.shared.getSession(id: sessionId) else {
throw TranscriptionStorageError.sessionNotFound
}
meetingTreatmentEligible = try await finalizeCloudSession(
session: latestSession,
allowForceProcess: allowCloudForceProcess
)
}
// Meeting provenance is persisted on the recording session, while the
// finalization reason only describes why this particular attempt ended.
// A max-duration split must not announce a meeting fragment as ready;
// explicit stop and detector-end completions may wake Chat after the
// backend/local reconciliation has reached completed.
await postMeetingCompletionIfReady(
session: session,
reason: reason,
meetingTreatmentEligible: meetingTreatmentEligible
)
} catch {
await markRetryableFailure(sessionId: sessionId, error: error)
}
}
private func storeMeetingContextIfEnabled(for session: TranscriptionSessionRecord) async {
guard session.conversationRole == .meeting else { return }
let enabled = await MainActor.run {
(
systemCalendar: SystemCalendarMeetingContextFeature.isEnabled,
onDeviceIdentity: OnDeviceMeetingIdentityFeature.isEnabled
)
}
guard enabled.systemCalendar || enabled.onDeviceIdentity else { return }
let end = max(session.finishedAt ?? Date(), session.startedAt.addingTimeInterval(1))
let interval = DateInterval(start: session.startedAt, end: end)
// This precedes conversation creation/force-processing so the backend overlap resolver can
// see the row. Both sources share one short ceiling, preserving fail-open finalization on
// slow local storage, Calendar, or backend I/O.
await withTaskGroup(of: Void.self) { group in
group.addTask {
await withTaskGroup(of: Void.self) { syncGroup in
if enabled.systemCalendar {
syncGroup.addTask {
await SystemCalendarMeetingContextService.shared.syncAuthorizedEvents(overlapping: interval)
}
}
if enabled.onDeviceIdentity {
syncGroup.addTask {
await OnDeviceMeetingIdentityService.shared.syncIdentity(overlapping: interval)
}
}
await syncGroup.waitForAll()
}
}
group.addTask {
try? await Task.sleep(for: .seconds(2))
}
_ = await group.next()
group.cancelAll()
}
}
private func defaultStrategy(for session: TranscriptionSessionRecord) -> TranscriptionFinalizationStrategy {
if session.backendId?.isEmpty == false {
return .cloudReconcile
}
return session.source == ConversationSource.desktop.rawValue ? .localSegments : .cloudReconcile
}
private func uploadLocalSegments(sessionId: Int64, allowBackendIdOverride: Bool = false) async throws -> Bool {
guard let bundle = try await TranscriptionStorage.shared.getSessionWithSegments(id: sessionId) else {
throw TranscriptionStorageError.sessionNotFound
}
guard !bundle.segments.isEmpty else {
log("ConversationFinalization: Deleting empty local session \(sessionId)")
try await TranscriptionStorage.shared.deleteSession(id: sessionId)
return false
}
var merged: [APIClient.UploadSegment] = []
for seg in bundle.segments {
let upload = APIClient.UploadSegment(
text: seg.text,
speaker: seg.speakerLabel ?? String(format: "SPEAKER_%02d", seg.speaker),
speaker_id: seg.speaker,
is_user: seg.isUser,
person_id: seg.personId,
start: seg.startTime,
end: seg.endTime
)
if let last = merged.last,
last.speaker_id == upload.speaker_id,
last.speaker == upload.speaker,
last.is_user == upload.is_user,
last.person_id == upload.person_id
{
merged[merged.count - 1] = APIClient.UploadSegment(
text: last.text + " " + upload.text,
speaker: last.speaker,
speaker_id: last.speaker_id,
is_user: last.is_user,
person_id: last.person_id,
start: last.start,
end: upload.end
)
} else {
merged.append(upload)
}
}
let uploadSegments = Self.compactSegmentsForBackendLimit(merged)
if uploadSegments.count != merged.count {
log(
"ConversationFinalization: Compacted local session \(sessionId) from \(merged.count) to \(uploadSegments.count) segments for backend upload"
)
}
let iso = ISO8601DateFormatter()
let clientProcessing = await resolveClientProcessing(
sessionId: sessionId,
uploadSegments: uploadSegments,
startedAt: bundle.session.startedAt
)
let request = APIClient.CreateConversationFromSegmentsRequest(
transcript_segments: uploadSegments,
source: bundle.session.source,
started_at: iso.string(from: bundle.session.startedAt),
finished_at: bundle.session.finishedAt.map { iso.string(from: $0) },
language: bundle.session.language,
client_conversation_id: Self.localClientConversationId(session: bundle.session, sessionId: sessionId),
conversation_role: bundle.session.conversationRole.rawValue,
conversation_finalization_reason: bundle.session.finalizationReason?.rawValue,
client_processing: clientProcessing
)
let response = try await apiClient.createConversationFromSegments(request)
let status = LocalConversationStatus(rawValue: response.status) ?? .processing
let completed = try await TranscriptionStorage.shared.markSessionCompleted(
id: sessionId,
backendId: response.id,
conversationStatus: status,
allowBackendIdOverride: allowBackendIdOverride
)
guard completed else {
if let latest = try await TranscriptionStorage.shared.getSession(id: sessionId),
latest.status == .completed,
latest.backendSynced
{
return response.meetingTreatmentEligible
}
throw TranscriptionStorageError.invalidState(
"from-segments returned \(response.id) but local completion was rejected"
)
}
await hydrateUploadedLocalConversation(id: response.id)
log("ConversationFinalization: Uploaded local session \(sessionId) -> backend conversation \(response.id)")
return response.meetingTreatmentEligible
}
private func resolveClientProcessing(
sessionId: Int64,
uploadSegments: [APIClient.UploadSegment],
startedAt: Date
) async -> Data? {
let flagEnabled = localProjectionHooks?.flagEnabled ?? FreeTierLocalProcessingFlag.isEnabled()
let entitlement: SubscriptionEntitlementDecision
if let pinned = localProjectionHooks?.entitlement {
entitlement = pinned
} else {
entitlement = await SubscriptionEntitlementService.shared.decisionForManagedProactivity()
}
let thermalState = localProjectionHooks?.thermalState ?? ProcessInfo.processInfo.thermalState
let decision = LocalProjectionFinalization.decision(
flagEnabled: flagEnabled,
entitlement: entitlement,
thermalState: thermalState
)
guard decision != .skip else { return nil }
let summarizer: (any ConversationSummarizing)?
if let hooked = localProjectionHooks?.summarizer {
summarizer = hooked
} else {
summarizer = await makeProductionSummarizer()
}
return await LocalProjectionFinalization.projection(
decision: decision,
sessionId: sessionId,
segments: uploadSegments.map(\.hashSegment),
startedAt: startedAt,
summarizer: summarizer
)
}
private func makeProductionSummarizer() async -> ConversationChunkSummarizer? {
guard let queue = await RewindDatabase.shared.getDatabaseQueue() else { return nil }
return ConversationChunkSummarizer(
runtime: LocalInferenceRuntime.makeDefault(),
store: GRDBLocalProjectionStore(queue: queue)
)
}
private func hydrateUploadedLocalConversation(id conversationId: String) async {
do {
let conversation = try await apiClient.getConversation(id: conversationId)
_ = try await TranscriptionStorage.shared.syncServerConversation(conversation)
log("ConversationFinalization: Hydrated uploaded local conversation \(conversationId)")
} catch {
logError(
"ConversationFinalization: Failed to hydrate uploaded local conversation \(conversationId)",
error: error
)
}
}
static func compactSegmentsForBackendLimit(
_ segments: [APIClient.UploadSegment],
maxSegments: Int = 500
) -> [APIClient.UploadSegment] {
guard maxSegments > 0, segments.count > maxSegments else { return segments }
var compacted: [APIClient.UploadSegment] = []
compacted.reserveCapacity(maxSegments)
for index in 0..<maxSegments {
let startIndex = index * segments.count / maxSegments
let endIndex = (index + 1) * segments.count / maxSegments
let group = Array(segments[startIndex..<endIndex])
guard let first = group.first, let last = group.last else { continue }
let sameSpeaker = group.allSatisfy { segment in
segment.speaker == first.speaker
&& segment.speaker_id == first.speaker_id
&& segment.is_user == first.is_user
&& segment.person_id == first.person_id
}
compacted.append(
APIClient.UploadSegment(
text: group.map(\.text).joined(separator: " "),
speaker: sameSpeaker ? first.speaker : "MIXED",
speaker_id: sameSpeaker ? first.speaker_id : nil,
is_user: sameSpeaker ? first.is_user : false,
person_id: sameSpeaker ? first.person_id : nil,
start: first.start,
end: last.end
)
)
}
return compacted
}
private func finalizeCloudSession(
session: TranscriptionSessionRecord,
allowForceProcess: Bool
) async throws -> Bool? {
guard let sessionId = session.id else { return nil }
if let backendId = session.backendId, !backendId.isEmpty {
if let clientConversationId = session.clientConversationId,
!clientConversationId.isEmpty,
backendId != clientConversationId
{
log(
"ConversationFinalization: Rejecting mismatched backend binding for session \(sessionId); resolving exact client recording id instead"
)
if try await completeCloudConversation(
id: clientConversationId,
sessionId: sessionId,
allowForceProcess: allowForceProcess,
allowBackendIdOverride: true
) {
return nil
}
throw TranscriptionStorageError.invalidState(
"Bound backend conversation conflicts with client recording identity")
}
let conversation: ServerConversation
if allowForceProcess {
conversation = try await apiClient.finalizeConversation(id: backendId)
} else {
conversation = try await apiClient.getConversation(id: backendId)
}
if DesktopConversationMatchPolicy.canCompleteBoundBackendConversation(
id: conversation.id,
boundBackendId: backendId,
status: conversation.status,
source: conversation.source
) {
let status = LocalConversationStatus(rawValue: conversation.status.rawValue) ?? .processing
try await TranscriptionStorage.shared.markSessionCompleted(
id: sessionId,
backendId: conversation.id,
conversationStatus: status
)
log("ConversationFinalization: Finalized cloud session \(sessionId) by backend id \(conversation.id)")
return nil
}
throw TranscriptionStorageError.invalidState("Bound backend conversation is not completed")
}
if let clientConversationId = session.clientConversationId, !clientConversationId.isEmpty {
if try await completeCloudConversation(
id: clientConversationId,
sessionId: sessionId,
allowForceProcess: true
) {
return nil
}
}
if allowForceProcess, let conversation = try await apiClient.forceProcessConversation() {
if DesktopConversationMatchPolicy.matchesDesktopConversation(
startedAt: conversation.startedAt,
source: conversation.source,
sessionStartedAt: session.startedAt
) {
let status = LocalConversationStatus(rawValue: conversation.status.rawValue) ?? .processing
try await TranscriptionStorage.shared.markSessionCompleted(
id: sessionId,
backendId: conversation.id,
conversationStatus: status
)
log("ConversationFinalization: Force-processed unbound cloud session \(sessionId) -> \(conversation.id)")
return nil
}
}
let finishedAt = session.finishedAt ?? session.startedAt.addingTimeInterval(1)
let existing = try await apiClient.getConversations(
limit: 5,
statuses: DesktopConversationMatchPolicy.cloudReconciliationStatuses,
includeDiscarded: true,
startDate: session.startedAt.addingTimeInterval(-5),
endDate: finishedAt.addingTimeInterval(5)
)
let timestampMatches = existing.filter { conv in
DesktopConversationMatchPolicy.matchesDesktopConversation(
startedAt: conv.startedAt,
source: conv.source,
sessionStartedAt: session.startedAt
)
}
for match in timestampMatches {
if try await completeTimestampMatchedConversation(match, sessionId: sessionId) {
return nil
}
}
if session.retryCount >= maxRetries - 1 {
if let clientConversationId = session.clientConversationId, !clientConversationId.isEmpty {
if try await completeCloudConversation(
id: clientConversationId,
sessionId: sessionId,
allowForceProcess: true
) {
return nil
}
}
let outcome = try await resolveExhaustedCloudReconciliation(session: session, sessionId: sessionId)
if outcome.handled {
return outcome.meetingTreatmentEligible
}
}
throw TranscriptionStorageError.invalidState("No matching backend conversation found")
}
private func completeTimestampMatchedConversation(
_ match: ServerConversation,
sessionId: Int64
) async throws -> Bool {
let conversation: ServerConversation
if DesktopConversationMatchPolicy.shouldFinalizeTimestampMatchedConversation(status: match.status) {
conversation = try await apiClient.finalizeConversation(id: match.id)
} else {
conversation = match
}
guard
DesktopConversationMatchPolicy.canCompleteTimestampMatchedConversation(
status: conversation.status,
source: conversation.source
), conversation.id == match.id
else {
return false
}
let status = LocalConversationStatus(rawValue: conversation.status.rawValue) ?? .processing
try await TranscriptionStorage.shared.markSessionCompleted(
id: sessionId,
backendId: conversation.id,
conversationStatus: status
)
log("ConversationFinalization: Reconciled cloud session \(sessionId) by timestamp \(conversation.id)")
return true
}
@discardableResult
func resolveExhaustedCloudReconciliation(
session: TranscriptionSessionRecord,
sessionId: Int64
) async throws -> ExhaustedCloudReconciliationOutcome {
let segmentCount = try await TranscriptionStorage.shared.getSegmentCount(sessionId: sessionId)
switch Self.cloudReconciliationExhaustionAction(session: session, segmentCount: segmentCount) {
case .keepRetrying:
return ExhaustedCloudReconciliationOutcome(handled: false, meetingTreatmentEligible: nil)
case .uploadLocalSegments:
log(
"ConversationFinalization: Cloud reconciliation exhausted for session \(sessionId); uploading \(segmentCount) saved local segments"
)
let meetingTreatmentEligible = try await uploadLocalSegments(
sessionId: sessionId,
allowBackendIdOverride: session.backendId?.isEmpty == false
)
return ExhaustedCloudReconciliationOutcome(
handled: true,
meetingTreatmentEligible: meetingTreatmentEligible
)
case .discardEmptyDesktopSession:
log("ConversationFinalization: Deleting empty unreconciled desktop session \(sessionId)")
try await TranscriptionStorage.shared.deleteSession(id: sessionId)
return ExhaustedCloudReconciliationOutcome(handled: true, meetingTreatmentEligible: false)
case .reportFailure:
return ExhaustedCloudReconciliationOutcome(handled: false, meetingTreatmentEligible: nil)
}
}
struct ExhaustedCloudReconciliationOutcome: Equatable {
let handled: Bool
let meetingTreatmentEligible: Bool?
}
enum CloudReconciliationExhaustionAction: Equatable {
case keepRetrying
case uploadLocalSegments
case discardEmptyDesktopSession
case reportFailure
}
static func cloudReconciliationExhaustionAction(
session: TranscriptionSessionRecord,
segmentCount: Int,
maxRetries: Int = 5
) -> CloudReconciliationExhaustionAction {
guard session.retryCount >= maxRetries - 1 else {
return .keepRetrying
}
guard segmentCount == 0 else {
return .uploadLocalSegments
}
guard session.source == ConversationSource.desktop.rawValue else {
return .reportFailure
}
return .discardEmptyDesktopSession
}
private func completeCloudConversation(
id conversationId: String,
sessionId: Int64,
allowForceProcess: Bool,
allowBackendIdOverride: Bool = false
) async throws -> Bool {
let conversation: ServerConversation
do {
if allowForceProcess {
conversation = try await apiClient.finalizeConversation(id: conversationId)
} else {
conversation = try await apiClient.getConversation(id: conversationId)
}
} catch APIError.httpError(let statusCode, _) where statusCode == 404 {
return false
}
guard
DesktopConversationMatchPolicy.canCompleteBoundBackendConversation(
id: conversation.id,
boundBackendId: conversationId,
status: conversation.status,
source: conversation.source
)
else {
return false
}
let status = LocalConversationStatus(rawValue: conversation.status.rawValue) ?? .processing
try await TranscriptionStorage.shared.markSessionCompleted(
id: sessionId,
backendId: conversation.id,
conversationStatus: status,
allowBackendIdOverride: allowBackendIdOverride
)
log("ConversationFinalization: Reconciled cloud session \(sessionId) by conversation id \(conversation.id)")
return true
}
private func markRetryableFailure(sessionId: Int64, error: Error) async {
let message = error.localizedDescription
do {
let session = try await TranscriptionStorage.shared.getSession(id: sessionId)
let retryCount = (session?.retryCount ?? 0) + 1
if retryCount >= maxRetries {
// Retries are exhausted. The in-line reconciliation fallback (resolveExhaustedCloudReconciliation)
// only runs when the final attempt returns cleanly with no match; when it fails by *throwing*
// (backend/network error), we land here instead and would abandon the session, dropping any
// recorded audio/transcript we still hold locally (#9083). Try to finalize from saved local
// segments first so the recording is not lost.
if let session,
let outcome = try? await resolveExhaustedCloudReconciliation(session: session, sessionId: sessionId),
outcome.handled
{
log("ConversationFinalization: Recovered exhausted session \(sessionId) from local data after finalize error")
await postMeetingCompletionIfReady(
session: session,
reason: .retry,
meetingTreatmentEligible: outcome.meetingTreatmentEligible
)
return
}
let segmentCount = try? await TranscriptionStorage.shared.getSegmentCount(sessionId: sessionId)
let diagnostics = ReconciliationFailureDiagnostics(
session: session,
segmentCount: segmentCount,
retryCount: retryCount,
maxRetries: maxRetries,
maxLocalFallbackRetries: maxLocalFallbackRetries
)
await AnalyticsManager.shared.conversationReconciliationFailed(
error: "session_reconciliation_failed",
reason: "cloud_reconcile_exhausted",
source: session?.source,
stage: session?.finalizationStrategy?.rawValue,
retryCount: retryCount,
hasBackendId: session?.backendId?.isEmpty == false,
hasClientConversationId: session?.clientConversationId?.isEmpty == false,
segmentCount: segmentCount,
diagnostics: diagnostics
)
}
try await TranscriptionStorage.shared.incrementRetryCount(id: sessionId)
try await TranscriptionStorage.shared.markSessionFailed(id: sessionId, error: message)
} catch {
logError("ConversationFinalization: Failed to record finalization failure for session \(sessionId)", error: error)
}
}
static func localClientConversationId(session: TranscriptionSessionRecord, sessionId: Int64) -> String {
let startedAtMs = Int64((session.startedAt.timeIntervalSince1970 * 1000).rounded())
return session.clientConversationId ?? "macos-local-\(sessionId)-\(startedAtMs)"
}
/// Meeting completion is a post-sync signal. Persisted max-duration
/// fragments remain silent even when a later crash-recovery attempt uses a
/// generic `.retry` reason.
static func shouldNotifyMeetingCompletion(
session: TranscriptionSessionRecord,
reason: TranscriptionFinalizationReason
) -> Bool {
guard session.conversationRole == .meeting else { return false }
let effectiveReason = session.finalizationReason ?? reason
return effectiveReason == .meetingEnded
|| effectiveReason == .userStop
|| effectiveReason == .crashRecovery
}
static func shouldWakeMeetingCompletion(finalizationStatus: ConversationFinalizationStatusResponse) -> Bool {
finalizationStatus.status == "completed" && finalizationStatus.meetingTreatmentEligible == true
}
private func postMeetingCompletionIfReady(
session: TranscriptionSessionRecord,
reason: TranscriptionFinalizationReason,
meetingTreatmentEligible: Bool?
) async {
guard Self.shouldNotifyMeetingCompletion(session: session, reason: reason), let sessionId = session.id else {
return
}
do {
guard let completed = try await TranscriptionStorage.shared.getSession(id: sessionId),
completed.status == .completed, completed.backendSynced, completed.backendId?.isEmpty == false
else { return }
guard let conversationID = completed.backendId else { return }
// `meetingTreatmentEligible` is deliberately NOT consulted here. It can originate
// from the from-segments response, which returns before the backend has finalized
// the conversation and therefore reports the default `false` rather than a decided
// verdict. `waitForFinalizationProjectionIfNeeded` reads the authoritative
// finalization projection, applies the same eligibility rule via
// `shouldWakeMeetingCompletion`, and fails closed on 404, so it is the only correct
// gate for the wake decision. The parameter is retained for the callers' plumbing;
// removing it is follow-up cleanup, not a behavior change.
guard
await waitForFinalizationProjectionIfNeeded(
conversationID: conversationID,
strategy: completed.finalizationStrategy ?? defaultStrategy(for: completed)
)
else { return }
scheduleMeetingCompletionNotification(conversationID: conversationID)
} catch {
logError("ConversationFinalization: Failed to verify meeting completion \(sessionId)", error: error)
}
}
/// Cloud finalization marks the conversation completed before its durable
/// worker finishes fanout. Do not wake Chat during that gap, and require the
/// backend's duration/speech policy result before notifying: the first
/// materialization would consume its debounce window while no meeting intent
/// exists. A missing legacy job has no authoritative eligibility result and
/// therefore fails closed.
private func waitForFinalizationProjectionIfNeeded(
conversationID: String,
strategy: TranscriptionFinalizationStrategy
) async -> Bool {
guard strategy == .cloudReconcile else { return true }
// The first probe is immediate; subsequent bounded delays cover the normal
// Cloud Tasks admission/worker/fanout path without keeping recovery alive
// indefinitely. A missing projection is an older inline-finalization path.
let delays: [UInt64] = [0, 250_000_000, 500_000_000, 1_000_000_000, 2_000_000_000, 4_000_000_000]
for delay in delays {
if delay > 0 {
try? await Task.sleep(nanoseconds: delay)
}
do {
let status = try await apiClient.getConversationFinalizationStatus(id: conversationID)
if status.status == "completed" {
return Self.shouldWakeMeetingCompletion(finalizationStatus: status)
}
if status.status == "dead_letter" {
log("ConversationFinalization: Skipping meeting wake for dead-letter conversation \(conversationID)")
return false
}
} catch APIError.httpError(statusCode: 404, detail: _) {
// Old inline finalization has no authoritative treatment result. Fail
// closed so a short meeting cannot bypass the backend policy.
return false
} catch {
// Transient status-read failures should not make a completed meeting
// permanently silent; continue through the bounded retry window.
}
}
log("ConversationFinalization: Finalization projection not terminal for \(conversationID); deferring Chat wake")
scheduleFinalizationProjectionPoll(conversationID: conversationID)
return false
}
/// Keep a slow Cloud Tasks fanout from becoming permanently silent after the
/// foreground debounce has been consumed. Poll only the affected conversation
/// and coalesce duplicate recovery attempts by conversation id.
private func scheduleFinalizationProjectionPoll(conversationID: String) {
guard pendingFinalizationProjectionPolls.insert(conversationID).inserted else { return }
Task { [weak self] in
defer {
Task { [weak self] in
await self?.clearFinalizationProjectionPoll(conversationID: conversationID)
}
}
let delays: [UInt64] = [5_000_000_000, 15_000_000_000, 30_000_000_000, 60_000_000_000, 120_000_000_000]
for delay in delays {
try? await Task.sleep(nanoseconds: delay)
guard !Task.isCancelled else { return }
do {
let status = try await self?.apiClient.getConversationFinalizationStatus(id: conversationID)
if let status, status.status == "completed" {
if Self.shouldWakeMeetingCompletion(finalizationStatus: status) {
await self?.scheduleMeetingCompletionNotification(conversationID: conversationID)
}
return
}
if status?.status == "dead_letter" { return }
} catch APIError.httpError(statusCode: 404, detail: _) {
return
} catch {
continue
}
}
}
}
private func clearFinalizationProjectionPoll(conversationID: String) {
pendingFinalizationProjectionPolls.remove(conversationID)
}
/// Coalesce recovery completions arriving in one burst into one Chat wake.
/// The materializer fetches all ready receipts, so one notification is enough
/// and avoids bypassing its foreground debounce once per stale session.
private func scheduleMeetingCompletionNotification(conversationID: String) {
pendingMeetingCompletionConversationIDs.insert(conversationID)
guard meetingCompletionNotificationTask == nil else { return }
meetingCompletionNotificationTask = Task { [weak self] in
try? await Task.sleep(nanoseconds: 250_000_000)
guard !Task.isCancelled else { return }
await self?.flushMeetingCompletionNotifications()
}
}
private func flushMeetingCompletionNotifications() {
guard !pendingMeetingCompletionConversationIDs.isEmpty else {
meetingCompletionNotificationTask = nil
return
}
let conversationIDs = Array(pendingMeetingCompletionConversationIDs).sorted()
pendingMeetingCompletionConversationIDs.removeAll()
meetingCompletionNotificationTask = nil
let notification = MeetingCompletionNotification(conversationIDs: conversationIDs)
Task { @MainActor in
NotificationCenter.default.post(
name: .desktopMeetingConversationDidComplete,
object: notification
)
}
}
/// The default Apple Silicon path creates its backend conversation through
/// `/from-segments`, so no cloud-listen `memory_created` event exists. Emit the
/// same activation contract at that successful, exactly-once storage transition.
static func localConversationCreatedTelemetry(
session: TranscriptionSessionRecord,
conversationId: String
) -> ConversationCreatedTelemetry {
ConversationCreatedTelemetry(session: session, conversationId: conversationId)
}
}
extension Notification.Name {
static let desktopMeetingConversationDidComplete = Notification.Name(
"com.omi.desktop.meetingConversationDidComplete")
}
/// Test seam for S11 attach. Production reads the client flag, S13
/// entitlement cache, live thermal state, and the GRDB summarizer.
struct LocalProjectionTestHooks: Sendable {
var flagEnabled: Bool
var entitlement: SubscriptionEntitlementDecision
var thermalState: ProcessInfo.ThermalState
var summarizer: (any ConversationSummarizing)?
}
struct ConversationCreatedTelemetry: Equatable, Sendable {
let conversationId: String
let source: String
let durationSeconds: Int?
init(session: TranscriptionSessionRecord, conversationId: String) {
self.conversationId = conversationId
source = session.source
durationSeconds = session.finishedAt.map {
max(0, Int($0.timeIntervalSince(session.startedAt)))
}
}
}
struct MeetingCompletionNotification: Sendable, Equatable {
let conversationIDs: [String]
}
struct ReconciliationFailureDiagnostics {
let sessionStatus: String?
let conversationStatus: String?
let finalizationReason: String?
let hasFinishedAt: Bool
let hasFinalizationStartedAt: Bool
let hasFinalizationCompletedAt: Bool
let hasInputDeviceName: Bool
let hasLocalSegments: Bool?
let sessionAgeSeconds: Int?
let sessionDurationSeconds: Int?
let localFallbackAvailable: Bool
let localFallbackRetriesRemaining: Int
init(
session: TranscriptionSessionRecord?,
segmentCount: Int?,
retryCount: Int,
maxRetries: Int,
maxLocalFallbackRetries: Int
) {
let now = Date()
sessionStatus = session?.status.rawValue
conversationStatus = session?.conversationStatus.rawValue
finalizationReason = session?.finalizationReason?.rawValue
hasFinishedAt = session?.finishedAt != nil
hasFinalizationStartedAt = session?.finalizationStartedAt != nil
hasFinalizationCompletedAt = session?.finalizationCompletedAt != nil
hasInputDeviceName = session?.inputDeviceName?.isEmpty == false
hasLocalSegments = segmentCount.map { $0 > 0 }
sessionAgeSeconds = session.map { max(0, Int(now.timeIntervalSince($0.createdAt).rounded())) }
if let startedAt = session?.startedAt {
let finishedAt = session?.finishedAt ?? now
sessionDurationSeconds = max(0, Int(finishedAt.timeIntervalSince(startedAt).rounded()))
} else {
sessionDurationSeconds = nil
}
localFallbackAvailable =
session?.finalizationStrategy == .cloudReconcile
&& (segmentCount ?? 0) > 0
&& retryCount >= maxRetries
localFallbackRetriesRemaining = max(0, maxRetries + maxLocalFallbackRetries - retryCount)
}
}