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): serialize clears with syncs and keep blocking FFI… · pingdotgg/t3code@adf3b7e

File tree

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

‎apps/desktop/src/intents/DesktopAppleIntents.ts‎

Copy file name to clipboardExpand all lines: apps/desktop/src/intents/DesktopAppleIntents.ts
+48-15Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ export class DesktopAppleIntentsNativeError extends Schema.TaggedError<DesktopAp
2828
interface AppleIntentsNativeBinding {
2929
readonly isSupported: () => boolean;
3030
readonly bootstrap: () => void;
31-
readonly syncIndex: (json: string) => number;
32-
readonly removeEnvironment: (environmentId: string) => number;
33-
readonly clearIndex: () => number;
31+
readonly syncIndex: (json: string) => Promise<number>;
32+
readonly removeEnvironment: (environmentId: string) => Promise<number>;
33+
readonly clearIndex: () => Promise<number>;
3434
readonly setOnscreenContext: (json: string) => number;
3535
readonly syncAuthCredentials: (json: string) => number;
3636
readonly resolveSpotlightUrl: (identifier: string) => string | null;
@@ -63,15 +63,26 @@ async function loadNativeBinding(dylibPath: string): Promise<AppleIntentsNativeB
6363
paramsValue: [value],
6464
});
6565

66+
// These exports block until Spotlight finishes, so they run off the Electron main thread.
67+
const callInWorker = (funcName: string, params: ReadonlyArray<string>): Promise<number> =>
68+
load({
69+
library: FFI_LIBRARY_KEY,
70+
funcName,
71+
retType: DataType.I32,
72+
paramsType: params.map(() => DataType.String),
73+
paramsValue: [...params],
74+
runInNewThread: true,
75+
});
76+
6677
const binding: AppleIntentsNativeBinding = {
6778
isSupported: () => callVoid("t3_apple_intents_is_supported") === 1,
6879
bootstrap: () => {
6980
callVoid("t3_apple_intents_bootstrap");
7081
},
71-
syncIndex: (json) => callString("t3_apple_intents_sync_index", json),
82+
syncIndex: (json) => callInWorker("t3_apple_intents_sync_index", [json]),
7283
removeEnvironment: (environmentId) =>
73-
callString("t3_apple_intents_remove_environment", environmentId),
74-
clearIndex: () => callVoid("t3_apple_intents_clear_index"),
84+
callInWorker("t3_apple_intents_remove_environment", [environmentId]),
85+
clearIndex: () => callInWorker("t3_apple_intents_clear_index", []),
7586
setOnscreenContext: (json) => callString("t3_apple_intents_set_onscreen_context", json),
7687
syncAuthCredentials: (json) => callString("t3_apple_intents_sync_auth_credentials", json),
7788
resolveSpotlightUrl: (identifier) => {
@@ -129,14 +140,34 @@ export class DesktopAppleIntents extends Context.Service<
129140
}
130141
>()("@t3tools/desktop/intents/DesktopAppleIntents") {}
131142

143+
const NATIVE_TIMEOUT_STATUS = -2;
144+
const NATIVE_MUTATION_TIMEOUT = "30 seconds";
145+
146+
const checkNativeStatus = (operation: string, status: number): DesktopAppleIntentsMutation =>
147+
status === 0
148+
? Effect.void
149+
: Effect.fail(new DesktopAppleIntentsNativeError({ operation, status }));
150+
132151
// A failure keeps the renderer's sync baseline where it was, so the next flush retries.
133152
const runNative = (operation: string, call: () => number): DesktopAppleIntentsMutation =>
134-
Effect.sync(call).pipe(
135-
Effect.flatMap((status) =>
136-
status === 0
137-
? Effect.void
138-
: Effect.fail(new DesktopAppleIntentsNativeError({ operation, status })),
139-
),
153+
Effect.sync(call).pipe(Effect.flatMap((status) => checkNativeStatus(operation, status)));
154+
155+
const runNativeInWorker = (
156+
operation: string,
157+
call: () => Promise<number>,
158+
): DesktopAppleIntentsMutation =>
159+
Effect.tryPromise({
160+
try: call,
161+
catch: () => new DesktopAppleIntentsNativeError({ operation, status: -1 }),
162+
}).pipe(
163+
Effect.timeoutOrElse({
164+
duration: NATIVE_MUTATION_TIMEOUT,
165+
orElse: () =>
166+
Effect.fail(
167+
new DesktopAppleIntentsNativeError({ operation, status: NATIVE_TIMEOUT_STATUS }),
168+
),
169+
}),
170+
Effect.flatMap((status) => checkNativeStatus(operation, status)),
140171
);
141172

142173
const encodeNativeJson = (operation: string, value: unknown) =>
@@ -201,19 +232,21 @@ export const layer = Layer.effect(
201232
const binding = yield* supportedBinding;
202233
if (!binding) return;
203234
const json = yield* encodeNativeJson("syncIndex", delta);
204-
yield* runNative("syncIndex", () => binding.syncIndex(json));
235+
yield* runNativeInWorker("syncIndex", () => binding.syncIndex(json));
205236
}),
206237
removeEnvironment: Effect.fn("DesktopAppleIntents.removeEnvironment")(
207238
function* (environmentId) {
208239
const binding = yield* supportedBinding;
209240
if (!binding) return;
210-
yield* runNative("removeEnvironment", () => binding.removeEnvironment(environmentId));
241+
yield* runNativeInWorker("removeEnvironment", () =>
242+
binding.removeEnvironment(environmentId),
243+
);
211244
},
212245
),
213246
clearIndex: Effect.gen(function* () {
214247
const binding = yield* supportedBinding;
215248
if (!binding) return;
216-
yield* runNative("clearIndex", () => binding.clearIndex());
249+
yield* runNativeInWorker("clearIndex", () => binding.clearIndex());
217250
}),
218251
setOnscreenContext: Effect.fn("DesktopAppleIntents.setOnscreenContext")(function* (context) {
219252
const binding = yield* supportedBinding;
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
+77-55Lines changed: 77 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ let lastCredentialSignature = "";
121121
let lastOnscreenSignature = "";
122122
// The native index persists across launches, so assume it holds data until a clear succeeds.
123123
let nativeMayHoldData = true;
124+
// Syncs and clears share one queue, so a write that started before a clear cannot land after it.
125+
let syncQueue: Promise<void> = Promise.resolve();
126+
127+
function enqueueAppleIntentsWork(work: () => Promise<void>): Promise<void> {
128+
const run = syncQueue.then(work);
129+
syncQueue = run.catch(() => undefined);
130+
return run;
131+
}
124132

125133
async function readDpopPrivateScalarBase64Url(): Promise<string | null> {
126134
try {
@@ -133,13 +141,69 @@ async function readDpopPrivateScalarBase64Url(): Promise<string | null> {
133141
}
134142
}
135143

136-
/** Clears the native index and credentials; the sync cache resets only once the clear succeeds. */
137-
export async function clearAppleIntentsIndex(): Promise<void> {
138-
await clearAppleIntentsNativeIndex();
139-
lastSnapshot = null;
140-
lastCredentialSignature = "";
141-
lastOnscreenSignature = "";
142-
nativeMayHoldData = false;
144+
async function clearNativeIndex(): Promise<void> {
145+
try {
146+
await clearAppleIntentsNativeIndex();
147+
nativeMayHoldData = false;
148+
} finally {
149+
// A failed clear may have removed part of the index, so the next sync starts from scratch.
150+
lastSnapshot = null;
151+
lastCredentialSignature = "";
152+
lastOnscreenSignature = "";
153+
}
154+
}
155+
156+
/** Clears the native index and credentials after any sync already in flight. */
157+
export function clearAppleIntentsIndex(): Promise<void> {
158+
return enqueueAppleIntentsWork(clearNativeIndex);
159+
}
160+
161+
async function syncNativeIndex(source: Atom.Type<typeof appleIntentsSourceAtom>): Promise<void> {
162+
if (!source.enabled) {
163+
if (nativeMayHoldData) await clearNativeIndex();
164+
return;
165+
}
166+
167+
const nextSnapshot = buildAppleIntentsSnapshot(source.inputs, {
168+
indexMessagePreviews: source.indexMessagePreviews,
169+
nowMs: DateTime.toEpochMillis(DateTime.nowUnsafe()),
170+
resolve: m,
171+
});
172+
const diff = diffAppleIntentsSnapshots(lastSnapshot, nextSnapshot);
173+
if (diff.hasChanges) {
174+
nativeMayHoldData = true;
175+
try {
176+
await syncAppleIntentsIndex(diff.delta);
177+
} catch (error) {
178+
// Native storage may have committed part of the delta, so resync everything next time.
179+
lastSnapshot = null;
180+
throw error;
181+
}
182+
lastSnapshot = nextSnapshot;
183+
}
184+
185+
const onscreenSig = JSON.stringify(source.onscreen);
186+
if (onscreenSig !== lastOnscreenSignature) {
187+
setAppleIntentsOnscreenContext(source.onscreen);
188+
lastOnscreenSignature = onscreenSig;
189+
}
190+
191+
const needsDpop = source.rawCredentials.some((item) => item.authScheme === "DPoP");
192+
const dpopScalar = needsDpop ? await readDpopPrivateScalarBase64Url() : null;
193+
const credentials: AppleIntentAuthCredential[] = source.rawCredentials.map((item) => ({
194+
environmentId: item.environmentId,
195+
httpBaseUrl: item.httpBaseUrl,
196+
accessToken: item.accessToken,
197+
authScheme: item.authScheme,
198+
relayManaged: item.relayManaged,
199+
...(item.authScheme === "DPoP" && dpopScalar ? { dpopKeyRef: dpopScalar } : {}),
200+
}));
201+
const credSig = JSON.stringify(credentials);
202+
if (credSig !== lastCredentialSignature) {
203+
nativeMayHoldData = true;
204+
await syncAppleIntentsAuthCredentials(credentials);
205+
lastCredentialSignature = credSig;
206+
}
143207
}
144208

145209
export function recordAppleIntentsNavigationState(state: NavigationState | undefined): void {
@@ -154,56 +218,14 @@ export function installAppleIntents(): () => void {
154218

155219
let disposed = false;
156220
let debounceTimer: ReturnType<typeof setTimeout> | null = null;
157-
let syncQueue: Promise<void> = Promise.resolve();
158221

159222
const flush = (source: Atom.Type<typeof appleIntentsSourceAtom>) => {
160-
syncQueue = syncQueue
161-
.then(async () => {
162-
if (disposed || !source.preferencesReady) return;
163-
164-
if (!source.enabled) {
165-
if (nativeMayHoldData) await clearAppleIntentsIndex();
166-
return;
167-
}
168-
169-
const nextSnapshot = buildAppleIntentsSnapshot(source.inputs, {
170-
indexMessagePreviews: source.indexMessagePreviews,
171-
nowMs: DateTime.toEpochMillis(DateTime.nowUnsafe()),
172-
resolve: m,
173-
});
174-
const diff = diffAppleIntentsSnapshots(lastSnapshot, nextSnapshot);
175-
if (diff.hasChanges) {
176-
nativeMayHoldData = true;
177-
await syncAppleIntentsIndex(diff.delta);
178-
lastSnapshot = nextSnapshot;
179-
}
180-
181-
const onscreenSig = JSON.stringify(source.onscreen);
182-
if (onscreenSig !== lastOnscreenSignature) {
183-
setAppleIntentsOnscreenContext(source.onscreen);
184-
lastOnscreenSignature = onscreenSig;
185-
}
186-
187-
const needsDpop = source.rawCredentials.some((item) => item.authScheme === "DPoP");
188-
const dpopScalar = needsDpop ? await readDpopPrivateScalarBase64Url() : null;
189-
const credentials: AppleIntentAuthCredential[] = source.rawCredentials.map((item) => ({
190-
environmentId: item.environmentId,
191-
httpBaseUrl: item.httpBaseUrl,
192-
accessToken: item.accessToken,
193-
authScheme: item.authScheme,
194-
relayManaged: item.relayManaged,
195-
...(item.authScheme === "DPoP" && dpopScalar ? { dpopKeyRef: dpopScalar } : {}),
196-
}));
197-
const credSig = JSON.stringify(credentials);
198-
if (credSig !== lastCredentialSignature) {
199-
nativeMayHoldData = true;
200-
await syncAppleIntentsAuthCredentials(credentials);
201-
lastCredentialSignature = credSig;
202-
}
203-
})
204-
.catch((error: unknown) => {
205-
console.warn("[apple-intents] sync failed", error);
206-
});
223+
void enqueueAppleIntentsWork(async () => {
224+
if (disposed || !source.preferencesReady) return;
225+
await syncNativeIndex(source);
226+
}).catch((error: unknown) => {
227+
console.warn("[apple-intents] sync failed", error);
228+
});
207229
};
208230

209231
const unsubscribe = appAtomRegistry.subscribe(

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.