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
| 1 | /** |
| 2 | * Network connector lifecycle shared by `AWS.Lambda.NetworkConnector` and |
| 3 | * `AWS.Lambda.WebFunction` (which manages a connector for its `vpc`). |
| 4 | * |
| 5 | * NOT exported from `index.ts`. |
| 6 | */ |
| 7 | import * as lambdacore from "@distilled.cloud/aws/lambda-core"; |
| 8 | import { deepEqual } from "alchemy/Diff"; |
| 9 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 10 | import type { ScopedPlanStatusSession } from "alchemy/Report"; |
| 11 | import { createInternalTags } from "alchemy/Tags"; |
| 12 | import * as Data from "effect/Data"; |
| 13 | import * as Effect from "effect/Effect"; |
| 14 | import * as Schedule from "effect/Schedule"; |
| 15 | import type { NetworkConnectorProps } from "./NetworkConnector.ts"; |
| 16 | |
| 17 | // === Helpers =============================================================== |
| 18 | |
| 19 | export const resolveName = (id: string, name?: string) => |
| 20 | name ? Effect.succeed(name) : createPhysicalName({ id, maxLength: 64, delimiter: "-" }); |
| 21 | |
| 22 | // A freshly-created IAM operator role (and its inline policies) takes a few |
| 23 | // seconds to propagate before Lambda can assume it. A genuinely misconfigured |
| 24 | // role still fails once the bounded retry window elapses. |
| 25 | export const isOperatorRolePropagationError = (e: { _tag: string }) => |
| 26 | e._tag === "OperatorRoleNotAssumable"; |
| 27 | |
| 28 | export const retryRolePropagation = |
| 29 | (session: ScopedPlanStatusSession) => |
| 30 | <A, E extends { _tag: string }, R>(self: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> => |
| 31 | self.pipe( |
| 32 | Effect.tapError((e) => |
| 33 | isOperatorRolePropagationError(e) |
| 34 | ? session.note("Waiting for the operator role to become assumable by Lambda...") |
| 35 | : Effect.void, |
| 36 | ), |
| 37 | Effect.retry({ |
| 38 | while: (e) => isOperatorRolePropagationError(e), |
| 39 | schedule: Schedule.max([Schedule.fixed(1_000), Schedule.recurs(50)]), |
| 40 | }), |
| 41 | ); |
| 42 | |
| 43 | export const getConnector = (identifier: string) => |
| 44 | lambdacore |
| 45 | .getNetworkConnector({ Identifier: identifier }) |
| 46 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 47 | |
| 48 | export const normalizeList = (values: readonly string[] | undefined): string[] => |
| 49 | [...(values ?? [])].sort(); |
| 50 | |
| 51 | export const desiredEgress = ( |
| 52 | props: NetworkConnectorProps, |
| 53 | ): lambdacore.NetworkConnectorVpcEgressConfiguration => ({ |
| 54 | SubnetIds: props.subnetIds, |
| 55 | SecurityGroupIds: props.securityGroupIds, |
| 56 | // The API rejects a VPC_EGRESS connector without a protocol, so apply the |
| 57 | // documented default. |
| 58 | NetworkProtocol: props.networkProtocol ?? "IPv4", |
| 59 | // The API requires AssociatedComputeResourceTypes for VPC_EGRESS connectors, |
| 60 | // so apply the documented default when the prop is omitted. |
| 61 | AssociatedComputeResourceTypes: props.associatedComputeResourceTypes ?? ["MicroVm"], |
| 62 | }); |
| 63 | |
| 64 | export const egressEqual = ( |
| 65 | a: lambdacore.NetworkConnectorVpcEgressConfiguration | undefined, |
| 66 | b: lambdacore.NetworkConnectorVpcEgressConfiguration | undefined, |
| 67 | ): boolean => |
| 68 | deepEqual( |
| 69 | { |
| 70 | SubnetIds: normalizeList(a?.SubnetIds), |
| 71 | SecurityGroupIds: normalizeList(a?.SecurityGroupIds), |
| 72 | NetworkProtocol: a?.NetworkProtocol, |
| 73 | AssociatedComputeResourceTypes: normalizeList(a?.AssociatedComputeResourceTypes), |
| 74 | }, |
| 75 | { |
| 76 | SubnetIds: normalizeList(b?.SubnetIds), |
| 77 | SecurityGroupIds: normalizeList(b?.SecurityGroupIds), |
| 78 | NetworkProtocol: b?.NetworkProtocol, |
| 79 | AssociatedComputeResourceTypes: normalizeList(b?.AssociatedComputeResourceTypes), |
| 80 | }, |
| 81 | ); |
| 82 | |
| 83 | // Create the connector with ownership tags and wait for it to become ACTIVE. |
| 84 | export const createConnector = Effect.fn(function* ( |
| 85 | name: string, |
| 86 | news: NetworkConnectorProps, |
| 87 | id: string, |
| 88 | session: ScopedPlanStatusSession, |
| 89 | ) { |
| 90 | const internalTags = yield* createInternalTags(id); |
| 91 | yield* session.note(`Creating network connector ${name}...`); |
| 92 | const created = yield* lambdacore |
| 93 | .createNetworkConnector({ |
| 94 | Name: name, |
| 95 | Configuration: { VpcEgressConfiguration: desiredEgress(news) }, |
| 96 | OperatorRole: news.operatorRole, |
| 97 | Tags: { ...internalTags, ...news.tags }, |
| 98 | }) |
| 99 | .pipe( |
| 100 | retryRolePropagation(session), |
| 101 | // A concurrent create with the same name is a race; fall back to reading |
| 102 | // the existing connector. |
| 103 | Effect.catchTag("ResourceConflictException", () => |
| 104 | getConnector(name).pipe( |
| 105 | Effect.flatMap((existing) => |
| 106 | existing |
| 107 | ? Effect.succeed(existing) |
| 108 | : Effect.die(`Network connector ${name} conflicted but was not found.`), |
| 109 | ), |
| 110 | ), |
| 111 | ), |
| 112 | ); |
| 113 | return yield* waitForActive(created.Id ?? created.Arn, session); |
| 114 | }); |
| 115 | |
| 116 | // Apply config / operator-role changes against observed state, waiting for the |
| 117 | // update to settle; otherwise return the observed connector untouched. |
| 118 | export const syncConnector = Effect.fn(function* ( |
| 119 | connector: lambdacore.GetNetworkConnectorResponse, |
| 120 | name: string, |
| 121 | news: NetworkConnectorProps, |
| 122 | session: ScopedPlanStatusSession, |
| 123 | ) { |
| 124 | const observedEgress = connector.Configuration?.VpcEgressConfiguration; |
| 125 | if ( |
| 126 | egressEqual(observedEgress, desiredEgress(news)) && |
| 127 | (connector.OperatorRole ?? undefined) === news.operatorRole |
| 128 | ) { |
| 129 | return connector; |
| 130 | } |
| 131 | yield* session.note(`Updating network connector ${name}...`); |
| 132 | yield* lambdacore |
| 133 | .updateNetworkConnector({ |
| 134 | Identifier: connector.Id, |
| 135 | Configuration: { VpcEgressConfiguration: desiredEgress(news) }, |
| 136 | OperatorRole: news.operatorRole, |
| 137 | }) |
| 138 | .pipe(retryRolePropagation(session)); |
| 139 | return yield* waitForUpdate(connector.Id, session); |
| 140 | }); |
| 141 | |
| 142 | export class ConnectorPending extends Data.TaggedError("ConnectorPending")<{ |
| 143 | identifier: string; |
| 144 | state: string; |
| 145 | }> {} |
| 146 | |
| 147 | export class ConnectorFailed extends Data.TaggedError("ConnectorFailed")<{ |
| 148 | identifier: string; |
| 149 | state: string; |
| 150 | reason?: string; |
| 151 | }> {} |
| 152 | |
| 153 | export const waitForActive = (identifier: string, session: ScopedPlanStatusSession) => |
| 154 | Effect.gen(function* () { |
| 155 | const connector = yield* lambdacore.getNetworkConnector({ |
| 156 | Identifier: identifier, |
| 157 | }); |
| 158 | switch (connector.State) { |
| 159 | case "ACTIVE": |
| 160 | return connector; |
| 161 | case "FAILED": |
| 162 | case "DELETE_FAILED": |
| 163 | return yield* new ConnectorFailed({ |
| 164 | identifier, |
| 165 | state: connector.State, |
| 166 | reason: connector.StateReason ?? connector.StateReasonCode, |
| 167 | }); |
| 168 | default: |
| 169 | return yield* new ConnectorPending({ |
| 170 | identifier, |
| 171 | state: connector.State ?? "PENDING", |
| 172 | }); |
| 173 | } |
| 174 | }).pipe( |
| 175 | Effect.retry({ |
| 176 | while: (e) => e._tag === "ConnectorPending", |
| 177 | schedule: Schedule.max([Schedule.fixed(10_000), Schedule.recurs(72)]).pipe( |
| 178 | Schedule.tap(({ attempt }) => |
| 179 | session.note(`Waiting for network connector to become ACTIVE... (${attempt * 10}s)`), |
| 180 | ), |
| 181 | ), |
| 182 | }), |
| 183 | ); |
| 184 | |
| 185 | export const waitForUpdate = (identifier: string, session: ScopedPlanStatusSession) => |
| 186 | Effect.gen(function* () { |
| 187 | const connector = yield* lambdacore.getNetworkConnector({ |
| 188 | Identifier: identifier, |
| 189 | }); |
| 190 | switch (connector.LastUpdateStatus) { |
| 191 | case "Successful": |
| 192 | case undefined: |
| 193 | return connector; |
| 194 | case "Failed": |
| 195 | return yield* new ConnectorFailed({ |
| 196 | identifier, |
| 197 | state: connector.LastUpdateStatus, |
| 198 | reason: connector.LastUpdateStatusReason ?? connector.LastUpdateStatusReasonCode, |
| 199 | }); |
| 200 | default: |
| 201 | return yield* new ConnectorPending({ |
| 202 | identifier, |
| 203 | state: connector.LastUpdateStatus ?? "InProgress", |
| 204 | }); |
| 205 | } |
| 206 | }).pipe( |
| 207 | Effect.retry({ |
| 208 | while: (e) => e._tag === "ConnectorPending", |
| 209 | schedule: Schedule.max([Schedule.fixed(10_000), Schedule.recurs(72)]).pipe( |
| 210 | Schedule.tap(({ attempt }) => |
| 211 | session.note(`Waiting for network connector update... (${attempt * 10}s)`), |
| 212 | ), |
| 213 | ), |
| 214 | }), |
| 215 | ); |
| 216 | |
| 217 | export const waitForDeleted = (identifier: string, session: ScopedPlanStatusSession) => |
| 218 | Effect.gen(function* () { |
| 219 | const connector = yield* lambdacore |
| 220 | .getNetworkConnector({ Identifier: identifier }) |
| 221 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 222 | // A missing connector is the success signal — it has been deleted. |
| 223 | if (!connector) return; |
| 224 | if (connector.State === "DELETE_FAILED") { |
| 225 | return yield* new ConnectorFailed({ |
| 226 | identifier, |
| 227 | state: connector.State, |
| 228 | reason: connector.StateReason ?? connector.StateReasonCode, |
| 229 | }); |
| 230 | } |
| 231 | return yield* new ConnectorPending({ |
| 232 | identifier, |
| 233 | state: connector.State ?? "DELETING", |
| 234 | }); |
| 235 | }).pipe( |
| 236 | Effect.retry({ |
| 237 | while: (e) => e._tag === "ConnectorPending", |
| 238 | schedule: Schedule.max([Schedule.fixed(10_000), Schedule.recurs(72)]).pipe( |
| 239 | Schedule.tap(({ attempt }) => |
| 240 | session.note(`Waiting for network connector deletion... (${attempt * 10}s)`), |
| 241 | ), |
| 242 | ), |
| 243 | }), |
| 244 | ); |