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 | import * as Clock from "effect/Clock" |
| 2 | import * as Console from "effect/Console" |
| 3 | import * as Context from "effect/Context" |
| 4 | import * as Effect from "effect/Effect" |
| 5 | import type * as HttpClient from "effect/http/HttpClient" |
| 6 | import * as Layer from "effect/Layer" |
| 7 | import type * as Redacted from "effect/Redacted" |
| 8 | import type { GitHubRepository } from "./GitHub.ts" |
| 9 | import { |
| 10 | diagnosePending, |
| 11 | type Diagnosis, |
| 12 | liveLatest, |
| 13 | type NpmCli, |
| 14 | promote, |
| 15 | type PromoteResult, |
| 16 | type ReleasePackage |
| 17 | } from "./NpmPromote.ts" |
| 18 | import { NpmPromoteError } from "./NpmPromoteError.ts" |
| 19 | import * as TrackingIssue from "./TrackingIssue.ts" |
| 20 | import type { OverdueRelease } from "./TrackingIssue.ts" |
| 21 | import * as WebsiteDeploy from "./WebsiteDeploy.ts" |
| 22 | import type { VersionBump } from "./Workspace.ts" |
| 23 | |
| 24 | /** |
| 25 | * Website deployments, with a persisted record of which revisions were |
| 26 | * deployed so a failed or interrupted deploy is retried by the next run. |
| 27 | */ |
| 28 | export class Website extends Context.Service<Website, { |
| 29 | readonly isDeployed: (revision: string) => Effect.Effect<boolean, NpmPromoteError> |
| 30 | /** Requests the deployment and records it once the request succeeded. */ |
| 31 | readonly deploy: (revision: string) => Effect.Effect<void, NpmPromoteError> |
| 32 | }>()("@effect/npm-promote/Website") { |
| 33 | /** |
| 34 | * Records deploys as GitHub deployments in `github.repository` and sends the |
| 35 | * dispatch to Effect-TS/website with `dispatchToken`. |
| 36 | */ |
| 37 | static readonly layerGitHub = (github: GitHubRepository, dispatchToken: Redacted.Redacted<string>) => |
| 38 | Layer.effect( |
| 39 | Website, |
| 40 | Effect.map(Effect.context<HttpClient.HttpClient>(), (context) => |
| 41 | Website.of({ |
| 42 | isDeployed: (revision) => Effect.provide(WebsiteDeploy.isDeployed(github, revision), context), |
| 43 | deploy: (revision) => Effect.provide(WebsiteDeploy.deploy(github, dispatchToken, revision), context) |
| 44 | })) |
| 45 | ) |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * The single tracking issue for an overdue release. |
| 50 | */ |
| 51 | export class Alerts extends Context.Service<Alerts, { |
| 52 | readonly open: (release: OverdueRelease) => Effect.Effect<void, NpmPromoteError> |
| 53 | readonly resolve: Effect.Effect<void, NpmPromoteError> |
| 54 | }>()("@effect/npm-promote/Alerts") { |
| 55 | static readonly layerGitHub = (github: GitHubRepository) => |
| 56 | Layer.effect( |
| 57 | Alerts, |
| 58 | Effect.map(Effect.context<HttpClient.HttpClient>(), (context) => |
| 59 | Alerts.of({ |
| 60 | open: (release) => |
| 61 | TrackingIssue.upsert(github, TrackingIssue.render(release)).pipe( |
| 62 | Effect.flatMap((issue) => Console.log(`Tracking issue #${issue.number}: ${issue.action}`)), |
| 63 | Effect.provide(context) |
| 64 | ), |
| 65 | resolve: TrackingIssue.close(github, "Promotion succeeded, so this is resolved.").pipe( |
| 66 | Effect.flatMap(Effect.forEach((number) => Console.log(`Closed tracking issue #${number}`), { |
| 67 | discard: true |
| 68 | })), |
| 69 | Effect.provide(context) |
| 70 | ) |
| 71 | })) |
| 72 | ) |
| 73 | } |
| 74 | |
| 75 | export interface Release { |
| 76 | readonly packages: ReadonlyArray<ReleasePackage> |
| 77 | readonly bump: VersionBump |
| 78 | } |
| 79 | |
| 80 | interface Options { |
| 81 | readonly overdueAfterHours: number |
| 82 | readonly dryRun?: boolean | undefined |
| 83 | } |
| 84 | |
| 85 | const format = (pkg: ReleasePackage) => `${pkg.name}@${pkg.version}` |
| 86 | |
| 87 | /** |
| 88 | * Opens or updates the tracking issue once the version bump is older than the |
| 89 | * threshold. `diagnoses` only runs when the release is overdue. |
| 90 | */ |
| 91 | const alertIfOverdue = Effect.fnUntraced(function*<R>( |
| 92 | release: Release, |
| 93 | effect: ReleasePackage, |
| 94 | options: Options, |
| 95 | diagnoses: Effect.Effect<ReadonlyArray<Diagnosis>, never, R> |
| 96 | ) { |
| 97 | const ageHours = ((yield* Clock.currentTimeMillis) - release.bump.committedAt.getTime()) / 3_600_000 |
| 98 | if (ageHours < options.overdueAfterHours) { |
| 99 | yield* Console.log(`The version bump landed ${ageHours.toFixed(1)} hours ago, so promotion is not overdue yet.`) |
| 100 | return |
| 101 | } |
| 102 | const overdue: OverdueRelease = { |
| 103 | version: effect.version, |
| 104 | bump: release.bump, |
| 105 | overdueAfterHours: options.overdueAfterHours, |
| 106 | diagnoses: yield* diagnoses |
| 107 | } |
| 108 | if (options.dryRun === true) { |
| 109 | const content = TrackingIssue.render(overdue) |
| 110 | yield* Console.log(`Would open or update the tracking issue "${content.title}":\n\n${content.body}`) |
| 111 | return |
| 112 | } |
| 113 | yield* (yield* Alerts).open(overdue) |
| 114 | }) |
| 115 | |
| 116 | const onPromoted = Effect.fnUntraced(function*( |
| 117 | release: Release, |
| 118 | effect: ReleasePackage, |
| 119 | options: Options, |
| 120 | result: Extract<PromoteResult, { _tag: "Promoted" }> |
| 121 | ) { |
| 122 | const dryRun = options.dryRun === true |
| 123 | if (result.promoted.length === 0) { |
| 124 | yield* Console.log("Every package already has latest at or above its target version.") |
| 125 | } |
| 126 | for (const pkg of result.promoted) { |
| 127 | yield* Console.log(`${dryRun ? "Would promote" : "Promoted"} ${format(pkg)} to latest`) |
| 128 | } |
| 129 | |
| 130 | // Deploy only while `effect`'s live latest is exactly this release. A higher |
| 131 | // latest means a newer release superseded it and owns the website; a lower |
| 132 | // one is retried by the next run. |
| 133 | const deployed = yield* Effect.exit(Effect.gen(function*() { |
| 134 | const latest = yield* liveLatest(effect.name) |
| 135 | const atTarget = latest === effect.version || (dryRun && result.promoted.some((pkg) => pkg.name === effect.name)) |
| 136 | if (!atTarget) { |
| 137 | yield* Console.log(`effect's latest is ${latest}, not ${effect.version}, so the website is not deployed.`) |
| 138 | return |
| 139 | } |
| 140 | if (dryRun) { |
| 141 | yield* Console.log(`Would deploy the website for ${release.bump.sha} unless a deployment is recorded.`) |
| 142 | return |
| 143 | } |
| 144 | const website = yield* Website |
| 145 | if (yield* website.isDeployed(release.bump.sha)) { |
| 146 | return |
| 147 | } |
| 148 | yield* website.deploy(release.bump.sha) |
| 149 | yield* Console.log(`Requested the website deploy for ${release.bump.sha}`) |
| 150 | })) |
| 151 | |
| 152 | // Every package is at or above its target, so the release no longer needs |
| 153 | // tracking, whether or not it still owns the website. A failure here is |
| 154 | // logged and never blocks or fails the deploy. |
| 155 | if (dryRun) { |
| 156 | yield* Console.log("Would close the tracking issue, if one is open.") |
| 157 | } else { |
| 158 | yield* (yield* Alerts).resolve.pipe( |
| 159 | Effect.catch((error) => Console.warn(`Could not close the tracking issue: ${error.message}`)) |
| 160 | ) |
| 161 | } |
| 162 | return yield* deployed |
| 163 | }) |
| 164 | |
| 165 | /** |
| 166 | * One scheduled run: promotes the release, deploys the website for the bump |
| 167 | * commit once `effect`'s `latest` is the target and no deployment is recorded, |
| 168 | * and opens or resolves the tracking issue. |
| 169 | * |
| 170 | * Fails when promotion or the deploy fails. Promotion failures alert when the |
| 171 | * release is overdue and promotion is known to be pending. |
| 172 | */ |
| 173 | export const promoteRelease = Effect.fnUntraced(function*( |
| 174 | release: Release, |
| 175 | options: Options |
| 176 | ): Effect.fn.Return<void, NpmPromoteError, HttpClient.HttpClient | NpmCli | Website | Alerts> { |
| 177 | const effect = release.packages.find((pkg) => pkg.name === "effect") |
| 178 | if (effect === undefined) { |
| 179 | return yield* new NpmPromoteError({ message: "The release has no effect package" }) |
| 180 | } |
| 181 | |
| 182 | const result = yield* promote(release.packages, { dryRun: options.dryRun }).pipe( |
| 183 | Effect.catchTag("PromotionError", (error) => |
| 184 | alertIfOverdue( |
| 185 | release, |
| 186 | effect, |
| 187 | options, |
| 188 | Effect.succeed([{ _tag: "PromotionFailed", pkg: error.pkg, reason: error.reason }]) |
| 189 | ).pipe( |
| 190 | Effect.catch((alertError) => |
| 191 | Console.warn(`Could not open or update the tracking issue: ${alertError.message}`) |
| 192 | ), |
| 193 | Effect.andThen(Effect.fail(new NpmPromoteError({ message: error.message, cause: error }))) |
| 194 | )), |
| 195 | // A read outage alone is not evidence of an overdue promotion. |
| 196 | Effect.catchTag("RegistryError", (error) => { |
| 197 | const original = new NpmPromoteError({ message: error.message, cause: error }) |
| 198 | if (!error.promotionPending) { |
| 199 | return Effect.fail(original) |
| 200 | } |
| 201 | // Known-pending read failures are reported as unchecked, never as missing. |
| 202 | return alertIfOverdue( |
| 203 | release, |
| 204 | effect, |
| 205 | options, |
| 206 | Effect.succeed([{ _tag: "Unknown", pkg: error.pkg, reason: error.message }]) |
| 207 | ).pipe( |
| 208 | Effect.catch((alertError) => |
| 209 | Console.warn(`Could not open or update the tracking issue: ${alertError.message}`) |
| 210 | ), |
| 211 | Effect.andThen(Effect.fail(original)) |
| 212 | ) |
| 213 | }) |
| 214 | ) |
| 215 | |
| 216 | if (result._tag === "Promoted") { |
| 217 | return yield* onPromoted(release, effect, options, result) |
| 218 | } |
| 219 | yield* Console.log( |
| 220 | `Waiting for ${result.pending.length} package(s) to show up in the npm registry:\n${ |
| 221 | result.pending.map((pkg) => ` ${format(pkg)}`).join("\n") |
| 222 | }` |
| 223 | ) |
| 224 | yield* alertIfOverdue(release, effect, options, diagnosePending(result.pending)) |
| 225 | }) |
| 226 | |
| 227 | /** |
| 228 | * Stand-ins for dry runs, which never deploy or touch issues. |
| 229 | */ |
| 230 | export const layerDryRun = Layer.mergeAll( |
| 231 | Layer.succeed( |
| 232 | Website, |
| 233 | Website.of({ |
| 234 | isDeployed: () => Effect.die("Website is not available in a dry run"), |
| 235 | deploy: () => Effect.die("Website is not available in a dry run") |
| 236 | }) |
| 237 | ), |
| 238 | Layer.succeed( |
| 239 | Alerts, |
| 240 | Alerts.of({ |
| 241 | open: () => Effect.die("Alerts are not available in a dry run"), |
| 242 | resolve: Effect.die("Alerts are not available in a dry run") |
| 243 | }) |
| 244 | ) |
| 245 | ) |