Skip to content

perf(gmail): API efficiency, atomic label updates, metadata search, audit accuracy, and HTTP retry resilience - #20

Merged
willtmc merged 1 commit into
mainfrom
perf/gmail-api-resilience-and-efficiency
Nov 25, 2026
Merged

willtmc merged 1 commit into
mainfrom
perf/gmail-api-resilience-and-efficiency

Conversation

@willtmc

@willtmc willtmc commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Summary

Improves Gmail API efficiency, atomicity, drafts-audit accuracy, and HTTP timeout/retry resilience in GmailService and drafts-audit:

  1. Fix downloadAttachments double-fetch cache bug (src/gmail-service.ts):
    • Forward attachment part size from downloadPartsForMessages into downloadAttachments.
    • On cache hit where attachment.size > 0 matches the on-disk file size, return { cached: true } immediately with 0 Gmail API calls.
    • When expected size is unknown or mismatches disk, call attachments.get once (never twice), decode the payload, reuse disk if decoded length matches, otherwise write with 0600 perms.
  2. Atomic single-request modifyLabels + whitespace trimming (src/gmail-service.ts):
    • Trim whitespace, drop empty entries, and deduplicate labels in both resolveLabelIds and modifyLabels.
    • Combine addLabelIds and removeLabelIds into a single atomic gmail.users.threads.modify request per thread, eliminating partial-failure states.
  3. Optimize searchThreads (src/gmail-service.ts):
    • Fetch threads with format: "metadata" and metadataHeaders: ["From", "To", "Cc", "Subject", "Date"] instead of format: "full".
    • Fetch thread metadata with bounded concurrency (5) via mapWithConcurrency, preserving input order and full ThreadSearchResult compatibility.
  4. Fix drafts audit error swallowing, bounded concurrency & multi-recipient search (src/gmail-service.ts, src/drafts-audit.ts):
    • In fetchThread and fetchMessageMetadata, treat only genuine HTTP 404 (isGmailNotFoundError) as null; propagate 401, 403, 429, 5xx, and network errors so transient/auth failures never silently produce false not_proven_duplicate results.
    • In buildSentSearchQuery, search across unique To/Cc recipients with (to:a@x.com OR to:b@y.com) (capped at 5 unique emails) instead of only the first recipient.
    • Use bounded concurrency (5) with in-flight promise deduplication when fetching draft details, threads, and sent-message metadata in auditDrafts.
  5. HTTP timeout & transient 429/5xx retry/backoff (src/gmail-service.ts):
    • Configure a 30_000 ms timeout on both the OAuth2Client transporter and google.gmail(...) client.
    • Configure Gaxios retry: true with exponential backoff (retry: 3, 429 + 500–599, httpMethodsToRetry: ["GET", "HEAD", "OPTIONS", "PUT", "DELETE"]), explicitly excluding POST so non-idempotent sends/draft creates are never auto-retried.

Verification

  • npm ci --no-audit --no-fund
  • npm run lint (biome check src/) — passed (7 files, 0 issues)
  • npm run typecheck (tsc --noEmit) — passed
  • npm run build — passed
  • npm test — 50/50 tests passing, including new unit tests in test/gmail-service-perf.test.js and expanded tests in test/drafts-audit.test.js

@willtmc
willtmc merged commit 19d1445 into main Nov 25, 2026
1 check passed
@willtmc
willtmc deleted the perf/gmail-api-resilience-and-efficiency branch November 25, 2026 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant