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: bound Gmail and Calendar API request attempts (#11716)

[._.]/290d31e5e3c4
diff --git a/server/routes/messageGmailReply.test.js b/server/routes/messageGmailReply.test.js
index bc302ffb03..d16806468e 100644
--- a/server/routes/messageGmailReply.test.js
+++ b/server/routes/messageGmailReply.test.js
@@ -25,7 +25,10 @@ vi.mock('../services/messageAccounts.js', () => ({
vi.mock('../services/messageSync.js', () => ({
getMessage: async (accountId, id) => doubles.original?.id === id && doubles.original.accountId === accountId ? doubles.original : null
}));
-vi.mock('../services/googleAuth.js', () => ({ getAuthenticatedClient: async () => doubles.auth }));
+vi.mock('../services/googleAuth.js', () => ({
+ GOOGLE_API_TIMEOUT_MS: 30_000,
+ getAuthenticatedClient: async () => doubles.auth
+}));
vi.mock('@googleapis/gmail', () => ({ gmail: () => ({ users: { messages: { get: doubles.metadata, send: doubles.submit } } }) }));
vi.mock('../services/messagePlaywrightSync.js', () => ({ getSelectors: vi.fn(), updateSelectors: vi.fn(), testSelectors: vi.fn(), launchProvider: vi.fn() }));
vi.mock('../services/messageEvaluator.js', () => ({ evaluateMessages: vi.fn(), generateReplyBody: vi.fn() }));
diff --git a/server/services/calendarCacheConcurrency.test.js b/server/services/calendarCacheConcurrency.test.js
index 1145b90348..35824d644c 100644
--- a/server/services/calendarCacheConcurrency.test.js
+++ b/server/services/calendarCacheConcurrency.test.js
@@ -20,7 +20,7 @@ vi.mock('./settings.js', () => ({ getSettings: vi.fn() }));
vi.mock('../lib/cliProviderRun.js', () => ({
pickCliProvider: vi.fn(), runCliProviderPrompt: vi.fn(),
}));
-vi.mock('./googleAuth.js', () => ({ getAuthenticatedClient: vi.fn() }));
+vi.mock('./googleAuth.js', () => ({ getAuthenticatedClient: vi.fn(), GOOGLE_API_TIMEOUT_MS: 30_000 }));
vi.mock('@googleapis/calendar', () => ({ calendar: vi.fn() }));
import { atomicWrite, readJSONFile } from '../lib/fileUtils.js';
diff --git a/server/services/calendarGoogleApiSync.js b/server/services/calendarGoogleApiSync.js
index e1d4b7600b..cd1fc5d526 100644
--- a/server/services/calendarGoogleApiSync.js
+++ b/server/services/calendarGoogleApiSync.js
@@ -1,6 +1,6 @@
import { calendar } from '@googleapis/calendar';
import { ServerError } from '../lib/errorHandler.js';
-import { getAuthenticatedClient } from './googleAuth.js';
+import { getAuthenticatedClient, GOOGLE_API_TIMEOUT_MS } from './googleAuth.js';
import { getAccount, updateSubcalendars, mergeDiscoveredSubcalendars } from './calendarAccounts.js';
import { pushSyncEvents, getSyncDateRange } from './calendarGoogleSync.js';
@@ -30,7 +30,7 @@ async function runApiSyncAccount(accountId, io) {
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 });
+ const cal = calendar({ version: 'v3', auth, timeout: GOOGLE_API_TIMEOUT_MS });
const { pastDate, futureDate } = getSyncDateRange();
let totalNew = 0;
@@ -110,7 +110,7 @@ export async function apiDiscoverCalendars(accountId) {
const auth = await getAuthenticatedClient();
if (!auth) throw new ServerError('Google OAuth not configured', { status: 401 });
- const cal = calendar({ version: 'v3', auth });
+ const cal = calendar({ version: 'v3', auth, timeout: GOOGLE_API_TIMEOUT_MS });
const allCalendars = [];
let pageToken;
diff --git a/server/services/calendarGoogleApiSync.test.js b/server/services/calendarGoogleApiSync.test.js
index f83c3fbf1f..ca2edbafa7 100644
--- a/server/services/calendarGoogleApiSync.test.js
+++ b/server/services/calendarGoogleApiSync.test.js
@@ -8,7 +8,7 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
// API response authoritative enough to clear a stale join link (#6289).
vi.mock('@googleapis/calendar', () => ({ calendar: vi.fn() }));
-vi.mock('./googleAuth.js', () => ({ getAuthenticatedClient: vi.fn() }));
+vi.mock('./googleAuth.js', () => ({ getAuthenticatedClient: vi.fn(), GOOGLE_API_TIMEOUT_MS: 30_000 }));
vi.mock('./calendarAccounts.js', () => ({
getAccount: vi.fn(),
@@ -137,4 +137,64 @@ describe('apiSyncAccount conference-metadata projection (#6289)', () => {
await expect(apiSyncAccount(ACCOUNT_ID, null)).rejects.toMatchObject({ status: 401 });
expect(pushSyncEvents).not.toHaveBeenCalled();
});
+
+ it('aborts stalled SDK requests at the default deadline and releases the account lock', async () => {
+ const sdk = await vi.importActual('@googleapis/calendar');
+ vi.useFakeTimers();
+ // Node's native AbortSignal timer bypasses fake timers. Inject only its
+ // clock; the real SDK and transport still create and consume the deadline.
+ const timeout = vi.spyOn(AbortSignal, 'timeout').mockImplementation(ms => {
+ const controller = new AbortController();
+ setTimeout(() => controller.abort(new DOMException('Request timed out', 'TimeoutError')), ms);
+ return controller.signal;
+ });
+ const cleanup = new AbortController();
+ const started = Promise.withResolvers();
+ const attempts = [];
+ let healthy = false;
+ const auth = new sdk.auth.OAuth2({ transporterOptions: {
+ signal: cleanup.signal,
+ fetchImplementation: async (_url, { signal }) => {
+ if (healthy) return Response.json({ items: [] });
+ const attempt = { startedAt: Date.now(), signal, abortedAt: null };
+ attempts.push(attempt);
+ started.resolve();
+ return new Promise((_resolve, reject) => {
+ const abort = () => { attempt.abortedAt = Date.now(); reject(signal.reason); };
+ if (signal.aborted) abort();
+ else signal.addEventListener('abort', abort, { once: true });
+ });
+ },
+ } });
+ auth.setCredentials({ access_token: 'synthetic-token', expiry_date: Date.now() + 3_600_000 });
+ getAuthenticatedClient.mockResolvedValue(auth);
+ calendar.mockImplementation(sdk.calendar);
+ // Observe rejection immediately so advancing the clock cannot create an
+ // unhandled rejection before the assertions inspect the final SDK error.
+ const result = apiSyncAccount(ACCOUNT_ID, null).then(value => ({ value }), error => ({ error }));
+ try {
+ await started.promise;
+ await expect(apiSyncAccount(ACCOUNT_ID, null)).rejects.toMatchObject({ status: 409 });
+ await vi.advanceTimersByTimeAsync(29_999);
+ expect(attempts[0].signal.aborted).toBe(false);
+ await vi.advanceTimersByTimeAsync(1);
+ expect(attempts[0].signal.aborted).toBe(true);
+ // 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(attempts.every(attempt => attempt.abortedAt - attempt.startedAt === 30_000)).toBe(true);
+ expect(pushSyncEvents).not.toHaveBeenCalled();
+ healthy = true;
+ await expect(apiSyncAccount(ACCOUNT_ID, null)).resolves.toMatchObject({ status: 'success' });
+ expect(pushSyncEvents).toHaveBeenCalledOnce();
+ } finally {
+ cleanup.abort();
+ await vi.runAllTimersAsync();
+ await result;
+ timeout.mockRestore();
+ vi.useRealTimers();
+ calendar.mockReset();
+ }
+ });
});
diff --git a/server/services/calendarSync.test.js b/server/services/calendarSync.test.js
index a9663f409d..58c3ad00a6 100644
--- a/server/services/calendarSync.test.js
+++ b/server/services/calendarSync.test.js
@@ -19,6 +19,7 @@ vi.mock('./calendarAccounts.js', () => ({
// 401 this test pins.
vi.mock('./googleAuth.js', () => ({
getAuthenticatedClient: vi.fn(async () => null),
+ GOOGLE_API_TIMEOUT_MS: 30_000,
}));
// tribe.js is loaded dynamically by logCalendarTouchpoints; mock it so the
diff --git a/server/services/goalCalendarScheduler.js b/server/services/goalCalendarScheduler.js
index 1add355a0b..035535cbce 100644
--- a/server/services/goalCalendarScheduler.js
+++ b/server/services/goalCalendarScheduler.js
@@ -1,7 +1,7 @@
import { calendar } from '@googleapis/calendar';
import { v4 as uuidv4 } from '../lib/uuid.js';
import { ServerError } from '../lib/errorHandler.js';
-import { getAuthenticatedClient, needsScopeUpgrade, getTokens } from './googleAuth.js';
+import { getAuthenticatedClient, needsScopeUpgrade, getTokens, GOOGLE_API_TIMEOUT_MS } from './googleAuth.js';
import { loadJSON, mutateGoals, GOALS_FILE, DEFAULT_GOALS } from './identity/store.js';
const TIME_SLOT_HOURS = {
@@ -46,7 +46,7 @@ export async function scheduleTimeBlocks(goalId) {
const milestones = [...(goal.milestones || [])].sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
if (!milestones.length) throw new ServerError('No milestones/phases to schedule', { status: 400, code: 'NO_MILESTONES' });
- const cal = calendar({ version: 'v3', auth });
+ const cal = calendar({ version: 'v3', auth, timeout: GOOGLE_API_TIMEOUT_MS });
const { preferredDays, timeSlot, sessionDurationMinutes, subcalendarId } = goal.timeBlockConfig;
const calendarId = subcalendarId || 'primary';
const preferredDayNums = new Set(preferredDays.map(d => DAY_MAP[d]));
@@ -152,7 +152,7 @@ export async function removeScheduledEvents(goalId) {
const goal = goals.goals.find(g => g.id === goalId);
if (!goal) throw new ServerError('Goal not found', { status: 404, code: 'NOT_FOUND' });
- const cal = calendar({ version: 'v3', auth });
+ const cal = calendar({ version: 'v3', auth, timeout: GOOGLE_API_TIMEOUT_MS });
const events = goal.scheduledEvents || [];
// Delete events in parallel
diff --git a/server/services/goalCalendarScheduler.test.js b/server/services/goalCalendarScheduler.test.js
index d30925604a..230eeb4f58 100644
--- a/server/services/goalCalendarScheduler.test.js
+++ b/server/services/goalCalendarScheduler.test.js
@@ -47,6 +47,7 @@ vi.mock('@googleapis/calendar', () => ({
vi.mock('./googleAuth.js', () => ({
getAuthenticatedClient: vi.fn(async () => ({})),
+ GOOGLE_API_TIMEOUT_MS: 30_000,
needsScopeUpgrade: vi.fn(() => false),
getTokens: vi.fn(async () => ({ access_token: 'fake' })),
}));
diff --git a/server/services/googleApiTimeout.test.js b/server/services/googleApiTimeout.test.js
new file mode 100644
index 0000000000..e1be77b5a5
--- /dev/null
+++ b/server/services/googleApiTimeout.test.js
@@ -0,0 +1,31 @@
+import { readFileSync } from 'node:fs';
+import { describe, expect, it } from 'vitest';
+import { GOOGLE_API_TIMEOUT_MS } from './googleAuth.js';
+
+const serviceFiles = [
+ 'messageGmailSync.js',
+ 'messageActions.js',
+ 'calendarGoogleApiSync.js',
+ 'goalCalendarScheduler.js',
+];
+
+describe('Google API client request timeout', () => {
+ it('sets the shared deadline on every Gmail and Calendar service client', () => {
+ const constructors = serviceFiles.flatMap(file => {
+ const source = readFileSync(new URL(`./${file}`, import.meta.url), 'utf8');
+ return [...source.matchAll(/\b(?:gmail|calendar)\(\s*\{[^}]*\}\s*\)/gs)]
+ .map(([construction]) => ({ file, construction }));
+ });
+
+ expect(GOOGLE_API_TIMEOUT_MS).toBe(30_000);
+ expect(constructors).toHaveLength(8);
+ for (const { file, construction } of constructors) {
+ expect(construction, file).toContain('timeout: GOOGLE_API_TIMEOUT_MS');
+ }
+ });
+
+ it('keeps the shorter Gmail reply-metadata request override', () => {
+ const source = readFileSync(new URL('./messageGmailSync.js', import.meta.url), 'utf8');
+ expect(source).toMatch(/metadata\s*=\s*await gmailClient\.users\.messages\.get\([\s\S]*?\},\s*\{ timeout: 10_000, retry: false \}\)/);
+ });
+});
diff --git a/server/services/googleAuth.js b/server/services/googleAuth.js
index 0cd846c522..ca871f5e46 100644
--- a/server/services/googleAuth.js
+++ b/server/services/googleAuth.js
@@ -12,6 +12,7 @@ const SCOPES = [
'https://www.googleapis.com/auth/calendar',
'https://www.googleapis.com/auth/gmail.modify'
];
+export const GOOGLE_API_TIMEOUT_MS = 30_000;
export const OAUTH_REDIRECT_URI = `http://${process.env.PUBLIC_HOST || 'localhost'}:${process.env.PORT || PORTS.API}/api/calendar/google/oauth/callback`;
let oAuth2Client = null;
diff --git a/server/services/messageActions.js b/server/services/messageActions.js
index a402b953f9..c78120fcb2 100644
--- a/server/services/messageActions.js
+++ b/server/services/messageActions.js
@@ -109,12 +109,12 @@ export async function executeAction(accountId, messageId, action) {
*/
async function executeGmailApiAction(message, action) {
const { gmail } = await import('@googleapis/gmail');
- const { getAuthenticatedClient } = await import('./googleAuth.js');
+ const { getAuthenticatedClient, GOOGLE_API_TIMEOUT_MS } = await import('./googleAuth.js');
const auth = await getAuthenticatedClient();
if (!auth) throw new Error('Google OAuth not configured');
- const gmailClient = gmail({ version: 'v1', auth });
+ const gmailClient = gmail({ version: 'v1', auth, timeout: GOOGLE_API_TIMEOUT_MS });
if (action === 'delete') {
await gmailClient.users.messages.trash({ userId: 'me', id: message.apiId });
diff --git a/server/services/messageActions.test.js b/server/services/messageActions.test.js
index 9c4f277c76..9739cc52ed 100644
--- a/server/services/messageActions.test.js
+++ b/server/services/messageActions.test.js
@@ -5,7 +5,7 @@ vi.mock('./messageAccounts.js', () => ({ getAccount: mocks.account }));
vi.mock('./messageSync.js', () => ({ getMessage: mocks.message, removeMessageFromCache: mocks.write }));
vi.mock('./messagePlaywrightSync.js', () => ({ findOrOpenPage: async () => ({}), getPages: async () => [], isAuthPage: () => false, evaluateOnPage: mocks.evaluate }));
vi.mock('./messageTriageRules.js', () => ({ recordCorrection: mocks.correction }));
-vi.mock('./googleAuth.js', () => ({ getAuthenticatedClient: async () => ({}) }));
+vi.mock('./googleAuth.js', () => ({ getAuthenticatedClient: async () => ({}), GOOGLE_API_TIMEOUT_MS: 30_000 }));
vi.mock('@googleapis/gmail', () => ({ gmail: () => ({ users: { messages: { trash: mocks.trash, modify: mocks.modify } } }) }));
import { executeAction } from './messageActions.js';
const accountId = '11111111-1111-1111-1111-111111111111';
diff --git a/server/services/messageGmailSync.js b/server/services/messageGmailSync.js
index 72e18ec862..956b0e9ed0 100644
--- a/server/services/messageGmailSync.js
+++ b/server/services/messageGmailSync.js
@@ -7,7 +7,7 @@ import { messageLogError } from '../lib/messageLogError.js';
import { gmail } from '@googleapis/gmail';
import { v4 as uuidv4 } from '../lib/uuid.js';
import crypto from 'crypto';
-import { getAuthenticatedClient } from './googleAuth.js';
+import { getAuthenticatedClient, GOOGLE_API_TIMEOUT_MS } from './googleAuth.js';
import { htmlToText as sharedHtmlToText } from '../lib/htmlToText.js';
import { ServerError } from '../lib/errorHandler.js';
@@ -236,7 +236,7 @@ export async function syncGmail(account, cache, io, options = {}) {
return { messages: [], status: 'not-configured' };
}
- const gmailClient = gmail({ version: 'v1', auth });
+ const gmailClient = gmail({ version: 'v1', auth, timeout: GOOGLE_API_TIMEOUT_MS });
// Refresh the owner's send-as aliases opportunistically each sync (#2831) — one cheap
// settings call. Returned up to syncAccount so the activity timeline can exclude every
// owner address, not just the primary, from received-message participants. `null` on
@@ -416,7 +416,7 @@ export async function prepareGmailReply(account, draft, original) {
}
const auth = await getAuthenticatedClient();
if (!auth) return replyRefusal('GMAIL_NOT_CONFIGURED', 'Google OAuth is not configured. Connect Google before sending this reply.', 502);
- const gmailClient = gmail({ version: 'v1', auth });
+ const gmailClient = gmail({ version: 'v1', auth, timeout: GOOGLE_API_TIMEOUT_MS });
const metadata = await gmailClient.users.messages.get({
userId: 'me', id: original.apiId, format: 'metadata',
metadataHeaders: ['Message-ID', 'References', 'In-Reply-To', 'Subject']
@@ -487,7 +487,7 @@ export async function sendGmail(account, draft, delivery = null) {
return { success: false, error: 'Google sign-in could not be refreshed — nothing was sent', status: 502, code: 'GMAIL_AUTH_FAILED' };
}
- const gmailClient = gmail({ version: 'v1', auth });
+ const gmailClient = gmail({ version: 'v1', auth, timeout: GOOGLE_API_TIMEOUT_MS });
// Build RFC 2822 message
const toLine = Array.isArray(draft.to) ? draft.to.join(', ') : draft.to;
diff --git a/server/services/messageGmailSync.send.test.js b/server/services/messageGmailSync.send.test.js
index 7e0697b9a6..02c7b23784 100644
--- a/server/services/messageGmailSync.send.test.js
+++ b/server/services/messageGmailSync.send.test.js
@@ -18,7 +18,7 @@ vi.mock('../lib/fileUtils.js', () => ({
safeJSONParse: JSON.parse
}));
vi.mock('./messageAccounts.js', () => ({ getAccount: doubles.getAccount, listAccounts: async () => [] }));
-vi.mock('./googleAuth.js', () => ({ getAuthenticatedClient: async () => doubles.auth }));
+vi.mock('./googleAuth.js', () => ({ getAuthenticatedClient: async () => doubles.auth, GOOGLE_API_TIMEOUT_MS: 30_000 }));
import { sendDraft } from './messageSender.js';
import { getDraft, approveDraft, updateDraft, deleteDraft, reconcileDraftSend } from './messageDrafts.js';
diff --git a/server/services/messageLogging.test.js b/server/services/messageLogging.test.js
index 44c37609f1..1598309d14 100644
--- a/server/services/messageLogging.test.js
+++ b/server/services/messageLogging.test.js
@@ -29,7 +29,7 @@ vi.mock('../lib/fileUtils.js', async (original) => ({
atomicWrite: async (path, value) => { doubles.files.set(path, JSON.stringify(value)); }
}));
vi.mock('./readinessNotify.js', () => ({ noteReadinessChanged: () => {} }));
-vi.mock('./googleAuth.js', () => ({ getAuthenticatedClient: async () => ({ getAccessToken: async () => ({ token: 'synthetic-token' }) }) }));
+vi.mock('./googleAuth.js', () => ({ getAuthenticatedClient: async () => ({ getAccessToken: async () => ({ token: 'synthetic-token' }) }), GOOGLE_API_TIMEOUT_MS: 30_000 }));
vi.mock('@googleapis/gmail', () => ({
gmail: () => ({ users: {
messages: { send: doubles.send, list: doubles.list, get: doubles.get, trash: doubles.trash, modify: doubles.modify },
diff --git a/server/services/messageSync.test.js b/server/services/messageSync.test.js
index b6ca7ae732..0b7148119c 100644
--- a/server/services/messageSync.test.js
+++ b/server/services/messageSync.test.js
@@ -46,7 +46,7 @@ vi.mock('@googleapis/gmail', () => ({
settings: { sendAs: { list: async () => ({ data: { sendAs: [] } }) } },
} }),
}));
-vi.mock('./googleAuth.js', () => ({ getAuthenticatedClient: async () => ({}) }));
+vi.mock('./googleAuth.js', () => ({ getAuthenticatedClient: async () => ({}), GOOGLE_API_TIMEOUT_MS: 30_000 }));
vi.mock('./messageGmailSync.js', () => ({
syncGmail: vi.fn()
@@ -506,6 +506,19 @@ describe('syncAccount', () => {
expect(mockIo.emit).toHaveBeenCalledWith('messages:changed', {});
});
+ it('releases the account lock after a Gmail request timeout', async () => {
+ getAccount.mockResolvedValue({ id: VALID_UUID, name: 'Gmail', type: 'gmail', enabled: true });
+ readFile.mockResolvedValue(JSON.stringify({ syncCursor: null, messages: [] }));
+ syncGmail.mockRejectedValueOnce(new Error('Google API request timed out'))
+ .mockResolvedValueOnce({ messages: [], status: 'success' });
+
+ await expect(syncAccount(VALID_UUID, mockIo)).resolves.toMatchObject({
+ error: 'Google API request timed out', status: 502,
+ });
+ await expect(syncAccount(VALID_UUID, mockIo)).resolves.toMatchObject({ status: 'success' });
+ expect(syncGmail).toHaveBeenCalledTimes(2);
+ });
+
it('should call syncPlaywright for outlook accounts with mode', async () => {
getAccount.mockResolvedValue({ id: VALID_UUID, name: 'Outlook', type: 'outlook', enabled: true });
readFile.mockResolvedValue(JSON.stringify({ syncCursor: null, messages: [] }));