/** * Captures packed project starts for one template, optionally reusing one exact release closure across sets. * Packages and the generated application install outside the workspace; a finite * registry scope owns setup, and measured starts own separate process groups. * Evidence retains fixture identities and every attempt; historical baseline * evidence and restart preparation remain separate from each certification set. */ import { resolve } from "node:path"; import { rm } from "node:fs/promises"; import { ownedNativePromise } from "@relkit/cli/tooling"; import { Effect, Layer } from "effect"; import { preparePackedFixture } from "./dev-readiness/packed-fixture.js"; import { packedStartRequest, measurePackedSet } from "./dev-readiness/benchmark-packed-measure.js"; import { benchmarkNativeLive, BenchmarkNative } from "./dev-readiness/benchmark-native.service.js"; import { readinessBenchmarkLive } from "./dev-readiness/benchmark.service.js"; import { benchmarkReportEffect } from "./dev-readiness/benchmark-report.js"; import { packedReadinessIdentity } from "./dev-readiness/benchmark-identity.js"; import { benchmarkHostIdentity } from "./dev-readiness/benchmark-host.js"; import { runBenchmarkMain } from "./dev-readiness/benchmark-main.js"; import { candidateReadiness, candidateSummary } from "./dev-readiness/benchmark-packed-summary.js"; import type { StartRequest } from "./dev-readiness/benchmark.types.js"; /** * Allocates an available loopback port before the measurement clock starts. * @returns The freed ephemeral port used by one isolated fixture. */ const allocatePort = Effect.fn("ReadinessBenchmark.allocatePort")(() => Effect.scoped( Effect.gen(function* () { const server = yield* Effect.acquireRelease( Effect.sync(() => Bun.serve({ hostname: "127.0.0.1", port: 0, fetch: () => new Response() }), ), (server) => Effect.promise(() => server.stop(true)), ); if (server.port === undefined) return yield* Effect.die(new Error("Probe did not allocate a port")); return server.port; }), ), ); /** * Installs a packed candidate and records the requested sequential command launches. * @returns Completion after measurement scopes, report I/O and registry shutdown. */ const main = Effect.fn("ReadinessBenchmark.packedCandidate")(function* () { const repository = resolve(import.meta.dir, ".."); const count = Number(option("--runs", "5")); if (!Number.isSafeInteger(count) || count < 1 || count > 100) return yield* Effect.die(new Error("--runs must be between 1 and 100")); const set = option("--set", "diagnostic"); if (set !== "diagnostic" && set !== "fresh" && set !== "restart") return yield* Effect.die(new Error("--set must be diagnostic, fresh, or restart")); const artifactDirectory = option("--artifacts", ""); if (process.argv.includes("--artifacts") && artifactDirectory === "") return yield* Effect.die(new Error("--artifacts requires a directory")); const backendPort = yield* allocatePort(); const inspectorPort = yield* allocatePort(); yield* Effect.logInfo("Preparing and installing the packed candidate fixture"); const fixture = yield* preparePackedFixture( repository, process.argv.includes("--no-examples") ? "no-examples" : "examples", artifactDirectory === "" ? undefined : artifactDirectory, option("--template", "minimal"), ); const request = yield* packedStartRequest(fixture.projectRoot, backendPort, inspectorPort); const identity = yield* packedReadinessIdentity(fixture.projectRoot, fixture.artifacts); const path = resolve( repository, option( "--output", `openspec/changes/fast-generated-dev-readiness/evidence/candidate-packed${fixture.examples === "no-examples" ? "-no-examples" : ""}.json`, ), ); const native = yield* BenchmarkNative; const { attempts, preparationAttempt } = yield* measurePackedSet( fixture, set, count, backendPort, inspectorPort, process.argv.includes("--cleanup"), (attempts, preparation) => native.report( path, JSON.stringify( packedReport(fixture, request, attempts, identity, set, preparation, count), null, 2, ) + "\n", ), ); const report = packedReport(fixture, request, attempts, identity, set, preparationAttempt, count); yield* (yield* BenchmarkNative).report(path, JSON.stringify(report, null, 2) + "\n"); yield* Effect.logInfo("Packed candidate recorded", { path, summary: report.summary }); if (process.argv.includes("--cleanup")) yield* ownedNativePromise("benchmark.cleanupFixture", () => rm(fixture.temporary, { recursive: true, force: true }), ); }); /** * Reads a driver setting without changing the measured command or project. * @param name - Literal driver flag. * @param fallback - Explicit default when the flag is omitted. * @returns Supplied option value or the documented default. */ function option(name: string, fallback: string): string { const index = process.argv.indexOf(name); return index < 0 ? fallback : (process.argv[index + 1] ?? fallback); } /** * Builds explicitly diagnostic or certification evidence without changing or omitting any measured attempt. * @param fixture - Actual outside-workspace packed installation identity. * @param request - Complete response contract used by every measured launch. * @param attempts - All launched attempts, including failed acquisition or cleanup. * @param identity - Captured executable and package identities of the installed candidate. * @returns Serializable report whose statistics retain strict per-run failure policy. */ function packedReport( fixture: Effect.Success>, request: StartRequest, attempts: readonly (Effect.Success> & { readonly projectRoot: string; })[], identity: Effect.Success>, set: "diagnostic" | "fresh" | "restart", preparationAttempt?: Effect.Success>, expectedRuns = attempts.length, ) { const summary = candidateSummary(attempts, preparationAttempt); const readinessOnly = process.argv.includes("--readiness-only"); let mode = "candidate-certification"; if (readinessOnly) mode = "candidate-readiness"; if (set === "diagnostic") mode = "candidate-diagnostic"; return { protocol: "relkit.dev-readiness", version: readinessOnly ? 2 : 1, mode, set, command: "bun dev", thresholdMs: 1_000, environment: benchmarkHostIdentity(), fixture, identity, request: { projectRoot: request.projectRoot, url: request.url, expectedBody: request.expectedBody, }, attempts, ...(preparationAttempt === undefined ? {} : { preparationAttempt }), expectedRuns, summary: { ...summary, passed: attempts.length === expectedRuns && summary.passed }, ...(readinessOnly ? { readiness: candidateReadiness(attempts, preparationAttempt, expectedRuns) } : {}), }; } if (import.meta.main) await runBenchmarkMain( Effect.scoped(main()).pipe( Effect.provide(readinessBenchmarkLive.pipe(Layer.provideMerge(benchmarkNativeLive))), ), );