Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeChildProcess from "node:child_process";
3import * as NodeReadline from "node:readline";
4
5import * as NodeServices from "@effect/platform-node/NodeServices";
6import { describe, expect, it } from "@effect/vitest";
7import * as HostProcess from "@t3tools/shared/HostProcess";
8import * as Deferred from "effect/Deferred";
9import * as Effect from "effect/Effect";
10import * as Exit from "effect/Exit";
11import * as Fiber from "effect/Fiber";
12import * as FileSystem from "effect/FileSystem";
13import * as Layer from "effect/Layer";
14import * as Path from "effect/Path";
15import * as Scope from "effect/Scope";
16import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
17
18import * as OpenCodeRuntime from "./OpenCodeRuntime.ts";
19import * as OpenCodeServerLedger from "./OpenCodeServerLedger.ts";
20
21const SERVE_ARGS = ["serve", "--hostname=127.0.0.1", "--port=4096"];
22
23const groupExists = (pgid: number) => {
24 try {
25 process.kill(-pgid, 0);
26 return true;
27 } catch {
28 return false;
29 }
30};
31
32// A group of zombies can be reaped between a check and the kill, so ESRCH just means it is gone.
33const killGroup = (pgid: number) =>
34 Effect.sync(() => {
35 try {
36 process.kill(-pgid, "SIGKILL");
37 } catch (cause) {
38 if (!(cause instanceof Error && "code" in cause && cause.code === "ESRCH")) throw cause;
39 }
40 });
41
42// libuv ignores a failed setsid(), which happens when the new pid equals the id of a group
43// that still has members (such as a sleep orphaned by an earlier test). The child would then
44// share the test worker's group, so the script first confirms it leads its own group and exits
45// with 97 before starting anything if it does not.
46const groupLeaderGuard = `pgid="$(ps -o pgid= -p $$ 2>&1 | tr -d ' ')"; [ "$pgid" = "$$" ] || { echo "ungrouped:$pgid"; exit 97; }; echo grouped; `;
47
48// Yields the group, or a description of why this shell cannot be used as one.
49const spawnGroupLeader = (args: ReadonlyArray<string>, script: string) =>
50 Effect.gen(function* () {
51 const child = NodeChildProcess.spawn("/bin/sh", ["-c", groupLeaderGuard + script, ...args], {
52 detached: true,
53 stdio: ["ignore", "pipe", "ignore"],
54 });
55 const pid = child.pid;
56 if (pid === undefined) return yield* Effect.die("spawn failed");
57 const grouped = yield* Deferred.make<true | string>();
58 const exited = yield* Deferred.make<NodeJS.Signals | null>();
59 const printed = yield* Deferred.make<void>();
60 child.once("exit", (_code, signal) => Deferred.doneUnsafe(exited, Effect.succeed(signal)));
61 // "close" fires after stdout drains, so a guard line always lands first. It only fires before
62 // "grouped" when the shell died without starting the script, such as from a signal.
63 child.once("close", (code, signal) =>
64 Deferred.doneUnsafe(
65 grouped,
66 Effect.succeed(
67 `shell ${pid} exited (code ${code}, signal ${signal}) before its group check`,
68 ),
69 ),
70 );
71 NodeReadline.createInterface({ input: child.stdout }).on("line", (line) => {
72 if (line === "grouped") Deferred.doneUnsafe(grouped, Effect.succeed(true));
73 if (line.startsWith("ungrouped:")) {
74 const output = line.slice("ungrouped:".length);
75 Deferred.doneUnsafe(
76 grouped,
77 Effect.succeed(`\`ps -o pgid= -p ${pid}\` printed ${JSON.stringify(output)}`),
78 );
79 }
80 if (line === "ready") Deferred.doneUnsafe(printed, Effect.void);
81 });
82 const result = yield* Deferred.await(grouped);
83 return result === true
84 ? { pid, exited: Deferred.await(exited), printed: Deferred.await(printed) }
85 : result;
86 });
87
88// A pid colliding with a live group id is rare, so a few retries clear it. Repeated failures
89// mean the guard itself is broken (for example a missing or BusyBox `ps`).
90const GROUP_LEADER_ATTEMPTS = 5;
91
92const spawnGroup = (args: ReadonlyArray<string>, script = "sleep 600 & wait") =>
93 Effect.acquireRelease(
94 Effect.gen(function* () {
95 let failure = "";
96 for (let attempt = 0; attempt < GROUP_LEADER_ATTEMPTS; attempt++) {
97 const group = yield* spawnGroupLeader(args, script);
98 if (typeof group !== "string") return group;
99 failure = group;
100 }
101 return yield* Effect.die(
102 `no shell led its own process group after ${GROUP_LEADER_ATTEMPTS} attempts; last: ${failure}`,
103 );
104 }),
105 ({ pid }) => killGroup(pid),
106 );
107
108const recordFromDeadServer = (stateDir: string, server: { readonly pid: number }) =>
109 Effect.gen(function* () {
110 const previousServer = yield* spawnGroup([]);
111 const previousLedger = yield* OpenCodeServerLedger.make({
112 stateDir,
113 ownerPid: previousServer.pid,
114 });
115 yield* Effect.asVoid(previousLedger.track({ pid: server.pid, port: 4096, args: SERVE_ARGS }));
116 yield* killGroup(previousServer.pid);
117 yield* previousServer.exited;
118 });
119
120const hostPlatform = HostProcess.Platform.defaultValue();
121// procps accepts the same `ps` flags as macOS, so Linux also covers the macOS path.
122const observedPlatforms: ReadonlyArray<NodeJS.Platform> =
123 hostPlatform === "linux" ? ["linux", "darwin"] : hostPlatform === "darwin" ? ["darwin"] : [];
124
125describe.each(observedPlatforms)("OpenCodeServerLedger observing as %s", (platform) => {
126 const provideHost = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
127 effect.pipe(
128 Effect.provideService(HostProcess.Platform, platform),
129 Effect.provide(NodeServices.layer),
130 );
131
132 it.live("stops an OpenCode server group left by a server that died", () =>
133 Effect.gen(function* () {
134 const fs = yield* FileSystem.FileSystem;
135 const path = yield* Path.Path;
136 const stateDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-opencode-ledger-" });
137 const orphan = yield* spawnGroup(SERVE_ARGS);
138 yield* recordFromDeadServer(stateDir, orphan);
139 expect(yield* fs.readDirectory(path.join(stateDir, "opencode-servers"))).toHaveLength(1);
140
141 const restarted = yield* OpenCodeServerLedger.make({ stateDir });
142 yield* restarted.reapOrphans;
143
144 expect(yield* orphan.exited).toBe("SIGTERM");
145 expect(groupExists(orphan.pid)).toBe(false);
146 expect(yield* fs.readDirectory(path.join(stateDir, "opencode-servers"))).toEqual([]);
147 }).pipe(provideHost),
148 );
149
150 it.live("records a group whose spawned wrapper already exited", () =>
151 Effect.gen(function* () {
152 const fs = yield* FileSystem.FileSystem;
153 const path = yield* Path.Path;
154 const stateDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-opencode-ledger-" });
155 // The inner shell prints once it has exec'd. Until then it still carries the wrapper's
156 // command line, and recording that would make the reaper skip the group as recycled.
157 const wrapper = yield* spawnGroup(
158 SERVE_ARGS,
159 '/bin/sh -c "echo ready; sleep 600 & wait" "$0" "$@" & exit 0',
160 );
161 yield* wrapper.exited;
162 yield* wrapper.printed;
163 expect(groupExists(wrapper.pid)).toBe(true);
164
165 yield* recordFromDeadServer(stateDir, wrapper);
166 expect(yield* fs.readDirectory(path.join(stateDir, "opencode-servers"))).toHaveLength(1);
167
168 const restarted = yield* OpenCodeServerLedger.make({ stateDir });
169 yield* restarted.reapOrphans;
170
171 expect(groupExists(wrapper.pid)).toBe(false);
172 expect(yield* fs.readDirectory(path.join(stateDir, "opencode-servers"))).toEqual([]);
173 }).pipe(provideHost),
174 );
175
176 it.live("leaves a recycled pid alone when its start time does not match", () =>
177 Effect.gen(function* () {
178 const fs = yield* FileSystem.FileSystem;
179 const path = yield* Path.Path;
180 const stateDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-opencode-ledger-" });
181 const unrelated = yield* spawnGroup(SERVE_ARGS);
182 yield* recordFromDeadServer(stateDir, unrelated);
183 const entryPath = path.join(stateDir, "opencode-servers", `${unrelated.pid}.json`);
184 const entry = yield* fs.readFileString(entryPath);
185 yield* fs.writeFileString(entryPath, entry.replace(/"startTime":"[^"]+"/, '"startTime":"1"'));
186
187 const restarted = yield* OpenCodeServerLedger.make({ stateDir });
188 yield* restarted.reapOrphans;
189
190 expect(groupExists(unrelated.pid)).toBe(true);
191 expect(yield* fs.exists(entryPath)).toBe(false);
192 }).pipe(provideHost),
193 );
194
195 it.live("leaves a serve process on another port alone", () =>
196 Effect.gen(function* () {
197 const fs = yield* FileSystem.FileSystem;
198 const path = yield* Path.Path;
199 const stateDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-opencode-ledger-" });
200 const unrelated = yield* spawnGroup(SERVE_ARGS);
201 yield* recordFromDeadServer(stateDir, unrelated);
202 const entryPath = path.join(stateDir, "opencode-servers", `${unrelated.pid}.json`);
203 const entry = yield* fs.readFileString(entryPath);
204 yield* fs.writeFileString(entryPath, entry.replace("--port=4096", "--port=4097"));
205
206 const restarted = yield* OpenCodeServerLedger.make({ stateDir });
207 yield* restarted.reapOrphans;
208
209 expect(groupExists(unrelated.pid)).toBe(true);
210 expect(yield* fs.exists(entryPath)).toBe(false);
211 }).pipe(provideHost),
212 );
213
214 it.live("leaves the servers of a running T3 server alone", () =>
215 Effect.gen(function* () {
216 const fs = yield* FileSystem.FileSystem;
217 const path = yield* Path.Path;
218 const stateDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-opencode-ledger-" });
219 const running = yield* OpenCodeServerLedger.make({ stateDir });
220 const server = yield* spawnGroup(SERVE_ARGS);
221 const forget = yield* running.track({ pid: server.pid, port: 4096, args: SERVE_ARGS });
222
223 const other = yield* OpenCodeServerLedger.make({ stateDir });
224 yield* other.reapOrphans;
225 expect(groupExists(server.pid)).toBe(true);
226
227 yield* forget;
228 expect(yield* fs.readDirectory(path.join(stateDir, "opencode-servers"))).toEqual([]);
229 }).pipe(provideHost),
230 );
231});
232
233describe.skipIf(observedPlatforms.length === 0)("OpenCode server startup", () => {
234 it.live("stops the group when interrupted while the server is being recorded", () =>
235 Effect.gen(function* () {
236 const fs = yield* FileSystem.FileSystem;
237 const path = yield* Path.Path;
238 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-opencode-startup-" });
239 const binaryPath = path.join(tempDir, "opencode");
240 yield* fs.writeFileString(binaryPath, "#!/bin/sh\nsleep 600 &\nexit 0\n");
241 yield* fs.chmod(binaryPath, 0o755);
242
243 const spawned = yield* Deferred.make<ChildProcessSpawner.ChildProcessHandle>();
244 const recording = yield* Deferred.make<number>();
245 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
246 const layerRuntime = OpenCodeRuntime.layer.pipe(
247 Layer.provide(
248 Layer.succeed(
249 ChildProcessSpawner.ChildProcessSpawner,
250 ChildProcessSpawner.make((command) =>
251 spawner
252 .spawn(command)
253 .pipe(Effect.tap((handle) => Deferred.succeed(spawned, handle))),
254 ),
255 ),
256 ),
257 Layer.provide(
258 Layer.succeed(
259 OpenCodeServerLedger.OpenCodeServerLedger,
260 OpenCodeServerLedger.OpenCodeServerLedger.of({
261 track: (server) =>
262 Effect.gen(function* () {
263 yield* (yield* Deferred.await(spawned)).exitCode.pipe(Effect.ignore);
264 yield* Deferred.succeed(recording, server.pid);
265 return yield* Effect.never;
266 }),
267 }),
268 ),
269 ),
270 );
271
272 const serverScope = yield* Scope.make();
273 const starting = yield* OpenCodeRuntime.OpenCodeRuntime.pipe(
274 Effect.flatMap((runtime) =>
275 runtime.startOpenCodeServerProcess({ binaryPath, directory: tempDir, port: 4096 }),
276 ),
277 Effect.provideService(Scope.Scope, serverScope),
278 Effect.provide(layerRuntime),
279 Effect.forkChild,
280 );
281 const pgid = yield* Deferred.await(recording);
282 yield* Effect.addFinalizer(() => killGroup(pgid));
283 expect(groupExists(pgid)).toBe(true);
284
285 yield* Fiber.interrupt(starting);
286 yield* Scope.close(serverScope, Exit.void);
287
288 expect(groupExists(pgid)).toBe(false);
289 }).pipe(Effect.provide(NodeServices.layer)),
290 );
291
292 // Runs on the test clock, which never advances here: a teardown that sleeps
293 // out its grace instead of watching the group would never finish.
294 it.effect("stops a running server as soon as its group exits", () =>
295 Effect.gen(function* () {
296 const fs = yield* FileSystem.FileSystem;
297 const path = yield* Path.Path;
298 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-opencode-stop-" });
299 const binaryPath = path.join(tempDir, "opencode");
300 // The background child shares the leader's group. The leader reaps it before exiting, so no
301 // zombie keeps the group alive under an init that does not reap. The trap is set after the
302 // fork: a child forked with the trap already set can swallow the signal before it execs.
303 yield* fs.writeFileString(
304 binaryPath,
305 [
306 "#!/bin/sh",
307 "sleep 600 &",
308 "trap 'wait; exit 0' TERM",
309 'echo "opencode server listening on http://127.0.0.1:4096"',
310 "wait",
311 "",
312 ].join("\n"),
313 );
314 yield* fs.chmod(binaryPath, 0o755);
315
316 const tracked = yield* Deferred.make<number>();
317 const layerRuntime = OpenCodeRuntime.layer.pipe(
318 Layer.provide(
319 Layer.succeed(
320 OpenCodeServerLedger.OpenCodeServerLedger,
321 OpenCodeServerLedger.OpenCodeServerLedger.of({
322 track: (server) => Deferred.succeed(tracked, server.pid).pipe(Effect.as(Effect.void)),
323 }),
324 ),
325 ),
326 );
327
328 const serverScope = yield* Scope.make();
329 const server = yield* OpenCodeRuntime.OpenCodeRuntime.pipe(
330 Effect.flatMap((runtime) =>
331 runtime.startOpenCodeServerProcess({
332 binaryPath,
333 directory: tempDir,
334 port: 4096,
335 verify: () => Effect.succeed("1.0.0"),
336 }),
337 ),
338 Effect.provideService(Scope.Scope, serverScope),
339 Effect.provide(layerRuntime),
340 );
341 const pgid = yield* Deferred.await(tracked);
342 yield* Effect.addFinalizer(() => killGroup(pgid));
343 expect(yield* server.isRunning).toBe(true);
344
345 yield* Scope.close(serverScope, Exit.void);
346
347 expect(groupExists(pgid)).toBe(false);
348 }).pipe(Effect.provide(NodeServices.layer)),
349 );
350});