agentpath 0.0.0-stage → 0.1.0-alpha.2
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/LICENSE.md +105 -0
- package/README.md +101 -2
- package/THIRD_PARTY_NOTICES +12 -0
- package/apps/demo-site/dist/site.js +92 -0
- package/apps/studio/dist/assets/geist-cyrillic-ext-wght-normal-DjL33-gN.woff2 +0 -0
- package/apps/studio/dist/assets/geist-cyrillic-wght-normal-BEAKL7Jp.woff2 +0 -0
- package/apps/studio/dist/assets/geist-latin-ext-wght-normal-DC-KSUi6.woff2 +0 -0
- package/apps/studio/dist/assets/geist-latin-wght-normal-BgDaEnEv.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-cyrillic-ext-wght-normal-X_5orZeX.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-cyrillic-wght-normal-DiZS0aHC.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-latin-ext-wght-normal-Bwz-egvJ.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-latin-wght-normal-XN7g48iV.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-symbols2-wght-normal-CO5SzqOn.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-vietnamese-wght-normal-DadHysG0.woff2 +0 -0
- package/apps/studio/dist/assets/geist-vietnamese-wght-normal-6IgcOCM7.woff2 +0 -0
- package/apps/studio/dist/assets/index-DH4wJPHm.js +67 -0
- package/apps/studio/dist/assets/index-DrnNh2ip.css +1 -0
- package/apps/studio/dist/index.html +14 -0
- package/audit.md +173 -0
- package/autonomous-mode.md +104 -0
- package/ci.md +118 -0
- package/coding-agents.md +143 -0
- package/concepts.md +145 -0
- package/contracts.md +356 -0
- package/examples/tasks/ambiguous.json +98 -0
- package/examples/tasks/assertion-failure.json +98 -0
- package/examples/tasks/broken.json +98 -0
- package/examples/tasks/fixed.json +98 -0
- package/examples/tasks/recovery.json +19 -0
- package/images/compare-recovery-desktop.jpg +0 -0
- package/images/compare-variants-desktop.jpg +0 -0
- package/images/overview-desktop.jpg +0 -0
- package/images/run-autonomous-passed-desktop.jpg +0 -0
- package/images/run-failed-desktop.jpg +0 -0
- package/images/run-failed-mobile.jpg +0 -0
- package/images/run-missing-artifact-desktop.jpg +0 -0
- package/images/task-editor-laptop.jpg +0 -0
- package/installation.md +102 -0
- package/ledger.md +62 -0
- package/licenses/-fontsource-variable-geist-mono.txt +93 -0
- package/licenses/-fontsource-variable-geist.txt +93 -0
- package/licenses/lucide-react.txt +43 -0
- package/licenses/react-dom.txt +21 -0
- package/licenses/react.txt +21 -0
- package/licenses/scheduler.txt +21 -0
- package/licenses/tailwindcss.txt +21 -0
- package/licenses/zod.txt +21 -0
- package/package.json +165 -4
- package/packages/cli/bin/agentpath.js +39 -0
- package/packages/cli/dist/audit-rerun.js +384 -0
- package/packages/cli/dist/audit.js +387 -0
- package/packages/cli/dist/change-summaries.js +87 -0
- package/packages/cli/dist/ci-report.js +88 -0
- package/packages/cli/dist/demo.js +88 -0
- package/packages/cli/dist/doctor.js +265 -0
- package/packages/cli/dist/explain.js +89 -0
- package/packages/cli/dist/git.js +7 -0
- package/packages/cli/dist/inspect.js +118 -0
- package/packages/cli/dist/ledger.js +509 -0
- package/packages/cli/dist/local.js +102 -0
- package/packages/cli/dist/main.js +708 -0
- package/packages/cli/dist/mcp.js +231 -0
- package/packages/cli/dist/paths.js +6 -0
- package/packages/cli/dist/playwright-fixture.js +185 -0
- package/packages/cli/dist/playwright-reporter.js +49 -0
- package/packages/cli/dist/tasks.js +37 -0
- package/packages/cli/dist/test-source.js +283 -0
- package/packages/cli/dist/watch.js +96 -0
- package/packages/contracts/dist/agent-settings.js +18 -0
- package/packages/contracts/dist/agent.js +175 -0
- package/packages/contracts/dist/audit.js +115 -0
- package/packages/contracts/dist/guidance.js +339 -0
- package/packages/contracts/dist/index.js +14 -0
- package/packages/contracts/dist/inspect.js +63 -0
- package/packages/contracts/dist/ledger.js +80 -0
- package/packages/contracts/dist/local.js +171 -0
- package/packages/contracts/dist/outcomes.js +94 -0
- package/packages/contracts/dist/platform.js +105 -0
- package/packages/contracts/dist/playwright-live.js +76 -0
- package/packages/contracts/dist/policy.js +93 -0
- package/packages/contracts/dist/run.js +178 -0
- package/packages/contracts/dist/task-issues.js +39 -0
- package/packages/contracts/dist/task.js +185 -0
- package/packages/contracts/dist/watch.js +57 -0
- package/packages/core/dist/action-feedback.js +103 -0
- package/packages/core/dist/actions.js +160 -0
- package/packages/core/dist/agent-loop.js +122 -0
- package/packages/core/dist/artifacts.js +156 -0
- package/packages/core/dist/assertions.js +120 -0
- package/packages/core/dist/autonomous.js +1 -0
- package/packages/core/dist/browser.js +196 -0
- package/packages/core/dist/compare.js +141 -0
- package/packages/core/dist/control-state.js +62 -0
- package/packages/core/dist/diagnostics.js +134 -0
- package/packages/core/dist/evidence-diff.js +93 -0
- package/packages/core/dist/evidence.js +203 -0
- package/packages/core/dist/index.js +22 -0
- package/packages/core/dist/inspect.js +257 -0
- package/packages/core/dist/integrity.js +176 -0
- package/packages/core/dist/liveness.js +114 -0
- package/packages/core/dist/local-files.js +61 -0
- package/packages/core/dist/locators.js +214 -0
- package/packages/core/dist/model-provider.js +88 -0
- package/packages/core/dist/observation.js +257 -0
- package/packages/core/dist/playwright-live.js +519 -0
- package/packages/core/dist/playwright-results.js +543 -0
- package/packages/core/dist/policy.js +182 -0
- package/packages/core/dist/privacy.js +93 -0
- package/packages/core/dist/recovery.js +54 -0
- package/packages/core/dist/retention.js +204 -0
- package/packages/core/dist/run-index.js +177 -0
- package/packages/core/dist/run-plan.js +28 -0
- package/packages/core/dist/run-repository.js +518 -0
- package/packages/core/dist/runner.js +913 -0
- package/packages/core/dist/storage.js +63 -0
- package/packages/core/dist/task-fingerprint.js +36 -0
- package/packages/core/dist/test-change-rerun.js +84 -0
- package/packages/core/dist/test-changes.js +896 -0
- package/packages/core/dist/timing.js +41 -0
- package/packages/core/dist/watch.js +165 -0
- package/packages/server/dist/index.js +265 -0
- package/packages/server/dist/task-store.js +103 -0
- package/quickstart.md +85 -0
- package/security.md +136 -0
- package/try-on-your-repo.md +54 -0
- package/understanding-results.md +159 -0
- package/writing-tasks.md +196 -0
|
@@ -0,0 +1,518 @@
|
|
|
1
|
+
import { lstat, readdir } from "node:fs/promises";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
import { agentTraceSchema, accessibilitySnapshotSchema, artifactPayloadSchemas, browserObservationsSchema, captureStateRecordSchema, runCursorSchema, runDetailSchema, runEventsSchema, operationObservationsSchema, runIdSchema, runLivenessRecordSchema, runMetadataSchema, policyRecordSchema, runPlanSchema, runResultSchema, runSummarySchema, } from "#agentpath/contracts";
|
|
4
|
+
import { artifactMatchesSeal, readIntegrity, verifyRunIntegrity } from "./integrity.js";
|
|
5
|
+
import { evaluateLiveness, LIVENESS_FILE } from "./liveness.js";
|
|
6
|
+
import { isMissing, LocalFileUnavailable, parseRecord, readLocalFile, StoredRecordInvalid } from "./local-files.js";
|
|
7
|
+
import { isIndexCorruption, RunIndex } from "./run-index.js";
|
|
8
|
+
/** A malformed or foreign pagination cursor. */
|
|
9
|
+
export class InvalidCursor extends Error {
|
|
10
|
+
}
|
|
11
|
+
export const DEFAULT_PAGE_SIZE = 50;
|
|
12
|
+
export const MAX_PAGE_SIZE = 200;
|
|
13
|
+
const unfinishedStatuses = new Set(["running", "stale", "abandoned", "incomplete"]);
|
|
14
|
+
function encodeCursor(summary) {
|
|
15
|
+
return Buffer.from(JSON.stringify({ v: 1, k: summary.startedAt ?? "", id: summary.runId })).toString("base64url");
|
|
16
|
+
}
|
|
17
|
+
function decodeCursor(cursor) {
|
|
18
|
+
try {
|
|
19
|
+
return runCursorSchema.parse(JSON.parse(Buffer.from(cursor, "base64url").toString()));
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
throw new InvalidCursor("The pagination cursor is invalid.");
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const timestamp = (value) => value === undefined ? undefined : new Date(value).toISOString();
|
|
26
|
+
async function pool(items, limit, map) {
|
|
27
|
+
const results = new Array(items.length);
|
|
28
|
+
let next = 0;
|
|
29
|
+
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, async () => {
|
|
30
|
+
while (next < items.length) {
|
|
31
|
+
const index = next++;
|
|
32
|
+
results[index] = await map(items[index]);
|
|
33
|
+
}
|
|
34
|
+
}));
|
|
35
|
+
return results;
|
|
36
|
+
}
|
|
37
|
+
export class RunRepository {
|
|
38
|
+
livenessOptions;
|
|
39
|
+
root;
|
|
40
|
+
constructor(root = ".agentpath/runs", livenessOptions = {}) {
|
|
41
|
+
this.livenessOptions = livenessOptions;
|
|
42
|
+
this.root = resolve(root);
|
|
43
|
+
}
|
|
44
|
+
async ids() {
|
|
45
|
+
try {
|
|
46
|
+
if (!(await lstat(this.root)).isDirectory())
|
|
47
|
+
return [];
|
|
48
|
+
return (await readdir(this.root, { withFileTypes: true }))
|
|
49
|
+
.filter((entry) => entry.isDirectory() && runIdSchema.safeParse(entry.name).success)
|
|
50
|
+
.map((entry) => entry.name);
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
if (!isMissing(error))
|
|
54
|
+
throw error;
|
|
55
|
+
return [];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
async events(id) {
|
|
59
|
+
const bytes = await this.read(id, "events.jsonl");
|
|
60
|
+
if (!bytes)
|
|
61
|
+
return [];
|
|
62
|
+
// Ignore the last partial line while an append is in progress or after interruption.
|
|
63
|
+
const lines = bytes.toString().split("\n").slice(0, -1);
|
|
64
|
+
const events = parseRecord(() => runEventsSchema.parse(lines.map((line) => JSON.parse(line))));
|
|
65
|
+
if (events.some((event, index) => event.runId !== id || event.sequence !== index)) {
|
|
66
|
+
throw new StoredRecordInvalid("The stored event sequence is invalid.");
|
|
67
|
+
}
|
|
68
|
+
return events;
|
|
69
|
+
}
|
|
70
|
+
/** Reads the owner lease of an unfinished run. A malformed lease never makes the run unreadable. */
|
|
71
|
+
async liveness(id) {
|
|
72
|
+
let record;
|
|
73
|
+
try {
|
|
74
|
+
record = await this.json(id, LIVENESS_FILE, runLivenessRecordSchema);
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
if (!(error instanceof StoredRecordInvalid))
|
|
78
|
+
throw error;
|
|
79
|
+
return { state: "unknown", reason: "liveness-record-invalid" };
|
|
80
|
+
}
|
|
81
|
+
if (record && record.runId !== id)
|
|
82
|
+
return { state: "unknown", reason: "liveness-record-invalid" };
|
|
83
|
+
return evaluateLiveness(record, this.livenessOptions);
|
|
84
|
+
}
|
|
85
|
+
async unfinished(id, state) {
|
|
86
|
+
if (state?.active)
|
|
87
|
+
return { status: "running", liveness: { state: "active", reason: "in-process" } };
|
|
88
|
+
const liveness = await this.liveness(id);
|
|
89
|
+
const status = liveness.state === "active" ? "running" : liveness.state === "stale" ? "stale"
|
|
90
|
+
: liveness.state === "abandoned" ? "abandoned" : "incomplete";
|
|
91
|
+
return { status, liveness };
|
|
92
|
+
}
|
|
93
|
+
async detail(id, state, recorded) {
|
|
94
|
+
const events = recorded ?? await this.events(id);
|
|
95
|
+
const first = events[0];
|
|
96
|
+
const last = events.at(-1);
|
|
97
|
+
const result = last?.type === "run.finished" ? await this.json(id, "result.json", runResultSchema) : undefined;
|
|
98
|
+
if (last?.type === "run.finished" && (!result || result.runId !== id || result.status !== last.status)) {
|
|
99
|
+
throw new StoredRecordInvalid("The stored result does not agree with its terminal event.");
|
|
100
|
+
}
|
|
101
|
+
const [plan, browserObservations, metadata, operations, captureStates] = await Promise.all([
|
|
102
|
+
this.json(id, "plan.json", runPlanSchema),
|
|
103
|
+
result ? this.json(id, "browser-observations.json", browserObservationsSchema) : undefined,
|
|
104
|
+
this.json(id, "metadata.json", runMetadataSchema),
|
|
105
|
+
result ? this.json(id, "operations.json", operationObservationsSchema) : undefined,
|
|
106
|
+
result ? this.json(id, "capture-state.json", captureStateRecordSchema) : undefined,
|
|
107
|
+
]);
|
|
108
|
+
if (metadata && (metadata.runId !== id || (result && metadata.taskId !== result.taskId))) {
|
|
109
|
+
throw new StoredRecordInvalid("Run metadata identity does not agree with its result.");
|
|
110
|
+
}
|
|
111
|
+
if (operations && operations.runId !== id)
|
|
112
|
+
throw new StoredRecordInvalid("Operation evidence belongs to a different run.");
|
|
113
|
+
if (captureStates && captureStates.runId !== id)
|
|
114
|
+
throw new StoredRecordInvalid("Capture states belong to a different run.");
|
|
115
|
+
const live = result ? undefined : await this.unfinished(id, state);
|
|
116
|
+
const agent = await this.json(id, "agent.json", agentTraceSchema);
|
|
117
|
+
// Policy evidence is read only for finalized runs, so a partial record is never reported as a verdict.
|
|
118
|
+
const policy = result ? await this.json(id, "policy.json", policyRecordSchema) : undefined;
|
|
119
|
+
if (policy && policy.runId !== id)
|
|
120
|
+
throw new StoredRecordInvalid("Policy evidence belongs to a different run.");
|
|
121
|
+
if (agent && (agent.runId !== id || (result &&
|
|
122
|
+
(result.mode !== "autonomous" || agent.verifiedCompletion !== (result.status === "passed") || agent.actionCount !== result.steps.length)))) {
|
|
123
|
+
throw new StoredRecordInvalid("Agent evidence does not agree with its run result.");
|
|
124
|
+
}
|
|
125
|
+
return runDetailSchema.parse({
|
|
126
|
+
summary: {
|
|
127
|
+
runId: id, mode: result?.mode ?? plan?.mode,
|
|
128
|
+
taskId: result?.taskId ?? (first?.type === "run.started" ? first.taskId : undefined),
|
|
129
|
+
startedAt: result?.startedAt ?? first?.at,
|
|
130
|
+
status: result?.status ?? live.status,
|
|
131
|
+
durationMs: result?.durationMs,
|
|
132
|
+
...(live ? { liveness: live.liveness } : {}),
|
|
133
|
+
},
|
|
134
|
+
result, plan, browserObservations, metadata, operations, agent, policy, captureStates, cancelling: state?.cancelling ?? false,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
/** Explicit integrity verification against the sealed digest sidecar. */
|
|
138
|
+
integrity(id) {
|
|
139
|
+
return verifyRunIntegrity(this.root, id);
|
|
140
|
+
}
|
|
141
|
+
async comparisonDetail(id) {
|
|
142
|
+
const detail = await this.detail(id);
|
|
143
|
+
const issues = [];
|
|
144
|
+
const result = detail.result;
|
|
145
|
+
const snapshots = [];
|
|
146
|
+
let integrityStatus;
|
|
147
|
+
const missing = (path) => issues.push({ path, reason: "missing" });
|
|
148
|
+
if (!result)
|
|
149
|
+
missing("result.json");
|
|
150
|
+
if (!detail.metadata)
|
|
151
|
+
missing("metadata.json");
|
|
152
|
+
if (!detail.plan)
|
|
153
|
+
missing("plan.json");
|
|
154
|
+
if (!detail.operations)
|
|
155
|
+
missing("operations.json");
|
|
156
|
+
if (!detail.browserObservations)
|
|
157
|
+
missing("browser-observations.json");
|
|
158
|
+
if (result && detail.metadata?.policyFingerprint && !detail.policy)
|
|
159
|
+
missing("policy.json");
|
|
160
|
+
if (detail.browserObservations?.cleanup === "unconfirmed")
|
|
161
|
+
issues.push({ path: "browser-observations.json", reason: "inconsistent" });
|
|
162
|
+
if (result) {
|
|
163
|
+
// Any difference from the sealed digests prevents strict recovery verification. Legacy runs have no seal.
|
|
164
|
+
const integrity = await this.integrity(id);
|
|
165
|
+
integrityStatus = integrity.status;
|
|
166
|
+
if (integrity.status === "unsealed")
|
|
167
|
+
issues.push({ path: "integrity.json", reason: "inconsistent" });
|
|
168
|
+
for (const issue of integrity.issues) {
|
|
169
|
+
issues.push({ path: issue.path, reason: issue.problem === "truncated" ? "modified" : issue.problem });
|
|
170
|
+
}
|
|
171
|
+
for (const [index, ref] of result.artifacts.entries()) {
|
|
172
|
+
try {
|
|
173
|
+
await this.readArtifact(id, result, index);
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
if (!isMissing(error) && !(error instanceof StoredRecordInvalid))
|
|
177
|
+
throw error;
|
|
178
|
+
issues.push({ path: ref.path, reason: isMissing(error) ? "missing" : "unreadable" });
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (!result.artifacts.some((ref) => ref.kind === "environment"))
|
|
182
|
+
missing("environment.json");
|
|
183
|
+
if (result.status === "failed" && !result.artifacts.some((ref) => ref.kind === "failure"))
|
|
184
|
+
missing("evidence/failure.json");
|
|
185
|
+
const executed = [...result.steps.map((entry) => ({ phase: "step", ...entry })),
|
|
186
|
+
...result.assertions.map((entry) => ({ phase: "assertion", ...entry }))];
|
|
187
|
+
for (const entry of executed) {
|
|
188
|
+
const phase = entry.phase === "step" ? "step" : "assertion";
|
|
189
|
+
const target = (phase === "step" ? detail.plan?.steps : detail.plan?.assertions)?.[entry.index]?.target;
|
|
190
|
+
const ref = entry.evidence.filter((item) => item.kind === "accessibility").at(-1);
|
|
191
|
+
if (!ref || !target?.role)
|
|
192
|
+
continue;
|
|
193
|
+
const index = result.artifacts.findIndex((item) => item.kind === ref.kind && item.path === ref.path);
|
|
194
|
+
if (index < 0) {
|
|
195
|
+
issues.push({ path: ref.path, reason: "inconsistent" });
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
try {
|
|
199
|
+
const snapshot = accessibilitySnapshotSchema.parse((await this.readArtifact(id, result, index)).data);
|
|
200
|
+
snapshots.push({ phase, index: entry.index, path: ref.path, nodes: snapshot.nodes.filter((node) => node.role === target.role) });
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
if (!isMissing(error) && !(error instanceof StoredRecordInvalid))
|
|
204
|
+
throw error;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
for (const ref of executed.flatMap((entry) => entry.evidence)) {
|
|
208
|
+
if (!result.artifacts.some((item) => item.path === ref.path && item.kind === ref.kind)) {
|
|
209
|
+
issues.push({ path: ref.path, reason: "inconsistent" });
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
const events = await this.events(id);
|
|
213
|
+
const finished = events.flatMap((event) => event.type === "step.finished" ? [{ phase: "step", encoded: JSON.stringify(event.step) }]
|
|
214
|
+
: event.type === "assertion.finished" ? [{ phase: "assertion", encoded: JSON.stringify(event.assertion) }] : []);
|
|
215
|
+
if (finished.length !== executed.length || finished.some((entry, index) => {
|
|
216
|
+
const actual = executed[index];
|
|
217
|
+
if (!actual || actual.phase !== entry.phase)
|
|
218
|
+
return true;
|
|
219
|
+
const { phase, ...operation } = actual;
|
|
220
|
+
return phase !== entry.phase || JSON.stringify(operation) !== entry.encoded;
|
|
221
|
+
}))
|
|
222
|
+
issues.push({ path: "events.jsonl", reason: "inconsistent" });
|
|
223
|
+
const observations = detail.operations?.operations;
|
|
224
|
+
if (observations && (observations.length !== executed.length ||
|
|
225
|
+
executed.some((entry) => observations.filter((item) => item.phase === entry.phase && item.index === entry.index).length !== 1))) {
|
|
226
|
+
issues.push({ path: "operations.json", reason: "inconsistent" });
|
|
227
|
+
}
|
|
228
|
+
for (const phase of ["step", "assertion"]) {
|
|
229
|
+
const recorded = phase === "step" ? result.steps : result.assertions;
|
|
230
|
+
const planned = phase === "step" ? detail.plan?.steps : detail.plan?.assertions;
|
|
231
|
+
const required = phase === "step" ? detail.metadata?.stepCount : detail.metadata?.assertionCount;
|
|
232
|
+
if (planned && required !== undefined && (planned.length !== required ||
|
|
233
|
+
(result.status === "passed" && recorded.length !== required))) {
|
|
234
|
+
issues.push({ path: "plan.json", reason: "inconsistent" });
|
|
235
|
+
}
|
|
236
|
+
if (recorded.some((entry, position) => entry.index !== position ||
|
|
237
|
+
(planned && ("action" in entry ? entry.action : entry.assertion) !== planned[position]?.kind))) {
|
|
238
|
+
issues.push({ path: "result.json", reason: "inconsistent" });
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
const unique = issues.filter((issue, index) => issues.findIndex((other) => other.path === issue.path && other.reason === issue.reason) === index);
|
|
243
|
+
return runDetailSchema.parse({ ...detail, snapshots, evidenceAvailability: { checked: true, issues: unique, ...(integrityStatus ? { integrity: integrityStatus } : {}) } });
|
|
244
|
+
}
|
|
245
|
+
async artifact(id, index) {
|
|
246
|
+
const { result } = await this.detail(id);
|
|
247
|
+
if (!result)
|
|
248
|
+
throw new LocalFileUnavailable();
|
|
249
|
+
return this.readArtifact(id, result, index);
|
|
250
|
+
}
|
|
251
|
+
async readArtifact(id, result, index) {
|
|
252
|
+
const ref = result.artifacts[index];
|
|
253
|
+
if (!ref)
|
|
254
|
+
throw new LocalFileUnavailable();
|
|
255
|
+
const extension = ref.kind === "screenshot" ? ".png" : ref.kind === "trace" ? ".zip" : ".json";
|
|
256
|
+
if (!ref.path.endsWith(extension))
|
|
257
|
+
throw new LocalFileUnavailable();
|
|
258
|
+
const bytes = await this.read(id, ref.path, 80 * 1024 * 1024);
|
|
259
|
+
if (!bytes)
|
|
260
|
+
throw new LocalFileUnavailable();
|
|
261
|
+
// Validation on read: a sealed artifact must still match its recorded digest.
|
|
262
|
+
if (!artifactMatchesSeal(await readIntegrity(this.root, id), ref.path, bytes)) {
|
|
263
|
+
throw new StoredRecordInvalid("The artifact differs from its recorded integrity digest.");
|
|
264
|
+
}
|
|
265
|
+
return {
|
|
266
|
+
ref,
|
|
267
|
+
data: parseRecord(() => artifactPayloadSchemas[ref.kind].parse(extension === ".json" ? JSON.parse(bytes.toString()) : bytes)),
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
// ---- Listing ---------------------------------------------------------------------------------------
|
|
271
|
+
/** Cheap change detector for canonical files, used to decide when an indexed row must be derived again. */
|
|
272
|
+
async signature(id) {
|
|
273
|
+
const part = async (file) => {
|
|
274
|
+
try {
|
|
275
|
+
const info = await lstat(join(this.root, id, file));
|
|
276
|
+
return `${info.size}:${Math.trunc(info.mtimeMs * 1000)}`;
|
|
277
|
+
}
|
|
278
|
+
catch (error) {
|
|
279
|
+
if (!isMissing(error))
|
|
280
|
+
throw error;
|
|
281
|
+
return "-";
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
return `${await part("events.jsonl")}/${await part("result.json")}`;
|
|
285
|
+
}
|
|
286
|
+
/** Derives one index row from canonical run files. */
|
|
287
|
+
async indexRow(id) {
|
|
288
|
+
const signature = await this.signature(id);
|
|
289
|
+
try {
|
|
290
|
+
const { summary, result } = await this.detail(id, undefined);
|
|
291
|
+
return {
|
|
292
|
+
runId: id, signature, record: result?.status ?? "unfinished",
|
|
293
|
+
...(summary.taskId ? { taskId: summary.taskId } : {}),
|
|
294
|
+
...(summary.startedAt ? { startedAt: summary.startedAt } : {}),
|
|
295
|
+
...(summary.durationMs !== undefined ? { durationMs: summary.durationMs } : {}),
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
catch (error) {
|
|
299
|
+
if (!(error instanceof StoredRecordInvalid))
|
|
300
|
+
throw error;
|
|
301
|
+
return { runId: id, signature, record: "unreadable" };
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
async summary(row, state) {
|
|
305
|
+
if (row.record === "unreadable")
|
|
306
|
+
return { runId: row.runId, status: "unreadable" };
|
|
307
|
+
let mode;
|
|
308
|
+
try {
|
|
309
|
+
mode = (await this.json(row.runId, "plan.json", runPlanSchema))?.mode;
|
|
310
|
+
}
|
|
311
|
+
catch (error) {
|
|
312
|
+
if (!(error instanceof StoredRecordInvalid))
|
|
313
|
+
throw error;
|
|
314
|
+
return { runId: row.runId, status: "unreadable" };
|
|
315
|
+
}
|
|
316
|
+
const base = {
|
|
317
|
+
runId: row.runId, mode,
|
|
318
|
+
...(row.taskId ? { taskId: row.taskId } : {}),
|
|
319
|
+
...(row.startedAt ? { startedAt: row.startedAt } : {}),
|
|
320
|
+
};
|
|
321
|
+
if (row.record !== "unfinished") {
|
|
322
|
+
return runSummarySchema.parse({ ...base, status: row.record, ...(row.durationMs !== undefined ? { durationMs: row.durationMs } : {}) });
|
|
323
|
+
}
|
|
324
|
+
return runSummarySchema.parse({ ...base, ...await this.unfinished(row.runId, state) });
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Brings the index up to date with the run directories: new directories are derived, removed ones are
|
|
328
|
+
* deleted, and unfinished runs are re-derived when their files changed. Finalized rows are not reparsed.
|
|
329
|
+
*/
|
|
330
|
+
async reconcile(index, ids) {
|
|
331
|
+
const known = index.signatures();
|
|
332
|
+
const present = new Set(ids);
|
|
333
|
+
const deletes = [...known.keys()].filter((id) => !present.has(id));
|
|
334
|
+
const candidates = await pool(ids, 32, async (id) => {
|
|
335
|
+
const entry = known.get(id);
|
|
336
|
+
if (!entry)
|
|
337
|
+
return id;
|
|
338
|
+
if (entry.record !== "unfinished")
|
|
339
|
+
return undefined;
|
|
340
|
+
return entry.signature === await this.signature(id) ? undefined : id;
|
|
341
|
+
});
|
|
342
|
+
const stale = candidates.filter((id) => id !== undefined);
|
|
343
|
+
const upserts = await pool(stale, 16, (id) => this.indexRow(id));
|
|
344
|
+
if (upserts.length || deletes.length)
|
|
345
|
+
index.apply(upserts, deletes);
|
|
346
|
+
return { derived: upserts.length, removed: deletes.length };
|
|
347
|
+
}
|
|
348
|
+
/** Opens and reconciles the index, discarding and rebuilding it once if it is corrupt. */
|
|
349
|
+
async withIndex(use, reconcile = true) {
|
|
350
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
351
|
+
let index;
|
|
352
|
+
try {
|
|
353
|
+
try {
|
|
354
|
+
index = await RunIndex.open(this.root);
|
|
355
|
+
if (!index)
|
|
356
|
+
return undefined;
|
|
357
|
+
if (reconcile)
|
|
358
|
+
await this.reconcile(index, await this.ids());
|
|
359
|
+
}
|
|
360
|
+
catch (error) {
|
|
361
|
+
// The index is derived: one that cannot be opened or brought up to date (read-only, locked) means a
|
|
362
|
+
// scan, never a failed listing. Errors from the listing itself still propagate below.
|
|
363
|
+
if (!isIndexCorruption(error))
|
|
364
|
+
return undefined;
|
|
365
|
+
throw error;
|
|
366
|
+
}
|
|
367
|
+
return await use(index);
|
|
368
|
+
}
|
|
369
|
+
catch (error) {
|
|
370
|
+
if (!isIndexCorruption(error))
|
|
371
|
+
throw error;
|
|
372
|
+
index?.close();
|
|
373
|
+
index = undefined;
|
|
374
|
+
await RunIndex.destroy(this.root);
|
|
375
|
+
}
|
|
376
|
+
finally {
|
|
377
|
+
index?.close();
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return undefined;
|
|
381
|
+
}
|
|
382
|
+
/** Paginated listing, newest first. Uses the derived index and falls back to a full scan without one. */
|
|
383
|
+
async list(options = {}) {
|
|
384
|
+
const limit = Math.min(Math.max(1, options.limit ?? DEFAULT_PAGE_SIZE), MAX_PAGE_SIZE);
|
|
385
|
+
const after = options.cursor === undefined ? undefined : decodeCursor(options.cursor);
|
|
386
|
+
const statuses = options.status ? new Set(options.status) : undefined;
|
|
387
|
+
const since = timestamp(options.since);
|
|
388
|
+
const until = timestamp(options.until);
|
|
389
|
+
const records = statuses && [...new Set([...statuses].map((status) => unfinishedStatuses.has(status) ? "unfinished" : status))];
|
|
390
|
+
const matches = (summary) => !statuses || statuses.has(summary.status);
|
|
391
|
+
const indexed = await this.withIndex(async (index) => {
|
|
392
|
+
const runs = [];
|
|
393
|
+
let position = after;
|
|
394
|
+
// Liveness-derived statuses are filtered after evaluation, so read in bounded batches until full.
|
|
395
|
+
for (let batch = 0; batch < 50 && runs.length <= limit; batch++) {
|
|
396
|
+
const size = limit + 1 - runs.length;
|
|
397
|
+
const rows = index.page({ records, taskId: options.taskId, since, until, after: position, limit: size });
|
|
398
|
+
const refreshed = [];
|
|
399
|
+
for (const row of rows) {
|
|
400
|
+
// Finalized rows are revalidated against the files with a cheap stat before they are returned.
|
|
401
|
+
const current = row.record === "unfinished" || row.signature === await this.signature(row.runId)
|
|
402
|
+
? row : await this.indexRow(row.runId).catch((error) => {
|
|
403
|
+
if (isMissing(error))
|
|
404
|
+
return undefined;
|
|
405
|
+
throw error;
|
|
406
|
+
});
|
|
407
|
+
if (current && current !== row)
|
|
408
|
+
refreshed.push(current);
|
|
409
|
+
if (current) {
|
|
410
|
+
const summary = await this.summary(current, options.state?.(current.runId));
|
|
411
|
+
if (matches(summary) && (!options.taskId || summary.taskId === options.taskId))
|
|
412
|
+
runs.push(summary);
|
|
413
|
+
}
|
|
414
|
+
position = { k: row.startedAt ?? "", id: row.runId };
|
|
415
|
+
if (runs.length > limit)
|
|
416
|
+
break;
|
|
417
|
+
}
|
|
418
|
+
if (refreshed.length)
|
|
419
|
+
index.apply(refreshed);
|
|
420
|
+
if (rows.length < size)
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
return runs;
|
|
424
|
+
// A follow-up page continues a listing that was reconciled on its first page; reconciling every page made a
|
|
425
|
+
// full walk of many runs quadratic. Removed runs are still caught by the per-row signature check above.
|
|
426
|
+
}, after === undefined);
|
|
427
|
+
const source = indexed ? "index" : "scan";
|
|
428
|
+
let runs = indexed;
|
|
429
|
+
if (!runs) {
|
|
430
|
+
runs = (await this.scan(options.state)).filter((summary) => matches(summary) &&
|
|
431
|
+
(!options.taskId || summary.taskId === options.taskId) &&
|
|
432
|
+
(!since || (summary.startedAt !== undefined && summary.startedAt >= since)) &&
|
|
433
|
+
(!until || (summary.startedAt !== undefined && summary.startedAt < until)) &&
|
|
434
|
+
(!after || (summary.startedAt ?? "") < after.k || ((summary.startedAt ?? "") === after.k && summary.runId < after.id)))
|
|
435
|
+
.slice(0, limit + 1);
|
|
436
|
+
}
|
|
437
|
+
const page = runs.slice(0, limit);
|
|
438
|
+
const last = page.at(-1);
|
|
439
|
+
return { runs: page, source, ...(runs.length > limit && last ? { nextCursor: encodeCursor(last) } : {}) };
|
|
440
|
+
}
|
|
441
|
+
/** The pre-index implementation: derive every run from its files, newest first. */
|
|
442
|
+
async scan(state) {
|
|
443
|
+
const list = await pool(await this.ids(), 16, async (id) => {
|
|
444
|
+
try {
|
|
445
|
+
return (await this.detail(id, state?.(id))).summary;
|
|
446
|
+
}
|
|
447
|
+
catch (error) {
|
|
448
|
+
// One malformed record must not hide every other run.
|
|
449
|
+
if (!(error instanceof StoredRecordInvalid))
|
|
450
|
+
throw error;
|
|
451
|
+
return { runId: id, status: "unreadable" };
|
|
452
|
+
}
|
|
453
|
+
});
|
|
454
|
+
return list.sort((a, b) => (b.startedAt ?? "").localeCompare(a.startedAt ?? "") || b.runId.localeCompare(a.runId));
|
|
455
|
+
}
|
|
456
|
+
/** Discards the index and derives every row again from the run directories. */
|
|
457
|
+
async reindex() {
|
|
458
|
+
await RunIndex.destroy(this.root);
|
|
459
|
+
const index = await RunIndex.open(this.root);
|
|
460
|
+
if (!index)
|
|
461
|
+
return { indexed: 0, unreadable: 0, available: false };
|
|
462
|
+
try {
|
|
463
|
+
const rows = await pool(await this.ids(), 16, (id) => this.indexRow(id));
|
|
464
|
+
index.apply(rows, [], true);
|
|
465
|
+
return { indexed: rows.length, unreadable: rows.filter((row) => row.record === "unreadable").length, available: true };
|
|
466
|
+
}
|
|
467
|
+
finally {
|
|
468
|
+
index.close();
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
/** Best-effort incremental update by a writer. The index is derived, so failures are ignored. */
|
|
472
|
+
async recordInIndex(id) {
|
|
473
|
+
try {
|
|
474
|
+
const index = await RunIndex.open(this.root);
|
|
475
|
+
if (!index)
|
|
476
|
+
return;
|
|
477
|
+
try {
|
|
478
|
+
index.apply([await this.indexRow(id)]);
|
|
479
|
+
}
|
|
480
|
+
finally {
|
|
481
|
+
index.close();
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
catch {
|
|
485
|
+
// Readers reconcile missing or outdated rows from the run directories.
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
/** Removes a row after its directory was deleted. */
|
|
489
|
+
async forget(id) {
|
|
490
|
+
try {
|
|
491
|
+
const index = await RunIndex.open(this.root);
|
|
492
|
+
if (!index)
|
|
493
|
+
return;
|
|
494
|
+
try {
|
|
495
|
+
index.apply([], [id]);
|
|
496
|
+
}
|
|
497
|
+
finally {
|
|
498
|
+
index.close();
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
catch {
|
|
502
|
+
// Reconciliation removes rows whose directories are gone.
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
async json(id, filename, schema) {
|
|
506
|
+
const bytes = await this.read(id, filename);
|
|
507
|
+
return bytes ? parseRecord(() => schema.parse(JSON.parse(bytes.toString()))) : undefined;
|
|
508
|
+
}
|
|
509
|
+
async read(id, filename, limit = 8 * 1024 * 1024) {
|
|
510
|
+
try {
|
|
511
|
+
return await readLocalFile(this.root, `${runIdSchema.parse(id)}/${filename}`, limit);
|
|
512
|
+
}
|
|
513
|
+
catch (error) {
|
|
514
|
+
if (!isMissing(error))
|
|
515
|
+
throw error;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
}
|