@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 481 lines 16.8 KB
Raw
1 import * as aiplatform from "@distilled.cloud/gcp/aiplatform_v1";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import { tagRecord } from "alchemy/Tags";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import { GcpEnvironment } from "../Environment.ts";
11 import { createInternalLabels, hasAlchemyLabels, toLabels } from "../Labels.ts";
12 import type { Providers } from "../Providers.ts";
13 import { listLocations } from "./names.ts";
14 import { resourceNameFromOperation, waitForOperation } from "./operations.ts";
15 import {
16 AiPlatformNotResolved,
17 AiPlatformStillExists,
18 collectPages,
19 jsonEqual,
20 locationParent,
21 normalizeLocation,
22 parseResourceName,
23 toPhysicalId,
24 userLabels,
25 type DiskSpec,
26 type EncryptionSpec,
27 type MachineSpec,
28 } from "./shared.ts";
29
30 const COLLECTION = "persistentResources";
31 const DEFAULT_MACHINE_TYPE = "n1-standard-4";
32 const DEFAULT_REPLICA_COUNT = "1";
33
34 export type ResourcePoolAutoscalingSpec = {
35 /** Minimum replicas (must be > 0 for Persistent Resource). */
36 minReplicaCount?: string;
37 /** Maximum replicas. */
38 maxReplicaCount?: string;
39 };
40
41 export type ResourcePool = {
42 /**
43 * Unique id within the PersistentResource. Generated if omitted.
44 */
45 id?: string;
46 /** Machine spec. Immutable. */
47 machineSpec?: MachineSpec;
48 /** Replica count (string). @default "1" */
49 replicaCount?: string;
50 /** Disk spec. */
51 diskSpec?: DiskSpec;
52 /** Autoscaling spec. */
53 autoscalingSpec?: ResourcePoolAutoscalingSpec;
54 };
55
56 export type RaySpec = {
57 /** Default Ray image URI. */
58 imageUri?: string;
59 /** Per-pool Ray images keyed by resource pool id. */
60 resourcePoolImages?: Record<string, string>;
61 /** Resource pool that serves as the Ray head node. */
62 headNodeResourcePoolId?: string;
63 /** Disable Ray OSS log export. */
64 rayLogsDisabled?: boolean;
65 /** Disable Ray metrics. */
66 rayMetricsDisabled?: boolean;
67 };
68
69 export type ServiceAccountSpec = {
70 /** Enforce a custom service account for workloads. */
71 enableCustomServiceAccount?: boolean;
72 /** Service account email. */
73 serviceAccount?: string;
74 };
75
76 export type PersistentResourceProps = {
77 /**
78 * Persistent resource id. If omitted, a unique RFC1035 name is
79 * generated. Must match `/^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$/`.
80 * Immutable — changing it replaces the resource.
81 */
82 persistentResourceId?: string;
83 /**
84 * Vertex AI location. Immutable — changing it replaces the resource.
85 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
86 */
87 location?: string;
88 /**
89 * Display name (max 128 UTF-8 characters). Defaults to the resource id.
90 * Changing it replaces the resource.
91 */
92 displayName?: string;
93 /**
94 * User labels. Alchemy ownership labels are merged in automatically.
95 * Changing them replaces the resource.
96 */
97 labels?: Record<string, string>;
98 /**
99 * Resource pools. At least one pool is required. Pool machine specs
100 * are immutable — changing them replaces the resource. Replica counts
101 * update in place only for Ray clusters (`raySpec`); GCP rejects updates
102 * to other persistent resources, so a replica change replaces them.
103 */
104 resourcePools?: ResourcePool[];
105 /**
106 * VPC network to peer (`projects/{project}/global/networks/{network}`).
107 * Immutable.
108 */
109 network?: string;
110 /**
111 * Reserved IP ranges under the VPC.
112 */
113 reservedIpRanges?: string[];
114 /**
115 * Customer-managed encryption key. Immutable.
116 */
117 encryptionSpec?: EncryptionSpec;
118 /**
119 * Ray cluster configuration.
120 */
121 raySpec?: RaySpec;
122 /**
123 * Workload identity / custom service account.
124 */
125 serviceAccountSpec?: ServiceAccountSpec;
126 };
127
128 export type PersistentResource = Resource<
129 "GCP.AIPlatform.PersistentResource",
130 PersistentResourceProps,
131 {
132 /** Full resource name. */
133 name: string;
134 /** Persistent resource id (last path segment). */
135 persistentResourceId: string;
136 /** Project id. */
137 project: string;
138 /** Location id. */
139 location: string;
140 /** Display name. */
141 displayName: string | undefined;
142 /** User labels (Alchemy ownership labels stripped). */
143 labels: Record<string, string>;
144 /** Server-reported state (`PROVISIONING`, `RUNNING`, …). */
145 state: string | undefined;
146 /** VPC network. */
147 network: string | undefined;
148 /** Resource pool ids. */
149 resourcePoolIds: string[];
150 /** RFC3339 creation timestamp. */
151 createTime: string | undefined;
152 /** RFC3339 last-update timestamp. */
153 updateTime: string | undefined;
154 /** Time the resource first entered `RUNNING`. */
155 startTime: string | undefined;
156 },
157 never,
158 Providers
159 >;
160
161 /**
162 * A Vertex AI Persistent Resource — dedicated node pools for custom
163 * training and Ray-on-Vertex workloads.
164 *
165 * Changing `persistentResourceId`, `location`, `network`,
166 * `encryptionSpec`, or pool machine specs replaces the resource. Replica
167 * counts, labels, and display name update in place.
168 *
169 * Provisioning typically takes several minutes.
170 *
171 * ### Creating a Persistent Resource
172 * **Example:** Single n1-standard-4 pool
173 * ```typescript
174 * const pool = yield* GCP.AIPlatform.PersistentResource("Train", {
175 * resourcePools: [
176 * {
177 * id: "worker",
178 * replicaCount: "1",
179 * machineSpec: { machineType: "n1-standard-4" },
180 * },
181 * ],
182 * });
183 * ```
184 *
185 * @resource
186 * @category AIPlatform
187 */
188 export const PersistentResource = Resource<PersistentResource>("GCP.AIPlatform.PersistentResource");
189
190 export class PersistentResourceNotResolved extends Data.TaggedError(
191 "GCP.AIPlatform.PersistentResourceNotResolved",
192 )<{
193 resourceName: string;
194 }> {}
195
196 const resourceName = (project: string, location: string, id: string) =>
197 `${locationParent(project, location)}/${COLLECTION}/${id}`;
198
199 const toPools = (
200 pools: ResourcePool[] | undefined,
201 ): aiplatform.GoogleCloudAiplatformV1ResourcePool[] =>
202 (pools ?? [{}]).map((pool, index) => ({
203 id: pool.id ?? `pool-${index}`,
204 replicaCount: pool.replicaCount ?? DEFAULT_REPLICA_COUNT,
205 machineSpec: {
206 machineType: pool.machineSpec?.machineType ?? DEFAULT_MACHINE_TYPE,
207 acceleratorType: pool.machineSpec?.acceleratorType,
208 acceleratorCount: pool.machineSpec?.acceleratorCount,
209 gpuPartitionSize: pool.machineSpec?.gpuPartitionSize,
210 tpuTopology: pool.machineSpec?.tpuTopology,
211 },
212 diskSpec: pool.diskSpec,
213 autoscalingSpec: pool.autoscalingSpec,
214 }));
215
216 const machineKey = (pools: ResourcePool[] | undefined) =>
217 JSON.stringify(
218 toPools(pools).map((pool) => ({
219 id: pool.id,
220 machineSpec: pool.machineSpec,
221 })),
222 );
223
224 const toAttrs = (
225 resource: aiplatform.GoogleCloudAiplatformV1PersistentResource,
226 project: string,
227 ) => {
228 const name = resource.name ?? "";
229 const parsed = parseResourceName(name, COLLECTION);
230 return {
231 name,
232 persistentResourceId: parsed.id,
233 project: parsed.project || project,
234 location: parsed.location,
235 displayName: resource.displayName,
236 labels: userLabels(resource.labels),
237 state: resource.state,
238 network: resource.network,
239 resourcePoolIds: (resource.resourcePools ?? []).map((pool) => pool.id ?? ""),
240 createTime: resource.createTime,
241 updateTime: resource.updateTime,
242 startTime: resource.startTime,
243 };
244 };
245
246 const getByName = (name: string) =>
247 aiplatform
248 .getProjectsLocationsPersistentResources({ name })
249 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
250
251 const waitUntilExists = (name: string) =>
252 getByName(name).pipe(
253 Effect.filterOrFail(
254 (resource): resource is aiplatform.GoogleCloudAiplatformV1PersistentResource =>
255 resource !== undefined,
256 () => new AiPlatformNotResolved({ resourceName: name }),
257 ),
258 Effect.retry({
259 while: (error) => error._tag === "GCP.AIPlatform.NotResolved",
260 times: 8,
261 schedule: Schedule.spaced("2 seconds"),
262 }),
263 );
264
265 const waitUntilGone = (name: string) =>
266 getByName(name).pipe(
267 Effect.filterOrFail(
268 (resource) => resource === undefined,
269 () => new AiPlatformStillExists({ resourceName: name }),
270 ),
271 Effect.asVoid,
272 Effect.retry({
273 while: (error) => error._tag === "GCP.AIPlatform.StillExists",
274 // Tearing down the cluster takes several minutes.
275 times: 75,
276 schedule: Schedule.spaced("8 seconds"),
277 }),
278 );
279
280 export const PersistentResourceProvider = () =>
281 Provider.succeed(PersistentResource, {
282 stables: ["name", "persistentResourceId", "project", "location", "createTime"],
283
284 diff: Effect.fn(function* ({ news, olds, output }) {
285 const env = yield* GcpEnvironment.current;
286 if (!isResolved(news)) return undefined;
287 const previousId = olds?.persistentResourceId ?? output?.persistentResourceId;
288 const nextId = news.persistentResourceId ?? previousId;
289 const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region);
290 const nextLocation = normalizeLocation(news.location ?? output?.location, env.region);
291 const networkChanged = (news.network ?? olds?.network ?? "") !== (olds?.network ?? "");
292 const machineChanged =
293 olds !== undefined && machineKey(news.resourcePools) !== machineKey(olds.resourcePools);
294 // PATCH only accepts `resource_pools.replica_count`; `display_name` and
295 // `labels` are rejected ("Unrecognized path"), so changing them
296 // replaces the resource.
297 const displayNameChanged =
298 olds !== undefined && (news.displayName ?? "") !== (olds.displayName ?? "");
299 const labelsChanged =
300 olds !== undefined &&
301 JSON.stringify(
302 Object.entries(news.labels ?? {}).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)),
303 ) !==
304 JSON.stringify(
305 Object.entries(olds.labels ?? {}).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)),
306 );
307 // GCP only updates Ray-cluster persistent resources ("Currently we only
308 // support the update function on Ray cluster"); any other resource is
309 // replaced when its replica counts change.
310 const replicasChanged =
311 olds !== undefined &&
312 news.raySpec === undefined &&
313 JSON.stringify((news.resourcePools ?? []).map((p) => p.replicaCount)) !==
314 JSON.stringify((olds.resourcePools ?? []).map((p) => p.replicaCount));
315 const replace =
316 replicasChanged ||
317 (previousId !== undefined && nextId !== undefined && nextId !== previousId) ||
318 previousLocation !== nextLocation ||
319 (olds !== undefined && networkChanged) ||
320 machineChanged ||
321 displayNameChanged ||
322 labelsChanged;
323 if (!replace) return undefined;
324 return {
325 action: "replace" as const,
326 deleteFirst:
327 previousLocation === nextLocation && previousId !== undefined && nextId === previousId,
328 };
329 }),
330
331 read: Effect.fn(function* ({ id, olds, output }) {
332 const env = yield* GcpEnvironment.current;
333 const resourceId = yield* toPhysicalId(
334 id,
335 olds?.persistentResourceId,
336 output?.persistentResourceId,
337 );
338 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
339 const name = output?.name ?? resourceName(env.project, location, resourceId);
340 const existing = yield* getByName(name);
341 if (existing === undefined) return undefined;
342 const attrs = toAttrs(existing, env.project);
343 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
344 }),
345
346 list: () =>
347 Effect.gen(function* () {
348 const env = yield* GcpEnvironment.current;
349 const pages = (yield* Effect.forEach(listLocations(env.region), (location) =>
350 collectPages(
351 aiplatform.listProjectsLocationsPersistentResources.pages({
352 parent: locationParent(env.project, location),
353 pageSize: 100,
354 }),
355 ).pipe(Effect.catchTag("NotFound", () => Effect.succeed([]))),
356 )).flat();
357 return pages.flatMap((page) =>
358 (page.persistentResources ?? [])
359 .filter((resource) =>
360 Object.keys(resource.labels ?? {}).some((key) => key.startsWith("alchemy-")),
361 )
362 .map((resource) => toAttrs(resource, env.project)),
363 );
364 }),
365
366 reconcile: Effect.fn(function* ({ id, news, output }) {
367 const env = yield* GcpEnvironment.current;
368 const resourceId = yield* toPhysicalId(
369 id,
370 news.persistentResourceId,
371 output?.persistentResourceId,
372 );
373 const location = normalizeLocation(news.location ?? output?.location, env.region);
374 const name = resourceName(env.project, location, resourceId);
375 const desiredLabels = {
376 ...toLabels(news.labels),
377 ...(yield* createInternalLabels(id)),
378 };
379 const displayName = news.displayName ?? resourceId;
380 const resourcePools = toPools(news.resourcePools);
381 const resourceRuntimeSpec =
382 news.raySpec || news.serviceAccountSpec
383 ? {
384 raySpec: news.raySpec
385 ? {
386 imageUri: news.raySpec.imageUri,
387 resourcePoolImages: news.raySpec.resourcePoolImages,
388 headNodeResourcePoolId: news.raySpec.headNodeResourcePoolId,
389 rayLogsSpec: news.raySpec.rayLogsDisabled ? { disabled: true } : undefined,
390 rayMetricSpec: news.raySpec.rayMetricsDisabled ? { disabled: true } : undefined,
391 }
392 : undefined,
393 serviceAccountSpec: news.serviceAccountSpec,
394 }
395 : undefined;
396
397 let current = yield* getByName(output?.name ?? name);
398
399 if (current === undefined) {
400 const created = yield* aiplatform
401 .createProjectsLocationsPersistentResources({
402 parent: locationParent(env.project, location),
403 persistentResourceId: resourceId,
404 body: {
405 displayName,
406 labels: desiredLabels,
407 resourcePools,
408 network: news.network,
409 reservedIpRanges: news.reservedIpRanges,
410 encryptionSpec: news.encryptionSpec,
411 resourceRuntimeSpec,
412 },
413 })
414 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
415 if (created !== undefined) {
416 yield* waitForOperation(created, { alreadyExistsOk: true });
417 }
418 const createdName = resourceNameFromOperation(created ?? {}) ?? output?.name ?? name;
419 current = yield* waitUntilExists(createdName);
420 }
421
422 if (current === undefined) {
423 return yield* new PersistentResourceNotResolved({ resourceName: name });
424 }
425
426 const observedName = current.name ?? name;
427 // Reads fill in server defaults (disk spec, used replicas), and a pool's
428 // machine spec is immutable (a change replaces), so only replica counts
429 // are compared and patched.
430 const replicaCounts = (
431 pools: ReadonlyArray<{ id?: string; replicaCount?: string }> | undefined,
432 ) =>
433 Object.fromEntries(
434 (pools ?? []).map((pool, index) => [
435 pool.id ?? String(index),
436 String(pool.replicaCount ?? ""),
437 ]),
438 );
439 const poolsChanged = !jsonEqual(
440 replicaCounts(current.resourcePools),
441 replicaCounts(resourcePools),
442 );
443
444 // Display name and labels are create-only; `diff` replaces the resource
445 // when they change.
446 // Only Ray clusters accept updates; `diff` replaces other resources.
447 if (poolsChanged && news.raySpec !== undefined) {
448 const patched = yield* aiplatform.patchProjectsLocationsPersistentResources({
449 name: observedName,
450 updateMask: "resource_pools.replica_count",
451 body: { name: observedName, resourcePools },
452 });
453 yield* waitForOperation(patched);
454 current = yield* getByName(observedName);
455 }
456
457 if (current === undefined) {
458 return yield* new PersistentResourceNotResolved({ resourceName: observedName });
459 }
460 return toAttrs(current, env.project);
461 }),
462
463 delete: Effect.fn(function* ({ output }) {
464 // A PROVISIONING resource rejects deletes ("is being created thus can
465 // not be deleted now"), so wait for provisioning to settle first.
466 yield* getByName(output.name).pipe(
467 Effect.repeat({
468 schedule: Schedule.spaced("10 seconds"),
469 until: (resource) => resource?.state !== "PROVISIONING",
470 times: 60,
471 }),
472 );
473 const operation = yield* aiplatform
474 .deleteProjectsLocationsPersistentResources({ name: output.name })
475 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
476 if (operation !== undefined) {
477 yield* waitForOperation(operation, { notFoundOk: true });
478 }
479 yield* waitUntilGone(output.name);
480 }),
481 });