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

main
ts 71 lines 2.86 KB
Raw
1 import * as datasync from "@distilled.cloud/aws/datasync";
2 import { diffTags } from "alchemy/Tags";
3 import * as Effect from "effect/Effect";
4 import * as Schedule from "effect/Schedule";
5 import * as Stream from "effect/Stream";
6
7 /**
8 * A freshly-created IAM role (or a just-attached inline policy) takes a
9 * while to propagate to DataSync: `CreateLocation*` transiently rejects it
10 * as `LocationRoleNotAssumable` (patched from `InvalidRequestException` +
11 * "Invalid IAM role") or fails its location access test as
12 * `LocationAccessTestFailed` (patched from `InvalidRequestException` +
13 * "location access test failed"). Bounded retry (~60s), explicitly typed so
14 * declaration emit never widens the provider layer (see PATTERNS §7).
15 */
16 export const retryWhileRoleNotAssumable = <A, E extends { _tag: string }, R>(
17 self: Effect.Effect<A, E, R>,
18 ): Effect.Effect<A, E, R> =>
19 Effect.retry(self, {
20 while: (e) => e._tag === "LocationRoleNotAssumable" || e._tag === "LocationAccessTestFailed",
21 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(20)]),
22 });
23
24 /** Convert a DataSync tag list to a plain record. */
25 export const dsTagsToRecord = (
26 tags: readonly datasync.TagListEntry[] | undefined,
27 ): Record<string, string> => Object.fromEntries((tags ?? []).map((t) => [t.Key, t.Value ?? ""]));
28
29 /** Read the observed tags currently attached to a DataSync resource. */
30 export const readObservedTags = Effect.fn(function* (resourceArn: string) {
31 const res = yield* datasync.listTagsForResource({ ResourceArn: resourceArn });
32 return dsTagsToRecord(res.Tags);
33 });
34
35 /**
36 * Diff observed cloud tags against the desired set and apply the delta.
37 * DataSync `tagResource` upserts; `untagResource` removes by key.
38 */
39 export const syncTags = Effect.fn(function* (
40 resourceArn: string,
41 observed: Record<string, string>,
42 desired: Record<string, string>,
43 ) {
44 const { upsert, removed } = diffTags(observed, desired);
45 if (upsert.length > 0) {
46 yield* datasync.tagResource({
47 ResourceArn: resourceArn,
48 Tags: upsert.map((t) => ({ Key: t.Key, Value: t.Value })),
49 });
50 }
51 if (removed.length > 0) {
52 yield* datasync.untagResource({ ResourceArn: resourceArn, Keys: removed });
53 }
54 });
55
56 /**
57 * Scan every DataSync location and return the ARN of the one whose
58 * `LocationUri` matches (ignoring a trailing slash). DataSync has no
59 * create-idempotency token, so this makes reconcile idempotent across state
60 * loss.
61 */
62 export const findLocationArnByUri = Effect.fn(function* (expectedUri: string) {
63 const strip = (u: string) => u.replace(/\/+$/, "");
64 const target = strip(expectedUri);
65 const locations = yield* datasync.listLocations.pages({}).pipe(
66 Stream.runCollect,
67 Effect.map((chunk) => Array.from(chunk).flatMap((p) => p.Locations ?? [])),
68 );
69 return locations.find((l) => l.LocationUri !== undefined && strip(l.LocationUri) === target)
70 ?.LocationArn;
71 });