@parcha/agentrun-computer 0.1.0-beta.68 → 0.1.0-beta.69

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 (50) hide show
  1. package/README.md +55 -3
  2. package/box/README.md +8 -0
  3. package/box/install.sh +40 -2
  4. package/dist/boxd.js +43 -8
  5. package/dist/computerd-linux-amd64 +0 -0
  6. package/dist/computerd-linux-arm64 +0 -0
  7. package/dist/computerd.json +1 -1
  8. package/dist/conformance/cli.d.ts +6 -0
  9. package/dist/conformance/cli.js +14 -3
  10. package/dist/conformance/northflank.d.ts +44 -0
  11. package/dist/conformance/northflank.js +50 -0
  12. package/dist/descriptor.d.ts +6 -0
  13. package/dist/descriptor.js +2 -0
  14. package/dist/descriptors/agent37.d.ts +1 -0
  15. package/dist/descriptors/agent37.js +2 -0
  16. package/dist/descriptors/daytona.d.ts +1 -0
  17. package/dist/descriptors/daytona.js +2 -0
  18. package/dist/descriptors/e2b.d.ts +1 -0
  19. package/dist/descriptors/e2b.js +2 -0
  20. package/dist/descriptors/fly.d.ts +1 -0
  21. package/dist/descriptors/fly.js +2 -0
  22. package/dist/descriptors/groundbox.d.ts +1 -0
  23. package/dist/descriptors/groundbox.js +2 -0
  24. package/dist/descriptors/index.d.ts +34 -1
  25. package/dist/descriptors/index.js +3 -2
  26. package/dist/descriptors/instabox.d.ts +1 -0
  27. package/dist/descriptors/instabox.js +2 -0
  28. package/dist/descriptors/modal.d.ts +1 -0
  29. package/dist/descriptors/modal.js +2 -0
  30. package/dist/descriptors/northflank.d.ts +24 -0
  31. package/dist/descriptors/northflank.js +30 -0
  32. package/dist/descriptors/orgo.d.ts +1 -0
  33. package/dist/descriptors/orgo.js +2 -0
  34. package/dist/descriptors/render.d.ts +1 -0
  35. package/dist/descriptors/render.js +2 -0
  36. package/dist/groundbox.js +15 -2
  37. package/dist/hosted/download.d.ts +16 -0
  38. package/dist/hosted/download.js +108 -2
  39. package/dist/hosted/provider.js +11 -4
  40. package/dist/hosted/scripts.d.ts +2 -0
  41. package/dist/hosted/scripts.js +29 -0
  42. package/dist/northflank.d.ts +44 -0
  43. package/dist/northflank.js +448 -0
  44. package/dist/orgo-desktop.js +2 -2
  45. package/dist/orgo-names.d.ts +15 -1
  46. package/dist/orgo-names.js +13 -0
  47. package/dist/orgo.d.ts +10 -0
  48. package/dist/orgo.js +64 -4
  49. package/dist/providers.d.ts +10 -4
  50. package/package.json +7 -2
@@ -12,6 +12,7 @@ import { KERNEL } from "./kernel.js";
12
12
  import { LOCAL } from "./local-chrome.js";
13
13
  import { MODAL } from "./modal.js";
14
14
  import { MORPH } from "./morph.js";
15
+ import { NORTHFLANK } from "./northflank.js";
15
16
  import { ORGO } from "./orgo.js";
16
17
  import { PHONE_HARNESS } from "./phone-harness.js";
17
18
  import { RENDER } from "./render.js";
@@ -77,6 +78,7 @@ export declare const DESCRIPTORS: readonly [{
77
78
  }];
78
79
  readonly connect: "either";
79
80
  readonly metered: true;
81
+ readonly sized: true;
80
82
  readonly mounts: readonly [];
81
83
  readonly unmounted: {
82
84
  readonly archil: "Archil mounts don't work on Daytona's hosted cloud, where Archil's client hangs on its sandboxes; a self-hosted Daytona target takes one once its operator sets DAYTONA_ARCHIL_EGRESS";
@@ -136,6 +138,7 @@ export declare const DESCRIPTORS: readonly [{
136
138
  }];
137
139
  readonly connect: "either";
138
140
  readonly metered: true;
141
+ readonly sized: true;
139
142
  readonly mounts: readonly ["archil"];
140
143
  readonly network: readonly ["none", "open", "allow"];
141
144
  readonly capabilities: readonly [{
@@ -190,6 +193,7 @@ export declare const DESCRIPTORS: readonly [{
190
193
  }];
191
194
  readonly connect: "either";
192
195
  readonly metered: true;
196
+ readonly sized: true;
193
197
  readonly mounts: readonly ["archil"];
194
198
  readonly network: readonly ["none", "open", "allow"];
195
199
  readonly capabilities: readonly [import("../kinds.js").KindCapabilities, import("../kinds.js").KindCapabilities];
@@ -212,6 +216,7 @@ export declare const DESCRIPTORS: readonly [{
212
216
  readonly option: "secrets";
213
217
  };
214
218
  readonly metered: true;
219
+ readonly sized: true;
215
220
  readonly mounts: readonly ["archil"];
216
221
  readonly network: readonly ["none", "open", "allow"];
217
222
  readonly capabilities: readonly [import("../kinds.js").KindCapabilities, {
@@ -287,6 +292,7 @@ export declare const DESCRIPTORS: readonly [{
287
292
  readonly required: true;
288
293
  }];
289
294
  readonly metered: true;
295
+ readonly sized: true;
290
296
  readonly connect: "dial";
291
297
  readonly mounts: readonly [];
292
298
  readonly network: readonly ["none", "open", "allow"];
@@ -318,6 +324,7 @@ export declare const DESCRIPTORS: readonly [{
318
324
  readonly required: true;
319
325
  }];
320
326
  readonly metered: true;
327
+ readonly sized: true;
321
328
  readonly connect: "dial";
322
329
  readonly mounts: readonly [];
323
330
  readonly network: readonly ["none", "open", "allow"];
@@ -338,6 +345,7 @@ export declare const DESCRIPTORS: readonly [{
338
345
  readonly picked: true;
339
346
  }];
340
347
  readonly metered: true;
348
+ readonly sized: true;
341
349
  readonly mounts: readonly [];
342
350
  readonly unmounted: {
343
351
  readonly sync: "Orgo's provider makes no /workspace for a synced folder yet";
@@ -547,6 +555,7 @@ export declare const DESCRIPTORS: readonly [{
547
555
  readonly required: true;
548
556
  }];
549
557
  readonly metered: true;
558
+ readonly sized: true;
550
559
  readonly mounts: readonly [];
551
560
  readonly network: readonly ["none", "open", "allow"];
552
561
  readonly capabilities: readonly [import("../kinds.js").KindCapabilities, {
@@ -611,6 +620,7 @@ export declare const DESCRIPTORS: readonly [{
611
620
  readonly required: true;
612
621
  }];
613
622
  readonly metered: true;
623
+ readonly sized: true;
614
624
  readonly mounts: readonly [];
615
625
  readonly network: readonly ["none", "open", "allow"];
616
626
  readonly capabilities: readonly [import("../kinds.js").KindCapabilities];
@@ -618,8 +628,31 @@ export declare const DESCRIPTORS: readonly [{
618
628
  readonly sandbox: "minimal";
619
629
  };
620
630
  readonly load: () => Promise<typeof import("../instabox.js")>;
631
+ }, {
632
+ readonly id: "northflank";
633
+ readonly label: "Northflank";
634
+ readonly keys: readonly [{
635
+ readonly name: "NORTHFLANK_API_TOKEN";
636
+ readonly option: "token";
637
+ readonly required: true;
638
+ }, {
639
+ readonly name: "NORTHFLANK_COMPUTER_PROJECT_ID";
640
+ readonly option: "projectId";
641
+ readonly required: true;
642
+ }, {
643
+ readonly name: "NORTHFLANK_COMPUTER_TEAM_ID";
644
+ readonly option: "teamId";
645
+ readonly required: false;
646
+ }];
647
+ readonly metered: true;
648
+ readonly sized: true;
649
+ readonly mounts: readonly [];
650
+ readonly network: readonly ["none", "open", "allow"];
651
+ readonly capabilities: readonly [import("../kinds.js").KindCapabilities];
652
+ readonly prepareImage: true;
653
+ readonly load: () => Promise<typeof import("../northflank.js")>;
621
654
  }];
622
- export { AGENT37, BOXD, BROWSERBASE, CLOUDFLARE, DAYTONA, DOCKER, E2B, FLY, GROUNDBOX, INSTABOX, KERNEL, LOCAL, MODAL, MORPH, ORGO, PHONE_HARNESS, RENDER, SMOLCLOUD, SPRITES, VERCEL };
655
+ export { AGENT37, BOXD, BROWSERBASE, CLOUDFLARE, DAYTONA, DOCKER, E2B, FLY, GROUNDBOX, INSTABOX, KERNEL, LOCAL, MODAL, MORPH, NORTHFLANK, ORGO, PHONE_HARNESS, RENDER, SMOLCLOUD, SPRITES, VERCEL };
623
656
  export { AGENT37_BASE_TEMPLATE } from "./agent37.js";
624
657
  export { BOXD_DEFAULT_IMAGE } from "./boxd.js";
625
658
  export { GROUNDBOX_OWN_IMAGE } from "./groundbox.js";
@@ -14,14 +14,15 @@ import { KERNEL } from "./kernel.js";
14
14
  import { LOCAL } from "./local-chrome.js";
15
15
  import { MODAL } from "./modal.js";
16
16
  import { MORPH } from "./morph.js";
17
+ import { NORTHFLANK } from "./northflank.js";
17
18
  import { ORGO } from "./orgo.js";
18
19
  import { PHONE_HARNESS } from "./phone-harness.js";
19
20
  import { RENDER } from "./render.js";
20
21
  import { SMOLCLOUD } from "./smolcloud.js";
21
22
  import { SPRITES } from "./sprites.js";
22
23
  import { VERCEL } from "./vercel.js";
23
- export const DESCRIPTORS = [DOCKER, DAYTONA, MODAL, E2B, AGENT37, MORPH, CLOUDFLARE, FLY, RENDER, ORGO, PHONE_HARNESS, BROWSERBASE, KERNEL, LOCAL, BOXD, SMOLCLOUD, GROUNDBOX, VERCEL, SPRITES, INSTABOX];
24
- export { AGENT37, BOXD, BROWSERBASE, CLOUDFLARE, DAYTONA, DOCKER, E2B, FLY, GROUNDBOX, INSTABOX, KERNEL, LOCAL, MODAL, MORPH, ORGO, PHONE_HARNESS, RENDER, SMOLCLOUD, SPRITES, VERCEL };
24
+ export const DESCRIPTORS = [DOCKER, DAYTONA, MODAL, E2B, AGENT37, MORPH, CLOUDFLARE, FLY, RENDER, ORGO, PHONE_HARNESS, BROWSERBASE, KERNEL, LOCAL, BOXD, SMOLCLOUD, GROUNDBOX, VERCEL, SPRITES, INSTABOX, NORTHFLANK];
25
+ export { AGENT37, BOXD, BROWSERBASE, CLOUDFLARE, DAYTONA, DOCKER, E2B, FLY, GROUNDBOX, INSTABOX, KERNEL, LOCAL, MODAL, MORPH, NORTHFLANK, ORGO, PHONE_HARNESS, RENDER, SMOLCLOUD, SPRITES, VERCEL };
25
26
  export { AGENT37_BASE_TEMPLATE } from "./agent37.js";
26
27
  export { BOXD_DEFAULT_IMAGE } from "./boxd.js";
27
28
  export { GROUNDBOX_OWN_IMAGE } from "./groundbox.js";
@@ -9,6 +9,7 @@ export declare const INSTABOX: {
9
9
  readonly required: true;
10
10
  }];
11
11
  readonly metered: true;
12
+ readonly sized: true;
12
13
  readonly mounts: readonly [];
13
14
  readonly network: readonly ["none", "open", "allow"];
14
15
  readonly capabilities: readonly [import("../kinds.js").KindCapabilities];
@@ -11,6 +11,8 @@ export const INSTABOX = defineProvider({
11
11
  label: "InstaBox",
12
12
  keys: [{ name: "INSTABOX_API_KEY", option: "apiKey", required: true }],
13
13
  metered: true,
14
+ // Its create answers the size the vendor runs the computer at (resources), which its time is priced by.
15
+ sized: true,
14
16
  mounts: [],
15
17
  // All three are init's, through root in the microVM (the machine user's passwordless sudo).
16
18
  network: ["none", "open", "allow"],
@@ -36,6 +36,7 @@ export declare const MODAL: {
36
36
  }];
37
37
  readonly connect: "either";
38
38
  readonly metered: true;
39
+ readonly sized: true;
39
40
  readonly mounts: readonly ["archil"];
40
41
  readonly network: readonly ["none", "open", "allow"];
41
42
  readonly capabilities: readonly [{
@@ -43,6 +43,8 @@ export const MODAL = defineProvider({
43
43
  ],
44
44
  connect: "either",
45
45
  metered: true,
46
+ // Its create answers the size the vendor runs the computer at (resources), which its time is priced by.
47
+ sized: true,
46
48
  mounts: ["archil"],
47
49
  // None: its programs run in a network of their own, their loopback alone (isolation.ts), and the sandbox is made with
48
50
  // its network blocked. Open and a list: the sandbox's network is not blocked, and its programs' one way out is init's
@@ -0,0 +1,24 @@
1
+ export declare const NORTHFLANK: {
2
+ readonly id: "northflank";
3
+ readonly label: "Northflank";
4
+ readonly keys: readonly [{
5
+ readonly name: "NORTHFLANK_API_TOKEN";
6
+ readonly option: "token";
7
+ readonly required: true;
8
+ }, {
9
+ readonly name: "NORTHFLANK_COMPUTER_PROJECT_ID";
10
+ readonly option: "projectId";
11
+ readonly required: true;
12
+ }, {
13
+ readonly name: "NORTHFLANK_COMPUTER_TEAM_ID";
14
+ readonly option: "teamId";
15
+ readonly required: false;
16
+ }];
17
+ readonly metered: true;
18
+ readonly sized: true;
19
+ readonly mounts: readonly [];
20
+ readonly network: readonly ["none", "open", "allow"];
21
+ readonly capabilities: readonly [import("../kinds.js").KindCapabilities];
22
+ readonly prepareImage: true;
23
+ readonly load: () => Promise<typeof import("../northflank.js")>;
24
+ };
@@ -0,0 +1,30 @@
1
+ // Northflank's descriptor: a Linux sandbox on a Northflank service (a Kata microVM on Northflank Cloud) in the customer's
2
+ // own project, started from our box image, the public registry tag its prepareImage() answers (box-image.ts), which
3
+ // Northflank pulls at each create. The token is under the name Northflank's own client reads (NORTHFLANK_API_TOKEN,
4
+ // @northflank/js-client). The project has no default, so Settings asks for it; its key is neither NF_PROJECT_ID, which
5
+ // Northflank injects into every service it runs, nor NORTHFLANK_PROJECT_ID, a name other tools use, so a server
6
+ // Northflank runs never takes its own project for the computers'. The team is for an organisation token alone. No Archil mount and no desktop yet: both are unverified until
7
+ // a live run.
8
+ import { defineProvider } from "../descriptor.js";
9
+ import { SANDBOX } from "./shared.js";
10
+ export const NORTHFLANK = defineProvider({
11
+ id: "northflank",
12
+ label: "Northflank",
13
+ keys: [
14
+ { name: "NORTHFLANK_API_TOKEN", option: "token", required: true },
15
+ { name: "NORTHFLANK_COMPUTER_PROJECT_ID", option: "projectId", required: true },
16
+ { name: "NORTHFLANK_COMPUTER_TEAM_ID", option: "teamId", required: false },
17
+ ],
18
+ metered: true,
19
+ // Its create answers the size the vendor runs the computer at (resources), which its time is priced by: Northflank's
20
+ // get-service (API docs, services/get-service) answers billing.deploymentPlan, required, and the plan maps to its
21
+ // vCPUs and MB.
22
+ sized: true,
23
+ mounts: [],
24
+ // All three are init's, through root in the microVM (unverified until the live run: root makes a network namespace).
25
+ network: ["none", "open", "allow"],
26
+ capabilities: [SANDBOX],
27
+ // No default image: every computer starts from the box image its prepareImage() answers, unless its spec names one.
28
+ prepareImage: true,
29
+ load: () => import("../northflank.js"),
30
+ });
@@ -60,6 +60,7 @@ export declare const ORGO: {
60
60
  readonly picked: true;
61
61
  }];
62
62
  readonly metered: true;
63
+ readonly sized: true;
63
64
  readonly mounts: readonly [];
64
65
  readonly unmounted: {
65
66
  readonly sync: "Orgo's provider makes no /workspace for a synced folder yet";
@@ -37,6 +37,8 @@ export const ORGO = defineProvider({
37
37
  { name: "ORGO_WORKSPACE_ID", option: "workspaceId", required: false, picked: true },
38
38
  ],
39
39
  metered: true,
40
+ // Its create answers the size the vendor runs the computer at (resources), which its time is priced by.
41
+ sized: true,
40
42
  // None yet: an Archil mount needs an Orgo template that bakes the helper, and a synced folder a /workspace its create
41
43
  // makes, which the shared hosted adapter's does and Orgo's own does not.
42
44
  mounts: [],
@@ -11,6 +11,7 @@ export declare const RENDER: {
11
11
  readonly required: true;
12
12
  }];
13
13
  readonly metered: true;
14
+ readonly sized: true;
14
15
  readonly connect: "dial";
15
16
  readonly mounts: readonly [];
16
17
  readonly network: readonly ["none", "open", "allow"];
@@ -13,6 +13,8 @@ export const RENDER = defineProvider({
13
13
  { name: "RENDER_WORKSPACE_ID", option: "ownerId", required: true },
14
14
  ],
15
15
  metered: true,
16
+ // Its create answers the size the vendor runs the computer at (resources), which its time is priced by.
17
+ sized: true,
16
18
  // Its machines dial the server: Render's runs take no stdin, so nothing reaches into one.
17
19
  connect: "dial",
18
20
  // None: a machine that dials takes no Archil mount (mountsOf).
package/dist/groundbox.js CHANGED
@@ -183,7 +183,20 @@ const QUEUE_HIGH = 8 * 1024 * 1024;
183
183
  const QUEUE_LOW = 2 * 1024 * 1024;
184
184
  /** The bytes a chunk of base64 holds. */
185
185
  const bytesOf = (base64) => Math.floor((base64.length * 3) / 4);
186
- /** A sandbox as the adapter reads a machine; none for a record Groundbox keeps of one that has ended. */
186
+ /** A figure of the answer's size: a whole number above 0, else none (never a guess). */
187
+ const sized = (value) => (isWhole(value) && value > 0 ? value : undefined);
188
+ /**
189
+ * The size Groundbox says a sandbox runs at (its cpus, memory_gb and disk_gb), read from its answer over anything the
190
+ * create asked: a spec that names no size gets Groundbox's own, which only the answer says. None where it says none.
191
+ */
192
+ function resourcesOf(sandbox) {
193
+ const cpus = sized(sandbox.cpus);
194
+ const memoryGb = sized(sandbox.memory_gb);
195
+ const diskGb = sized(sandbox.disk_gb);
196
+ const resources = { ...(cpus === undefined ? {} : { cpus }), ...(memoryGb === undefined ? {} : { memoryMb: memoryGb * 1024 }), ...(diskGb === undefined ? {} : { diskGb }) };
197
+ return Object.keys(resources).length > 0 ? { resources } : {};
198
+ }
199
+ /** A sandbox as the adapter reads a machine, with the size it runs at; none for a record Groundbox keeps of one that has ended. */
187
200
  function machineOf(sandbox) {
188
201
  const labels = { ...(sandbox.metadata ?? {}) };
189
202
  // A status this host does not know is a machine that is there and not running: the janitor still finds and ends it.
@@ -193,7 +206,7 @@ function machineOf(sandbox) {
193
206
  if (state === undefined)
194
207
  return undefined;
195
208
  const created = dateOf(sandbox.created_at);
196
- return { id: sandbox.id, labels, state, ...(created ? { createdAt: created } : {}) };
209
+ return { id: sandbox.id, labels, state, ...(created ? { createdAt: created } : {}), ...resourcesOf(sandbox) };
197
210
  }
198
211
  /** Why Groundbox's metadata can't hold these labels, in words; undefined when it can. */
199
212
  function labelProblem(labels) {
@@ -6,6 +6,13 @@ export interface DownloadBounds {
6
6
  /** The download of one copy. */
7
7
  downloadMs: number;
8
8
  }
9
+ /**
10
+ * The most bytes the pulls' copies of one connection hold at once. A copy is made one at a time, with what the copies
11
+ * still held leave of this as its tar's file-size limit (tree-stage's, in 512-byte blocks): a tar past it is cut off,
12
+ * and its pull is the daemon's. A copy is held from its download until its pull's end, its last read, or the
13
+ * connection's close, so a pull read once and left holds its room until then.
14
+ */
15
+ export declare const TREE_COPY_BYTES: number;
9
16
  /**
10
17
  * connection, with every files.read past one chunk read from one downloaded copy after its first chunk. A first chunk
11
18
  * (offset 0, a whole chunk) whose file runs past it, and is at most the surface's read limit (MAX_READ_BYTES, all a copy
@@ -19,5 +26,14 @@ export interface DownloadBounds {
19
26
  * host with no download. The copy in the machine is removed once it is downloaded, without holding up the read. A close
20
27
  * of the connection or of its client lets every copy go: each read after it, or still waiting on a copy, is the closed
21
28
  * daemon client's (gone).
29
+ *
30
+ * A tar.pull of an absolute path outside /proc is read from a copy too. The daemon begins it, so a path it refuses is
31
+ * never copied; at the pull's first read, tree-stage makes its tar as the daemon's user (one pull's at a time per
32
+ * connection, within the room the copies still held leave of TREE_COPY_BYTES), it is downloaded and removed from the
33
+ * machine, and the daemon's own pull is ended. Every read of the pull is then answered from the copy, as the daemon answers (maxBytes of it, fewer only at
34
+ * its end, which eof marks), and the copy goes at its end, at tar.pull.end, or at the connection's close. A copy that
35
+ * fails at any step (the daemon's user unread, tree-stage refused or cut off, the download) leaves the pull the
36
+ * daemon's, from its first byte, since nothing of the copy was answered yet. A call of another method does not let a
37
+ * pull's copy go: once read from, the daemon's pull is ended, and the copy is the one tar of the pull there is.
22
38
  */
23
39
  export declare function downloadingConnection(host: MachineHost, download: NonNullable<MachineHost["download"]>, id: string, connection: ComputerConnection, bounds: DownloadBounds): ComputerConnection;
@@ -2,14 +2,16 @@
2
2
  // every read, and the rest comes from one copy of the file, made as the daemon's user (read-stage) and downloaded whole.
3
3
  // On Daytona the bridge is a session command's log, read at about 0.3 MB/s (the sandbox bench, 11-25); its toolbox's
4
4
  // files/download is not. The daemon's read is the check: a file it refuses is never copied, and the copy, opened as its
5
- // user, holds what that user can read and nothing more, whatever the path names by the time it is copied.
5
+ // user, holds what that user can read and nothing more, whatever the path names by the time it is copied. A tar.pull
6
+ // takes the same way out: once the daemon has begun it (its check), the tar is made as the daemon's user (tree-stage)
7
+ // and downloaded whole, and its reads are answered from that copy.
6
8
  import { randomBytes } from "node:crypto";
7
9
  import { posix } from "node:path";
8
10
  import { MAX_CHUNK_BYTES } from "../protocol.js";
9
11
  import { ok } from "../result.js";
10
12
  import { MAX_READ_BYTES } from "../surface/core.js";
11
13
  import { hostCall, runToExit, untilAborted } from "./process.js";
12
- import { READ_COPIES, readStage, readUnstage } from "./scripts.js";
14
+ import { READ_COPIES, readStage, readUnstage, treeStage } from "./scripts.js";
13
15
  /**
14
16
  * An absolute path as computerd's files.read opens it: cleaned first (filepath.Clean, files.go cleanPath), so a ".."
15
17
  * takes off the name before it, a symlinked directory's or not. read-stage's cat opens the path as given, where a ".."
@@ -20,6 +22,13 @@ const cleaned = (path) => posix.resolve(path);
20
22
  const copyable = ({ path, length }) => path.startsWith("/") && !cleaned(path).startsWith("/proc/") && (length === undefined || length === MAX_CHUNK_BYTES);
21
23
  /** The bytes one connection's copies may hold at once: two files at the read limit. */
22
24
  const COPIES_BYTES = 2 * MAX_READ_BYTES;
25
+ /**
26
+ * The most bytes the pulls' copies of one connection hold at once. A copy is made one at a time, with what the copies
27
+ * still held leave of this as its tar's file-size limit (tree-stage's, in 512-byte blocks): a tar past it is cut off,
28
+ * and its pull is the daemon's. A copy is held from its download until its pull's end, its last read, or the
29
+ * connection's close, so a pull read once and left holds its room until then.
30
+ */
31
+ export const TREE_COPY_BYTES = 256 * 1024 * 1024;
23
32
  /**
24
33
  * connection, with every files.read past one chunk read from one downloaded copy after its first chunk. A first chunk
25
34
  * (offset 0, a whole chunk) whose file runs past it, and is at most the surface's read limit (MAX_READ_BYTES, all a copy
@@ -33,10 +42,23 @@ const COPIES_BYTES = 2 * MAX_READ_BYTES;
33
42
  * host with no download. The copy in the machine is removed once it is downloaded, without holding up the read. A close
34
43
  * of the connection or of its client lets every copy go: each read after it, or still waiting on a copy, is the closed
35
44
  * daemon client's (gone).
45
+ *
46
+ * A tar.pull of an absolute path outside /proc is read from a copy too. The daemon begins it, so a path it refuses is
47
+ * never copied; at the pull's first read, tree-stage makes its tar as the daemon's user (one pull's at a time per
48
+ * connection, within the room the copies still held leave of TREE_COPY_BYTES), it is downloaded and removed from the
49
+ * machine, and the daemon's own pull is ended. Every read of the pull is then answered from the copy, as the daemon answers (maxBytes of it, fewer only at
50
+ * its end, which eof marks), and the copy goes at its end, at tar.pull.end, or at the connection's close. A copy that
51
+ * fails at any step (the daemon's user unread, tree-stage refused or cut off, the download) leaves the pull the
52
+ * daemon's, from its first byte, since nothing of the copy was answered yet. A call of another method does not let a
53
+ * pull's copy go: once read from, the daemon's pull is ended, and the copy is the one tar of the pull there is.
36
54
  */
37
55
  export function downloadingConnection(host, download, id, connection, bounds) {
38
56
  const { client } = connection;
39
57
  const copies = new Map();
58
+ /** Pulls the daemon began, by its id. */
59
+ const trees = new Map();
60
+ /** The last pull's copy, so the next starts after it: one at a time. */
61
+ let treeTurn = Promise.resolve();
40
62
  let closed = false;
41
63
  let user;
42
64
  /** The daemon's uid and gid, from its own /proc/self/status: each of its four ids the same, else none. */
@@ -70,6 +92,49 @@ export function downloadingConnection(host, download, id, connection, bounds) {
70
92
  unstage();
71
93
  return got.ok && got.value.length === size ? got.value : undefined;
72
94
  };
95
+ /** The tar of path's pull, made as the daemon's user within `blocks` 512-byte blocks and downloaded until signal
96
+ * aborts: its bytes, else undefined. */
97
+ const treeCopyOf = async (path, blocks, signal) => {
98
+ const who = await userOf();
99
+ if (who === undefined || signal.aborted)
100
+ return undefined;
101
+ const copy = `${READ_COPIES}/${randomBytes(16).toString("hex")}`;
102
+ const unstage = () => void runToExit(host, id, readUnstage(copy), { ms: bounds.execMs, what: `removing the tar of ${path}` });
103
+ const staged = await runToExit(host, id, treeStage(who[0], who[1], path, copy, blocks), { ms: bounds.execMs, signal, what: `making the tar of ${path} as the daemon's user` });
104
+ if (!staged.ok || staged.value.exitCode !== 0) {
105
+ if (!staged.ok)
106
+ unstage();
107
+ return undefined;
108
+ }
109
+ const got = await hostCall(host, `downloading the tar of ${path}`, (ctx) => download(id, copy, ctx), bounds.downloadMs, signal);
110
+ unstage();
111
+ return got.ok ? got.value : undefined;
112
+ };
113
+ /** The 512-byte blocks the copies still held leave of TREE_COPY_BYTES. */
114
+ const roomBlocks = () => {
115
+ let held = 0;
116
+ for (const each of trees.values())
117
+ held += each.held ?? 0;
118
+ return Math.floor((TREE_COPY_BYTES - held) / 512);
119
+ };
120
+ /** pullId's copy, one at a time: made after the last pull's copy settles, within the room the copies still held leave;
121
+ * once it is held, the daemon's pull is ended. */
122
+ const treeBytes = (pullId, tree) => {
123
+ const made = treeTurn.then(() => {
124
+ const blocks = roomBlocks();
125
+ return tree.stop.signal.aborted || blocks < 1 ? undefined : treeCopyOf(tree.path, blocks, tree.stop.signal);
126
+ });
127
+ // The turn waits for the copy and keeps none of its bytes.
128
+ treeTurn = made.then(() => undefined, () => undefined);
129
+ return made.then(async (bytes) => {
130
+ if (bytes === undefined || tree.stop.signal.aborted)
131
+ return undefined;
132
+ tree.held = bytes.length;
133
+ // The daemon's tar is no longer read: its pull is ended, and frees its slot of the daemon's MaxPulls.
134
+ await client.call("tar.pull.end", { id: pullId }).catch(() => undefined);
135
+ return bytes;
136
+ }, () => undefined);
137
+ };
73
138
  /** key's copy let go, and stopped if it is still coming. */
74
139
  const letGo = (key) => {
75
140
  copies.get(key)?.stop.abort();
@@ -82,9 +147,50 @@ export function downloadingConnection(host, download, id, connection, bounds) {
82
147
  const drop = () => {
83
148
  closed = true;
84
149
  letAllGo();
150
+ for (const tree of trees.values())
151
+ tree.stop.abort();
152
+ trees.clear();
153
+ };
154
+ /** A tar.pull's call: begin noted for a copy, read answered from the copy where it is held, end lets it go. */
155
+ const pullCall = async (method, params, options) => {
156
+ if (method === "tar.pull.begin") {
157
+ const begun = await client.call(method, params, options);
158
+ const { path } = params;
159
+ if (begun.ok && !closed && path.startsWith("/") && !cleaned(path).startsWith("/proc/")) {
160
+ trees.set(begun.value.id, { path: cleaned(path), stop: new AbortController(), read: 0 });
161
+ }
162
+ return begun;
163
+ }
164
+ if (method === "tar.pull.end") {
165
+ const { id: pullId } = params;
166
+ trees.get(pullId)?.stop.abort();
167
+ trees.delete(pullId);
168
+ return client.call(method, params, options);
169
+ }
170
+ const { id: pullId, maxBytes } = params;
171
+ const tree = trees.get(pullId);
172
+ const size = maxBytes ?? MAX_CHUNK_BYTES;
173
+ if (tree === undefined || !Number.isSafeInteger(size) || size < 1 || size > MAX_CHUNK_BYTES)
174
+ return client.call(method, params, options);
175
+ tree.bytes ??= treeBytes(pullId, tree);
176
+ const bytes = await untilAborted(tree.bytes, [options?.signal, tree.stop.signal]);
177
+ if (bytes === undefined) {
178
+ // Nothing of the copy was answered (a copy is answered only once held), so the daemon's pull is read from its start.
179
+ if (!options?.signal?.aborted)
180
+ trees.delete(pullId);
181
+ return client.call(method, params, options);
182
+ }
183
+ const data = bytes.subarray(tree.read, tree.read + size);
184
+ tree.read += data.length;
185
+ const eof = data.length < size;
186
+ if (eof)
187
+ trees.delete(pullId);
188
+ return ok({ data: Buffer.from(data.buffer, data.byteOffset, data.byteLength).toString("base64"), eof });
85
189
  };
86
190
  const reading = {
87
191
  async call(method, params, options) {
192
+ if (!closed && (method === "tar.pull.begin" || method === "tar.pull.read" || method === "tar.pull.end"))
193
+ return pullCall(method, params, options);
88
194
  if (method !== "files.read" || closed) {
89
195
  letAllGo();
90
196
  return client.call(method, params, options);
@@ -511,8 +511,12 @@ export class HostedProvider {
511
511
  /**
512
512
  * A dial create of one request after any other of it on this server, whichever provider instance makes it. A
513
513
  * machine's token and socket go by the request's name, so two machines of one request must never dial at once: the
514
- * second create finds the first's computer instead (#createOnce). One server process is what dial asks for. A caller
515
- * that aborts while it waits stops waiting; the turn after it still waits for the create before it.
514
+ * second create finds the first's computer instead (#createOnce). The turns hold within one process alone: a dial
515
+ * request must be created by one process. Two processes sharing one MachineTokens that create one request's name
516
+ * cut each other's machine: the second mint replaces the first's token, and a converge delete revokes the name's
517
+ * token and drops the name's socket, which may be the kept machine's (#converge). Serve never does: its request
518
+ * carries the run's claim epoch, and each epoch is one claim's. A caller that aborts while it waits stops waiting;
519
+ * the turn after it still waits for the create before it.
516
520
  */
517
521
  async #inTurn(hub, name, signal, make) {
518
522
  const turns = DIAL_TURNS.get(hub) ?? new Map();
@@ -637,6 +641,9 @@ export class HostedProvider {
637
641
  * The listing that settles a create (after a failed prepare, and the last) is by the name alone, never narrowed
638
642
  * (nameAsk): two creates of one request that ask another run or epoch must still see each other's machine, so one is
639
643
  * kept and the other answered exists. `also` narrows the first look after the vendor's answer alone (#createOnce).
644
+ * Across processes this settles the vendor's machines alone. A dial machine's token and hub socket go by the name,
645
+ * not the machine, so a delete here revokes the name's token and drops the name's socket in this process's hub, and
646
+ * either can be the kept machine's own. So a dial request must be created by one process (#inTurn).
640
647
  */
641
648
  async #converge(name, mine, signal, also) {
642
649
  const listed = await foundBy(this.host, "the listing by the request's name", byName(name), this.#timeouts, signal, also);
@@ -912,8 +919,8 @@ export class HostedProvider {
912
919
  if (!read.ok)
913
920
  return read;
914
921
  const machine = read.value;
915
- // A machine the vendor failed is gone in its words: the create stops in them, not at createMs.
916
- if (machine?.why !== undefined && machine.state === "gone")
922
+ // A machine the vendor failed, gone or stopped in its words: the create stops in them, not at createMs.
923
+ if (machine?.why !== undefined && (machine.state === "gone" || machine.state === "stopped"))
917
924
  return this.#fail("vendor", `${this.#label} could not start ${ref.name}: ${machine.why}`);
918
925
  if (machine === undefined || machine.state === "gone")
919
926
  return this.#fail("gone", `${ref.name} was gone before it ran`);
@@ -21,6 +21,8 @@ export declare const READ_COPIES = "/run/agentrun-read";
21
21
  export declare const readStage: (uid: string, gid: string, path: string, copy: string) => string[];
22
22
  /** The read-unstage argv: copy removed. */
23
23
  export declare const readUnstage: (copy: string) => string[];
24
+ /** The tree-stage argv: the tar of path written to copy, made as uid:gid, at most `blocks` 512-byte blocks long. */
25
+ export declare const treeStage: (uid: string, gid: string, path: string, copy: string, blocks: number) => string[];
24
26
  /**
25
27
  * The session buses a vendor's own processes leave in /tmp (Daytona's D-Bus, /tmp/dbus-Y8OW6E3FDx), which no network
26
28
  * namespace cuts: every child host's create closes them to the computer's user before init starts. The window
@@ -120,6 +120,35 @@ const READ_UNSTAGE_SCRIPT = ["# agentrun-computer: read-unstage", ROOT, 'exec $p
120
120
  export const readStage = (uid, gid, path, copy) => argvOf(READ_STAGE_SCRIPT, [uid, gid, path, copy]);
121
121
  /** The read-unstage argv: copy removed. */
122
122
  export const readUnstage = (copy) => argvOf(READ_UNSTAGE_SCRIPT, [copy]);
123
+ /**
124
+ * tree-stage: [uid, gid, path, copy, blocks]. Writes to copy the tar computerd's tar.pull gives of path, made as uid:gid
125
+ * with no supplementary groups (setpriv), so it holds only what the daemon's user can read: a directory's entries named
126
+ * relative to it (a directory's name ending in "/"), a file one entry named for it; a hard link as a file of its own, a
127
+ * symlink as a link, and sockets, devices and pipes left out (find's types f, d and l). A name past ustar's 100 bytes
128
+ * goes in a pax header. find's names go to tar through a pipe, and root writes the copy alone, under umask 077 in
129
+ * READ_COPIES: the copy is the one file the script writes, so removing it (read-unstage) leaves nothing of a stage that
130
+ * was killed part way. The tar's file-size limit is `blocks` 512-byte blocks (ulimit -f in a POSIX sh: dash, busybox,
131
+ * and bash run as sh), so a tree past it fails rather than filling the machine's /run. find's own status comes back
132
+ * on fd 4, since a POSIX sh's pipeline answers only tar's. Any failure (a directory the user can't read, a file changed
133
+ * while it was read, a path whose resolved name is not its own, find or tar missing) removes the copy and fails the
134
+ * script, and the daemon's pull is read instead.
135
+ */
136
+ const TREE_STAGE_SCRIPT = [
137
+ "# agentrun-computer: tree-stage",
138
+ ROOT,
139
+ `exec $p sh -c 'umask 077; cd / && mkdir -p "\${3%/*}" && chmod 0700 "\${3%/*}" && r=$(realpath -e -- "$2") || exit 1
140
+ if [ -d "$r" ]; then
141
+ f=$( { { setpriv --reuid="$0" --regid="$1" --clear-groups -- find "$r" -mindepth 1 \\( -type f -o -type d -o -type l \\) -printf "%P\\0"; echo $? >&4; } |
142
+ ( ulimit -f "$4" && exec setpriv --reuid="$0" --regid="$1" --clear-groups -- tar -C "$r" --null --no-recursion -T - --hard-dereference --format=pax -cf - ) >"$3"; } 4>&1 ) &&
143
+ [ "$f" = 0 ]
144
+ else
145
+ [ -f "$r" ] && [ "\${r##*/}" = "\${2##*/}" ] && printf "%s\\0" "\${r##*/}" |
146
+ ( ulimit -f "$4" && exec setpriv --reuid="$0" --regid="$1" --clear-groups -- tar -C "\${r%/*}" --null --no-recursion -T - --format=pax -cf - ) >"$3"
147
+ fi
148
+ [ "$?" = 0 ] || { rm -f -- "$3"; exit 1; }' "$@"`,
149
+ ].join("\n");
150
+ /** The tree-stage argv: the tar of path written to copy, made as uid:gid, at most `blocks` 512-byte blocks long. */
151
+ export const treeStage = (uid, gid, path, copy, blocks) => argvOf(TREE_STAGE_SCRIPT, [uid, gid, path, copy, String(blocks)]);
123
152
  /**
124
153
  * The session buses a vendor's own processes leave in /tmp (Daytona's D-Bus, /tmp/dbus-Y8OW6E3FDx), which no network
125
154
  * namespace cuts: every child host's create closes them to the computer's user before init starts. The window
@@ -0,0 +1,44 @@
1
+ import { HostedProvider, type HostedOptions } from "./hosted/provider.js";
2
+ import { type Result } from "./result.js";
3
+ /** The parts of a WebSocket the host uses: Node's global WebSocket, or a stand-in in tests. */
4
+ export interface NorthflankSocket {
5
+ /** 0 connecting, 1 open, 2 closing, 3 closed. */
6
+ readonly readyState: number;
7
+ /** Bytes sent and not yet on the wire: a write waits while it is high. */
8
+ readonly bufferedAmount: number;
9
+ send(data: string): void;
10
+ close(code?: number): void;
11
+ addEventListener(type: "open" | "message" | "close" | "error", listener: (event: {
12
+ data?: unknown;
13
+ code?: number;
14
+ message?: string;
15
+ }) => void): void;
16
+ }
17
+ export type NorthflankSocketFactory = (url: string) => NorthflankSocket;
18
+ /** The Northflank provider's options: the token and the project, then the shared adapter's (HostedOptions). */
19
+ export interface NorthflankProviderOptions extends Omit<HostedOptions, "kind" | "desktop"> {
20
+ /** A Northflank API token whose role may manage services and run commands in the project. The caller passes it. */
21
+ token: string;
22
+ /** The customer's project the services live in (its id, a slug). Required: there is no default project. */
23
+ projectId: string;
24
+ /** The team, for an organisation token alone (D use-the-api: /v1/teams/{teamId}/projects/...). */
25
+ teamId?: string;
26
+ /** The API's root; default https://api.northflank.com. */
27
+ apiUrl?: string;
28
+ /** The fetch every REST call goes through; default the global one. */
29
+ fetch?: typeof fetch;
30
+ /** For tests: every exec socket opens through it. Default Node's global WebSocket. */
31
+ webSocket?: NorthflankSocketFactory;
32
+ }
33
+ export declare function create(options: NorthflankProviderOptions): Promise<NorthflankProvider>;
34
+ export declare const conformance: () => Promise<typeof import("./conformance/northflank.js")>;
35
+ export declare const DEFAULT_API_URL = "https://api.northflank.com";
36
+ /**
37
+ * Computers that are Northflank services in the customer's project, on the shared adapter: a sandbox from the box image
38
+ * (or the spec's), no desktop.
39
+ */
40
+ export declare class NorthflankProvider extends HostedProvider {
41
+ /** The provider contract's prepareImage (its descriptor declares it): the box image's public tag. No call: Northflank pulls it at each create. */
42
+ prepareImage(): Promise<Result<string>>;
43
+ constructor(options: NorthflankProviderOptions);
44
+ }