sailfork / t3code-github / .repos/ alchemy-effect/ packages/ gcp/ src/ TPU/ internal.ts

import * as tpu from "@distilled.cloud/gcp/tpu_v2";
import { createPhysicalName } from "alchemy/PhysicalName";
import { tagRecord } from "alchemy/Tags";
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Schedule from "effect/Schedule";
import * as Stream from "effect/Stream";
import { alchemyLabelKeys, stripInternalLabels } from "../Labels.ts";
import { waitForOperation as waitForGcpOperation } from "../Operation.ts";

// A zone, not a region: TPUs (and the v2-8 default) exist only in a few zones.
export const DEFAULT_LOCATION = "us-central1-c";
export const DEFAULT_ACCELERATOR = "v2-8";
export const DEFAULT_RUNTIME = "tpu-ubuntu2204-base";
export const DEFAULT_NETWORK = "default";
export const MAX_NAME_LENGTH = 63;

export class ResourceNotResolved extends Data.TaggedError("GCP.TPU.ResourceNotResolved")<{
  resourceName: string;
}> {}

export class ResourceStillExists extends Data.TaggedError("GCP.TPU.ResourceStillExists")<{
  resourceName: string;
}> {}

export class ResourceNotReady extends Data.TaggedError("GCP.TPU.ResourceNotReady")<{
  resourceName: string;
  state: string;
}> {}

export class ResourceFailed extends Data.TaggedError("GCP.TPU.ResourceFailed")<{
  resourceName: string;
  state: string;
  message: string | undefined;
}> {}

export const lastSegment = (value: string | undefined) => {
  if (value === undefined || value.length === 0) return "";
  const trimmed = value.replace(/\/+$/, "");
  const parts = trimmed.split("/");
  return parts[parts.length - 1] || trimmed;
};

export const rfc1035 = (name: string, fallback = "tpu"): string => {
  let next = name
    .toLowerCase()
    .replace(/[^a-z0-9-]/g, "-")
    .replace(/-+/g, "-")
    .replace(/^-+|-+$/g, "");
  if (!/^[a-z]/.test(next)) next = `t${next}`;
  next = next.slice(0, MAX_NAME_LENGTH).replace(/-+$/g, "");
  if (next.length === 0) return fallback;
  if (!/[a-z0-9]$/.test(next)) next = `${next.slice(0, MAX_NAME_LENGTH - 1)}0`;
  return next.slice(0, MAX_NAME_LENGTH);
};

export const normalizeLocation = (location: string | undefined, fallback = DEFAULT_LOCATION) =>
  lastSegment(location ?? fallback).toLowerCase();

export const toPhysicalId = (
  id: string,
  explicit: string | undefined,
  existing: string | undefined,
  fallback = "tpu",
) =>
  Effect.gen(function* () {
    if (explicit !== undefined) return rfc1035(explicit, fallback);
    if (existing !== undefined) return existing;
    return rfc1035(
      yield* createPhysicalName({
        id,
        maxLength: MAX_NAME_LENGTH,
        lowercase: true,
      }),
      fallback,
    );
  });

export const parseName = (name: string, collection: string) => {
  const parts = name.split("/").filter((part) => part.length > 0);
  const collectionAt = parts.lastIndexOf(collection);
  const locationsAt = parts.lastIndexOf("locations");
  const projectsAt = parts.lastIndexOf("projects");
  return {
    project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
    location:
      locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : DEFAULT_LOCATION,
    id: collectionAt >= 0 && parts[collectionAt + 1] ? parts[collectionAt + 1]! : lastSegment(name),
  };
};

export const userLabels = (
  labels: Record<string, string | undefined> | null | undefined,
): Record<string, string> => stripInternalLabels(tagRecord(labels));

export const hasAlchemyLabelMap = (labels: Record<string, string | undefined> | null | undefined) =>
  Object.keys(labels ?? {}).some((key) => key.startsWith("alchemy-"));

export const stringMapOf = (
  map: Record<string, string | undefined> | null | undefined,
): Record<string, string> =>
  Object.fromEntries(
    Object.entries(map ?? {}).filter((entry): entry is [string, string] => entry[1] !== undefined),
  );

export const mapKey = (map: Record<string, string | undefined> | null | undefined) =>
  JSON.stringify(
    Object.fromEntries(
      Object.entries(stringMapOf(map)).sort(([left], [right]) => left.localeCompare(right)),
    ),
  );

export const stringsOf = (values: ReadonlyArray<string | undefined> | null | undefined): string[] =>
  (values ?? []).filter((value): value is string => value !== undefined);

export const stringsKey = (values: ReadonlyArray<string | undefined> | null | undefined) =>
  JSON.stringify([...stringsOf(values)].sort());

export const encodeDescription = (
  labels: Record<string, string>,
  description: string | undefined,
): string => {
  const marker = `[alchemy ${alchemyLabelKeys.stack}=${labels[alchemyLabelKeys.stack]} ${alchemyLabelKeys.stage}=${labels[alchemyLabelKeys.stage]} ${alchemyLabelKeys.id}=${labels[alchemyLabelKeys.id]}]`;
  return description ? `${marker}\n${description}` : marker;
};

export const parseDescription = (
  description: string | undefined,
): {
  labels: Record<string, string>;
  description: string | undefined;
} => {
  if (!description?.startsWith("[alchemy ")) {
    return { labels: {}, description };
  }
  const end = description.indexOf("]");
  if (end < 0) return { labels: {}, description };
  const labels: Record<string, string> = {};
  for (const part of description.slice("[alchemy ".length, end).split(/\s+/)) {
    const eq = part.indexOf("=");
    if (eq > 0) {
      labels[part.slice(0, eq)] = part.slice(eq + 1);
    }
  }
  const rest = description.slice(end + 1).replace(/^\n/, "");
  return { labels, description: rest.length > 0 ? rest : undefined };
};

export const hasOwnershipMarker = (description: string | undefined) =>
  Object.keys(parseDescription(description).labels).some((key) => key.startsWith("alchemy-"));

export const fieldMask = (fields: Array<string | false | undefined>) =>
  fields.filter((field): field is string => typeof field === "string").join(",");

export const replaceOnIdentity = (input: {
  previousId: string | undefined;
  nextId: string | undefined;
  previousLocation: string;
  nextLocation: string;
  extra?: boolean;
}) => {
  const replace =
    (input.extra ?? false) ||
    (input.previousId !== undefined &&
      input.nextId !== undefined &&
      input.nextId !== input.previousId) ||
    input.previousLocation !== input.nextLocation;
  if (!replace) return undefined;
  const samePhysical =
    input.previousLocation === input.nextLocation &&
    input.previousId !== undefined &&
    input.nextId === input.previousId;
  return {
    action: "replace" as const,
    deleteFirst: samePhysical,
  };
};

/**
 * Wait for a Cloud TPU operation. TPU VM creates take several minutes
 * (large slices longer). An operation that finished with `ALREADY_EXISTS`
 * (6) is a lost create race; `notFoundOk` also accepts `NOT_FOUND` (5).
 */
export const waitForOperation = (operation: tpu.Operation, options?: { notFoundOk?: boolean }) =>
  waitForGcpOperation(
    operation,
    (name) =>
      tpu
        .getProjectsLocationsOperations({ name })
        .pipe(
          Effect.catchTag("NotFound", (error) =>
            options?.notFoundOk === true
              ? Effect.succeed<tpu.Operation>({ name, done: true })
              : Effect.fail(error),
          ),
        ),
    { budget: "30 minutes" },
  ).pipe(
    Effect.catchIf(
      (error) =>
        error._tag === "GCP.OperationFailed" &&
        (error.code === 6 || (options?.notFoundOk === true && error.code === 5)),
      () => Effect.void,
    ),
    Effect.asVoid,
  );

export const waitUntilExists = <A, E extends { readonly _tag: string }, R>(
  get: Effect.Effect<A | undefined, E, R>,
  name: string,
): Effect.Effect<NonNullable<A>, E | ResourceNotResolved, R> =>
  get.pipe(
    Effect.filterOrFail(
      (value): value is NonNullable<A> => value != null,
      () => new ResourceNotResolved({ resourceName: name }),
    ),
    Effect.retry({
      while: (error) => error._tag === "GCP.TPU.ResourceNotResolved",
      times: 8,
      schedule: Schedule.spaced("1 second"),
    }),
  );

export const waitUntilGone = <A, E extends { readonly _tag: string }, R>(
  get: Effect.Effect<A | undefined, E, R>,
  name: string,
): Effect.Effect<void, E | ResourceStillExists, R> =>
  get.pipe(
    Effect.filterOrFail(
      (value) => value === undefined,
      () => new ResourceStillExists({ resourceName: name }),
    ),
    Effect.retry({
      while: (error) => error._tag === "GCP.TPU.ResourceStillExists",
      times: 10,
      schedule: Schedule.spaced("8 seconds"),
    }),
    Effect.asVoid,
  );

const READY_STATES = new Set(["READY", "ACTIVE", "ACCEPTED"]);
const FAILED_STATES = new Set(["FAILED", "ERROR", "TERMINATED", "PREEMPTED", "UNKNOWN"]);

const isReadyState = (state: string | undefined) => READY_STATES.has((state ?? "").toUpperCase());

const isFailedState = (state: string | undefined) => FAILED_STATES.has((state ?? "").toUpperCase());

export const waitUntilReady = <A, E extends { readonly _tag: string }, R>(
  get: Effect.Effect<A | undefined, E, R>,
  name: string,
  stateOf: (value: NonNullable<A>) => string | undefined,
  messageOf?: (value: NonNullable<A>) => string | undefined,
): Effect.Effect<NonNullable<A>, E | ResourceNotResolved | ResourceFailed | ResourceNotReady, R> =>
  get.pipe(
    Effect.filterOrFail(
      (value): value is NonNullable<A> => value != null,
      () => new ResourceNotResolved({ resourceName: name }),
    ),
    Effect.filterOrFail(
      (value) => !isFailedState(stateOf(value)),
      (value) =>
        new ResourceFailed({
          resourceName: name,
          state: (stateOf(value) ?? "").toUpperCase(),
          message: messageOf?.(value),
        }),
    ),
    Effect.filterOrFail(
      (value) => {
        const state = stateOf(value) ?? "";
        return isReadyState(state) || state.length === 0;
      },
      (value) =>
        new ResourceNotReady({
          resourceName: name,
          state: (stateOf(value) ?? "").toUpperCase(),
        }),
    ),
    Effect.retry({
      while: (error) =>
        error._tag === "GCP.TPU.ResourceNotReady" || error._tag === "GCP.TPU.ResourceNotResolved",
      times: 10,
      schedule: Schedule.spaced("8 seconds"),
    }),
  );

export const listLabeledPages = <Page, A, E, R>(
  pages: Stream.Stream<Page, E, R>,
  items: (page: Page) => readonly A[] | undefined,
  labelsOf: (item: A) => Record<string, string | undefined> | null | undefined,
) =>
  pages.pipe(
    Stream.flatMap((page) => Stream.fromIterable(items(page) ?? [])),
    Stream.filter((item) => hasAlchemyLabelMap(labelsOf(item))),
    Stream.runCollect,
    Effect.map((chunk) => Array.from(chunk)),
  );