Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

test(desktop): benchmark chat:getIndex, setLastActive and loadMessages across versions

Alfredo Lopeze0df3edc73ec
diff --git a/desktop-app/scripts/bench-chat-open.mjs b/desktop-app/scripts/bench-chat-open.mjs
new file mode 100644
index 000000000..660c86e3a
--- /dev/null
+++ b/desktop-app/scripts/bench-chat-open.mjs
@@ -0,0 +1,359 @@
+#!/usr/bin/env node
+/**
+ * ChatStore chat-open benchmark (issue #1091, plan sections 7.1 and 11).
+ *
+ * Run from desktop-app with Node 24 and installed HEAD dependencies:
+ * node scripts/bench-chat-open.mjs --quick --versions v0.10.0,v0.11.0 --out /tmp/chat-open.json
+ * node scripts/bench-chat-open.mjs --out /tmp/chat-open-full.json
+ *
+ * --versions selects any comma-separated subset of v0.10.0,v0.11.0,HEAD.
+ * --quick uses 5 agents x 20 chats; both pending-cleanup values and the permanent
+ * EACCES scenario still run, with 30 fresh-bind repetitions each. The full matrix
+ * is {5,25,100} agents x {20,200,1000} chats PER AGENT x {0,10} cleanups PER AGENT.
+ * A successful cleanup and a persistent filesystem failure are separate scenarios.
+ *
+ * Historical sources are extracted using git archive -> tar, made read-only and
+ * compiled with HEAD's node_modules/.bin/tsc into os.tmpdir(). HEAD snapshots the
+ * live worktree (including uncommitted changes); its source hashes and
+ * commit are recorded. No checkout, dependency installation or Electron launch.
+ * The emitted runtime graph is audited before import. We bypass only Electron's
+ * binding module: a fresh ChatStore + setAuthorityScope reproduces the completed
+ * bind; HEAD's actual compiled runChatGetIndex implements the measured handler.
+ *
+ * Fixtures contain synthetic catalogs and valid transcripts written by the
+ * version's own ChatStore. Only the opened chats have nonempty transcripts.
+ * Each repetition uses a fresh mkdtemp directory and store; setup, assertions and
+ * teardown are outside timing. Data is never written to user configuration dirs.
+ * "First" means first call after bind, not an OS-cold disk read (seeding warms
+ * the filesystem cache). Full/partial pages return exactly 100/37 messages.
+ * Extra warm-page stages repeat those reads on the same store. p50/p95 use the
+ * nearest-rank estimator and retain all raw milliseconds for audit.
+ *
+ * Pre-cleanup versions retain the same additive pending fields/artifacts but do
+ * not implement cleanup; this is explicit in JSON, never simulated as a feature.
+ * Failure injection targets only fixture .corrupt readdir calls (EACCES), matching
+ * chatStore.test.ts:305. Numeric failure/warning witnesses accompany retained
+ * queues/artifacts. An unmeasured real delete + fail + retry proves the model.
+ * Imported historical warnings are counted without logging IDs or error text.
+ * Warm stage-p95 sums and paired warm-open p95 are both reported; this Node
+ * benchmark does not measure the renderer chat-open time.
+ *
+ * --out defaults to a unique JSON path under os.tmpdir(). The selected output is
+ * exclusively created; an existing file is an error. JSON is also printed to
+ * stdout. Sources/data are removed even on failure; the JSON remains for review.
+ */
+import assert from 'node:assert/strict'
+import { promises as fs } from 'node:fs'
+import { arch, cpus, loadavg, platform, tmpdir } from 'node:os'
+import { basename, dirname, join, resolve } from 'node:path'
+import { performance } from 'node:perf_hooks'
+import { fileURLToPath, pathToFileURL } from 'node:url'
+import {
+ FULL_CHAT,
+ FULL_MESSAGES,
+ PAGE_SIZE,
+ PARTIAL_CHAT,
+ PARTIAL_MESSAGES,
+ PARTIAL_SIZE,
+ TARGET_AGENT,
+ captureFilesystemFailures,
+ createFixture,
+ inspectFixture,
+ verifyFailingFilesystemModel,
+} from './bench-chat-open/fixtures.mjs'
+import { compileVersion, git, setSourcePermissions } from './bench-chat-open/sources.mjs'
+
+export const REPETITIONS = 30
+const ADAPTER_FILES = new Map([
+ ['v0.10.0', 'v0.10.0.mjs'],
+ ['v0.11.0', 'v0.11.0.mjs'],
+ ['HEAD', 'head.mjs'],
+])
+
+export function parseOptions(args) {
+ const options = { versions: [...ADAPTER_FILES.keys()], quick: false, out: undefined, help: false }
+ for (let index = 0; index < args.length; index += 1) {
+ const arg = args[index]
+ if (arg === '--quick') options.quick = true
+ else if (arg === '--help') options.help = true
+ else if (arg === '--versions' || arg === '--out') {
+ const value = args[++index]
+ if (!value || value.startsWith('--')) throw new Error(`${arg} requires a value`)
+ if (arg === '--out') options.out = resolve(value)
+ else {
+ const versions = value.split(',').map(version => version.trim())
+ if (
+ versions.some(version => !ADAPTER_FILES.has(version)) ||
+ new Set(versions).size !== versions.length
+ ) {
+ throw new Error('--versions must contain unique supported refs')
+ }
+ options.versions = versions
+ }
+ } else throw new Error(`Unknown option: ${arg}`)
+ }
+ return options
+}
+
+export function matrix(quick) {
+ const cases = []
+ for (const agents of quick ? [5] : [5, 25, 100]) {
+ for (const chatsPerAgent of quick ? [20] : [20, 200, 1000]) {
+ for (const pendingCleanupsPerAgent of [0, 10]) {
+ for (const cleanupFilesystem of pendingCleanupsPerAgent
+ ? ['healthy', 'EACCES']
+ : ['healthy']) {
+ cases.push({ agents, chatsPerAgent, pendingCleanupsPerAgent, cleanupFilesystem })
+ }
+ }
+ }
+ }
+ return cases
+}
+
+export function distribution(values) {
+ assert.ok(values.length > 0 && values.every(value => Number.isFinite(value) && value >= 0))
+ const sorted = [...values].sort((left, right) => left - right)
+ return {
+ samples: values.length,
+ p50Ms: sorted[Math.ceil(sorted.length * 0.5) - 1],
+ p95Ms: sorted[Math.ceil(sorted.length * 0.95) - 1],
+ samplesMs: values,
+ }
+}
+
+async function measure(operation) {
+ const start = performance.now()
+ const value = await operation()
+ return { ms: performance.now() - start, value }
+}
+
+async function runScenario(runDir, adapter, compiled, dimensions) {
+ const stageSamples = Object.fromEntries(
+ [
+ 'firstGetIndexAfterBind',
+ 'nextGetIndex',
+ 'setLastActive',
+ 'loadMessagesFullPage',
+ 'loadMessagesPartialPage',
+ 'loadMessagesFullPageWarm',
+ 'loadMessagesPartialPageWarm',
+ 'chatOpenWarm',
+ ].map(stage => [stage, []])
+ )
+ const witnesses = []
+ const initialLoad = loadavg()
+ for (let repetition = 0; repetition < REPETITIONS; repetition += 1) {
+ const fixture = await createFixture({
+ runDir,
+ adapter,
+ ChatStore: compiled.ChatStore,
+ ...dimensions,
+ })
+ const capture = captureFilesystemFailures(
+ fixture.root,
+ dimensions.cleanupFilesystem === 'EACCES'
+ )
+ try {
+ const store = adapter.bind(compiled.ChatStore, fixture.root)
+ const context = compiled.context(store)
+ const first = await measure(() => context.getIndex(TARGET_AGENT))
+ assert.equal(first.value.chats.length, dimensions.chatsPerAgent)
+ const next = await measure(() => context.getIndex(TARGET_AGENT))
+ assert.equal(next.value.chats.length, dimensions.chatsPerAgent)
+ const active = await measure(() => store.setLastActiveChatId(TARGET_AGENT, FULL_CHAT))
+ const full = await measure(() => store.loadMessages(TARGET_AGENT, FULL_CHAT, PAGE_SIZE, 0))
+ assert.deepEqual(full.value, FULL_MESSAGES)
+ const partial = await measure(() =>
+ store.loadMessages(TARGET_AGENT, PARTIAL_CHAT, PAGE_SIZE, 0)
+ )
+ assert.deepEqual(partial.value, PARTIAL_MESSAGES)
+ const fullWarm = await measure(() =>
+ store.loadMessages(TARGET_AGENT, FULL_CHAT, PAGE_SIZE, 0)
+ )
+ assert.deepEqual(fullWarm.value, FULL_MESSAGES)
+ const partialWarm = await measure(() =>
+ store.loadMessages(TARGET_AGENT, PARTIAL_CHAT, PAGE_SIZE, 0)
+ )
+ assert.deepEqual(partialWarm.value, PARTIAL_MESSAGES)
+ const values = [
+ first.ms,
+ next.ms,
+ active.ms,
+ full.ms,
+ partial.ms,
+ fullWarm.ms,
+ partialWarm.ms,
+ first.ms + active.ms + fullWarm.ms,
+ ]
+ Object.keys(stageSamples).forEach((stage, index) => stageSamples[stage].push(values[index]))
+ if (adapter.ref === 'HEAD') await context.settle()
+ const state = await inspectFixture(fixture, dimensions.chatsPerAgent)
+ const pendingTotal = dimensions.agents * dimensions.pendingCleanupsPerAgent
+ if (!adapter.handlesPendingCleanups || dimensions.cleanupFilesystem === 'EACCES') {
+ assert.equal(state.pending, pendingTotal)
+ assert.equal(state.retainedArtifactDirectories, pendingTotal)
+ } else if (adapter.ref === 'v0.11.0') {
+ assert.equal(state.pending, 0)
+ assert.equal(state.retainedArtifactDirectories, 0)
+ }
+ if (adapter.handlesPendingCleanups && dimensions.cleanupFilesystem === 'EACCES') {
+ assert.ok(capture.counters.filesystemFailures > 0)
+ assert.ok(capture.counters.warnings > 0)
+ }
+ witnesses.push({
+ repetition,
+ ...state,
+ ...capture.counters,
+ cleanupErrors: context.cleanupErrors,
+ catalogBytes: fixture.catalogBytes,
+ indexChats: first.value.chats.length,
+ fullPageMessages: full.value.length,
+ partialPageMessages: partial.value.length,
+ })
+ } finally {
+ capture.restore()
+ await fs.rm(fixture.root, { recursive: true, force: true })
+ }
+ }
+ const stages = Object.fromEntries(
+ Object.entries(stageSamples).map(([stage, samples]) => [stage, distribution(samples)])
+ )
+ return {
+ ...dimensions,
+ repetitions: REPETITIONS,
+ loadavgBefore: initialLoad,
+ loadavgAfter: loadavg(),
+ stages,
+ warmStageP95SumMs:
+ stages.firstGetIndexAfterBind.p95Ms +
+ stages.setLastActive.p95Ms +
+ stages.loadMessagesFullPageWarm.p95Ms,
+ witnesses,
+ }
+}
+
+export function comparisons(results) {
+ const baseline = results.find(result => result.version === 'v0.10.0')
+ if (!baseline) return []
+ const key = scenario =>
+ JSON.stringify([
+ scenario.agents,
+ scenario.chatsPerAgent,
+ scenario.pendingCleanupsPerAgent,
+ scenario.cleanupFilesystem,
+ ])
+ const byScenario = new Map(baseline.scenarios.map(scenario => [key(scenario), scenario]))
+ return results
+ .filter(result => result !== baseline)
+ .flatMap(result =>
+ result.scenarios.map(scenario => {
+ const reference = byScenario.get(key(scenario))
+ assert.ok(reference && reference.warmStageP95SumMs > 0)
+ return {
+ version: result.version,
+ agents: scenario.agents,
+ chatsPerAgent: scenario.chatsPerAgent,
+ pendingCleanupsPerAgent: scenario.pendingCleanupsPerAgent,
+ cleanupFilesystem: scenario.cleanupFilesystem,
+ baselineWarmStageP95SumMs: reference.warmStageP95SumMs,
+ warmStageP95SumMs: scenario.warmStageP95SumMs,
+ changePercent: (scenario.warmStageP95SumMs / reference.warmStageP95SumMs - 1) * 100,
+ withinTenPercent: scenario.warmStageP95SumMs <= reference.warmStageP95SumMs * 1.1,
+ }
+ })
+ )
+}
+
+export async function main(args = process.argv.slice(2)) {
+ const options = parseOptions(args)
+ if (options.help) {
+ process.stdout.write(
+ 'Usage: node scripts/bench-chat-open.mjs [--quick] [--versions v0.10.0,v0.11.0,HEAD] [--out path]\n'
+ )
+ return
+ }
+ if (Number(process.versions.node.split('.')[0]) !== 24)
+ throw new Error('This benchmark requires Node 24')
+ if (process.env.EVENFIRE_CHAT_SPANS_FILE)
+ throw new Error('Unset EVENFIRE_CHAT_SPANS_FILE for comparable timing')
+ const desktopRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..')
+ const repoRoot = git(desktopRoot, ['rev-parse', '--show-toplevel'])
+ const runDir = await fs.mkdtemp(join(tmpdir(), 'evenfire-chat-open-'))
+ const out = options.out ?? join(tmpdir(), `${basename(runDir)}.json`)
+ const started = performance.now()
+ try {
+ const prepared = []
+ for (const ref of options.versions) {
+ const { default: adapter } = await import(`./bench-chat-open/${ADAPTER_FILES.get(ref)}`)
+ const compiled = await compileVersion({ adapter, repoRoot, desktopRoot, runDir })
+ prepared.push({ adapter, compiled })
+ }
+ const results = []
+ for (const { adapter, compiled } of prepared) {
+ process.stderr.write(
+ `Benchmark ${adapter.ref}: ${matrix(options.quick).length} scenarios x ${REPETITIONS} repetitions\n`
+ )
+ const failingFilesystemModel = await verifyFailingFilesystemModel({
+ runDir,
+ adapter,
+ ChatStore: compiled.ChatStore,
+ })
+ const scenarios = []
+ for (const dimensions of matrix(options.quick)) {
+ scenarios.push(await runScenario(runDir, adapter, compiled, dimensions))
+ }
+ results.push({ ...compiled.metadata, failingFilesystemModel, scenarios })
+ }
+ const cpuInfo = cpus()
+ assert.ok(cpuInfo.length > 0)
+ const summary = {
+ schemaVersion: 1,
+ createdAt: new Date().toISOString(),
+ nodeVersion: process.version,
+ machine: {
+ platform: platform(),
+ arch: arch(),
+ cpuModel: cpuInfo[0].model,
+ logicalCpus: cpuInfo.length,
+ loadavg: loadavg(),
+ },
+ quick: options.quick,
+ repetitions: REPETITIONS,
+ pageSize: PAGE_SIZE,
+ partialPageMessages: PARTIAL_SIZE,
+ percentileMethod: 'nearest-rank',
+ durationMs: performance.now() - started,
+ out,
+ results,
+ comparisons: comparisons(results),
+ }
+ const json = `${JSON.stringify(summary, null, 2)}\n`
+ await fs.writeFile(out, json, { flag: 'wx', mode: 0o600 })
+ process.stdout.write(json)
+ } finally {
+ // A compile/verification failure can leave a read-only source snapshot.
+ for (const entry of await fs.readdir(runDir, { withFileTypes: true })) {
+ if (entry.isDirectory() && entry.name.startsWith('source-')) {
+ const root = join(runDir, entry.name, 'snapshot')
+ await setSourcePermissions(root, false)
+ }
+ }
+ await fs.rm(runDir, { recursive: true, force: true })
+ }
+}
+
+if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) {
+ main().catch(error => {
+ // Keep arbitrary imported error text and identifiers out of benchmark logs.
+ process.stderr.write(
+ `${JSON.stringify({
+ benchmark: 'chat-open',
+ outcome: 'error',
+ code: error.code ?? error.name,
+ commandExit: error.status ?? null,
+ })}\n`
+ )
+ process.exitCode = 1
+ })
+}
diff --git a/desktop-app/scripts/bench-chat-open/benchmark.test.mjs b/desktop-app/scripts/bench-chat-open/benchmark.test.mjs
new file mode 100644
index 000000000..1631e63a2
--- /dev/null
+++ b/desktop-app/scripts/bench-chat-open/benchmark.test.mjs
@@ -0,0 +1,131 @@
+import assert from 'node:assert/strict'
+import { promises as fs } from 'node:fs'
+import { createRequire } from 'node:module'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { test } from 'node:test'
+import { REPETITIONS, distribution, matrix, parseOptions } from '../bench-chat-open.mjs'
+import { captureFilesystemFailures } from './fixtures.mjs'
+import { AUTHORITY_SCOPE } from './fixtures.mjs'
+import head from './head.mjs'
+import { auditRuntime } from './sources.mjs'
+import v011 from './v0.11.0.mjs'
+
+const ts = createRequire(import.meta.url)('typescript')
+
+test('quick retains both cleanup populations and the failing-filesystem scenario with 30 repetitions', () => {
+ assert.equal(REPETITIONS, 30)
+ assert.equal(matrix(false).length, 27)
+ assert.deepEqual(matrix(true), [
+ { agents: 5, chatsPerAgent: 20, pendingCleanupsPerAgent: 0, cleanupFilesystem: 'healthy' },
+ { agents: 5, chatsPerAgent: 20, pendingCleanupsPerAgent: 10, cleanupFilesystem: 'healthy' },
+ { agents: 5, chatsPerAgent: 20, pendingCleanupsPerAgent: 10, cleanupFilesystem: 'EACCES' },
+ ])
+})
+
+test('version restriction preserves historical refs and rejects duplicates or command-like refs', () => {
+ assert.deepEqual(parseOptions(['--versions', 'v0.11.0,v0.10.0', '--quick']).versions, [
+ 'v0.11.0',
+ 'v0.10.0',
+ ])
+ assert.throws(() => parseOptions(['--versions', 'v0.10.0,v0.10.0']))
+ assert.throws(() => parseOptions(['--versions', '--help']))
+ assert.throws(() => parseOptions(['--versions', 'origin/dev']))
+})
+
+test('nearest-rank p95 uses the 29th of 30 observations and rejects missing/nonfinite samples', () => {
+ const samples = Array.from({ length: 30 }, (_, index) => 30 - index)
+ assert.deepEqual(distribution(samples), { samples: 30, p50Ms: 15, p95Ms: 29, samplesMs: samples })
+ assert.throws(() => distribution([]))
+ assert.throws(() => distribution([0, NaN]))
+})
+
+test('runtime audit accepts macOS temporary-path aliases and blocks escaping or Electron imports', async () => {
+ const root = await fs.mkdtemp(join(tmpdir(), 'chat-open-audit-test-'))
+ try {
+ const compiled = join(root, 'compiled')
+ await fs.mkdir(compiled)
+ const entry = join(compiled, 'entry.js')
+ await fs.writeFile(entry, "require('node:fs'); require('./helper.js')\n")
+ await fs.writeFile(join(compiled, 'helper.js'), "require('node:path')\n")
+ const runtime = await auditRuntime(ts, compiled, [entry])
+ assert.equal(runtime.files.length, 2)
+ assert.ok(runtime.imports.includes('node:fs'))
+ assert.ok(runtime.imports.includes('node:path'))
+ await fs.writeFile(join(root, 'outside.js'), "require('node:os')\n")
+ await assert.rejects(auditRuntime(ts, compiled, [join(root, 'outside.js')]), /escapes/)
+ await fs.symlink(join(root, 'outside.js'), join(compiled, 'outside.js'))
+ await fs.writeFile(entry, "require('./outside.js')\n")
+ await assert.rejects(auditRuntime(ts, compiled, [entry]), /escapes/)
+ await fs.writeFile(entry, "require('electron')\n")
+ await assert.rejects(auditRuntime(ts, compiled, [entry]), /Non-Node runtime dependency/)
+ } finally {
+ await fs.rm(root, { recursive: true, force: true })
+ }
+})
+
+test('filesystem fault is confined to fixture cleanup, is witnessed, and restores the real filesystem', async () => {
+ const root = await fs.mkdtemp(join(tmpdir(), 'chat-open-fs-test-'))
+ const original = fs.readdir
+ try {
+ const corrupt = join(root, 'agent', '.corrupt')
+ await fs.mkdir(corrupt, { recursive: true })
+ await fs.writeFile(join(corrupt, 'proof'), 'synthetic')
+ const capture = captureFilesystemFailures(root, true)
+ try {
+ assert.ok((await fs.readdir(root)).includes('agent'))
+ await assert.rejects(fs.readdir(corrupt), { code: 'EACCES' })
+ assert.equal(capture.counters.filesystemFailures, 1)
+ } finally {
+ capture.restore()
+ }
+ assert.equal(fs.readdir, original)
+ assert.deepEqual(await fs.readdir(corrupt), ['proof'])
+ } finally {
+ await fs.rm(root, { recursive: true, force: true })
+ }
+})
+
+test('v0.11.0 adapter reports a cleanup error and still reads the catalog in handler order', async () => {
+ const calls = []
+ const context = v011.context({
+ async retryPendingDeleteCleanups() {
+ calls.push('cleanup')
+ throw Object.assign(new Error('Synthetic denial'), { code: 'EACCES' })
+ },
+ async getIndex() {
+ calls.push('index')
+ return { chats: [{}] }
+ },
+ })
+ assert.equal((await context.getIndex('synthetic')).chats.length, 1)
+ assert.deepEqual(calls, ['cleanup', 'index'])
+ assert.equal(context.cleanupErrors, 1)
+})
+
+test('HEAD delegates to the supplied compiled export and drains cleanup only after measurement', async () => {
+ const calls = []
+ const store = {
+ async retryPendingDeleteCleanups(scope) {
+ assert.deepEqual(scope, AUTHORITY_SCOPE)
+ calls.push('settle')
+ },
+ }
+ const result = { chats: [1] }
+ // This distinct behavior witnesses delegation to the compiled export instead
+ // of an adapter-owned copy of a historical store sequence.
+ const runChatGetIndex = async (deps, agentRef) => {
+ calls.push('export')
+ assert.equal(deps.store, store)
+ assert.equal(agentRef, 'synthetic')
+ assert.deepEqual(deps.getChatDeletionFenceAuthority(), { authorityScope: AUTHORITY_SCOPE })
+ deps.warnCleanupFailure(new Error('Synthetic warning'))
+ return result
+ }
+ const context = head.context(store, { runChatGetIndex })
+ assert.equal(await context.getIndex('synthetic'), result)
+ assert.deepEqual(calls, ['export'])
+ assert.equal(context.cleanupErrors, 1)
+ await context.settle()
+ assert.deepEqual(calls, ['export', 'settle'])
+})
diff --git a/desktop-app/scripts/bench-chat-open/fixtures.mjs b/desktop-app/scripts/bench-chat-open/fixtures.mjs
new file mode 100644
index 000000000..baee73f2e
--- /dev/null
+++ b/desktop-app/scripts/bench-chat-open/fixtures.mjs
@@ -0,0 +1,227 @@
+import assert from 'node:assert/strict'
+import { promises as fs } from 'node:fs'
+import { join, sep } from 'node:path'
+
+export const AUTHORITY_SCOPE = Object.freeze({
+ environmentKey: 'benchmark-environment',
+ userId: 'benchmark-user',
+ teamId: 'benchmark-team',
+})
+export const TARGET_AGENT = 'agent-000'
+export const FULL_CHAT = 'chat-0000'
+export const PARTIAL_CHAT = 'chat-0001'
+export const PAGE_SIZE = 100
+export const PARTIAL_SIZE = 37
+
+function messages(count) {
+ return Array.from({ length: count }, (_, index) => ({
+ id: `message-${index}`,
+ role: index % 2 === 0 ? 'user' : 'assistant',
+ content: 'Synthetic benchmark message.',
+ timestamp: 1_700_000_000_000 + index,
+ }))
+}
+
+export const FULL_MESSAGES = messages(PAGE_SIZE)
+export const PARTIAL_MESSAGES = messages(PARTIAL_SIZE)
+
+export function captureFilesystemFailures(root, failCleanup) {
+ const originalReaddir = fs.readdir
+ const originalWarn = console.warn
+ const counters = { filesystemFailures: 0, warnings: 0 }
+ // Imported historical code logs identifiers on cleanup failure. Count those
+ // diagnostics without copying their identifiers or error text into bench logs.
+ console.warn = () => {
+ counters.warnings += 1
+ }
+ fs.readdir = function (path, ...args) {
+ if (
+ failCleanup &&
+ typeof path === 'string' &&
+ path.startsWith(`${root}${sep}`) &&
+ path.endsWith(`${sep}.corrupt`)
+ ) {
+ counters.filesystemFailures += 1
+ return Promise.reject(
+ Object.assign(new Error('Synthetic filesystem denial'), { code: 'EACCES' })
+ )
+ }
+ return originalReaddir.call(this, path, ...args)
+ }
+ return {
+ counters,
+ restore() {
+ fs.readdir = originalReaddir
+ console.warn = originalWarn
+ },
+ }
+}
+
+export async function createFixture({
+ runDir,
+ adapter,
+ ChatStore,
+ agents,
+ chatsPerAgent,
+ pendingCleanupsPerAgent,
+}) {
+ const root = await fs.mkdtemp(join(runDir, 'data-'))
+ const agentNames = Array.from(
+ { length: agents },
+ (_, index) => `agent-${String(index).padStart(3, '0')}`
+ )
+ const tombstones = Array.from({ length: pendingCleanupsPerAgent }, (_, index) => ({
+ chatId: `deleted-${index}`,
+ authorityScope: { ...AUTHORITY_SCOPE },
+ }))
+ const chats = Array.from({ length: chatsPerAgent }, (_, index) => ({
+ id: `chat-${String(index).padStart(4, '0')}`,
+ title: 'Synthetic benchmark chat',
+ createdAt: '2024-01-01T00:00:00.000Z',
+ updatedAt: '2024-01-01T00:00:00.000Z',
+ messageCount: index === 0 ? PAGE_SIZE : index === 1 ? PARTIAL_SIZE : 0,
+ errorCount: 0,
+ toolCallCount: 0,
+ }))
+ const index = {
+ version: 2,
+ lastActiveChatId: PARTIAL_CHAT,
+ onboardingDismissed: false,
+ chats,
+ ...(tombstones.length
+ ? { deletedChatTombstones: tombstones, pendingChatCleanup: tombstones }
+ : {}),
+ }
+ const catalog = JSON.stringify(index, null, 2)
+ const emptyChatCatalog = JSON.stringify(
+ {
+ ...index,
+ chats: chats.map(chat => ({ ...chat, messageCount: 0 })),
+ },
+ null,
+ 2
+ )
+ for (const agent of agentNames) {
+ const agentDir = join(root, agent)
+ await fs.mkdir(join(agentDir, '.corrupt'), { recursive: true, mode: 0o700 })
+ await fs.writeFile(
+ join(agentDir, 'index.json'),
+ agent === TARGET_AGENT ? catalog : emptyChatCatalog,
+ { mode: 0o600 }
+ )
+ for (const tombstone of tombstones) {
+ const chatDir = join(agentDir, 'chats', tombstone.chatId)
+ await fs.mkdir(chatDir, { recursive: true, mode: 0o700 })
+ await fs.writeFile(
+ join(chatDir, 'meta.json'),
+ JSON.stringify({
+ version: 1,
+ chatId: tombstone.chatId,
+ pageSize: PAGE_SIZE,
+ messageCount: 0,
+ localMessageCount: 0,
+ pages: [],
+ }),
+ { mode: 0o600 }
+ )
+ await fs.writeFile(
+ join(agentDir, `${tombstone.chatId}.json`),
+ JSON.stringify({
+ version: 2,
+ chatId: tombstone.chatId,
+ messages: [],
+ }),
+ { mode: 0o600 }
+ )
+ }
+ }
+ // Use each version's own writer for valid page/compatibility metadata. Other
+ // catalog entries are empty chats: their transcripts are not opened by this bench.
+ const seedStore = adapter.bind(ChatStore, root)
+ await seedStore.saveMessages(TARGET_AGENT, FULL_CHAT, FULL_MESSAGES)
+ await seedStore.saveMessages(TARGET_AGENT, PARTIAL_CHAT, PARTIAL_MESSAGES)
+ // Seeding updates activity/counts. Restore identical catalogs before a fresh bind.
+ await fs.writeFile(join(root, TARGET_AGENT, 'index.json'), catalog, { mode: 0o600 })
+ assert.equal(
+ JSON.parse(await fs.readFile(join(root, TARGET_AGENT, 'index.json'), 'utf8')).chats.length,
+ chatsPerAgent
+ )
+ if (tombstones.length) {
+ assert.ok(
+ (await fs.stat(join(root, TARGET_AGENT, 'chats', tombstones[0].chatId))).isDirectory()
+ )
+ }
+ return { root, agentNames, tombstones, catalogBytes: Buffer.byteLength(catalog) }
+}
+
+export async function inspectFixture(fixture, chatsPerAgent) {
+ let pending = 0
+ let retainedArtifactDirectories = 0
+ for (const agent of fixture.agentNames) {
+ const agentDir = join(fixture.root, agent)
+ const index = JSON.parse(await fs.readFile(join(agentDir, 'index.json'), 'utf8'))
+ assert.equal(index.chats.length, chatsPerAgent)
+ assert.deepEqual(index.deletedChatTombstones ?? [], fixture.tombstones)
+ pending += (index.pendingChatCleanup ?? []).length
+ for (const tombstone of fixture.tombstones) {
+ try {
+ assert.ok((await fs.stat(join(agentDir, 'chats', tombstone.chatId))).isDirectory())
+ retainedArtifactDirectories += 1
+ } catch (error) {
+ if (error.code !== 'ENOENT') throw error
+ }
+ }
+ }
+ const targetIndex = JSON.parse(
+ await fs.readFile(join(fixture.root, TARGET_AGENT, 'index.json'), 'utf8')
+ )
+ assert.equal(targetIndex.lastActiveChatId, FULL_CHAT)
+ return { pending, retainedArtifactDirectories }
+}
+
+export async function verifyFailingFilesystemModel({ runDir, adapter, ChatStore }) {
+ if (!adapter.handlesPendingCleanups) {
+ return { supported: false, reason: 'This version predates durable pending-delete cleanup.' }
+ }
+ const root = await fs.mkdtemp(join(runDir, 'cleanup-proof-'))
+ const store = adapter.bind(ChatStore, root)
+ const chatId = 'cleanup-proof'
+ const artifact = join(root, TARGET_AGENT, 'chats', chatId)
+ try {
+ await store.createChat(TARGET_AGENT, chatId)
+ await store.saveMessages(TARGET_AGENT, chatId, PARTIAL_MESSAGES)
+ assert.deepEqual(await store.loadMessages(TARGET_AGENT, chatId), PARTIAL_MESSAGES)
+ assert.ok((await fs.stat(artifact)).isDirectory())
+ // chatStore.test.ts:305-335: EACCES from readdir during delete leaves the
+ // tombstone + cleanup queue and real artifacts; a subsequent retry removes them.
+ const failure = captureFilesystemFailures(root, true)
+ let failedAttempts
+ try {
+ assert.deepEqual(await store.deleteChat(TARGET_AGENT, chatId, AUTHORITY_SCOPE), {
+ cleanupPending: true,
+ })
+ assert.equal((await store.getIndex(TARGET_AGENT)).pendingChatCleanup.length, 1)
+ assert.ok((await fs.stat(artifact)).isDirectory())
+ await store.retryPendingDeleteCleanups(AUTHORITY_SCOPE, TARGET_AGENT)
+ assert.equal((await store.getIndex(TARGET_AGENT)).pendingChatCleanup.length, 1)
+ failedAttempts = { ...failure.counters }
+ assert.ok(failedAttempts.filesystemFailures >= 2)
+ assert.ok(failedAttempts.warnings > 0)
+ } finally {
+ failure.restore()
+ }
+ await store.retryPendingDeleteCleanups(AUTHORITY_SCOPE, TARGET_AGENT)
+ assert.equal((await store.getIndex(TARGET_AGENT)).pendingChatCleanup.length, 0)
+ assert.equal((await store.getIndex(TARGET_AGENT)).deletedChatTombstones.length, 1)
+ await assert.rejects(fs.stat(artifact), { code: 'ENOENT' })
+ return {
+ supported: true,
+ verified: true,
+ ...failedAttempts,
+ retainedWhileFailing: true,
+ removedAfterRetry: true,
+ }
+ } finally {
+ await fs.rm(root, { recursive: true, force: true })
+ }
+}
diff --git a/desktop-app/scripts/bench-chat-open/head.mjs b/desktop-app/scripts/bench-chat-open/head.mjs
new file mode 100644
index 000000000..0e4f81eba
--- /dev/null
+++ b/desktop-app/scripts/bench-chat-open/head.mjs
@@ -0,0 +1,37 @@
+import assert from 'node:assert/strict'
+import { AUTHORITY_SCOPE } from './fixtures.mjs'
+
+export default {
+ ref: 'HEAD',
+ handlesPendingCleanups: true,
+ // HEAD's chat:getIndex handler in ipc.ts delegates to chatGetIndexHandler.ts;
+ // the benchmark calls that compiled export.
+ verifyHandler({ source }) {
+ assert.ok(source.includes('runChatGetIndex'))
+ },
+ bind(ChatStore, root) {
+ const store = new ChatStore(root)
+ store.setAuthorityScope(AUTHORITY_SCOPE)
+ return store
+ },
+ context(store, { runChatGetIndex }) {
+ assert.equal(typeof runChatGetIndex, 'function')
+ let cleanupErrors = 0
+ const deps = {
+ store,
+ getChatDeletionFenceAuthority: () => ({ authorityScope: AUTHORITY_SCOPE }),
+ warnCleanupFailure: () => {
+ cleanupErrors += 1
+ },
+ }
+ return {
+ get cleanupErrors() {
+ return cleanupErrors
+ },
+ getIndex: agentRef => runChatGetIndex(deps, agentRef),
+ // Drain the real cleanup outside timing before removing its fixture. This
+ // also keeps a nonblocking handler from leaking work into another run.
+ settle: () => store.retryPendingDeleteCleanups(AUTHORITY_SCOPE),
+ }
+ },
+}
diff --git a/desktop-app/scripts/bench-chat-open/sources.mjs b/desktop-app/scripts/bench-chat-open/sources.mjs
new file mode 100644
index 000000000..caf203c9d
--- /dev/null
+++ b/desktop-app/scripts/bench-chat-open/sources.mjs
@@ -0,0 +1,203 @@
+import { execFileSync } from 'node:child_process'
+import { createHash } from 'node:crypto'
+import { promises as fs } from 'node:fs'
+import { createRequire, isBuiltin } from 'node:module'
+import { isAbsolute, join, relative, sep } from 'node:path'
+
+const COMMAND_TIMEOUT_MS = 60_000
+
+export function git(repoRoot, args) {
+ return execFileSync('git', args, {
+ cwd: repoRoot,
+ encoding: 'utf8',
+ timeout: COMMAND_TIMEOUT_MS,
+ maxBuffer: 16 * 1024 * 1024,
+ }).trim()
+}
+
+export async function setSourcePermissions(root, readOnly) {
+ // Never follow the node_modules symlink into the working checkout.
+ await fs.chmod(root, readOnly ? 0o555 : 0o700)
+ for (const entry of await fs.readdir(root, { withFileTypes: true })) {
+ const path = join(root, entry.name)
+ if (entry.isDirectory()) await setSourcePermissions(path, readOnly)
+ else if (entry.isFile()) await fs.chmod(path, readOnly ? 0o444 : 0o600)
+ }
+}
+
+function runtimeDependencies(ts, file, source) {
+ const tree = ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true)
+ const imports = new Set()
+ function visit(node) {
+ if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) {
+ if (node.moduleSpecifier && ts.isStringLiteral(node.moduleSpecifier)) {
+ imports.add(node.moduleSpecifier.text)
+ }
+ } else if (
+ ts.isCallExpression(node) &&
+ ((ts.isIdentifier(node.expression) && node.expression.text === 'require') ||
+ node.expression.kind === ts.SyntaxKind.ImportKeyword)
+ ) {
+ if (node.arguments.length !== 1 || !ts.isStringLiteral(node.arguments[0])) {
+ throw new Error('A dynamic runtime dependency cannot be audited for Electron isolation')
+ }
+ imports.add(node.arguments[0].text)
+ }
+ ts.forEachChild(node, visit)
+ }
+ visit(tree)
+ return [...imports]
+}
+
+export async function auditRuntime(ts, compiledRoot, entryFiles) {
+ const canonicalRoot = await fs.realpath(compiledRoot)
+ const seen = new Set()
+ const imports = new Set()
+ async function visit(file) {
+ file = await fs.realpath(file)
+ const withinRoot = relative(canonicalRoot, file)
+ if (withinRoot === '..' || withinRoot.startsWith(`..${sep}`) || isAbsolute(withinRoot)) {
+ throw new Error('Runtime dependency escapes the compiled source snapshot')
+ }
+ if (seen.has(file)) return
+ seen.add(file)
+ for (const specifier of runtimeDependencies(ts, file, await fs.readFile(file, 'utf8'))) {
+ imports.add(specifier)
+ if (isBuiltin(specifier)) continue
+ if (!specifier.startsWith('.')) {
+ throw new Error(`Non-Node runtime dependency requires explicit isolation: ${specifier}`)
+ }
+ const next = createRequire(file).resolve(specifier)
+ await visit(next)
+ }
+ }
+ for (const entry of entryFiles) await visit(entry)
+ return { files: [...seen].sort(), imports: [...imports].sort() }
+}
+
+export function handlerLines(ipcSource) {
+ const lines = ipcSource.split('\n')
+ const start = lines.findIndex(line => line.includes("ipcMain.handle('chat:getIndex'"))
+ if (start < 0) throw new Error('chat:getIndex handler is missing from this source version')
+ const end = lines.findIndex((line, index) => index > start && line === ' })')
+ if (end < 0) throw new Error('chat:getIndex handler boundary is missing')
+ return { start: start + 1, end: end + 1, source: lines.slice(start, end + 1).join('\n') }
+}
+
+export async function compileVersion({ adapter, repoRoot, desktopRoot, runDir }) {
+ const versionDir = await fs.mkdtemp(join(runDir, 'source-'))
+ const snapshotRoot = join(versionDir, 'snapshot')
+ const snapshotDesktop = join(snapshotRoot, 'desktop-app')
+ const sourceRoot = join(snapshotDesktop, 'src')
+ const compiledRoot = join(versionDir, 'compiled')
+ await fs.mkdir(snapshotDesktop, { recursive: true })
+ const commit = git(repoRoot, ['rev-parse', `${adapter.ref}^{commit}`])
+ if (adapter.ref === 'HEAD') {
+ // Include the live, possibly uncommitted handler; freeze it before compiling.
+ await fs.access(join(desktopRoot, 'src/chatGetIndexHandler.ts'))
+ await fs.cp(join(desktopRoot, 'src'), sourceRoot, { recursive: true })
+ for (const file of await fs.readdir(desktopRoot)) {
+ if (file === 'package.json' || /^tsconfig.*\.json$/.test(file)) {
+ await fs.copyFile(join(desktopRoot, file), join(snapshotDesktop, file))
+ }
+ }
+ } else {
+ // Equivalent to git archive <ref> ... | tar -x -C <tmpdir>, with each exit checked.
+ const archive = execFileSync(
+ 'git',
+ [
+ 'archive',
+ commit,
+ 'desktop-app/src',
+ 'desktop-app/package.json',
+ 'desktop-app/tsconfig*.json',
+ ],
+ { cwd: repoRoot, timeout: COMMAND_TIMEOUT_MS, maxBuffer: 64 * 1024 * 1024 }
+ )
+ execFileSync('tar', ['-x', '-C', snapshotRoot], {
+ input: archive,
+ timeout: COMMAND_TIMEOUT_MS,
+ maxBuffer: 1024 * 1024,
+ })
+ }
+ await fs.symlink(join(desktopRoot, 'node_modules'), join(snapshotDesktop, 'node_modules'), 'dir')
+ const ipcSource = await fs.readFile(join(sourceRoot, 'ipc.ts'), 'utf8')
+ // Verify against git show as well as the archived bytes before trusting an adapter.
+ if (
+ adapter.ref !== 'HEAD' &&
+ ipcSource !== `${git(repoRoot, ['show', `${commit}:desktop-app/src/ipc.ts`])}\n`
+ ) {
+ throw new Error('Archived ipc.ts does not match git show')
+ }
+ const handler = handlerLines(ipcSource)
+ adapter.verifyHandler(handler)
+ await setSourcePermissions(snapshotRoot, true)
+ const entries = ['chatStore.ts', ...(adapter.ref === 'HEAD' ? ['chatGetIndexHandler.ts'] : [])]
+ const config = join(versionDir, 'tsconfig.bench.json')
+ await fs.writeFile(
+ config,
+ JSON.stringify({
+ extends: join(snapshotDesktop, 'tsconfig.json'),
+ compilerOptions: {
+ rootDir: sourceRoot,
+ outDir: compiledRoot,
+ types: ['node'],
+ typeRoots: [join(desktopRoot, 'node_modules/@types')],
+ noEmit: false,
+ noEmitOnError: true,
+ incremental: false,
+ declaration: false,
+ sourceMap: false,
+ },
+ files: entries.map(file => join(sourceRoot, file)),
+ include: [],
+ exclude: [],
+ }),
+ { mode: 0o600 }
+ )
+ execFileSync(process.execPath, [join(desktopRoot, 'node_modules/.bin/tsc'), '-p', config], {
+ cwd: desktopRoot,
+ timeout: COMMAND_TIMEOUT_MS,
+ maxBuffer: 4 * 1024 * 1024,
+ })
+ const requireHead = createRequire(join(desktopRoot, 'package.json'))
+ const ts = requireHead('typescript')
+ const runtimeRoot = await fs.realpath(compiledRoot)
+ const runtime = await auditRuntime(
+ ts,
+ runtimeRoot,
+ entries.map(file => join(runtimeRoot, file.replace(/\.ts$/, '.js')))
+ )
+ const sources = await Promise.all(
+ runtime.files.map(async file => {
+ const name = relative(runtimeRoot, file).replace(/\.js$/, '.ts')
+ return {
+ file: `desktop-app/src/${name}`,
+ sha256: createHash('sha256')
+ .update(await fs.readFile(join(sourceRoot, name)))
+ .digest('hex'),
+ }
+ })
+ )
+ const load = createRequire(join(compiledRoot, 'chatStore.js'))
+ const { ChatStore } = load('./chatStore.js')
+ const handlerModule = adapter.ref === 'HEAD' ? load('./chatGetIndexHandler.js') : undefined
+ return {
+ ChatStore,
+ context: store => adapter.context(store, handlerModule),
+ snapshotRoot,
+ metadata: {
+ version: adapter.ref,
+ commit,
+ sourceMode: adapter.ref === 'HEAD' ? 'live-worktree-snapshot' : 'read-only-git-archive',
+ compilerVersion: ts.version,
+ sources,
+ ipcSha256: createHash('sha256').update(ipcSource).digest('hex'),
+ handlerLines: { start: handler.start, end: handler.end },
+ runtimeImports: runtime.imports,
+ electronIsolation:
+ 'Only the compiled ChatStore/handler runtime dependency graph is loaded; every dependency is audited as a Node builtin or a relative module. Electron bindings and ipc.ts are never loaded.',
+ handlesPendingCleanups: adapter.handlesPendingCleanups,
+ },
+ }
+}
diff --git a/desktop-app/scripts/bench-chat-open/v0.10.0.mjs b/desktop-app/scripts/bench-chat-open/v0.10.0.mjs
new file mode 100644
index 000000000..7754568ee
--- /dev/null
+++ b/desktop-app/scripts/bench-chat-open/v0.10.0.mjs
@@ -0,0 +1,19 @@
+import assert from 'node:assert/strict'
+
+export default {
+ ref: 'v0.10.0',
+ handlesPendingCleanups: false,
+ // git show v0.10.0:desktop-app/src/ipc.ts:1575-1580: getIndex only.
+ verifyHandler({ start, end, source }) {
+ assert.equal(start, 1575)
+ assert.equal(end, 1580)
+ assert.ok(source.includes('return requireChatStore().getIndex(agentRef)'))
+ assert.ok(!source.includes('retryPendingDeleteCleanups'))
+ },
+ bind(ChatStore, root) {
+ return new ChatStore(root)
+ },
+ context(store) {
+ return { cleanupErrors: 0, getIndex: agentRef => store.getIndex(agentRef) }
+ },
+}
diff --git a/desktop-app/scripts/bench-chat-open/v0.11.0.mjs b/desktop-app/scripts/bench-chat-open/v0.11.0.mjs
new file mode 100644
index 000000000..3ec54f3fe
--- /dev/null
+++ b/desktop-app/scripts/bench-chat-open/v0.11.0.mjs
@@ -0,0 +1,41 @@
+import assert from 'node:assert/strict'
+import { AUTHORITY_SCOPE } from './fixtures.mjs'
+
+export default {
+ ref: 'v0.11.0',
+ handlesPendingCleanups: true,
+ // git show v0.11.0:desktop-app/src/ipc.ts:1839-1862; cleanup at 1851-1860,
+ // followed by getIndex at 1861. Sender/payload checks precede these store costs.
+ verifyHandler({ start, end, source }) {
+ assert.equal(start, 1839)
+ assert.equal(end, 1862)
+ assert.ok(
+ source.indexOf('await store.retryPendingDeleteCleanups') <
+ source.indexOf('return store.getIndex')
+ )
+ assert.ok(source.includes('retryPendingDeleteCleanups(authority.authorityScope, agentRef)'))
+ },
+ bind(ChatStore, root) {
+ const store = new ChatStore(root)
+ store.setAuthorityScope(AUTHORITY_SCOPE)
+ return store
+ },
+ context(store) {
+ let cleanupErrors = 0
+ return {
+ get cleanupErrors() {
+ return cleanupErrors
+ },
+ async getIndex(agentRef) {
+ try {
+ await store.retryPendingDeleteCleanups(AUTHORITY_SCOPE, agentRef)
+ } catch {
+ // The historical handler reports the cleanup failure, then returns the
+ // catalog. Retain a numeric witness instead of logging identifiers/errors.
+ cleanupErrors += 1
+ }
+ return store.getIndex(agentRef)
+ },
+ }
+ },
+}
diff --git a/desktop-app/src/__tests__/chatGetIndexHandler.test.ts b/desktop-app/src/__tests__/chatGetIndexHandler.test.ts
new file mode 100644
index 000000000..8f1380e60
--- /dev/null
+++ b/desktop-app/src/__tests__/chatGetIndexHandler.test.ts
@@ -0,0 +1,140 @@
+import { describe, expect, it, vi } from 'vitest'
+import { execFileSync } from 'node:child_process'
+import fs from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import ts from 'typescript'
+import { runChatGetIndex } from '../chatGetIndexHandler.js'
+import type { ChatIndex } from '../types.js'
+
+const authorityScope = { environmentKey: 'env-test', userId: 'user-test', teamId: null }
+const index: ChatIndex = {
+ version: 2,
+ chats: [
+ {
+ id: 'chat-1',
+ title: 'Chat',
+ createdAt: '2026-10-13T00:00:00Z',
+ updatedAt: '2026-10-13T00:00:00Z',
+ messageCount: 1,
+ },
+ ],
+ lastActiveChatId: 'chat-1',
+}
+
+describe('runChatGetIndex', () => {
+ it('awaits cleanup before reading the index, preserving the IPC baseline order', async () => {
+ const order: string[] = []
+ let release!: () => void
+ const cleanup = new Promise<void>(resolve => {
+ release = resolve
+ })
+ const deps = {
+ getChatDeletionFenceAuthority: vi.fn(() => {
+ order.push('authority')
+ return { authorityScope }
+ }),
+ store: {
+ retryPendingDeleteCleanups: vi.fn(async () => {
+ order.push('cleanup:start')
+ await cleanup
+ order.push('cleanup:end')
+ }),
+ getIndex: vi.fn(async () => {
+ order.push('read')
+ return index
+ }),
+ },
+ warnCleanupFailure: vi.fn(),
+ }
+ const result = runChatGetIndex(deps, 'agent-1')
+ expect(order).toEqual(['authority', 'cleanup:start'])
+ expect(deps.store.retryPendingDeleteCleanups).toHaveBeenCalledWith(authorityScope, 'agent-1')
+ expect(deps.store.getIndex).toHaveBeenCalledTimes(0)
+ release()
+ expect(await result).toBe(index)
+ expect(order).toEqual(['authority', 'cleanup:start', 'cleanup:end', 'read'])
+ expect(deps.store.getIndex).toHaveBeenCalledWith('agent-1')
+ expect(deps.warnCleanupFailure).toHaveBeenCalledTimes(0)
+ })
+
+ it.each(['authority', 'cleanup'])(
+ 'reports a %s failure and still reads the same index',
+ async stage => {
+ const failure = new Error('Cleanup failed')
+ const deps = {
+ getChatDeletionFenceAuthority: vi.fn(() => {
+ if (stage === 'authority') throw failure
+ return { authorityScope }
+ }),
+ store: {
+ retryPendingDeleteCleanups: vi.fn(async () => {
+ throw failure
+ }),
+ getIndex: vi.fn(async () => index),
+ },
+ warnCleanupFailure: vi.fn(),
+ }
+ expect(await runChatGetIndex(deps, 'agent-1')).toBe(index)
+ expect(deps.getChatDeletionFenceAuthority).toHaveBeenCalledTimes(1)
+ expect(deps.store.getIndex).toHaveBeenCalledTimes(1)
+ expect(deps.warnCleanupFailure).toHaveBeenCalledWith(failure)
+ }
+ )
+
+ it('propagates index read failures after cleanup has completed', async () => {
+ const failure = new Error('Read failed')
+ const cleanup = vi.fn(async () => {})
+ const read = vi.fn(async () => {
+ throw failure
+ })
+ await expect(
+ runChatGetIndex(
+ {
+ store: { retryPendingDeleteCleanups: cleanup, getIndex: read },
+ getChatDeletionFenceAuthority: () => ({ authorityScope }),
+ warnCleanupFailure: vi.fn(),
+ },
+ 'agent-1'
+ )
+ ).rejects.toBe(failure)
+ expect(cleanup).toHaveBeenCalledTimes(1)
+ expect(read).toHaveBeenCalledTimes(1)
+ })
+
+ it('can be compiled and imported by a plain Node benchmark without Electron', () => {
+ const dir = fs.mkdtempSync(join(tmpdir(), 'chat-get-index-node-'))
+ try {
+ const source = fs.readFileSync(join(__dirname, '../chatGetIndexHandler.ts'), 'utf8')
+ const compiled = ts.transpileModule(source, {
+ compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS },
+ }).outputText
+ const modulePath = join(dir, 'chatGetIndexHandler.cjs')
+ fs.writeFileSync(modulePath, compiled)
+ const output = execFileSync(
+ process.execPath,
+ [
+ '-e',
+ `
+ const { runChatGetIndex } = require(process.argv[1]);
+ const order = [];
+ runChatGetIndex({
+ getChatDeletionFenceAuthority: () => ({ authorityScope: {} }),
+ store: {
+ retryPendingDeleteCleanups: async () => { order.push('cleanup'); },
+ getIndex: async () => { order.push('read'); return { chats: [1] }; }
+ },
+ warnCleanupFailure: error => { throw error; }
+ }, 'agent-1').then(index => process.stdout.write(JSON.stringify({ order, count: index.chats.length })));
+ `,
+ modulePath,
+ ],
+ { cwd: dir, encoding: 'utf8' }
+ )
+ expect(JSON.parse(output)).toEqual({ order: ['cleanup', 'read'], count: 1 })
+ expect(compiled).not.toContain('electron')
+ } finally {
+ fs.rmSync(dir, { recursive: true, force: true })
+ }
+ })
+})
diff --git a/desktop-app/src/chatGetIndexHandler.ts b/desktop-app/src/chatGetIndexHandler.ts
new file mode 100644
index 000000000..28f5d2c36
--- /dev/null
+++ b/desktop-app/src/chatGetIndexHandler.ts
@@ -0,0 +1,35 @@
+import type { ChatAuthorityScope, ChatIndex } from './types.js'
+
+export interface ChatGetIndexDependencies {
+ store: {
+ retryPendingDeleteCleanups(scope: ChatAuthorityScope, agentRef?: string): Promise<void>
+ getIndex(agentRef: string): Promise<ChatIndex>
+ }
+ getChatDeletionFenceAuthority(): { authorityScope: ChatAuthorityScope }
+ warnCleanupFailure(error: unknown): void
+}
+
+/**
+ * chat:getIndex logic without Electron dependencies, so the chat-open benchmark
+ * (scripts/bench-chat-open.mjs) measures the same code the IPC handler runs.
+ *
+ * The catalog read must never depend on the deleted-chat artifact cleanup:
+ * a cleanup failure (scope changed mid-flight, no session, an unreadable
+ * chats directory) would otherwise reject the read and leave the renderer
+ * without the tombstones it filters deleted chats with. The failure is
+ * reported and the cleanup stays queued for the next read. Only this agent's
+ * queue is retried (the renderer reads every agent in a fan-out); the store
+ * walks every agent once per bind on its own.
+ */
+export async function runChatGetIndex(
+ deps: ChatGetIndexDependencies,
+ agentRef: string
+): Promise<ChatIndex> {
+ try {
+ const authority = deps.getChatDeletionFenceAuthority()
+ await deps.store.retryPendingDeleteCleanups(authority.authorityScope, agentRef)
+ } catch (error) {
+ deps.warnCleanupFailure(error)
+ }
+ return deps.store.getIndex(agentRef)
+}
diff --git a/desktop-app/src/ipc.ts b/desktop-app/src/ipc.ts
index 67038e6cd..9d7a1d4f3 100644
--- a/desktop-app/src/ipc.ts
+++ b/desktop-app/src/ipc.ts
@@ -12,6 +12,7 @@ import { promises as fs } from 'node:fs'
import path from 'node:path'
import { parseFileReferenceV1 } from '@clerum/gfs-interaction-policy'
import { AppService } from './appService.js'
+import { runChatGetIndex } from './chatGetIndexHandler.js'
import {
getChatStoreBindingGeneration,
requireChatStore,
@@ -1840,25 +1841,20 @@ export function registerIpcHandlers(service: AppService): void {
assertTrustedSender(event)
const agentRef = sanitizeString(payload?.agentRef)
if (!agentRef) throw new Error('agentRef is required')
- const store = requireChatStore()
- // The catalog read must never depend on the deleted-chat artifact cleanup:
- // a cleanup failure (scope changed mid-flight, no session, an unreadable
- // chats directory) would otherwise reject the read and leave the renderer
- // without the tombstones it filters deleted chats with. The failure is
- // reported with its code and the cleanup stays queued for the next read.
- // Only this agent's queue is retried (the renderer reads every agent in a
- // fan-out); the store walks every agent once per bind on its own.
- try {
- const authority = service.getChatDeletionFenceAuthority()
- await store.retryPendingDeleteCleanups(authority.authorityScope, agentRef)
- } catch (error) {
- const code = (error as NodeJS.ErrnoException | undefined)?.code
- console.warn(
- `[chat:getIndex] Deleted-chat cleanup retry failed for agent "${agentRef}"${code ? ` (${code})` : ''}; returning the index with its tombstones`,
- error
- )
- }
- return store.getIndex(agentRef)
+ return runChatGetIndex(
+ {
+ store: requireChatStore(),
+ getChatDeletionFenceAuthority: () => service.getChatDeletionFenceAuthority(),
+ warnCleanupFailure: error => {
+ const code = (error as NodeJS.ErrnoException | undefined)?.code
+ console.warn(
+ `[chat:getIndex] Deleted-chat cleanup retry failed for agent "${agentRef}"${code ? ` (${code})` : ''}; returning the index with its tombstones`,
+ error
+ )
+ },
+ },
+ agentRef
+ )
})
ipcMain.handle('chat:dismissOnboarding', async (event, payload: { agentRef: string }) => {