Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(connected-reads): only whole Gmail pages count toward the page limit

AdityaUmale76b59b7e7c8b
diff --git a/server/connected-reads.test.ts b/server/connected-reads.test.ts
index 2e6f51817c..98ea391198 100644
--- a/server/connected-reads.test.ts
+++ b/server/connected-reads.test.ts
@@ -114,6 +114,24 @@ describe("a check's reads", () => {
expect(frames.filter((frame) => frame.slug === "GMAIL_FETCH_EMAILS").every((frame) => Number(frame.args!.max_results) <= 50)).toBe(true);
});
+ it("asking again at a smaller size does not use up the pages a read may take", async () => {
+ const messages = Array.from({ length: 90 }, (_, index) => ({ messageId: `m${index}`, threadId: `t${index}`, messageTimestamp: new Date(Date.UTC(2026, 9, 22, 0, index)).toISOString(), sender: "[email protected]", subject: "s" }));
+ // Four whole pages of 20, then the next page comes back shortened at 20 and at 10, and whole at 5.
+ const { deps, frames } = relay({
+ GMAIL_FETCH_EMAILS: (args) => {
+ const size = Number(args.max_results);
+ const start = Number(args.page_token ?? 0);
+ const page = messages.slice(start, start + size);
+ if (start >= 80 && size > 5) return { successful: true, data_preview: { messages: [...page.slice(0, 2), `...${page.length - 2} more items`] } };
+ return { successful: true, data: { messages: page, ...(start + size < messages.length ? { nextPageToken: String(start + size) } : {}) } };
+ },
+ });
+ const read = await createConnectedReads(deps).session().emails(0, 100);
+ expect(frames.filter((frame) => frame.slug === "GMAIL_FETCH_EMAILS").map((frame) => Number(frame.args!.max_results))).toEqual([20, 20, 20, 20, 20, 10, 5, 5]);
+ expect(read).toMatchObject({ more: false });
+ expect(read.emails).toHaveLength(90);
+ });
+
it("a shortened answer is an error that says so, never an empty list", async () => {
const { deps } = relay({ GMAIL_FETCH_EMAILS: () => ({ successful: true, data_preview: { messages: ["...50 more items"] } }) });
const mail = await createConnectedReads(deps).session().emails(0, 5).catch((error: unknown) => error);
diff --git a/server/connected-reads.ts b/server/connected-reads.ts
index 0951188286..f1f80a0d78 100644
--- a/server/connected-reads.ts
+++ b/server/connected-reads.ts
@@ -548,7 +548,8 @@ export function createConnectedReads(deps: ConnectedReadDeps): Omit<ConnectedRea
const found: EmailHeader[] = [];
let page: string | undefined;
let size = GMAIL_PAGE;
- for (let index = 0; index < GMAIL_PAGES; index++) {
+ // Only whole pages count toward GMAIL_PAGES; a shortened answer is asked again smaller, at most twice per read.
+ for (let pages = 0; pages < GMAIL_PAGES;) {
const data = await call(GMAIL_FETCH_TOOL, {
user_id: "me", query: `in:inbox after:${seconds(sinceMs)} -category:promotions -category:social`,
max_results: size, include_payload: false, verbose: false, ...(page ? { page_token: page } : {}),
@@ -557,6 +558,7 @@ export function createConnectedReads(deps: ConnectedReadDeps): Omit<ConnectedRea
size = Math.max(GMAIL_PAGE_MIN, Math.floor(size / 2));
continue;
}
+ pages++;
found.push(...parsed("gmail", () => emailsFromData(data)));
page = nextPageOf(data);
if (!page) break;