@moinulmoin/eden-runtime-cloudflare 0.11.0 → 0.11.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.
@@ -22724,6 +22724,15 @@ var init_serialization = __esm({
22724
22724
  });
22725
22725
 
22726
22726
  // ../../node_modules/.pnpm/@workflow+world@5.1.0/node_modules/@workflow/world/dist/start-hook.js
22727
+ function startHookId(runId) {
22728
+ if (!runId.startsWith("wrun_") || runId.length <= "wrun_".length) {
22729
+ throw new Error(`Not a workflow run id: "${runId}"`);
22730
+ }
22731
+ return `hook_${runId.slice("wrun_".length)}`;
22732
+ }
22733
+ function isStartHookId(runId, hookId) {
22734
+ return runId.startsWith("wrun_") && hookId === startHookId(runId);
22735
+ }
22727
22736
  var StartHookSchema, START_HOOK_CANDIDATE_MAX_AGE_MS;
22728
22737
  var init_start_hook = __esm({
22729
22738
  "../../node_modules/.pnpm/@workflow+world@5.1.0/node_modules/@workflow/world/dist/start-hook.js"() {
@@ -24519,6 +24528,9 @@ var init_core3 = __esm({
24519
24528
  "CREATE TABLE IF NOT EXISTS workflow_hooks (hook_id TEXT PRIMARY KEY, run_id TEXT NOT NULL, token TEXT NOT NULL, data BLOB NOT NULL)",
24520
24529
  "CREATE INDEX IF NOT EXISTS workflow_hooks_run ON workflow_hooks(run_id)",
24521
24530
  "CREATE INDEX IF NOT EXISTS workflow_hooks_token ON workflow_hooks(token)",
24531
+ // Decisions outlive Hook disposal and token takeover; retries must never
24532
+ // turn a refused candidate into a second run.
24533
+ "CREATE TABLE IF NOT EXISTS workflow_start_hook_admissions (run_id TEXT PRIMARY KEY, token TEXT NOT NULL, conflicting_run_id TEXT, cancelled INTEGER NOT NULL DEFAULT 0, created_at INTEGER NOT NULL)",
24522
24534
  // Routing tombstones outlive invocation-data retention; never store payloads here.
24523
24535
  "CREATE TABLE IF NOT EXISTS workflow_hook_disposals (run_id TEXT NOT NULL, hook_id TEXT NOT NULL, token TEXT NOT NULL, claimed_run_id TEXT NOT NULL, claimed_hook_id TEXT NOT NULL, PRIMARY KEY(run_id,hook_id))",
24524
24536
  "CREATE TABLE IF NOT EXISTS workflow_waits (wait_id TEXT PRIMARY KEY, run_id TEXT NOT NULL, data BLOB NOT NULL)",
@@ -24820,6 +24832,47 @@ var init_core3 = __esm({
24820
24832
  }
24821
24833
  throw new HookNotFoundError(hookId);
24822
24834
  }
24835
+ rejectStartHook(reason) {
24836
+ throw new WorkflowWorldError(`Start Hook admission rejected: ${reason}`, { status: 400, code: "START_HOOK_ADMISSION_REJECTED", reason });
24837
+ }
24838
+ admitStartHook(id, request) {
24839
+ if (request.eventType !== "run_created" && request.eventType !== "run_started")
24840
+ return { request };
24841
+ const input2 = request.eventData;
24842
+ const recorded = this.rows("SELECT * FROM workflow_start_hook_admissions WHERE run_id=?", id)[0];
24843
+ if (recorded?.cancelled)
24844
+ this.rejectStartHook("expired");
24845
+ if (input2?.startHook === void 0) {
24846
+ if (recorded && !this.rows("SELECT id FROM workflow_runs WHERE id=?", id).length)
24847
+ return { request, error: new HookConflictError(String(recorded.token), String(recorded.conflicting_run_id)) };
24848
+ return { request };
24849
+ }
24850
+ const parsed = StartHookSchema.safeParse(input2.startHook);
24851
+ if (!parsed.success)
24852
+ this.rejectStartHook("invalid");
24853
+ const startHook = { ...parsed.data, hookId: startHookId(id) };
24854
+ if (parsed.data.hookId && parsed.data.hookId !== startHook.hookId)
24855
+ this.rejectStartHook("invalid");
24856
+ if (startHook.tokenRetentionUntil && startHook.tokenRetentionUntil.getTime() > this.now() + 30 * 864e5)
24857
+ this.rejectStartHook("invalid");
24858
+ const candidateTime = ulidToDate(id.replace(/^wrun_/, ""))?.getTime();
24859
+ if (candidateTime === void 0 || this.now() - candidateTime > START_HOOK_CANDIDATE_MAX_AGE_MS && (request.eventType === "run_created" || !this.rows("SELECT id FROM workflow_runs WHERE id=?", id).length))
24860
+ this.rejectStartHook("expired");
24861
+ if (recorded) {
24862
+ if (recorded.token !== startHook.token)
24863
+ this.rejectStartHook("token_mismatch");
24864
+ if (recorded.conflicting_run_id)
24865
+ return { request, error: new HookConflictError(startHook.token, String(recorded.conflicting_run_id)) };
24866
+ } else {
24867
+ if (this.rows("SELECT id FROM workflow_runs WHERE id=?", id).length)
24868
+ this.rejectStartHook("ordinary_run");
24869
+ const owner = this.findHook(startHook.token, true);
24870
+ this.sql.exec("INSERT INTO workflow_start_hook_admissions(run_id,token,conflicting_run_id,created_at) VALUES(?,?,?,?)", id, startHook.token, owner?.runId ?? null, this.now());
24871
+ if (owner)
24872
+ return { request, error: new HookConflictError(startHook.token, owner.runId) };
24873
+ }
24874
+ return { request: { ...request, eventData: { ...input2, startHook } } };
24875
+ }
24823
24876
  createRun(runId, data) {
24824
24877
  if ((data.specVersion ?? SPEC_VERSION_CURRENT) > SPEC_VERSION_CURRENT)
24825
24878
  throw new RunNotSupportedError(data.specVersion, SPEC_VERSION_CURRENT);
@@ -24831,6 +24884,8 @@ var init_core3 = __esm({
24831
24884
  const now = new Date(this.now());
24832
24885
  const run = WorkflowRunSchema.parse(compact({ runId, ...d, status: "pending", attributes: d.attributes ?? {}, createdAt: now, updatedAt: now, specVersion: data.specVersion ?? SPEC_VERSION_CURRENT }));
24833
24886
  this.saveRun(run);
24887
+ if (d.startHook)
24888
+ this.saveHook(HookSchema.parse(compact({ ...d.startHook, runId, hookId: startHookId(runId), ownerId: "", projectId: "", environment: "", createdAt: now, specVersion: run.specVersion, isWebhook: false, isSystem: false })));
24834
24889
  this.sql.exec("INSERT INTO workflow_event_slots(run_id) VALUES(?)", runId);
24835
24890
  const event = this.insertEvent(runId, data);
24836
24891
  return { run, event };
@@ -24839,10 +24894,14 @@ var init_core3 = __esm({
24839
24894
  const id = runId || (request.eventType === "run_created" ? `wrun_${ulid3(this.now())}` : "");
24840
24895
  if (!id)
24841
24896
  throw new Error("runId is required for non-run_created events");
24897
+ const hasStartHook = (request.eventType === "run_created" || request.eventType === "run_started") && request.eventData?.startHook !== void 0;
24842
24898
  if (request.eventType === "run_created" && runId) {
24843
24899
  const error61 = validateUlidTimestamp(id, "wrun_");
24844
- if (error61)
24900
+ if (error61) {
24901
+ if (hasStartHook)
24902
+ this.rejectStartHook("expired");
24845
24903
  throw new WorkflowWorldError(error61);
24904
+ }
24846
24905
  }
24847
24906
  let digest;
24848
24907
  if (request.eventType === "hook_received" && params?.resumeId) {
@@ -24852,7 +24911,21 @@ var init_core3 = __esm({
24852
24911
  if (params.resumePayloadDigest && params.resumePayloadDigest !== digest)
24853
24912
  throw new WorkflowWorldError("Hook resume digest does not match its payload", { status: 422 });
24854
24913
  }
24855
- return this.atomic(() => this.createSync(id, request, params, digest));
24914
+ const result = this.atomic(() => {
24915
+ const admission = this.admitStartHook(id, request);
24916
+ if (admission.error)
24917
+ return admission.error;
24918
+ if (request.eventType === "run_cancelled" && !this.rows("SELECT id FROM workflow_runs WHERE id=?", id).length && this.rows("SELECT run_id FROM workflow_start_hook_admissions WHERE run_id=?", id).length) {
24919
+ this.sql.exec("UPDATE workflow_start_hook_admissions SET cancelled=1 WHERE run_id=?", id);
24920
+ return new WorkflowWorldError("Start Hook candidate cancelled", { status: 409, code: "START_HOOK_CANDIDATE_CANCELLED" });
24921
+ }
24922
+ if (hasStartHook && request.eventType === "run_created" && this.rows("SELECT id FROM workflow_runs WHERE id=?", id).length)
24923
+ return this.eventResult(id, { run: this.run(id) }, params);
24924
+ return this.createSync(id, admission.request, params, digest);
24925
+ });
24926
+ if (result instanceof Error)
24927
+ throw result;
24928
+ return result;
24856
24929
  }
24857
24930
  createSync(id, request, params, digest) {
24858
24931
  const now = new Date(this.now());
@@ -24866,6 +24939,8 @@ var init_core3 = __esm({
24866
24939
  let stepCreated = false;
24867
24940
  const type = data.eventType;
24868
24941
  const correlationId = data.correlationId;
24942
+ if (type === "hook_created" && correlationId && isStartHookId(id, correlationId))
24943
+ throw new WorkflowWorldError("The start Hook id is reserved for atomic admission", { status: 400 });
24869
24944
  if (type === "run_created") {
24870
24945
  ({ run, event } = this.createRun(id, data));
24871
24946
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moinulmoin/eden-runtime-cloudflare",
3
- "version": "0.11.0",
3
+ "version": "0.11.1",
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.11.0",
23
+ "@moinulmoin/eden-world-cloudflare": "0.11.1",
24
24
  "@cloudflare/workers-types": "5.20261006.1"
25
25
  },
26
26
  "repository": {