@open-qa/qa 0.1.0-alpha.4 → 0.1.0-alpha.5

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 (109) hide show
  1. package/THIRD_PARTY.md +5 -0
  2. package/drizzle/0017_provider_notifications.sql +13 -0
  3. package/drizzle/0018_classification_registry.sql +39 -0
  4. package/drizzle/0019_trial_audit.sql +44 -0
  5. package/drizzle/meta/0017_snapshot.json +2092 -0
  6. package/drizzle/meta/0018_snapshot.json +2102 -0
  7. package/drizzle/meta/0019_snapshot.json +2451 -0
  8. package/drizzle/meta/_journal.json +21 -0
  9. package/package.json +12 -7
  10. package/src/access-binding.d.ts +8 -0
  11. package/src/access-binding.js +26 -0
  12. package/src/adapters/classification-write.d.ts +87 -0
  13. package/src/adapters/classification-write.js +64 -0
  14. package/src/adapters/e2b-executor.js +4 -0
  15. package/src/adapters/file-run-store.d.ts +186 -41
  16. package/src/adapters/file-run-store.js +87 -19
  17. package/src/adapters/file-trial-audit.d.ts +280 -0
  18. package/src/adapters/file-trial-audit.js +105 -0
  19. package/src/adapters/pg-classification-store.d.ts +98 -20
  20. package/src/adapters/pg-classification-store.js +84 -44
  21. package/src/adapters/pg-run-store.d.ts +1 -0
  22. package/src/adapters/pg-run-store.js +70 -9
  23. package/src/adapters/run-metadata.d.ts +2 -0
  24. package/src/adapters/run-metadata.js +10 -0
  25. package/src/adapters/secret-manager-credential-resolver.d.ts +1 -1
  26. package/src/attempt-audit.d.ts +363 -0
  27. package/src/attempt-audit.js +253 -0
  28. package/src/audit-command.d.ts +13 -0
  29. package/src/audit-command.js +77 -0
  30. package/src/classification-command.d.ts +14 -9
  31. package/src/classification-command.js +119 -26
  32. package/src/classification-model.d.ts +10 -3
  33. package/src/classification-model.js +40 -24
  34. package/src/classification-notifier.d.ts +4 -3
  35. package/src/classification-notifier.js +26 -6
  36. package/src/classify-runs.d.ts +2 -1
  37. package/src/classify-runs.js +39 -3
  38. package/src/cli-program.js +53 -7
  39. package/src/commands.d.ts +1 -0
  40. package/src/commands.js +2 -0
  41. package/src/db/database.d.ts +7 -0
  42. package/src/db/database.js +15 -0
  43. package/src/db/event-chunks.d.ts +38 -29
  44. package/src/db/flow-status.d.ts +7 -3
  45. package/src/db/flow-status.js +240 -16
  46. package/src/db/queries.d.ts +24 -1
  47. package/src/db/queries.js +22 -0
  48. package/src/db/schema.d.ts +333 -4
  49. package/src/db/schema.js +83 -11
  50. package/src/db/trial-audit.d.ts +526 -0
  51. package/src/db/trial-audit.js +84 -0
  52. package/src/draft-api.d.ts +25 -421
  53. package/src/draft-api.js +10 -3
  54. package/src/engine.d.ts +47 -4
  55. package/src/engine.js +97 -8
  56. package/src/execution-snapshot.d.ts +1 -1
  57. package/src/execution-snapshot.js +22 -17
  58. package/src/export-command.d.ts +6 -0
  59. package/src/export-command.js +106 -0
  60. package/src/flow-check.js +1 -1
  61. package/src/flow-loader.d.ts +72 -6
  62. package/src/flow-loader.js +76 -12
  63. package/src/http-stage-client.d.ts +98 -1
  64. package/src/independent-model.js +53 -6
  65. package/src/judge-review-store.d.ts +13 -0
  66. package/src/judge-review-store.js +58 -0
  67. package/src/local-host-executor.d.ts +3 -1
  68. package/src/local-host-executor.js +2 -0
  69. package/src/quarantine-command.d.ts +5 -1
  70. package/src/quarantine-command.js +56 -13
  71. package/src/remote-command.js +2 -0
  72. package/src/report/browser.js +37 -37
  73. package/src/report/ctrf.d.ts +983 -0
  74. package/src/report/ctrf.js +306 -0
  75. package/src/report/generate.d.ts +1 -1
  76. package/src/report/generate.js +29 -6
  77. package/src/report/load.d.ts +738 -42
  78. package/src/report/load.js +78 -6
  79. package/src/result-notifier.js +1 -1
  80. package/src/result-trace.d.ts +62 -0
  81. package/src/result-trace.js +81 -0
  82. package/src/run-command.d.ts +2 -0
  83. package/src/run-command.js +285 -62
  84. package/src/run-setup-error.d.ts +1 -1
  85. package/src/run-setup-error.js +2 -2
  86. package/src/server/app.js +5 -4
  87. package/src/server/index.d.ts +2 -0
  88. package/src/server/index.js +11 -2
  89. package/src/server/repository.d.ts +166 -29
  90. package/src/server/repository.js +14 -2
  91. package/src/studio-review.js +1 -1
  92. package/src/system-config.d.ts +2 -2
  93. package/src/system-config.js +5 -4
  94. package/src/trial-audit.d.ts +630 -0
  95. package/src/trial-audit.js +272 -0
  96. package/src/visual-baseline.d.ts +4 -0
  97. package/src/visual-baseline.js +42 -0
  98. package/src/webhook-notification.d.ts +15 -0
  99. package/src/webhook-notification.js +61 -0
  100. package/src/worker/execution.d.ts +2 -1
  101. package/src/worker/execution.js +122 -3
  102. package/src/worker/index.d.ts +4 -0
  103. package/src/worker/index.js +10 -1
  104. package/src/worker/quarantine-notifications.js +2 -2
  105. package/src/worker/queue.d.ts +6 -2
  106. package/src/worker/queue.js +80 -19
  107. package/src/worker/result-notifications.d.ts +10 -2
  108. package/src/worker/result-notifications.js +120 -23
  109. package/src/worker/store.js +6 -3
@@ -0,0 +1,526 @@
1
+ import type { Pool, PoolClient } from "pg";
2
+ import { type QaDatabase } from "./database.js";
3
+ import { type TrialAuditStore, type TrialGroup, type TrialReceipt } from "../trial-audit.js";
4
+ export declare class PgTrialAudit implements TrialAuditStore {
5
+ private readonly db;
6
+ private readonly projectId;
7
+ constructor(db: QaDatabase, projectId: string);
8
+ create(input: TrialGroup): Promise<void>;
9
+ append(groupId: string, input: TrialReceipt): Promise<void>;
10
+ list(flow?: string): Promise<{
11
+ schemaVersion: 1;
12
+ groupId: string;
13
+ projectId: string;
14
+ batchId: string;
15
+ caseId: string;
16
+ flow: string;
17
+ caseRevision: string;
18
+ target: string;
19
+ targetRevision: string | null;
20
+ generation: number | null;
21
+ planned: number;
22
+ startedAt: number;
23
+ environment: Record<string, import("zod").JSONType>;
24
+ independent?: boolean | undefined;
25
+ quarantinePolicy?: {
26
+ expiresAfterHours: number;
27
+ evidenceMaxAgeHours: number;
28
+ minimumPassedTrials: number;
29
+ flakyStreak: number;
30
+ } | undefined;
31
+ }[]>;
32
+ get(groupId: string): Promise<{
33
+ group: {
34
+ schemaVersion: 1;
35
+ groupId: string;
36
+ projectId: string;
37
+ batchId: string;
38
+ caseId: string;
39
+ flow: string;
40
+ caseRevision: string;
41
+ target: string;
42
+ targetRevision: string | null;
43
+ generation: number | null;
44
+ planned: number;
45
+ startedAt: number;
46
+ environment: Record<string, import("zod").JSONType>;
47
+ independent?: boolean | undefined;
48
+ quarantinePolicy?: {
49
+ expiresAfterHours: number;
50
+ evidenceMaxAgeHours: number;
51
+ minimumPassedTrials: number;
52
+ flakyStreak: number;
53
+ } | undefined;
54
+ };
55
+ receipts: ({
56
+ kind: "attempt-started";
57
+ trace: {
58
+ schemaVersion: 1;
59
+ projectId: string;
60
+ caseId: string;
61
+ caseRevision: string | null;
62
+ executionId: string;
63
+ attemptId: string;
64
+ attempt: number;
65
+ started: boolean;
66
+ retryOf: string | null;
67
+ batchId: string | null;
68
+ trial: {
69
+ groupId: string;
70
+ index: number;
71
+ count: number;
72
+ } | null;
73
+ target: string | null;
74
+ targetRevision: string | null;
75
+ commit: string | null;
76
+ parameters: {
77
+ runtime: "claude-code" | "codex" | "scripted" | "pi" | null;
78
+ modelAccess: "env" | "login" | "personal" | "team" | null;
79
+ placement: "local-host" | "e2b" | null;
80
+ agentAt: "host" | "stage" | null;
81
+ headed: boolean | null;
82
+ level: "semantic" | "pixel" | "hybrid" | null;
83
+ device: string | null;
84
+ persona: string | null;
85
+ replay: "off" | "enabled" | null;
86
+ };
87
+ parameterFingerprint: string;
88
+ environmentFingerprint: string;
89
+ acceptance?: {
90
+ source: string;
91
+ revision: string;
92
+ criteria: {
93
+ id: string;
94
+ text: string;
95
+ source?: string | undefined;
96
+ }[];
97
+ flows: {
98
+ flow: string;
99
+ covers: string[];
100
+ }[];
101
+ } | undefined;
102
+ covers?: string[] | undefined;
103
+ traceability?: {
104
+ caseId?: string | undefined;
105
+ risk?: string | undefined;
106
+ owner?: string | undefined;
107
+ } | undefined;
108
+ context?: {
109
+ device?: string | undefined;
110
+ ci?: {
111
+ pipeline?: string | undefined;
112
+ task?: string | undefined;
113
+ runId?: string | undefined;
114
+ runUrl?: string | undefined;
115
+ } | undefined;
116
+ build?: {
117
+ id?: string | undefined;
118
+ url?: string | undefined;
119
+ } | undefined;
120
+ correlation?: {
121
+ traceId?: string | undefined;
122
+ spanId?: string | undefined;
123
+ requestId?: string | undefined;
124
+ } | undefined;
125
+ productPr?: {
126
+ url: string;
127
+ base?: string | undefined;
128
+ head?: string | undefined;
129
+ revision?: string | undefined;
130
+ } | undefined;
131
+ qa?: {
132
+ issue?: string | undefined;
133
+ pr?: string | undefined;
134
+ } | undefined;
135
+ defects?: {
136
+ url: string;
137
+ acceptance: string[];
138
+ }[] | undefined;
139
+ } | undefined;
140
+ outcome?: "skipped" | "blocked" | "not-run" | "expected-failure" | undefined;
141
+ engineResult?: {
142
+ verdict: "error" | "passed" | "failed" | "inconclusive" | "canceled";
143
+ reason: string | null;
144
+ finishedAt: number;
145
+ } | undefined;
146
+ evidence?: {
147
+ path: string;
148
+ actor: string | null;
149
+ view: string | null;
150
+ step: string | null;
151
+ eventSeq: number;
152
+ }[] | undefined;
153
+ };
154
+ } | {
155
+ kind: "attempt-finished";
156
+ attemptId: string;
157
+ at: number;
158
+ verdict: "error" | "passed" | "failed" | "inconclusive" | "canceled";
159
+ outcome?: "skipped" | "blocked" | "not-run" | "expected-failure" | undefined;
160
+ usage?: {
161
+ inputTokens: number;
162
+ outputTokens: number;
163
+ actions: number;
164
+ minutes: number;
165
+ cacheReadTokens?: number | undefined;
166
+ cacheWriteTokens?: number | undefined;
167
+ costUsd?: number | undefined;
168
+ costUnknown?: true | undefined;
169
+ e2bMinutes?: number | undefined;
170
+ } | undefined;
171
+ audit?: {
172
+ schemaVersion: 1;
173
+ usage: {
174
+ inputTokens: number;
175
+ outputTokens: number;
176
+ role: "heal" | "main" | "judge" | "reviewer" | "classifier";
177
+ billing: "subscription" | "metered" | null;
178
+ cacheReadTokens?: number | undefined;
179
+ cacheWriteTokens?: number | undefined;
180
+ costUsd?: number | undefined;
181
+ costUnknown?: true | undefined;
182
+ model?: {
183
+ provider: string | null;
184
+ model: string | null;
185
+ seed: number | "unknown" | "unsupported";
186
+ billingVersion: string | null;
187
+ deployment?: string | undefined;
188
+ promptVersion?: string | undefined;
189
+ promptHash?: string | undefined;
190
+ parameters?: Record<string, import("zod").JSONType> | undefined;
191
+ } | undefined;
192
+ billingMode?: "subscription" | "metered" | undefined;
193
+ costUnknownReason?: "subscription" | "unpriced" | "usage-unavailable" | "billing-unrecorded" | undefined;
194
+ actor?: string | undefined;
195
+ step?: string | undefined;
196
+ }[];
197
+ judges: {
198
+ eventSeq: number;
199
+ actor: string | null;
200
+ step: string | null;
201
+ verdict: "passed" | "failed" | "inconclusive";
202
+ confidence: number | null;
203
+ reason: string;
204
+ rubricVersion: string | null;
205
+ rubricHash: string | null;
206
+ outcome: {
207
+ eventSeq: number;
208
+ type: string;
209
+ passed: boolean;
210
+ }[];
211
+ reviewStatus: "recorded" | "pending" | "deterministic";
212
+ }[];
213
+ reviewer: {
214
+ eventSeq: number;
215
+ status: "completed" | "incomplete" | "skipped";
216
+ reason: string;
217
+ failureType: string | null;
218
+ rubricVersion: string | null;
219
+ rubricHash: string | null;
220
+ }[];
221
+ environment?: {
222
+ image: {
223
+ template: string | null;
224
+ digest: string | null;
225
+ };
226
+ runtime: string | null;
227
+ views: {
228
+ view: string;
229
+ browser: string | null;
230
+ viewport: {
231
+ width: number;
232
+ height: number;
233
+ } | null;
234
+ device: string | null;
235
+ }[];
236
+ } | undefined;
237
+ } | undefined;
238
+ observation?: Record<string, import("zod").JSONType> | undefined;
239
+ evidence?: {
240
+ path: string;
241
+ actor: string | null;
242
+ view: string | null;
243
+ step: string | null;
244
+ eventSeq: number;
245
+ }[] | undefined;
246
+ } | {
247
+ kind: "classifier-usage";
248
+ attemptId: string;
249
+ usage: {
250
+ inputTokens: number;
251
+ outputTokens: number;
252
+ cacheReadTokens?: number | undefined;
253
+ cacheWriteTokens?: number | undefined;
254
+ costUsd?: number | undefined;
255
+ costUnknown?: true | undefined;
256
+ model?: {
257
+ provider: string | null;
258
+ model: string | null;
259
+ seed: number | "unknown" | "unsupported";
260
+ billingVersion: string | null;
261
+ deployment?: string | undefined;
262
+ promptVersion?: string | undefined;
263
+ promptHash?: string | undefined;
264
+ parameters?: Record<string, import("zod").JSONType> | undefined;
265
+ } | undefined;
266
+ billingMode?: "subscription" | "metered" | undefined;
267
+ costUnknownReason?: "subscription" | "unpriced" | "usage-unavailable" | "billing-unrecorded" | undefined;
268
+ }[];
269
+ } | {
270
+ kind: "trial-finished";
271
+ index: number;
272
+ attemptId: string;
273
+ quarantined: boolean;
274
+ } | {
275
+ kind: "group-finished";
276
+ at: number;
277
+ })[];
278
+ }>;
279
+ }
280
+ export declare function readTrialHistory(client: Pool | PoolClient, projectId: string, groupId: string): Promise<{
281
+ group: {
282
+ schemaVersion: 1;
283
+ groupId: string;
284
+ projectId: string;
285
+ batchId: string;
286
+ caseId: string;
287
+ flow: string;
288
+ caseRevision: string;
289
+ target: string;
290
+ targetRevision: string | null;
291
+ generation: number | null;
292
+ planned: number;
293
+ startedAt: number;
294
+ environment: Record<string, import("zod").JSONType>;
295
+ independent?: boolean | undefined;
296
+ quarantinePolicy?: {
297
+ expiresAfterHours: number;
298
+ evidenceMaxAgeHours: number;
299
+ minimumPassedTrials: number;
300
+ flakyStreak: number;
301
+ } | undefined;
302
+ };
303
+ receipts: ({
304
+ kind: "attempt-started";
305
+ trace: {
306
+ schemaVersion: 1;
307
+ projectId: string;
308
+ caseId: string;
309
+ caseRevision: string | null;
310
+ executionId: string;
311
+ attemptId: string;
312
+ attempt: number;
313
+ started: boolean;
314
+ retryOf: string | null;
315
+ batchId: string | null;
316
+ trial: {
317
+ groupId: string;
318
+ index: number;
319
+ count: number;
320
+ } | null;
321
+ target: string | null;
322
+ targetRevision: string | null;
323
+ commit: string | null;
324
+ parameters: {
325
+ runtime: "claude-code" | "codex" | "scripted" | "pi" | null;
326
+ modelAccess: "env" | "login" | "personal" | "team" | null;
327
+ placement: "local-host" | "e2b" | null;
328
+ agentAt: "host" | "stage" | null;
329
+ headed: boolean | null;
330
+ level: "semantic" | "pixel" | "hybrid" | null;
331
+ device: string | null;
332
+ persona: string | null;
333
+ replay: "off" | "enabled" | null;
334
+ };
335
+ parameterFingerprint: string;
336
+ environmentFingerprint: string;
337
+ acceptance?: {
338
+ source: string;
339
+ revision: string;
340
+ criteria: {
341
+ id: string;
342
+ text: string;
343
+ source?: string | undefined;
344
+ }[];
345
+ flows: {
346
+ flow: string;
347
+ covers: string[];
348
+ }[];
349
+ } | undefined;
350
+ covers?: string[] | undefined;
351
+ traceability?: {
352
+ caseId?: string | undefined;
353
+ risk?: string | undefined;
354
+ owner?: string | undefined;
355
+ } | undefined;
356
+ context?: {
357
+ device?: string | undefined;
358
+ ci?: {
359
+ pipeline?: string | undefined;
360
+ task?: string | undefined;
361
+ runId?: string | undefined;
362
+ runUrl?: string | undefined;
363
+ } | undefined;
364
+ build?: {
365
+ id?: string | undefined;
366
+ url?: string | undefined;
367
+ } | undefined;
368
+ correlation?: {
369
+ traceId?: string | undefined;
370
+ spanId?: string | undefined;
371
+ requestId?: string | undefined;
372
+ } | undefined;
373
+ productPr?: {
374
+ url: string;
375
+ base?: string | undefined;
376
+ head?: string | undefined;
377
+ revision?: string | undefined;
378
+ } | undefined;
379
+ qa?: {
380
+ issue?: string | undefined;
381
+ pr?: string | undefined;
382
+ } | undefined;
383
+ defects?: {
384
+ url: string;
385
+ acceptance: string[];
386
+ }[] | undefined;
387
+ } | undefined;
388
+ outcome?: "skipped" | "blocked" | "not-run" | "expected-failure" | undefined;
389
+ engineResult?: {
390
+ verdict: "error" | "passed" | "failed" | "inconclusive" | "canceled";
391
+ reason: string | null;
392
+ finishedAt: number;
393
+ } | undefined;
394
+ evidence?: {
395
+ path: string;
396
+ actor: string | null;
397
+ view: string | null;
398
+ step: string | null;
399
+ eventSeq: number;
400
+ }[] | undefined;
401
+ };
402
+ } | {
403
+ kind: "attempt-finished";
404
+ attemptId: string;
405
+ at: number;
406
+ verdict: "error" | "passed" | "failed" | "inconclusive" | "canceled";
407
+ outcome?: "skipped" | "blocked" | "not-run" | "expected-failure" | undefined;
408
+ usage?: {
409
+ inputTokens: number;
410
+ outputTokens: number;
411
+ actions: number;
412
+ minutes: number;
413
+ cacheReadTokens?: number | undefined;
414
+ cacheWriteTokens?: number | undefined;
415
+ costUsd?: number | undefined;
416
+ costUnknown?: true | undefined;
417
+ e2bMinutes?: number | undefined;
418
+ } | undefined;
419
+ audit?: {
420
+ schemaVersion: 1;
421
+ usage: {
422
+ inputTokens: number;
423
+ outputTokens: number;
424
+ role: "heal" | "main" | "judge" | "reviewer" | "classifier";
425
+ billing: "subscription" | "metered" | null;
426
+ cacheReadTokens?: number | undefined;
427
+ cacheWriteTokens?: number | undefined;
428
+ costUsd?: number | undefined;
429
+ costUnknown?: true | undefined;
430
+ model?: {
431
+ provider: string | null;
432
+ model: string | null;
433
+ seed: number | "unknown" | "unsupported";
434
+ billingVersion: string | null;
435
+ deployment?: string | undefined;
436
+ promptVersion?: string | undefined;
437
+ promptHash?: string | undefined;
438
+ parameters?: Record<string, import("zod").JSONType> | undefined;
439
+ } | undefined;
440
+ billingMode?: "subscription" | "metered" | undefined;
441
+ costUnknownReason?: "subscription" | "unpriced" | "usage-unavailable" | "billing-unrecorded" | undefined;
442
+ actor?: string | undefined;
443
+ step?: string | undefined;
444
+ }[];
445
+ judges: {
446
+ eventSeq: number;
447
+ actor: string | null;
448
+ step: string | null;
449
+ verdict: "passed" | "failed" | "inconclusive";
450
+ confidence: number | null;
451
+ reason: string;
452
+ rubricVersion: string | null;
453
+ rubricHash: string | null;
454
+ outcome: {
455
+ eventSeq: number;
456
+ type: string;
457
+ passed: boolean;
458
+ }[];
459
+ reviewStatus: "recorded" | "pending" | "deterministic";
460
+ }[];
461
+ reviewer: {
462
+ eventSeq: number;
463
+ status: "completed" | "incomplete" | "skipped";
464
+ reason: string;
465
+ failureType: string | null;
466
+ rubricVersion: string | null;
467
+ rubricHash: string | null;
468
+ }[];
469
+ environment?: {
470
+ image: {
471
+ template: string | null;
472
+ digest: string | null;
473
+ };
474
+ runtime: string | null;
475
+ views: {
476
+ view: string;
477
+ browser: string | null;
478
+ viewport: {
479
+ width: number;
480
+ height: number;
481
+ } | null;
482
+ device: string | null;
483
+ }[];
484
+ } | undefined;
485
+ } | undefined;
486
+ observation?: Record<string, import("zod").JSONType> | undefined;
487
+ evidence?: {
488
+ path: string;
489
+ actor: string | null;
490
+ view: string | null;
491
+ step: string | null;
492
+ eventSeq: number;
493
+ }[] | undefined;
494
+ } | {
495
+ kind: "classifier-usage";
496
+ attemptId: string;
497
+ usage: {
498
+ inputTokens: number;
499
+ outputTokens: number;
500
+ cacheReadTokens?: number | undefined;
501
+ cacheWriteTokens?: number | undefined;
502
+ costUsd?: number | undefined;
503
+ costUnknown?: true | undefined;
504
+ model?: {
505
+ provider: string | null;
506
+ model: string | null;
507
+ seed: number | "unknown" | "unsupported";
508
+ billingVersion: string | null;
509
+ deployment?: string | undefined;
510
+ promptVersion?: string | undefined;
511
+ promptHash?: string | undefined;
512
+ parameters?: Record<string, import("zod").JSONType> | undefined;
513
+ } | undefined;
514
+ billingMode?: "subscription" | "metered" | undefined;
515
+ costUnknownReason?: "subscription" | "unpriced" | "usage-unavailable" | "billing-unrecorded" | undefined;
516
+ }[];
517
+ } | {
518
+ kind: "trial-finished";
519
+ index: number;
520
+ attemptId: string;
521
+ quarantined: boolean;
522
+ } | {
523
+ kind: "group-finished";
524
+ at: number;
525
+ })[];
526
+ }>;
@@ -0,0 +1,84 @@
1
+ import { assertPostgresText, publicId } from "./database.js";
2
+ import {
3
+ trialGroupSchema,
4
+ trialReceiptKey,
5
+ trialReceiptSchema
6
+ } from "../trial-audit.js";
7
+ class PgTrialAudit {
8
+ constructor(db, projectId) {
9
+ this.db = db;
10
+ this.projectId = projectId;
11
+ publicId.parse(projectId);
12
+ }
13
+ db;
14
+ projectId;
15
+ async create(input) {
16
+ const group = trialGroupSchema.parse(input);
17
+ if (group.projectId !== this.projectId) throw new Error("Trial project mismatch");
18
+ assertPostgresText(group);
19
+ const result = await this.db.pool.query(
20
+ "INSERT INTO qa.trial_groups(project_id,group_id,flow,started_at,document) VALUES($1,$2,$3,$4,$5) ON CONFLICT DO NOTHING",
21
+ [group.projectId, group.groupId, group.flow, group.startedAt, JSON.stringify(group)]
22
+ );
23
+ if (!result.rowCount) {
24
+ const { rowCount } = await this.db.pool.query(
25
+ "SELECT 1 FROM qa.trial_groups WHERE project_id=$1 AND group_id=$2 AND document=$3::jsonb",
26
+ [group.projectId, group.groupId, JSON.stringify(group)]
27
+ );
28
+ if (!rowCount) throw new Error("Conflicting immutable trial group");
29
+ }
30
+ }
31
+ async append(groupId, input) {
32
+ const receipt = trialReceiptSchema.parse(input);
33
+ assertPostgresText(receipt);
34
+ const { rows } = await this.db.pool.query(
35
+ "SELECT document FROM qa.trial_groups WHERE project_id=$1 AND group_id=$2",
36
+ [this.projectId, groupId]
37
+ );
38
+ if (!rows[0]) throw new Error("Trial group not found");
39
+ const args = [
40
+ this.projectId,
41
+ groupId,
42
+ trialReceiptKey(trialGroupSchema.parse(rows[0].document), receipt),
43
+ JSON.stringify(receipt)
44
+ ];
45
+ const result = await this.db.pool.query(
46
+ "INSERT INTO qa.trial_group_receipts(project_id,group_id,key,document) VALUES($1,$2,$3,$4) ON CONFLICT DO NOTHING",
47
+ args
48
+ );
49
+ if (!result.rowCount) {
50
+ const { rowCount } = await this.db.pool.query(
51
+ "SELECT 1 FROM qa.trial_group_receipts WHERE project_id=$1 AND group_id=$2 AND key=$3 AND document=$4::jsonb",
52
+ args
53
+ );
54
+ if (!rowCount) throw new Error("Conflicting immutable trial receipt");
55
+ }
56
+ }
57
+ async list(flow) {
58
+ const { rows } = await this.db.pool.query(
59
+ "SELECT document FROM qa.trial_groups WHERE project_id=$1 AND ($2::text IS NULL OR flow=$2) ORDER BY started_at DESC,group_id",
60
+ [this.projectId, flow ?? null]
61
+ );
62
+ return rows.map(({ document }) => trialGroupSchema.parse(document));
63
+ }
64
+ get(groupId) {
65
+ return readTrialHistory(this.db.pool, this.projectId, groupId);
66
+ }
67
+ }
68
+ async function readTrialHistory(client, projectId, groupId) {
69
+ const { rows } = await client.query(
70
+ `SELECT document, COALESCE((SELECT jsonb_agg(document ORDER BY key) FROM qa.trial_group_receipts r
71
+ WHERE r.project_id=g.project_id AND r.group_id=g.group_id), '[]'::jsonb) AS receipts
72
+ FROM qa.trial_groups g WHERE project_id=$1 AND group_id=$2`,
73
+ [projectId, groupId]
74
+ );
75
+ const row = rows[0];
76
+ return row && {
77
+ group: trialGroupSchema.parse(row.document),
78
+ receipts: row.receipts.map((receipt) => trialReceiptSchema.parse(receipt))
79
+ };
80
+ }
81
+ export {
82
+ PgTrialAudit,
83
+ readTrialHistory
84
+ };