@adea-ai/app-framework-release-tools 0.5.0 → 0.7.0

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.
@@ -0,0 +1,155 @@
1
+ // @bun
2
+ // ../../ci/prepare-daily-workspace.ts
3
+ import { lstat, mkdir, mkdtemp, open, opendir, realpath, rm, writeFile } from "fs/promises";
4
+ import { join, relative, resolve } from "path";
5
+ import { constants } from "fs";
6
+ var identityFields = ["tag", "sourceSha", "workflowSha", "runId", "runAttempt"];
7
+ var buckets = ["artifacts", "receipts"];
8
+ function requireThat(ok, message) {
9
+ if (!ok)
10
+ throw new Error(message);
11
+ }
12
+ async function realDirectory(path) {
13
+ requireThat((await lstat(path)).isDirectory(), "expected a real workspace directory");
14
+ }
15
+ var fileReadFlags = constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK;
16
+ async function readBounded(path, limit) {
17
+ const file = await open(path, fileReadFlags);
18
+ try {
19
+ const info = await file.stat();
20
+ requireThat(info.isFile() && info.size > 0 && info.size <= limit, "invalid workspace metadata");
21
+ const bytes = Buffer.alloc(info.size + 1);
22
+ let read = 0;
23
+ while (read < bytes.length) {
24
+ const { bytesRead } = await file.read(bytes, read, bytes.length - read, read);
25
+ if (!bytesRead)
26
+ break;
27
+ read += bytesRead;
28
+ }
29
+ requireThat(read === info.size, "workspace metadata changed during read");
30
+ return JSON.parse(bytes.subarray(0, read).toString());
31
+ } finally {
32
+ await file.close();
33
+ }
34
+ }
35
+ async function copyBounded(source, destination, reportedSize) {
36
+ const input = await open(source, fileReadFlags);
37
+ try {
38
+ const before = await input.stat();
39
+ requireThat(before.isFile() && before.size === reportedSize, "workspace input changed before copy");
40
+ const output = await open(destination, "wx", 384);
41
+ try {
42
+ const buffer = Buffer.alloc(64 * 1024);
43
+ let size = 0;
44
+ while (size < reportedSize) {
45
+ const { bytesRead } = await input.read(buffer, 0, Math.min(buffer.length, reportedSize - size), size);
46
+ requireThat(bytesRead > 0, "workspace input truncated during copy");
47
+ await output.writeFile(buffer.subarray(0, bytesRead));
48
+ size += bytesRead;
49
+ }
50
+ const after = await input.stat();
51
+ requireThat(after.size === reportedSize && after.mtimeMs === before.mtimeMs && after.ctimeMs === before.ctimeMs, "workspace input changed during copy");
52
+ } finally {
53
+ await output.close();
54
+ }
55
+ } finally {
56
+ await input.close();
57
+ }
58
+ }
59
+ async function identity(path) {
60
+ const data = await readBounded(path, 4096);
61
+ requireThat(data && Object.keys(data).length === identityFields.length && identityFields.every((key) => Object.hasOwn(data, key)) && typeof data.tag === "string" && /^v[0-9]+[.][0-9]+[.][0-9]+$/.test(data.tag) && typeof data.sourceSha === "string" && /^[0-9a-f]{40}$/.test(data.sourceSha) && typeof data.workflowSha === "string" && /^[0-9a-f]{40}$/.test(data.workflowSha) && typeof data.runId === "string" && /^[A-Za-z0-9_.-]{1,128}$/.test(data.runId) && typeof data.runAttempt === "number" && Number.isSafeInteger(data.runAttempt) && data.runAttempt > 0, "invalid workspace coordinator identity");
62
+ return JSON.stringify(Object.fromEntries(identityFields.map((key) => [key, data[key]])));
63
+ }
64
+ async function exactNames(path, expected) {
65
+ const pending = new Set(expected);
66
+ for await (const entry of await opendir(path)) {
67
+ requireThat(pending.delete(entry.name), "workspace contains missing or unexpected entries");
68
+ }
69
+ requireThat(pending.size === 0, "workspace contains missing or unexpected entries");
70
+ }
71
+ async function prepare(args) {
72
+ const [cli, config, expectedIdentity, workspace, parent, policyFile, ...extra] = args;
73
+ requireThat(cli && config && expectedIdentity && workspace && parent && policyFile && extra.length === 0, "usage: prepare-daily-workspace <release-cli.js> <config> <identity> <workspace> <private-parent> <ownership.json>");
74
+ const policy = await readBounded(policyFile, 256 * 1024);
75
+ const limit = policy?.maxBytes;
76
+ const ownership = policy?.workers;
77
+ requireThat(policy && Object.keys(policy).length === 2 && typeof limit === "number" && Number.isSafeInteger(limit) && limit > 0 && limit <= 64 * 1024 ** 3 && ownership && typeof ownership === "object" && !Array.isArray(ownership), "invalid workspace ownership policy");
78
+ const workers = Object.keys(ownership);
79
+ requireThat(workers.length > 0 && workers.length <= 64 && workers.every((name) => /^[a-z][a-z0-9-]{0,79}$/.test(name)), "invalid workspace worker namespace");
80
+ await realDirectory(workspace);
81
+ await realDirectory(parent);
82
+ const attached = await realpath(workspace);
83
+ const privateParent = await realpath(parent);
84
+ const outputRelative = relative(attached, privateParent);
85
+ requireThat(outputRelative === ".." || outputRelative.startsWith("../"), "private output must be separate from worker workspace");
86
+ await exactNames(attached, workers);
87
+ const expected = await identity(expectedIdentity);
88
+ const files = {
89
+ artifacts: new Map,
90
+ receipts: new Map
91
+ };
92
+ let aggregate = 0;
93
+ for (const worker of workers) {
94
+ const root = join(attached, worker);
95
+ await realDirectory(root);
96
+ await exactNames(root, ["identity.json", ...buckets]);
97
+ requireThat(await identity(join(root, "identity.json")) === expected, "worker identity mismatch");
98
+ const owned = ownership[worker];
99
+ requireThat(owned && Object.keys(owned).length === 2, "invalid worker ownership declaration");
100
+ for (const bucket of buckets) {
101
+ const dir = join(root, bucket);
102
+ await realDirectory(dir);
103
+ const names = owned[bucket];
104
+ requireThat(Array.isArray(names) && names.length <= 256 && new Set(names).size === names.length && names.every((name) => typeof name === "string" && /^[A-Za-z0-9][A-Za-z0-9._+-]{0,239}$/.test(name)), "invalid or ambiguous file ownership policy");
105
+ await exactNames(dir, names);
106
+ for (const name of names) {
107
+ requireThat(files[bucket].size < 256 && !files[bucket].has(name), "duplicate or excessive workspace input");
108
+ const path = join(dir, name);
109
+ const stat = await lstat(path);
110
+ requireThat(stat.isFile() && stat.size > 0, "expected a regular workspace input file");
111
+ if (bucket === "receipts")
112
+ requireThat(stat.size <= 64 * 1024, "workspace receipt exceeds byte limit");
113
+ aggregate += stat.size;
114
+ requireThat(Number.isSafeInteger(aggregate) && aggregate <= limit, "workspace exceeds aggregate byte limit");
115
+ files[bucket].set(name, { path, size: stat.size });
116
+ }
117
+ }
118
+ }
119
+ const scratch = await mkdtemp(join(privateParent, ".daily-workspace-"));
120
+ try {
121
+ for (const bucket of buckets) {
122
+ const dir = join(scratch, bucket);
123
+ await mkdir(dir, { mode: 448 });
124
+ for (const [name, source] of files[bucket]) {
125
+ await copyBounded(source.path, join(dir, name), source.size);
126
+ }
127
+ }
128
+ const stagedIdentity = join(scratch, "identity.json");
129
+ await writeFile(stagedIdentity, expected, { flag: "wx", mode: 384 });
130
+ const child = Bun.spawnSync([
131
+ process.execPath,
132
+ resolve(cli),
133
+ "daily-matrix",
134
+ "stage-local",
135
+ resolve(config),
136
+ stagedIdentity,
137
+ join(scratch, "artifacts"),
138
+ join(scratch, "receipts"),
139
+ privateParent
140
+ ], {
141
+ env: { PATH: process.env.PATH, TMPDIR: scratch },
142
+ stdout: "inherit",
143
+ stderr: "inherit"
144
+ });
145
+ requireThat(child.exitCode === 0, "Daily workspace did not qualify for local staging");
146
+ } finally {
147
+ await rm(scratch, { recursive: true, force: true });
148
+ }
149
+ }
150
+ try {
151
+ await prepare(process.argv.slice(2));
152
+ } catch (error) {
153
+ console.error(error instanceof Error ? error.message : "Unable to stage the Daily workspace");
154
+ process.exitCode = 1;
155
+ }