@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,77 @@
1
+ import { createHash } from "node:crypto";
2
+ import { PgCredentialStore } from "../db/credentials.js";
3
+ import { personalSecretName } from "../credential-references.js";
4
+ import { backend, InvalidRequest } from "./errors.js";
5
+ class PgCredentialService {
6
+ constructor(db, project, secretProject, secrets) {
7
+ this.db = db;
8
+ this.project = project;
9
+ this.secretProject = secretProject;
10
+ this.secrets = secrets;
11
+ this.links = new PgCredentialStore(db, project, secretProject);
12
+ }
13
+ db;
14
+ project;
15
+ secretProject;
16
+ secrets;
17
+ links;
18
+ async list(userId) {
19
+ return (await this.links.list(userId)).map(
20
+ (link) => ({
21
+ id: link.runtime,
22
+ expiresAt: link.expiresAt,
23
+ state: link.expiresAt !== null && link.expiresAt <= Date.now() ? "expired" : "connected"
24
+ })
25
+ );
26
+ }
27
+ async connect(userId, input) {
28
+ if (input.expiresAt !== null && input.expiresAt <= Date.now())
29
+ throw new InvalidRequest("Credential has expired");
30
+ const payload = JSON.stringify({ kind: input.kind, token: input.token });
31
+ await this.lock(userId, input.runtime, async (links) => {
32
+ const name = personalSecretName(this.secretProject, userId, input.runtime);
33
+ const secretRef = await backend(
34
+ "secret-manager",
35
+ () => this.secrets.addVersion(name, payload)
36
+ );
37
+ await links.put({ userId, runtime: input.runtime, secretRef, expiresAt: input.expiresAt });
38
+ });
39
+ }
40
+ async revoke(userId, runtime) {
41
+ await this.lock(userId, runtime, async (links) => {
42
+ await links.remove(userId, runtime);
43
+ await backend(
44
+ "secret-manager",
45
+ () => this.secrets.revoke(personalSecretName(this.secretProject, userId, runtime))
46
+ );
47
+ });
48
+ }
49
+ async lock(userId, runtime, operation) {
50
+ const client = await this.db.pool.connect();
51
+ const key = JSON.stringify([this.project, userId, runtime]);
52
+ let broken = false;
53
+ const onError = () => {
54
+ broken = true;
55
+ };
56
+ client.on("error", onError);
57
+ try {
58
+ await client.query("SELECT pg_advisory_lock(hashtextextended($1,0))", [key]);
59
+ await operation(new PgCredentialStore({ pool: client }, this.project, this.secretProject));
60
+ } finally {
61
+ try {
62
+ if (!broken) await client.query("SELECT pg_advisory_unlock(hashtextextended($1,0))", [key]);
63
+ } catch {
64
+ broken = true;
65
+ }
66
+ client.release(broken);
67
+ client.off("error", onError);
68
+ }
69
+ }
70
+ }
71
+ function projectUserId(identity, project) {
72
+ return createHash("sha256").update(JSON.stringify([identity, project])).digest("hex");
73
+ }
74
+ export {
75
+ PgCredentialService,
76
+ projectUserId
77
+ };
@@ -0,0 +1,9 @@
1
+ export type ErrorCategory = "identity-rejected" | "identity-verifier" | "identity-keys" | "invalid-request" | "conflict" | "repository" | "secret-manager" | "object-store";
2
+ export declare class ServerFailure extends Error {
3
+ readonly category: ErrorCategory;
4
+ constructor(category: ErrorCategory);
5
+ }
6
+ export declare class InvalidRequest extends Error {
7
+ }
8
+ export declare function backend<T>(category: ErrorCategory, operation: () => Promise<T>): Promise<T>;
9
+ export declare function logFailure(category: ErrorCategory, route: string, projectId?: string): void;
@@ -0,0 +1,26 @@
1
+ class ServerFailure extends Error {
2
+ constructor(category) {
3
+ super(category.startsWith("identity-") ? "Unauthorized" : "QA service unavailable");
4
+ this.category = category;
5
+ }
6
+ category;
7
+ }
8
+ class InvalidRequest extends Error {
9
+ }
10
+ async function backend(category, operation) {
11
+ try {
12
+ return await operation();
13
+ } catch (error) {
14
+ if (error instanceof ServerFailure) throw error;
15
+ throw new ServerFailure(category);
16
+ }
17
+ }
18
+ function logFailure(category, route, projectId) {
19
+ console.error("QA server request failed", { category, route, projectId });
20
+ }
21
+ export {
22
+ InvalidRequest,
23
+ ServerFailure,
24
+ backend,
25
+ logFailure
26
+ };
@@ -0,0 +1,35 @@
1
+ import type { ScreenAccess } from "../screen-url.js";
2
+ import { QaDatabase } from "../db/database.js";
3
+ import type { EventObjects } from "../db/event-chunks.js";
4
+ import { type AuthPolicy } from "./auth.js";
5
+ import { type CredentialSecrets } from "./credentials.js";
6
+ import { type LeaseAlert } from "./maintenance.js";
7
+ export { createQaServer } from "./app.js";
8
+ export { remoteVerifier, jwtVerifier, type AuthPolicy } from "./auth.js";
9
+ export { PgServerRepository, type ServerRepository } from "./repository.js";
10
+ export { PgCredentialService, type CredentialSecrets } from "./credentials.js";
11
+ export { startLeaseMaintenance } from "./maintenance.js";
12
+ export declare function startQaServer(options: {
13
+ db: QaDatabase;
14
+ auth: AuthPolicy;
15
+ objects: EventObjects;
16
+ secrets: CredentialSecrets;
17
+ projects: Readonly<Record<string, {
18
+ secretProject: string;
19
+ }>>;
20
+ alert: (event: LeaseAlert) => Promise<void>;
21
+ screenAccess?: (project: string, runId: string) => Promise<ScreenAccess | undefined>;
22
+ hostname?: string;
23
+ port?: number;
24
+ }): {
25
+ app: import("hono").Hono<{
26
+ Variables: {
27
+ principal: import("./auth.js").Principal;
28
+ project: string;
29
+ repository: import("./repository.js").ServerRepository;
30
+ userId: string;
31
+ };
32
+ }, import("hono/types").BlankSchema, "/">;
33
+ server: import("@hono/node-server").ServerType;
34
+ close(): Promise<void>;
35
+ };
@@ -0,0 +1,96 @@
1
+ import { serve } from "@hono/node-server";
2
+ import { SecretRedactor } from "@open-qa/qa-engine";
3
+ import { PgClassificationStore } from "../adapters/pg-classification-store.js";
4
+ import { PgReplayCache } from "../adapters/pg-replay-cache.js";
5
+ import { DbLeasePool } from "../adapters/db-lease-pool.js";
6
+ import { createQaServer } from "./app.js";
7
+ import { validateProjectGrants } from "./auth.js";
8
+ import { backend } from "./errors.js";
9
+ import { PgServerRepository } from "./repository.js";
10
+ import { PgCredentialService } from "./credentials.js";
11
+ import { startLeaseMaintenance } from "./maintenance.js";
12
+ import { createQaServer as createQaServer2 } from "./app.js";
13
+ import { remoteVerifier, jwtVerifier } from "./auth.js";
14
+ import { PgServerRepository as PgServerRepository2 } from "./repository.js";
15
+ import { PgCredentialService as PgCredentialService2 } from "./credentials.js";
16
+ import { startLeaseMaintenance as startLeaseMaintenance2 } from "./maintenance.js";
17
+ function startQaServer(options) {
18
+ validateProjectGrants(options.auth, options.projects);
19
+ const objects = {
20
+ putImmutable: (key, data) => backend("object-store", () => options.objects.putImmutable(key, data)),
21
+ read: (key) => backend("object-store", () => options.objects.read(key)),
22
+ remove: (key) => backend("object-store", () => options.objects.remove(key))
23
+ };
24
+ const redactor = new SecretRedactor();
25
+ const repositories = new Map(
26
+ Object.entries(options.projects).map(([project]) => [
27
+ project,
28
+ new PgServerRepository(options.db, project, objects, redactor)
29
+ ])
30
+ );
31
+ const caches = new Map(
32
+ Object.keys(options.projects).map((project) => [
33
+ project,
34
+ new PgReplayCache(options.db, project, redactor)
35
+ ])
36
+ );
37
+ const classifications = new Map(
38
+ Object.keys(options.projects).map((project) => [
39
+ project,
40
+ new PgClassificationStore(options.db, project)
41
+ ])
42
+ );
43
+ const credentials = new Map(
44
+ Object.entries(options.projects).map(([project, config]) => [
45
+ project,
46
+ new PgCredentialService(options.db, project, config.secretProject, options.secrets)
47
+ ])
48
+ );
49
+ const required = (items, project) => {
50
+ const item = items.get(project);
51
+ if (!item) throw new Error("Unknown QA project");
52
+ return item;
53
+ };
54
+ const app = createQaServer({
55
+ auth: options.auth,
56
+ screenAccess: options.screenAccess,
57
+ redactor,
58
+ repository: (project) => required(repositories, project),
59
+ credentials: (project) => required(credentials, project),
60
+ classifications: (project) => required(classifications, project),
61
+ replayCache: (project) => required(caches, project)
62
+ });
63
+ const maintenance = Object.keys(options.projects).map(
64
+ (project) => startLeaseMaintenance(
65
+ project,
66
+ new DbLeasePool(options.db, project, "qa-server-maintenance", async () => {
67
+ throw new Error("Resource resolution is unavailable in qa-server");
68
+ }),
69
+ options.alert
70
+ )
71
+ );
72
+ const server = serve({
73
+ fetch: app.fetch,
74
+ hostname: options.hostname ?? "127.0.0.1",
75
+ port: options.port ?? 4100
76
+ });
77
+ return {
78
+ app,
79
+ server,
80
+ async close() {
81
+ await new Promise(
82
+ (resolve, reject) => server.close((error) => error ? reject(error) : resolve())
83
+ );
84
+ await Promise.all(maintenance.map((item) => item.stop()));
85
+ }
86
+ };
87
+ }
88
+ export {
89
+ PgCredentialService2 as PgCredentialService,
90
+ PgServerRepository2 as PgServerRepository,
91
+ createQaServer2 as createQaServer,
92
+ jwtVerifier,
93
+ remoteVerifier,
94
+ startLeaseMaintenance2 as startLeaseMaintenance,
95
+ startQaServer
96
+ };
@@ -0,0 +1,13 @@
1
+ export interface LeaseMaintenance {
2
+ quarantineExpired(): Promise<number>;
3
+ quarantinedCount(): Promise<number>;
4
+ }
5
+ export type LeaseAlert = {
6
+ projectId: string;
7
+ kind: "leases-quarantined" | "lease-maintenance-failed";
8
+ count?: number;
9
+ };
10
+ export declare function startLeaseMaintenance(projectId: string, pool: LeaseMaintenance, alert: (event: LeaseAlert) => Promise<void>, intervalMs?: number, alertIntervalMs?: number): {
11
+ tick: () => Promise<void>;
12
+ stop(): Promise<void>;
13
+ };
@@ -0,0 +1,54 @@
1
+ function startLeaseMaintenance(projectId, pool, alert, intervalMs = 3e4, alertIntervalMs = 36e5) {
2
+ if (!Number.isInteger(intervalMs) || intervalMs < 10 || intervalMs > 2147483647)
3
+ throw new Error("Invalid lease maintenance interval");
4
+ if (!Number.isInteger(alertIntervalMs) || alertIntervalMs < 10 || alertIntervalMs > 2147483647)
5
+ throw new Error("Invalid lease alert interval");
6
+ let stopped = false;
7
+ let running;
8
+ let lastAlert;
9
+ const tick = () => {
10
+ if (stopped) return Promise.resolve();
11
+ if (running) return running;
12
+ running = (async () => {
13
+ try {
14
+ await pool.quarantineExpired();
15
+ const count = await pool.quarantinedCount();
16
+ if (!count) lastAlert = void 0;
17
+ else if (!lastAlert || count !== lastAlert.count || Date.now() - lastAlert.at >= alertIntervalMs) {
18
+ console.warn("QA leases quarantined; confirmed stage cleanup is required", {
19
+ projectId,
20
+ count
21
+ });
22
+ await alert({ projectId, kind: "leases-quarantined", count });
23
+ lastAlert = { count, at: Date.now() };
24
+ }
25
+ } catch {
26
+ console.error("QA lease maintenance failed", { projectId });
27
+ try {
28
+ await alert({ projectId, kind: "lease-maintenance-failed" });
29
+ } catch {
30
+ console.error("QA lease maintenance alert failed", { projectId });
31
+ }
32
+ }
33
+ })().finally(() => {
34
+ running = void 0;
35
+ });
36
+ return running;
37
+ };
38
+ const timer = setInterval(() => {
39
+ void tick();
40
+ }, intervalMs);
41
+ timer.unref();
42
+ void tick();
43
+ return {
44
+ tick,
45
+ async stop() {
46
+ stopped = true;
47
+ clearInterval(timer);
48
+ await running;
49
+ }
50
+ };
51
+ }
52
+ export {
53
+ startLeaseMaintenance
54
+ };