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

main
ts 83 lines 2.8 KB
Raw
1 import * as mm from "@distilled.cloud/aws/mailmanager";
2 import { diffTags, tagRecord } from "alchemy/Tags";
3 import * as Effect from "effect/Effect";
4 import * as Schedule from "effect/Schedule";
5
6 /**
7 * Read the observed tags for any Mail Manager resource ARN. Tag reads are
8 * best-effort — a race with deletion (or a missing-tags edge) degrades to an
9 * empty record rather than failing the lifecycle operation.
10 */
11 export const readMailManagerTags = (arn: string) =>
12 mm.listTagsForResource({ ResourceArn: arn }).pipe(
13 Effect.map((r) => tagRecord(r.Tags ?? [])),
14 Effect.orElseSucceed(() => ({})),
15 );
16
17 /**
18 * Converge the tags on a Mail Manager resource to `desired`, diffing against
19 * the OBSERVED cloud tags so adoption converges.
20 */
21 export const syncMailManagerTags = Effect.fn(function* (
22 arn: string,
23 desired: Record<string, string>,
24 ) {
25 const current = yield* readMailManagerTags(arn);
26 const { upsert, removed } = diffTags(current, desired);
27 if (upsert.length > 0) {
28 yield* mm.tagResource({ ResourceArn: arn, Tags: upsert });
29 }
30 if (removed.length > 0) {
31 yield* mm.untagResource({ ResourceArn: arn, TagKeys: removed });
32 }
33 });
34
35 /**
36 * Retry an operation while Mail Manager reports a ConflictException — e.g.
37 * deleting an ingress point that is still PROVISIONING. Explicitly typed so
38 * `Retry.Return`'s conditional type never leaks into declaration emit.
39 */
40 export const retryWhileMailManagerConflict = <A, E extends { readonly _tag: string }, R>(
41 self: Effect.Effect<A, E, R>,
42 ): Effect.Effect<A, E, R> =>
43 Effect.retry(self, {
44 while: (e) => e._tag === "ConflictException",
45 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(10)]),
46 });
47
48 /**
49 * Repeat a poll until `done` holds (bounded). Explicitly typed for the same
50 * declaration-emit reason as above.
51 */
52 export const repeatUntilMailManagerStable = <A, E, R>(
53 self: Effect.Effect<A, E, R>,
54 done: (a: A) => boolean,
55 times = 24,
56 ): Effect.Effect<A, E, R> =>
57 Effect.repeat(self, {
58 schedule: Schedule.spaced("5 seconds"),
59 until: done,
60 times,
61 });
62
63 /**
64 * Deep structural equality on JSON-ish values with key order and
65 * `undefined`-valued members normalized away. Used to compare desired config
66 * structs (rules, policy statements, authentication) against observed cloud
67 * state.
68 */
69 const normalize = (value: unknown): unknown => {
70 if (Array.isArray(value)) return value.map(normalize);
71 if (value !== null && typeof value === "object") {
72 return Object.fromEntries(
73 Object.entries(value as Record<string, unknown>)
74 .filter(([, v]) => v !== undefined)
75 .sort(([l], [r]) => l.localeCompare(r))
76 .map(([k, v]) => [k, normalize(v)]),
77 );
78 }
79 return value;
80 };
81
82 export const sameShape = (l: unknown, r: unknown) =>
83 JSON.stringify(normalize(l)) === JSON.stringify(normalize(r));