@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, 04:45 UTC

main
ts 635 lines 22.1 KB
Raw
1 import { hashDockerBuildInputs, resolveDockerBuildPaths } from "@alchemy.run/docker/BuildHash";
2 import { Docker } from "@alchemy.run/docker/Docker";
3 import * as ecr from "@distilled.cloud/aws/ecr";
4 import { createPhysicalName } from "alchemy/PhysicalName";
5 import { createInternalTags, createTagsList, hasAlchemyTags } from "alchemy/Tags";
6 import { sha256Object } from "alchemy/Util/sha256";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import * as Schema from "effect/Schema";
11 import { buildAndPushEcrImage } from "../ECR/Image.ts";
12 import { AWSEnvironment } from "../Environment.ts";
13 import { normalizePolicyDocument } from "../IAM/Policy.ts";
14 import type { FunctionImageConfig } from "./Function.ts";
15
16 export class FunctionImageInvalidProps extends Data.TaggedError("FunctionImageInvalidProps")<{
17 readonly message: string;
18 readonly cause?: unknown;
19 }> {}
20
21 export class FunctionImageNotReady extends Data.TaggedError("FunctionImageNotReady")<{
22 readonly message: string;
23 readonly cause?: unknown;
24 }> {}
25
26 export class FunctionImageOperationFailed extends Data.TaggedError("FunctionImageOperationFailed")<{
27 readonly message: string;
28 readonly cause?: unknown;
29 }> {}
30
31 export class FunctionImageUnexpectedResponse extends Data.TaggedError(
32 "FunctionImageUnexpectedResponse",
33 )<{
34 readonly message: string;
35 readonly cause?: unknown;
36 }> {}
37
38 /** A Lambda container image built from a local Docker context. */
39 export interface FunctionDockerImageSource extends FunctionImageConfig {
40 uri?: never;
41 /** Docker build context directory. */
42 context: string;
43 /** Dockerfile path, relative to {@link context} unless absolute. */
44 dockerfile: string;
45 /**
46 * Docker build arguments (`--build-arg`).
47 *
48 * Build arguments are stored in deployment state and may be retained in
49 * image layers. Do not use them for secrets.
50 */
51 buildArgs?: Record<string, string>;
52 }
53
54 /** An existing private ECR image, addressed by tag or immutable digest. */
55 export interface FunctionEcrImageSource extends FunctionImageConfig {
56 /**
57 * Full private ECR image URI, including a tag or digest.
58 *
59 * The repository must be in the same AWS Region as the Lambda function.
60 * Alchemy observes the current ECR digest on every plan, so repointing a
61 * tag updates the function even when the URI string itself is unchanged.
62 */
63 uri: string;
64 context?: never;
65 dockerfile?: never;
66 buildArgs?: never;
67 }
68
69 /**
70 * Image input for an image-packaged Lambda function.
71 *
72 * Source values must be literal because Lambda's circular-dependency
73 * pre-create phase needs the deployable image before normal Output resolution.
74 */
75 export type FunctionImageSource = FunctionDockerImageSource | FunctionEcrImageSource;
76
77 // Instruction overrides ride along on the source object; they are function
78 // configuration, not image identity, so they never enter the image hash.
79 const FunctionImageConfigFields = {
80 command: Schema.String.pipe(Schema.Array, Schema.optionalKey),
81 entryPoint: Schema.String.pipe(Schema.Array, Schema.optionalKey),
82 workingDirectory: Schema.optionalKey(Schema.String),
83 };
84
85 const FunctionDockerImageSourceSchema = Schema.Struct({
86 context: Schema.String,
87 dockerfile: Schema.String,
88 buildArgs: Schema.optionalKey(Schema.Record(Schema.String, Schema.String)),
89 ...FunctionImageConfigFields,
90 }) satisfies Schema.Schema<FunctionDockerImageSource>;
91
92 const FunctionEcrImageSourceSchema = Schema.Struct({
93 uri: Schema.String,
94 ...FunctionImageConfigFields,
95 }) satisfies Schema.Schema<FunctionEcrImageSource>;
96
97 const FunctionImageSourceRecordSchema = Schema.Record(Schema.String, Schema.Unknown);
98
99 export interface ParsedFunctionImageUri {
100 uri: string;
101 registryId: string;
102 registryHost: string;
103 region: string;
104 repositoryName: string;
105 repositoryUri: string;
106 imageId: { imageTag: string } | { imageDigest: string };
107 }
108
109 /** Parse and validate a tagged or digest-pinned private ECR image URI. */
110 export const parseFunctionImageUri = Effect.fn("AWS.Lambda.parseImageUri")(function* (
111 id: string,
112 uri: string,
113 ) {
114 const slash = uri.indexOf("/");
115 if (slash <= 0 || slash === uri.length - 1) {
116 return yield* new FunctionImageInvalidProps({
117 message: `Function(${id}): image.uri must be a private ECR image URI including a tag or digest`,
118 });
119 }
120
121 const registryHost = uri.slice(0, slash);
122 const registry = /^(\d{12})\.dkr\.ecr(-fips)?\.([a-z0-9-]+)\.(.+)$/.exec(registryHost);
123 if (registry === null) {
124 return yield* new FunctionImageInvalidProps({
125 message: `Function(${id}): image.uri must use a private ECR registry such as 123456789012.dkr.ecr.us-east-1.amazonaws.com/repository:tag`,
126 });
127 }
128 if (registry[2] !== undefined) {
129 return yield* new FunctionImageInvalidProps({
130 message: `Function(${id}): Lambda container images do not support ECR FIPS endpoints`,
131 });
132 }
133
134 const reference = uri.slice(slash + 1);
135 const digestSeparator = reference.lastIndexOf("@");
136 const tagSeparator = reference.lastIndexOf(
137 ":",
138 digestSeparator >= 0 ? digestSeparator : reference.length,
139 );
140 if (digestSeparator >= 0 && tagSeparator >= 0) {
141 return yield* new FunctionImageInvalidProps({
142 message: `Function(${id}): image.uri must identify the image by either tag or digest, not both`,
143 });
144 }
145 const repositoryName = reference.slice(0, digestSeparator >= 0 ? digestSeparator : tagSeparator);
146 if (repositoryName.length === 0) {
147 return yield* new FunctionImageInvalidProps({
148 message: `Function(${id}): image.uri has no ECR repository name`,
149 });
150 }
151
152 const resolveImageId = Effect.gen(function* () {
153 if (digestSeparator >= 0) {
154 const imageDigest = reference.slice(digestSeparator + 1);
155 if (!/^sha256:[0-9a-f]{64}$/.test(imageDigest)) {
156 return yield* new FunctionImageInvalidProps({
157 message: `Function(${id}): image.uri digest must be sha256 followed by 64 lowercase hexadecimal characters`,
158 });
159 }
160 return { imageDigest } as const;
161 }
162 if (tagSeparator <= 0 || tagSeparator === reference.length - 1) {
163 return yield* new FunctionImageInvalidProps({
164 message: `Function(${id}): image.uri must include an explicit ECR tag or digest`,
165 });
166 }
167 return { imageTag: reference.slice(tagSeparator + 1) } as const;
168 });
169 const imageId = yield* resolveImageId;
170
171 return {
172 uri,
173 registryId: registry[1],
174 registryHost,
175 region: registry[3],
176 repositoryName,
177 repositoryUri: `${registryHost}/${repositoryName}`,
178 imageId,
179 } satisfies ParsedFunctionImageUri;
180 });
181
182 export interface FunctionImageAttributes {
183 /** Image source kind used by the Function. */
184 source: "build" | "uri";
185 /** Stable identity hash for the requested URI and resolved image digest. */
186 hash: string;
187 /** Tag- or digest-based URI passed to Lambda. */
188 imageUri: string;
189 /** Digest-pinned URI resolved from ECR. */
190 resolvedImageUri: string;
191 /** Registry manifest digest. */
192 digest: string;
193 /** Backing ECR repository name. */
194 repositoryName: string;
195 /** Backing ECR repository URI. */
196 repositoryUri: string;
197 /** Whether the Function owns and may delete the backing repository. */
198 ownsRepository: boolean;
199 }
200
201 export interface ResolveFunctionImageOptions {
202 id: string;
203 source: FunctionImageSource;
204 architecture: "x86_64" | "arm64";
205 previousImage?: Omit<FunctionImageAttributes, "hash">;
206 session: { note: (message: string) => Effect.Effect<void> };
207 }
208
209 export interface FunctionImageIdentity {
210 source: "build" | "uri";
211 hash: string;
212 imageUri?: string;
213 resolvedImageUri?: string;
214 }
215
216 /**
217 * Increment when the Lambda image build behavior changes in a way that must
218 * invalidate already-pushed content-addressed images.
219 */
220 const functionImageBuilderVersion = 2;
221
222 export const functionImagePlatform = (architecture: "x86_64" | "arm64") => {
223 if (architecture === "x86_64") {
224 return "linux/amd64";
225 }
226 if (architecture === "arm64") {
227 return "linux/arm64";
228 }
229 throw new Error(`Unsupported Lambda image architecture: ${architecture as string}`);
230 };
231
232 const hashDecodedFunctionImageBuild = Effect.fn(function* (
233 source: FunctionDockerImageSource,
234 architecture: "x86_64" | "arm64",
235 ) {
236 const buildHash = yield* hashDockerBuildInputs(
237 {
238 ...source,
239 platform: functionImagePlatform(architecture),
240 },
241 "effective",
242 );
243 return (yield* sha256Object({
244 builderVersion: functionImageBuilderVersion,
245 buildHash,
246 })).slice(0, 32);
247 });
248
249 /**
250 * Hash the Docker build context, Dockerfile, build args, target platform, and
251 * Lambda image-builder version without including absolute paths.
252 */
253 export const hashFunctionImageBuild = Effect.fn(function* (
254 source: FunctionDockerImageSource,
255 architecture: "x86_64" | "arm64",
256 ) {
257 const decoded = yield* Schema.decodeEffect(FunctionDockerImageSourceSchema)(source);
258 return yield* hashDecodedFunctionImageBuild(decoded, architecture);
259 });
260
261 export const decodeFunctionImageSource = Effect.fn("AWS.Lambda.decodeFunctionImageSource")(
262 function* (id: string, source: unknown) {
263 const record = yield* Schema.decodeUnknownEffect(FunctionImageSourceRecordSchema)(source).pipe(
264 Effect.mapError(
265 (error) =>
266 new FunctionImageInvalidProps({
267 message: `Function(${id}): image must be an object`,
268 cause: error,
269 }),
270 ),
271 );
272 const hasUri = record.uri !== undefined;
273 const hasBuildSource =
274 record.context !== undefined ||
275 record.dockerfile !== undefined ||
276 record.buildArgs !== undefined;
277 if (hasUri && hasBuildSource) {
278 return yield* new FunctionImageInvalidProps({
279 message: `Function(${id}): image.uri cannot be combined with image.context, image.dockerfile, or image.buildArgs; declare exactly one image source`,
280 });
281 }
282 if (!hasUri && !hasBuildSource) {
283 return yield* new FunctionImageInvalidProps({
284 message: `Function(${id}): image must declare either image.uri or both image.context and image.dockerfile`,
285 });
286 }
287 const decodeError = (error: unknown) =>
288 new Error(
289 `Function(${id}): image source values must be literal and valid before Lambda pre-create`,
290 { cause: error },
291 );
292 if (hasUri) {
293 return yield* Schema.decodeUnknownEffect(FunctionEcrImageSourceSchema)(source).pipe(
294 Effect.map((decoded): FunctionEcrImageSource => decoded),
295 Effect.mapError(decodeError),
296 );
297 }
298 return yield* Schema.decodeUnknownEffect(FunctionDockerImageSourceSchema)(source).pipe(
299 Effect.map((decoded): FunctionDockerImageSource => decoded),
300 Effect.mapError(decodeError),
301 );
302 },
303 );
304
305 const isFunctionEcrImageSource = (source: FunctionImageSource): source is FunctionEcrImageSource =>
306 source.uri !== undefined;
307
308 /**
309 * Lambda-specific image resolver. It deliberately stays separate from the
310 * ECS/EKS image-source abstraction because Lambda deploys private ECR images
311 * directly and its managed local-build path owns a repository pull policy.
312 */
313 export const makeFunctionImage = Effect.gen(function* () {
314 const docker = yield* Docker;
315
316 const createRepositoryName = (id: string) =>
317 createPhysicalName({
318 id: `${id}-image`,
319 maxLength: 256,
320 lowercase: true,
321 });
322
323 const desiredRepositoryPolicy = Effect.fn(function* () {
324 const { accountId, region } = yield* AWSEnvironment.current;
325 return JSON.stringify({
326 Version: "2012-10-17",
327 Statement: [
328 {
329 Sid: "LambdaECRImageRetrievalPolicy",
330 Effect: "Allow",
331 Principal: { Service: "lambda.amazonaws.com" },
332 Action: ["ecr:BatchGetImage", "ecr:GetDownloadUrlForLayer"],
333 Condition: {
334 StringLike: {
335 "aws:sourceArn": `arn:aws:lambda:${region}:${accountId}:function:*`,
336 },
337 },
338 },
339 ],
340 });
341 });
342
343 const ensureRepository = Effect.fn(function* (id: string, repositoryName: string) {
344 let repository = yield* ecr.describeRepositories({ repositoryNames: [repositoryName] }).pipe(
345 Effect.map((response) => response.repositories?.[0]),
346 Effect.catchTag("RepositoryNotFoundException", () => Effect.undefined),
347 );
348
349 if (!repository?.repositoryArn || !repository.repositoryUri) {
350 const tags = yield* createInternalTags(id);
351 repository = yield* ecr
352 .createRepository({
353 repositoryName,
354 imageTagMutability: "IMMUTABLE",
355 imageScanningConfiguration: { scanOnPush: true },
356 tags: createTagsList(tags),
357 })
358 .pipe(
359 Effect.map((response) => response.repository),
360 Effect.catchTag("RepositoryAlreadyExistsException", () =>
361 ecr
362 .describeRepositories({ repositoryNames: [repositoryName] })
363 .pipe(Effect.map((response) => response.repositories?.[0])),
364 ),
365 );
366 }
367
368 if (!repository?.repositoryArn || !repository.repositoryUri) {
369 return yield* Effect.die(
370 new Error(`Failed to create or read Lambda image repository '${repositoryName}'`),
371 );
372 }
373
374 const tags = yield* ecr.listTagsForResource({
375 resourceArn: repository.repositoryArn,
376 });
377 if (!(yield* hasAlchemyTags(id, tags.tags))) {
378 return yield* Effect.die(
379 new Error(
380 `Lambda image repository '${repositoryName}' exists but is not owned by Function(${id})`,
381 ),
382 );
383 }
384
385 // Tags are content hashes, so changing what an existing tag references
386 // would violate the identity contract used by Lambda deployment state.
387 // Verify ownership before migrating repositories created by older versions.
388 if (repository.imageTagMutability !== "IMMUTABLE") {
389 yield* ecr.putImageTagMutability({
390 repositoryName,
391 imageTagMutability: "IMMUTABLE",
392 });
393 }
394
395 const desiredPolicy = yield* desiredRepositoryPolicy();
396 const observedPolicy = yield* ecr.getRepositoryPolicy({ repositoryName }).pipe(
397 Effect.map((response) => response.policyText),
398 Effect.catchTag("RepositoryPolicyNotFoundException", () => Effect.undefined),
399 );
400 if (
401 observedPolicy === undefined ||
402 normalizePolicyDocument(observedPolicy) !== normalizePolicyDocument(desiredPolicy)
403 ) {
404 yield* ecr.setRepositoryPolicy({
405 repositoryName,
406 policyText: desiredPolicy,
407 });
408 }
409
410 return {
411 repositoryName,
412 repositoryUri: repository.repositoryUri,
413 };
414 });
415
416 const describeImage = Effect.fn(function* (repositoryName: string, imageTag: string) {
417 const response = yield* ecr
418 .describeImages({
419 repositoryName,
420 imageIds: [{ imageTag }],
421 })
422 .pipe(
423 Effect.catchTag(
424 ["ImageNotFoundException", "RepositoryNotFoundException"],
425 () => Effect.undefined,
426 ),
427 );
428 return response?.imageDetails?.[0];
429 });
430
431 const resolveExternalImage = Effect.fn("AWS.Lambda.resolveExternalImage")(function* (
432 id: string,
433 source: FunctionEcrImageSource,
434 previousImage?: Omit<FunctionImageAttributes, "hash">,
435 ) {
436 const parsed = yield* parseFunctionImageUri(id, source.uri);
437 const { region } = yield* AWSEnvironment.current;
438 if (parsed.region !== region) {
439 return yield* new FunctionImageInvalidProps({
440 message: `Function(${id}): ECR image region '${parsed.region}' must match Lambda region '${region}'`,
441 });
442 }
443
444 const detail = yield* ecr
445 .describeImages({
446 registryId: parsed.registryId,
447 repositoryName: parsed.repositoryName,
448 imageIds: [parsed.imageId],
449 })
450 .pipe(
451 Effect.catchTag(["ImageNotFoundException", "RepositoryNotFoundException"], () =>
452 Effect.fail(
453 new FunctionImageOperationFailed({
454 message: `Function(${id}): ECR image '${source.uri}' does not exist or is not accessible`,
455 }),
456 ),
457 ),
458 Effect.map((response) => response.imageDetails?.[0]),
459 );
460 const digest = detail?.imageDigest;
461 if (digest === undefined) {
462 return yield* new FunctionImageUnexpectedResponse({
463 message: `Function(${id}): ECR image '${source.uri}' did not resolve to a registry digest`,
464 });
465 }
466
467 const hash = (yield* sha256Object({ uri: source.uri, digest })).slice(0, 32);
468 return {
469 source: "uri",
470 hash,
471 imageUri: source.uri,
472 resolvedImageUri: `${parsed.repositoryUri}@${digest}`,
473 digest,
474 repositoryName: parsed.repositoryName,
475 repositoryUri: parsed.repositoryUri,
476 ownsRepository:
477 previousImage?.ownsRepository === true &&
478 previousImage.repositoryUri === parsed.repositoryUri,
479 } satisfies FunctionImageAttributes;
480 });
481
482 const identity = Effect.fn(function* (
483 id: string,
484 source: FunctionImageSource,
485 architecture: "x86_64" | "arm64",
486 ) {
487 const decoded = yield* decodeFunctionImageSource(id, source);
488 if (isFunctionEcrImageSource(decoded)) {
489 const external = yield* resolveExternalImage(id, decoded);
490 return {
491 source: external.source,
492 hash: external.hash,
493 imageUri: external.imageUri,
494 resolvedImageUri: external.resolvedImageUri,
495 } satisfies FunctionImageIdentity;
496 }
497 return {
498 source: "build",
499 hash: yield* hashDecodedFunctionImageBuild(decoded, architecture),
500 } satisfies FunctionImageIdentity;
501 });
502
503 const hash = Effect.fn(function* (
504 id: string,
505 source: FunctionImageSource,
506 architecture: "x86_64" | "arm64",
507 ) {
508 return (yield* identity(id, source, architecture)).hash;
509 });
510
511 const resolve = Effect.fn(function* (options: ResolveFunctionImageOptions) {
512 const source = yield* decodeFunctionImageSource(options.id, options.source);
513 if (isFunctionEcrImageSource(source)) {
514 const image = yield* resolveExternalImage(options.id, source, options.previousImage);
515 yield* options.session.note(image.imageUri);
516 return image;
517 }
518 const imageTag = yield* hashDecodedFunctionImageBuild(source, options.architecture);
519 const repositoryName =
520 options.previousImage?.ownsRepository === true
521 ? options.previousImage.repositoryName
522 : yield* createRepositoryName(options.id);
523 // Re-ensure even with persisted metadata: the repository or its Lambda
524 // pull policy may have drifted out-of-band.
525 const ensured = yield* ensureRepository(options.id, repositoryName);
526 const imageUri = `${ensured.repositoryUri}:${imageTag}`;
527
528 let detail = yield* describeImage(repositoryName, imageTag);
529 if (!detail?.imageDigest) {
530 const build = yield* resolveDockerBuildPaths(source);
531 yield* options.session.note(`Building Lambda image ${imageUri}...`);
532 const pushed = yield* buildAndPushEcrImage(docker, {
533 imageUri,
534 context: build.context,
535 dockerfile: build.dockerfile,
536 platform: functionImagePlatform(options.architecture),
537 buildArgs: source.buildArgs,
538 args: ["--provenance=false"],
539 }).pipe(
540 Effect.as(true),
541 Effect.catch((buildError) =>
542 // Two reconcilers can both observe a missing tag and build it. With
543 // immutable tags, the second push loses the race. Accept that failure
544 // only when ECR proves the desired content-addressed tag now exists.
545 describeImage(repositoryName, imageTag).pipe(
546 Effect.flatMap((image) =>
547 image?.imageDigest !== undefined ? Effect.succeed(false) : Effect.fail(buildError),
548 ),
549 ),
550 ),
551 );
552 detail = yield* describeImage(repositoryName, imageTag).pipe(
553 Effect.filterOrFail(
554 (image) => image?.imageDigest !== undefined,
555 () =>
556 new FunctionImageNotReady({ message: `Image ${imageUri} not found in ECR after push` }),
557 ),
558 Effect.retry({
559 schedule: Schedule.spaced("1 second"),
560 times: 8,
561 }),
562 );
563 yield* options.session.note(
564 pushed ? `Pushed ${imageUri}` : `Reused concurrently pushed ${imageUri}`,
565 );
566 }
567
568 const digest = detail?.imageDigest;
569 if (digest === undefined) {
570 return yield* new FunctionImageUnexpectedResponse({
571 message: `Image ${imageUri} has no registry digest after push`,
572 });
573 }
574 return {
575 source: "build",
576 hash: imageTag,
577 imageUri,
578 resolvedImageUri: `${ensured.repositoryUri}@${digest}`,
579 digest,
580 repositoryName,
581 repositoryUri: ensured.repositoryUri,
582 ownsRepository: true,
583 } satisfies FunctionImageAttributes;
584 });
585
586 const deleteRepository = (repositoryName: string) =>
587 ecr
588 .deleteRepository({ repositoryName, force: true })
589 .pipe(Effect.catchTag("RepositoryNotFoundException", () => Effect.void));
590
591 const isReady = Effect.fn(function* (id: string, repositoryName: string, imageTag: string) {
592 const repository = yield* ecr.describeRepositories({ repositoryNames: [repositoryName] }).pipe(
593 Effect.map((response) => response.repositories?.[0]),
594 Effect.catchTag("RepositoryNotFoundException", () => Effect.undefined),
595 );
596 if (
597 repository?.repositoryArn === undefined ||
598 repository.repositoryUri === undefined ||
599 repository.imageTagMutability !== "IMMUTABLE"
600 ) {
601 return false;
602 }
603
604 const [tags, observedPolicy, image] = yield* Effect.all(
605 [
606 ecr
607 .listTagsForResource({ resourceArn: repository.repositoryArn })
608 .pipe(Effect.catchTag("RepositoryNotFoundException", () => Effect.undefined)),
609 ecr.getRepositoryPolicy({ repositoryName }).pipe(
610 Effect.map((response) => response.policyText),
611 Effect.catchTag(
612 ["RepositoryNotFoundException", "RepositoryPolicyNotFoundException"],
613 () => Effect.undefined,
614 ),
615 ),
616 describeImage(repositoryName, imageTag),
617 ] as const,
618 { concurrency: 3 },
619 );
620 if (tags === undefined || !(yield* hasAlchemyTags(id, tags.tags))) {
621 return false;
622 }
623
624 const desiredPolicy = yield* desiredRepositoryPolicy();
625 return (
626 observedPolicy !== undefined &&
627 normalizePolicyDocument(observedPolicy) === normalizePolicyDocument(desiredPolicy) &&
628 image?.imageDigest !== undefined
629 );
630 });
631
632 return { hash, identity, resolve, isReady, deleteRepository };
633 });
634
635 export interface FunctionImage extends Effect.Success<typeof makeFunctionImage> {}