The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import * as Clock from "effect/Clock"; |
| 3 | import * as Duration from "effect/Duration"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Option from "effect/Option"; |
| 6 | import * as Stream from "effect/Stream"; |
| 7 | import { Bucket, ORPHAN_GRACE, RegistryConfig, SWEEP_LOOKAHEAD, tarballKey } from "./Bindings.ts"; |
| 8 | import * as GitHub from "./GitHub.ts"; |
| 9 | import * as Tags from "./Tags.ts"; |
| 10 | |
| 11 | /** |
| 12 | * Expiry sweep. Tags tied to pull requests are extended while any of those |
| 13 | * pull requests is open and pinned to the latest close time plus TTL once |
| 14 | * they all close. Expired tags go away, and tarballs nothing points at are |
| 15 | * deleted from R2. |
| 16 | */ |
| 17 | export const sweep = Effect.gen(function* () { |
| 18 | const { policy } = yield* RegistryConfig; |
| 19 | const r2 = yield* Cloudflare.R2.ReadWriteBucket(Bucket); |
| 20 | const github = yield* GitHub.GitHubApp; |
| 21 | |
| 22 | const now = yield* Clock.currentTimeMillis; |
| 23 | const ttl = Duration.toMillis(policy.ttl); |
| 24 | const due = yield* Tags.dueLinked(now + Duration.toMillis(SWEEP_LOOKAHEAD)); |
| 25 | |
| 26 | // One lookup per pull request across every due row. `undefined` means the |
| 27 | // pull request is still open; a failed lookup is recorded as such. |
| 28 | const lookups = yield* Effect.forEach( |
| 29 | new Set(due.flatMap((row) => row.linked_prs)), |
| 30 | (ref) => { |
| 31 | const [repo, number] = ref.split("#"); |
| 32 | return github.getPullRequest(repo!, Number(number)).pipe( |
| 33 | Effect.map((pr) => |
| 34 | pr.state === "open" ? undefined : Date.parse(pr.merged_at ?? pr.closed_at ?? "") || now, |
| 35 | ), |
| 36 | Effect.map((closedAt) => [ref, { closedAt }] as const), |
| 37 | Effect.catch((e) => |
| 38 | Effect.logWarning(`pull request ${ref} lookup failed: ${GitHub.describe(e)}`).pipe( |
| 39 | Effect.as([ref, undefined] as const), |
| 40 | ), |
| 41 | ), |
| 42 | ); |
| 43 | }, |
| 44 | { concurrency: 4 }, |
| 45 | ); |
| 46 | const results = new Map(lookups); |
| 47 | const extended = yield* Effect.forEach(due, (row) => { |
| 48 | const known = row.linked_prs.flatMap((ref) => { |
| 49 | const result = results.get(ref); |
| 50 | return result === undefined ? [] : [result.closedAt]; |
| 51 | }); |
| 52 | // Open pull requests dominate; otherwise the latest close wins. A row |
| 53 | // whose pull requests could not all be checked is kept past the next |
| 54 | // sweep rather than left to expire: it is re-checked, never deleted, |
| 55 | // on GitHub's bad day. |
| 56 | const closes = known.filter((closedAt) => closedAt !== undefined); |
| 57 | const expiresAt = |
| 58 | closes.length < known.length |
| 59 | ? now + ttl |
| 60 | : known.length < row.linked_prs.length |
| 61 | ? Math.max(row.expires_at, now + Duration.toMillis(SWEEP_LOOKAHEAD)) |
| 62 | : Math.max(...closes) + ttl; |
| 63 | return expiresAt === row.expires_at |
| 64 | ? Effect.succeed(false) |
| 65 | : Tags.setExpiry(row.package, row.tag, expiresAt).pipe(Effect.as(true)); |
| 66 | }).pipe(Effect.map((changed) => changed.filter(Boolean).length)); |
| 67 | |
| 68 | const removed = yield* Tags.deleteExpired(now); |
| 69 | const referenced = yield* Tags.referencedTarballs(); |
| 70 | const unreferenced = removed.filter( |
| 71 | ({ package: pkg, sha256 }) => !referenced.has(`${pkg}/${sha256}`), |
| 72 | ); |
| 73 | yield* Effect.forEach( |
| 74 | unreferenced, |
| 75 | ({ package: pkg, sha256 }) => r2.delete(tarballKey(pkg, sha256)), |
| 76 | { discard: true }, |
| 77 | ); |
| 78 | |
| 79 | // Uploads that never got tagged. |
| 80 | const orphans = yield* Stream.paginate(undefined as string | undefined, (cursor) => |
| 81 | r2 |
| 82 | .list({ cursor, limit: 500 }) |
| 83 | .pipe( |
| 84 | Effect.map( |
| 85 | (page) => |
| 86 | [page.objects, page.truncated ? Option.some(page.cursor) : Option.none()] as const, |
| 87 | ), |
| 88 | ), |
| 89 | ).pipe( |
| 90 | Stream.filter((object) => { |
| 91 | const match = object.key.match(/^(.+)\/([a-f0-9]{64})\.tgz$/); |
| 92 | return ( |
| 93 | match !== null && |
| 94 | !referenced.has(`${decodeURIComponent(match[1]!)}/${match[2]}`) && |
| 95 | object.uploaded.getTime() < now - Duration.toMillis(ORPHAN_GRACE) |
| 96 | ); |
| 97 | }), |
| 98 | Stream.mapEffect((object) => r2.delete(object.key)), |
| 99 | Stream.runCount, |
| 100 | ); |
| 101 | yield* Effect.logInfo( |
| 102 | `sweep: ${due.length} due, ${extended} extended, ${removed.length} tags expired, ${unreferenced.length + orphans} tarballs deleted`, |
| 103 | ); |
| 104 | }); |