The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as oam from "@distilled.cloud/aws/oam"; |
| 2 | import * as Retry from "@distilled.cloud/aws/Retry"; |
| 3 | import { createInternalTags, diffTags } from "alchemy/Tags"; |
| 4 | import * as Data from "effect/Data"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Schedule from "effect/Schedule"; |
| 7 | import * as Stream from "effect/Stream"; |
| 8 | |
| 9 | const mutationSchedule = Schedule.max([Schedule.spaced("1 second"), Schedule.recurs(20)]); |
| 10 | |
| 11 | const isRetryableOamMutationError = (error: unknown, conflict: boolean): boolean => { |
| 12 | if (!error || typeof error !== "object") return false; |
| 13 | const tag = (error as { _tag?: string })._tag; |
| 14 | return ( |
| 15 | tag === "TooManyRequestsException" || |
| 16 | tag === "ServiceQuotaExceededException" || |
| 17 | tag === "InternalServiceFault" || |
| 18 | (conflict && tag === "ConflictException") |
| 19 | ); |
| 20 | }; |
| 21 | |
| 22 | /** One non-nested, bounded retry owner for OAM control-plane mutations. */ |
| 23 | export const retryOamMutation = <A, E, R>( |
| 24 | effect: Effect.Effect<A, E, R>, |
| 25 | options?: { conflict?: boolean }, |
| 26 | ): Effect.Effect<A, E, Exclude<R, Retry.Retry>> => |
| 27 | Retry.policy({ |
| 28 | while: (error) => isRetryableOamMutationError(error, options?.conflict === true), |
| 29 | schedule: mutationSchedule, |
| 30 | })(effect); |
| 31 | |
| 32 | class SinkLinksStillAttached extends Data.TaggedError("SinkLinksStillAttached")<{ |
| 33 | readonly sinkArn: string; |
| 34 | readonly count: number; |
| 35 | }> {} |
| 36 | |
| 37 | class OamResourceStillExists extends Data.TaggedError("OamResourceStillExists")<{ |
| 38 | readonly arn: string; |
| 39 | }> {} |
| 40 | |
| 41 | const dependencySchedule = Schedule.max([Schedule.spaced("2 seconds"), Schedule.recurs(10)]); |
| 42 | |
| 43 | const goneSchedule = Schedule.max([Schedule.spaced("1 second"), Schedule.recurs(10)]); |
| 44 | |
| 45 | /** |
| 46 | * Delete a sink only after attached links have drained, then observe absence. |
| 47 | */ |
| 48 | export const deleteSinkAndWait = Effect.fn(function* (sinkArn: string) { |
| 49 | yield* oam.listAttachedLinks.pages({ SinkIdentifier: sinkArn }).pipe( |
| 50 | Stream.runCollect, |
| 51 | Effect.map((pages) => Array.from(pages).reduce((count, page) => count + page.Items.length, 0)), |
| 52 | Effect.flatMap((count) => |
| 53 | count === 0 ? Effect.void : Effect.fail(new SinkLinksStillAttached({ sinkArn, count })), |
| 54 | ), |
| 55 | Effect.retry({ |
| 56 | while: (error) => error._tag === "SinkLinksStillAttached", |
| 57 | schedule: dependencySchedule, |
| 58 | }), |
| 59 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 60 | ); |
| 61 | |
| 62 | yield* retryOamMutation(oam.deleteSink({ Identifier: sinkArn }), { |
| 63 | // A just-deleted link can remain visible to DeleteSink after the list has |
| 64 | // converged. Retry only this bounded dependency-violation window. |
| 65 | conflict: true, |
| 66 | }).pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 67 | |
| 68 | const observe = Retry.none(oam.getSink({ Identifier: sinkArn })); |
| 69 | yield* observe.pipe( |
| 70 | Effect.flatMap(() => Effect.fail(new OamResourceStillExists({ arn: sinkArn }))), |
| 71 | Effect.retry({ |
| 72 | while: (error) => |
| 73 | error._tag === "OamResourceStillExists" || error._tag === "TooManyRequestsException", |
| 74 | schedule: goneSchedule, |
| 75 | }), |
| 76 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 77 | ); |
| 78 | }); |
| 79 | |
| 80 | /** Idempotently delete a link and observe absence before its sink can delete. */ |
| 81 | export const deleteLinkAndWait = Effect.fn(function* (linkArn: string) { |
| 82 | yield* retryOamMutation(oam.deleteLink({ Identifier: linkArn })).pipe( |
| 83 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 84 | ); |
| 85 | |
| 86 | const observe = Retry.none(oam.getLink({ Identifier: linkArn })); |
| 87 | yield* observe.pipe( |
| 88 | Effect.flatMap(() => Effect.fail(new OamResourceStillExists({ arn: linkArn }))), |
| 89 | Effect.retry({ |
| 90 | while: (error) => |
| 91 | error._tag === "OamResourceStillExists" || error._tag === "TooManyRequestsException", |
| 92 | schedule: goneSchedule, |
| 93 | }), |
| 94 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 95 | ); |
| 96 | }); |
| 97 | |
| 98 | /** |
| 99 | * Read the observed tags on an OAM sink or link, tolerating a |
| 100 | * not-yet-visible resource (returns `{}`). |
| 101 | */ |
| 102 | export const readOamTags = Effect.fn(function* (resourceArn: string) { |
| 103 | return yield* oam.listTagsForResource({ ResourceArn: resourceArn }).pipe( |
| 104 | Effect.map((r) => (r.Tags ?? {}) as Record<string, string>), |
| 105 | Effect.catchTag(["ResourceNotFoundException", "ValidationException"], () => |
| 106 | Effect.succeed({} as Record<string, string>), |
| 107 | ), |
| 108 | ); |
| 109 | }); |
| 110 | |
| 111 | /** |
| 112 | * Converge the tags on an OAM sink or link to the desired user tags merged |
| 113 | * with the internal Alchemy ownership tags, diffing against OBSERVED cloud |
| 114 | * tags so adoption converges. |
| 115 | */ |
| 116 | export const syncOamTags = Effect.fn(function* ( |
| 117 | resourceArn: string, |
| 118 | id: string, |
| 119 | userTags: Record<string, string> | undefined, |
| 120 | ) { |
| 121 | const internalTags = yield* createInternalTags(id); |
| 122 | const desired = { ...userTags, ...internalTags }; |
| 123 | const observed = yield* readOamTags(resourceArn); |
| 124 | const { upsert, removed } = diffTags(observed, desired); |
| 125 | if (upsert.length > 0) { |
| 126 | yield* retryOamMutation( |
| 127 | oam.tagResource({ |
| 128 | ResourceArn: resourceArn, |
| 129 | Tags: Object.fromEntries(upsert.map((t) => [t.Key, t.Value])), |
| 130 | }), |
| 131 | ); |
| 132 | } |
| 133 | if (removed.length > 0) { |
| 134 | yield* retryOamMutation(oam.untagResource({ ResourceArn: resourceArn, TagKeys: removed })); |
| 135 | } |
| 136 | }); |