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

main
ts 607 lines 21.5 KB
Raw
1 import * as logging from "@distilled.cloud/gcp/logging_v2";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import { createPhysicalName } from "alchemy/PhysicalName";
5 import * as Provider from "alchemy/Provider";
6 import { Resource } from "alchemy/Resource";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import * as Stream from "effect/Stream";
11 import { GcpEnvironment } from "../Environment.ts";
12 import { alchemyLabelKeys, createInternalLabels, hasAlchemyLabels } from "../Labels.ts";
13 import type { Providers } from "../Providers.ts";
14 import { waitForDeleteOperation } from "./operations.ts";
15
16 const MAX_NAME_LENGTH = 100;
17 const DEFAULT_LOCATION = "global";
18 const DEFAULT_RETENTION_DAYS = 30;
19
20 export interface LogBucketIndexConfig {
21 /**
22 * LogEntry field path to index (e.g. `jsonPayload.request.status`).
23 */
24 fieldPath: string;
25 /**
26 * Indexed value type.
27 */
28 type: "INDEX_TYPE_STRING" | "INDEX_TYPE_INTEGER";
29 }
30
31 export interface LogBucketCmekSettings {
32 /**
33 * Cloud KMS key used to encrypt new log entries
34 * (`projects/{project}/locations/{location}/keyRings/{ring}/cryptoKeys/{key}`).
35 * Once set, CMEK cannot be disabled; the key may be rotated.
36 */
37 kmsKeyName: string;
38 }
39
40 export type LogBucketProps = {
41 /**
42 * Bucket id (the `{bucket}` segment of
43 * `projects/{project}/locations/{location}/buckets/{bucket}`). If omitted,
44 * a unique name is generated from the stack, stage, and logical id.
45 * Limited to 100 characters: letters, digits, underscores, hyphens,
46 * periods; first character must be alphanumeric. Immutable — changing
47 * it replaces the bucket.
48 */
49 bucketId?: string;
50 /**
51 * Location that stores log entries. `global` leaves placement unspecified.
52 * Immutable after create — changing it replaces the bucket.
53 * @default "global"
54 */
55 location?: string;
56 /**
57 * Human-readable description. Log buckets have no labels field, so
58 * Alchemy ownership is stored in a `[alchemy …]` prefix and stripped
59 * from attributes.
60 */
61 description?: string;
62 /**
63 * Days to retain log entries before they are deleted. Minimum 1. `0` at
64 * create uses the GCP default of 30. Cannot be changed while the bucket
65 * is locked.
66 * @default 30
67 */
68 retentionDays?: number;
69 /**
70 * When true, retention cannot be reduced and the bucket can only be
71 * deleted if empty. Locking cannot be undone.
72 * @default false
73 */
74 locked?: boolean;
75 /**
76 * Enable Log Analytics. Once enabled, it cannot be disabled.
77 * @default false
78 */
79 analyticsEnabled?: boolean;
80 /**
81 * Log entry field paths denied in this bucket (e.g. `textPayload`,
82 * `jsonPayload`).
83 */
84 restrictedFields?: string[];
85 /**
86 * Custom indexed fields. Automatically indexed paths cannot be changed.
87 */
88 indexConfigs?: LogBucketIndexConfig[];
89 /**
90 * Customer-managed encryption for new log entries. Once configured,
91 * CMEK cannot be disabled.
92 */
93 cmekSettings?: LogBucketCmekSettings;
94 };
95
96 export type LogBucket = Resource<
97 "GCP.Logging.LogBucket",
98 LogBucketProps,
99 {
100 /** Full resource name `projects/{project}/locations/{location}/buckets/{bucketId}`. */
101 name: string;
102 /** Bucket id (last path segment). */
103 bucketId: string;
104 /** Project id. */
105 project: string;
106 /** Location that stores log entries. */
107 location: string;
108 /** User description with the Alchemy ownership prefix stripped. */
109 description: string | undefined;
110 /** Retention period in days. */
111 retentionDays: number;
112 /** Whether the bucket is locked. */
113 locked: boolean;
114 /** Whether Log Analytics is enabled. */
115 analyticsEnabled: boolean;
116 /** Restricted field paths. */
117 restrictedFields: string[];
118 /** Custom index configs. */
119 indexConfigs: LogBucketIndexConfig[];
120 /** CMEK key name, if configured. */
121 cmekSettings: LogBucketCmekSettings | undefined;
122 /** Bucket lifecycle (`ACTIVE`, `DELETE_REQUESTED`, …). */
123 lifecycleState: string | undefined;
124 /** RFC3339 creation timestamp. Unset on GCP default buckets. */
125 createTime: string | undefined;
126 /** RFC3339 last-update timestamp. */
127 updateTime: string | undefined;
128 },
129 never,
130 Providers
131 >;
132
133 /**
134 * A Cloud Logging log bucket that stores log entries in a location.
135 *
136 * Log buckets have no labels field, so Alchemy stamps ownership into the
137 * description for `list` / nuke. `bucketId` and `location` are identity —
138 * changing either replaces the bucket. Delete marks the bucket
139 * `DELETE_REQUESTED`; GCP purges it after 7 days. Reconcile undeletes a
140 * bucket still in that grace period so the same name can be reused.
141 *
142 * ### Creating a Log Bucket
143 * **Example:** Generated name in `global`
144 * ```typescript
145 * const bucket = yield* GCP.Logging.LogBucket("AppLogs", {
146 * description: "application logs",
147 * });
148 * ```
149 *
150 * **Example:** Named bucket with custom retention
151 * ```typescript
152 * const bucket = yield* GCP.Logging.LogBucket("AppLogs", {
153 * bucketId: "app-logs",
154 * location: "global",
155 * description: "application logs",
156 * retentionDays: 31,
157 * });
158 * ```
159 *
160 * ### Updating a Log Bucket
161 * **Example:** Change description and retention
162 * ```typescript
163 * const bucket = yield* GCP.Logging.LogBucket("AppLogs", {
164 * location: "global",
165 * description: "retained application logs",
166 * retentionDays: 60,
167 * });
168 * ```
169 *
170 * @resource
171 * @category Logging
172 */
173 export const LogBucket = Resource<LogBucket>("GCP.Logging.LogBucket");
174
175 export class LogBucketNotResolved extends Data.TaggedError("GCP.Logging.LogBucketNotResolved")<{
176 resourceName: string;
177 }> {}
178
179 export class LogBucketFailed extends Data.TaggedError("GCP.Logging.LogBucketFailed")<{
180 resourceName: string;
181 state: string | undefined;
182 }> {}
183
184 const lastSegment = (value: string) => {
185 const trimmed = value.replace(/\/+$/, "");
186 const parts = trimmed.split("/");
187 return parts[parts.length - 1] || trimmed;
188 };
189
190 const resourceName = (project: string, location: string, bucketId: string) =>
191 `projects/${project}/locations/${location}/buckets/${bucketId}`;
192
193 const parseBucketName = (name: string) => {
194 const match = name.match(/^projects\/([^/]+)\/locations\/([^/]+)\/buckets\/([^/]+)$/);
195 if (!match) return undefined;
196 return { project: match[1]!, location: match[2]!, bucketId: match[3]! };
197 };
198
199 const bucketIdOf = (bucket: logging.LogBucket, fallback?: string) => {
200 const parsed = parseBucketName(bucket.name ?? "");
201 return parsed?.bucketId ?? fallback ?? lastSegment(bucket.name ?? "");
202 };
203
204 const locationOf = (bucket: logging.LogBucket, fallback: string) => {
205 const parsed = parseBucketName(bucket.name ?? "");
206 return parsed?.location ?? fallback;
207 };
208
209 const toId = (id: string, bucketId: string | undefined, existing?: string) =>
210 Effect.gen(function* () {
211 if (bucketId !== undefined) return bucketId;
212 if (existing !== undefined) return existing;
213 const generated = yield* createPhysicalName({
214 id,
215 maxLength: MAX_NAME_LENGTH,
216 lowercase: true,
217 });
218 return /^[a-z0-9]/.test(generated) ? generated : `b${generated}`.slice(0, MAX_NAME_LENGTH);
219 });
220
221 const encodeDescription = (
222 labels: Record<string, string>,
223 description: string | undefined,
224 ): string => {
225 const marker = `[alchemy ${alchemyLabelKeys.stack}=${labels[alchemyLabelKeys.stack]} ${alchemyLabelKeys.stage}=${labels[alchemyLabelKeys.stage]} ${alchemyLabelKeys.id}=${labels[alchemyLabelKeys.id]}]`;
226 return description ? `${marker}\n${description}` : marker;
227 };
228
229 const parseDescription = (
230 description: string | undefined,
231 ): {
232 labels: Record<string, string>;
233 description: string | undefined;
234 } => {
235 if (!description?.startsWith("[alchemy ")) {
236 return { labels: {}, description };
237 }
238 const end = description.indexOf("]");
239 if (end < 0) return { labels: {}, description };
240 const labels: Record<string, string> = {};
241 for (const part of description.slice("[alchemy ".length, end).split(/\s+/)) {
242 const eq = part.indexOf("=");
243 if (eq > 0) {
244 labels[part.slice(0, eq)] = part.slice(eq + 1);
245 }
246 }
247 const rest = description.slice(end + 1).replace(/^\n/, "");
248 return { labels, description: rest.length > 0 ? rest : undefined };
249 };
250
251 const hasOwnershipMarker = (description: string | undefined) =>
252 Object.keys(parseDescription(description).labels).some((key) => key.startsWith("alchemy-"));
253
254 const isDeleted = (bucket: logging.LogBucket | undefined): bucket is undefined =>
255 bucket === undefined || bucket.lifecycleState === "DELETE_REQUESTED";
256
257 const isPending = (state: string | undefined) => state === "CREATING" || state === "UPDATING";
258
259 const jsonEqual = (left: unknown, right: unknown) =>
260 JSON.stringify(left ?? null) === JSON.stringify(right ?? null);
261
262 const canonRestricted = (fields: readonly string[] | undefined) =>
263 [...(fields ?? [])].slice().sort();
264
265 const canonIndexConfigs = (
266 configs: readonly logging.IndexConfig[] | readonly LogBucketIndexConfig[] | undefined,
267 ): LogBucketIndexConfig[] =>
268 [...(configs ?? [])]
269 .flatMap((config) =>
270 config.fieldPath
271 ? [
272 {
273 fieldPath: config.fieldPath,
274 type: (config.type ?? "INDEX_TYPE_STRING") as LogBucketIndexConfig["type"],
275 },
276 ]
277 : [],
278 )
279 .sort((left, right) => left.fieldPath.localeCompare(right.fieldPath));
280
281 const toAttrs = (bucket: logging.LogBucket, project: string, location: string) => {
282 const bucketId = bucketIdOf(bucket);
283 const resolvedLocation = locationOf(bucket, location);
284 const parsed = parseDescription(bucket.description);
285 const cmekKey = bucket.cmekSettings?.kmsKeyName;
286 return {
287 name: bucket.name ?? (bucketId ? resourceName(project, resolvedLocation, bucketId) : ""),
288 bucketId,
289 project,
290 location: resolvedLocation,
291 description: parsed.description,
292 retentionDays: bucket.retentionDays ?? DEFAULT_RETENTION_DAYS,
293 locked: bucket.locked === true,
294 analyticsEnabled: bucket.analyticsEnabled === true,
295 restrictedFields: [...(bucket.restrictedFields ?? [])],
296 indexConfigs: canonIndexConfigs(bucket.indexConfigs),
297 cmekSettings: cmekKey ? { kmsKeyName: cmekKey } : undefined,
298 lifecycleState: bucket.lifecycleState,
299 createTime: bucket.createTime,
300 updateTime: bucket.updateTime,
301 };
302 };
303
304 const getByName = (name: string) =>
305 logging
306 .getProjectsLocationsBuckets({ name })
307 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
308
309 const undelete = (name: string) =>
310 logging
311 .undeleteProjectsLocationsBuckets({ name, body: {} })
312 .pipe(Effect.catchTag(["NotFound", "Conflict"], () => Effect.void));
313
314 const waitUntilActive = (name: string) =>
315 Effect.gen(function* () {
316 const bucket = yield* getByName(name);
317 if (bucket === undefined || isDeleted(bucket)) {
318 return yield* new LogBucketNotResolved({ resourceName: name });
319 }
320 if (bucket.lifecycleState === "FAILED") {
321 return yield* new LogBucketFailed({
322 resourceName: name,
323 state: bucket.lifecycleState,
324 });
325 }
326 if (isPending(bucket.lifecycleState)) {
327 return yield* new LogBucketNotResolved({ resourceName: name });
328 }
329 return bucket;
330 }).pipe(
331 Effect.retry({
332 while: (error) => error._tag === "GCP.Logging.LogBucketNotResolved",
333 times: 8,
334 schedule: Schedule.spaced("1 second"),
335 }),
336 );
337
338 const waitUntilDeleted = (name: string) =>
339 getByName(name).pipe(
340 Effect.flatMap((bucket) =>
341 isDeleted(bucket)
342 ? Effect.void
343 : Effect.fail(new LogBucketNotResolved({ resourceName: name })),
344 ),
345 Effect.retry({
346 while: (error) => error._tag === "GCP.Logging.LogBucketNotResolved",
347 times: 8,
348 schedule: Schedule.spaced("1 second"),
349 }),
350 );
351
352 const toCreateBody = (props: LogBucketProps, description: string): logging.LogBucket => ({
353 description,
354 retentionDays: props.retentionDays,
355 locked: props.locked === true ? true : undefined,
356 analyticsEnabled: props.analyticsEnabled === true ? true : undefined,
357 restrictedFields:
358 props.restrictedFields && props.restrictedFields.length > 0
359 ? [...props.restrictedFields]
360 : undefined,
361 indexConfigs:
362 props.indexConfigs && props.indexConfigs.length > 0
363 ? props.indexConfigs.map((config) => ({
364 fieldPath: config.fieldPath,
365 type: config.type,
366 }))
367 : undefined,
368 cmekSettings: props.cmekSettings ? { kmsKeyName: props.cmekSettings.kmsKeyName } : undefined,
369 });
370
371 export const LogBucketProvider = () =>
372 Provider.succeed(LogBucket, {
373 stables: ["name", "bucketId", "project", "location", "createTime"],
374
375 diff: Effect.fn(function* ({ news, olds, output }) {
376 if (!isResolved(news)) return undefined;
377 const previousId = olds?.bucketId ?? output?.bucketId;
378 const idChanged =
379 previousId !== undefined && news.bucketId !== undefined && news.bucketId !== previousId;
380 const previousLocation = olds?.location ?? output?.location;
381 const locationChanged =
382 previousLocation !== undefined &&
383 news.location !== undefined &&
384 news.location !== previousLocation;
385 if (!idChanged && !locationChanged) return undefined;
386 return { action: "replace" as const, deleteFirst: false };
387 }),
388
389 read: Effect.fn(function* ({ id, olds, output }) {
390 const env = yield* GcpEnvironment.current;
391 const location = olds?.location ?? output?.location ?? DEFAULT_LOCATION;
392 const bucketId = yield* toId(id, olds?.bucketId, output?.bucketId);
393 const name = output?.name ?? resourceName(env.project, location, bucketId);
394 const existing = yield* getByName(name);
395 if (isDeleted(existing)) return undefined;
396 const attrs = toAttrs(existing, env.project, location);
397 const { labels } = parseDescription(existing.description);
398 return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs);
399 }),
400
401 list: () =>
402 Effect.gen(function* () {
403 const env = yield* GcpEnvironment.current;
404 return yield* logging.listProjectsLocationsBuckets
405 .pages({
406 parent: `projects/${env.project}/locations/-`,
407 pageSize: 1000,
408 })
409 .pipe(
410 Stream.flatMap((page) => Stream.fromIterable(page.buckets ?? [])),
411 Stream.filter((bucket) => !isDeleted(bucket) && hasOwnershipMarker(bucket.description)),
412 Stream.map((bucket) =>
413 toAttrs(bucket, env.project, locationOf(bucket, DEFAULT_LOCATION)),
414 ),
415 Stream.runCollect,
416 Effect.map((chunk) => Array.from(chunk)),
417 );
418 }),
419
420 reconcile: Effect.fn(function* ({ id, news, output }) {
421 const env = yield* GcpEnvironment.current;
422 const location = news.location ?? output?.location ?? DEFAULT_LOCATION;
423 const bucketId = yield* toId(id, news.bucketId, output?.bucketId);
424 const name = resourceName(env.project, location, bucketId);
425 const ownership = yield* createInternalLabels(id);
426 const desiredDescription = encodeDescription(ownership, news.description);
427
428 let current = yield* getByName(output?.name ?? name);
429
430 if (current !== undefined && current.lifecycleState === "DELETE_REQUESTED") {
431 yield* undelete(current.name ?? name);
432 current = yield* waitUntilActive(current.name ?? name);
433 }
434
435 if (current === undefined) {
436 const created = yield* logging
437 .createProjectsLocationsBuckets({
438 parent: `projects/${env.project}/locations/${location}`,
439 bucketId,
440 body: toCreateBody(news, desiredDescription),
441 })
442 .pipe(Effect.catchTag("Conflict", () => getByName(name)));
443 current = created ?? undefined;
444 if (current !== undefined && current.lifecycleState === "DELETE_REQUESTED") {
445 yield* undelete(current.name ?? name);
446 current = yield* waitUntilActive(current.name ?? name);
447 } else if (current !== undefined && isPending(current.lifecycleState)) {
448 current = yield* waitUntilActive(current.name ?? name);
449 }
450 }
451
452 if (isDeleted(current)) {
453 return yield* new LogBucketNotResolved({ resourceName: name });
454 }
455
456 const desiredLocked = news.locked === true;
457 const desiredAnalytics = news.analyticsEnabled === true;
458 const descriptionChanged = (current.description ?? "") !== desiredDescription;
459 const retentionChanged =
460 news.retentionDays !== undefined &&
461 current.locked !== true &&
462 (current.retentionDays ?? DEFAULT_RETENTION_DAYS) !== news.retentionDays;
463 const restrictedChanged =
464 news.restrictedFields !== undefined &&
465 !jsonEqual(
466 canonRestricted(current.restrictedFields),
467 canonRestricted(news.restrictedFields),
468 );
469 const indexChanged =
470 news.indexConfigs !== undefined &&
471 !jsonEqual(canonIndexConfigs(current.indexConfigs), canonIndexConfigs(news.indexConfigs));
472 const analyticsChanged =
473 news.analyticsEnabled !== undefined &&
474 desiredAnalytics &&
475 current.analyticsEnabled !== true;
476 const cmekChanged =
477 news.cmekSettings !== undefined &&
478 (current.cmekSettings?.kmsKeyName ?? "") !== news.cmekSettings.kmsKeyName;
479 const lockedChanged = news.locked !== undefined && desiredLocked && current.locked !== true;
480
481 const syncMask = [
482 descriptionChanged ? "description" : undefined,
483 retentionChanged ? "retentionDays" : undefined,
484 restrictedChanged ? "restrictedFields" : undefined,
485 indexChanged ? "indexConfigs" : undefined,
486 analyticsChanged ? "analyticsEnabled" : undefined,
487 cmekChanged ? "cmekSettings" : undefined,
488 ].filter((field): field is string => field !== undefined);
489
490 if (syncMask.length > 0) {
491 const body: logging.LogBucket = {};
492 if (descriptionChanged) body.description = desiredDescription;
493 if (retentionChanged) body.retentionDays = news.retentionDays;
494 if (restrictedChanged) {
495 body.restrictedFields = [...(news.restrictedFields ?? [])];
496 }
497 if (indexChanged) {
498 body.indexConfigs = (news.indexConfigs ?? []).map((config) => ({
499 fieldPath: config.fieldPath,
500 type: config.type,
501 }));
502 }
503 if (analyticsChanged) body.analyticsEnabled = true;
504 if (cmekChanged) {
505 body.cmekSettings = { kmsKeyName: news.cmekSettings?.kmsKeyName };
506 }
507 current = yield* logging.patchProjectsLocationsBuckets({
508 name: current.name ?? name,
509 updateMask: syncMask.join(","),
510 body,
511 });
512 if (isPending(current.lifecycleState)) {
513 current = yield* waitUntilActive(current.name ?? name);
514 }
515 }
516
517 if (lockedChanged) {
518 current = yield* logging.patchProjectsLocationsBuckets({
519 name: current.name ?? name,
520 updateMask: "locked",
521 body: { locked: true },
522 });
523 if (isPending(current.lifecycleState)) {
524 current = yield* waitUntilActive(current.name ?? name);
525 }
526 }
527
528 return toAttrs(current, env.project, location);
529 }),
530
531 delete: Effect.fn(function* ({ output }) {
532 const name = output.name;
533 if (!name) return;
534
535 yield* getByName(name).pipe(
536 Effect.flatMap((bucket) => {
537 if (isDeleted(bucket)) return Effect.void;
538 if (bucket !== undefined && isPending(bucket.lifecycleState)) {
539 return Effect.fail(new LogBucketNotResolved({ resourceName: name }));
540 }
541 return Effect.void;
542 }),
543 Effect.retry({
544 while: (error) => error._tag === "GCP.Logging.LogBucketNotResolved",
545 times: 10,
546 schedule: Schedule.spaced("2 seconds"),
547 }),
548 );
549
550 const live = yield* getByName(name);
551 if (isDeleted(live)) return;
552
553 const ignoreIfGone = (error: { readonly _tag: "BadRequest" }) =>
554 getByName(name).pipe(
555 Effect.flatMap((bucket) => (isDeleted(bucket) ? Effect.void : Effect.fail(error))),
556 );
557
558 const deleteBucket = logging.deleteProjectsLocationsBuckets({ name }).pipe(
559 Effect.catchTags({
560 NotFound: () => Effect.void,
561 BadRequest: ignoreIfGone,
562 }),
563 );
564
565 yield* deleteBucket.pipe(
566 Effect.catchTag("BadRequest", () =>
567 Effect.gen(function* () {
568 const links = yield* logging.listProjectsLocationsBucketsLinks
569 .pages({
570 parent: name,
571 pageSize: 100,
572 })
573 .pipe(
574 Stream.flatMap((page) => Stream.fromIterable(page.links ?? [])),
575 Stream.runCollect,
576 Effect.map((chunk) => Array.from(chunk)),
577 Effect.catchTag("NotFound", () => Effect.succeed([] as logging.Link[])),
578 );
579 yield* Effect.forEach(
580 links,
581 (link) =>
582 link.name
583 ? logging
584 .deleteProjectsLocationsBucketsLinks({
585 name: link.name,
586 })
587 .pipe(
588 Effect.flatMap((operation) => waitForDeleteOperation(operation)),
589 Effect.catchTag(["NotFound", "BadRequest"], () => Effect.void),
590 )
591 : Effect.void,
592 { concurrency: 4 },
593 );
594 yield* deleteBucket.pipe(
595 Effect.retry({
596 while: (error) => error._tag === "BadRequest",
597 times: 8,
598 schedule: Schedule.spaced("2 seconds"),
599 }),
600 Effect.catchTag("BadRequest", ignoreIfGone),
601 );
602 }),
603 ),
604 );
605 yield* waitUntilDeleted(name);
606 }),
607 });