@open-qa/qa 0.0.0-stage → 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.
Files changed (313) hide show
  1. package/LICENSE +204 -0
  2. package/NOTICE +7 -0
  3. package/THIRD_PARTY.md +12 -0
  4. package/THIRD_PARTY_NOTICES.md +11 -0
  5. package/drizzle/0000_qa_core.sql +158 -0
  6. package/drizzle/0001_run_health.sql +3 -0
  7. package/drizzle/0002_bounded_cleanup.sql +5 -0
  8. package/drizzle/0003_step_replay_cache.sql +9 -0
  9. package/drizzle/0004_credential_expiry.sql +1 -0
  10. package/drizzle/0005_server_api.sql +19 -0
  11. package/drizzle/0006_submission_created.sql +3 -0
  12. package/drizzle/0007_findings.sql +19 -0
  13. package/drizzle/0008_classification_labels.sql +14 -0
  14. package/drizzle/0009_job_queue.sql +20 -0
  15. package/drizzle/0010_result_notifications.sql +13 -0
  16. package/drizzle/0011_result_notification_retries.sql +5 -0
  17. package/drizzle/0012_result_notification_flows.sql +44 -0
  18. package/drizzle/0013_flow_quarantine.sql +35 -0
  19. package/drizzle/0014_run_budgets.sql +2 -0
  20. package/drizzle/0015_flow_lifecycle.sql +9 -0
  21. package/drizzle/0016_open_qa_classification.sql +28 -0
  22. package/drizzle/meta/0000_snapshot.json +1002 -0
  23. package/drizzle/meta/0001_snapshot.json +1021 -0
  24. package/drizzle/meta/0002_snapshot.json +1027 -0
  25. package/drizzle/meta/0003_snapshot.json +1081 -0
  26. package/drizzle/meta/0004_snapshot.json +1087 -0
  27. package/drizzle/meta/0005_snapshot.json +1210 -0
  28. package/drizzle/meta/0006_snapshot.json +1244 -0
  29. package/drizzle/meta/0007_snapshot.json +1353 -0
  30. package/drizzle/meta/0008_snapshot.json +1435 -0
  31. package/drizzle/meta/0009_snapshot.json +1587 -0
  32. package/drizzle/meta/0010_snapshot.json +1675 -0
  33. package/drizzle/meta/0011_snapshot.json +1692 -0
  34. package/drizzle/meta/0012_snapshot.json +1818 -0
  35. package/drizzle/meta/0013_snapshot.json +2016 -0
  36. package/drizzle/meta/0014_snapshot.json +2028 -0
  37. package/drizzle/meta/0015_snapshot.json +2064 -0
  38. package/drizzle/meta/0016_snapshot.json +2076 -0
  39. package/drizzle/meta/_journal.json +125 -0
  40. package/package.json +118 -4
  41. package/problem-matcher.json +32 -0
  42. package/src/adapters/db-lease-pool.d.ts +25 -0
  43. package/src/adapters/db-lease-pool.js +293 -0
  44. package/src/adapters/dir-artifact-store.d.ts +9 -0
  45. package/src/adapters/dir-artifact-store.js +64 -0
  46. package/src/adapters/e2b-agent.d.ts +7 -0
  47. package/src/adapters/e2b-agent.js +94 -0
  48. package/src/adapters/e2b-artifacts.d.ts +3 -0
  49. package/src/adapters/e2b-artifacts.js +66 -0
  50. package/src/adapters/e2b-bounded.d.ts +1 -0
  51. package/src/adapters/e2b-bounded.js +19 -0
  52. package/src/adapters/e2b-client.d.ts +33 -0
  53. package/src/adapters/e2b-client.js +114 -0
  54. package/src/adapters/e2b-executor.d.ts +26 -0
  55. package/src/adapters/e2b-executor.js +194 -0
  56. package/src/adapters/e2b-lifetime.d.ts +20 -0
  57. package/src/adapters/e2b-lifetime.js +73 -0
  58. package/src/adapters/e2b-prune.d.ts +13 -0
  59. package/src/adapters/e2b-prune.js +42 -0
  60. package/src/adapters/file-replay-cache.d.ts +14 -0
  61. package/src/adapters/file-replay-cache.js +120 -0
  62. package/src/adapters/file-run-store.d.ts +175 -0
  63. package/src/adapters/file-run-store.js +280 -0
  64. package/src/adapters/fixed-lease-pool.d.ts +10 -0
  65. package/src/adapters/fixed-lease-pool.js +237 -0
  66. package/src/adapters/gcs/artifact-store.d.ts +49 -0
  67. package/src/adapters/gcs/artifact-store.js +247 -0
  68. package/src/adapters/gcs/client.d.ts +22 -0
  69. package/src/adapters/gcs/client.js +106 -0
  70. package/src/adapters/gcs/download.d.ts +9 -0
  71. package/src/adapters/gcs/download.js +43 -0
  72. package/src/adapters/gcs/index.d.ts +8 -0
  73. package/src/adapters/gcs/index.js +23 -0
  74. package/src/adapters/gcs/periodic-flush.d.ts +5 -0
  75. package/src/adapters/gcs/periodic-flush.js +16 -0
  76. package/src/adapters/gcs/pg-catalog.d.ts +16 -0
  77. package/src/adapters/gcs/pg-catalog.js +149 -0
  78. package/src/adapters/gcs/replay-completeness.d.ts +1 -0
  79. package/src/adapters/gcs/replay-completeness.js +65 -0
  80. package/src/adapters/gcs/scan-upload.d.ts +10 -0
  81. package/src/adapters/gcs/scan-upload.js +26 -0
  82. package/src/adapters/index.d.ts +5 -0
  83. package/src/adapters/index.js +12 -0
  84. package/src/adapters/lease-transaction.d.ts +2 -0
  85. package/src/adapters/lease-transaction.js +61 -0
  86. package/src/adapters/local-credential-resolver.d.ts +5 -0
  87. package/src/adapters/local-credential-resolver.js +15 -0
  88. package/src/adapters/local-env.d.ts +1 -0
  89. package/src/adapters/local-env.js +37 -0
  90. package/src/adapters/local-files.d.ts +17 -0
  91. package/src/adapters/local-files.js +150 -0
  92. package/src/adapters/pg-classification-store.d.ts +58 -0
  93. package/src/adapters/pg-classification-store.js +112 -0
  94. package/src/adapters/pg-replay-cache.d.ts +16 -0
  95. package/src/adapters/pg-replay-cache.js +111 -0
  96. package/src/adapters/pg-run-store.d.ts +58 -0
  97. package/src/adapters/pg-run-store.js +399 -0
  98. package/src/adapters/resource-pools.d.ts +2 -0
  99. package/src/adapters/resource-pools.js +28 -0
  100. package/src/adapters/secret-manager-credential-resolver.d.ts +44 -0
  101. package/src/adapters/secret-manager-credential-resolver.js +102 -0
  102. package/src/artifact-scan.d.ts +21 -0
  103. package/src/artifact-scan.js +139 -0
  104. package/src/budget-confirmation.d.ts +23 -0
  105. package/src/budget-confirmation.js +43 -0
  106. package/src/cache-command.d.ts +7 -0
  107. package/src/cache-command.js +107 -0
  108. package/src/classification-command.d.ts +14 -0
  109. package/src/classification-command.js +72 -0
  110. package/src/classification-diff.d.ts +10 -0
  111. package/src/classification-diff.js +47 -0
  112. package/src/classification-model.d.ts +17 -0
  113. package/src/classification-model.js +51 -0
  114. package/src/classification-notifier.d.ts +21 -0
  115. package/src/classification-notifier.js +34 -0
  116. package/src/classify-runs.d.ts +31 -0
  117. package/src/classify-runs.js +138 -0
  118. package/src/cli-program.d.ts +9 -0
  119. package/src/cli-program.js +172 -0
  120. package/src/cli.d.ts +2 -0
  121. package/src/cli.js +12 -0
  122. package/src/commands.d.ts +7 -0
  123. package/src/commands.js +17 -0
  124. package/src/credential-references.d.ts +24 -0
  125. package/src/credential-references.js +33 -0
  126. package/src/db/commands.d.ts +1 -0
  127. package/src/db/commands.js +34 -0
  128. package/src/db/credentials.d.ts +16 -0
  129. package/src/db/credentials.js +73 -0
  130. package/src/db/database.d.ts +12 -0
  131. package/src/db/database.js +135 -0
  132. package/src/db/event-chunks.d.ts +405 -0
  133. package/src/db/event-chunks.js +74 -0
  134. package/src/db/fencing.d.ts +8 -0
  135. package/src/db/fencing.js +16 -0
  136. package/src/db/flow-status.d.ts +41 -0
  137. package/src/db/flow-status.js +208 -0
  138. package/src/db/index.d.ts +12 -0
  139. package/src/db/index.js +24 -0
  140. package/src/db/queries.d.ts +73 -0
  141. package/src/db/queries.js +122 -0
  142. package/src/db/resources.d.ts +14 -0
  143. package/src/db/resources.js +61 -0
  144. package/src/db/schema.d.ts +3532 -0
  145. package/src/db/schema.js +501 -0
  146. package/src/demo-command.d.ts +1 -0
  147. package/src/demo-command.js +50 -0
  148. package/src/doctor-command.d.ts +15 -0
  149. package/src/doctor-command.js +275 -0
  150. package/src/doctor-summary.d.ts +2 -0
  151. package/src/doctor-summary.js +31 -0
  152. package/src/draft-api.d.ts +460 -0
  153. package/src/draft-api.js +333 -0
  154. package/src/draft-command.d.ts +10 -0
  155. package/src/draft-command.js +73 -0
  156. package/src/draft-hooks.d.ts +3 -0
  157. package/src/draft-hooks.js +33 -0
  158. package/src/draft-pages.d.ts +11 -0
  159. package/src/draft-pages.js +82 -0
  160. package/src/engine.d.ts +105 -0
  161. package/src/engine.js +323 -0
  162. package/src/execution-snapshot.d.ts +21 -0
  163. package/src/execution-snapshot.js +141 -0
  164. package/src/flow-check.d.ts +9 -0
  165. package/src/flow-check.js +68 -0
  166. package/src/flow-lifecycle.d.ts +6 -0
  167. package/src/flow-lifecycle.js +61 -0
  168. package/src/flow-lint.d.ts +8 -0
  169. package/src/flow-lint.js +73 -0
  170. package/src/flow-loader.d.ts +219 -0
  171. package/src/flow-loader.js +236 -0
  172. package/src/flow-source.d.ts +2 -0
  173. package/src/flow-source.js +6 -0
  174. package/src/flows-api.d.ts +1 -0
  175. package/src/flows-api.js +12 -0
  176. package/src/heal-notification.d.ts +16 -0
  177. package/src/heal-notification.js +62 -0
  178. package/src/hooks/api.d.ts +29 -0
  179. package/src/hooks/api.js +15 -0
  180. package/src/hooks/consumer.d.ts +18 -0
  181. package/src/hooks/consumer.js +98 -0
  182. package/src/hooks/loader.d.ts +6 -0
  183. package/src/hooks/loader.js +28 -0
  184. package/src/hooks/registry.d.ts +22 -0
  185. package/src/hooks/registry.js +23 -0
  186. package/src/hooks/runner.d.ts +19 -0
  187. package/src/hooks/runner.js +279 -0
  188. package/src/hooks/session-cache.d.ts +16 -0
  189. package/src/hooks/session-cache.js +42 -0
  190. package/src/hooks/session-store.d.ts +20 -0
  191. package/src/hooks/session-store.js +95 -0
  192. package/src/hooks/worker.d.ts +1 -0
  193. package/src/hooks/worker.js +84 -0
  194. package/src/http-stage-client.d.ts +367 -0
  195. package/src/http-stage-client.js +190 -0
  196. package/src/independent-model.d.ts +22 -0
  197. package/src/independent-model.js +215 -0
  198. package/src/lifecycle-command.d.ts +15 -0
  199. package/src/lifecycle-command.js +190 -0
  200. package/src/local-command.d.ts +3 -0
  201. package/src/local-command.js +27 -0
  202. package/src/local-host-executor.d.ts +38 -0
  203. package/src/local-host-executor.js +196 -0
  204. package/src/open-command.d.ts +7 -0
  205. package/src/open-command.js +39 -0
  206. package/src/operations-api.d.ts +2 -0
  207. package/src/operations-api.js +6 -0
  208. package/src/personas.d.ts +2 -0
  209. package/src/personas.js +61 -0
  210. package/src/program-runner.d.ts +3 -0
  211. package/src/program-runner.js +263 -0
  212. package/src/program-sandbox.py +75 -0
  213. package/src/program-scratch.d.ts +2 -0
  214. package/src/program-scratch.js +48 -0
  215. package/src/program-worker.mjs +101 -0
  216. package/src/public-id.d.ts +2 -0
  217. package/src/public-id.js +5 -0
  218. package/src/public-report-url.d.ts +1 -0
  219. package/src/public-report-url.js +18 -0
  220. package/src/publication-check.d.ts +1 -0
  221. package/src/publication-check.js +31 -0
  222. package/src/quarantine-command.d.ts +9 -0
  223. package/src/quarantine-command.js +69 -0
  224. package/src/remote-command.d.ts +30 -0
  225. package/src/remote-command.js +156 -0
  226. package/src/replay-image.d.ts +1 -0
  227. package/src/replay-image.js +35 -0
  228. package/src/report/assets.d.ts +1 -0
  229. package/src/report/assets.js +5 -0
  230. package/src/report/browser.css +414 -0
  231. package/src/report/browser.d.ts +1 -0
  232. package/src/report/browser.js +219 -0
  233. package/src/report/data.d.ts +1 -0
  234. package/src/report/data.js +4 -0
  235. package/src/report/generate.d.ts +1 -0
  236. package/src/report/generate.js +54 -0
  237. package/src/report/load.d.ts +870 -0
  238. package/src/report/load.js +139 -0
  239. package/src/report/resource-table.d.ts +2 -0
  240. package/src/report/resource-table.js +4 -0
  241. package/src/report/status-icon.d.ts +2 -0
  242. package/src/report/status-icon.js +20 -0
  243. package/src/report/style.css +413 -0
  244. package/src/result-notifier.d.ts +121 -0
  245. package/src/result-notifier.js +230 -0
  246. package/src/result-transports.d.ts +12 -0
  247. package/src/result-transports.js +150 -0
  248. package/src/run-command.d.ts +32 -0
  249. package/src/run-command.js +493 -0
  250. package/src/run-setup-error.d.ts +11 -0
  251. package/src/run-setup-error.js +18 -0
  252. package/src/sandboxes-command.d.ts +9 -0
  253. package/src/sandboxes-command.js +29 -0
  254. package/src/screen-proxy.d.ts +5 -0
  255. package/src/screen-proxy.js +79 -0
  256. package/src/screen-url.d.ts +18 -0
  257. package/src/screen-url.js +54 -0
  258. package/src/server/app.d.ts +29 -0
  259. package/src/server/app.js +350 -0
  260. package/src/server/auth.d.ts +33 -0
  261. package/src/server/auth.js +113 -0
  262. package/src/server/credentials.d.ts +30 -0
  263. package/src/server/credentials.js +77 -0
  264. package/src/server/errors.d.ts +9 -0
  265. package/src/server/errors.js +26 -0
  266. package/src/server/index.d.ts +35 -0
  267. package/src/server/index.js +96 -0
  268. package/src/server/maintenance.d.ts +13 -0
  269. package/src/server/maintenance.js +54 -0
  270. package/src/server/repository.d.ts +534 -0
  271. package/src/server/repository.js +219 -0
  272. package/src/setup-command.d.ts +10 -0
  273. package/src/setup-command.js +52 -0
  274. package/src/stage-environment.d.ts +1 -0
  275. package/src/stage-environment.js +35 -0
  276. package/src/stage-supervisor.mjs +95 -0
  277. package/src/studio-command.d.ts +7 -0
  278. package/src/studio-command.js +47 -0
  279. package/src/studio-control.d.ts +5 -0
  280. package/src/studio-control.js +207 -0
  281. package/src/studio-review.d.ts +24 -0
  282. package/src/studio-review.js +64 -0
  283. package/src/studio-worker.d.ts +1 -0
  284. package/src/studio-worker.js +48 -0
  285. package/src/system-config.d.ts +7 -0
  286. package/src/system-config.js +70 -0
  287. package/src/terminal-reporter.d.ts +13 -0
  288. package/src/terminal-reporter.js +93 -0
  289. package/src/vendor/gmail.d.ts +45 -0
  290. package/src/vendor/gmail.js +193 -0
  291. package/src/watch-command.d.ts +16 -0
  292. package/src/watch-command.js +93 -0
  293. package/src/watch-state.d.ts +4 -0
  294. package/src/watch-state.js +53 -0
  295. package/src/worker/execution-error.d.ts +5 -0
  296. package/src/worker/execution-error.js +10 -0
  297. package/src/worker/execution.d.ts +24 -0
  298. package/src/worker/execution.js +190 -0
  299. package/src/worker/index.d.ts +8 -0
  300. package/src/worker/index.js +40 -0
  301. package/src/worker/prune.d.ts +3 -0
  302. package/src/worker/prune.js +22 -0
  303. package/src/worker/quarantine-notifications.d.ts +8 -0
  304. package/src/worker/quarantine-notifications.js +89 -0
  305. package/src/worker/queue.d.ts +65 -0
  306. package/src/worker/queue.js +438 -0
  307. package/src/worker/result-notifications.d.ts +28 -0
  308. package/src/worker/result-notifications.js +347 -0
  309. package/src/worker/store.d.ts +4 -0
  310. package/src/worker/store.js +46 -0
  311. package/src/worker/worker.d.ts +21 -0
  312. package/src/worker/worker.js +140 -0
  313. package/README.md +0 -3
@@ -0,0 +1,31 @@
1
+ import { readdir } from "node:fs/promises";
2
+ import { join } from "node:path";
3
+ import { SecretRedactor } from "@open-qa/qa-engine";
4
+ import { assertDirectory } from "./adapters/local-files.js";
5
+ import { FileRunStore } from "./adapters/file-run-store.js";
6
+ import { artifactScanSchema, scanRunArtifacts } from "./artifact-scan.js";
7
+ async function assertPublishableRuns(root, secrets) {
8
+ try {
9
+ const redactor = new SecretRedactor();
10
+ redactor.add(secrets);
11
+ const store = new FileRunStore(root);
12
+ await assertDirectory(root);
13
+ await assertDirectory(join(root, "runs"));
14
+ for (const entry of await readdir(join(root, "runs"), { withFileTypes: true })) {
15
+ if (entry.isSymbolicLink()) throw new Error("Symbolic run directory");
16
+ if (!entry.isDirectory()) continue;
17
+ const run = await store.getRun(entry.name);
18
+ const receipt = artifactScanSchema.safeParse(run?.run.metadata?.artifactScan);
19
+ if (!receipt.success || receipt.data.status !== "passed")
20
+ throw new Error("Missing passing secret scan");
21
+ const current = await scanRunArtifacts(root, entry.name, redactor);
22
+ if (current.status !== "passed" || JSON.stringify(current.files) !== JSON.stringify(receipt.data.files))
23
+ throw new Error("Artifact scan no longer matches run files");
24
+ }
25
+ } catch {
26
+ throw new Error("Artifact publication check failed; upload blocked");
27
+ }
28
+ }
29
+ export {
30
+ assertPublishableRuns
31
+ };
@@ -0,0 +1,9 @@
1
+ import type { FlowStatusStore, RepeatResult } from "@open-qa/qa-engine";
2
+ export declare function printRepeat(result: RepeatResult | null, output: (line: string) => void): void;
3
+ export declare function quarantineCommand(action: "list" | "release", flow: string | undefined, options?: {
4
+ project?: string;
5
+ root?: string;
6
+ }, adapters?: {
7
+ store?: FlowStatusStore;
8
+ output?: (line: string) => void;
9
+ }): Promise<number>;
@@ -0,0 +1,69 @@
1
+ import { resolve } from "node:path";
2
+ import { QaDatabase, publicId } from "./db/database.js";
3
+ import { PgFlowStatus } from "./db/flow-status.js";
4
+ import { loadConfig } from "./flow-loader.js";
5
+ function printRepeat(result, output) {
6
+ if (!result) {
7
+ output("No saved repeat result. Run --repeat before releasing this flow.");
8
+ return;
9
+ }
10
+ const canceled = result.runs.filter((run) => run.verdict === "canceled").length;
11
+ const completed = result.runs.length - canceled;
12
+ const passed = result.runs.filter((run) => run.verdict === "passed").length;
13
+ output(
14
+ `Latest repeat \xB7 ${new Date(result.finishedAt).toISOString()} \xB7 target ${result.target} \xB7 ${completed}/${result.requested} completed \xB7 ${canceled} canceled \xB7 ${passed}/${completed} passed (${completed ? (passed / completed * 100).toFixed(1) : "0.0"}%)`
15
+ );
16
+ for (const [index, run] of result.runs.entries())
17
+ output(` ${index + 1}. ${run.runId} \xB7 ${run.verdict} \xB7 ${run.category ?? "unclassified"}`);
18
+ }
19
+ async function quarantineCommand(action, flow, options = {}, adapters = {}) {
20
+ const output = adapters.output ?? console.log;
21
+ let database;
22
+ try {
23
+ let store = adapters.store;
24
+ if (!store) {
25
+ const project = options.project ?? process.env.QA_PROJECT_ID ?? (await loadConfig(resolve(options.root ?? ".qa"))).projectId;
26
+ if (!publicId.safeParse(project).success)
27
+ throw new Error("Quarantine requires a valid QA project ID");
28
+ if (!process.env.QA_DATABASE_URL) throw new Error("Quarantine requires QA_DATABASE_URL");
29
+ database = new QaDatabase(process.env.QA_DATABASE_URL);
30
+ store = new PgFlowStatus(database, project);
31
+ }
32
+ if (action === "list") {
33
+ const rows = await store.list();
34
+ if (!rows.length) output("No quarantined flows");
35
+ for (const row2 of rows) {
36
+ output(
37
+ `${row2.flow} \xB7 quarantined \xB7 flaky streak ${row2.flakyStreak} \xB7 owner ${row2.owner ?? "unassigned"}`
38
+ );
39
+ printRepeat(row2.latestRepeat, output);
40
+ }
41
+ return 0;
42
+ }
43
+ if (!flow) throw new Error("quarantine release requires a flow");
44
+ const current = await store.get(flow);
45
+ if (current?.status === "active") {
46
+ output(`${current.flow} \xB7 already active \xB7 flaky streak unchanged (${current.flakyStreak})`);
47
+ printRepeat(current.latestRepeat, output);
48
+ return 0;
49
+ }
50
+ const row = await store.release(flow);
51
+ if (!row) {
52
+ output("Flow has no recorded quarantine state");
53
+ return 1;
54
+ }
55
+ output(`${row.flow} \xB7 active \xB7 flaky streak reset`);
56
+ printRepeat(row.latestRepeat, output);
57
+ return 0;
58
+ } catch {
59
+ throw new Error(
60
+ "QA quarantine command failed; check the project ID, QA_DATABASE_URL and applied migrations"
61
+ );
62
+ } finally {
63
+ await database?.close();
64
+ }
65
+ }
66
+ export {
67
+ printRepeat,
68
+ quarantineCommand
69
+ };
@@ -0,0 +1,30 @@
1
+ import { type BudgetPrompt } from "./budget-confirmation.js";
2
+ import type { RunOptions } from "./run-command.js";
3
+ export interface RemoteOptions {
4
+ server?: string;
5
+ project?: string;
6
+ idempotencyKey?: string;
7
+ }
8
+ export declare class QaRemoteError extends Error {
9
+ readonly status: number;
10
+ constructor(status: number);
11
+ }
12
+ export declare class QaRemoteClient {
13
+ private readonly env;
14
+ private readonly request;
15
+ private readonly base;
16
+ constructor(options: RemoteOptions, env?: NodeJS.ProcessEnv, request?: typeof fetch);
17
+ call(path: string, method?: string, body?: unknown, key?: string, signal?: AbortSignal): Promise<unknown>;
18
+ }
19
+ export declare function remoteRunCommand(flow: string, options: RunOptions & RemoteOptions, deps?: BudgetPrompt & {
20
+ client?: QaRemoteClient;
21
+ output?: (line: string) => void;
22
+ }): Promise<number>;
23
+ export declare function authCommand(action: "connect" | "status" | "revoke", kind: string | undefined, options: RemoteOptions & {
24
+ runtime?: string;
25
+ expiresAt?: string;
26
+ }, deps?: {
27
+ client?: QaRemoteClient;
28
+ input?: () => Promise<string>;
29
+ output?: (line: string) => void;
30
+ }): Promise<number>;
@@ -0,0 +1,156 @@
1
+ import { resolve } from "node:path";
2
+ import { loadConfig } from "./flow-loader.js";
3
+ import { confirmRunEstimate } from "./budget-confirmation.js";
4
+ import { randomUUID } from "node:crypto";
5
+ import { z } from "zod";
6
+ import {
7
+ batchDtoSchema,
8
+ runUsageSchema,
9
+ serverIdSchema,
10
+ credentialConnectSchema,
11
+ credentialDtoSchema,
12
+ submitBatchSchema
13
+ } from "@open-qa/qa-engine/protocol";
14
+ class QaRemoteError extends Error {
15
+ constructor(status) {
16
+ super(`QA server rejected request (${status})`);
17
+ this.status = status;
18
+ }
19
+ status;
20
+ }
21
+ class QaRemoteClient {
22
+ constructor(options, env = process.env, request = fetch) {
23
+ this.env = env;
24
+ this.request = request;
25
+ const project = serverIdSchema.safeParse(options.project ?? env.QA_PROJECT_ID);
26
+ let base;
27
+ try {
28
+ base = new URL(options.server ?? env.QA_SERVER_URL ?? "");
29
+ } catch {
30
+ throw new Error("Configure QA_SERVER_URL or --server");
31
+ }
32
+ if (!project.success) throw new Error("Configure QA_PROJECT_ID or --project");
33
+ if (base.protocol !== "https:" && !(base.protocol === "http:" && ["127.0.0.1", "localhost", "[::1]"].includes(base.hostname)) || base.username || base.password || base.search || base.hash)
34
+ throw new Error("QA server requires HTTPS or loopback HTTP");
35
+ base.pathname = `${base.pathname.replace(/\/$/, "")}/projects/${project.data}`;
36
+ this.base = base;
37
+ }
38
+ env;
39
+ request;
40
+ base;
41
+ async call(path, method = "GET", body, key, signal) {
42
+ const token = this.env.QA_IDENTITY_TOKEN;
43
+ if (!token || /\s/.test(token))
44
+ throw new Error("Configure QA_IDENTITY_TOKEN from the trusted identity provider");
45
+ const mode = this.env.QA_IDENTITY_KIND ?? "proxy";
46
+ if (mode !== "proxy" && mode !== "bearer")
47
+ throw new Error("QA_IDENTITY_KIND must be proxy or bearer");
48
+ const headers = {
49
+ "X-QA-Request": "1",
50
+ "Content-Type": "application/json",
51
+ ...mode === "proxy" ? { "CF-Access-JWT-Assertion": token } : { Authorization: `Bearer ${token}` }
52
+ };
53
+ if (key) headers["Idempotency-Key"] = key;
54
+ let response;
55
+ try {
56
+ response = await this.request(`${this.base.href}${path}`, {
57
+ method,
58
+ headers,
59
+ body: body === void 0 ? void 0 : JSON.stringify(body),
60
+ redirect: "error",
61
+ signal: signal ? AbortSignal.any([signal, AbortSignal.timeout(3e4)]) : AbortSignal.timeout(3e4)
62
+ });
63
+ } catch {
64
+ throw new Error("QA server request failed; retry with the same idempotency key");
65
+ }
66
+ if (!response.ok) throw new QaRemoteError(response.status);
67
+ try {
68
+ return await response.json();
69
+ } catch {
70
+ throw new Error("Invalid QA server response");
71
+ }
72
+ }
73
+ }
74
+ async function remoteRunCommand(flow, options, deps = {}) {
75
+ if (options.headed || options.level || options.persona || options.tag)
76
+ throw new Error("Remote runs use published flow IDs and server configuration");
77
+ if (options.agentAt && options.agentAt !== "stage")
78
+ throw new Error("QA server agents must run at stage");
79
+ if (options.modelAccess && !["personal", "team"].includes(options.modelAccess))
80
+ throw new Error("Remote model access must be personal or team");
81
+ const request = submitBatchSchema.parse({
82
+ flows: [flow],
83
+ target: options.target ?? "local",
84
+ runtime: options.runtime ?? "claude-code",
85
+ modelAccess: options.modelAccess === "team" ? "team" : "personal",
86
+ retries: options.retries ?? 1
87
+ });
88
+ const output = deps.output ?? console.log;
89
+ const client = deps.client ?? new QaRemoteClient(options);
90
+ const config = await loadConfig(resolve(options.root ?? ".qa"));
91
+ await confirmRunEstimate({
92
+ thresholds: config.confirmAbove,
93
+ ...deps,
94
+ flows: request.flows,
95
+ recentUsage: async (flow2) => {
96
+ try {
97
+ return z.object({ samples: z.array(runUsageSchema).max(5) }).parse(await client.call(`/usage?flow=${encodeURIComponent(flow2)}`)).samples;
98
+ } catch (error) {
99
+ if (error instanceof QaRemoteError && [404, 503].includes(error.status)) return [];
100
+ throw error;
101
+ }
102
+ },
103
+ subscription: request.modelAccess === "personal" && request.runtime === "claude-code",
104
+ acceptEstimate: options.acceptEstimate,
105
+ attempts: request.retries + 1
106
+ });
107
+ const key = options.idempotencyKey ?? randomUUID();
108
+ output(`Idempotency key: ${key}`);
109
+ const result = batchDtoSchema.parse(await client.call("/batches", "POST", request, key));
110
+ output(`Batch queued: ${result.id}`);
111
+ return 0;
112
+ }
113
+ async function authCommand(action, kind, options, deps = {}) {
114
+ const client = deps.client ?? new QaRemoteClient(options);
115
+ const output = deps.output ?? console.log;
116
+ if (action === "status") {
117
+ const response = z.object({ credentials: z.array(credentialDtoSchema) }).parse(await client.call("/credentials"));
118
+ output(
119
+ response.credentials.length ? response.credentials.map((link) => `${link.id}: ${link.state}`).join("\n") : "No personal credentials connected"
120
+ );
121
+ } else if (action === "revoke") {
122
+ const runtime = z.enum(["claude-code", "pi"]).safeParse(kind);
123
+ if (!runtime.success) throw new Error("Revoke requires a runtime: claude-code or pi");
124
+ await client.call(`/credentials/${runtime.data}`, "DELETE");
125
+ output("Personal credential revoked");
126
+ } else {
127
+ const runtime = options.runtime ?? (kind === "claude" ? "claude-code" : "pi");
128
+ const token = (await (deps.input ?? readToken)()).trim();
129
+ const input = credentialConnectSchema.safeParse({
130
+ kind,
131
+ runtime,
132
+ token,
133
+ expiresAt: options.expiresAt ? Date.parse(options.expiresAt) : null
134
+ });
135
+ if (!input.success) throw new Error("Invalid credential kind, runtime, expiry or token");
136
+ await client.call("/credentials", "POST", input.data);
137
+ output("Personal credential connected");
138
+ }
139
+ return 0;
140
+ }
141
+ async function readToken() {
142
+ if (process.stdin.isTTY)
143
+ throw new Error("Pipe the credential to stdin; do not put it in command arguments");
144
+ let value = "";
145
+ for await (const chunk of process.stdin) {
146
+ value += String(chunk);
147
+ if (value.length > 65536) throw new Error("Credential exceeds size limit");
148
+ }
149
+ return value;
150
+ }
151
+ export {
152
+ QaRemoteClient,
153
+ QaRemoteError,
154
+ authCommand,
155
+ remoteRunCommand
156
+ };
@@ -0,0 +1 @@
1
+ export declare function loadReplayImage(root: string, runId: string, crop: string): Promise<string | undefined>;
@@ -0,0 +1,35 @@
1
+ import { open } from "node:fs/promises";
2
+ import { createHash } from "node:crypto";
3
+ import { fileURLToPath } from "node:url";
4
+ import { DirArtifactStore } from "./adapters/dir-artifact-store.js";
5
+ import { FileRunStore } from "./adapters/file-run-store.js";
6
+ import { noFollowRead } from "./adapters/local-files.js";
7
+ import { artifactScanSchema } from "./artifact-scan.js";
8
+ async function loadReplayImage(root, runId, crop) {
9
+ if (!/^shots\/[a-f0-9-]{36}-target\.png$/.test(crop)) return void 0;
10
+ try {
11
+ const path = `stage/${crop}`;
12
+ const url = await new DirArtifactStore(root).signedUrl(runId, path, 1e3);
13
+ const scan = artifactScanSchema.parse(
14
+ (await new FileRunStore(root).getRun(runId))?.run.metadata?.artifactScan
15
+ );
16
+ const file = await open(fileURLToPath(url), noFollowRead);
17
+ try {
18
+ const stat = await file.stat();
19
+ if (!stat.isFile() || stat.size > 196608) return void 0;
20
+ const data = Buffer.alloc(196609);
21
+ const { bytesRead } = await file.read(data, 0, data.length, 0);
22
+ if (bytesRead !== stat.size) return void 0;
23
+ const buffer = data.subarray(0, bytesRead);
24
+ if (createHash("sha256").update(buffer).digest("hex") !== scan.files[path]) return void 0;
25
+ return buffer.toString("base64");
26
+ } finally {
27
+ await file.close();
28
+ }
29
+ } catch {
30
+ return void 0;
31
+ }
32
+ }
33
+ export {
34
+ loadReplayImage
35
+ };
@@ -0,0 +1 @@
1
+ export { assetMime, embeddedAssets } from "@open-qa/qa-inspect/server";
@@ -0,0 +1,5 @@
1
+ import { assetMime, embeddedAssets } from "@open-qa/qa-inspect/server";
2
+ export {
3
+ assetMime,
4
+ embeddedAssets
5
+ };