Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(graphql-cache): stop failing native cache commands that still commit (#9537)

teocc5a9f7d0401
diff --git a/apps/web/docs/graphql-normalized-cache-plan.md b/apps/web/docs/graphql-normalized-cache-plan.md
index 09bb86c33e..326b775ca0 100644
--- a/apps/web/docs/graphql-normalized-cache-plan.md
+++ b/apps/web/docs/graphql-normalized-cache-plan.md
@@ -591,7 +591,7 @@ apps/web/src/lib/graphql-cache/ # JS glue
Locks / BroadcastChannel machinery. DB at
`{app_data_dir}/graphql-cache/cache.turso`.
JS side: `createTauriCacheHost` (`host/tauri-host.ts`) — invoke-based
- RPC with the same 10s timeout + Error-normalized rejections, event
+ RPC with the same read-only 10s timeout + Error-normalized rejections, event
subscription filtered by clientId prefix; `isTauri()` selects it in
`graphql-soup.ts`.
@@ -1196,12 +1196,12 @@ unrelated queued hydration just like other authoritative writes.
`graphql.subscription.cache_queue` times preceding effects from the same
subscription operation. `graphql.subscription.cache_apply` starts afterward
-and includes host readiness, IPC, engine admission and persistence. Native IPC
-has a 10-second request deadline, so a 10-second apply duration alone does not
-prove successful engine work. Apply/write/delete spans carry `cache.failure`
-from the bounded cache error classifier; use it with `cache.effects_applied`
-to distinguish timeouts from successful slow applies. Native engine admission
-has a `graphql_cache.native.lock` phase, emitted once by `EngineAccess`
+and includes host readiness, IPC, engine admission and persistence. Native
+writes and deletes have no IPC deadline (see Native IPC transport), so a long
+apply is a slow engine queue, not a timeout. Apply/write/delete spans carry
+`cache.failure` from the bounded cache error classifier; use it with
+`cache.effects_applied` to separate engine failures from slow applies. Native
+engine admission has a `graphql_cache.native.lock` phase, emitted once by `EngineAccess`
for each foreground or hydrate acquisition. Its `graphql_cache.lock.wait_ms`
includes waiting until admission or supersession, not time holding the lock. Native scheduling changes require a
rebuilt native binary; older installed binaries retain their scheduling.
@@ -1217,6 +1217,23 @@ cache changes (including resets), hydration or mutation settlements, the host
flushes earlier invalidations so other observers cannot overtake them. Disposal
drops pending callbacks.
+Tauri cannot cancel a command, and the engine runs commands one at a time, so a
+caller that stops waiting only loses the outcome of work that still happens.
+As in worker-host, only reads and initialization have the ten-second deadline:
+a read falls back to the network, and every request waits behind init. Writes,
+hydration, queue claims and settlements, deletes, invalidations, teardown and
+clear wait for the engine's answer. A timeout would report a failure for a
+commit that still lands, and callers act on it: the mutation queue runner holds
+later edits for its five-minute lease after an unconfirmed commit, and a failed
+logout clear rotates the cache scope, which the running native engine refuses
+with `graphql cache scope mismatch`.
+
+The deadline counts only time the page runs on time. When the page itself
+stalls (an occluded or napping macOS window, a long task), the overdue timer
+would otherwise run before an answer the engine already sent and fail every
+request in flight at once, so a timer that fires more than a second late starts
+a new window.
+
Native live views advertise a specification-bound registration token. The host
retains at most 32 descriptors and resumes an unchanged specification from its
id, token and revision; a missing registration triggers a full request. Changed
@@ -1225,7 +1242,8 @@ installed binaries omit the token and keep receiving full requests. Empty record
selections fetch only the current revision, with no unused fragment.
`graphql_cache.ipc.request` is a `perf`-class span (`sampling: 'perf'`) that
-records command and outcome. The webview does not re-encode read
+records command and outcome, plus `graphql_cache.ipc.page_stall_ms` when a page
+stall extended its deadline. The webview does not re-encode read
arguments to count bytes or create spans for notification callbacks.
`graphql_cache.ipc.notification` in the native emitter measures serialization and
emission, recording the single serialized payload's byte count. Native read, watch,
diff --git a/apps/web/src/lib/graphql-cache/host/tauri-host.test.ts b/apps/web/src/lib/graphql-cache/host/tauri-host.test.ts
index ceb83255b7..54c76c743d 100644
--- a/apps/web/src/lib/graphql-cache/host/tauri-host.test.ts
+++ b/apps/web/src/lib/graphql-cache/host/tauri-host.test.ts
@@ -1174,6 +1174,135 @@ describe('createTauriCacheHost', () => {
}
});
+ it('delivers the outcome of durable commands that outlast the read timeout', async () => {
+ vi.useFakeTimers();
+ try {
+ const answers = new Map<string, (value: unknown) => void>();
+ invokeMock.mockImplementation((command: string) =>
+ command === 'graphql_cache_init_with_schema' ||
+ command === 'graphql_cache_inspect_mutations'
+ ? Promise.resolve(null)
+ : new Promise((resolve) => answers.set(command, resolve))
+ );
+ const host = createTauriCacheHost({
+ scope: 'scope-1',
+ requestTimeoutMs: 50,
+ });
+
+ const write = host.writeQuery({ query: '{ x }', data: { x: 1 } });
+ const hydrate = host.hydrateQuery({ query: '{ x }', data: { x: 1 } });
+ const commit = host.commitOptimisticWrite(
+ '7',
+ { owner: 'runner', generation: '2' },
+ { query: 'mutation { m }', data: { m: 1 } }
+ );
+ const deletion = host.deleteRecords(['A:1']);
+ await vi.dynamicImportSettled();
+ // The engine is still applying earlier work long after a read would
+ // have given up; these commands commit regardless of who is waiting.
+ await vi.advanceTimersByTimeAsync(5_000);
+ answers.get('graphql_cache_write')?.({
+ changed: ['A:1'],
+ affectedOps: [],
+ reset: false,
+ revision: '3',
+ });
+ answers.get('graphql_cache_hydrate')?.({
+ kind: 'void',
+ revision: '4',
+ revisionAdvanced: false,
+ });
+ answers.get('graphql_cache_commit_optimistic_write')?.({
+ kind: 'committed',
+ changed: [],
+ affectedOps: [],
+ reset: false,
+ revision: '5',
+ });
+ answers.get('graphql_cache_delete_records')?.({
+ revision: '6',
+ affectedOps: [],
+ });
+
+ await expect(write).resolves.toEqual({
+ changed: ['A:1'],
+ affectedOps: [],
+ reset: false,
+ revision: '3',
+ });
+ await expect(hydrate).resolves.toEqual({
+ kind: 'void',
+ revision: '4',
+ revisionAdvanced: false,
+ });
+ await expect(commit).resolves.toEqual({
+ kind: 'committed',
+ changed: [],
+ affectedOps: [],
+ reset: false,
+ revision: '5',
+ });
+ await expect(deletion).resolves.toEqual({
+ revision: '6',
+ affectedOps: [],
+ });
+ host.dispose();
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
+ it('does not time out reads whose deadline passed while the page was stalled', async () => {
+ vi.useFakeTimers();
+ try {
+ let now = 0;
+ vi.spyOn(performance, 'now').mockImplementation(() => now);
+ let answerWatch: (value: unknown) => void = () => {};
+ invokeMock.mockImplementation((command: string) => {
+ if (command === 'graphql_cache_watch')
+ return new Promise((resolve) => {
+ answerWatch = resolve;
+ });
+ if (command === 'graphql_cache_read') return new Promise(() => {});
+ return Promise.resolve(null);
+ });
+ const host = createTauriCacheHost({
+ scope: 'scope-1',
+ requestTimeoutMs: 50,
+ });
+
+ const watch = host.watchQuery?.({ opKey: 1, query: '{ x }' });
+ const read = host.readQuery({ query: '{ y }' });
+ const readTimedOut = expect(read).rejects.toThrow(
+ 'graphql cache ipc timeout: graphql_cache_read'
+ );
+ await vi.dynamicImportSettled();
+ // The page stops running for 20 s. The engine answered meanwhile, but
+ // that answer is queued behind the overdue deadline timer.
+ now = 20_000;
+ await vi.advanceTimersByTimeAsync(50);
+ answerWatch({ kind: 'hit', data: { x: 1 }, revision: '2' });
+
+ await expect(watch).resolves.toEqual({
+ kind: 'hit',
+ data: { x: 1 },
+ revision: '2',
+ });
+ const stillWaiting = Symbol('still waiting');
+ await expect(Promise.race([read, stillWaiting])).resolves.toBe(
+ stillWaiting
+ );
+ // An engine that still does not answer once the page runs on time
+ // fails the read as before.
+ now += 50;
+ await vi.advanceTimersByTimeAsync(50);
+ await readTimedOut;
+ host.dispose();
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
it('tolerates a failed listener setup (no unhandled rejection)', async () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
try {
diff --git a/apps/web/src/lib/graphql-cache/host/tauri-host.ts b/apps/web/src/lib/graphql-cache/host/tauri-host.ts
index 930c721aa2..56afbac174 100644
--- a/apps/web/src/lib/graphql-cache/host/tauri-host.ts
+++ b/apps/web/src/lib/graphql-cache/host/tauri-host.ts
@@ -90,9 +90,10 @@ export interface TauriHostOptions {
scope: string;
hotCapacity?: number;
/**
- * Request timeout in ms (default 10s, matching worker-host). Applies to all
- * commands except optimistic enqueue, which remains pending because retrying
- * an uncertain durable operation could duplicate user intent.
+ * Read and initialization timeout in ms (default 10s, matching worker-host).
+ * Other commands keep running natively after a caller stops waiting, so they
+ * wait for the engine's outcome instead of reporting a failure that may
+ * already have committed.
*/
requestTimeoutMs?: number;
/** Reports an asynchronous durable-storage initialization failure. */
@@ -111,6 +112,30 @@ const INSPECT_MUTATIONS_COMMAND = 'graphql_cache_inspect_mutations';
const INIT_COMMAND = 'graphql_cache_init_with_schema';
+// The only commands a caller stops waiting for: a read falls back to the
+// network, and a hung init would block every request. Tauri cannot cancel a
+// command, so any other command still commits after its caller gives up.
+const TIMED_COMMANDS: ReadonlySet<string> = new Set([
+ INIT_COMMAND,
+ INSPECT_MUTATIONS_COMMAND,
+ 'graphql_cache_current_revision',
+ 'graphql_cache_current_storage_generation',
+ 'graphql_cache_read',
+ WATCH_COMMAND,
+ 'graphql_cache_read_records_by_keys',
+ 'graphql_cache_search',
+ ENTITY_FILTER_COMMAND,
+ ENTITY_FILTER_RESUME_COMMAND,
+ CALENDAR_RANGE_COMMAND,
+ 'graphql_cache_inspect_query_variants',
+ 'graphql_cache_inspect_query',
+]);
+
+// A deadline timer running this late measured a stall of the page itself (an
+// occluded or napping window, a long task): the engine's answer may already be
+// queued behind it. Time out only after a window the page ran through on time.
+const PAGE_STALL_MS = 1_000;
+
const DRAFTS_NEED_UPDATE =
'Update Macro to sync drafts saved on this device. Your queued drafts are preserved until the app is updated.';
const CACHE_NEEDS_UPDATE =
@@ -187,8 +212,7 @@ export function createTauriCacheHost(options: TauriHostOptions): CacheHost {
async function request<T>(
command: string,
- args: Record<string, unknown>,
- timeout = true
+ args: Record<string, unknown>
): Promise<T> {
if (command !== 'graphql_cache_read') pendingReads.clear();
const span = Telemetry.span('graphql_cache.ipc.request', {
@@ -199,14 +223,25 @@ export function createTauriCacheHost(options: TauriHostOptions): CacheHost {
const wireArgs = traceparent ? { ...args, traceparent } : args;
let timer: ReturnType<typeof setTimeout> | undefined;
let timedOut = false;
+ let pageStallMs = 0;
try {
const operation = span.run(() => invoke<T>(command, wireArgs));
- const deadline = timeout
+ const deadline = TIMED_COMMANDS.has(command)
? new Promise<never>((_, reject) => {
- timer = setTimeout(() => {
- timedOut = true;
- reject(new Error(`graphql cache ipc timeout: ${command}`));
- }, requestTimeoutMs);
+ const arm = () => {
+ const due = performance.now() + requestTimeoutMs;
+ timer = setTimeout(() => {
+ const late = performance.now() - due;
+ if (late > PAGE_STALL_MS) {
+ pageStallMs += late;
+ arm();
+ return;
+ }
+ timedOut = true;
+ reject(new Error(`graphql cache ipc timeout: ${command}`));
+ }, requestTimeoutMs);
+ };
+ arm();
})
: undefined;
const value = await (deadline
@@ -225,6 +260,11 @@ export function createTauriCacheHost(options: TauriHostOptions): CacheHost {
throw error instanceof Error ? error : new Error(String(error));
} finally {
clearTimeout(timer);
+ if (pageStallMs > 0)
+ span.setAttr(
+ 'graphql_cache.ipc.page_stall_ms',
+ Math.round(pageStallMs)
+ );
span.end();
}
}
@@ -637,8 +677,7 @@ export function createTauriCacheHost(options: TauriHostOptions): CacheHost {
owner: claim.owner,
nowMs: claim.nowMs,
leaseExpiresAtMs: claim.leaseExpiresAtMs,
- },
- false
+ }
);
},