@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 1,061 lines 35.3 KB
Raw
1 import { Credentials } from "@distilled.cloud/gcp/Credentials";
2 import * as cloudrun from "@distilled.cloud/gcp/run_v2";
3 import { Unowned } from "alchemy/AdoptPolicy";
4 import type * as Bundle from "alchemy/Bundle/Bundle";
5 import { isResolved } from "alchemy/Diff";
6 import { createPhysicalName } from "alchemy/PhysicalName";
7 import { Platform, type Main, type PlatformProps, type PlatformServices } from "alchemy/Platform";
8 import * as Provider from "alchemy/Provider";
9 import { Resource, type ResourceBinding } from "alchemy/Resource";
10 import {
11 createContainerRuntimeContext,
12 type HostRuntimeContext,
13 type ServerHost,
14 } from "alchemy/Runtime/Process";
15 import type { RuntimeContext } from "alchemy/RuntimeContext";
16 import { tagRecord } from "alchemy/Tags";
17 import * as Data from "effect/Data";
18 import * as Effect from "effect/Effect";
19 import * as Schedule from "effect/Schedule";
20 import type { Scope } from "effect/Scope";
21 import * as Stream from "effect/Stream";
22 import { destroyHostImageRepository, makeImageSource } from "../ArtifactRegistry/ImageSource.ts";
23 import { GcpEnvironment } from "../Environment.ts";
24 import { retryActAs, type AppliedIamGrant, type GcpHostBinding } from "../Host.ts";
25 import {
26 alchemyRuntimeEnv,
27 isManagedServiceAccount,
28 makeGcpBootstrap,
29 mergeContainerEnv,
30 releaseHostIdentity,
31 resolveHostIdentity,
32 } from "../HostRuntime.ts";
33 import {
34 createInternalLabels,
35 diffLabels,
36 hasAlchemyLabels,
37 stripInternalLabels,
38 toLabels,
39 } from "../Labels.ts";
40 import {
41 type LongRunningOperation,
42 waitForOperation as waitForLongRunningOperation,
43 } from "../Operation.ts";
44 import type { Providers } from "../Providers.ts";
45
46 const DEFAULT_IMAGE = "us-docker.pkg.dev/cloudrun/container/worker-pool";
47 const MAX_NAME_LENGTH = 49;
48
49 export type WorkerPoolEnvVar = {
50 /** Environment variable name. */
51 name?: string;
52 /** Literal value. Mutually exclusive with `valueSource`. */
53 value?: string;
54 /** Secret Manager source for the value. */
55 valueSource?: {
56 secretKeyRef?: {
57 secret?: string;
58 version?: string;
59 };
60 };
61 };
62
63 export type WorkerPoolContainer = {
64 /** DNS_LABEL container name. */
65 name?: string;
66 /**
67 * Container image (Artifact Registry, GCR, or Docker Hub). Required
68 * unless `template` is omitted, in which case the public Cloud Run
69 * worker-pool image is used.
70 */
71 image?: string;
72 /** Entrypoint. */
73 command?: string[];
74 /** Arguments to the entrypoint. */
75 args?: string[];
76 /** Environment variables. */
77 env?: WorkerPoolEnvVar[];
78 /** CPU / memory / GPU requirements. */
79 resources?: {
80 /** Resource limits. Keys: `cpu`, `memory`, `nvidia.com/gpu`. */
81 limits?: Record<string, string>;
82 /** Allocate CPU only during requests. */
83 cpuIdle?: boolean;
84 /** Boost CPU on startup to reduce cold starts. */
85 startupCpuBoost?: boolean;
86 };
87 /** Working directory. */
88 workingDir?: string;
89 /** Volume mounts. Names must match `volumes`. */
90 volumeMounts?: Array<{
91 name: string;
92 mountPath: string;
93 subPath?: string;
94 }>;
95 /** Containers that must start before this one. */
96 dependsOn?: string[];
97 };
98
99 export type WorkerPoolRevisionTemplate = {
100 /** Containers that make up the revision. */
101 containers?: WorkerPoolContainer[];
102 /** Runtime service account email. Defaults to the project Compute SA. */
103 serviceAccount?: string;
104 /** Revision labels. */
105 labels?: Record<string, string>;
106 /** Revision annotations. */
107 annotations?: Record<string, string>;
108 /** Direct VPC egress / connector. */
109 vpcAccess?: cloudrun.GoogleCloudRunV2VpcAccess;
110 /** Volumes available to containers. */
111 volumes?: cloudrun.GoogleCloudRunV2VolumeList;
112 /** CMEK used to encrypt the container image. */
113 encryptionKey?: string;
114 /** Unique revision name. Generated from the worker pool name if omitted. */
115 revision?: string;
116 /** Node selector (e.g. GPU accelerator). */
117 nodeSelector?: cloudrun.GoogleCloudRunV2NodeSelector;
118 /** True if GPU zonal redundancy is disabled. */
119 gpuZonalRedundancyDisabled?: boolean;
120 /** Service mesh connectivity. */
121 serviceMesh?: cloudrun.GoogleCloudRunV2ServiceMesh;
122 };
123
124 export type InstanceSplit = {
125 /**
126 * Allocation type (`INSTANCE_SPLIT_ALLOCATION_TYPE_LATEST` or
127 * `INSTANCE_SPLIT_ALLOCATION_TYPE_REVISION`).
128 */
129 type?: string;
130 /** Revision to assign instances to when allocating by revision. */
131 revision?: string;
132 /** Percent of instances (0–100). */
133 percent?: number;
134 };
135
136 export type WorkerPoolScaling = {
137 /** Total instances in manual scaling mode. */
138 manualInstanceCount?: number;
139 };
140
141 export type WorkerPoolBinaryAuthorization = {
142 /** Use the project's default Binary Authorization policy. */
143 useDefault?: boolean;
144 /** Breakglass justification. Requires `useDefault`. */
145 breakglassJustification?: string;
146 /** Policy path `projects/{project}/platforms/cloudRun/{policy}`. */
147 policy?: string;
148 };
149
150 export type WorkerPoolProps = PlatformProps & {
151 /**
152 * Worker pool id (the `{workerPool}` segment of
153 * `projects/{project}/locations/{location}/workerPools/{workerPool}`).
154 * If omitted, a unique name is generated from the stack, stage, and
155 * logical id. Must begin with a letter, not end with a hyphen, and be
156 * fewer than 50 characters. Immutable — changing it replaces the pool.
157 */
158 workerPoolId?: string;
159 /**
160 * Region (`us-central1`, `europe-west1`, …). Immutable — changing it
161 * replaces the pool. `US-CENTRAL1` is accepted and normalized to
162 * `us-central1`.
163 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
164 */
165 location?: string;
166 /**
167 * User labels. Alchemy ownership labels are merged in automatically.
168 */
169 labels?: Record<string, string>;
170 /**
171 * Unstructured annotations. Cloud Run rejects `run.googleapis.com` /
172 * `cloud.googleapis.com` / Knative namespaces.
173 */
174 annotations?: Record<string, string>;
175 /**
176 * Human-readable description (max 512 characters).
177 */
178 description?: string;
179 /**
180 * Launch stage (`GA`, `BETA`, `ALPHA`). Defaults to GA.
181 */
182 launchStage?: string;
183 /**
184 * Binary Authorization settings.
185 */
186 binaryAuthorization?: WorkerPoolBinaryAuthorization;
187 /**
188 * Instance split. Empty / omitted sends 100% to the latest Ready
189 * revision.
190 */
191 instanceSplits?: InstanceSplit[];
192 /**
193 * Worker-pool-level scaling (manual instance count).
194 */
195 scaling?: WorkerPoolScaling;
196 /**
197 * Revision template. If omitted, a single container runs the public
198 * Cloud Run worker-pool image. The Effect-native `main` form fills
199 * `image` for you.
200 */
201 template?: WorkerPoolRevisionTemplate;
202 /**
203 * Module entrypoint for an Effect-native worker pool (typically
204 * `import.meta.url`). Alchemy bundles the program, builds a container,
205 * and deploys the pool. Bindings attach env + IAM onto the runtime SA.
206 */
207 main?: string;
208 /**
209 * Named export to load from `main`.
210 * @default "default"
211 */
212 handler?: string;
213 /**
214 * Additional environment variables for the Effect-native container.
215 */
216 env?: Record<string, any>;
217 /**
218 * Bundler configuration for `main`.
219 */
220 build?: Bundle.BundleConfig;
221 };
222
223 export type WorkerPool = Resource<
224 "GCP.Run.WorkerPool",
225 WorkerPoolProps,
226 {
227 /** Full resource name `projects/{project}/locations/{location}/workerPools/{workerPool}`. */
228 name: string;
229 /** Worker pool id (last path segment). */
230 workerPoolId: string;
231 /** Project id. */
232 project: string;
233 /** Region (`us-central1`, …). */
234 location: string;
235 /** User labels (Alchemy ownership labels stripped). */
236 labels: Record<string, string>;
237 /** User annotations. */
238 annotations: Record<string, string>;
239 /** Description. */
240 description: string | undefined;
241 /** Server-assigned UUID. */
242 uid: string | undefined;
243 /** Launch stage. */
244 launchStage: string | undefined;
245 /** Manual instance count. */
246 manualInstanceCount: number | undefined;
247 /** Latest created revision name. */
248 latestCreatedRevision: string | undefined;
249 /** Latest Ready revision name. */
250 latestReadyRevision: string | undefined;
251 /** Terminal condition state (`CONDITION_SUCCEEDED`, …). */
252 terminalConditionState: string | undefined;
253 /** True while Cloud Run is still reconciling the desired state. */
254 reconciling: boolean;
255 /** Observed generation. */
256 generation: string | undefined;
257 /** Image of the first container. */
258 image: string | undefined;
259 /** RFC3339 creation timestamp. */
260 createTime: string | undefined;
261 /** RFC3339 last-update timestamp. */
262 updateTime: string | undefined;
263 /** Runtime service account email. */
264 serviceAccount: string | undefined;
265 /**
266 * Numeric unique ID of the runtime service account: the `sub` of the
267 * Google ID tokens the host fetches. An AWS binding's IAM role trusts
268 * it, so the host reaches AWS without an access key.
269 */
270 serviceAccountUniqueId: string | undefined;
271 /** True when Alchemy minted the per-host runtime service account. */
272 managedServiceAccount: boolean;
273 /** IAM roles bindings granted to the runtime service account. */
274 iamGrants: AppliedIamGrant[];
275 /** Hash of the bundled `main` program and its bootstrap (Effect-native only). */
276 codeHash: string | undefined;
277 },
278 GcpHostBinding,
279 Providers
280 >;
281
282 export type WorkerPoolRuntimeContext = HostRuntimeContext;
283 export type WorkerPoolServices = Credentials | GcpEnvironment | ServerHost;
284 /**
285 * Effect-native Worker Pool shape: a `run` entry that executes when
286 * the container starts, and/or a `fetch` HTTP handler.
287 */
288 export type WorkerPoolShape =
289 | void
290 | (Exclude<Main<WorkerPoolServices>, void> & {
291 run?: Effect.Effect<
292 void,
293 never,
294 WorkerPoolServices | PlatformServices | RuntimeContext | Scope
295 >;
296 });
297
298 /**
299 * A Cloud Run worker pool (pull-based revision + instance split).
300 *
301 * Changing `workerPoolId` or `location` replaces the pool. Updates to
302 * `template` create a new revision.
303 *
304 * ### Creating a Worker Pool
305 * **Example:** Generated name, default worker image
306 * ```typescript
307 * const pool = yield* GCP.Run.WorkerPool("workers", {});
308 * ```
309 *
310 * **Example:** Explicit id, image, env, and labels
311 * ```typescript
312 * const pool = yield* GCP.Run.WorkerPool("workers", {
313 * workerPoolId: "order-workers",
314 * location: "us-central1",
315 * description: "order pull workers",
316 * labels: { env: "prod" },
317 * scaling: { manualInstanceCount: 1 },
318 * template: {
319 * containers: [
320 * {
321 * image: "us-docker.pkg.dev/cloudrun/container/worker-pool",
322 * env: [{ name: "ENV", value: "prod" }],
323 * },
324 * ],
325 * },
326 * });
327 * ```
328 *
329 * ### Reading a Worker Pool
330 * **Example:** Get the live worker pool
331 * ```typescript
332 * const getWorkerPool = yield* GCP.Run.GetWorkerPool(pool);
333 * const live = yield* getWorkerPool();
334 * ```
335 *
336 * ### Effect-native Worker Pool with bindings
337 * **Example:** Pull workers with Memorystore
338 * ```typescript
339 * export class Workers extends GCP.Run.WorkerPool<Workers>()(
340 * "Workers",
341 * { main: import.meta.url, scaling: { manualInstanceCount: 1 } },
342 * Effect.gen(function* () {
343 * const redis = yield* GCP.Redis.ReadWriteRedis(cache);
344 * return {
345 * run: redis.set("worker", "up"),
346 * };
347 * }).pipe(Effect.provide(GCP.Redis.ReadWriteRedisHttp)),
348 * ) {}
349 * ```
350 *
351 * @resource
352 * @category Run
353 */
354 export const WorkerPool: Platform<
355 WorkerPool,
356 WorkerPoolServices,
357 WorkerPoolShape,
358 WorkerPoolRuntimeContext
359 > = Platform("GCP.Run.WorkerPool", {
360 createRuntimeContext: createContainerRuntimeContext("GCP.Run.WorkerPool") as (
361 id: string,
362 ) => WorkerPoolRuntimeContext,
363 });
364
365 export class WorkerPoolNotResolved extends Data.TaggedError("GCP.Run.WorkerPoolNotResolved")<{
366 resourceName: string;
367 }> {}
368
369 export class WorkerPoolNotReady extends Data.TaggedError("GCP.Run.WorkerPoolNotReady")<{
370 resourceName: string;
371 state: string;
372 message: string;
373 }> {}
374
375 export class WorkerPoolReconciling extends Data.TaggedError("GCP.Run.WorkerPoolReconciling")<{
376 resourceName: string;
377 state: string;
378 }> {}
379
380 export class WorkerPoolStillExists extends Data.TaggedError("GCP.Run.WorkerPoolStillExists")<{
381 resourceName: string;
382 }> {}
383
384 const lastSegment = (value: string) => {
385 const trimmed = value.replace(/\/+$/, "");
386 const parts = trimmed.split("/");
387 return parts[parts.length - 1] || trimmed;
388 };
389
390 const normalizeLocation = (location: string | undefined, defaultLocation: string) =>
391 lastSegment(location ?? defaultLocation).toLowerCase();
392
393 const resourceName = (project: string, location: string, workerPoolId: string) =>
394 `projects/${project}/locations/${location}/workerPools/${workerPoolId}`;
395
396 const parseName = (name: string, defaultLocation: string) => {
397 const parts = name.split("/").filter((part) => part.length > 0);
398 const poolsAt = parts.lastIndexOf("workerPools");
399 const locationsAt = parts.lastIndexOf("locations");
400 const projectsAt = parts.lastIndexOf("projects");
401 return {
402 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
403 location:
404 locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : defaultLocation,
405 workerPoolId: poolsAt >= 0 && parts[poolsAt + 1] ? parts[poolsAt + 1]! : lastSegment(name),
406 };
407 };
408
409 const userLabels = (
410 labels: Record<string, string | undefined> | null | undefined,
411 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
412
413 const userAnnotations = (
414 annotations: Record<string, string | undefined> | null | undefined,
415 ): Record<string, string> => tagRecord(annotations);
416
417 const recordsEqual = (left: Record<string, string>, right: Record<string, string>) => {
418 const leftKeys = Object.keys(left).sort();
419 const rightKeys = Object.keys(right).sort();
420 return (
421 leftKeys.length === rightKeys.length &&
422 leftKeys.every((key, index) => key === rightKeys[index] && left[key] === right[key])
423 );
424 };
425
426 const rfc1035 = (name: string): string => {
427 let next = name
428 .toLowerCase()
429 .replace(/[^a-z0-9-]/g, "-")
430 .replace(/-+/g, "-")
431 .replace(/^-+|-+$/g, "");
432 if (!/^[a-z]/.test(next)) next = `w${next}`;
433 next = next.slice(0, MAX_NAME_LENGTH).replace(/-+$/g, "");
434 return next.length > 0 ? next : "workerpool";
435 };
436
437 const toId = (id: string, workerPoolId: string | undefined, existing?: string) =>
438 Effect.gen(function* () {
439 if (workerPoolId !== undefined) return workerPoolId;
440 if (existing !== undefined) return existing;
441 return rfc1035(
442 yield* createPhysicalName({
443 id,
444 maxLength: MAX_NAME_LENGTH,
445 lowercase: true,
446 }),
447 );
448 });
449
450 const desiredTemplate = (
451 news: WorkerPoolProps,
452 ): cloudrun.GoogleCloudRunV2WorkerPoolRevisionTemplate => {
453 const template = news.template ?? {};
454 return {
455 ...template,
456 containers: template.containers ?? [{ image: DEFAULT_IMAGE }],
457 };
458 };
459
460 const envFingerprint = (env: cloudrun.GoogleCloudRunV2EnvVarList | undefined) =>
461 JSON.stringify(
462 [...(env ?? [])]
463 .map((item) => ({
464 name: item.name ?? "",
465 value: item.value ?? "",
466 secret: item.valueSource?.secretKeyRef?.secret ?? "",
467 version: item.valueSource?.secretKeyRef?.version ?? "",
468 }))
469 .sort((left, right) => left.name.localeCompare(right.name)),
470 );
471
472 const containerNeedsSync = (
473 desired: cloudrun.GoogleCloudRunV2ContainerList | undefined,
474 observed: cloudrun.GoogleCloudRunV2ContainerList | undefined,
475 ) => {
476 const want = desired ?? [];
477 const have = observed ?? [];
478 if (want.length !== have.length) return true;
479 return want.some((container, index) => {
480 const current = have[index];
481 if (current === undefined) return true;
482 if ((container.image ?? "") !== (current.image ?? "")) return true;
483 if (
484 container.command !== undefined &&
485 JSON.stringify(container.command) !== JSON.stringify(current.command ?? [])
486 ) {
487 return true;
488 }
489 if (
490 container.args !== undefined &&
491 JSON.stringify(container.args) !== JSON.stringify(current.args ?? [])
492 ) {
493 return true;
494 }
495 if (container.workingDir !== undefined && container.workingDir !== (current.workingDir ?? "")) {
496 return true;
497 }
498 if (
499 container.env !== undefined &&
500 envFingerprint(container.env) !== envFingerprint(current.env)
501 ) {
502 return true;
503 }
504 if (container.resources?.limits !== undefined) {
505 const wantLimits = tagRecord(container.resources.limits);
506 const haveLimits = tagRecord(current.resources?.limits);
507 for (const [key, value] of Object.entries(wantLimits)) {
508 if (haveLimits[key] !== value) return true;
509 }
510 }
511 if (
512 container.resources?.cpuIdle !== undefined &&
513 container.resources.cpuIdle !== current.resources?.cpuIdle
514 ) {
515 return true;
516 }
517 if (
518 container.resources?.startupCpuBoost !== undefined &&
519 container.resources.startupCpuBoost !== current.resources?.startupCpuBoost
520 ) {
521 return true;
522 }
523 if (container.name !== undefined && container.name !== (current.name ?? "")) {
524 return true;
525 }
526 if (
527 container.volumeMounts !== undefined &&
528 JSON.stringify(container.volumeMounts) !== JSON.stringify(current.volumeMounts ?? [])
529 ) {
530 return true;
531 }
532 if (
533 container.dependsOn !== undefined &&
534 JSON.stringify(container.dependsOn) !== JSON.stringify(current.dependsOn ?? [])
535 ) {
536 return true;
537 }
538 return false;
539 });
540 };
541
542 const scalingFingerprint = (scaling: WorkerPoolScaling | undefined) =>
543 JSON.stringify({
544 manual: scaling?.manualInstanceCount ?? null,
545 });
546
547 const instanceSplitFingerprint = (splits: InstanceSplit[] | undefined) =>
548 JSON.stringify(
549 (splits ?? []).map((split) => ({
550 type: split.type ?? "",
551 revision: split.revision ?? "",
552 percent: split.percent ?? 0,
553 })),
554 );
555
556 const stable = (value: unknown): string =>
557 JSON.stringify(value, (_key, current) => {
558 if (current && typeof current === "object" && !Array.isArray(current)) {
559 return Object.fromEntries(
560 Object.entries(current as Record<string, unknown>)
561 .filter(([, item]) => item !== undefined)
562 .sort(([a], [b]) => a.localeCompare(b)),
563 );
564 }
565 return current;
566 });
567
568 const templateNeedsSync = (
569 desired: cloudrun.GoogleCloudRunV2WorkerPoolRevisionTemplate,
570 observed: cloudrun.GoogleCloudRunV2WorkerPoolRevisionTemplate | undefined,
571 ) => {
572 const current = observed ?? {};
573 if (containerNeedsSync(desired.containers, current.containers)) {
574 return true;
575 }
576 if (
577 desired.serviceAccount !== undefined &&
578 desired.serviceAccount !== (current.serviceAccount ?? "")
579 ) {
580 return true;
581 }
582 if (
583 desired.encryptionKey !== undefined &&
584 desired.encryptionKey !== (current.encryptionKey ?? "")
585 ) {
586 return true;
587 }
588 if (desired.revision !== undefined && desired.revision !== (current.revision ?? "")) {
589 return true;
590 }
591 if (
592 desired.gpuZonalRedundancyDisabled !== undefined &&
593 desired.gpuZonalRedundancyDisabled !== (current.gpuZonalRedundancyDisabled === true)
594 ) {
595 return true;
596 }
597 if (
598 desired.vpcAccess !== undefined &&
599 stable(desired.vpcAccess) !== stable(current.vpcAccess ?? {})
600 ) {
601 return true;
602 }
603 if (desired.volumes !== undefined && stable(desired.volumes) !== stable(current.volumes ?? [])) {
604 return true;
605 }
606 if (
607 desired.nodeSelector !== undefined &&
608 stable(desired.nodeSelector) !== stable(current.nodeSelector ?? {})
609 ) {
610 return true;
611 }
612 if (
613 desired.serviceMesh !== undefined &&
614 stable(desired.serviceMesh) !== stable(current.serviceMesh ?? {})
615 ) {
616 return true;
617 }
618 if (
619 desired.labels !== undefined &&
620 !recordsEqual(userAnnotations(desired.labels), userAnnotations(current.labels))
621 ) {
622 return true;
623 }
624 if (
625 desired.annotations !== undefined &&
626 !recordsEqual(userAnnotations(desired.annotations), userAnnotations(current.annotations))
627 ) {
628 return true;
629 }
630 return false;
631 };
632
633 const HOST_TYPE = "GCP.Run.WorkerPool";
634
635 // Worker pools have no ingress, so they run the one-shot/loop bootstrap
636 // (no HTTP server); a long-running `run` keeps the instance alive.
637 const bootstrapFor = (news: WorkerPoolProps) =>
638 makeGcpBootstrap("CloudRunJob", news.handler ?? "default");
639
640 const toAttrs = (
641 pool: cloudrun.GoogleCloudRunV2WorkerPool,
642 project: string,
643 region: string,
644 extras: {
645 iamGrants?: AppliedIamGrant[];
646 codeHash?: string;
647 serviceAccountUniqueId?: string;
648 } = {},
649 ): WorkerPool["Attributes"] => {
650 const name = pool.name ?? "";
651 const parsed = parseName(name, region);
652 return {
653 name,
654 workerPoolId: parsed.workerPoolId,
655 project: parsed.project || project,
656 location: parsed.location,
657 labels: userLabels(pool.labels),
658 annotations: userAnnotations(pool.annotations),
659 description: pool.description,
660 uid: pool.uid,
661 launchStage: pool.launchStage,
662 manualInstanceCount: pool.scaling?.manualInstanceCount,
663 latestCreatedRevision: pool.latestCreatedRevision,
664 latestReadyRevision: pool.latestReadyRevision,
665 terminalConditionState: pool.terminalCondition?.state,
666 reconciling: pool.reconciling === true,
667 generation: pool.generation,
668 image: pool.template?.containers?.[0]?.image,
669 createTime: pool.createTime,
670 updateTime: pool.updateTime,
671 serviceAccount: pool.template?.serviceAccount,
672 managedServiceAccount: isManagedServiceAccount({
673 project: parsed.project || project,
674 hostType: HOST_TYPE,
675 resourceName: name,
676 serviceAccount: pool.template?.serviceAccount,
677 }),
678 serviceAccountUniqueId: extras.serviceAccountUniqueId,
679 iamGrants: extras.iamGrants ?? [],
680 codeHash: extras.codeHash,
681 };
682 };
683
684 const getByName = (name: string) =>
685 cloudrun
686 .getProjectsLocationsWorkerPools({ name })
687 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
688
689 /** Waits on a Cloud Run long-running operation (revisions roll out in minutes). */
690 const waitForOperation = (
691 operation: cloudrun.GoogleLongrunningOperation,
692 options?: { notFoundOk?: boolean },
693 ) =>
694 waitForLongRunningOperation(
695 operation,
696 (name) => {
697 const get = cloudrun.getProjectsLocationsOperations({ name });
698 return options?.notFoundOk === true
699 ? get.pipe(
700 Effect.catchTag("NotFound", () =>
701 Effect.succeed<LongRunningOperation>({ name, done: true }),
702 ),
703 )
704 : get.pipe(
705 // A just-returned operation can briefly 404 on read.
706 Effect.retry({
707 while: (error) => error._tag === "NotFound",
708 times: 5,
709 schedule: Schedule.exponential("250 millis"),
710 }),
711 );
712 },
713 { budget: "10 minutes" },
714 ).pipe(
715 // google.rpc.Code NOT_FOUND: the resource was already gone.
716 Effect.catchTag("GCP.OperationFailed", (error) =>
717 options?.notFoundOk === true && error.code === 5
718 ? Effect.succeed<LongRunningOperation>(operation)
719 : Effect.fail(error),
720 ),
721 );
722
723 const isPendingPool = (pool: cloudrun.GoogleCloudRunV2WorkerPool) => {
724 const state = pool.terminalCondition?.state ?? "";
725 return (
726 pool.reconciling === true ||
727 state === "CONDITION_PENDING" ||
728 state === "CONDITION_RECONCILING" ||
729 state === "" ||
730 state === "STATE_UNSPECIFIED"
731 );
732 };
733
734 const waitUntilReady = (name: string) =>
735 getByName(name).pipe(
736 Effect.filterOrFail(
737 (pool): pool is cloudrun.GoogleCloudRunV2WorkerPool =>
738 pool !== undefined && pool.deleteTime === undefined,
739 () => new WorkerPoolNotResolved({ resourceName: name }),
740 ),
741 Effect.filterOrFail(
742 (pool) => (pool.terminalCondition?.state ?? "") !== "CONDITION_FAILED",
743 (pool) =>
744 new WorkerPoolNotReady({
745 resourceName: name,
746 state: pool.terminalCondition?.state ?? "",
747 message: pool.terminalCondition?.message ?? "revision failed",
748 }),
749 ),
750 Effect.filterOrFail(
751 (pool) => !isPendingPool(pool),
752 (pool) =>
753 new WorkerPoolReconciling({
754 resourceName: name,
755 state: pool.terminalCondition?.state || "reconciling",
756 }),
757 ),
758 Effect.retry({
759 while: (error) =>
760 error._tag === "GCP.Run.WorkerPoolReconciling" ||
761 error._tag === "GCP.Run.WorkerPoolNotResolved",
762 times: 10,
763 schedule: Schedule.spaced("4 seconds"),
764 }),
765 );
766
767 const waitUntilGone = (name: string) =>
768 getByName(name).pipe(
769 Effect.flatMap((pool) =>
770 pool === undefined
771 ? Effect.void
772 : Effect.fail(new WorkerPoolStillExists({ resourceName: name })),
773 ),
774 Effect.retry({
775 while: (error) => error._tag === "GCP.Run.WorkerPoolStillExists",
776 times: 10,
777 schedule: Schedule.spaced("2 seconds"),
778 }),
779 );
780
781 const toCreateBody = (
782 news: WorkerPoolProps,
783 labels: Record<string, string>,
784 template: cloudrun.GoogleCloudRunV2WorkerPoolRevisionTemplate,
785 ): cloudrun.GoogleCloudRunV2WorkerPool => ({
786 labels,
787 annotations: news.annotations,
788 description: news.description,
789 launchStage: news.launchStage,
790 binaryAuthorization: news.binaryAuthorization,
791 instanceSplits: news.instanceSplits,
792 scaling: news.scaling,
793 template,
794 });
795
796 const listAt = (project: string, location: string, region: string) =>
797 cloudrun.listProjectsLocationsWorkerPools
798 .pages({
799 parent: `projects/${project}/locations/${location}`,
800 pageSize: 1000,
801 })
802 .pipe(
803 Stream.flatMap((page) => Stream.fromIterable(page.workerPools ?? [])),
804 Stream.filter(
805 (pool) =>
806 pool.deleteTime === undefined &&
807 Object.keys(pool.labels ?? {}).some((key) => key.startsWith("alchemy-")),
808 ),
809 Stream.map((pool) => toAttrs(pool, project, region)),
810 Stream.runCollect,
811 Effect.map((chunk) => Array.from(chunk)),
812 );
813
814 export const WorkerPoolProvider = () =>
815 Provider.succeed(WorkerPool, {
816 stables: ["name", "workerPoolId", "project", "location", "uid", "createTime"],
817
818 diff: Effect.fn(function* ({ news, olds, output }) {
819 if (!isResolved(news)) return undefined;
820 const env = yield* GcpEnvironment.current;
821 const previousId = olds?.workerPoolId ?? output?.workerPoolId;
822 const nextId = news.workerPoolId ?? previousId;
823 const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region);
824 const nextLocation = normalizeLocation(news.location ?? output?.location, env.region);
825 const idChanged = previousId !== undefined && nextId !== undefined && nextId !== previousId;
826 const locationChanged = previousLocation !== nextLocation;
827 if (idChanged || locationChanged) {
828 return { action: "replace" as const, deleteFirst: false };
829 }
830 // A code-only change leaves the props untouched; hash the bundled
831 // program (and bootstrap) so it still surfaces as an update.
832 if (output !== undefined && news.main !== undefined) {
833 const images = yield* makeImageSource;
834 const hash = yield* images.hash({
835 source: { main: news.main, handler: news.handler, build: news.build },
836 isExternal: news.isExternal,
837 bootstrap: bootstrapFor(news),
838 });
839 if (hash !== undefined && hash !== output.codeHash) {
840 return { action: "update" as const };
841 }
842 }
843 return undefined;
844 }),
845
846 read: Effect.fn(function* ({ id, olds, output }) {
847 const env = yield* GcpEnvironment.current;
848 const workerPoolId = yield* toId(id, olds?.workerPoolId, output?.workerPoolId);
849 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
850 const name = output?.name ?? resourceName(env.project, location, workerPoolId);
851 const existing = yield* getByName(name);
852 if (existing === undefined || existing.deleteTime !== undefined) {
853 return undefined;
854 }
855 const attrs = toAttrs(existing, env.project, env.region, {
856 iamGrants: output?.iamGrants,
857 codeHash: output?.codeHash,
858 serviceAccountUniqueId: output?.serviceAccountUniqueId,
859 });
860 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
861 }),
862
863 list: () =>
864 Effect.gen(function* () {
865 const env = yield* GcpEnvironment.current;
866 // WorkerPools list rejects the `-` wildcard; Services/Jobs accept it.
867 return yield* listAt(env.project, "-", env.region).pipe(
868 Effect.catchTag("LocationWildcardUnsupported", () =>
869 // `us-central1` was the default before `GCP.Region`; keep
870 // listing it so older pools are still found.
871 Effect.forEach([...new Set([env.region, "us-central1"])], (location) =>
872 listAt(env.project, location, env.region),
873 ).pipe(Effect.map((groups) => groups.flat())),
874 ),
875 );
876 }),
877
878 reconcile: Effect.fn(function* ({ id, news, output, bindings, session }) {
879 const env = yield* GcpEnvironment.current;
880 const workerPoolId = yield* toId(id, news.workerPoolId, output?.workerPoolId);
881 const location = normalizeLocation(news.location ?? output?.location, env.region);
882 const name = resourceName(env.project, location, workerPoolId);
883 const parent = `projects/${env.project}/locations/${location}`;
884 const desiredLabels = {
885 ...toLabels(news.labels),
886 ...(yield* createInternalLabels(id)),
887 };
888 const desiredAnnotations = news.annotations;
889 const template = desiredTemplate(news);
890 const identity = yield* resolveHostIdentity({
891 project: env.project,
892 hostType: HOST_TYPE,
893 resourceName: name,
894 userServiceAccount: template.serviceAccount,
895 effectNative: news.main !== undefined,
896 bindings: bindings as ResourceBinding<GcpHostBinding>[],
897 output,
898 });
899 template.serviceAccount = identity.serviceAccount;
900 let codeHash: string | undefined;
901 if (news.main !== undefined) {
902 const images = yield* makeImageSource;
903 const image = yield* images
904 .resolve({
905 id,
906 source: {
907 main: news.main,
908 handler: news.handler ?? "default",
909 build: news.build,
910 },
911 repositoryName: rfc1035(`${workerPoolId}-src`),
912 location,
913 isExternal: news.isExternal,
914 bootstrap: bootstrapFor(news),
915 session,
916 })
917 .pipe(Effect.onError(() => identity.cleanup));
918 codeHash = image.codeHash;
919 const container = template.containers?.[0] ?? {};
920 template.containers = [
921 {
922 ...container,
923 image: image.imageUri,
924 env: mergeContainerEnv(container.env, identity.env, yield* alchemyRuntimeEnv, news.env),
925 },
926 ];
927 } else {
928 const runtimeEnv = { ...identity.env, ...news.env };
929 if (Object.keys(runtimeEnv).length > 0 && template.containers) {
930 template.containers = template.containers.map((container, index) =>
931 index === 0
932 ? {
933 ...container,
934 env: mergeContainerEnv(container.env, runtimeEnv),
935 }
936 : container,
937 );
938 }
939 }
940
941 let current = yield* getByName(name);
942 if (current?.deleteTime !== undefined) {
943 yield* waitUntilGone(name);
944 current = undefined;
945 }
946
947 if (current === undefined) {
948 const created = yield* cloudrun
949 .createProjectsLocationsWorkerPools({
950 parent,
951 workerPoolId,
952 body: toCreateBody(news, desiredLabels, template),
953 })
954 .pipe(
955 retryActAs,
956 Effect.catchTag("Conflict", () => Effect.undefined),
957 Effect.onError(() => identity.cleanup),
958 );
959 if (created !== undefined) {
960 yield* waitForOperation(created);
961 }
962 current = yield* waitUntilReady(name);
963 }
964
965 if (current === undefined) {
966 return yield* new WorkerPoolNotResolved({ resourceName: name });
967 }
968
969 const observedLabels = tagRecord(current.labels);
970 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
971 const labelsChanged = upsert.length > 0 || removed.length > 0;
972 const descriptionChanged = (current.description ?? "") !== (news.description ?? "");
973 const annotationsChanged =
974 desiredAnnotations !== undefined &&
975 !recordsEqual(userAnnotations(current.annotations), userAnnotations(desiredAnnotations));
976 const launchStageChanged =
977 news.launchStage !== undefined && (current.launchStage ?? "GA") !== news.launchStage;
978 const binaryAuthorizationChanged =
979 news.binaryAuthorization !== undefined &&
980 stable(news.binaryAuthorization) !== stable(current.binaryAuthorization);
981 const scalingChanged =
982 news.scaling !== undefined &&
983 scalingFingerprint(current.scaling) !== scalingFingerprint(news.scaling);
984 const instanceSplitsChanged =
985 news.instanceSplits !== undefined &&
986 instanceSplitFingerprint(current.instanceSplits) !==
987 instanceSplitFingerprint(news.instanceSplits);
988 const templateChanged =
989 news.template !== undefined && templateNeedsSync(template, current.template);
990
991 if (
992 labelsChanged ||
993 descriptionChanged ||
994 annotationsChanged ||
995 launchStageChanged ||
996 binaryAuthorizationChanged ||
997 scalingChanged ||
998 instanceSplitsChanged ||
999 templateChanged
1000 ) {
1001 const updateMask = [
1002 labelsChanged ? "labels" : undefined,
1003 descriptionChanged ? "description" : undefined,
1004 annotationsChanged ? "annotations" : undefined,
1005 launchStageChanged ? "launchStage" : undefined,
1006 binaryAuthorizationChanged ? "binaryAuthorization" : undefined,
1007 scalingChanged ? "scaling" : undefined,
1008 instanceSplitsChanged ? "instanceSplits" : undefined,
1009 templateChanged ? "template" : undefined,
1010 ].filter((field): field is string => field !== undefined);
1011
1012 const patched = yield* cloudrun.patchProjectsLocationsWorkerPools({
1013 name,
1014 updateMask: updateMask.join(","),
1015 body: {
1016 name,
1017 labels: desiredLabels,
1018 description: news.description,
1019 annotations: desiredAnnotations,
1020 launchStage: news.launchStage,
1021 binaryAuthorization: news.binaryAuthorization,
1022 scaling: news.scaling,
1023 instanceSplits: news.instanceSplits,
1024 template,
1025 },
1026 });
1027 yield* waitForOperation(patched);
1028 current = yield* waitUntilReady(name);
1029 }
1030
1031 if (current === undefined) {
1032 return yield* new WorkerPoolNotResolved({ resourceName: name });
1033 }
1034
1035 return toAttrs(current, env.project, env.region, {
1036 iamGrants: identity.grants,
1037 codeHash,
1038 serviceAccountUniqueId: identity.uniqueId,
1039 });
1040 }),
1041
1042 delete: Effect.fn(function* ({ id, output }) {
1043 const operation = yield* cloudrun
1044 .deleteProjectsLocationsWorkerPools({ name: output.name })
1045 .pipe(
1046 Effect.retry({
1047 while: (error) => error._tag === "Conflict",
1048 times: 8,
1049 schedule: Schedule.spaced("2 seconds"),
1050 }),
1051 Effect.catchTag("NotFound", () => Effect.undefined),
1052 );
1053 if (operation !== undefined) {
1054 yield* waitForOperation(operation, { notFoundOk: true });
1055 }
1056 yield* waitUntilGone(output.name);
1057 // Effect-native hosts build into a per-host repository on reconcile.
1058 yield* destroyHostImageRepository(id, output, rfc1035(`${output.workerPoolId}-src`));
1059 yield* releaseHostIdentity(output);
1060 }),
1061 });