mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-03 17:51:58 +01:00
## Thinking Path > - Paperclip manages agent work through shared server and database services. > - High query volume can spend main-thread time on SQL construction and result decoding. > - The existing bounded response capture counts rows and bytes, but it cannot name the operation that executed a query. > - Bundled callers can be missing, and measuring every parsed JSON value adds work to the diagnostic. > - This pull request carries a semantic execution owner into each measured query and bounds response sizing. > - Operators can rank query volume by owner before choosing a performance fix. ## Linked Issues or Issue Description Refs #17504, #17522, #17537, #17564. **What existing behavior does this improve?** The opt-in `PAPERCLIP_DB_RESPONSE_CAPTURE_WINDOW` diagnostic. **Subsystem affected** Database diagnostics and existing server execution boundaries. **Current behavior** The capture groups by table, operation, and builder caller. It cannot distinguish execution owners that reuse a prepared query. Bundled deployments need source maps for caller locations. The capture visits every returned value and serializes parsed JSON again to estimate its size. **Proposed behavior** Version 2 captures use fixed semantic owners for HTTP requests, delivery queues, execution phases, and budget work. They count completed calls, returned rows, errors, and completion wall time. They sample successful results 1, 17, 33, and so on, with fixed cell and string/byte limits. Complex values remain explicitly unmeasured. Semantic ownership works without source maps. **Reason and benefit** A short census can identify which operations issue the most queries or return the most rows. It preserves each query's execution owner through pooled connections, concurrent operations, and prepared-query reuse. It reduces diagnostic work without claiming per-query CPU attribution. **Breaking changes** Opt-in capture output moves from `entry.v1`/`summary.v1` to `entry.v2`/`summary.v2`. Sampled primitive byte lower bounds replace all-result byte estimates. Consumers must use the new fields and must not extrapolate them into a total. There is no database migration or application API change. ## What Changed - Added `withDatabaseOperation(owner, action)` to the DB package. The server owns semantic labels. Queries retain execution ownership in their completion closure. - Added fixed owners at existing HTTP, delivery, budget, and execution timing boundaries. Delivery work replaces the context of the producer that woke it. - Added complete call/row/error counters and completion wall durations within the existing bounded window. Durations include waits and are not CPU measurements. - Sampled and bounded primitive-value sizing. Complex and oversized values remain unmeasured. Known owners skip repeated stack extraction. - Preserved the deadline, group limit, paced output, query outcomes, and existing coverage exclusions. Updated the operator documentation. - Added real PostgreSQL owner-isolation and prepared-query tests, server scope tests, and plain bundled-runtime proof. - Moved projection-test and benchmark full-response sizing into an explicit test helper. Existing payload-size thresholds remain unchanged; these synthetic checks do not use the production sampler. ## Verification - Final-commit DB capture suites: 43 tests passed in 13.86 seconds against the actual PostgreSQL driver, including concurrent owners, prepared reuse, transaction failure, a dedicated client, sampling, and a query crossing the deadline. DB typecheck passed. - Server attribution and existing delivery/execution/budget suites: 53 tests passed. Final concurrent HTTP GET/JSON POST scope refinement: 5 tests passed. - Runtime bundle suite: 8 existing tests passed. The new no-source-map owner test passed after its fixture was corrected to use a driver thenable. An ordinary native Promise can bypass an overridden `.then`; the real PostgreSQL tests separately cover driver integration. - Observer-only experiment: three alternating pairs per case on Node 24.22.2, macOS arm64, 3,000 calls per run after warm-up. For an already-materialized synthetic JSON result, median capture CPU fell from 82.37 to 1.87 microseconds/call. For a 100-row primitive result, it fell from 8.10 to 4.40 microseconds/call. Every result retained identity and all calls were counted. The new diagnostic intentionally measures less byte data; these are observer costs, not database latency or a hosted application speedup. Disabled-case timings were below one microsecond/call and too small for a performance conclusion. - Final lifecycle regression: 28 unit tests and DB typecheck passed. Closing the last capture disables and replaces owner storage; overlapping captures retain it. - Final-commit full recursive typecheck passed in 38.38 seconds and full build passed in 40.81 seconds. The build stamp matches `83ed4ba9cebd68d48929c46f15ab32d5c0b9687b`. Focused server attribution tests also passed 5/5 before the test-helper-only migration. Full test acceptance will use the repository's parallel CI gates; the unsharded local command takes hours. - Verification failures retained: initial sandbox runs could not open local IPC/PostgreSQL listeners; permitted reruns exercised the real services. One old test expected JSON serialization and was updated to assert explicit unmeasured complex values. The first bundle fixture used the wrong promise boundary, as described above. A retained-callback regression also demonstrated that disabling and reusing the same AsyncLocalStorage instance could resurrect an old owner; replacing the instance after the last capture closes fixes that case. - The first CI run found older projection tests consuming the previous full-response fields. Those consumers now use the full-response test helper, preserving their byte thresholds. The two complete affected route suites plus helper tests passed 115/115 in 73.08 seconds. A temporary full-row query negative control then failed the unchanged 2,048-byte limit at 5,000,812 bytes and was restored; five other service suites passed 34/34 and the affected chat cases passed 3/3. All three previously failing CI shards passed on the corrected head. A local skill-list benchmark smoke check completed 255 captured queries with no in-flight queries or overflow; this was integration verification under concurrent test load, not a timing benchmark. - Two final-head runner jobs lost communication with GitHub before reporting a test result. One targeted retry of those jobs and their dependent aggregate passed. All 53 final-head checks passed, with 3 intentional skips; previously passing suites were preserved. - Hosted capture remains pending until the reviewed revision is deployed and a bounded window is explicitly configured. Reproduce the focused checks with Node 24 and the repository pnpm version: ```sh pnpm exec vitest run packages/db/src/response-capture.test.ts packages/db/src/response-capture-postgres.test.ts pnpm --dir server exec vitest run --config vitest.config.ts src/__tests__/database-operation-attribution.test.ts scripts/bundle-runtime.test.mjs pnpm -r typecheck pnpm build ``` ## Risks - Owner context is active while a valid future capture window is armed, up to 30 minutes before accounting starts. No-window and expired calls take the direct path. Diagnostics still add bounded work while enabled. - A nearest nested owner is reported. Broad `http.request` and unattributed groups remain visible; this does not identify every service callsite or SQL shape. - The existing census excludes tagged/raw explicit-execute/streaming paths and internal protocol commands. Its counts are not PostgreSQL transaction totals. - Completion wall time includes queue/database/network/scheduling waits. Concurrent durations overlap. Sampled bytes exclude complex values, large strings, and values beyond the sizing limits. - Activation is startup-only. Operators must choose the window after artifact preparation, allow rollout/warm-up time, and remove the expired setting afterward. ## Model Used OpenAI Codex, GPT-6 family, with code execution, repository inspection, and parallel agents. The exact deployment model ID and context-window size are not exposed in this session. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
250 lines
16 KiB
TypeScript
250 lines
16 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { createHash, randomUUID } from "node:crypto";
|
|
import { execFileSync } from "node:child_process";
|
|
import { promises as fs } from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
import { connect, createServer, type Socket } from "node:net";
|
|
import express from "express";
|
|
import { get as httpGet } from "node:http";
|
|
import { context, trace } from "@opentelemetry/api";
|
|
import { AsyncLocalStorageContextManager } from "@opentelemetry/context-async-hooks";
|
|
import { InMemorySpanExporter, SimpleSpanProcessor, TracerProvider } from "@opentelemetry/sdk-trace";
|
|
import { eq } from "drizzle-orm";
|
|
import { authUsers, companies, companyMemberships, companySkills, companySkillSources,
|
|
projectSkillSources, projects, applyPendingMigrations, closeRegisteredClients, createDb,
|
|
startEmbeddedPostgresTestDatabase } from "../../packages/db/src/index.js";
|
|
import * as schema from "../../packages/db/src/schema/index.js";
|
|
import { createFullDatabaseResponseCapture, installDatabaseResponseCapture } from "../src/__tests__/helpers/full-database-response-capture.js";
|
|
import { withRunExecutionTiming } from "../src/services/execution-preparation-timing.js";
|
|
|
|
const argument = (name: string) => process.argv.find(value => value.startsWith(`${name}=`))?.slice(name.length + 1);
|
|
const sampleCount = Number(argument("--samples") ?? 5);
|
|
const baselineRef = argument("--baseline-ref");
|
|
const delayMs = Number(argument("--network-delay-ms") ?? 3.5);
|
|
const captureArgument = argument("--capture-values") ?? "true";
|
|
assert.ok(["true", "false"].includes(captureArgument));
|
|
const captureValues = captureArgument === "true";
|
|
assert.ok(Number.isInteger(sampleCount) && sampleCount >= 3 && sampleCount <= 10);
|
|
assert.ok(Number.isFinite(delayMs) && delayMs >= 0 && delayMs <= 20);
|
|
const previous = { home: process.env.PAPERCLIP_HOME, instance: process.env.PAPERCLIP_INSTANCE_ID,
|
|
endpoint: process.env.OTEL_EXPORTER_OTLP_ENDPOINT };
|
|
delete process.env.OTEL_EXPORTER_OTLP_ENDPOINT;
|
|
const { companySkillService } = await import("../src/services/company-skills.js");
|
|
const { createAgentLifecycle } = await import("../src/services/agent-lifecycle.js");
|
|
const { companySkillRoutes } = await import("../src/routes/company-skills.js");
|
|
const { skillAccessRequestContext, withSkillActor } = await import("../src/services/skill-access.js");
|
|
const home = await fs.mkdtemp(path.join(os.tmpdir(), "skill-list-benchmark-"));
|
|
const sourceFiles = [fileURLToPath(import.meta.url), fileURLToPath(new URL("../src/services/company-skills.ts", import.meta.url))];
|
|
const sourceHash = async () => {
|
|
const hash = createHash("sha256");
|
|
for (const file of sourceFiles) hash.update(await fs.readFile(file));
|
|
return hash.digest("hex");
|
|
};
|
|
const sourceRevision = execFileSync("git", ["rev-parse", "HEAD"], { cwd: fileURLToPath(new URL("../../", import.meta.url)), encoding: "utf8" }).trim();
|
|
const measuredSourceHash = await sourceHash();
|
|
let database: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>> | undefined;
|
|
let db: ReturnType<typeof createDb> | undefined;
|
|
let proxy: ReturnType<typeof createServer> | undefined;
|
|
let server: ReturnType<ReturnType<typeof express>["listen"]> | undefined;
|
|
let baselineFile: string | undefined;
|
|
let baselineRevision: string | undefined;
|
|
let baselineSourceHash: string | undefined;
|
|
let provider: TracerProvider | undefined;
|
|
let manager: AsyncLocalStorageContextManager | undefined;
|
|
const sockets = new Set<Socket>();
|
|
try {
|
|
process.env.PAPERCLIP_HOME = home;
|
|
process.env.PAPERCLIP_INSTANCE_ID = "default";
|
|
const port = argument("--postgres-port");
|
|
if (port) {
|
|
assert.match(port, /^\d{1,5}$/);
|
|
assert.ok(Number(port) > 0 && Number(port) < 65536);
|
|
const user = encodeURIComponent(argument("--postgres-user") ?? os.userInfo().username);
|
|
const name = `skill_list_${randomUUID().replaceAll("-", "")}_test`;
|
|
const adminUrl = `postgres://${user}@127.0.0.1:${port}/postgres`;
|
|
const connectionString = `postgres://${user}@127.0.0.1:${port}/${name}`;
|
|
const admin = createDb(adminUrl, { maxConnections: 1 }).$client;
|
|
try { await admin.unsafe(`CREATE DATABASE "${name}"`); } finally { await admin.end({ timeout: 1 }); }
|
|
const cleanup = async () => {
|
|
await closeRegisteredClients(connectionString);
|
|
const client = createDb(adminUrl, { maxConnections: 1 }).$client;
|
|
try { await client.unsafe(`DROP DATABASE "${name}"`); } finally { await client.end({ timeout: 1 }); }
|
|
};
|
|
database = { connectionString, cleanup };
|
|
await applyPendingMigrations(connectionString);
|
|
} else database = await startEmbeddedPostgresTestDatabase("skill-list-benchmark-");
|
|
|
|
const target = new URL(database.connectionString);
|
|
const upstreamPort = Number(target.port);
|
|
if (delayMs) {
|
|
proxy = createServer(client => {
|
|
const upstream = connect({ host: "127.0.0.1", port: upstreamPort });
|
|
for (const socket of [client, upstream]) {
|
|
socket.setNoDelay(true); sockets.add(socket);
|
|
socket.on("error", () => { client.destroy(); upstream.destroy(); });
|
|
socket.on("close", () => sockets.delete(socket));
|
|
}
|
|
const relay = (from: Socket, to: Socket) => {
|
|
from.on("data", chunk => { from.pause(); setTimeout(() => {
|
|
if (to.destroyed) return;
|
|
if (to.write(chunk)) from.resume();
|
|
else to.once("drain", () => from.resume());
|
|
}, delayMs); });
|
|
from.on("end", () => setTimeout(() => to.end(), delayMs));
|
|
};
|
|
relay(client, upstream); relay(upstream, client);
|
|
});
|
|
await new Promise<void>(resolve => proxy!.listen(0, "127.0.0.1", resolve));
|
|
const address = proxy.address(); assert.ok(address && typeof address !== "string");
|
|
target.port = String(address.port);
|
|
}
|
|
db = createDb(target.href);
|
|
const companyId = randomUUID(), userId = randomUUID();
|
|
await db.insert(companies).values({ id: companyId, name: "Skill list benchmark", issuePrefix: "SLB" });
|
|
await db.insert(authUsers).values({ id: userId, name: "Fixture owner", email: `${userId}@example.invalid`, createdAt: new Date(), updatedAt: new Date() });
|
|
await db.insert(companyMemberships).values({ companyId, principalType: "user", principalId: userId, membershipRole: "owner" });
|
|
const projectRows = await db.insert(projects).values(Array.from({ length: 14 }, (_, i) => ({ companyId, name: `Fixture ${i}` }))).returning();
|
|
const sources = await db.insert(companySkillSources).values(Array.from({ length: 10 }, (_, i) => ({ companyId,
|
|
repositoryUrl: `https://example.invalid/fixture-${i}`, fullName: `fixture/${i}`, trackingRef: "main",
|
|
accessScope: i < 4 ? "projects" as const : "company" as const }))).returning();
|
|
await db.insert(projectSkillSources).values(sources.slice(0, 8).map((source, i) => ({ companyId, sourceId: source.id,
|
|
projectId: projectRows[i % 5]!.id, repositoryUrl: source.repositoryUrl, trackingRef: "main", status: "ready" as const })));
|
|
const skills = await db.insert(companySkills).values(Array.from({ length: 155 }, (_, i) => ({ companyId,
|
|
key: `fixture/skill-${i}`, slug: `skill-${i}`, name: `Skill ${String(i).padStart(3, "0")}`,
|
|
markdown: "Synthetic unused markdown. ".repeat(280), sourceType: "github",
|
|
sourceLocator: "https://example.invalid/immutable-fixture", accessSourceId: sources[i % 10]!.id,
|
|
metadata: { unused: "metadata ".repeat(1000) }, fileInventory: [{ path: "SKILL.md", kind: "skill" as const }],
|
|
}))).returning();
|
|
const lifecycle = createAgentLifecycle(db);
|
|
for (let i = 0; i < 40; i++) await lifecycle.requestHire(companyId, {
|
|
name: `Reader ${i}`, adapterType: "process", status: i === 39 ? "terminated" : "paused",
|
|
adapterConfig: { instructions: "Synthetic agent context. ".repeat(2600),
|
|
paperclipSkillSync: { desiredSkills: [skills[i % 155]!.key, { key: skills[(i + 1) % 155]!.id, versionId: null }] } },
|
|
runtimeConfig: { unused: "runtime ".repeat(1000) }, metadata: { unused: "metadata ".repeat(1000) },
|
|
}, { runnerResolved: true });
|
|
await companySkillService(db).list(companyId);
|
|
|
|
const actor = { type: "board" as const, source: "session" as const, userId, companyIds: [companyId],
|
|
memberships: [{ companyId, membershipRole: "owner" }] };
|
|
let baselineService: ReturnType<typeof companySkillService> | undefined;
|
|
if (baselineRef) {
|
|
assert.equal(captureValues, false, "Paired acceptance uses --capture-values=false");
|
|
baselineRevision = execFileSync("git", ["rev-parse", "--verify", "--end-of-options", `${baselineRef}^{commit}`], { encoding: "utf8" }).trim();
|
|
const source = execFileSync("git", ["show", `${baselineRevision}:server/src/services/company-skills.ts`], { encoding: "utf8", maxBuffer: 2 * 1024 * 1024 });
|
|
baselineSourceHash = createHash("sha256").update(source).digest("hex");
|
|
baselineFile = fileURLToPath(new URL(`../src/services/.company-skills-benchmark-${randomUUID()}.ts`, import.meta.url));
|
|
await fs.writeFile(baselineFile, source, { flag: "wx", mode: 0o600 });
|
|
const baselineModule = await import(pathToFileURL(baselineFile).href) as { companySkillService: typeof companySkillService };
|
|
baselineService = baselineModule.companySkillService(db);
|
|
await withSkillActor(actor, () => baselineService!.list(companyId));
|
|
}
|
|
|
|
const exporter = new InMemorySpanExporter();
|
|
provider = new TracerProvider({ spanProcessors: [new SimpleSpanProcessor({ exporter })] });
|
|
manager = new AsyncLocalStorageContextManager().enable();
|
|
assert.ok(context.setGlobalContextManager(manager)); assert.ok(trace.setGlobalTracerProvider(provider));
|
|
process.env.OTEL_EXPORTER_OTLP_ENDPOINT = "http://127.0.0.1:4318";
|
|
const app = express();
|
|
app.use((req, res, next) => {
|
|
req.actor = actor;
|
|
void withRunExecutionTiming(randomUUID(), () => new Promise<void>(resolve => { res.once("finish", resolve); next(); })).catch(next);
|
|
});
|
|
const candidateService = companySkillService(db);
|
|
if (baselineService) app.get("/paired/:variant", skillAccessRequestContext, async (req, res) => {
|
|
const selected = req.params.variant === "baseline" ? baselineService! : candidateService;
|
|
res.json(await selected.list(companyId));
|
|
});
|
|
app.use("/api", companySkillRoutes(db));
|
|
app.use((error: Error, _req: express.Request, res: express.Response, _next: express.NextFunction) => res.status(500).json({ error: error.message }));
|
|
server = app.listen(0, "127.0.0.1");
|
|
await new Promise<void>(resolve => server!.once("listening", resolve));
|
|
const address = server.address(); assert.ok(address && typeof address !== "string");
|
|
const url = `http://127.0.0.1:${address.port}/api/companies/${companyId}/skills`;
|
|
let queryCount = 0;
|
|
db.$client.options.debug = () => { queryCount++; };
|
|
const request = async (etag?: string, requestUrl = url) => {
|
|
queryCount = 0; exporter.reset();
|
|
const started = performance.now();
|
|
const response = await new Promise<{ status: number; body: string; etag: string | undefined }>((resolve, reject) => {
|
|
const request = httpGet(requestUrl, { headers: etag ? { "if-none-match": etag } : {} }, response => {
|
|
const chunks: Buffer[] = [];
|
|
response.on("data", chunk => chunks.push(Buffer.from(chunk)));
|
|
response.on("error", reject);
|
|
response.on("end", () => resolve({ status: response.statusCode!, body: Buffer.concat(chunks).toString("utf8"), etag: response.headers.etag }));
|
|
});
|
|
request.setTimeout(20_000, () => request.destroy(new Error("Local benchmark request timed out")));
|
|
request.on("error", reject);
|
|
});
|
|
const body = response.body;
|
|
const elapsedMs = performance.now() - started;
|
|
assert.equal(response.status, etag ? 304 : 200, "Unexpected local skills-list HTTP status");
|
|
if (!etag) {
|
|
const items = JSON.parse(body) as Array<{ key: string; attachedAgentCount: number }>;
|
|
assert.ok(items.length >= 155);
|
|
assert.equal(items.find(item => item.key === skills[0]!.key)?.attachedAgentCount, 1);
|
|
assert.equal(items.find(item => item.key === skills[39]!.key)?.attachedAgentCount, 1);
|
|
assert.equal(items.find(item => item.key === skills[40]!.key)?.attachedAgentCount, 0);
|
|
}
|
|
await provider!.forceFlush();
|
|
return { elapsedMs, queryCount, status: response.status, etag: response.etag,
|
|
bodyHash: createHash("sha256").update(body).digest("hex"),
|
|
phases: exporter.getFinishedSpans().filter(span => span.name.startsWith("skills.")).map(span => ({ name: span.name,
|
|
durationMs: span.duration[0] * 1000 + span.duration[1] / 1e6 })) };
|
|
};
|
|
const first = await request(); assert.ok(first.etag);
|
|
const events: Array<Record<string, unknown>> = [], now = Date.now();
|
|
const capture = captureValues ? createFullDatabaseResponseCapture({ start: now - 1, end: now + 59_000 }, event => events.push(event)) : undefined;
|
|
if (capture) installDatabaseResponseCapture(db, schema, capture);
|
|
const samples = [];
|
|
try {
|
|
for (let i = 0; i < sampleCount; i++) {
|
|
const variants = baselineService ? (i % 2 ? ["candidate", "baseline"] : ["baseline", "candidate"]) : ["candidate"];
|
|
for (const variant of variants) {
|
|
const requestUrl = baselineService ? `http://127.0.0.1:${address.port}/paired/${variant}` : url;
|
|
const result = await request(i % 2 ? first.etag! : undefined, requestUrl);
|
|
if (result.status === 200) assert.equal(result.bodyHash, first.bodyHash, "Baseline and candidate must return the complete same list");
|
|
samples.push({ variant, pair: i, ...result });
|
|
console.error(JSON.stringify({ sample: i, variant, elapsedMs: result.elapsedMs, queries: result.queryCount, status: result.status }));
|
|
}
|
|
}
|
|
if (capture) assert.ok(Date.now() < now + 59_000, "Capture expired; repeat with fewer samples or less injected delay");
|
|
} finally { capture?.flush(); }
|
|
assert.equal(await sourceHash(), measuredSourceHash, "Source changed during measurement");
|
|
const timings = samples.map(sample => sample.elapsedMs).sort((a, b) => a - b);
|
|
const summary = events.find(event => event.event === "paperclip.test_db_response_capture.summary.v1");
|
|
if (capture) assert.ok(summary && summary.inFlightAtEnd === 0 && summary.overflowCalls === 0);
|
|
const rowCount = (await db.select().from(companySkills).where(eq(companySkills.companyId, companyId))).length;
|
|
console.log(JSON.stringify({ schema: "paperclip.skill-list-benchmark.v1", sourceRevision, measuredSourceHash,
|
|
fixture: { remoteSkills: 155, totalSkills: rowCount, agents: 40, nonterminatedAgents: 39, projects: 14, sources: 10 },
|
|
captureValues, networkDelayMsPerDirection: delayMs, postgresVersion: await db.$client`select version()`,
|
|
boundary: "production router and real service; synthetic authenticated board actor; postgres-js response values",
|
|
medianMs: timings[Math.floor(timings.length / 2)], minMs: timings[0], maxMs: timings.at(-1),
|
|
baselineRevision, baselineSourceHash,
|
|
baselineDependencyScope: baselineService ? "Only company-skills.ts differs; imported helpers and runtime dependencies use this checkout for both versions" : null,
|
|
variantResults: ["baseline", "candidate"].flatMap(variant => {
|
|
const selected = samples.filter(sample => sample.variant === variant);
|
|
if (!selected.length) return [];
|
|
const durations = selected.map(sample => sample.elapsedMs).sort((a, b) => a - b);
|
|
return [{ variant, samples: selected.length, medianMs: durations[Math.floor(durations.length / 2)], minMs: durations[0], maxMs: durations.at(-1) }];
|
|
}),
|
|
samples, responseCapture: events }, null, 2));
|
|
} finally {
|
|
const failures: unknown[] = [];
|
|
const clean = async (operation: () => Promise<unknown>) => { try { await operation(); } catch (error) { failures.push(error); } };
|
|
await clean(async () => { if (baselineFile) await fs.rm(baselineFile, { force: true }); });
|
|
await clean(async () => { if (server) { server.closeAllConnections(); await new Promise<void>(resolve => server!.close(() => resolve())); } });
|
|
await clean(async () => db?.$client.end({ timeout: 1 }));
|
|
for (const socket of sockets) socket.destroy();
|
|
await clean(async () => { if (proxy) await new Promise<void>(resolve => proxy!.close(() => resolve())); });
|
|
await clean(async () => database?.cleanup());
|
|
await clean(async () => provider?.shutdown());
|
|
trace.disable(); context.disable(); manager?.disable();
|
|
for (const [name, value] of Object.entries({ PAPERCLIP_HOME: previous.home, PAPERCLIP_INSTANCE_ID: previous.instance, OTEL_EXPORTER_OTLP_ENDPOINT: previous.endpoint })) {
|
|
if (value === undefined) delete process.env[name]; else process.env[name] = value;
|
|
}
|
|
await clean(() => fs.rm(home, { recursive: true, force: true }));
|
|
if (failures.length) throw new AggregateError(failures, "Local benchmark cleanup failed");
|
|
}
|