The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 24, 22:45 UTC
[codex] Structure server environment-label probe failures (#3321)
Co-authored-by: codex <codex@users.noreply.github.com>
Julius Marminge committed
Jun 20, 2026 at 15:13 UTC
a51691a9b0a98e65f0e9b2b775b22a2382595e87
2 files changed
+235
-41
apps/server/src/environment/ServerEnvironmentLabel.test.ts
+124
-22
@@ -2,12 +2,28 @@ import { afterEach, describe, expect, it } from "@effect/vitest";
2
import * as Effect from "effect/Effect";
3
import * as FileSystem from "effect/FileSystem";
4
import * as Layer from "effect/Layer";
5
+import * as Logger from "effect/Logger";
6
+import * as PlatformError from "effect/PlatformError";
7
+import * as References from "effect/References";
8
+import * as Schema from "effect/Schema";
9
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
10
import { HostProcessHostname, HostProcessPlatform } from "@t3tools/shared/hostProcess";
11
import { vi } from "vite-plus/test";
12
13
import * as ProcessRunner from "../processRunner.ts";
10
-import { resolveServerEnvironmentLabel } from "./ServerEnvironmentLabel.ts";
14
+import * as ServerEnvironmentLabel from "./ServerEnvironmentLabel.ts";
15
+
16
+const isServerEnvironmentLabelFileError = Schema.is(
17
+ ServerEnvironmentLabel.ServerEnvironmentLabelFileError,
18
+);
19
+const isServerEnvironmentLabelCommandError = Schema.is(
20
+ ServerEnvironmentLabel.ServerEnvironmentLabelCommandError,
21
+);
22
+
23
+interface CapturedLog {
24
+ readonly message: unknown;
25
+ readonly annotations: Readonly<Record<string, unknown>>;
26
+}
27
28
const runMock = vi.fn<ProcessRunner.ProcessRunner["Service"]["run"]>();
29
@@ -47,7 +63,7 @@ afterEach(() => {
63
describe("resolveServerEnvironmentLabel", () => {
64
it.effect("uses hostname fallback regardless of launch mode", () =>
65
Effect.gen(function* () {
50
- const result = yield* resolveServerEnvironmentLabel({
66
+ const result = yield* ServerEnvironmentLabel.resolveServerEnvironmentLabel({
67
cwdBaseName: "t3code",
68
}).pipe(Effect.provide(withHostPlatform(TestLayer, "win32", "macbook-pro")));
69
@@ -68,7 +84,7 @@ describe("resolveServerEnvironmentLabel", () => {
84
}),
85
);
86
71
- const result = yield* resolveServerEnvironmentLabel({
87
+ const result = yield* ServerEnvironmentLabel.resolveServerEnvironmentLabel({
88
cwdBaseName: "t3code",
89
}).pipe(Effect.provide(withHostPlatform(TestLayer, "darwin", "macbook-pro")));
90
@@ -85,7 +101,7 @@ describe("resolveServerEnvironmentLabel", () => {
101
102
it.effect("prefers Linux PRETTY_HOSTNAME from machine-info", () =>
103
Effect.gen(function* () {
88
- const result = yield* resolveServerEnvironmentLabel({
104
+ const result = yield* ServerEnvironmentLabel.resolveServerEnvironmentLabel({
105
cwdBaseName: "t3code",
106
}).pipe(Effect.provide(withHostPlatform(LinuxMachineInfoLayer, "linux", "buildbox")));
107
@@ -107,7 +123,7 @@ describe("resolveServerEnvironmentLabel", () => {
123
}),
124
);
125
110
- const result = yield* resolveServerEnvironmentLabel({
126
+ const result = yield* ServerEnvironmentLabel.resolveServerEnvironmentLabel({
127
cwdBaseName: "t3code",
128
}).pipe(Effect.provide(withHostPlatform(TestLayer, "linux", "runner-01")));
129
@@ -124,7 +140,7 @@ describe("resolveServerEnvironmentLabel", () => {
140
141
it.effect("falls back to the hostname when friendly labels are unavailable", () =>
142
Effect.gen(function* () {
127
- const result = yield* resolveServerEnvironmentLabel({
143
+ const result = yield* ServerEnvironmentLabel.resolveServerEnvironmentLabel({
144
cwdBaseName: "t3code",
145
}).pipe(Effect.provide(withHostPlatform(TestLayer, "win32", "JULIUS-LAPTOP")));
146
@@ -132,25 +148,111 @@ describe("resolveServerEnvironmentLabel", () => {
148
}),
149
);
150
135
- it.effect("falls back to the hostname when the friendly-label command is missing", () =>
136
- Effect.gen(function* () {
137
- runMock.mockReturnValueOnce(
138
- Effect.fail(
139
- new ProcessRunner.ProcessSpawnError({
140
- command: "scutil",
141
- argumentCount: 2,
142
- cause: new Error("spawn scutil ENOENT"),
143
- }),
144
- ),
145
- );
151
+ it.effect("falls back to the hostname when the friendly-label command is missing", () => {
152
+ const logs: CapturedLog[] = [];
153
+ const logger = Logger.make(({ fiber, message }) => {
154
+ logs.push({
155
+ message,
156
+ annotations: fiber.getRef(References.CurrentLogAnnotations),
157
+ });
158
+ });
159
+ const spawnCause = new Error("spawn scutil ENOENT");
160
+ const processError = new ProcessRunner.ProcessSpawnError({
161
+ command: "scutil",
162
+ argumentCount: 2,
163
+ cause: spawnCause,
164
+ });
165
+ runMock.mockReturnValueOnce(Effect.fail(processError));
166
147
- const result = yield* resolveServerEnvironmentLabel({
167
+ return Effect.gen(function* () {
168
+ const result = yield* ServerEnvironmentLabel.resolveServerEnvironmentLabel({
169
cwdBaseName: "t3code",
149
- }).pipe(Effect.provide(withHostPlatform(TestLayer, "darwin", "macbook-pro")));
170
+ });
171
172
expect(result).toBe("macbook-pro");
152
- }),
153
- );
173
+ expect(logs[0]?.message).toEqual([
174
+ "Failed to run environment-label probe 'macos-computer-name' with scutil.",
175
+ ]);
176
+ const error = logs[0]?.annotations.cause;
177
+ expect(isServerEnvironmentLabelCommandError(error)).toBe(true);
178
+ if (isServerEnvironmentLabelCommandError(error)) {
179
+ expect(error.probe).toBe("macos-computer-name");
180
+ expect(error.executable).toBe("scutil");
181
+ expect(error.argumentCount).toBe(2);
182
+ expect(error).not.toHaveProperty("args");
183
+ expect(error.message).not.toContain("--get");
184
+ expect(error.message).not.toContain("ComputerName");
185
+ expect(error.cause).toBe(processError);
186
+ expect(processError.cause).toBe(spawnCause);
187
+ }
188
+ }).pipe(
189
+ Effect.provide(
190
+ Layer.mergeAll(
191
+ withHostPlatform(TestLayer, "darwin", "macbook-pro"),
192
+ Logger.layer([logger], { mergeWithExisting: false }),
193
+ Layer.succeed(References.MinimumLogLevel, "Debug"),
194
+ ),
195
+ ),
196
+ );
197
+ });
198
+
199
+ it.effect("continues to hostnamectl after a machine-info inspect failure", () => {
200
+ const logs: CapturedLog[] = [];
201
+ const logger = Logger.make(({ fiber, message }) => {
202
+ logs.push({
203
+ message,
204
+ annotations: fiber.getRef(References.CurrentLogAnnotations),
205
+ });
206
+ });
207
+ const fileCause = new Error("permission denied");
208
+ const platformError = PlatformError.systemError({
209
+ _tag: "PermissionDenied",
210
+ module: "FileSystem",
211
+ method: "exists",
212
+ pathOrDescriptor: "/etc/machine-info",
213
+ cause: fileCause,
214
+ });
215
+ const fileSystemLayer = FileSystem.layerNoop({
216
+ exists: () => Effect.fail(platformError),
217
+ });
218
+ runMock.mockReturnValueOnce(
219
+ Effect.succeed({
220
+ stdout: "CI Runner\n",
221
+ stderr: "",
222
+ code: ChildProcessSpawner.ExitCode(0),
223
+ timedOut: false,
224
+ stdoutTruncated: false,
225
+ stderrTruncated: false,
226
+ }),
227
+ );
228
+
229
+ return Effect.gen(function* () {
230
+ const result = yield* ServerEnvironmentLabel.resolveServerEnvironmentLabel({
231
+ cwdBaseName: "t3code",
232
+ });
233
+
234
+ expect(result).toBe("CI Runner");
235
+ expect(logs[0]?.message).toEqual([
236
+ "Failed to inspect environment-label file at /etc/machine-info.",
237
+ ]);
238
+ const error = logs[0]?.annotations.cause;
239
+ expect(isServerEnvironmentLabelFileError(error)).toBe(true);
240
+ if (isServerEnvironmentLabelFileError(error)) {
241
+ expect(error.operation).toBe("inspect");
242
+ expect(error.path).toBe("/etc/machine-info");
243
+ expect(error.cause).toBe(platformError);
244
+ expect(platformError.cause).toBe(fileCause);
245
+ }
246
+ }).pipe(
247
+ Effect.provide(
248
+ Layer.mergeAll(
249
+ withHostPlatform(Layer.merge(ProcessRunnerTest, fileSystemLayer), "linux", "buildbox"),
250
+ Logger.layer([logger], { mergeWithExisting: false }),
251
+ Layer.succeed(References.MinimumLogLevel, "Debug"),
252
+ ),
253
+ ),
254
+ );
255
+ });
256
257
it.effect("falls back to the cwd basename when the hostname is blank", () =>
258
Effect.gen(function* () {
@@ -165,7 +267,7 @@ describe("resolveServerEnvironmentLabel", () => {
267
}),
268
);
269
168
- const result = yield* resolveServerEnvironmentLabel({
270
+ const result = yield* ServerEnvironmentLabel.resolveServerEnvironmentLabel({
271
cwdBaseName: "t3code",
272
}).pipe(Effect.provide(withHostPlatform(TestLayer, "linux", " ")));
273
apps/server/src/environment/ServerEnvironmentLabel.ts
+111
-19
@@ -2,6 +2,7 @@ import { HostProcessHostname, HostProcessPlatform } from "@t3tools/shared/hostPr
2
import * as Effect from "effect/Effect";
3
import * as FileSystem from "effect/FileSystem";
4
import * as Option from "effect/Option";
5
+import * as Schema from "effect/Schema";
6
7
import * as ProcessRunner from "../processRunner.ts";
8
@@ -9,6 +10,39 @@ interface ResolveServerEnvironmentLabelInput {
10
readonly cwdBaseName: string;
11
}
12
13
+const ServerEnvironmentLabelCommandProbe = Schema.Literals([
14
+ "macos-computer-name",
15
+ "linux-pretty-hostname",
16
+]);
17
+type ServerEnvironmentLabelCommandProbe = typeof ServerEnvironmentLabelCommandProbe.Type;
18
+
19
+export class ServerEnvironmentLabelFileError extends Schema.TaggedErrorClass<ServerEnvironmentLabelFileError>()(
20
+ "ServerEnvironmentLabelFileError",
21
+ {
22
+ operation: Schema.Literals(["inspect", "read"]),
23
+ path: Schema.String,
24
+ cause: Schema.Defect(),
25
+ },
26
+) {
27
+ override get message(): string {
28
+ return `Failed to ${this.operation} environment-label file at ${this.path}.`;
29
+ }
30
+}
31
+
32
+export class ServerEnvironmentLabelCommandError extends Schema.TaggedErrorClass<ServerEnvironmentLabelCommandError>()(
33
+ "ServerEnvironmentLabelCommandError",
34
+ {
35
+ probe: ServerEnvironmentLabelCommandProbe,
36
+ executable: Schema.String,
37
+ argumentCount: Schema.Number,
38
+ cause: Schema.Defect(),
39
+ },
40
+) {
41
+ override get message(): string {
42
+ return `Failed to run environment-label probe '${this.probe}' with ${this.executable}.`;
43
+ }
44
+}
45
+
46
function normalizeLabel(value: string | null | undefined): string | null {
47
const trimmed = value?.trim();
48
return trimmed && trimmed.length > 0 ? trimmed : null;
@@ -34,30 +68,80 @@ function parseMachineInfoValue(raw: string, key: string): string | null {
68
69
const readLinuxMachineInfo = Effect.fn("readLinuxMachineInfo")(function* () {
70
const fileSystem = yield* FileSystem.FileSystem;
37
- const exists = yield* fileSystem
38
- .exists("/etc/machine-info")
39
- .pipe(Effect.orElseSucceed(() => false));
40
- if (!exists) {
41
- return null;
42
- }
43
-
44
- return yield* fileSystem
45
- .readFileString("/etc/machine-info")
46
- .pipe(Effect.orElseSucceed(() => null));
71
+ const machineInfoPath = "/etc/machine-info";
72
+ return yield* fileSystem.exists(machineInfoPath).pipe(
73
+ Effect.mapError(
74
+ (cause) =>
75
+ new ServerEnvironmentLabelFileError({
76
+ operation: "inspect",
77
+ path: machineInfoPath,
78
+ cause,
79
+ }),
80
+ ),
81
+ Effect.flatMap((exists) =>
82
+ exists
83
+ ? fileSystem.readFileString(machineInfoPath).pipe(
84
+ Effect.mapError(
85
+ (cause) =>
86
+ new ServerEnvironmentLabelFileError({
87
+ operation: "read",
88
+ path: machineInfoPath,
89
+ cause,
90
+ }),
91
+ ),
92
+ )
93
+ : Effect.succeed(null),
94
+ ),
95
+ Effect.catchTags({
96
+ ServerEnvironmentLabelFileError: (error) =>
97
+ Effect.logDebug(error.message).pipe(
98
+ Effect.annotateLogs({
99
+ operation: error.operation,
100
+ path: error.path,
101
+ cause: error,
102
+ }),
103
+ Effect.as(null),
104
+ ),
105
+ }),
106
+ );
107
});
108
49
-const runFriendlyLabelCommand = Effect.fn("runFriendlyLabelCommand")(function* (
50
- command: string,
51
- args: readonly string[],
52
-) {
109
+const runFriendlyLabelCommand = Effect.fn("runFriendlyLabelCommand")(function* (input: {
110
+ readonly probe: ServerEnvironmentLabelCommandProbe;
111
+ readonly command: string;
112
+ readonly args: readonly string[];
113
+}) {
114
const processRunner = yield* ProcessRunner.ProcessRunner;
115
const result = yield* processRunner
116
.run({
56
- command,
57
- args,
117
+ command: input.command,
118
+ args: input.args,
119
timeoutBehavior: "timedOutResult",
120
})
60
- .pipe(Effect.option);
121
+ .pipe(
122
+ Effect.mapError(
123
+ (cause) =>
124
+ new ServerEnvironmentLabelCommandError({
125
+ probe: input.probe,
126
+ executable: input.command,
127
+ argumentCount: input.args.length,
128
+ cause,
129
+ }),
130
+ ),
131
+ Effect.map(Option.some),
132
+ Effect.catchTags({
133
+ ServerEnvironmentLabelCommandError: (error) =>
134
+ Effect.logDebug(error.message).pipe(
135
+ Effect.annotateLogs({
136
+ probe: error.probe,
137
+ executable: error.executable,
138
+ argumentCount: error.argumentCount,
139
+ cause: error,
140
+ }),
141
+ Effect.as(Option.none()),
142
+ ),
143
+ }),
144
+ );
145
146
if (Option.isNone(result) || result.value.code !== 0) {
147
return null;
@@ -69,7 +153,11 @@ const runFriendlyLabelCommand = Effect.fn("runFriendlyLabelCommand")(function* (
153
const resolveFriendlyHostLabel = Effect.fn("resolveFriendlyHostLabel")(function* () {
154
const platform = yield* HostProcessPlatform;
155
if (platform === "darwin") {
72
- return yield* runFriendlyLabelCommand("scutil", ["--get", "ComputerName"]);
156
+ return yield* runFriendlyLabelCommand({
157
+ probe: "macos-computer-name",
158
+ command: "scutil",
159
+ args: ["--get", "ComputerName"],
160
+ });
161
}
162
163
if (platform === "linux") {
@@ -81,7 +169,11 @@ const resolveFriendlyHostLabel = Effect.fn("resolveFriendlyHostLabel")(function*
169
}
170
}
171
84
- return yield* runFriendlyLabelCommand("hostnamectl", ["--pretty"]);
172
+ return yield* runFriendlyLabelCommand({
173
+ probe: "linux-pretty-hostname",
174
+ command: "hostnamectl",
175
+ args: ["--pretty"],
176
+ });
177
}
178
179
return null;