Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(agents): replenish warm sessions after successful creation (#7585)
Wolf Mermelstein94ccfd34c2b6
diff --git a/apps/web/src/features/block-agent/context/pending-session.ts b/apps/web/src/features/block-agent/context/pending-session.tsindex daf548c976..d6c36e603f 100644--- a/apps/web/src/features/block-agent/context/pending-session.ts+++ b/apps/web/src/features/block-agent/context/pending-session.ts@@ -23,7 +23,10 @@import { handleAiUsageLimitError } from '@app/features/paywall/ai-usage-limit-handling';import { AgentSession } from '@core/agent-session/AgentSession';import { PromptTrace } from '@core/agent-session/prompt-telemetry';-import { takeWarmAgentSession } from '@queries/agent-session/warm';+import {+ replenishWarmAgentSession,+ takeWarmAgentSession,+} from '@queries/agent-session/warm';import { refetchSoupEntity } from '@queries/soup/normalized-cache';import { agentHarnessServiceClient } from '@service-agent-harness/client';import type {@@ -151,6 +154,8 @@ export function startPendingSession(trace?.stage('created');// Normally the id this tab minted; an older service may mint its own.const created = result.value.session.id;+ // A warm claim releases its server reservation before creation answers.+ replenishWarmAgentSession(result.value.session.ownerId);void refetchSoupEntity(created, 'agentSession', { created: true });// Hold the block in preflight while selected settings are confirmed,// then adopt the session before issuing the first prompt so that promptdiff --git a/apps/web/src/features/block-agent/context/resolve-session-id.test.ts b/apps/web/src/features/block-agent/context/resolve-session-id.test.tsindex c1e68f41cc..3da640b50e 100644--- a/apps/web/src/features/block-agent/context/resolve-session-id.test.ts+++ b/apps/web/src/features/block-agent/context/resolve-session-id.test.ts@@ -10,8 +10,10 @@ import { createRoot } from 'solid-js';import { beforeEach, describe, expect, it, vi } from 'vitest';const takeWarm = vi.hoisted(() => vi.fn<() => string | undefined>());+const replenishWarm = vi.hoisted(() => vi.fn<(userId: string) => void>());vi.mock('@queries/agent-session/warm', () => ({takeWarmAgentSession: takeWarm,+ replenishWarmAgentSession: replenishWarm,}));const refetchSoupEntity = vi.hoisted(() => vi.fn(async () => {}));@@ -130,6 +132,7 @@ const flush = () => new Promise((resolve) => setTimeout(resolve, 0));beforeEach(() => {takeWarm.mockReset();+ replenishWarm.mockReset();refetchSoupEntity.mockClear();create.control.mockReset();create.release.mockReset();@@ -506,10 +509,42 @@ it('falls back to a cold session when claiming a warm session fails', async () =.lastCall?.[0];expect(request?.id).not.toBe('stale-warm');});+ expect(replenishWarm).not.toHaveBeenCalled();create.resolve?.();const { pendingSession } = await import('./pending-session');await vi.waitFor(() =>expect(pendingSession(placeholder)?.sessionId()).toBeTruthy());expect(pendingSession(placeholder)?.failed()).toBe(false);+ expect(replenishWarm).toHaveBeenCalledExactlyOnceWith('session-owner');+});++it.each([undefined, 'ready-warm'])(+ 'replenishes only after successful creation when the reservation is %s',+ async (warmId) => {+ takeWarm.mockReturnValueOnce(warmId);+ startPendingSession({ userId: 'session-owner' });+ await flush();+ expect(replenishWarm).not.toHaveBeenCalled();+ create.resolve?.();+ await flush();+ expect(replenishWarm).toHaveBeenCalledExactlyOnceWith('session-owner');+ }+);++it('does not replenish when both the warm claim and fallback creation fail', async () => {+ takeWarm.mockReturnValueOnce('stale-warm');+ const placeholder = startPendingSession({ userId: 'session-owner' });+ create.reject?.();+ await vi.waitFor(() =>+ expect(+ vi.mocked(agentHarnessServiceClient.create).mock.lastCall?.[0].id+ ).not.toBe('stale-warm')+ );+ create.reject?.();+ const { pendingSession } = await import('./pending-session');+ await vi.waitFor(() =>+ expect(pendingSession(placeholder)?.failed()).toBe(true)+ );+ expect(replenishWarm).not.toHaveBeenCalled();});diff --git a/apps/web/src/lib/queries/agent-session/warm.test.tsx b/apps/web/src/lib/queries/agent-session/warm.test.tsxindex c2cbebe2c8..c9bd58b44b 100644--- a/apps/web/src/lib/queries/agent-session/warm.test.tsx+++ b/apps/web/src/lib/queries/agent-session/warm.test.tsx@@ -1,69 +1,254 @@/** @vitest-environment jsdom */import { MACRO_NEW_BOT_ID } from '@core/constant/macroNew';import { agentHarnessServiceClient } from '@service-agent-harness/client';+import type { WarmAgentSessionResponse } from '@service-agent-harness/generated/schemas';import { QueryClient, QueryClientProvider } from '@tanstack/solid-query';import { ok } from 'neverthrow';import { render } from 'solid-js/web';-import { afterEach, expect, it, vi } from 'vitest';-import { takeWarmAgentSession, useWarmAgentSessionQuery } from './warm';+import { afterEach, beforeEach, expect, it, vi } from 'vitest';+import {+ replenishWarmAgentSession,+ takeWarmAgentSession,+ useWarmAgentSessionQuery,+} from './warm';vi.mock('@service-agent-harness/client', () => ({agentHarnessServiceClient: { warm: vi.fn() },}));-const disposers: (() => void)[] = [];++const OWNER = 'warm-test-user';+const warm = vi.mocked(agentHarnessServiceClient.warm);+const disposers = new Set<() => void>();+const clients: QueryClient[] = [];++beforeEach(() => {+ warm.mockResolvedValue(ok({ session: null }));+});+afterEach(() => {- for (const dispose of disposers.splice(0)) dispose();- vi.clearAllMocks();+ for (const dispose of disposers) dispose();+ for (const client of clients.splice(0)) client.clear();+ vi.restoreAllMocks();+ vi.resetAllMocks();});-it('deduplicates page mounts and claims only the same owner and configuration once', async () => {- vi.mocked(agentHarnessServiceClient.warm).mockResolvedValue(- ok({- session: { id: 'warm-id', model: 'model-a', instructions: null },- } as never)- );+function prepared(id: string) {+ return ok<WarmAgentSessionResponse>({+ session: {+ id,+ model: 'model-a',+ instructions: null,+ botId: MACRO_NEW_BOT_ID,+ canEdit: true,+ createdAt: '2026-10-06T00:00:00Z',+ modifiedAt: '2026-10-06T00:00:00Z',+ harness: 'in-memory',+ isArchived: false,+ name: '',+ ownerId: OWNER,+ sandboxSize: 'default',+ status: { kind: 'no_messages' },+ workspace: '/workspace',+ },+ });+}++function setup() {const client = new QueryClient();- const take = (options: Parameters<typeof takeWarmAgentSession>[0]) =>- takeWarmAgentSession(options, client);- const Hook = () => {- useWarmAgentSessionQuery(() => 'warm-test-user');- return null;- };- for (let i = 0; i < 2; i++) {- const host = document.createElement('div');- disposers.push(- render(- () => (- <QueryClientProvider client={client}>- <Hook />- </QueryClientProvider>- ),- host- )+ clients.push(client);+ const take = (options: Parameters<typeof takeWarmAgentSession>[0] = {}) =>+ takeWarmAgentSession({ userId: OWNER, ...options }, client);+ const replenish = () => replenishWarmAgentSession(OWNER, client);+ const settled = () => vi.waitFor(() => expect(client.isFetching()).toBe(0));+ const mount = () => {+ const Hook = () => {+ useWarmAgentSessionQuery(() => OWNER);+ return null;+ };+ const dispose = render(+ () => (+ <QueryClientProvider client={client}>+ <Hook />+ </QueryClientProvider>+ ),+ document.createElement('div'));- }- await vi.waitFor(() =>- expect(agentHarnessServiceClient.warm).toHaveBeenCalledTimes(1)+ const unmount = () => {+ disposers.delete(unmount);+ dispose();+ };+ disposers.add(unmount);+ return unmount;+ };+ return { take, replenish, settled, mount };+}++it('does not warm for an owner who has no existing warming query', () => {+ const { replenish } = setup();+ replenish();+ expect(warm).not.toHaveBeenCalled();+});++it('retries a failed initial preparation only after successful creation', async () => {+ warm.mockRejectedValueOnce(new Error('offline'));+ warm.mockResolvedValueOnce(prepared('recovered'));+ const { mount, settled, take, replenish } = setup();+ mount();+ await settled();+ expect(warm).toHaveBeenCalledTimes(1);+ expect(take()).toBeUndefined();+ await settled();+ expect(warm).toHaveBeenCalledTimes(1);++ replenish();+ await settled();+ expect(warm).toHaveBeenCalledTimes(2);+ expect(take()).toBe('recovered');+ await settled();+ expect(warm).toHaveBeenCalledTimes(2);+});++it('deduplicates page mounts and preserves a reservation for the matching owner and settings', async () => {+ warm.mockResolvedValueOnce(prepared('warm-id'));+ const { mount, settled, take, replenish } = setup();+ mount();+ mount();+ await settled();+ expect(warm).toHaveBeenCalledTimes(1);+ expect(take({ userId: 'other' })).toBeUndefined();+ expect(take({ userId: undefined })).toBeUndefined();+ expect(take({ botId: 'another-bot' })).toBeUndefined();+ expect(take({ repoUrl: 'https://github.com/example/repo' })).toBeUndefined();+ expect(take({ modelOverride: 'different' })).toBeUndefined();+ expect(take({ instructions: 'different' })).toBeUndefined();+ replenish();+ await settled();+ expect(warm).toHaveBeenCalledTimes(1);+ expect(take({ botId: MACRO_NEW_BOT_ID, modelOverride: 'model-a' })).toBe(+ 'warm-id');- await vi.waitFor(() => expect(client.isFetching()).toBe(0));- expect(take({ userId: 'other', botId: MACRO_NEW_BOT_ID })).toBeUndefined();- expect(- take({- userId: 'warm-test-user',- botId: MACRO_NEW_BOT_ID,- modelOverride: 'different',- })- ).toBeUndefined();- expect(- take({- userId: 'warm-test-user',- botId: MACRO_NEW_BOT_ID,- instructions: 'different',- })- ).toBeUndefined();- expect(take({ userId: 'warm-test-user' })).toBe('warm-id');- expect(- take({ userId: 'warm-test-user', botId: MACRO_NEW_BOT_ID })- ).toBeUndefined();- client.clear();+ expect(take()).toBeUndefined();+ await settled();+});++it('waits for the claim to release capacity before preparing one shared replacement', async () => {+ const replacement = Promise.withResolvers<ReturnType<typeof prepared>>();+ let claimComplete = false;+ warm.mockResolvedValueOnce(prepared('first'));+ // The owner already has two reservations. Only a completed claim frees a slot.+ warm.mockImplementationOnce(async () =>+ claimComplete ? replacement.promise : ok({ session: null })+ );+ const { mount, settled, take, replenish } = setup();+ mount();+ mount();+ await settled();+ expect(take()).toBe('first');+ expect(take()).toBeUndefined();+ // The destination can mount while POST /agent-sessions is still pending.+ mount();+ await settled();+ expect(warm).toHaveBeenCalledTimes(1);+ claimComplete = true;+ replenish();+ await vi.waitFor(() => expect(warm).toHaveBeenCalledTimes(2));+ mount();+ expect(warm).toHaveBeenCalledTimes(2);+ replacement.resolve(prepared('second'));+ await settled();+ expect(take()).toBe('second');+ expect(take()).toBeUndefined();+ replenish();+ await settled();+ expect(warm).toHaveBeenCalledTimes(3);+});++it('keeps the same in-flight replacement across navigation to another warming surface', async () => {+ const replacement = Promise.withResolvers<ReturnType<typeof prepared>>();+ warm.mockResolvedValueOnce(prepared('first'));+ warm.mockReturnValueOnce(replacement.promise);+ const { mount, settled, take, replenish } = setup();+ const leaveHome = mount();+ await settled();+ expect(take()).toBe('first');+ replenish();+ await vi.waitFor(() => expect(warm).toHaveBeenCalledTimes(2));+ leaveHome();+ mount();+ replacement.resolve(prepared('second'));+ await settled();+ expect(warm).toHaveBeenCalledTimes(2);+ expect(take()).toBe('second');+ await settled();+});++it.each(['consumed', 'expired'] as const)(+ 'waits for a new observer before replacing an inactive %s reservation',+ async (reason) => {+ warm.mockResolvedValueOnce(prepared('first'));+ warm.mockResolvedValueOnce(prepared('second'));+ const { mount, settled, take, replenish } = setup();+ const unmount = mount();+ await settled();+ unmount();+ if (reason === 'expired') {+ const now = vi+ .spyOn(Date, 'now')+ .mockReturnValue(Date.now() + 5 * 60_000);+ expect(take()).toBeUndefined();+ now.mockRestore();+ } else {+ expect(take()).toBe('first');+ }+ replenish();+ await settled();+ expect(warm).toHaveBeenCalledTimes(1);+ expect(take()).toBeUndefined();+ mount();+ mount();+ await settled();+ expect(warm).toHaveBeenCalledTimes(2);+ expect(take()).toBe('second');+ await settled();+ }+);++it.each(['empty', 'failed'] as const)(+ 'does not loop when replacement preparation returns %s',+ async (result) => {+ warm.mockResolvedValueOnce(prepared('first'));+ if (result === 'failed') warm.mockRejectedValueOnce(new Error('offline'));+ const { mount, settled, take, replenish } = setup();+ mount();+ await settled();+ expect(take()).toBe('first');+ replenish();+ await settled();+ expect(warm).toHaveBeenCalledTimes(2);+ expect(take()).toBeUndefined();+ expect(take()).toBeUndefined();+ await settled();+ expect(warm).toHaveBeenCalledTimes(2);+ }+);++it('joins an in-flight refresh when an expired reservation is discarded', async () => {+ const replacement = Promise.withResolvers<ReturnType<typeof prepared>>();+ warm.mockResolvedValueOnce(prepared('first'));+ warm.mockReturnValueOnce(replacement.promise);+ const { mount, settled, take, replenish } = setup();+ const unmount = mount();+ await settled();+ unmount();+ const now = vi.spyOn(Date, 'now').mockReturnValue(Date.now() + 5 * 60_000);+ mount();+ await vi.waitFor(() => expect(warm).toHaveBeenCalledTimes(2));+ expect(take()).toBeUndefined();+ now.mockRestore();+ replenish();+ expect(warm).toHaveBeenCalledTimes(2);+ replacement.resolve(prepared('second'));+ await settled();+ expect(take()).toBe('second');+ await settled();});diff --git a/apps/web/src/lib/queries/agent-session/warm.ts b/apps/web/src/lib/queries/agent-session/warm.tsindex bdb6389da9..0a82a2f6ea 100644--- a/apps/web/src/lib/queries/agent-session/warm.ts+++ b/apps/web/src/lib/queries/agent-session/warm.ts@@ -26,10 +26,12 @@ export function useWarmAgentSessionQuery(userId: Accessor<string | undefined>) {gcTime: CLIENT_WARM_TTL_MS,retry: false,refetchOnWindowFocus: false,- queryFn: async ({ signal }: { signal: AbortSignal }) => {+ queryFn: async () => {if (!owner) return null;+ // Keep one reservation in flight across Home -> Agents navigation.+ // Aborting on unmount would abandon its server ID and warm another.const result = await throwOnErr(() =>- agentHarnessServiceClient.warm(uuidv7(), signal)+ agentHarnessServiceClient.warm(uuidv7()));if (result.session) {return {@@ -45,7 +47,10 @@ export function useWarmAgentSessionQuery(userId: Accessor<string | undefined>) {});}-/** Consume once, only for the owner and exact default-agent configuration. */+/**+ * Consume once, only for the owner and exact default-agent configuration.+ * Keep the empty cache fresh until creation finishes and releases server capacity.+ */export function takeWarmAgentSession(options: {userId?: string;@@ -77,3 +82,19 @@ export function takeWarmAgentSession(client.setQueryData(key, null);return entry.id;}++/** Prepare one replacement after creation succeeds, joining any existing request. */+export function replenishWarmAgentSession(+ userId: string,+ client: QueryClient = queryClient+): void {+ const key = agentSessionWarmKeys.owner(userId).queryKey;+ const query = client.getQueryState<WarmSession | null>(key);+ // A different configuration may have left a usable reservation in the cache.+ // No query means this owner has not mounted a warming surface.+ if (!query || query.data) return;+ void client.invalidateQueries(+ { queryKey: key, exact: true },+ { cancelRefetch: false }+ );+}diff --git a/docs/AGENT_GUIDE/ai-chat.md b/docs/AGENT_GUIDE/ai-chat.mdindex f14c6a6cfe..f9b395e721 100644--- a/docs/AGENT_GUIDE/ai-chat.md+++ b/docs/AGENT_GUIDE/ai-chat.md@@ -1420,3 +1420,17 @@ prepared session only when the user, model, and instructions match. Otherpersonas and changed settings use normal creation. Warm failures must not blocksending. Unclaimed sessions expire after ten minutes; a claimed conversationmust remain visible and usable after that deadline.++Taking a reservation clears it immediately, but replacement preparation waits+until session creation succeeds. This lets a warm claim release its server+reservation before requesting another. An active Home or Agents surface then+prepares one replacement; otherwise preparation waits for the next mount. Verify+a second conversation can reuse that replacement without waiting five minutes,+including when another tab already holds the owner's other warm reservation.+Successful cold creation also replenishes an empty cache left by an expired or+failed reservation. A failed create does not trigger more warming.+An already-started preparation stays in the shared cache across Home-to-Agents+navigation, so mounting the next surface joins that request instead of warming+another server session.+Empty or failed warm responses do not trigger a refill loop, and mismatched+agent settings leave a usable reservation available for the default agent.