@open-qa/qa 0.1.0-alpha.3 → 0.1.0-alpha.4
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/package.json +7 -7
- package/src/cli-program.js +1 -1
- package/src/doctor-command.js +2 -1
- package/src/execution-snapshot.js +2 -1
- package/src/report/browser.css +1 -1
- package/src/report/browser.js +33 -33
- package/src/run-command.d.ts +7 -0
- package/src/run-command.js +21 -7
- package/src/run-setup-error.d.ts +11 -0
- package/src/run-setup-error.js +18 -0
- package/src/studio-command.d.ts +1 -0
- package/src/studio-command.js +11 -0
- package/src/studio-control.d.ts +5 -0
- package/src/studio-control.js +207 -0
- package/src/studio-review.d.ts +24 -0
- package/src/studio-review.js +64 -0
- package/src/studio-worker.d.ts +1 -0
- package/src/studio-worker.js +48 -0
package/src/run-command.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type BudgetPrompt } from "./budget-confirmation.js";
|
|
2
2
|
import { type FlowLifecycleStore } from "@open-qa/qa-engine";
|
|
3
3
|
import { type EngineAdapters } from "./engine.js";
|
|
4
|
+
import { loadConfig, loadFlowDocument } from "./flow-loader.js";
|
|
4
5
|
export interface RunOptions {
|
|
5
6
|
acceptEstimate?: boolean;
|
|
6
7
|
executor?: string;
|
|
@@ -18,6 +19,12 @@ export interface RunOptions {
|
|
|
18
19
|
repeat?: number;
|
|
19
20
|
}
|
|
20
21
|
export interface RunAdapters extends EngineAdapters, BudgetPrompt {
|
|
22
|
+
studio?: {
|
|
23
|
+
runId: string;
|
|
24
|
+
verify: (config: Awaited<ReturnType<typeof loadConfig>>, doc: Awaited<ReturnType<typeof loadFlowDocument>>) => void;
|
|
25
|
+
created: () => void;
|
|
26
|
+
saved: () => void;
|
|
27
|
+
};
|
|
21
28
|
lifecycle?: FlowLifecycleStore;
|
|
22
29
|
output?: (line: string) => void;
|
|
23
30
|
signal?: AbortSignal;
|
package/src/run-command.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { RunSetupError } from "./run-setup-error.js";
|
|
2
|
+
import { isProtectedTarget } from "@open-qa/qa-engine";
|
|
1
3
|
import { confirmRunEstimate } from "./budget-confirmation.js";
|
|
2
4
|
import { writeWatchState, removeWatchState } from "./watch-state.js";
|
|
3
5
|
import { e2bConfig } from "./adapters/e2b-client.js";
|
|
@@ -88,6 +90,11 @@ async function runCommand(path, options, adapters = {}) {
|
|
|
88
90
|
throw new Error(`Target ${targetName} URL is outside its allow list`);
|
|
89
91
|
const trigger = (await stat(path)).isDirectory() ? "suite" : "manual";
|
|
90
92
|
let flows = trigger === "suite" ? await loadFlowDocuments(root, path) : [await loadFlowDocument(path, root)];
|
|
93
|
+
if (adapters.studio) {
|
|
94
|
+
if (trigger !== "manual" || isProtectedTarget(targetName, target) || flows.length !== 1)
|
|
95
|
+
throw new Error("Target unavailable in Studio");
|
|
96
|
+
adapters.studio.verify(config, flows[0]);
|
|
97
|
+
}
|
|
91
98
|
if (options.tag) flows = flows.filter(({ flow }) => flow.tags?.includes(options.tag));
|
|
92
99
|
if (!flows.length) throw new Error("No flows match the selection");
|
|
93
100
|
const loaded = flows;
|
|
@@ -121,12 +128,15 @@ async function runCommand(path, options, adapters = {}) {
|
|
|
121
128
|
)
|
|
122
129
|
);
|
|
123
130
|
for (const plan of plans) {
|
|
124
|
-
if ((targetName
|
|
131
|
+
if (isProtectedTarget(targetName, target) && plan.prod !== "safe")
|
|
125
132
|
throw new Error(`Flow ${plan.key} must declare prod: safe for target ${targetName}`);
|
|
126
133
|
for (const view of Object.values(plan.views)) {
|
|
127
134
|
if (!["web", "telegram", "discord", "email"].includes(view.surface))
|
|
128
|
-
throw new
|
|
129
|
-
|
|
135
|
+
throw new RunSetupError(
|
|
136
|
+
"unsupported-surface",
|
|
137
|
+
`Unsupported local view surface: ${view.surface}`
|
|
138
|
+
);
|
|
139
|
+
if (view.surface === "telegram" && isProtectedTarget(targetName, target))
|
|
130
140
|
throw new Error("Telegram requires a non-production target");
|
|
131
141
|
}
|
|
132
142
|
}
|
|
@@ -141,18 +151,19 @@ async function runCommand(path, options, adapters = {}) {
|
|
|
141
151
|
attempts: (retries + 1) * repeat
|
|
142
152
|
});
|
|
143
153
|
const env = await optionalEnv(root);
|
|
144
|
-
const stageTarget = targetName
|
|
154
|
+
const stageTarget = isProtectedTarget(targetName, target) ? "prod" : targetName === "local" ? "dev" : "staging";
|
|
145
155
|
const systems = new Map(
|
|
146
156
|
plans.map((plan) => [plan, resolveSystems(stageTarget, target.systems, env, plan)])
|
|
147
157
|
);
|
|
148
158
|
let access;
|
|
149
159
|
if (target.access === "cloudflare") {
|
|
150
160
|
if (!env.QA_CF_ACCESS_CLIENT_ID || !env.QA_CF_ACCESS_CLIENT_SECRET)
|
|
151
|
-
throw new
|
|
161
|
+
throw new RunSetupError(
|
|
162
|
+
"access-credentials",
|
|
152
163
|
`Cloudflare Access requires QA_CF_ACCESS_CLIENT_ID and QA_CF_ACCESS_CLIENT_SECRET in ${join(root, ".env.local")}`
|
|
153
164
|
);
|
|
154
165
|
if (new URL(target.url).protocol !== "https:")
|
|
155
|
-
throw new
|
|
166
|
+
throw new RunSetupError("access-https", "Cloudflare Access requires an HTTPS target");
|
|
156
167
|
access = {
|
|
157
168
|
origin: new URL(target.url).origin,
|
|
158
169
|
clientId: env.QA_CF_ACCESS_CLIENT_ID,
|
|
@@ -191,7 +202,7 @@ async function runCommand(path, options, adapters = {}) {
|
|
|
191
202
|
database = !adapters.flowStatus && process.env.QA_DATABASE_URL ? new QaDatabase(process.env.QA_DATABASE_URL) : void 0;
|
|
192
203
|
const flowStatus = adapters.flowStatus ?? (database ? new PgFlowStatus(database, config.projectId) : void 0);
|
|
193
204
|
for (const plan of plans.flatMap((plan2) => Array.from({ length: repeat }, () => plan2))) {
|
|
194
|
-
if (signal.aborted) {
|
|
205
|
+
if (signal.aborted && !adapters.studio) {
|
|
195
206
|
code = Math.max(code, 1);
|
|
196
207
|
break;
|
|
197
208
|
}
|
|
@@ -234,6 +245,7 @@ async function runCommand(path, options, adapters = {}) {
|
|
|
234
245
|
}
|
|
235
246
|
})
|
|
236
247
|
);
|
|
248
|
+
adapters.studio?.created();
|
|
237
249
|
output(` ${plan.key} \xB7 ${run.id}`);
|
|
238
250
|
},
|
|
239
251
|
appendEvents: async (id, events) => {
|
|
@@ -255,6 +267,7 @@ async function runCommand(path, options, adapters = {}) {
|
|
|
255
267
|
finishRun: async (id, result2) => {
|
|
256
268
|
if (localStore) await removeWatchState(root, id);
|
|
257
269
|
await store.finishRun(id, { ...result2, executionObservation: { ...engine.observation } });
|
|
270
|
+
adapters.studio?.saved();
|
|
258
271
|
if (localStore) {
|
|
259
272
|
try {
|
|
260
273
|
reports.set(id, await generateReport(root, id));
|
|
@@ -278,6 +291,7 @@ async function runCommand(path, options, adapters = {}) {
|
|
|
278
291
|
}
|
|
279
292
|
};
|
|
280
293
|
const engine = createEngine({
|
|
294
|
+
runId: adapters.studio?.runId,
|
|
281
295
|
batchId,
|
|
282
296
|
heal: config.heal,
|
|
283
297
|
replay: options.repeat !== void 0 ? "off" : options.replay,
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const setupReasons: {
|
|
2
|
+
readonly "unsupported-surface": "This flow uses an unsupported local view surface. Choose web, Telegram, Discord or email.";
|
|
3
|
+
readonly "access-credentials": "Configure the Cloudflare Access client ID and client secret in the project’s .env.local file.";
|
|
4
|
+
readonly "access-https": "Configure an HTTPS URL for this Cloudflare Access environment.";
|
|
5
|
+
readonly "review-changed": "The configuration or estimate changed. Review the flow again before starting.";
|
|
6
|
+
readonly configuration: "Check the flow, environment and systems configuration. Run open-qa doctor for details.";
|
|
7
|
+
};
|
|
8
|
+
export declare class RunSetupError extends Error {
|
|
9
|
+
readonly code: keyof typeof setupReasons;
|
|
10
|
+
constructor(code: keyof typeof setupReasons, detail: string);
|
|
11
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const setupReasons = {
|
|
2
|
+
"unsupported-surface": "This flow uses an unsupported local view surface. Choose web, Telegram, Discord or email.",
|
|
3
|
+
"access-credentials": "Configure the Cloudflare Access client ID and client secret in the project\u2019s .env.local file.",
|
|
4
|
+
"access-https": "Configure an HTTPS URL for this Cloudflare Access environment.",
|
|
5
|
+
"review-changed": "The configuration or estimate changed. Review the flow again before starting.",
|
|
6
|
+
configuration: "Check the flow, environment and systems configuration. Run open-qa doctor for details."
|
|
7
|
+
};
|
|
8
|
+
class RunSetupError extends Error {
|
|
9
|
+
constructor(code, detail) {
|
|
10
|
+
super(detail);
|
|
11
|
+
this.code = code;
|
|
12
|
+
}
|
|
13
|
+
code;
|
|
14
|
+
}
|
|
15
|
+
export {
|
|
16
|
+
RunSetupError,
|
|
17
|
+
setupReasons
|
|
18
|
+
};
|
package/src/studio-command.d.ts
CHANGED
package/src/studio-command.js
CHANGED
|
@@ -9,6 +9,16 @@ async function studioCommand(options) {
|
|
|
9
9
|
])
|
|
10
10
|
if (value) args.push(key, value);
|
|
11
11
|
if (options.open === false) args.push("--no-open");
|
|
12
|
+
let control;
|
|
13
|
+
if (options.allowRun) {
|
|
14
|
+
const { tsImport } = await import("tsx/esm/api");
|
|
15
|
+
const { listenStudio } = await tsImport(
|
|
16
|
+
"./studio-control.js",
|
|
17
|
+
import.meta.url
|
|
18
|
+
);
|
|
19
|
+
control = await listenStudio(options.root ?? ".qa");
|
|
20
|
+
args.push("--control-socket", control.path);
|
|
21
|
+
}
|
|
12
22
|
const child = spawn(
|
|
13
23
|
process.execPath,
|
|
14
24
|
[fileURLToPath(import.meta.resolve("@open-qa/qa-studio/cli")), ...args],
|
|
@@ -26,6 +36,7 @@ async function studioCommand(options) {
|
|
|
26
36
|
child.once("exit", (code) => resolve(code ?? 1));
|
|
27
37
|
});
|
|
28
38
|
} finally {
|
|
39
|
+
await control?.close();
|
|
29
40
|
process.off("SIGINT", stop);
|
|
30
41
|
process.off("SIGTERM", stop);
|
|
31
42
|
process.off("SIGHUP", stop);
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { fork } from "node:child_process";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { constants } from "node:fs";
|
|
4
|
+
import { mkdtemp, link, open, realpath, rm, unlink, writeFile } from "node:fs/promises";
|
|
5
|
+
import { createServer } from "node:http";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
7
|
+
import { join, resolve } from "node:path";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
import { z } from "zod";
|
|
10
|
+
import { setupReasons } from "./run-setup-error.js";
|
|
11
|
+
import { reviewFlow, StudioError } from "./studio-review.js";
|
|
12
|
+
const runId = z.string().regex(/^run_[\da-f-]{36}$/);
|
|
13
|
+
const command = z.discriminatedUnion("action", [
|
|
14
|
+
z.strictObject({
|
|
15
|
+
action: z.literal("review"),
|
|
16
|
+
project: z.string(),
|
|
17
|
+
flowId: z.string().regex(/^[a-f0-9]{64}$/),
|
|
18
|
+
source: z.string().max(1024),
|
|
19
|
+
target: z.string().min(1).max(128)
|
|
20
|
+
}),
|
|
21
|
+
z.strictObject({
|
|
22
|
+
action: z.literal("start"),
|
|
23
|
+
ticket: z.uuid(),
|
|
24
|
+
key: z.uuid(),
|
|
25
|
+
accepted: z.boolean()
|
|
26
|
+
}),
|
|
27
|
+
z.strictObject({ action: z.enum(["stop", "status"]), runId })
|
|
28
|
+
]);
|
|
29
|
+
async function lockOwner(lock) {
|
|
30
|
+
const handle = await open(lock, constants.O_RDONLY | constants.O_NONBLOCK | constants.O_NOFOLLOW);
|
|
31
|
+
try {
|
|
32
|
+
const bytes = Buffer.alloc(64);
|
|
33
|
+
if (!(await handle.stat()).isFile()) return;
|
|
34
|
+
const { bytesRead } = await handle.read(bytes, 0, bytes.length, 0);
|
|
35
|
+
const active = runId.safeParse(bytes.toString("utf8", 0, bytesRead));
|
|
36
|
+
return active.success ? active.data : void 0;
|
|
37
|
+
} finally {
|
|
38
|
+
await handle.close();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
async function acquireStudioLock(root, id) {
|
|
42
|
+
const lock = join(root, ".studio-run.lock"), staging = lock + "." + randomUUID();
|
|
43
|
+
await writeFile(staging, id, { mode: 384, flag: "wx" });
|
|
44
|
+
try {
|
|
45
|
+
try {
|
|
46
|
+
await link(staging, lock);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
if (error.code !== "EEXIST") throw error;
|
|
49
|
+
throw new StudioError(
|
|
50
|
+
`Another run holds the project lock at ${lock}. Check its worker and resource leases before manually clearing a stale lock.`,
|
|
51
|
+
409,
|
|
52
|
+
await lockOwner(lock)
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
} finally {
|
|
56
|
+
await unlink(staging);
|
|
57
|
+
}
|
|
58
|
+
return async () => {
|
|
59
|
+
try {
|
|
60
|
+
if (await lockOwner(lock) === id) await unlink(lock);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
if (error.code !== "ENOENT") throw error;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
async function listenStudio(directory) {
|
|
67
|
+
const root = await realpath(directory), temp = await mkdtemp(join(tmpdir(), "open-qa-"));
|
|
68
|
+
const path = join(temp, "control.sock"), tickets = /* @__PURE__ */ new Map();
|
|
69
|
+
const runs = /* @__PURE__ */ new Map();
|
|
70
|
+
let active;
|
|
71
|
+
let closing = false;
|
|
72
|
+
async function start(t, accepted) {
|
|
73
|
+
const r = t.request;
|
|
74
|
+
const current = await reviewFlow(root, r.project, r.source, r.flowId, r.target);
|
|
75
|
+
if (JSON.stringify(current) !== JSON.stringify(t.review))
|
|
76
|
+
throw new StudioError("Configuration changed; review again.");
|
|
77
|
+
if (current.prompt && !accepted)
|
|
78
|
+
throw new StudioError("Confirm this cost estimate to continue.", 400);
|
|
79
|
+
const id = "run_" + randomUUID();
|
|
80
|
+
const release = await acquireStudioLock(root, id);
|
|
81
|
+
try {
|
|
82
|
+
if (closing) throw new StudioError("Launcher is closing.");
|
|
83
|
+
const worker = fork(fileURLToPath(new URL("./studio-worker.js", import.meta.url)), [], {
|
|
84
|
+
execArgv: ["--import", import.meta.resolve("tsx")],
|
|
85
|
+
stdio: ["ignore", "ignore", "ignore", "ipc"]
|
|
86
|
+
});
|
|
87
|
+
const result = { runId: id, state: "starting" };
|
|
88
|
+
if (runs.size >= 128) runs.delete(runs.keys().next().value);
|
|
89
|
+
runs.set(id, result);
|
|
90
|
+
let timer;
|
|
91
|
+
const stop = () => {
|
|
92
|
+
if (result.state === "stopping") return;
|
|
93
|
+
result.state = "stopping";
|
|
94
|
+
if (worker.connected) worker.send("stop", () => {
|
|
95
|
+
});
|
|
96
|
+
timer = setTimeout(() => worker.kill("SIGKILL"), 15e3);
|
|
97
|
+
};
|
|
98
|
+
let saved = false;
|
|
99
|
+
worker.on("message", (m) => {
|
|
100
|
+
if (typeof m === "string" && Object.hasOwn(setupReasons, m))
|
|
101
|
+
result.reason = setupReasons[m];
|
|
102
|
+
if (m === "saved") saved = true;
|
|
103
|
+
if (m === "running" && result.state === "starting") result.state = "running";
|
|
104
|
+
});
|
|
105
|
+
const done = new Promise((resolveDone) => {
|
|
106
|
+
worker.once("error", () => {
|
|
107
|
+
result.state = "failed";
|
|
108
|
+
result.reason = "Studio could not launch the local worker. Restart Studio and try again.";
|
|
109
|
+
});
|
|
110
|
+
worker.once("close", async (_code, signal) => {
|
|
111
|
+
clearTimeout(timer);
|
|
112
|
+
result.state = signal ? "interrupted" : saved ? "finished" : "failed";
|
|
113
|
+
if (!signal)
|
|
114
|
+
await release().catch(() => {
|
|
115
|
+
result.state = "interrupted";
|
|
116
|
+
});
|
|
117
|
+
if (active?.runId === id) active = void 0;
|
|
118
|
+
resolveDone();
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
active = { runId: id, stop, done };
|
|
122
|
+
worker.send(
|
|
123
|
+
{ root, path: resolve(root, r.source), runId: id, review: current, accepted },
|
|
124
|
+
(error) => {
|
|
125
|
+
if (error) stop();
|
|
126
|
+
}
|
|
127
|
+
);
|
|
128
|
+
return result;
|
|
129
|
+
} catch (error) {
|
|
130
|
+
await release();
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const server = createServer(async (req, res) => {
|
|
135
|
+
const reply = (status, value) => {
|
|
136
|
+
res.writeHead(status, { "Content-Type": "application/json" });
|
|
137
|
+
res.end(JSON.stringify(value));
|
|
138
|
+
};
|
|
139
|
+
try {
|
|
140
|
+
if (closing || req.method !== "POST" || req.url !== "/")
|
|
141
|
+
throw new StudioError("Unavailable.", 403);
|
|
142
|
+
const chunks = [];
|
|
143
|
+
let size = 0;
|
|
144
|
+
for await (const chunk of req) {
|
|
145
|
+
size += chunk.length;
|
|
146
|
+
if (size > 4096) throw new StudioError("Request too large.", 413);
|
|
147
|
+
chunks.push(chunk);
|
|
148
|
+
}
|
|
149
|
+
const parsed = command.safeParse(JSON.parse(Buffer.concat(chunks).toString()));
|
|
150
|
+
if (!parsed.success) throw new StudioError("Invalid request.", 400);
|
|
151
|
+
const input = parsed.data;
|
|
152
|
+
if (input.action === "review") {
|
|
153
|
+
const review = await reviewFlow(
|
|
154
|
+
root,
|
|
155
|
+
input.project,
|
|
156
|
+
input.source,
|
|
157
|
+
input.flowId,
|
|
158
|
+
input.target
|
|
159
|
+
);
|
|
160
|
+
for (const [k, t] of tickets) if (t.expires <= Date.now()) tickets.delete(k);
|
|
161
|
+
if (tickets.size >= 128)
|
|
162
|
+
throw new StudioError("Too many recent reviews. Try again later.", 429);
|
|
163
|
+
const ticket = randomUUID();
|
|
164
|
+
tickets.set(ticket, { request: input, review, expires: Date.now() + 10 * 6e4 });
|
|
165
|
+
return reply(200, { ticket, ...review });
|
|
166
|
+
}
|
|
167
|
+
if (input.action === "start") {
|
|
168
|
+
const t = tickets.get(input.ticket);
|
|
169
|
+
if (!t || t.expires <= Date.now()) throw new StudioError("Review expired; review again.");
|
|
170
|
+
if (t.start && t.start.key !== input.key)
|
|
171
|
+
throw new StudioError("This review has already been used.");
|
|
172
|
+
t.start ??= { key: input.key, result: start(t, input.accepted) };
|
|
173
|
+
return reply(200, await t.start.result);
|
|
174
|
+
}
|
|
175
|
+
const result = runs.get(input.runId);
|
|
176
|
+
if (!result) throw new StudioError("This launcher does not own the run.", 404);
|
|
177
|
+
if (input.action === "stop" && active?.runId === input.runId) active.stop();
|
|
178
|
+
reply(200, result);
|
|
179
|
+
} catch (error) {
|
|
180
|
+
if (error instanceof StudioError)
|
|
181
|
+
reply(error.status, { error: error.message, runId: error.runId });
|
|
182
|
+
else
|
|
183
|
+
reply(400, {
|
|
184
|
+
error: "Unable to read or start this flow. Review the project configuration."
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
server.requestTimeout = 1e4;
|
|
189
|
+
await new Promise((res, rej) => {
|
|
190
|
+
server.once("error", rej);
|
|
191
|
+
server.listen(path, res);
|
|
192
|
+
});
|
|
193
|
+
return {
|
|
194
|
+
path,
|
|
195
|
+
async close() {
|
|
196
|
+
closing = true;
|
|
197
|
+
const closed = new Promise((res) => server.close(() => res()));
|
|
198
|
+
active?.stop();
|
|
199
|
+
await Promise.all([closed, active?.done]);
|
|
200
|
+
await rm(temp, { force: true, recursive: true });
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
export {
|
|
205
|
+
acquireStudioLock,
|
|
206
|
+
listenStudio
|
|
207
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { loadConfig, type LoadedFlow } from "./flow-loader.js";
|
|
2
|
+
export declare class StudioError extends Error {
|
|
3
|
+
status: number;
|
|
4
|
+
runId?: string;
|
|
5
|
+
constructor(message: string, status?: number, runId?: string);
|
|
6
|
+
}
|
|
7
|
+
export declare function studioRevision(config: Awaited<ReturnType<typeof loadConfig>>, doc: LoadedFlow): string;
|
|
8
|
+
export declare function reviewFlow(root: string, project: string, source: string, id: string, name: string): Promise<{
|
|
9
|
+
revision: string;
|
|
10
|
+
estimate: string[];
|
|
11
|
+
prompt: string;
|
|
12
|
+
flow: string;
|
|
13
|
+
target: string;
|
|
14
|
+
url: string;
|
|
15
|
+
budget: {
|
|
16
|
+
actions?: number | undefined;
|
|
17
|
+
minutes?: number | undefined;
|
|
18
|
+
usd?: number | undefined;
|
|
19
|
+
} | null;
|
|
20
|
+
recording: "rrweb"[];
|
|
21
|
+
policy: "never" | "safe";
|
|
22
|
+
runtime: string;
|
|
23
|
+
}>;
|
|
24
|
+
export type Review = Awaited<ReturnType<typeof reviewFlow>>;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { compile, isProtectedTarget, targetAllowsUrl } from "@open-qa/qa-engine";
|
|
4
|
+
import { loadConfig, loadFlowDocument } from "./flow-loader.js";
|
|
5
|
+
import { confirmRunEstimate } from "./budget-confirmation.js";
|
|
6
|
+
import { FileRunStore } from "./adapters/file-run-store.js";
|
|
7
|
+
class StudioError extends Error {
|
|
8
|
+
status;
|
|
9
|
+
runId;
|
|
10
|
+
constructor(message, status = 409, runId) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.status = status;
|
|
13
|
+
this.runId = runId;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function studioRevision(config, doc) {
|
|
17
|
+
return createHash("sha256").update(JSON.stringify([config, doc.definitions])).digest("hex");
|
|
18
|
+
}
|
|
19
|
+
async function reviewFlow(root, project, source, id, name) {
|
|
20
|
+
const config = await loadConfig(root);
|
|
21
|
+
const target = config.targets && Object.hasOwn(config.targets, name) && config.targets[name];
|
|
22
|
+
if (!target || config.projectId !== project || !targetAllowsUrl(target, target.url))
|
|
23
|
+
throw new StudioError("This test environment is unavailable.", 400);
|
|
24
|
+
if (isProtectedTarget(name, target))
|
|
25
|
+
throw new StudioError("Protected environments require the CLI.", 403);
|
|
26
|
+
const doc = await loadFlowDocument(resolve(root, source), root);
|
|
27
|
+
if (createHash("sha256").update(doc.flow.key).digest("hex") !== id)
|
|
28
|
+
throw new StudioError("Configuration changed; review again.");
|
|
29
|
+
const plan = compile(doc.flow);
|
|
30
|
+
const estimate = [];
|
|
31
|
+
let prompt = "";
|
|
32
|
+
const store = new FileRunStore(root);
|
|
33
|
+
await confirmRunEstimate({
|
|
34
|
+
flows: [plan.key],
|
|
35
|
+
recentUsage: store.recentUsage.bind(store),
|
|
36
|
+
thresholds: config.confirmAbove,
|
|
37
|
+
subscription: true,
|
|
38
|
+
attempts: 1,
|
|
39
|
+
interactive: true,
|
|
40
|
+
output: (line) => estimate.push(line),
|
|
41
|
+
confirm: async (message) => {
|
|
42
|
+
prompt = message;
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
const url = new URL(target.url);
|
|
47
|
+
return {
|
|
48
|
+
revision: studioRevision(config, doc),
|
|
49
|
+
estimate,
|
|
50
|
+
prompt,
|
|
51
|
+
flow: plan.key,
|
|
52
|
+
target: name,
|
|
53
|
+
url: url.origin + url.pathname,
|
|
54
|
+
budget: plan.budget ?? null,
|
|
55
|
+
recording: plan.record,
|
|
56
|
+
policy: plan.prod,
|
|
57
|
+
runtime: "Claude Code with subscription login on the local host (one attempt)"
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
export {
|
|
61
|
+
StudioError,
|
|
62
|
+
reviewFlow,
|
|
63
|
+
studioRevision
|
|
64
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { RunSetupError } from "./run-setup-error.js";
|
|
2
|
+
import { runCommand } from "./run-command.js";
|
|
3
|
+
import { studioRevision } from "./studio-review.js";
|
|
4
|
+
const controller = new AbortController();
|
|
5
|
+
const cancel = () => controller.abort(new Error("Studio canceled this run"));
|
|
6
|
+
process.once("disconnect", cancel);
|
|
7
|
+
process.once("message", async (start) => {
|
|
8
|
+
process.on("message", (message) => {
|
|
9
|
+
if (message === "stop") cancel();
|
|
10
|
+
});
|
|
11
|
+
let estimate = "";
|
|
12
|
+
let created = false;
|
|
13
|
+
try {
|
|
14
|
+
process.exitCode = await runCommand(
|
|
15
|
+
start.path,
|
|
16
|
+
{ root: start.root, target: start.review.target, retries: 0 },
|
|
17
|
+
{
|
|
18
|
+
signal: controller.signal,
|
|
19
|
+
interactive: true,
|
|
20
|
+
output: (line) => {
|
|
21
|
+
if (line.startsWith("Estimate ")) estimate = line;
|
|
22
|
+
},
|
|
23
|
+
confirm: async (message) => {
|
|
24
|
+
if (!start.accepted || message !== start.review.prompt || estimate !== start.review.estimate[0])
|
|
25
|
+
throw new RunSetupError("review-changed", "Review changed");
|
|
26
|
+
return true;
|
|
27
|
+
},
|
|
28
|
+
studio: {
|
|
29
|
+
runId: start.runId,
|
|
30
|
+
verify: (config, doc) => {
|
|
31
|
+
if (studioRevision(config, doc) !== start.review.revision)
|
|
32
|
+
throw new RunSetupError("review-changed", "Review changed");
|
|
33
|
+
},
|
|
34
|
+
created: () => {
|
|
35
|
+
created = true;
|
|
36
|
+
process.send?.("running");
|
|
37
|
+
},
|
|
38
|
+
saved: () => process.send?.("saved")
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
);
|
|
42
|
+
} catch (error) {
|
|
43
|
+
if (!created) process.send?.(error instanceof RunSetupError ? error.code : "configuration");
|
|
44
|
+
process.exitCode = 2;
|
|
45
|
+
} finally {
|
|
46
|
+
if (process.connected) process.disconnect();
|
|
47
|
+
}
|
|
48
|
+
});
|