Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/archil/src/ApiToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const observe = Effect.fn(function* (match: { tokenId?: string; name: string })
const revoke = (tokenId: string) =>
archil.deleteToken({ id: tokenId }).pipe(
Effect.asVoid,
Effect.catchTag("ApiTokenNotFound", () => Effect.void),
Effect.catchTag("NotFound", () => Effect.void),
);

/** Create an API key and return its attributes. */
Expand Down
5 changes: 3 additions & 2 deletions packages/archil/src/Branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export const toBranchAttributes = (branch: archil.BranchResponse): BranchAttribu
export const getBranch = (diskId: string, name: string) =>
archil.getBranch({ id: diskId, name }).pipe(
Effect.map(({ data }) => toBranchAttributes(data)),
Effect.catchTag("BranchNotFound", () => Effect.undefined),
Effect.catchTag("NotFound", () => Effect.undefined),
);

/** Create a branch, or return the existing one with that name. */
Expand All @@ -136,7 +136,8 @@ export const ensureBranch = Effect.fn(function* (input: {
})
.pipe(
Effect.map(({ data }) => toBranchAttributes(data)),
Effect.catchTag("BranchConflict", (conflict) =>
// A concurrent create of the same name won the race.
Effect.catchTag("Conflict", (conflict) =>
getBranch(input.disk, input.name).pipe(
Effect.flatMap((found) => (found ? Effect.succeed(found) : Effect.fail(conflict))),
),
Expand Down
26 changes: 10 additions & 16 deletions packages/archil/src/Clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type * as Scope from "effect/Scope";
import * as Stream from "effect/Stream";
import type { Authorize, ObjectsRunner } from "./ArchilBindings.ts";
import type { BranchAttributes } from "./Branch.ts";
import { toBranchAttributes } from "./Branch.ts";
import { ensureBranch, toBranchAttributes } from "./Branch.ts";
import type { DiskAttributes } from "./Disk.ts";
import type { DiskUserAttributes } from "./DiskUser.ts";
import {
Expand Down Expand Up @@ -609,18 +609,12 @@ export const makeBranchDiskClient = (
authorize(
Effect.gen(function* () {
const id = yield* diskId;
const existing = yield* archil.getBranch({ id, name: options.name }).pipe(
Effect.map(({ data }) => toBranchAttributes(data)),
Effect.catchTag("BranchNotFound", () => Effect.undefined),
);
if (existing) return existing;
const { data } = yield* archil.createBranch({
id,
branch_name: options.name,
from_checkpoint_name: options.fromCheckpoint,
from_branch: options.fromBranch,
return yield* ensureBranch({
disk: id,
name: options.name,
fromCheckpoint: options.fromCheckpoint,
fromBranch: options.fromBranch,
});
return toBranchAttributes(data);
}),
),
getBranch: (name) =>
Expand All @@ -629,7 +623,7 @@ export const makeBranchDiskClient = (
const id = yield* diskId;
return yield* archil.getBranch({ id, name }).pipe(
Effect.map(({ data }) => toBranchAttributes(data)),
Effect.catchTag("BranchNotFound", () => Effect.undefined),
Effect.catchTag("NotFound", () => Effect.undefined),
);
}),
),
Expand Down Expand Up @@ -721,16 +715,16 @@ export const makeSandboxOps = (
authorize: Authorize,
forkName: (name: string | undefined) => Effect.Effect<string | undefined> = Effect.succeed,
): SandboxOps => {
// A paused sandbox (idle TTL) refuses process connections; resume it and
// try once more.
// A paused sandbox (idle TTL) refuses process connections with
// `sandbox_terminated`; resume it and try once more.
const awake = <A, E extends { readonly _tag: string }, R2>(
use: (sid: string) => Effect.Effect<A, E, R2>,
) =>
authorize(
Effect.flatMap(sid, (id) =>
use(id).pipe(
Effect.catchIf(
(e) => e._tag === "SandboxStateConflict",
(e) => e._tag === "SandboxTerminated",
(e) =>
archil
.getSandbox({ sid: id })
Expand Down
5 changes: 3 additions & 2 deletions packages/archil/src/CreateDisk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as Layer from "effect/Layer";
import * as Stream from "effect/Stream";
import { ApiAccess, runtimeName, runtimePrefix } from "./ArchilBindings.ts";
import { makeDiskHandle, type DiskHandle } from "./Clients.ts";
import { deleteDisk, ensureDisk, type DiskStorage } from "./Disk.ts";
import { deleteDisk, ensureDisk, type DiskIdMissing, type DiskStorage } from "./Disk.ts";
import type { ArchilFailed, ArchilNotReady } from "./internal/archil.ts";
import { findDiskByName, listAllDisks } from "./internal/archil.ts";

Expand All @@ -23,7 +23,8 @@ export type CreateDiskError =
| archil.GetDiskError
| archil.ListDisksError
| ArchilFailed
| ArchilNotReady;
| ArchilNotReady
| DiskIdMissing;

export interface CreateDiskClient {
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/archil/src/CreateImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const CreateImageHttp = Layer.effect(
authorize(
archil.getImage({ image_id: imageId }).pipe(
Effect.map(({ data }) => toImageAttributes(data, data.source)),
Effect.catchTag("ImageNotFound", () => Effect.undefined),
Effect.catchTag("NotFound", () => Effect.undefined),
),
),
} satisfies CreateImageClient;
Expand Down
2 changes: 1 addition & 1 deletion packages/archil/src/CreateSandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export const CreateSandboxHttp = Layer.effect(
toCreateSandboxRequest(name, { ...request, image: defaultImage }, mounts),
)
.pipe(
Effect.catchTag("SandboxAlreadyExists", (conflict) =>
Effect.catchTag("SandboxNameConflict", (conflict) =>
findSandboxByName(name).pipe(
Effect.flatMap((found) =>
found ? Effect.succeed({ data: found }) : Effect.fail(conflict),
Expand Down
24 changes: 11 additions & 13 deletions packages/archil/src/Disk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Unowned } from "alchemy/AdoptPolicy";
import { deepEqual, isResolved } from "alchemy/Diff";
import * as Provider from "alchemy/Provider";
import { Resource } from "alchemy/Resource";
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Redacted from "effect/Redacted";
import * as Stream from "effect/Stream";
Expand Down Expand Up @@ -377,6 +378,11 @@ export const syncDiskSettings = Effect.fn(function* (
return { allowedIps, allowSandboxMount };
});

/** Archil reported a disk created but returned no id, and no disk has its name. */
export class DiskIdMissing extends Data.TaggedError("Archil.DiskIdMissing")<{
readonly diskName: string;
}> {}

/** Ensure a disk with this name exists and is available (create-or-observe). */
export const ensureDisk = Effect.fn(function* (
name: string,
Expand All @@ -388,27 +394,19 @@ export const ensureDisk = Effect.fn(function* (
if (found?.status === "deleting") yield* waitForDiskGone(found.id);
const observed = found?.status === "deleting" ? undefined : found;
if (observed) return yield* waitForDisk(observed.id);
// Archil answers a create of an existing name with that disk's id, so a
// concurrent create of the same name needs no special case.
const created = yield* archil
.createDisk({ name, mounts: storage ? [toMountConfig(storage)] : undefined })
.pipe(
Effect.map(({ data }) => data.diskId),
// A concurrent create of the same name won the race.
Effect.catchTag("DiskAlreadyExists", () =>
findDiskByName(name).pipe(Effect.map((disk) => disk?.id)),
),
);
.pipe(Effect.map(({ data }) => data.diskId));
const id = created ?? (yield* findDiskByName(name))?.id;
if (!id) {
return yield* new archil.DiskNotFound({
error: `Archil created disk ${name} but returned no id`,
});
}
if (!id) return yield* new DiskIdMissing({ diskName: name });
return yield* waitForDisk(id);
});

/** Delete a disk and wait until it is gone. Deleting a missing disk succeeds. */
export const deleteDisk = Effect.fn(function* (id: string) {
yield* archil.deleteDisk({ id }).pipe(Effect.catchTag("DiskNotFound", () => Effect.void));
yield* archil.deleteDisk({ id }).pipe(Effect.catchTag("NotFound", () => Effect.void));
yield* waitForDiskGone(id);
});

Expand Down
4 changes: 2 additions & 2 deletions packages/archil/src/DiskUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export const removeDiskUser = Effect.fn(function* (attrs: {
identifier: attrs.type === "awssts" ? undefined : attrs.identifier,
principal: attrs.type === "awssts" ? attrs.principal : undefined,
})
.pipe(Effect.catchTag("DiskNotFound", () => Effect.void));
.pipe(Effect.catchTag("NotFound", () => Effect.void));
});

export const DiskUserProvider = () =>
Expand Down Expand Up @@ -309,7 +309,7 @@ export const DiskUserProvider = () =>
identifier: output?.identifier,
principal: output?.principal ?? olds?.principal,
nickname,
}).pipe(Effect.catchTag("DiskNotFound", () => Effect.undefined));
}).pipe(Effect.catchTag("NotFound", () => Effect.undefined));
if (!observed) return undefined;
return (
output ?? {
Expand Down
6 changes: 3 additions & 3 deletions packages/archil/src/Image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,15 @@ export const ImageProvider = () =>
lookup: Effect.fn(function* ({ imageId }) {
return yield* archil.getImage({ image_id: imageId }).pipe(
Effect.map(({ data }) => toImageAttributes(data, "")),
Effect.catchTag("ImageNotFound", () => Effect.undefined),
Effect.catchTag("NotFound", () => Effect.undefined),
);
}),

read: Effect.fn(function* ({ output }) {
if (!output) return undefined;
return yield* archil.getImage({ image_id: output.imageId }).pipe(
Effect.map(({ data }) => toImageAttributes(data, output.hash, output.source)),
Effect.catchTag("ImageNotFound", () => Effect.undefined),
Effect.catchTag("NotFound", () => Effect.undefined),
);
}),
reconcile: Effect.fn(function* ({ news, output, session }) {
Expand All @@ -285,7 +285,7 @@ export const ImageProvider = () =>
if (output && output.hash === plan.hash) {
const observed = yield* archil.getImage({ image_id: output.imageId }).pipe(
Effect.map(({ data }) => data),
Effect.catchTag("ImageNotFound", () => Effect.undefined),
Effect.catchTag("NotFound", () => Effect.undefined),
);
if (observed?.status === "ready")
return toImageAttributes(observed, plan.hash, output.source);
Expand Down
22 changes: 8 additions & 14 deletions packages/archil/src/Sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ export const observeSandbox = Effect.fn(function* (
) {
const ports = yield* archil.listSandboxPorts({ sid: sandbox.sandbox_id }).pipe(
Effect.map(({ data }) => data.ports ?? []),
Effect.catchTag("SandboxNotFound", () => Effect.succeed([])),
Effect.catchTag("NotFound", () => Effect.succeed([])),
);
return toSandboxAttributes(sandbox, ports, program, derivedImage);
});
Expand Down Expand Up @@ -562,10 +562,10 @@ const stopSandbox = Effect.fn(function* (sid: string) {
const observed = yield* getSandbox(sid);
if (observed === undefined || deletableSandboxStates.has(observed.status)) return;
// Let a booting or pausing sandbox settle, then stop it.
yield* waitForSandbox(sid).pipe(Effect.catchTag("SandboxNotFound", () => Effect.undefined));
yield* waitForSandbox(sid).pipe(Effect.catchTag("NotFound", () => Effect.undefined));
yield* archil.stopSandbox({ sid }).pipe(
Effect.asVoid,
Effect.catchTag("SandboxNotFound", () => Effect.void),
Effect.catchTag("NotFound", () => Effect.void),
);
yield* waitForSandboxStopped(sid);
});
Expand All @@ -581,7 +581,7 @@ export const deleteSandbox = Effect.fn(function* (sid: string) {
yield* stopSandbox(sid);
yield* archil.deleteSandbox({ sid }).pipe(
Effect.asVoid,
Effect.catchTag("SandboxNotFound", () => Effect.void),
Effect.catchTag("NotFound", () => Effect.void),
);
yield* waitForSandboxGone(sid);
});
Expand Down Expand Up @@ -898,16 +898,10 @@ export const SandboxProvider = () =>
);
sandbox = yield* archil.createSandbox(request).pipe(
Effect.map(({ data }) => data),
Effect.catchTag("SandboxAlreadyExists", () =>
Effect.catchTag("SandboxNameConflict", (conflict) =>
findSandboxByName(name).pipe(
Effect.flatMap((found) =>
found
? Effect.succeed(found)
: Effect.fail(
new archil.SandboxAlreadyExists({
error: `Sandbox ${name} exists but is not visible to this key`,
}),
),
found ? Effect.succeed(found) : Effect.fail(conflict),
),
),
),
Expand Down Expand Up @@ -946,7 +940,7 @@ export const SandboxProvider = () =>
if (output?.port !== undefined && output.port !== port) {
yield* archil
.deleteSandboxPort({ sid, port: output.port })
.pipe(Effect.catchTag("SandboxNotFound", () => Effect.void));
.pipe(Effect.catchTag("NotFound", () => Effect.void));
}
yield* deployProgram({
sid,
Expand All @@ -963,7 +957,7 @@ export const SandboxProvider = () =>
if (output.port !== undefined && !(news.ports ?? []).includes(output.port)) {
yield* archil
.deleteSandboxPort({ sid, port: output.port })
.pipe(Effect.catchTag("SandboxNotFound", () => Effect.void));
.pipe(Effect.catchTag("NotFound", () => Effect.void));
}
}
// Return
Expand Down
6 changes: 3 additions & 3 deletions packages/archil/src/SandboxPortToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const createPortToken = Effect.fn(function* (sid: string, port: number, t
export const revokePortToken = (sid: string, tokenId: string) =>
archil.deleteSandboxPortToken({ sid, token_id: tokenId }).pipe(
Effect.asVoid,
Effect.catchTag("SandboxPortTokenNotFound", () => Effect.void),
Effect.catchTag("NotFound", () => Effect.void),
);

export const SandboxPortTokenProvider = () =>
Expand All @@ -100,7 +100,7 @@ export const SandboxPortTokenProvider = () =>
.getSandboxPortToken({ sid: output.sandboxId, token_id: output.tokenId })
.pipe(
Effect.map(() => output),
Effect.catchTag("SandboxPortTokenNotFound", () => Effect.undefined),
Effect.catchTag("NotFound", () => Effect.undefined),
);
}),
reconcile: Effect.fn(function* ({ news, output }) {
Expand All @@ -110,7 +110,7 @@ export const SandboxPortTokenProvider = () =>
.getSandboxPortToken({ sid: output.sandboxId, token_id: output.tokenId })
.pipe(
Effect.as(true),
Effect.catchTag("SandboxPortTokenNotFound", () => Effect.succeed(false)),
Effect.catchTag("NotFound", () => Effect.succeed(false)),
);
if (live) return output;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/archil/src/internal/archil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export const findDiskByName = Effect.fn(function* (name: string) {
export const getDisk = (id: string) =>
archil.getDisk({ id }).pipe(
Effect.map(({ data }) => (data.status === "deleted" ? undefined : data)),
Effect.catchTag("DiskNotFound", () => Effect.undefined),
Effect.catchTag("NotFound", () => Effect.undefined),
);

/** Wait until a disk is `available`. */
Expand Down Expand Up @@ -178,7 +178,7 @@ export const findSandboxByName = Effect.fn(function* (name: string) {
export const getSandbox = (sid: string) =>
archil.getSandbox({ sid }).pipe(
Effect.map(({ data }) => (data.status === "deleted" ? undefined : data)),
Effect.catchTag("SandboxNotFound", () => Effect.undefined),
Effect.catchTag("NotFound", () => Effect.undefined),
);

const settledSandboxStates = new Set(["running", "paused", "stopped", "exited"]);
Expand Down
6 changes: 3 additions & 3 deletions packages/archil/src/internal/hosted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@ export const ensureRunning = Effect.fn(function* (sid: string) {
if (sandbox.status === "paused") {
yield* archil
.resumeSandbox({ sid })
.pipe(Effect.catchTag("SandboxStateConflict", () => Effect.void));
.pipe(Effect.catchTag("SandboxStartConflict", () => Effect.void));
} else if (sandbox.status === "stopped" || sandbox.status === "exited") {
yield* archil
.startSandbox({ sid })
.pipe(Effect.catchTag("SandboxStateConflict", () => Effect.void));
.pipe(Effect.catchTag("SandboxStartConflict", () => Effect.void));
}
const ready = yield* waitForSandbox(sid);
if (ready.status !== "running") {
Expand Down Expand Up @@ -391,7 +391,7 @@ export const deployProgram = Effect.fn(function* (input: {
/** Stop the program and remove its files. Removing a missing one succeeds. */
export const removeProgram = Effect.fn(function* (sid: string, name: string) {
const sandbox = yield* waitForSandbox(sid).pipe(
Effect.catchTag("SandboxNotFound", () => Effect.undefined),
Effect.catchTag("NotFound", () => Effect.undefined),
);
if (sandbox === undefined || sandbox.status === "deleted") return;
yield* ensureRunning(sid);
Expand Down
Loading
Loading