Skip to content

fix(gmail): limit concurrent attachment downloads when forwarding or revising drafts - #50671

Merged
nwparker merged 1 commit into
mainfrom
bugbash/pr-50598
Nov 27, 2026
Merged

nwparker merged 1 commit into
mainfrom
bugbash/pr-50598

Conversation

@nwparker

@nwparker nwparker commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

When Orca forwards an email or revises a draft that has a lot of attachments, it used to ask Gmail for every attachment at the same moment. Gmail only allows a handful of requests at once per user, so with more than about ten files it said "too many requests" and the whole draft failed. Now Orca downloads the attachments ten at a time, so the draft goes through.

Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​66 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​66
Prod 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​9 $\color{#cf222e}{\Huge{\mathbf{−}}}$​11 $\color{#cf222e}{\Huge{\mathbf{−}}}$​2

What Changed

  • Before: savedAttachments in src/main/google/google-gmail-draft-compose.ts ran Promise.all over every saved attachment. A message with more than 10 attachments started more than 10 Gmail requests together. Gmail could answer one of them with a 429 (too many concurrent requests), and that failed the whole forward or revision.
  • After: the downloads go through the existing inBatches helper with the shared GMAIL_CALLS_AT_ONCE limit (10) from google-gmail-read.ts, the same limit that thread listing already uses. Attachments keep their order. The total-size check still runs first, unchanged.
  • Adds google-gmail-draft-compose.test.ts, which forwards a message with 12 attachments through a fake Gmail API that returns 429 once more than 10 requests are in flight. It checks that all 12 attachments arrive in order with their contents intact.

Why

Both composeForward and composeRevision (called from saveGmailDraft) go through savedAttachments, so one change fixes both. Reusing the existing helper and limit keeps every Gmail call path on the same concurrency rule. We didn't add a new limiter or retry logic.

Linked Issue

Fixes #50596

Original fix by @Afloat16 in #50598 (authorship preserved on the commit).

Visual Proof

N/A. This is a main-process change to how Gmail requests are scheduled. Nothing in the UI changes. The before/after evidence is test output:

Before (origin/main) After (this PR)
Forwarding 12 attachments: GoogleToolError: Too many concurrent requests for user (fails) All 12 attachments forwarded in order, contents intact
Revising a draft with 25 attachments: peak 25 requests in flight, fails at the 429 limit Peak 10 in flight, 50 calls, order kept

Testing

  • Reproduced the failure on origin/main with the new test plus a temporary probe (12-attachment forward, 25-attachment revision). Both fail with the modeled 429. Both pass on this branch.

  • vitest over src/main/google/: 7 files, 54 tests passed.

  • CI static gates on the changed files are clean: anti-slop oxlint, native code-quality oxlint, ast-grep, check-changed-code-quality.mjs, and oxfmt --check.

  • Platform: macOS (unit level). The code is platform-independent main-process logic.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

  • Every added test fails without this change and avoids the test anti-patterns

Performance

This only runs when a user forwards a message or revises a draft that has saved attachments. It is async main-process network I/O with no sync work, polling or subprocesses, and the number of requests and bytes is unchanged. Benchmark with a simulated 100 ms Gmail latency per call (old vs new wall time, peak requests in flight):

Attachments Old New
0 57 ms, peak 1 45 ms, peak 1
1 156 ms, peak 1 153 ms, peak 1
5 156 ms, peak 5 167 ms, peak 5
10 161 ms, peak 10 165 ms, peak 10
11 170 ms, peak 11 281 ms, peak 10
20 175 ms, peak 20 272 ms, peak 10
50 167 ms, peak 50 629 ms, peak 10

Up to 10 attachments (the typical case) nothing changes. Above 10, the old path's faster times only hold against a fake that allows unlimited parallel requests. Real Gmail rejects that with a 429. The extra latency only shows up where the old code would have failed.

Risk

Level: Low

The worst case is that forwarding a message with many attachments takes a bit longer, about one extra Gmail round trip per 10 attachments. No saved data, settings, IPC, or remote protocol changes. A plain revert restores the old behavior.

Review

Reviewed the production callers (composeForward, composeRevision via saveGmailDraft), attachment ordering, the aggregate size limit, and error propagation. They are unchanged apart from the concurrency bound.

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

No security, cross-platform, SSH, mobile, or backwards-compatibility impact. Gmail calls run in the desktop main process, and the change adds no new endpoints or protocol fields.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • I stated the risk: what could break, for whom, and how to back it out
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

…revising drafts

Forwarding a message or revising a draft downloaded every saved
attachment at once, so messages with more than ten files could trip
Gmail's per-user concurrency limit (429) and fail the whole draft.
Download them through the existing inBatches helper with the shared
GMAIL_CALLS_AT_ONCE budget, keeping attachment order and size limits.

Fixes #50596

Co-authored-by: Neil <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Saved attachment downloads during message composition now run in batches sized by the existing Gmail request budget. A test models request failures above 10 concurrent calls and checks that composition returns all 12 attachments, including expected names, type, and decoded data.

Severity of issue fixed: Medium

Merge Risk: Low · up to 5a80d

Forwarding or revising a draft with more than 10 saved attachments now downloads them in batches of 10. This avoids request refusals but takes longer than before for those messages. Messages with 10 or fewer attachments are unaffected. The repository's zero-performance-regression rule means the owner should explicitly accept this trade-off before merging.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: Gmail attachment downloads are now limited in concurrency when forwarding or revising drafts. The change in the summary matches this: savedAttachments switches from un…
Description check Passed The description follows the repository template closely. It has ELI5, What Changed, Why, Linked Issue (Fixes #50596, with a note on original authorship), Visual Proof (N/A with a reason and test outpu…
Linked Issues check Passed [#50596] Requires saved-attachment retrieval for forwards and revisions to use the existing inBatches helper and GMAIL_CALLS_AT_ONCE (10), keep attachment order, keep the aggregate byte limit, and ret…
Out of Scope Changes check Passed All changes map to #50596. The production change is a limited edit inside savedAttachments: the import of GMAIL_CALLS_AT_ONCE and inBatches, and the replacement of the concurrent map with a batched ma…
Full details: Title check

Explanation

The title names the main change: Gmail attachment downloads are now limited in concurrency when forwarding or revising drafts. The change in the summary matches this: savedAttachments switches from unbounded Promise.all to inBatches with GMAIL_CALLS_AT_ONCE (10). The title follows the conventional commit style and is specific enough for a teammate scanning history. At 84 characters it is slightly long but within reason.

Full details: Description check

Explanation

The description follows the repository template closely. It has ELI5, What Changed, Why, Linked Issue (Fixes #50596, with a note on original authorship), Visual Proof (N/A with a reason and test output table), Testing with checkboxes, Performance (a benchmark table with a 'None' line replaced by a measured explanation), Risk with a Level of Low and a revert path, AI Disclosure, Review, Agent skill upstream boundary, Notes, and Checklist. The Performance section does not use the single-line 'None, because' form. It gives a measured trade-off instead, which the template allows for larger impact. The AI Disclosure section is empty. The template says to fill it in only for non-StablyAI team members, and the author's membership is not stated, so this is a minor gap to confirm.

Full details: Linked Issues check

Explanation

[#50596] Requires saved-attachment retrieval for forwards and revisions to use the existing inBatches helper and GMAIL_CALLS_AT_ONCE (10), keep attachment order, keep the aggregate byte limit, and return all files. The head diff of src/main/google/google-gmail-draft-compose.ts replaces Promise.all over all files with inBatches(files, GMAIL_CALLS_AT_ONCE, ...). inBatches (google-gmail-read.ts) runs at most limit items at once and appends results in order, so order is kept. The TOOL_DOWNLOAD_MAX_BYTES size check runs before the download and is unchanged. Both composeForward and composeRevision reach savedAttachments, so both paths are covered. The new test google-gmail-draft-compose.test.ts forwards 12 attachments through a fake get that throws 429 when more than 10 calls are active. It asserts 12 attachments, the first named part-1.txt with decoded data, and the last named part-12.txt. The issue's 12-attachment reproduction is covered at the modeled boundary. Limits: the 429 is a local model, as the issue states, and the test does not exercise a live provider. Verdict is based on source and test code at the reviewed head; the test was not executed in this review.

Full details: Out of Scope Changes check

Explanation

All changes map to #50596. The production change is a limited edit inside savedAttachments: the import of GMAIL_CALLS_AT_ONCE and inBatches, and the replacement of the concurrent map with a batched map. The per-file mapping body is unchanged. The new test file covers the same code path and adds no other behavior. No other files change in the diff reviewed. The PR description's performance and benchmark tables describe the PR text, not code, and do not add scope.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: fd92a2d8-19d8-4d9a-a704-210f4729e512
Commits

Reviewing files that changed from the base of the PR and between af84263 and 5a80d2d.

Files selected for processing: 2

  • src/main/google/google-gmail-draft-compose.test.ts
  • src/main/google/google-gmail-draft-compose.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.

@nwparker
nwparker merged commit e142c1f into main Nov 27, 2026
54 checks passed
@nwparker
nwparker deleted the bugbash/pr-50598 branch November 27, 2026 08:49
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.

[Bug]: Forwarding many attachments exceeds Orca's Gmail request budget

2 participants