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

main
ts 137 lines 5.12 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as logs from "@distilled.cloud/aws/cloudwatch-logs";
5 import * as Lambda from "@distilled.cloud/aws/lambda";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as Schedule from "effect/Schedule";
9 import ShutdownProbe from "./shutdown-probe.ts";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 const filterLogs = Effect.fn(function* (logGroupName: string, filterPattern: string) {
14 const result = yield* logs
15 .filterLogEvents({ logGroupName, filterPattern })
16 .pipe(Effect.catch(() => Effect.succeed({ events: [] })));
17 return (result.events ?? []).length > 0;
18 });
19
20 /**
21 * The fast half of the Shutdown-phase contract: the generated entry
22 * registers an internal extension with the Extensions API at cold start.
23 * Registration is the documented precondition for Lambda's SIGTERM window —
24 * without any registered extension the sandbox is killed with 0 ms and no
25 * signal; with an internal extension the runtime gets SIGTERM + 500 ms.
26 *
27 * A successful invoke proves registration doesn't hang Init (a registered
28 * extension must signal readiness via the parked `/event/next` long-poll —
29 * getting that wrong wedges Init and every invoke 502s). Lambda logs the
30 * registration as an `EXTENSION` platform line, which pins that the
31 * registration was actually accepted.
32 *
33 * The slow half — Lambda delivering SIGTERM at idle spin-down and the
34 * instance finalizers running — takes many idle minutes and is gated below.
35 */
36 test.provider(
37 "entry registers the graceful-shutdown internal extension",
38 (stack) =>
39 Effect.gen(function* () {
40 yield* stack.destroy();
41
42 const fn = yield* stack.deploy(
43 Effect.gen(function* () {
44 return yield* ShutdownProbe;
45 }),
46 );
47
48 const client = yield* HttpClient.HttpClient;
49 const res = yield* client.get(fn.functionUrl!).pipe(
50 Effect.retry({
51 schedule: Schedule.exponential("500 millis"),
52 times: 10,
53 }),
54 );
55 expect(res.status).toBe(200);
56 expect(yield* res.text).toBe("ok");
57
58 const logGroupName = `/aws/lambda/${fn.functionName}`;
59 const registered = yield* filterLogs(logGroupName, '"alchemy-graceful-shutdown"').pipe(
60 Effect.repeat({
61 schedule: Schedule.spaced("3 seconds"),
62 until: (found) => found,
63 times: 20,
64 }),
65 );
66 expect(registered).toBe(true);
67
68 // Request-scope finalizers settle inline per invocation — the marker
69 // must reach the logs after a normal request.
70 const requestFinalized = yield* filterLogs(logGroupName, '"ALCHEMY_REQUEST_FINALIZED"').pipe(
71 Effect.repeat({
72 schedule: Schedule.spaced("3 seconds"),
73 until: (found) => found,
74 times: 20,
75 }),
76 );
77 expect(requestFinalized).toBe(true);
78
79 yield* stack.destroy();
80
81 // Out-of-band proof the destroy removed the function from the cloud.
82 yield* Lambda.getFunction({ FunctionName: fn.functionName }).pipe(
83 Effect.flatMap(() => Effect.fail(new Error(`Function ${fn.functionName} still exists`))),
84 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
85 Effect.retry({
86 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
87 }),
88 );
89 }),
90 { tags: ["provider:aws", "provider:aws:lambda", "live"], timeout: 600_000 },
91 );
92
93 /**
94 * The slow half: leave the sandbox idle until Lambda spins it down, then
95 * assert the SIGTERM handler closed the instance scope and the init-level
96 * finalizer's marker reached CloudWatch. Idle spin-down takes several
97 * minutes and is not under our control, so this only runs when explicitly
98 * requested:
99 *
100 * AWS_LAMBDA_TEST_SHUTDOWN=1 bun vitest run test/AWS/Lambda/Shutdown.test.ts
101 */
102 test.provider.skipIf(!process.env.AWS_LAMBDA_TEST_SHUTDOWN)(
103 "instance-scope finalizers run at idle spin-down",
104 (stack) =>
105 Effect.gen(function* () {
106 const fn = yield* stack.deploy(
107 Effect.gen(function* () {
108 return yield* ShutdownProbe;
109 }),
110 );
111
112 const client = yield* HttpClient.HttpClient;
113 const res = yield* client.get(fn.functionUrl!).pipe(
114 Effect.retry({
115 schedule: Schedule.exponential("500 millis"),
116 times: 10,
117 }),
118 );
119 expect(res.status).toBe(200);
120
121 // Do NOT invoke again — every invoke resets the idle clock. Poll the
122 // log group until the shutdown marker lands. No stack.destroy: keep
123 // the deployment so a re-run continues observing the same function.
124 const logGroupName = `/aws/lambda/${fn.functionName}`;
125 const finalized = yield* filterLogs(logGroupName, '"ALCHEMY_INSTANCE_FINALIZED"').pipe(
126 Effect.repeat({
127 schedule: Schedule.spaced("30 seconds"),
128 until: (found) => found,
129 times: 17,
130 }),
131 );
132 expect(finalized).toBe(true);
133
134 yield* stack.destroy();
135 }),
136 { tags: ["provider:aws", "provider:aws:lambda", "live"], timeout: 600_000 },
137 );