The best way to control Claude Code, Codex and more from any computer or phone

main
ts 477 lines 15.7 KB
Raw
1 import * as gkeonprem from "@distilled.cloud/gcp/gkeonprem_v1";
2 import type { GcpOpContext } from "@distilled.cloud/gcp/Protocol";
3 import { createPhysicalName } from "alchemy/PhysicalName";
4 import { tagRecord } from "alchemy/Tags";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8 import * as Stream from "effect/Stream";
9 import {
10 alchemyLabelKeys,
11 createInternalLabels,
12 hasAlchemyLabels,
13 stripInternalLabels,
14 toLabels,
15 } from "../Labels.ts";
16 import { waitForOperation as waitForLongRunningOperation } from "../Operation.ts";
17
18 export const MAX_NAME_LENGTH = 63;
19 export const VMWARE_NAME_LENGTH = 40;
20
21 export { createInternalLabels, hasAlchemyLabels, stripInternalLabels, toLabels };
22
23 export class ResourceNotResolved extends Data.TaggedError("GCP.GKEOnPrem.ResourceNotResolved")<{
24 resourceName: string;
25 }> {}
26
27 export class ResourceStillExists extends Data.TaggedError("GCP.GKEOnPrem.ResourceStillExists")<{
28 resourceName: string;
29 }> {}
30
31 export class ResourceNotReady extends Data.TaggedError("GCP.GKEOnPrem.ResourceNotReady")<{
32 resourceName: string;
33 state: string;
34 }> {}
35
36 export class ResourceFailed extends Data.TaggedError("GCP.GKEOnPrem.ResourceFailed")<{
37 resourceName: string;
38 state: string;
39 details: string | undefined;
40 }> {}
41
42 export const lastSegment = (value: string) => {
43 const trimmed = value.replace(/\/+$/, "");
44 const parts = trimmed.split("/");
45 return parts[parts.length - 1] || trimmed;
46 };
47
48 export const rfc1035 = (
49 name: string,
50 fallback = "gkeonprem",
51 maxLength = MAX_NAME_LENGTH,
52 ): string => {
53 let next = name
54 .toLowerCase()
55 .replace(/[^a-z0-9-]/g, "-")
56 .replace(/-+/g, "-")
57 .replace(/^-+|-+$/g, "");
58 if (!/^[a-z]/.test(next)) next = `g${next}`;
59 next = next.slice(0, maxLength).replace(/-+$/g, "");
60 if (next.length === 0) return fallback.slice(0, maxLength);
61 if (!/[a-z0-9]$/.test(next)) next = `${next.slice(0, maxLength - 1)}0`;
62 return next.slice(0, maxLength);
63 };
64
65 export const normalizeLocation = (location: string | undefined, fallback: string) =>
66 lastSegment(location ?? fallback).toLowerCase();
67
68 export const parentOf = (project: string, location: string) =>
69 `projects/${project}/locations/${lastSegment(location).toLowerCase()}`;
70
71 export const toPhysicalId = (
72 id: string,
73 explicit: string | undefined,
74 existing: string | undefined,
75 fallback = "gkeonprem",
76 maxLength = MAX_NAME_LENGTH,
77 ) =>
78 Effect.gen(function* () {
79 if (explicit !== undefined) return rfc1035(explicit, fallback, maxLength);
80 if (existing !== undefined) return existing;
81 return rfc1035(
82 yield* createPhysicalName({
83 id,
84 maxLength,
85 lowercase: true,
86 }),
87 fallback,
88 maxLength,
89 );
90 });
91
92 export const parseName = (name: string, collection: string, fallbackLocation: string) => {
93 const parts = name.split("/").filter((part) => part.length > 0);
94 const collectionAt = parts.lastIndexOf(collection);
95 const locationsAt = parts.lastIndexOf("locations");
96 const projectsAt = parts.lastIndexOf("projects");
97 return {
98 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
99 location:
100 locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : fallbackLocation,
101 id: collectionAt >= 0 && parts[collectionAt + 1] ? parts[collectionAt + 1]! : lastSegment(name),
102 parent:
103 collectionAt > 0
104 ? parts.slice(0, collectionAt).join("/")
105 : parts.slice(0, Math.max(0, parts.length - 1)).join("/"),
106 };
107 };
108
109 export const expandParent = (
110 value: string,
111 project: string,
112 location: string,
113 collection: string,
114 ) => {
115 if (value.includes("/")) return value.replace(/\/+$/, "");
116 return `projects/${project}/locations/${location}/${collection}/${rfc1035(value, collection)}`;
117 };
118
119 export const membershipName = (value: string, project: string) => {
120 const next = value.replace(/\/+$/, "");
121 if (next.includes("/")) return next;
122 return `projects/${project}/locations/global/memberships/${rfc1035(next, "membership")}`;
123 };
124
125 export const stringMap = (
126 value: Record<string, string | undefined> | null | undefined,
127 ): Record<string, string> =>
128 Object.fromEntries(
129 Object.entries(value ?? {}).filter(
130 (entry): entry is [string, string] => typeof entry[1] === "string" && entry[1].length > 0,
131 ),
132 );
133
134 export const userLabels = (
135 labels: Record<string, string | undefined> | null | undefined,
136 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
137
138 export const hasAlchemyLabelMap = (labels: Record<string, string | undefined> | null | undefined) =>
139 Object.keys(labels ?? {}).some((key) => key.startsWith("alchemy-"));
140
141 export const encodeOwnership = (
142 labels: Record<string, string>,
143 text: string | undefined,
144 ): string => {
145 const marker = `[alchemy ${alchemyLabelKeys.stack}=${labels[alchemyLabelKeys.stack]} ${alchemyLabelKeys.stage}=${labels[alchemyLabelKeys.stage]} ${alchemyLabelKeys.id}=${labels[alchemyLabelKeys.id]}]`;
146 const trimmed = text?.trim();
147 return trimmed && trimmed.length > 0 ? `${marker}\n${trimmed}` : marker;
148 };
149
150 export const parseOwnership = (
151 text: string | undefined,
152 ): {
153 labels: Record<string, string>;
154 text: string | undefined;
155 } => {
156 if (!text?.startsWith("[alchemy ")) {
157 return { labels: {}, text };
158 }
159 const end = text.indexOf("]");
160 if (end < 0) return { labels: {}, text };
161 const labels: Record<string, string> = {};
162 for (const part of text.slice("[alchemy ".length, end).split(/\s+/)) {
163 const eq = part.indexOf("=");
164 if (eq > 0) {
165 labels[part.slice(0, eq)] = part.slice(eq + 1);
166 }
167 }
168 const rest = text.slice(end + 1).replace(/^\s+/, "");
169 return { labels, text: rest.length > 0 ? rest : undefined };
170 };
171
172 export const hasOwnershipMarker = (text: string | undefined) =>
173 Object.keys(parseOwnership(text).labels).some((key) => key.startsWith("alchemy-"));
174
175 export const isOwned = (
176 annotations: Record<string, string | undefined> | null | undefined,
177 text: string | undefined,
178 ) => hasAlchemyLabelMap(annotations) || hasOwnershipMarker(text);
179
180 export const desiredAnnotations = (
181 ownership: Record<string, string>,
182 labels: Record<string, string> | undefined,
183 annotations: Record<string, string> | undefined,
184 ): Record<string, string> => ({
185 ...toLabels(labels),
186 ...stringMap(annotations),
187 ...ownership,
188 });
189
190 export const canonical = (value: unknown): unknown => {
191 if (value === undefined || value === null) return undefined;
192 if (typeof value === "boolean" || typeof value === "number") return value;
193 if (typeof value === "string") {
194 return value.length === 0 ? undefined : value;
195 }
196 if (Array.isArray(value)) {
197 const items = value.map(canonical).filter((item) => item !== undefined);
198 return items.length === 0 ? undefined : items;
199 }
200 if (typeof value === "object") {
201 const entries = Object.entries(value as Record<string, unknown>)
202 .map(([key, item]) => [key, canonical(item)] as const)
203 .filter(([, item]) => item !== undefined)
204 .sort(([left], [right]) => left.localeCompare(right));
205 if (entries.length === 0) return undefined;
206 return Object.fromEntries(entries);
207 }
208 return undefined;
209 };
210
211 export const fingerprint = (value: unknown): string => JSON.stringify(canonical(value) ?? null);
212
213 const pickDefined = (observed: unknown, desired: unknown): unknown => {
214 if (desired === undefined || desired === null) return undefined;
215 if (Array.isArray(desired)) {
216 if (!Array.isArray(observed)) return observed;
217 return desired.map((item, index) => pickDefined(observed[index], item));
218 }
219 if (typeof desired === "object") {
220 if (typeof observed !== "object" || observed === null) return observed;
221 const rec = observed as Record<string, unknown>;
222 return Object.fromEntries(
223 Object.entries(desired as Record<string, unknown>).map(([key, item]) => [
224 key,
225 pickDefined(rec[key], item),
226 ]),
227 );
228 }
229 return observed;
230 };
231
232 export const differs = (observed: unknown, desired: unknown): boolean => {
233 if (desired === undefined) return false;
234 return fingerprint(pickDefined(observed, desired)) !== fingerprint(desired);
235 };
236
237 export const fieldMask = (fields: Array<string | false | undefined>) =>
238 fields.filter((field): field is string => typeof field === "string").join(",");
239
240 export const sameText = (left: string | undefined, right: string | undefined) =>
241 (left ?? "") === (right ?? "");
242
243 export const textState = (state: string | undefined) =>
244 state === undefined ? undefined : `${state}`;
245
246 export const replaceOnIdentity = (input: {
247 previousId: string | undefined;
248 nextId: string | undefined;
249 previousLocation: string;
250 nextLocation: string;
251 extra?: boolean;
252 previousParent?: string;
253 nextParent?: string;
254 }) => {
255 const parentChanged =
256 (input.previousParent ?? "") !== "" &&
257 (input.nextParent ?? "") !== "" &&
258 (input.previousParent ?? "") !== (input.nextParent ?? "");
259 const replace =
260 (input.extra ?? false) ||
261 parentChanged ||
262 (input.previousId !== undefined &&
263 input.nextId !== undefined &&
264 input.nextId !== input.previousId) ||
265 input.previousLocation !== input.nextLocation;
266 if (!replace) return undefined;
267 const samePhysical =
268 input.previousLocation === input.nextLocation &&
269 !parentChanged &&
270 input.previousId !== undefined &&
271 input.nextId === input.previousId;
272 return {
273 action: "replace" as const,
274 deleteFirst: samePhysical,
275 };
276 };
277
278 const READY_STATES = new Set(["RUNNING", "READY", "DEGRADED"]);
279 const FAILED_STATES = new Set(["ERROR", "FAILED"]);
280
281 export const isReadyState = (state: string | undefined) =>
282 READY_STATES.has((state ?? "").toUpperCase());
283
284 export const isFailedState = (state: string | undefined) =>
285 FAILED_STATES.has((state ?? "").toUpperCase());
286
287 /**
288 * Wait on a GKE On-Prem long-running operation. Cluster and node-pool enrollment takes up to ~30 minutes.
289 * `ALREADY_EXISTS` (a create race) counts as success; so does `NOT_FOUND`
290 * when `notFoundOk` (deletes). Returns the final operation.
291 */
292 export const waitForOperation = (
293 operation: gkeonprem.Operation,
294 options?: { notFoundOk?: boolean },
295 ) =>
296 Effect.suspend(() => {
297 let latest = operation;
298 return waitForLongRunningOperation(
299 operation,
300 (name) => {
301 const get = gkeonprem.getProjectsLocationsOperations({ name }).pipe(
302 Effect.tap((current) =>
303 Effect.sync(() => {
304 latest = current;
305 }),
306 ),
307 );
308 const observe: Effect.Effect<
309 gkeonprem.Operation,
310 gkeonprem.GetProjectsLocationsOperationsError,
311 GcpOpContext
312 > =
313 options?.notFoundOk === true
314 ? get.pipe(
315 Effect.catchTag("NotFound", () =>
316 Effect.succeed<gkeonprem.Operation>({ name, done: true }),
317 ),
318 )
319 : get.pipe(
320 // A just-returned operation can briefly 404 on read.
321 Effect.retry({
322 while: (error) => error._tag === "NotFound",
323 times: 5,
324 schedule: Schedule.exponential("250 millis"),
325 }),
326 );
327 return observe;
328 },
329 { budget: "30 minutes", interval: "10 seconds" },
330 ).pipe(
331 Effect.map(() => latest),
332 // google.rpc.Code ALREADY_EXISTS = 6, NOT_FOUND = 5.
333 Effect.catchTag("GCP.OperationFailed", (error) =>
334 error.code === 6 || (options?.notFoundOk === true && error.code === 5)
335 ? Effect.succeed(latest)
336 : Effect.fail(error),
337 ),
338 );
339 });
340
341 export const waitUntilExists = <A, E extends { readonly _tag: string }, R>(
342 get: Effect.Effect<A, E, R>,
343 name: string,
344 ): Effect.Effect<Exclude<A, undefined>, E | ResourceNotResolved, R> =>
345 get.pipe(
346 Effect.filterOrFail(
347 (value): value is Exclude<A, undefined> => value !== undefined,
348 () => new ResourceNotResolved({ resourceName: name }),
349 ),
350 Effect.retry({
351 while: (error) => error._tag === "GCP.GKEOnPrem.ResourceNotResolved",
352 times: 8,
353 schedule: Schedule.spaced("1 second"),
354 }),
355 );
356
357 export const waitUntilGone = <A, E extends { readonly _tag: string }, R>(
358 get: Effect.Effect<A, E, R>,
359 name: string,
360 ): Effect.Effect<void, E | ResourceStillExists, R> =>
361 get.pipe(
362 Effect.filterOrFail(
363 (value) => value === undefined,
364 () => new ResourceStillExists({ resourceName: name }),
365 ),
366 Effect.asVoid,
367 Effect.retry({
368 while: (error) => error._tag === "GCP.GKEOnPrem.ResourceStillExists",
369 times: 10,
370 schedule: Schedule.spaced("2 seconds"),
371 }),
372 );
373
374 export const waitUntilReady = <A, E extends { readonly _tag: string }, R>(
375 get: Effect.Effect<A, E, R>,
376 name: string,
377 stateOf: (value: Exclude<A, undefined>) => string | undefined,
378 detailsOf?: (value: Exclude<A, undefined>) => string | undefined,
379 options?: {
380 times?: number;
381 interval?: `${number} seconds`;
382 },
383 ): Effect.Effect<
384 Exclude<A, undefined>,
385 E | ResourceNotResolved | ResourceFailed | ResourceNotReady,
386 R
387 > =>
388 get.pipe(
389 Effect.filterOrFail(
390 (value): value is Exclude<A, undefined> => value !== undefined,
391 () => new ResourceNotResolved({ resourceName: name }),
392 ),
393 Effect.filterOrFail(
394 (value) => !isFailedState(stateOf(value)),
395 (value) =>
396 new ResourceFailed({
397 resourceName: name,
398 state: stateOf(value) ?? "",
399 details: detailsOf?.(value),
400 }),
401 ),
402 Effect.filterOrFail(
403 (value) => {
404 const state = stateOf(value) ?? "";
405 return isReadyState(state) || state.length === 0;
406 },
407 (value) => new ResourceNotReady({ resourceName: name, state: stateOf(value) ?? "" }),
408 ),
409 Effect.retry({
410 while: (error) =>
411 error._tag === "GCP.GKEOnPrem.ResourceNotReady" ||
412 error._tag === "GCP.GKEOnPrem.ResourceNotResolved",
413 // Cluster enrollment takes up to ~30 minutes.
414 times: options?.times ?? 180,
415 schedule: Schedule.spaced(options?.interval ?? "10 seconds"),
416 }),
417 );
418
419 export const collectPages = <Page, Item, E extends { readonly _tag: string }, R>(
420 stream: Stream.Stream<Page, E, R>,
421 pick: (page: Page) => readonly Item[] | undefined,
422 ) =>
423 stream.pipe(
424 Stream.flatMap((page) => Stream.fromIterable(pick(page) ?? [])),
425 Stream.runCollect,
426 Effect.map((chunk) => Array.from(chunk)),
427 // A missing parent lists as empty.
428 Effect.catchIf(
429 (error) => error._tag === "NotFound",
430 () => Effect.succeed([] as Item[]),
431 ),
432 );
433
434 export const listAtLocation = <A, E extends { readonly _tag: string }, R>(
435 project: string,
436 region: string,
437 list: (parent: string) => Effect.Effect<A[], E, R>,
438 ) =>
439 list(`projects/${project}/locations/-`).pipe(
440 Effect.catchIf(
441 // Some collections reject the `-` location wildcard.
442 (error) => error._tag === "NotFound",
443 () => list(`projects/${project}/locations/${region}`),
444 ),
445 );
446
447 export const listAtNested = <A, E extends { readonly _tag: string }, R>(
448 project: string,
449 region: string,
450 nested: string,
451 list: (parent: string) => Effect.Effect<A[], E, R>,
452 ) =>
453 list(`projects/${project}/locations/-/${nested}`).pipe(
454 Effect.catchIf(
455 // Some collections reject the `-` location wildcard.
456 (error) => error._tag === "NotFound",
457 () => list(`projects/${project}/locations/${region}/${nested}`),
458 ),
459 );
460
461 export const listChildrenOf = <Parent, Child, E1, E2, R>(
462 parents: Effect.Effect<Parent[], E1, R>,
463 nameOf: (parent: Parent) => string | undefined,
464 listChildren: (parent: string) => Effect.Effect<Child[], E2, R>,
465 ) =>
466 parents.pipe(
467 Effect.flatMap((items) =>
468 Effect.forEach(
469 items,
470 (item) => {
471 const name = nameOf(item);
472 return name ? listChildren(name) : Effect.succeed([] as Child[]);
473 },
474 { concurrency: 5 },
475 ).pipe(Effect.map((chunks) => chunks.flat())),
476 ),
477 );