@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.
- package/THIRD_PARTY.md +5 -0
- package/drizzle/0017_provider_notifications.sql +13 -0
- package/drizzle/0018_classification_registry.sql +39 -0
- package/drizzle/0019_trial_audit.sql +44 -0
- package/drizzle/meta/0017_snapshot.json +2092 -0
- package/drizzle/meta/0018_snapshot.json +2102 -0
- package/drizzle/meta/0019_snapshot.json +2451 -0
- package/drizzle/meta/_journal.json +21 -0
- package/package.json +12 -7
- package/src/access-binding.d.ts +8 -0
- package/src/access-binding.js +26 -0
- package/src/adapters/classification-write.d.ts +87 -0
- package/src/adapters/classification-write.js +64 -0
- package/src/adapters/e2b-executor.js +4 -0
- package/src/adapters/file-run-store.d.ts +186 -41
- package/src/adapters/file-run-store.js +87 -19
- package/src/adapters/file-trial-audit.d.ts +280 -0
- package/src/adapters/file-trial-audit.js +105 -0
- package/src/adapters/pg-classification-store.d.ts +98 -20
- package/src/adapters/pg-classification-store.js +84 -44
- package/src/adapters/pg-run-store.d.ts +1 -0
- package/src/adapters/pg-run-store.js +70 -9
- package/src/adapters/run-metadata.d.ts +2 -0
- package/src/adapters/run-metadata.js +10 -0
- package/src/adapters/secret-manager-credential-resolver.d.ts +1 -1
- package/src/attempt-audit.d.ts +363 -0
- package/src/attempt-audit.js +253 -0
- package/src/audit-command.d.ts +13 -0
- package/src/audit-command.js +77 -0
- package/src/classification-command.d.ts +14 -9
- package/src/classification-command.js +119 -26
- package/src/classification-model.d.ts +10 -3
- package/src/classification-model.js +40 -24
- package/src/classification-notifier.d.ts +4 -3
- package/src/classification-notifier.js +26 -6
- package/src/classify-runs.d.ts +2 -1
- package/src/classify-runs.js +39 -3
- package/src/cli-program.js +53 -7
- package/src/commands.d.ts +1 -0
- package/src/commands.js +2 -0
- package/src/db/database.d.ts +7 -0
- package/src/db/database.js +15 -0
- package/src/db/event-chunks.d.ts +38 -29
- package/src/db/flow-status.d.ts +7 -3
- package/src/db/flow-status.js +240 -16
- package/src/db/queries.d.ts +24 -1
- package/src/db/queries.js +22 -0
- package/src/db/schema.d.ts +333 -4
- package/src/db/schema.js +83 -11
- package/src/db/trial-audit.d.ts +526 -0
- package/src/db/trial-audit.js +84 -0
- package/src/draft-api.d.ts +25 -421
- package/src/draft-api.js +10 -3
- package/src/engine.d.ts +47 -4
- package/src/engine.js +97 -8
- package/src/execution-snapshot.d.ts +1 -1
- package/src/execution-snapshot.js +22 -17
- package/src/export-command.d.ts +6 -0
- package/src/export-command.js +106 -0
- package/src/flow-check.js +1 -1
- package/src/flow-loader.d.ts +72 -6
- package/src/flow-loader.js +76 -12
- package/src/http-stage-client.d.ts +98 -1
- package/src/independent-model.js +53 -6
- package/src/judge-review-store.d.ts +13 -0
- package/src/judge-review-store.js +58 -0
- package/src/local-host-executor.d.ts +3 -1
- package/src/local-host-executor.js +2 -0
- package/src/quarantine-command.d.ts +5 -1
- package/src/quarantine-command.js +56 -13
- package/src/remote-command.js +2 -0
- package/src/report/browser.js +37 -37
- package/src/report/ctrf.d.ts +983 -0
- package/src/report/ctrf.js +306 -0
- package/src/report/generate.d.ts +1 -1
- package/src/report/generate.js +29 -6
- package/src/report/load.d.ts +738 -42
- package/src/report/load.js +78 -6
- package/src/result-notifier.js +1 -1
- package/src/result-trace.d.ts +62 -0
- package/src/result-trace.js +81 -0
- package/src/run-command.d.ts +2 -0
- package/src/run-command.js +285 -62
- package/src/run-setup-error.d.ts +1 -1
- package/src/run-setup-error.js +2 -2
- package/src/server/app.js +5 -4
- package/src/server/index.d.ts +2 -0
- package/src/server/index.js +11 -2
- package/src/server/repository.d.ts +166 -29
- package/src/server/repository.js +14 -2
- package/src/studio-review.js +1 -1
- package/src/system-config.d.ts +2 -2
- package/src/system-config.js +5 -4
- package/src/trial-audit.d.ts +630 -0
- package/src/trial-audit.js +272 -0
- package/src/visual-baseline.d.ts +4 -0
- package/src/visual-baseline.js +42 -0
- package/src/webhook-notification.d.ts +15 -0
- package/src/webhook-notification.js +61 -0
- package/src/worker/execution.d.ts +2 -1
- package/src/worker/execution.js +122 -3
- package/src/worker/index.d.ts +4 -0
- package/src/worker/index.js +10 -1
- package/src/worker/quarantine-notifications.js +2 -2
- package/src/worker/queue.d.ts +6 -2
- package/src/worker/queue.js +80 -19
- package/src/worker/result-notifications.d.ts +10 -2
- package/src/worker/result-notifications.js +120 -23
- package/src/worker/store.js +6 -3
package/src/worker/queue.js
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { setTimeout as delay } from "node:timers/promises";
|
|
3
3
|
import { z } from "zod";
|
|
4
|
-
import { classifyFailure } from "@open-qa/qa-engine";
|
|
5
4
|
import {
|
|
5
|
+
classifyFailure,
|
|
6
|
+
loadClassificationRegistry,
|
|
7
|
+
quarantinePolicySchema
|
|
8
|
+
} from "@open-qa/qa-engine";
|
|
9
|
+
import {
|
|
10
|
+
readResultTrace,
|
|
6
11
|
verdictSchema,
|
|
12
|
+
readClassification,
|
|
7
13
|
batchDtoSchema,
|
|
8
14
|
submitBatchSchema
|
|
9
15
|
} from "@open-qa/qa-engine/protocol";
|
|
10
16
|
import { publicId } from "../db/database.js";
|
|
11
17
|
import { PgFlowStatus } from "../db/flow-status.js";
|
|
12
18
|
import { failureCategorySchema } from "@open-qa/qa-engine/protocol";
|
|
19
|
+
import { savePgClassificationInTransaction } from "../adapters/pg-classification-store.js";
|
|
20
|
+
import { buildResultTrace } from "../result-trace.js";
|
|
13
21
|
const JOB_LEASE_MS = 12e4;
|
|
14
22
|
const token = (row) => ({
|
|
15
23
|
projectId: row.project_id,
|
|
@@ -33,13 +41,16 @@ async function lockJob(client, job) {
|
|
|
33
41
|
return result.rows[0];
|
|
34
42
|
}
|
|
35
43
|
class PgJobQueue {
|
|
36
|
-
constructor(db, projects) {
|
|
44
|
+
constructor(db, projects, classificationRegistry = loadClassificationRegistry()) {
|
|
37
45
|
this.db = db;
|
|
38
46
|
this.projects = projects;
|
|
47
|
+
this.classificationRegistry = classificationRegistry;
|
|
39
48
|
z.array(publicId).min(1).parse(projects);
|
|
49
|
+
for (const project of projects) this.classificationScope(project);
|
|
40
50
|
}
|
|
41
51
|
db;
|
|
42
52
|
projects;
|
|
53
|
+
classificationRegistry;
|
|
43
54
|
classificationNotifications = /* @__PURE__ */ new Map();
|
|
44
55
|
completingNotifications = /* @__PURE__ */ new Map();
|
|
45
56
|
deferClassificationNotification(job, notify) {
|
|
@@ -94,6 +105,10 @@ class PgJobQueue {
|
|
|
94
105
|
if (!retained.has(key) && !this.completingNotifications.has(key) && this.classificationNotifications.get(key) === entry)
|
|
95
106
|
this.classificationNotifications.delete(key);
|
|
96
107
|
}
|
|
108
|
+
classificationScope(project) {
|
|
109
|
+
if (!this.projects.includes(project)) throw new Error("Unknown worker project");
|
|
110
|
+
return this.classificationRegistry.forProject(project);
|
|
111
|
+
}
|
|
97
112
|
scoped(job) {
|
|
98
113
|
if (!this.projects.includes(job.projectId)) throw new Error("Unknown worker project");
|
|
99
114
|
}
|
|
@@ -195,6 +210,16 @@ class PgJobQueue {
|
|
|
195
210
|
);
|
|
196
211
|
});
|
|
197
212
|
}
|
|
213
|
+
async recordPreparedTrace(job, trace) {
|
|
214
|
+
this.scoped(job);
|
|
215
|
+
await this.db.transaction(async (client) => {
|
|
216
|
+
await lockJob(client, job);
|
|
217
|
+
await client.query(
|
|
218
|
+
"UPDATE qa.runs SET metadata=jsonb_set(COALESCE(metadata,'{}'::jsonb),'{resultTrace}',$3::jsonb) WHERE project_id=$1 AND id=$2 AND phase='queued' AND verdict IS NULL",
|
|
219
|
+
[job.projectId, job.runId, JSON.stringify(trace)]
|
|
220
|
+
);
|
|
221
|
+
});
|
|
222
|
+
}
|
|
198
223
|
async start(job) {
|
|
199
224
|
this.scoped(job);
|
|
200
225
|
return this.db.transaction(async (client) => {
|
|
@@ -358,20 +383,46 @@ class PgJobQueue {
|
|
|
358
383
|
[job.projectId, job.jobId]
|
|
359
384
|
);
|
|
360
385
|
const { rows } = await client.query(
|
|
361
|
-
|
|
386
|
+
`SELECT j.attempts,j.retry_of,j.verified_revision,r.verdict,r.flow,r.batch_id,
|
|
387
|
+
s.response->'request' AS request,r.metadata->'resultTrace' AS prior_trace
|
|
388
|
+
FROM qa.jobs j JOIN qa.runs r ON r.project_id=j.project_id AND r.id=j.run_id
|
|
389
|
+
JOIN qa.server_submissions s ON s.project_id=r.project_id AND s.batch_id=r.batch_id
|
|
390
|
+
WHERE j.project_id=$1 AND j.id=$2`,
|
|
362
391
|
[job.projectId, job.jobId]
|
|
363
392
|
);
|
|
364
393
|
const row = rows[0];
|
|
365
|
-
if (row.attempts === 0 && row.retries !== 0 && ["failed", "error"].includes(row.verdict)) {
|
|
394
|
+
if (row.attempts === 0 && row.request?.retries !== 0 && ["failed", "error"].includes(row.verdict)) {
|
|
366
395
|
const existing = await client.query(
|
|
367
396
|
"SELECT id FROM qa.jobs WHERE project_id=$1 AND retry_of=$2",
|
|
368
397
|
[job.projectId, job.runId]
|
|
369
398
|
);
|
|
370
399
|
if (!existing.rowCount) {
|
|
371
400
|
const id = randomUUID();
|
|
401
|
+
const request = row.request;
|
|
402
|
+
const prior = readResultTrace(row.prior_trace);
|
|
403
|
+
const trace2 = prior.status === "recorded" && prior.trace.projectId === job.projectId && prior.trace.attemptId === job.runId ? prior.trace : void 0;
|
|
404
|
+
const resultTrace = buildResultTrace({
|
|
405
|
+
projectId: job.projectId,
|
|
406
|
+
flow: row.flow,
|
|
407
|
+
plan: trace2,
|
|
408
|
+
attemptId: id,
|
|
409
|
+
started: false,
|
|
410
|
+
retryOf: job.runId,
|
|
411
|
+
batchId: row.batch_id,
|
|
412
|
+
target: request?.target,
|
|
413
|
+
commit: request?.commit,
|
|
414
|
+
context: request?.runContext,
|
|
415
|
+
parameters: { runtime: request?.runtime, modelAccess: request?.modelAccess }
|
|
416
|
+
});
|
|
372
417
|
await client.query(
|
|
373
|
-
"INSERT INTO qa.runs(project_id,id,batch_id,flow,started_at,metadata)
|
|
374
|
-
[
|
|
418
|
+
"INSERT INTO qa.runs(project_id,id,batch_id,flow,started_at,metadata) VALUES($1,$2,$3,$4,extract(epoch FROM clock_timestamp())*1000,$5)",
|
|
419
|
+
[
|
|
420
|
+
job.projectId,
|
|
421
|
+
id,
|
|
422
|
+
row.batch_id,
|
|
423
|
+
row.flow,
|
|
424
|
+
{ retryOf: job.runId, attempt: 2, resultTrace }
|
|
425
|
+
]
|
|
375
426
|
);
|
|
376
427
|
await client.query(
|
|
377
428
|
"INSERT INTO qa.jobs(project_id,id,run_id,attempts,retry_of) VALUES($1,$2,$3,1,$4)",
|
|
@@ -380,17 +431,16 @@ class PgJobQueue {
|
|
|
380
431
|
}
|
|
381
432
|
return { runIds: [], quarantined: true };
|
|
382
433
|
}
|
|
434
|
+
let verifiedRetryPassed = false;
|
|
383
435
|
if (row.retry_of && row.verdict === "passed" && row.verified_revision) {
|
|
384
436
|
const original = await client.query(
|
|
385
437
|
"SELECT infrastructure_failure FROM qa.jobs WHERE project_id=$1 AND run_id=$2 AND verified_revision=$3",
|
|
386
438
|
[job.projectId, row.retry_of, row.verified_revision]
|
|
387
439
|
);
|
|
388
440
|
if (original.rows[0]) {
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
]);
|
|
393
|
-
const classification = await classifyFailure(
|
|
441
|
+
verifiedRetryPassed = true;
|
|
442
|
+
const scope = this.classificationScope(job.projectId);
|
|
443
|
+
const classification2 = await classifyFailure(
|
|
394
444
|
{
|
|
395
445
|
infrastructureFailure: !!original.rows[0].infrastructure_failure,
|
|
396
446
|
sameVersionRetryPassed: true,
|
|
@@ -402,28 +452,39 @@ class PgJobQueue {
|
|
|
402
452
|
classify: async () => {
|
|
403
453
|
throw new Error("Retry classification does not use a model");
|
|
404
454
|
}
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
"UPDATE qa.runs SET classification=$3,metadata=jsonb_set(COALESCE(metadata,'{}'::jsonb),'{failureClassification}',$4::jsonb) WHERE project_id=$1 AND id=$2 AND NOT EXISTS(SELECT 1 FROM qa.classification_labels WHERE project_id=$1 AND run_id=$2)",
|
|
409
|
-
[job.projectId, row.retry_of, classification.category, JSON.stringify(classification)]
|
|
455
|
+
},
|
|
456
|
+
void 0,
|
|
457
|
+
scope
|
|
410
458
|
);
|
|
459
|
+
await savePgClassificationInTransaction(client, scope, row.retry_of, classification2, true);
|
|
411
460
|
}
|
|
412
461
|
}
|
|
413
462
|
const final = await client.query(
|
|
414
463
|
`SELECT r.flow,r.verdict,COALESCE(original.classification,r.classification) AS category,
|
|
415
|
-
r.metadata
|
|
464
|
+
COALESCE(original.metadata->'failureClassification',r.metadata->'failureClassification') AS classification,
|
|
465
|
+
r.metadata->>'flowOwner' AS owner, r.metadata->'resultTrace' AS trace,
|
|
466
|
+
r.metadata->'quarantinePolicy' AS quarantine_policy FROM qa.runs r LEFT JOIN qa.runs original
|
|
416
467
|
ON original.project_id=r.project_id AND original.id=$3 WHERE r.project_id=$1 AND r.id=$2`,
|
|
417
468
|
[job.projectId, job.runId, row.retry_of]
|
|
418
469
|
);
|
|
419
470
|
const run = final.rows[0];
|
|
420
471
|
const parsed = failureCategorySchema.safeParse(run.category);
|
|
472
|
+
const classification = readClassification(run.classification);
|
|
473
|
+
const trace = readResultTrace(run.trace ?? void 0);
|
|
474
|
+
const policy = quarantinePolicySchema.safeParse(run.quarantine_policy);
|
|
421
475
|
const state = await new PgFlowStatus(this.db, job.projectId).recordInTransaction(client, {
|
|
422
476
|
flow: run.flow,
|
|
423
477
|
executionId: job.runId,
|
|
424
478
|
verdict: run.verdict,
|
|
425
|
-
category: parsed.success ? parsed.data : null,
|
|
426
|
-
|
|
479
|
+
category: classification ? classification.category : parsed.success ? parsed.data : null,
|
|
480
|
+
flakiness: verifiedRetryPassed ? "observed" : classification?.flakiness,
|
|
481
|
+
owner: run.owner ?? void 0,
|
|
482
|
+
...trace.status === "recorded" && trace.trace.caseRevision && { revision: trace.trace.caseRevision },
|
|
483
|
+
quarantine: {
|
|
484
|
+
...policy.success && { policy: policy.data },
|
|
485
|
+
...trace.status === "recorded" && { caseId: trace.trace.caseId },
|
|
486
|
+
...trace.status === "recorded" && trace.trace.target && { target: trace.trace.target }
|
|
487
|
+
}
|
|
427
488
|
});
|
|
428
489
|
return {
|
|
429
490
|
runIds: row.retry_of ? [row.retry_of, job.runId] : [job.runId],
|
|
@@ -1,8 +1,13 @@
|
|
|
1
|
+
import { type NotificationKind, type ProviderRegistry } from "@open-qa/qa-engine";
|
|
1
2
|
import { QaDatabase } from "../db/database.js";
|
|
2
3
|
import { type ResultContext, type ResultTransports } from "../result-notifier.js";
|
|
3
4
|
export interface ResultNotificationOptions {
|
|
4
5
|
projects: readonly string[];
|
|
5
|
-
transports
|
|
6
|
+
transports?: (projectId: string) => ResultTransports;
|
|
7
|
+
notifications?: Readonly<Record<string, {
|
|
8
|
+
registry: ProviderRegistry;
|
|
9
|
+
routes: Partial<Record<NotificationKind, readonly string[]>>;
|
|
10
|
+
}>>;
|
|
6
11
|
onError?: (message: string) => void;
|
|
7
12
|
presentation: (projectId: string, batchId: string, runIds: readonly string[]) => Promise<{
|
|
8
13
|
publicOrigins: readonly string[];
|
|
@@ -18,9 +23,12 @@ export declare const RESULT_DELIVERY_RETRY_WINDOW_SECONDS: number;
|
|
|
18
23
|
export declare class PgResultNotifications {
|
|
19
24
|
private readonly db;
|
|
20
25
|
private readonly options;
|
|
26
|
+
private readonly routes;
|
|
27
|
+
private readonly registries;
|
|
21
28
|
constructor(db: QaDatabase, options: ResultNotificationOptions);
|
|
22
|
-
enqueue(projectId: string, batchId: string, context
|
|
29
|
+
enqueue(projectId: string, batchId: string, context?: ResultContext): Promise<void>;
|
|
23
30
|
private notifyRuns;
|
|
31
|
+
private deliverEvent;
|
|
24
32
|
private summarizeFlows;
|
|
25
33
|
private drainNewFlows;
|
|
26
34
|
private reportFailure;
|
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
+
import {
|
|
4
|
+
notificationKindSchema
|
|
5
|
+
} from "@open-qa/qa-engine";
|
|
6
|
+
import { publicReportUrl } from "../public-report-url.js";
|
|
3
7
|
import { verdictSchema } from "@open-qa/qa-engine/protocol";
|
|
4
8
|
import { publicId } from "../db/database.js";
|
|
9
|
+
import { PgFlowStatus } from "../db/flow-status.js";
|
|
5
10
|
import { drainQuarantineNotifications } from "./quarantine-notifications.js";
|
|
6
11
|
import {
|
|
7
12
|
resultChannels,
|
|
8
13
|
resultContextSchema,
|
|
9
14
|
resultNotifiers
|
|
10
15
|
} from "../result-notifier.js";
|
|
16
|
+
function receiptId(...parts) {
|
|
17
|
+
return createHash("sha256").update(JSON.stringify(parts)).digest("hex");
|
|
18
|
+
}
|
|
11
19
|
const RESULT_DELIVERY_MAX_ATTEMPTS = 5;
|
|
12
20
|
const RESULT_DELIVERY_INITIAL_BACKOFF_SECONDS = 15 * 60;
|
|
13
21
|
const RESULT_DELIVERY_RETRY_WINDOW_SECONDS = RESULT_DELIVERY_INITIAL_BACKOFF_SECONDS * (2 ** (RESULT_DELIVERY_MAX_ATTEMPTS - 1) - 1);
|
|
@@ -23,38 +31,65 @@ class PgResultNotifications {
|
|
|
23
31
|
this.options = options;
|
|
24
32
|
if (!z.array(publicId).min(1).safeParse(options.projects).success)
|
|
25
33
|
throw new Error("Invalid result notification projects");
|
|
34
|
+
for (const [project, configuration] of Object.entries(options.notifications ?? {})) {
|
|
35
|
+
if (!options.projects.includes(project))
|
|
36
|
+
throw new Error("Unknown result notification project");
|
|
37
|
+
this.registries.set(project, configuration.registry);
|
|
38
|
+
const routes = /* @__PURE__ */ new Map();
|
|
39
|
+
for (const [event, ids] of Object.entries(configuration.routes)) {
|
|
40
|
+
const kind = notificationKindSchema.parse(event);
|
|
41
|
+
for (const id of ids) {
|
|
42
|
+
const provider = configuration.registry.notification(id);
|
|
43
|
+
const channel = `event-${receiptId(kind, provider.id)}`;
|
|
44
|
+
if (routes.has(channel)) throw new Error(`Duplicate notification route: ${kind}/${id}`);
|
|
45
|
+
routes.set(channel, { kind, provider: provider.id, channel });
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
this.routes.set(project, routes);
|
|
49
|
+
}
|
|
26
50
|
}
|
|
27
51
|
db;
|
|
28
52
|
options;
|
|
53
|
+
routes = /* @__PURE__ */ new Map();
|
|
54
|
+
registries = /* @__PURE__ */ new Map();
|
|
29
55
|
// Only trusted trigger assembly calls this; it is deliberately absent from the public submission API.
|
|
30
56
|
async enqueue(projectId, batchId, context) {
|
|
31
57
|
try {
|
|
32
58
|
if (!this.options.projects.includes(projectId)) throw new Error("Unknown project");
|
|
33
59
|
publicId.parse(batchId);
|
|
34
|
-
context = resultContextSchema.parse(context);
|
|
60
|
+
if (context !== void 0) context = resultContextSchema.parse(context);
|
|
35
61
|
await this.db.transaction(async (client) => {
|
|
36
62
|
const batch = await client.query(
|
|
37
63
|
"SELECT commit,target FROM qa.batches WHERE project_id=$1 AND id=$2 FOR UPDATE",
|
|
38
64
|
[projectId, batchId]
|
|
39
65
|
);
|
|
40
|
-
if (!batch.rows[0] || batch.rows[0].commit && batch.rows[0].commit !== context.commit || batch.rows[0].target && batch.rows[0].target !== context.target)
|
|
66
|
+
if (!batch.rows[0] || context && batch.rows[0].commit && batch.rows[0].commit !== context.commit || context && batch.rows[0].target && batch.rows[0].target !== context.target)
|
|
41
67
|
throw new Error("Batch context mismatch");
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
68
|
+
if (context) {
|
|
69
|
+
const mismatch = await client.query(
|
|
70
|
+
"SELECT channel FROM qa.result_notifications WHERE project_id=$1 AND batch_id=$2 AND channel IN ('commit','pr','discord') AND context<>$3::jsonb",
|
|
71
|
+
[projectId, batchId, context]
|
|
72
|
+
);
|
|
73
|
+
if (mismatch.rowCount) throw new Error("Result context already registered");
|
|
74
|
+
for (const channel of resultChannels(context))
|
|
75
|
+
await client.query(
|
|
76
|
+
`INSERT INTO qa.result_notifications(project_id,batch_id,channel,kind,provider,context)
|
|
77
|
+
VALUES($1,$2,$3,'batch.completed',$3,$4) ON CONFLICT DO NOTHING`,
|
|
78
|
+
[projectId, batchId, channel, context]
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
for (const { channel, kind, provider } of this.routes.get(projectId)?.values() ?? [])
|
|
48
82
|
await client.query(
|
|
49
|
-
|
|
50
|
-
|
|
83
|
+
`INSERT INTO qa.result_notifications(project_id,batch_id,channel,kind,provider,context)
|
|
84
|
+
VALUES($1,$2,$3,$4,$5,'{}') ON CONFLICT DO NOTHING`,
|
|
85
|
+
[projectId, batchId, channel, kind, provider]
|
|
51
86
|
);
|
|
52
87
|
});
|
|
53
88
|
} catch {
|
|
54
89
|
throw new Error("QA result registration failed");
|
|
55
90
|
}
|
|
56
91
|
}
|
|
57
|
-
async notifyRuns(row, context, runs, quarantined = 0) {
|
|
92
|
+
async notifyRuns(row, context, runs, quarantined = 0, quarantineOverdue = 0) {
|
|
58
93
|
if (!runs.length && row.channel !== "commit") return;
|
|
59
94
|
const presentation = await this.options.presentation(
|
|
60
95
|
row.project_id,
|
|
@@ -64,6 +99,7 @@ class PgResultNotifications {
|
|
|
64
99
|
const summary = {
|
|
65
100
|
batchId: row.batch_id,
|
|
66
101
|
quarantined,
|
|
102
|
+
...quarantineOverdue && { quarantineOverdue },
|
|
67
103
|
runs: runs.map((run) => ({
|
|
68
104
|
runId: run.id,
|
|
69
105
|
verdict: verdictSchema.parse(run.verdict),
|
|
@@ -71,6 +107,7 @@ class PgResultNotifications {
|
|
|
71
107
|
reportUrl: presentation.runs[run.id]?.reportUrl
|
|
72
108
|
}))
|
|
73
109
|
};
|
|
110
|
+
if (!this.options.transports) throw new Error("Missing legacy notification transports");
|
|
74
111
|
const notifier = resultNotifiers(
|
|
75
112
|
row.project_id,
|
|
76
113
|
{ ...context, checkContext: context.checkContext ?? "open-qa/smoke" },
|
|
@@ -88,6 +125,53 @@ class PgResultNotifications {
|
|
|
88
125
|
if (!notifier) throw new Error("Notification channel mismatch");
|
|
89
126
|
await notifier.notify(summary);
|
|
90
127
|
}
|
|
128
|
+
async deliverEvent(row, runs) {
|
|
129
|
+
const registry = this.registries.get(row.project_id);
|
|
130
|
+
const kind = notificationKindSchema.parse(row.kind);
|
|
131
|
+
if (!registry || !row.provider || row.channel !== `event-${receiptId(kind, row.provider)}`)
|
|
132
|
+
throw new Error("Missing notification route");
|
|
133
|
+
const provider = registry.notification(row.provider);
|
|
134
|
+
const selected = kind === "flow.failed" ? runs.filter((run) => run.verdict === "failed" || run.verdict === "error") : runs;
|
|
135
|
+
if (!selected.length) return;
|
|
136
|
+
const presentation = await this.options.presentation(
|
|
137
|
+
row.project_id,
|
|
138
|
+
row.batch_id,
|
|
139
|
+
selected.map((run) => run.id)
|
|
140
|
+
);
|
|
141
|
+
const event = {
|
|
142
|
+
version: 1,
|
|
143
|
+
id: receiptId(row.project_id, row.batch_id, kind, provider.id),
|
|
144
|
+
kind,
|
|
145
|
+
project: row.project_id,
|
|
146
|
+
batch: row.batch_id,
|
|
147
|
+
flows: selected.map((run) => ({
|
|
148
|
+
flow: run.flow,
|
|
149
|
+
run: run.id,
|
|
150
|
+
verdict: verdictSchema.parse(run.verdict),
|
|
151
|
+
quarantined: Boolean(run.quarantined),
|
|
152
|
+
links: {
|
|
153
|
+
report: publicReportUrl(presentation.runs[run.id]?.reportUrl, presentation.publicOrigins),
|
|
154
|
+
replay: publicReportUrl(presentation.runs[run.id]?.replayUrl, presentation.publicOrigins)
|
|
155
|
+
}
|
|
156
|
+
}))
|
|
157
|
+
};
|
|
158
|
+
const controller = new AbortController();
|
|
159
|
+
let timer;
|
|
160
|
+
try {
|
|
161
|
+
await Promise.race([
|
|
162
|
+
provider.deliver(event, controller.signal),
|
|
163
|
+
new Promise((_, reject) => {
|
|
164
|
+
timer = setTimeout(() => {
|
|
165
|
+
controller.abort();
|
|
166
|
+
reject(new Error("QA result delivery timed out"));
|
|
167
|
+
}, 5e3);
|
|
168
|
+
})
|
|
169
|
+
]);
|
|
170
|
+
} finally {
|
|
171
|
+
clearTimeout(timer);
|
|
172
|
+
controller.abort();
|
|
173
|
+
}
|
|
174
|
+
}
|
|
91
175
|
async summarizeFlows(client, row) {
|
|
92
176
|
await client.query(
|
|
93
177
|
`UPDATE qa.result_notifications n SET
|
|
@@ -231,16 +315,16 @@ class PgResultNotifications {
|
|
|
231
315
|
async drain() {
|
|
232
316
|
try {
|
|
233
317
|
const pending = await this.db.pool.query(
|
|
234
|
-
`SELECT n.project_id,n.batch_id,n.channel,n.context->>'source' AS source FROM qa.result_notifications n
|
|
318
|
+
`SELECT n.project_id,n.batch_id,n.channel,n.kind,n.provider,n.context->>'source' AS source FROM qa.result_notifications n
|
|
235
319
|
JOIN qa.batches b ON b.project_id=n.project_id AND b.id=n.batch_id
|
|
236
|
-
WHERE n.project_id=ANY($1::text[]) AND n.completed_at IS NULL AND n.abandoned_at IS NULL AND (n.context->>'source'='new-flow' OR n.attempts<${RESULT_DELIVERY_MAX_ATTEMPTS})
|
|
320
|
+
WHERE n.project_id=ANY($1::text[]) AND n.completed_at IS NULL AND n.abandoned_at IS NULL AND ((n.channel='pr' AND n.context->>'source'='new-flow') OR n.attempts<${RESULT_DELIVERY_MAX_ATTEMPTS})
|
|
237
321
|
AND n.available_at<=clock_timestamp() AND ${terminal}
|
|
238
322
|
ORDER BY b.created_at DESC,b.id DESC,n.channel LIMIT 100`,
|
|
239
323
|
[this.options.projects]
|
|
240
324
|
);
|
|
241
325
|
let failed = false;
|
|
242
326
|
for (const row of pending.rows) {
|
|
243
|
-
if (row.source === "new-flow") {
|
|
327
|
+
if (row.channel === "pr" && row.source === "new-flow") {
|
|
244
328
|
if ((await this.drainNewFlows(row)).length) failed = true;
|
|
245
329
|
continue;
|
|
246
330
|
}
|
|
@@ -254,8 +338,9 @@ class PgResultNotifications {
|
|
|
254
338
|
key
|
|
255
339
|
);
|
|
256
340
|
if (!locked.rows[0]) return true;
|
|
257
|
-
const
|
|
258
|
-
|
|
341
|
+
const generic = row.channel.startsWith("event-");
|
|
342
|
+
const context = generic ? void 0 : resultContextSchema.parse(locked.rows[0].context);
|
|
343
|
+
if (row.channel === "pr" && context?.source === "pr-label") {
|
|
259
344
|
const lock = await client.query(
|
|
260
345
|
"SELECT pg_try_advisory_xact_lock(hashtextextended($1,1045)) AS ok",
|
|
261
346
|
[
|
|
@@ -303,12 +388,20 @@ class PgResultNotifications {
|
|
|
303
388
|
ORDER BY r.started_at,r.id`,
|
|
304
389
|
[row.project_id, row.batch_id]
|
|
305
390
|
);
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
391
|
+
if (context) {
|
|
392
|
+
const quarantined = runs.rows.filter((run) => run.quarantined);
|
|
393
|
+
const current = row.channel === "commit" && quarantined.length ? await new PgFlowStatus(this.db, row.project_id).list(
|
|
394
|
+
quarantined.map(({ flow }) => flow),
|
|
395
|
+
client
|
|
396
|
+
) : [];
|
|
397
|
+
await this.notifyRuns(
|
|
398
|
+
row,
|
|
399
|
+
context,
|
|
400
|
+
runs.rows.filter((run) => !run.quarantined),
|
|
401
|
+
quarantined.length,
|
|
402
|
+
current.filter(({ quarantineDeadline }) => quarantineDeadline?.overdue).length
|
|
403
|
+
);
|
|
404
|
+
} else await this.deliverEvent(row, runs.rows);
|
|
312
405
|
await client.query(
|
|
313
406
|
"UPDATE qa.result_notifications SET completed_at=clock_timestamp() WHERE project_id=$1 AND batch_id=$2 AND channel=$3",
|
|
314
407
|
key
|
|
@@ -332,7 +425,11 @@ class PgResultNotifications {
|
|
|
332
425
|
if (failure) this.reportFailure(row, failure);
|
|
333
426
|
}
|
|
334
427
|
}
|
|
335
|
-
|
|
428
|
+
if (this.options.transports)
|
|
429
|
+
await drainQuarantineNotifications(this.db, {
|
|
430
|
+
...this.options,
|
|
431
|
+
transports: this.options.transports
|
|
432
|
+
});
|
|
336
433
|
if (failed) throw new Error("QA result delivery failed");
|
|
337
434
|
} catch {
|
|
338
435
|
throw new Error("QA result delivery failed");
|
package/src/worker/store.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { readExecutionSnapshot } from "@open-qa/qa-engine/protocol";
|
|
2
1
|
import { PgRunStore } from "../adapters/pg-run-store.js";
|
|
2
|
+
import { assertNewRunMetadata } from "../adapters/run-metadata.js";
|
|
3
3
|
import { assertPostgresText } from "../db/database.js";
|
|
4
4
|
import { lockJob } from "./queue.js";
|
|
5
5
|
function jobRunStore(queue, job, objects) {
|
|
@@ -14,8 +14,7 @@ function jobRunStore(queue, job, objects) {
|
|
|
14
14
|
async createRun(input) {
|
|
15
15
|
if (input.id !== job.runId) throw new Error("Worker run ID mismatch");
|
|
16
16
|
assertPostgresText(input);
|
|
17
|
-
|
|
18
|
-
throw new Error("Invalid execution snapshot");
|
|
17
|
+
assertNewRunMetadata(input.metadata);
|
|
19
18
|
await queue.db.transaction(async (client) => {
|
|
20
19
|
await lockJob(client, job);
|
|
21
20
|
const result = await client.query(
|
|
@@ -38,6 +37,10 @@ function jobRunStore(queue, job, objects) {
|
|
|
38
37
|
finishRun: (id, result) => {
|
|
39
38
|
if (id !== job.runId) throw new Error("Worker run ID mismatch");
|
|
40
39
|
return store.finishRun(id, result);
|
|
40
|
+
},
|
|
41
|
+
recordClassifierUsage: (id, samples) => {
|
|
42
|
+
if (id !== job.runId) throw new Error("Worker run ID mismatch");
|
|
43
|
+
return store.recordClassifierUsage(id, samples);
|
|
41
44
|
}
|
|
42
45
|
};
|
|
43
46
|
}
|