// @vitest-environment node import { mkdtemp, rm, writeFile } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { afterEach, describe, expect, it } from 'vitest'; import { createCodingConversationStore } from '../../electron/coding-projects/conversation-store'; import { createCodingProjectAgent } from '../../electron/coding-projects/project-config'; import { createCodingProjectStore, createLocalCodingProject, createMemoryCodingProjectStorage, } from '../../electron/coding-projects/project-store'; import { summarizeMeasurements } from '../../scripts/probe-pi-runtime.mjs'; const roots: string[] = []; const samples = Number.parseInt(process.env.MAKELORE_PI_PERF_SAMPLES ?? '10', 10); afterEach(async () => { await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true }))); }); describe('PI-150 local metadata performance fragments', () => { it('reports project, Agent, and Conversation metadata distributions', async () => { expect(samples).toBeGreaterThan(0); const projectMs: number[] = []; const agentMs: number[] = []; const conversationMs: number[] = []; for (let index = 0; index < samples; index += 1) { const projectPath = await mkdtemp(path.join(tmpdir(), 'makelore-pi-release-perf-')); roots.push(projectPath); const store = createCodingProjectStore(createMemoryCodingProjectStorage(), { createId: () => `project-${index}`, now: () => '2026-08-24T00:00:00.000Z', }); let startedAt = performance.now(); await createLocalCodingProject({ projectPath, now: '2026-08-24T00:00:00.000Z', }, store); projectMs.push(performance.now() - startedAt); startedAt = performance.now(); const agent = await createCodingProjectAgent(projectPath, { id: `builder-${index}`, avatarId: 'avatar-01', roleName: '实现者', name: `Builder ${index}`, model: { accountId: 'account-a', modelId: 'model-a', thinkingLevel: 'medium' }, modelResolution: 'resolved', responsibility: { mission: 'Implement', owns: [], boundaries: [], collaborators: [], principles: [], }, }, { now: '2026-08-24T00:00:00.000Z' }); agentMs.push(performance.now() - startedAt); const conversations = createCodingConversationStore(projectPath, { createId: () => `00000000-0000-4000-8000-${String(index).padStart(12, '0')}`, now: () => '2026-08-24T00:00:00.000Z', }); startedAt = performance.now(); await conversations.create({ agentId: agent.id, title: `Conversation ${index}`, model: agent.model, modelResolution: agent.modelResolution, }); conversationMs.push(performance.now() - startedAt); } const report = { projectMetadataMs: summarizeMeasurements(projectMs), agentMetadataMs: summarizeMeasurements(agentMs), conversationMetadataMs: summarizeMeasurements(conversationMs), }; expect(report.projectMetadataMs.p95).toBeLessThanOrEqual(1_000); expect(report.agentMetadataMs.p95).toBeLessThanOrEqual(500); expect(report.conversationMetadataMs.p95).toBeLessThanOrEqual(500); const reportPath = process.env.MAKELORE_PI_PERF_METADATA_FRAGMENT; if (reportPath) await writeFile(reportPath, `${JSON.stringify(report, null, 2)}\n`); }); });