@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 297 lines 10.3 KB
Raw
1 import * as tpu from "@distilled.cloud/gcp/tpu_v2";
2 import { createPhysicalName } from "alchemy/PhysicalName";
3 import { tagRecord } from "alchemy/Tags";
4 import * as Data from "effect/Data";
5 import * as Effect from "effect/Effect";
6 import * as Schedule from "effect/Schedule";
7 import * as Stream from "effect/Stream";
8 import { alchemyLabelKeys, stripInternalLabels } from "../Labels.ts";
9 import { waitForOperation as waitForGcpOperation } from "../Operation.ts";
10
11 // A zone, not a region: TPUs (and the v2-8 default) exist only in a few zones.
12 export const DEFAULT_LOCATION = "us-central1-c";
13 export const DEFAULT_ACCELERATOR = "v2-8";
14 export const DEFAULT_RUNTIME = "tpu-ubuntu2204-base";
15 export const DEFAULT_NETWORK = "default";
16 export const MAX_NAME_LENGTH = 63;
17
18 export class ResourceNotResolved extends Data.TaggedError("GCP.TPU.ResourceNotResolved")<{
19 resourceName: string;
20 }> {}
21
22 export class ResourceStillExists extends Data.TaggedError("GCP.TPU.ResourceStillExists")<{
23 resourceName: string;
24 }> {}
25
26 export class ResourceNotReady extends Data.TaggedError("GCP.TPU.ResourceNotReady")<{
27 resourceName: string;
28 state: string;
29 }> {}
30
31 export class ResourceFailed extends Data.TaggedError("GCP.TPU.ResourceFailed")<{
32 resourceName: string;
33 state: string;
34 message: string | undefined;
35 }> {}
36
37 export const lastSegment = (value: string | undefined) => {
38 if (value === undefined || value.length === 0) return "";
39 const trimmed = value.replace(/\/+$/, "");
40 const parts = trimmed.split("/");
41 return parts[parts.length - 1] || trimmed;
42 };
43
44 export const rfc1035 = (name: string, fallback = "tpu"): string => {
45 let next = name
46 .toLowerCase()
47 .replace(/[^a-z0-9-]/g, "-")
48 .replace(/-+/g, "-")
49 .replace(/^-+|-+$/g, "");
50 if (!/^[a-z]/.test(next)) next = `t${next}`;
51 next = next.slice(0, MAX_NAME_LENGTH).replace(/-+$/g, "");
52 if (next.length === 0) return fallback;
53 if (!/[a-z0-9]$/.test(next)) next = `${next.slice(0, MAX_NAME_LENGTH - 1)}0`;
54 return next.slice(0, MAX_NAME_LENGTH);
55 };
56
57 export const normalizeLocation = (location: string | undefined, fallback = DEFAULT_LOCATION) =>
58 lastSegment(location ?? fallback).toLowerCase();
59
60 export const toPhysicalId = (
61 id: string,
62 explicit: string | undefined,
63 existing: string | undefined,
64 fallback = "tpu",
65 ) =>
66 Effect.gen(function* () {
67 if (explicit !== undefined) return rfc1035(explicit, fallback);
68 if (existing !== undefined) return existing;
69 return rfc1035(
70 yield* createPhysicalName({
71 id,
72 maxLength: MAX_NAME_LENGTH,
73 lowercase: true,
74 }),
75 fallback,
76 );
77 });
78
79 export const parseName = (name: string, collection: string) => {
80 const parts = name.split("/").filter((part) => part.length > 0);
81 const collectionAt = parts.lastIndexOf(collection);
82 const locationsAt = parts.lastIndexOf("locations");
83 const projectsAt = parts.lastIndexOf("projects");
84 return {
85 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
86 location:
87 locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : DEFAULT_LOCATION,
88 id: collectionAt >= 0 && parts[collectionAt + 1] ? parts[collectionAt + 1]! : lastSegment(name),
89 };
90 };
91
92 export const userLabels = (
93 labels: Record<string, string | undefined> | null | undefined,
94 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
95
96 export const hasAlchemyLabelMap = (labels: Record<string, string | undefined> | null | undefined) =>
97 Object.keys(labels ?? {}).some((key) => key.startsWith("alchemy-"));
98
99 export const stringMapOf = (
100 map: Record<string, string | undefined> | null | undefined,
101 ): Record<string, string> =>
102 Object.fromEntries(
103 Object.entries(map ?? {}).filter((entry): entry is [string, string] => entry[1] !== undefined),
104 );
105
106 export const mapKey = (map: Record<string, string | undefined> | null | undefined) =>
107 JSON.stringify(
108 Object.fromEntries(
109 Object.entries(stringMapOf(map)).sort(([left], [right]) => left.localeCompare(right)),
110 ),
111 );
112
113 export const stringsOf = (values: ReadonlyArray<string | undefined> | null | undefined): string[] =>
114 (values ?? []).filter((value): value is string => value !== undefined);
115
116 export const stringsKey = (values: ReadonlyArray<string | undefined> | null | undefined) =>
117 JSON.stringify([...stringsOf(values)].sort());
118
119 export const encodeDescription = (
120 labels: Record<string, string>,
121 description: string | undefined,
122 ): string => {
123 const marker = `[alchemy ${alchemyLabelKeys.stack}=${labels[alchemyLabelKeys.stack]} ${alchemyLabelKeys.stage}=${labels[alchemyLabelKeys.stage]} ${alchemyLabelKeys.id}=${labels[alchemyLabelKeys.id]}]`;
124 return description ? `${marker}\n${description}` : marker;
125 };
126
127 export const parseDescription = (
128 description: string | undefined,
129 ): {
130 labels: Record<string, string>;
131 description: string | undefined;
132 } => {
133 if (!description?.startsWith("[alchemy ")) {
134 return { labels: {}, description };
135 }
136 const end = description.indexOf("]");
137 if (end < 0) return { labels: {}, description };
138 const labels: Record<string, string> = {};
139 for (const part of description.slice("[alchemy ".length, end).split(/\s+/)) {
140 const eq = part.indexOf("=");
141 if (eq > 0) {
142 labels[part.slice(0, eq)] = part.slice(eq + 1);
143 }
144 }
145 const rest = description.slice(end + 1).replace(/^\n/, "");
146 return { labels, description: rest.length > 0 ? rest : undefined };
147 };
148
149 export const hasOwnershipMarker = (description: string | undefined) =>
150 Object.keys(parseDescription(description).labels).some((key) => key.startsWith("alchemy-"));
151
152 export const fieldMask = (fields: Array<string | false | undefined>) =>
153 fields.filter((field): field is string => typeof field === "string").join(",");
154
155 export const replaceOnIdentity = (input: {
156 previousId: string | undefined;
157 nextId: string | undefined;
158 previousLocation: string;
159 nextLocation: string;
160 extra?: boolean;
161 }) => {
162 const replace =
163 (input.extra ?? false) ||
164 (input.previousId !== undefined &&
165 input.nextId !== undefined &&
166 input.nextId !== input.previousId) ||
167 input.previousLocation !== input.nextLocation;
168 if (!replace) return undefined;
169 const samePhysical =
170 input.previousLocation === input.nextLocation &&
171 input.previousId !== undefined &&
172 input.nextId === input.previousId;
173 return {
174 action: "replace" as const,
175 deleteFirst: samePhysical,
176 };
177 };
178
179 /**
180 * Wait for a Cloud TPU operation. TPU VM creates take several minutes
181 * (large slices longer). An operation that finished with `ALREADY_EXISTS`
182 * (6) is a lost create race; `notFoundOk` also accepts `NOT_FOUND` (5).
183 */
184 export const waitForOperation = (operation: tpu.Operation, options?: { notFoundOk?: boolean }) =>
185 waitForGcpOperation(
186 operation,
187 (name) =>
188 tpu
189 .getProjectsLocationsOperations({ name })
190 .pipe(
191 Effect.catchTag("NotFound", (error) =>
192 options?.notFoundOk === true
193 ? Effect.succeed<tpu.Operation>({ name, done: true })
194 : Effect.fail(error),
195 ),
196 ),
197 { budget: "30 minutes" },
198 ).pipe(
199 Effect.catchIf(
200 (error) =>
201 error._tag === "GCP.OperationFailed" &&
202 (error.code === 6 || (options?.notFoundOk === true && error.code === 5)),
203 () => Effect.void,
204 ),
205 Effect.asVoid,
206 );
207
208 export const waitUntilExists = <A, E extends { readonly _tag: string }, R>(
209 get: Effect.Effect<A | undefined, E, R>,
210 name: string,
211 ): Effect.Effect<NonNullable<A>, E | ResourceNotResolved, R> =>
212 get.pipe(
213 Effect.filterOrFail(
214 (value): value is NonNullable<A> => value != null,
215 () => new ResourceNotResolved({ resourceName: name }),
216 ),
217 Effect.retry({
218 while: (error) => error._tag === "GCP.TPU.ResourceNotResolved",
219 times: 8,
220 schedule: Schedule.spaced("1 second"),
221 }),
222 );
223
224 export const waitUntilGone = <A, E extends { readonly _tag: string }, R>(
225 get: Effect.Effect<A | undefined, E, R>,
226 name: string,
227 ): Effect.Effect<void, E | ResourceStillExists, R> =>
228 get.pipe(
229 Effect.filterOrFail(
230 (value) => value === undefined,
231 () => new ResourceStillExists({ resourceName: name }),
232 ),
233 Effect.retry({
234 while: (error) => error._tag === "GCP.TPU.ResourceStillExists",
235 times: 10,
236 schedule: Schedule.spaced("8 seconds"),
237 }),
238 Effect.asVoid,
239 );
240
241 const READY_STATES = new Set(["READY", "ACTIVE", "ACCEPTED"]);
242 const FAILED_STATES = new Set(["FAILED", "ERROR", "TERMINATED", "PREEMPTED", "UNKNOWN"]);
243
244 const isReadyState = (state: string | undefined) => READY_STATES.has((state ?? "").toUpperCase());
245
246 const isFailedState = (state: string | undefined) => FAILED_STATES.has((state ?? "").toUpperCase());
247
248 export const waitUntilReady = <A, E extends { readonly _tag: string }, R>(
249 get: Effect.Effect<A | undefined, E, R>,
250 name: string,
251 stateOf: (value: NonNullable<A>) => string | undefined,
252 messageOf?: (value: NonNullable<A>) => string | undefined,
253 ): Effect.Effect<NonNullable<A>, E | ResourceNotResolved | ResourceFailed | ResourceNotReady, R> =>
254 get.pipe(
255 Effect.filterOrFail(
256 (value): value is NonNullable<A> => value != null,
257 () => new ResourceNotResolved({ resourceName: name }),
258 ),
259 Effect.filterOrFail(
260 (value) => !isFailedState(stateOf(value)),
261 (value) =>
262 new ResourceFailed({
263 resourceName: name,
264 state: (stateOf(value) ?? "").toUpperCase(),
265 message: messageOf?.(value),
266 }),
267 ),
268 Effect.filterOrFail(
269 (value) => {
270 const state = stateOf(value) ?? "";
271 return isReadyState(state) || state.length === 0;
272 },
273 (value) =>
274 new ResourceNotReady({
275 resourceName: name,
276 state: (stateOf(value) ?? "").toUpperCase(),
277 }),
278 ),
279 Effect.retry({
280 while: (error) =>
281 error._tag === "GCP.TPU.ResourceNotReady" || error._tag === "GCP.TPU.ResourceNotResolved",
282 times: 10,
283 schedule: Schedule.spaced("8 seconds"),
284 }),
285 );
286
287 export const listLabeledPages = <Page, A, E, R>(
288 pages: Stream.Stream<Page, E, R>,
289 items: (page: Page) => readonly A[] | undefined,
290 labelsOf: (item: A) => Record<string, string | undefined> | null | undefined,
291 ) =>
292 pages.pipe(
293 Stream.flatMap((page) => Stream.fromIterable(items(page) ?? [])),
294 Stream.filter((item) => hasAlchemyLabelMap(labelsOf(item))),
295 Stream.runCollect,
296 Effect.map((chunk) => Array.from(chunk)),
297 );