@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, 06:00 UTC

main
ts 124 lines 4.49 KB
Raw
1 import * as osis from "@distilled.cloud/aws/osis";
2 import * as Data from "effect/Data";
3 import * as Effect from "effect/Effect";
4 import * as Schedule from "effect/Schedule";
5
6 /**
7 * Convert OSIS's `[{ Key, Value }]` tag list into a plain record, dropping
8 * any entry missing a key or value.
9 */
10 export const toTagRecord = (
11 tags: ReadonlyArray<{ Key?: string; Value?: string }> | undefined,
12 ): Record<string, string> =>
13 Object.fromEntries(
14 (tags ?? [])
15 .filter(
16 (tag): tag is { Key: string; Value: string } =>
17 typeof tag.Key === "string" && typeof tag.Value === "string",
18 )
19 .map((tag) => [tag.Key, tag.Value]),
20 );
21
22 /**
23 * Read the observed tags for an OSIS pipeline by ARN. A pipeline that has
24 * just been created (or is mid-transition) can transiently reject the call;
25 * treat any failure as "no observed tags" so tag reconciliation still runs.
26 */
27 export const readPipelineTags = Effect.fn(function* (arn: string) {
28 const response = yield* osis
29 .listTagsForResource({ Arn: arn })
30 .pipe(Effect.orElseSucceed(() => undefined));
31 return toTagRecord(response?.Tags);
32 });
33
34 /**
35 * An OSIS pipeline whose asynchronous create/update converged to a
36 * `*_FAILED` status.
37 */
38 export class PipelineOperationFailed extends Data.TaggedError("OsisPipelineOperationFailed")<{
39 readonly pipelineName: string;
40 readonly status: string;
41 readonly reason: string | undefined;
42 }> {}
43
44 const isTransitional = (status: string | undefined): boolean =>
45 status === "CREATING" ||
46 status === "UPDATING" ||
47 status === "STARTING" ||
48 status === "STOPPING" ||
49 status === "DELETING";
50
51 const isFailedStatus = (status: string | undefined): boolean =>
52 status === "CREATE_FAILED" || status === "UPDATE_FAILED" || status === "START_FAILED";
53
54 /**
55 * Poll an OSIS pipeline until its `Status` reaches a terminal value
56 * (bounded: 15s x 60 = ~15 minutes; pipeline creation typically takes 5-10
57 * minutes). `*_FAILED` fails immediately with the status reason. If the
58 * bounded schedule exhausts while still transitional, the last observation
59 * is returned as-is so the caller sees the real (still-converging) pipeline
60 * rather than a spurious failure.
61 *
62 * The explicit `Effect.Effect<A, E, R>` return annotation is load-bearing:
63 * inlining repeat/retry combinators in provider lifecycle code lets their
64 * conditional return types survive into declaration emit and widen the
65 * provider layer to `unknown` R, poisoning `AWS.providers()` downstream.
66 */
67 export const waitForPipelineSettled = <E extends { readonly _tag: string }, R>(
68 pipelineName: string,
69 read: Effect.Effect<osis.Pipeline | undefined, E, R>,
70 ): Effect.Effect<osis.Pipeline | undefined, E | PipelineOperationFailed, R> =>
71 Effect.flatMap(
72 Effect.repeat(read, {
73 schedule: Schedule.max([Schedule.fixed("15 seconds"), Schedule.recurs(60)]),
74 until: (pipeline) => pipeline === undefined || !isTransitional(pipeline.Status),
75 }),
76 (pipeline): Effect.Effect<osis.Pipeline | undefined, PipelineOperationFailed> =>
77 pipeline !== undefined && isFailedStatus(pipeline.Status)
78 ? Effect.fail(
79 new PipelineOperationFailed({
80 pipelineName,
81 status: pipeline.Status!,
82 reason: pipeline.StatusReason?.Description,
83 }),
84 )
85 : Effect.succeed(pipeline),
86 );
87
88 /**
89 * Retry an effect while OSIS reports `ConflictException` — raised when a
90 * delete/update races an in-flight state transition. Bounded: 15s x 20.
91 */
92 export const retryWhilePipelineConflict = <A, E extends { readonly _tag: string }, R>(
93 self: Effect.Effect<A, E, R>,
94 ): Effect.Effect<A, E, R> =>
95 Effect.retry(self, {
96 while: (e) => e._tag === "ConflictException",
97 schedule: Schedule.max([Schedule.fixed("15 seconds"), Schedule.recurs(20)]),
98 });
99
100 /** Structural deep equality over JSON-shaped values (order-insensitive keys). */
101 export const jsonEquals = (a: unknown, b: unknown): boolean => {
102 if (a === b) return true;
103 if (Array.isArray(a) && Array.isArray(b)) {
104 return a.length === b.length && a.every((v, i) => jsonEquals(v, b[i]));
105 }
106 if (
107 typeof a === "object" &&
108 a !== null &&
109 typeof b === "object" &&
110 b !== null &&
111 !Array.isArray(a) &&
112 !Array.isArray(b)
113 ) {
114 const ka = Object.keys(a as Record<string, unknown>);
115 const kb = Object.keys(b as Record<string, unknown>);
116 return (
117 ka.length === kb.length &&
118 ka.every((k) =>
119 jsonEquals((a as Record<string, unknown>)[k], (b as Record<string, unknown>)[k]),
120 )
121 );
122 }
123 return false;
124 };