@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 305 lines 10.1 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 * as Stream from "effect/Stream";
11 import { GcpEnvironment } from "../Environment.ts";
12 import { hasAlchemyLabels } from "../Labels.ts";
13 import type { Providers } from "../Providers.ts";
14 import { isJobTerminal } from "./internal.ts";
15 import {
16 alchemyIdFilter,
17 createInternalLabels,
18 hasAlchemyPrefix,
19 listLocations,
20 locationOf,
21 lastSegment,
22 normalizeLocation,
23 projectOf,
24 stableJson,
25 toDisplayName,
26 toLabels,
27 userLabels,
28 } from "./names.ts";
29 import { waitForOperation } from "./operations.ts";
30
31 export type NasJobProps = {
32 /**
33 * Vertex AI location. Immutable — changing it replaces the job.
34 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
35 */
36 location?: string;
37 /**
38 * Display name (max 128 Unicode characters). Generated from the stack,
39 * stage, and logical id when omitted.
40 */
41 displayName?: string;
42 /**
43 * User labels. Alchemy ownership labels are merged in automatically.
44 */
45 labels?: Record<string, string>;
46 /**
47 * Neural Architecture Search specification.
48 */
49 nasJobSpec: aiplatform.GoogleCloudAiplatformV1NasJobSpec;
50 /**
51 * Enable restricted-image training for the tenant project.
52 * @default false
53 */
54 enableRestrictedImageTraining?: boolean;
55 /**
56 * Customer-managed encryption key. Immutable.
57 */
58 encryptionSpec?: aiplatform.GoogleCloudAiplatformV1EncryptionSpec;
59 };
60
61 export type NasJob = Resource<
62 "GCP.AIPlatform.NasJob",
63 NasJobProps,
64 {
65 /** Full resource name. */
66 name: string;
67 /** Job id (last path segment). */
68 nasJobId: string;
69 /** Project id. */
70 project: string;
71 /** Location id. */
72 location: string;
73 /** Display name. */
74 displayName: string | undefined;
75 /** User labels (Alchemy ownership labels stripped). */
76 labels: Record<string, string>;
77 /** Job state. */
78 state: string | undefined;
79 /** Whether restricted-image training is enabled. */
80 enableRestrictedImageTraining: boolean;
81 /** RFC3339 creation timestamp. */
82 createTime: string | undefined;
83 /** RFC3339 start timestamp. */
84 startTime: string | undefined;
85 /** RFC3339 end timestamp. */
86 endTime: string | undefined;
87 },
88 never,
89 Providers
90 >;
91
92 /**
93 * A Vertex AI Neural Architecture Search job.
94 *
95 * The API assigns the job id. There is no update method — changing display
96 * name, labels, or `nasJobSpec` replaces the job. Delete cancels a running
97 * job first.
98 *
99 * ### Creating a NasJob
100 * **Example:** Multi-trial search
101 * ```typescript
102 * const job = yield* GCP.AIPlatform.NasJob("Search", {
103 * nasJobSpec: {
104 * searchSpaceSpec: "{}",
105 * multiTrialAlgorithmSpec: {
106 * metric: { metricId: "accuracy", goal: "MAXIMIZE" },
107 * searchTrialSpec: {
108 * maxTrialCount: 1,
109 * maxParallelTrialCount: 1,
110 * searchTrialJobSpec: {
111 * workerPoolSpecs: [{
112 * machineSpec: { machineType: "n1-standard-4" },
113 * replicaCount: "1",
114 * containerSpec: { imageUri: "gcr.io/cloud-aiplatform/training/tf-cpu.2-8:latest" },
115 * }],
116 * },
117 * },
118 * },
119 * },
120 * });
121 * ```
122 *
123 * @resource
124 * @category AIPlatform
125 */
126 export const NasJob = Resource<NasJob>("GCP.AIPlatform.NasJob");
127
128 export class NasJobNotResolved extends Data.TaggedError("GCP.AIPlatform.NasJobNotResolved")<{
129 resourceName: string;
130 }> {}
131
132 export class NasJobStillExists extends Data.TaggedError("GCP.AIPlatform.NasJobStillExists")<{
133 resourceName: string;
134 }> {}
135
136 const toAttrs = (job: aiplatform.GoogleCloudAiplatformV1NasJob, project: string) => {
137 const name = job.name ?? "";
138 return {
139 name,
140 nasJobId: lastSegment(name),
141 project: projectOf(name, project),
142 location: locationOf(name),
143 displayName: job.displayName,
144 labels: userLabels(job.labels),
145 state: job.state,
146 enableRestrictedImageTraining: job.enableRestrictedImageTraining === true,
147 createTime: job.createTime,
148 startTime: job.startTime,
149 endTime: job.endTime,
150 };
151 };
152
153 const getByName = (name: string) =>
154 aiplatform
155 .getProjectsLocationsNasJobs({ name })
156 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
157
158 const listPage = (parent: string, filter?: string) =>
159 aiplatform.listProjectsLocationsNasJobs.pages({ parent, pageSize: 100, filter }).pipe(
160 Stream.runCollect,
161 Effect.map((pages) => Array.from(pages).flatMap((page) => page.nasJobs ?? [])),
162 Effect.catchTag("NotFound", () =>
163 Effect.succeed([] as aiplatform.GoogleCloudAiplatformV1NasJob[]),
164 ),
165 );
166
167 const findOwned = (project: string, location: string, labels: Record<string, string>) =>
168 listPage(`projects/${project}/locations/${location}`, alchemyIdFilter(labels)).pipe(
169 Effect.map((items) => items.find((item) => hasAlchemyPrefix(item.labels)) ?? undefined),
170 );
171
172 const waitUntilGone = (name: string) =>
173 getByName(name).pipe(
174 Effect.filterOrFail(
175 (job) => job === undefined,
176 () => new NasJobStillExists({ resourceName: name }),
177 ),
178 Effect.asVoid,
179 Effect.retry({
180 while: (error) => error._tag === "GCP.AIPlatform.NasJobStillExists",
181 times: 10,
182 schedule: Schedule.spaced("2 seconds"),
183 }),
184 );
185
186 export const NasJobProvider = () =>
187 Provider.succeed(NasJob, {
188 stables: ["name", "nasJobId", "project", "location", "createTime"],
189
190 diff: Effect.fn(function* ({ news, olds, output }) {
191 const env = yield* GcpEnvironment.current;
192 if (!isResolved(news)) return undefined;
193 const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region);
194 const nextLocation = normalizeLocation(
195 news.location ?? olds?.location ?? output?.location,
196 env.region,
197 );
198 const previousDisplay = olds?.displayName ?? output?.displayName;
199 const displayChanged =
200 news.displayName !== undefined &&
201 previousDisplay !== undefined &&
202 news.displayName !== previousDisplay;
203 const specsChanged =
204 olds !== undefined && stableJson(olds.nasJobSpec) !== stableJson(news.nasJobSpec);
205 if (previousLocation !== nextLocation || displayChanged || specsChanged) {
206 return { action: "replace" as const, deleteFirst: false };
207 }
208 return undefined;
209 }),
210
211 read: Effect.fn(function* ({ id, olds, output }) {
212 const env = yield* GcpEnvironment.current;
213 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
214 const ownership = yield* createInternalLabels(id);
215 const existing =
216 (output?.name !== undefined ? yield* getByName(output.name) : undefined) ??
217 (yield* findOwned(env.project, location, ownership));
218 if (existing === undefined) return undefined;
219 const attrs = toAttrs(existing, env.project);
220 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
221 }),
222
223 list: () =>
224 Effect.gen(function* () {
225 const env = yield* GcpEnvironment.current;
226 const pages = yield* Effect.forEach(
227 listLocations(env.region),
228 (location) => listPage(`projects/${env.project}/locations/${location}`),
229 { concurrency: 4 },
230 );
231 return pages
232 .flat()
233 .filter((job) => hasAlchemyPrefix(job.labels))
234 .map((job) => toAttrs(job, env.project));
235 }),
236
237 reconcile: Effect.fn(function* ({ id, news, output }) {
238 const env = yield* GcpEnvironment.current;
239 const location = normalizeLocation(news.location ?? output?.location, env.region);
240 const displayName = yield* toDisplayName(id, news.displayName, output?.displayName);
241 const desiredLabels = {
242 ...toLabels(news.labels),
243 ...(yield* createInternalLabels(id)),
244 };
245
246 let current =
247 (output?.name !== undefined ? yield* getByName(output.name) : undefined) ??
248 (yield* findOwned(env.project, location, desiredLabels));
249
250 if (current === undefined) {
251 const created = yield* aiplatform
252 .createProjectsLocationsNasJobs({
253 parent: `projects/${env.project}/locations/${location}`,
254 body: {
255 displayName,
256 labels: desiredLabels,
257 nasJobSpec: news.nasJobSpec,
258 enableRestrictedImageTraining: news.enableRestrictedImageTraining === true,
259 encryptionSpec: news.encryptionSpec,
260 },
261 })
262 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
263 current = created ?? (yield* findOwned(env.project, location, desiredLabels));
264 }
265
266 if (current === undefined || current.name === undefined) {
267 return yield* new NasJobNotResolved({
268 resourceName: output?.name ?? `${location}/nasJobs`,
269 });
270 }
271
272 return toAttrs(current, env.project);
273 }),
274
275 delete: Effect.fn(function* ({ output }) {
276 yield* aiplatform
277 .cancelProjectsLocationsNasJobs({
278 name: output.name,
279 body: {},
280 })
281 .pipe(Effect.catchTag(["NotFound", "BadRequest", "Conflict"], () => Effect.void));
282 // Running jobs reject deletes; wait for the cancel to land.
283 yield* getByName(output.name).pipe(
284 Effect.repeat({
285 schedule: Schedule.spaced("4 seconds"),
286 until: (job) => job === undefined || isJobTerminal(job.state),
287 times: 45,
288 }),
289 );
290 const operation = yield* aiplatform
291 .deleteProjectsLocationsNasJobs({ name: output.name })
292 .pipe(
293 Effect.retry({
294 while: (error) => error._tag === "Conflict",
295 times: 8,
296 schedule: Schedule.spaced("2 seconds"),
297 }),
298 Effect.catchTag("NotFound", () => Effect.undefined),
299 );
300 if (operation !== undefined) {
301 yield* waitForOperation(operation, { notFoundOk: true });
302 }
303 yield* waitUntilGone(output.name);
304 }),
305 });