GeezerVison is showing github.com — fix(apple-intents): retry failed syncs and reconcile cross-process re… · pingdotgg/t3code@f344079
File treeExpand file treeCollapse file tree Open diff view settings mobile/src/features/apple-intents web/src/components/desktop
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/apple-intents/appleIntentsRuntime.ts +28-3Lines changed: 28 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,6 +36,8 @@ import { | 36 | 36 |
| 37 | 37 | const DPOP_PROOF_KEY_STORAGE_KEY = "t3code.cloud.dpop-proof-key"; | 38 | 38 | const SYNC_DEBOUNCE_MS = 400; | | 39 | +const SYNC_RETRY_BASE_MS = 2_000; | | 40 | +const SYNC_MAX_RETRIES = 5; | 39 | 41 |
| 40 | 42 | const activeOnscreenThreadRefAtom = Atom.make<ScopedThreadRef | null>(null).pipe( | 41 | 43 | Atom.keepAlive, | @@ -218,14 +220,33 @@ export function installAppleIntents(): () => void { | 218 | 220 |
| 219 | 221 | let disposed = false; | 220 | 222 | let debounceTimer: ReturnType<typeof setTimeout> | null = null; | | 223 | + let retryTimer: ReturnType<typeof setTimeout> | null = null; | | 224 | + let retryAttempt = 0; | | 225 | + | | 226 | + // An unchanged source never re-triggers the subscription, so a failed sync retries on its own. | | 227 | + const scheduleRetry = () => { | | 228 | + if (disposed || retryTimer !== null || retryAttempt >= SYNC_MAX_RETRIES) return; | | 229 | + const delay = SYNC_RETRY_BASE_MS * 2 ** retryAttempt; | | 230 | + retryAttempt += 1; | | 231 | + retryTimer = setTimeout(() => { | | 232 | + retryTimer = null; | | 233 | + flush(appAtomRegistry.get(appleIntentsSourceAtom)); | | 234 | + }, delay); | | 235 | + }; | 221 | 236 |
| 222 | 237 | const flush = (source: Atom.Type<typeof appleIntentsSourceAtom>) => { | 223 | 238 | void enqueueAppleIntentsWork(async () => { | 224 | 239 | if (disposed || !source.preferencesReady) return; | 225 | 240 | await syncNativeIndex(source); | 226 | | - }).catch((error: unknown) => { | 227 | | - console.warn("[apple-intents] sync failed", error); | 228 | | - }); | | 241 | + }).then( | | 242 | + () => { | | 243 | + retryAttempt = 0; | | 244 | + }, | | 245 | + (error: unknown) => { | | 246 | + console.warn("[apple-intents] sync failed", error); | | 247 | + scheduleRetry(); | | 248 | + }, | | 249 | + ); | 229 | 250 | }; | 230 | 251 |
| 231 | 252 | const unsubscribe = appAtomRegistry.subscribe( | @@ -246,6 +267,10 @@ export function installAppleIntents(): () => void { | 246 | 267 | clearTimeout(debounceTimer); | 247 | 268 | debounceTimer = null; | 248 | 269 | } | | 270 | + if (retryTimer !== null) { | | 271 | + clearTimeout(retryTimer); | | 272 | + retryTimer = null; | | 273 | + } | 249 | 274 | unsubscribe(); | 250 | 275 | }; | 251 | 276 | } |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/desktop/DesktopAppleIntentsCoordinator.tsx +28-3Lines changed: 28 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,6 +29,8 @@ import { environmentShell } from "../../state/shell"; | 29 | 29 | import { resolveThreadRouteTarget } from "../../threadRoutes"; | 30 | 30 |
| 31 | 31 | const SYNC_DEBOUNCE_MS = 400; | | 32 | +const SYNC_RETRY_BASE_MS = 2_000; | | 33 | +const SYNC_MAX_RETRIES = 5; | 32 | 34 |
| 33 | 35 | interface RawDesktopCredential { | 34 | 36 | readonly environmentId: EnvironmentId; | @@ -227,15 +229,34 @@ export function DesktopAppleIntentsCoordinator() { | 227 | 229 |
| 228 | 230 | let disposed = false; | 229 | 231 | let debounceTimer: ReturnType<typeof setTimeout> | null = null; | | 232 | + let retryTimer: ReturnType<typeof setTimeout> | null = null; | | 233 | + let retryAttempt = 0; | 230 | 234 | const cleanups: Array<() => void> = []; | 231 | 235 |
| | 236 | + // Unchanged sources never re-trigger a flush, so a failed sync retries on its own. | | 237 | + const scheduleRetry = () => { | | 238 | + if (disposed || retryTimer !== null || retryAttempt >= SYNC_MAX_RETRIES) return; | | 239 | + const delay = SYNC_RETRY_BASE_MS * 2 ** retryAttempt; | | 240 | + retryAttempt += 1; | | 241 | + retryTimer = setTimeout(() => { | | 242 | + retryTimer = null; | | 243 | + flush(); | | 244 | + }, delay); | | 245 | + }; | | 246 | + | 232 | 247 | const flush = () => { | 233 | 248 | void enqueueAppleIntentsWork(async () => { | 234 | 249 | if (disposed) return; | 235 | 250 | await syncNativeIndex(bridge, router); | 236 | | - }).catch((error: unknown) => { | 237 | | - console.warn("[apple-intents] desktop sync failed", error); | 238 | | - }); | | 251 | + }).then( | | 252 | + () => { | | 253 | + retryAttempt = 0; | | 254 | + }, | | 255 | + (error: unknown) => { | | 256 | + console.warn("[apple-intents] desktop sync failed", error); | | 257 | + scheduleRetry(); | | 258 | + }, | | 259 | + ); | 239 | 260 | }; | 240 | 261 |
| 241 | 262 | const scheduleFlush = () => { | @@ -267,6 +288,10 @@ export function DesktopAppleIntentsCoordinator() { | 267 | 288 | clearTimeout(debounceTimer); | 268 | 289 | debounceTimer = null; | 269 | 290 | } | | 291 | + if (retryTimer !== null) { | | 292 | + clearTimeout(retryTimer); | | 293 | + retryTimer = null; | | 294 | + } | 270 | 295 | for (const cleanup of cleanups) cleanup(); | 271 | 296 | }; | 272 | 297 | }, [router]); | |
Collapse file Copy file name to clipboardExpand all lines: native/apple-intents/Sources/T3IntentsCore/Bridge/T3IntentsRuntime.swift +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,8 +2,9 @@ import AppIntents | 2 | 2 | import CoreSpotlight | 3 | 3 | import Foundation | 4 | 4 |
| 5 | | -/// Runs index mutations one at a time, each store write together with its Spotlight update, so a | 6 | | -/// clear can never interleave with an in-flight sync or a system reindex callback. | | 5 | +/// Runs this process's index mutations one at a time, each store write together with its Spotlight | | 6 | +/// update, so a clear never interleaves with an in-flight sync or reindex callback here. Reindex | | 7 | +/// callbacks in another process are reconciled by `T3SpotlightIndexer.reindex` instead. | 7 | 8 | enum T3IndexOperationQueue { | 8 | 9 | private static let lock = NSLock() | 9 | 10 | nonisolated(unsafe) private static var tail: Task<Void, Never>? | |
Collapse file Copy file name to clipboardExpand all lines: native/apple-intents/Sources/T3IntentsCore/Entities/ProjectEntity.swift +2-8Lines changed: 2 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -120,20 +120,14 @@ public struct ProjectEntityQuery: IndexedEntityQuery, EnumerableEntityQuery, Ent | 120 | 120 | ) async throws { | 121 | 121 | let store = store | 122 | 122 | try await T3IndexOperationQueue.run { | 123 | | - let entities = store.projects(for: identifiers).map(ProjectEntity.init(record:)) | 124 | | - try await T3SpotlightIndexer.withDeadline { | 125 | | - try await T3SpotlightIndexer.indexAppEntities(projects: entities) | 126 | | - } | | 123 | + try await T3SpotlightIndexer.reindex(projects: store.projects(for: identifiers), store: store) | 127 | 124 | } | 128 | 125 | } | 129 | 126 |
| 130 | 127 | public func reindexAllEntities(indexDescription: CSSearchableIndexDescription) async throws { | 131 | 128 | let store = store | 132 | 129 | try await T3IndexOperationQueue.run { | 133 | | - let entities = store.allProjects().map(ProjectEntity.init(record:)) | 134 | | - try await T3SpotlightIndexer.withDeadline { | 135 | | - try await T3SpotlightIndexer.indexAppEntities(projects: entities) | 136 | | - } | | 130 | + try await T3SpotlightIndexer.reindex(projects: store.allProjects(), store: store) | 137 | 131 | } | 138 | 132 | } | 139 | 133 | } |
Collapse file Copy file name to clipboardExpand all lines: native/apple-intents/Sources/T3IntentsCore/Entities/ThreadEntity.swift +2-8Lines changed: 2 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -215,20 +215,14 @@ public struct ThreadEntityQuery: IndexedEntityQuery, EntityStringQuery, Sendable | 215 | 215 | ) async throws { | 216 | 216 | let store = store | 217 | 217 | try await T3IndexOperationQueue.run { | 218 | | - let entities = store.threads(for: identifiers).map(ThreadEntity.init(record:)) | 219 | | - try await T3SpotlightIndexer.withDeadline { | 220 | | - try await T3SpotlightIndexer.indexAppEntities(threads: entities) | 221 | | - } | | 218 | + try await T3SpotlightIndexer.reindex(threads: store.threads(for: identifiers), store: store) | 222 | 219 | } | 223 | 220 | } | 224 | 221 |
| 225 | 222 | public func reindexAllEntities(indexDescription: CSSearchableIndexDescription) async throws { | 226 | 223 | let store = store | 227 | 224 | try await T3IndexOperationQueue.run { | 228 | | - let entities = store.allThreads().map(ThreadEntity.init(record:)) | 229 | | - try await T3SpotlightIndexer.withDeadline { | 230 | | - try await T3SpotlightIndexer.indexAppEntities(threads: entities) | 231 | | - } | | 225 | + try await T3SpotlightIndexer.reindex(threads: store.allThreads(), store: store) | 232 | 226 | } | 233 | 227 | } | 234 | 228 | } | |
Collapse file Copy file name to clipboardExpand all lines: native/apple-intents/Sources/T3IntentsCore/Spotlight/T3SpotlightIndexer.swift +30Lines changed: 30 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -41,6 +41,36 @@ public enum T3SpotlightIndexer { | 41 | 41 | } | 42 | 42 | } | 43 | 43 |
| | 44 | + /// System reindex callbacks can run in the extension while the host clears the shared store, and | | 45 | + /// the in-process queue cannot order the two. A clear empties the store before Spotlight, so | | 46 | + /// re-reading the store after writing finds, and deletes, anything that landed after that clear. | | 47 | + public static func reindex( | | 48 | + projects: [T3ProjectRecord] = [], | | 49 | + threads: [T3ThreadRecord] = [], | | 50 | + store: T3IndexStore | | 51 | + ) async throws { | | 52 | + try await indexEntities(projects: projects, threads: threads) | | 53 | + let missing = missingRecordIds( | | 54 | + projectIds: projects.map(\.id), | | 55 | + threadIds: threads.map(\.id), | | 56 | + in: store | | 57 | + ) | | 58 | + try await removeEntities(threadIds: missing.threadIds, projectIds: missing.projectIds) | | 59 | + } | | 60 | + | | 61 | + static func missingRecordIds( | | 62 | + projectIds: [String], | | 63 | + threadIds: [String], | | 64 | + in store: T3IndexStore | | 65 | + ) -> (projectIds: [String], threadIds: [String]) { | | 66 | + let keptProjects = Set(store.projects(for: projectIds).map(\.id)) | | 67 | + let keptThreads = Set(store.threads(for: threadIds).map(\.id)) | | 68 | + return ( | | 69 | + projectIds.filter { !keptProjects.contains($0) }, | | 70 | + threadIds.filter { !keptThreads.contains($0) } | | 71 | + ) | | 72 | + } | | 73 | + | 44 | 74 | public static func clearAll() async throws { | 45 | 75 | try await withDeadline { | 46 | 76 | try await CSSearchableIndex.default().deleteAllSearchableItems() | |
Collapse file Copy file name to clipboardExpand all lines: native/apple-intents/Tests/T3IntentsCoreTests/T3IntentsCoreTests.swift +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -292,6 +292,27 @@ struct T3IntentsCoreTests { | 292 | 292 | #expect(try await T3SpotlightIndexer.withDeadline(seconds: 5) { 42 } == 42) | 293 | 293 | } | 294 | 294 |
| | 295 | + @Test("A reindex that races a clear in another process finds what the clear removed") | | 296 | + func reindexReconcilesAgainstStore() throws { | | 297 | + let store = makeTempStore() | | 298 | + try store.upsertThreadRecord(makeThread(id: "thread-1")) | | 299 | + let beforeClear = T3SpotlightIndexer.missingRecordIds( | | 300 | + projectIds: ["env-1:proj-gone"], | | 301 | + threadIds: ["env-1:thread-1", "env-1:thread-gone"], | | 302 | + in: store | | 303 | + ) | | 304 | + #expect(beforeClear.projectIds == ["env-1:proj-gone"]) | | 305 | + #expect(beforeClear.threadIds == ["env-1:thread-gone"]) | | 306 | + | | 307 | + try store.clearAll() | | 308 | + let afterClear = T3SpotlightIndexer.missingRecordIds( | | 309 | + projectIds: [], | | 310 | + threadIds: ["env-1:thread-1"], | | 311 | + in: store | | 312 | + ) | | 313 | + #expect(afterClear.threadIds == ["env-1:thread-1"]) | | 314 | + } | | 315 | + | 295 | 316 | private func makeThread(id: String, status: String = "idle") -> T3ThreadRecord { | 296 | 317 | T3ThreadRecord( | 297 | 318 | id: "env-1:\(id)", | |
|