@moinulmoin/eden-runtime-cloudflare 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,51 @@
1
- import { Container, ContainerProxy } from "@cloudflare/containers";
1
+ import { DurableObject, WorkerEntrypoint } from "cloudflare:workers";
2
2
  import { WorldCore } from "@moinulmoin/eden-world-cloudflare/core";
3
- import { EVE_HOST_DEFAULTS, EVE_SCHEDULE_WAKE_LEAD_MS, EveHostError, assertNonEmpty, assertStableOrigin, createEveHostLifecycleObserver, createEveReadinessGate, createTrustedEveRequest, eveScheduleCronFiresWithin, IDENTIFIER_PATTERN, readProtectedRuntimeVariables, WORKER_NAME_PATTERN, isEveWorkflowInternalRoute, } from "./eve-host.js";
4
- export { ContainerProxy };
3
+ import { EVE_HOST_DEFAULTS, EVE_HOST_SLEEP_SNAPSHOT_MARGIN_MS, EVE_SCHEDULE_WAKE_LEAD_MS, parseEveSleepAfterMs, EveHostError, assertNonEmpty, assertStableOrigin, createEveReadinessGate, createTrustedEveRequest, eveScheduleCronFiresWithin, readProtectedRuntimeVariables, WORKER_NAME_PATTERN, isEveWorkflowInternalRoute, } from "./eve-host.js";
5
4
  const EVE_CLOUDFLARE_CONTAINERS_CA_PATH = "/etc/cloudflare/certs/cloudflare-containers-ca.crt";
5
+ /**
6
+ * Cloudflare's durable_object containers expose `/dev/kvm`, which makes Eve's
7
+ * `DefaultSandbox` pick microsandbox at runtime, while Eden's isolated builder
8
+ * (no KVM, no Docker daemon) prepared the just-bash template — every sandbox
9
+ * tool then fails with a provider mismatch. Hiding the device before Eve
10
+ * starts keeps runtime selection equal to the build's. Same Eve start
11
+ * command as the image entrypoint.
12
+ */
13
+ const EVE_HOST_ENTRYPOINT = Object.freeze([
14
+ "/bin/sh",
15
+ "-c",
16
+ "rm -f /dev/kvm; cd /workspace && exec ./node_modules/.bin/eve start --host 0.0.0.0 --port 8080",
17
+ ]);
18
+ /** Durable Object storage keys owned by `EveHostDurableContainer`. */
19
+ const EVE_HOST_CONTAINER_ACTIVITY_KEY = "eden.eve.host.last-activity-at";
20
+ const EVE_HOST_CONTAINER_SNAPSHOT_KEY = "eden.eve.host.workspace-snapshot";
21
+ const EVE_HOST_CONTAINER_SNAPSHOT_ATTEMPTS_KEY = "eden.eve.host.snapshot-attempts";
22
+ /**
23
+ * The image the current container was started with. `inspect()` reports an
24
+ * empty image for a container restored from a snapshot (and while one is
25
+ * starting), so update detection falls back to this record.
26
+ */
27
+ const EVE_HOST_CONTAINER_STARTED_IMAGE_KEY = "eden.eve.host.started-image";
28
+ /**
29
+ * Sandbox-session carry keys. The meta record names the active carry; its
30
+ * archive streams into `<chunkPrefix>.<carryId>.<index>` values so no single
31
+ * key+value pair approaches the SQLite-backed 2 MiB storage limit and the
32
+ * whole archive never sits in isolate memory (128 MB limit).
33
+ */
34
+ const EVE_HOST_SANDBOX_CARRY_KEY = "eden.eve.host.sandbox-carry";
35
+ const EVE_HOST_SANDBOX_CARRY_CHUNK_PREFIX = "eden.eve.host.sandbox-carry";
36
+ /** The only `/workspace` subtree carried across an image update. */
37
+ const EVE_HOST_SANDBOX_CARRY_SOURCE = "/workspace/.eve/sandbox-cache/just-bash";
38
+ /**
39
+ * Carry archive limits. `chunkBytes` stays safely under the per-key limit;
40
+ * `maxBytes` caps the compressed archive (tests shrink both).
41
+ */
42
+ export const EVE_HOST_CARRY_LIMITS = {
43
+ chunkBytes: 1024 * 1024,
44
+ maxBytes: 1024 * 1024 * 1024,
45
+ };
46
+ /** Snapshot attempts the alarm retries before destroying anyway so a stuck
47
+ * snapshot path never keeps billing alive forever. */
48
+ const EVE_HOST_SNAPSHOT_MAX_ATTEMPTS = 3;
6
49
  /** The deployment's single SQLite-backed Workflow World. */
7
50
  export class EdenWorldDurableObject {
8
51
  core;
@@ -21,14 +64,12 @@ export class EdenWorldDurableObject {
21
64
  delivery: {
22
65
  deliver: async (message) => {
23
66
  const origin = env.EVE_PUBLIC_ORIGIN;
24
- const binding = env.EVE_CONTAINER_BINDING_NAME;
25
67
  const instance = env.EVE_CONTAINER_INSTANCE_NAME;
26
- if (typeof origin !== "string" || typeof binding !== "string" ||
27
- typeof instance !== "string") {
68
+ if (typeof origin !== "string" || typeof instance !== "string") {
28
69
  throw new Error("The World queue Container identity is unavailable.");
29
70
  }
30
- const namespace = env[binding];
31
- const response = await namespace.getByName(instance).containerFetch(new Request(new URL(`/.well-known/workflow/v1/${message.path}`, origin), {
71
+ const namespace = env.EVE_CONTAINER;
72
+ const response = await namespace.getByName(instance).loopbackFetch(new Request(new URL(`/.well-known/workflow/v1/${message.path}`, origin), {
32
73
  method: "POST",
33
74
  headers: {
34
75
  ...message.headers,
@@ -38,7 +79,7 @@ export class EdenWorldDurableObject {
38
79
  "x-vqs-message-attempt": String(message.attempt),
39
80
  },
40
81
  body: message.body,
41
- }), EVE_HOST_DEFAULTS.internalPort);
82
+ }));
42
83
  // Body transport errors must remain retryable; an HTTP status alone
43
84
  // is not an acknowledgement of a streamed Container response.
44
85
  const body = await response.text();
@@ -74,25 +115,25 @@ export class EdenWorldDurableObject {
74
115
  }
75
116
  }
76
117
  /**
77
- * Delivers the container's own HTTPS requests to its public workers.dev origin
78
- * back into the container over the internal port instead of the public edge.
79
- * The Workflow local World posts queue deliveries to
80
- * `WORKFLOW_LOCAL_BASE_URL`, which must remain the public origin so Eve's
81
- * externally visible callback URLs stay correct; re-routing that traffic
82
- * inside the Worker lets the public Worker path refuse the unauthenticated
83
- * queue endpoints without breaking delivery.
118
+ * Delivers the container's own HTTPS requests to its public workers.dev
119
+ * origin back into the deployment without crossing the public edge. The
120
+ * Workflow local World posts queue deliveries to `WORKFLOW_LOCAL_BASE_URL`,
121
+ * which must remain the public origin so Eve's externally visible callback
122
+ * URLs stay correct; the DO registers an outbound-HTTPS intercept for that
123
+ * hostname aimed at `EveHostLoopback`, and this function routes the request:
124
+ * non-public hosts egress plainly, `/__eden/world/rpc` reaches the World
125
+ * Durable Object, and everything else re-enters the container over the
126
+ * internal port so the public Worker path can keep refusing the
127
+ * unauthenticated queue endpoints.
84
128
  */
85
129
  export async function routeEveOutboundRequest(request, env) {
86
130
  const publicOrigin = env.EVE_PUBLIC_ORIGIN;
87
- const bindingName = env.EVE_CONTAINER_BINDING_NAME;
88
131
  const instanceName = env.EVE_CONTAINER_INSTANCE_NAME;
89
132
  if (typeof publicOrigin !== "string" ||
90
- typeof bindingName !== "string" ||
91
133
  typeof instanceName !== "string") {
92
134
  throw new EveHostError("HOST_ORIGIN_UNAVAILABLE", "The container loopback identity is unavailable to the outbound handler.");
93
135
  }
94
136
  assertStableOrigin(publicOrigin);
95
- assertNonEmpty(bindingName, "Container binding name");
96
137
  assertNonEmpty(instanceName, "Container instance name");
97
138
  const hostname = new URL(request.url).hostname;
98
139
  if (hostname !== new URL(publicOrigin).hostname) {
@@ -104,48 +145,56 @@ export async function routeEveOutboundRequest(request, env) {
104
145
  return new Response(null, { status: 404 });
105
146
  return world.get(world.idFromName("world")).fetch(request);
106
147
  }
107
- const namespace = env[bindingName];
148
+ const namespace = env.EVE_CONTAINER;
108
149
  if (namespace === undefined) {
109
150
  throw new EveHostError("HOST_READINESS_UNPROVEN", "The configured Container binding is unavailable to the outbound handler.");
110
151
  }
111
- return namespace
112
- .getByName(instanceName)
113
- .containerFetch(request, EVE_HOST_DEFAULTS.internalPort);
152
+ return namespace.getByName(instanceName).loopbackFetch(request);
114
153
  }
115
154
  /**
116
- * Durable Object storage key recording which deployment last started the
117
- * Container. Cloudflare does not roll DO-managed Container instances onto a
118
- * new image when the Worker redeploys — the image is selected at
119
- * `ctx.container.start()` — so an in-place update must restart the running
120
- * instance itself before new code is served.
155
+ * This module is the only Eve-host surface that depends on provider runtime
156
+ * packages. It is bundled into the deployed Worker directory instead of the
157
+ * package's public declaration surface, so plain-Node consumers never load
158
+ * `cloudflare:workers`-dependent code.
159
+ */
160
+ /**
161
+ * The WorkerEntrypoint the container's intercepted outbound HTTPS traffic is
162
+ * delivered to (registered by `EveHostDurableContainer` right after every
163
+ * `ctx.container.start()`). Props carry the DO instance name because
164
+ * entrypoint props cannot hold a Fetcher.
121
165
  */
122
- const EVE_HOST_CONTAINER_DEPLOYMENT_KEY = "eden.eve.host.started-deployment";
123
- /** Restart attempts are rate-limited under this key: during the
124
- * Worker-active-before-image-rollout window a restart still launches the old
125
- * image, so retries must wait for the rollout. */
126
- const EVE_HOST_CONTAINER_RESTART_ATTEMPT_KEY = "eden.eve.host.restart-attempted-at";
127
- /** Compares image references by their generation-scoped repository name so
128
- * digest and tag spellings of the same pushed image match. */
129
- function containerRepositoryName(image) {
130
- return (image.split("@")[0] ?? "").split(":")[0] ?? "";
166
+ export class EveHostLoopback extends WorkerEntrypoint {
167
+ async fetch(request) {
168
+ return routeEveOutboundRequest(request, this.env);
169
+ }
131
170
  }
132
- export class EveHostContainer extends Container {
133
- defaultPort = EVE_HOST_DEFAULTS.internalPort;
134
- sleepAfter = EVE_HOST_DEFAULTS.sleepAfter;
135
- requiredPorts = [EVE_HOST_DEFAULTS.internalPort];
136
- interceptHttps = true;
137
- enableInternet = true;
138
- pingEndpoint = "localhost/eve/v1/health";
139
- lifecycle = createEveHostLifecycleObserver();
140
- readinessStarted = false;
141
- /** Evidence is cached only under the deployment whose image was verified. */
171
+ /**
172
+ * The Eve host Durable Object. It drives `ctx.container` directly: the
173
+ * image is selected at `start()` from this Worker version's `images.eve`
174
+ * entry, the writable filesystem is preserved across sleep via a container
175
+ * snapshot, and an update restarts the instance when `inspect()` shows the
176
+ * running image predates the configured one.
177
+ */
178
+ export class EveHostDurableContainer extends DurableObject {
179
+ /** Evidence is cached only while the container it was proven on keeps running. */
142
180
  readinessEvidence;
181
+ /** True once the running container's image was proven to equal `images.eve`. */
182
+ imageVerified = false;
143
183
  /**
144
184
  * Bumped whenever the Container stops or is reset. Readiness work captures
145
- * the epoch it started under and may only record a marker or cache
146
- * evidence if no stop happened in between.
185
+ * the epoch it started under and may only cache evidence if no stop
186
+ * happened in between.
147
187
  */
148
188
  readinessEpoch = 0;
189
+ monitorAttached = false;
190
+ /** Serializes sandbox-session carry-outs on this Durable Object instance. */
191
+ carryOutQueue = Promise.resolve();
192
+ /** True only while the last start was fresh from `images.eve`, not a snapshot. */
193
+ pendingCarryRestore = false;
194
+ /** Shared single-flight carry restore for the current container generation. */
195
+ carryRestorePromise;
196
+ envVars;
197
+ sleepAfterMs;
149
198
  readiness;
150
199
  constructor(ctx, env) {
151
200
  super(ctx, env);
@@ -183,160 +232,388 @@ export class EveHostContainer extends Container {
183
232
  ? {}
184
233
  : { EDEN_EVE_RUNTIME_REVISION: env.EDEN_EVE_RUNTIME_REVISION }),
185
234
  };
186
- const sleepAfterOverride = env.EDEN_EVE_CONTAINER_SLEEP_AFTER;
187
- if (sleepAfterOverride !== undefined) {
188
- // `env` is only readable after `super()`, so the override is applied in
189
- // the constructor body; the base class reads `this.sleepAfter` again on
190
- // every subsequent activity renewal.
191
- this.sleepAfter = sleepAfterOverride;
192
- }
235
+ this.sleepAfterMs = parseEveSleepAfterMs(env.EDEN_EVE_CONTAINER_SLEEP_AFTER ?? EVE_HOST_DEFAULTS.sleepAfter);
193
236
  this.readiness = createEveReadinessGate({
194
- startAndWaitForPorts: (options) => this.startAndWaitForPorts(options),
195
- healthFetch: (request) => this.containerFetch(request, EVE_HOST_DEFAULTS.internalPort),
237
+ start: async () => {
238
+ await this.ensureContainerStarted();
239
+ },
240
+ healthFetch: (request) => {
241
+ const container = this.containerRuntime();
242
+ if (container === undefined) {
243
+ throw new EveHostError("HOST_READINESS_UNPROVEN", "The Container runtime is unavailable.");
244
+ }
245
+ const url = new URL(request.url);
246
+ return container
247
+ .getTcpPort(EVE_HOST_DEFAULTS.internalPort)
248
+ .fetch(new Request(`http://container${url.pathname}${url.search}`, request));
249
+ },
196
250
  });
251
+ const runtime = this.containerRuntime();
252
+ if (runtime?.running === true) {
253
+ // DO restarts lose the inactivity timeout; the monitor for this
254
+ // container generation is also gone, so both are re-attached here.
255
+ this.ctx.blockConcurrencyWhile(async () => {
256
+ await runtime.setInactivityTimeout(this.inactivityTimeoutMs);
257
+ this.attachMonitor(runtime);
258
+ });
259
+ }
197
260
  }
198
- /**
199
- * Compares the deployment the running Container was started under (stored
200
- * in Durable Object storage, which survives Worker redeploys) against the
201
- * deployment the *current* Worker stamps on the forwarded request. A
202
- * stale Durable Object still detects the update because the header is
203
- * stamped by new Worker code, not this code's environment.
204
- *
205
- * DO-managed Container instances are excluded from the application's fleet
206
- * rollout — the image is only re-selected on the next
207
- * `ctx.container.start()` — so an update must stop the instance itself.
208
- * A missing marker means the running image cannot be trusted, so the
209
- * container is restarted too (first update from runtimes that predate the
210
- * marker). Restarts are rate-limited because Cloudflare activates a new
211
- * Worker before the image rollout lands; starting in that window would
212
- * re-launch the *old* image, so the deployment marker is only recorded
213
- * after `inspect()` proves the running image is the expected one.
214
- */
215
- async reconcileStartedDeployment(requestDeploymentId, requestImage) {
216
- // Requests without the Worker's deployment header (scheduled wakes on old
217
- // Worker code, probes) cannot arbitrate drift; the Durable Object's own
218
- // environment may itself be stale, so never fall back to it here.
219
- if (requestDeploymentId === undefined)
261
+ containerRuntime() {
262
+ return this.ctx.container;
263
+ }
264
+ /** Container lifetime the platform keeps after the DO goes inactive: long
265
+ * enough for the idle alarm to snapshot first, within Cloudflare's 6 h cap. */
266
+ get inactivityTimeoutMs() {
267
+ return this.sleepAfterMs + EVE_HOST_SLEEP_SNAPSHOT_MARGIN_MS;
268
+ }
269
+ attachMonitor(runtime) {
270
+ if (this.monitorAttached)
220
271
  return;
221
- const storage = this.ctx?.storage;
222
- if (storage === undefined ||
223
- typeof storage.get !== "function" ||
224
- typeof storage.put !== "function") {
272
+ this.monitorAttached = true;
273
+ runtime.monitor().then(() => {
274
+ this.monitorAttached = false;
275
+ this.recordContainerStopped();
276
+ }, () => {
277
+ this.monitorAttached = false;
278
+ this.recordContainerStopped();
279
+ });
280
+ }
281
+ recordContainerStopped() {
282
+ this.readinessEpoch += 1;
283
+ this.readinessEvidence = undefined;
284
+ this.imageVerified = false;
285
+ this.pendingCarryRestore = false;
286
+ this.carryRestorePromise = undefined;
287
+ this.readiness.reset();
288
+ }
289
+ /** Routes the container's own requests to its public origin back inside. */
290
+ async registerLoopbackIntercept(runtime) {
291
+ const exportsMap = this.ctx.exports;
292
+ const entryFactory = exportsMap?.["EveHostLoopback"];
293
+ if (typeof entryFactory !== "function")
225
294
  return;
295
+ const entrypoint = entryFactory({ props: {} });
296
+ await runtime.interceptOutboundHttps(new URL(this.env.EVE_PUBLIC_ORIGIN ?? "").hostname, entrypoint);
297
+ }
298
+ /**
299
+ * Records request activity and arms the sleep alarm at
300
+ * `lastActivityAt + sleepAfter`. The alarm — not the inactivity timeout —
301
+ * is what snapshots the writable filesystem before the container stops.
302
+ */
303
+ async touchActivity() {
304
+ const now = Date.now();
305
+ await this.ctx.storage.put(EVE_HOST_CONTAINER_ACTIVITY_KEY, now);
306
+ await this.ctx.storage.setAlarm(now + this.sleepAfterMs);
307
+ }
308
+ /**
309
+ * Starts the container when it is not running. A stored workspace snapshot
310
+ * is restored only when it was captured from the exact image this Worker
311
+ * version configures — restoring an older snapshot silently runs the
312
+ * previous image — so after an image update the container starts fresh and
313
+ * the readiness path restores the carried sandbox sessions instead.
314
+ */
315
+ async ensureContainerStarted() {
316
+ const runtime = this.containerRuntime();
317
+ if (runtime === undefined) {
318
+ throw new EveHostError("HOST_READINESS_UNPROVEN", "The Container runtime is unavailable on this Durable Object.");
226
319
  }
227
- const lastStarted = await storage.get(EVE_HOST_CONTAINER_DEPLOYMENT_KEY);
228
- if (lastStarted === requestDeploymentId)
320
+ if (runtime.running)
229
321
  return;
230
- const runtime = this.ctx?.container;
231
- if (runtime?.running !== true)
322
+ const expectedImage = runtime.images["eve"];
323
+ if (expectedImage === undefined) {
324
+ throw new EveHostError("HOST_READINESS_UNPROVEN", "The configured Container image is unavailable to this Worker version.");
325
+ }
326
+ const storedSnapshot = await this.ctx.storage.get(EVE_HOST_CONTAINER_SNAPSHOT_KEY);
327
+ const snapshotId = storedSnapshot !== undefined &&
328
+ storedSnapshot.image === expectedImage
329
+ ? storedSnapshot.id
330
+ : undefined;
331
+ runtime.start({
332
+ ...(snapshotId === undefined
333
+ ? { image: expectedImage }
334
+ : { containerSnapshot: { id: snapshotId } }),
335
+ instance: EVE_HOST_DEFAULTS.instance,
336
+ enableInternet: true,
337
+ env: this.envVars,
338
+ entrypoint: EVE_HOST_ENTRYPOINT,
339
+ });
340
+ // Set before any await: a failure below makes the readiness retry see a
341
+ // running container and skip this method, so the restore must already be
342
+ // pending. A start from the configured image (an update) has no sandbox
343
+ // sessions yet; a same-image snapshot wake already contains them.
344
+ this.pendingCarryRestore = snapshotId === undefined;
345
+ // Both start sources run `expectedImage` (a snapshot is only restored
346
+ // from that image).
347
+ await this.ctx.storage.put(EVE_HOST_CONTAINER_STARTED_IMAGE_KEY, expectedImage);
348
+ // Intercepts do not survive a container start and late registration lags;
349
+ // register inside the same block before readiness polling.
350
+ await this.registerLoopbackIntercept(runtime);
351
+ this.attachMonitor(runtime);
352
+ await runtime.setInactivityTimeout(this.inactivityTimeoutMs);
353
+ }
354
+ /**
355
+ * Replaces the running container when it still serves a pre-update image:
356
+ * this Worker version's `images.eve` is selected at `start()`, so an
357
+ * in-place update must stop the stale instance itself. `inspect()` reports
358
+ * an empty image for a snapshot-restored container, so the image recorded
359
+ * at start stands in for it.
360
+ */
361
+ async reconcileRunningImage() {
362
+ if (this.imageVerified)
232
363
  return;
233
- if (requestImage !== undefined &&
234
- (await this.containerServesImage(runtime, requestImage)) === true) {
235
- // The running instance already provably serves this deployment's image
236
- // (e.g. the rollout replaced it); safe to mark it as started.
237
- await storage.put(EVE_HOST_CONTAINER_DEPLOYMENT_KEY, requestDeploymentId);
364
+ const runtime = this.containerRuntime();
365
+ if (runtime === undefined || !runtime.running)
238
366
  return;
367
+ const expectedImage = runtime.images["eve"];
368
+ let info;
369
+ try {
370
+ info = await runtime.inspect();
239
371
  }
240
- const attemptedAt = await storage.get(EVE_HOST_CONTAINER_RESTART_ATTEMPT_KEY);
241
- const now = Date.now();
242
- if (attemptedAt !== undefined && now - attemptedAt < 30_000)
372
+ catch {
373
+ info = null;
374
+ }
375
+ const runningImage = info?.image !== undefined && info.image !== ""
376
+ ? info.image
377
+ : await this.ctx.storage.get(EVE_HOST_CONTAINER_STARTED_IMAGE_KEY);
378
+ if (runningImage === expectedImage) {
379
+ this.imageVerified = true;
243
380
  return;
244
- await storage.put(EVE_HOST_CONTAINER_RESTART_ATTEMPT_KEY, now);
245
- this.lifecycle.record("replaced");
246
- this.readinessEpoch += 1;
247
- this.readinessStarted = false;
248
- this.readinessEvidence = undefined;
249
- this.readiness.reset();
250
- await this.stop();
381
+ }
382
+ this.recordContainerStopped();
383
+ // The stale container's conversation sandboxes must survive the update:
384
+ // stream them into Durable Object storage before the destroy makes the
385
+ // container unreachable. A failed carry still lets the update proceed.
386
+ try {
387
+ await this.carrySandboxSessionsOut(runtime);
388
+ }
389
+ catch (error) {
390
+ console.error("The Eve sandbox session carry failed before the image update:", error instanceof Error ? error.message : "unknown");
391
+ }
392
+ await runtime.destroy();
251
393
  const stoppedAt = Date.now();
252
394
  while (runtime.running && Date.now() - stoppedAt < 10_000) {
253
395
  await new Promise((resolve) => setTimeout(resolve, 25));
254
396
  }
255
397
  }
256
- async containerServesImage(runtime, expectedImage) {
257
- const inspect = runtime.inspect;
258
- if (typeof inspect !== "function")
259
- return undefined;
260
- let info;
398
+ /**
399
+ * Streams `/workspace/.eve/sandbox-cache/just-bash/sessions` — every
400
+ * conversation's sandbox files and metadata — into Durable Object storage,
401
+ * so a fresh container started by an image update can restore them. Not
402
+ * carried: templates and the rest of `/workspace`, which come from the new
403
+ * image. One carry runs at a time on this Durable Object; a failure never
404
+ * blocks the destroy that follows and leaves the previous carry intact.
405
+ */
406
+ carrySandboxSessionsOut(runtime) {
407
+ const run = this.carryOutQueue.then(() => this.carrySandboxSessionsOutNow(runtime));
408
+ this.carryOutQueue = run.then(() => undefined, () => undefined);
409
+ return run;
410
+ }
411
+ async carrySandboxSessionsOutNow(runtime) {
412
+ const previous = await this.ctx.storage.get(EVE_HOST_SANDBOX_CARRY_KEY);
413
+ const carryId = globalThis.crypto.randomUUID();
414
+ const chunkPrefix = `${EVE_HOST_SANDBOX_CARRY_CHUNK_PREFIX}.${carryId}.`;
415
+ const writtenChunkKeys = [];
416
+ const discardWrittenChunks = async () => {
417
+ for (const key of writtenChunkKeys)
418
+ await this.ctx.storage.delete(key);
419
+ };
420
+ const exec = await runtime.exec([
421
+ "sh",
422
+ "-c",
423
+ `cd ${EVE_HOST_SANDBOX_CARRY_SOURCE} && [ -d sessions ] && tar -czf - sessions || true`,
424
+ ], { stdout: "pipe", stderr: "pipe" });
425
+ const buffered = [];
426
+ let bufferedBytes = 0;
427
+ let chunks = 0;
428
+ let bytes = 0;
429
+ const flushOnce = async () => {
430
+ if (bufferedBytes === 0)
431
+ return;
432
+ const size = Math.min(bufferedBytes, EVE_HOST_CARRY_LIMITS.chunkBytes);
433
+ const value = new Uint8Array(size);
434
+ let offset = 0;
435
+ while (offset < size) {
436
+ const part = buffered[0];
437
+ if (part === undefined)
438
+ break;
439
+ const take = Math.min(part.byteLength, size - offset);
440
+ value.set(take === part.byteLength ? part : part.subarray(0, take), offset);
441
+ offset += take;
442
+ if (take === part.byteLength)
443
+ buffered.shift();
444
+ else
445
+ buffered[0] = part.subarray(take);
446
+ }
447
+ bufferedBytes -= size;
448
+ const key = `${chunkPrefix}${chunks}`;
449
+ writtenChunkKeys.push(key);
450
+ await this.ctx.storage.put(key, value);
451
+ chunks += 1;
452
+ bytes += value.byteLength;
453
+ };
454
+ const flushBufferedChunks = async () => {
455
+ while (bufferedBytes >= EVE_HOST_CARRY_LIMITS.chunkBytes) {
456
+ await flushOnce();
457
+ }
458
+ };
459
+ try {
460
+ const stdout = exec.stdout;
461
+ if (stdout === null)
462
+ throw new Error("the exec stdout was not piped");
463
+ const reader = stdout.getReader();
464
+ let overflowed = false;
465
+ for (;;) {
466
+ const { done, value } = await reader.read();
467
+ if (done)
468
+ break;
469
+ if (value === undefined)
470
+ continue;
471
+ buffered.push(value);
472
+ bufferedBytes += value.byteLength;
473
+ if (bytes + bufferedBytes > EVE_HOST_CARRY_LIMITS.maxBytes) {
474
+ overflowed = true;
475
+ break;
476
+ }
477
+ await flushBufferedChunks();
478
+ }
479
+ await reader.cancel().catch(() => undefined);
480
+ if (overflowed) {
481
+ await discardWrittenChunks();
482
+ console.error(`The Eve sandbox session carry exceeded ${EVE_HOST_CARRY_LIMITS.maxBytes} compressed bytes; keeping the previous carry.`);
483
+ return;
484
+ }
485
+ await flushOnce();
486
+ }
487
+ catch (error) {
488
+ await discardWrittenChunks();
489
+ throw error;
490
+ }
491
+ let exitCode = -1;
261
492
  try {
262
- info = await inspect.call(runtime);
493
+ exitCode = await exec.exitCode;
263
494
  }
264
495
  catch {
265
- return undefined;
496
+ exitCode = -1;
497
+ }
498
+ if (exitCode !== 0) {
499
+ await discardWrittenChunks();
500
+ console.error(`The Eve sandbox session carry failed with exit code ${exitCode}; keeping the previous carry.`);
501
+ return;
502
+ }
503
+ if (bytes === 0) {
504
+ // No sessions directory in this container (nothing to carry); keep any
505
+ // previous carry as the restore source.
506
+ return;
507
+ }
508
+ await this.ctx.storage.put(EVE_HOST_SANDBOX_CARRY_KEY, {
509
+ id: carryId,
510
+ chunks,
511
+ bytes,
512
+ at: Date.now(),
513
+ });
514
+ if (previous !== undefined) {
515
+ for (let index = 0; index < previous.chunks; index += 1) {
516
+ await this.ctx.storage.delete(`${EVE_HOST_SANDBOX_CARRY_CHUNK_PREFIX}.${previous.id}.${index}`);
517
+ }
266
518
  }
267
- if (info?.image === undefined)
268
- return undefined;
269
- return containerRepositoryName(info.image) ===
270
- containerRepositoryName(expectedImage);
271
519
  }
272
520
  /**
273
- * Records the started deployment only when the running Container provably
274
- * serves this deployment's image (`inspect()` matches the forwarded
275
- * `x-eden-eve-image`). `false` or unknown (no `inspect`, a throwing call,
276
- * or no image field) never records — a start inside the
277
- * Worker-active-before-image-rollout window must not pin the old image.
278
- * When the Worker stamps no image, the marker is recorded unverified,
279
- * matching pre-drift behavior.
521
+ * Single restore per fresh container generation, shared by every
522
+ * concurrent readiness caller: the first caller starts it, everyone awaits
523
+ * the same promise, and no request is forwarded before it settles.
280
524
  */
281
- async recordStartedDeployment(requestDeploymentId, requestImage) {
282
- if (requestDeploymentId === undefined)
283
- return true;
284
- const storage = this.ctx?.storage;
285
- if (storage === undefined ||
286
- typeof storage.get !== "function" ||
287
- typeof storage.put !== "function") {
288
- return true;
289
- }
290
- const runtime = this.ctx?.container;
291
- if (requestImage !== undefined) {
292
- // A stopped (or stopping) instance can't prove which image it serves.
293
- if (runtime?.running !== true) {
294
- this.lifecycle.record("image_mismatch");
295
- return false;
525
+ async restoreCarriedSandboxSessions() {
526
+ if (!this.pendingCarryRestore)
527
+ return;
528
+ this.carryRestorePromise ??= this.carrySandboxSessionsIn();
529
+ await this.carryRestorePromise;
530
+ this.pendingCarryRestore = false;
531
+ }
532
+ /**
533
+ * Extracts the carried sandbox sessions into a container that started
534
+ * fresh from `images.eve`. The archive streams chunk by chunk from Durable
535
+ * Object storage through exec stdin, so it never sits in isolate memory. A
536
+ * failure is logged and swallowed: the agent still serves, only the
537
+ * carried files are missing.
538
+ */
539
+ async carrySandboxSessionsIn() {
540
+ try {
541
+ const carry = await this.ctx.storage.get(EVE_HOST_SANDBOX_CARRY_KEY);
542
+ if (carry === undefined)
543
+ return;
544
+ let nextChunk = 0;
545
+ const archive = new ReadableStream({
546
+ pull: async (controller) => {
547
+ if (nextChunk >= carry.chunks) {
548
+ controller.close();
549
+ return;
550
+ }
551
+ const chunk = await this.ctx.storage.get(`${EVE_HOST_SANDBOX_CARRY_CHUNK_PREFIX}.${carry.id}.${nextChunk}`);
552
+ const index = nextChunk;
553
+ nextChunk += 1;
554
+ if (chunk === undefined) {
555
+ controller.error(new Error(`The sandbox carry chunk ${index} is missing.`));
556
+ return;
557
+ }
558
+ controller.enqueue(chunk);
559
+ },
560
+ });
561
+ const runtime = this.containerRuntime();
562
+ if (runtime === undefined) {
563
+ throw new Error("the Container runtime is unavailable");
564
+ }
565
+ const exec = await runtime.exec([
566
+ "sh",
567
+ "-c",
568
+ `mkdir -p ${EVE_HOST_SANDBOX_CARRY_SOURCE} && tar -xzf - -C ${EVE_HOST_SANDBOX_CARRY_SOURCE}`,
569
+ ], { stdin: archive, stderr: "pipe" });
570
+ let exitCode = -1;
571
+ try {
572
+ exitCode = await exec.exitCode;
296
573
  }
297
- const verified = await this.containerServesImage(runtime, requestImage);
298
- if (verified !== true) {
299
- this.lifecycle.record("image_mismatch");
300
- return false;
574
+ catch {
575
+ exitCode = -1;
576
+ }
577
+ if (exitCode !== 0) {
578
+ console.error(`The Eve sandbox session restore failed with exit code ${exitCode}; the agent serves without the carried files.`);
301
579
  }
302
580
  }
303
- await storage.put(EVE_HOST_CONTAINER_DEPLOYMENT_KEY, requestDeploymentId);
304
- return true;
305
- }
306
- async ensureEveReady(signal = new AbortController().signal, requestDeploymentId, requestImage) {
307
- await this.reconcileStartedDeployment(requestDeploymentId, requestImage);
308
- // Cached evidence only serves the deployment it was verified under;
309
- // a request for a different deployment re-runs readiness.
310
- if (this.readinessEvidence !== undefined &&
311
- this.readinessEvidence.deploymentId === requestDeploymentId) {
312
- return this.readinessEvidence.evidence;
581
+ catch (error) {
582
+ console.error("The Eve sandbox session restore failed; the agent serves without the carried files:", error instanceof Error ? error.message : "unknown");
313
583
  }
314
- if (!this.readinessStarted) {
315
- this.readinessStarted = true;
316
- this.lifecycle.record("start_requested");
584
+ }
585
+ async ensureEveReady(signal = new AbortController().signal) {
586
+ await this.reconcileRunningImage();
587
+ if (this.readinessEvidence !== undefined) {
588
+ return this.readinessEvidence;
317
589
  }
318
590
  const epoch = this.readinessEpoch;
319
591
  const evidence = await this.readiness(signal);
320
- if (epoch === this.readinessEpoch &&
321
- await this.recordStartedDeployment(requestDeploymentId, requestImage) &&
322
- epoch === this.readinessEpoch) {
323
- this.readinessEvidence = {
324
- deploymentId: requestDeploymentId,
325
- evidence,
326
- };
592
+ if (epoch === this.readinessEpoch) {
593
+ // Restore carried sandbox sessions while still inside the single-flight
594
+ // readiness path, before evidence is cached: every fetch and loopback
595
+ // request waits on this evidence, so nothing reaches Eve first.
596
+ await this.restoreCarriedSandboxSessions();
597
+ this.readinessEvidence = evidence;
598
+ // Readiness under this Worker version proves the running container
599
+ // serves `images.eve`: it was either verified by inspect() or freshly
600
+ // started from the configured image above.
601
+ this.imageVerified = true;
327
602
  }
328
- this.lifecycle.record("health_ready", evidence.healthStatus);
329
603
  return evidence;
330
604
  }
331
605
  async fetch(request) {
332
606
  const requestDeploymentId = request.headers.get("x-eden-eve-deployment-id") ?? undefined;
333
- await this.ensureEveReady(request.signal, requestDeploymentId, request.headers.get("x-eden-eve-image") ?? undefined);
334
- const response = await super.fetch(request);
335
- // Reporting the verified marker back lets deploy health-gate promotion on
336
- // the served container actually running this generation's image.
337
- if (this.readinessEvidence !== undefined &&
338
- this.readinessEvidence.deploymentId === requestDeploymentId &&
339
- requestDeploymentId !== undefined) {
607
+ const response = await this.forwardToEve(request);
608
+ // Reporting the verified start back lets deploy health-gate promotion on
609
+ // the served container actually running this generation's image: the
610
+ // header echoes only when the running container's image equals this
611
+ // Worker version's configured image and the request names this
612
+ // deployment.
613
+ if (requestDeploymentId !== undefined &&
614
+ requestDeploymentId === this.env.EDEN_EVE_DEPLOYMENT_ID &&
615
+ this.readinessEvidence !== undefined &&
616
+ this.imageVerified) {
340
617
  const headers = new Headers(response.headers);
341
618
  headers.set("x-eden-eve-started-deployment", requestDeploymentId);
342
619
  return new Response(response.body, {
@@ -347,26 +624,91 @@ export class EveHostContainer extends Container {
347
624
  }
348
625
  return response;
349
626
  }
350
- onStart() {
351
- if (this.lifecycle.events.some((event) => event.type === "started")) {
352
- this.lifecycle.record("replaced");
353
- }
354
- this.lifecycle.record("started");
627
+ /**
628
+ * Queue deliveries and loopback traffic enter here: same readiness and
629
+ * forwarding as `fetch` without the deployment-header echo rules (the
630
+ * loopback path is not a deployment-health signal).
631
+ */
632
+ async loopbackFetch(request) {
633
+ return this.forwardToEve(request);
355
634
  }
356
- onStop(params) {
357
- this.readinessEpoch += 1;
358
- this.readinessStarted = false;
359
- this.readinessEvidence = undefined;
360
- this.readiness.reset();
361
- this.lifecycle.record("stopped", `${params.reason}:${params.exitCode}`);
635
+ async forwardToEve(request) {
636
+ await this.ensureEveReady(request.signal);
637
+ await this.touchActivity();
638
+ const runtime = this.containerRuntime();
639
+ if (runtime === undefined) {
640
+ throw new EveHostError("HOST_READINESS_UNPROVEN", "The Container runtime is unavailable on this Durable Object.");
641
+ }
642
+ const url = new URL(request.url);
643
+ return runtime
644
+ .getTcpPort(EVE_HOST_DEFAULTS.internalPort)
645
+ .fetch(new Request(`http://container${url.pathname}${url.search}`, request));
362
646
  }
363
- onError(error) {
364
- this.readinessEpoch += 1;
365
- this.readinessStarted = false;
366
- this.readinessEvidence = undefined;
367
- this.readiness.reset();
368
- this.lifecycle.record("errored", error instanceof Error ? error.name : "unknown");
369
- throw new EveHostError("HOST_READINESS_UNPROVEN", "The Eve Container supervisor reported an error.");
647
+ /**
648
+ * The idle-sleep path: once the container has been quiet for `sleepAfter`,
649
+ * snapshot the writable filesystem (so `/workspace` survives the sleep)
650
+ * and destroy the instance. A snapshot failure retries up to
651
+ * `EVE_HOST_SNAPSHOT_MAX_ATTEMPTS` times, then destroys anyway so billing
652
+ * for the running container stops.
653
+ */
654
+ async alarm() {
655
+ const runtime = this.containerRuntime();
656
+ const lastActivity = (await this.ctx.storage.get(EVE_HOST_CONTAINER_ACTIVITY_KEY)) ?? 0;
657
+ const now = Date.now();
658
+ if (now - lastActivity < this.sleepAfterMs) {
659
+ await this.ctx.storage.setAlarm(lastActivity + this.sleepAfterMs);
660
+ return;
661
+ }
662
+ if (runtime === undefined || !runtime.running)
663
+ return;
664
+ // The snapshot restores same-image wakes; the carry additionally lets an
665
+ // update that lands while asleep restore the conversation sandboxes. A
666
+ // failed carry must not degrade the snapshot path, so it is only logged.
667
+ try {
668
+ await this.carrySandboxSessionsOut(runtime);
669
+ }
670
+ catch (error) {
671
+ console.error("The Eve sandbox session carry failed during idle sleep:", error instanceof Error ? error.message : "unknown");
672
+ }
673
+ const attempts = (await this.ctx.storage.get(EVE_HOST_CONTAINER_SNAPSHOT_ATTEMPTS_KEY)) ?? 0;
674
+ try {
675
+ const snapshot = await runtime.snapshotContainer({
676
+ name: "eden-workspace",
677
+ });
678
+ const image = (await runtime.inspect().catch(() => null))?.image ??
679
+ runtime.images["eve"] ??
680
+ "";
681
+ const record = {
682
+ id: snapshot.id,
683
+ image,
684
+ at: now,
685
+ };
686
+ await this.ctx.storage.put(EVE_HOST_CONTAINER_SNAPSHOT_KEY, record);
687
+ await this.ctx.storage.put(EVE_HOST_CONTAINER_SNAPSHOT_ATTEMPTS_KEY, 0);
688
+ // The snapshot takes seconds and requests may interleave with it; a
689
+ // request that arrived meanwhile must not have its container destroyed
690
+ // underneath it. Keep running and re-arm sleep from that activity.
691
+ const activityAfterSnapshot = (await this.ctx.storage.get(EVE_HOST_CONTAINER_ACTIVITY_KEY)) ?? 0;
692
+ if (activityAfterSnapshot !== lastActivity) {
693
+ await this.ctx.storage.setAlarm(activityAfterSnapshot + this.sleepAfterMs);
694
+ return;
695
+ }
696
+ await runtime.destroy(new Error("idle-snapshot-done"));
697
+ this.recordContainerStopped();
698
+ }
699
+ catch (error) {
700
+ const message = error instanceof Error ? error.message : "unknown";
701
+ if (attempts + 1 >= EVE_HOST_SNAPSHOT_MAX_ATTEMPTS) {
702
+ console.error(`The Eve workspace snapshot failed ${attempts + 1} times; destroying the container without it: ${message}`);
703
+ await this.ctx.storage.put(EVE_HOST_CONTAINER_SNAPSHOT_ATTEMPTS_KEY, 0);
704
+ await runtime.destroy(new Error("idle-snapshot-abandoned"));
705
+ this.recordContainerStopped();
706
+ return;
707
+ }
708
+ console.error(`The Eve workspace snapshot failed (attempt ${attempts + 1}): ${message}`);
709
+ await this.ctx.storage.put(EVE_HOST_CONTAINER_SNAPSHOT_ATTEMPTS_KEY, attempts + 1);
710
+ await this.ctx.storage.setAlarm(now + 60_000);
711
+ }
370
712
  }
371
713
  }
372
714
  export function createEveHostWorker(options) {
@@ -375,12 +717,9 @@ export function createEveHostWorker(options) {
375
717
  }
376
718
  assertStableOrigin(options.publicOrigin, options.workerName);
377
719
  assertNonEmpty(options.stableContainerInstanceName, "Container instance name");
378
- if (!IDENTIFIER_PATTERN.test(options.containerBindingName)) {
379
- throw new EveHostError("HOST_READINESS_UNPROVEN", "The Container binding name is not valid.");
380
- }
381
720
  const schedules = options.schedules ?? [];
382
721
  const resolveContainer = (env) => {
383
- const namespace = env[options.containerBindingName];
722
+ const namespace = env.EVE_CONTAINER;
384
723
  if (namespace === undefined) {
385
724
  throw new EveHostError("HOST_READINESS_UNPROVEN", "The configured Container binding is unavailable.");
386
725
  }
@@ -400,9 +739,6 @@ export function createEveHostWorker(options) {
400
739
  deploymentId: options.deploymentId,
401
740
  generationId: options.generationId,
402
741
  correlationId: globalThis.crypto.randomUUID(),
403
- ...(options.containerImage === undefined
404
- ? {}
405
- : { containerImage: options.containerImage }),
406
742
  ...(options.runtimeRevisionHandle === undefined
407
743
  ? {}
408
744
  : { runtimeRevisionHandle: options.runtimeRevisionHandle }),
@@ -433,17 +769,7 @@ export function createEveHostWorker(options) {
433
769
  // Wake only: the in-process Nitro runner inside the container
434
770
  // fires the tick itself, so the schedule runs exactly once.
435
771
  ctx.waitUntil(resolveContainer(env)
436
- .fetch(new Request(new URL(EVE_HOST_DEFAULTS.healthPath, options.publicOrigin), {
437
- headers: {
438
- // Stamp the Worker's own deployment so a stale Durable
439
- // Object never falls back to its own environment when
440
- // deciding whether to restart the running Container.
441
- "x-eden-eve-deployment-id": options.deploymentId,
442
- ...(options.containerImage === undefined
443
- ? {}
444
- : { "x-eden-eve-image": options.containerImage }),
445
- },
446
- }))
772
+ .fetch(new Request(new URL(EVE_HOST_DEFAULTS.healthPath, options.publicOrigin)))
447
773
  .catch((error) => {
448
774
  console.error("The scheduled wake request failed:", error instanceof Error ? error.message : "unknown");
449
775
  }));