The best way to control Claude Code, Codex and more from any computer or phone

main
ts 1,696 lines 63.6 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { WorkflowResource } from "@alchemy.run/cloudflare/Workflows/Workflow";
4 import { generateWorkflowName } from "@alchemy.run/cloudflare/Workflows/WorkflowName";
5 import { expect } from "@alchemy.run/test";
6 import * as Test from "@alchemy.run/test/Alchemy";
7 import * as TestCore from "@alchemy.run/test/Core";
8 import * as queues from "@distilled.cloud/cloudflare/queues";
9 import * as workers from "@distilled.cloud/cloudflare/workers";
10 import * as workflows from "@distilled.cloud/cloudflare/workflows";
11 import { adopt } from "alchemy/AdoptPolicy";
12 import * as Output from "alchemy/Output";
13 import { sha256 } from "alchemy/Util/sha256";
14 import * as Cause from "effect/Cause";
15 import * as Clock from "effect/Clock";
16 import * as Data from "effect/Data";
17 import * as Effect from "effect/Effect";
18 import * as Exit from "effect/Exit";
19 import * as HttpClient from "effect/http/HttpClient";
20 import * as HttpClientRequest from "effect/http/HttpClientRequest";
21 import { MinimumLogLevel } from "effect/References";
22 import * as Schedule from "effect/Schedule";
23 import * as Schema from "effect/Schema";
24 import * as Stream from "effect/Stream";
25 import { requestWorker } from "../Utils/WorkerRequest.ts";
26 import ColdEffectWorker, {
27 COLD_EFFECT_WORKFLOW_NAME,
28 } from "./fixtures/workflow-async/effect-worker.ts";
29 import { AsyncWorkflowWorker } from "./fixtures/workflow-async/stack.ts";
30
31 const { test } = Test.make({ providers: Cloudflare.providers() });
32
33 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
34
35 const retry = Effect.retry({
36 schedule: Schedule.spaced("3 seconds"),
37 times: 10,
38 });
39 const hostMain = `${import.meta.dirname}/fixtures/workflow-async/worker.ts`;
40 const consumerMain = `${import.meta.dirname}/fixtures/workflow-async/consumer.ts`;
41
42 const WorkflowStatus = Schema.Struct({
43 status: Schema.String,
44 output: Schema.optional(
45 Schema.NullOr(
46 Schema.Struct({
47 greeting: Schema.String,
48 workflowName: Schema.optional(Schema.String),
49 }),
50 ),
51 ),
52 error: Schema.optional(Schema.NullOr(Schema.Struct({ message: Schema.optional(Schema.String) }))),
53 });
54
55 const WorkflowEvents = Schema.Array(
56 Schema.Struct({
57 type: Schema.String,
58 source: Schema.Struct({ type: Schema.String, workflowName: Schema.String }),
59 payload: Schema.Struct({ instanceId: Schema.String }),
60 }),
61 );
62
63 const EnvResponse = Schema.Struct({
64 greeting: Schema.String,
65 config: Schema.String,
66 effect: Schema.String,
67 output: Schema.String,
68 asset: Schema.String,
69 });
70
71 const getJson = <A>(url: string, schema: Schema.Decoder<A>, retries = 10) =>
72 Effect.gen(function* () {
73 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
74 return yield* client.get(url).pipe(
75 Effect.flatMap((res) => res.json),
76 Effect.flatMap(Schema.decodeUnknownEffect(schema)),
77 Effect.retry({ schedule: Schedule.spaced("3 seconds"), times: retries }),
78 );
79 });
80
81 /**
82 * Right after a deploy, the Workflows engine can run a new instance against a
83 * script version it has not loaded yet. The instance errors with one of these
84 * messages, and a later instance succeeds.
85 */
86 class WorkflowEngineNotReady extends Data.TaggedError("WorkflowEngineNotReady")<{
87 readonly message: string;
88 }> {}
89
90 const isEngineNotReady = (message: string | undefined) =>
91 message === "Worker not found." ||
92 (message?.startsWith("The entrypoint name ") === true &&
93 message.includes(" was not found in this worker."));
94
95 const runWorkflowToCompletion = (url: string, expectedWorkflowName?: string) =>
96 runWorkflowOnce(url, expectedWorkflowName).pipe(
97 Effect.retry({
98 while: (error) => error instanceof WorkflowEngineNotReady,
99 schedule: Schedule.spaced("5 seconds"),
100 times: 6,
101 }),
102 );
103
104 const runWorkflowOnce = (url: string, expectedWorkflowName?: string) =>
105 Effect.gen(function* () {
106 const http = yield* HttpClient.HttpClient;
107 const client = HttpClient.filterStatusOk(http);
108 if (expectedWorkflowName !== undefined) {
109 const identity = yield* getJson(
110 `${url}/workflow/identity`,
111 Schema.Struct({ workflowName: Schema.optional(Schema.String) }),
112 ).pipe(
113 Effect.tap((identity) =>
114 Effect.logInfo("Workflow identity readiness", {
115 expected: expectedWorkflowName,
116 observed: identity.workflowName,
117 }),
118 ),
119 Effect.repeat({
120 schedule: Schedule.spaced("2 seconds"),
121 until: (identity) => identity.workflowName === expectedWorkflowName,
122 times: 10,
123 }),
124 Effect.timeout("30 seconds"),
125 );
126 expect(identity.workflowName).toBe(expectedWorkflowName);
127 // A ready `/workflow/identity` proves only one host has the new Worker:
128 // this request can still land on one serving the workers.dev
129 // placeholder 404, or get a 502 from the edge (with no Worker
130 // invocation) while the new version rolls out.
131 const rejected = yield* requestWorker(
132 HttpClientRequest.post(`${url}/workflow/start/world`).pipe(
133 HttpClientRequest.setHeader("x-expected-workflow-name", `${expectedWorkflowName}-stale`),
134 ),
135 ).pipe(
136 Effect.tap((response) => response.text),
137 Effect.repeat({
138 schedule: Schedule.spaced("2 seconds"),
139 until: (response) => response.status < 500,
140 times: 10,
141 }),
142 );
143 expect(rejected.status).toBe(409);
144 }
145 const { instanceId } = yield* client
146 .post(
147 `${url}/workflow/start/world`,
148 expectedWorkflowName === undefined
149 ? undefined
150 : { headers: { "x-expected-workflow-name": expectedWorkflowName } },
151 )
152 .pipe(
153 Effect.flatMap((res) => res.json),
154 Effect.flatMap(Schema.decodeUnknownEffect(Schema.Struct({ instanceId: Schema.String }))),
155 retry,
156 );
157
158 // A new instance can answer `instance.not_found` (a 500 here) for over
159 // 30 seconds after `create` returns its id.
160 const status = yield* getJson(`${url}/workflow/status/${instanceId}`, WorkflowStatus, 20).pipe(
161 Effect.repeat({
162 schedule: Schedule.spaced("3 seconds"),
163 until: (s) => s.status === "complete" || s.status === "errored",
164 times: 20,
165 }),
166 Effect.timeout("90 seconds"),
167 );
168 if (status.status === "errored" && isEngineNotReady(status.error?.message)) {
169 return yield* new WorkflowEngineNotReady({ message: status.error!.message! });
170 }
171 if (status.status !== "complete") {
172 return yield* Effect.fail(
173 new Error(`workflow ${status.status}: ${JSON.stringify(status.error)}`),
174 );
175 }
176 expect(status.error).toBeFalsy();
177 expect(status.output?.greeting).toBe("Hello, world!");
178 return { ...status, instanceId };
179 });
180
181 const readWorkflowBinding = (scriptName: string) =>
182 Effect.gen(function* () {
183 const { accountId } = yield* yield* CloudflareEnvironment;
184 const settings = yield* workers.getScriptScriptAndVersionSetting({
185 accountId,
186 scriptName,
187 });
188 const bindings = (settings.bindings ?? []).filter((binding) => binding.type === "workflow");
189 expect(bindings).toHaveLength(1);
190 return bindings[0]!;
191 });
192
193 const expectWorkerGone = (accountId: string, scriptName: string) =>
194 workers.getScriptScriptAndVersionSetting({ accountId, scriptName }).pipe(
195 Effect.as(false),
196 Effect.catchTag("WorkerNotFound", () => Effect.succeed(true)),
197 Effect.catchTag("WorkerHasNoVersions", () => Effect.succeed(false)),
198 Effect.repeat({
199 schedule: Schedule.spaced("2 seconds"),
200 until: Boolean,
201 times: 10,
202 }),
203 Effect.tap((gone) => Effect.sync(() => expect(gone).toBe(true))),
204 );
205
206 const expectWorkflowGone = (accountId: string, workflowName: string) =>
207 workflows.getWorkflow({ accountId, workflowName }).pipe(
208 Effect.as(false),
209 Effect.catchTag("WorkflowNotFound", () => Effect.succeed(true)),
210 Effect.repeat({
211 schedule: Schedule.spaced("2 seconds"),
212 until: Boolean,
213 times: 10,
214 }),
215 Effect.tap((gone) => Effect.sync(() => expect(gone).toBe(true))),
216 );
217
218 const bindingSnapshot = (binding: Cloudflare.Workflows.WorkflowBinding) => ({
219 kind: binding.kind,
220 name: binding.name,
221 className: binding.className,
222 workflowName: binding.workflowName,
223 scriptName: binding.scriptName,
224 workflowIsOutput: Output.isOutput(binding.workflowName),
225 scriptIsOutput: Output.isOutput(binding.scriptName),
226 });
227
228 test.provider(
229 "async worker workflow binding is a direct first-deployment queue subscription source",
230 (stack) =>
231 Effect.gen(function* () {
232 yield* stack.destroy();
233 const { accountId } = yield* yield* CloudflareEnvironment;
234 const program = Effect.gen(function* () {
235 const worker = yield* AsyncWorkflowWorker;
236 const queue = yield* Cloudflare.Queues.Queue("WorkflowEventsQueue");
237 const subscription = yield* Cloudflare.Queues.Subscription("WorkflowEvents", {
238 source: worker.env.MY_WORKFLOW,
239 events: ["instance.completed", "instance.errored"],
240 queueId: queue.queueId,
241 });
242 expect(subscription.Props.source).toEqual({
243 type: "workflows.workflow",
244 workflowName: worker.env.MY_WORKFLOW.workflowName,
245 });
246 yield* Cloudflare.Queues.Consumer("WorkflowEventsConsumer", {
247 queueId: queue.queueId,
248 scriptName: worker.workerName,
249 settings: { batchSize: 1, maxWaitTimeMs: 1000 },
250 });
251 return {
252 worker,
253 queue,
254 subscription,
255 binding: bindingSnapshot(worker.env.MY_WORKFLOW),
256 env: {
257 greeting: worker.env.GREETING,
258 config: worker.env.CONFIG,
259 effect: worker.env.EFFECT,
260 output: worker.env.OUTPUT,
261 outputIsOutput: Output.isOutput(worker.env.OUTPUT),
262 assets: worker.env.ASSETS,
263 },
264 };
265 });
266 const deployed = yield* stack.deploy(program);
267 const plan = yield* stack.plan(program);
268 expect(plan.resources.Greeting.downstream).toContain("WorkflowEvents");
269 expect(
270 Object.values(plan.resources).filter(
271 (node) => node.resource.Type === "Cloudflare.Workflow",
272 ),
273 ).toHaveLength(1);
274
275 const { binding, worker, queue, subscription } = deployed;
276 expect(binding.workflowIsOutput).toBe(true);
277 expect(binding.scriptIsOutput).toBe(true);
278 expect(binding.kind).toBe("Cloudflare.Workflow");
279 expect(binding.name).toBe("Greeting");
280 expect(binding.className).toBe("MyWorkflow");
281 expect(binding.scriptName).toBe(worker.workerName);
282 expect(deployed.env).toEqual({
283 greeting: "hello",
284 config: "configured",
285 effect: "effect",
286 output: "output",
287 outputIsOutput: true,
288 assets: { kind: "Cloudflare.Workers.Assets" },
289 });
290
291 const uploaded = yield* readWorkflowBinding(worker.workerName);
292 expect(uploaded.workflowName).toBe(binding.workflowName);
293 expect(uploaded.className).toBe(binding.className);
294 const observed = yield* workflows.getWorkflow({
295 accountId,
296 workflowName: binding.workflowName,
297 });
298 expect(observed.name).toBe(binding.workflowName);
299 expect(observed.scriptName).toBe(binding.scriptName);
300 expect(observed.className).toBe(binding.className);
301 const source = {
302 type: "workflows.workflow",
303 workflowName: binding.workflowName,
304 };
305 expect(subscription.source).toEqual(source);
306 expect(plan.resources.WorkflowEvents.state).toHaveProperty("props.source", source);
307 const liveSubscription = yield* queues.getSubscription({
308 accountId,
309 subscriptionId: subscription.subscriptionId,
310 });
311 expect(liveSubscription.source).toEqual(expect.objectContaining(source));
312 expect(liveSubscription.destination.queueId).toBe(queue.queueId);
313
314 expect(yield* getJson(`${worker.url}/env`, EnvResponse)).toEqual({
315 greeting: "hello",
316 config: "configured",
317 effect: "effect",
318 output: "output",
319 asset: "workflow asset\n",
320 });
321 const { terminal, events } = yield* runUntilWorkflowEvent(
322 `${worker.url}/events`,
323 runWorkflowToCompletion(worker.url!),
324 );
325 expectWorkflowEvent(events, terminal.instanceId, source);
326 yield* Effect.logInfo(`Workflow queue lifecycle delivery: ${JSON.stringify(events)}`);
327
328 yield* stack.destroy();
329 yield* expectWorkerGone(accountId, worker.workerName);
330 yield* expectWorkflowGone(accountId, binding.workflowName);
331 const queueGone = yield* queues.getQueue({ accountId, queueId: queue.queueId }).pipe(
332 Effect.as(false),
333 Effect.catchTag("QueueNotFound", () => Effect.succeed(true)),
334 );
335 expect(queueGone).toBe(true);
336 const subscriptionGone = yield* queues
337 .getSubscription({
338 accountId,
339 subscriptionId: subscription.subscriptionId,
340 })
341 .pipe(
342 Effect.as(false),
343 Effect.catchTag("SubscriptionNotFound", () => Effect.succeed(true)),
344 );
345 expect(subscriptionGone).toBe(true);
346 }).pipe(logLevel),
347 {
348 tags: [
349 "provider:cloudflare",
350 "provider:cloudflare:queue",
351 "provider:cloudflare:worker",
352 "provider:cloudflare:workflow",
353 "live",
354 ],
355 timeout: 300_000,
356 },
357 );
358
359 const hasEvent = (events: typeof WorkflowEvents.Type, instanceId: string) =>
360 events.some((event) => event.payload.instanceId === instanceId);
361
362 // The events Durable Object can answer "internal error" for over 30 seconds
363 // after a deploy, so a failed read counts as no events yet.
364 const pollWorkflowEvents = (eventsUrl: string, instanceId: string) =>
365 getJson(eventsUrl, WorkflowEvents, 0).pipe(
366 Effect.tap((events) => Effect.logInfo("Workflow event poll", { instanceId, events })),
367 Effect.catch((error) =>
368 Effect.logWarning("Workflow event poll failed", { instanceId, error: String(error) }).pipe(
369 Effect.as([] as typeof WorkflowEvents.Type),
370 ),
371 ),
372 Effect.repeat({
373 schedule: Schedule.spaced("5 seconds"),
374 until: (events) => hasEvent(events, instanceId),
375 times: 18,
376 }),
377 Effect.timeoutOrElse({ duration: "90 seconds", orElse: () => Effect.succeed([]) }),
378 );
379
380 /**
381 * Run a Workflow and wait for its `instance.completed` event to reach the
382 * subscribed Queue's consumer. Delivery has taken over a minute, and an
383 * instance that completes right after its subscription is created can produce
384 * no event at all, so a missing event gets one more run.
385 */
386 const runUntilWorkflowEvent = <T extends { readonly instanceId: string }, E, R>(
387 eventsUrl: string,
388 run: Effect.Effect<T, E, R>,
389 ) =>
390 Effect.gen(function* () {
391 let terminal = yield* run;
392 let events = yield* pollWorkflowEvents(eventsUrl, terminal.instanceId);
393 if (!hasEvent(events, terminal.instanceId)) {
394 yield* Effect.logWarning("Workflow event not delivered; running the Workflow again", {
395 instanceId: terminal.instanceId,
396 });
397 terminal = yield* run;
398 events = yield* pollWorkflowEvents(eventsUrl, terminal.instanceId);
399 }
400 return { terminal, events };
401 });
402
403 const expectWorkflowEvent = (
404 events: typeof WorkflowEvents.Type,
405 instanceId: string,
406 source: { type: string; workflowName: string },
407 ) =>
408 expect(events).toContainEqual(
409 expect.objectContaining({
410 type: "cf.workflows.workflow.instance.completed",
411 source: expect.objectContaining(source),
412 payload: expect.objectContaining({ instanceId }),
413 }),
414 );
415
416 const expectWorkflowSubscriptionGone = (
417 accountId: string,
418 subscriptionId: string,
419 queueId: string,
420 ) =>
421 Effect.gen(function* () {
422 expect(
423 yield* queues.getSubscription({ accountId, subscriptionId }).pipe(
424 Effect.as(false),
425 Effect.catchTag("SubscriptionNotFound", () => Effect.succeed(true)),
426 ),
427 ).toBe(true);
428 expect(
429 yield* queues.getQueue({ accountId, queueId }).pipe(
430 Effect.as(false),
431 Effect.catchTag("QueueNotFound", () => Effect.succeed(true)),
432 ),
433 ).toBe(true);
434 });
435
436 test.provider(
437 "Workflow.ref same-stack and WorkflowResource sources deliver queue events without duplicate ownership",
438 (stack) =>
439 Effect.gen(function* () {
440 yield* stack.destroy();
441 const { accountId } = yield* yield* CloudflareEnvironment;
442 const program = (reference: boolean) =>
443 Effect.gen(function* () {
444 const worker = yield* AsyncWorkflowWorker;
445 const workflow = reference
446 ? yield* Cloudflare.Workflow.ref("Greeting")
447 : yield* WorkflowResource("Greeting", {
448 className: "MyWorkflow",
449 scriptName: worker.workerName,
450 });
451 expect(workflow.Type).toBe("Cloudflare.Workflow");
452 expect(Output.isOutput(workflow.workflowName)).toBe(true);
453 if (reference) {
454 expect(typeof workflow).toBe("function");
455 expect(Output.isOutput(workflow)).toBe(true);
456 }
457 const queue = yield* Cloudflare.Queues.Queue("RefEventsQueue");
458 const subscription = yield* Cloudflare.Queues.Subscription("RefEvents", {
459 source: workflow,
460 events: ["instance.completed"],
461 queueId: queue.queueId,
462 });
463 expect(subscription.Props.source).toEqual({
464 type: "workflows.workflow",
465 workflowName: expect.anything(),
466 });
467 yield* Cloudflare.Queues.Consumer("RefEventsConsumer", {
468 queueId: queue.queueId,
469 scriptName: worker.workerName,
470 settings: { batchSize: 1, maxWaitTimeMs: 1000 },
471 });
472 return {
473 worker,
474 queue,
475 subscription,
476 workflowName: workflow.workflowName,
477 };
478 });
479 const firstPlan = yield* stack.plan(program(false));
480 expect(firstPlan.resources.Greeting.downstream).toContain("RefEvents");
481 const first = yield* stack.deploy(program(false));
482 const original = yield* workflows.getWorkflow({
483 accountId,
484 workflowName: first.workflowName,
485 });
486 // Resource refs read persisted state, so deploy the host before using one.
487 const deployed = yield* stack.deploy(program(true));
488 const plan = yield* stack.plan(program(true));
489 expect(
490 Object.values(plan.resources).filter(
491 (node) => node.resource.Type === "Cloudflare.Workflow",
492 ),
493 ).toHaveLength(1);
494 expect(deployed.workflowName).toBe(first.workflowName);
495 expect(deployed.subscription.subscriptionId).toBe(first.subscription.subscriptionId);
496 const source = {
497 type: "workflows.workflow",
498 workflowName: first.workflowName,
499 };
500 expect(plan.resources.RefEvents.state).toHaveProperty("props.source", source);
501 const observed = yield* queues.getSubscription({
502 accountId,
503 subscriptionId: deployed.subscription.subscriptionId,
504 });
505 expect(observed.source).toEqual(expect.objectContaining(source));
506 expect(observed.destination.queueId).toBe(deployed.queue.queueId);
507 const { terminal, events } = yield* runUntilWorkflowEvent(
508 `${deployed.worker.url}/events`,
509 runWorkflowToCompletion(deployed.worker.url!),
510 );
511 expectWorkflowEvent(events, terminal.instanceId, {
512 type: "workflows.workflow",
513 workflowName: first.workflowName,
514 });
515
516 yield* stack.deploy(AsyncWorkflowWorker);
517 yield* expectWorkflowSubscriptionGone(
518 accountId,
519 deployed.subscription.subscriptionId,
520 deployed.queue.queueId,
521 );
522 expect(
523 (yield* workflows.getWorkflow({
524 accountId,
525 workflowName: first.workflowName,
526 })).id,
527 ).toBe(original.id);
528 expect(yield* readWorkflowName(deployed.worker.workerName)).toBe(first.workflowName);
529 yield* stack.destroy();
530 yield* expectWorkerGone(accountId, deployed.worker.workerName);
531 yield* expectWorkflowGone(accountId, first.workflowName);
532 }).pipe(logLevel),
533 {
534 tags: [
535 "provider:cloudflare",
536 "provider:cloudflare:queue",
537 "provider:cloudflare:worker",
538 "provider:cloudflare:workflow",
539 "live",
540 ],
541 timeout: 300_000,
542 },
543 );
544
545 test.provider(
546 "Workflow.ref cross-stack and cross-stage source delivers queue events without owning the host",
547 (stack) => {
548 const host = TestCore.scratchStack(
549 { providers: Cloudflare.providers(), stage: `${stack.stage}-host` },
550 "WorkflowRefHost",
551 "test/Cloudflare/Workers/WorkflowAsync.test.ts",
552 );
553 return Effect.gen(function* () {
554 yield* stack.destroy();
555 yield* host.destroy();
556 const { accountId } = yield* yield* CloudflareEnvironment;
557 const hosted = yield* host.deploy(
558 Effect.gen(function* () {
559 const worker = yield* AsyncWorkflowWorker;
560 return { worker, workflowName: worker.env.MY_WORKFLOW.workflowName };
561 }),
562 );
563 const original = yield* workflows.getWorkflow({
564 accountId,
565 workflowName: hosted.workflowName,
566 });
567 const program = Effect.gen(function* () {
568 const worker = yield* Cloudflare.Worker("RefEventsWorker", {
569 main: hostMain,
570 env: { EVENTS: Cloudflare.DurableObject("WorkflowEvents") },
571 });
572 const queue = yield* Cloudflare.Queues.Queue("RefEventsQueue");
573 const Subscription = yield* Cloudflare.Queues.Subscription;
574 const subscription = yield* Subscription(
575 "RefEvents",
576 Effect.gen(function* () {
577 return {
578 source: yield* Cloudflare.Workflow.ref("Greeting", {
579 stack: host.name,
580 stage: host.stage,
581 }),
582 events: ["instance.completed"],
583 queueId: queue.queueId,
584 };
585 }),
586 );
587 yield* Cloudflare.Queues.Consumer("RefEventsConsumer", {
588 queueId: queue.queueId,
589 scriptName: worker.workerName,
590 settings: { batchSize: 1, maxWaitTimeMs: 1000 },
591 });
592 return { worker, queue, subscription };
593 });
594 const deployed = yield* stack.deploy(program);
595 const plan = yield* stack.plan(program);
596 expect(
597 Object.values(plan.resources).filter(
598 (node) => node.resource.Type === "Cloudflare.Workflow",
599 ),
600 ).toHaveLength(0);
601 const source = {
602 type: "workflows.workflow",
603 workflowName: hosted.workflowName,
604 };
605 expect(plan.resources.RefEvents.state).toHaveProperty("props.source", source);
606 expect(deployed.subscription.source).toEqual(source);
607 const observed = yield* queues.getSubscription({
608 accountId,
609 subscriptionId: deployed.subscription.subscriptionId,
610 });
611 expect(observed.source).toEqual(expect.objectContaining(source));
612 expect(observed.destination.queueId).toBe(deployed.queue.queueId);
613 const { terminal, events } = yield* runUntilWorkflowEvent(
614 `${deployed.worker.url}/events`,
615 runWorkflowToCompletion(hosted.worker.url!),
616 );
617 expectWorkflowEvent(events, terminal.instanceId, {
618 type: "workflows.workflow",
619 workflowName: hosted.workflowName,
620 });
621
622 yield* stack.destroy();
623 yield* expectWorkerGone(accountId, deployed.worker.workerName);
624 yield* expectWorkflowSubscriptionGone(
625 accountId,
626 deployed.subscription.subscriptionId,
627 deployed.queue.queueId,
628 );
629 expect(
630 (yield* workflows.getWorkflow({
631 accountId,
632 workflowName: hosted.workflowName,
633 })).id,
634 ).toBe(original.id);
635 expect(yield* readWorkflowName(hosted.worker.workerName)).toBe(hosted.workflowName);
636 yield* host.destroy();
637 yield* expectWorkerGone(accountId, hosted.worker.workerName);
638 yield* expectWorkflowGone(accountId, hosted.workflowName);
639 }).pipe(
640 logLevel,
641 Effect.ensuring(stack.destroy().pipe(Effect.andThen(host.destroy()), Effect.ignore)),
642 );
643 },
644 {
645 tags: [
646 "provider:cloudflare",
647 "provider:cloudflare:queue",
648 "provider:cloudflare:worker",
649 "provider:cloudflare:workflow",
650 "live",
651 ],
652 timeout: 300_000,
653 },
654 );
655
656 test.provider(
657 "async worker workflow binding accepts scriptName and exposes cross-script identity without duplicate resources",
658 (stack) =>
659 Effect.gen(function* () {
660 yield* stack.destroy();
661 const { accountId } = yield* yield* CloudflareEnvironment;
662 const deploy = (withConsumer: boolean) =>
663 stack.deploy(
664 Effect.gen(function* () {
665 const host = yield* AsyncWorkflowWorker;
666 const consumer = withConsumer
667 ? yield* Cloudflare.Worker(
668 "WorkflowConsumer",
669 Effect.succeed({
670 main: consumerMain,
671 env: {
672 MY_WORKFLOW: Cloudflare.Workflow<{ value: string }>("RemoteGreeting", {
673 className: "MyWorkflow",
674 scriptName: host.workerName,
675 }),
676 },
677 }),
678 )
679 : undefined;
680 return {
681 host,
682 consumer,
683 hostBinding: bindingSnapshot(host.env.MY_WORKFLOW),
684 consumerBinding: consumer ? bindingSnapshot(consumer.env.MY_WORKFLOW) : undefined,
685 };
686 }),
687 );
688 const first = yield* deploy(false);
689 const hostWorkflow = yield* workflows.getWorkflow({
690 accountId,
691 workflowName: first.hostBinding.workflowName,
692 });
693 const deployed = yield* deploy(true);
694 const { host, consumer, hostBinding, consumerBinding } = deployed;
695 expect(consumerBinding?.workflowIsOutput).toBe(true);
696 expect(consumerBinding?.scriptIsOutput).toBe(true);
697 expect(consumerBinding?.workflowName).toBe(hostBinding.workflowName);
698 expect(consumerBinding?.scriptName).toBe(host.workerName);
699 expect(consumerBinding?.name).toBe("RemoteGreeting");
700 expect((yield* readWorkflowBinding(consumer!.workerName)).workflowName).toBe(
701 hostBinding.workflowName,
702 );
703 const all = yield* workflows.listWorkflows.items({ accountId }).pipe(Stream.runCollect);
704 const owned = Array.from(all).filter(
705 (workflow) =>
706 workflow.scriptName === host.workerName || workflow.scriptName === consumer!.workerName,
707 );
708 expect(owned).toHaveLength(1);
709 expect(owned[0]?.id).toBe(hostWorkflow.id);
710 expect(owned[0]?.name).toBe(hostBinding.workflowName);
711 yield* runWorkflowToCompletion(consumer!.url!);
712
713 yield* stack.destroy();
714 yield* expectWorkerGone(accountId, consumer!.workerName);
715 yield* expectWorkerGone(accountId, host.workerName);
716 yield* expectWorkflowGone(accountId, hostBinding.workflowName);
717 }).pipe(logLevel),
718 {
719 tags: [
720 "provider:cloudflare",
721 "provider:cloudflare:worker",
722 "provider:cloudflare:workflow",
723 "live",
724 ],
725 timeout: 120_000,
726 },
727 );
728
729 const waitForAppliedStepLimit = (workflowName: string, expected: number) =>
730 Effect.gen(function* () {
731 const { accountId } = yield* yield* CloudflareEnvironment;
732 const versions = yield* workflows.listVersions
733 .items({ accountId, workflowName })
734 .pipe(Stream.runCollect);
735 return Array.from(versions).some((version) => version.limits?.steps === expected);
736 }).pipe(
737 Effect.repeat({
738 schedule: Schedule.spaced("3 seconds"),
739 until: Boolean,
740 times: 10,
741 }),
742 Effect.tap((applied) => Effect.sync(() => expect(applied).toBe(true))),
743 );
744
745 test.provider(
746 "async worker workflow binding applies a per-workflow step limit",
747 (stack) =>
748 Effect.gen(function* () {
749 yield* stack.destroy();
750 const { accountId } = yield* yield* CloudflareEnvironment;
751 const deployed = yield* stack.deploy(
752 Effect.gen(function* () {
753 const worker = yield* Cloudflare.Worker("limits-workflow-worker", {
754 main: hostMain,
755 env: {
756 MY_WORKFLOW: Cloudflare.Workflow("MyWorkflow", {
757 limits: { steps: 100 },
758 }),
759 },
760 });
761 return { worker, workflowName: worker.env.MY_WORKFLOW.workflowName };
762 }),
763 );
764 yield* waitForAppliedStepLimit(deployed.workflowName, 100);
765 yield* stack.destroy();
766 yield* expectWorkerGone(accountId, deployed.worker.workerName);
767 yield* expectWorkflowGone(accountId, deployed.workflowName);
768 }).pipe(logLevel),
769 {
770 tags: [
771 "provider:cloudflare",
772 "provider:cloudflare:worker",
773 "provider:cloudflare:workflow",
774 "live",
775 ],
776 timeout: 120_000,
777 },
778 );
779
780 const scheduledWorkflowMain = `${import.meta.dirname}/fixtures/workflow-schedules/async-worker.ts`;
781 const waitForAppliedSchedules = (workflowName: string, expected: string[]) =>
782 Effect.gen(function* () {
783 const { accountId } = yield* yield* CloudflareEnvironment;
784 const workflow = yield* workflows.getWorkflow({ accountId, workflowName });
785 const crons = (workflow.schedules ?? []).map((schedule) => schedule.cron);
786 return crons;
787 }).pipe(
788 Effect.flatMap((crons) =>
789 crons.length === expected.length && crons.every((cron, index) => cron === expected[index])
790 ? Effect.succeed(crons)
791 : Effect.fail(new Error(`schedules not applied yet: ${JSON.stringify(crons)}`)),
792 ),
793 retry,
794 );
795
796 test.provider(
797 "async worker workflow binding applies native cron schedules",
798 (stack) =>
799 Effect.gen(function* () {
800 yield* stack.destroy();
801 const { accountId } = yield* yield* CloudflareEnvironment;
802 const yearly = "0 0 1 1 *";
803 const other = "0 0 2 1 *";
804 const deployWith = (schedules: string[]) =>
805 stack.deploy(
806 Effect.gen(function* () {
807 const worker = yield* Cloudflare.Worker("scheduled-workflow-worker", {
808 main: scheduledWorkflowMain,
809 env: {
810 HOURLY: Cloudflare.Workflow("HourlyWorkflow", {
811 className: "HourlyWorkflow",
812 schedules,
813 }),
814 },
815 });
816 return { worker, workflowName: worker.env.HOURLY.workflowName };
817 }),
818 );
819 const created = yield* deployWith([yearly]);
820 expect(yield* waitForAppliedSchedules(created.workflowName, [yearly])).toEqual([yearly]);
821 yield* deployWith([other]);
822 expect(yield* waitForAppliedSchedules(created.workflowName, [other])).toEqual([other]);
823 yield* deployWith([]);
824 expect(yield* waitForAppliedSchedules(created.workflowName, [])).toEqual([]);
825 yield* stack.destroy();
826 yield* expectWorkerGone(accountId, created.worker.workerName);
827 yield* expectWorkflowGone(accountId, created.workflowName);
828 }).pipe(logLevel),
829 {
830 tags: [
831 "provider:cloudflare",
832 "provider:cloudflare:worker",
833 "provider:cloudflare:workflow",
834 "live",
835 ],
836 timeout: 120_000,
837 },
838 );
839
840 const namedWorkflowMain = `${import.meta.dirname}/fixtures/workflow-async/worker.ts`;
841 const physicalName = (scratch: Test.ScratchStack) =>
842 sha256(`${scratch.name}:${scratch.stage}`).pipe(
843 Effect.map((hash) => `alchemy-workflow-${hash.slice(0, 16)}`),
844 );
845 const readWorkflowName = (scriptName: string) =>
846 readWorkflowBinding(scriptName).pipe(Effect.map((binding) => binding.workflowName));
847
848 const namedHost = (workflowName?: string, schedules?: string[]) =>
849 Cloudflare.Worker("NamedHost", {
850 main: namedWorkflowMain,
851 env: {
852 MY_WORKFLOW: Cloudflare.Workflow("MyWorkflow", {
853 workflowName,
854 schedules,
855 }),
856 },
857 });
858
859 test.provider(
860 "physical names preserve defaults, replace on rename, and retain schedules",
861 (scratch) =>
862 Effect.gen(function* () {
863 yield* scratch.destroy();
864 const { accountId } = yield* yield* CloudflareEnvironment;
865 const explicit = yield* physicalName(scratch);
866 const original = yield* scratch.deploy(namedHost());
867 const generated = yield* readWorkflowName(original.workerName);
868 expect(generated).toBe(yield* generateWorkflowName(original.workerName, "MyWorkflow"));
869 const first = yield* workflows.getWorkflow({
870 accountId,
871 workflowName: generated,
872 });
873
874 yield* scratch.deploy(namedHost(generated));
875 expect((yield* workflows.getWorkflow({ accountId, workflowName: generated })).id).toBe(
876 first.id,
877 );
878
879 const renamed = yield* scratch.deploy(namedHost(explicit, ["0 0 1 1 *"]));
880 const replacement = yield* workflows.getWorkflow({
881 accountId,
882 workflowName: explicit,
883 });
884 expect(replacement.id).not.toBe(first.id);
885 expect(yield* readWorkflowName(renamed.workerName)).toBe(explicit);
886 yield* expectWorkflowGone(accountId, generated);
887 expect(yield* waitForAppliedSchedules(explicit, ["0 0 1 1 *"])).toEqual(["0 0 1 1 *"]);
888
889 const preserved = yield* scratch.deploy(namedHost());
890 expect(yield* readWorkflowName(preserved.workerName)).toBe(explicit);
891 const observed = yield* workflows.getWorkflow({
892 accountId,
893 workflowName: explicit,
894 });
895 expect(observed.id).toBe(replacement.id);
896 expect(observed.schedules?.map((schedule) => schedule.cron)).toEqual(["0 0 1 1 *"]);
897 const terminal = yield* runWorkflowToCompletion(preserved.url!).pipe(
898 Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 2 }),
899 );
900 expect(terminal.output?.workflowName).toBe(explicit);
901
902 yield* scratch.deploy(namedHost(undefined, []));
903 expect(yield* waitForAppliedSchedules(explicit, [])).toEqual([]);
904 yield* scratch.destroy();
905 yield* expectWorkflowGone(accountId, explicit);
906 }).pipe(logLevel),
907 {
908 tags: [
909 "provider:cloudflare",
910 "provider:cloudflare:worker",
911 "provider:cloudflare:workflow",
912 "live",
913 ],
914 timeout: 120_000,
915 },
916 );
917
918 const jurisdictionHost = (jurisdiction?: Cloudflare.Workflows.WorkflowJurisdiction) =>
919 Cloudflare.Worker("JurisdictionHost", {
920 main: namedWorkflowMain,
921 env: {
922 MY_WORKFLOW: Cloudflare.Workflow("MyWorkflow", { jurisdiction }),
923 },
924 });
925
926 test.provider(
927 "jurisdiction pins instances and replaces the workflow when it changes",
928 (stack) =>
929 Effect.gen(function* () {
930 yield* stack.destroy();
931 const { accountId } = yield* yield* CloudflareEnvironment;
932
933 const eu = yield* stack.deploy(jurisdictionHost("eu"));
934 const workflowName = yield* readWorkflowName(eu.workerName);
935 const created = yield* workflows.getWorkflow({ accountId, workflowName });
936 expect(created.jurisdiction).toBe("eu");
937 const terminal = yield* runWorkflowToCompletion(eu.url!);
938 expect(terminal.status).toBe("complete");
939
940 const settled = yield* stack.plan(jurisdictionHost("eu"));
941 expect(settled.resources.MyWorkflow?.action).toEqual("noop");
942
943 const replacePlan = yield* stack.plan(jurisdictionHost("us"));
944 const replaceChange = replacePlan.resources.MyWorkflow!;
945 expect(replaceChange.action).toEqual("replace");
946 if (replaceChange.action === "replace") expect(replaceChange.deleteFirst).toBe(true);
947
948 yield* stack.deploy(jurisdictionHost("us"));
949 const us = yield* workflows.getWorkflow({ accountId, workflowName });
950 expect(us.id).not.toBe(created.id);
951 expect(us.jurisdiction).toBe("us");
952
953 const unrestricted = yield* stack.deploy(jurisdictionHost());
954 const removed = yield* workflows.getWorkflow({ accountId, workflowName });
955 expect(removed.id).not.toBe(us.id);
956 expect(removed.jurisdiction ?? undefined).toBeUndefined();
957 expect(yield* readWorkflowName(unrestricted.workerName)).toBe(workflowName);
958
959 yield* stack.destroy();
960 yield* expectWorkflowGone(accountId, workflowName);
961 }).pipe(logLevel),
962 {
963 tags: [
964 "provider:cloudflare",
965 "provider:cloudflare:worker",
966 "provider:cloudflare:workflow",
967 "live",
968 ],
969 timeout: 180_000,
970 },
971 );
972
973 const namedJurisdictionHost = (
974 workflowName: string,
975 jurisdiction: Cloudflare.Workflows.WorkflowJurisdiction,
976 ) =>
977 Cloudflare.Worker("NamedJurisdictionHost", {
978 main: namedWorkflowMain,
979 env: {
980 MY_WORKFLOW: Cloudflare.Workflow("NamedWorkflow", {
981 className: "MyWorkflow",
982 workflowName,
983 jurisdiction,
984 }),
985 },
986 });
987
988 test.provider(
989 "an explicitly named workflow replaces itself when its jurisdiction changes",
990 (stack) =>
991 Effect.gen(function* () {
992 yield* stack.destroy();
993 const { accountId } = yield* yield* CloudflareEnvironment;
994 const workflowName = yield* physicalName(stack);
995
996 yield* stack.deploy(namedJurisdictionHost(workflowName, "eu"));
997 const created = yield* workflows.getWorkflow({ accountId, workflowName });
998 expect(created.jurisdiction).toBe("eu");
999
1000 // The delete-first replacement reads its own previous generation under
1001 // the same explicit name; that is not a foreign workflow.
1002 yield* stack.deploy(namedJurisdictionHost(workflowName, "us"));
1003 const us = yield* workflows.getWorkflow({ accountId, workflowName });
1004 expect(us.id).not.toBe(created.id);
1005 expect(us.jurisdiction).toBe("us");
1006
1007 yield* stack.destroy();
1008 yield* expectWorkflowGone(accountId, workflowName);
1009 }).pipe(logLevel),
1010 {
1011 tags: [
1012 "provider:cloudflare",
1013 "provider:cloudflare:worker",
1014 "provider:cloudflare:workflow",
1015 "live",
1016 ],
1017 timeout: 180_000,
1018 },
1019 );
1020
1021 test.provider(
1022 "direct workflow sources with Effect props preserve explicit identity through omission and rename",
1023 (stack) =>
1024 Effect.gen(function* () {
1025 yield* stack.destroy();
1026 const { accountId } = yield* yield* CloudflareEnvironment;
1027 const explicit = yield* physicalName(stack);
1028 const program = (workflowName?: string) =>
1029 Effect.gen(function* () {
1030 const worker = yield* Cloudflare.Worker("IdentityHost", {
1031 main: hostMain,
1032 env: {
1033 MY_WORKFLOW: Cloudflare.Workflow("IdentityWorkflow", {
1034 className: "MyWorkflow",
1035 workflowName,
1036 }),
1037 WORKFLOW_NAME: workflowName ?? explicit,
1038 EVENTS: Cloudflare.DurableObject("WorkflowEvents"),
1039 },
1040 });
1041 const queue = yield* Cloudflare.Queues.Queue("IdentityQueue");
1042 const subscription = yield* Cloudflare.Queues.Subscription(
1043 "IdentityEvents",
1044 Effect.succeed({
1045 source: worker.env.MY_WORKFLOW,
1046 events: ["instance.completed"],
1047 queueId: queue.queueId,
1048 }),
1049 );
1050 expect(subscription.Props.source).toEqual({
1051 type: "workflows.workflow",
1052 workflowName: worker.env.MY_WORKFLOW.workflowName,
1053 });
1054 yield* Cloudflare.Queues.Consumer("IdentityConsumer", {
1055 queueId: queue.queueId,
1056 scriptName: worker.workerName,
1057 settings: { batchSize: 1, maxWaitTimeMs: 1000 },
1058 });
1059 return {
1060 worker,
1061 queue,
1062 subscription,
1063 binding: bindingSnapshot(worker.env.MY_WORKFLOW),
1064 };
1065 });
1066 const identities: string[] = [];
1067 const subscriptions: string[] = [];
1068 const deployments = yield* Effect.forEach(
1069 [explicit, undefined, `${explicit}-renamed`],
1070 (name) =>
1071 Effect.gen(function* () {
1072 const deployed = yield* stack.deploy(program(name));
1073 const expected = name ?? explicit;
1074 const { worker, binding, subscription } = deployed;
1075 expect(binding.workflowIsOutput).toBe(true);
1076 expect(binding.scriptIsOutput).toBe(true);
1077 expect(binding.workflowName).toBe(expected);
1078 expect(binding.scriptName).toBe(worker.workerName);
1079 // Script settings can briefly report the previous version's
1080 // bindings after an update.
1081 const boundName = yield* readWorkflowName(worker.workerName).pipe(
1082 Effect.repeat({
1083 schedule: Schedule.spaced("2 seconds"),
1084 until: (workflowName) => workflowName === expected,
1085 times: 10,
1086 }),
1087 );
1088 expect(boundName).toBe(expected);
1089 const observed = yield* workflows.getWorkflow({
1090 accountId,
1091 workflowName: binding.workflowName,
1092 });
1093 expect(observed.name).toBe(expected);
1094 expect(observed.scriptName).toBe(binding.scriptName);
1095 identities.push(observed.id!);
1096 subscriptions.push(subscription.subscriptionId);
1097 const source = {
1098 type: "workflows.workflow",
1099 workflowName: expected,
1100 };
1101 const liveSubscription = yield* queues.getSubscription({
1102 accountId,
1103 subscriptionId: subscription.subscriptionId,
1104 });
1105 expect(liveSubscription.source).toEqual(expect.objectContaining(source));
1106 expect(liveSubscription.destination.queueId).toBe(deployed.queue.queueId);
1107 const plan = yield* stack.plan(program(name));
1108 expect(plan.resources.IdentityWorkflow.downstream).toContain("IdentityEvents");
1109 expect(plan.resources.IdentityEvents.state).toHaveProperty("props.source", source);
1110 yield* Effect.logInfo("Workflow delivery phase", {
1111 phase: name ?? "omitted",
1112 workflow: observed,
1113 subscription: liveSubscription,
1114 });
1115 const { terminal, events } = yield* runUntilWorkflowEvent(
1116 `${worker.url}/events`,
1117 runWorkflowToCompletion(worker.url!, expected),
1118 );
1119 expect(terminal.output?.workflowName).toBe(expected);
1120 if (!hasEvent(events, terminal.instanceId)) {
1121 const diagnostics = yield* Effect.gen(function* () {
1122 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
1123 const startedAt = yield* Clock.currentTimeMillis;
1124 yield* Effect.logInfo(
1125 "Workflow event diagnostics",
1126 JSON.stringify({
1127 queue: yield* queues.getQueue({
1128 accountId,
1129 queueId: deployed.queue.queueId,
1130 }),
1131 metrics: yield* queues.getMetricsQueue({
1132 accountId,
1133 queueId: deployed.queue.queueId,
1134 }),
1135 subscription: yield* queues.getSubscription({
1136 accountId,
1137 subscriptionId: subscription.subscriptionId,
1138 }),
1139 storage: yield* getJson(
1140 `${worker.url}/events/diagnostics?instance=${terminal.instanceId}`,
1141 Schema.Unknown,
1142 ),
1143 }),
1144 );
1145 const probeId = `probe-${terminal.instanceId}`;
1146 yield* queues.bulkPushMessages({
1147 accountId,
1148 queueId: deployed.queue.queueId,
1149 messages: [
1150 {
1151 contentType: "json",
1152 body: { type: "diagnostic.queue.probe", probeId },
1153 },
1154 ],
1155 });
1156 yield* Effect.logInfo("Queue probe accepted", { probeId });
1157 const messages = (snapshot: unknown): unknown[] =>
1158 typeof snapshot === "object" &&
1159 snapshot !== null &&
1160 "entries" in snapshot &&
1161 Array.isArray(snapshot.entries)
1162 ? snapshot.entries.flatMap((entry: unknown) =>
1163 Array.isArray(entry) ? [entry[1]] : [],
1164 )
1165 : [];
1166 const hasProbe = (snapshot: unknown) =>
1167 messages(snapshot).some(
1168 (body) =>
1169 typeof body === "object" &&
1170 body !== null &&
1171 "type" in body &&
1172 body.type === "diagnostic.queue.probe" &&
1173 "probeId" in body &&
1174 body.probeId === probeId,
1175 );
1176 const hasOriginal = (snapshot: unknown) =>
1177 messages(snapshot).some(
1178 (body) =>
1179 typeof body === "object" &&
1180 body !== null &&
1181 "payload" in body &&
1182 typeof body.payload === "object" &&
1183 body.payload !== null &&
1184 "instanceId" in body.payload &&
1185 body.payload.instanceId === terminal.instanceId,
1186 );
1187 const probeStorage = yield* client
1188 .get(`${worker.url}/events/diagnostics?probe=${probeId}`)
1189 .pipe(
1190 Effect.flatMap((response) => response.json),
1191 Effect.tap((snapshot) =>
1192 Effect.gen(function* () {
1193 const observedAt = yield* Clock.currentTimeMillis;
1194 yield* Effect.logInfo(
1195 "Queue delivery sample",
1196 JSON.stringify({
1197 observedAt,
1198 elapsedMs: observedAt - startedAt,
1199 originalPresent: hasOriginal(snapshot),
1200 probePresent: hasProbe(snapshot),
1201 storage: snapshot,
1202 }),
1203 );
1204 }),
1205 ),
1206 Effect.repeat({
1207 schedule: Schedule.spaced("3 seconds"),
1208 until: (snapshot) => hasOriginal(snapshot) && hasProbe(snapshot),
1209 times: 9,
1210 }),
1211 );
1212 yield* Effect.logInfo(
1213 "Queue probe delivery",
1214 JSON.stringify({
1215 probeId,
1216 observedAt: yield* Clock.currentTimeMillis,
1217 originalPresent: hasOriginal(probeStorage),
1218 probePresent: hasProbe(probeStorage),
1219 storage: probeStorage,
1220 metrics: yield* queues.getMetricsQueue({
1221 accountId,
1222 queueId: deployed.queue.queueId,
1223 }),
1224 }),
1225 );
1226 }).pipe(Effect.timeout("30 seconds"), Effect.exit);
1227 if (Exit.isFailure(diagnostics)) {
1228 yield* Effect.logWarning(
1229 "Workflow event diagnostics failed",
1230 Cause.pretty(diagnostics.cause),
1231 );
1232 }
1233 }
1234 expectWorkflowEvent(events, terminal.instanceId, source);
1235 return deployed;
1236 }),
1237 );
1238 expect(identities[1]).toBe(identities[0]);
1239 expect(identities[2]).not.toBe(identities[0]);
1240 expect(subscriptions[1]).toBe(subscriptions[0]);
1241 expect(subscriptions[2]).not.toBe(subscriptions[0]);
1242 yield* expectWorkflowGone(accountId, explicit);
1243 const final = deployments[2]!;
1244 yield* stack.destroy();
1245 yield* expectWorkerGone(accountId, final.worker.workerName);
1246 yield* expectWorkflowGone(accountId, `${explicit}-renamed`);
1247 expect(
1248 yield* queues.getQueue({ accountId, queueId: final.queue.queueId }).pipe(
1249 Effect.as(false),
1250 Effect.catchTag("QueueNotFound", () => Effect.succeed(true)),
1251 ),
1252 ).toBe(true);
1253 for (const subscriptionId of new Set(subscriptions)) {
1254 expect(
1255 yield* queues.getSubscription({ accountId, subscriptionId }).pipe(
1256 Effect.as(false),
1257 Effect.catchTag("SubscriptionNotFound", () => Effect.succeed(true)),
1258 ),
1259 ).toBe(true);
1260 }
1261 }).pipe(logLevel),
1262 {
1263 tags: [
1264 "provider:cloudflare",
1265 "provider:cloudflare:queue",
1266 "provider:cloudflare:worker",
1267 "provider:cloudflare:workflow",
1268 "live",
1269 ],
1270 timeout: 600_000,
1271 },
1272 );
1273
1274 test.provider(
1275 "physical names require adoption and preserve adopted identity and schedules",
1276 (scratch) =>
1277 Effect.gen(function* () {
1278 yield* scratch.destroy();
1279 const { accountId } = yield* yield* CloudflareEnvironment;
1280 const workflowName = yield* physicalName(scratch);
1281 const host = Cloudflare.Worker("AdoptionHost", {
1282 main: namedWorkflowMain,
1283 });
1284 const deployed = yield* scratch.deploy(host);
1285 yield* expectWorkflowGone(accountId, workflowName);
1286 const foreign = yield* workflows.putWorkflow({
1287 accountId,
1288 workflowName,
1289 scriptName: deployed.workerName,
1290 className: "MyWorkflow",
1291 schedules: [{ cron: "0 0 1 1 *" }],
1292 });
1293 yield* Effect.addFinalizer(() =>
1294 workflows.deleteWorkflow({ accountId, workflowName }).pipe(
1295 Effect.catchTag("WorkflowNotFound", () => Effect.void),
1296 Effect.orDie,
1297 ),
1298 );
1299 const definition = (adopting: boolean, schedules?: string[]) =>
1300 Effect.gen(function* () {
1301 const worker = yield* host;
1302 return yield* WorkflowResource("Adopted", {
1303 workflowName,
1304 className: "MyWorkflow",
1305 scriptName: worker.workerName,
1306 schedules,
1307 }).pipe(adopt(adopting));
1308 });
1309 const denied = yield* scratch.plan(definition(false)).pipe(Effect.exit);
1310 expect(Exit.isFailure(denied)).toBe(true);
1311 if (Exit.isFailure(denied))
1312 expect(Cause.pretty(denied.cause)).toContain("OwnedBySomeoneElse");
1313 expect((yield* workflows.getWorkflow({ accountId, workflowName })).id).toBe(foreign.id);
1314
1315 const adopted = yield* scratch.deploy(definition(true));
1316 expect(adopted.workflowName).toBe(workflowName);
1317 expect(adopted.workflowId).toBe(foreign.id);
1318 expect(adopted.schedules).toEqual(["0 0 1 1 *"]);
1319 const updated = yield* scratch.deploy(definition(false, []));
1320 expect(updated.workflowId).toBe(foreign.id);
1321 expect(yield* waitForAppliedSchedules(workflowName, [])).toEqual([]);
1322 yield* scratch.destroy();
1323 yield* expectWorkflowGone(accountId, workflowName);
1324 }).pipe(logLevel),
1325 {
1326 tags: [
1327 "provider:cloudflare",
1328 "provider:cloudflare:worker",
1329 "provider:cloudflare:workflow",
1330 "live",
1331 ],
1332 timeout: 120_000,
1333 },
1334 );
1335
1336 test.provider(
1337 "physical names refuse occupied rename targets even with adoption enabled",
1338 (scratch) =>
1339 Effect.gen(function* () {
1340 yield* scratch.destroy();
1341 const { accountId } = yield* yield* CloudflareEnvironment;
1342 const originalName = yield* physicalName(scratch);
1343 const occupiedName = `${originalName}-occupied`;
1344 const occupiedHost = Cloudflare.Worker("OccupiedHost", {
1345 main: namedWorkflowMain,
1346 env: {
1347 MY_WORKFLOW: Cloudflare.Workflow("OccupiedWorkflow", {
1348 className: "MyWorkflow",
1349 workflowName: occupiedName,
1350 schedules: ["0 0 1 1 *"],
1351 }),
1352 },
1353 });
1354 const definition = (name: string) =>
1355 Effect.gen(function* () {
1356 const host = yield* namedHost(name);
1357 const occupied = yield* occupiedHost;
1358 return { host, occupied };
1359 });
1360 yield* scratch.deploy(definition(originalName));
1361 const original = yield* workflows.getWorkflow({
1362 accountId,
1363 workflowName: originalName,
1364 });
1365 const occupied = yield* workflows.getWorkflow({
1366 accountId,
1367 workflowName: occupiedName,
1368 });
1369 const denied = yield* scratch
1370 .deploy(definition(occupiedName).pipe(adopt(true)))
1371 .pipe(Effect.exit);
1372 expect(Exit.isFailure(denied)).toBe(true);
1373 if (Exit.isFailure(denied))
1374 expect(Cause.pretty(denied.cause)).toContain("OwnedBySomeoneElse");
1375 const untouched = yield* workflows.getWorkflow({
1376 accountId,
1377 workflowName: occupiedName,
1378 });
1379 expect(untouched.id).toBe(occupied.id);
1380 expect(untouched.scriptName).toBe(occupied.scriptName);
1381 expect(untouched.schedules).toEqual(occupied.schedules);
1382 expect(
1383 (yield* workflows.getWorkflow({
1384 accountId,
1385 workflowName: originalName,
1386 })).id,
1387 ).toBe(original.id);
1388 // Removing the rejected source must not delete the occupied target.
1389 yield* scratch.deploy(
1390 Effect.gen(function* () {
1391 yield* Cloudflare.Worker("NamedHost", { main: namedWorkflowMain });
1392 yield* occupiedHost;
1393 }),
1394 );
1395 yield* expectWorkflowGone(accountId, originalName);
1396 expect(
1397 (yield* workflows.getWorkflow({
1398 accountId,
1399 workflowName: occupiedName,
1400 })).id,
1401 ).toBe(occupied.id);
1402 yield* scratch.destroy();
1403 yield* expectWorkflowGone(accountId, originalName);
1404 yield* expectWorkflowGone(accountId, occupiedName);
1405 }).pipe(logLevel),
1406 {
1407 tags: [
1408 "provider:cloudflare",
1409 "provider:cloudflare:worker",
1410 "provider:cloudflare:workflow",
1411 "live",
1412 ],
1413 timeout: 120_000,
1414 },
1415 );
1416
1417 test.provider(
1418 "direct foreign workflow subscription sources do not own the host Workflow",
1419 (scratch) =>
1420 Effect.gen(function* () {
1421 yield* scratch.destroy();
1422 const { accountId } = yield* yield* CloudflareEnvironment;
1423 const workflowName = yield* physicalName(scratch);
1424 const host = namedHost(workflowName);
1425 yield* scratch.deploy(host);
1426 const original = yield* workflows.getWorkflow({
1427 accountId,
1428 workflowName,
1429 });
1430 const deployed = yield* scratch.deploy(
1431 Effect.gen(function* () {
1432 const worker = yield* host;
1433 const consumer = yield* Cloudflare.Worker("NamedConsumer", {
1434 main: namedWorkflowMain,
1435 env: {
1436 MY_WORKFLOW: Cloudflare.Workflow("ForeignWorkflow", {
1437 className: "MyWorkflow",
1438 scriptName: worker.workerName,
1439 workflowName,
1440 }),
1441 },
1442 });
1443 const queue = yield* Cloudflare.Queues.Queue("ForeignEventsQueue");
1444 const Subscription = yield* Cloudflare.Queues.Subscription;
1445 const subscription = yield* Subscription("ForeignEvents", {
1446 source: consumer.env.MY_WORKFLOW,
1447 events: ["instance.completed"],
1448 queueId: queue.queueId,
1449 });
1450 expect(subscription.Props.source).toEqual({
1451 type: "workflows.workflow",
1452 workflowName: consumer.env.MY_WORKFLOW.workflowName,
1453 });
1454 return {
1455 worker,
1456 consumer,
1457 queue,
1458 subscription,
1459 hostBinding: bindingSnapshot(worker.env.MY_WORKFLOW),
1460 consumerBinding: bindingSnapshot(consumer.env.MY_WORKFLOW),
1461 };
1462 }),
1463 );
1464 expect(deployed.hostBinding.workflowName).toBe(workflowName);
1465 expect(deployed.consumerBinding.workflowName).toBe(workflowName);
1466 expect(deployed.consumerBinding.scriptName).toBe(deployed.worker.workerName);
1467 expect(deployed.consumerBinding.workflowIsOutput).toBe(true);
1468 expect(deployed.consumerBinding.scriptIsOutput).toBe(true);
1469 const source = { type: "workflows.workflow", workflowName };
1470 expect(deployed.subscription.source).toEqual(source);
1471 const liveSubscription = yield* queues.getSubscription({
1472 accountId,
1473 subscriptionId: deployed.subscription.subscriptionId,
1474 });
1475 expect(liveSubscription.source).toEqual(expect.objectContaining(source));
1476 expect(liveSubscription.destination.queueId).toBe(deployed.queue.queueId);
1477 const owned = yield* workflows.listWorkflows.items({ accountId }).pipe(
1478 Stream.runCollect,
1479 Effect.map((all) =>
1480 Array.from(all).filter(
1481 (workflow) =>
1482 workflow.scriptName === deployed.worker.workerName ||
1483 workflow.scriptName === deployed.consumer.workerName,
1484 ),
1485 ),
1486 );
1487 expect(owned).toHaveLength(1);
1488 expect(owned[0]?.id).toBe(original.id);
1489 expect(yield* readWorkflowName(deployed.consumer.workerName)).toBe(workflowName);
1490 const terminal = yield* runWorkflowToCompletion(deployed.consumer.url!).pipe(
1491 Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 2 }),
1492 );
1493 expect(terminal.output?.workflowName).toBe(workflowName);
1494 yield* scratch.deploy(host);
1495 yield* expectWorkerGone(accountId, deployed.consumer.workerName);
1496 expect(
1497 yield* queues
1498 .getSubscription({
1499 accountId,
1500 subscriptionId: deployed.subscription.subscriptionId,
1501 })
1502 .pipe(
1503 Effect.as(false),
1504 Effect.catchTag("SubscriptionNotFound", () => Effect.succeed(true)),
1505 ),
1506 ).toBe(true);
1507 expect(
1508 yield* queues.getQueue({ accountId, queueId: deployed.queue.queueId }).pipe(
1509 Effect.as(false),
1510 Effect.catchTag("QueueNotFound", () => Effect.succeed(true)),
1511 ),
1512 ).toBe(true);
1513 expect((yield* workflows.getWorkflow({ accountId, workflowName })).id).toBe(original.id);
1514 yield* scratch.destroy();
1515 yield* expectWorkerGone(accountId, deployed.worker.workerName);
1516 yield* expectWorkflowGone(accountId, workflowName);
1517 }).pipe(logLevel),
1518 {
1519 tags: [
1520 "provider:cloudflare",
1521 "provider:cloudflare:queue",
1522 "provider:cloudflare:worker",
1523 "provider:cloudflare:workflow",
1524 "live",
1525 ],
1526 timeout: 120_000,
1527 },
1528 );
1529
1530 for (const dev of [false, true]) {
1531 const { test: hostTest } = Test.make({
1532 providers: Cloudflare.providers(),
1533 dev,
1534 });
1535
1536 hostTest.provider(
1537 `binding-only host changes update dependent scriptName in the same deploy (dev: ${dev})`,
1538 (scratch) =>
1539 Effect.gen(function* () {
1540 yield* scratch.destroy();
1541 const workflowName = yield* physicalName(scratch);
1542 const definition = (destination: "A" | "B") =>
1543 Effect.gen(function* () {
1544 const a = yield* Cloudflare.Worker("HostA", {
1545 main: namedWorkflowMain,
1546 });
1547 const b = yield* Cloudflare.Worker("HostB", {
1548 main: namedWorkflowMain,
1549 });
1550 const host = destination === "A" ? a : b;
1551 const workflow = yield* WorkflowResource("MovedWorkflow", {
1552 workflowName,
1553 className: "MyWorkflow",
1554 schedules: ["0 0 1 1 *"],
1555 });
1556 yield* workflow.bind`host`({ scriptName: host.workerName });
1557 const consumer = yield* Cloudflare.Worker("HostConsumer", {
1558 main: namedWorkflowMain,
1559 env: { WORKFLOW_SCRIPT_NAME: workflow.scriptName },
1560 });
1561 return { host, workflow, consumer };
1562 });
1563 const expectScript = (url: string, scriptName: string) =>
1564 requestWorker(
1565 HttpClientRequest.get(`${url}/workflow/script-name`).pipe(
1566 HttpClientRequest.setHeader("cache-control", "no-cache"),
1567 ),
1568 { retryDelay: "3 seconds" },
1569 ).pipe(
1570 Effect.flatMap((response) =>
1571 response.text.pipe(
1572 Effect.flatMap((body) =>
1573 response.status === 200
1574 ? Effect.succeed(body)
1575 : Effect.fail(
1576 new Error(`GET ${url}/workflow/script-name: ${response.status}: ${body}`),
1577 ),
1578 ),
1579 ),
1580 ),
1581 Effect.repeat({
1582 schedule: Schedule.spaced("3 seconds"),
1583 until: (name) => name === scriptName,
1584 times: 10,
1585 }),
1586 Effect.tap((name) => Effect.sync(() => expect(name).toBe(scriptName))),
1587 Effect.timeout("45 seconds"),
1588 );
1589 const original = yield* scratch.deploy(definition("A"));
1590 yield* expectScript(original.consumer.url!, original.host.workerName);
1591 if (dev) {
1592 expect(original.workflow.workflowId).toMatch(/^dev:/);
1593 expect(original.consumer.url).toMatch(/^http:\/\/localhost:/);
1594 }
1595
1596 const moved = yield* scratch.deploy(definition("B"));
1597 expect(moved.host.workerName).not.toBe(original.host.workerName);
1598 expect(moved.workflow.workflowId).toBe(original.workflow.workflowId);
1599 expect(moved.workflow.scriptName).toBe(moved.host.workerName);
1600 yield* expectScript(moved.consumer.url!, moved.host.workerName);
1601 if (!dev) {
1602 const observed = yield* workflows.getWorkflow({
1603 accountId: moved.workflow.accountId,
1604 workflowName,
1605 });
1606 expect(observed.id).toBe(original.workflow.workflowId);
1607 expect(observed.scriptName).toBe(moved.host.workerName);
1608 expect(observed.schedules?.map((schedule) => schedule.cron)).toEqual(["0 0 1 1 *"]);
1609 }
1610 yield* scratch.destroy();
1611 if (!dev) {
1612 yield* expectWorkflowGone(moved.workflow.accountId, workflowName);
1613 }
1614 }).pipe(logLevel),
1615 {
1616 tags: [
1617 "provider:cloudflare",
1618 "provider:cloudflare:worker",
1619 "provider:cloudflare:workflow",
1620 ...(dev ? ["local"] : ["live"]),
1621 ],
1622 timeout: 120_000,
1623 },
1624 );
1625 }
1626
1627 for (const api of ["async", "Effect"]) {
1628 test.provider(
1629 `public ${api} Workflow cold adoption with a new destination Worker`,
1630 (scratch) =>
1631 Effect.gen(function* () {
1632 yield* scratch.destroy();
1633 const { accountId } = yield* yield* CloudflareEnvironment;
1634 const workflowName =
1635 api === "async" ? yield* physicalName(scratch) : COLD_EFFECT_WORKFLOW_NAME;
1636 const source = Cloudflare.Worker("ColdAdoptionSource", {
1637 main: namedWorkflowMain,
1638 });
1639 const seededHost = yield* scratch.deploy(source);
1640 yield* expectWorkflowGone(accountId, workflowName);
1641 const existing = yield* workflows.putWorkflow({
1642 accountId,
1643 workflowName,
1644 scriptName: seededHost.workerName,
1645 className: "MyWorkflow",
1646 schedules: [{ cron: "0 0 1 1 *" }],
1647 });
1648 yield* Effect.addFinalizer(() =>
1649 workflows.deleteWorkflow({ accountId, workflowName }).pipe(
1650 Effect.catchTag("WorkflowNotFound", () => Effect.void),
1651 Effect.orDie,
1652 ),
1653 );
1654 const definition = Effect.gen(function* () {
1655 yield* source;
1656 if (api === "async") return yield* namedHost(workflowName);
1657 return yield* ColdEffectWorker;
1658 });
1659 const denied = yield* scratch.plan(definition.pipe(adopt(false))).pipe(Effect.exit);
1660 expect(Exit.isFailure(denied)).toBe(true);
1661 if (Exit.isFailure(denied))
1662 expect(Cause.pretty(denied.cause)).toContain("OwnedBySomeoneElse");
1663 const untouched = yield* workflows.getWorkflow({
1664 accountId,
1665 workflowName,
1666 });
1667 expect(untouched.id).toBe(existing.id);
1668 expect(untouched.scriptName).toBe(seededHost.workerName);
1669 expect(untouched.schedules?.map((schedule) => schedule.cron)).toEqual(["0 0 1 1 *"]);
1670 const destination = yield* scratch.deploy(definition.pipe(adopt(true)));
1671 const adopted = yield* workflows.getWorkflow({
1672 accountId,
1673 workflowName,
1674 });
1675 expect(adopted.id).toBe(existing.id);
1676 expect(adopted.scriptName).toBe(destination.workerName);
1677 expect(adopted.schedules?.map((schedule) => schedule.cron)).toEqual(["0 0 1 1 *"]);
1678 const terminal = yield* runWorkflowToCompletion(destination.url!).pipe(
1679 Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 2 }),
1680 );
1681 expect(terminal.output?.greeting).toBe("Hello, world!");
1682 expect(terminal.output?.workflowName).toBe(workflowName);
1683 yield* scratch.destroy();
1684 yield* expectWorkflowGone(accountId, workflowName);
1685 }).pipe(logLevel),
1686 {
1687 tags: [
1688 "provider:cloudflare",
1689 "provider:cloudflare:worker",
1690 "provider:cloudflare:workflow",
1691 "live",
1692 ],
1693 timeout: 120_000,
1694 },
1695 );
1696 }