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

main
ts 386 lines 14.3 KB
Raw
1 import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
2 import type * as dlp from "@distilled.cloud/gcp/dlp_v2";
3 import { createPhysicalName } from "alchemy/PhysicalName";
4 import * as Config from "effect/Config";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as Option from "effect/Option";
8 import * as Schedule from "effect/Schedule";
9 import * as Stream from "effect/Stream";
10 import { GcpEnvironment } from "../Environment.ts";
11 import { alchemyLabelKeys, createInternalLabels, hasAlchemyLabels } from "../Labels.ts";
12
13 /** Optional deploy-time setting from the ConfigProvider (env by default). */
14 const optionalSetting = (key: string) =>
15 Effect.gen(function* () {
16 const value = yield* Config.option(Config.String(key));
17 return Option.getOrUndefined(value)?.trim() || undefined;
18 });
19
20 export const MAX_ID_LENGTH = 100;
21 export const MAX_DESCRIPTION_LENGTH = 256;
22 export const MAX_DISPLAY_NAME_LENGTH = 100;
23 export const MAX_CONTENT_POLICY_DISPLAY_NAME_LENGTH = 63;
24 export const DEFAULT_LOCATION = "global";
25 export const DEFAULT_REGIONAL_LOCATION = "us";
26 /**
27 * Locations swept by `list`, alongside the stack region. `us-central1` stays
28 * so resources created under the former hard-coded default remain reachable.
29 */
30 export const LIST_LOCATIONS = ["us-central1", "global", "us"] as const;
31
32 export class DlpNotResolved extends Data.TaggedError("GCP.DLP.NotResolved")<{
33 resourceName: string;
34 }> {}
35
36 export class StoredInfoTypeNotReady extends Data.TaggedError("GCP.DLP.StoredInfoTypeNotReady")<{
37 resourceName: string;
38 state: string | undefined;
39 }> {}
40
41 export class StoredInfoTypeFailed extends Data.TaggedError("GCP.DLP.StoredInfoTypeFailed")<{
42 resourceName: string;
43 state: string;
44 message: string;
45 }> {}
46
47 const storedInfoTypeFailure = (
48 name: string,
49 version: dlp.GooglePrivacyDlpV2StoredInfoTypeVersion | undefined,
50 ) =>
51 version?.state === "FAILED" || version?.state === "INVALID"
52 ? new StoredInfoTypeFailed({
53 resourceName: name,
54 state: version.state,
55 message:
56 (version.errors ?? [])
57 .map((error) => error.details?.message ?? "")
58 .filter((message) => message.length > 0)
59 .join("; ") || `stored info type version is ${version.state}`,
60 })
61 : undefined;
62
63 /**
64 * Poll a stored info type until its current version is `READY` and no
65 * pending versions remain (create and update both build a new version
66 * asynchronously). Fails on a `FAILED`/`INVALID` version.
67 */
68 export const waitForStoredInfoTypeReady = <E extends { readonly _tag: string }, R>(
69 name: string,
70 get: Effect.Effect<dlp.GooglePrivacyDlpV2StoredInfoType | undefined, E, R>,
71 ) =>
72 get.pipe(
73 Effect.flatMap(
74 (
75 stored,
76 ): Effect.Effect<
77 dlp.GooglePrivacyDlpV2StoredInfoType,
78 StoredInfoTypeNotReady | StoredInfoTypeFailed
79 > => {
80 if (stored === undefined) {
81 return Effect.fail(new StoredInfoTypeNotReady({ resourceName: name, state: undefined }));
82 }
83 const pending = stored.pendingVersions ?? [];
84 const failed = storedInfoTypeFailure(
85 name,
86 pending[pending.length - 1] ?? stored.currentVersion,
87 );
88 if (failed !== undefined) return Effect.fail(failed);
89 if (stored.currentVersion?.state !== "READY" || pending.length > 0) {
90 return Effect.fail(
91 new StoredInfoTypeNotReady({
92 resourceName: name,
93 state: pending[0]?.state ?? stored.currentVersion?.state,
94 }),
95 );
96 }
97 return Effect.succeed(stored);
98 },
99 ),
100 Effect.retry({
101 while: (error) => error._tag === "GCP.DLP.StoredInfoTypeNotReady",
102 times: 120,
103 schedule: Schedule.spaced("5 seconds"),
104 }),
105 );
106
107 export class OrganizationNotResolved extends Data.TaggedError("GCP.DLP.OrganizationNotResolved")<{
108 project: string;
109 }> {}
110
111 export const lastSegment = (value: string) => {
112 const trimmed = value.replace(/\/+$/, "");
113 const parts = trimmed.split("/");
114 return parts[parts.length - 1] || trimmed;
115 };
116
117 export const parseName = (name: string, collection: string) => {
118 const parts = name.split("/").filter((part) => part.length > 0);
119 const collectionAt = parts.lastIndexOf(collection);
120 const locationsAt = parts.lastIndexOf("locations");
121 const projectsAt = parts.lastIndexOf("projects");
122 const orgsAt = parts.lastIndexOf("organizations");
123 return {
124 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
125 organization: orgsAt >= 0 && parts[orgsAt + 1] ? parts[orgsAt + 1]! : "",
126 location:
127 locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : DEFAULT_LOCATION,
128 id: collectionAt >= 0 && parts[collectionAt + 1] ? parts[collectionAt + 1]! : lastSegment(name),
129 parent:
130 collectionAt > 0
131 ? parts.slice(0, collectionAt).join("/")
132 : parts.slice(0, Math.max(0, parts.length - 1)).join("/"),
133 };
134 };
135
136 export const locationOf = (name: string, fallback = DEFAULT_LOCATION) => {
137 const parts = name.split("/");
138 const index = parts.indexOf("locations");
139 return index >= 0 ? (parts[index + 1] ?? fallback) : fallback;
140 };
141
142 export const organizationOf = (name: string) => {
143 const parts = name.split("/");
144 const index = parts.indexOf("organizations");
145 return index >= 0 ? (parts[index + 1] ?? "") : "";
146 };
147
148 export const projectOf = (name: string) => {
149 const parts = name.split("/");
150 const index = parts.indexOf("projects");
151 return index >= 0 ? (parts[index + 1] ?? "") : "";
152 };
153
154 export const organizationParent = (value: string) =>
155 value.startsWith("organizations/") ? value : `organizations/${lastSegment(value)}`;
156
157 export const organizationIdOf = (value: string) => lastSegment(value);
158
159 export const locationParent = (project: string, location: string) =>
160 `projects/${project}/locations/${location}`;
161
162 export const organizationLocationParent = (organization: string, location: string) =>
163 `${organizationParent(organization)}/locations/${location}`;
164
165 export const locationParentsOf = (organization: string, region: string) =>
166 Array.from(new Set<string>([...LIST_LOCATIONS, region])).map((location) =>
167 organizationLocationParent(organization, location),
168 );
169
170 export const organizationLocationParentsOf = locationParentsOf;
171
172 export const normalizeLocation = (location: string | undefined, fallback = DEFAULT_LOCATION) =>
173 lastSegment(location ?? fallback).toLowerCase();
174
175 export const jsonEqual = (left: unknown, right: unknown) =>
176 JSON.stringify(left ?? null) === JSON.stringify(right ?? null);
177
178 export const sameText = (left: string | undefined, right: string | undefined) =>
179 (left ?? "") === (right ?? "");
180
181 export const updateMaskOf = (...fields: Array<string | undefined>) =>
182 fields.filter((field): field is string => field !== undefined).join(",");
183
184 export const toResourceId = (
185 id: string,
186 requested: string | undefined,
187 existing: string | undefined,
188 prefix = "d",
189 ) =>
190 Effect.gen(function* () {
191 if (requested !== undefined) return requested;
192 if (existing !== undefined) return existing;
193 const generated = yield* createPhysicalName({
194 id,
195 maxLength: MAX_ID_LENGTH,
196 lowercase: true,
197 });
198 const next = /^[a-zA-Z]/.test(generated) ? generated : `${prefix}${generated}`;
199 return next.replace(/[^a-zA-Z0-9_-]/g, "-").slice(0, MAX_ID_LENGTH);
200 });
201
202 export const toPhysicalId = (
203 id: string,
204 explicit: string | undefined,
205 existing: string | undefined,
206 ) => toResourceId(id, explicit, existing);
207
208 const markerOf = (stack: string, stage: string, id: string) =>
209 `[alchemy ${alchemyLabelKeys.stack}=${stack} ${alchemyLabelKeys.stage}=${stage} ${alchemyLabelKeys.id}=${id}]`;
210
211 const fitMarker = (labels: Record<string, string>, maxLength: number) => {
212 let stack = labels[alchemyLabelKeys.stack] ?? "x";
213 let stage = labels[alchemyLabelKeys.stage] ?? "x";
214 let id = labels[alchemyLabelKeys.id] ?? "x";
215 let marker = markerOf(stack, stage, id);
216 while (marker.length > maxLength && (stack.length > 1 || stage.length > 1 || id.length > 1)) {
217 if (stack.length >= stage.length && stack.length >= id.length) {
218 stack = stack.slice(0, -1);
219 } else if (stage.length >= id.length) {
220 stage = stage.slice(0, -1);
221 } else {
222 id = id.slice(0, -1);
223 }
224 marker = markerOf(stack, stage, id);
225 }
226 return marker.slice(0, maxLength);
227 };
228
229 export const encodeOwnership = (
230 labels: Record<string, string>,
231 text: string | undefined,
232 maxLength = MAX_DESCRIPTION_LENGTH,
233 ): string => {
234 const marker = fitMarker(labels, maxLength);
235 const trimmed = text?.trim();
236 if (!trimmed) return marker.slice(0, maxLength);
237 return `${marker}\n${trimmed}`.slice(0, maxLength);
238 };
239
240 export const encodeOwnershipLine = (
241 labels: Record<string, string>,
242 text: string | undefined,
243 maxLength = MAX_DISPLAY_NAME_LENGTH,
244 ): string => {
245 const trimmed = text?.replace(/[\r\n]+/g, " ").trim();
246 const reserved =
247 trimmed && trimmed.length > 0 ? Math.min(trimmed.length + 1, Math.max(0, maxLength - 24)) : 0;
248 const marker = fitMarker(labels, Math.max(24, maxLength - reserved));
249 if (!trimmed) return marker.slice(0, maxLength);
250 return `${marker} ${trimmed}`.slice(0, maxLength);
251 };
252
253 export const encodeDescription = encodeOwnership;
254 export const encodeDisplayName = encodeOwnershipLine;
255
256 export const parseOwnership = (
257 text: string | undefined,
258 ): {
259 labels: Record<string, string>;
260 text: string | undefined;
261 } => {
262 if (!text?.startsWith("[alchemy ")) {
263 return { labels: {}, text };
264 }
265 const end = text.indexOf("]");
266 if (end < 0) return { labels: {}, text };
267 const labels: Record<string, string> = {};
268 for (const part of text.slice("[alchemy ".length, end).split(/\s+/)) {
269 const eq = part.indexOf("=");
270 if (eq > 0) {
271 labels[part.slice(0, eq)] = part.slice(eq + 1);
272 }
273 }
274 const rest = text.slice(end + 1).replace(/^[\s\n]+/, "");
275 return { labels, text: rest.length > 0 ? rest : undefined };
276 };
277
278 export const hasOwnershipMarker = (text: string | undefined) =>
279 Object.keys(parseOwnership(text).labels).some((key) => key.startsWith("alchemy-"));
280
281 const prefixMatch = (expected: string, observed: string) =>
282 expected === observed || expected.startsWith(observed) || observed.startsWith(expected);
283
284 export const ownedByAlchemy = (id: string, text: string | undefined) =>
285 Effect.gen(function* () {
286 const expected = yield* createInternalLabels(id);
287 const { labels } = parseOwnership(text);
288 if (!hasOwnershipMarker(text)) return false;
289 const exact = yield* hasAlchemyLabels(id, labels);
290 if (exact) return true;
291 return (
292 prefixMatch(expected[alchemyLabelKeys.stack] ?? "", labels[alchemyLabelKeys.stack] ?? "") &&
293 prefixMatch(expected[alchemyLabelKeys.stage] ?? "", labels[alchemyLabelKeys.stage] ?? "") &&
294 prefixMatch(expected[alchemyLabelKeys.id] ?? "", labels[alchemyLabelKeys.id] ?? "")
295 );
296 });
297
298 export const hybridLabels = (labels: Record<string, string>) => {
299 const startLetter = (value: string) => (/^[a-z]/.test(value) ? value : `x${value}`.slice(0, 63));
300 return {
301 [alchemyLabelKeys.stack]: startLetter(labels[alchemyLabelKeys.stack] ?? "x"),
302 [alchemyLabelKeys.stage]: startLetter(labels[alchemyLabelKeys.stage] ?? "x"),
303 [alchemyLabelKeys.id]: startLetter(labels[alchemyLabelKeys.id] ?? "x"),
304 };
305 };
306
307 export const hasHybridOwnership = (labels: Record<string, string | undefined> | null | undefined) =>
308 Object.keys(labels ?? {}).some((key) => key.startsWith("alchemy-"));
309
310 export const collectPages = <Page, A, E, R>(
311 pages: Stream.Stream<Page, E, R>,
312 items: (page: Page) => readonly A[] | undefined,
313 ) =>
314 pages.pipe(
315 Stream.flatMap((page) => Stream.fromIterable(items(page) ?? [])),
316 Stream.runCollect,
317 Effect.map((chunk) => Array.from(chunk)),
318 );
319
320 export const canonical = (value: unknown): unknown => {
321 if (value === undefined || value === null) return undefined;
322 if (typeof value === "boolean" || typeof value === "number") return value;
323 if (typeof value === "string") {
324 return value.length === 0 ? undefined : value;
325 }
326 if (Array.isArray(value)) {
327 const items = value.map(canonical).filter((item) => item !== undefined);
328 return items.length === 0 ? undefined : items;
329 }
330 if (typeof value === "object") {
331 const entries = Object.entries(value as Record<string, unknown>)
332 .map(([key, item]) => [key, canonical(item)] as const)
333 .filter(([, item]) => item !== undefined)
334 .sort(([left], [right]) => left.localeCompare(right));
335 if (entries.length === 0) return undefined;
336 return Object.fromEntries(entries);
337 }
338 return undefined;
339 };
340
341 export const fingerprint = (value: unknown): string => JSON.stringify(canonical(value) ?? null);
342
343 const parentOf = (name: string) =>
344 name.startsWith("projects/")
345 ? resourcemanager.getProjects({ name }).pipe(
346 Effect.map((resource) => resource.parent),
347 Effect.catchTag("NotFound", () => Effect.undefined),
348 )
349 : name.startsWith("folders/")
350 ? resourcemanager.getFolders({ name }).pipe(
351 Effect.map((folder) => folder.parent),
352 Effect.catchTag("NotFound", () => Effect.undefined),
353 )
354 : Effect.undefined;
355
356 export const tryResolveOrganization = () =>
357 Effect.gen(function* () {
358 const fromConfig = yield* optionalSetting("GOOGLE_ORGANIZATION_ID");
359 if (fromConfig) return organizationParent(fromConfig);
360 const env = yield* GcpEnvironment.current;
361 let current: string | undefined = `projects/${env.project}`;
362 for (let i = 0; i < 8; i++) {
363 if (current === undefined) return undefined;
364 if (current.startsWith("organizations/")) return current;
365 current = yield* parentOf(current);
366 }
367 return undefined;
368 });
369
370 export const resolveOrganization = (explicit: string | undefined, existing: string | undefined) =>
371 Effect.gen(function* () {
372 if (explicit !== undefined) return organizationParent(explicit);
373 if (existing !== undefined) return organizationParent(existing);
374 const resolved = yield* tryResolveOrganization();
375 if (resolved === undefined) {
376 const env = yield* GcpEnvironment.current;
377 return yield* new OrganizationNotResolved({ project: env.project });
378 }
379 return resolved;
380 });
381
382 export const replaceOnIdentity = (changed: boolean) =>
383 changed ? ({ action: "replace" as const, deleteFirst: false } as const) : undefined;
384
385 export const replaceOn = (previous: string | undefined, next: string | undefined) =>
386 replaceOnIdentity(previous !== undefined && next !== undefined && previous !== next);