@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, 04:45 UTC

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