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
perf(agents): keep recently viewed agent sessions open across switches (#9356)
Eric Hayes7312222a048b
diff --git a/apps/web/src/features/block-agent/context/create-agent-session.test.tsx b/apps/web/src/features/block-agent/context/create-agent-session.test.tsxindex a167274861..49e6b1e8e1 100644--- a/apps/web/src/features/block-agent/context/create-agent-session.test.tsx+++ b/apps/web/src/features/block-agent/context/create-agent-session.test.tsx@@ -70,6 +70,9 @@ vi.mock('@queries/agent-session/session-metadata-sync', () => ({const { cleanup, render, screen } = await import('@solidjs/testing-library');const { AgentSession } = await import('@core/agent-session/AgentSession');+const { forgetRecentlyViewed } = await import(+ '@core/agent-session/recently-viewed'+);const { createAgentSession } = await import('./create-agent-session');const { TraceFlow } = await import('@app/observability/flow');@@ -102,6 +105,7 @@ const mount = () =>beforeEach(() => {vi.clearAllMocks();+ forgetRecentlyViewed(SESSION);for (let leaked = AgentSession.get(SESSION);leaked;@@ -173,6 +177,36 @@ describe('createAgentSession', () => {expect(await screen.findByText('no access')).toBeTruthy();expect(screen.queryByText('loading')).toBeNull();});++ describe('a session viewed before', () => {+ const viewAndLeave = async () => {+ mount();+ expect(await screen.findByText('A session')).toBeTruthy();+ cleanup();+ };++ it('reopens without fetching the log again, and reads the row behind it', async () => {+ await viewAndLeave();+ harness.get.mockResolvedValue(ok({ ...session, name: 'Renamed' }));++ mount();+ expect(await screen.findByText('Renamed')).toBeTruthy();+ expect(logSource.fetched).toHaveBeenCalledOnce();+ expect(harness.get).toHaveBeenCalledTimes(2);+ });++ it('closes on a refusal of the reread row', async () => {+ await viewAndLeave();+ harness.get.mockResolvedValue(err([{ code: 'FORBIDDEN' }]));++ mount();+ expect(await screen.findByText('no access')).toBeTruthy();+ expect(screen.getByText('untitled')).toBeTruthy();+ cleanup();+ // No longer held: nothing keeps the refused session open.+ expect(AgentSession.get(SESSION)).toBeUndefined();+ });+ });});it('keeps view readiness pending through provisioning and the first transcript load', async () => {diff --git a/apps/web/src/features/block-agent/context/create-agent-session.ts b/apps/web/src/features/block-agent/context/create-agent-session.tsindex 49312f767a..cfe298aefc 100644--- a/apps/web/src/features/block-agent/context/create-agent-session.ts+++ b/apps/web/src/features/block-agent/context/create-agent-session.ts@@ -15,6 +15,11 @@ import {type AgentSessionRecord,type IssueResult,} from '@core/agent-session/AgentSession';+import {+ forgetRecentlyViewed,+ isRecentlyViewed,+ retainRecentlyViewed,+} from '@core/agent-session/recently-viewed';import { fireAndForget } from '@core/util/fireAndForget';import type { AgentSessionRenamedEvent } from '@queries/agent-session/realtime-protocol';import {@@ -230,6 +235,7 @@ export function createAgentSession(const record = await session.load();if (superseded(session)) return record.session;await show(session, record, 'fetched');+ retainRecentlyViewed(session.id);return renameRefresh > renameRefreshAtStart &&latestRename?.agentSessionId === session.id? { ...record.session, name: latestRename.name }@@ -240,6 +246,7 @@ export function createAgentSession(live,async (session) => {const renameRefreshAtStart = renameRefresh;+ const reused = isRecentlyViewed(session.id);batch(() => {setList(reconcile([]));@@ -248,6 +255,17 @@ export function createAgentSession(setSettleFailure(undefined);});+ // Held open since an earlier view: the fold is current, so it goes+ // straight up. The row is from that view, and is read again behind it.+ if (reused) {+ const row = await settle(session, renameRefreshAtStart);+ // Started as the resource resolves, so its answer cannot land first.+ if (!superseded(session)) {+ fireAndForget(revalidateRow(session), 'agent-session');+ }+ return row;+ }+const cached = await session.warm();if (!cached || superseded(session)) {return settle(session, renameRefreshAtStart);@@ -330,6 +348,27 @@ export function createAgentSession(}));+ /**+ * The row of a session held open since an earlier view, read again. A+ * refusal ends the reopen as a denied load would have; a newer rename or+ * update wins over this read.+ */+ async function revalidateRow(session: AgentSession): Promise<void> {+ const run = ++updateRefresh;+ const renameRefreshAtStart = renameRefresh;+ const row = await session.readRow();+ if (run !== updateRefresh || superseded(session)) return;+ if (row.isErr()) {+ if (row.error === 'denied') {+ forgetRecentlyViewed(session.id);+ setSettleFailure(new AgentSessionAccessDenied(session.id));+ }+ return;+ }+ if (renameRefresh !== renameRefreshAtStart) return;+ mutate((current) => (current ? row.value : current));+ }+// A first fetch would suspend; see `openedPending`.//// Once the load has failed, `resource.latest` rethrows the error on everydiff --git a/apps/web/src/features/block-agent/context/pending-session.test.tsx b/apps/web/src/features/block-agent/context/pending-session.test.tsxindex 5a7262c3cb..8a2c234637 100644--- a/apps/web/src/features/block-agent/context/pending-session.test.tsx+++ b/apps/web/src/features/block-agent/context/pending-session.test.tsx@@ -37,6 +37,12 @@ vi.mock('@core/constant/featureFlags', async (importOriginal) => {vi.mock('@service-agent-harness/client', () => ({agentHarnessServiceClient: harness,}));+// These cases count the pending navigation's own acquisitions; holding the+// session open after a view is `recently-viewed.test.ts`'s concern.+vi.mock('@core/agent-session/recently-viewed', () => ({+ isRecentlyViewed: () => false,+ retainRecentlyViewed: () => {},+}));vi.mock('@queries/soup/normalized-cache', () => ({refetchSoupEntity: vi.fn(async () => {}),}));diff --git a/apps/web/src/lib/core/agent-session/AgentSession.test.ts b/apps/web/src/lib/core/agent-session/AgentSession.test.tsindex 74da0060fc..ac4197a23a 100644--- a/apps/web/src/lib/core/agent-session/AgentSession.test.ts+++ b/apps/web/src/lib/core/agent-session/AgentSession.test.ts@@ -1191,6 +1191,33 @@ describe('AgentSession', () => {live.release();});+ it('reports a second open of a loaded session as reused, fetching nothing', async () => {+ const first = AgentSession.acquire(SESSION);+ await first.load();+ const second = AgentSession.acquire(SESSION);+ await second.load();++ expect(logSource.watch).toHaveBeenCalledOnce();+ expect(harness.get).toHaveBeenCalledOnce();+ expect(+ loadSpans().map((span) => span.attributes['agent.session.load.outcome'])+ ).toEqual(['loaded', 'reused']);+ second.release();+ first.release();+ });++ it('does not trace a hold as an open', async () => {+ const live = AgentSession.acquire(SESSION);+ await live.load();+ const held = AgentSession.acquire(SESSION, 'hold');++ expect(+ loadSpans().map((span) => span.attributes['agent.session.load.outcome'])+ ).toEqual(['loaded']);+ held.release();+ live.release();+ });+it('says why a load failed', async () => {harness.get.mockResolvedValueOnce(err([{ code: 'HTTP_ERROR' }]));const live = AgentSession.acquire(SESSION);diff --git a/apps/web/src/lib/core/agent-session/AgentSession.ts b/apps/web/src/lib/core/agent-session/AgentSession.tsindex 4bac5514d4..1b9dde1539 100644--- a/apps/web/src/lib/core/agent-session/AgentSession.ts+++ b/apps/web/src/lib/core/agent-session/AgentSession.ts@@ -52,6 +52,7 @@ import type {} from '@service-agent-harness/generated/schemas';import { subscribeGraphqlSoupReconnected } from '@service-storage/graphql-soup';import { CombinedError } from '@urql/core';+import { err, ok, type Result } from 'neverthrow';import { v7 as uuidv7 } from 'uuid';import {type LoadFailure,@@ -173,14 +174,22 @@ export class AgentSession {/*** The shared instance for `id`, created on first acquisition. Every* acquisition needs a matching {@link release}; the last one closes the- * machine and drops the subscriptions.+ * machine and drops the subscriptions. `hold` keeps a session open for a+ * later view without being one, so it is not traced as an open.*/- static acquire(id: string): AgentSession {+ static acquire(id: string, purpose: 'view' | 'hold' = 'view'): AgentSession {const open = AgentSession.open.get(id);const session = open ?? new AgentSession(id);AgentSession.open.set(id, session);session.references += 1;+ if (purpose === 'hold') return session;traceAcquire(id, open === undefined, session.references);+ // A surface opening a session that is already folded waits for nothing;+ // its render is measured from now, not from the load long ago.+ if (session.loaded) {+ session.trace = new SessionLoadTrace(id);+ session.trace.end('reused');+ }return session;}@@ -265,6 +274,11 @@ export class AgentSession {/** Prompts issued here still waiting for their first output. */private readonly prompts = new Set<PromptTrace>();+ /** The fetched log is folded and live rows go straight in. */+ get loaded(): boolean {+ return this.ready && !this.loadFailed;+ }+private setTurn(turn: TurnState | undefined): void {const next = turn ?? 'idle';this.turn = next;@@ -355,6 +369,19 @@ export class AgentSession {* afresh. The old watch is stopped first: its rows belong to a load that* is no longer the one in flight.*/+ /**+ * Read the session row again, for a surface reopening a session that was+ * held open since an earlier view: the fold is current, the row may not+ * be. A refusal forgets the cached log, as a load's does.+ */+ async readRow(): Promise<Result<AgentSessionResponse, 'denied' | 'failed'>> {+ const row = await agentHarnessServiceClient.get(this.id);+ if (row.isOk()) return ok(row.value);+ if (!accessDenied(row.error)) return err('failed');+ this.forgetCached();+ return err('denied');+ }+private reissueReads(): void {this.watch.stop();this.watch = this.openWatch();diff --git a/apps/web/src/lib/core/agent-session/load-telemetry.ts b/apps/web/src/lib/core/agent-session/load-telemetry.tsindex 6f96ea71ae..a92030b639 100644--- a/apps/web/src/lib/core/agent-session/load-telemetry.ts+++ b/apps/web/src/lib/core/agent-session/load-telemetry.ts@@ -32,7 +32,9 @@ export type LoadOutcome =/** The fetch or the fold failed. */| 'failed'/** Still unsettled at {@link STALL_THRESHOLD_MS} — reported, not concluded. */- | 'stalled';+ | 'stalled'+ /** Already loaded when acquired, so nothing was fetched or folded. */+ | 'reused';/** Why a load ended `failed`, bounded so failures group. */export type LoadFailure =@@ -350,7 +352,7 @@ export class SessionLoadTrace {this.#set('agent.session.load.failure', this.#failure ?? 'unknown');}// Nothing will paint for a load that did not land.- if (outcome !== 'loaded') this.#frames?.stop();+ if (outcome !== 'loaded' && outcome !== 'reused') this.#frames?.stop();try {performance.measure('agent.session.load', {start: this.#startedAt,diff --git a/apps/web/src/lib/core/agent-session/recently-viewed.test.ts b/apps/web/src/lib/core/agent-session/recently-viewed.test.tsnew file mode 100644index 0000000000..e88519bc79--- /dev/null+++ b/apps/web/src/lib/core/agent-session/recently-viewed.test.ts@@ -0,0 +1,55 @@+import { beforeEach, describe, expect, it, vi } from 'vitest';++const sessions = vi.hoisted(() => ({ open: new Map<string, number>() }));++vi.mock('./AgentSession', () => ({+ AgentSession: {+ acquire: (id: string) => {+ sessions.open.set(id, (sessions.open.get(id) ?? 0) + 1);+ return {+ release: () => {+ const references = (sessions.open.get(id) ?? 0) - 1;+ if (references > 0) sessions.open.set(id, references);+ else sessions.open.delete(id);+ },+ };+ },+ },+}));++// A fresh module per test, so no test inherits another's held sessions.+let retainRecentlyViewed: (id: string) => void;+beforeEach(async () => {+ vi.resetModules();+ sessions.open.clear();+ ({ retainRecentlyViewed } = await import('./recently-viewed'));+});++describe('retainRecentlyViewed', () => {+ it('holds one acquisition per session however often it is viewed', () => {+ retainRecentlyViewed('a');+ retainRecentlyViewed('a');++ expect(sessions.open.get('a')).toBe(1);+ });++ it('closes the least recently viewed session past eight', () => {+ for (const id of ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']) {+ retainRecentlyViewed(id);+ }+ // Viewing `a` again makes `b` the oldest.+ retainRecentlyViewed('a');+ retainRecentlyViewed('i');++ expect([...sessions.open.keys()].sort()).toEqual([+ 'a',+ 'c',+ 'd',+ 'e',+ 'f',+ 'g',+ 'h',+ 'i',+ ]);+ });+});diff --git a/apps/web/src/lib/core/agent-session/recently-viewed.ts b/apps/web/src/lib/core/agent-session/recently-viewed.tsnew file mode 100644index 0000000000..5d8b5230c9--- /dev/null+++ b/apps/web/src/lib/core/agent-session/recently-viewed.ts@@ -0,0 +1,45 @@+/**+ * Sessions this tab viewed last, kept open after their surface goes away.+ *+ * Switching between agent sessions unmounts one block and mounts another, and+ * the last release closes the session: switching back paid for a cache read,+ * the session row and the whole log again, each a network round trip. Holding+ * one extra acquisition on the few sessions viewed last keeps their fold and+ * their live rows, so a switch back renders what the machine already holds.+ *+ * Only sessions that loaded are held: a failed or refused load must stay+ * retryable from scratch, and an abandoned one has nothing worth keeping.+ */++import { AgentSession } from './AgentSession';++/** How many sessions stay open past their last surface. */+const RETAINED_SESSIONS = 8;++/** Held acquisitions, least recently viewed first. */+const retained = new Map<string, AgentSession>();++/** `id` was just viewed: hold it, and let go of the oldest past the limit. */+export function retainRecentlyViewed(id: string): void {+ const held = retained.get(id);+ retained.delete(id);+ retained.set(id, held ?? AgentSession.acquire(id, 'hold'));+ for (const [oldest, session] of retained) {+ if (retained.size <= RETAINED_SESSIONS) break;+ retained.delete(oldest);+ session.release();+ }+}++/** Stop holding `id`, for a session the viewer may no longer see. */+export function forgetRecentlyViewed(id: string): void {+ const held = retained.get(id);+ if (!held) return;+ retained.delete(id);+ held.release();+}++/** Whether `id` is held open from an earlier view. */+export function isRecentlyViewed(id: string): boolean {+ return retained.has(id);+}