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

main
ts 133 lines 5.07 KB
Raw
1 import * as Fly from "@alchemy.run/fly";
2 import { makeMachineLeases } from "@alchemy.run/fly/leases";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as machines from "@distilled.cloud/fly-io/machines";
6 import * as Retry from "@distilled.cloud/fly-io/Retry";
7 import * as Deferred from "effect/Deferred";
8 import * as Effect from "effect/Effect";
9 import * as Exit from "effect/Exit";
10 import * as Fiber from "effect/Fiber";
11 import * as HttpClient from "effect/http/HttpClient";
12 import * as Scope from "effect/Scope";
13 import { assertAppGone } from "./fixtures/bluegreen.ts";
14 import { sanitizeExecFailure } from "./fixtures/exec-lease.ts";
15
16 const { test } = Test.make({ providers: Fly.providers() });
17
18 test.provider(
19 "native lease cleanup waits for an in-flight renewal before releasing authority",
20 (stack) =>
21 Effect.gen(function* () {
22 yield* stack.destroy();
23 const target = yield* stack.deploy(
24 Effect.gen(function* () {
25 const app = yield* Fly.App("Site");
26 return yield* Fly.Machine("Worker", {
27 app,
28 image: "nginx:alpine",
29 region: "iad",
30 skipLaunch: true,
31 });
32 }),
33 );
34 try {
35 yield* Effect.gen(function* () {
36 const entered = yield* Deferred.make<void>();
37 const resume = yield* Deferred.make<void>();
38 const closed = yield* Deferred.make<void>();
39 const client = yield* HttpClient.HttpClient;
40 let acquisitions = 0;
41 let releases = 0;
42 const observed = HttpClient.transform(client, (response, request) =>
43 Effect.gen(function* () {
44 const path = request.url.split("?")[0]!;
45 if (path.endsWith(`/machines/${target.machineId}/lease`)) {
46 if (request.method === "POST") {
47 acquisitions++;
48 if (acquisitions === 2) {
49 yield* Deferred.succeed(entered, undefined);
50 yield* Deferred.await(resume);
51 }
52 } else if (request.method === "DELETE") releases++;
53 }
54 return yield* response;
55 }),
56 );
57 yield* Effect.gen(function* () {
58 const scope = yield* Scope.make();
59 yield* Effect.addFinalizer((exit) =>
60 Deferred.succeed(resume, undefined).pipe(Effect.andThen(Scope.close(scope, exit))),
61 );
62 const leases = yield* makeMachineLeases(target.appName).pipe(
63 Effect.provideService(Scope.Scope, scope),
64 );
65 yield* leases.acquire([target.machineId]);
66 yield* Effect.logInfo("Lease renewal probe", {
67 phase: "acquired",
68 acquisitions,
69 releases,
70 });
71 yield* Deferred.await(entered).pipe(
72 Effect.timeout("40 seconds"),
73 Effect.tapError((error) =>
74 Effect.logInfo("Lease renewal probe", {
75 phase: "await renewal",
76 acquisitions,
77 releases,
78 tag: error._tag,
79 }),
80 ),
81 );
82 const closing = yield* Scope.close(scope, Exit.void).pipe(
83 Effect.tap(() => Deferred.succeed(closed, undefined)),
84 Effect.forkScoped,
85 );
86 yield* Effect.sleep("200 millis");
87 expect(yield* Deferred.isDone(closed)).toBe(false);
88 expect(releases).toBe(0);
89 const held = yield* machines
90 .getMachineLease({
91 app_name: target.appName,
92 machine_id: target.machineId,
93 })
94 .pipe(Retry.none, Effect.timeout("10 seconds"));
95 expect(typeof held.data?.nonce === "string").toBe(true);
96 yield* Deferred.succeed(resume, undefined);
97 yield* Fiber.join(closing).pipe(
98 Effect.timeout("20 seconds"),
99 Effect.tapError((error) =>
100 Effect.logInfo("Lease renewal probe", {
101 phase: "await close",
102 acquisitions,
103 releases,
104 tag: error._tag,
105 }),
106 ),
107 );
108 expect(releases).toBe(1);
109 expect(acquisitions).toBe(2);
110 const absent = yield* machines
111 .getMachineLease({
112 app_name: target.appName,
113 machine_id: target.machineId,
114 })
115 .pipe(
116 Retry.none,
117 Effect.as(false),
118 Effect.catchTag("NotFound", () => Effect.succeed(true)),
119 Effect.timeout("10 seconds"),
120 );
121 expect(absent).toBe(true);
122 }).pipe(Effect.provideService(HttpClient.HttpClient, observed), Effect.scoped);
123 }).pipe(Effect.scoped);
124 } finally {
125 yield* stack.destroy();
126 yield* assertAppGone(target.appName);
127 }
128 }).pipe(sanitizeExecFailure),
129 {
130 tags: ["provider:fly", "provider:fly:app", "provider:fly:machine", "live"],
131 timeout: 240_000,
132 },
133 );