The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as metastore from "@distilled.cloud/gcp/metastore_v1"; |
| 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 { |
| 9 | alchemyLabelKeys, |
| 10 | createInternalLabels, |
| 11 | hasAlchemyLabels, |
| 12 | stripInternalLabels, |
| 13 | } from "../Labels.ts"; |
| 14 | import { waitForOperation as waitForGcpOperation } from "../Operation.ts"; |
| 15 | |
| 16 | export const DEFAULT_HIVE_VERSION = "3.1.2"; |
| 17 | export const MAX_NAME_LENGTH = 63; |
| 18 | |
| 19 | export class ResourceNotResolved extends Data.TaggedError("GCP.Metastore.ResourceNotResolved")<{ |
| 20 | resourceName: string; |
| 21 | }> {} |
| 22 | |
| 23 | export class ResourceStillExists extends Data.TaggedError("GCP.Metastore.ResourceStillExists")<{ |
| 24 | resourceName: string; |
| 25 | }> {} |
| 26 | |
| 27 | export class ResourceNotReady extends Data.TaggedError("GCP.Metastore.ResourceNotReady")<{ |
| 28 | resourceName: string; |
| 29 | state: string; |
| 30 | }> {} |
| 31 | |
| 32 | export class ResourceFailed extends Data.TaggedError("GCP.Metastore.ResourceFailed")<{ |
| 33 | resourceName: string; |
| 34 | state: string; |
| 35 | details: string | undefined; |
| 36 | }> {} |
| 37 | |
| 38 | export const lastSegment = (value: string) => { |
| 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 = "metastore"): 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 = `m${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, defaultLocation: string) => |
| 58 | lastSegment(location ?? defaultLocation).toLowerCase(); |
| 59 | |
| 60 | export const locationParent = (project: string, location: string) => |
| 61 | `projects/${project}/locations/${lastSegment(location).toLowerCase()}`; |
| 62 | |
| 63 | export const toPhysicalId = ( |
| 64 | id: string, |
| 65 | explicit: string | undefined, |
| 66 | existing: string | undefined, |
| 67 | fallback = "metastore", |
| 68 | ) => |
| 69 | Effect.gen(function* () { |
| 70 | if (explicit !== undefined) return rfc1035(explicit, fallback); |
| 71 | if (existing !== undefined) return existing; |
| 72 | return rfc1035( |
| 73 | yield* createPhysicalName({ |
| 74 | id, |
| 75 | maxLength: MAX_NAME_LENGTH, |
| 76 | lowercase: true, |
| 77 | }), |
| 78 | fallback, |
| 79 | ); |
| 80 | }); |
| 81 | |
| 82 | export const parseName = (name: string, collection: string, defaultLocation: string) => { |
| 83 | const parts = name.split("/").filter((part) => part.length > 0); |
| 84 | const collectionAt = parts.lastIndexOf(collection); |
| 85 | const locationsAt = parts.lastIndexOf("locations"); |
| 86 | const projectsAt = parts.lastIndexOf("projects"); |
| 87 | return { |
| 88 | project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "", |
| 89 | location: |
| 90 | locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : defaultLocation, |
| 91 | id: collectionAt >= 0 && parts[collectionAt + 1] ? parts[collectionAt + 1]! : lastSegment(name), |
| 92 | parent: |
| 93 | collectionAt > 0 |
| 94 | ? parts.slice(0, collectionAt).join("/") |
| 95 | : parts.slice(0, Math.max(0, parts.length - 1)).join("/"), |
| 96 | }; |
| 97 | }; |
| 98 | |
| 99 | export const expandParent = ( |
| 100 | value: string, |
| 101 | project: string, |
| 102 | location: string, |
| 103 | collection: string, |
| 104 | ) => { |
| 105 | if (value.includes("/")) return value.replace(/\/+$/, ""); |
| 106 | return `projects/${project}/locations/${location}/${collection}/${value}`; |
| 107 | }; |
| 108 | |
| 109 | export const userLabels = ( |
| 110 | labels: Record<string, string | undefined> | null | undefined, |
| 111 | ): Record<string, string> => stripInternalLabels(tagRecord(labels)); |
| 112 | |
| 113 | export const hasAlchemyLabelMap = (labels: Record<string, string | undefined> | null | undefined) => |
| 114 | Object.keys(labels ?? {}).some((key) => key.startsWith("alchemy-")); |
| 115 | |
| 116 | export const canonical = (value: unknown): unknown => { |
| 117 | if (value === undefined || value === null) return undefined; |
| 118 | if (typeof value === "boolean" || typeof value === "number") return value; |
| 119 | if (typeof value === "string") { |
| 120 | return value.length === 0 ? undefined : value; |
| 121 | } |
| 122 | if (Array.isArray(value)) { |
| 123 | const items = value.map(canonical).filter((item) => item !== undefined); |
| 124 | return items.length === 0 ? undefined : items; |
| 125 | } |
| 126 | if (typeof value === "object") { |
| 127 | const entries = Object.entries(value as Record<string, unknown>) |
| 128 | .map(([key, item]) => [key, canonical(item)] as const) |
| 129 | .filter(([, item]) => item !== undefined) |
| 130 | .sort(([left], [right]) => left.localeCompare(right)); |
| 131 | if (entries.length === 0) return undefined; |
| 132 | return Object.fromEntries(entries); |
| 133 | } |
| 134 | return undefined; |
| 135 | }; |
| 136 | |
| 137 | export const fingerprint = (value: unknown): string => JSON.stringify(canonical(value) ?? null); |
| 138 | |
| 139 | export const fieldMask = (fields: Array<string | false | undefined>) => |
| 140 | fields.filter((field): field is string => typeof field === "string").join(","); |
| 141 | |
| 142 | export const sameText = (left: string | undefined, right: string | undefined) => |
| 143 | (left ?? "") === (right ?? ""); |
| 144 | |
| 145 | export const replaceOnIdentity = (input: { |
| 146 | previousId: string | undefined; |
| 147 | nextId: string | undefined; |
| 148 | previousLocation: string; |
| 149 | nextLocation: string; |
| 150 | extra?: boolean; |
| 151 | previousParent?: string; |
| 152 | nextParent?: string; |
| 153 | }) => { |
| 154 | const parentChanged = |
| 155 | (input.previousParent ?? "") !== "" && |
| 156 | (input.nextParent ?? "") !== "" && |
| 157 | (input.previousParent ?? "") !== (input.nextParent ?? ""); |
| 158 | const replace = |
| 159 | (input.extra ?? false) || |
| 160 | parentChanged || |
| 161 | (input.previousId !== undefined && |
| 162 | input.nextId !== undefined && |
| 163 | input.nextId !== input.previousId) || |
| 164 | input.previousLocation !== input.nextLocation; |
| 165 | if (!replace) return undefined; |
| 166 | const samePhysical = |
| 167 | input.previousLocation === input.nextLocation && |
| 168 | !parentChanged && |
| 169 | input.previousId !== undefined && |
| 170 | input.nextId === input.previousId; |
| 171 | return { |
| 172 | action: "replace" as const, |
| 173 | deleteFirst: samePhysical, |
| 174 | }; |
| 175 | }; |
| 176 | |
| 177 | const READY_STATES = new Set(["ACTIVE", "READY", "SUCCEEDED"]); |
| 178 | const FAILED_STATES = new Set(["FAILED", "ERROR"]); |
| 179 | |
| 180 | export const isReadyState = (state: string | undefined) => |
| 181 | READY_STATES.has((state ?? "").toUpperCase()); |
| 182 | |
| 183 | export const isFailedState = (state: string | undefined) => |
| 184 | FAILED_STATES.has((state ?? "").toUpperCase()); |
| 185 | |
| 186 | /** |
| 187 | * Wait for a Dataproc Metastore long-running operation. Service creates |
| 188 | * and deletes take 20-30 minutes. |
| 189 | */ |
| 190 | export const waitForOperation = (operation: metastore.Operation) => |
| 191 | waitForGcpOperation(operation, (name) => metastore.getProjectsLocationsOperations({ name }), { |
| 192 | budget: "40 minutes", |
| 193 | interval: "10 seconds", |
| 194 | }); |
| 195 | |
| 196 | export const waitUntilExists = <A, E extends { readonly _tag: string }, R>( |
| 197 | get: Effect.Effect<A, E, R>, |
| 198 | name: string, |
| 199 | ): Effect.Effect<Exclude<A, undefined>, E | ResourceNotResolved, R> => |
| 200 | get.pipe( |
| 201 | Effect.filterOrFail( |
| 202 | (value): value is Exclude<A, undefined> => value !== undefined, |
| 203 | () => new ResourceNotResolved({ resourceName: name }), |
| 204 | ), |
| 205 | Effect.retry({ |
| 206 | while: (error) => error._tag === "GCP.Metastore.ResourceNotResolved", |
| 207 | times: 8, |
| 208 | schedule: Schedule.spaced("1 second"), |
| 209 | }), |
| 210 | ); |
| 211 | |
| 212 | export const waitUntilGone = <A, E extends { readonly _tag: string }, R>( |
| 213 | get: Effect.Effect<A, E, R>, |
| 214 | name: string, |
| 215 | ): Effect.Effect<void, E | ResourceStillExists, R> => |
| 216 | get.pipe( |
| 217 | Effect.filterOrFail( |
| 218 | (value) => value === undefined, |
| 219 | () => new ResourceStillExists({ resourceName: name }), |
| 220 | ), |
| 221 | Effect.asVoid, |
| 222 | Effect.retry({ |
| 223 | while: (error) => error._tag === "GCP.Metastore.ResourceStillExists", |
| 224 | times: 10, |
| 225 | schedule: Schedule.spaced("5 seconds"), |
| 226 | }), |
| 227 | ); |
| 228 | |
| 229 | export const waitUntilReady = <A, E extends { readonly _tag: string }, R>( |
| 230 | get: Effect.Effect<A, E, R>, |
| 231 | name: string, |
| 232 | stateOf: (value: Exclude<A, undefined>) => string | undefined, |
| 233 | detailsOf?: (value: Exclude<A, undefined>) => string | undefined, |
| 234 | options?: { |
| 235 | times?: number; |
| 236 | interval?: `${number} seconds`; |
| 237 | }, |
| 238 | ): Effect.Effect< |
| 239 | Exclude<A, undefined>, |
| 240 | E | ResourceNotResolved | ResourceFailed | ResourceNotReady, |
| 241 | R |
| 242 | > => |
| 243 | get.pipe( |
| 244 | Effect.filterOrFail( |
| 245 | (value): value is Exclude<A, undefined> => value !== undefined, |
| 246 | () => new ResourceNotResolved({ resourceName: name }), |
| 247 | ), |
| 248 | Effect.filterOrFail( |
| 249 | (value) => !isFailedState(stateOf(value)), |
| 250 | (value) => |
| 251 | new ResourceFailed({ |
| 252 | resourceName: name, |
| 253 | state: stateOf(value) ?? "", |
| 254 | details: detailsOf?.(value), |
| 255 | }), |
| 256 | ), |
| 257 | Effect.filterOrFail( |
| 258 | (value) => { |
| 259 | const state = stateOf(value) ?? ""; |
| 260 | return isReadyState(state) || state.length === 0; |
| 261 | }, |
| 262 | (value) => new ResourceNotReady({ resourceName: name, state: stateOf(value) ?? "" }), |
| 263 | ), |
| 264 | Effect.retry({ |
| 265 | while: (error) => |
| 266 | error._tag === "GCP.Metastore.ResourceNotReady" || |
| 267 | error._tag === "GCP.Metastore.ResourceNotResolved", |
| 268 | times: options?.times ?? 8, |
| 269 | schedule: Schedule.spaced(options?.interval ?? "3 seconds"), |
| 270 | }), |
| 271 | ); |
| 272 | |
| 273 | // Metastore accepts the `locations/-` wildcard, so one call covers every region. |
| 274 | export const listAtLocation = <A, E, R>( |
| 275 | project: string, |
| 276 | list: (parent: string) => Effect.Effect<A[], E, R>, |
| 277 | ): Effect.Effect<A[], E, R> => list(`projects/${project}/locations/-`); |
| 278 | |
| 279 | export const listAtNested = <A, E, R>( |
| 280 | project: string, |
| 281 | nested: string, |
| 282 | list: (parent: string) => Effect.Effect<A[], E, R>, |
| 283 | ): Effect.Effect<A[], E, R> => list(`projects/${project}/locations/-/${nested}`); |
| 284 | |
| 285 | export const listLabeledPages = <Page, A, E, R>( |
| 286 | pages: Stream.Stream<Page, E, R>, |
| 287 | items: (page: Page) => readonly A[] | undefined, |
| 288 | labelsOf: (item: A) => Record<string, string | undefined> | null | undefined, |
| 289 | ): Effect.Effect<A[], E, R> => |
| 290 | listOwnedPages(pages, items, (item) => hasAlchemyLabelMap(labelsOf(item))); |
| 291 | |
| 292 | export const listOwnedPages = <Page, A, E, R>( |
| 293 | pages: Stream.Stream<Page, E, R>, |
| 294 | items: (page: Page) => readonly A[] | undefined, |
| 295 | owned: (item: A) => boolean, |
| 296 | ): Effect.Effect<A[], E, R> => |
| 297 | pages.pipe( |
| 298 | Stream.flatMap((page) => Stream.fromIterable(items(page) ?? [])), |
| 299 | Stream.filter(owned), |
| 300 | Stream.runCollect, |
| 301 | Effect.map((chunk) => Array.from(chunk)), |
| 302 | ); |
| 303 | |
| 304 | const markerOf = (labels: Record<string, string>, stack: string, stage: string, id: string) => |
| 305 | `[alchemy ${alchemyLabelKeys.stack}=${stack} ${alchemyLabelKeys.stage}=${stage} ${alchemyLabelKeys.id}=${id}]`; |
| 306 | |
| 307 | const fitMarker = (labels: Record<string, string>, maxLength: number) => { |
| 308 | let stack = labels[alchemyLabelKeys.stack] ?? "x"; |
| 309 | let stage = labels[alchemyLabelKeys.stage] ?? "x"; |
| 310 | let id = labels[alchemyLabelKeys.id] ?? "x"; |
| 311 | let marker = markerOf(labels, stack, stage, id); |
| 312 | while (marker.length > maxLength && (stack.length > 1 || stage.length > 1 || id.length > 1)) { |
| 313 | if (stack.length >= stage.length && stack.length >= id.length) { |
| 314 | stack = stack.slice(0, -1); |
| 315 | } else if (stage.length >= id.length) { |
| 316 | stage = stage.slice(0, -1); |
| 317 | } else { |
| 318 | id = id.slice(0, -1); |
| 319 | } |
| 320 | marker = markerOf(labels, stack, stage, id); |
| 321 | } |
| 322 | return marker.length <= maxLength ? marker : `${marker.slice(0, maxLength - 1)}]`; |
| 323 | }; |
| 324 | |
| 325 | export const encodeOwnership = ( |
| 326 | labels: Record<string, string>, |
| 327 | text: string | undefined, |
| 328 | ): string => { |
| 329 | const marker = fitMarker(labels, 8000); |
| 330 | const trimmed = text?.trim(); |
| 331 | return trimmed && trimmed.length > 0 ? `${marker}\n${trimmed}` : marker; |
| 332 | }; |
| 333 | |
| 334 | export const parseOwnership = ( |
| 335 | text: string | undefined, |
| 336 | ): { |
| 337 | labels: Record<string, string>; |
| 338 | text: string | undefined; |
| 339 | } => { |
| 340 | if (!text?.startsWith("[alchemy ")) { |
| 341 | return { labels: {}, text }; |
| 342 | } |
| 343 | const end = text.indexOf("]"); |
| 344 | if (end < 0) return { labels: {}, text }; |
| 345 | const labels: Record<string, string> = {}; |
| 346 | for (const part of text.slice("[alchemy ".length, end).split(/\s+/)) { |
| 347 | const eq = part.indexOf("="); |
| 348 | if (eq > 0) { |
| 349 | labels[part.slice(0, eq)] = part.slice(eq + 1); |
| 350 | } |
| 351 | } |
| 352 | const rest = text.slice(end + 1).replace(/^[\s\n]+/, ""); |
| 353 | return { labels, text: rest.length > 0 ? rest : undefined }; |
| 354 | }; |
| 355 | |
| 356 | export const hasOwnershipMarker = (text: string | undefined) => |
| 357 | Object.keys(parseOwnership(text).labels).some((key) => key.startsWith("alchemy-")); |
| 358 | |
| 359 | const prefixMatch = (expected: string, observed: string) => |
| 360 | expected === observed || expected.startsWith(observed) || observed.startsWith(expected); |
| 361 | |
| 362 | export const ownedByAlchemy = (id: string, text: string | undefined) => |
| 363 | Effect.gen(function* () { |
| 364 | const expected = yield* createInternalLabels(id); |
| 365 | const { labels } = parseOwnership(text); |
| 366 | if (!hasOwnershipMarker(text)) return false; |
| 367 | const exact = yield* hasAlchemyLabels(id, labels); |
| 368 | if (exact) return true; |
| 369 | return ( |
| 370 | prefixMatch(expected[alchemyLabelKeys.stack] ?? "", labels[alchemyLabelKeys.stack] ?? "") && |
| 371 | prefixMatch(expected[alchemyLabelKeys.stage] ?? "", labels[alchemyLabelKeys.stage] ?? "") && |
| 372 | prefixMatch(expected[alchemyLabelKeys.id] ?? "", labels[alchemyLabelKeys.id] ?? "") |
| 373 | ); |
| 374 | }); |