From 458244112fa2c1c894e6e2feaa46e0e33779c6a2 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 25 Jul 2026 05:44:48 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20agent=20thread?= =?UTF-8?q?=20fetching=20with=20getFullThread?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The agent currently fetches a thread context by executing an N+1 query: it first fetches all thread message metadata (`stub.getEmails`), then iterates over each one to fetch the full content (`stub.getEmail`). This introduces multiple sequential RPC calls and database reads. Replaced this with a single call to `getFullThread(stub, threadId)`, which handles the complete retrieval in 2 queries inside the DO instead of N+1. This improves performance when loading agent context for longer threads. Co-authored-by: schmug <38227427+schmug@users.noreply.github.com> --- .jules/bolt.md | 3 +++ workers/agent/index.ts | 26 ++++---------------------- 2 files changed, 7 insertions(+), 22 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index d3d39015..480e8263 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -24,3 +24,6 @@ ## 2026-06-29 - Optimize D1 Queries by Batching **Learning:** Found N+1 query pattern on the hub stats page (`hub/src/routes/admin/stats.ts`) when iterating over the list of inbound peers to query `events_pulled_24h` for each peer sequentially using `.first()`. This introduces significant roundtrip latency and resource overhead as the number of peers grows. D1 supports batching queries, which can collapse N queries into a single HTTP roundtrip. D1's returned `batch` types can be peculiar, particularly when running against tests using Miniflare or test shims where row result structure might differ across platforms. **Action:** Replaced sequential queries in `Promise.all` with `db.batch()` to combine the multiple `db.prepare(...).bind(...)` calls into a single execution, and normalized the output structure parsing. This significantly reduces N+1 overhead and improves performance scaling as the peer count increases. +## 2026-07-25 - Avoid N+1 requests in agent thread loading +**Learning:** Found N+1 query pattern on the `workers/agent/index.ts` file, where `threadEmails.map` iterated to call `stub.getEmail` sequentially which adds overhead to agent context loading. +**Action:** Replaced loop inside `workers/agent/index.ts` with a single call to `getFullThread(stub, emailData.threadId)`. diff --git a/workers/agent/index.ts b/workers/agent/index.ts index a651a819..e8fb1297 100644 --- a/workers/agent/index.ts +++ b/workers/agent/index.ts @@ -21,6 +21,7 @@ import { getMailboxStub, stripHtmlToText, textToHtml, + getFullThread, } from "../lib/email-helpers"; import { resolveMailboxSettings } from "../lib/mailbox-settings"; import type { EmailFull, EmailMetadata } from "../lib/schemas"; @@ -433,28 +434,9 @@ export class EmailAgent extends AIChatAgent { } // Load thread for conversation context - const threadEmails = (await stub.getEmails({ - thread_id: emailData.threadId, - })) as EmailMetadata[]; - if (threadEmails.length > 1) { - const fullThread = await Promise.all( - threadEmails.map(async (e) => { - const full = (await stub.getEmail(e.id)) as EmailFull | null; - const text = full?.body ? stripHtmlToText(full.body) : ""; - return { - id: e.id, - sender: e.sender, - recipient: e.recipient, - subject: e.subject, - date: e.date, - folder_id: e.folder_id, - body_text: text, - }; - }), - ); - // ⚡ Bolt: Optimize sorting by replacing `new Date(b.date).getTime()` with `Date.parse(b.date)` - fullThread.sort((a, b) => Date.parse(a.date) - Date.parse(b.date)); - threadContext = fullThread + const threadEmails = await getFullThread(stub, emailData.threadId); + if (threadEmails.message_count > 1) { + threadContext = threadEmails.messages .map( (e) => `[${e.date}] ${e.sender} → ${e.recipient} (${e.folder_id}): ${e.body_text.substring(0, 500)}`,