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

main
ts 157 lines 5.77 KB
Raw
1 /**
2 * The pack hasher on dynamically loaded Workers, end to end (DESIGN
3 * §22.12): pushes stream through four loaded hasher isolates, and the
4 * result clones back byte-identical under `fsck --strict`.
5 */
6 import * as Cloudflare from "@alchemy.run/cloudflare";
7 import { GitApi } from "@alchemy.run/git/Api";
8 import { expect } from "@alchemy.run/test";
9 import * as Test from "@alchemy.run/test/Alchemy";
10 import * as Data from "effect/Data";
11 import * as Effect from "effect/Effect";
12 import * as FileSystem from "effect/FileSystem";
13 import * as HttpApiClient from "effect/http-api/HttpApiClient";
14 import * as HttpClient from "effect/http/HttpClient";
15 import * as HttpClientRequest from "effect/http/HttpClientRequest";
16 import * as ChildProcess from "effect/process/ChildProcess";
17 import { MinimumLogLevel } from "effect/References";
18 import * as Schedule from "effect/Schedule";
19 import * as Stream from "effect/Stream";
20 import { makeLoaderTestStack, TEST_SECRET } from "./fixtures/loader-stack.ts";
21
22 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
23 providers: Cloudflare.providers(),
24 });
25
26 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
27
28 const Stack = makeLoaderTestStack("GitHasherLoaderStack");
29
30 const makeClient = (url: string, token: string) =>
31 HttpApiClient.make(GitApi, {
32 baseUrl: url,
33 transformClient: HttpClient.mapRequest((request) =>
34 request.pipe(HttpClientRequest.bearerToken(token)),
35 ),
36 });
37
38 const edgeRetry = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
39 effect.pipe(
40 Effect.timeout("10 seconds"),
41 Effect.retry({
42 while: (error: unknown) =>
43 (error as { _tag?: string })._tag === "TimeoutError" ||
44 (error as { _tag?: string })._tag === "HttpClientError",
45 schedule: Schedule.spaced("1500 millis"),
46 times: 20,
47 }),
48 );
49
50 class ShellError extends Data.TaggedError("ShellError")<{
51 readonly script: string;
52 readonly exitCode: number;
53 readonly stdout: string;
54 readonly stderr: string;
55 }> {
56 override get message(): string {
57 return `shell script exited ${this.exitCode}\n--- script ---\n${this.script}\n--- stdout ---\n${this.stdout}\n--- stderr ---\n${this.stderr}`;
58 }
59 }
60
61 const sh = Effect.fn(function* (cwd: string, script: string) {
62 const handle = yield* ChildProcess.make("sh", ["-ec", script], {
63 cwd,
64 env: {
65 GIT_TERMINAL_PROMPT: "0",
66 GIT_CONFIG_NOSYSTEM: "1",
67 GIT_CONFIG_GLOBAL: "/dev/null",
68 GIT_AUTHOR_NAME: "Hasher Bot",
69 GIT_AUTHOR_EMAIL: "hasher@example.com",
70 GIT_COMMITTER_NAME: "Hasher Bot",
71 GIT_COMMITTER_EMAIL: "hasher@example.com",
72 GIT_AUTHOR_DATE: "2026-01-01T00:00:00Z",
73 GIT_COMMITTER_DATE: "2026-01-01T00:00:00Z",
74 },
75 extendEnv: true,
76 });
77 const [exitCode, stdout, stderr] = yield* Effect.all(
78 [
79 handle.exitCode,
80 Stream.mkString(Stream.decodeText(handle.stdout)),
81 Stream.mkString(Stream.decodeText(handle.stderr)),
82 ],
83 { concurrency: 3 },
84 );
85 if (exitCode !== 0) {
86 return yield* new ShellError({ script, exitCode, stdout, stderr });
87 }
88 return { stdout: stdout.trim(), stderr };
89 }, Effect.timeout("180 seconds"));
90
91 const stack = beforeAll(
92 deploy(Stack).pipe(
93 Effect.tap(({ url }) =>
94 Effect.gen(function* () {
95 yield* Effect.logInfo(`git-service (loader hasher) deployed at ${url}`);
96 const admin = yield* makeClient(url, TEST_SECRET);
97 yield* admin.repos.list({ query: {} }).pipe(
98 edgeRetry,
99 Effect.retry({
100 schedule: Schedule.spaced("1500 millis"),
101 times: 40,
102 }),
103 );
104 }),
105 ),
106 logLevel,
107 ),
108 );
109 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack).pipe(logLevel));
110
111 test(
112 "a multi-part push is hashed on loaded Workers and clones back under fsck --strict",
113 Effect.gen(function* () {
114 const { url } = yield* stack;
115 const admin = yield* makeClient(url, TEST_SECRET);
116 const owner = "loader";
117 const name = "hashed";
118 yield* admin.repos.delete({ params: { owner, repo: name } }).pipe(
119 Effect.catchTag("RepoNotFound", () => Effect.void),
120 edgeRetry,
121 );
122 const created = yield* admin.repos.create({ payload: { owner, name } }).pipe(edgeRetry);
123 const parsed = new URL(url);
124 const remote = `${parsed.protocol}//x:${TEST_SECRET}@${parsed.host}/${owner}/${name}.git`;
125 const fs = yield* FileSystem.FileSystem;
126 const dir = yield* fs.makeTempDirectory({ prefix: "git-lambda-" });
127 // ~14 MiB of incompressible blobs plus text files that delta well: a
128 // body that spills (four Lambda chunks, two R2 parts) and carries
129 // both non-delta and delta entries across chunk edges.
130 yield* sh(
131 dir,
132 `git init -q -b main src && cd src
133 for i in $(seq 1 14); do head -c 1000000 /dev/urandom > bin-$i.dat; done
134 for i in $(seq 1 200); do seq 1 400 | sed "s/^/line $i /" > text-$i.txt; done
135 git add -A && git commit -qm base
136 for i in $(seq 1 60); do echo "edit $i" >> text-$i.txt; done
137 git add -A && git commit -qm edits
138 git push -q ${remote} main`,
139 );
140 const back = yield* sh(
141 dir,
142 `git clone -q ${remote} back && cd back && git fsck --strict && git rev-parse HEAD`,
143 );
144 const head = yield* sh(dir, "cd src && git rev-parse HEAD");
145 expect(back.stdout.trim().split("\n").pop()).toBe(head.stdout.trim());
146 const repo = yield* admin.repos.get({ params: { owner, repo: name } }).pipe(edgeRetry);
147 const push = repo.lastPush;
148 expect(push).not.toBeNull();
149 // Four 4 MiB chunks were dispatched to the loaded hashers.
150 expect(push?.phases?.chunks ?? 0).toBeGreaterThanOrEqual(4);
151 yield* Effect.logInfo(`lastPush: ${JSON.stringify(push)}`);
152 }),
153 {
154 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
155 timeout: 600_000,
156 },
157 );