The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { fileURLToPath } from "node:url"; |
| 2 | import { assert, describe, expect, it } from "@alchemy.run/test"; |
| 3 | import * as Clock from "effect/Clock"; |
| 4 | import * as Duration from "effect/Duration"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as FileSystem from "effect/FileSystem"; |
| 7 | import * as Path from "effect/Path"; |
| 8 | import * as ChildProcess from "effect/process/ChildProcess"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import * as Sink from "effect/Sink"; |
| 11 | import * as Stream from "effect/Stream"; |
| 12 | import { PlatformServices } from "@/Util/PlatformServices.ts"; |
| 13 | import { assertDead, lifecycleFixture } from "../Command/fixture/lifecycle-support.ts"; |
| 14 | import { |
| 15 | assertPidExited, |
| 16 | isAlive, |
| 17 | killPid, |
| 18 | pidListeningOn, |
| 19 | waitForExit, |
| 20 | } from "./fixtures/process-effect.ts"; |
| 21 | import { runtimes } from "./fixtures/runtimes.ts"; |
| 22 | |
| 23 | const PARENT_TS = fileURLToPath(new URL("./fixtures/rpc-spawner-parent.ts", import.meta.url)); |
| 24 | const CHILD_TS_URL = new URL("./fixtures/rpc-server-entry.ts", import.meta.url).toString(); |
| 25 | const DEVSERVER_PARENT_TS = fileURLToPath( |
| 26 | new URL("./fixtures/rpc-spawner-devserver-parent.ts", import.meta.url), |
| 27 | ); |
| 28 | // The dev sidecar entry plus the provider group it serves `Command.Dev` from. |
| 29 | const SIDECAR_TS_URL = new URL("../../src/Local/Sidecar.ts", import.meta.url).toString(); |
| 30 | const COMMAND_PROVIDERS_TS_URL = new URL("../../src/Command/Local.ts", import.meta.url).toString(); |
| 31 | const LONG_RUNNING_CJS = fileURLToPath( |
| 32 | new URL("../Command/fixture/long-running.cjs", import.meta.url), |
| 33 | ); |
| 34 | |
| 35 | for (const runtime of runtimes()) { |
| 36 | describe.skipIf(!runtime.available)( |
| 37 | `Local.RpcSpawner cleanup (${runtime.name})`, |
| 38 | { tags: ["local"] }, |
| 39 | () => { |
| 40 | /** |
| 41 | * Boots the parent fixture and waits until it has reported both its own |
| 42 | * pid and the child's RPC url (from which we resolve the child's pid via |
| 43 | * `lsof`). Retries the stdout parse on a schedule until both fields are |
| 44 | * populated. |
| 45 | */ |
| 46 | const launch = Effect.gen(function* () { |
| 47 | const [bin, ...args] = runtime.argv(PARENT_TS); |
| 48 | const child = yield* ChildProcess.make(bin, [...args, CHILD_TS_URL], { |
| 49 | stdout: "pipe", |
| 50 | forceKillAfter: "1 second", |
| 51 | }); |
| 52 | const output = yield* child.stdout.pipe( |
| 53 | Stream.decodeText, |
| 54 | Stream.run( |
| 55 | Sink.fold( |
| 56 | () => "", |
| 57 | (acc) => !acc.includes("CHILD_URL=") || !acc.includes("PARENT_PID="), |
| 58 | (acc, chunk) => Effect.succeed(acc + chunk), |
| 59 | ), |
| 60 | ), |
| 61 | // The parent fixture live-transforms TypeScript and spawns a second |
| 62 | // process doing the same; under worker contention (and on Windows, |
| 63 | // where process spawn is slower) this can take well over 5 seconds. |
| 64 | Effect.timeout("30 seconds"), |
| 65 | ); |
| 66 | |
| 67 | const childUrl = output.match(/CHILD_URL=(\S+)/)?.[1]; |
| 68 | const parentPid = Number.parseInt(output.match(/PARENT_PID=(\d+)/)?.[1]!, 10); |
| 69 | |
| 70 | assert(childUrl, `child url not found in output: ${output}`); |
| 71 | assert(!Number.isNaN(parentPid), `parent pid not found in output: ${output}`); |
| 72 | |
| 73 | const childPid = yield* pidListeningOn(childUrl); |
| 74 | |
| 75 | yield* Effect.addFinalizer(() => killPid(childPid, "SIGKILL")); |
| 76 | |
| 77 | return { child, parentPid, childPid }; |
| 78 | }); |
| 79 | |
| 80 | it.live( |
| 81 | "child dies after parent receives SIGTERM", |
| 82 | () => |
| 83 | Effect.gen(function* () { |
| 84 | const { child, parentPid, childPid } = yield* launch; |
| 85 | expect(yield* isAlive(childPid)).toBe(true); |
| 86 | yield* killPid(parentPid, "SIGTERM"); |
| 87 | // waitForExit wraps `handle.exitCode`, which resolves once |
| 88 | // the OS reports the parent's exit. |
| 89 | yield* waitForExit(child, Duration.seconds(10)); |
| 90 | yield* assertPidExited(childPid); |
| 91 | }).pipe(Effect.provide(PlatformServices)), |
| 92 | { tags: ["local"], timeout: 45_000 }, |
| 93 | ); |
| 94 | |
| 95 | it.live( |
| 96 | "child dies after parent receives SIGKILL", |
| 97 | () => |
| 98 | Effect.gen(function* () { |
| 99 | const { child, parentPid, childPid } = yield* launch; |
| 100 | expect(yield* isAlive(childPid)).toBe(true); |
| 101 | yield* killPid(parentPid, "SIGKILL"); |
| 102 | yield* waitForExit(child, Duration.seconds(10)); |
| 103 | yield* assertPidExited(childPid); |
| 104 | }).pipe(Effect.provide(PlatformServices)), |
| 105 | { tags: ["local"], timeout: 45_000 }, |
| 106 | ); |
| 107 | |
| 108 | it.live( |
| 109 | "DevServer child dies after parent receives SIGTERM", |
| 110 | () => |
| 111 | Effect.gen(function* () { |
| 112 | const [bin, ...args] = runtime.argv(DEVSERVER_PARENT_TS); |
| 113 | const fs = yield* FileSystem.FileSystem; |
| 114 | // `/tmp` doesn't exist on Windows — use a real temp directory. |
| 115 | const tmpDir = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-devserver-" }); |
| 116 | const pidFile = `${tmpDir}/${process.pid}-${runtime.name}.json`; |
| 117 | const child = yield* ChildProcess.make( |
| 118 | bin, |
| 119 | [ |
| 120 | ...args, |
| 121 | SIDECAR_TS_URL, |
| 122 | COMMAND_PROVIDERS_TS_URL, |
| 123 | `node ${LONG_RUNNING_CJS}`, |
| 124 | pidFile, |
| 125 | ], |
| 126 | { stdout: "pipe", forceKillAfter: "1 second" }, |
| 127 | ); |
| 128 | const output = yield* child.stdout.pipe( |
| 129 | Stream.decodeText, |
| 130 | Stream.run( |
| 131 | Sink.fold( |
| 132 | () => "", |
| 133 | (acc) => !acc.includes("PARENT_PID=") || !acc.includes("DEVSERVER_PID="), |
| 134 | (acc, chunk) => Effect.succeed(acc + chunk), |
| 135 | ), |
| 136 | ), |
| 137 | // See `launch` above — fixture startup can exceed 10s under |
| 138 | // contention and on Windows. |
| 139 | Effect.timeout("30 seconds"), |
| 140 | ); |
| 141 | |
| 142 | const parentPid = Number.parseInt(output.match(/PARENT_PID=(\d+)/)?.[1]!, 10); |
| 143 | const devServerPid = Number.parseInt(output.match(/DEVSERVER_PID=(\d+)/)?.[1]!, 10); |
| 144 | |
| 145 | assert(!Number.isNaN(parentPid), `parent pid not found in output: ${output}`); |
| 146 | assert(!Number.isNaN(devServerPid), `dev server pid not found in output: ${output}`); |
| 147 | |
| 148 | yield* Effect.addFinalizer(() => killPid(devServerPid, "SIGKILL")); |
| 149 | |
| 150 | expect(yield* isAlive(devServerPid)).toBe(true); |
| 151 | yield* killPid(parentPid, "SIGTERM"); |
| 152 | yield* waitForExit(child, Duration.seconds(10)); |
| 153 | yield* assertPidExited(devServerPid); |
| 154 | }).pipe(Effect.provide(PlatformServices)), |
| 155 | { tags: ["local"], timeout: 45_000 }, |
| 156 | ); |
| 157 | }, |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | it.live.skipIf(process.platform === "win32")( |
| 162 | "sidecar shutdown finishes multiple Command grace periods before its outer hard kill", |
| 163 | () => |
| 164 | Effect.gen(function* () { |
| 165 | const fs = yield* FileSystem.FileSystem; |
| 166 | const path = yield* Path.Path; |
| 167 | const home = yield* fs.makeTempDirectoryScoped({ |
| 168 | directory: "/tmp", |
| 169 | prefix: "rpc-command-shutdown-", |
| 170 | }); |
| 171 | const fixtures = yield* Effect.forEach( |
| 172 | ["stubborn", "stubborn", "stubborn", "cooperative"], |
| 173 | lifecycleFixture, |
| 174 | ); |
| 175 | const ready = path.join(home, "ready"); |
| 176 | const entry = yield* path.fromFileUrl( |
| 177 | new URL("./fixtures/rpc-spawner-commands.ts", import.meta.url), |
| 178 | ); |
| 179 | const parent = yield* ChildProcess.make("bun", ["run", entry], { |
| 180 | env: { |
| 181 | ALCHEMY_HOME: home, |
| 182 | COMMAND_FIXTURES: JSON.stringify({ |
| 183 | home, |
| 184 | ready, |
| 185 | commands: fixtures.map((fixture) => fixture.props), |
| 186 | }), |
| 187 | }, |
| 188 | extendEnv: true, |
| 189 | stdout: "ignore", |
| 190 | stderr: "inherit", |
| 191 | forceKillAfter: "6 seconds", |
| 192 | }); |
| 193 | expect( |
| 194 | yield* fs |
| 195 | .exists(ready) |
| 196 | .pipe( |
| 197 | Effect.repeat({ schedule: Schedule.spaced("50 millis"), until: Boolean, times: 400 }), |
| 198 | ), |
| 199 | ).toBe(true); |
| 200 | const pids = yield* Effect.forEach(fixtures, (fixture) => fixture.ready); |
| 201 | const start = yield* Clock.currentTimeMillis; |
| 202 | yield* parent.kill({ killSignal: "SIGTERM", forceKillAfter: "6 seconds" }); |
| 203 | expect((yield* Clock.currentTimeMillis) - start).toBeLessThan(5000); |
| 204 | expect(yield* fixtures[3]!.has("wrapper.clean")).toBe(true); |
| 205 | for (const fixture of fixtures) expect(yield* fixture.has("wrapper.term")).toBe(true); |
| 206 | for (const pair of pids) { |
| 207 | yield* assertDead(pair.wrapper); |
| 208 | yield* assertDead(pair.leaf); |
| 209 | } |
| 210 | }).pipe(Effect.scoped, Effect.provide(PlatformServices)), |
| 211 | { tags: ["local"], timeout: 40_000 }, |
| 212 | ); |