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

main
ts 119 lines 4.26 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { defaultComputeServiceAccount } from "@alchemy.run/gcp/Host";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as aiplatform from "@distilled.cloud/gcp/aiplatform_v1";
7 import * as Output from "alchemy/Output";
8 import * as Effect from "effect/Effect";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({ providers: GCP.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 // The job provisions a Colab runtime and runs the notebook (2-4 minutes).
17 const runLifecycle = !process.env.FAST;
18
19 const waitUntilGone = (name: string) =>
20 aiplatform.getProjectsLocationsNotebookExecutionJobs({ name }).pipe(
21 Effect.as("found" as const),
22 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
23 Effect.repeat({
24 schedule: Schedule.spaced("2 seconds"),
25 until: (status) => status === "gone",
26 times: 10,
27 }),
28 );
29
30 const emptyNotebook =
31 "eyJuYmZvcm1hdCI6NCwibmJmb3JtYXRfbWlub3IiOjUsIm1ldGFkYXRhIjp7fSwiY2VsbHMiOltdfQ==";
32
33 test.provider(
34 "getProjectsLocationsNotebookExecutionJobs on a missing job fails with a typed tag",
35 (stack) =>
36 Effect.gen(function* () {
37 const { project } = yield* GcpEnvironment.current;
38 const parent = `projects/${project}/locations/us-central1`;
39 yield* stack.destroy();
40
41 const error = yield* Effect.flip(
42 aiplatform.getProjectsLocationsNotebookExecutionJobs({
43 name: `${parent}/notebookExecutionJobs/alchemy-missing`,
44 }),
45 );
46 expect(error._tag).toEqual("NotFound");
47 const page = yield* aiplatform.listProjectsLocationsNotebookExecutionJobs({
48 parent,
49 pageSize: 10,
50 });
51 expect((page.notebookExecutionJobs ?? []).map((item) => item.name)).not.toContain(
52 `${parent}/notebookExecutionJobs/alchemy-missing`,
53 );
54
55 yield* stack.destroy();
56 }).pipe(logLevel),
57 {
58 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
59 timeout: 90_000,
60 },
61 );
62
63 test.provider.skipIf(!runLifecycle)(
64 "create and delete a notebook execution job",
65 (stack) =>
66 Effect.gen(function* () {
67 const { project } = yield* GcpEnvironment.current;
68 const serviceAccount = yield* defaultComputeServiceAccount(project);
69 yield* stack.destroy();
70
71 const created = yield* stack.deploy(
72 Effect.gen(function* () {
73 const template = yield* GCP.AIPlatform.NotebookRuntimeTemplate("Runtime", {
74 location: "us-central1",
75 displayName: "alchemy-notebook-runtime",
76 machineSpec: { machineType: "e2-standard-4" },
77 networkSpec: { enableInternetAccess: true },
78 // Standard disk keeps the run clear of the regional SSD quota.
79 dataPersistentDiskSpec: {
80 diskType: "pd-standard",
81 diskSizeGb: "100",
82 },
83 labels: { env: "test" },
84 });
85 const bucket = yield* GCP.Storage.Bucket("NotebookOut", {
86 location: "US-CENTRAL1",
87 forceDestroy: true,
88 });
89 return yield* GCP.AIPlatform.NotebookExecutionJob("Nightly", {
90 location: "us-central1",
91 displayName: "alchemy-notebook-job",
92 notebookRuntimeTemplateResourceName: template.name,
93 directNotebookSource: { content: emptyNotebook },
94 gcsOutputUri: Output.interpolate`gs://${bucket.bucketName}/notebook-out`,
95 serviceAccount,
96 labels: { env: "test" },
97 });
98 }),
99 );
100
101 expect(created.name).toContain("/notebookExecutionJobs/");
102 expect(created.location).toEqual("us-central1");
103 expect(created.labels).toMatchObject({ env: "test" });
104
105 const fetched = yield* aiplatform.getProjectsLocationsNotebookExecutionJobs({
106 name: created.name,
107 });
108 expect(fetched.name).toEqual(created.name);
109
110 yield* stack.destroy();
111
112 const gone = yield* waitUntilGone(created.name);
113 expect(gone).toEqual("gone");
114 }).pipe(logLevel),
115 {
116 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
117 timeout: 600_000,
118 },
119 );