@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.
- package/LICENSE +204 -0
- package/NOTICE +7 -0
- package/THIRD_PARTY.md +12 -0
- package/THIRD_PARTY_NOTICES.md +11 -0
- package/drizzle/0000_qa_core.sql +158 -0
- package/drizzle/0001_run_health.sql +3 -0
- package/drizzle/0002_bounded_cleanup.sql +5 -0
- package/drizzle/0003_step_replay_cache.sql +9 -0
- package/drizzle/0004_credential_expiry.sql +1 -0
- package/drizzle/0005_server_api.sql +19 -0
- package/drizzle/0006_submission_created.sql +3 -0
- package/drizzle/0007_findings.sql +19 -0
- package/drizzle/0008_classification_labels.sql +14 -0
- package/drizzle/0009_job_queue.sql +20 -0
- package/drizzle/0010_result_notifications.sql +13 -0
- package/drizzle/0011_result_notification_retries.sql +5 -0
- package/drizzle/0012_result_notification_flows.sql +44 -0
- package/drizzle/0013_flow_quarantine.sql +35 -0
- package/drizzle/0014_run_budgets.sql +2 -0
- package/drizzle/0015_flow_lifecycle.sql +9 -0
- package/drizzle/0016_open_qa_classification.sql +28 -0
- package/drizzle/meta/0000_snapshot.json +1002 -0
- package/drizzle/meta/0001_snapshot.json +1021 -0
- package/drizzle/meta/0002_snapshot.json +1027 -0
- package/drizzle/meta/0003_snapshot.json +1081 -0
- package/drizzle/meta/0004_snapshot.json +1087 -0
- package/drizzle/meta/0005_snapshot.json +1210 -0
- package/drizzle/meta/0006_snapshot.json +1244 -0
- package/drizzle/meta/0007_snapshot.json +1353 -0
- package/drizzle/meta/0008_snapshot.json +1435 -0
- package/drizzle/meta/0009_snapshot.json +1587 -0
- package/drizzle/meta/0010_snapshot.json +1675 -0
- package/drizzle/meta/0011_snapshot.json +1692 -0
- package/drizzle/meta/0012_snapshot.json +1818 -0
- package/drizzle/meta/0013_snapshot.json +2016 -0
- package/drizzle/meta/0014_snapshot.json +2028 -0
- package/drizzle/meta/0015_snapshot.json +2064 -0
- package/drizzle/meta/0016_snapshot.json +2076 -0
- package/drizzle/meta/_journal.json +125 -0
- package/package.json +118 -4
- package/problem-matcher.json +32 -0
- package/src/adapters/db-lease-pool.d.ts +25 -0
- package/src/adapters/db-lease-pool.js +293 -0
- package/src/adapters/dir-artifact-store.d.ts +9 -0
- package/src/adapters/dir-artifact-store.js +64 -0
- package/src/adapters/e2b-agent.d.ts +7 -0
- package/src/adapters/e2b-agent.js +94 -0
- package/src/adapters/e2b-artifacts.d.ts +3 -0
- package/src/adapters/e2b-artifacts.js +66 -0
- package/src/adapters/e2b-bounded.d.ts +1 -0
- package/src/adapters/e2b-bounded.js +19 -0
- package/src/adapters/e2b-client.d.ts +33 -0
- package/src/adapters/e2b-client.js +114 -0
- package/src/adapters/e2b-executor.d.ts +26 -0
- package/src/adapters/e2b-executor.js +194 -0
- package/src/adapters/e2b-lifetime.d.ts +20 -0
- package/src/adapters/e2b-lifetime.js +73 -0
- package/src/adapters/e2b-prune.d.ts +13 -0
- package/src/adapters/e2b-prune.js +42 -0
- package/src/adapters/file-replay-cache.d.ts +14 -0
- package/src/adapters/file-replay-cache.js +120 -0
- package/src/adapters/file-run-store.d.ts +175 -0
- package/src/adapters/file-run-store.js +280 -0
- package/src/adapters/fixed-lease-pool.d.ts +10 -0
- package/src/adapters/fixed-lease-pool.js +237 -0
- package/src/adapters/gcs/artifact-store.d.ts +49 -0
- package/src/adapters/gcs/artifact-store.js +247 -0
- package/src/adapters/gcs/client.d.ts +22 -0
- package/src/adapters/gcs/client.js +106 -0
- package/src/adapters/gcs/download.d.ts +9 -0
- package/src/adapters/gcs/download.js +43 -0
- package/src/adapters/gcs/index.d.ts +8 -0
- package/src/adapters/gcs/index.js +23 -0
- package/src/adapters/gcs/periodic-flush.d.ts +5 -0
- package/src/adapters/gcs/periodic-flush.js +16 -0
- package/src/adapters/gcs/pg-catalog.d.ts +16 -0
- package/src/adapters/gcs/pg-catalog.js +149 -0
- package/src/adapters/gcs/replay-completeness.d.ts +1 -0
- package/src/adapters/gcs/replay-completeness.js +65 -0
- package/src/adapters/gcs/scan-upload.d.ts +10 -0
- package/src/adapters/gcs/scan-upload.js +26 -0
- package/src/adapters/index.d.ts +5 -0
- package/src/adapters/index.js +12 -0
- package/src/adapters/lease-transaction.d.ts +2 -0
- package/src/adapters/lease-transaction.js +61 -0
- package/src/adapters/local-credential-resolver.d.ts +5 -0
- package/src/adapters/local-credential-resolver.js +15 -0
- package/src/adapters/local-env.d.ts +1 -0
- package/src/adapters/local-env.js +37 -0
- package/src/adapters/local-files.d.ts +17 -0
- package/src/adapters/local-files.js +150 -0
- package/src/adapters/pg-classification-store.d.ts +58 -0
- package/src/adapters/pg-classification-store.js +112 -0
- package/src/adapters/pg-replay-cache.d.ts +16 -0
- package/src/adapters/pg-replay-cache.js +111 -0
- package/src/adapters/pg-run-store.d.ts +58 -0
- package/src/adapters/pg-run-store.js +399 -0
- package/src/adapters/resource-pools.d.ts +2 -0
- package/src/adapters/resource-pools.js +28 -0
- package/src/adapters/secret-manager-credential-resolver.d.ts +44 -0
- package/src/adapters/secret-manager-credential-resolver.js +102 -0
- package/src/artifact-scan.d.ts +21 -0
- package/src/artifact-scan.js +139 -0
- package/src/budget-confirmation.d.ts +23 -0
- package/src/budget-confirmation.js +43 -0
- package/src/cache-command.d.ts +7 -0
- package/src/cache-command.js +107 -0
- package/src/classification-command.d.ts +14 -0
- package/src/classification-command.js +72 -0
- package/src/classification-diff.d.ts +10 -0
- package/src/classification-diff.js +47 -0
- package/src/classification-model.d.ts +17 -0
- package/src/classification-model.js +51 -0
- package/src/classification-notifier.d.ts +21 -0
- package/src/classification-notifier.js +34 -0
- package/src/classify-runs.d.ts +31 -0
- package/src/classify-runs.js +138 -0
- package/src/cli-program.d.ts +9 -0
- package/src/cli-program.js +172 -0
- package/src/cli.d.ts +2 -0
- package/src/cli.js +12 -0
- package/src/commands.d.ts +7 -0
- package/src/commands.js +17 -0
- package/src/credential-references.d.ts +24 -0
- package/src/credential-references.js +33 -0
- package/src/db/commands.d.ts +1 -0
- package/src/db/commands.js +34 -0
- package/src/db/credentials.d.ts +16 -0
- package/src/db/credentials.js +73 -0
- package/src/db/database.d.ts +12 -0
- package/src/db/database.js +135 -0
- package/src/db/event-chunks.d.ts +405 -0
- package/src/db/event-chunks.js +74 -0
- package/src/db/fencing.d.ts +8 -0
- package/src/db/fencing.js +16 -0
- package/src/db/flow-status.d.ts +41 -0
- package/src/db/flow-status.js +208 -0
- package/src/db/index.d.ts +12 -0
- package/src/db/index.js +24 -0
- package/src/db/queries.d.ts +73 -0
- package/src/db/queries.js +122 -0
- package/src/db/resources.d.ts +14 -0
- package/src/db/resources.js +61 -0
- package/src/db/schema.d.ts +3532 -0
- package/src/db/schema.js +501 -0
- package/src/demo-command.d.ts +1 -0
- package/src/demo-command.js +50 -0
- package/src/doctor-command.d.ts +15 -0
- package/src/doctor-command.js +275 -0
- package/src/doctor-summary.d.ts +2 -0
- package/src/doctor-summary.js +31 -0
- package/src/draft-api.d.ts +460 -0
- package/src/draft-api.js +333 -0
- package/src/draft-command.d.ts +10 -0
- package/src/draft-command.js +73 -0
- package/src/draft-hooks.d.ts +3 -0
- package/src/draft-hooks.js +33 -0
- package/src/draft-pages.d.ts +11 -0
- package/src/draft-pages.js +82 -0
- package/src/engine.d.ts +105 -0
- package/src/engine.js +323 -0
- package/src/execution-snapshot.d.ts +21 -0
- package/src/execution-snapshot.js +141 -0
- package/src/flow-check.d.ts +9 -0
- package/src/flow-check.js +68 -0
- package/src/flow-lifecycle.d.ts +6 -0
- package/src/flow-lifecycle.js +61 -0
- package/src/flow-lint.d.ts +8 -0
- package/src/flow-lint.js +73 -0
- package/src/flow-loader.d.ts +219 -0
- package/src/flow-loader.js +236 -0
- package/src/flow-source.d.ts +2 -0
- package/src/flow-source.js +6 -0
- package/src/flows-api.d.ts +1 -0
- package/src/flows-api.js +12 -0
- package/src/heal-notification.d.ts +16 -0
- package/src/heal-notification.js +62 -0
- package/src/hooks/api.d.ts +29 -0
- package/src/hooks/api.js +15 -0
- package/src/hooks/consumer.d.ts +18 -0
- package/src/hooks/consumer.js +98 -0
- package/src/hooks/loader.d.ts +6 -0
- package/src/hooks/loader.js +28 -0
- package/src/hooks/registry.d.ts +22 -0
- package/src/hooks/registry.js +23 -0
- package/src/hooks/runner.d.ts +19 -0
- package/src/hooks/runner.js +279 -0
- package/src/hooks/session-cache.d.ts +16 -0
- package/src/hooks/session-cache.js +42 -0
- package/src/hooks/session-store.d.ts +20 -0
- package/src/hooks/session-store.js +95 -0
- package/src/hooks/worker.d.ts +1 -0
- package/src/hooks/worker.js +84 -0
- package/src/http-stage-client.d.ts +367 -0
- package/src/http-stage-client.js +190 -0
- package/src/independent-model.d.ts +22 -0
- package/src/independent-model.js +215 -0
- package/src/lifecycle-command.d.ts +15 -0
- package/src/lifecycle-command.js +190 -0
- package/src/local-command.d.ts +3 -0
- package/src/local-command.js +27 -0
- package/src/local-host-executor.d.ts +38 -0
- package/src/local-host-executor.js +196 -0
- package/src/open-command.d.ts +7 -0
- package/src/open-command.js +39 -0
- package/src/operations-api.d.ts +2 -0
- package/src/operations-api.js +6 -0
- package/src/personas.d.ts +2 -0
- package/src/personas.js +61 -0
- package/src/program-runner.d.ts +3 -0
- package/src/program-runner.js +263 -0
- package/src/program-sandbox.py +75 -0
- package/src/program-scratch.d.ts +2 -0
- package/src/program-scratch.js +48 -0
- package/src/program-worker.mjs +101 -0
- package/src/public-id.d.ts +2 -0
- package/src/public-id.js +5 -0
- package/src/public-report-url.d.ts +1 -0
- package/src/public-report-url.js +18 -0
- package/src/publication-check.d.ts +1 -0
- package/src/publication-check.js +31 -0
- package/src/quarantine-command.d.ts +9 -0
- package/src/quarantine-command.js +69 -0
- package/src/remote-command.d.ts +30 -0
- package/src/remote-command.js +156 -0
- package/src/replay-image.d.ts +1 -0
- package/src/replay-image.js +35 -0
- package/src/report/assets.d.ts +1 -0
- package/src/report/assets.js +5 -0
- package/src/report/browser.css +414 -0
- package/src/report/browser.d.ts +1 -0
- package/src/report/browser.js +219 -0
- package/src/report/data.d.ts +1 -0
- package/src/report/data.js +4 -0
- package/src/report/generate.d.ts +1 -0
- package/src/report/generate.js +54 -0
- package/src/report/load.d.ts +870 -0
- package/src/report/load.js +139 -0
- package/src/report/resource-table.d.ts +2 -0
- package/src/report/resource-table.js +4 -0
- package/src/report/status-icon.d.ts +2 -0
- package/src/report/status-icon.js +20 -0
- package/src/report/style.css +413 -0
- package/src/result-notifier.d.ts +121 -0
- package/src/result-notifier.js +230 -0
- package/src/result-transports.d.ts +12 -0
- package/src/result-transports.js +150 -0
- package/src/run-command.d.ts +32 -0
- package/src/run-command.js +493 -0
- package/src/run-setup-error.d.ts +11 -0
- package/src/run-setup-error.js +18 -0
- package/src/sandboxes-command.d.ts +9 -0
- package/src/sandboxes-command.js +29 -0
- package/src/screen-proxy.d.ts +5 -0
- package/src/screen-proxy.js +79 -0
- package/src/screen-url.d.ts +18 -0
- package/src/screen-url.js +54 -0
- package/src/server/app.d.ts +29 -0
- package/src/server/app.js +350 -0
- package/src/server/auth.d.ts +33 -0
- package/src/server/auth.js +113 -0
- package/src/server/credentials.d.ts +30 -0
- package/src/server/credentials.js +77 -0
- package/src/server/errors.d.ts +9 -0
- package/src/server/errors.js +26 -0
- package/src/server/index.d.ts +35 -0
- package/src/server/index.js +96 -0
- package/src/server/maintenance.d.ts +13 -0
- package/src/server/maintenance.js +54 -0
- package/src/server/repository.d.ts +534 -0
- package/src/server/repository.js +219 -0
- package/src/setup-command.d.ts +10 -0
- package/src/setup-command.js +52 -0
- package/src/stage-environment.d.ts +1 -0
- package/src/stage-environment.js +35 -0
- package/src/stage-supervisor.mjs +95 -0
- package/src/studio-command.d.ts +7 -0
- package/src/studio-command.js +47 -0
- package/src/studio-control.d.ts +5 -0
- package/src/studio-control.js +207 -0
- package/src/studio-review.d.ts +24 -0
- package/src/studio-review.js +64 -0
- package/src/studio-worker.d.ts +1 -0
- package/src/studio-worker.js +48 -0
- package/src/system-config.d.ts +7 -0
- package/src/system-config.js +70 -0
- package/src/terminal-reporter.d.ts +13 -0
- package/src/terminal-reporter.js +93 -0
- package/src/vendor/gmail.d.ts +45 -0
- package/src/vendor/gmail.js +193 -0
- package/src/watch-command.d.ts +16 -0
- package/src/watch-command.js +93 -0
- package/src/watch-state.d.ts +4 -0
- package/src/watch-state.js +53 -0
- package/src/worker/execution-error.d.ts +5 -0
- package/src/worker/execution-error.js +10 -0
- package/src/worker/execution.d.ts +24 -0
- package/src/worker/execution.js +190 -0
- package/src/worker/index.d.ts +8 -0
- package/src/worker/index.js +40 -0
- package/src/worker/prune.d.ts +3 -0
- package/src/worker/prune.js +22 -0
- package/src/worker/quarantine-notifications.d.ts +8 -0
- package/src/worker/quarantine-notifications.js +89 -0
- package/src/worker/queue.d.ts +65 -0
- package/src/worker/queue.js +438 -0
- package/src/worker/result-notifications.d.ts +28 -0
- package/src/worker/result-notifications.js +347 -0
- package/src/worker/store.d.ts +4 -0
- package/src/worker/store.js +46 -0
- package/src/worker/worker.d.ts +21 -0
- package/src/worker/worker.js +140 -0
- package/README.md +0 -3
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { classifyFailure } from "@open-qa/qa-engine";
|
|
5
|
+
import {
|
|
6
|
+
verdictSchema,
|
|
7
|
+
batchDtoSchema,
|
|
8
|
+
submitBatchSchema
|
|
9
|
+
} from "@open-qa/qa-engine/protocol";
|
|
10
|
+
import { publicId } from "../db/database.js";
|
|
11
|
+
import { PgFlowStatus } from "../db/flow-status.js";
|
|
12
|
+
import { failureCategorySchema } from "@open-qa/qa-engine/protocol";
|
|
13
|
+
const JOB_LEASE_MS = 12e4;
|
|
14
|
+
const token = (row) => ({
|
|
15
|
+
projectId: row.project_id,
|
|
16
|
+
jobId: row.id,
|
|
17
|
+
runId: row.run_id,
|
|
18
|
+
owner: row.owner,
|
|
19
|
+
epoch: row.epoch
|
|
20
|
+
});
|
|
21
|
+
const values = (job) => [job.projectId, job.jobId, job.owner, job.epoch, job.runId];
|
|
22
|
+
async function lockJob(client, job) {
|
|
23
|
+
await client.query(
|
|
24
|
+
"SELECT id FROM qa.jobs WHERE project_id=$1 AND id=$2 FOR UPDATE",
|
|
25
|
+
values(job).slice(0, 2)
|
|
26
|
+
);
|
|
27
|
+
const result = await client.query(
|
|
28
|
+
`SELECT * FROM qa.jobs WHERE project_id=$1 AND id=$2 AND owner=$3 AND epoch=$4 AND run_id=$5
|
|
29
|
+
AND state IN ('claimed','running','cancel-requested') AND lease_until>clock_timestamp()`,
|
|
30
|
+
values(job)
|
|
31
|
+
);
|
|
32
|
+
if (!result.rows[0]) throw new Error("Stale job owner or epoch");
|
|
33
|
+
return result.rows[0];
|
|
34
|
+
}
|
|
35
|
+
class PgJobQueue {
|
|
36
|
+
constructor(db, projects) {
|
|
37
|
+
this.db = db;
|
|
38
|
+
this.projects = projects;
|
|
39
|
+
z.array(publicId).min(1).parse(projects);
|
|
40
|
+
}
|
|
41
|
+
db;
|
|
42
|
+
projects;
|
|
43
|
+
classificationNotifications = /* @__PURE__ */ new Map();
|
|
44
|
+
completingNotifications = /* @__PURE__ */ new Map();
|
|
45
|
+
deferClassificationNotification(job, notify) {
|
|
46
|
+
this.scoped(job);
|
|
47
|
+
this.classificationNotifications.set(JSON.stringify([job.projectId, job.runId]), {
|
|
48
|
+
job,
|
|
49
|
+
notify
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
async notifyCompletion(job, completion) {
|
|
53
|
+
if (!completion) return;
|
|
54
|
+
const key = JSON.stringify([job.projectId, job.runId]);
|
|
55
|
+
const notify = this.classificationNotifications.get(key)?.notify;
|
|
56
|
+
this.classificationNotifications.delete(key);
|
|
57
|
+
if (!completion.quarantined && completion.runIds.length && notify) {
|
|
58
|
+
try {
|
|
59
|
+
await notify(completion.runIds);
|
|
60
|
+
} catch {
|
|
61
|
+
try {
|
|
62
|
+
console.warn("QA classification notification failed");
|
|
63
|
+
} catch {
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
async complete(job, action) {
|
|
69
|
+
const key = JSON.stringify([job.projectId, job.runId]);
|
|
70
|
+
this.completingNotifications.set(key, (this.completingNotifications.get(key) ?? 0) + 1);
|
|
71
|
+
try {
|
|
72
|
+
await this.notifyCompletion(job, await action());
|
|
73
|
+
} finally {
|
|
74
|
+
const remaining = this.completingNotifications.get(key) - 1;
|
|
75
|
+
if (remaining) this.completingNotifications.set(key, remaining);
|
|
76
|
+
else this.completingNotifications.delete(key);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async pruneClassificationNotifications() {
|
|
80
|
+
const pending = [...this.classificationNotifications.entries()];
|
|
81
|
+
if (!pending.length) return;
|
|
82
|
+
const { rows } = await this.db.pool.query(
|
|
83
|
+
`SELECT j.project_id,j.run_id FROM qa.jobs j
|
|
84
|
+
JOIN jsonb_to_recordset($1::jsonb) AS p(project_id text,run_id text)
|
|
85
|
+
ON j.project_id=p.project_id AND j.run_id=p.run_id WHERE j.cleaned_at IS NULL`,
|
|
86
|
+
[
|
|
87
|
+
JSON.stringify(
|
|
88
|
+
pending.map(([, { job }]) => ({ project_id: job.projectId, run_id: job.runId }))
|
|
89
|
+
)
|
|
90
|
+
]
|
|
91
|
+
);
|
|
92
|
+
const retained = new Set(rows.map((row) => JSON.stringify([row.project_id, row.run_id])));
|
|
93
|
+
for (const [key, entry] of pending)
|
|
94
|
+
if (!retained.has(key) && !this.completingNotifications.has(key) && this.classificationNotifications.get(key) === entry)
|
|
95
|
+
this.classificationNotifications.delete(key);
|
|
96
|
+
}
|
|
97
|
+
scoped(job) {
|
|
98
|
+
if (!this.projects.includes(job.projectId)) throw new Error("Unknown worker project");
|
|
99
|
+
}
|
|
100
|
+
async claim(owner, concurrency, signal) {
|
|
101
|
+
publicId.parse(owner);
|
|
102
|
+
z.number().int().min(1).max(8).parse(concurrency);
|
|
103
|
+
for (let attempt = 0; ; attempt++) {
|
|
104
|
+
signal?.throwIfAborted();
|
|
105
|
+
try {
|
|
106
|
+
return await this.db.transaction(async (client) => {
|
|
107
|
+
await client.query("SET LOCAL lock_timeout='250ms'");
|
|
108
|
+
const locked = await client.query(
|
|
109
|
+
"SELECT pg_try_advisory_xact_lock(hashtextextended($1,1042)) AS ok",
|
|
110
|
+
[owner]
|
|
111
|
+
);
|
|
112
|
+
if (!locked.rows[0].ok) return void 0;
|
|
113
|
+
const count = await client.query(
|
|
114
|
+
"SELECT count(*)::int AS count FROM qa.worker_slots s JOIN qa.jobs j ON j.project_id=s.project_id AND j.id=s.job_id WHERE j.owner=$1",
|
|
115
|
+
[owner]
|
|
116
|
+
);
|
|
117
|
+
if (count.rows[0].count >= concurrency) return void 0;
|
|
118
|
+
const slot = await client.query(
|
|
119
|
+
"SELECT id FROM qa.worker_slots WHERE job_id IS NULL ORDER BY id FOR UPDATE SKIP LOCKED LIMIT 1"
|
|
120
|
+
);
|
|
121
|
+
if (!slot.rows[0]) return void 0;
|
|
122
|
+
for (let scanned = 0; scanned < 100; scanned++) {
|
|
123
|
+
signal?.throwIfAborted();
|
|
124
|
+
const selected = await client.query(
|
|
125
|
+
`SELECT * FROM qa.jobs WHERE project_id=ANY($1::text[]) AND state='queued' AND available_at<=clock_timestamp() ORDER BY available_at,project_id,id FOR UPDATE SKIP LOCKED LIMIT 1`,
|
|
126
|
+
[this.projects]
|
|
127
|
+
);
|
|
128
|
+
const row = selected.rows[0];
|
|
129
|
+
if (!row) return void 0;
|
|
130
|
+
const details = await client.query(
|
|
131
|
+
`SELECT r.flow,r.batch_id,b.created_by,b.trigger,COALESCE(previous_run.metadata->>'workerTargetRevision',previous.verified_revision,b.commit) AS commit,b.target,s.response->'request' AS request FROM qa.runs r JOIN qa.batches b ON b.project_id=r.project_id AND b.id=r.batch_id JOIN qa.server_submissions s ON s.project_id=b.project_id AND s.batch_id=b.id LEFT JOIN qa.jobs previous ON previous.project_id=r.project_id AND previous.run_id=$3 LEFT JOIN qa.runs previous_run ON previous_run.project_id=previous.project_id AND previous_run.id=previous.run_id WHERE r.project_id=$1 AND r.id=$2`,
|
|
132
|
+
[row.project_id, row.run_id, row.retry_of]
|
|
133
|
+
);
|
|
134
|
+
const detail = details.rows[0];
|
|
135
|
+
const identityValid = detail && publicId.safeParse(detail.created_by).success && batchDtoSchema.shape.trigger.safeParse(detail.trigger).success;
|
|
136
|
+
const request = submitBatchSchema.safeParse(detail?.request);
|
|
137
|
+
const reason = !identityValid ? "invalid-job-identity" : !request.success ? "invalid-job-request" : void 0;
|
|
138
|
+
if (reason) {
|
|
139
|
+
await client.query(
|
|
140
|
+
"UPDATE qa.jobs SET state='done',cleaned_at=clock_timestamp() WHERE project_id=$1 AND id=$2",
|
|
141
|
+
[row.project_id, row.id]
|
|
142
|
+
);
|
|
143
|
+
await client.query(
|
|
144
|
+
"UPDATE qa.runs SET verdict='error',reason=$3,phase='finished',finished_at=extract(epoch FROM clock_timestamp())*1000 WHERE project_id=$1 AND id=$2",
|
|
145
|
+
[row.project_id, row.run_id, reason]
|
|
146
|
+
);
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (!detail || !request.success) throw new Error("Invalid queued job");
|
|
150
|
+
const updated = await client.query(
|
|
151
|
+
`UPDATE qa.jobs SET owner=$3,epoch=nextval('qa.jobs_epoch_seq'),state='claimed',lease_until=clock_timestamp()+interval '2 minutes' WHERE project_id=$1 AND id=$2 RETURNING *`,
|
|
152
|
+
[row.project_id, row.id, owner]
|
|
153
|
+
);
|
|
154
|
+
await client.query("UPDATE qa.worker_slots SET project_id=$2,job_id=$3 WHERE id=$1", [
|
|
155
|
+
slot.rows[0].id,
|
|
156
|
+
row.project_id,
|
|
157
|
+
row.id
|
|
158
|
+
]);
|
|
159
|
+
signal?.throwIfAborted();
|
|
160
|
+
return {
|
|
161
|
+
...token(updated.rows[0]),
|
|
162
|
+
flow: detail.flow,
|
|
163
|
+
batchId: detail.batch_id,
|
|
164
|
+
createdBy: detail.created_by,
|
|
165
|
+
trigger: detail.trigger,
|
|
166
|
+
commit: detail.commit,
|
|
167
|
+
target: detail.target,
|
|
168
|
+
retryOf: row.retry_of,
|
|
169
|
+
request: request.data
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
signal?.throwIfAborted();
|
|
173
|
+
return void 0;
|
|
174
|
+
});
|
|
175
|
+
} catch (error) {
|
|
176
|
+
const code = error && typeof error === "object" && "code" in error ? error.code : void 0;
|
|
177
|
+
if (!["55P03", "40P01", "40001"].includes(String(code)) || attempt >= 2) throw error;
|
|
178
|
+
await delay(25 * 2 ** attempt, void 0, { signal });
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
async recordRevision(job, revision) {
|
|
183
|
+
this.scoped(job);
|
|
184
|
+
z.string().regex(/^[a-f0-9]{40,64}$/).parse(revision);
|
|
185
|
+
await this.db.transaction(async (client) => {
|
|
186
|
+
await lockJob(client, job);
|
|
187
|
+
await client.query("UPDATE qa.jobs SET verified_revision=$3 WHERE project_id=$1 AND id=$2", [
|
|
188
|
+
job.projectId,
|
|
189
|
+
job.jobId,
|
|
190
|
+
revision
|
|
191
|
+
]);
|
|
192
|
+
await client.query(
|
|
193
|
+
"UPDATE qa.runs SET metadata=jsonb_set(COALESCE(metadata,'{}'::jsonb),'{workerTargetRevision}',to_jsonb($3::text)) WHERE project_id=$1 AND id=$2",
|
|
194
|
+
[job.projectId, job.runId, revision]
|
|
195
|
+
);
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
async start(job) {
|
|
199
|
+
this.scoped(job);
|
|
200
|
+
return this.db.transaction(async (client) => {
|
|
201
|
+
const row = await lockJob(client, job);
|
|
202
|
+
if (row.state === "cancel-requested") return false;
|
|
203
|
+
if (row.state !== "claimed") throw new Error("Job already started");
|
|
204
|
+
await client.query("UPDATE qa.jobs SET state='running' WHERE project_id=$1 AND id=$2", [
|
|
205
|
+
job.projectId,
|
|
206
|
+
job.jobId
|
|
207
|
+
]);
|
|
208
|
+
return true;
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
async heartbeat(job) {
|
|
212
|
+
this.scoped(job);
|
|
213
|
+
return this.db.transaction(async (client) => {
|
|
214
|
+
const row = await lockJob(client, job);
|
|
215
|
+
await client.query(
|
|
216
|
+
"UPDATE qa.jobs SET lease_until=clock_timestamp()+interval '2 minutes' WHERE project_id=$1 AND id=$2",
|
|
217
|
+
[job.projectId, job.jobId]
|
|
218
|
+
);
|
|
219
|
+
return { cancelled: row.state === "cancel-requested" };
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
async finish(job, outcome) {
|
|
223
|
+
this.scoped(job);
|
|
224
|
+
verdictSchema.parse(outcome.verdict);
|
|
225
|
+
if (outcome.verifiedRevision != null)
|
|
226
|
+
z.string().regex(/^[a-f0-9]{40,64}$/).parse(outcome.verifiedRevision);
|
|
227
|
+
await this.complete(
|
|
228
|
+
job,
|
|
229
|
+
() => this.db.transaction(async (client) => {
|
|
230
|
+
const row = await lockJob(client, job);
|
|
231
|
+
const verdict = row.state === "cancel-requested" ? "canceled" : outcome.verdict;
|
|
232
|
+
await client.query(
|
|
233
|
+
`UPDATE qa.runs SET verdict=$3,phase='finished',finished_at=extract(epoch FROM clock_timestamp())*1000 WHERE project_id=$1 AND id=$2`,
|
|
234
|
+
[job.projectId, job.runId, verdict]
|
|
235
|
+
);
|
|
236
|
+
await client.query(
|
|
237
|
+
`UPDATE qa.jobs SET state='done',verified_revision=CASE WHEN $5 THEN $3 ELSE verified_revision END,infrastructure_failure=$4 WHERE project_id=$1 AND id=$2`,
|
|
238
|
+
[
|
|
239
|
+
job.projectId,
|
|
240
|
+
job.jobId,
|
|
241
|
+
outcome.verifiedRevision ?? null,
|
|
242
|
+
outcome.infrastructureFailure ? 1 : 0,
|
|
243
|
+
outcome.verifiedRevision !== void 0
|
|
244
|
+
]
|
|
245
|
+
);
|
|
246
|
+
if (outcome.cleanupConfirmed) return this.releaseLocked(client, job);
|
|
247
|
+
else await this.quarantine(client, job);
|
|
248
|
+
})
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
async release(job) {
|
|
252
|
+
this.scoped(job);
|
|
253
|
+
await this.complete(
|
|
254
|
+
job,
|
|
255
|
+
() => this.db.transaction(async (client) => {
|
|
256
|
+
await client.query("SELECT id FROM qa.jobs WHERE project_id=$1 AND id=$2 FOR UPDATE", [
|
|
257
|
+
job.projectId,
|
|
258
|
+
job.jobId
|
|
259
|
+
]);
|
|
260
|
+
const { rows } = await client.query(
|
|
261
|
+
"SELECT * FROM qa.jobs WHERE project_id=$1 AND id=$2 AND owner=$3 AND epoch=$4 AND run_id=$5 AND state='done' AND cleaned_at IS NOT NULL",
|
|
262
|
+
values(job)
|
|
263
|
+
);
|
|
264
|
+
if (!rows[0]) throw new Error("Stale job owner or epoch; cleanup confirmation required");
|
|
265
|
+
return this.releaseLocked(client, job);
|
|
266
|
+
})
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
async expire() {
|
|
270
|
+
return this.db.transaction(async (client) => {
|
|
271
|
+
const { rows } = await client.query(
|
|
272
|
+
`SELECT * FROM qa.jobs WHERE project_id=ANY($1::text[]) AND state IN ('claimed','running','cancel-requested') AND lease_until<=clock_timestamp() ORDER BY lease_until FOR UPDATE SKIP LOCKED LIMIT 100`,
|
|
273
|
+
[this.projects]
|
|
274
|
+
);
|
|
275
|
+
for (const row of rows) {
|
|
276
|
+
const job = token(row);
|
|
277
|
+
await client.query(
|
|
278
|
+
"UPDATE qa.jobs SET state='lost',epoch=nextval('qa.jobs_epoch_seq'),infrastructure_failure=1 WHERE project_id=$1 AND id=$2",
|
|
279
|
+
[job.projectId, job.jobId]
|
|
280
|
+
);
|
|
281
|
+
await client.query(
|
|
282
|
+
"UPDATE qa.runs SET verdict='error',reason='worker-lost',phase='finished',finished_at=extract(epoch FROM clock_timestamp())*1000 WHERE project_id=$1 AND id=$2",
|
|
283
|
+
[job.projectId, job.runId]
|
|
284
|
+
);
|
|
285
|
+
await this.quarantine(client, job);
|
|
286
|
+
}
|
|
287
|
+
const canceled = await client.query(
|
|
288
|
+
"UPDATE qa.jobs SET state='done',cleaned_at=clock_timestamp() WHERE project_id=ANY($1::text[]) AND state='cancel-requested' AND owner IS NULL RETURNING *",
|
|
289
|
+
[this.projects]
|
|
290
|
+
);
|
|
291
|
+
for (const row of canceled.rows)
|
|
292
|
+
await client.query(
|
|
293
|
+
"UPDATE qa.runs SET verdict='canceled',phase='finished',finished_at=extract(epoch FROM clock_timestamp())*1000 WHERE project_id=$1 AND id=$2",
|
|
294
|
+
[row.project_id, row.run_id]
|
|
295
|
+
);
|
|
296
|
+
return rows.length;
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
async pendingCleanup() {
|
|
300
|
+
await this.pruneClassificationNotifications();
|
|
301
|
+
const { rows } = await this.db.pool.query(
|
|
302
|
+
"SELECT * FROM qa.jobs WHERE project_id=ANY($1::text[]) AND state IN ('lost','done') AND cleaned_at IS NULL ORDER BY available_at LIMIT 100",
|
|
303
|
+
[this.projects]
|
|
304
|
+
);
|
|
305
|
+
return rows.map(token);
|
|
306
|
+
}
|
|
307
|
+
async recover(job, stopAndRevoke) {
|
|
308
|
+
this.scoped(job);
|
|
309
|
+
await this.complete(job, async () => {
|
|
310
|
+
const recovery = await this.db.pool.query(
|
|
311
|
+
`UPDATE qa.jobs SET epoch=nextval('qa.jobs_epoch_seq') WHERE project_id=$1 AND id=$2 AND owner=$3 AND epoch=$4 AND run_id=$5 AND state IN ('lost','done') AND cleaned_at IS NULL RETURNING *`,
|
|
312
|
+
values(job)
|
|
313
|
+
);
|
|
314
|
+
if (!recovery.rows[0]) throw new Error("Stale job recovery token");
|
|
315
|
+
const current = token(recovery.rows[0]);
|
|
316
|
+
await stopAndRevoke(current);
|
|
317
|
+
const completion = await this.db.transaction(async (client) => {
|
|
318
|
+
await client.query("SELECT id FROM qa.jobs WHERE project_id=$1 AND id=$2 FOR UPDATE", [
|
|
319
|
+
job.projectId,
|
|
320
|
+
job.jobId
|
|
321
|
+
]);
|
|
322
|
+
const check = await client.query(
|
|
323
|
+
"SELECT id FROM qa.jobs WHERE project_id=$1 AND id=$2 AND owner=$3 AND epoch=$4 AND run_id=$5 AND state IN ('lost','done') AND cleaned_at IS NULL",
|
|
324
|
+
values(current)
|
|
325
|
+
);
|
|
326
|
+
if (!check.rowCount) throw new Error("Stale job recovery token");
|
|
327
|
+
await client.query(
|
|
328
|
+
"DELETE FROM qa.lease_claims WHERE project_id=$1 AND lease_id IN (SELECT id FROM qa.leases WHERE project_id=$1 AND run_id=$2 AND state='quarantined')",
|
|
329
|
+
[job.projectId, job.runId]
|
|
330
|
+
);
|
|
331
|
+
await client.query(
|
|
332
|
+
"UPDATE qa.leases SET state='released' WHERE project_id=$1 AND run_id=$2 AND state='quarantined'",
|
|
333
|
+
[job.projectId, job.runId]
|
|
334
|
+
);
|
|
335
|
+
return this.releaseLocked(client, current);
|
|
336
|
+
});
|
|
337
|
+
return completion;
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
async quarantine(client, job) {
|
|
341
|
+
await client.query(
|
|
342
|
+
"UPDATE qa.leases SET state='quarantined',epoch=nextval('qa.leases_epoch_seq') WHERE project_id=$1 AND run_id=$2 AND state='active'",
|
|
343
|
+
[job.projectId, job.runId]
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
async releaseLocked(client, job) {
|
|
347
|
+
const outstanding = await client.query(
|
|
348
|
+
"SELECT id FROM qa.leases WHERE project_id=$1 AND run_id=$2 AND state<>'released' LIMIT 1",
|
|
349
|
+
[job.projectId, job.runId]
|
|
350
|
+
);
|
|
351
|
+
if (outstanding.rowCount) throw new Error("Job resources still require confirmed cleanup");
|
|
352
|
+
await client.query(
|
|
353
|
+
"UPDATE qa.jobs SET cleaned_at=clock_timestamp() WHERE project_id=$1 AND id=$2",
|
|
354
|
+
[job.projectId, job.jobId]
|
|
355
|
+
);
|
|
356
|
+
await client.query(
|
|
357
|
+
"UPDATE qa.worker_slots SET project_id=NULL,job_id=NULL WHERE project_id=$1 AND job_id=$2",
|
|
358
|
+
[job.projectId, job.jobId]
|
|
359
|
+
);
|
|
360
|
+
const { rows } = await client.query(
|
|
361
|
+
"SELECT j.attempts,j.retry_of,j.verified_revision,r.verdict,(s.response->'request'->>'retries')::int AS retries FROM qa.jobs j JOIN qa.runs r ON r.project_id=j.project_id AND r.id=j.run_id JOIN qa.server_submissions s ON s.project_id=r.project_id AND s.batch_id=r.batch_id WHERE j.project_id=$1 AND j.id=$2",
|
|
362
|
+
[job.projectId, job.jobId]
|
|
363
|
+
);
|
|
364
|
+
const row = rows[0];
|
|
365
|
+
if (row.attempts === 0 && row.retries !== 0 && ["failed", "error"].includes(row.verdict)) {
|
|
366
|
+
const existing = await client.query(
|
|
367
|
+
"SELECT id FROM qa.jobs WHERE project_id=$1 AND retry_of=$2",
|
|
368
|
+
[job.projectId, job.runId]
|
|
369
|
+
);
|
|
370
|
+
if (!existing.rowCount) {
|
|
371
|
+
const id = randomUUID();
|
|
372
|
+
await client.query(
|
|
373
|
+
"INSERT INTO qa.runs(project_id,id,batch_id,flow,started_at,metadata) SELECT project_id,$3,batch_id,flow,extract(epoch FROM clock_timestamp())*1000,jsonb_build_object('retryOf',id) FROM qa.runs WHERE project_id=$1 AND id=$2",
|
|
374
|
+
[job.projectId, job.runId, id]
|
|
375
|
+
);
|
|
376
|
+
await client.query(
|
|
377
|
+
"INSERT INTO qa.jobs(project_id,id,run_id,attempts,retry_of) VALUES($1,$2,$3,1,$4)",
|
|
378
|
+
[job.projectId, randomUUID(), id, job.runId]
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
return { runIds: [], quarantined: true };
|
|
382
|
+
}
|
|
383
|
+
if (row.retry_of && row.verdict === "passed" && row.verified_revision) {
|
|
384
|
+
const original = await client.query(
|
|
385
|
+
"SELECT infrastructure_failure FROM qa.jobs WHERE project_id=$1 AND run_id=$2 AND verified_revision=$3",
|
|
386
|
+
[job.projectId, row.retry_of, row.verified_revision]
|
|
387
|
+
);
|
|
388
|
+
if (original.rows[0]) {
|
|
389
|
+
await client.query("SELECT id FROM qa.runs WHERE project_id=$1 AND id=$2 FOR UPDATE", [
|
|
390
|
+
job.projectId,
|
|
391
|
+
row.retry_of
|
|
392
|
+
]);
|
|
393
|
+
const classification = await classifyFailure(
|
|
394
|
+
{
|
|
395
|
+
infrastructureFailure: !!original.rows[0].infrastructure_failure,
|
|
396
|
+
sameVersionRetryPassed: true,
|
|
397
|
+
reviews: [],
|
|
398
|
+
gitDiff: null,
|
|
399
|
+
failure: ""
|
|
400
|
+
},
|
|
401
|
+
{
|
|
402
|
+
classify: async () => {
|
|
403
|
+
throw new Error("Retry classification does not use a model");
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
);
|
|
407
|
+
await client.query(
|
|
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)]
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
const final = await client.query(
|
|
414
|
+
`SELECT r.flow,r.verdict,COALESCE(original.classification,r.classification) AS category,
|
|
415
|
+
r.metadata->>'flowOwner' AS owner FROM qa.runs r LEFT JOIN qa.runs original
|
|
416
|
+
ON original.project_id=r.project_id AND original.id=$3 WHERE r.project_id=$1 AND r.id=$2`,
|
|
417
|
+
[job.projectId, job.runId, row.retry_of]
|
|
418
|
+
);
|
|
419
|
+
const run = final.rows[0];
|
|
420
|
+
const parsed = failureCategorySchema.safeParse(run.category);
|
|
421
|
+
const state = await new PgFlowStatus(this.db, job.projectId).recordInTransaction(client, {
|
|
422
|
+
flow: run.flow,
|
|
423
|
+
executionId: job.runId,
|
|
424
|
+
verdict: run.verdict,
|
|
425
|
+
category: parsed.success ? parsed.data : null,
|
|
426
|
+
owner: run.owner ?? void 0
|
|
427
|
+
});
|
|
428
|
+
return {
|
|
429
|
+
runIds: row.retry_of ? [row.retry_of, job.runId] : [job.runId],
|
|
430
|
+
quarantined: state.quarantined
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
export {
|
|
435
|
+
JOB_LEASE_MS,
|
|
436
|
+
PgJobQueue,
|
|
437
|
+
lockJob
|
|
438
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { QaDatabase } from "../db/database.js";
|
|
2
|
+
import { type ResultContext, type ResultTransports } from "../result-notifier.js";
|
|
3
|
+
export interface ResultNotificationOptions {
|
|
4
|
+
projects: readonly string[];
|
|
5
|
+
transports: (projectId: string) => ResultTransports;
|
|
6
|
+
onError?: (message: string) => void;
|
|
7
|
+
presentation: (projectId: string, batchId: string, runIds: readonly string[]) => Promise<{
|
|
8
|
+
publicOrigins: readonly string[];
|
|
9
|
+
runs: Readonly<Record<string, {
|
|
10
|
+
reportUrl?: string;
|
|
11
|
+
replayUrl?: string;
|
|
12
|
+
}>>;
|
|
13
|
+
}>;
|
|
14
|
+
}
|
|
15
|
+
export declare const RESULT_DELIVERY_MAX_ATTEMPTS = 5;
|
|
16
|
+
export declare const RESULT_DELIVERY_INITIAL_BACKOFF_SECONDS: number;
|
|
17
|
+
export declare const RESULT_DELIVERY_RETRY_WINDOW_SECONDS: number;
|
|
18
|
+
export declare class PgResultNotifications {
|
|
19
|
+
private readonly db;
|
|
20
|
+
private readonly options;
|
|
21
|
+
constructor(db: QaDatabase, options: ResultNotificationOptions);
|
|
22
|
+
enqueue(projectId: string, batchId: string, context: ResultContext): Promise<void>;
|
|
23
|
+
private notifyRuns;
|
|
24
|
+
private summarizeFlows;
|
|
25
|
+
private drainNewFlows;
|
|
26
|
+
private reportFailure;
|
|
27
|
+
drain(): Promise<void>;
|
|
28
|
+
}
|