Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
Seanaye/feat/telemetry for failed worker (#6716)
Sean Ayef8be6822f7da
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.tsindex 0a4d3a95a2..96c28e8745 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@@ -1,4 +1,7 @@+import type { NormalizedCacheExchangeOptions } from '@graphql-cache/exchange/normalized-cache-exchange';import type { BrowserTursoCacheRolloutDecision } from '@graphql-cache/rollout-policy';+import type { Operation } from '@urql/core';+import { parse } from 'graphql';import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import type {GraphqlSoupEntityType,@@ -118,22 +121,33 @@ const mocks = vi.hoisted(() => {queueDepth: () => queuedMutationCount,recordSubscriptionDisposal: () => cleanupOrder.push('subscriptions'),cleanupOrder: () => [...cleanupOrder],- failInitialization: () =>- initializationErrorHandler?.(- new Error('injected initialization failure')- ),+ failInitialization: (+ error = new Error('injected initialization failure')+ ) => initializationErrorHandler?.(error),plainClient,realtimeClient,replaceSubscriptions,platformFetch,toastFailure,+ telemetryError: vi.fn(),+ normalizedCacheExchange: vi.fn(+ (host: unknown, _options?: NormalizedCacheExchangeOptions) => ({+ kind: 'cache',+ host,+ })+ ),createWorkerCacheHost: vi.fn((options: { onInitializationError?: (error: Error) => void }) => {initializationErrorHandler = options.onInitializationError;return host;}),- createTauriCacheHost: vi.fn(() => host),+ createTauriCacheHost: vi.fn(+ (options: { onInitializationError?: (error: Error) => void }) => {+ initializationErrorHandler = options.onInitializationError;+ return host;+ }+ ),};});@@ -180,7 +194,10 @@ vi.mock('@graphql-cache/scope', () => ({getOrCreateCacheScope: () => 'anonymous-scope',}));vi.mock('@graphql-cache/exchange/normalized-cache-exchange', () => ({- normalizedCacheExchange: (host: unknown) => ({ kind: 'cache', host }),+ normalizedCacheExchange: mocks.normalizedCacheExchange,+}));+vi.mock('@macro-inc/observability', () => ({+ Telemetry: { error: mocks.telemetryError },}));vi.mock('@service-auth/fetch', () => ({ getMacroApiToken: vi.fn() }));vi.mock('graphql-ws', () => ({@@ -312,6 +329,7 @@ describe('GraphQL Soup browser cache session gate', () => {mocks.tauri = false;mocks.resetQueue();mocks.platformFetch.mockReset();+ mocks.telemetryError.mockReset();});afterEach(() => {@@ -448,13 +466,124 @@ describe('GraphQL Soup browser cache session gate', () => {expect(mocks.host.dispose).not.toHaveBeenCalled();});+ it.each(['query', 'mutation'] as const)(+ 'reports handled cache-disposed failures for %s without exporting operation payloads',+ async (kind) => {+ const soup = await import('./graphql-soup');+ soup.getGraphqlSoupClient();+ const error = new Error('cache worker host was disposed');+ const operation: Operation = {+ kind,+ key: 42,+ query: parse('query PrivateDocument { user { id } }'),+ variables: { privateValue: 'do-not-export' },+ context: { url: 'http://dss.test', requestPolicy: 'cache-first' },+ };+ const report =+ mocks.normalizedCacheExchange.mock.calls[0]?.[1]?.onCacheError;++ report?.(+ new Error('cache worker host was disposed for page navigation'),+ operation+ );+ expect(mocks.telemetryError).not.toHaveBeenCalled();+ report?.(error, operation);++ expect(mocks.telemetryError).toHaveBeenCalledExactlyOnceWith(error, {+ 'error.source': 'graphql-cache',+ 'cache.backend': 'turso-wasm-opfs',+ 'cache.phase': 'operation',+ 'cache.operation_kind': kind,+ });+ }+ );++ it.each([+ { native: false, backend: 'turso-wasm-opfs' },+ { native: true, backend: 'native' },+ ])(+ 'reports $backend initialization failures once despite in-flight operation errors',+ async ({ native, backend }) => {+ vi.spyOn(console, 'warn').mockImplementation(() => {});+ mocks.tauri = native;+ const soup = await import('./graphql-soup');+ soup.getGraphqlSoupClient();+ const report =+ mocks.normalizedCacheExchange.mock.calls[0]?.[1]?.onCacheError;+ expect(report).toBeDefined();+ const operation: Operation = {+ kind: 'query',+ key: 42,+ query: parse('query { user { id } }'),+ variables: {},+ context: { url: 'http://dss.test', requestPolicy: 'cache-first' },+ };+ const error = new Error('injected initialization failure');++ mocks.failInitialization(error);+ // Rejected in-flight reads reach the exchange after the host reports+ // initialization failure. Cleanup can also reject outstanding work.+ await Promise.resolve();+ report?.(error, operation);+ report?.(new Error('cache worker host was disposed'), operation);++ expect(mocks.telemetryError).toHaveBeenCalledExactlyOnceWith(error, {+ 'error.source': 'graphql-cache',+ 'cache.backend': backend,+ 'cache.phase': 'initialization',+ });+ expect(soup.getGraphqlSoupClient()).toBe(mocks.realtimeClient);+ expect(soup.getGraphqlCacheHost()).toBeUndefined();+ expect(soup.graphqlCacheEnabled()).toBe(false);+ }+ );++ it('reports synchronous cache construction failures before falling back', async () => {+ vi.spyOn(console, 'warn').mockImplementation(() => {});+ const error = new Error('cache worker construction failed');+ mocks.createWorkerCacheHost.mockImplementationOnce(() => {+ throw error;+ });+ const soup = await import('./graphql-soup');++ expect(soup.getGraphqlSoupClient()).toBe(mocks.realtimeClient);+ expect(mocks.telemetryError).toHaveBeenCalledExactlyOnceWith(error, {+ 'error.source': 'graphql-cache',+ 'cache.backend': 'turso-wasm-opfs',+ 'cache.phase': 'initialization',+ });+ });++ it('does not let telemetry failures prevent terminal-cache fallback', async () => {+ vi.spyOn(console, 'warn').mockImplementation(() => {});+ mocks.telemetryError.mockImplementationOnce(() => {+ throw new Error('telemetry unavailable');+ });+ const soup = await import('./graphql-soup');+ soup.getGraphqlSoupClient();++ expect(() => mocks.failInitialization()).not.toThrow();+ expect(mocks.telemetryError).toHaveBeenCalledOnce();+ expect(soup.getGraphqlSoupClient()).toBe(mocks.realtimeClient);+ expect(mocks.cleanupOrder()).toEqual(['subscriptions', 'host']);+ });+it('unsubscribes cache operations before disposing a failed host', async () => {const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});const soup = await import('./graphql-soup');const cachedClient = soup.getGraphqlSoupClient();- mocks.failInitialization();+ const error = new Error('injected initialization failure');+ mocks.failInitialization(error);+ expect(mocks.telemetryError).toHaveBeenCalledExactlyOnceWith(error, {+ 'error.source': 'graphql-cache',+ 'cache.backend': 'turso-wasm-opfs',+ 'cache.phase': 'initialization',+ });+ // Late failure notifications from the old host do not emit duplicate logs.+ mocks.failInitialization(error);+ expect(mocks.telemetryError).toHaveBeenCalledOnce();expect(cachedClient).not.toBe(mocks.realtimeClient);expect(soup.getGraphqlSoupClient()).toBe(mocks.realtimeClient);expect(soup.graphqlCacheEnabled()).toBe(false);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.tsindex 180d5159d6..dcb6dad01d 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@@ -21,6 +21,7 @@ import {import { registerCacheHost } from '@graphql-cache/lifecycle';import { getBrowserTursoCacheRolloutDecision } from '@graphql-cache/rollout';import { getOrCreateCacheScope } from '@graphql-cache/scope';+import { Telemetry } from '@macro-inc/observability';import { notificationStateFromGraphql } from '@notifications/notification-state';import { getMacroApiToken } from '@service-auth/fetch';import type { ApiUserNotification } from '@service-notification/generated/schemas/apiUserNotification';@@ -39,6 +40,7 @@ import {createClient,type DocumentInput,fetchExchange,+ type Operation,type RequestPolicy,subscriptionExchange,} from '@urql/core';@@ -400,6 +402,31 @@ export function getGraphqlSoupClient(): Client {if (cachedClient) return cachedClient;disposeUncachedRealtimeClient();cachedClient = (() => {+ const reportCacheError = (+ error: unknown,+ phase: 'initialization' | 'operation',+ operationKind?: Operation['kind']+ ) => {+ try {+ // Navigation deliberately rejects outstanding reads. Do not turn that+ // expected shutdown into an error; unexpected disposal still reports.+ if (+ error instanceof Error &&+ error.message === 'cache worker host was disposed for page navigation'+ )+ return;+ // Caught cache failures never reach window.unhandledrejection. Report+ // them through the Datadog-bound exporter without query/variable data.+ Telemetry.error(error, {+ 'error.source': 'graphql-cache',+ 'cache.backend': native ? 'native' : 'turso-wasm-opfs',+ 'cache.phase': phase,+ ...(operationKind ? { 'cache.operation_kind': operationKind } : {}),+ });+ } catch {+ // Observability must not prevent network fallback or cache cleanup.+ }+ };let host: CacheHost | undefined;let websocketClient: GraphqlWsClient | undefined;let unregisterHost: () => void = () => undefined;@@ -414,6 +441,7 @@ export function getGraphqlSoupClient(): Client {};const onInitializationError = (error: Error) => {if (!host || cachedCacheHost !== host) return;+ reportCacheError(error, 'initialization');fallbackAfterInitializationFailure();toast.failure('Local cache unavailable', {subtext: 'Macro will continue without local caching for this session.',@@ -442,6 +470,12 @@ export function getGraphqlSoupClient(): Client {preferGetMethod: false,exchanges: [normalizedCacheExchange(host, {+ onCacheError: (error, operation) => {+ // Initialization failure already reports before retiring the host;+ // rejected in-flight operations must not report it again.+ if (!host || cachedCacheHost !== host) return;+ reportCacheError(error, 'operation', operation.kind);+ },entityResolvers: {GraphqlUser: {emailThread: entityFromArgument('GraphqlSoupEmailThread', [@@ -473,6 +507,7 @@ export function getGraphqlSoupClient(): Client {browserCacheClientActivated = !native;return client;} catch (error) {+ reportCacheError(error, 'initialization');cleanup();cachedCacheHost = undefined;cachedCacheCleanup = undefined;