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(cache): preserve navigation cause for uncertain enqueues (#7769)

Sean Aye0a66bd76b530
diff --git a/apps/web/src/lib/graphql-cache/host/worker-host.test.ts b/apps/web/src/lib/graphql-cache/host/worker-host.test.ts
index ee262bff4d..f780fbbc9c 100644
--- a/apps/web/src/lib/graphql-cache/host/worker-host.test.ts
+++ b/apps/web/src/lib/graphql-cache/host/worker-host.test.ts
@@ -1334,9 +1334,11 @@ describe('createWorkerCacheHost', () => {
},
{ owner: 'runner', nowMs: 1, leaseExpiresAtMs: 101 }
);
+ const transportError = new Error('coordinator MessagePort messageerror');
const mutationRejected = expect(mutation).rejects.toMatchObject({
message: expect.stringContaining('coordinator MessagePort messageerror'),
errorCode: 'admitted-enqueue-uncertain',
+ cause: transportError,
});
await vi.waitFor(() =>
expect(
@@ -1346,7 +1348,7 @@ describe('createWorkerCacheHost', () => {
).toHaveLength(1)
);
- adapter.terminalError(new Error('coordinator MessagePort messageerror'));
+ adapter.terminalError(transportError);
await mutationRejected;
adapter.terminalError(new Error('duplicate terminal callback'));
adapter.replace(2);
@@ -2027,6 +2029,7 @@ describe('createWorkerCacheHost', () => {
);
const rejected = expect(mutation).rejects.toMatchObject({
errorCode: 'admitted-enqueue-uncertain',
+ cause: expect.any(CacheNavigationError),
});
await vi.waitFor(() =>
expect(old.requests.at(-1)?.kind).toBe('enqueue-optimistic-mutation')
@@ -2061,6 +2064,7 @@ describe('createWorkerCacheHost', () => {
const rejected = expect(mutation).rejects.toMatchObject({
message: expect.stringContaining('disposed for page navigation'),
errorCode: 'admitted-enqueue-uncertain',
+ cause: expect.any(CacheNavigationError),
});
await vi.waitFor(() =>
expect(
diff --git a/apps/web/src/lib/graphql-cache/host/worker-host.ts b/apps/web/src/lib/graphql-cache/host/worker-host.ts
index 3744369d09..9f06602784 100644
--- a/apps/web/src/lib/graphql-cache/host/worker-host.ts
+++ b/apps/web/src/lib/graphql-cache/host/worker-host.ts
@@ -177,9 +177,10 @@ const asError = (error: unknown): Error =>
class CacheResponseError extends Error {
constructor(
message: string,
- readonly errorCode?: CacheResponseErrorCode
+ readonly errorCode?: CacheResponseErrorCode,
+ options?: ErrorOptions
) {
- super(message);
+ super(message, options);
this.name = 'CacheResponseError';
}
}
@@ -573,7 +574,8 @@ export function createWorkerCacheHost(options: WorkerHostOptions): CacheHost {
entry.reject(
new CacheResponseError(
`${error.message}: admitted optimistic enqueue outcome is uncertain`,
- ADMITTED_ENQUEUE_UNCERTAIN_ERROR_CODE
+ ADMITTED_ENQUEUE_UNCERTAIN_ERROR_CODE,
+ { cause: error }
)
);
} else {
diff --git a/apps/web/src/lib/service-clients/service-storage/graphql-soup.test.ts b/apps/web/src/lib/service-clients/service-storage/graphql-soup.test.ts
index 100da33df3..3bd31bb54b 100644
--- a/apps/web/src/lib/service-clients/service-storage/graphql-soup.test.ts
+++ b/apps/web/src/lib/service-clients/service-storage/graphql-soup.test.ts
@@ -778,6 +778,60 @@ describe('GraphQL Soup browser cache session gate', () => {
}
);
+ it.each([
+ ['navigation', 'admitted-enqueue-uncertain', false],
+ ['transport', 'admitted-enqueue-uncertain', true],
+ ['abrupt', 'admitted-enqueue-uncertain', true],
+ ['message-only', 'admitted-enqueue-uncertain', true],
+ ['missing', 'admitted-enqueue-uncertain', true],
+ ['navigation', undefined, true],
+ ] as const)(
+ 'classifies wrapped cache errors by cause %s and code %s (report: %s)',
+ async (causeKind, errorCode, shouldReport) => {
+ const soup = await import('./graphql-soup');
+ soup.getGraphqlSoupClient();
+ const { CacheNavigationError } = await import(
+ '@graphql-cache/host/navigation-error'
+ );
+ const navigation = new CacheNavigationError();
+ const causes = {
+ navigation,
+ transport: new Error('coordinator MessagePort messageerror'),
+ abrupt: new Error('cache worker host was abruptly disposed'),
+ 'message-only': new Error(navigation.message),
+ missing: undefined,
+ };
+ const error = Object.assign(
+ new Error('admitted optimistic enqueue outcome is uncertain', {
+ cause: causes[causeKind],
+ }),
+ { name: 'CacheResponseError', errorCode }
+ );
+ const operation: Operation = {
+ kind: 'mutation',
+ key: 42,
+ query: parse('mutation PrivateMutation { update }'),
+ variables: { privateValue: 'do-not-export' },
+ context: { url: 'http://dss.test', requestPolicy: 'cache-first' },
+ };
+ const report =
+ mocks.normalizedCacheExchange.mock.calls[0]?.[1]?.onCacheError;
+ expect(report).toBeDefined();
+ report?.(error, operation);
+
+ if (shouldReport) {
+ expect(mocks.telemetryError).toHaveBeenCalledExactlyOnceWith(error, {
+ 'error.source': 'graphql-cache',
+ 'cache.backend': 'turso-wasm-opfs',
+ 'cache.phase': 'operation',
+ 'cache.operation_kind': 'mutation',
+ });
+ } else {
+ expect(mocks.telemetryError).not.toHaveBeenCalled();
+ }
+ }
+ );
+
it.each([
{ native: false, backend: 'turso-wasm-opfs' },
{ native: true, backend: 'native' },
diff --git a/apps/web/src/lib/service-clients/service-storage/graphql-soup.ts b/apps/web/src/lib/service-clients/service-storage/graphql-soup.ts
index 02faf076cf..41682ccd95 100644
--- a/apps/web/src/lib/service-clients/service-storage/graphql-soup.ts
+++ b/apps/web/src/lib/service-clients/service-storage/graphql-soup.ts
@@ -23,7 +23,10 @@ import {
entityFromArgument,
} from '@graphql-cache/index';
import { registerCacheHost } from '@graphql-cache/lifecycle';
-import { isOwnerLockUnavailableError } from '@graphql-cache/protocol';
+import {
+ isAdmittedEnqueueUncertainError,
+ isOwnerLockUnavailableError,
+} from '@graphql-cache/protocol';
import { getBrowserTursoCacheRolloutDecision } from '@graphql-cache/rollout';
import { getOrCreateCacheScope } from '@graphql-cache/scope';
import { Telemetry } from '@macro-inc/observability';
@@ -519,11 +522,14 @@ export function getGraphqlSoupClient(): Client {
operationKind?: Operation['kind']
) => {
try {
- // Navigation deliberately rejects outstanding reads, and a tab on
- // another deployed build can own the database. Neither is an error;
- // unexpected disposal still reports.
+ // Navigation cancels outstanding work. Admitted enqueues retain their
+ // uncertainty code to prevent unsafe retries, but preserve navigation
+ // as their cause. Only that expected cancellation is quiet; uncertain
+ // transport failures and unexpected disposal must still report.
if (
error instanceof CacheNavigationError ||
+ (isAdmittedEnqueueUncertainError(error) &&
+ error.cause instanceof CacheNavigationError) ||
isOwnerLockUnavailableError(error)
)
return;