overvues 0.0.1 → 0.4.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/README.md +58 -2
  2. package/dist/ansi.js +24 -0
  3. package/dist/attach-client.js +586 -0
  4. package/dist/auth-connect-browser.js +112 -0
  5. package/dist/auth-connect.js +306 -0
  6. package/dist/browser-bootstrap.js +190 -0
  7. package/dist/browser-loopback.js +289 -0
  8. package/dist/build-identity.js +6 -0
  9. package/dist/codex-reviewer-apparmor.json +4 -0
  10. package/dist/codex-reviewer-seccomp.json +1238 -0
  11. package/dist/codex-runtime-protocol.js +1 -0
  12. package/dist/codex-subscription.js +53 -0
  13. package/dist/config.js +297 -0
  14. package/dist/connect-comparison-code.js +26 -0
  15. package/dist/development-runner.js +109 -0
  16. package/dist/dns-doctor.js +390 -0
  17. package/dist/docker-names.js +47 -0
  18. package/dist/doctor.js +114 -0
  19. package/dist/gadget-job.js +804 -0
  20. package/dist/health-check.js +112 -0
  21. package/dist/help.js +71 -0
  22. package/dist/host-auth.js +22 -0
  23. package/dist/host-diagnostics.js +20 -0
  24. package/dist/host-runtime-identity.js +39 -0
  25. package/dist/host-runtime-metadata.js +56 -0
  26. package/dist/host-service.js +377 -0
  27. package/dist/host-stack.js +484 -0
  28. package/dist/host-supervisor.js +237 -0
  29. package/dist/host-update.js +387 -0
  30. package/dist/hub-address.js +100 -0
  31. package/dist/hub-client.js +459 -0
  32. package/dist/hub-kind.js +88 -0
  33. package/dist/identity.js +73 -0
  34. package/dist/index.js +185 -0
  35. package/dist/init.js +295 -0
  36. package/dist/isolated-run-cache.js +116 -0
  37. package/dist/local-stack.js +685 -0
  38. package/dist/machine-capacity.js +128 -0
  39. package/dist/managed-runner-image.js +13 -0
  40. package/dist/os-acted-record.js +49 -0
  41. package/dist/os-adapter.js +8190 -0
  42. package/dist/os-command.js +292 -0
  43. package/dist/os-handoff.js +274 -0
  44. package/dist/os-keychain.js +247 -0
  45. package/dist/os-machine.js +818 -0
  46. package/dist/os-owner.js +61 -0
  47. package/dist/os-private-file.js +98 -0
  48. package/dist/os-protocol.js +468 -0
  49. package/dist/os-session-store.js +133 -0
  50. package/dist/os-status.js +102 -0
  51. package/dist/picker.js +863 -0
  52. package/dist/portable-workspace-capability.js +60 -0
  53. package/dist/project-environment-capability.js +62 -0
  54. package/dist/project-environment.js +28 -0
  55. package/dist/project-preparation.js +134 -0
  56. package/dist/readiness.js +28 -0
  57. package/dist/redaction.js +27 -0
  58. package/dist/reviewer-isolation-capability.js +61 -0
  59. package/dist/runner-api-auth.js +80 -0
  60. package/dist/runner-command-fence.js +536 -0
  61. package/dist/runner-inspection.js +57 -0
  62. package/dist/runner-inventory.js +587 -0
  63. package/dist/runner-server.js +2520 -0
  64. package/dist/runner-session.js +470 -0
  65. package/dist/runner-slug-folder.js +39 -0
  66. package/dist/runner-slug.js +40 -0
  67. package/dist/setup.js +139 -0
  68. package/dist/shutdown.js +102 -0
  69. package/dist/splash.js +65 -0
  70. package/dist/staged-workspaces.js +441 -0
  71. package/dist/staging-disk.js +97 -0
  72. package/dist/terminal-lane.js +339 -0
  73. package/dist/terminal-recovery.js +502 -0
  74. package/licenses/capnweb.txt +23 -0
  75. package/licenses/moby-profiles.txt +212 -0
  76. package/licenses/ws.txt +22 -0
  77. package/package.json +28 -7
  78. package/cli.js +0 -2
@@ -0,0 +1,818 @@
1
+ // `tiller`'s side of deploy, pull and preview: it takes the Hub's requests (`os-protocol.ts`)
2
+ // from its own Hub connection and makes the OS calls itself, with the OS session it keeps.
3
+ //
4
+ // - The session is read from the store for each request, used to sign in to the OS, and goes
5
+ // nowhere else: not to the Hub, not into a log line, not into an answer.
6
+ // - Every request is validated whole before anything is done, and refused otherwise.
7
+ // - A request that changes the OS is checked back with the Hub first: the Hub must hold an
8
+ // approval by the owner's browser session for exactly that request.
9
+ // - Built files come from the Hub by fingerprint and are hashed here before they are used.
10
+ // - A Ship id or approval id is written to disk before the OS is touched, and never acted on
11
+ // twice.
12
+ // - One line is logged per action, so the person at the machine can see what was done.
13
+ // - What a preview is connected to is worked out here, from the live app's connections as the
14
+ // OS reports them and the Gatekeeper catalog this build carries. The Hub names only which
15
+ // connections the owner turned on; the catalog decides whether that is allowed.
16
+ import { fileActedRecord } from "./os-acted-record.js";
17
+ import { checkPreviewConnections, connectOsAdapter, fingerprintFiles, openOsConnection, OsAdapterError, previewCatalog, } from "./os-adapter.js";
18
+ import { builtFilesPath, isOsId, needsOsApproval, osApprovalId, osApprovalProblem, osRequestSubject, parseOsRequestMessage, isOsCommitAuthor, parseOsWatchMessage, readFilesUploadPath, OS_APPROVAL_CHECK_PATH, OS_NOT_CONNECTED_MESSAGE, OS_PROTOCOL, OS_READ_FILES_HISTORY_DEPTH, OS_WATCH_DEFAULT_SECONDS, } from "./os-protocol.js";
19
+ import { findGadgetBuildRecord } from "./gadget-job.js";
20
+ import { machineOsSessionStore, osSessionExpiry, osSessionExpiryLine } from "./os-session-store.js";
21
+ import { osStatusConnectLogLine, osStatusLogLine, osStatusSource } from "./os-status.js";
22
+ import { redactValues } from "./redaction.js";
23
+ import { CLI, OS } from "./identity.js";
24
+ // os-protocol.ts imports nothing (the Hub imports it), so its sentence names `tiller`; this
25
+ // program says its own command.
26
+ const NOT_CONNECTED_MESSAGE = OS_NOT_CONNECTED_MESSAGE.replace("`tiller os connect`", `\`${CLI.command} os connect\``).replace("the OS", OS.the);
27
+ /** The most built files `tiller` reads from the Hub for one deploy. */
28
+ const MAX_BUILT_FILES_BYTES = 64 * 1024 * 1024;
29
+ const CONNECT_TIMEOUT_MS = 30_000;
30
+ const OS_WORK_TIMEOUT_MS = 10 * 60_000;
31
+ const HUB_TIMEOUT_MS = 60_000;
32
+ /** How often the stored session is looked at, so the Hub hears of `tiller os connect` and `forget`. */
33
+ export const OS_STATUS_CHECK_MS = 5_000;
34
+ class Refused extends Error {
35
+ refusal;
36
+ constructor(refusal) {
37
+ super(refusal.message);
38
+ this.refusal = refusal;
39
+ this.name = "Refused";
40
+ }
41
+ }
42
+ const refuse = (code, message, more = {}) => {
43
+ throw new Refused({ code, message, ...more });
44
+ };
45
+ const short = (commit) => commit.slice(0, 8);
46
+ /** An address as host and folder path, or null. Two addresses are the same place when these match. */
47
+ const place = (address) => {
48
+ try {
49
+ const url = new URL(address ?? "");
50
+ return `${url.host}/${url.pathname.split("/").filter(Boolean).join("/")}`;
51
+ }
52
+ catch {
53
+ return null;
54
+ }
55
+ };
56
+ const KEPT_WORDS = "It stays connected until the preview is removed, because its files are deleted with the preview, after you approve that in Overvues.";
57
+ const where = (app) => `gadget ${app.gadgetId} in workspace ${app.workspaceId}`;
58
+ /** Signs in to the OS the session is for. The session goes to that address and nowhere else. */
59
+ export const connectStoredOs = async (stored, log) => {
60
+ const connection = await openOsConnection({ osUrl: stored.osUrl });
61
+ try {
62
+ return await connectOsAdapter({ connection, session: stored.session, log });
63
+ }
64
+ catch (error) {
65
+ connection.close?.();
66
+ throw error;
67
+ }
68
+ };
69
+ /**
70
+ * Calls to this machine's own Hub: only the OS routes, only at the Hub's own address, with the
71
+ * machine's credentials, and never following a redirect to somewhere else.
72
+ */
73
+ export function hubFetcher(hubUrl, headers) {
74
+ return (path, init) => {
75
+ if (!path.startsWith("/api/machine/os/"))
76
+ throw new Error(`not an ${OS.noun} route on the Hub`);
77
+ const url = new URL(path, hubUrl);
78
+ if (url.origin !== new URL(hubUrl).origin)
79
+ throw new Error("not this machine's Hub");
80
+ return fetch(url, {
81
+ method: init.method,
82
+ headers: { ...headers(), ...(init.body !== undefined ? { "content-type": "application/json" } : {}) },
83
+ ...(init.body !== undefined ? { body: init.body } : {}),
84
+ redirect: "error",
85
+ signal: AbortSignal.timeout(HUB_TIMEOUT_MS),
86
+ });
87
+ };
88
+ }
89
+ async function within(ms, what, work, onTimeout) {
90
+ let timer;
91
+ try {
92
+ return await Promise.race([
93
+ work,
94
+ new Promise((_, reject) => {
95
+ timer = setTimeout(() => {
96
+ onTimeout?.();
97
+ reject(new Refused({ code: "os", message: `${what} did not finish in time.` }));
98
+ }, ms);
99
+ }),
100
+ ]);
101
+ }
102
+ finally {
103
+ clearTimeout(timer);
104
+ }
105
+ }
106
+ /** A response body as text, refused past `limit` bytes. */
107
+ async function textUpTo(response, limit) {
108
+ const reader = response.body?.getReader();
109
+ if (!reader)
110
+ return "";
111
+ const chunks = [];
112
+ let total = 0;
113
+ for (;;) {
114
+ const { done, value } = await reader.read();
115
+ if (done)
116
+ break;
117
+ total += value.byteLength;
118
+ if (total > limit) {
119
+ await reader.cancel().catch(() => { });
120
+ return null;
121
+ }
122
+ chunks.push(value);
123
+ }
124
+ return Buffer.concat(chunks).toString("utf8");
125
+ }
126
+ const watchKey = (app) => `${app.workspaceId}\n${app.gadgetId}`;
127
+ export class OsMachine {
128
+ options;
129
+ now;
130
+ connectOs;
131
+ /** One OS action at a time. */
132
+ queue = Promise.resolve();
133
+ watched = new Map();
134
+ watchSeconds = OS_WATCH_DEFAULT_SECONDS;
135
+ watchTimer = null;
136
+ statusTimer = null;
137
+ send = () => { };
138
+ lastStatus = "";
139
+ /** The session of the request in hand, so that no line or answer can carry it. */
140
+ secret = null;
141
+ status;
142
+ constructor(options) {
143
+ this.options = options;
144
+ this.now = options.now ?? Date.now;
145
+ this.connectOs = options.connectOs ?? connectStoredOs;
146
+ // Whether a usable session is stored, for which OS, and when it ends: never the session.
147
+ this.status = osStatusSource(options.sessions, options.machineId, this.now, (line) => this.log(line));
148
+ }
149
+ /**
150
+ * The Hub connection opened: say whether this machine holds a session, and log what was said,
151
+ * so the log shows the Hub was told after every connection. The Hub answers with its watch list.
152
+ */
153
+ connected(send) {
154
+ this.send = send;
155
+ this.lastStatus = "";
156
+ this.log(osStatusConnectLogLine(this.reportStatus()));
157
+ if (!this.statusTimer) {
158
+ // Only whether a session is stored, and for which OS, is compared and sent.
159
+ this.statusTimer = setInterval(() => this.reportStatus(), OS_STATUS_CHECK_MS);
160
+ this.statusTimer.unref?.();
161
+ }
162
+ }
163
+ /** A message from the Hub connection. Anything that is not an OS message is left alone. */
164
+ async receive(raw, send) {
165
+ this.send = send;
166
+ const type = raw?.type;
167
+ if (type === "os-watch") {
168
+ await this.setWatch(raw);
169
+ return;
170
+ }
171
+ if (type === "os-status-request") {
172
+ // The Hub holds no word from this connection: say it again, changed or not. Only the status.
173
+ if (raw.machineId !== this.options.machineId)
174
+ return;
175
+ this.lastStatus = "";
176
+ this.reportStatus();
177
+ return;
178
+ }
179
+ if (type !== "os-request")
180
+ return;
181
+ const rawId = raw.requestId;
182
+ const parsed = parseOsRequestMessage(raw);
183
+ if (!parsed.ok || parsed.value.machineId !== this.options.machineId) {
184
+ const message = parsed.ok ? "The request is for another machine." : `The request was refused: ${parsed.message}.`;
185
+ this.log(`${OS.noun}: refused a request from the Hub: ${parsed.ok ? "it is for another machine" : parsed.message}`);
186
+ if (isOsId(rawId)) {
187
+ send({ type: "os-response", requestId: rawId, machineId: this.options.machineId, ok: false, action: null, refused: { code: "invalid-request", message } });
188
+ }
189
+ return;
190
+ }
191
+ send({ type: "os-received", requestId: parsed.value.requestId });
192
+ const response = await this.inTurn(() => this.answer(parsed.value));
193
+ send(response);
194
+ this.reportStatus();
195
+ }
196
+ /** Reads every watched app's head and reports the ones that changed. The timer calls this. */
197
+ pollWatched() {
198
+ return this.inTurn(() => this.readWatched());
199
+ }
200
+ close() {
201
+ if (this.watchTimer)
202
+ clearInterval(this.watchTimer);
203
+ this.watchTimer = null;
204
+ if (this.statusTimer)
205
+ clearInterval(this.statusTimer);
206
+ this.statusTimer = null;
207
+ this.watched.clear();
208
+ this.send = () => { };
209
+ }
210
+ // ── Plumbing ───────────────────────────────────────────────────────
211
+ inTurn(work) {
212
+ const next = this.queue.then(work, work);
213
+ this.queue = next.catch(() => { });
214
+ return next;
215
+ }
216
+ clean(text) {
217
+ return this.secret ? redactValues(text, [this.secret, encodeURIComponent(this.secret)], `[${OS.noun} session removed]`) : text;
218
+ }
219
+ log(line) {
220
+ this.options.log(this.clean(line));
221
+ }
222
+ reportStatus() {
223
+ const status = this.status();
224
+ const text = JSON.stringify(status);
225
+ if (text === this.lastStatus)
226
+ return status;
227
+ if (this.lastStatus)
228
+ this.log(osStatusLogLine(status));
229
+ this.lastStatus = text;
230
+ this.send(status);
231
+ return status;
232
+ }
233
+ loadSession() {
234
+ let stored;
235
+ try {
236
+ stored = this.options.sessions.load();
237
+ }
238
+ catch (error) {
239
+ return refuse("not-connected", `The ${OS.noun} session stored on this machine cannot be used: ${error instanceof Error ? error.message : "it could not be read"}`);
240
+ }
241
+ if (!stored)
242
+ return refuse("not-connected", NOT_CONNECTED_MESSAGE);
243
+ const expiry = osSessionExpiry(stored.expiresAt, this.now());
244
+ if (expiry.state === "expired")
245
+ return refuse("not-connected", osSessionExpiryLine(expiry));
246
+ return stored;
247
+ }
248
+ toRefusal(error) {
249
+ if (error instanceof Refused)
250
+ return { ...error.refusal, message: this.clean(error.refusal.message) };
251
+ if (error instanceof OsAdapterError) {
252
+ return { code: error.code === "session-refused" ? "session-refused" : "os", message: this.clean(error.message), adapterCode: error.code };
253
+ }
254
+ return { code: "machine", message: this.clean(error instanceof Error ? error.message : String(error)) };
255
+ }
256
+ async withOs(stored, work) {
257
+ const os = await within(CONNECT_TIMEOUT_MS, `Connecting to ${OS.the}`, this.connectOs(stored, (line) => this.log(`${OS.noun}: ${line}`)));
258
+ try {
259
+ if (os.account.id !== stored.account.id) {
260
+ refuse("session-refused", `${OS.The} says the stored session belongs to another account than the one this machine was connected as. Run \`${CLI.command} os forget\`, then \`${CLI.command} os connect\`.`);
261
+ }
262
+ return await within(OS_WORK_TIMEOUT_MS, `The ${OS.noun} call`, work(os), () => os.close());
263
+ }
264
+ finally {
265
+ os.close();
266
+ }
267
+ }
268
+ async answer(message) {
269
+ const { requestId, request } = message;
270
+ const base = { type: "os-response", requestId, machineId: this.options.machineId };
271
+ try {
272
+ const stored = this.loadSession();
273
+ this.secret = stored.session;
274
+ const result = await this.run(request, stored, requestId);
275
+ return { ...base, ok: true, action: request.action, result };
276
+ }
277
+ catch (error) {
278
+ const refused = this.toRefusal(error);
279
+ this.log(`${OS.noun}: ${request.action} refused (${refused.code}): ${refused.message}`);
280
+ return { ...base, ok: false, action: request.action, refused };
281
+ }
282
+ finally {
283
+ this.secret = null;
284
+ }
285
+ }
286
+ run(request, stored, requestId) {
287
+ switch (request.action) {
288
+ case "deploy": return this.deploy(request, stored, requestId);
289
+ case "redeploy": return this.redeploy(request, stored, requestId);
290
+ case "read-head": return this.readHead(request, stored);
291
+ case "read-files": return this.readFiles(request, stored, requestId);
292
+ case "read-connections": return this.readConnections(request, stored);
293
+ case "preview-publish": return this.previewPublish(request, stored, requestId);
294
+ case "preview-connect": return this.previewConnect(request, stored, requestId);
295
+ case "preview-remove": return this.previewRemove(request, stored, requestId);
296
+ }
297
+ }
298
+ // ── The approval, the built files, the record ──────────────────────
299
+ /** Asks the Hub whether the owner's browser session approved exactly this request. */
300
+ async requireApproval(request, requestId) {
301
+ if (!needsOsApproval(request))
302
+ return;
303
+ const check = {
304
+ protocol: OS_PROTOCOL, machineId: this.options.machineId, requestId, approvalId: osApprovalId(request), action: request.action,
305
+ subject: osRequestSubject(request),
306
+ };
307
+ let answer;
308
+ try {
309
+ const response = await this.options.hubFetch(OS_APPROVAL_CHECK_PATH, { method: "POST", body: JSON.stringify(check) });
310
+ if (!response.ok)
311
+ refuse("not-approved", `The Hub did not confirm that the owner approved this (HTTP ${response.status}), so nothing was done.`);
312
+ answer = JSON.parse(await textUpTo(response, 64 * 1024) ?? "null");
313
+ }
314
+ catch (error) {
315
+ if (error instanceof Refused)
316
+ throw error;
317
+ refuse("not-approved", "The Hub could not be asked whether the owner approved this, so nothing was done.");
318
+ }
319
+ const problem = osApprovalProblem(answer, request, this.now());
320
+ if (problem)
321
+ refuse("not-approved", `Not approved: ${problem}. Nothing was done.`);
322
+ }
323
+ /** The built files frozen under a fingerprint, fetched from the Hub and hashed here. */
324
+ async builtFiles(fingerprint) {
325
+ let text;
326
+ try {
327
+ const response = await this.options.hubFetch(builtFilesPath(fingerprint), { method: "GET" });
328
+ if (!response.ok)
329
+ refuse("files", `The Hub did not hand over the built files (HTTP ${response.status}), so nothing was sent to ${OS.the}.`);
330
+ text = await textUpTo(response, MAX_BUILT_FILES_BYTES);
331
+ }
332
+ catch (error) {
333
+ if (error instanceof Refused)
334
+ throw error;
335
+ return refuse("files", `The built files could not be fetched from the Hub, so nothing was sent to ${OS.the}.`);
336
+ }
337
+ if (text === null)
338
+ return refuse("files", `The built files are larger than this machine accepts, so nothing was sent to ${OS.the}.`);
339
+ let files;
340
+ try {
341
+ files = JSON.parse(text)?.files;
342
+ }
343
+ catch {
344
+ files = null;
345
+ }
346
+ if (!files || typeof files !== "object" || Array.isArray(files) || !Object.keys(files).length
347
+ || Object.entries(files).some(([path, value]) => !path || typeof value !== "string")) {
348
+ return refuse("files", `What the Hub handed over is not a set of built files, so nothing was sent to ${OS.the}.`);
349
+ }
350
+ if (await fingerprintFiles(files) !== fingerprint) {
351
+ return refuse("files", `The built files the Hub handed over do not match the fingerprint the request names, so nothing was sent to ${OS.the}.`);
352
+ }
353
+ return files;
354
+ }
355
+ /**
356
+ * `builtFiles` hashed what the Hub handed over. That only shows the Hub is consistent with
357
+ * itself; this shows the files are ones a job on this machine produced for this app. Called
358
+ * before anything is recorded, so the Ship id or approval id is not used up by a refusal.
359
+ */
360
+ requireBuiltHere(appId, fingerprint) {
361
+ let builtHere = false;
362
+ try {
363
+ builtHere = this.options.builtHere(appId, fingerprint);
364
+ }
365
+ catch {
366
+ builtHere = false;
367
+ }
368
+ if (!builtHere) {
369
+ refuse("not-built-here", `This machine has no record of building these files for this app, so nothing was sent to ${OS.the}. Build and test must run on this machine first.`);
370
+ }
371
+ }
372
+ recorded(kind, id) {
373
+ try {
374
+ return this.options.acted.get(kind, id);
375
+ }
376
+ catch (error) {
377
+ return refuse("machine", `This machine's record of what it has done on ${OS.the} cannot be used (${error instanceof Error ? error.message : "unreadable"}), so nothing was done.`);
378
+ }
379
+ }
380
+ static recordedOf(entry) {
381
+ return {
382
+ state: entry.state, startedAt: entry.startedAt,
383
+ ...(entry.app ? { app: entry.app } : {}), ...(entry.preview ? { preview: entry.preview } : {}),
384
+ };
385
+ }
386
+ /**
387
+ * What an earlier request for this id ended with, when it was this same request and ended;
388
+ * null when the id is new. Anything else is refused: this machine does not act on an id twice.
389
+ */
390
+ repeatOf(kind, id, subject, name) {
391
+ const entry = this.recorded(kind, id);
392
+ if (!entry)
393
+ return null;
394
+ const recorded = OsMachine.recordedOf(entry);
395
+ if (entry.subject !== subject) {
396
+ return refuse("already-acted", `${name} was already used for a different request, so nothing was done.`, { recorded });
397
+ }
398
+ if (entry.state === "done") {
399
+ this.log(`${OS.noun}: ${name}: asked again; answered from this machine's record, nothing was done`);
400
+ return entry;
401
+ }
402
+ return refuse("already-acted", entry.state === "started"
403
+ ? `This machine already started on ${name} and does not know how it ended. It will not act on it again; read the app's head to see what ${OS.the} holds.`
404
+ : `This machine already acted on ${name} and it ended in an error. It will not act on it again; read the app's head to see what ${OS.the} holds.`, { recorded });
405
+ }
406
+ /**
407
+ * Writes "started" to disk, runs the work, and writes how it ended. From "started" on, the
408
+ * id is used up whatever happens.
409
+ */
410
+ async acting(kind, id, action, subject, work) {
411
+ const entry = { kind, id, action, subject, state: "started", startedAt: new Date(this.now()).toISOString() };
412
+ try {
413
+ this.options.acted.put(entry);
414
+ }
415
+ catch (error) {
416
+ refuse("machine", `This machine could not write its record (${error instanceof Error ? error.message : "write failed"}), so nothing was done.`);
417
+ }
418
+ const save = () => {
419
+ try {
420
+ this.options.acted.put(entry);
421
+ }
422
+ catch (error) {
423
+ this.log(`${OS.noun}: could not update this machine's record: ${error instanceof Error ? error.message : "write failed"}`);
424
+ }
425
+ };
426
+ try {
427
+ const result = await work(entry, save);
428
+ Object.assign(entry, { state: "done", finishedAt: new Date(this.now()).toISOString(), result });
429
+ save();
430
+ return result;
431
+ }
432
+ catch (error) {
433
+ const refusal = this.toRefusal(error);
434
+ Object.assign(entry, { state: "failed", finishedAt: new Date(this.now()).toISOString() });
435
+ entry.refused = refusal;
436
+ save();
437
+ throw new Refused({ ...refusal, recorded: OsMachine.recordedOf(entry) });
438
+ }
439
+ }
440
+ // ── Deploy ─────────────────────────────────────────────────────────
441
+ describeDeploy(shipId, app, outcome) {
442
+ const to = where(app);
443
+ switch (outcome.outcome) {
444
+ case "stale": return `${OS.noun}: Ship ${shipId}: nothing deployed to ${to}: an edit was made in ${OS.the} after ${short(outcome.baseCommit)}`;
445
+ case "deployed": return `${OS.noun}: Ship ${shipId}: deployed to ${to}, commit ${short(outcome.commitId)} (${outcome.commitSource})`;
446
+ case "deployed-head-differs": return `${OS.noun}: Ship ${shipId}: deployed to ${to}, but an edit landed in ${OS.the} at the same moment (head ${short(outcome.headCommit)}); a pull is needed`;
447
+ case "deployed-server-not-loading": return `${OS.noun}: Ship ${shipId}: deployed to ${to}, commit ${short(outcome.commitId)}, and the app's server does not load`;
448
+ }
449
+ }
450
+ async deploy(request, stored, requestId) {
451
+ const subject = osRequestSubject(request);
452
+ const { shipId, fingerprint } = request;
453
+ const earlier = this.repeatOf("ship", shipId, subject, `Ship ${shipId}`);
454
+ if (earlier)
455
+ return { ...earlier.result, repeated: true };
456
+ await this.requireApproval(request, requestId);
457
+ const files = await this.builtFiles(fingerprint);
458
+ this.requireBuiltHere(request.appId, fingerprint);
459
+ return this.withOs(stored, (os) => this.acting("ship", shipId, "deploy", subject, async (entry, save) => {
460
+ let app;
461
+ let target;
462
+ if (request.target.kind === "first-deploy") {
463
+ const created = await os.createApp({ title: request.target.title, ...(request.target.workspaceId ? { workspaceId: request.target.workspaceId } : {}) });
464
+ app = { workspaceId: created.workspaceId, gadgetId: created.gadgetId, createdWorkspace: created.createdWorkspace, headCommit: created.headCommit };
465
+ entry.app = app;
466
+ save();
467
+ this.log(`${OS.noun}: Ship ${shipId}: created the app ${JSON.stringify(request.target.title)} as ${where(app)}`);
468
+ target = { workspaceId: app.workspaceId, gadgetId: app.gadgetId, baseCommit: app.headCommit };
469
+ }
470
+ else {
471
+ target = request.target;
472
+ }
473
+ const outcome = await os.deploy({
474
+ shipId, workspaceId: target.workspaceId, gadgetId: target.gadgetId, baseCommit: target.baseCommit, fingerprint, files,
475
+ });
476
+ this.log(this.describeDeploy(shipId, target, outcome));
477
+ return { ...outcome, ...(app ? { app } : {}), repeated: false };
478
+ }));
479
+ }
480
+ async redeploy(request, stored, requestId) {
481
+ const subject = osRequestSubject(request);
482
+ const { shipId, ofShipId, workspaceId, gadgetId } = request;
483
+ const earlier = this.repeatOf("ship", shipId, subject, `Ship ${shipId}`);
484
+ if (earlier)
485
+ return { ...earlier.result, repeated: true };
486
+ // Only this machine's own record of the earlier Ship says what may be put back, and where.
487
+ const of = this.recorded("ship", ofShipId);
488
+ const outcome = of?.state === "done" ? of.result : null;
489
+ let ofTarget = of?.app;
490
+ if (of && !ofTarget) {
491
+ try {
492
+ const target = JSON.parse(of.subject).target;
493
+ if (typeof target?.workspaceId === "string" && typeof target.gadgetId === "number")
494
+ ofTarget = { workspaceId: target.workspaceId, gadgetId: target.gadgetId };
495
+ }
496
+ catch { /* not a deploy this machine recorded */ }
497
+ }
498
+ if (!of || of.action !== "deploy" || outcome?.outcome !== "deployed-server-not-loading"
499
+ || ofTarget?.workspaceId !== workspaceId || ofTarget.gadgetId !== gadgetId) {
500
+ return refuse("not-redeployable", `This machine has no record that Ship ${ofShipId} left ${where(request)} with a server that does not load, so nothing was put back.`);
501
+ }
502
+ // The commits come from this machine's own record of that deploy, never from the request.
503
+ const { baseCommit, previousCommit } = outcome.redeploy;
504
+ await this.requireApproval(request, requestId);
505
+ return this.withOs(stored, async (os) => {
506
+ const previous = await os.readFiles({ workspaceId, commitId: previousCommit });
507
+ if (previous.notRead.length) {
508
+ const [first] = previous.notRead;
509
+ refuse("not-redeployable", `The version before Ship ${ofShipId} holds ${previous.notRead.length} file(s) that could not be read as text (${first.message ?? first.path}), so it cannot be put back from here. Nothing was changed.`);
510
+ }
511
+ if (!Object.keys(previous.files).length)
512
+ refuse("not-redeployable", `The version before Ship ${ofShipId} has no files to put back. Nothing was changed.`);
513
+ const fingerprint = await fingerprintFiles(previous.files);
514
+ return this.acting("ship", shipId, "redeploy", subject, async () => {
515
+ const result = await os.deploy({ shipId, workspaceId, gadgetId, baseCommit, fingerprint, files: previous.files });
516
+ this.log(`${this.describeDeploy(shipId, request, result)} (the version before Ship ${ofShipId}, ${short(previousCommit)}, put back)`);
517
+ return { ...result, repeated: false };
518
+ });
519
+ });
520
+ }
521
+ // ── Pull, and the connection report ────────────────────────────────
522
+ readHead(request, stored) {
523
+ const { workspaceId, gadgetId } = request;
524
+ return this.withOs(stored, async (os) => {
525
+ const head = await os.readHead({ workspaceId, gadgetId });
526
+ this.log(`${OS.noun}: read the head of ${where(request)} for ${request.for}: ${short(head.commitId)}`);
527
+ return { workspaceId, gadgetId, title: head.title, commitId: head.commitId };
528
+ });
529
+ }
530
+ readFiles(request, stored, requestId) {
531
+ const { workspaceId, gadgetId, commitId } = request;
532
+ return this.withOs(stored, async (os) => {
533
+ // Only the named gadget's own commits are read: a workspace holds other apps' commits too.
534
+ const head = await os.readHead({ workspaceId, gadgetId });
535
+ const belongs = head.commitId === commitId
536
+ || this.watched.get(watchKey(request))?.seen.has(commitId) === true
537
+ || (await os.readCommits({ workspaceId, fromCommit: head.commitId, depth: OS_READ_FILES_HISTORY_DEPTH })).some((commit) => commit.commitId === commitId);
538
+ if (!belongs) {
539
+ refuse("invalid-request", `Commit ${short(commitId)} is not one of the last ${OS_READ_FILES_HISTORY_DEPTH} commits of ${where(request)}, so its files were not read.`);
540
+ }
541
+ const read = await os.readFiles({ workspaceId, commitId });
542
+ const fingerprint = await fingerprintFiles(read.files);
543
+ // Who the OS recorded as the commit's author, for the Hub's "Edited in Overvues" commit. A read that fails leaves it out.
544
+ const recorded = (await os.readCommits({ workspaceId, fromCommit: commitId, depth: 1 }).catch(() => []))[0];
545
+ const author = recorded?.commitId === commitId && isOsCommitAuthor(recorded.author) ? { name: recorded.author.name, email: recorded.author.email } : null;
546
+ const upload = {
547
+ protocol: OS_PROTOCOL, machineId: this.options.machineId, requestId, workspaceId, gadgetId, commitId, fingerprint,
548
+ files: read.files, notRead: read.notRead,
549
+ };
550
+ let status;
551
+ try {
552
+ status = (await this.options.hubFetch(readFilesUploadPath(requestId), { method: "POST", body: JSON.stringify(upload) })).status;
553
+ }
554
+ catch {
555
+ return refuse("machine", `The files were read from ${OS.the} but could not be sent to the Hub.`);
556
+ }
557
+ if (status < 200 || status >= 300)
558
+ refuse("machine", `The files were read from ${OS.the} but the Hub did not accept them (HTTP ${status}).`);
559
+ const fileCount = Object.keys(read.files).length;
560
+ this.log(`${OS.noun}: read ${fileCount} file(s) of ${where(request)} at ${short(commitId)} for a pull and sent them to the Hub`);
561
+ return { workspaceId, gadgetId, commitId, fingerprint, fileCount, notRead: read.notRead, ...(author ? { author } : {}) };
562
+ });
563
+ }
564
+ readConnections(request, stored) {
565
+ const { workspaceId, gadgetId } = request;
566
+ return this.withOs(stored, async (os) => {
567
+ const bindings = await os.readConnections({ workspaceId, gadgetId });
568
+ this.log(`${OS.noun}: read the connections of ${where(request)} for the connection report: ${bindings.connections.length} connection(s), ${bindings.otherBindings.length} other binding(s)`);
569
+ return { workspaceId, gadgetId, connections: bindings.connections, otherBindings: bindings.otherBindings };
570
+ });
571
+ }
572
+ // ── Previews ───────────────────────────────────────────────────────
573
+ async previewPublish(request, stored, requestId) {
574
+ const subject = osRequestSubject(request);
575
+ const { approvalId, previewId, live, fingerprint } = request;
576
+ const earlier = this.repeatOf("approval", approvalId, subject, `the approval ${approvalId}`);
577
+ if (earlier)
578
+ return { ...earlier.result, repeated: true };
579
+ await this.requireApproval(request, requestId);
580
+ const files = await this.builtFiles(fingerprint);
581
+ this.requireBuiltHere(request.appId, fingerprint);
582
+ return this.withOs(stored, (os) => this.acting("approval", approvalId, "preview-publish", subject, async (entry, save) => {
583
+ const created = await os.createPreview({
584
+ ...(request.previewsWorkspaceId ? { workspaceId: request.previewsWorkspaceId } : {}),
585
+ previewId, live, appTitle: request.appTitle, branchName: request.branchName,
586
+ });
587
+ const preview = { workspaceId: created.workspaceId, gadgetId: created.gadgetId, createdWorkspace: created.createdWorkspace, created: created.created };
588
+ entry.preview = preview;
589
+ save();
590
+ const ref = { workspaceId: created.workspaceId, gadgetId: created.gadgetId, previewId, live };
591
+ // Its connections first, so the server published next starts with them. A connection that
592
+ // could not be given is said in the answer and does not hold the publish.
593
+ let connections;
594
+ try {
595
+ connections = { connections: await this.connectPreview(os, ref, request.branchName, request.turnedOn), problem: null };
596
+ }
597
+ catch (error) {
598
+ const refused = this.toRefusal(error);
599
+ this.log(`${OS.noun}: preview ${previewId}: its connections were not given: ${refused.message}`);
600
+ connections = { connections: [], problem: refused.message };
601
+ }
602
+ let publish = await os.publishPreview({ ...ref, baseCommit: created.headCommit, files, fingerprint });
603
+ let retried = false;
604
+ if (publish.outcome === "published-head-differs") {
605
+ // Something else landed in the preview at that moment. Once more, from the new head.
606
+ retried = true;
607
+ publish = await os.publishPreview({ ...ref, baseCommit: publish.headCommit, files, fingerprint });
608
+ }
609
+ const how = publish.outcome === "published" ? `published, commit ${short(publish.commitId)}${publish.server.loads ? "" : ", and its server does not load"}`
610
+ : publish.outcome === "stale" ? `not published: the preview changed in ${OS.the} meanwhile`
611
+ : "published, but the preview does not hold the built files; it is out of date";
612
+ this.log(`${OS.noun}: preview ${previewId} of ${where(live)}: ${created.created ? "created and " : ""}${how}${retried ? " (after one retry)" : ""}`);
613
+ return { preview: { ...preview, title: created.title }, connections, publish, retried, repeated: false };
614
+ }));
615
+ }
616
+ async previewConnect(request, stored, requestId) {
617
+ const subject = osRequestSubject(request);
618
+ const { approvalId, previewId, workspaceId, gadgetId, live } = request;
619
+ const earlier = this.repeatOf("approval", approvalId, subject, `the approval ${approvalId}`);
620
+ if (earlier)
621
+ return { ...earlier.result, repeated: true };
622
+ await this.requireApproval(request, requestId);
623
+ return this.withOs(stored, (os) => this.acting("approval", approvalId, "preview-connect", subject, async () => {
624
+ const connections = await this.connectPreview(os, { workspaceId, gadgetId, previewId, live }, request.branchName, request.turnedOn);
625
+ return { connections, problem: null, repeated: false };
626
+ }));
627
+ }
628
+ /**
629
+ * Gives a preview what the catalog says it gets from the live app's connections, as the OS
630
+ * has them now, and takes away what it should no longer have: a connection turned off, or
631
+ * one the live app or the catalog no longer gives. A preview's own area is never taken away
632
+ * here (its files go with the preview's removal), and nothing gets automatic approval.
633
+ */
634
+ async connectPreview(os, ref, branchName, turnedOn) {
635
+ const catalog = previewCatalog();
636
+ const live = (await os.readConnections(ref.live)).connections;
637
+ const check = checkPreviewConnections({ catalog, live, branchName, previewId: ref.previewId, turnedOn: [...turnedOn] });
638
+ const bound = (await os.readConnections(ref)).connections;
639
+ const wanted = new Map(check.connections.filter((answer) => answer.gets === "same-as-live" || answer.gets === "own-area").map((answer) => [answer.name, answer]));
640
+ const stale = bound.filter((connection) => {
641
+ const want = wanted.get(connection.name);
642
+ return !want || place(want.resource) !== place(connection.resource);
643
+ }).map((connection) => connection.name);
644
+ const taken = stale.length ? (await os.removePreviewConnections({ ...ref, names: stale })).connections : [];
645
+ const kept = new Set(taken.filter((entry) => entry.state === "kept-own-area" || entry.state === "kept-shared").map((entry) => entry.name));
646
+ const toBind = check.connections.filter((answer) => !kept.has(answer.name));
647
+ const bindings = toBind.some((answer) => wanted.has(answer.name))
648
+ ? (await os.bindPreviewConnections({
649
+ ...ref,
650
+ connections: toBind.map((answer) => ({ name: answer.name, gets: answer.gets, ...(answer.resource ? { resource: answer.resource } : {}), automaticApproval: false })),
651
+ })).connections
652
+ : [];
653
+ const boundNow = new Map(bindings.map((entry) => [entry.name, entry]));
654
+ const result = check.connections.map((answer) => {
655
+ const base = {
656
+ name: answer.name, ...(answer.kindName ? { kindName: answer.kindName } : {}), gets: answer.gets, code: answer.code,
657
+ canTurnOn: answer.canTurnOn, ...(answer.area ? { area: answer.area } : {}),
658
+ };
659
+ if (kept.has(answer.name))
660
+ return { ...base, state: "kept", reason: `${answer.reason} ${KEPT_WORDS}` };
661
+ const done = boundNow.get(answer.name);
662
+ switch (done?.state) {
663
+ case "bound":
664
+ case "already-bound": return { ...base, state: "connected", reason: answer.reason };
665
+ case "needs-account": return {
666
+ ...base, state: "needs-account",
667
+ reason: `${answer.name}: more than one of your accounts in ${OS.the} can reach it, so none was chosen and it is not connected.`,
668
+ };
669
+ case "no-account": return {
670
+ ...base, state: "no-account",
671
+ reason: `${answer.name}: none of your accounts in ${OS.the} can reach it, so it is not connected. `
672
+ + `Open the live app's Connections in ${OS.the}, allow this connection for your account, then update the preview.`,
673
+ };
674
+ case "refused-by-os": return { ...base, state: "refused", reason: `${answer.name}: ${OS.the} refused to make this connection for the preview.` };
675
+ case "refused-nested": return { ...base, state: "refused", reason: done.why };
676
+ default: return { ...base, state: "not-connected", reason: answer.reason };
677
+ }
678
+ });
679
+ // What the preview had that the live app no longer has.
680
+ for (const connection of bound) {
681
+ if (check.connections.some((answer) => answer.name === connection.name))
682
+ continue;
683
+ const stays = kept.has(connection.name);
684
+ result.push({
685
+ name: connection.name, gets: "nothing", code: "not-live", canTurnOn: false, state: stays ? "kept" : "not-connected",
686
+ reason: `${connection.name}: the live app no longer has this connection, so the preview does not get it.${stays ? ` ${KEPT_WORDS}` : ""}`,
687
+ });
688
+ }
689
+ const words = result.map((entry) => `${entry.name} ${entry.state === "connected" ? entry.gets : entry.state}`).join(", ") || "none";
690
+ const away = taken.filter((entry) => entry.state === "removed").map((entry) => entry.name);
691
+ this.log(`${OS.noun}: preview ${ref.previewId}: connections: ${words}${away.length ? `; took away ${away.join(", ")}` : ""}`);
692
+ return result;
693
+ }
694
+ async previewRemove(request, stored, requestId) {
695
+ const subject = osRequestSubject(request);
696
+ const { approvalId, previewId, workspaceId, gadgetId, live } = request;
697
+ // A removal that stopped for the person's approval in the OS goes on under the same approval.
698
+ const before = this.recorded("approval", approvalId);
699
+ const goingOn = before?.subject === subject && before.state === "done"
700
+ && before.result?.outcome === "waiting-for-approval";
701
+ if (!goingOn) {
702
+ const earlier = this.repeatOf("approval", approvalId, subject, `the approval ${approvalId}`);
703
+ if (earlier)
704
+ return { ...earlier.result, repeated: true };
705
+ await this.requireApproval(request, requestId);
706
+ }
707
+ return this.withOs(stored, (os) => this.acting("approval", approvalId, "preview-remove", subject, async () => {
708
+ // The hub does not know the preview's app (its publish got no answer): look for it by its id.
709
+ const found = gadgetId === null ? await os.findPreview({ workspaceId, previewId, live }) : { workspaceId, gadgetId };
710
+ if (!found) {
711
+ this.log(`${OS.noun}: preview ${previewId}: not in the Previews workspace ${workspaceId}; nothing to remove`);
712
+ return { outcome: "removed", alreadyGone: true, steps: [], left: [], repeated: false };
713
+ }
714
+ const result = await os.removePreview({ workspaceId, gadgetId: found.gadgetId, previewId, live });
715
+ this.log(result.outcome === "removed"
716
+ ? `${OS.noun}: preview ${previewId} (${where({ workspaceId, gadgetId: found.gadgetId })}): ${result.alreadyGone ? "already gone" : "removed"}`
717
+ : `${OS.noun}: preview ${previewId} (${where({ workspaceId, gadgetId: found.gadgetId })}): not removed yet; deleting its files waits for approval in ${OS.the}`);
718
+ return { ...result, repeated: false };
719
+ }));
720
+ }
721
+ // ── The watch ──────────────────────────────────────────────────────
722
+ async setWatch(raw) {
723
+ const parsed = parseOsWatchMessage(raw);
724
+ if (!parsed.ok || parsed.value.machineId !== this.options.machineId) {
725
+ this.log(`${OS.noun}: refused a watch list from the Hub: ${parsed.ok ? "it is for another machine" : parsed.message}`);
726
+ return;
727
+ }
728
+ const next = new Map();
729
+ for (const app of parsed.value.apps) {
730
+ const before = this.watched.get(watchKey(app));
731
+ next.set(watchKey(app), {
732
+ app: { workspaceId: app.workspaceId, gadgetId: app.gadgetId },
733
+ known: app.lastCommit ?? before?.known,
734
+ seen: before?.seen ?? new Set(),
735
+ problem: false,
736
+ });
737
+ }
738
+ this.watched = next;
739
+ this.watchSeconds = parsed.value.intervalSeconds ?? OS_WATCH_DEFAULT_SECONDS;
740
+ if (this.watchTimer)
741
+ clearInterval(this.watchTimer);
742
+ this.watchTimer = null;
743
+ if (next.size) {
744
+ this.watchTimer = setInterval(() => { void this.pollWatched().catch(() => { }); }, this.watchSeconds * 1000);
745
+ this.watchTimer.unref?.();
746
+ }
747
+ this.log(`${OS.noun}: watching ${next.size} app(s) for edits made in ${OS.the}, every ${this.watchSeconds} s`);
748
+ if (parsed.value.checkNow)
749
+ await this.pollWatched();
750
+ }
751
+ async readWatched() {
752
+ if (!this.watched.size)
753
+ return;
754
+ let stored;
755
+ try {
756
+ stored = this.options.sessions.load();
757
+ }
758
+ catch {
759
+ stored = null;
760
+ }
761
+ this.reportStatus();
762
+ if (!stored || osSessionExpiry(stored.expiresAt, this.now()).state === "expired")
763
+ return;
764
+ this.secret = stored.session;
765
+ const apps = [...this.watched.values()];
766
+ const problem = (watched, error) => {
767
+ if (watched.problem)
768
+ return;
769
+ watched.problem = true;
770
+ const refused = this.toRefusal(error);
771
+ this.log(`${OS.noun}: could not read the head of ${where(watched.app)}: ${refused.message}`);
772
+ this.send({ type: "os-watch-problem", machineId: this.options.machineId, ...watched.app, refused });
773
+ };
774
+ try {
775
+ await this.withOs(stored, async (os) => {
776
+ for (const watched of apps) {
777
+ try {
778
+ const head = await os.readHead(watched.app);
779
+ watched.problem = false;
780
+ watched.seen.add(head.commitId);
781
+ if (watched.seen.size > 50)
782
+ watched.seen.delete(watched.seen.values().next().value);
783
+ if (head.commitId === watched.known)
784
+ continue;
785
+ this.log(`${OS.noun}: ${where(watched.app)} has a new head, ${short(head.commitId)}; told the Hub`);
786
+ this.send({
787
+ type: "os-head-changed", machineId: this.options.machineId, ...watched.app, commitId: head.commitId,
788
+ ...(watched.known ? { previousCommit: watched.known } : {}),
789
+ });
790
+ watched.known = head.commitId;
791
+ }
792
+ catch (error) {
793
+ problem(watched, error);
794
+ }
795
+ }
796
+ });
797
+ }
798
+ catch (error) {
799
+ for (const watched of apps)
800
+ problem(watched, error);
801
+ }
802
+ finally {
803
+ this.secret = null;
804
+ }
805
+ }
806
+ }
807
+ /** The machine's side as `tiller host` runs it: the session and the record in `tiller`'s config folder. */
808
+ export function createOsMachine(input) {
809
+ return new OsMachine({
810
+ machineId: input.machineId,
811
+ // The Mac keychain once a session is kept there, otherwise the file. A locked keychain is "unlock", never the file instead.
812
+ sessions: machineOsSessionStore(input.configDir),
813
+ acted: fileActedRecord(input.configDir),
814
+ hubFetch: hubFetcher(input.hubUrl, input.hubHeaders),
815
+ builtHere: (appId, fingerprint) => findGadgetBuildRecord(input.localStateDir, appId, fingerprint) !== null,
816
+ log: input.log,
817
+ });
818
+ }