[codex] Structure web cloud-link failures (#3316)

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex committed 2026-06-20 19:21:42 -07:00
1 parent 252622a255
commit 65ed5d9909
2 files changed
+674 -163

No files matched your search

+251 -3
View File
@@ -2,12 +2,14 @@ import {
type DesktopBridge,
EnvironmentId,
type RelayClientInstallProgressEvent,
type RelayClientStatus,
WS_METHODS,
} from "@t3tools/contracts";
import { RelayWebClientId } from "@t3tools/contracts/relay";
import { describe, expect, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Logger from "effect/Logger";
import * as Option from "effect/Option";
import * as Stream from "effect/Stream";
import * as SubscriptionRef from "effect/SubscriptionRef";
@@ -26,7 +28,9 @@ import { remoteHttpClientLayer } from "@t3tools/client-runtime/rpc";
import { __resetDesktopPrimaryAuthForTests } from "../environments/primary/desktopAuth";
import {
CloudEnvironmentLinkOperationError,
collectCloudLinkTargets,
isCloudEnvironmentLinkError,
linkPrimaryEnvironmentToCloud,
listManagedCloudEnvironments,
normalizeRelayBaseUrl,
@@ -48,12 +52,21 @@ const relayClientInstallDialog = vi.hoisted(() => ({
finish: vi.fn(),
}));
const cloudPublicConfig = vi.hoisted(() => ({
relayUrl: "https://relay.example.test",
}));
vi.mock("./relayClientInstallDialog", () => ({
requestRelayClientInstallConfirmation: relayClientInstallDialog.requestConfirmation,
reportRelayClientInstallProgress: relayClientInstallDialog.reportProgress,
finishRelayClientInstall: relayClientInstallDialog.finish,
}));
vi.mock("./publicConfig", async (importOriginal) => ({
...(await importOriginal<typeof import("./publicConfig")>()),
resolveCloudPublicConfig: () => ({ relayUrl: cloudPublicConfig.relayUrl }),
}));
const createProof = vi.fn(() => Effect.succeed("dpop-proof"));
const dpopSignerLayer = Layer.succeed(
ManagedRelay.ManagedRelayDpopSigner,
@@ -75,7 +88,7 @@ function relayLayer() {
}
function registryLayer(options?: {
readonly status?: { readonly status: "available"; readonly version: string };
readonly status?: RelayClientStatus;
readonly installEvents?: ReadonlyArray<RelayClientInstallProgressEvent>;
}) {
return Layer.effect(
@@ -83,7 +96,14 @@ function registryLayer(options?: {
Effect.gen(function* () {
const client = {
[WS_METHODS.cloudGetRelayClientStatus]: () =>
Effect.succeed(options?.status ?? { status: "available", version: "2026.6.0" }),
Effect.succeed(
options?.status ?? {
status: "available",
executablePath: "/managed/cloudflared",
source: "managed",
version: "2026.6.0",
},
),
[WS_METHODS.cloudInstallRelayClient]: () =>
Stream.fromIterable(options?.installEvents ?? []),
} as unknown as RpcSession["client"];
@@ -141,6 +161,7 @@ function bodyText(body: BodyInit | null | undefined): string {
beforeEach(() => {
vi.clearAllMocks();
cloudPublicConfig.relayUrl = "https://relay.example.test";
vi.stubEnv("VITE_T3CODE_RELAY_URL", "https://relay.example.test");
relayClientInstallDialog.requestConfirmation.mockResolvedValue(true);
});
@@ -195,6 +216,47 @@ describe("web cloud link environment client", () => {
}),
);
it.effect("preserves structured relay failures and trace IDs", () =>
Effect.gen(function* () {
vi.stubGlobal(
"fetch",
vi.fn().mockResolvedValue(
Response.json(
{
_tag: "RelayAuthInvalidError",
code: "auth_invalid",
reason: "invalid_bearer",
traceId: "trace-web-cloud-link",
},
{ status: 401 },
),
),
);
const error = yield* withServices(
listManagedCloudEnvironments({ clerkToken: "clerk-token" }),
).pipe(Effect.flip);
expect(error).toBeInstanceOf(CloudEnvironmentLinkOperationError);
expect(error).toMatchObject({
action: "list relay-managed environments",
relayUrlInputLength: "https://relay.example.test".length,
relayUrlProtocol: "https:",
relayUrlHostname: "relay.example.test",
traceId: "trace-web-cloud-link",
relayError: {
_tag: "RelayAuthInvalidError",
reason: "invalid_bearer",
},
cause: {
_tag: "ManagedRelayRequestFailedError",
},
});
expect(error.message).toBe("Could not list relay-managed environments.");
expect(isCloudEnvironmentLinkError(error)).toBe(true);
}),
);
it.effect("reads primary cloud link state from the explicit target", () =>
Effect.gen(function* () {
const fetchMock = vi.fn().mockResolvedValue(
@@ -225,6 +287,96 @@ describe("web cloud link environment client", () => {
}),
);
it.effect("preserves structured environment API failures and their cause chain", () =>
Effect.gen(function* () {
vi.stubGlobal(
"fetch",
vi.fn().mockResolvedValue(
Response.json(
{
_tag: "EnvironmentHttpUnauthorizedError",
message: "Environment bearer token is invalid.",
},
{ status: 401 },
),
),
);
const error = yield* withServices(readPrimaryCloudLinkState({ target: TARGET })).pipe(
Effect.flip,
);
expect(error).toBeInstanceOf(CloudEnvironmentLinkOperationError);
expect(error).toMatchObject({
action: "read environment cloud link state",
environmentId: TARGET.environmentId,
httpBaseUrlInputLength: TARGET.httpBaseUrl.length,
httpBaseUrlProtocol: "http:",
httpBaseUrlHostname: "127.0.0.1",
environmentError: {
_tag: "EnvironmentHttpUnauthorizedError",
message: "Environment bearer token is invalid.",
},
});
expect(error.cause).toBeDefined();
expect(error.message).toBe(
`Could not read environment cloud link state for environment "${TARGET.environmentId}".`,
);
expect(isCloudEnvironmentLinkError(error)).toBe(true);
}),
);
it.effect("preserves invalid environment HTTP URL parser causes", () =>
Effect.gen(function* () {
const invalidUrl =
"https://user:password@[invalid-host]/private/path?access_token=secret#fragment";
const error = yield* withServices(
readPrimaryCloudLinkState({
target: {
...TARGET,
httpBaseUrl: invalidUrl,
},
}),
).pipe(Effect.flip);
expect(error).toMatchObject({
_tag: "CloudEnvironmentLinkOperationError",
action: "initialize the environment HTTP client",
environmentId: TARGET.environmentId,
httpBaseUrlInputLength: invalidUrl.length,
});
expect(error.cause).toBeInstanceOf(TypeError);
expect(error).not.toHaveProperty("httpBaseUrl");
expect(error).not.toHaveProperty("httpBaseUrlProtocol");
expect(error).not.toHaveProperty("httpBaseUrlHostname");
expect(error.message).not.toMatch(/user|password|private|path|access_token|secret|fragment/);
}),
);
it("keeps environment endpoint secrets out of mapped error attributes", () => {
const httpBaseUrl =
"https://user:[email protected]/private/path?access_token=secret#fragment";
const cause = new Error("request failed");
const error = CloudEnvironmentLinkOperationError.fromEnvironmentApi({
action: "read environment cloud link state",
environmentId: TARGET.environmentId,
httpBaseUrl,
cause,
});
expect(error).toMatchObject({
httpBaseUrlInputLength: httpBaseUrl.length,
httpBaseUrlProtocol: "https:",
httpBaseUrlHostname: "environment.example.test",
cause,
});
expect(error.cause).toBe(cause);
expect(error).not.toHaveProperty("httpBaseUrl");
const diagnostics = JSON.stringify(error);
expect(diagnostics).not.toMatch(/user|password|private|path|access_token|secret|fragment/);
expect(error.message).not.toMatch(/user|password|private|path|access_token|secret|fragment/);
});
it.effect("uses desktop bearer auth for primary cloud link state", () =>
Effect.gen(function* () {
const fetchMock = vi.fn().mockResolvedValue(
@@ -306,6 +458,37 @@ describe("web cloud link environment client", () => {
}),
);
it.effect("validates the environment endpoint before prompting to install a relay client", () =>
Effect.gen(function* () {
const invalidUrl =
"https://user:password@[invalid-host]/private/path?access_token=secret#fragment";
const error = yield* withServices(
linkPrimaryEnvironmentToCloud({
target: {
...TARGET,
httpBaseUrl: invalidUrl,
},
clerkToken: "clerk-token",
}),
{
status: { status: "missing", version: "2026.6.0" },
},
).pipe(Effect.flip);
expect(error).toMatchObject({
_tag: "CloudEnvironmentLinkOperationError",
action: "derive the environment endpoint origin",
environmentId: TARGET.environmentId,
httpBaseUrlInputLength: invalidUrl.length,
});
expect(error.cause).toBeInstanceOf(TypeError);
expect(error).not.toHaveProperty("httpBaseUrl");
expect(error.message).not.toMatch(/user|password|private|path|access_token|secret|fragment/);
expect(relayClientInstallDialog.requestConfirmation).not.toHaveBeenCalled();
}),
);
it.effect("installs a missing relay client before linking", () =>
Effect.gen(function* () {
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(Response.json({ malformed: true })));
@@ -316,7 +499,12 @@ describe("web cloud link environment client", () => {
clerkToken: "clerk-token",
}),
{
status: { status: "available", version: "2026.6.0" },
status: {
status: "available",
executablePath: "/managed/cloudflared",
source: "managed",
version: "2026.6.0",
},
installEvents: [],
},
).pipe(Effect.flip);
@@ -348,4 +536,64 @@ describe("web cloud link environment client", () => {
);
}),
);
it.effect("keeps configured relay URL secrets out of revoke warnings", () => {
const relayUrl =
"https://relay-user:[email protected]/private/workspace?access_token=relay-secret#relay-fragment";
cloudPublicConfig.relayUrl = relayUrl;
const capturedLogs: Array<ReadonlyArray<unknown>> = [];
const logger = Logger.make(({ message }) => {
capturedLogs.push(Array.isArray(message) ? message : [message]);
});
const fetchMock = vi
.fn()
.mockResolvedValueOnce(
Response.json({ ok: true, endpointRuntimeStatus: { status: "disabled" } }),
)
.mockResolvedValueOnce(Response.json({ malformed: true }, { status: 500 }));
vi.stubGlobal("fetch", fetchMock);
return unlinkPrimaryEnvironmentFromCloud({
target: TARGET,
clerkToken: "clerk-token",
}).pipe(
Effect.provide(
Layer.merge(
services(),
Logger.layer([logger], {
mergeWithExisting: false,
}),
),
),
Effect.tap(() =>
Effect.sync(() => {
expect(capturedLogs).toHaveLength(1);
const logFields = capturedLogs[0]?.find(
(value): value is Record<string, unknown> =>
typeof value === "object" && value !== null,
);
expect(logFields).toMatchObject({
relayUrlInputLength: relayUrl.length,
relayUrlProtocol: "https:",
relayUrlHostname: "relay.example.test",
causeTag: "ManagedRelayRequestFailedError",
});
expect(logFields).not.toHaveProperty("relayUrl");
expect(logFields).not.toHaveProperty("cause");
const logText = capturedLogs[0]
?.filter((value): value is string => typeof value === "string")
.join(" ");
for (const secret of [
"relay-user",
"relay-password",
"/private/workspace",
"access_token=relay-secret",
"relay-fragment",
]) {
expect(logText).not.toContain(secret);
}
}),
),
);
});
});
+423 -160
View File
@@ -1,4 +1,3 @@
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
@@ -19,13 +18,14 @@ import {
type RelayClientDeviceRecord,
type RelayClientEnvironmentRecord,
type RelayEnvironmentLinkResponse,
type RelayProtectedError as RelayProtectedErrorType,
type RelayManagedEndpointProviderKind,
RelayProtectedError,
} from "@t3tools/contracts/relay";
import { EnvironmentRegistry } from "@t3tools/client-runtime/connection";
import { request, runStream } from "@t3tools/client-runtime/rpc";
import { makeEnvironmentHttpApiClient } from "@t3tools/client-runtime/rpc";
import { ManagedRelay } from "@t3tools/client-runtime/relay";
import { getUrlDiagnostics } from "@t3tools/shared/urlDiagnostics";
import {
readPrimaryEnvironmentDescriptor,
@@ -51,17 +51,218 @@ function relayUrl(): string | null {
return resolveCloudPublicConfig().relayUrl;
}
export class CloudEnvironmentLinkError extends Data.TaggedError("CloudEnvironmentLinkError")<{
readonly message: string;
readonly cause?: unknown;
readonly traceId?: string;
}> {}
const EnvironmentCloudApiError = Schema.Union([
EnvironmentHttpBadRequestError,
EnvironmentHttpUnauthorizedError,
EnvironmentHttpForbiddenError,
EnvironmentHttpConflictError,
EnvironmentHttpInternalServerError,
EnvironmentCloudEndpointUnavailableError,
]);
type EnvironmentCloudApiError = typeof EnvironmentCloudApiError.Type;
const isEnvironmentCloudApiError = Schema.is(EnvironmentCloudApiError);
const relayClientRpcError = (message: string) => (cause: unknown) =>
new CloudEnvironmentLinkError({
message,
cause,
});
function relayUrlDiagnosticFields(relayUrl: string | undefined) {
if (relayUrl === undefined) return {};
const diagnostics = getUrlDiagnostics(relayUrl);
return {
relayUrlInputLength: diagnostics.inputLength,
...(diagnostics.protocol === undefined ? {} : { relayUrlProtocol: diagnostics.protocol }),
...(diagnostics.hostname === undefined ? {} : { relayUrlHostname: diagnostics.hostname }),
};
}
function httpBaseUrlDiagnosticFields(httpBaseUrl: string | undefined) {
if (httpBaseUrl === undefined) return {};
const diagnostics = getUrlDiagnostics(httpBaseUrl);
return {
httpBaseUrlInputLength: diagnostics.inputLength,
...(diagnostics.protocol === undefined ? {} : { httpBaseUrlProtocol: diagnostics.protocol }),
...(diagnostics.hostname === undefined ? {} : { httpBaseUrlHostname: diagnostics.hostname }),
};
}
export const CloudEnvironmentLinkAction = Schema.Literals([
"check relay client availability",
"confirm relay client installation",
"install the relay client",
"list relay-managed environments",
"list cloud devices",
"read environment cloud link state",
"update environment cloud preferences",
"unlink the environment from cloud",
"revoke the cloud environment link",
"create an environment link challenge",
"obtain an environment link proof",
"link the environment",
"derive the environment endpoint origin",
"initialize the environment HTTP client",
"configure environment relay access",
]);
export type CloudEnvironmentLinkAction = typeof CloudEnvironmentLinkAction.Type;
export class CloudEnvironmentLinkOperationError extends Schema.TaggedErrorClass<CloudEnvironmentLinkOperationError>()(
"CloudEnvironmentLinkOperationError",
{
action: CloudEnvironmentLinkAction,
environmentId: Schema.optionalKey(Schema.String),
relayUrlInputLength: Schema.optionalKey(Schema.Number),
relayUrlProtocol: Schema.optionalKey(Schema.String),
relayUrlHostname: Schema.optionalKey(Schema.String),
httpBaseUrlInputLength: Schema.optionalKey(Schema.Number),
httpBaseUrlProtocol: Schema.optionalKey(Schema.String),
httpBaseUrlHostname: Schema.optionalKey(Schema.String),
traceId: Schema.optionalKey(Schema.String),
relayError: Schema.optionalKey(RelayProtectedError),
environmentError: Schema.optionalKey(EnvironmentCloudApiError),
cause: Schema.Defect(),
},
) {
static fromManagedRelay(input: {
readonly action: CloudEnvironmentLinkAction;
readonly cause: ManagedRelay.ManagedRelayClientError;
readonly environmentId?: string;
readonly relayUrl?: string;
readonly httpBaseUrl?: string;
}): CloudEnvironmentLinkOperationError {
const requestFailure =
input.cause._tag === "ManagedRelayRequestFailedError" ? input.cause : undefined;
return new CloudEnvironmentLinkOperationError({
action: input.action,
cause: input.cause,
...(input.environmentId === undefined ? {} : { environmentId: input.environmentId }),
...relayUrlDiagnosticFields(input.relayUrl),
...httpBaseUrlDiagnosticFields(input.httpBaseUrl),
...(requestFailure?.traceId === undefined ? {} : { traceId: requestFailure.traceId }),
...(requestFailure?.relayError === undefined
? {}
: { relayError: requestFailure.relayError }),
});
}
static fromEnvironmentApi(input: {
readonly action: CloudEnvironmentLinkAction;
readonly cause: unknown;
readonly environmentId: string;
readonly httpBaseUrl: string;
}): CloudEnvironmentLinkOperationError {
const environmentError = CloudEnvironmentLinkOperationError.findEnvironmentApiError(
input.cause,
);
return new CloudEnvironmentLinkOperationError({
action: input.action,
environmentId: input.environmentId,
...httpBaseUrlDiagnosticFields(input.httpBaseUrl),
cause: input.cause,
...(environmentError === undefined ? {} : { environmentError }),
});
}
private static findEnvironmentApiError(cause: unknown): EnvironmentCloudApiError | undefined {
const seen = new Set<unknown>();
let current = cause;
while (typeof current === "object" && current !== null && !seen.has(current)) {
if (isEnvironmentCloudApiError(current)) {
return current;
}
seen.add(current);
current = "cause" in current ? current.cause : undefined;
}
return undefined;
}
override get message(): string {
const environment =
this.environmentId === undefined ? "" : ` for environment "${this.environmentId}"`;
return `Could not ${this.action}${environment}.`;
}
}
export class CloudRelayUrlNotConfiguredError extends Schema.TaggedErrorClass<CloudRelayUrlNotConfiguredError>()(
"CloudRelayUrlNotConfiguredError",
{ environmentId: Schema.optionalKey(Schema.String) },
) {
override get message(): string {
return "T3CODE_RELAY_URL is not configured.";
}
}
export class CloudRelayClientInstallUnsupportedError extends Schema.TaggedErrorClass<CloudRelayClientInstallUnsupportedError>()(
"CloudRelayClientInstallUnsupportedError",
{
environmentId: Schema.String,
phase: Schema.Literals(["preflight", "post-install"]),
version: Schema.String,
platform: Schema.String,
architecture: Schema.String,
},
) {
override get message(): string {
return `T3 Code cannot install the relay client automatically on ${this.platform}-${this.architecture}.`;
}
}
export class CloudRelayClientInstallCancelledError extends Schema.TaggedErrorClass<CloudRelayClientInstallCancelledError>()(
"CloudRelayClientInstallCancelledError",
{
environmentId: Schema.String,
version: Schema.String,
},
) {
override get message(): string {
return "Relay client installation was cancelled.";
}
}
export class CloudRelayClientInstallIncompleteError extends Schema.TaggedErrorClass<CloudRelayClientInstallIncompleteError>()(
"CloudRelayClientInstallIncompleteError",
{
environmentId: Schema.String,
version: Schema.String,
},
) {
override get message(): string {
return "The relay client install completed without a final status.";
}
}
export class CloudRelayClientUnavailableAfterInstallError extends Schema.TaggedErrorClass<CloudRelayClientUnavailableAfterInstallError>()(
"CloudRelayClientUnavailableAfterInstallError",
{
environmentId: Schema.String,
version: Schema.String,
},
) {
override get message(): string {
return "The relay client is still unavailable after installation.";
}
}
export class CloudEnvironmentLinkResponseMismatchError extends Schema.TaggedErrorClass<CloudEnvironmentLinkResponseMismatchError>()(
"CloudEnvironmentLinkResponseMismatchError",
{
environmentId: Schema.String,
field: Schema.Literals(["environment id", "endpoint provider"]),
expected: Schema.String,
actual: Schema.String,
},
) {
override get message(): string {
return `Relay returned link credentials with an unexpected ${this.field}.`;
}
}
export const CloudEnvironmentLinkError = Schema.Union([
CloudEnvironmentLinkOperationError,
CloudRelayUrlNotConfiguredError,
CloudRelayClientInstallUnsupportedError,
CloudRelayClientInstallCancelledError,
CloudRelayClientInstallIncompleteError,
CloudRelayClientUnavailableAfterInstallError,
CloudEnvironmentLinkResponseMismatchError,
]);
export type CloudEnvironmentLinkError = typeof CloudEnvironmentLinkError.Type;
export const isCloudEnvironmentLinkError = Schema.is(CloudEnvironmentLinkError);
function ensureRelayClientAvailable(
environmentId: EnvironmentId,
@@ -70,21 +271,40 @@ function ensureRelayClientAvailable(
const registry = yield* EnvironmentRegistry;
const status = yield* registry
.run(environmentId, request(WS_METHODS.cloudGetRelayClientStatus, {}))
.pipe(Effect.mapError(relayClientRpcError("Could not check relay client availability.")));
.pipe(
Effect.mapError(
(cause) =>
new CloudEnvironmentLinkOperationError({
action: "check relay client availability",
environmentId,
cause,
}),
),
);
if (status.status === "available") return;
if (status.status === "unsupported") {
return yield* new CloudEnvironmentLinkError({
message: `T3 Code cannot install the relay client automatically on ${status.platform}-${status.arch}.`,
return yield* new CloudRelayClientInstallUnsupportedError({
environmentId,
phase: "preflight",
version: status.version,
platform: status.platform,
architecture: status.arch,
});
}
const confirmed = yield* Effect.tryPromise({
try: () => requestRelayClientInstallConfirmation(status.version),
catch: relayClientRpcError("Could not confirm relay client installation."),
catch: (cause) =>
new CloudEnvironmentLinkOperationError({
action: "confirm relay client installation",
environmentId,
cause,
}),
});
if (!confirmed) {
return yield* new CloudEnvironmentLinkError({
message: "Relay client installation was cancelled.",
return yield* new CloudRelayClientInstallCancelledError({
environmentId,
version: status.version,
});
}
@@ -97,112 +317,73 @@ function ensureRelayClientAvailable(
)
.pipe(
Stream.runLast,
Effect.mapError(relayClientRpcError("Could not install the relay client.")),
Effect.mapError(
(cause) =>
new CloudEnvironmentLinkOperationError({
action: "install the relay client",
environmentId,
cause,
}),
),
Effect.ensuring(Effect.sync(finishRelayClientInstall)),
);
if (Option.isNone(installed) || installed.value.type !== "complete") {
return yield* new CloudEnvironmentLinkError({
message: "The relay client install completed without a final status.",
return yield* new CloudRelayClientInstallIncompleteError({
environmentId,
version: status.version,
});
}
const installedStatus = installed.value.status;
if (installedStatus.status !== "available") {
return yield* new CloudEnvironmentLinkError({
message:
installedStatus.status === "unsupported"
? `T3 Code cannot install the relay client automatically on ${installedStatus.platform}-${installedStatus.arch}.`
: "The relay client is still unavailable after installation.",
});
return yield* installedStatus.status === "unsupported"
? new CloudRelayClientInstallUnsupportedError({
environmentId,
phase: "post-install",
version: installedStatus.version,
platform: installedStatus.platform,
architecture: installedStatus.arch,
})
: new CloudRelayClientUnavailableAfterInstallError({
environmentId,
version: installedStatus.version,
});
}
});
}
const isEnvironmentCloudApiError = Schema.is(
Schema.Union([
EnvironmentHttpBadRequestError,
EnvironmentHttpUnauthorizedError,
EnvironmentHttpForbiddenError,
EnvironmentHttpConflictError,
EnvironmentHttpInternalServerError,
EnvironmentCloudEndpointUnavailableError,
]),
);
function relayProtectedErrorMessage(error: RelayProtectedErrorType): string {
switch (error._tag) {
case "RelayAuthInvalidError":
switch (error.reason) {
case "missing_bearer":
case "invalid_bearer":
return "Relay rejected the cloud session token.";
case "invalid_dpop":
return "Relay rejected the DPoP proof.";
case "not_authorized":
return "Relay rejected the authenticated request.";
}
case "RelayEnvironmentLinkProofExpiredError":
return "Relay rejected an expired environment link proof.";
case "RelayEnvironmentLinkProofInvalidError":
return `Relay rejected the environment link proof (${error.reason}).`;
case "RelayEnvironmentConnectNotAuthorizedError":
return "Relay rejected the environment connection request.";
case "RelayEnvironmentEndpointUnavailableError":
return `Relay could not reach the environment endpoint (${error.reason}).`;
case "RelayEnvironmentEndpointTimedOutError":
return "Relay timed out while contacting the environment endpoint.";
case "RelayEnvironmentLinkFailedError":
return `Relay could not link the environment (${error.reason}).`;
case "RelayEnvironmentLinkUnavailableError":
return `Relay cannot provision the managed endpoint (${error.reason}).`;
case "RelayAgentActivityPublishProofExpiredError":
return "Relay rejected an expired agent activity publish proof.";
case "RelayAgentActivityPublishProofInvalidError":
return `Relay rejected the agent activity publish proof (${error.reason}).`;
case "RelayInternalError":
return `Relay encountered an internal error (${error.reason}).`;
}
}
function decodedRelayClientError(message: string) {
return (cause: ManagedRelay.ManagedRelayClientError) => {
const relayError =
cause._tag === "ManagedRelayRequestFailedError" ? cause.relayError : undefined;
const traceId = cause._tag === "ManagedRelayRequestFailedError" ? cause.traceId : undefined;
const detail = relayError ? relayProtectedErrorMessage(relayError) : null;
return new CloudEnvironmentLinkError({
message: detail ? `${message}: ${detail}` : message,
cause,
...(traceId ? { traceId } : {}),
});
};
}
function findEnvironmentCloudApiError(cause: unknown): { readonly message: string } | null {
if (isEnvironmentCloudApiError(cause)) {
return cause;
}
if (typeof cause !== "object" || cause === null) {
return null;
}
return "cause" in cause ? findEnvironmentCloudApiError(cause.cause) : null;
}
const environmentApiError = (message: string) => (cause: unknown) => {
const environmentError = findEnvironmentCloudApiError(cause);
return new CloudEnvironmentLinkError({
message: environmentError
? `${message.replace(/[.:]$/, "")}: ${environmentError.message}`
: message,
cause,
function endpointOrigin(input: { readonly environmentId: string; readonly httpBaseUrl: string }) {
return Effect.try({
try: () => {
const url = new URL(input.httpBaseUrl);
return {
localHttpHost: "127.0.0.1",
localHttpPort: Number(url.port || (url.protocol === "https:" ? 443 : 80)),
};
},
catch: (cause) =>
new CloudEnvironmentLinkOperationError({
action: "derive the environment endpoint origin",
environmentId: input.environmentId,
...httpBaseUrlDiagnosticFields(input.httpBaseUrl),
cause,
}),
});
};
}
function endpointOrigin(httpBaseUrl: string) {
const url = new URL(httpBaseUrl);
return {
localHttpHost: "127.0.0.1",
localHttpPort: Number(url.port || (url.protocol === "https:" ? 443 : 80)),
};
function makeCloudEnvironmentHttpApiClient(input: {
readonly environmentId: string;
readonly httpBaseUrl: string;
}) {
return Effect.try({
try: () => makeEnvironmentHttpApiClient(input.httpBaseUrl),
catch: (cause) =>
new CloudEnvironmentLinkOperationError({
action: "initialize the environment HTTP client",
environmentId: input.environmentId,
...httpBaseUrlDiagnosticFields(input.httpBaseUrl),
cause,
}),
}).pipe(Effect.flatten);
}
const MANAGED_ENDPOINT_PROVIDER_KIND =
@@ -214,13 +395,19 @@ function ensureLinkedEnvironmentMatches(input: {
readonly link: RelayEnvironmentLinkResponse;
}): Effect.Effect<void, CloudEnvironmentLinkError> {
if (input.link.environmentId !== input.expectedEnvironmentId) {
return new CloudEnvironmentLinkError({
message: "Relay returned credentials for a different environment.",
return new CloudEnvironmentLinkResponseMismatchError({
environmentId: input.expectedEnvironmentId,
field: "environment id",
expected: input.expectedEnvironmentId,
actual: input.link.environmentId,
});
}
if (input.link.endpoint.providerKind !== input.expectedProviderKind) {
return new CloudEnvironmentLinkError({
message: "Relay returned credentials for a different endpoint provider.",
return new CloudEnvironmentLinkResponseMismatchError({
environmentId: input.expectedEnvironmentId,
field: "endpoint provider",
expected: input.expectedProviderKind,
actual: input.link.endpoint.providerKind,
});
}
return Effect.void;
@@ -275,9 +462,7 @@ export function listManagedCloudEnvironments(input: {
return Effect.gen(function* () {
const configuredRelayUrl = relayUrl();
if (!configuredRelayUrl) {
return yield* new CloudEnvironmentLinkError({
message: "T3CODE_RELAY_URL is not configured.",
});
return yield* new CloudRelayUrlNotConfiguredError({});
}
const relayClient = yield* ManagedRelay.ManagedRelayClient;
return yield* relayClient
@@ -285,12 +470,12 @@ export function listManagedCloudEnvironments(input: {
clerkToken: input.clerkToken,
})
.pipe(
Effect.mapError(
(cause) =>
new CloudEnvironmentLinkError({
message: "Could not list relay-managed environments.",
cause,
}),
Effect.mapError((cause) =>
CloudEnvironmentLinkOperationError.fromManagedRelay({
action: "list relay-managed environments",
relayUrl: configuredRelayUrl,
cause,
}),
),
);
});
@@ -304,19 +489,18 @@ export function listCloudDevices(input: {
ManagedRelay.ManagedRelayClient
> {
return Effect.gen(function* () {
if (!relayUrl()) {
return yield* new CloudEnvironmentLinkError({
message: "T3CODE_RELAY_URL is not configured.",
});
const configuredRelayUrl = relayUrl();
if (!configuredRelayUrl) {
return yield* new CloudRelayUrlNotConfiguredError({});
}
const relayClient = yield* ManagedRelay.ManagedRelayClient;
return yield* relayClient.listDevices({ clerkToken: input.clerkToken }).pipe(
Effect.mapError(
(cause) =>
new CloudEnvironmentLinkError({
message: "Could not list cloud devices.",
cause,
}),
Effect.mapError((cause) =>
CloudEnvironmentLinkOperationError.fromManagedRelay({
action: "list cloud devices",
relayUrl: configuredRelayUrl,
cause,
}),
),
);
});
@@ -326,10 +510,20 @@ export function readPrimaryCloudLinkState(input: {
readonly target: CloudLinkTarget;
}): Effect.Effect<CloudLinkState | null, CloudEnvironmentLinkError, HttpClient.HttpClient> {
return Effect.gen(function* () {
const client = yield* makeEnvironmentHttpApiClient(input.target.httpBaseUrl);
return yield* client.connect
.linkState({ headers: {} })
.pipe(Effect.mapError(environmentApiError("Could not read environment cloud link state.")));
const client = yield* makeCloudEnvironmentHttpApiClient({
environmentId: input.target.environmentId,
httpBaseUrl: input.target.httpBaseUrl,
});
return yield* client.connect.linkState({ headers: {} }).pipe(
Effect.mapError((cause) =>
CloudEnvironmentLinkOperationError.fromEnvironmentApi({
action: "read environment cloud link state",
environmentId: input.target.environmentId,
httpBaseUrl: input.target.httpBaseUrl,
cause,
}),
),
);
}).pipe(Effect.provide(primaryEnvironmentHttpLayer));
}
@@ -338,14 +532,24 @@ export function updatePrimaryCloudPreferences(input: {
readonly publishAgentActivity: boolean;
}): Effect.Effect<CloudLinkState, CloudEnvironmentLinkError, HttpClient.HttpClient> {
return Effect.gen(function* () {
const client = yield* makeEnvironmentHttpApiClient(input.target.httpBaseUrl);
const client = yield* makeCloudEnvironmentHttpApiClient({
environmentId: input.target.environmentId,
httpBaseUrl: input.target.httpBaseUrl,
});
return yield* client.connect
.preferences({
headers: {},
payload: input,
})
.pipe(
Effect.mapError(environmentApiError("Could not update environment cloud preferences.")),
Effect.mapError((cause) =>
CloudEnvironmentLinkOperationError.fromEnvironmentApi({
action: "update environment cloud preferences",
environmentId: input.target.environmentId,
httpBaseUrl: input.target.httpBaseUrl,
cause,
}),
),
);
}).pipe(Effect.provide(primaryEnvironmentHttpLayer));
}
@@ -359,10 +563,20 @@ export function unlinkPrimaryEnvironmentFromCloud(input: {
HttpClient.HttpClient | ManagedRelay.ManagedRelayClient
> {
return Effect.gen(function* () {
const client = yield* makeEnvironmentHttpApiClient(input.target.httpBaseUrl);
yield* client.connect
.unlink({ headers: {} })
.pipe(Effect.mapError(environmentApiError("Could not unlink the environment from cloud.")));
const client = yield* makeCloudEnvironmentHttpApiClient({
environmentId: input.target.environmentId,
httpBaseUrl: input.target.httpBaseUrl,
});
yield* client.connect.unlink({ headers: {} }).pipe(
Effect.mapError((cause) =>
CloudEnvironmentLinkOperationError.fromEnvironmentApi({
action: "unlink the environment from cloud",
environmentId: input.target.environmentId,
httpBaseUrl: input.target.httpBaseUrl,
cause,
}),
),
);
const configuredRelayUrl = relayUrl();
if (configuredRelayUrl && input.clerkToken) {
@@ -373,11 +587,27 @@ export function unlinkPrimaryEnvironmentFromCloud(input: {
environmentId: EnvironmentId.make(input.target.environmentId),
})
.pipe(
Effect.catch((cause) =>
Effect.logWarning("Could not revoke cloud environment link after local unlink.", {
Effect.catch((cause) => {
const error = CloudEnvironmentLinkOperationError.fromManagedRelay({
action: "revoke the cloud environment link",
environmentId: input.target.environmentId,
relayUrl: configuredRelayUrl,
cause,
}),
),
});
return Effect.logWarning(error.message, {
environmentId: input.target.environmentId,
...(error.relayUrlInputLength === undefined
? {}
: { relayUrlInputLength: error.relayUrlInputLength }),
...(error.relayUrlProtocol === undefined
? {}
: { relayUrlProtocol: error.relayUrlProtocol }),
...(error.relayUrlHostname === undefined
? {}
: { relayUrlHostname: error.relayUrlHostname }),
causeTag: cause._tag,
});
}),
);
}
}).pipe(Effect.provide(primaryEnvironmentHttpLayer));
@@ -394,12 +624,19 @@ export function linkPrimaryEnvironmentToCloud(input: {
return Effect.gen(function* () {
const configuredRelayUrl = relayUrl();
if (!configuredRelayUrl) {
return yield* new CloudEnvironmentLinkError({
message: "T3CODE_RELAY_URL is not configured.",
return yield* new CloudRelayUrlNotConfiguredError({
environmentId: input.target.environmentId,
});
}
const origin = yield* endpointOrigin({
environmentId: input.target.environmentId,
httpBaseUrl: input.target.httpBaseUrl,
});
const environmentClient = yield* makeCloudEnvironmentHttpApiClient({
environmentId: input.target.environmentId,
httpBaseUrl: input.target.httpBaseUrl,
});
const relayClient = yield* ManagedRelay.ManagedRelayClient;
const environmentClient = yield* makeEnvironmentHttpApiClient(input.target.httpBaseUrl);
yield* ensureRelayClientAvailable(EnvironmentId.make(input.target.environmentId));
const challenge = yield* relayClient
@@ -412,10 +649,13 @@ export function linkPrimaryEnvironmentToCloud(input: {
},
})
.pipe(
Effect.mapError(
decodedRelayClientError(
`${configuredRelayUrl}/v1/client/environment-link-challenges failed`,
),
Effect.mapError((cause) =>
CloudEnvironmentLinkOperationError.fromManagedRelay({
action: "create an environment link challenge",
environmentId: input.target.environmentId,
relayUrl: configuredRelayUrl,
cause,
}),
),
);
const proof = yield* environmentClient.connect
@@ -429,10 +669,19 @@ export function linkPrimaryEnvironmentToCloud(input: {
wsBaseUrl: input.target.wsBaseUrl,
providerKind: MANAGED_ENDPOINT_PROVIDER_KIND,
},
origin: endpointOrigin(input.target.httpBaseUrl),
origin,
},
})
.pipe(Effect.mapError(environmentApiError("Could not obtain environment link proof.")));
.pipe(
Effect.mapError((cause) =>
CloudEnvironmentLinkOperationError.fromEnvironmentApi({
action: "obtain an environment link proof",
environmentId: input.target.environmentId,
httpBaseUrl: input.target.httpBaseUrl,
cause,
}),
),
);
const link = yield* relayClient
.linkEnvironment({
clerkToken: input.clerkToken,
@@ -444,8 +693,13 @@ export function linkPrimaryEnvironmentToCloud(input: {
},
})
.pipe(
Effect.mapError(
decodedRelayClientError(`${configuredRelayUrl}/v1/client/environment-links failed`),
Effect.mapError((cause) =>
CloudEnvironmentLinkOperationError.fromManagedRelay({
action: "link the environment",
environmentId: input.target.environmentId,
relayUrl: configuredRelayUrl,
cause,
}),
),
);
yield* ensureLinkedEnvironmentMatches({
@@ -466,6 +720,15 @@ export function linkPrimaryEnvironmentToCloud(input: {
endpointRuntime: link.endpointRuntime,
},
})
.pipe(Effect.mapError(environmentApiError("Could not configure environment relay access.")));
.pipe(
Effect.mapError((cause) =>
CloudEnvironmentLinkOperationError.fromEnvironmentApi({
action: "configure environment relay access",
environmentId: input.target.environmentId,
httpBaseUrl: input.target.httpBaseUrl,
cause,
}),
),
);
}).pipe(Effect.provide(primaryEnvironmentHttpLayer));
}