@moinulmoin/eden-runtime-cloudflare 0.12.0 → 0.14.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.
@@ -25802,6 +25802,9 @@ function createTrustedEveRequest(request, metadata) {
25802
25802
  headers.set("x-eden-eve-public-origin", metadata.publicOrigin);
25803
25803
  headers.set("x-eden-eve-deployment-id", metadata.deploymentId);
25804
25804
  headers.set("x-eden-eve-release-id", metadata.releaseId ?? metadata.deploymentId);
25805
+ if (metadata.previousReleaseId !== void 0) {
25806
+ headers.set("x-eden-eve-previous-release-id", metadata.previousReleaseId);
25807
+ }
25805
25808
  headers.set("x-eden-eve-generation-id", metadata.generationId);
25806
25809
  headers.set("x-eden-eve-correlation-id", metadata.correlationId);
25807
25810
  if (metadata.runtimeRevisionHandle !== void 0) {
@@ -25850,6 +25853,7 @@ var RESERVED_EVE_HOST_VARIABLES = /* @__PURE__ */ new Set([
25850
25853
  "EDEN_EVE_GENERATION_ID",
25851
25854
  "EDEN_EVE_DEPLOYMENTS",
25852
25855
  "EDEN_EVE_RELEASE_ID",
25856
+ "EDEN_EVE_PREVIOUS_RELEASE_ID",
25853
25857
  "EDEN_AFFINITY_CONTRACT",
25854
25858
  "EDEN_EVE_CONTROL_TOKEN",
25855
25859
  "EVE_SCHEDULES_DISABLED",
@@ -26069,6 +26073,64 @@ function createEveReadinessGate(options) {
26069
26073
  // src/eve-host-runtime.ts
26070
26074
  var EVE_CLOUDFLARE_CONTAINERS_CA_PATH = "/etc/cloudflare/certs/cloudflare-containers-ca.crt";
26071
26075
  var EVE_HOST_STALE_WORKER_REASON = "stale Worker version";
26076
+ var EVE_HOST_RELEASE_WAIT_MS = 3e4;
26077
+ var EVE_HOST_RELEASE_RETRY_DELAYS_MS = [0, 250, 500, 1e3, 2e3, 4e3];
26078
+ function isStaleRelease(error61) {
26079
+ return error61 instanceof Error && error61.message.includes(EVE_HOST_STALE_WORKER_REASON);
26080
+ }
26081
+ function switchingVersions(retryAfterSeconds) {
26082
+ return new Response("Eden is switching versions. Retry the request.\n", {
26083
+ status: 503,
26084
+ headers: { "retry-after": String(retryAfterSeconds) }
26085
+ });
26086
+ }
26087
+ function releaseHeaders(env) {
26088
+ const release = env.EDEN_EVE_RELEASE_ID ?? env.EDEN_EVE_DEPLOYMENT_ID;
26089
+ return {
26090
+ ...release === void 0 ? {} : { "x-eden-eve-release-id": release },
26091
+ ...env.EDEN_EVE_PREVIOUS_RELEASE_ID === void 0 ? {} : { "x-eden-eve-previous-release-id": env.EDEN_EVE_PREVIOUS_RELEASE_ID }
26092
+ };
26093
+ }
26094
+ function placeCaller(env, headers, caller) {
26095
+ if (caller === void 0 || caller === (env.EDEN_EVE_RELEASE_ID ?? env.EDEN_EVE_DEPLOYMENT_ID)) return "current";
26096
+ if (caller === env.EDEN_EVE_PREVIOUS_RELEASE_ID) return "older";
26097
+ return headers.get("x-eden-eve-previous-release-id") === (env.EDEN_EVE_RELEASE_ID ?? env.EDEN_EVE_DEPLOYMENT_ID) ? "newer" : "unknown";
26098
+ }
26099
+ async function sendWhileSwitching(send, deadline, signal) {
26100
+ const gone = () => signal?.aborted === true;
26101
+ for (let attempt = 0; ; attempt += 1) {
26102
+ try {
26103
+ return await send();
26104
+ } catch (error61) {
26105
+ if (!isStaleRelease(error61)) throw error61;
26106
+ const left = deadline - Date.now();
26107
+ if (left <= 0 || gone()) return switchingVersions(5);
26108
+ const delay = EVE_HOST_RELEASE_RETRY_DELAYS_MS[attempt] ?? 4e3;
26109
+ await new Promise((resolve) => setTimeout(resolve, Math.min(delay, left)));
26110
+ if (Date.now() > deadline || gone()) return switchingVersions(5);
26111
+ }
26112
+ }
26113
+ }
26114
+ var EVE_HOST_RETAINED_SWITCHING = new Error("A retained deployment is switching versions.");
26115
+ var worldsOnThisRelease = /* @__PURE__ */ new WeakSet();
26116
+ async function nudgeWorld(env, world, deadline) {
26117
+ if (worldsOnThisRelease.has(world)) return new Response(null, { status: 405 });
26118
+ let timer;
26119
+ try {
26120
+ const response = await Promise.race([
26121
+ world.get(world.idFromName("world")).fetch(
26122
+ new Request(new URL("/__eden/world/rpc", env.EVE_PUBLIC_ORIGIN), { headers: releaseHeaders(env) })
26123
+ ),
26124
+ new Promise((resolve) => {
26125
+ timer = setTimeout(() => resolve(switchingVersions(5)), Math.max(0, deadline - Date.now()));
26126
+ })
26127
+ ]);
26128
+ if (response.status === 405) worldsOnThisRelease.add(world);
26129
+ return response;
26130
+ } finally {
26131
+ clearTimeout(timer);
26132
+ }
26133
+ }
26072
26134
  function deploymentImages(env) {
26073
26135
  const raw = env.EDEN_EVE_DEPLOYMENTS;
26074
26136
  return typeof raw === "string" ? JSON.parse(raw) : {};
@@ -26259,6 +26321,9 @@ var EdenWorldDurableObject = class extends DurableObject {
26259
26321
  void ctx.blockConcurrencyWhile(async () => this.core.migrate());
26260
26322
  }
26261
26323
  async fetch(request) {
26324
+ if (placeCaller(this.env, request.headers, request.headers.get("x-eden-eve-release-id") ?? void 0) === "newer") {
26325
+ this.ctx.abort(EVE_HOST_STALE_WORKER_REASON);
26326
+ }
26262
26327
  if (request.method !== "POST") return new Response(null, { status: 405 });
26263
26328
  const result = await this.core.handleRpc(new Uint8Array(await request.arrayBuffer()));
26264
26329
  return new Response(result, {
@@ -26311,7 +26376,21 @@ async function routeEveOutboundRequest(request, env, sourceDeploymentId, expecte
26311
26376
  }
26312
26377
  const world = env.EDEN_WORLD;
26313
26378
  if (world === void 0) return new Response(null, { status: 404 });
26314
- return world.get(world.idFromName("world")).fetch(request);
26379
+ const deadline = Date.now() + EVE_HOST_RELEASE_WAIT_MS;
26380
+ const nudge = await sendWhileSwitching(() => nudgeWorld(env, world, deadline), deadline);
26381
+ if (nudge.status === 503) return nudge;
26382
+ await nudge.body?.cancel();
26383
+ const headers = new Headers(request.headers);
26384
+ headers.delete("x-eden-eve-release-id");
26385
+ headers.delete("x-eden-eve-previous-release-id");
26386
+ for (const [name, value] of Object.entries(releaseHeaders(env))) headers.set(name, value);
26387
+ try {
26388
+ return await world.get(world.idFromName("world")).fetch(new Request(request, { headers }));
26389
+ } catch (error61) {
26390
+ if (!isStaleRelease(error61)) throw error61;
26391
+ worldsOnThisRelease.delete(world);
26392
+ return switchingVersions(1);
26393
+ }
26315
26394
  }
26316
26395
  if (env.EDEN_EVE_WORLD_CLOUDFLARE && isEveWorkflowInternalRoute(new URL(request.url).pathname)) {
26317
26396
  return new Response(null, { status: 403 });
@@ -26361,6 +26440,7 @@ var EveHostDurableContainer = class extends DurableObject {
26361
26440
  activeDeliveries = 0;
26362
26441
  activeForwarding = 0;
26363
26442
  schedulerRoleReconcile;
26443
+ imageReconcile;
26364
26444
  retainedFences;
26365
26445
  deliveryClaims = /* @__PURE__ */ new Map();
26366
26446
  async identifyDeployment(requested) {
@@ -26402,18 +26482,10 @@ var EveHostDurableContainer = class extends DurableObject {
26402
26482
  for (const id of Object.keys(deploymentImages(this.env))) {
26403
26483
  if (id === this.deploymentId) continue;
26404
26484
  const request = new Request(new URL("/__eden/internal/fence-schedules", this.env.EVE_PUBLIC_ORIGIN), {
26405
- headers: {
26406
- "x-eden-eve-deployment-id": id,
26407
- "x-eden-eve-release-id": this.env.EDEN_EVE_RELEASE_ID ?? this.env.EDEN_EVE_DEPLOYMENT_ID
26408
- }
26485
+ headers: { "x-eden-eve-deployment-id": id, ...releaseHeaders(this.env) }
26409
26486
  });
26410
- let response;
26411
- try {
26412
- response = await namespace.getByName(deploymentInstance(this.env, id)).fetch(request);
26413
- } catch (error61) {
26414
- if (!(error61 instanceof Error) || !error61.message.includes(EVE_HOST_STALE_WORKER_REASON)) throw error61;
26415
- response = await namespace.getByName(deploymentInstance(this.env, id)).fetch(request);
26416
- }
26487
+ const response = await namespace.getByName(deploymentInstance(this.env, id)).fetch(request);
26488
+ if (response.status === 503) throw EVE_HOST_RETAINED_SWITCHING;
26417
26489
  if (!response.ok) throw new Error(`Cannot fence schedules on retained deployment ${id}.`);
26418
26490
  await response.body?.cancel();
26419
26491
  }
@@ -26656,6 +26728,7 @@ var EveHostDurableContainer = class extends DurableObject {
26656
26728
  const worldToken = globalThis.crypto.randomUUID();
26657
26729
  await this.ctx.storage.put("eden.eve.host.world-token", worldToken);
26658
26730
  this.envVars.EDEN_WORLD_TOKEN = worldToken;
26731
+ await this.ctx.storage.put(EVE_HOST_CONTAINER_STARTED_IMAGE_KEY, expectedImage);
26659
26732
  runtime.start({
26660
26733
  ...snapshotId === void 0 ? { image: expectedImage } : { containerSnapshot: { id: snapshotId } },
26661
26734
  instance: EVE_HOST_DEFAULTS.instance,
@@ -26671,7 +26744,6 @@ var EveHostDurableContainer = class extends DurableObject {
26671
26744
  this.pendingCarryRestore = snapshotId === void 0;
26672
26745
  this.startedFromSnapshot = snapshotId !== void 0;
26673
26746
  await this.ctx.storage.delete("eden.eve.host.scheduler-fence-pending");
26674
- await this.ctx.storage.put(EVE_HOST_CONTAINER_STARTED_IMAGE_KEY, expectedImage);
26675
26747
  await this.registerLoopbackIntercept(runtime);
26676
26748
  this.attachMonitor(runtime);
26677
26749
  await runtime.setInactivityTimeout(this.inactivityTimeoutMs);
@@ -26684,6 +26756,12 @@ var EveHostDurableContainer = class extends DurableObject {
26684
26756
  * at start stands in for it.
26685
26757
  */
26686
26758
  async reconcileRunningImage() {
26759
+ this.imageReconcile ??= this.replaceStaleImage().finally(() => {
26760
+ this.imageReconcile = void 0;
26761
+ });
26762
+ await this.imageReconcile;
26763
+ }
26764
+ async replaceStaleImage() {
26687
26765
  if (this.imageVerified) return;
26688
26766
  const runtime = this.containerRuntime();
26689
26767
  if (runtime === void 0 || !runtime.running) return;
@@ -26968,8 +27046,12 @@ var EveHostDurableContainer = class extends DurableObject {
26968
27046
  async fetch(request) {
26969
27047
  const requestDeploymentId = request.headers.get("x-eden-eve-deployment-id") ?? void 0;
26970
27048
  const requestedRelease = request.headers.get("x-eden-eve-release-id") ?? requestDeploymentId;
26971
- if (requestedRelease !== void 0 && requestedRelease !== (this.env.EDEN_EVE_RELEASE_ID ?? this.env.EDEN_EVE_DEPLOYMENT_ID)) {
26972
- this.ctx.abort(EVE_HOST_STALE_WORKER_REASON);
27049
+ const caller = placeCaller(this.env, request.headers, requestedRelease);
27050
+ if (caller === "newer") this.ctx.abort(EVE_HOST_STALE_WORKER_REASON);
27051
+ if (caller === "unknown") return switchingVersions(5);
27052
+ const olderCaller = caller === "older";
27053
+ if (olderCaller && requestDeploymentId !== void 0 && (this.env.EDEN_EVE_DEPLOYMENTS === void 0 ? requestDeploymentId !== this.env.EDEN_EVE_DEPLOYMENT_ID : deploymentImages(this.env)[requestDeploymentId] === void 0)) {
27054
+ return switchingVersions(1);
26973
27055
  }
26974
27056
  await this.identifyDeployment(requestDeploymentId);
26975
27057
  if (new URL(request.url).pathname === "/__eden/internal/fence-schedules") {
@@ -26979,8 +27061,14 @@ var EveHostDurableContainer = class extends DurableObject {
26979
27061
  }
26980
27062
  return new Response(null, { status: 204 });
26981
27063
  }
26982
- const response = await this.forwardToEve(request);
26983
- if (requestDeploymentId !== void 0 && requestDeploymentId === this.deploymentId && this.readinessEvidence !== void 0 && this.imageVerified) {
27064
+ let response;
27065
+ try {
27066
+ response = await this.forwardToEve(request);
27067
+ } catch (error61) {
27068
+ if (error61 !== EVE_HOST_RETAINED_SWITCHING) throw error61;
27069
+ return switchingVersions(5);
27070
+ }
27071
+ if (requestDeploymentId !== void 0 && requestDeploymentId === this.deploymentId && !olderCaller && this.readinessEvidence !== void 0 && this.imageVerified) {
26984
27072
  const headers = new Headers(response.headers);
26985
27073
  headers.set("x-eden-eve-started-deployment", this.env.EDEN_EVE_RELEASE_ID ?? requestDeploymentId);
26986
27074
  return new Response(response.body, {
@@ -27385,29 +27473,34 @@ function createEveHostWorker(options) {
27385
27473
  publicOrigin: options.publicOrigin,
27386
27474
  deploymentId: options.deploymentId,
27387
27475
  releaseId: options.releaseId ?? options.deploymentId,
27476
+ ...env.EDEN_EVE_PREVIOUS_RELEASE_ID === void 0 ? {} : { previousReleaseId: env.EDEN_EVE_PREVIOUS_RELEASE_ID },
27388
27477
  generationId: options.generationId,
27389
27478
  correlationId: globalThis.crypto.randomUUID(),
27390
27479
  ...options.runtimeRevisionHandle === void 0 ? {} : { runtimeRevisionHandle: options.runtimeRevisionHandle }
27391
27480
  };
27392
27481
  const forwarded = createTrustedEveRequest(request, metadata);
27393
- const bodyless = forwarded.body === null ? forwarded : createTrustedEveRequest(new Request(
27482
+ const read2 = request.method === "GET" || request.method === "HEAD";
27483
+ const check2 = read2 ? forwarded : createTrustedEveRequest(new Request(
27394
27484
  new URL(EVE_HOST_DEFAULTS.healthPath, options.publicOrigin),
27395
27485
  { signal: forwarded.signal }
27396
27486
  ), metadata);
27397
- let container = resolveContainer(env);
27398
- let response;
27487
+ let container;
27488
+ const response = await sendWhileSwitching(
27489
+ () => {
27490
+ container = resolveContainer(env);
27491
+ return container.fetch(check2);
27492
+ },
27493
+ Date.now() + EVE_HOST_RELEASE_WAIT_MS,
27494
+ request.signal
27495
+ );
27496
+ if (read2 || !response.ok) return response;
27497
+ await response.body?.cancel();
27399
27498
  try {
27400
- response = await container.fetch(bodyless);
27499
+ return await container.fetch(forwarded);
27401
27500
  } catch (error61) {
27402
- if (!(error61 instanceof Error) || !error61.message.includes(EVE_HOST_STALE_WORKER_REASON)) {
27403
- throw error61;
27404
- }
27405
- container = resolveContainer(env);
27406
- response = await container.fetch(bodyless);
27501
+ if (!isStaleRelease(error61)) throw error61;
27502
+ return switchingVersions(1);
27407
27503
  }
27408
- if (bodyless === forwarded) return response;
27409
- await response.body?.cancel();
27410
- return container.fetch(forwarded);
27411
27504
  };
27412
27505
  return {
27413
27506
  async fetch(request, env) {
@@ -27461,7 +27554,10 @@ function createEveHostWorker(options) {
27461
27554
  ctx.waitUntil(
27462
27555
  forwardToContainer(new Request(
27463
27556
  new URL(EVE_HOST_DEFAULTS.healthPath, options.publicOrigin)
27464
- ), env).catch((error61) => {
27557
+ ), env).then((response) => {
27558
+ if (!response.ok) console.error("The scheduled wake request failed:", `HTTP ${response.status}`);
27559
+ return response;
27560
+ }).catch((error61) => {
27465
27561
  console.error(
27466
27562
  "The scheduled wake request failed:",
27467
27563
  error61 instanceof Error ? error61.message : "unknown"
@@ -88,6 +88,7 @@ export declare class EveHostDurableContainer extends DurableObject<EveHostContai
88
88
  private activeDeliveries;
89
89
  private activeForwarding;
90
90
  private schedulerRoleReconcile;
91
+ private imageReconcile;
91
92
  private retainedFences;
92
93
  private readonly deliveryClaims;
93
94
  private identifyDeployment;
@@ -126,6 +127,7 @@ export declare class EveHostDurableContainer extends DurableObject<EveHostContai
126
127
  * at start stands in for it.
127
128
  */
128
129
  private reconcileRunningImage;
130
+ private replaceStaleImage;
129
131
  /**
130
132
  * Streams `/workspace/.eve/sandbox-cache/just-bash/sessions` — every
131
133
  * conversation's sandbox files and metadata — into Durable Object storage,
@@ -3,6 +3,103 @@ import { WorldCore } from "@moinulmoin/eden-world-cloudflare/core";
3
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";
4
4
  const EVE_CLOUDFLARE_CONTAINERS_CA_PATH = "/etc/cloudflare/certs/cloudflare-containers-ca.crt";
5
5
  const EVE_HOST_STALE_WORKER_REASON = "stale Worker version";
6
+ /**
7
+ * How long a request waits for an object that is still on the release before
8
+ * its caller's. A reset object can come back on the old code until Cloudflare
9
+ * has the new version where the object runs: 20 s was seen on 2026-11-20.
10
+ */
11
+ const EVE_HOST_RELEASE_WAIT_MS = 30_000;
12
+ const EVE_HOST_RELEASE_RETRY_DELAYS_MS = [0, 250, 500, 1_000, 2_000, 4_000];
13
+ function isStaleRelease(error) {
14
+ return error instanceof Error && error.message.includes(EVE_HOST_STALE_WORKER_REASON);
15
+ }
16
+ function switchingVersions(retryAfterSeconds) {
17
+ return new Response("Eden is switching versions. Retry the request.\n", {
18
+ status: 503,
19
+ headers: { "retry-after": String(retryAfterSeconds) },
20
+ });
21
+ }
22
+ /** The release stamp an object uses to place its caller. */
23
+ function releaseHeaders(env) {
24
+ const release = env.EDEN_EVE_RELEASE_ID ?? env.EDEN_EVE_DEPLOYMENT_ID;
25
+ return {
26
+ ...(release === undefined ? {} : { "x-eden-eve-release-id": release }),
27
+ ...(env.EDEN_EVE_PREVIOUS_RELEASE_ID === undefined
28
+ ? {}
29
+ : { "x-eden-eve-previous-release-id": env.EDEN_EVE_PREVIOUS_RELEASE_ID }),
30
+ };
31
+ }
32
+ /**
33
+ * Where an object places a caller. It serves its own release and the one it
34
+ * replaced. It resets only for the release that names it as the one it
35
+ * replaces, which proves that caller is newer. Any other release gets a 503,
36
+ * so two releases can never reset each other.
37
+ */
38
+ function placeCaller(env, headers, caller) {
39
+ if (caller === undefined || caller === (env.EDEN_EVE_RELEASE_ID ?? env.EDEN_EVE_DEPLOYMENT_ID))
40
+ return "current";
41
+ if (caller === env.EDEN_EVE_PREVIOUS_RELEASE_ID)
42
+ return "older";
43
+ return headers.get("x-eden-eve-previous-release-id") === (env.EDEN_EVE_RELEASE_ID ?? env.EDEN_EVE_DEPLOYMENT_ID)
44
+ ? "newer"
45
+ : "unknown";
46
+ }
47
+ /**
48
+ * Sends a request that is safe to repeat (a read) until the object stops
49
+ * resetting for it. An object-wide reset says nothing about whether a request
50
+ * ran, so a write is never sent through here. No try starts after
51
+ * `deadline`; a failed stub can stay broken, so `send` resolves the object
52
+ * anew on every try.
53
+ */
54
+ async function sendWhileSwitching(send, deadline, signal) {
55
+ const gone = () => signal?.aborted === true;
56
+ for (let attempt = 0;; attempt += 1) {
57
+ try {
58
+ return await send();
59
+ }
60
+ catch (error) {
61
+ if (!isStaleRelease(error))
62
+ throw error;
63
+ const left = deadline - Date.now();
64
+ if (left <= 0 || gone())
65
+ return switchingVersions(5);
66
+ const delay = EVE_HOST_RELEASE_RETRY_DELAYS_MS[attempt] ?? 4_000;
67
+ await new Promise((resolve) => setTimeout(resolve, Math.min(delay, left)));
68
+ // A timer can fire late; the deadline is checked again before the try.
69
+ if (Date.now() > deadline || gone())
70
+ return switchingVersions(5);
71
+ }
72
+ }
73
+ }
74
+ /** A retained version's object could not place this release; see `placeCaller`. */
75
+ const EVE_HOST_RETAINED_SWITCHING = new Error("A retained deployment is switching versions.");
76
+ const worldsOnThisRelease = new WeakSet();
77
+ /**
78
+ * One read-only request before a World call, bounded by `deadline`. A World
79
+ * object on the release before this one resets itself for it and the call
80
+ * throws. It proves nothing about routes: it only keeps a World call from
81
+ * being the request that meets the reset. The World object answers a GET
82
+ * with 405; only that answer is remembered.
83
+ */
84
+ async function nudgeWorld(env, world, deadline) {
85
+ if (worldsOnThisRelease.has(world))
86
+ return new Response(null, { status: 405 });
87
+ let timer;
88
+ try {
89
+ const response = await Promise.race([
90
+ world.get(world.idFromName("world")).fetch(new Request(new URL("/__eden/world/rpc", env.EVE_PUBLIC_ORIGIN), { headers: releaseHeaders(env) })),
91
+ new Promise((resolve) => {
92
+ timer = setTimeout(() => resolve(switchingVersions(5)), Math.max(0, deadline - Date.now()));
93
+ }),
94
+ ]);
95
+ if (response.status === 405)
96
+ worldsOnThisRelease.add(world);
97
+ return response;
98
+ }
99
+ finally {
100
+ clearTimeout(timer);
101
+ }
102
+ }
6
103
  function deploymentImages(env) {
7
104
  const raw = env.EDEN_EVE_DEPLOYMENTS;
8
105
  return typeof raw === "string" ? JSON.parse(raw) : {};
@@ -87,7 +184,8 @@ async function publish(from, to) {
87
184
  * versions before 0.83 pick microsandbox at runtime, although Eden's isolated
88
185
  * builder prepared just-bash. Hide it to keep those versions' provider
89
186
  * selection aligned. Eve 0.83 removed microsandbox and records its provider
90
- * during build; the device removal remains for older supported Eve releases.
187
+ * during build, so no supported Eve release needs the device removal; it stays
188
+ * as a precaution.
91
189
  */
92
190
  const EVE_HOST_ENTRYPOINT = Object.freeze([
93
191
  "/bin/sh",
@@ -245,6 +343,13 @@ export class EdenWorldDurableObject extends DurableObject {
245
343
  void ctx.blockConcurrencyWhile(async () => this.core.migrate());
246
344
  }
247
345
  async fetch(request) {
346
+ // Cloudflare can keep this object on an earlier release after a deploy,
347
+ // with that release's route table. It resets only for a container of
348
+ // the release that replaced its own; every other caller is served as
349
+ // before.
350
+ if (placeCaller(this.env, request.headers, request.headers.get("x-eden-eve-release-id") ?? undefined) === "newer") {
351
+ this.ctx.abort(EVE_HOST_STALE_WORKER_REASON);
352
+ }
248
353
  if (request.method !== "POST")
249
354
  return new Response(null, { status: 405 });
250
355
  const result = await this.core.handleRpc(new Uint8Array(await request.arrayBuffer()));
@@ -310,7 +415,27 @@ export async function routeEveOutboundRequest(request, env, sourceDeploymentId,
310
415
  const world = env.EDEN_WORLD;
311
416
  if (world === undefined)
312
417
  return new Response(null, { status: 404 });
313
- return world.get(world.idFromName("world")).fetch(request);
418
+ const deadline = Date.now() + EVE_HOST_RELEASE_WAIT_MS;
419
+ const nudge = await sendWhileSwitching(() => nudgeWorld(env, world, deadline), deadline);
420
+ if (nudge.status === 503)
421
+ return nudge;
422
+ await nudge.body?.cancel();
423
+ const headers = new Headers(request.headers);
424
+ headers.delete("x-eden-eve-release-id");
425
+ headers.delete("x-eden-eve-previous-release-id");
426
+ for (const [name, value] of Object.entries(releaseHeaders(env)))
427
+ headers.set(name, value);
428
+ try {
429
+ return await world.get(world.idFromName("world")).fetch(new Request(request, { headers }));
430
+ }
431
+ catch (error) {
432
+ if (!isStaleRelease(error))
433
+ throw error;
434
+ // The World object was reset while this call was in flight. Whether it
435
+ // ran is unknown, so it is not sent again.
436
+ worldsOnThisRelease.delete(world);
437
+ return switchingVersions(1);
438
+ }
314
439
  }
315
440
  if (env.EDEN_EVE_WORLD_CLOUDFLARE && isEveWorkflowInternalRoute(new URL(request.url).pathname)) {
316
441
  return new Response(null, { status: 403 });
@@ -376,6 +501,7 @@ export class EveHostDurableContainer extends DurableObject {
376
501
  activeDeliveries = 0;
377
502
  activeForwarding = 0;
378
503
  schedulerRoleReconcile;
504
+ imageReconcile;
379
505
  retainedFences;
380
506
  deliveryClaims = new Map();
381
507
  async identifyDeployment(requested) {
@@ -424,20 +550,13 @@ export class EveHostDurableContainer extends DurableObject {
424
550
  if (id === this.deploymentId)
425
551
  continue;
426
552
  const request = new Request(new URL("/__eden/internal/fence-schedules", this.env.EVE_PUBLIC_ORIGIN), {
427
- headers: {
428
- "x-eden-eve-deployment-id": id,
429
- "x-eden-eve-release-id": this.env.EDEN_EVE_RELEASE_ID ?? this.env.EDEN_EVE_DEPLOYMENT_ID,
430
- },
553
+ headers: { "x-eden-eve-deployment-id": id, ...releaseHeaders(this.env) },
431
554
  });
432
- let response;
433
- try {
434
- response = await namespace.getByName(deploymentInstance(this.env, id)).fetch(request);
435
- }
436
- catch (error) {
437
- if (!(error instanceof Error) || !error.message.includes(EVE_HOST_STALE_WORKER_REASON))
438
- throw error;
439
- response = await namespace.getByName(deploymentInstance(this.env, id)).fetch(request);
440
- }
555
+ // One try: a retained object still on the old release resets itself
556
+ // and this throws, and the Worker that owns the request waits.
557
+ const response = await namespace.getByName(deploymentInstance(this.env, id)).fetch(request);
558
+ if (response.status === 503)
559
+ throw EVE_HOST_RETAINED_SWITCHING;
441
560
  if (!response.ok)
442
561
  throw new Error(`Cannot fence schedules on retained deployment ${id}.`);
443
562
  await response.body?.cancel();
@@ -688,6 +807,10 @@ export class EveHostDurableContainer extends DurableObject {
688
807
  const worldToken = globalThis.crypto.randomUUID();
689
808
  await this.ctx.storage.put("eden.eve.host.world-token", worldToken);
690
809
  this.envVars.EDEN_WORLD_TOKEN = worldToken;
810
+ // Both start sources run `expectedImage` (a snapshot is only restored
811
+ // from that image). Recorded before the start, so a request that inspects
812
+ // the new container before it reports its image does not replace it.
813
+ await this.ctx.storage.put(EVE_HOST_CONTAINER_STARTED_IMAGE_KEY, expectedImage);
691
814
  runtime.start({
692
815
  ...(snapshotId === undefined
693
816
  ? { image: expectedImage }
@@ -710,9 +833,6 @@ export class EveHostDurableContainer extends DurableObject {
710
833
  // A fresh process receives the retained scheduler fence even when its
711
834
  // filesystem came from a snapshot of the formerly-current deployment.
712
835
  await this.ctx.storage.delete("eden.eve.host.scheduler-fence-pending");
713
- // Both start sources run `expectedImage` (a snapshot is only restored
714
- // from that image).
715
- await this.ctx.storage.put(EVE_HOST_CONTAINER_STARTED_IMAGE_KEY, expectedImage);
716
836
  // Intercepts do not survive a container start and late registration lags;
717
837
  // register inside the same block before readiness polling.
718
838
  await this.registerLoopbackIntercept(runtime);
@@ -727,6 +847,12 @@ export class EveHostDurableContainer extends DurableObject {
727
847
  * at start stands in for it.
728
848
  */
729
849
  async reconcileRunningImage() {
850
+ // One replacement at a time: a second caller that judged the old
851
+ // container would otherwise destroy the new one the first just started.
852
+ this.imageReconcile ??= this.replaceStaleImage().finally(() => { this.imageReconcile = undefined; });
853
+ await this.imageReconcile;
854
+ }
855
+ async replaceStaleImage() {
730
856
  if (this.imageVerified)
731
857
  return;
732
858
  const runtime = this.containerRuntime();
@@ -1015,8 +1141,22 @@ export class EveHostDurableContainer extends DurableObject {
1015
1141
  async fetch(request) {
1016
1142
  const requestDeploymentId = request.headers.get("x-eden-eve-deployment-id") ?? undefined;
1017
1143
  const requestedRelease = request.headers.get("x-eden-eve-release-id") ?? requestDeploymentId;
1018
- if (requestedRelease !== undefined && requestedRelease !== (this.env.EDEN_EVE_RELEASE_ID ?? this.env.EDEN_EVE_DEPLOYMENT_ID)) {
1144
+ const caller = placeCaller(this.env, request.headers, requestedRelease);
1145
+ if (caller === "newer")
1019
1146
  this.ctx.abort(EVE_HOST_STALE_WORKER_REASON);
1147
+ if (caller === "unknown")
1148
+ return switchingVersions(5);
1149
+ const olderCaller = caller === "older";
1150
+ // A Worker of the release this one replaced is still serving while
1151
+ // Cloudflare switches versions. It is served only as the deployment it
1152
+ // names: with affinity, one this release still routes (the same one after
1153
+ // a same-version update, the kept one after an Eve upgrade); without
1154
+ // affinity every release is its own deployment, so it never is. Otherwise
1155
+ // the client retries and reaches the new Worker.
1156
+ if (olderCaller && requestDeploymentId !== undefined && (this.env.EDEN_EVE_DEPLOYMENTS === undefined
1157
+ ? requestDeploymentId !== this.env.EDEN_EVE_DEPLOYMENT_ID
1158
+ : deploymentImages(this.env)[requestDeploymentId] === undefined)) {
1159
+ return switchingVersions(1);
1020
1160
  }
1021
1161
  await this.identifyDeployment(requestDeploymentId);
1022
1162
  if (new URL(request.url).pathname === "/__eden/internal/fence-schedules") {
@@ -1026,7 +1166,15 @@ export class EveHostDurableContainer extends DurableObject {
1026
1166
  }
1027
1167
  return new Response(null, { status: 204 });
1028
1168
  }
1029
- const response = await this.forwardToEve(request);
1169
+ let response;
1170
+ try {
1171
+ response = await this.forwardToEve(request);
1172
+ }
1173
+ catch (error) {
1174
+ if (error !== EVE_HOST_RETAINED_SWITCHING)
1175
+ throw error;
1176
+ return switchingVersions(5);
1177
+ }
1030
1178
  // Reporting the verified start back lets deploy health-gate promotion on
1031
1179
  // the served container actually running this generation's image: the
1032
1180
  // header echoes only when the running container's image equals this
@@ -1034,6 +1182,7 @@ export class EveHostDurableContainer extends DurableObject {
1034
1182
  // deployment.
1035
1183
  if (requestDeploymentId !== undefined &&
1036
1184
  requestDeploymentId === this.deploymentId &&
1185
+ !olderCaller &&
1037
1186
  this.readinessEvidence !== undefined &&
1038
1187
  this.imageVerified) {
1039
1188
  const headers = new Headers(response.headers);
@@ -1462,6 +1611,7 @@ export function createEveHostWorker(options) {
1462
1611
  publicOrigin: options.publicOrigin,
1463
1612
  deploymentId: options.deploymentId,
1464
1613
  releaseId: options.releaseId ?? options.deploymentId,
1614
+ ...(env.EDEN_EVE_PREVIOUS_RELEASE_ID === undefined ? {} : { previousReleaseId: env.EDEN_EVE_PREVIOUS_RELEASE_ID }),
1465
1615
  generationId: options.generationId,
1466
1616
  correlationId: globalThis.crypto.randomUUID(),
1467
1617
  ...(options.runtimeRevisionHandle === undefined
@@ -1469,31 +1619,31 @@ export function createEveHostWorker(options) {
1469
1619
  : { runtimeRevisionHandle: options.runtimeRevisionHandle }),
1470
1620
  };
1471
1621
  const forwarded = createTrustedEveRequest(request, metadata);
1472
- // Synchronize the DO version before sending an upload. Cloning a body
1473
- // would retain an unread tee of the entire upload in isolate memory.
1474
- const bodyless = forwarded.body === null
1622
+ // A read is its own version check. Anything else waits for a health read
1623
+ // first, so the object is on a release that serves this Worker before the
1624
+ // request is sent. Cloning a body would retain an unread tee of the entire
1625
+ // upload in isolate memory.
1626
+ const read = request.method === "GET" || request.method === "HEAD";
1627
+ const check = read
1475
1628
  ? forwarded
1476
1629
  : createTrustedEveRequest(new Request(new URL(EVE_HOST_DEFAULTS.healthPath, options.publicOrigin), { signal: forwarded.signal }), metadata);
1477
- let container = resolveContainer(env);
1478
- let response;
1630
+ let container;
1631
+ const response = await sendWhileSwitching(() => {
1632
+ container = resolveContainer(env);
1633
+ return container.fetch(check);
1634
+ }, Date.now() + EVE_HOST_RELEASE_WAIT_MS, request.signal);
1635
+ if (read || !response.ok)
1636
+ return response;
1637
+ await response.body?.cancel();
1479
1638
  try {
1480
- response = await container.fetch(bodyless);
1639
+ // Sent exactly once: a reset from here on does not say whether it ran.
1640
+ return await container.fetch(forwarded);
1481
1641
  }
1482
1642
  catch (error) {
1483
- if (!(error instanceof Error) ||
1484
- !error.message.includes(EVE_HOST_STALE_WORKER_REASON)) {
1643
+ if (!isStaleRelease(error))
1485
1644
  throw error;
1486
- }
1487
- // Only the pre-forwarding stale abort is safe to retry. A failed stub
1488
- // can remain broken, so resolve it again and retry the bodyless request once.
1489
- container = resolveContainer(env);
1490
- response = await container.fetch(bodyless);
1645
+ return switchingVersions(1);
1491
1646
  }
1492
- if (bodyless === forwarded)
1493
- return response;
1494
- await response.body?.cancel();
1495
- // Stream the original body exactly once, outside the retry path.
1496
- return container.fetch(forwarded);
1497
1647
  };
1498
1648
  return {
1499
1649
  async fetch(request, env) {
@@ -1548,6 +1698,11 @@ export function createEveHostWorker(options) {
1548
1698
  // Wake only: the in-process Nitro runner inside the container
1549
1699
  // fires the tick itself, so the schedule runs exactly once.
1550
1700
  ctx.waitUntil(forwardToContainer(new Request(new URL(EVE_HOST_DEFAULTS.healthPath, options.publicOrigin)), env)
1701
+ .then((response) => {
1702
+ if (!response.ok)
1703
+ console.error("The scheduled wake request failed:", `HTTP ${response.status}`);
1704
+ return response;
1705
+ })
1551
1706
  .catch((error) => {
1552
1707
  console.error("The scheduled wake request failed:", error instanceof Error ? error.message : "unknown");
1553
1708
  }));
@@ -117,6 +117,8 @@ export interface EveHostConfigRequest extends EveHostIdentity {
117
117
  readonly accountId?: string;
118
118
  readonly stableWorkersDevOrigin: string;
119
119
  readonly containerImage: string;
120
+ /** The release this publish replaces, as its Workers stamped their requests. */
121
+ readonly previousReleaseId?: string;
120
122
  readonly retainedDeployments?: Readonly<Record<string, EveRetainedDeployment>>;
121
123
  readonly containerImageBuildContext?: string;
122
124
  readonly runtimeVariableNames?: readonly string[];
@@ -169,6 +171,7 @@ export interface EveHostWranglerConfig {
169
171
  readonly EDEN_EVE_GENERATION_ID: string;
170
172
  readonly EDEN_EVE_DEPLOYMENTS?: string;
171
173
  readonly EDEN_EVE_RELEASE_ID: string;
174
+ readonly EDEN_EVE_PREVIOUS_RELEASE_ID?: string;
172
175
  readonly EVE_RUNTIME_VARIABLE_NAMES: readonly string[];
173
176
  readonly EDEN_EVE_RUNTIME_REVISION?: string;
174
177
  readonly EDEN_EVE_CONTAINER_SLEEP_AFTER?: string;
@@ -220,6 +223,8 @@ export declare function createEveHostConfig(request: EveHostConfigRequest): EveH
220
223
  export declare function generateEveHostWorkerSource(request: EveGeneratedWorkerSourceRequest): string;
221
224
  export interface EveHostForwardingMetadata {
222
225
  readonly releaseId?: string;
226
+ /** The release this Worker's release replaced. */
227
+ readonly previousReleaseId?: string;
223
228
  readonly publicOrigin: string;
224
229
  readonly deploymentId: string;
225
230
  readonly generationId: string;
@@ -235,6 +240,7 @@ export interface EveHostContainerEnvironment {
235
240
  readonly EDEN_EVE_DEPLOYMENT_ID?: string;
236
241
  readonly EDEN_EVE_GENERATION_ID?: string;
237
242
  readonly EDEN_EVE_RELEASE_ID?: string;
243
+ readonly EDEN_EVE_PREVIOUS_RELEASE_ID?: string;
238
244
  readonly EDEN_EVE_CONTROL_TOKEN?: string;
239
245
  readonly EDEN_EVE_DEPLOYMENTS?: string;
240
246
  readonly EDEN_EVE_RUNTIME_REVISION?: string;
package/dist/eve-host.js CHANGED
@@ -420,6 +420,7 @@ export function createEveHostConfig(request) {
420
420
  EVE_CONTAINER_INSTANCE_NAME: request.stableContainerInstanceName,
421
421
  EDEN_EVE_DEPLOYMENT_ID: request.deploymentId,
422
422
  EDEN_EVE_RELEASE_ID: request.releaseId ?? request.deploymentId,
423
+ ...(request.previousReleaseId === undefined ? {} : { EDEN_EVE_PREVIOUS_RELEASE_ID: request.previousReleaseId }),
423
424
  EDEN_EVE_GENERATION_ID: request.generationId,
424
425
  ...(request.retainedDeployments === undefined ? {} : { EDEN_EVE_DEPLOYMENTS: JSON.stringify(deployments) }),
425
426
  EVE_RUNTIME_VARIABLE_NAMES: request.runtimeVariableNames ?? [],
@@ -539,6 +540,9 @@ export function createTrustedEveRequest(request, metadata) {
539
540
  headers.set("x-eden-eve-public-origin", metadata.publicOrigin);
540
541
  headers.set("x-eden-eve-deployment-id", metadata.deploymentId);
541
542
  headers.set("x-eden-eve-release-id", metadata.releaseId ?? metadata.deploymentId);
543
+ if (metadata.previousReleaseId !== undefined) {
544
+ headers.set("x-eden-eve-previous-release-id", metadata.previousReleaseId);
545
+ }
542
546
  headers.set("x-eden-eve-generation-id", metadata.generationId);
543
547
  headers.set("x-eden-eve-correlation-id", metadata.correlationId);
544
548
  if (metadata.runtimeRevisionHandle !== undefined) {
@@ -588,6 +592,7 @@ const RESERVED_EVE_HOST_VARIABLES = new Set([
588
592
  "EDEN_EVE_GENERATION_ID",
589
593
  "EDEN_EVE_DEPLOYMENTS",
590
594
  "EDEN_EVE_RELEASE_ID",
595
+ "EDEN_EVE_PREVIOUS_RELEASE_ID",
591
596
  "EDEN_AFFINITY_CONTRACT",
592
597
  "EDEN_EVE_CONTROL_TOKEN",
593
598
  "EVE_SCHEDULES_DISABLED",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moinulmoin/eden-runtime-cloudflare",
3
- "version": "0.12.0",
3
+ "version": "0.14.0",
4
4
  "description": "Cloudflare Worker/Container host runtime for Eden's Eve deployments.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -20,7 +20,7 @@
20
20
  "NOTICE"
21
21
  ],
22
22
  "devDependencies": {
23
- "@moinulmoin/eden-world-cloudflare": "0.12.0",
23
+ "@moinulmoin/eden-world-cloudflare": "0.14.0",
24
24
  "@cloudflare/workers-types": "5.20261006.1"
25
25
  },
26
26
  "repository": {