@prismatic-io/lux 0.0.1 → 0.0.2-preview.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/answerers/persona/index.d.ts +2 -1
- package/lib/answerers/persona/index.d.ts.map +1 -1
- package/lib/answerers/persona/index.js +20 -5
- package/lib/answerers/persona/index.js.map +1 -1
- package/lib/answerers/terminal/index.js +1 -1
- package/lib/answerers/terminal/index.js.map +1 -1
- package/lib/assertions/authoring.d.ts +14 -0
- package/lib/assertions/authoring.d.ts.map +1 -0
- package/lib/assertions/authoring.js +3 -0
- package/lib/assertions/authoring.js.map +1 -0
- package/lib/assertions/core/helpers.d.ts +2 -7
- package/lib/assertions/core/helpers.d.ts.map +1 -1
- package/lib/assertions/core/helpers.js +2 -6
- package/lib/assertions/core/helpers.js.map +1 -1
- package/lib/assertions/core/index.d.ts.map +1 -1
- package/lib/assertions/core/index.js +2 -1
- package/lib/assertions/core/index.js.map +1 -1
- package/lib/assertions/core/resource-checks.d.ts +19 -0
- package/lib/assertions/core/resource-checks.d.ts.map +1 -1
- package/lib/assertions/core/resource-checks.js +32 -0
- package/lib/assertions/core/resource-checks.js.map +1 -1
- package/lib/authoring.d.ts +1 -0
- package/lib/authoring.d.ts.map +1 -1
- package/lib/authoring.js +1 -0
- package/lib/authoring.js.map +1 -1
- package/lib/cli/program.d.ts.map +1 -1
- package/lib/cli/program.js +12 -67
- package/lib/cli/program.js.map +1 -1
- package/lib/cli/render/reporter.d.ts.map +1 -1
- package/lib/cli/render/reporter.js +16 -3
- package/lib/cli/render/reporter.js.map +1 -1
- package/lib/cli/run-options.d.ts +1 -0
- package/lib/cli/run-options.d.ts.map +1 -1
- package/lib/cli/run-options.js.map +1 -1
- package/lib/cli/view.d.ts +13 -0
- package/lib/cli/view.d.ts.map +1 -0
- package/lib/cli/view.js +126 -0
- package/lib/cli/view.js.map +1 -0
- package/lib/core/action-events.d.ts +29 -0
- package/lib/core/action-events.d.ts.map +1 -0
- package/lib/core/action-events.js +51 -0
- package/lib/core/action-events.js.map +1 -0
- package/lib/core/agent-activity.d.ts +49 -0
- package/lib/core/agent-activity.d.ts.map +1 -0
- package/lib/core/agent-activity.js +206 -0
- package/lib/core/agent-activity.js.map +1 -0
- package/lib/core/agent-events.d.ts +26 -0
- package/lib/core/agent-events.d.ts.map +1 -0
- package/lib/core/agent-events.js +13 -0
- package/lib/core/agent-events.js.map +1 -0
- package/lib/core/artifact-evidence.d.ts +8 -0
- package/lib/core/artifact-evidence.d.ts.map +1 -1
- package/lib/core/artifact-evidence.js +36 -4
- package/lib/core/artifact-evidence.js.map +1 -1
- package/lib/core/assertion.d.ts +19 -9
- package/lib/core/assertion.d.ts.map +1 -1
- package/lib/core/assertion.js +26 -0
- package/lib/core/assertion.js.map +1 -1
- package/lib/core/case-authoring-state.d.ts +12 -0
- package/lib/core/case-authoring-state.d.ts.map +1 -0
- package/lib/core/case-authoring-state.js +43 -0
- package/lib/core/case-authoring-state.js.map +1 -0
- package/lib/core/case.d.ts +29 -1
- package/lib/core/case.d.ts.map +1 -1
- package/lib/core/case.js +14 -6
- package/lib/core/case.js.map +1 -1
- package/lib/core/driver-metrics.d.ts +98 -0
- package/lib/core/driver-metrics.d.ts.map +1 -0
- package/lib/core/driver-metrics.js +36 -0
- package/lib/core/driver-metrics.js.map +1 -0
- package/lib/core/driver.d.ts +3 -0
- package/lib/core/driver.d.ts.map +1 -1
- package/lib/core/driver.js.map +1 -1
- package/lib/core/events.d.ts +36 -0
- package/lib/core/events.d.ts.map +1 -1
- package/lib/core/events.js +12 -2
- package/lib/core/events.js.map +1 -1
- package/lib/core/experiment.d.ts +19 -0
- package/lib/core/experiment.d.ts.map +1 -1
- package/lib/core/experiment.js +2 -0
- package/lib/core/experiment.js.map +1 -1
- package/lib/core/index.d.ts +4 -0
- package/lib/core/index.d.ts.map +1 -1
- package/lib/core/index.js +4 -0
- package/lib/core/index.js.map +1 -1
- package/lib/core/lifecycle-fixtures.d.ts +3 -1
- package/lib/core/lifecycle-fixtures.d.ts.map +1 -1
- package/lib/core/lifecycle-fixtures.js +4 -2
- package/lib/core/lifecycle-fixtures.js.map +1 -1
- package/lib/core/lifecycle-hooks.d.ts +9 -0
- package/lib/core/lifecycle-hooks.d.ts.map +1 -1
- package/lib/core/lifecycle-hooks.js +8 -2
- package/lib/core/lifecycle-hooks.js.map +1 -1
- package/lib/core/run.d.ts +150 -0
- package/lib/core/run.d.ts.map +1 -1
- package/lib/core/run.js +9 -0
- package/lib/core/run.js.map +1 -1
- package/lib/core/wire.d.ts +12 -0
- package/lib/core/wire.d.ts.map +1 -1
- package/lib/core/wire.js +5 -1
- package/lib/core/wire.js.map +1 -1
- package/lib/drivers/claude-code/parse-events.d.ts.map +1 -1
- package/lib/drivers/claude-code/parse-events.js +73 -8
- package/lib/drivers/claude-code/parse-events.js.map +1 -1
- package/lib/drivers/codex/app-events.d.ts +9 -2
- package/lib/drivers/codex/app-events.d.ts.map +1 -1
- package/lib/drivers/codex/app-events.js +93 -12
- package/lib/drivers/codex/app-events.js.map +1 -1
- package/lib/drivers/codex/index.d.ts.map +1 -1
- package/lib/drivers/codex/index.js +10 -14
- package/lib/drivers/codex/index.js.map +1 -1
- package/lib/drivers/subprocess/index.d.ts.map +1 -1
- package/lib/drivers/subprocess/index.js +6 -0
- package/lib/drivers/subprocess/index.js.map +1 -1
- package/lib/index.d.ts +10 -6
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +10 -6
- package/lib/index.js.map +1 -1
- package/lib/orchestrator/annotation-store.d.ts.map +1 -1
- package/lib/orchestrator/annotation-store.js +6 -2
- package/lib/orchestrator/annotation-store.js.map +1 -1
- package/lib/orchestrator/case-extensions.d.ts +6 -0
- package/lib/orchestrator/case-extensions.d.ts.map +1 -0
- package/lib/orchestrator/case-extensions.js +28 -0
- package/lib/orchestrator/case-extensions.js.map +1 -0
- package/lib/orchestrator/compare.d.ts +13 -1
- package/lib/orchestrator/compare.d.ts.map +1 -1
- package/lib/orchestrator/compare.js +12 -0
- package/lib/orchestrator/compare.js.map +1 -1
- package/lib/orchestrator/config.d.ts.map +1 -1
- package/lib/orchestrator/config.js +0 -2
- package/lib/orchestrator/config.js.map +1 -1
- package/lib/orchestrator/doctor.d.ts.map +1 -1
- package/lib/orchestrator/doctor.js +5 -3
- package/lib/orchestrator/doctor.js.map +1 -1
- package/lib/orchestrator/experiment-contracts.d.ts +19 -0
- package/lib/orchestrator/experiment-contracts.d.ts.map +1 -1
- package/lib/orchestrator/experiment-evaluation.d.ts.map +1 -1
- package/lib/orchestrator/experiment-evaluation.js +17 -0
- package/lib/orchestrator/experiment-evaluation.js.map +1 -1
- package/lib/orchestrator/experiment-report.d.ts +19 -0
- package/lib/orchestrator/experiment-report.d.ts.map +1 -1
- package/lib/orchestrator/experiment-runs.d.ts +38 -0
- package/lib/orchestrator/experiment-runs.d.ts.map +1 -1
- package/lib/orchestrator/grader-experiment.d.ts +12 -0
- package/lib/orchestrator/grader-experiment.d.ts.map +1 -1
- package/lib/orchestrator/grading.d.ts +2 -1
- package/lib/orchestrator/grading.d.ts.map +1 -1
- package/lib/orchestrator/grading.js +16 -4
- package/lib/orchestrator/grading.js.map +1 -1
- package/lib/orchestrator/index.d.ts +1 -1
- package/lib/orchestrator/index.d.ts.map +1 -1
- package/lib/orchestrator/index.js +1 -1
- package/lib/orchestrator/index.js.map +1 -1
- package/lib/orchestrator/list-runs.d.ts +7 -1
- package/lib/orchestrator/list-runs.d.ts.map +1 -1
- package/lib/orchestrator/list-runs.js +50 -6
- package/lib/orchestrator/list-runs.js.map +1 -1
- package/lib/orchestrator/loader.d.ts.map +1 -1
- package/lib/orchestrator/loader.js +2 -0
- package/lib/orchestrator/loader.js.map +1 -1
- package/lib/orchestrator/orchestrator.d.ts +3 -0
- package/lib/orchestrator/orchestrator.d.ts.map +1 -1
- package/lib/orchestrator/orchestrator.js +42 -14
- package/lib/orchestrator/orchestrator.js.map +1 -1
- package/lib/orchestrator/owned-lock.d.ts.map +1 -1
- package/lib/orchestrator/owned-lock.js +52 -38
- package/lib/orchestrator/owned-lock.js.map +1 -1
- package/lib/orchestrator/regrade.d.ts +6 -0
- package/lib/orchestrator/regrade.d.ts.map +1 -1
- package/lib/orchestrator/regrade.js +8 -4
- package/lib/orchestrator/regrade.js.map +1 -1
- package/lib/orchestrator/run-execution.d.ts +2 -1
- package/lib/orchestrator/run-execution.d.ts.map +1 -1
- package/lib/orchestrator/run-execution.js +20 -5
- package/lib/orchestrator/run-execution.js.map +1 -1
- package/lib/orchestrator/suite-compare.d.ts +34 -0
- package/lib/orchestrator/suite-compare.d.ts.map +1 -1
- package/lib/orchestrator/suite-summary.d.ts +68 -0
- package/lib/orchestrator/suite-summary.d.ts.map +1 -1
- package/lib/orchestrator/suite-summary.js +45 -7
- package/lib/orchestrator/suite-summary.js.map +1 -1
- package/lib/previewer/artifacts.d.ts +25 -0
- package/lib/previewer/artifacts.d.ts.map +1 -0
- package/lib/previewer/artifacts.js +180 -0
- package/lib/previewer/artifacts.js.map +1 -0
- package/lib/previewer/server.d.ts +105 -0
- package/lib/previewer/server.d.ts.map +1 -0
- package/lib/previewer/server.js +542 -0
- package/lib/previewer/server.js.map +1 -0
- package/package.json +2 -2
- package/skills/lux/SKILL.md +7 -3
- package/skills/lux/references/cli.md +25 -2
- package/skills/lux/references/eval-authoring.md +158 -4
- package/skills/lux-answerer/SKILL.md +1 -1
- package/src/answerers/persona/index.ts +21 -4
- package/src/answerers/terminal/index.ts +1 -1
- package/src/assertions/authoring.ts +81 -0
- package/src/assertions/core/helpers.ts +3 -6
- package/src/assertions/core/index.ts +2 -0
- package/src/assertions/core/resource-checks.ts +35 -0
- package/src/authoring.ts +1 -0
- package/src/cli/program.ts +14 -93
- package/src/cli/render/reporter.ts +18 -3
- package/src/cli/run-options.ts +1 -0
- package/src/cli/view.ts +168 -0
- package/src/core/action-events.ts +59 -0
- package/src/core/agent-activity.ts +236 -0
- package/src/core/agent-events.ts +17 -0
- package/src/core/artifact-evidence.ts +47 -4
- package/src/core/assertion.ts +41 -22
- package/src/core/case-authoring-state.ts +65 -0
- package/src/core/case.ts +57 -6
- package/src/core/driver-metrics.ts +51 -0
- package/src/core/driver.ts +3 -0
- package/src/core/events.ts +14 -2
- package/src/core/experiment.ts +2 -0
- package/src/core/index.ts +4 -0
- package/src/core/lifecycle-fixtures.ts +7 -2
- package/src/core/lifecycle-hooks.ts +16 -2
- package/src/core/run.ts +9 -0
- package/src/core/wire.ts +5 -1
- package/src/drivers/claude-code/parse-events.ts +98 -12
- package/src/drivers/codex/app-events.ts +116 -16
- package/src/drivers/codex/index.ts +16 -14
- package/src/drivers/subprocess/index.ts +6 -0
- package/src/index.ts +50 -6
- package/src/orchestrator/annotation-store.ts +5 -1
- package/src/orchestrator/case-extensions.ts +44 -0
- package/src/orchestrator/compare.ts +18 -0
- package/src/orchestrator/config.ts +0 -2
- package/src/orchestrator/doctor.ts +5 -3
- package/src/orchestrator/experiment-evaluation.ts +23 -0
- package/src/orchestrator/grading.ts +21 -2
- package/src/orchestrator/index.ts +1 -0
- package/src/orchestrator/list-runs.ts +66 -5
- package/src/orchestrator/loader.ts +2 -0
- package/src/orchestrator/orchestrator.ts +86 -40
- package/src/orchestrator/owned-lock.ts +54 -34
- package/src/orchestrator/regrade.ts +8 -7
- package/src/orchestrator/run-execution.ts +24 -3
- package/src/orchestrator/suite-summary.ts +101 -8
- package/src/previewer/artifacts.ts +221 -0
- package/src/previewer/server.ts +765 -0
- package/viewer/app.css +2075 -0
- package/viewer/app.js +606 -0
- package/viewer/artifact-view.js +48 -0
- package/viewer/charts.js +86 -0
- package/viewer/detail.js +1214 -0
- package/viewer/format.js +129 -0
- package/viewer/index.html +151 -0
- package/lib/answerers/scripted/index.d.ts +0 -8
- package/lib/answerers/scripted/index.d.ts.map +0 -1
- package/lib/answerers/scripted/index.js +0 -163
- package/lib/answerers/scripted/index.js.map +0 -1
- package/src/answerers/scripted/index.ts +0 -186
|
@@ -0,0 +1,765 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http";
|
|
4
|
+
import { basename, relative, resolve, sep } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { type DriverMetricDescriptor, type DriverMetrics, exec } from "../core/index.js";
|
|
7
|
+
import {
|
|
8
|
+
compareRuns,
|
|
9
|
+
compareSuiteRuns,
|
|
10
|
+
type ListedRun,
|
|
11
|
+
listExperimentRuns,
|
|
12
|
+
listRuns,
|
|
13
|
+
listSuiteRuns,
|
|
14
|
+
loadExperimentCandidates,
|
|
15
|
+
loadRunArtifacts,
|
|
16
|
+
loadRunDetail,
|
|
17
|
+
loadSuiteSummary,
|
|
18
|
+
loadVerifiedExperimentDecisionSnapshot,
|
|
19
|
+
} from "../orchestrator/index.js";
|
|
20
|
+
import { compareArtifactPreviews, loadArtifactPreview } from "./artifacts.js";
|
|
21
|
+
|
|
22
|
+
const LOOPBACK_HOST = "127.0.0.1";
|
|
23
|
+
const ASSET_ROOT = fileURLToPath(new URL("../../viewer/", import.meta.url));
|
|
24
|
+
|
|
25
|
+
type PreviewKind = "run" | "suite" | "experiment";
|
|
26
|
+
type PreviewStatus = "passed" | "failed" | "running" | "aborted" | "error" | "ungraded";
|
|
27
|
+
|
|
28
|
+
type PreviewRun = {
|
|
29
|
+
id: string;
|
|
30
|
+
kind: "run";
|
|
31
|
+
status: PreviewStatus;
|
|
32
|
+
caseId: string;
|
|
33
|
+
driver: string;
|
|
34
|
+
agentId: string | null;
|
|
35
|
+
startedAt: string | null;
|
|
36
|
+
durationMs: number | null;
|
|
37
|
+
timeToFirstActionMs: number | null;
|
|
38
|
+
timeToFirstMutationMs: number | null;
|
|
39
|
+
timeToFirstDurableMutationMs: number | null;
|
|
40
|
+
driverMetrics: DriverMetrics;
|
|
41
|
+
subagentCount: number;
|
|
42
|
+
score: number | null;
|
|
43
|
+
assertions: { passed: number; total: number };
|
|
44
|
+
path: string;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
type PreviewSuite = {
|
|
48
|
+
id: string;
|
|
49
|
+
kind: "suite";
|
|
50
|
+
status: PreviewStatus;
|
|
51
|
+
suiteId: string;
|
|
52
|
+
startedAt: string;
|
|
53
|
+
endedAt: string | null;
|
|
54
|
+
concurrency: number;
|
|
55
|
+
profileId?: string | undefined;
|
|
56
|
+
items: Array<{
|
|
57
|
+
caseId: string;
|
|
58
|
+
canonicalCaseId: string;
|
|
59
|
+
status: string;
|
|
60
|
+
exitReason: string | null;
|
|
61
|
+
casePassed: boolean | null;
|
|
62
|
+
runId: string | null;
|
|
63
|
+
errorMessage?: string | undefined;
|
|
64
|
+
}>;
|
|
65
|
+
path: string;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
type PreviewExperiment = {
|
|
69
|
+
id: string;
|
|
70
|
+
kind: "experiment";
|
|
71
|
+
status: PreviewStatus;
|
|
72
|
+
experimentId: string;
|
|
73
|
+
campaignId: string;
|
|
74
|
+
mode: "experiment" | "optimize";
|
|
75
|
+
startedAt: string;
|
|
76
|
+
endedAt: string | null;
|
|
77
|
+
activeTimeMs: number;
|
|
78
|
+
candidateCount: number;
|
|
79
|
+
championCandidateId: string | null;
|
|
80
|
+
budget: {
|
|
81
|
+
metricCalls: number;
|
|
82
|
+
judgeCalls: number;
|
|
83
|
+
proposalCalls: number;
|
|
84
|
+
costUsd: number;
|
|
85
|
+
};
|
|
86
|
+
evaluations: Array<{
|
|
87
|
+
candidateId: string;
|
|
88
|
+
profileId: string;
|
|
89
|
+
split: "train" | "validation" | "test";
|
|
90
|
+
passRate: number;
|
|
91
|
+
metrics: Record<string, number>;
|
|
92
|
+
metricDefinitions: Record<string, DriverMetricDescriptor>;
|
|
93
|
+
failedCases: Array<{ caseId: string; score: number | null; feedback: string[] }>;
|
|
94
|
+
}>;
|
|
95
|
+
path: string;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
export type PreviewSnapshot = {
|
|
99
|
+
generatedAt: string;
|
|
100
|
+
runsRoot: string;
|
|
101
|
+
initial: { kind: PreviewKind; id: string } | null;
|
|
102
|
+
runs: PreviewRun[];
|
|
103
|
+
suites: PreviewSuite[];
|
|
104
|
+
experiments: PreviewExperiment[];
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
type PreviewAssets = {
|
|
108
|
+
html: Buffer;
|
|
109
|
+
files: Map<string, { body: Buffer; contentType: string }>;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export type PreviewServer = {
|
|
113
|
+
url: string;
|
|
114
|
+
closed: Promise<void>;
|
|
115
|
+
close: () => Promise<void>;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
export type StartPreviewServerOptions = {
|
|
119
|
+
runsRoot: string;
|
|
120
|
+
port?: number | undefined;
|
|
121
|
+
initialPath?: string | undefined;
|
|
122
|
+
initialKind?: PreviewKind | undefined;
|
|
123
|
+
caseFilter?: string | undefined;
|
|
124
|
+
signal?: AbortSignal | undefined;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const opaqueId = (runsRoot: string, path: string): string =>
|
|
128
|
+
Buffer.from(relative(resolve(runsRoot), resolve(path))).toString("base64url");
|
|
129
|
+
|
|
130
|
+
const displayPath = (runsRoot: string, path: string): string => {
|
|
131
|
+
const value = relative(resolve(runsRoot), resolve(path));
|
|
132
|
+
return value.split(sep).join("/");
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const runStatus = (run: ListedRun): PreviewStatus => {
|
|
136
|
+
if (run.exitReason && run.exitReason !== "done") return "error";
|
|
137
|
+
if (run.casePassed === true) return "passed";
|
|
138
|
+
if (run.casePassed === false) return "failed";
|
|
139
|
+
return "ungraded";
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const normalizeStatus = (status: string): PreviewStatus => {
|
|
143
|
+
if (status === "passed" || status === "failed" || status === "running" || status === "aborted") {
|
|
144
|
+
return status;
|
|
145
|
+
}
|
|
146
|
+
return "ungraded";
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const buildSnapshot = async (
|
|
150
|
+
runsRoot: string,
|
|
151
|
+
options: Pick<StartPreviewServerOptions, "initialPath" | "initialKind" | "caseFilter">,
|
|
152
|
+
): Promise<PreviewSnapshot> => {
|
|
153
|
+
const absoluteRunsRoot = resolve(runsRoot);
|
|
154
|
+
const [listedRuns, listedSuites, listedExperiments] = await Promise.all([
|
|
155
|
+
listRuns(absoluteRunsRoot),
|
|
156
|
+
listSuiteRuns(absoluteRunsRoot),
|
|
157
|
+
listExperimentRuns(absoluteRunsRoot),
|
|
158
|
+
]);
|
|
159
|
+
const runs = listedRuns
|
|
160
|
+
.filter((run) => !options.caseFilter || run.caseSlug === options.caseFilter)
|
|
161
|
+
.map(
|
|
162
|
+
(run): PreviewRun => ({
|
|
163
|
+
id: opaqueId(absoluteRunsRoot, run.runDir),
|
|
164
|
+
kind: "run",
|
|
165
|
+
status: runStatus(run),
|
|
166
|
+
caseId: run.caseSlug,
|
|
167
|
+
driver: run.driverName,
|
|
168
|
+
agentId: run.driverAgentId,
|
|
169
|
+
startedAt: run.startedAt,
|
|
170
|
+
durationMs: run.durationMs,
|
|
171
|
+
timeToFirstActionMs: run.timeToFirstActionMs,
|
|
172
|
+
timeToFirstMutationMs: run.timeToFirstMutationMs,
|
|
173
|
+
timeToFirstDurableMutationMs: run.timeToFirstDurableMutationMs,
|
|
174
|
+
driverMetrics: run.driverMetrics,
|
|
175
|
+
subagentCount: run.subagentCount,
|
|
176
|
+
score: run.caseScore,
|
|
177
|
+
assertions: { passed: run.assertionPassed, total: run.assertionTotal },
|
|
178
|
+
path: displayPath(absoluteRunsRoot, run.runDir),
|
|
179
|
+
}),
|
|
180
|
+
);
|
|
181
|
+
const runIdsByPath = new Map(
|
|
182
|
+
listedRuns.map((run) => [resolve(run.runDir), opaqueId(absoluteRunsRoot, run.runDir)]),
|
|
183
|
+
);
|
|
184
|
+
const suites = listedSuites.map(
|
|
185
|
+
(suite): PreviewSuite => ({
|
|
186
|
+
id: opaqueId(absoluteRunsRoot, suite.suiteDir),
|
|
187
|
+
kind: "suite",
|
|
188
|
+
status: normalizeStatus(suite.status),
|
|
189
|
+
suiteId: suite.suiteId,
|
|
190
|
+
startedAt: suite.startedAt,
|
|
191
|
+
endedAt: suite.endedAt,
|
|
192
|
+
concurrency: suite.concurrency,
|
|
193
|
+
...(suite.profileId ? { profileId: suite.profileId } : {}),
|
|
194
|
+
items: suite.items.map((item) => ({
|
|
195
|
+
caseId: item.caseId,
|
|
196
|
+
canonicalCaseId: item.canonicalCaseId,
|
|
197
|
+
status: item.status,
|
|
198
|
+
exitReason: item.exitReason,
|
|
199
|
+
casePassed: item.casePassed,
|
|
200
|
+
runId: item.runDir
|
|
201
|
+
? (runIdsByPath.get(resolve(suite.suiteDir, item.runDir)) ?? null)
|
|
202
|
+
: null,
|
|
203
|
+
...(item.errorMessage ? { errorMessage: item.errorMessage } : {}),
|
|
204
|
+
})),
|
|
205
|
+
path: displayPath(absoluteRunsRoot, suite.suiteDir),
|
|
206
|
+
}),
|
|
207
|
+
);
|
|
208
|
+
const experiments = listedExperiments.map(
|
|
209
|
+
(experiment): PreviewExperiment => ({
|
|
210
|
+
id: opaqueId(absoluteRunsRoot, experiment.experimentDir),
|
|
211
|
+
kind: "experiment",
|
|
212
|
+
status: normalizeStatus(experiment.status),
|
|
213
|
+
experimentId: experiment.experimentId,
|
|
214
|
+
campaignId: experiment.campaignId,
|
|
215
|
+
mode: experiment.mode,
|
|
216
|
+
startedAt: experiment.startedAt,
|
|
217
|
+
endedAt: experiment.endedAt,
|
|
218
|
+
activeTimeMs: experiment.activeTimeMs,
|
|
219
|
+
candidateCount: experiment.candidates.length,
|
|
220
|
+
championCandidateId: experiment.championCandidateId ?? null,
|
|
221
|
+
budget: {
|
|
222
|
+
metricCalls: experiment.budgetUsage.metricCalls,
|
|
223
|
+
judgeCalls: experiment.budgetUsage.judgeCalls,
|
|
224
|
+
proposalCalls: experiment.budgetUsage.proposalCalls,
|
|
225
|
+
costUsd: experiment.budgetUsage.costUsd,
|
|
226
|
+
},
|
|
227
|
+
evaluations: experiment.evaluations.map((evaluation) => ({
|
|
228
|
+
candidateId: evaluation.candidateId,
|
|
229
|
+
profileId: evaluation.profileId,
|
|
230
|
+
split: evaluation.split,
|
|
231
|
+
passRate: evaluation.passRate,
|
|
232
|
+
metrics: evaluation.metrics,
|
|
233
|
+
metricDefinitions: evaluation.metricDefinitions ?? {},
|
|
234
|
+
failedCases: evaluation.failedCases,
|
|
235
|
+
})),
|
|
236
|
+
path: displayPath(absoluteRunsRoot, experiment.experimentDir),
|
|
237
|
+
}),
|
|
238
|
+
);
|
|
239
|
+
const initialPath = options.initialPath ? resolve(options.initialPath) : null;
|
|
240
|
+
const initialKind = options.initialKind;
|
|
241
|
+
const candidates: Array<PreviewRun | PreviewSuite | PreviewExperiment> = [
|
|
242
|
+
...runs,
|
|
243
|
+
...suites,
|
|
244
|
+
...experiments,
|
|
245
|
+
];
|
|
246
|
+
let initialEntry: (typeof candidates)[number] | undefined;
|
|
247
|
+
if (initialPath) {
|
|
248
|
+
initialEntry = candidates.find(
|
|
249
|
+
(entry) =>
|
|
250
|
+
(!initialKind || entry.kind === initialKind) &&
|
|
251
|
+
resolve(absoluteRunsRoot, entry.path) === initialPath,
|
|
252
|
+
);
|
|
253
|
+
} else if (initialKind) {
|
|
254
|
+
initialEntry = candidates.find((entry) => entry.kind === initialKind);
|
|
255
|
+
}
|
|
256
|
+
return {
|
|
257
|
+
generatedAt: new Date().toISOString(),
|
|
258
|
+
runsRoot: absoluteRunsRoot,
|
|
259
|
+
initial: initialEntry ? { kind: initialEntry.kind, id: initialEntry.id } : null,
|
|
260
|
+
runs,
|
|
261
|
+
suites,
|
|
262
|
+
experiments,
|
|
263
|
+
};
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
const securityHeaders = {
|
|
267
|
+
"Content-Security-Policy":
|
|
268
|
+
"default-src 'self'; base-uri 'none'; connect-src 'self'; form-action 'none'; frame-ancestors 'none'; img-src 'self' data:; object-src 'none'; script-src 'self'; style-src 'self'",
|
|
269
|
+
"Cross-Origin-Opener-Policy": "same-origin",
|
|
270
|
+
"Cross-Origin-Resource-Policy": "same-origin",
|
|
271
|
+
"Permissions-Policy": "camera=(), geolocation=(), microphone=()",
|
|
272
|
+
"Referrer-Policy": "no-referrer",
|
|
273
|
+
"X-Content-Type-Options": "nosniff",
|
|
274
|
+
"X-Frame-Options": "DENY",
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
const send = (
|
|
278
|
+
request: IncomingMessage,
|
|
279
|
+
response: ServerResponse,
|
|
280
|
+
status: number,
|
|
281
|
+
contentType: string,
|
|
282
|
+
body: Buffer | string,
|
|
283
|
+
cacheControl = "no-store",
|
|
284
|
+
): void => {
|
|
285
|
+
const bytes = Buffer.isBuffer(body) ? body : Buffer.from(body);
|
|
286
|
+
response.writeHead(status, {
|
|
287
|
+
...securityHeaders,
|
|
288
|
+
"Cache-Control": cacheControl,
|
|
289
|
+
"Content-Length": bytes.length,
|
|
290
|
+
"Content-Type": contentType,
|
|
291
|
+
});
|
|
292
|
+
response.end(request.method === "HEAD" ? undefined : bytes);
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
const sendJson = (
|
|
296
|
+
request: IncomingMessage,
|
|
297
|
+
response: ServerResponse,
|
|
298
|
+
status: number,
|
|
299
|
+
value: unknown,
|
|
300
|
+
): void => {
|
|
301
|
+
send(request, response, status, "application/json; charset=utf-8", JSON.stringify(value));
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
const findRunById = async (runsRoot: string, id: string): Promise<ListedRun | null> => {
|
|
305
|
+
const runs = await listRuns(runsRoot);
|
|
306
|
+
return runs.find((run) => opaqueId(runsRoot, run.runDir) === id) ?? null;
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
const findRunsById = async (
|
|
310
|
+
runsRoot: string,
|
|
311
|
+
ids: readonly string[],
|
|
312
|
+
): Promise<ListedRun[] | null> => {
|
|
313
|
+
const runs = await listRuns(runsRoot);
|
|
314
|
+
const found = ids.map((id) => runs.find((run) => opaqueId(runsRoot, run.runDir) === id) ?? null);
|
|
315
|
+
return found.every((run): run is ListedRun => run !== null) ? found : null;
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
const findSuiteById = async (
|
|
319
|
+
runsRoot: string,
|
|
320
|
+
id: string,
|
|
321
|
+
): Promise<Awaited<ReturnType<typeof listSuiteRuns>>[number] | null> => {
|
|
322
|
+
const suites = await listSuiteRuns(runsRoot);
|
|
323
|
+
return suites.find((suite) => opaqueId(runsRoot, suite.suiteDir) === id) ?? null;
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
const findSuitesById = async (
|
|
327
|
+
runsRoot: string,
|
|
328
|
+
ids: readonly string[],
|
|
329
|
+
): Promise<Awaited<ReturnType<typeof listSuiteRuns>> | null> => {
|
|
330
|
+
const suites = await listSuiteRuns(runsRoot);
|
|
331
|
+
const found = ids.map(
|
|
332
|
+
(id) => suites.find((suite) => opaqueId(runsRoot, suite.suiteDir) === id) ?? null,
|
|
333
|
+
);
|
|
334
|
+
return found.every(
|
|
335
|
+
(suite): suite is Awaited<ReturnType<typeof listSuiteRuns>>[number] => suite !== null,
|
|
336
|
+
)
|
|
337
|
+
? found
|
|
338
|
+
: null;
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
const findExperimentById = async (
|
|
342
|
+
runsRoot: string,
|
|
343
|
+
id: string,
|
|
344
|
+
): Promise<Awaited<ReturnType<typeof listExperimentRuns>>[number] | null> => {
|
|
345
|
+
const experiments = await listExperimentRuns(runsRoot);
|
|
346
|
+
return (
|
|
347
|
+
experiments.find((experiment) => opaqueId(runsRoot, experiment.experimentDir) === id) ?? null
|
|
348
|
+
);
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
const artifactPreview = async (run: ListedRun, path: string) => {
|
|
352
|
+
const [detail, indexed] = await Promise.all([
|
|
353
|
+
loadRunDetail(run.runDir, { maxArtifacts: 2_000 }),
|
|
354
|
+
loadRunArtifacts(run.runDir),
|
|
355
|
+
]);
|
|
356
|
+
return loadArtifactPreview(run.runDir, indexed, detail.artifacts, path);
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
const publicRunDetail = async (runsRoot: string, runDir: string) => ({
|
|
360
|
+
...(await loadRunDetail(runDir, { maxArtifacts: 2_000 })),
|
|
361
|
+
runDir: displayPath(runsRoot, runDir),
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
const requiredPair = (url: URL): [string, string] | null => {
|
|
365
|
+
const before = url.searchParams.get("before");
|
|
366
|
+
const after = url.searchParams.get("after");
|
|
367
|
+
return before && after && before !== after ? [before, after] : null;
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
const isLoopbackHost = (host: string | undefined): boolean =>
|
|
371
|
+
host === LOOPBACK_HOST || host?.startsWith(`${LOOPBACK_HOST}:`) === true;
|
|
372
|
+
|
|
373
|
+
type RouteContext = {
|
|
374
|
+
request: IncomingMessage;
|
|
375
|
+
response: ServerResponse;
|
|
376
|
+
url: URL;
|
|
377
|
+
runsRoot: string;
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
const handleRunComparison = async ({
|
|
381
|
+
request,
|
|
382
|
+
response,
|
|
383
|
+
url,
|
|
384
|
+
runsRoot,
|
|
385
|
+
}: RouteContext): Promise<void> => {
|
|
386
|
+
const pair = requiredPair(url);
|
|
387
|
+
if (!pair) {
|
|
388
|
+
sendJson(request, response, 400, { error: "two distinct run ids are required" });
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
const runs = await findRunsById(runsRoot, pair);
|
|
392
|
+
if (!runs) {
|
|
393
|
+
sendJson(request, response, 404, { error: "run not found" });
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
const [before, after] = runs;
|
|
397
|
+
if (!before || !after) throw new Error("run pair resolution failed");
|
|
398
|
+
const [comparison, beforeDetail, afterDetail] = await Promise.all([
|
|
399
|
+
compareRuns({ pathA: before.runDir, pathB: after.runDir }),
|
|
400
|
+
publicRunDetail(runsRoot, before.runDir),
|
|
401
|
+
publicRunDetail(runsRoot, after.runDir),
|
|
402
|
+
]);
|
|
403
|
+
sendJson(request, response, 200, {
|
|
404
|
+
kind: "run",
|
|
405
|
+
comparison: {
|
|
406
|
+
...comparison,
|
|
407
|
+
pathA: displayPath(runsRoot, before.runDir),
|
|
408
|
+
pathB: displayPath(runsRoot, after.runDir),
|
|
409
|
+
},
|
|
410
|
+
before: beforeDetail,
|
|
411
|
+
after: afterDetail,
|
|
412
|
+
});
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
const handleSuiteComparison = async ({
|
|
416
|
+
request,
|
|
417
|
+
response,
|
|
418
|
+
url,
|
|
419
|
+
runsRoot,
|
|
420
|
+
}: RouteContext): Promise<void> => {
|
|
421
|
+
const pair = requiredPair(url);
|
|
422
|
+
if (!pair) {
|
|
423
|
+
sendJson(request, response, 400, { error: "two distinct suite ids are required" });
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
const suites = await findSuitesById(runsRoot, pair);
|
|
427
|
+
if (!suites) {
|
|
428
|
+
sendJson(request, response, 404, { error: "suite not found" });
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
const [before, after] = suites;
|
|
432
|
+
if (!before || !after) throw new Error("suite pair resolution failed");
|
|
433
|
+
const [comparison, beforeSummary, afterSummary] = await Promise.all([
|
|
434
|
+
compareSuiteRuns(before.suiteDir, after.suiteDir),
|
|
435
|
+
loadSuiteSummary(before.suiteDir),
|
|
436
|
+
loadSuiteSummary(after.suiteDir),
|
|
437
|
+
]);
|
|
438
|
+
sendJson(request, response, 200, {
|
|
439
|
+
kind: "suite",
|
|
440
|
+
comparison: {
|
|
441
|
+
...comparison,
|
|
442
|
+
pathA: displayPath(runsRoot, before.suiteDir),
|
|
443
|
+
pathB: displayPath(runsRoot, after.suiteDir),
|
|
444
|
+
},
|
|
445
|
+
before: beforeSummary,
|
|
446
|
+
after: afterSummary,
|
|
447
|
+
});
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
const handleArtifactComparison = async ({
|
|
451
|
+
request,
|
|
452
|
+
response,
|
|
453
|
+
url,
|
|
454
|
+
runsRoot,
|
|
455
|
+
}: RouteContext): Promise<void> => {
|
|
456
|
+
const pair = requiredPair(url);
|
|
457
|
+
const path = url.searchParams.get("path");
|
|
458
|
+
if (!pair || !path) {
|
|
459
|
+
sendJson(request, response, 400, {
|
|
460
|
+
error: "two distinct run ids and an artifact path are required",
|
|
461
|
+
});
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
const runs = await findRunsById(runsRoot, pair);
|
|
465
|
+
if (!runs) {
|
|
466
|
+
sendJson(request, response, 404, { error: "run not found" });
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
const [before, after] = runs;
|
|
470
|
+
if (!before || !after) throw new Error("artifact run pair resolution failed");
|
|
471
|
+
const [beforePreview, afterPreview] = await Promise.all([
|
|
472
|
+
artifactPreview(before, path),
|
|
473
|
+
artifactPreview(after, path),
|
|
474
|
+
]);
|
|
475
|
+
sendJson(request, response, 200, compareArtifactPreviews(path, beforePreview, afterPreview));
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
const handleComparisonRoute = async (context: RouteContext): Promise<boolean> => {
|
|
479
|
+
if (context.url.pathname === "/api/compare/runs") {
|
|
480
|
+
await handleRunComparison(context);
|
|
481
|
+
return true;
|
|
482
|
+
}
|
|
483
|
+
if (context.url.pathname === "/api/compare/suites") {
|
|
484
|
+
await handleSuiteComparison(context);
|
|
485
|
+
return true;
|
|
486
|
+
}
|
|
487
|
+
if (context.url.pathname === "/api/compare/artifacts") {
|
|
488
|
+
await handleArtifactComparison(context);
|
|
489
|
+
return true;
|
|
490
|
+
}
|
|
491
|
+
return false;
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
const handleArtifactRoute = async ({
|
|
495
|
+
request,
|
|
496
|
+
response,
|
|
497
|
+
url,
|
|
498
|
+
runsRoot,
|
|
499
|
+
}: RouteContext): Promise<boolean> => {
|
|
500
|
+
if (url.pathname !== "/api/artifact") return false;
|
|
501
|
+
const runId = url.searchParams.get("run");
|
|
502
|
+
const path = url.searchParams.get("path");
|
|
503
|
+
if (!runId || !path) {
|
|
504
|
+
sendJson(request, response, 400, { error: "a run id and artifact path are required" });
|
|
505
|
+
return true;
|
|
506
|
+
}
|
|
507
|
+
const run = await findRunById(runsRoot, runId);
|
|
508
|
+
if (!run) {
|
|
509
|
+
sendJson(request, response, 404, { error: "run not found" });
|
|
510
|
+
return true;
|
|
511
|
+
}
|
|
512
|
+
const preview = await artifactPreview(run, path);
|
|
513
|
+
sendJson(request, response, preview.exists ? 200 : 404, preview);
|
|
514
|
+
return true;
|
|
515
|
+
};
|
|
516
|
+
|
|
517
|
+
const experimentDetail = async (
|
|
518
|
+
runsRoot: string,
|
|
519
|
+
experiment: NonNullable<Awaited<ReturnType<typeof findExperimentById>>>,
|
|
520
|
+
): Promise<Record<string, unknown>> => {
|
|
521
|
+
const [candidateResult, decisionResult] = await Promise.allSettled([
|
|
522
|
+
loadExperimentCandidates(experiment.experimentDir),
|
|
523
|
+
loadVerifiedExperimentDecisionSnapshot(experiment.experimentDir),
|
|
524
|
+
]);
|
|
525
|
+
const integrityErrors = [candidateResult, decisionResult].flatMap((result) =>
|
|
526
|
+
result.status === "rejected"
|
|
527
|
+
? [result.reason instanceof Error ? result.reason.message : String(result.reason)]
|
|
528
|
+
: [],
|
|
529
|
+
);
|
|
530
|
+
return {
|
|
531
|
+
experiment: {
|
|
532
|
+
experimentId: experiment.experimentId,
|
|
533
|
+
campaignId: experiment.campaignId,
|
|
534
|
+
mode: experiment.mode,
|
|
535
|
+
evaluationKind: experiment.evaluationKind,
|
|
536
|
+
status: experiment.status,
|
|
537
|
+
startedAt: experiment.startedAt,
|
|
538
|
+
endedAt: experiment.endedAt,
|
|
539
|
+
activeTimeMs: experiment.activeTimeMs,
|
|
540
|
+
championCandidateId: experiment.championCandidateId ?? null,
|
|
541
|
+
errorMessage: experiment.errorMessage ?? null,
|
|
542
|
+
selection: experiment.selection ?? null,
|
|
543
|
+
budgetUsage: experiment.budgetUsage,
|
|
544
|
+
evaluations: experiment.evaluations.map((evaluation) => ({
|
|
545
|
+
candidateId: evaluation.candidateId,
|
|
546
|
+
profileId: evaluation.profileId,
|
|
547
|
+
split: evaluation.split,
|
|
548
|
+
repetitions: evaluation.repetitions,
|
|
549
|
+
passRate: evaluation.passRate,
|
|
550
|
+
casePassRates: evaluation.casePassRates,
|
|
551
|
+
metrics: evaluation.metrics,
|
|
552
|
+
metricDefinitions: evaluation.metricDefinitions ?? {},
|
|
553
|
+
costUsd: evaluation.costUsd ?? null,
|
|
554
|
+
failedCases: evaluation.failedCases,
|
|
555
|
+
})),
|
|
556
|
+
},
|
|
557
|
+
candidates:
|
|
558
|
+
candidateResult.status === "fulfilled"
|
|
559
|
+
? candidateResult.value.map((candidate) => ({
|
|
560
|
+
id: candidate.id,
|
|
561
|
+
parentIds: candidate.parentIds,
|
|
562
|
+
createdAt: candidate.createdAt,
|
|
563
|
+
proposer: candidate.proposer,
|
|
564
|
+
sourceBytes: candidate.sourceBytes,
|
|
565
|
+
rationale: candidate.rationale ?? null,
|
|
566
|
+
components: Object.keys(candidate.components).sort(),
|
|
567
|
+
}))
|
|
568
|
+
: [],
|
|
569
|
+
decision:
|
|
570
|
+
decisionResult.status === "fulfilled"
|
|
571
|
+
? {
|
|
572
|
+
...decisionResult.value.report,
|
|
573
|
+
experimentDir: displayPath(runsRoot, experiment.experimentDir),
|
|
574
|
+
}
|
|
575
|
+
: null,
|
|
576
|
+
integrity: {
|
|
577
|
+
verified: integrityErrors.length === 0,
|
|
578
|
+
errors: integrityErrors,
|
|
579
|
+
},
|
|
580
|
+
};
|
|
581
|
+
};
|
|
582
|
+
|
|
583
|
+
const handleDetailRoute = async ({
|
|
584
|
+
request,
|
|
585
|
+
response,
|
|
586
|
+
url,
|
|
587
|
+
runsRoot,
|
|
588
|
+
}: RouteContext): Promise<boolean> => {
|
|
589
|
+
if (url.pathname.startsWith("/api/suites/")) {
|
|
590
|
+
const suite = await findSuiteById(runsRoot, url.pathname.slice("/api/suites/".length));
|
|
591
|
+
if (!suite) sendJson(request, response, 404, { error: "suite not found" });
|
|
592
|
+
else
|
|
593
|
+
sendJson(request, response, 200, {
|
|
594
|
+
suite: {
|
|
595
|
+
...suite,
|
|
596
|
+
suiteDir: displayPath(runsRoot, suite.suiteDir),
|
|
597
|
+
},
|
|
598
|
+
summary: await loadSuiteSummary(suite.suiteDir),
|
|
599
|
+
});
|
|
600
|
+
return true;
|
|
601
|
+
}
|
|
602
|
+
if (url.pathname.startsWith("/api/experiments/")) {
|
|
603
|
+
const experiment = await findExperimentById(
|
|
604
|
+
runsRoot,
|
|
605
|
+
url.pathname.slice("/api/experiments/".length),
|
|
606
|
+
);
|
|
607
|
+
if (!experiment) sendJson(request, response, 404, { error: "experiment not found" });
|
|
608
|
+
else sendJson(request, response, 200, await experimentDetail(runsRoot, experiment));
|
|
609
|
+
return true;
|
|
610
|
+
}
|
|
611
|
+
if (url.pathname.startsWith("/api/runs/")) {
|
|
612
|
+
const run = await findRunById(runsRoot, url.pathname.slice("/api/runs/".length));
|
|
613
|
+
if (!run) sendJson(request, response, 404, { error: "run not found" });
|
|
614
|
+
else sendJson(request, response, 200, await publicRunDetail(runsRoot, run.runDir));
|
|
615
|
+
return true;
|
|
616
|
+
}
|
|
617
|
+
return false;
|
|
618
|
+
};
|
|
619
|
+
|
|
620
|
+
const requestHandler =
|
|
621
|
+
(
|
|
622
|
+
assets: PreviewAssets,
|
|
623
|
+
runsRoot: string,
|
|
624
|
+
options: Pick<StartPreviewServerOptions, "initialPath" | "initialKind" | "caseFilter">,
|
|
625
|
+
) =>
|
|
626
|
+
async (request: IncomingMessage, response: ServerResponse): Promise<void> => {
|
|
627
|
+
if (!isLoopbackHost(request.headers.host)) {
|
|
628
|
+
sendJson(request, response, 421, { error: "untrusted host" });
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
if (request.method !== "GET" && request.method !== "HEAD") {
|
|
632
|
+
sendJson(request, response, 405, { error: "method not allowed" });
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
const url = new URL(request.url ?? "/", `http://${LOOPBACK_HOST}`);
|
|
636
|
+
if (url.pathname === "/" || url.pathname === "/index.html") {
|
|
637
|
+
send(request, response, 200, "text/html; charset=utf-8", assets.html);
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
const asset = assets.files.get(url.pathname);
|
|
641
|
+
if (asset) {
|
|
642
|
+
send(request, response, 200, asset.contentType, asset.body, "no-cache");
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
if (url.pathname === "/api/snapshot") {
|
|
646
|
+
sendJson(request, response, 200, await buildSnapshot(runsRoot, options));
|
|
647
|
+
return;
|
|
648
|
+
}
|
|
649
|
+
const context = { request, response, url, runsRoot };
|
|
650
|
+
if (await handleComparisonRoute(context)) return;
|
|
651
|
+
if (await handleArtifactRoute(context)) return;
|
|
652
|
+
if (await handleDetailRoute(context)) return;
|
|
653
|
+
if (url.pathname === "/health") {
|
|
654
|
+
sendJson(request, response, 200, { ok: true });
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
657
|
+
sendJson(request, response, 404, { error: "not found" });
|
|
658
|
+
};
|
|
659
|
+
|
|
660
|
+
const loadAssets = async (): Promise<PreviewAssets> => {
|
|
661
|
+
const htmlPath = resolve(ASSET_ROOT, "index.html");
|
|
662
|
+
const definitions = [
|
|
663
|
+
["/assets/app.css", "app.css", "text/css; charset=utf-8"],
|
|
664
|
+
["/assets/app.js", "app.js", "text/javascript; charset=utf-8"],
|
|
665
|
+
["/assets/artifact-view.js", "artifact-view.js", "text/javascript; charset=utf-8"],
|
|
666
|
+
["/assets/charts.js", "charts.js", "text/javascript; charset=utf-8"],
|
|
667
|
+
["/assets/detail.js", "detail.js", "text/javascript; charset=utf-8"],
|
|
668
|
+
["/assets/format.js", "format.js", "text/javascript; charset=utf-8"],
|
|
669
|
+
] as const;
|
|
670
|
+
const paths = [htmlPath, ...definitions.map((definition) => resolve(ASSET_ROOT, definition[1]))];
|
|
671
|
+
for (const path of paths) {
|
|
672
|
+
if (!existsSync(path)) throw new Error(`Lux preview asset is missing: ${basename(path)}`);
|
|
673
|
+
}
|
|
674
|
+
const [html, ...bodies] = await Promise.all(paths.map((path) => readFile(path)));
|
|
675
|
+
if (!html) throw new Error("Lux preview index is empty");
|
|
676
|
+
const files = new Map(
|
|
677
|
+
definitions.map(([url, , contentType], index) => {
|
|
678
|
+
const body = bodies[index];
|
|
679
|
+
if (!body) throw new Error(`Lux preview asset is empty: ${url}`);
|
|
680
|
+
return [url, { body, contentType }];
|
|
681
|
+
}),
|
|
682
|
+
);
|
|
683
|
+
return { html, files };
|
|
684
|
+
};
|
|
685
|
+
|
|
686
|
+
const listen = (server: Server, port: number): Promise<void> =>
|
|
687
|
+
new Promise((resolveListen, reject) => {
|
|
688
|
+
const onError = (error: Error): void => {
|
|
689
|
+
server.off("listening", onListening);
|
|
690
|
+
reject(error);
|
|
691
|
+
};
|
|
692
|
+
const onListening = (): void => {
|
|
693
|
+
server.off("error", onError);
|
|
694
|
+
resolveListen();
|
|
695
|
+
};
|
|
696
|
+
server.once("error", onError);
|
|
697
|
+
server.once("listening", onListening);
|
|
698
|
+
server.listen(port, LOOPBACK_HOST);
|
|
699
|
+
});
|
|
700
|
+
|
|
701
|
+
export const startPreviewServer = async (
|
|
702
|
+
options: StartPreviewServerOptions,
|
|
703
|
+
): Promise<PreviewServer> => {
|
|
704
|
+
const runsRoot = resolve(options.runsRoot);
|
|
705
|
+
const assets = await loadAssets();
|
|
706
|
+
const server = createServer((request, response) => {
|
|
707
|
+
void requestHandler(
|
|
708
|
+
assets,
|
|
709
|
+
runsRoot,
|
|
710
|
+
options,
|
|
711
|
+
)(request, response).catch((error: unknown) => {
|
|
712
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
713
|
+
if (!response.headersSent) sendJson(request, response, 500, { error: message });
|
|
714
|
+
else response.destroy();
|
|
715
|
+
});
|
|
716
|
+
});
|
|
717
|
+
await listen(server, options.port ?? 0);
|
|
718
|
+
const address = server.address();
|
|
719
|
+
if (!address || typeof address === "string") {
|
|
720
|
+
server.close();
|
|
721
|
+
throw new Error("Lux preview server did not receive a TCP address");
|
|
722
|
+
}
|
|
723
|
+
let closePromise: Promise<void> | null = null;
|
|
724
|
+
const close = (): Promise<void> => {
|
|
725
|
+
closePromise ??= new Promise((resolveClose, reject) => {
|
|
726
|
+
server.close((error) => {
|
|
727
|
+
options.signal?.removeEventListener("abort", onAbort);
|
|
728
|
+
if (error) reject(error);
|
|
729
|
+
else resolveClose();
|
|
730
|
+
});
|
|
731
|
+
});
|
|
732
|
+
return closePromise;
|
|
733
|
+
};
|
|
734
|
+
const onAbort = (): void => {
|
|
735
|
+
void close();
|
|
736
|
+
};
|
|
737
|
+
if (options.signal?.aborted) onAbort();
|
|
738
|
+
else options.signal?.addEventListener("abort", onAbort, { once: true });
|
|
739
|
+
const closed = new Promise<void>((resolveClosed) => {
|
|
740
|
+
server.once("close", resolveClosed);
|
|
741
|
+
});
|
|
742
|
+
return {
|
|
743
|
+
url: `http://${LOOPBACK_HOST}:${address.port}`,
|
|
744
|
+
closed,
|
|
745
|
+
close,
|
|
746
|
+
};
|
|
747
|
+
};
|
|
748
|
+
|
|
749
|
+
export const openPreviewUrl = async (url: string): Promise<void> => {
|
|
750
|
+
let launcher = { command: "xdg-open", args: [url] };
|
|
751
|
+
if (process.platform === "darwin") {
|
|
752
|
+
launcher = { command: "open", args: [url] };
|
|
753
|
+
} else if (process.platform === "win32") {
|
|
754
|
+
launcher = { command: "cmd.exe", args: ["/d", "/s", "/c", "start", "", url] };
|
|
755
|
+
}
|
|
756
|
+
const result = await exec(launcher.command, launcher.args, {
|
|
757
|
+
timeoutMs: 5_000,
|
|
758
|
+
maxBuffer: 2_000,
|
|
759
|
+
});
|
|
760
|
+
if (result.exitCode !== 0 || result.timedOut) {
|
|
761
|
+
throw new Error(
|
|
762
|
+
result.stderr.trim() || `browser launcher exited ${result.exitCode ?? "early"}`,
|
|
763
|
+
);
|
|
764
|
+
}
|
|
765
|
+
};
|