@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,237 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { open, readdir, rmdir } from "node:fs/promises";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { join, resolve } from "node:path";
|
|
5
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
import { jsonObjectSchema } from "@open-qa/qa-engine/protocol";
|
|
8
|
+
import {
|
|
9
|
+
acquireLock,
|
|
10
|
+
assertDirectory,
|
|
11
|
+
hasCode,
|
|
12
|
+
lockConflict,
|
|
13
|
+
lockName,
|
|
14
|
+
noFollowRead,
|
|
15
|
+
releaseLock
|
|
16
|
+
} from "./local-files.js";
|
|
17
|
+
import { leaseTransaction } from "./lease-transaction.js";
|
|
18
|
+
import { readLocalEnv } from "./local-env.js";
|
|
19
|
+
import { parseResourcePools } from "./resource-pools.js";
|
|
20
|
+
function canonical(value) {
|
|
21
|
+
if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
|
|
22
|
+
if (value !== null && typeof value === "object")
|
|
23
|
+
return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${canonical(value[key])}`).join(",")}}`;
|
|
24
|
+
return JSON.stringify(value);
|
|
25
|
+
}
|
|
26
|
+
const timing = z.number().int().positive().max(2147483647);
|
|
27
|
+
const requestSchema = z.object({
|
|
28
|
+
actor: z.string().min(1),
|
|
29
|
+
kind: z.string().min(1),
|
|
30
|
+
count: z.number().int().positive().max(100).default(1)
|
|
31
|
+
});
|
|
32
|
+
const expirySchema = z.object({ expiresAt: z.number().finite().nonnegative() });
|
|
33
|
+
class FixedLeasePool {
|
|
34
|
+
qaRoot;
|
|
35
|
+
lockRoot;
|
|
36
|
+
constructor(qaRoot = ".qa", lockRoot = join(homedir(), ".open-qa", "locks")) {
|
|
37
|
+
this.qaRoot = resolve(qaRoot);
|
|
38
|
+
this.lockRoot = resolve(lockRoot);
|
|
39
|
+
}
|
|
40
|
+
async acquire(spec, signal) {
|
|
41
|
+
signal?.throwIfAborted();
|
|
42
|
+
const requests = z.array(requestSchema).parse(spec.requests ?? []);
|
|
43
|
+
const ttlMs = timing.parse(spec.ttlMs ?? 60 * 6e4);
|
|
44
|
+
const timeoutMs = z.number().int().nonnegative().max(2147483647).parse(spec.timeoutMs ?? (requests.length ? 10 * 6e4 : 0));
|
|
45
|
+
const id = randomUUID();
|
|
46
|
+
const names = [...new Set(spec.resources)].sort();
|
|
47
|
+
if (!names.length && !requests.length)
|
|
48
|
+
return {
|
|
49
|
+
id,
|
|
50
|
+
runId: spec.runId,
|
|
51
|
+
actor: spec.actor,
|
|
52
|
+
resources: {},
|
|
53
|
+
expiresAt: Date.now() + ttlMs
|
|
54
|
+
};
|
|
55
|
+
const env = await readLocalEnv(this.qaRoot);
|
|
56
|
+
let configured = {};
|
|
57
|
+
if (names.length) {
|
|
58
|
+
try {
|
|
59
|
+
configured = jsonObjectSchema.parse(JSON.parse(env.QA_RESOURCES ?? ""));
|
|
60
|
+
} catch {
|
|
61
|
+
throw new Error(
|
|
62
|
+
`Invalid QA_RESOURCES in ${join(this.qaRoot, ".env.local")}; expected a JSON object keyed by exclusive resource name`
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const slots = names.map((resource) => {
|
|
67
|
+
if (!Object.hasOwn(configured, resource))
|
|
68
|
+
throw new Error(
|
|
69
|
+
`Requested QA resource ${JSON.stringify(resource)} is not configured in ${join(this.qaRoot, ".env.local")}`
|
|
70
|
+
);
|
|
71
|
+
return [{ resource, value: configured[resource], actor: spec.actor }];
|
|
72
|
+
});
|
|
73
|
+
const pools = requests.length ? parseResourcePools(env.QA_RESOURCE_POOLS) : {};
|
|
74
|
+
const counts = /* @__PURE__ */ new Map();
|
|
75
|
+
for (const request of requests) {
|
|
76
|
+
const pool = Object.hasOwn(pools, request.kind) ? pools[request.kind] : void 0;
|
|
77
|
+
if (!pool)
|
|
78
|
+
throw new Error(
|
|
79
|
+
`Requested QA resource kind ${JSON.stringify(request.kind)} is not configured in QA_RESOURCE_POOLS`
|
|
80
|
+
);
|
|
81
|
+
const count = (counts.get(request.kind) ?? 0) + request.count;
|
|
82
|
+
counts.set(request.kind, count);
|
|
83
|
+
if (Object.keys(pool).length < count)
|
|
84
|
+
throw new Error(`Insufficient QA pool capacity for ${request.kind}`);
|
|
85
|
+
for (let i = 0; i < request.count; i++)
|
|
86
|
+
slots.push(
|
|
87
|
+
Object.entries(pool).sort(([a], [b]) => a.localeCompare(b)).map(([resource, value]) => ({
|
|
88
|
+
resource,
|
|
89
|
+
value,
|
|
90
|
+
actor: request.actor,
|
|
91
|
+
kind: request.kind
|
|
92
|
+
}))
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
slots.sort((a, b) => a.length - b.length);
|
|
96
|
+
const deadline = Date.now() + timeoutMs;
|
|
97
|
+
let conflict = "";
|
|
98
|
+
let retryDelayMs = 50;
|
|
99
|
+
for (; ; ) {
|
|
100
|
+
signal?.throwIfAborted();
|
|
101
|
+
const result = await leaseTransaction(
|
|
102
|
+
this.lockRoot,
|
|
103
|
+
async () => {
|
|
104
|
+
const expiresAt = Date.now() + ttlMs;
|
|
105
|
+
const selected = [];
|
|
106
|
+
const held = /* @__PURE__ */ new Set();
|
|
107
|
+
const take = async (index) => {
|
|
108
|
+
if (index === slots.length) return true;
|
|
109
|
+
for (const candidate of slots[index]) {
|
|
110
|
+
signal?.throwIfAborted();
|
|
111
|
+
if (timeoutMs > 0 && Date.now() >= deadline)
|
|
112
|
+
throw new Error(
|
|
113
|
+
"lease-timeout: QA resources were not available before the acquisition deadline"
|
|
114
|
+
);
|
|
115
|
+
const previous = selected.findLast(
|
|
116
|
+
(item) => item.kind !== void 0 && item.kind === candidate.kind
|
|
117
|
+
);
|
|
118
|
+
if (previous && candidate.resource.localeCompare(previous.resource) <= 0) continue;
|
|
119
|
+
const directories = this.directories(candidate.resource, candidate.value);
|
|
120
|
+
if (directories.some((directory) => held.has(directory))) continue;
|
|
121
|
+
const acquired = [];
|
|
122
|
+
let available = true;
|
|
123
|
+
try {
|
|
124
|
+
for (const directory of directories) {
|
|
125
|
+
await this.reapExpired(directory);
|
|
126
|
+
if (!await acquireLock(directory, id, {
|
|
127
|
+
runId: spec.runId,
|
|
128
|
+
resource: candidate.resource,
|
|
129
|
+
expiresAt
|
|
130
|
+
})) {
|
|
131
|
+
conflict = `Requested QA resource ${JSON.stringify(candidate.resource)} is already leased. ${await lockConflict(directory)}`;
|
|
132
|
+
available = false;
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
acquired.push(directory);
|
|
136
|
+
held.add(directory);
|
|
137
|
+
}
|
|
138
|
+
if (available) {
|
|
139
|
+
selected.push(candidate);
|
|
140
|
+
if (await take(index + 1)) return true;
|
|
141
|
+
selected.pop();
|
|
142
|
+
}
|
|
143
|
+
} catch (error) {
|
|
144
|
+
for (const directory of acquired.reverse()) {
|
|
145
|
+
held.delete(directory);
|
|
146
|
+
await releaseLock(directory, id);
|
|
147
|
+
}
|
|
148
|
+
throw error;
|
|
149
|
+
}
|
|
150
|
+
for (const directory of acquired.reverse()) {
|
|
151
|
+
held.delete(directory);
|
|
152
|
+
await releaseLock(directory, id);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return false;
|
|
156
|
+
};
|
|
157
|
+
try {
|
|
158
|
+
if (!await take(0)) return void 0;
|
|
159
|
+
signal?.throwIfAborted();
|
|
160
|
+
return {
|
|
161
|
+
id,
|
|
162
|
+
runId: spec.runId,
|
|
163
|
+
actor: spec.actor,
|
|
164
|
+
expiresAt,
|
|
165
|
+
resources: Object.fromEntries(
|
|
166
|
+
selected.filter((c) => !c.kind).map((c) => [c.resource, c.value])
|
|
167
|
+
),
|
|
168
|
+
allocations: selected.filter(
|
|
169
|
+
(c) => c.kind !== void 0
|
|
170
|
+
).map((c) => ({ ...c }))
|
|
171
|
+
};
|
|
172
|
+
} catch (error) {
|
|
173
|
+
for (const directory of held) await releaseLock(directory, id);
|
|
174
|
+
throw error;
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
signal,
|
|
178
|
+
requests.length || timeoutMs > 0 ? deadline : void 0
|
|
179
|
+
);
|
|
180
|
+
if (result) return result;
|
|
181
|
+
if (!requests.length && timeoutMs === 0) throw new Error(conflict);
|
|
182
|
+
if (Date.now() >= deadline)
|
|
183
|
+
throw new Error(
|
|
184
|
+
"lease-timeout: QA resources were not available before the acquisition deadline"
|
|
185
|
+
);
|
|
186
|
+
await delay(Math.min(retryDelayMs, Math.max(1, deadline - Date.now())), void 0, {
|
|
187
|
+
signal
|
|
188
|
+
});
|
|
189
|
+
retryDelayMs = Math.min(retryDelayMs * 2, 1e3);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
async release(lease) {
|
|
193
|
+
if (!/^[a-f0-9-]{36}$/.test(lease.id)) throw new Error("Invalid local lease ID");
|
|
194
|
+
await leaseTransaction(this.lockRoot, async () => {
|
|
195
|
+
const resources = [
|
|
196
|
+
...Object.entries(lease.resources).map(([resource, value]) => ({ resource, value })),
|
|
197
|
+
...lease.allocations ?? []
|
|
198
|
+
];
|
|
199
|
+
for (const { resource, value } of resources)
|
|
200
|
+
for (const directory of this.directories(resource, value))
|
|
201
|
+
await releaseLock(directory, lease.id);
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
async reapExpired(directory) {
|
|
205
|
+
let owners;
|
|
206
|
+
try {
|
|
207
|
+
await assertDirectory(directory);
|
|
208
|
+
owners = await readdir(directory);
|
|
209
|
+
} catch (error) {
|
|
210
|
+
if (hasCode(error, "ENOENT")) return;
|
|
211
|
+
throw error;
|
|
212
|
+
}
|
|
213
|
+
if (!owners.length) {
|
|
214
|
+
await rmdir(directory);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (owners.length !== 1 || !/^[a-f0-9-]{36}$/.test(owners[0])) return;
|
|
218
|
+
const file = await open(join(directory, owners[0]), noFollowRead);
|
|
219
|
+
try {
|
|
220
|
+
const parsed = expirySchema.safeParse(JSON.parse(await file.readFile("utf8")));
|
|
221
|
+
if (parsed.success && parsed.data.expiresAt <= Date.now())
|
|
222
|
+
await releaseLock(directory, owners[0]);
|
|
223
|
+
} catch (error) {
|
|
224
|
+
if (!(error instanceof SyntaxError)) throw error;
|
|
225
|
+
} finally {
|
|
226
|
+
await file.close();
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
directories(resource, value) {
|
|
230
|
+
return ["name-" + lockName(resource), "value-" + lockName(canonical(value))].map(
|
|
231
|
+
(key) => join(this.lockRoot, "leases", key)
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
export {
|
|
236
|
+
FixedLeasePool
|
|
237
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type ArtifactStore, type SecretRedactor } from "@open-qa/qa-engine";
|
|
2
|
+
import { type Artifact, type FlushArtifactsRequest } from "@open-qa/qa-engine/protocol";
|
|
3
|
+
import { type GcsClient } from "./client.js";
|
|
4
|
+
import type { ArtifactScan } from "../../artifact-scan.js";
|
|
5
|
+
export declare const UPLOAD_TTL_MS: number;
|
|
6
|
+
export declare const REPORT_TTL_MS: number;
|
|
7
|
+
export declare const RETENTION_MS: number;
|
|
8
|
+
export interface ArtifactRun {
|
|
9
|
+
batchId: string;
|
|
10
|
+
startedAt: number;
|
|
11
|
+
keep: "all" | "failed";
|
|
12
|
+
verdict: string | null;
|
|
13
|
+
scanned: Record<string, string>;
|
|
14
|
+
publishedScanned: Record<string, string>;
|
|
15
|
+
}
|
|
16
|
+
export interface StoredArtifact extends Artifact {
|
|
17
|
+
objectKey: string;
|
|
18
|
+
sha256: string;
|
|
19
|
+
}
|
|
20
|
+
export interface ArtifactCatalog {
|
|
21
|
+
run(runId: string): Promise<ArtifactRun | undefined>;
|
|
22
|
+
artifact(runId: string, path: string): Promise<StoredArtifact | undefined>;
|
|
23
|
+
commit(runId: string, artifacts: StoredArtifact[]): Promise<void>;
|
|
24
|
+
clear(runId: string): Promise<void>;
|
|
25
|
+
recordScan(runId: string, scan: ArtifactScan): Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
export declare function recordingFreeReport(runId: string): Uint8Array;
|
|
28
|
+
export declare class GcsArtifactStore implements ArtifactStore {
|
|
29
|
+
private readonly client;
|
|
30
|
+
private readonly projectId;
|
|
31
|
+
private readonly catalog;
|
|
32
|
+
private readonly now;
|
|
33
|
+
constructor(client: GcsClient, projectId: string, catalog: ArtifactCatalog, now?: () => number);
|
|
34
|
+
private run;
|
|
35
|
+
private key;
|
|
36
|
+
private verify;
|
|
37
|
+
put(runId: string, input: Artifact, data: Uint8Array): Promise<void>;
|
|
38
|
+
uploadDestination(runId: string, snapshot: string, paths: string[]): Promise<FlushArtifactsRequest["destination"]>;
|
|
39
|
+
commitUpload(runId: string, snapshot: string, inputs: Artifact[]): Promise<void>;
|
|
40
|
+
scanUpload(runId: string, snapshot: string, inputs: Artifact[], redactor: SecretRedactor, scratch: string): Promise<void>;
|
|
41
|
+
private verifyObject;
|
|
42
|
+
signedUrl(runId: string, path: string, expiresInMs?: number): Promise<string>;
|
|
43
|
+
reportUrl(runId: string): Promise<string>;
|
|
44
|
+
/** Call after committing the passed verdict/keep policy, before publishing the safe report.
|
|
45
|
+
* Empty generations revoke old GET contents and prevent late signed PUTs from resurrecting them.
|
|
46
|
+
* On failure retry this operation; no download is authorized while the policy disallows it.
|
|
47
|
+
*/
|
|
48
|
+
pruneRecordings(runId: string): Promise<void>;
|
|
49
|
+
}
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { ARTIFACT_NAMESPACES } from "@open-qa/qa-engine";
|
|
2
|
+
import {
|
|
3
|
+
artifactSchema
|
|
4
|
+
} from "@open-qa/qa-engine/protocol";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { sha256 } from "../../db/event-chunks.js";
|
|
7
|
+
import { publicId } from "../../public-id.js";
|
|
8
|
+
import { errorCode, objectPath } from "./client.js";
|
|
9
|
+
import { scanUpload } from "./scan-upload.js";
|
|
10
|
+
import { assertReplayComplete } from "./replay-completeness.js";
|
|
11
|
+
const UPLOAD_TTL_MS = 60 * 60 * 1e3;
|
|
12
|
+
const REPORT_TTL_MS = 7 * 24 * 60 * 60 * 1e3;
|
|
13
|
+
const RETENTION_MS = 30 * 24 * 60 * 60 * 1e3;
|
|
14
|
+
function emptyReport(runId, title) {
|
|
15
|
+
publicId.parse(runId);
|
|
16
|
+
return Buffer.from(
|
|
17
|
+
`<!doctype html><html lang="en"><meta charset="utf-8"><meta http-equiv="Content-Security-Policy" content="default-src 'none'; base-uri 'none'; form-action 'none'"><title>${title}</title><h1>Run ${runId}: passed</h1><p>Recording removed by keep: failed.</p></html>`
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
function recordingFreeReport(runId) {
|
|
21
|
+
return emptyReport(runId, "open-qa report");
|
|
22
|
+
}
|
|
23
|
+
function isRecordingFreeReport(runId, hash) {
|
|
24
|
+
return sha256(recordingFreeReport(runId)) === hash;
|
|
25
|
+
}
|
|
26
|
+
function removed(run) {
|
|
27
|
+
return run.keep === "failed" && run.verdict === "passed";
|
|
28
|
+
}
|
|
29
|
+
function allowed(run, path) {
|
|
30
|
+
objectPath(path);
|
|
31
|
+
if (path === "run.json" || path.startsWith("run.json/") || /^uploads-/.test(path) || /\.(mp4|zip)$/i.test(path))
|
|
32
|
+
throw new Error("Reserved artifact path");
|
|
33
|
+
if (removed(run) && path !== "report.html")
|
|
34
|
+
throw new Error("Recording removed by retention policy");
|
|
35
|
+
}
|
|
36
|
+
class GcsArtifactStore {
|
|
37
|
+
constructor(client, projectId, catalog, now = Date.now) {
|
|
38
|
+
this.client = client;
|
|
39
|
+
this.projectId = projectId;
|
|
40
|
+
this.catalog = catalog;
|
|
41
|
+
this.now = now;
|
|
42
|
+
publicId.parse(projectId);
|
|
43
|
+
}
|
|
44
|
+
client;
|
|
45
|
+
projectId;
|
|
46
|
+
catalog;
|
|
47
|
+
now;
|
|
48
|
+
async run(runId) {
|
|
49
|
+
publicId.parse(runId);
|
|
50
|
+
const run = await this.catalog.run(runId);
|
|
51
|
+
if (!run) throw new Error("Artifact run scope denied");
|
|
52
|
+
publicId.parse(run.batchId);
|
|
53
|
+
if (!Number.isFinite(run.startedAt) || this.now() >= run.startedAt + RETENTION_MS)
|
|
54
|
+
throw new Error("Artifact retention expired");
|
|
55
|
+
return run;
|
|
56
|
+
}
|
|
57
|
+
key(runId, run, snapshot, path) {
|
|
58
|
+
publicId.parse(snapshot);
|
|
59
|
+
objectPath(path);
|
|
60
|
+
return `open-qa-runs/${this.projectId}/${run.batchId}/${runId}/${snapshot}/${path}`;
|
|
61
|
+
}
|
|
62
|
+
verify(runId, run, artifact, data) {
|
|
63
|
+
allowed(run, artifact.path);
|
|
64
|
+
if (data.byteLength !== artifact.bytes || data.byteLength > 64 * 1024 * 1024)
|
|
65
|
+
throw new Error("Artifact byte count does not match data or exceeds limit");
|
|
66
|
+
const hash = sha256(data);
|
|
67
|
+
if (run.scanned[artifact.path] !== hash)
|
|
68
|
+
throw new Error("Artifact has no matching secret scan");
|
|
69
|
+
if (removed(run) && !isRecordingFreeReport(runId, hash))
|
|
70
|
+
throw new Error("Report must exclude removed recordings");
|
|
71
|
+
return hash;
|
|
72
|
+
}
|
|
73
|
+
async put(runId, input, data) {
|
|
74
|
+
const artifact = artifactSchema.parse(input);
|
|
75
|
+
const run = await this.run(runId);
|
|
76
|
+
const hash = this.verify(runId, run, artifact, data);
|
|
77
|
+
const objectKey = this.key(runId, run, hash, artifact.path);
|
|
78
|
+
try {
|
|
79
|
+
await this.client.write(objectKey, data);
|
|
80
|
+
} catch (error) {
|
|
81
|
+
if (errorCode(error) !== 412) throw new Error("GCS artifact upload failed");
|
|
82
|
+
await this.verifyObject(runId, run, { ...artifact, objectKey, sha256: hash });
|
|
83
|
+
}
|
|
84
|
+
await this.catalog.commit(runId, [{ ...artifact, objectKey, sha256: hash }]);
|
|
85
|
+
}
|
|
86
|
+
async uploadDestination(runId, snapshot, paths) {
|
|
87
|
+
const run = await this.run(runId);
|
|
88
|
+
if (removed(run)) throw new Error("Recording removed by retention policy");
|
|
89
|
+
const urls = {};
|
|
90
|
+
if (paths.length > 1e4 || new Set(paths).size !== paths.length)
|
|
91
|
+
throw new Error("Invalid artifact upload inventory");
|
|
92
|
+
const keys = paths.map((path) => {
|
|
93
|
+
allowed(run, path);
|
|
94
|
+
return this.key(runId, run, snapshot, path);
|
|
95
|
+
});
|
|
96
|
+
const reservation = Buffer.from(JSON.stringify(keys));
|
|
97
|
+
const reservationKey = this.key(runId, run, snapshot, `uploads-${sha256(reservation)}.json`);
|
|
98
|
+
try {
|
|
99
|
+
await this.client.write(reservationKey, reservation);
|
|
100
|
+
} catch (error) {
|
|
101
|
+
if (errorCode(error) !== 412) throw new Error("GCS upload reservation failed");
|
|
102
|
+
}
|
|
103
|
+
if (removed(await this.run(runId))) throw new Error("Recording removed by retention policy");
|
|
104
|
+
for (const path of paths) {
|
|
105
|
+
allowed(run, path);
|
|
106
|
+
const object = this.key(runId, run, snapshot, path);
|
|
107
|
+
try {
|
|
108
|
+
urls[path] = await this.client.sign(object, {
|
|
109
|
+
version: "v4",
|
|
110
|
+
action: "write",
|
|
111
|
+
expires: Math.min(this.now() + UPLOAD_TTL_MS, run.startedAt + RETENTION_MS),
|
|
112
|
+
contentType: "application/octet-stream",
|
|
113
|
+
extensionHeaders: {
|
|
114
|
+
"x-goog-if-generation-match": "0",
|
|
115
|
+
"Cache-Control": "private, no-store"
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
} catch {
|
|
119
|
+
throw new Error("GCS artifact signing failed");
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return { type: "signed-urls", urls, snapshot };
|
|
123
|
+
}
|
|
124
|
+
async commitUpload(runId, snapshot, inputs) {
|
|
125
|
+
const run = await this.run(runId);
|
|
126
|
+
const artifacts = [];
|
|
127
|
+
const files = /* @__PURE__ */ new Map();
|
|
128
|
+
if (new Set(inputs.map((a) => a.path)).size !== inputs.length)
|
|
129
|
+
throw new Error("Duplicate artifact path");
|
|
130
|
+
const validated = z.array(artifactSchema).max(1e4).parse(inputs);
|
|
131
|
+
if (validated.reduce((total, artifact) => total + artifact.bytes, 0) > 256 * 1024 * 1024)
|
|
132
|
+
throw new Error("Artifact snapshot exceeds limit");
|
|
133
|
+
for (const artifact of validated) {
|
|
134
|
+
const stored = {
|
|
135
|
+
...artifact,
|
|
136
|
+
objectKey: this.key(runId, run, snapshot, artifact.path),
|
|
137
|
+
sha256: run.scanned[artifact.path] ?? ""
|
|
138
|
+
};
|
|
139
|
+
files.set(artifact.path, await this.verifyObject(runId, run, stored));
|
|
140
|
+
artifacts.push(stored);
|
|
141
|
+
}
|
|
142
|
+
assertReplayComplete(files);
|
|
143
|
+
await this.catalog.commit(runId, artifacts);
|
|
144
|
+
}
|
|
145
|
+
async scanUpload(runId, snapshot, inputs, redactor, scratch) {
|
|
146
|
+
const run = await this.run(runId);
|
|
147
|
+
const artifacts = z.array(artifactSchema).max(1e4).parse(inputs);
|
|
148
|
+
if (new Set(artifacts.map((a) => a.path)).size !== artifacts.length)
|
|
149
|
+
throw new Error("Duplicate artifact path");
|
|
150
|
+
for (const artifact of artifacts) {
|
|
151
|
+
allowed(run, artifact.path);
|
|
152
|
+
this.key(runId, run, snapshot, artifact.path);
|
|
153
|
+
}
|
|
154
|
+
let scan;
|
|
155
|
+
try {
|
|
156
|
+
scan = await scanUpload(
|
|
157
|
+
runId,
|
|
158
|
+
artifacts,
|
|
159
|
+
redactor,
|
|
160
|
+
scratch,
|
|
161
|
+
(artifact) => this.client.read(this.key(runId, run, snapshot, artifact.path))
|
|
162
|
+
);
|
|
163
|
+
} catch {
|
|
164
|
+
throw new Error("Artifact scan unavailable");
|
|
165
|
+
}
|
|
166
|
+
await this.catalog.recordScan(runId, scan);
|
|
167
|
+
if (scan.status !== "passed") throw new Error("Artifact sharing blocked");
|
|
168
|
+
}
|
|
169
|
+
async verifyObject(runId, run, artifact) {
|
|
170
|
+
let data;
|
|
171
|
+
try {
|
|
172
|
+
data = await this.client.read(artifact.objectKey);
|
|
173
|
+
} catch {
|
|
174
|
+
throw new Error("GCS artifact unavailable");
|
|
175
|
+
}
|
|
176
|
+
if (this.verify(runId, run, artifact, data) !== artifact.sha256)
|
|
177
|
+
throw new Error("Artifact digest mismatch");
|
|
178
|
+
return data;
|
|
179
|
+
}
|
|
180
|
+
async signedUrl(runId, path, expiresInMs = REPORT_TTL_MS) {
|
|
181
|
+
if (!Number.isSafeInteger(expiresInMs) || expiresInMs <= 0 || expiresInMs > REPORT_TTL_MS)
|
|
182
|
+
throw new Error("Artifact URL expiry must be at most seven days");
|
|
183
|
+
const run = await this.run(runId);
|
|
184
|
+
allowed(run, path);
|
|
185
|
+
const artifact = await this.catalog.artifact(runId, path);
|
|
186
|
+
if (!artifact || artifact.path !== path) throw new Error("Artifact unavailable");
|
|
187
|
+
const prefixes = ARTIFACT_NAMESPACES.map(
|
|
188
|
+
(namespace) => `${namespace}/${this.projectId}/${run.batchId}/${runId}/`
|
|
189
|
+
);
|
|
190
|
+
objectPath(artifact.objectKey);
|
|
191
|
+
if (!prefixes.some((prefix) => artifact.objectKey.startsWith(prefix)) || !artifact.objectKey.endsWith(`/${path}`))
|
|
192
|
+
throw new Error("Artifact object scope denied");
|
|
193
|
+
await this.verifyObject(runId, { ...run, scanned: run.publishedScanned }, artifact);
|
|
194
|
+
try {
|
|
195
|
+
return await this.client.sign(artifact.objectKey, {
|
|
196
|
+
version: "v4",
|
|
197
|
+
action: "read",
|
|
198
|
+
expires: Math.min(this.now() + expiresInMs, run.startedAt + RETENTION_MS),
|
|
199
|
+
responseDisposition: `attachment; filename="${path.split("/").at(-1)}"`,
|
|
200
|
+
responseType: "application/octet-stream"
|
|
201
|
+
});
|
|
202
|
+
} catch {
|
|
203
|
+
throw new Error("GCS artifact signing failed");
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
reportUrl(runId) {
|
|
207
|
+
return this.signedUrl(runId, "report.html", REPORT_TTL_MS);
|
|
208
|
+
}
|
|
209
|
+
/** Call after committing the passed verdict/keep policy, before publishing the safe report.
|
|
210
|
+
* Empty generations revoke old GET contents and prevent late signed PUTs from resurrecting them.
|
|
211
|
+
* On failure retry this operation; no download is authorized while the policy disallows it.
|
|
212
|
+
*/
|
|
213
|
+
async pruneRecordings(runId) {
|
|
214
|
+
const run = await this.run(runId);
|
|
215
|
+
if (!removed(run)) throw new Error("Recording retention requires a passed keep-failed run");
|
|
216
|
+
try {
|
|
217
|
+
for (const namespace of ARTIFACT_NAMESPACES) {
|
|
218
|
+
const prefix = `${namespace}/${this.projectId}/${run.batchId}/${runId}/`;
|
|
219
|
+
for await (const key of this.client.list(prefix)) {
|
|
220
|
+
objectPath(key);
|
|
221
|
+
if (!key.startsWith(prefix)) throw new Error("Artifact object scope denied");
|
|
222
|
+
if (/\/uploads-[a-f0-9]{64}\.json$/.test(key)) {
|
|
223
|
+
const data = await this.client.read(key);
|
|
224
|
+
if (data.length) {
|
|
225
|
+
for (const planned of z.array(z.string()).max(1e4).parse(JSON.parse(Buffer.from(data).toString("utf8")))) {
|
|
226
|
+
objectPath(planned);
|
|
227
|
+
if (!planned.startsWith(prefix)) throw new Error("Artifact object scope denied");
|
|
228
|
+
await this.client.erase(planned);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
} else if (key !== this.key(runId, run, sha256(recordingFreeReport(runId)), "report.html"))
|
|
232
|
+
await this.client.erase(key);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
} catch {
|
|
236
|
+
throw new Error("GCS recording cleanup failed");
|
|
237
|
+
}
|
|
238
|
+
await this.catalog.clear(runId);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
export {
|
|
242
|
+
GcsArtifactStore,
|
|
243
|
+
REPORT_TTL_MS,
|
|
244
|
+
RETENTION_MS,
|
|
245
|
+
UPLOAD_TTL_MS,
|
|
246
|
+
recordingFreeReport
|
|
247
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type GetSignedUrlConfig } from "@google-cloud/storage";
|
|
2
|
+
import type { EventObjects } from "../../db/event-chunks.js";
|
|
3
|
+
export interface GcsClient {
|
|
4
|
+
write(key: string, data: Uint8Array): Promise<void>;
|
|
5
|
+
read(key: string): Promise<Uint8Array>;
|
|
6
|
+
remove(key: string): Promise<void>;
|
|
7
|
+
sign(key: string, options: GetSignedUrlConfig): Promise<string>;
|
|
8
|
+
list(prefix: string): AsyncIterable<string>;
|
|
9
|
+
erase(key: string): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
export declare function errorCode(error: unknown): number | undefined;
|
|
12
|
+
export declare function createGcsClient(bucketName: string): GcsClient;
|
|
13
|
+
export declare function objectPath(key: string): string;
|
|
14
|
+
export declare class GcsEventObjects implements EventObjects {
|
|
15
|
+
private readonly client;
|
|
16
|
+
private readonly projectId;
|
|
17
|
+
constructor(client: GcsClient, projectId: string);
|
|
18
|
+
private key;
|
|
19
|
+
putImmutable(key: string, data: Uint8Array): Promise<void>;
|
|
20
|
+
read(key: string): Promise<Uint8Array<ArrayBufferLike>>;
|
|
21
|
+
remove(key: string): Promise<void>;
|
|
22
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { ARTIFACT_NAMESPACES } from "@open-qa/qa-engine";
|
|
2
|
+
import { Storage } from "@google-cloud/storage";
|
|
3
|
+
import { sha256 } from "../../db/event-chunks.js";
|
|
4
|
+
import { publicId } from "../../public-id.js";
|
|
5
|
+
function errorCode(error) {
|
|
6
|
+
if (error && typeof error === "object" && "code" in error) return Number(error.code);
|
|
7
|
+
}
|
|
8
|
+
function createGcsClient(bucketName) {
|
|
9
|
+
if (!/^[a-z0-9][a-z0-9._-]{1,220}[a-z0-9]$/.test(bucketName))
|
|
10
|
+
throw new Error("Invalid QA artifact bucket");
|
|
11
|
+
const bucket = new Storage({ retryOptions: { autoRetry: false } }).bucket(bucketName);
|
|
12
|
+
return {
|
|
13
|
+
async write(key, data) {
|
|
14
|
+
await bucket.file(key).save(Buffer.from(data), {
|
|
15
|
+
resumable: false,
|
|
16
|
+
preconditionOpts: { ifGenerationMatch: 0 },
|
|
17
|
+
metadata: { contentType: "application/octet-stream", cacheControl: "private, no-store" }
|
|
18
|
+
});
|
|
19
|
+
},
|
|
20
|
+
async read(key) {
|
|
21
|
+
const [metadata] = await bucket.file(key).getMetadata();
|
|
22
|
+
if (!metadata.generation || !Number.isSafeInteger(Number(metadata.size)) || Number(metadata.size) > 64 * 1024 * 1024)
|
|
23
|
+
throw new Error("Artifact exceeds download limit");
|
|
24
|
+
const [data] = await bucket.file(key, { generation: metadata.generation }).download();
|
|
25
|
+
return data;
|
|
26
|
+
},
|
|
27
|
+
async remove(key) {
|
|
28
|
+
await bucket.file(key).delete({ ignoreNotFound: true });
|
|
29
|
+
},
|
|
30
|
+
async sign(key, options) {
|
|
31
|
+
const [url] = await bucket.file(key).getSignedUrl(options);
|
|
32
|
+
return url;
|
|
33
|
+
},
|
|
34
|
+
async *list(prefix) {
|
|
35
|
+
for await (const file of bucket.getFilesStream({ prefix })) yield file.name;
|
|
36
|
+
},
|
|
37
|
+
async erase(key) {
|
|
38
|
+
await bucket.file(key).save(Buffer.alloc(0), {
|
|
39
|
+
resumable: false,
|
|
40
|
+
metadata: { contentType: "application/octet-stream", cacheControl: "private, no-store" }
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function objectPath(key) {
|
|
46
|
+
if (key.length > 1024 || key.split("/").some((part) => !/^[a-zA-Z0-9][a-zA-Z0-9._-]*$/.test(part)))
|
|
47
|
+
throw new Error("Invalid artifact object path");
|
|
48
|
+
return key;
|
|
49
|
+
}
|
|
50
|
+
class GcsEventObjects {
|
|
51
|
+
constructor(client, projectId) {
|
|
52
|
+
this.client = client;
|
|
53
|
+
this.projectId = projectId;
|
|
54
|
+
publicId.parse(projectId);
|
|
55
|
+
}
|
|
56
|
+
client;
|
|
57
|
+
projectId;
|
|
58
|
+
key(key) {
|
|
59
|
+
objectPath(key);
|
|
60
|
+
if (!key.startsWith(`events/${this.projectId}/`)) throw new Error("Object scope denied");
|
|
61
|
+
return `open-qa-runs/${key}`;
|
|
62
|
+
}
|
|
63
|
+
async putImmutable(key, data) {
|
|
64
|
+
const object = this.key(key);
|
|
65
|
+
if (data.byteLength > 4 * 1024 * 1024) throw new Error("Event chunk exceeds upload limit");
|
|
66
|
+
try {
|
|
67
|
+
await this.client.write(object, data);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
if (errorCode(error) !== 412) throw new Error("GCS event upload failed");
|
|
70
|
+
const previous = await this.read(key);
|
|
71
|
+
if (sha256(previous) !== sha256(data)) throw new Error("Immutable object content conflict");
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
async read(key) {
|
|
75
|
+
objectPath(key);
|
|
76
|
+
const explicit = ARTIFACT_NAMESPACES.some(
|
|
77
|
+
(namespace) => key.startsWith(`${namespace}/${this.projectId}/`)
|
|
78
|
+
);
|
|
79
|
+
const object = explicit ? key : this.key(key);
|
|
80
|
+
try {
|
|
81
|
+
return await this.client.read(object);
|
|
82
|
+
} catch {
|
|
83
|
+
throw new Error("GCS event object unavailable");
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
async remove(key) {
|
|
87
|
+
this.key(key);
|
|
88
|
+
try {
|
|
89
|
+
for (const namespace of ARTIFACT_NAMESPACES) {
|
|
90
|
+
try {
|
|
91
|
+
await this.client.remove(`${namespace}/${key}`);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
if (errorCode(error) !== 404) throw error;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
} catch (error) {
|
|
97
|
+
if (errorCode(error) !== 404) throw new Error("GCS event removal failed");
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
export {
|
|
102
|
+
GcsEventObjects,
|
|
103
|
+
createGcsClient,
|
|
104
|
+
errorCode,
|
|
105
|
+
objectPath
|
|
106
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface AuthorizedArtifact {
|
|
2
|
+
url: string;
|
|
3
|
+
expiresAt: number;
|
|
4
|
+
}
|
|
5
|
+
/** Recheck the caller's authenticated project/run grant on each authorization.
|
|
6
|
+
* One 403 may refresh a revoked/expired URL; 404, local expiry and other failures terminate.
|
|
7
|
+
* Return bytes to an attachment/off-origin consumer, never to executable Studio HTML.
|
|
8
|
+
*/
|
|
9
|
+
export declare function downloadArtifact(authorize: () => Promise<AuthorizedArtifact>, signal: AbortSignal, send?: typeof fetch, now?: () => number): Promise<Uint8Array>;
|