forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnboardingImportEvidenceService.swift
More file actions
315 lines (281 loc) · 10.7 KB
/
Copy pathOnboardingImportEvidenceService.swift
File metadata and controls
315 lines (281 loc) · 10.7 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
import Foundation
protocol ImportEvidenceBatchCreating {
func createMemoryImportBatch(_ batch: ImportEvidenceBatch) async throws -> ImportEvidenceBatchResponse
func createMemoryImportBatch(
_ batch: ImportEvidenceBatch,
authorizationSnapshot: RuntimeOwnerAuthorizationSnapshot?
) async throws -> ImportEvidenceBatchResponse
}
protocol MemoryBatchCreating {
func createMemoriesBatch(_ memories: [MemoryBatchItem]) async throws -> BatchMemoriesResponse
func createMemoriesBatch(
_ memories: [MemoryBatchItem],
authorizationSnapshot: RuntimeOwnerAuthorizationSnapshot?
) async throws -> BatchMemoriesResponse
}
extension ImportEvidenceBatchCreating {
func createMemoryImportBatch(
_ batch: ImportEvidenceBatch,
authorizationSnapshot _: RuntimeOwnerAuthorizationSnapshot?
) async throws -> ImportEvidenceBatchResponse {
try await createMemoryImportBatch(batch)
}
}
extension MemoryBatchCreating {
func createMemoriesBatch(
_ memories: [MemoryBatchItem],
authorizationSnapshot _: RuntimeOwnerAuthorizationSnapshot?
) async throws -> BatchMemoriesResponse {
try await createMemoriesBatch(memories)
}
}
extension APIClient: ImportEvidenceBatchCreating {}
extension APIClient: MemoryBatchCreating {}
enum OnboardingImportEvidenceService {
private static let retryBackoffSeconds: [UInt64] = [2, 5, 10]
static func save(
_ artifacts: [ImportEvidenceBatchItem],
sourceType: String,
logPrefix: String,
importRunId: String? = nil,
sourceAccountHash: String? = nil,
legacyMemories: [MemoryBatchItem]? = nil,
authorizationSnapshot: RuntimeOwnerAuthorizationSnapshot? = nil,
apiClient: ImportEvidenceBatchCreating = APIClient.shared,
legacyApiClient: MemoryBatchCreating = APIClient.shared,
sleep: @escaping (UInt64) async -> Void = sleepSeconds
) async -> (saved: Int, failed: Int) {
guard !artifacts.isEmpty, isAuthorized(authorizationSnapshot) else { return (0, 0) }
let importRunId = importRunId ?? Self.newImportRunId(sourceType: sourceType)
let artifacts = withClientDeviceProvenance(artifacts)
let chunks = artifacts.chunked(maxSize: APIClient.memoryImportBatchMaxSize)
var saved = 0
var failed = 0
for chunk in chunks {
guard isAuthorized(authorizationSnapshot) else { return (saved, failed) }
do {
let response = try await createChunkWithRetry(
chunk,
sourceType: sourceType,
importRunId: importRunId,
sourceAccountHash: sourceAccountHash,
logPrefix: logPrefix,
apiClient: apiClient,
authorizationSnapshot: authorizationSnapshot,
sleep: sleep
)
guard isAuthorized(authorizationSnapshot) else { return (saved, failed) }
saved += response.artifactsCreated + response.artifactsDeduped
failed += max(0, chunk.count - response.artifactsReceived)
} catch {
if Self.isLegacyMemorySystemError(error), let legacyMemories {
log("\(logPrefix): Import evidence unavailable for legacy memory system; using legacy memory batch path")
return await OnboardingMemoryBatchImportService.save(
legacyMemories,
logPrefix: logPrefix,
authorizationSnapshot: authorizationSnapshot,
apiClient: legacyApiClient,
sleep: sleep
)
}
failed += chunk.count
log("\(logPrefix): Failed saving import evidence batch (\(chunk.count) items): \(error)")
}
}
return (saved, failed)
}
private static func withClientDeviceProvenance(_ artifacts: [ImportEvidenceBatchItem]) -> [ImportEvidenceBatchItem] {
let deviceId = ClientDeviceService.shared.clientDeviceId
return artifacts.map { artifact in
guard artifact.clientDeviceId == nil else { return artifact }
return ImportEvidenceBatchItem(
externalId: artifact.externalId,
occurredAt: artifact.occurredAt,
title: artifact.title,
snippet: artifact.snippet,
content: artifact.content,
contentHash: artifact.contentHash,
metadata: artifact.metadata,
clientDeviceId: deviceId
)
}
}
private static func createChunkWithRetry(
_ chunk: [ImportEvidenceBatchItem],
sourceType: String,
importRunId: String,
sourceAccountHash: String?,
logPrefix: String,
apiClient: ImportEvidenceBatchCreating,
authorizationSnapshot: RuntimeOwnerAuthorizationSnapshot?,
sleep: @escaping (UInt64) async -> Void
) async throws -> ImportEvidenceBatchResponse {
var lastError: Error?
for attempt in 0...retryBackoffSeconds.count {
do {
guard isAuthorized(authorizationSnapshot) else { throw AuthError.userChangedDuringRequest }
return try await apiClient.createMemoryImportBatch(
ImportEvidenceBatch(
sourceType: sourceType,
importRunId: importRunId,
sourceAccountHash: sourceAccountHash,
items: chunk
),
authorizationSnapshot: authorizationSnapshot
)
} catch {
lastError = error
guard shouldRetry(error), attempt < retryBackoffSeconds.count else {
throw error
}
let delay = retryBackoffSeconds[attempt]
log(
"\(logPrefix): Retrying import evidence batch after \(delay)s "
+ "(\(chunk.count) items, attempt \(attempt + 2)): \(error)"
)
await sleep(delay)
}
}
throw lastError ?? APIError.invalidResponse
}
private static func shouldRetry(_ error: Error) -> Bool {
if case APIError.httpError(let statusCode, _) = error {
return statusCode == 429 || (500...599).contains(statusCode)
}
guard let urlError = error as? URLError else { return false }
switch urlError.code {
case .timedOut,
.cannotFindHost,
.cannotConnectToHost,
.dnsLookupFailed,
.networkConnectionLost,
.notConnectedToInternet:
return true
default:
return false
}
}
private static func sleepSeconds(_ seconds: UInt64) async {
try? await Task.sleep(nanoseconds: seconds * 1_000_000_000)
}
private static func isAuthorized(_ authorizationSnapshot: RuntimeOwnerAuthorizationSnapshot?) -> Bool {
!Task.isCancelled
&& (authorizationSnapshot.map(RuntimeOwnerIdentity.isAuthorizationCurrent) ?? true)
}
private static func isLegacyMemorySystemError(_ error: Error) -> Bool {
guard case APIError.httpError(let statusCode, let detail) = error else { return false }
if statusCode == 403 && detail == "memory_import_requires_canonical" { return true }
// Deployments without the canonical import router (prod today) 404 this
// endpoint; without falling back the whole scan context is silently lost.
return statusCode == 404
}
private static func newImportRunId(sourceType: String) -> String {
let normalizedSource =
sourceType
.lowercased()
.replacingOccurrences(of: #"[^a-z0-9_:-]+"#, with: "-", options: .regularExpression)
.trimmingCharacters(in: CharacterSet(charactersIn: "-"))
return "desktop-\(normalizedSource)-\(UUID().uuidString.lowercased())"
}
}
enum OnboardingMemoryBatchImportService {
private static let retryBackoffSeconds: [UInt64] = [2, 5, 10]
static func save(
_ memories: [MemoryBatchItem],
logPrefix: String,
authorizationSnapshot: RuntimeOwnerAuthorizationSnapshot? = nil,
apiClient: MemoryBatchCreating = APIClient.shared,
sleep: @escaping (UInt64) async -> Void = sleepSeconds
) async -> (saved: Int, failed: Int) {
guard !memories.isEmpty, isAuthorized(authorizationSnapshot) else { return (0, 0) }
let chunks = memories.chunked(maxSize: APIClient.memoriesBatchMaxSize)
var saved = 0
var failed = 0
for chunk in chunks {
guard isAuthorized(authorizationSnapshot) else { return (saved, failed) }
do {
let response = try await createChunkWithRetry(
chunk,
logPrefix: logPrefix,
apiClient: apiClient,
authorizationSnapshot: authorizationSnapshot,
sleep: sleep
)
guard isAuthorized(authorizationSnapshot) else { return (saved, failed) }
saved += response.createdCount
failed += max(0, chunk.count - response.createdCount)
} catch {
failed += chunk.count
log("\(logPrefix): Failed saving legacy memory batch (\(chunk.count) items): \(error)")
}
}
return (saved, failed)
}
private static func createChunkWithRetry(
_ chunk: [MemoryBatchItem],
logPrefix: String,
apiClient: MemoryBatchCreating,
authorizationSnapshot: RuntimeOwnerAuthorizationSnapshot?,
sleep: @escaping (UInt64) async -> Void
) async throws -> BatchMemoriesResponse {
var lastError: Error?
for attempt in 0...retryBackoffSeconds.count {
do {
guard isAuthorized(authorizationSnapshot) else { throw AuthError.userChangedDuringRequest }
return try await apiClient.createMemoriesBatch(
chunk,
authorizationSnapshot: authorizationSnapshot)
} catch {
lastError = error
guard shouldRetry(error), attempt < retryBackoffSeconds.count else {
throw error
}
let delay = retryBackoffSeconds[attempt]
log(
"\(logPrefix): Retrying legacy memory batch after \(delay)s "
+ "(\(chunk.count) items, attempt \(attempt + 2)): \(error)"
)
await sleep(delay)
}
}
throw lastError ?? APIError.invalidResponse
}
private static func shouldRetry(_ error: Error) -> Bool {
if case APIError.httpError(let statusCode, _) = error {
return statusCode == 429 || (500...599).contains(statusCode)
}
guard let urlError = error as? URLError else { return false }
switch urlError.code {
case .timedOut,
.cannotFindHost,
.cannotConnectToHost,
.dnsLookupFailed,
.networkConnectionLost,
.notConnectedToInternet:
return true
default:
return false
}
}
private static func sleepSeconds(_ seconds: UInt64) async {
try? await Task.sleep(nanoseconds: seconds * 1_000_000_000)
}
private static func isAuthorized(_ authorizationSnapshot: RuntimeOwnerAuthorizationSnapshot?) -> Bool {
!Task.isCancelled
&& (authorizationSnapshot.map(RuntimeOwnerIdentity.isAuthorizationCurrent) ?? true)
}
}
extension Array {
func chunked(maxSize: Int) -> [[Element]] {
precondition(maxSize > 0, "chunk size must be positive")
guard !isEmpty else { return [] }
var chunks: [[Element]] = []
var index = startIndex
while index < endIndex {
let chunkEnd = self.index(index, offsetBy: maxSize, limitedBy: endIndex) ?? endIndex
chunks.append(Array(self[index..<chunkEnd]))
index = chunkEnd
}
return chunks
}
}