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, 04:45 UTC
| 1 | import * as dynamodb from "@distilled.cloud/aws/dynamodb"; |
| 2 | import * as s3 from "@distilled.cloud/aws/s3"; |
| 3 | import type { StateStoreImpl, StateStoreLifecycle } from "alchemy/Contributions"; |
| 4 | import * as Provisioning from "alchemy/State/Provisioning"; |
| 5 | import { StateStoreError } from "alchemy/State/State"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import { deleteAllObjects } from "../AssetsBucket.ts"; |
| 9 | import { AWSEnvironment } from "../Environment.ts"; |
| 10 | import { |
| 11 | createOverflowBucketName, |
| 12 | createStateTableName, |
| 13 | dynamoDBState, |
| 14 | provisionStateTable, |
| 15 | stateTableExists, |
| 16 | } from "../StateStore/DynamoDBState.ts"; |
| 17 | import { provisionStateBucket, stateBucketExists, stateEnvironment } from "../StateStore/State.ts"; |
| 18 | |
| 19 | /** |
| 20 | * Ensure a state table and its overflow bucket (by default the account's in |
| 21 | * this region), with the settings first use would give them. |
| 22 | */ |
| 23 | const bootstrapStateTable = Effect.fn("AWS.state.bootstrap")(function* (name?: string) { |
| 24 | const { accountId, region } = yield* AWSEnvironment.current; |
| 25 | if (name !== undefined) yield* Provisioning.storeName(name); |
| 26 | const tableName = createStateTableName(name); |
| 27 | const bucketName = createOverflowBucketName(accountId, region, name); |
| 28 | const existed = yield* stateTableExists(tableName); |
| 29 | yield* provisionStateTable(tableName); |
| 30 | // Provisioned ahead too, so a record that overflows mid-deploy in CI |
| 31 | // does not need to create it. |
| 32 | yield* provisionStateBucket(bucketName, region); |
| 33 | return { accountId, region, tableName, existed }; |
| 34 | }); |
| 35 | |
| 36 | /** |
| 37 | * Delete a state table, lifting its deletion protection, and empty and |
| 38 | * delete its overflow bucket, every object version included. The S3 store it |
| 39 | * replaced is kept: `--backend aws-s3` tears that down. A missing table or |
| 40 | * bucket is not an error. |
| 41 | */ |
| 42 | const teardownStateTable = Effect.fn("AWS.state.teardownTable")(function* (name?: string) { |
| 43 | const { accountId, region } = yield* AWSEnvironment.current; |
| 44 | if (name !== undefined) yield* Provisioning.storeName(name); |
| 45 | const tableName = createStateTableName(name); |
| 46 | const bucketName = createOverflowBucketName(accountId, region, name); |
| 47 | const deleted: string[] = []; |
| 48 | |
| 49 | const table = yield* dynamodb.describeTable({ TableName: tableName }).pipe( |
| 50 | Effect.map((result) => result.Table), |
| 51 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 52 | ); |
| 53 | if (table !== undefined) { |
| 54 | if (table.TableStatus !== "DELETING") { |
| 55 | if (table.DeletionProtectionEnabled) { |
| 56 | yield* dynamodb.updateTable({ TableName: tableName, DeletionProtectionEnabled: false }); |
| 57 | } |
| 58 | yield* dynamodb.deleteTable({ TableName: tableName }).pipe( |
| 59 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 60 | // Still creating or updating. |
| 61 | Effect.retry({ |
| 62 | while: (error) => error._tag === "ResourceInUseException", |
| 63 | schedule: Schedule.spaced("2 seconds"), |
| 64 | times: 30, |
| 65 | }), |
| 66 | ); |
| 67 | } |
| 68 | // Wait for it to go, so a bootstrap straight after creates a fresh one. |
| 69 | const remains = yield* stateTableExists(tableName).pipe( |
| 70 | Effect.repeat({ |
| 71 | until: (exists) => !exists, |
| 72 | schedule: Schedule.spaced("2 seconds"), |
| 73 | times: 30, |
| 74 | }), |
| 75 | ); |
| 76 | if (remains) { |
| 77 | return yield* new StateStoreError({ |
| 78 | message: `DynamoDB state table '${tableName}' is still deleting`, |
| 79 | }); |
| 80 | } |
| 81 | deleted.push(tableName); |
| 82 | } |
| 83 | |
| 84 | if (yield* stateBucketExists(bucketName)) { |
| 85 | yield* deleteAllObjects(bucketName); |
| 86 | yield* s3.deleteBucket({ Bucket: bucketName }).pipe( |
| 87 | Effect.catchTag("NoSuchBucket", () => Effect.void), |
| 88 | Effect.retry({ |
| 89 | while: (error) => error._tag === "OperationAborted" || error._tag === "ServiceUnavailable", |
| 90 | schedule: Schedule.max([Schedule.exponential(100), Schedule.recurs(10)]), |
| 91 | }), |
| 92 | ); |
| 93 | deleted.push(bucketName); |
| 94 | } |
| 95 | return { deleted }; |
| 96 | }); |
| 97 | |
| 98 | /** The AWS environment of the profile core runs the operation under. */ |
| 99 | const environment = stateEnvironment(); |
| 100 | |
| 101 | /** |
| 102 | * The DynamoDB state store's lifecycle, for `Alchemist.State` and |
| 103 | * `alchemy state`. The store also creates its table on first use, so |
| 104 | * bootstrapping is only needed to provision it ahead of time. |
| 105 | */ |
| 106 | export const lifecycle: StateStoreLifecycle = { |
| 107 | bootstrap: ({ name }) => |
| 108 | bootstrapStateTable(name).pipe( |
| 109 | Effect.map(({ tableName, accountId, region, existed }) => ({ |
| 110 | status: existed ? ("adopted" as const) : ("created" as const), |
| 111 | store: tableName, |
| 112 | location: { account: accountId, region }, |
| 113 | })), |
| 114 | Effect.provide(environment), |
| 115 | ), |
| 116 | teardown: ({ name }) => teardownStateTable(name).pipe(Effect.provide(environment)), |
| 117 | }; |
| 118 | |
| 119 | /** The state store AWS contributes, as `--backend aws`. */ |
| 120 | export const stateStore: StateStoreImpl = { |
| 121 | layer: (options) => dynamoDBState(options), |
| 122 | lifecycle, |
| 123 | }; |