The best way to control Claude Code, Codex and more from any computer or phone
[codex] Structure primary environment target failures (#3413)
Co-authored-by: codex <codex@users.noreply.github.com>
Julius Marminge committed
Jun 20, 2026 at 11:49 UTC
0e718855b899b8535ee84e3721f4c5a0de0934cb
3 files changed
+240
-27
apps/web/src/environments/primary/bootstrap.test.ts
+76
@@ -4,6 +4,10 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"
4
5
import {
6
getPrimaryKnownEnvironment,
7
+ isDesktopEnvironmentBootstrapIncompleteError,
8
+ isPrimaryEnvironmentProtocolUnsupportedError,
9
+ isPrimaryEnvironmentUrlInvalidError,
10
+ readPrimaryEnvironmentTarget,
11
resolvePrimaryEnvironmentHttpUrl,
12
resolveInitialPrimaryEnvironmentDescriptor,
13
resetPrimaryEnvironmentDescriptorForTests,
@@ -43,6 +47,15 @@ function installTestBrowser(url: string) {
47
});
48
}
49
50
+function captureThrown(run: () => unknown): unknown {
51
+ try {
52
+ run();
53
+ } catch (error) {
54
+ return error;
55
+ }
56
+ throw new Error("Expected the operation to throw.");
57
+}
58
+
59
describe("environmentBootstrap", () => {
60
beforeEach(() => {
61
vi.restoreAllMocks();
@@ -175,4 +188,67 @@ describe("environmentBootstrap", () => {
188
"http://127.0.0.1:5733/.well-known/t3/environment",
189
);
190
});
191
+
192
+ it("retains the URL parser cause without exposing the configured URL in its message", () => {
193
+ vi.stubEnv("VITE_HTTP_URL", "http://[");
194
+
195
+ const error = captureThrown(readPrimaryEnvironmentTarget);
196
+
197
+ expect(isPrimaryEnvironmentUrlInvalidError(error)).toBe(true);
198
+ if (!isPrimaryEnvironmentUrlInvalidError(error)) {
199
+ throw new Error("Expected a structured primary environment URL error.");
200
+ }
201
+ expect(error).toMatchObject({
202
+ source: "configured",
203
+ urlKind: "http-base-url",
204
+ message: "Could not parse http-base-url for the configured primary environment target.",
205
+ });
206
+ expect(error.cause).toBeInstanceOf(TypeError);
207
+ expect(error.message).not.toContain("http://[");
208
+ });
209
+
210
+ it("describes which desktop bootstrap endpoint is missing", () => {
211
+ vi.stubGlobal("window", {
212
+ location: new URL("http://127.0.0.1:5733/"),
213
+ history: { replaceState: vi.fn() },
214
+ desktopBridge: {
215
+ getLocalEnvironmentBootstrap: () => ({
216
+ label: "Local environment",
217
+ httpBaseUrl: "http://127.0.0.1:3773",
218
+ bootstrapToken: "desktop-bootstrap-token",
219
+ }),
220
+ },
221
+ });
222
+
223
+ const error = captureThrown(readPrimaryEnvironmentTarget);
224
+
225
+ expect(isDesktopEnvironmentBootstrapIncompleteError(error)).toBe(true);
226
+ if (!isDesktopEnvironmentBootstrapIncompleteError(error)) {
227
+ throw new Error("Expected a structured desktop bootstrap error.");
228
+ }
229
+ expect(error).toMatchObject({
230
+ hasHttpBaseUrl: true,
231
+ hasWsBaseUrl: false,
232
+ message: "Desktop bootstrap is missing wsBaseUrl for the local environment.",
233
+ });
234
+ });
235
+
236
+ it("preserves an unsupported window-origin protocol", () => {
237
+ vi.stubGlobal("window", {
238
+ location: { origin: "file:///tmp/t3code/" },
239
+ history: { replaceState: vi.fn() },
240
+ });
241
+
242
+ const error = captureThrown(readPrimaryEnvironmentTarget);
243
+
244
+ expect(isPrimaryEnvironmentProtocolUnsupportedError(error)).toBe(true);
245
+ if (!isPrimaryEnvironmentProtocolUnsupportedError(error)) {
246
+ throw new Error("Expected a structured primary environment protocol error.");
247
+ }
248
+ expect(error).toMatchObject({
249
+ source: "window-origin",
250
+ protocol: "file:",
251
+ message: "The window-origin primary environment target uses unsupported protocol file:.",
252
+ });
253
+ });
254
});
apps/web/src/environments/primary/index.ts
+7
@@ -38,7 +38,14 @@ export { refreshPrimarySessionState, usePrimarySessionState } from "./sessionSta
38
export { PrimaryEnvironmentHttpClient } from "./httpClient";
39
40
export {
41
+ DesktopEnvironmentBootstrapIncompleteError,
42
+ isDesktopEnvironmentBootstrapIncompleteError,
43
+ isPrimaryEnvironmentProtocolUnsupportedError,
44
+ isPrimaryEnvironmentUrlInvalidError,
45
+ PrimaryEnvironmentProtocolUnsupportedError,
46
+ PrimaryEnvironmentUrlInvalidError,
47
readPrimaryEnvironmentTarget,
48
resolvePrimaryEnvironmentHttpUrl,
49
isLoopbackHostname,
50
+ type PrimaryEnvironmentTarget,
51
} from "./target";
apps/web/src/environments/primary/target.ts
+157
-27
@@ -1,7 +1,72 @@
1
import type { DesktopEnvironmentBootstrap } from "@t3tools/contracts";
2
+import * as Schema from "effect/Schema";
3
+
4
+const PrimaryEnvironmentTargetSource = Schema.Literals([
5
+ "configured",
6
+ "window-origin",
7
+ "desktop-managed",
8
+]);
9
+type PrimaryEnvironmentTargetSource = typeof PrimaryEnvironmentTargetSource.Type;
10
+
11
+const PrimaryEnvironmentUrlKind = Schema.Literals([
12
+ "http-base-url",
13
+ "websocket-base-url",
14
+ "development-server-url",
15
+ "window-location-url",
16
+]);
17
+type PrimaryEnvironmentUrlKind = typeof PrimaryEnvironmentUrlKind.Type;
18
+
19
+export class PrimaryEnvironmentUrlInvalidError extends Schema.TaggedErrorClass<PrimaryEnvironmentUrlInvalidError>()(
20
+ "PrimaryEnvironmentUrlInvalidError",
21
+ {
22
+ source: PrimaryEnvironmentTargetSource,
23
+ urlKind: PrimaryEnvironmentUrlKind,
24
+ cause: Schema.Defect(),
25
+ },
26
+) {
27
+ override get message(): string {
28
+ return `Could not parse ${this.urlKind} for the ${this.source} primary environment target.`;
29
+ }
30
+}
31
+
32
+export class PrimaryEnvironmentProtocolUnsupportedError extends Schema.TaggedErrorClass<PrimaryEnvironmentProtocolUnsupportedError>()(
33
+ "PrimaryEnvironmentProtocolUnsupportedError",
34
+ {
35
+ source: PrimaryEnvironmentTargetSource,
36
+ protocol: Schema.String,
37
+ },
38
+) {
39
+ override get message(): string {
40
+ return `The ${this.source} primary environment target uses unsupported protocol ${this.protocol}.`;
41
+ }
42
+}
43
+
44
+export class DesktopEnvironmentBootstrapIncompleteError extends Schema.TaggedErrorClass<DesktopEnvironmentBootstrapIncompleteError>()(
45
+ "DesktopEnvironmentBootstrapIncompleteError",
46
+ {
47
+ hasHttpBaseUrl: Schema.Boolean,
48
+ hasWsBaseUrl: Schema.Boolean,
49
+ },
50
+) {
51
+ override get message(): string {
52
+ const missing = [
53
+ ...(this.hasHttpBaseUrl ? [] : ["httpBaseUrl"]),
54
+ ...(this.hasWsBaseUrl ? [] : ["wsBaseUrl"]),
55
+ ];
56
+ return `Desktop bootstrap is missing ${missing.join(" and ")} for the local environment.`;
57
+ }
58
+}
59
+
60
+export const isPrimaryEnvironmentUrlInvalidError = Schema.is(PrimaryEnvironmentUrlInvalidError);
61
+export const isPrimaryEnvironmentProtocolUnsupportedError = Schema.is(
62
+ PrimaryEnvironmentProtocolUnsupportedError,
63
+);
64
+export const isDesktopEnvironmentBootstrapIncompleteError = Schema.is(
65
+ DesktopEnvironmentBootstrapIncompleteError,
66
+);
67
68
export interface PrimaryEnvironmentTarget {
4
- readonly source: "configured" | "window-origin" | "desktop-managed";
69
+ readonly source: PrimaryEnvironmentTargetSource;
70
readonly target: {
71
readonly httpBaseUrl: string;
72
readonly wsBaseUrl: string;
@@ -14,15 +79,49 @@ function getDesktopLocalEnvironmentBootstrap(): DesktopEnvironmentBootstrap | nu
79
return window.desktopBridge?.getLocalEnvironmentBootstrap() ?? null;
80
}
81
17
-function normalizeBaseUrl(rawValue: string): string {
18
- return new URL(rawValue, window.location.origin).toString();
82
+function parseTargetUrl(input: {
83
+ readonly rawValue: string;
84
+ readonly baseUrl?: string;
85
+ readonly source: PrimaryEnvironmentTargetSource;
86
+ readonly urlKind: PrimaryEnvironmentUrlKind;
87
+}): URL {
88
+ try {
89
+ return input.baseUrl === undefined
90
+ ? new URL(input.rawValue)
91
+ : new URL(input.rawValue, input.baseUrl);
92
+ } catch (cause) {
93
+ throw new PrimaryEnvironmentUrlInvalidError({
94
+ source: input.source,
95
+ urlKind: input.urlKind,
96
+ cause,
97
+ });
98
+ }
99
+}
100
+
101
+function normalizeBaseUrl(
102
+ rawValue: string,
103
+ source: PrimaryEnvironmentTargetSource,
104
+ urlKind: PrimaryEnvironmentUrlKind,
105
+): string {
106
+ return parseTargetUrl({
107
+ rawValue,
108
+ baseUrl: window.location.origin,
109
+ source,
110
+ urlKind,
111
+ }).toString();
112
}
113
114
function swapBaseUrlProtocol(
115
rawValue: string,
116
nextProtocol: "http:" | "https:" | "ws:" | "wss:",
117
+ urlKind: PrimaryEnvironmentUrlKind,
118
): string {
25
- const url = new URL(normalizeBaseUrl(rawValue));
119
+ const url = parseTargetUrl({
120
+ rawValue,
121
+ baseUrl: window.location.origin,
122
+ source: "configured",
123
+ urlKind,
124
+ });
125
url.protocol = nextProtocol;
126
return url.toString();
127
}
@@ -38,15 +137,29 @@ export function isLoopbackHostname(hostname: string): boolean {
137
return LOOPBACK_HOSTNAMES.has(normalizeHostname(hostname));
138
}
139
41
-function resolveHttpRequestBaseUrl(httpBaseUrl: string): string {
140
+function resolveHttpRequestBaseUrl(primaryTarget: PrimaryEnvironmentTarget): string {
141
+ const httpBaseUrl = primaryTarget.target.httpBaseUrl;
142
const configuredDevServerUrl = import.meta.env.VITE_DEV_SERVER_URL?.trim();
143
if (!configuredDevServerUrl) {
144
return httpBaseUrl;
145
}
146
47
- const currentUrl = new URL(window.location.href);
48
- const targetUrl = new URL(httpBaseUrl);
49
- const devServerUrl = new URL(configuredDevServerUrl, currentUrl.origin);
147
+ const currentUrl = parseTargetUrl({
148
+ rawValue: window.location.href,
149
+ source: "window-origin",
150
+ urlKind: "window-location-url",
151
+ });
152
+ const targetUrl = parseTargetUrl({
153
+ rawValue: httpBaseUrl,
154
+ source: primaryTarget.source,
155
+ urlKind: "http-base-url",
156
+ });
157
+ const devServerUrl = parseTargetUrl({
158
+ rawValue: configuredDevServerUrl,
159
+ baseUrl: currentUrl.origin,
160
+ source: "configured",
161
+ urlKind: "development-server-url",
162
+ });
163
164
const isCurrentOriginDevServer =
165
(currentUrl.protocol === "http:" || currentUrl.protocol === "https:") &&
@@ -75,32 +188,39 @@ function resolveConfiguredPrimaryTarget(): PrimaryEnvironmentTarget | null {
188
const resolvedHttpBaseUrl =
189
configuredHttpBaseUrl ??
190
(configuredWsBaseUrl?.startsWith("wss:")
78
- ? swapBaseUrlProtocol(configuredWsBaseUrl, "https:")
79
- : swapBaseUrlProtocol(configuredWsBaseUrl!, "http:"));
191
+ ? swapBaseUrlProtocol(configuredWsBaseUrl, "https:", "websocket-base-url")
192
+ : swapBaseUrlProtocol(configuredWsBaseUrl!, "http:", "websocket-base-url"));
193
const resolvedWsBaseUrl =
194
configuredWsBaseUrl ??
195
(configuredHttpBaseUrl?.startsWith("https:")
83
- ? swapBaseUrlProtocol(configuredHttpBaseUrl, "wss:")
84
- : swapBaseUrlProtocol(configuredHttpBaseUrl!, "ws:"));
196
+ ? swapBaseUrlProtocol(configuredHttpBaseUrl, "wss:", "http-base-url")
197
+ : swapBaseUrlProtocol(configuredHttpBaseUrl!, "ws:", "http-base-url"));
198
199
return {
200
source: "configured",
201
target: {
89
- httpBaseUrl: normalizeBaseUrl(resolvedHttpBaseUrl),
90
- wsBaseUrl: normalizeBaseUrl(resolvedWsBaseUrl),
202
+ httpBaseUrl: normalizeBaseUrl(resolvedHttpBaseUrl, "configured", "http-base-url"),
203
+ wsBaseUrl: normalizeBaseUrl(resolvedWsBaseUrl, "configured", "websocket-base-url"),
204
},
205
};
206
}
207
208
function resolveWindowOriginPrimaryTarget(): PrimaryEnvironmentTarget {
96
- const httpBaseUrl = normalizeBaseUrl(window.location.origin);
97
- const url = new URL(httpBaseUrl);
209
+ const url = parseTargetUrl({
210
+ rawValue: window.location.origin,
211
+ source: "window-origin",
212
+ urlKind: "http-base-url",
213
+ });
214
+ const httpBaseUrl = url.toString();
215
if (url.protocol === "http:") {
216
url.protocol = "ws:";
217
} else if (url.protocol === "https:") {
218
url.protocol = "wss:";
219
} else {
103
- throw new Error(`Unsupported HTTP base URL protocol: ${url.protocol}`);
220
+ throw new PrimaryEnvironmentProtocolUnsupportedError({
221
+ source: "window-origin",
222
+ protocol: url.protocol,
223
+ });
224
}
225
return {
226
source: "window-origin",
@@ -120,16 +240,25 @@ function resolveDesktopPrimaryTarget(): PrimaryEnvironmentTarget | null {
240
return null;
241
}
242
if (!desktopBootstrap.httpBaseUrl || !desktopBootstrap.wsBaseUrl) {
123
- throw new Error(
124
- "Desktop bootstrap must provide both httpBaseUrl and wsBaseUrl for the local environment.",
125
- );
243
+ throw new DesktopEnvironmentBootstrapIncompleteError({
244
+ hasHttpBaseUrl: Boolean(desktopBootstrap.httpBaseUrl),
245
+ hasWsBaseUrl: Boolean(desktopBootstrap.wsBaseUrl),
246
+ });
247
}
248
249
return {
250
source: "desktop-managed",
251
target: {
131
- httpBaseUrl: normalizeBaseUrl(desktopBootstrap.httpBaseUrl),
132
- wsBaseUrl: normalizeBaseUrl(desktopBootstrap.wsBaseUrl),
252
+ httpBaseUrl: normalizeBaseUrl(
253
+ desktopBootstrap.httpBaseUrl,
254
+ "desktop-managed",
255
+ "http-base-url",
256
+ ),
257
+ wsBaseUrl: normalizeBaseUrl(
258
+ desktopBootstrap.wsBaseUrl,
259
+ "desktop-managed",
260
+ "websocket-base-url",
261
+ ),
262
},
263
};
264
}
@@ -139,11 +268,12 @@ export function resolvePrimaryEnvironmentHttpUrl(
268
searchParams?: Record<string, string>,
269
): string {
270
const primaryTarget = readPrimaryEnvironmentTarget();
142
- if (!primaryTarget) {
143
- throw new Error("Unable to resolve the primary environment HTTP base URL.");
144
- }
271
146
- const url = new URL(resolveHttpRequestBaseUrl(primaryTarget.target.httpBaseUrl));
272
+ const url = parseTargetUrl({
273
+ rawValue: resolveHttpRequestBaseUrl(primaryTarget),
274
+ source: primaryTarget.source,
275
+ urlKind: "http-base-url",
276
+ });
277
url.pathname = pathname;
278
if (searchParams) {
279
url.search = new URLSearchParams(searchParams).toString();
@@ -151,7 +281,7 @@ export function resolvePrimaryEnvironmentHttpUrl(
281
return url.toString();
282
}
283
154
-export function readPrimaryEnvironmentTarget(): PrimaryEnvironmentTarget | null {
284
+export function readPrimaryEnvironmentTarget(): PrimaryEnvironmentTarget {
285
return (
286
resolveDesktopPrimaryTarget() ??
287
resolveConfiguredPrimaryTarget() ??