Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

Fix Google Calendar partial sync failures (#11676) (#11722)

[._.]/f74e0d0a9d4f
diff --git a/server/services/calendarGoogleApiSync.js b/server/services/calendarGoogleApiSync.js
index cd1fc5d526..bdb651f798 100644
--- a/server/services/calendarGoogleApiSync.js
+++ b/server/services/calendarGoogleApiSync.js
@@ -1,7 +1,7 @@
import { calendar } from '@googleapis/calendar';
import { ServerError } from '../lib/errorHandler.js';
import { getAuthenticatedClient, GOOGLE_API_TIMEOUT_MS } from './googleAuth.js';
-import { getAccount, updateSubcalendars, mergeDiscoveredSubcalendars } from './calendarAccounts.js';
+import { getAccount, updateSubcalendars, mergeDiscoveredSubcalendars, updateSyncStatus } from './calendarAccounts.js';
import { pushSyncEvents, getSyncDateRange } from './calendarGoogleSync.js';
const apiSyncLocks = new Set();
@@ -21,86 +21,132 @@ async function runApiSyncAccount(accountId, io) {
if (!account) throw new ServerError('Account not found', { status: 404 });
if (account.type !== 'google-calendar') throw new ServerError('Not a Google Calendar account', { status: 400 });
- const auth = await getAuthenticatedClient();
- if (!auth) throw new ServerError('Google OAuth not configured. Set up credentials in Config tab.', { status: 401 });
-
const enabledCalendars = (account.subcalendars || []).filter(sc => sc.enabled && !sc.dormant);
if (enabledCalendars.length === 0) throw new ServerError('No enabled subcalendars', { status: 400 });
io?.emit('calendar:sync:started', { accountId, method: 'api' });
console.log(`📅 Starting Google API sync for ${account.name} (${enabledCalendars.length} calendars)`);
- const cal = calendar({ version: 'v3', auth, timeout: GOOGLE_API_TIMEOUT_MS });
- const { pastDate, futureDate } = getSyncDateRange();
-
- let totalNew = 0;
- let totalUpdated = 0;
- let totalPruned = 0;
- const results = [];
-
- const fetchResults = await Promise.all(enabledCalendars.map(async (sc) => {
- io?.emit('calendar:sync:progress', { accountId, message: `Fetching ${sc.name}...` });
- const allEvents = [];
- let pageToken;
- do {
- const response = await cal.events.list({
- calendarId: sc.calendarId,
- timeMin: pastDate.toISOString(),
- timeMax: futureDate.toISOString(),
- maxResults: 250,
- singleEvents: true,
- orderBy: 'startTime',
- pageToken
- });
- const items = response.data.items || [];
- allEvents.push(...items.map(item => ({
- id: item.id,
- summary: item.summary || '',
- start: item.start,
- end: item.end,
- location: item.location || '',
- description: item.description || '',
- status: item.status || 'confirmed',
- // Attendee/organizer identities feed Tribe touchpoint auto-logging (#2033).
- organizer: item.organizer || null,
- attendees: item.attendees || [],
- // Conference metadata feeds the cached `meetingUrl` join link (#6289).
- // Both keys are emitted UNCONDITIONALLY — an explicit null is what makes
- // a direct API response authoritative enough to clear a stale link,
- // where an omitted key would (correctly) preserve it. Only the entry
- // type + URI are carried forward; Google's conference passwords and
- // dial-in codes are dropped here rather than travelling any further.
- hangoutLink: item.hangoutLink || null,
- conferenceData: item.conferenceData
- ? {
- // Passed through unflattened — `selectMeetingUrl` owns every
- // "is this usable?" decision, so this layer only decides what is
- // allowed to travel at all.
- entryPoints: (item.conferenceData.entryPoints || []).map(ep => ({
- entryPointType: ep.entryPointType ?? null,
- uri: ep.uri ?? null
- }))
- }
- : null
- })));
- pageToken = response.data.nextPageToken;
- } while (pageToken);
- return { sc, allEvents };
- }));
-
- for (const { sc, allEvents } of fetchResults) {
- const syncResult = await pushSyncEvents(accountId, sc.calendarId, sc.name, allEvents, null, { dateRange: { pastDate, futureDate } });
- totalNew += syncResult.newEvents;
- totalUpdated += syncResult.updated;
- totalPruned += syncResult.pruned;
- results.push({ calendarId: sc.calendarId, calendarName: sc.name, events: allEvents.length, ...syncResult });
- console.log(`📅 Google API: ${sc.name} → ${allEvents.length} events`);
+ try {
+ const auth = await getAuthenticatedClient();
+ if (!auth) throw new ServerError('Google OAuth not configured. Set up credentials in Config tab.', { status: 401 });
+
+ const cal = calendar({ version: 'v3', auth, timeout: GOOGLE_API_TIMEOUT_MS });
+ const { pastDate, futureDate } = getSyncDateRange();
+
+ let totalNew = 0;
+ let totalUpdated = 0;
+ let totalPruned = 0;
+ const results = [];
+
+ const fetchResults = await Promise.allSettled(enabledCalendars.map(async (sc) => {
+ io?.emit('calendar:sync:progress', { accountId, message: `Fetching ${sc.name}...` });
+ const allEvents = [];
+ let pageToken;
+ do {
+ const response = await cal.events.list({
+ calendarId: sc.calendarId,
+ timeMin: pastDate.toISOString(),
+ timeMax: futureDate.toISOString(),
+ maxResults: 250,
+ singleEvents: true,
+ orderBy: 'startTime',
+ pageToken
+ });
+ const items = response.data.items || [];
+ allEvents.push(...items.map(item => ({
+ id: item.id,
+ summary: item.summary || '',
+ start: item.start,
+ end: item.end,
+ location: item.location || '',
+ description: item.description || '',
+ status: item.status || 'confirmed',
+ // Attendee/organizer identities feed Tribe touchpoint auto-logging (#2033).
+ organizer: item.organizer || null,
+ attendees: item.attendees || [],
+ // Conference metadata feeds the cached `meetingUrl` join link (#6289).
+ // Both keys are emitted UNCONDITIONALLY — an explicit null is what makes
+ // a direct API response authoritative enough to clear a stale link,
+ // where an omitted key would (correctly) preserve it. Only the entry
+ // type + URI are carried forward; Google's conference passwords and
+ // dial-in codes are dropped here rather than travelling any further.
+ hangoutLink: item.hangoutLink || null,
+ conferenceData: item.conferenceData
+ ? {
+ // Passed through unflattened — `selectMeetingUrl` owns every
+ // "is this usable?" decision, so this layer only decides what is
+ // allowed to travel at all.
+ entryPoints: (item.conferenceData.entryPoints || []).map(ep => ({
+ entryPointType: ep.entryPointType ?? null,
+ uri: ep.uri ?? null
+ }))
+ }
+ : null
+ })));
+ pageToken = response.data.nextPageToken;
+ } while (pageToken);
+ return { sc, allEvents };
+ }));
+
+ const failures = [];
+ for (let i = 0; i < fetchResults.length; i++) {
+ const fetchResult = fetchResults[i];
+ const sc = enabledCalendars[i];
+ if (fetchResult.status === 'rejected') {
+ const reason = fetchResult.reason;
+ const error = reason instanceof Error ? reason.message : String(reason);
+ const failure = {
+ calendarId: sc.calendarId,
+ calendarName: sc.name,
+ error,
+ ...(reason?.code ? { code: reason.code } : {}),
+ ...(reason?.name ? { name: reason.name } : {}),
+ };
+ failures.push(failure);
+ results.push({ ...failure, status: 'error' });
+ console.warn(`⚠️ Google API: ${sc.name} fetch failed: ${error}`);
+ continue;
+ }
+ const { allEvents } = fetchResult.value;
+ const syncResult = await pushSyncEvents(accountId, sc.calendarId, sc.name, allEvents, null, { dateRange: { pastDate, futureDate } });
+ totalNew += syncResult.newEvents;
+ totalUpdated += syncResult.updated;
+ totalPruned += syncResult.pruned;
+ results.push({ calendarId: sc.calendarId, calendarName: sc.name, events: allEvents.length, ...syncResult, status: 'success' });
+ console.log(`📅 Google API: ${sc.name} → ${allEvents.length} events`);
+ }
+
+ if (failures.length === enabledCalendars.length) {
+ const reason = summarizeApiFailures(failures);
+ const error = new ServerError(`Google API sync failed for all calendars: ${reason}`, { status: 502 });
+ error.calendars = failures;
+ error.cause = fetchResults.find(result => result.status === 'rejected')?.reason;
+ throw error;
+ }
+
+ const status = failures.length ? 'partial' : 'success';
+ const reason = failures.length ? summarizeApiFailures(failures) : undefined;
+ await updateSyncStatus(accountId, status);
+ io?.emit('calendar:sync:completed', {
+ accountId, newEvents: totalNew, updated: totalUpdated, pruned: totalPruned, status, method: 'api',
+ ...(failures.length ? { reason, failedCalendars: failures } : {}),
+ });
+ console.log(`📅 Google API sync ${status} for ${account.name}: ${totalNew} new, ${totalUpdated} updated, ${totalPruned} pruned`);
+
+ return { newEvents: totalNew, updated: totalUpdated, pruned: totalPruned, calendars: results, status, ...(reason ? { reason } : {}) };
+ } catch (error) {
+ const failure = error instanceof Error ? error : new Error(String(error));
+ console.error(`❌ Google API sync failed for ${account.name}: ${failure.message}`);
+ io?.emit('calendar:sync:failed', { accountId, error: failure.message, method: 'api', ...(failure.calendars ? { calendars: failure.calendars } : {}) });
+ await Promise.resolve().then(() => updateSyncStatus(accountId, 'error')).catch(() => {});
+ throw error instanceof Error ? error : failure;
}
+}
- io?.emit('calendar:sync:completed', { accountId, newEvents: totalNew, updated: totalUpdated, pruned: totalPruned, status: 'success', method: 'api' });
- console.log(`📅 Google API sync complete for ${account.name}: ${totalNew} new, ${totalUpdated} updated, ${totalPruned} pruned`);
-
- return { newEvents: totalNew, updated: totalUpdated, pruned: totalPruned, calendars: results, status: 'success' };
+function summarizeApiFailures(failures) {
+ const shown = failures.slice(0, 3).map(({ calendarName, error }) => `"${calendarName}": ${error}`).join('; ');
+ return failures.length > 3 ? `${shown}; and ${failures.length - 3} more` : shown;
}
export async function apiDiscoverCalendars(accountId) {
diff --git a/server/services/calendarGoogleApiSync.test.js b/server/services/calendarGoogleApiSync.test.js
index ca2edbafa7..76fe00b71c 100644
--- a/server/services/calendarGoogleApiSync.test.js
+++ b/server/services/calendarGoogleApiSync.test.js
@@ -13,6 +13,7 @@ vi.mock('./googleAuth.js', () => ({ getAuthenticatedClient: vi.fn(), GOOGLE_API_
vi.mock('./calendarAccounts.js', () => ({
getAccount: vi.fn(),
updateSubcalendars: vi.fn(async () => ({})),
+ updateSyncStatus: vi.fn(async () => ({})),
mergeDiscoveredSubcalendars: vi.fn((existing, discovered) => discovered),
}));
@@ -25,6 +26,7 @@ import { apiSyncAccount } from './calendarGoogleApiSync.js';
import { calendar } from '@googleapis/calendar';
import { getAuthenticatedClient } from './googleAuth.js';
import { getAccount } from './calendarAccounts.js';
+import { updateSyncStatus } from './calendarAccounts.js';
import { pushSyncEvents } from './calendarGoogleSync.js';
const ACCOUNT_ID = '33333333-3333-3333-3333-333333333333';
@@ -37,6 +39,14 @@ const account = () => ({
subcalendars: [{ calendarId: CAL_ID, name: 'Work', enabled: true, dormant: false }],
});
+const accountWithCalendars = () => ({
+ ...account(),
+ subcalendars: [
+ { calendarId: '[email protected]', name: 'Work', enabled: true, dormant: false },
+ { calendarId: '[email protected]', name: 'Personal', enabled: true, dormant: false },
+ ],
+});
+
const apiItem = (overrides = {}) => ({
id: 'upstream-1',
summary: 'Design review',
@@ -131,6 +141,97 @@ describe('apiSyncAccount conference-metadata projection (#6289)', () => {
expect(result.status).toBe('success');
});
+ it('syncs healthy calendars when another fetch fails and preserves failed-calendar cache data', async () => {
+ getAccount.mockResolvedValue(accountWithCalendars());
+ const list = vi.fn(({ calendarId }) => calendarId === '[email protected]'
+ ? Promise.resolve({ data: { items: [apiItem({ start: { dateTime: '2026-03-02T10:00:00-05:00' } })] } })
+ : Promise.reject(new Error('calendar permission revoked')));
+ calendar.mockReturnValue({ events: { list } });
+ const io = { emit: vi.fn() };
+
+ const result = await apiSyncAccount(ACCOUNT_ID, io);
+
+ expect(result.status).toBe('partial');
+ expect(result.calendars).toEqual(expect.arrayContaining([
+ expect.objectContaining({ calendarId: '[email protected]', status: 'success' }),
+ expect.objectContaining({ calendarId: '[email protected]', status: 'error', error: 'calendar permission revoked' }),
+ ]));
+ expect(pushSyncEvents).toHaveBeenCalledTimes(1);
+ expect(pushSyncEvents).toHaveBeenCalledWith(
+ ACCOUNT_ID,
+ 'Work',
+ [expect.objectContaining({ start: { dateTime: '2026-03-02T10:00:00-05:00' } })],
+ null,
+ expect.objectContaining({ dateRange: expect.any(Object) }),
+ );
+ expect(updateSyncStatus).toHaveBeenLastCalledWith(ACCOUNT_ID, 'partial');
+ expect(io.emit).toHaveBeenCalledWith('calendar:sync:completed', expect.objectContaining({
+ status: 'partial',
+ failedCalendars: [expect.objectContaining({ calendarId: '[email protected]' })],
+ }));
+ });
+
+ it('records an error and emits failure when every calendar fetch fails, then allows retry', async () => {
+ getAccount.mockResolvedValue(accountWithCalendars());
+ const list = vi.fn().mockRejectedValue(new Error('temporary Google outage'));
+ calendar.mockReturnValue({ events: { list } });
+ const io = { emit: vi.fn() };
+
+ await expect(apiSyncAccount(ACCOUNT_ID, io)).rejects.toThrow('Google API sync failed for all calendars');
+ expect(pushSyncEvents).not.toHaveBeenCalled();
+ expect(updateSyncStatus).toHaveBeenLastCalledWith(ACCOUNT_ID, 'error');
+ expect(io.emit).toHaveBeenCalledWith('calendar:sync:failed', expect.objectContaining({
+ method: 'api', calendars: expect.arrayContaining([expect.objectContaining({ error: 'temporary Google outage' })]),
+ }));
+
+ calendar.mockReturnValue({ events: { list: vi.fn().mockResolvedValue({ data: { items: [] } }) } });
+ await expect(apiSyncAccount(ACCOUNT_ID, null)).resolves.toMatchObject({ status: 'success' });
+ expect(pushSyncEvents).toHaveBeenCalledTimes(2);
+ expect(updateSyncStatus).toHaveBeenLastCalledWith(ACCOUNT_ID, 'success');
+ });
+
+ it('records rejected OAuth lookup and emits failure, then releases the account lock for retry', async () => {
+ const authError = Object.assign(new Error('OAuth token refresh failed'), { code: 'EAUTH' });
+ getAuthenticatedClient.mockRejectedValueOnce(authError);
+ const io = { emit: vi.fn() };
+
+ await expect(apiSyncAccount(ACCOUNT_ID, io)).rejects.toBe(authError);
+ expect(updateSyncStatus).toHaveBeenLastCalledWith(ACCOUNT_ID, 'error');
+ expect(io.emit).toHaveBeenCalledWith('calendar:sync:failed', expect.objectContaining({
+ accountId: ACCOUNT_ID, error: 'OAuth token refresh failed', method: 'api',
+ }));
+ expect(pushSyncEvents).not.toHaveBeenCalled();
+
+ mockListPage([]);
+ await expect(apiSyncAccount(ACCOUNT_ID, null)).resolves.toMatchObject({ status: 'success' });
+ expect(updateSyncStatus).toHaveBeenLastCalledWith(ACCOUNT_ID, 'success');
+ });
+
+ it('discards a failed calendar after a partial first page while syncing a healthy calendar', async () => {
+ getAccount.mockResolvedValue(accountWithCalendars());
+ const list = vi.fn(({ calendarId, pageToken }) => {
+ if (calendarId === '[email protected]') return Promise.resolve({ data: { items: [apiItem({ id: 'healthy-event' })] } });
+ if (pageToken) return Promise.reject(Object.assign(new Error('second page unavailable'), { code: 'ETIMEDOUT' }));
+ return Promise.resolve({ data: { items: [apiItem({ id: 'partial-event' })], nextPageToken: 'page-2' } });
+ });
+ calendar.mockReturnValue({ events: { list } });
+
+ const result = await apiSyncAccount(ACCOUNT_ID, null);
+
+ expect(result.status).toBe('partial');
+ expect(result.calendars).toEqual(expect.arrayContaining([
+ expect.objectContaining({ calendarId: '[email protected]', status: 'error', code: 'ETIMEDOUT' }),
+ ]));
+ expect(list).toHaveBeenCalledWith(expect.objectContaining({ calendarId: '[email protected]', pageToken: undefined }));
+ expect(list).toHaveBeenCalledWith(expect.objectContaining({ calendarId: '[email protected]', pageToken: 'page-2' }));
+ expect(pushSyncEvents).toHaveBeenCalledTimes(1);
+ expect(pushSyncEvents).toHaveBeenCalledWith(
+ ACCOUNT_ID, '[email protected]', 'Work', [expect.objectContaining({ id: 'healthy-event' })], null, expect.any(Object),
+ );
+ expect(pushSyncEvents.mock.calls.some(([, calendarId]) => calendarId === '[email protected]')).toBe(false);
+ });
+
it('refuses to sync without configured OAuth rather than writing an empty cache', async () => {
getAuthenticatedClient.mockResolvedValue(null);
@@ -182,7 +283,7 @@ describe('apiSyncAccount conference-metadata projection (#6289)', () => {
// Keep the SDK's normal retry policy: each attempted request owns a
// fresh deadline, rather than treating 30s as a whole-sync timeout.
await vi.runAllTimersAsync();
- expect(await result).toMatchObject({ error: { code: 'TimeoutError' } });
+ expect(await result).toMatchObject({ error: { cause: { code: 'TimeoutError' } } });
expect(attempts.every(attempt => attempt.abortedAt - attempt.startedAt === 30_000)).toBe(true);
expect(pushSyncEvents).not.toHaveBeenCalled();
healthy = true;