@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 288 lines 11.4 KB
Raw
1 import * as EMRServerless from "@alchemy.run/aws/EMRServerless";
2 import * as IAM from "@alchemy.run/aws/IAM";
3 import * as Lambda from "@alchemy.run/aws/Lambda";
4 import type * as emr from "@distilled.cloud/aws/emr-serverless";
5 import * as Duration from "effect/Duration";
6 import * as Effect from "effect/Effect";
7 import { HttpServerRequest } from "effect/http/HttpServerRequest";
8 import * as HttpServerResponse from "effect/http/HttpServerResponse";
9 import * as Layer from "effect/Layer";
10 import * as Schedule from "effect/Schedule";
11 import * as Stream from "effect/Stream";
12 import path from "pathe";
13
14 const main = path.resolve(import.meta.dirname, "handler.ts");
15
16 // Well-formed (16 chars of [0-9a-z]) but nonexistent ids — probes must answer
17 // with the typed ResourceNotFoundException, which proves the IAM grant passed
18 // authorization AND the application id was injected from the binding.
19 const FAKE_JOB_RUN_ID = "00abcdefabcdef01";
20 const FAKE_SESSION_ID = "00abcdefabcdef01";
21
22 // Session authorization can lag behind other EMR APIs on a fresh Lambda role.
23 const sessionAuthorizationPolicy = {
24 while: (error: emr.GetSessionEndpointError | emr.StartSessionError | emr.ListSessionsError) =>
25 error._tag === "AccessDeniedException",
26 schedule: Schedule.spaced("3 seconds"),
27 times: 8,
28 };
29
30 /** Deterministic names shared with the test for out-of-band verification. */
31 const stageSuffix = process.env.ALCHEMY_TEST_STAGE ? `-${process.env.ALCHEMY_TEST_STAGE}` : "";
32 export const BINDINGS_APP_NAME = `alchemy-test-emrs-bind${stageSuffix}`;
33 export const BINDINGS_ROLE_NAME = `alchemy-test-emrs-bind-role${stageSuffix}`;
34
35 export class EmrServerlessTestFunction extends Lambda.Function<Lambda.Function>()(
36 "EmrServerlessTestFunction",
37 ) {}
38
39 export default EmrServerlessTestFunction.make(
40 {
41 main,
42 functionUrl: true,
43 timeout: Duration.seconds(30),
44 },
45 Effect.gen(function* () {
46 // The Spark application the bindings are bound to. Auto-start lets a job
47 // submission start it; auto-stop reaps it after a minute idle. In the
48 // CREATED/STOPPED state it costs nothing.
49 const app = yield* EMRServerless.Application("BindingApp", {
50 applicationName: BINDINGS_APP_NAME,
51 releaseLabel: "emr-7.5.0",
52 autoStartConfiguration: { enabled: true },
53 autoStopConfiguration: { enabled: true, idleTimeout: "1 minute" },
54 tags: { purpose: "alchemy-test" },
55 });
56
57 // The execution role submitted job runs assume (no data permissions —
58 // the fixture cancels jobs immediately after submission).
59 yield* IAM.Role("JobRole", {
60 roleName: BINDINGS_ROLE_NAME,
61 assumeRolePolicyDocument: {
62 Version: "2012-10-17",
63 Statement: [
64 {
65 Effect: "Allow",
66 Principal: { Service: "emr-serverless.amazonaws.com" },
67 Action: ["sts:AssumeRole"],
68 },
69 ],
70 },
71 });
72
73 // Event source: subscribe the host to EMR Serverless job-run state
74 // changes. The deploy proves the EventBridge rule + invoke permission.
75 yield* EMRServerless.consumeJobRunEvents({ kinds: ["job-run-state-change"] }, (events) =>
76 Stream.runForEach(events, (event) =>
77 Effect.log(`emr-serverless job ${event.detail.jobRunId} -> ${event.detail.state}`),
78 ),
79 );
80
81 const startJobRun = yield* EMRServerless.StartJobRun(app);
82 const getJobRun = yield* EMRServerless.GetJobRun(app);
83 const cancelJobRun = yield* EMRServerless.CancelJobRun(app);
84 const listJobRuns = yield* EMRServerless.ListJobRuns(app);
85 const listJobRunAttempts = yield* EMRServerless.ListJobRunAttempts(app);
86 const getDashboardForJobRun = yield* EMRServerless.GetDashboardForJobRun(app);
87 const getResourceDashboard = yield* EMRServerless.GetResourceDashboard(app);
88 const startApplication = yield* EMRServerless.StartApplication(app);
89 const stopApplication = yield* EMRServerless.StopApplication(app);
90 const startSession = yield* EMRServerless.StartSession(app);
91 const getSession = yield* EMRServerless.GetSession(app);
92 const terminateSession = yield* EMRServerless.TerminateSession(app);
93 const listSessions = yield* EMRServerless.ListSessions(app);
94 const getSessionEndpoint = yield* EMRServerless.GetSessionEndpoint(app);
95
96 const bound = {
97 startJobRun,
98 getJobRun,
99 cancelJobRun,
100 listJobRuns,
101 listJobRunAttempts,
102 getDashboardForJobRun,
103 getResourceDashboard,
104 startApplication,
105 stopApplication,
106 startSession,
107 getSession,
108 terminateSession,
109 listSessions,
110 getSessionEndpoint,
111 };
112
113 // Run an operation and answer with its typed outcome: `ok` on success,
114 // the error's `_tag` otherwise. Probes assert the tag — a typed
115 // service error (not AccessDeniedException) proves the IAM grant and
116 // the injected application id both work.
117 const probe = <A, E extends { _tag: string }>(effect: Effect.Effect<A, E>) =>
118 effect.pipe(
119 Effect.map(() => ({ tag: "ok", detail: "" }) as const),
120 Effect.catch((error) =>
121 Effect.succeed({ tag: error._tag, detail: String(error) } as const),
122 ),
123 Effect.flatMap((outcome) => HttpServerResponse.json(outcome)),
124 );
125
126 return {
127 fetch: Effect.gen(function* () {
128 const request = yield* HttpServerRequest;
129 const url = new URL(request.originalUrl);
130 const pathname = url.pathname;
131
132 if (request.method === "GET" && pathname === "/bindings") {
133 return yield* HttpServerResponse.json({ bound: Object.keys(bound) });
134 }
135
136 // Real reads: application id injected from the binding.
137 if (request.method === "GET" && pathname === "/jobruns") {
138 const { jobRuns } = yield* listJobRuns().pipe(Effect.orDie);
139 return yield* HttpServerResponse.json({
140 ids: jobRuns.map((run) => run.id),
141 });
142 }
143 if (request.method === "GET" && pathname === "/sessions") {
144 return yield* probe(listSessions().pipe(Effect.retry(sessionAuthorizationPolicy)));
145 }
146
147 // Typed not-found probes on nonexistent sub-resources.
148 if (request.method === "GET" && pathname === "/jobrun") {
149 return yield* probe(getJobRun({ jobRunId: FAKE_JOB_RUN_ID }));
150 }
151 if (request.method === "GET" && pathname === "/jobrun-dashboard") {
152 return yield* probe(getDashboardForJobRun({ jobRunId: FAKE_JOB_RUN_ID }));
153 }
154 if (request.method === "GET" && pathname === "/jobrun-attempts") {
155 return yield* probe(listJobRunAttempts({ jobRunId: FAKE_JOB_RUN_ID }));
156 }
157 if (request.method === "POST" && pathname === "/jobrun-cancel-fake") {
158 return yield* probe(cancelJobRun({ jobRunId: FAKE_JOB_RUN_ID }));
159 }
160 if (request.method === "GET" && pathname === "/session") {
161 return yield* probe(getSession({ sessionId: FAKE_SESSION_ID }));
162 }
163 if (request.method === "GET" && pathname === "/session-endpoint") {
164 return yield* probe(
165 getSessionEndpoint({ sessionId: FAKE_SESSION_ID }).pipe(
166 Effect.retry(sessionAuthorizationPolicy),
167 ),
168 );
169 }
170 if (request.method === "POST" && pathname === "/session-terminate") {
171 return yield* probe(terminateSession({ sessionId: FAKE_SESSION_ID }));
172 }
173 if (request.method === "GET" && pathname === "/resource-dashboard") {
174 return yield* probe(
175 getResourceDashboard({
176 resourceId: FAKE_JOB_RUN_ID,
177 resourceType: "SPARK_DRIVER",
178 }),
179 );
180 }
181 // startSession is registration-covered; the runtime probe proves the
182 // grant + PassRole path end-to-end (the fixture application has no
183 // interactive configuration, so the service answers with a typed
184 // validation error rather than starting a billable session).
185 if (request.method === "POST" && pathname === "/session-start") {
186 const roleArn = url.searchParams.get("roleArn");
187 if (!roleArn) {
188 return yield* HttpServerResponse.json(
189 { error: "roleArn query parameter required" },
190 { status: 400 },
191 );
192 }
193 return yield* probe(
194 startSession({ executionRoleArn: roleArn }).pipe(
195 Effect.retry(sessionAuthorizationPolicy),
196 ),
197 );
198 }
199
200 // Application control.
201 if (request.method === "POST" && pathname === "/app-start") {
202 return yield* probe(startApplication());
203 }
204 if (request.method === "POST" && pathname === "/app-stop") {
205 return yield* probe(stopApplication());
206 }
207
208 // Real job-run round trip: submit (SparkPi from the EMR image — no
209 // S3 assets), then the test cancels it immediately.
210 if (request.method === "POST" && pathname === "/jobrun-run") {
211 const roleArn = url.searchParams.get("roleArn");
212 if (!roleArn) {
213 return yield* HttpServerResponse.json(
214 { error: "roleArn query parameter required" },
215 { status: 400 },
216 );
217 }
218 const started = yield* startJobRun({
219 executionRoleArn: roleArn,
220 jobDriver: {
221 sparkSubmit: {
222 entryPoint: "local:///usr/lib/spark/examples/src/main/python/pi.py",
223 },
224 },
225 executionTimeoutMinutes: 10,
226 }).pipe(Effect.orDie);
227 return yield* HttpServerResponse.json({
228 jobRunId: started.jobRunId,
229 arn: started.arn,
230 });
231 }
232 if (request.method === "POST" && pathname === "/jobrun-cancel") {
233 const id = url.searchParams.get("id");
234 if (!id) {
235 return yield* HttpServerResponse.json(
236 { error: "id query parameter required" },
237 { status: 400 },
238 );
239 }
240 const cancelled = yield* cancelJobRun({ jobRunId: id }).pipe(Effect.orDie);
241 return yield* HttpServerResponse.json({
242 jobRunId: cancelled.jobRunId,
243 });
244 }
245 if (request.method === "GET" && pathname === "/jobrun-detail") {
246 const id = url.searchParams.get("id");
247 if (!id) {
248 return yield* HttpServerResponse.json(
249 { error: "id query parameter required" },
250 { status: 400 },
251 );
252 }
253 const { jobRun } = yield* getJobRun({ jobRunId: id }).pipe(Effect.orDie);
254 return yield* HttpServerResponse.json({
255 jobRunId: jobRun.jobRunId,
256 state: jobRun.state,
257 stateDetails: jobRun.stateDetails,
258 });
259 }
260
261 return yield* HttpServerResponse.json(
262 { error: "Not found", method: request.method, pathname },
263 { status: 404 },
264 );
265 }).pipe(Effect.orDie),
266 };
267 }).pipe(
268 Effect.provide(
269 Layer.mergeAll(
270 Lambda.EventSource,
271 EMRServerless.StartJobRunHttp,
272 EMRServerless.GetJobRunHttp,
273 EMRServerless.CancelJobRunHttp,
274 EMRServerless.ListJobRunsHttp,
275 EMRServerless.ListJobRunAttemptsHttp,
276 EMRServerless.GetDashboardForJobRunHttp,
277 EMRServerless.GetResourceDashboardHttp,
278 EMRServerless.StartApplicationHttp,
279 EMRServerless.StopApplicationHttp,
280 EMRServerless.StartSessionHttp,
281 EMRServerless.GetSessionHttp,
282 EMRServerless.TerminateSessionHttp,
283 EMRServerless.ListSessionsHttp,
284 EMRServerless.GetSessionEndpointHttp,
285 ),
286 ),
287 ),
288 );