@getenergy/gke-sandboxes 0.3.0 → 0.3.1

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 (3) hide show
  1. package/README.md +7 -7
  2. package/dist/index.js +51 -34
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Give each cloud agent its own Linux sandbox, in your own Google Cloud project. A sandbox runs commands and keeps files. Put it to sleep while the user is away, and open the same ID later to get the files back.
4
4
 
5
- It costs about **$7.80 a month** to keep a 4 vCPU / 8 GB sandbox awake, less than every managed provider we priced.
5
+ A 2 vCPU / 4 GB sandbox costs about **1.9¢ for each hour it is awake**, cluster overhead included. Daytona and E2B list 16.6¢ for the same hour. A sleeping sandbox costs only its storage.
6
6
 
7
7
  <picture>
8
8
  <source media="(max-width: 600px)" srcset="docs/assets/running-sandbox.png">
9
- <img src="docs/assets/running-sandbox-wide.png" width="100%" alt="Monthly compute cost for a 4 vCPU and 8 GB sandbox that is awake for 730 hours at about 2% CPU. gke-sandboxes on C4D machines costs an estimated $7.80 per sandbox when many sandboxes split the machine costs (1×). At list price, Agent37 costs $8.80 (1.13×), Modal $25.75 (3.3×), Vercel Sandbox $130.91 (16.8×), Daytona $241.78 (31×), and E2B $241.78 (31×). Disk and plan fees are extra.">
9
+ <img src="docs/assets/running-sandbox-wide.png" width="100%" alt="Cost in US cents for each hour a 2 vCPU and 4 GB sandbox is awake, at about 4% CPU. gke-sandboxes costs an estimated 1.9¢, the whole cluster bill divided by the hours its sandboxes are awake (1×). At list price for compute, Modal 3.5¢ (1.8×), Fly.io 9.2¢ (4.7×), Vercel Sandbox 9.5¢ (4.9×), Daytona 16.6¢ (8.5×), and E2B 16.6¢ (8.5×). Managed disk and plan fees are extra.">
10
10
  </picture>
11
11
 
12
12
  ## Set it up
@@ -59,17 +59,17 @@ await sandboxes.sleep(sandbox.id);
59
59
 
60
60
  ## How it compares
61
61
 
62
- One sandbox at 4 vCPU and 8 GB. Costs are list prices for compute and storage only.
62
+ Costs are for one sandbox at 2 vCPU and 4 GB. Managed providers are at list prices for compute and storage only. Timings used 4 vCPU and 8 GB.
63
63
 
64
64
  #### Cost while awake
65
65
 
66
- Every bar in the chart above prices the same mostly idle sandbox, at the 2% CPU that Energy’s running sandboxes averaged. Daytona, E2B and Agent37 charge for the full size while it runs. Vercel charges for the CPU in use and all of the memory. Modal charges for what is used. The GKE figure is an estimate for 128 sandboxes sharing each C4D machine, with 40% added for spare machines and cluster overhead. Agents that run hotter fit fewer to a machine: at 6% CPU it is about $15.50.
66
+ Every bar in the chart above prices the same mostly idle sandbox, at the 0.076 vCPU that Energy’s running sandboxes averaged, about 4% of 2 vCPU. Daytona, E2B and Fly.io charge for the full size while it runs. Vercel charges for the CPU in use and all of the memory. Modal charges for what is used. The GKE figure is a whole cluster bill divided by its sandboxes: 520 awake at 56 to a machine, with two spare machines, node disks, system nodes, the cluster fee and snapshot storage. It needs that scale. A small cluster still pays for its minimum machines. Over a 730-hour month awake, 1.9¢ an hour is $14.22. Packed tighter on C4D machines, the GKE compute estimate falls to about $7.80 a month, or 1.1¢ an hour.
67
67
 
68
68
  #### Cost while asleep
69
69
 
70
70
  A sleeping sandbox is a snapshot in Cloud Storage. Machines you keep running still appear on your bill.
71
71
 
72
- <img src="docs/assets/monthly-cost.png" width="100%" alt="Storage for 20 GB retained for 730 hours. GKE snapshot about $0.37, Agent37 retained disk $1.80, Vercel Sandbox snapshot $1.60 on Pro, Daytona archived container $0, E2B paused sandbox $0 extra on Pro. Plan fees and infrastructure are excluded.">
72
+ <img src="docs/assets/monthly-cost.png" width="100%" alt="Storage for 20 GB retained for 730 hours. GKE snapshot about $0.37, Vercel Sandbox snapshot $1.60 on Pro, Daytona archived container $0, E2B paused sandbox $0 extra on Pro. Plan fees and infrastructure are excluded.">
73
73
 
74
74
  #### Start and wake
75
75
 
@@ -79,11 +79,11 @@ Medians. The GKE start time is with two sandboxes kept ready; without one it is
79
79
 
80
80
  #### Speed once running
81
81
 
82
- Building Better Auth took 67 s on a C4D machine with one sandbox, 81 s on a machine packed with 95 idle ones, and 100 s on Agent37. We have no build timings for the other providers.
82
+ Building Better Auth took 67 s on a C4D machine with one sandbox, and 81 s on a machine packed with 95 idle ones. We have no build timings for the managed providers.
83
83
 
84
84
  #### What sleep keeps
85
85
 
86
- gke-sandboxes restores files, not running processes. E2B and Agent37 can keep memory through sleep.
86
+ gke-sandboxes restores files, not running processes. E2B can keep memory through sleep.
87
87
 
88
88
  #### When a managed provider fits better
89
89
 
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createHash, randomBytes, randomInt, randomUUID } from 'node:crypto';
1
+ import { createHash, randomInt, randomUUID } from 'node:crypto';
2
2
  import { setTimeout as delay } from 'node:timers/promises';
3
3
  import { createWriteStream } from 'node:fs';
4
4
  import { readFile, rm } from 'node:fs/promises';
@@ -239,7 +239,7 @@ export class GkeSandboxes {
239
239
  if (!fresh && !(await this.markAcquired(pod)))
240
240
  continue;
241
241
  return {
242
- sandbox: this.environment(id, 'started'),
242
+ sandbox: this.makeEnvironment(id, 'started', pod.metadata.name),
243
243
  startState,
244
244
  labels: pod.metadata.labels,
245
245
  podName: pod.metadata.name,
@@ -564,42 +564,59 @@ export class GkeSandboxes {
564
564
  sandbox.labels = current.labels;
565
565
  }
566
566
  const startedAt = Date.now();
567
- if (pod && (terminating(pod) || !['Running', 'Pending'].includes(pod.status.phase))) {
568
- if (!terminating(pod)) {
569
- // Everything written since the last snapshot is gone with the pod.
570
- const reason = pod.status.containerStatuses.find(({ state }) => state?.terminated?.reason)?.state
571
- ?.terminated?.reason ??
572
- pod.status.reason ??
573
- pod.status.phase;
574
- this.options.onPodLost?.(sandbox.labels, pod.status.phase, reason, !!snapshot);
575
- await this.kubernetes.request('DELETE', `${this.pods}/${pod.metadata.name}`);
576
- }
577
- // The pod's last snapshot may be newer than the listing. One of another creation under
578
- // this id belongs to a sandbox that was replaced, not to this one.
579
- const [listed] = await this.list({ [SANDBOX_LABEL]: id });
580
- snapshot =
581
- listed && listed.labels[CREATED_LABEL] === sandbox.labels[CREATED_LABEL]
582
- ? listed.snapshots[0]
583
- : undefined;
584
- pod = null;
585
- }
586
567
  let created = false;
587
- if (!pod) {
568
+ while (true) {
569
+ signal.throwIfAborted();
570
+ if (pod && (terminating(pod) || !['Running', 'Pending'].includes(pod.status.phase))) {
571
+ if (!terminating(pod)) {
572
+ // Everything written since the last snapshot is gone with the pod.
573
+ const reason = pod.status.containerStatuses.find(({ state }) => state?.terminated?.reason)?.state
574
+ ?.terminated?.reason ??
575
+ pod.status.reason ??
576
+ pod.status.phase;
577
+ this.options.onPodLost?.(sandbox.labels, pod.status.phase, reason, !!snapshot);
578
+ await this.kubernetes.request('DELETE', `${this.pods}/${pod.metadata.name}`);
579
+ }
580
+ // The pod's last snapshot may be newer than the listing. One of another creation under
581
+ // this id belongs to a sandbox that was replaced, not to this one.
582
+ const [listed] = await this.list({ [SANDBOX_LABEL]: id });
583
+ snapshot =
584
+ listed && listed.labels[CREATED_LABEL] === sandbox.labels[CREATED_LABEL]
585
+ ? listed.snapshots[0]
586
+ : undefined;
587
+ pod = null;
588
+ }
589
+ if (pod)
590
+ break;
588
591
  // A fresh filesystem must also get the caller's initial labels.
589
592
  if (!snapshot)
590
593
  sandbox.labels = initialLabels;
591
- // A new name every time, so a wake never waits for the previous pod to finish shutting down.
592
- const name = `${id}-${randomBytes(3).toString('hex')}`;
593
- const own = PodSchema.parse(await this.kubernetes.request('POST', this.pods, this.manifest(sandbox, name, snapshot)));
594
- // Another dispatch or file request may have created one at the same moment. Both keep the
595
- // older one. This pod counts whether or not the listing has it yet.
596
- pod = [
597
- own,
598
- ...(await this.livePods(id)).filter(({ metadata }) => metadata.name !== name),
599
- ].sort(oldestLiveFirst)[0];
600
- created = pod === own;
601
- if (!created)
602
- await this.kubernetes.request('DELETE', `${this.pods}/${name}`);
594
+ // Named after the snapshot it starts from. Kubernetes creates a name once, so callers that
595
+ // start the sandbox at the same moment get one pod between them. And a wake never waits
596
+ // for the pod it replaces to finish shutting down, which started from an earlier snapshot.
597
+ const name = `${id}-${createHash('sha256')
598
+ .update(snapshot ?? '')
599
+ .digest('hex')
600
+ .slice(0, 6)}`;
601
+ try {
602
+ pod = PodSchema.parse(await this.kubernetes.request('POST', this.pods, this.manifest(sandbox, name, snapshot)));
603
+ created = true;
604
+ break;
605
+ }
606
+ catch (error) {
607
+ if (!(error instanceof KubernetesApiError && error.status === 409))
608
+ throw error;
609
+ }
610
+ // The name is taken. By a pod another caller just created, which is this caller's too. Or by
611
+ // one that started from the same snapshot and died or was deleted: Kubernetes removes a
612
+ // dead pod at once, and one still shutting down has to go before its name is free.
613
+ const taken = await this.kubernetes.watch(this.pods, name, (found) => {
614
+ const current = found ? PodSchema.parse(found) : null;
615
+ return current && terminating(current) ? undefined : current;
616
+ }, startedAt + START_SECONDS * 1000, signal);
617
+ if (taken === undefined)
618
+ throw new Error(`Sandbox ${id} did not finish starting.`);
619
+ pod = taken;
603
620
  }
604
621
  // Only the caller whose pod it is reports the start; one that waits for another's does not.
605
622
  const operation = created ? (snapshot ? 'wake' : 'create') : null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@getenergy/gke-sandboxes",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {