The best way to control Claude Code, Codex and more from any computer or phone

main
ts 507 lines 18.1 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as SQL from "@alchemy.run/cloudflare/D1/SQL";
3 import { sha256 } from "alchemy/Util/sha256";
4 import * as Clock from "effect/Clock";
5 import * as Duration from "effect/Duration";
6 import * as Effect from "effect/Effect";
7 import { Hex } from "effect/encoding";
8 import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
9 import * as Etag from "effect/http/Etag";
10 import * as FetchHttpClient from "effect/http/FetchHttpClient";
11 import * as HttpPlatform from "effect/http/HttpPlatform";
12 import * as HttpRouter from "effect/http/HttpRouter";
13 import { HttpServerRequest } from "effect/http/HttpServerRequest";
14 import * as HttpServerResponse from "effect/http/HttpServerResponse";
15 import * as Layer from "effect/Layer";
16 import * as Path from "effect/Path";
17 import * as Result from "effect/Result";
18 import * as Schema from "effect/Schema";
19 import { ManifestJson, type ManifestPackage } from "../Manifest.ts";
20 import {
21 BadRequest,
22 Forbidden,
23 manifestArtifactName,
24 MissingTarballs,
25 PackageTooLarge,
26 PkgApi,
27 RunNotInProgress,
28 RunRef,
29 Upstream,
30 } from "../Protocol.ts";
31 import { Bucket, Cache, COMMENT_MARKER, Database, RegistryConfig, tarballKey } from "./Bindings.ts";
32 import * as GitHub from "./GitHub.ts";
33 import * as KVCache from "./KVCache.ts";
34 import { sweep } from "./Sweep.ts";
35 import * as Tags from "./Tags.ts";
36
37 /** What GitHub says about a run. */
38 const Run = Schema.Struct({
39 ...RunRef.fields,
40 headSha: Schema.String,
41 headBranch: Schema.NullOr(Schema.String),
42 headRepo: Schema.String,
43 pr: Schema.NullOr(Schema.Number),
44 });
45 type Run = typeof Run.Type;
46
47 const bindings = Layer.mergeAll(
48 Cloudflare.R2.ReadWriteBucketBinding,
49 Cloudflare.D1.QueryDatabaseBinding,
50 Cloudflare.KV.ReadWriteNamespaceBinding,
51 Cloudflare.Workers.CronEventSourceLive,
52 FetchHttpClient.layer,
53 );
54
55 const SHORT = 7;
56
57 /** How long a resolved run is reused for uploads before asking GitHub again. */
58 const RUN_CACHE = Duration.minutes(1);
59
60 const CHECK_NAME = "Preview packages";
61
62 /** The public origin a request arrived on. */
63 const origin = Effect.map(
64 HttpServerRequest,
65 (request) => `https://${request.headers.host ?? "localhost"}`,
66 );
67
68 /**
69 * A run from a fork: its head commit belongs to another repository, so its
70 * workflow file, and therefore everything it packs, is the fork's.
71 */
72 const isFork = (run: Run) => run.headRepo !== run.repo;
73
74 /**
75 * Tags every package in a publication receives, all derived from the run.
76 * Runs on the repository's own commits get the head commit, the short
77 * commit, `branch:<name>`, and `pr:N` plus `pr:N:<short-sha>` for pull requests.
78 * A fork's run gets only `pr:N:<short-sha>`: commit tags are shared by every publisher, and a fork can
79 * run any commit it likes, including one already published from the
80 * repository, so letting it write them would let it repoint them.
81 */
82 export const tagsFor = (run: Run): string[] => {
83 if (isFork(run)) {
84 return run.pr === null ? [] : [installTag(run)];
85 }
86 const tags = [run.headSha, run.headSha.slice(0, SHORT)];
87 if (run.pr !== null) tags.push(`pr:${run.pr}`, installTag(run));
88 if (run.headBranch) tags.push(`branch:${run.headBranch}`);
89 return tags;
90 };
91
92 /** The tag install commands are written against. */
93 export const installTag = (run: Run) =>
94 run.pr !== null ? `pr:${run.pr}:${run.headSha.slice(0, SHORT)}` : run.headSha.slice(0, SHORT);
95
96 const upstream = (e: { readonly _tag: string; readonly message?: string }) =>
97 Upstream.make({ message: GitHub.describe(e) });
98
99 /**
100 * Storage failures are not part of the protocol: they are defects, which
101 * the router answers with a 500 after logging the cause.
102 */
103 const storageFailures = ["R2Error", "SqlError", "SchemaError"] as const;
104
105 /**
106 * Resolve the run a request names through GitHub; nothing about it is
107 * trusted from the client. Only allowed repositories are looked up at all,
108 * and the run must be in progress, since requests come from inside it.
109 */
110 const lookupRun = Effect.fn("lookupRun")(function* (ref: RunRef) {
111 const { policy } = yield* RegistryConfig;
112 const github = yield* GitHub.GitHubApp;
113
114 if (!policy.repos.includes(ref.repo)) {
115 return yield* Forbidden.make({ message: `${ref.repo} may not publish` });
116 }
117 const data = yield* github.getRun(ref.repo, ref.runId).pipe(Effect.mapError(upstream));
118 if (
119 data.status !== "in_progress" ||
120 (data.run_attempt !== undefined && data.run_attempt !== ref.attempt)
121 ) {
122 return yield* RunNotInProgress.make({
123 message: `run is not in progress: GitHub reports run ${ref.runId} as ${data.status}, attempt ${data.run_attempt ?? "unknown"}; the request is from attempt ${ref.attempt}`,
124 });
125 }
126 if (data.event !== "push" && data.event !== "pull_request") {
127 return yield* BadRequest.make({
128 message: `unsupported event ${data.event}`,
129 });
130 }
131 const headRepo = data.head_repository?.full_name ?? data.repository.full_name;
132 let pr: number | null = null;
133 if (data.event === "pull_request") {
134 if (data.head_branch === null) {
135 return yield* BadRequest.make({
136 message: `pull request run ${ref.runId} has no head branch`,
137 });
138 }
139 // The pull request whose head is this run's head, from the repository
140 // the run's head lives in. The same commit can head several pull
141 // requests, including one opened from a fork against a commit the
142 // repository already published; matching the head repository keeps a
143 // run's publication on its own pull request.
144 const pulls = yield* github
145 .pullRequestsForCommit(ref.repo, headRepo, data.head_branch, data.head_sha)
146 .pipe(Effect.mapError(upstream));
147 const first = pulls[0];
148 if (first === undefined) {
149 return yield* BadRequest.make({
150 message: `no open pull request from ${headRepo} has head ${data.head_sha}`,
151 });
152 }
153 pr = first.number;
154 } else if (headRepo !== ref.repo) {
155 return yield* BadRequest.make({
156 message: `push run ${ref.runId} is for ${headRepo}, not ${ref.repo}`,
157 });
158 }
159 return {
160 ...ref,
161 headSha: data.head_sha,
162 headBranch: data.head_branch,
163 headRepo,
164 pr,
165 } satisfies Run;
166 });
167
168 /**
169 * The proof. The job uploaded the manifest as an artifact of its run,
170 * named by the manifest's hash. Only the job can add artifacts to the
171 * run, and GitHub reports the run's artifacts to the App, so a matching
172 * name means this run vouched for exactly this manifest text.
173 */
174 const requireVouched = Effect.fn("requireVouched")(function* (run: Run, manifestText: string) {
175 const github = yield* GitHub.GitHubApp;
176 const expected = manifestArtifactName(yield* sha256(manifestText));
177 const artifacts = yield* github
178 .listRunArtifacts(run.repo, run.runId, expected)
179 .pipe(Effect.mapError(upstream));
180 if (!artifacts.some((a) => a.name === expected && !a.expired)) {
181 return yield* Forbidden.make({
182 message: `run ${run.runId} has not vouched for this manifest (no artifact ${expected})`,
183 });
184 }
185 });
186
187 const publish = Effect.fn("publish")(function* (run: Run, manifestText: string) {
188 const { policy } = yield* RegistryConfig;
189 const r2 = yield* Cloudflare.R2.ReadWriteBucket(Bucket);
190 const github = yield* GitHub.GitHubApp;
191 const base = yield* origin;
192
193 yield* requireVouched(run, manifestText);
194 const manifest = yield* Schema.decodeEffect(ManifestJson)(manifestText).pipe(
195 Effect.mapError((e) => BadRequest.make({ message: `invalid manifest: ${String(e)}` })),
196 );
197 // The run vouched for the manifest, but the tags come from the run's
198 // head, so the packed checkout has to be that commit: a `pull_request`
199 // job that packs the synthetic merge commit would otherwise publish bytes
200 // built from a commit that exists nowhere under the commit's name.
201 if (manifest.head !== run.headSha) {
202 return yield* BadRequest.make({
203 message: `manifest was packed at ${manifest.head}, but the run's head is ${run.headSha}`,
204 });
205 }
206 const packages = manifest.packages;
207 const maxSize = policy.maxPackageSize;
208 const tooLarge =
209 maxSize === undefined ? undefined : packages.find((pkg) => BigInt(pkg.size) > maxSize);
210 if (tooLarge !== undefined) {
211 return yield* PackageTooLarge.make({
212 message: `${tooLarge.name} exceeds ${maxSize} bytes`,
213 });
214 }
215
216 const missing = yield* Effect.filter(
217 packages,
218 (pkg) =>
219 r2.head(tarballKey(pkg.name, pkg.sha256)).pipe(Effect.map((object) => object === null)),
220 { concurrency: 8 },
221 ).pipe(Effect.map((packages) => packages.map(({ name, sha256 }) => ({ name, sha256 }))));
222 if (missing.length > 0) {
223 return yield* MissingTarballs.make({ missing });
224 }
225
226 const now = yield* Clock.currentTimeMillis;
227 const expiresAt = now + Duration.toMillis(policy.ttl);
228 const prs = run.pr !== null ? [`${run.repo}#${run.pr}`] : [];
229 const tags = tagsFor(run);
230 const tag = installTag(run);
231 const published = yield* Effect.forEach(
232 packages,
233 Effect.fn(function* (pkg: ManifestPackage) {
234 yield* Effect.forEach(
235 tags,
236 (tag) =>
237 Tags.upsert({
238 package: pkg.name,
239 tag,
240 sha256: pkg.sha256,
241 expiresAt,
242 prs,
243 }),
244 { discard: true },
245 );
246 return {
247 name: pkg.name,
248 group: pkg.group,
249 url: `${base}/${pkg.name}/${tag}`,
250 tags,
251 };
252 }),
253 { concurrency: 4 },
254 );
255
256 // A check on the commit itself, so the install lines are visible on
257 // pushes and on fork pull requests alike.
258 yield* github
259 .createCheckRun(run.repo, {
260 headSha: run.headSha,
261 name: CHECK_NAME,
262 title: `${packages.length} package(s) published`,
263 summary: GitHub.renderInstalls(published, manifest.groups),
264 detailsUrl: base,
265 })
266 .pipe(
267 Effect.catch((e) =>
268 Effect.logWarning(`check run on ${run.repo}@${run.headSha} failed: ${GitHub.describe(e)}`),
269 ),
270 );
271 if (run.pr !== null) {
272 const body = GitHub.renderComment(published, manifest.groups, {
273 publishedAt: now,
274 expiresAt,
275 });
276 yield* github
277 .upsertComment(run.repo, run.pr, COMMENT_MARKER, body)
278 .pipe(
279 Effect.catch((e) =>
280 Effect.logWarning(`comment on ${run.repo}#${run.pr} failed: ${GitHub.describe(e)}`),
281 ),
282 );
283 }
284 return { packages: published };
285 });
286
287 /**
288 * Content-addressed upload, streamed straight into R2 with the hash
289 * verified by R2 itself. Untagged objects are swept after a day.
290 */
291 const uploadTarball = Effect.fn("uploadTarball")(function* (
292 request: HttpServerRequest,
293 name: string,
294 sha256: string,
295 ) {
296 const { policy } = yield* RegistryConfig;
297 const r2 = yield* Cloudflare.R2.ReadWriteBucket(Bucket);
298
299 const contentLength = Number(request.headers["content-length"] ?? 0);
300 if (!Number.isSafeInteger(contentLength) || contentLength <= 0) {
301 return yield* BadRequest.make({ message: "Content-Length is required" });
302 }
303 const maxSize = policy.maxPackageSize;
304 if (maxSize !== undefined && BigInt(contentLength) > maxSize) {
305 return yield* PackageTooLarge.make({
306 message: `tarball exceeds ${maxSize} bytes`,
307 });
308 }
309 const key = tarballKey(name, sha256);
310 const existing = yield* r2.head(key);
311 if (existing !== null) {
312 return { name, sha256, size: existing.size, uploaded: false };
313 }
314 yield* r2
315 .put(key, request.stream, {
316 contentLength,
317 // The params schema already constrained this to 64 hex chars.
318 sha256: Result.getOrThrow(Hex.decode(sha256)),
319 })
320 .pipe(Effect.mapError((e) => BadRequest.make({ message: `upload rejected: ${String(e)}` })));
321 return { name, sha256, size: contentLength, uploaded: true };
322 });
323
324 /**
325 * Parse `/<name>/<tag>` and `/<name>/-/<sha256>.tgz`. Scoped names take
326 * two segments; the tag is everything after the name and may itself contain
327 * `/` and `:`.
328 */
329 export const parseInstallPath = (
330 pathname: string,
331 scope: string | undefined,
332 ):
333 | { kind: "tag"; name: string; tag: string }
334 | { kind: "tarball"; name: string; sha256: string }
335 | undefined => {
336 let path: string;
337 try {
338 path = decodeURIComponent(pathname.replace(/^\/+/, ""));
339 } catch {
340 // Malformed percent-encoding is a path nothing can be published under.
341 return undefined;
342 }
343 const tarball = path.match(/^(@[^/]+\/[^/@]+|[^/@]+)\/-\/([a-f0-9]{64})\.tgz$/);
344 if (tarball) {
345 return {
346 kind: "tarball",
347 name: qualify(tarball[1]!, scope),
348 sha256: tarball[2]!,
349 };
350 }
351 const segments = path.split("/");
352 const nameLength = path.startsWith("@") ? 2 : 1;
353 const name = segments.slice(0, nameLength).join("/");
354 const tag = segments.slice(nameLength).join("/");
355 if (segments.length <= nameLength || name === "" || tag === "") {
356 return undefined;
357 }
358 return { kind: "tag", name: qualify(name, scope), tag };
359 };
360
361 const qualify = (name: string, scope: string | undefined) =>
362 scope !== undefined && !name.startsWith("@") ? `${scope}/${name}` : name;
363
364 const notFound = (message: string) => HttpServerResponse.json({ error: message }, { status: 404 });
365
366 /**
367 * Install URLs: `/<name>/<tag>` redirects to the immutable tarball URL,
368 * `/<name>/-/<sha256>.tgz` streams it from R2. Everything the API routes
369 * do not claim lands here.
370 */
371 const install = Effect.gen(function* () {
372 const request = yield* HttpServerRequest;
373 const { aliases } = yield* RegistryConfig;
374 const r2 = yield* Cloudflare.R2.ReadWriteBucket(Bucket);
375
376 if (request.method !== "GET" && request.method !== "HEAD") {
377 return yield* HttpServerResponse.json({ error: "method not allowed" }, { status: 405 });
378 }
379 const host = request.headers.host ?? "localhost";
380 const target = parseInstallPath(new URL(request.url, `https://${host}`).pathname, aliases[host]);
381 if (target === undefined) {
382 return yield* notFound("not found");
383 }
384 if (target.kind === "tag") {
385 const sha256 = yield* Tags.resolve(target.name, target.tag);
386 if (sha256 === undefined) {
387 return yield* notFound(`${target.name}@${target.tag} not found`);
388 }
389 return HttpServerResponse.redirect(`/${target.name}/-/${sha256}.tgz`, {
390 status: 302,
391 headers: { "cache-control": "no-store" },
392 });
393 }
394 const object = yield* r2.get(tarballKey(target.name, target.sha256));
395 if (object === null) {
396 return yield* notFound("tarball not found");
397 }
398 const headers = {
399 "content-type": "application/gzip",
400 "content-length": String(object.size),
401 "cache-control": "public, max-age=31536000, immutable",
402 };
403 return request.method === "HEAD"
404 ? HttpServerResponse.empty({ status: 200, headers })
405 : HttpServerResponse.stream(object.body, { status: 200, headers });
406 });
407
408 const banner = Effect.map(origin, (base) =>
409 HttpServerResponse.text(
410 `Preview package registry. Install with: bun add ${base}/<package>/<commit|branch:name|pr:N|pr:N:short-sha>\n`,
411 ),
412 );
413
414 /**
415 * The registry never serves files, so the HTTP platform's file surface is
416 * stubbed rather than pulling a file system into the Worker.
417 */
418 const HttpPlatformStub = Layer.succeed(HttpPlatform.HttpPlatform, {
419 platform: "web",
420 compression: {
421 algorithms: new Set<HttpPlatform.CompressionAlgorithm>(),
422 compressResponse: (response) => Effect.succeed(response),
423 },
424 fileResponse: () => Effect.die("fileResponse is not supported"),
425 fileWebResponse: () => Effect.die("fileWebResponse is not supported"),
426 });
427
428 /**
429 * The registry Worker's Init phase. Runs at plan time to register bindings
430 * and the cron trigger, and once per isolate at runtime to build the
431 * router. Everything it needs comes from {@link RegistryConfig}.
432 */
433 export const handler = Effect.gen(function* () {
434 const config = yield* RegistryConfig;
435 // Binding the capabilities here is what registers the R2, D1, and KV
436 // bindings on the Worker at plan time. Handlers bind them again per event,
437 // which at runtime is just a lookup in the Worker's environment.
438 yield* Cloudflare.R2.ReadWriteBucket(Bucket);
439 yield* Cloudflare.KV.ReadWriteNamespace(Cache);
440 const d1 = yield* Cloudflare.D1.QueryDatabase(Database);
441
442 // Built once per isolate. The D1 client memoizes its connection on each
443 // event's scope, so its layer is safe to share.
444 const github = yield* GitHub.GitHubApp.pipe(Effect.provide(GitHub.GitHubAppLive));
445 const services = Layer.mergeAll(
446 Cloudflare.R2.ReadWriteBucketBinding,
447 Cloudflare.KV.ReadWriteNamespaceBinding,
448 SQL.D1Layer(d1),
449 Layer.succeed(GitHub.GitHubApp, github),
450 Layer.succeed(RegistryConfig, config),
451 );
452
453 // Recently resolved runs are reused so the uploads that follow a publish
454 // do not repeat the lookup. Failures are not kept.
455 const runs = KVCache.make(
456 (ref: RunRef) => `run:${ref.repo}#${ref.runId}:${ref.attempt}`,
457 Run,
458 (ref) => Effect.map(lookupRun(ref), (value) => ({ value, ttl: RUN_CACHE })),
459 );
460
461 yield* Cloudflare.Workers.cron(config.cron, () => sweep.pipe(Effect.provide(services)));
462
463 const registry = HttpApiBuilder.group(PkgApi, "Registry", (handlers) =>
464 handlers
465 .handle("publish", ({ payload }) =>
466 Effect.flatMap(runs(payload.run), (run) => publish(run, payload.manifest)).pipe(
467 Effect.catchTag(storageFailures, (e) => Effect.die(e)),
468 ),
469 )
470 // Raw so the body streams into R2 instead of being buffered.
471 .handleRaw("uploadTarball", ({ query, params, request }) =>
472 Effect.andThen(runs(query), uploadTarball(request, params.name, params.sha256)).pipe(
473 Effect.catchTag("R2Error", (e) => Effect.die(e)),
474 ),
475 ),
476 );
477
478 const healthGroup = HttpApiBuilder.group(PkgApi, "Health", (handlers) =>
479 handlers.handle("health", () => Effect.succeed({ ok: true })),
480 );
481
482 // Unlike `HttpApiBuilder.group`, the router defers a route's requirements
483 // to request time rather than the layer build, so the services are
484 // provided on the route effect itself.
485 const installRoutes = HttpRouter.use((router) =>
486 Effect.all([
487 router.add("GET", "/", banner),
488 router.add(
489 "*",
490 "/*",
491 install.pipe(
492 Effect.catchTag(storageFailures, (e) => Effect.die(e)),
493 Effect.provide(services),
494 ),
495 ),
496 ]),
497 );
498
499 return {
500 fetch: Layer.mergeAll(HttpApiBuilder.layer(PkgApi), installRoutes).pipe(
501 Layer.provide([registry, healthGroup]),
502 Layer.provide([Etag.layer, HttpPlatformStub, Path.layer]),
503 Layer.provide(services),
504 HttpRouter.toHttpEffect,
505 ),
506 };
507 }).pipe(Effect.provide(bindings));