@moinulmoin/eden-runtime-cloudflare 0.6.0 → 0.7.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.
@@ -23810,8 +23810,30 @@ var EVE_HOST_DEFAULTS = {
23810
23810
  healthPath: "/eve/v1/health",
23811
23811
  internalPort: 8080,
23812
23812
  instance: "standard-1",
23813
- sleepAfter: "24h"
23813
+ sleepAfter: "1h"
23814
23814
  };
23815
+ var EVE_HOST_SLEEP_SNAPSHOT_MARGIN_MS = 15 * 6e4;
23816
+ var EVE_HOST_MAX_SLEEP_AFTER_MS = 6 * 36e5 - EVE_HOST_SLEEP_SNAPSHOT_MARGIN_MS;
23817
+ function parseEveSleepAfterMs(value) {
23818
+ const match = /^([0-9]+)(ms|s|m|h|d)?$/u.exec(value.trim());
23819
+ if (match === null) {
23820
+ throw new EveHostError(
23821
+ "HOST_READINESS_UNPROVEN",
23822
+ "The Container sleepAfter override is not a parseable duration."
23823
+ );
23824
+ }
23825
+ const amount = Number.parseInt(match[1] ?? "", 10);
23826
+ const unit = match[2] ?? "s";
23827
+ const factor = unit === "ms" ? 1 : unit === "s" ? 1e3 : unit === "m" ? 6e4 : unit === "h" ? 36e5 : 864e5;
23828
+ const ms2 = amount * factor;
23829
+ if (ms2 > EVE_HOST_MAX_SLEEP_AFTER_MS) {
23830
+ throw new EveHostError(
23831
+ "HOST_READINESS_UNPROVEN",
23832
+ "The Container sleepAfter override exceeds 345m (5 h 45 min); Cloudflare stops a container within 6 hours of its Durable Object going idle, so Eden must snapshot before then."
23833
+ );
23834
+ }
23835
+ return ms2;
23836
+ }
23815
23837
  var EVE_SCHEDULE_WAKE_LEAD_MS = 24e4;
23816
23838
  var EVE_SCHEDULE_WAKE_GRACE_MS = 6e4;
23817
23839
  var MINUTE_MS = 6e4;
@@ -23944,15 +23966,7 @@ function isEveWorkflowInternalRoute(pathname) {
23944
23966
  }
23945
23967
  var EVE_HOST_OWNED_HEADERS = [
23946
23968
  "forwarded",
23947
- "x-forwarded-for",
23948
- "x-forwarded-host",
23949
- "x-forwarded-port",
23950
- "x-forwarded-proto",
23951
- "x-forwarded-server",
23952
- "x-forwarded-scheme",
23953
23969
  "x-real-ip",
23954
- "cf-connecting-ip",
23955
- "cf-visitor",
23956
23970
  "true-client-ip",
23957
23971
  "x-original-host",
23958
23972
  "x-original-proto",
@@ -23961,23 +23975,11 @@ var EVE_HOST_OWNED_HEADERS = [
23961
23975
  "x-proxy-host",
23962
23976
  "x-host",
23963
23977
  "x-eve-public-origin",
23964
- "x-workflow-local-base-url",
23965
- "x-eden-eve-callback-base",
23966
- "x-eden-eve-deployment-id",
23967
- "x-eden-eve-generation-id",
23968
- "x-eden-eve-started-deployment",
23969
- "x-eden-eve-public-origin",
23970
- "x-eden-eve-correlation-id",
23971
- "x-eden-eve-container-name",
23972
- "x-eden-eve-container-id",
23973
- "x-eden-eve-runtime-revision",
23974
- "x-eden-container-name",
23975
- "x-eden-container-id",
23976
- "cf-container-target-port"
23978
+ "x-workflow-local-base-url"
23977
23979
  ];
23978
23980
  var EVE_HOST_OWNED_HEADER_SET = new Set(EVE_HOST_OWNED_HEADERS);
23979
23981
  var WORKER_NAME_PATTERN = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/u;
23980
- var SUBDOMAIN_PATTERN = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/u;
23982
+ var SUBDOMAIN_PATTERN = WORKER_NAME_PATTERN;
23981
23983
  var IDENTIFIER_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/u;
23982
23984
  var EveHostError = class extends Error {
23983
23985
  code;
@@ -24090,21 +24092,6 @@ function throwIfAborted(signal) {
24090
24092
  );
24091
24093
  }
24092
24094
  }
24093
- function createEveHostLifecycleObserver(now = Date.now) {
24094
- const events = [];
24095
- return {
24096
- events,
24097
- record(type, safeStatus) {
24098
- const event = {
24099
- type,
24100
- at: now(),
24101
- ...safeStatus === void 0 ? {} : { safeStatus }
24102
- };
24103
- events.push(event);
24104
- return event;
24105
- }
24106
- };
24107
- }
24108
24095
  function responseBodyIsReady(value) {
24109
24096
  if (typeof value !== "object" || value === null) return false;
24110
24097
  const record2 = value;
@@ -24336,18 +24323,24 @@ function createEveReadinessGate(options) {
24336
24323
 
24337
24324
  // src/eve-host-runtime.ts
24338
24325
  var EVE_CLOUDFLARE_CONTAINERS_CA_PATH = "/etc/cloudflare/certs/cloudflare-containers-ca.crt";
24326
+ var EVE_HOST_STALE_WORKER_REASON = "stale Worker version";
24327
+ var EVE_HOST_ENTRYPOINT = Object.freeze([
24328
+ "/bin/sh",
24329
+ "-c",
24330
+ "rm -f /dev/kvm; cd /workspace && exec ./node_modules/.bin/eve start --host 0.0.0.0 --port 8080"
24331
+ ]);
24339
24332
  var EVE_HOST_CONTAINER_ACTIVITY_KEY = "eden.eve.host.last-activity-at";
24340
24333
  var EVE_HOST_CONTAINER_SNAPSHOT_KEY = "eden.eve.host.workspace-snapshot";
24341
24334
  var EVE_HOST_CONTAINER_SNAPSHOT_ATTEMPTS_KEY = "eden.eve.host.snapshot-attempts";
24335
+ var EVE_HOST_CONTAINER_STARTED_IMAGE_KEY = "eden.eve.host.started-image";
24336
+ var EVE_HOST_SANDBOX_CARRY_KEY = "eden.eve.host.sandbox-carry";
24337
+ var EVE_HOST_SANDBOX_CARRY_CHUNK_PREFIX = "eden.eve.host.sandbox-carry";
24338
+ var EVE_HOST_SANDBOX_CARRY_SOURCE = "/workspace/.eve/sandbox-cache/just-bash";
24339
+ var EVE_HOST_CARRY_LIMITS = {
24340
+ chunkBytes: 1024 * 1024,
24341
+ maxBytes: 1024 * 1024 * 1024
24342
+ };
24342
24343
  var EVE_HOST_SNAPSHOT_MAX_ATTEMPTS = 3;
24343
- function sleepAfterDurationMs(value) {
24344
- const match = /^([0-9]+)(ms|s|m|h|d)?$/u.exec(value.trim());
24345
- if (match === null) return void 0;
24346
- const amount = Number.parseInt(match[1] ?? "", 10);
24347
- const unit = match[2] ?? "s";
24348
- const factor = unit === "ms" ? 1 : unit === "s" ? 1e3 : unit === "m" ? 6e4 : unit === "h" ? 36e5 : 864e5;
24349
- return amount * factor;
24350
- }
24351
24344
  var EdenWorldDurableObject = class {
24352
24345
  core;
24353
24346
  constructor(ctx, env) {
@@ -24363,12 +24356,11 @@ var EdenWorldDurableObject = class {
24363
24356
  delivery: {
24364
24357
  deliver: async (message) => {
24365
24358
  const origin = env.EVE_PUBLIC_ORIGIN;
24366
- const binding = env.EVE_CONTAINER_BINDING_NAME;
24367
24359
  const instance = env.EVE_CONTAINER_INSTANCE_NAME;
24368
- if (typeof origin !== "string" || typeof binding !== "string" || typeof instance !== "string") {
24360
+ if (typeof origin !== "string" || typeof instance !== "string") {
24369
24361
  throw new Error("The World queue Container identity is unavailable.");
24370
24362
  }
24371
- const namespace = env[binding];
24363
+ const namespace = env.EVE_CONTAINER;
24372
24364
  const response = await namespace.getByName(instance).loopbackFetch(
24373
24365
  new Request(new URL(`/.well-known/workflow/v1/${message.path}`, origin), {
24374
24366
  method: "POST",
@@ -24411,16 +24403,14 @@ var EdenWorldDurableObject = class {
24411
24403
  };
24412
24404
  async function routeEveOutboundRequest(request, env) {
24413
24405
  const publicOrigin = env.EVE_PUBLIC_ORIGIN;
24414
- const bindingName = env.EVE_CONTAINER_BINDING_NAME;
24415
24406
  const instanceName = env.EVE_CONTAINER_INSTANCE_NAME;
24416
- if (typeof publicOrigin !== "string" || typeof bindingName !== "string" || typeof instanceName !== "string") {
24407
+ if (typeof publicOrigin !== "string" || typeof instanceName !== "string") {
24417
24408
  throw new EveHostError(
24418
24409
  "HOST_ORIGIN_UNAVAILABLE",
24419
24410
  "The container loopback identity is unavailable to the outbound handler."
24420
24411
  );
24421
24412
  }
24422
24413
  assertStableOrigin(publicOrigin);
24423
- assertNonEmpty(bindingName, "Container binding name");
24424
24414
  assertNonEmpty(instanceName, "Container instance name");
24425
24415
  const hostname3 = new URL(request.url).hostname;
24426
24416
  if (hostname3 !== new URL(publicOrigin).hostname) {
@@ -24431,7 +24421,7 @@ async function routeEveOutboundRequest(request, env) {
24431
24421
  if (world === void 0) return new Response(null, { status: 404 });
24432
24422
  return world.get(world.idFromName("world")).fetch(request);
24433
24423
  }
24434
- const namespace = env[bindingName];
24424
+ const namespace = env.EVE_CONTAINER;
24435
24425
  if (namespace === void 0) {
24436
24426
  throw new EveHostError(
24437
24427
  "HOST_READINESS_UNPROVEN",
@@ -24446,8 +24436,6 @@ var EveHostLoopback = class extends WorkerEntrypoint {
24446
24436
  }
24447
24437
  };
24448
24438
  var EveHostDurableContainer = class extends DurableObject {
24449
- lifecycle = createEveHostLifecycleObserver();
24450
- readinessStarted = false;
24451
24439
  /** Evidence is cached only while the container it was proven on keeps running. */
24452
24440
  readinessEvidence;
24453
24441
  /** True once the running container's image was proven to equal `images.eve`. */
@@ -24459,6 +24447,12 @@ var EveHostDurableContainer = class extends DurableObject {
24459
24447
  */
24460
24448
  readinessEpoch = 0;
24461
24449
  monitorAttached = false;
24450
+ /** Serializes sandbox-session carry-outs on this Durable Object instance. */
24451
+ carryOutQueue = Promise.resolve();
24452
+ /** True only while the last start was fresh from `images.eve`, not a snapshot. */
24453
+ pendingCarryRestore = false;
24454
+ /** Shared single-flight carry restore for the current container generation. */
24455
+ carryRestorePromise;
24462
24456
  envVars;
24463
24457
  sleepAfterMs;
24464
24458
  readiness;
@@ -24495,13 +24489,9 @@ var EveHostDurableContainer = class extends DurableObject {
24495
24489
  NODE_EXTRA_CA_CERTS: EVE_CLOUDFLARE_CONTAINERS_CA_PATH,
24496
24490
  ...env.EDEN_EVE_RUNTIME_REVISION === void 0 ? {} : { EDEN_EVE_RUNTIME_REVISION: env.EDEN_EVE_RUNTIME_REVISION }
24497
24491
  };
24498
- const sleepAfterOverride = env.EDEN_EVE_CONTAINER_SLEEP_AFTER;
24499
- this.sleepAfterMs = sleepAfterOverride === void 0 ? sleepAfterDurationMs(EVE_HOST_DEFAULTS.sleepAfter) ?? 864e5 : sleepAfterDurationMs(sleepAfterOverride) ?? (() => {
24500
- throw new EveHostError(
24501
- "HOST_READINESS_UNPROVEN",
24502
- "The Container sleepAfter override is not a parseable duration."
24503
- );
24504
- })();
24492
+ this.sleepAfterMs = parseEveSleepAfterMs(
24493
+ env.EDEN_EVE_CONTAINER_SLEEP_AFTER ?? EVE_HOST_DEFAULTS.sleepAfter
24494
+ );
24505
24495
  this.readiness = createEveReadinessGate({
24506
24496
  start: async () => {
24507
24497
  await this.ensureContainerStarted();
@@ -24531,9 +24521,10 @@ var EveHostDurableContainer = class extends DurableObject {
24531
24521
  containerRuntime() {
24532
24522
  return this.ctx.container;
24533
24523
  }
24534
- /** Container lifetime the platform keeps after the DO goes inactive. */
24524
+ /** Container lifetime the platform keeps after the DO goes inactive: long
24525
+ * enough for the idle alarm to snapshot first, within Cloudflare's 6 h cap. */
24535
24526
  get inactivityTimeoutMs() {
24536
- return this.sleepAfterMs + 15 * 6e4;
24527
+ return this.sleepAfterMs + EVE_HOST_SLEEP_SNAPSHOT_MARGIN_MS;
24537
24528
  }
24538
24529
  attachMonitor(runtime) {
24539
24530
  if (this.monitorAttached) return;
@@ -24541,23 +24532,21 @@ var EveHostDurableContainer = class extends DurableObject {
24541
24532
  runtime.monitor().then(
24542
24533
  () => {
24543
24534
  this.monitorAttached = false;
24544
- this.recordContainerStopped("exit");
24535
+ this.recordContainerStopped();
24545
24536
  },
24546
- (error61) => {
24537
+ () => {
24547
24538
  this.monitorAttached = false;
24548
- this.recordContainerStopped(
24549
- error61 instanceof Error ? error61.name : "runtime_signal"
24550
- );
24539
+ this.recordContainerStopped();
24551
24540
  }
24552
24541
  );
24553
24542
  }
24554
- recordContainerStopped(reason) {
24543
+ recordContainerStopped() {
24555
24544
  this.readinessEpoch += 1;
24556
- this.readinessStarted = false;
24557
24545
  this.readinessEvidence = void 0;
24558
24546
  this.imageVerified = false;
24547
+ this.pendingCarryRestore = false;
24548
+ this.carryRestorePromise = void 0;
24559
24549
  this.readiness.reset();
24560
- this.lifecycle.record("stopped", reason);
24561
24550
  }
24562
24551
  /** Routes the container's own requests to its public origin back inside. */
24563
24552
  async registerLoopbackIntercept(runtime) {
@@ -24585,7 +24574,7 @@ var EveHostDurableContainer = class extends DurableObject {
24585
24574
  * is restored only when it was captured from the exact image this Worker
24586
24575
  * version configures — restoring an older snapshot silently runs the
24587
24576
  * previous image — so after an image update the container starts fresh and
24588
- * workspace files intentionally do not carry across.
24577
+ * the readiness path restores the carried sandbox sessions instead.
24589
24578
  */
24590
24579
  async ensureContainerStarted() {
24591
24580
  const runtime = this.containerRuntime();
@@ -24606,24 +24595,26 @@ var EveHostDurableContainer = class extends DurableObject {
24606
24595
  const storedSnapshot = await this.ctx.storage.get(
24607
24596
  EVE_HOST_CONTAINER_SNAPSHOT_KEY
24608
24597
  );
24609
- if (this.lifecycle.events.some((event) => event.type === "started")) {
24610
- this.lifecycle.record("replaced");
24611
- }
24598
+ const snapshotId = storedSnapshot !== void 0 && storedSnapshot.image === expectedImage ? storedSnapshot.id : void 0;
24612
24599
  runtime.start({
24613
- ...storedSnapshot !== void 0 && storedSnapshot.image === expectedImage ? { containerSnapshot: { id: storedSnapshot.id } } : { image: expectedImage },
24600
+ ...snapshotId === void 0 ? { image: expectedImage } : { containerSnapshot: { id: snapshotId } },
24614
24601
  instance: EVE_HOST_DEFAULTS.instance,
24615
24602
  enableInternet: true,
24616
- env: this.envVars
24603
+ env: this.envVars,
24604
+ entrypoint: EVE_HOST_ENTRYPOINT
24617
24605
  });
24606
+ this.pendingCarryRestore = snapshotId === void 0;
24607
+ await this.ctx.storage.put(EVE_HOST_CONTAINER_STARTED_IMAGE_KEY, expectedImage);
24618
24608
  await this.registerLoopbackIntercept(runtime);
24619
24609
  this.attachMonitor(runtime);
24620
24610
  await runtime.setInactivityTimeout(this.inactivityTimeoutMs);
24621
- this.lifecycle.record("started");
24622
24611
  }
24623
24612
  /**
24624
- * Replaces the running container when `inspect()` proves it still serves a
24625
- * pre-update image: this Worker version's `images.eve` is selected at
24626
- * `start()`, so an in-place update must stop the stale instance itself.
24613
+ * Replaces the running container when it still serves a pre-update image:
24614
+ * this Worker version's `images.eve` is selected at `start()`, so an
24615
+ * in-place update must stop the stale instance itself. `inspect()` reports
24616
+ * an empty image for a snapshot-restored container, so the image recorded
24617
+ * at start stands in for it.
24627
24618
  */
24628
24619
  async reconcileRunningImage() {
24629
24620
  if (this.imageVerified) return;
@@ -24636,51 +24627,250 @@ var EveHostDurableContainer = class extends DurableObject {
24636
24627
  } catch {
24637
24628
  info = null;
24638
24629
  }
24639
- if (info?.image === expectedImage) {
24630
+ const runningImage = info?.image !== void 0 && info.image !== "" ? info.image : await this.ctx.storage.get(EVE_HOST_CONTAINER_STARTED_IMAGE_KEY);
24631
+ if (runningImage === expectedImage) {
24640
24632
  this.imageVerified = true;
24641
24633
  return;
24642
24634
  }
24643
- this.lifecycle.record("image_mismatch");
24644
- this.recordContainerStopped("replaced");
24635
+ this.recordContainerStopped();
24636
+ try {
24637
+ await this.carrySandboxSessionsOut(runtime);
24638
+ } catch (error61) {
24639
+ console.error(
24640
+ "The Eve sandbox session carry failed before the image update:",
24641
+ error61 instanceof Error ? error61.message : "unknown"
24642
+ );
24643
+ }
24645
24644
  await runtime.destroy();
24646
24645
  const stoppedAt = Date.now();
24647
24646
  while (runtime.running && Date.now() - stoppedAt < 1e4) {
24648
24647
  await new Promise((resolve) => setTimeout(resolve, 25));
24649
24648
  }
24650
24649
  }
24650
+ /**
24651
+ * Streams `/workspace/.eve/sandbox-cache/just-bash/sessions` — every
24652
+ * conversation's sandbox files and metadata — into Durable Object storage,
24653
+ * so a fresh container started by an image update can restore them. Not
24654
+ * carried: templates and the rest of `/workspace`, which come from the new
24655
+ * image. One carry runs at a time on this Durable Object; a failure never
24656
+ * blocks the destroy that follows and leaves the previous carry intact.
24657
+ */
24658
+ carrySandboxSessionsOut(runtime) {
24659
+ const run = this.carryOutQueue.then(
24660
+ () => this.carrySandboxSessionsOutNow(runtime)
24661
+ );
24662
+ this.carryOutQueue = run.then(
24663
+ () => void 0,
24664
+ () => void 0
24665
+ );
24666
+ return run;
24667
+ }
24668
+ async carrySandboxSessionsOutNow(runtime) {
24669
+ const previous = await this.ctx.storage.get(
24670
+ EVE_HOST_SANDBOX_CARRY_KEY
24671
+ );
24672
+ const carryId = globalThis.crypto.randomUUID();
24673
+ const chunkPrefix = `${EVE_HOST_SANDBOX_CARRY_CHUNK_PREFIX}.${carryId}.`;
24674
+ const writtenChunkKeys = [];
24675
+ const discardWrittenChunks = async () => {
24676
+ for (const key of writtenChunkKeys) await this.ctx.storage.delete(key);
24677
+ };
24678
+ const exec = await runtime.exec(
24679
+ [
24680
+ "sh",
24681
+ "-c",
24682
+ `cd ${EVE_HOST_SANDBOX_CARRY_SOURCE} && [ -d sessions ] && tar -czf - sessions || true`
24683
+ ],
24684
+ { stdout: "pipe", stderr: "pipe" }
24685
+ );
24686
+ const buffered = [];
24687
+ let bufferedBytes = 0;
24688
+ let chunks = 0;
24689
+ let bytes2 = 0;
24690
+ const flushOnce = async () => {
24691
+ if (bufferedBytes === 0) return;
24692
+ const size = Math.min(bufferedBytes, EVE_HOST_CARRY_LIMITS.chunkBytes);
24693
+ const value = new Uint8Array(size);
24694
+ let offset = 0;
24695
+ while (offset < size) {
24696
+ const part = buffered[0];
24697
+ if (part === void 0) break;
24698
+ const take = Math.min(part.byteLength, size - offset);
24699
+ value.set(
24700
+ take === part.byteLength ? part : part.subarray(0, take),
24701
+ offset
24702
+ );
24703
+ offset += take;
24704
+ if (take === part.byteLength) buffered.shift();
24705
+ else buffered[0] = part.subarray(take);
24706
+ }
24707
+ bufferedBytes -= size;
24708
+ const key = `${chunkPrefix}${chunks}`;
24709
+ writtenChunkKeys.push(key);
24710
+ await this.ctx.storage.put(key, value);
24711
+ chunks += 1;
24712
+ bytes2 += value.byteLength;
24713
+ };
24714
+ const flushBufferedChunks = async () => {
24715
+ while (bufferedBytes >= EVE_HOST_CARRY_LIMITS.chunkBytes) {
24716
+ await flushOnce();
24717
+ }
24718
+ };
24719
+ try {
24720
+ const stdout = exec.stdout;
24721
+ if (stdout === null) throw new Error("the exec stdout was not piped");
24722
+ const reader = stdout.getReader();
24723
+ let overflowed = false;
24724
+ for (; ; ) {
24725
+ const { done, value } = await reader.read();
24726
+ if (done) break;
24727
+ if (value === void 0) continue;
24728
+ buffered.push(value);
24729
+ bufferedBytes += value.byteLength;
24730
+ if (bytes2 + bufferedBytes > EVE_HOST_CARRY_LIMITS.maxBytes) {
24731
+ overflowed = true;
24732
+ break;
24733
+ }
24734
+ await flushBufferedChunks();
24735
+ }
24736
+ await reader.cancel().catch(() => void 0);
24737
+ if (overflowed) {
24738
+ await discardWrittenChunks();
24739
+ console.error(
24740
+ `The Eve sandbox session carry exceeded ${EVE_HOST_CARRY_LIMITS.maxBytes} compressed bytes; keeping the previous carry.`
24741
+ );
24742
+ return;
24743
+ }
24744
+ await flushOnce();
24745
+ } catch (error61) {
24746
+ await discardWrittenChunks();
24747
+ throw error61;
24748
+ }
24749
+ let exitCode = -1;
24750
+ try {
24751
+ exitCode = await exec.exitCode;
24752
+ } catch {
24753
+ exitCode = -1;
24754
+ }
24755
+ if (exitCode !== 0) {
24756
+ await discardWrittenChunks();
24757
+ console.error(
24758
+ `The Eve sandbox session carry failed with exit code ${exitCode}; keeping the previous carry.`
24759
+ );
24760
+ return;
24761
+ }
24762
+ if (bytes2 === 0) {
24763
+ return;
24764
+ }
24765
+ await this.ctx.storage.put(EVE_HOST_SANDBOX_CARRY_KEY, {
24766
+ id: carryId,
24767
+ chunks,
24768
+ bytes: bytes2,
24769
+ at: Date.now()
24770
+ });
24771
+ if (previous !== void 0) {
24772
+ for (let index = 0; index < previous.chunks; index += 1) {
24773
+ await this.ctx.storage.delete(
24774
+ `${EVE_HOST_SANDBOX_CARRY_CHUNK_PREFIX}.${previous.id}.${index}`
24775
+ );
24776
+ }
24777
+ }
24778
+ }
24779
+ /**
24780
+ * Single restore per fresh container generation, shared by every
24781
+ * concurrent readiness caller: the first caller starts it, everyone awaits
24782
+ * the same promise, and no request is forwarded before it settles.
24783
+ */
24784
+ async restoreCarriedSandboxSessions() {
24785
+ if (!this.pendingCarryRestore) return;
24786
+ this.carryRestorePromise ??= this.carrySandboxSessionsIn();
24787
+ await this.carryRestorePromise;
24788
+ this.pendingCarryRestore = false;
24789
+ }
24790
+ /**
24791
+ * Extracts the carried sandbox sessions into a container that started
24792
+ * fresh from `images.eve`. The archive streams chunk by chunk from Durable
24793
+ * Object storage through exec stdin, so it never sits in isolate memory. A
24794
+ * failure is logged and swallowed: the agent still serves, only the
24795
+ * carried files are missing.
24796
+ */
24797
+ async carrySandboxSessionsIn() {
24798
+ try {
24799
+ const carry = await this.ctx.storage.get(
24800
+ EVE_HOST_SANDBOX_CARRY_KEY
24801
+ );
24802
+ if (carry === void 0) return;
24803
+ let nextChunk = 0;
24804
+ const archive = new ReadableStream({
24805
+ pull: async (controller) => {
24806
+ if (nextChunk >= carry.chunks) {
24807
+ controller.close();
24808
+ return;
24809
+ }
24810
+ const chunk = await this.ctx.storage.get(
24811
+ `${EVE_HOST_SANDBOX_CARRY_CHUNK_PREFIX}.${carry.id}.${nextChunk}`
24812
+ );
24813
+ const index = nextChunk;
24814
+ nextChunk += 1;
24815
+ if (chunk === void 0) {
24816
+ controller.error(
24817
+ new Error(`The sandbox carry chunk ${index} is missing.`)
24818
+ );
24819
+ return;
24820
+ }
24821
+ controller.enqueue(chunk);
24822
+ }
24823
+ });
24824
+ const runtime = this.containerRuntime();
24825
+ if (runtime === void 0) {
24826
+ throw new Error("the Container runtime is unavailable");
24827
+ }
24828
+ const exec = await runtime.exec(
24829
+ [
24830
+ "sh",
24831
+ "-c",
24832
+ `mkdir -p ${EVE_HOST_SANDBOX_CARRY_SOURCE} && tar -xzf - -C ${EVE_HOST_SANDBOX_CARRY_SOURCE}`
24833
+ ],
24834
+ { stdin: archive, stderr: "pipe" }
24835
+ );
24836
+ let exitCode = -1;
24837
+ try {
24838
+ exitCode = await exec.exitCode;
24839
+ } catch {
24840
+ exitCode = -1;
24841
+ }
24842
+ if (exitCode !== 0) {
24843
+ console.error(
24844
+ `The Eve sandbox session restore failed with exit code ${exitCode}; the agent serves without the carried files.`
24845
+ );
24846
+ }
24847
+ } catch (error61) {
24848
+ console.error(
24849
+ "The Eve sandbox session restore failed; the agent serves without the carried files:",
24850
+ error61 instanceof Error ? error61.message : "unknown"
24851
+ );
24852
+ }
24853
+ }
24651
24854
  async ensureEveReady(signal = new AbortController().signal) {
24652
24855
  await this.reconcileRunningImage();
24653
24856
  if (this.readinessEvidence !== void 0) {
24654
24857
  return this.readinessEvidence;
24655
24858
  }
24656
- if (!this.readinessStarted) {
24657
- this.readinessStarted = true;
24658
- this.lifecycle.record("start_requested");
24659
- }
24660
24859
  const epoch = this.readinessEpoch;
24661
24860
  const evidence = await this.readiness(signal);
24662
24861
  if (epoch === this.readinessEpoch) {
24862
+ await this.restoreCarriedSandboxSessions();
24663
24863
  this.readinessEvidence = evidence;
24664
24864
  this.imageVerified = true;
24665
24865
  }
24666
- this.lifecycle.record("health_ready", evidence.healthStatus);
24667
24866
  return evidence;
24668
24867
  }
24669
24868
  async fetch(request) {
24670
24869
  const requestDeploymentId = request.headers.get("x-eden-eve-deployment-id") ?? void 0;
24671
- await this.ensureEveReady(request.signal);
24672
- await this.touchActivity();
24673
- const runtime = this.containerRuntime();
24674
- if (runtime === void 0) {
24675
- throw new EveHostError(
24676
- "HOST_READINESS_UNPROVEN",
24677
- "The Container runtime is unavailable on this Durable Object."
24678
- );
24870
+ if (requestDeploymentId !== void 0 && requestDeploymentId !== this.env.EDEN_EVE_DEPLOYMENT_ID) {
24871
+ this.ctx.abort(EVE_HOST_STALE_WORKER_REASON);
24679
24872
  }
24680
- const url2 = new URL(request.url);
24681
- const response = await runtime.getTcpPort(EVE_HOST_DEFAULTS.internalPort).fetch(
24682
- new Request(`http://container${url2.pathname}${url2.search}`, request)
24683
- );
24873
+ const response = await this.forwardToEve(request);
24684
24874
  if (requestDeploymentId !== void 0 && requestDeploymentId === this.env.EDEN_EVE_DEPLOYMENT_ID && this.readinessEvidence !== void 0 && this.imageVerified) {
24685
24875
  const headers = new Headers(response.headers);
24686
24876
  headers.set("x-eden-eve-started-deployment", requestDeploymentId);
@@ -24698,6 +24888,9 @@ var EveHostDurableContainer = class extends DurableObject {
24698
24888
  * loopback path is not a deployment-health signal).
24699
24889
  */
24700
24890
  async loopbackFetch(request) {
24891
+ return this.forwardToEve(request);
24892
+ }
24893
+ async forwardToEve(request) {
24701
24894
  await this.ensureEveReady(request.signal);
24702
24895
  await this.touchActivity();
24703
24896
  const runtime = this.containerRuntime();
@@ -24728,6 +24921,14 @@ var EveHostDurableContainer = class extends DurableObject {
24728
24921
  return;
24729
24922
  }
24730
24923
  if (runtime === void 0 || !runtime.running) return;
24924
+ try {
24925
+ await this.carrySandboxSessionsOut(runtime);
24926
+ } catch (error61) {
24927
+ console.error(
24928
+ "The Eve sandbox session carry failed during idle sleep:",
24929
+ error61 instanceof Error ? error61.message : "unknown"
24930
+ );
24931
+ }
24731
24932
  const attempts = await this.ctx.storage.get(
24732
24933
  EVE_HOST_CONTAINER_SNAPSHOT_ATTEMPTS_KEY
24733
24934
  ) ?? 0;
@@ -24749,7 +24950,7 @@ var EveHostDurableContainer = class extends DurableObject {
24749
24950
  return;
24750
24951
  }
24751
24952
  await runtime.destroy(new Error("idle-snapshot-done"));
24752
- this.recordContainerStopped("snapshot:idle");
24953
+ this.recordContainerStopped();
24753
24954
  } catch (error61) {
24754
24955
  const message = error61 instanceof Error ? error61.message : "unknown";
24755
24956
  if (attempts + 1 >= EVE_HOST_SNAPSHOT_MAX_ATTEMPTS) {
@@ -24763,7 +24964,7 @@ var EveHostDurableContainer = class extends DurableObject {
24763
24964
  await runtime.destroy(
24764
24965
  new Error("idle-snapshot-abandoned")
24765
24966
  );
24766
- this.recordContainerStopped("snapshot:abandoned");
24967
+ this.recordContainerStopped();
24767
24968
  return;
24768
24969
  }
24769
24970
  console.error(
@@ -24786,15 +24987,9 @@ function createEveHostWorker(options) {
24786
24987
  }
24787
24988
  assertStableOrigin(options.publicOrigin, options.workerName);
24788
24989
  assertNonEmpty(options.stableContainerInstanceName, "Container instance name");
24789
- if (!IDENTIFIER_PATTERN.test(options.containerBindingName)) {
24790
- throw new EveHostError(
24791
- "HOST_READINESS_UNPROVEN",
24792
- "The Container binding name is not valid."
24793
- );
24794
- }
24795
24990
  const schedules = options.schedules ?? [];
24796
24991
  const resolveContainer = (env) => {
24797
- const namespace = env[options.containerBindingName];
24992
+ const namespace = env.EVE_CONTAINER;
24798
24993
  if (namespace === void 0) {
24799
24994
  throw new EveHostError(
24800
24995
  "HOST_READINESS_UNPROVEN",
@@ -24803,20 +24998,40 @@ function createEveHostWorker(options) {
24803
24998
  }
24804
24999
  return namespace.getByName(options.stableContainerInstanceName);
24805
25000
  };
25001
+ const forwardToContainer = async (request, env) => {
25002
+ const metadata = {
25003
+ publicOrigin: options.publicOrigin,
25004
+ deploymentId: options.deploymentId,
25005
+ generationId: options.generationId,
25006
+ correlationId: globalThis.crypto.randomUUID(),
25007
+ ...options.runtimeRevisionHandle === void 0 ? {} : { runtimeRevisionHandle: options.runtimeRevisionHandle }
25008
+ };
25009
+ const forwarded = createTrustedEveRequest(request, metadata);
25010
+ const bodyless = forwarded.body === null ? forwarded : createTrustedEveRequest(new Request(
25011
+ new URL(EVE_HOST_DEFAULTS.healthPath, options.publicOrigin),
25012
+ { signal: forwarded.signal }
25013
+ ), metadata);
25014
+ let container = resolveContainer(env);
25015
+ let response;
25016
+ try {
25017
+ response = await container.fetch(bodyless);
25018
+ } catch (error61) {
25019
+ if (!(error61 instanceof Error) || !error61.message.includes(EVE_HOST_STALE_WORKER_REASON)) {
25020
+ throw error61;
25021
+ }
25022
+ container = resolveContainer(env);
25023
+ response = await container.fetch(bodyless);
25024
+ }
25025
+ if (bodyless === forwarded) return response;
25026
+ await response.body?.cancel();
25027
+ return container.fetch(forwarded);
25028
+ };
24806
25029
  return {
24807
25030
  async fetch(request, env) {
24808
25031
  if (isEveWorkflowInternalRoute(new URL(request.url).pathname)) {
24809
25032
  return new Response(null, { status: 404 });
24810
25033
  }
24811
- const container = resolveContainer(env);
24812
- const forwarded = createTrustedEveRequest(request, {
24813
- publicOrigin: options.publicOrigin,
24814
- deploymentId: options.deploymentId,
24815
- generationId: options.generationId,
24816
- correlationId: globalThis.crypto.randomUUID(),
24817
- ...options.runtimeRevisionHandle === void 0 ? {} : { runtimeRevisionHandle: options.runtimeRevisionHandle }
24818
- });
24819
- const response = await container.fetch(forwarded);
25034
+ const response = await forwardToContainer(request, env);
24820
25035
  const headers = new Headers(response.headers);
24821
25036
  headers.set("x-eden-eve-worker-name", options.workerName);
24822
25037
  headers.set("x-eden-eve-public-origin", options.publicOrigin);
@@ -24847,16 +25062,9 @@ function createEveHostWorker(options) {
24847
25062
  ));
24848
25063
  if (!due) return;
24849
25064
  ctx.waitUntil(
24850
- resolveContainer(env).fetch(new Request(
24851
- new URL(EVE_HOST_DEFAULTS.healthPath, options.publicOrigin),
24852
- {
24853
- headers: {
24854
- // Stamp the Worker's own deployment so a stale Durable
24855
- // Object never falls back to its own environment.
24856
- "x-eden-eve-deployment-id": options.deploymentId
24857
- }
24858
- }
24859
- )).catch((error61) => {
25065
+ forwardToContainer(new Request(
25066
+ new URL(EVE_HOST_DEFAULTS.healthPath, options.publicOrigin)
25067
+ ), env).catch((error61) => {
24860
25068
  console.error(
24861
25069
  "The scheduled wake request failed:",
24862
25070
  error61 instanceof Error ? error61.message : "unknown"
@@ -24868,6 +25076,7 @@ function createEveHostWorker(options) {
24868
25076
  };
24869
25077
  }
24870
25078
  export {
25079
+ EVE_HOST_CARRY_LIMITS,
24871
25080
  EdenWorldDurableObject,
24872
25081
  EveHostDurableContainer,
24873
25082
  EveHostLoopback,