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

main
ts 313 lines 10.7 KB
Raw
1 import { makeFunctionHttpHandler } from "@alchemy.run/aws/Lambda/HttpServer";
2 import {
3 DEFAULT_TIMEOUT_MARGIN_MS,
4 HandlerContext,
5 InvocationDeadlineState,
6 lambdaTimeoutOf,
7 readTimeoutGrace,
8 TIMEOUT_GRACE_ENV,
9 toDurationMillis,
10 withInvocationDeadline,
11 } from "@alchemy.run/aws/Lambda/InvocationDeadline";
12 import { describe, expect, it } from "@alchemy.run/test";
13 import type * as lambda from "aws-lambda";
14 import * as Duration from "effect/Duration";
15 import * as Effect from "effect/Effect";
16 import * as Exit from "effect/Exit";
17 import * as Fiber from "effect/Fiber";
18 import * as HttpServerRequest from "effect/http/HttpServerRequest";
19 import * as HttpServerResponse from "effect/http/HttpServerResponse";
20 import * as TestClock from "effect/testing/TestClock";
21
22 describe(
23 "readTimeoutGrace",
24 { tags: ["unit", "provider:aws", "provider:aws:lambda", "local"] },
25 () => {
26 it("never interrupts when unset, empty or unparseable", () => {
27 expect(readTimeoutGrace(undefined)).toBe(0);
28 expect(readTimeoutGrace("")).toBe(0);
29 expect(readTimeoutGrace("abc")).toBe(0);
30 expect(readTimeoutGrace("-1")).toBe(0);
31 });
32
33 it("parses whole milliseconds", () => {
34 expect(readTimeoutGrace("0")).toBe(0);
35 expect(readTimeoutGrace("1500")).toBe(1500);
36 expect(readTimeoutGrace("250.9")).toBe(250);
37 });
38 },
39 );
40
41 describe(
42 "toDurationMillis",
43 { tags: ["unit", "provider:aws", "provider:aws:lambda", "local"] },
44 () => {
45 it("returns undefined for undefined", () => {
46 expect(toDurationMillis(undefined)).toBeUndefined();
47 });
48
49 it("converts a Duration to whole milliseconds", () => {
50 expect(toDurationMillis(Duration.zero)).toBe(0);
51 expect(toDurationMillis(Duration.millis(250))).toBe(250);
52 expect(toDurationMillis(Duration.seconds(1))).toBe(1000);
53 expect(toDurationMillis(Duration.infinity)).toBeUndefined();
54 });
55
56 it("converts a state-JSON rehydrated Duration", () => {
57 const json = JSON.parse(JSON.stringify(Duration.millis(750))) as Duration.Duration;
58 expect(toDurationMillis(json)).toBe(750);
59 });
60 },
61 );
62
63 describe(
64 "lambdaTimeoutOf",
65 { tags: ["unit", "provider:aws", "provider:aws:lambda", "local"] },
66 () => {
67 const run = (props: Omit<Parameters<typeof lambdaTimeoutOf>[0], "id">) =>
68 Effect.runSync(Effect.result(lambdaTimeoutOf({ id: "Fn", ...props })));
69
70 it("adds the default margin to the default timeout", () => {
71 expect(DEFAULT_TIMEOUT_MARGIN_MS).toBe(1000);
72 expect(run({ timeout: undefined, timeoutMargin: undefined, interrupt: true })).toMatchObject({
73 success: { timeoutSeconds: 4, graceMs: 1000 },
74 });
75 });
76
77 it("interrupts at the exact timeout; Lambda's timeout rounds up", () => {
78 expect(
79 run({
80 timeout: Duration.millis(2500),
81 timeoutMargin: Duration.millis(200),
82 interrupt: true,
83 }),
84 ).toMatchObject({ success: { timeoutSeconds: 3, graceMs: 500 } });
85 });
86
87 it("configures the timeout as-is when the handler is never interrupted", () => {
88 expect(
89 run({ timeout: Duration.millis(2500), timeoutMargin: undefined, interrupt: false }),
90 ).toMatchObject({ success: { timeoutSeconds: 3, graceMs: 0 } });
91 expect(
92 run({ timeout: Duration.seconds(60), timeoutMargin: Duration.zero, interrupt: true }),
93 ).toMatchObject({ success: { timeoutSeconds: 60, graceMs: 0 } });
94 expect(
95 run({ timeout: Duration.seconds(900), timeoutMargin: undefined, interrupt: false }),
96 ).toMatchObject({ success: { timeoutSeconds: 900, graceMs: 0 } });
97 });
98
99 it("rejects a timeout whose margin does not fit in 900 seconds", () => {
100 const result = run({
101 timeout: Duration.seconds(900),
102 timeoutMargin: undefined,
103 interrupt: true,
104 });
105 expect(result).toMatchObject({
106 failure: { _tag: "AWS.Lambda.FunctionTimeoutTooLong", timeoutMs: 900_000, marginMs: 1000 },
107 });
108 expect(
109 run({ timeout: Duration.seconds(899), timeoutMargin: undefined, interrupt: true }),
110 ).toMatchObject({ success: { timeoutSeconds: 900, graceMs: 1000 } });
111 });
112 },
113 );
114
115 // A Lambda context whose hard timeout is `remainingMs` away. The guard reads
116 // it once, when the invocation starts.
117 const makeContext = (remainingMs: number): lambda.Context =>
118 ({
119 functionName: "deadline-fn",
120 awsRequestId: "req-1",
121 getRemainingTimeInMillis: () => remainingMs,
122 }) as lambda.Context;
123
124 // Runs `self` under the deadline guard with Lambda's timeout `remainingMs`
125 // away and `graceMs` of it reserved past the Function's timeout. `state` is
126 // the dispatcher's per-invocation deadline state.
127 const guarded = <A, E>(
128 self: Effect.Effect<A, E>,
129 { remainingMs, graceMs }: { remainingMs: number; graceMs: number },
130 options?: { interrupt?: boolean },
131 state: { fired: boolean } = { fired: false },
132 ) =>
133 Effect.gen(function* () {
134 process.env[TIMEOUT_GRACE_ENV] = String(graceMs);
135 return yield* self.pipe(
136 withInvocationDeadline(options),
137 Effect.provideService(HandlerContext, makeContext(remainingMs)),
138 Effect.provideService(InvocationDeadlineState, state),
139 Effect.ensuring(Effect.sync(() => delete process.env[TIMEOUT_GRACE_ENV])),
140 Effect.exit,
141 Effect.forkChild,
142 );
143 });
144
145 describe(
146 "withInvocationDeadline",
147 // Sets the grace env var, which the guard reads per invocation.
148 { tags: ["unit", "provider:aws", "provider:aws:lambda", "local"] },
149 () => {
150 it.effect(
151 "leaves a handler that finishes inside its timeout untouched",
152 () =>
153 Effect.gen(function* () {
154 const state = { fired: false };
155 const fiber = yield* guarded(
156 Effect.sleep("2 seconds").pipe(Effect.as("done")),
157 { remainingMs: 4000, graceMs: 1000 },
158 undefined,
159 state,
160 );
161 yield* TestClock.adjust("2 seconds");
162 expect(yield* Fiber.join(fiber)).toEqual(Exit.succeed("done"));
163 expect(state.fired).toBe(false);
164 }),
165 { exclusive: true },
166 );
167
168 it.effect(
169 "interrupts a handler at its timeout and runs its finalizers",
170 () =>
171 Effect.gen(function* () {
172 const events: string[] = [];
173 const state = { fired: false };
174 const fiber = yield* guarded(
175 Effect.never.pipe(
176 Effect.ensuring(Effect.sync(() => events.push("finalizer"))),
177 Effect.onInterrupt(() => Effect.sync(() => events.push("interrupted"))),
178 ),
179 { remainingMs: 4000, graceMs: 1000 },
180 undefined,
181 state,
182 );
183 yield* TestClock.adjust("2999 millis");
184 expect(events).toEqual([]);
185 yield* TestClock.adjust("1 millis");
186 const exit = yield* Fiber.join(fiber);
187 expect(events).toEqual(["finalizer", "interrupted"]);
188 // The dispatcher holds a fired invocation until Lambda's timeout.
189 expect(state.fired).toBe(true);
190 expect(exit).toMatchObject({
191 _tag: "Failure",
192 cause: {
193 reasons: [
194 {
195 _tag: "Fail",
196 error: { _tag: "AWS.Lambda.InvocationTimeoutError", budgetMs: 3000 },
197 },
198 ],
199 },
200 });
201 }),
202 { exclusive: true },
203 );
204
205 it.effect(
206 "never interrupts a handler without grace",
207 () =>
208 Effect.gen(function* () {
209 const fiber = yield* guarded(Effect.sleep("3500 millis").pipe(Effect.as("done")), {
210 remainingMs: 4000,
211 graceMs: 0,
212 });
213 yield* TestClock.adjust("3500 millis");
214 expect(yield* Fiber.join(fiber)).toEqual(Exit.succeed("done"));
215 }),
216 { exclusive: true },
217 );
218
219 it.effect(
220 "never interrupts a handler that opts out",
221 () =>
222 Effect.gen(function* () {
223 const fiber = yield* guarded(
224 Effect.sleep("3500 millis").pipe(Effect.as("done")),
225 { remainingMs: 4000, graceMs: 1000 },
226 { interrupt: false },
227 );
228 yield* TestClock.adjust("3500 millis");
229 expect(yield* Fiber.join(fiber)).toEqual(Exit.succeed("done"));
230 }),
231 { exclusive: true },
232 );
233
234 it.effect(
235 "an inner guard that opts out stands the outer one down",
236 () =>
237 Effect.gen(function* () {
238 const fiber = yield* guarded(
239 Effect.sleep("3500 millis").pipe(
240 Effect.as("done"),
241 withInvocationDeadline({ interrupt: false }),
242 ),
243 { remainingMs: 4000, graceMs: 1000 },
244 );
245 yield* TestClock.adjust("3500 millis");
246 expect(yield* Fiber.join(fiber)).toEqual(Exit.succeed("done"));
247 }),
248 { exclusive: true },
249 );
250
251 it.effect(
252 "fails an HTTP request that runs past its timeout after its finalizers",
253 () =>
254 Effect.gen(function* () {
255 const events: string[] = [];
256 const handler = Effect.never.pipe(
257 Effect.ensuring(Effect.sync(() => events.push("finalizer"))),
258 Effect.as(HttpServerResponse.text("never")),
259 );
260 const out = makeFunctionHttpHandler(
261 handler as Effect.Effect<
262 HttpServerResponse.HttpServerResponse,
263 never,
264 HttpServerRequest.HttpServerRequest
265 >,
266 )(functionUrlEvent);
267 if (!Effect.isEffect(out)) {
268 return yield* Effect.die("Expected Effect from Function URL handler");
269 }
270 const state = { fired: false };
271 const fiber = yield* guarded(out, { remainingMs: 4000, graceMs: 1000 }, undefined, state);
272 yield* TestClock.adjust("3 seconds");
273 const exit = yield* Fiber.join(fiber);
274 expect(events).toEqual(["finalizer"]);
275 expect(state.fired).toBe(true);
276 expect(exit).toMatchObject({
277 _tag: "Failure",
278 cause: {
279 reasons: [{ _tag: "Fail", error: { _tag: "AWS.Lambda.InvocationTimeoutError" } }],
280 },
281 });
282 }),
283 { exclusive: true },
284 );
285 },
286 );
287
288 const functionUrlEvent = {
289 version: "2.0",
290 routeKey: "$default",
291 rawPath: "/",
292 rawQueryString: "",
293 headers: { host: "example.lambda-url.us-east-1.on.aws", "x-forwarded-proto": "https" },
294 requestContext: {
295 accountId: "123456789012",
296 apiId: "api-id",
297 domainName: "example.lambda-url.us-east-1.on.aws",
298 domainPrefix: "example",
299 http: {
300 method: "GET",
301 path: "/",
302 protocol: "HTTP/1.1",
303 sourceIp: "203.0.113.42",
304 userAgent: "test",
305 },
306 requestId: "request-id",
307 routeKey: "$default",
308 stage: "$default",
309 time: "01/Jan/2026:00:00:00 +0000",
310 timeEpoch: 0,
311 },
312 isBase64Encoded: false,
313 } satisfies lambda.LambdaFunctionURLEvent;