GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(apple-intents): retry failed syncs and reconcile cross-process re… · pingdotgg/t3code@f344079

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps
    • mobile/src/features/apple-intents
      • appleIntentsRuntime.ts
    • web/src/components/desktop
      • DesktopAppleIntentsCoordinator.tsx
  • native/apple-intents
    • Sources/T3IntentsCore
      • Bridge
        • T3IntentsRuntime.swift
      • Entities
        • ProjectEntity.swift
        • ThreadEntity.swift
      • Spotlight
        • T3SpotlightIndexer.swift
    • Tests/T3IntentsCoreTests
      • T3IntentsCoreTests.swift
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/mobile/src/features/apple-intents/appleIntentsRuntime.ts‎

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 numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import {
3636

3737
const DPOP_PROOF_KEY_STORAGE_KEY = "t3code.cloud.dpop-proof-key";
3838
const SYNC_DEBOUNCE_MS = 400;
39+
const SYNC_RETRY_BASE_MS = 2_000;
40+
const SYNC_MAX_RETRIES = 5;
3941

4042
const activeOnscreenThreadRefAtom = Atom.make<ScopedThreadRef | null>(null).pipe(
4143
Atom.keepAlive,
@@ -218,14 +220,33 @@ export function installAppleIntents(): () => void {
218220

219221
let disposed = false;
220222
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+
};
221236

222237
const flush = (source: Atom.Type<typeof appleIntentsSourceAtom>) => {
223238
void enqueueAppleIntentsWork(async () => {
224239
if (disposed || !source.preferencesReady) return;
225240
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+
);
229250
};
230251

231252
const unsubscribe = appAtomRegistry.subscribe(
@@ -246,6 +267,10 @@ export function installAppleIntents(): () => void {
246267
clearTimeout(debounceTimer);
247268
debounceTimer = null;
248269
}
270+
if (retryTimer !== null) {
271+
clearTimeout(retryTimer);
272+
retryTimer = null;
273+
}
249274
unsubscribe();
250275
};
251276
}
Collapse file

‎apps/web/src/components/desktop/DesktopAppleIntentsCoordinator.tsx‎

Copy file name to clipboardExpand all lines: apps/web/src/components/desktop/DesktopAppleIntentsCoordinator.tsx
+28-3Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import { environmentShell } from "../../state/shell";
2929
import { resolveThreadRouteTarget } from "../../threadRoutes";
3030

3131
const SYNC_DEBOUNCE_MS = 400;
32+
const SYNC_RETRY_BASE_MS = 2_000;
33+
const SYNC_MAX_RETRIES = 5;
3234

3335
interface RawDesktopCredential {
3436
readonly environmentId: EnvironmentId;
@@ -227,15 +229,34 @@ export function DesktopAppleIntentsCoordinator() {
227229

228230
let disposed = false;
229231
let debounceTimer: ReturnType<typeof setTimeout> | null = null;
232+
let retryTimer: ReturnType<typeof setTimeout> | null = null;
233+
let retryAttempt = 0;
230234
const cleanups: Array<() => void> = [];
231235

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+
232247
const flush = () => {
233248
void enqueueAppleIntentsWork(async () => {
234249
if (disposed) return;
235250
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+
);
239260
};
240261

241262
const scheduleFlush = () => {
@@ -267,6 +288,10 @@ export function DesktopAppleIntentsCoordinator() {
267288
clearTimeout(debounceTimer);
268289
debounceTimer = null;
269290
}
291+
if (retryTimer !== null) {
292+
clearTimeout(retryTimer);
293+
retryTimer = null;
294+
}
270295
for (const cleanup of cleanups) cleanup();
271296
};
272297
}, [router]);
Collapse file

‎native/apple-intents/Sources/T3IntentsCore/Bridge/T3IntentsRuntime.swift‎

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 numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import AppIntents
22
import CoreSpotlight
33
import Foundation
44

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.
78
enum T3IndexOperationQueue {
89
private static let lock = NSLock()
910
nonisolated(unsafe) private static var tail: Task<Void, Never>?
Collapse file

‎native/apple-intents/Sources/T3IntentsCore/Entities/ProjectEntity.swift‎

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 numberDiff line numberDiff line change
@@ -120,20 +120,14 @@ public struct ProjectEntityQuery: IndexedEntityQuery, EnumerableEntityQuery, Ent
120120
) async throws {
121121
let store = store
122122
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)
127124
}
128125
}
129126

130127
public func reindexAllEntities(indexDescription: CSSearchableIndexDescription) async throws {
131128
let store = store
132129
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)
137131
}
138132
}
139133
}
Collapse file

‎native/apple-intents/Sources/T3IntentsCore/Entities/ThreadEntity.swift‎

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 numberDiff line numberDiff line change
@@ -215,20 +215,14 @@ public struct ThreadEntityQuery: IndexedEntityQuery, EntityStringQuery, Sendable
215215
) async throws {
216216
let store = store
217217
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)
222219
}
223220
}
224221

225222
public func reindexAllEntities(indexDescription: CSSearchableIndexDescription) async throws {
226223
let store = store
227224
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)
232226
}
233227
}
234228
}
Collapse file

‎native/apple-intents/Sources/T3IntentsCore/Spotlight/T3SpotlightIndexer.swift‎

Copy file name to clipboardExpand all lines: native/apple-intents/Sources/T3IntentsCore/Spotlight/T3SpotlightIndexer.swift
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,36 @@ public enum T3SpotlightIndexer {
4141
}
4242
}
4343

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+
4474
public static func clearAll() async throws {
4575
try await withDeadline {
4676
try await CSSearchableIndex.default().deleteAllSearchableItems()
Collapse file

‎native/apple-intents/Tests/T3IntentsCoreTests/T3IntentsCoreTests.swift‎

Copy file name to clipboardExpand all lines: native/apple-intents/Tests/T3IntentsCoreTests/T3IntentsCoreTests.swift
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,27 @@ struct T3IntentsCoreTests {
292292
#expect(try await T3SpotlightIndexer.withDeadline(seconds: 5) { 42 } == 42)
293293
}
294294

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+
295316
private func makeThread(id: String, status: String = "idle") -> T3ThreadRecord {
296317
T3ThreadRecord(
297318
id: "env-1:\(id)",

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.