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): append transcript messages without walking settled history (#9425)
Wolf Mermelsteinb8e114ffbf82
diff --git a/apps/web/AGENTS.md b/apps/web/AGENTS.mdindex f3d343a22c..965e0f503a 100644--- a/apps/web/AGENTS.md+++ b/apps/web/AGENTS.md@@ -918,6 +918,14 @@ On a physical iPhone, WKWebView still synthesises the compatibility `mousedown`/Use the existing status dot in Home and Agents rows with `bg-failure` when thepersisted `turnState` is `failed`; open the session to inspect its error.+### Agent transcript projection++Keep folded messages ordered by turn and author in the transcript store: append+at the new index, reconcile matching rows in place, binary-search earlier+updates, and splice only for real insertion/removal. Never `produce` the whole+array per append; it makes initial projection quadratic. Check with the+[session-open runner](src/features/block-agent/browser-test/README.md).+### Agent system eventsAgent activity labels share `block-agent/ui/TextShimmer.tsx` and the globaldiff --git a/apps/web/src/features/block-agent/browser-test/README.md b/apps/web/src/features/block-agent/browser-test/README.mdnew file mode 100644index 0000000000..0906fc221e--- /dev/null+++ b/apps/web/src/features/block-agent/browser-test/README.md@@ -0,0 +1,44 @@+# Agent session browser checks++Run the existing notification, mention and transcript checks with:++```sh+PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH=/path/to/chrome \+bunx playwright test --config src/features/block-agent/browser-test/playwright.config.ts+```++For session-open performance, use a regular owned Vite frontend, an authorized+local session and histories created through the local session API. Keep login+state, session IDs, raw logs and capture artifacts outside the repository. Never+seed a shared database or reuse another person's session to build a workload.++From `apps/web`:++```sh+PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH=/path/to/chrome \+SESSION_STATE=/tmp/test-session.json SESSION_ID=<owned-session-id> \+SESSION_MESSAGES=500 SESSION_OUTPUT=/tmp/session-open-long \+node src/features/block-agent/browser-test/session-open.mjs+```++`SESSION_BASE` defaults to `https://localhost:3008`. Warm the routes/dependencies+before capturing. `SESSION_READY_SELECTOR` overrides the initial Home split+readiness selector (`[data-split-id]`). The runner uses 1440x900 Chrome with 4x CPU+throttling, a 15-second initial settle and five opens separated by Home. The first+open has a fresh browser cache and samples CPU; subsequent opens can use the+persisted GraphQL log. Compare corresponding cache/profiler states. Use+`SESSION_SAMPLES` to change the count.++Results contain existing `agent.session.load.*` measures, navigation admission+time, decoded response bytes/timing, mounted virtual rows, end-scroll geometry,+main-thread CDP durations and main-thread heap after forced GC. The `fold`+phase includes listener projection. Render/paint measures start at session acquisition;+subtract navigation admission separately to include lazy view startup. Virtual+lists render the visible tail, not every message in the folded history.++`SESSION_MESSAGES` optionally verifies that the last row mounts and overflowing+history is pinned to the end. Captures fail on page errors, reloads, failed loads+or missing final rows. Each sample saves a screenshot, the first saves a CPU+profile, and the completed run saves JSON. The 500 ms post-paint observation and+screenshots are outside the load/paint measures; CDP task totals include driver+observation/GC overhead. Main-thread heap does not measure WASM worker memory.diff --git a/apps/web/src/features/block-agent/browser-test/session-open.mjs b/apps/web/src/features/block-agent/browser-test/session-open.mjsnew file mode 100644index 0000000000..968f45d1af--- /dev/null+++ b/apps/web/src/features/block-agent/browser-test/session-open.mjs@@ -0,0 +1,197 @@+import { chromium } from 'playwright';+import fs from 'node:fs';+const id = process.env.SESSION_ID;+const state = process.env.SESSION_STATE;+if (!id || !state) throw new Error('SESSION_ID and SESSION_STATE are required');+const base = process.env.SESSION_BASE ?? 'https://localhost:3008';+const output = process.env.SESSION_OUTPUT ?? '/tmp/agent-session-open';+const browser = await chromium.launch({+ executablePath: process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH,+ args: ['--enable-precise-memory-info'],+});+try {+ const context = await browser.newContext({+ ignoreHTTPSErrors: true,+ viewport: { width: 1440, height: 900 },+ storageState: state,+ });+ const page = await context.newPage();+ const errors = [];+ let loads = 0;+ page.on('pageerror', (e) => {+ errors.push(e.message);+ console.error('pageerror', e.message);+ });+ page.on('load', () => loads++);+ const cdp = await context.newCDPSession(page);+ await cdp.send('Performance.enable');+ await cdp.send('Emulation.setCPUThrottlingRate', { rate: 4 });+ await page.goto(`${base}/app/home`, { waitUntil: 'load', timeout: 120000 });+ await page.waitForSelector(+ process.env.SESSION_READY_SELECTOR ?? '[data-split-id]',+ { timeout: 90000 }+ );+ await page.waitForTimeout(15000);+ const responses = [];+ page.on('response', async (response) => {+ const request = response.request();+ if (request.resourceType() !== 'fetch' && request.resourceType() !== 'xhr')+ return;+ const url = new URL(response.url());+ if (+ !url.pathname.includes('graphql') &&+ !url.pathname.includes('agent-sessions')+ )+ return;+ try {+ const data = await response.body();+ responses.push({+ path: url.pathname,+ operation: request.postDataJSON()?.operationName,+ bytes: data.length,+ timing: request.timing(),+ status: response.status(),+ });+ } catch {}+ });+ const samples = [];+ const initialLoads = loads;+ for (+ let sample = 0;+ sample < Number(process.env.SESSION_SAMPLES ?? 5);+ sample+++ ) {+ await page.evaluate(() => {+ performance.clearMeasures();+ performance.clearMarks();+ window.debugAgentSessionLoad = true;+ });+ responses.length = 0;+ await cdp.send('HeapProfiler.collectGarbage');+ const before = Object.fromEntries(+ (await cdp.send('Performance.getMetrics')).metrics.map((x) => [+ x.name,+ x.value,+ ])+ );+ if (sample === 0) {+ await cdp.send('Profiler.enable');+ await cdp.send('Profiler.start');+ }+ const started = Date.now();+ const navigationStarted = await page.evaluate((id) => {+ const start = performance.now();+ history.pushState({}, '', '/app/agents/' + id);+ dispatchEvent(new PopStateEvent('popstate'));+ return start;+ }, id);+ await page.waitForFunction(+ (id) =>+ performance+ .getEntriesByType('measure')+ .some(+ (x) =>+ x.detail?.sessionId === id &&+ (x.name === 'agent.session.load.painted' ||+ (x.name === 'agent.session.load' &&+ x.detail.outcome === 'failed'))+ ),+ id,+ { timeout: 90000 }+ );+ const failed = await page.evaluate(+ (id) =>+ performance+ .getEntriesByName('agent.session.load')+ .some(+ (x) => x.detail?.sessionId === id && x.detail.outcome === 'failed'+ ),+ id+ );+ if (failed) throw new Error('Session load failed before painting');+ await page.waitForTimeout(500);+ const data = await page.evaluate(() => ({+ measures: performance+ .getEntriesByType('measure')+ .filter((x) => x.name.startsWith('agent.session'))+ .map((x) => ({+ name: x.name,+ start: x.startTime,+ ms: x.duration,+ detail: x.detail,+ })),+ rows: document.querySelectorAll('[data-index]').length,+ scroll: [...document.querySelectorAll('*')]+ .filter(+ (x) =>+ x.scrollHeight > x.clientHeight + 100 &&+ /^(auto|scroll)$/.test(getComputedStyle(x).overflowY)+ )+ .map((x) => ({+ top: x.scrollTop,+ height: x.scrollHeight,+ viewport: x.clientHeight,+ })),+ rowIndexes: [...document.querySelectorAll('[data-index]')].map((x) =>+ Number(x.getAttribute('data-index'))+ ),+ }));+ if (sample === 0) {+ const { profile } = await cdp.send('Profiler.stop');+ fs.writeFileSync(`${output}.cpuprofile`, JSON.stringify(profile));+ }+ await cdp.send('HeapProfiler.collectGarbage');+ const after = Object.fromEntries(+ (await cdp.send('Performance.getMetrics')).metrics.map((x) => [+ x.name,+ x.value,+ ])+ );+ const result = {+ sample,+ navigationStarted,+ wallMs: Date.now() - started,+ ...data,+ responses: [...responses],+ taskMs: 1000 * (after.TaskDuration - before.TaskDuration),+ scriptMs: 1000 * (after.ScriptDuration - before.ScriptDuration),+ styleMs: 1000 * (after.RecalcStyleDuration - before.RecalcStyleDuration),+ heapMB: after.JSHeapUsedSize / 1e6,+ nodes: after.Nodes,+ };+ samples.push(result);+ console.log(JSON.stringify(result));+ const expected = Number(process.env.SESSION_MESSAGES);+ if (expected > 0 && !result.rowIndexes.includes(expected - 1))+ throw new Error('The final transcript row is not mounted');+ if (+ expected > 30 &&+ !result.scroll.some((x) => Math.abs(x.height - x.viewport - x.top) < 2)+ )+ throw new Error('Transcript did not position at the end');+ await page.screenshot({ path: `${output}-${sample}.png` });+ if (errors.length || loads !== initialLoads)+ throw new Error(+ JSON.stringify({ errors, reloads: loads - initialLoads })+ );+ await page.evaluate(() => {+ history.pushState({}, '', '/app/home');+ dispatchEvent(new PopStateEvent('popstate'));+ });+ await page.waitForSelector(+ process.env.SESSION_READY_SELECTOR ?? '[data-split-id]',+ { timeout: 90000 }+ );+ await page.waitForTimeout(1500);+ }+ fs.writeFileSync(+ `${output}.json`,+ JSON.stringify(+ { id, samples, errors, reloads: loads - initialLoads },+ null,+ 2+ )+ );+} finally {+ await browser.close();+}diff --git a/apps/web/src/features/block-agent/browser-test/vite.config.ts b/apps/web/src/features/block-agent/browser-test/vite.config.tsindex d1290f2e52..ec375fbc89 100644--- a/apps/web/src/features/block-agent/browser-test/vite.config.ts+++ b/apps/web/src/features/block-agent/browser-test/vite.config.ts@@ -3,20 +3,23 @@ import tailwindcss from '@tailwindcss/vite';import { defineConfig } from 'vite';import solid from 'vite-plugin-solid';import solidSvg from 'vite-plugin-solid-svg';+import wasm from 'vite-plugin-wasm';import tsconfigPaths from 'vite-tsconfig-paths';const directory = fileURLToPath(new URL('.', import.meta.url));const webDirectory = fileURLToPath(new URL('../../../../', import.meta.url));export default defineConfig({root: directory,+ cacheDir: `${directory}/.vite`,plugins: [tailwindcss(),+ wasm(),solid(),solidSvg({ defaultAsComponent: true }),tsconfigPaths({ projects: [`${webDirectory}/tsconfig.json`] }),],resolve: {- dedupe: ['solid-js'],+ dedupe: ['solid-js', 'loro-crdt'],alias: [{find: '../context/AgentSessionContext',@@ -28,5 +31,9 @@ export default defineConfig({},],},+ optimizeDeps: {+ exclude: ['loro-crdt'],+ esbuildOptions: { target: 'esnext' },+ },server: { host: '127.0.0.1', strictPort: true },});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 1158fe875c..7159d3467a 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@@ -6,9 +6,15 @@* `<Suspense>` on its fallback forever.*/+import type {+ FoldedMessage,+ FoldedStreamEvent,+} from '@service-agent-fold/generated/types';+import type { AgentSessionLogEntryDto } from '@service-agent-harness/generated/schemas';import { err, ok } from 'neverthrow';import { batch, createSignal, Show, Suspense } from 'solid-js';import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import type { AgentSessionHandle } from './create-agent-session';const readiness = vi.hoisted(() => ({ painted: vi.fn(), cancelled: vi.fn() }));vi.mock('@app/observability/ui-operation', async (importOriginal) => {@@ -219,3 +225,110 @@ it('settles view readiness when provisioning fails without a session id', asyncawait Promise.resolve();expect(readiness.painted).toHaveBeenCalledOnce();});++it('appends without traversing settled history and preserves keyed rows across updates and rebases', async () => {+ const firstParts = [{ kind: 'text' as const, text: 'settled' }];+ let traversals = 0;+ const first = new Proxy<FoldedMessage>(+ {+ agentSessionId: SESSION,+ turn: 0,+ author: { kind: 'agent' },+ requestId: null,+ parts: firstParts,+ stop: null,+ pending: false,+ segments: [],+ phase: null,+ postedAt: null,+ },+ {+ ownKeys(target) {+ traversals++;+ return Reflect.ownKeys(target);+ },+ }+ );+ fold.readSession.mockResolvedValue({+ messages: [first],+ metadata: { turn: 'idle' },+ });+ let handle: AgentSessionHandle | undefined;+ function TranscriptState() {+ handle = createAgentSession(() => SESSION, {+ userId: () => 'me',+ provisioning: () => false,+ });+ return <h1>{handle.session()?.name}</h1>;+ }+ render(() => (+ <Suspense fallback={null}>+ <TranscriptState />+ </Suspense>+ ));+ await screen.findByText('A session');+ if (!handle) throw new Error('session was not mounted');+ const messages = handle.messages;+ const shared = AgentSession.get(SESSION);+ if (!shared) throw new Error('session was not acquired');+ const row: AgentSessionLogEntryDto = {+ id: 'row',+ direction: 'to_server',+ createdAt: '2026-01-01T00:00:00Z',+ content: { type: 'event', event: 'acp_ready' },+ };+ const publish = async (events: FoldedStreamEvent[]) => {+ fold.pushSession.mockResolvedValueOnce(events);+ AgentSession.ingest({ agentSessionId: SESSION, entries: [row] });+ await shared.snapshot();+ };+ const settled = messages()[0];+ traversals = 0;+ const tail: FoldedMessage = {+ ...first,+ turn: 2,+ parts: [{ kind: 'text', text: 'tail' }],+ };+ // Count only traversal by the projection, excluding construction above.+ traversals = 0;+ await publish([{ kind: 'new', message: tail }]);+ expect(traversals).toBe(0);+ expect(messages()[0]).toBe(settled);+ const tailIdentity = messages()[1];++ const middle: FoldedMessage = {+ ...tail,+ turn: 1,+ author: { kind: 'user', userId: 'me' },+ };+ const reply: FoldedMessage = {+ ...middle,+ author: { kind: 'agent' },+ };+ await publish([+ { kind: 'new', message: reply },+ { kind: 'new', message: middle },+ {+ kind: 'update',+ message: { ...tail, parts: [{ kind: 'text', text: 'updated' }] },+ },+ ]);+ expect(+ messages().map((message) => [message.turn, message.author.kind])+ ).toEqual([+ [0, 'agent'],+ [1, 'user'],+ [1, 'agent'],+ [2, 'agent'],+ ]);+ expect(messages()[0]).toBe(settled);+ expect(messages()[3]).toBe(tailIdentity);+ const middleIdentity = messages()[1];+ await publish([+ { kind: 'replace', messages: [middle, { ...tail, pending: true }] },+ ]);+ expect(messages().map((message) => message.turn)).toEqual([1, 2]);+ expect(messages()[0]).toBe(middleIdentity);+ expect(messages()[1]).toBe(tailIdentity);+ expect(messages()[1]?.pending).toBe(true);+});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 30fbeeac07..79a4d941c4 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@@ -142,18 +142,24 @@ export function createAgentSession(const upsert = (messages: FoldedMessage[]) =>batch(() => {for (const message of messages) {- // The list is short and appends dominate, so scan from the tail- // rather than binary-searching.- let index = list.length - 1;- while (index >= 0 && compareMessages(list[index]!, message) > 0) {- index--;+ // The fold publishes ordered snapshots as well as individual updates.+ // Find the row without scanning settled history for each snapshot row.+ let start = 0;+ let end = list.length;+ while (start < end) {+ const middle = (start + end) >>> 1;+ if (compareMessages(list[middle]!, message) < 0) start = middle + 1;+ else end = middle;}- if (index >= 0 && sameMessage(list[index]!, message)) {- // Path-scoped reconcile: a streaming turn replaces its message- // hundreds of times; only the changed content re-renders.- setList(index, reconcile(message));+ if (start < list.length && sameMessage(list[start]!, message)) {+ // Path-scoped reconcile preserves settled rows and streaming parts.+ setList(start, reconcile(message));+ } else if (start === list.length) {+ // Producing the whole array unwraps all earlier messages on every+ // append. Write only the new index, including during initial load.+ setList(start, message);} else {- const at = index + 1;+ const at = start;setList(produce((current: FoldedMessage[]) =>current.splice(at, 0, message)