Skip to content

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

Back inside the size limits: Gmail draft bookkeeping moves out of server/index.ts

milind-soni5ed3ee2f7511
diff --git a/server/host/gmail-drafts.ts b/server/host/gmail-drafts.ts
new file mode 100644
index 0000000000..b2c8709c75
--- /dev/null
+++ b/server/host/gmail-drafts.ts
@@ -0,0 +1,47 @@
+// The Gmail drafts one request has reserved, shared by the turn that asked and the handoffs it started. Each turn's generation
+// and each coordination node keeps the set it got first, so a child reserves against its parent's drafts.
+type DraftOwner = { generation: string; generations: { has(id: string): boolean }; gmailDrafts?: Set<string> };
+
+export function createGmailDrafts(deps: {
+ node(id: string): { threadId: string; parentId?: string | null; status: string } | null | undefined;
+ ended(status: string): boolean;
+ owner(threadId: string): DraftOwner | undefined;
+}) {
+ const byGeneration = new Map<string, Set<string>>();
+ const byNode = new Map<string, Set<string>>();
+
+ function forNode(id: string): Set<string> | undefined {
+ for (const cachedId of byNode.keys()) {
+ const node = deps.node(cachedId);
+ if (!node || deps.ended(node.status)) byNode.delete(cachedId);
+ }
+ const node = deps.node(id);
+ if (!node) return undefined;
+ const cached = byNode.get(id);
+ if (cached) return cached;
+ const owner = deps.owner(node.threadId);
+ const reserved = owner?.generations.has(id) && owner.gmailDrafts ? owner.gmailDrafts :
+ (node.parentId ? forNode(node.parentId) : undefined) ?? new Set<string>();
+ byNode.set(id, reserved);
+ return reserved;
+ }
+
+ return {
+ forNode,
+ forTurn(threadId: string, generation: string, handoffId?: string): Set<string> {
+ const cached = byGeneration.get(generation);
+ if (cached) return cached;
+ const owner = deps.owner(threadId);
+ const reserved = owner?.generation === generation && owner.gmailDrafts ? owner.gmailDrafts :
+ forNode(handoffId ?? generation) ?? new Set<string>();
+ byGeneration.set(generation, reserved);
+ return reserved;
+ },
+ forRoomTurn(generation: string, handoffId: string): void {
+ byGeneration.set(generation, forNode(handoffId) ?? new Set<string>());
+ },
+ endTurn(generation: string): void {
+ byGeneration.delete(generation);
+ },
+ };
+}
diff --git a/server/index.ts b/server/index.ts
index 8104d276fa..957a684558 100644
--- a/server/index.ts
+++ b/server/index.ts
@@ -1,6 +1,7 @@
// Import first: it reads a Cloud home's secrets off the launcher's pipe before any module can start a process.
import { BOOT_CLOUD_SECRETS } from "./cloud-secrets-boot.ts";
import { reserveGmailDrafts } from "./gmail-draft-policy.ts";
+import { createGmailDrafts } from "./host/gmail-drafts.ts";
import { AsyncLocalStorage } from "node:async_hooks";
import { createHash, randomBytes, randomUUID, timingSafeEqual } from "node:crypto";
import { existsSync, realpathSync, rmSync } from "node:fs";
@@ -1666,8 +1667,6 @@ const MAX_CREATED_BOT_INSTRUCTIONS = 4_000;
// Only an orphan backstop: normal terminal paths revoke a capability synchronously.
const INTERNAL_CAPABILITY_ORPHAN_MS = 30 * 24 * 60 * 60_000;
const internalCapabilities = new Map<string, InternalCapability>();
-const gmailDraftsByGeneration = new Map<string, Set<string>>();
-const gmailDraftsByNode = new Map<string, Set<string>>();
const activeInternalGenerationByThread = new Map<string, string>();
const internalGenerationByProviderTurn = new ProviderTurnGenerationRegistry();
const computerSelectionTurns = new Map<string, {
@@ -1745,7 +1744,7 @@ function mintInternalCapability(capability: Omit<InternalCapability, "orphanExpi
if (internalCapabilities.get(token)?.generation !== capability.generation) {
internalCapabilities.set(token, {
...capability,
- ...(capability.kind === "connectors" ? { gmailDrafts: gmailDraftsForTurn(capability.threadId, capability.generation, capability.roomHandoffId) } : {}),
+ ...(capability.kind === "connectors" ? { gmailDrafts: gmailDrafts.forTurn(capability.threadId, capability.generation, capability.roomHandoffId) } : {}),
orphanExpiresAt: Date.now() + INTERNAL_CAPABILITY_ORPHAN_MS,
turnStartedAt: Date.now(),
});
@@ -1764,7 +1763,7 @@ function endForeignTurns(threadId: string, generation?: string): void {
}
function revokeInternalCapabilityGeneration(threadId: string, generation: string): void {
- gmailDraftsByGeneration.delete(generation);
+ gmailDrafts.endTurn(generation);
if (standInRoots.delete(generation)) coordinator.endTurn(generation, { ok: true, text: "" });
endForeignTurns(threadId, generation);
if (guestDrivenTurns.get(threadId) === generation) guestDrivenTurns.delete(threadId);
@@ -1962,31 +1961,7 @@ const directRequestOwners = new Map<string, {
gmailDrafts?: Set<string>;
}>();
-function gmailDraftsForNode(id: string): Set<string> | undefined {
- for (const cachedId of gmailDraftsByNode.keys()) {
- const node = coordinator.node(cachedId);
- if (!node || ENDED.has(node.status)) gmailDraftsByNode.delete(cachedId);
- }
- const node = coordinator.node(id);
- if (!node) return undefined;
- const cached = gmailDraftsByNode.get(id);
- if (cached) return cached;
- const owner = directRequestOwners.get(node.threadId);
- const reserved = owner?.generations.has(id) && owner.gmailDrafts ? owner.gmailDrafts :
- (node.parentId ? gmailDraftsForNode(node.parentId) : undefined) ?? new Set<string>();
- gmailDraftsByNode.set(id, reserved);
- return reserved;
-}
-
-function gmailDraftsForTurn(threadId: string, generation: string, handoffId?: string): Set<string> {
- const cached = gmailDraftsByGeneration.get(generation);
- if (cached) return cached;
- const owner = directRequestOwners.get(threadId);
- const reserved = owner?.generation === generation && owner.gmailDrafts ? owner.gmailDrafts :
- gmailDraftsForNode(handoffId ?? generation) ?? new Set<string>();
- gmailDraftsByGeneration.set(generation, reserved);
- return reserved;
-}
+const gmailDrafts = createGmailDrafts({ node: (id) => coordinator.node(id), ended: (status) => ENDED.has(status as HandoffView["status"]), owner: (threadId) => directRequestOwners.get(threadId) });
type DirectTurnOutcome = HostTurnResult;
const directFollowupTurns = new ProviderTurnGenerationRegistry<DirectTurnOutcome>();
const directFollowupSettlers = new Map<string, { threadId: string; settle?: () => void }>();
@@ -7871,7 +7846,7 @@ async function beginTurn(
const requestOrigin = requestWorkOrigin({ trigger: opts?.automationSource, guest: guestConfined, handed: opts?.origin, continued,
line: !continuation && requestMessageId === userMessage.id ? userMessage : undefined });
directRequestOwners.set(threadId, { generation: dispatchClaimId, messageId: requestMessageId,
- gmailDrafts: opts?.coordination ? gmailDraftsForNode(opts.coordination.id) :
+ gmailDrafts: opts?.coordination ? gmailDrafts.forNode(opts.coordination.id) :
continuation && previousRequest && !previousRequest.stopped && (!requestMessageId || previousRequest.messageId === requestMessageId)
? previousRequest.gmailDrafts ?? new Set<string>() : new Set<string>(),
generations: requestGenerations, turnId: null, ...(opts?.automationSource ? { automation: opts.automationSource } : {}), origin: requestOrigin, ...(personLine || (continued && previousRequest?.personLine) ? { personLine: true as const } : {}),
@@ -9859,8 +9834,7 @@ async function runClaimedGroupMemberTurn(
return true;
}
const internalGeneration = beginInternalCapabilityGeneration(threadId);
- if (orchestration?.roomHandoffId) gmailDraftsByGeneration.set(internalGeneration,
- gmailDraftsForNode(orchestration.roomHandoffId) ?? new Set<string>());
+ if (orchestration?.roomHandoffId) gmailDrafts.forRoomTurn(internalGeneration, orchestration.roomHandoffId);
if (operation) roomTurnStarted(internalGeneration, operation);
if (roomGuestConfined) guestDrivenTurns.set(threadId, internalGeneration);
// Only a personal Cloud's owner answering their own line in a room they alone wrote in is theirs.
diff --git a/src/components/Composer.tsx b/src/components/Composer.tsx
index 1ed94e14ec..b30071f3be 100644
--- a/src/components/Composer.tsx
+++ b/src/components/Composer.tsx
@@ -52,10 +52,7 @@ import {
discardUnsentUpload,
composerShouldRefocus,
composerTakesFocusOnOpen,
- imageAttachmentFromFile,
- imageBytesLeft,
- imageSlotsLeft,
- imagesThatFit,
+ imageAttachmentFromFile, imageBytesLeft, imageSlotsLeft, imagesThatFit,
replyTargetTakesFocus,
intakeFiles,
isLongPaste,