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 21, 03:37 UTC
| 1 | // @effect-diagnostics nodeBuiltinImport:off |
| 2 | import * as NodeChildProcess from "node:child_process"; |
| 3 | |
| 4 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 5 | import { describe, expect, it } from "@effect/vitest"; |
| 6 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 7 | import * as Deferred from "effect/Deferred"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as FileSystem from "effect/FileSystem"; |
| 10 | import * as Path from "effect/Path"; |
| 11 | |
| 12 | import * as RelayClientLedger from "./RelayClientLedger.ts"; |
| 13 | |
| 14 | const isAlive = (pid: number) => { |
| 15 | try { |
| 16 | process.kill(pid, 0); |
| 17 | return true; |
| 18 | } catch { |
| 19 | return false; |
| 20 | } |
| 21 | }; |
| 22 | |
| 23 | const spawnSleep = Effect.acquireRelease( |
| 24 | Effect.gen(function* () { |
| 25 | const child = NodeChildProcess.spawn("sleep", ["600"], { stdio: "ignore" }); |
| 26 | const pid = child.pid; |
| 27 | if (pid === undefined) return yield* Effect.die("spawn failed"); |
| 28 | const exited = yield* Deferred.make<void>(); |
| 29 | child.once("exit", () => Deferred.doneUnsafe(exited, Effect.void)); |
| 30 | return { |
| 31 | pid, |
| 32 | kill: Effect.sync(() => child.kill("SIGKILL")).pipe(Effect.andThen(Deferred.await(exited))), |
| 33 | }; |
| 34 | }), |
| 35 | ({ kill }) => kill, |
| 36 | ); |
| 37 | |
| 38 | const entryNames = (stateDir: string) => |
| 39 | Effect.gen(function* () { |
| 40 | const fs = yield* FileSystem.FileSystem; |
| 41 | const path = yield* Path.Path; |
| 42 | return yield* fs |
| 43 | .readDirectory(path.join(stateDir, "relay-clients")) |
| 44 | .pipe(Effect.orElseSucceed(() => [])); |
| 45 | }); |
| 46 | |
| 47 | const hostPlatform = HostProcess.Platform.defaultValue(); |
| 48 | // procps accepts the same `ps` flags as macOS, so Linux also covers the macOS path. |
| 49 | const observedPlatforms: ReadonlyArray<NodeJS.Platform> = |
| 50 | hostPlatform === "linux" ? ["linux", "darwin"] : hostPlatform === "darwin" ? ["darwin"] : []; |
| 51 | |
| 52 | describe.each(observedPlatforms)("RelayClientLedger observing as %s", (platform) => { |
| 53 | const run = <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 54 | effect.pipe( |
| 55 | Effect.scoped, |
| 56 | Effect.provideService(HostProcess.Platform, platform), |
| 57 | Effect.provide(NodeServices.layer), |
| 58 | ); |
| 59 | |
| 60 | it.live("stops a client left by a dead server and keeps one whose server still runs", () => |
| 61 | run( |
| 62 | Effect.gen(function* () { |
| 63 | const fs = yield* FileSystem.FileSystem; |
| 64 | const stateDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-relay-ledger-" }); |
| 65 | const deadOwner = yield* spawnSleep; |
| 66 | const liveOwner = yield* spawnSleep; |
| 67 | const orphan = yield* spawnSleep; |
| 68 | const adopted = yield* spawnSleep; |
| 69 | |
| 70 | const dead = yield* RelayClientLedger.make({ stateDir, ownerPid: deadOwner.pid }); |
| 71 | yield* Effect.asVoid(dead.track(orphan.pid)); |
| 72 | const live = yield* RelayClientLedger.make({ stateDir, ownerPid: liveOwner.pid }); |
| 73 | yield* Effect.asVoid(live.track(adopted.pid)); |
| 74 | yield* deadOwner.kill; |
| 75 | |
| 76 | const restarted = yield* RelayClientLedger.make({ stateDir }); |
| 77 | yield* restarted.reapOrphans; |
| 78 | |
| 79 | expect(isAlive(orphan.pid)).toBe(false); |
| 80 | expect(isAlive(adopted.pid)).toBe(true); |
| 81 | expect(yield* entryNames(stateDir)).toEqual([`${adopted.pid}.json`]); |
| 82 | }), |
| 83 | ), |
| 84 | ); |
| 85 | |
| 86 | it.live("never signals a recycled pid and forgets the stale record", () => |
| 87 | run( |
| 88 | Effect.gen(function* () { |
| 89 | const fs = yield* FileSystem.FileSystem; |
| 90 | const path = yield* Path.Path; |
| 91 | const stateDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-relay-ledger-" }); |
| 92 | const deadOwner = yield* spawnSleep; |
| 93 | const unrelated = yield* spawnSleep; |
| 94 | |
| 95 | const dead = yield* RelayClientLedger.make({ stateDir, ownerPid: deadOwner.pid }); |
| 96 | yield* Effect.asVoid(dead.track(unrelated.pid)); |
| 97 | yield* deadOwner.kill; |
| 98 | // The recorded client has exited and its pid now belongs to another process. |
| 99 | const entryPath = path.join(stateDir, "relay-clients", `${unrelated.pid}.json`); |
| 100 | const entry = JSON.parse(yield* fs.readFileString(entryPath)); |
| 101 | entry.client.startTime = "0"; |
| 102 | yield* fs.writeFileString(entryPath, JSON.stringify(entry)); |
| 103 | |
| 104 | const restarted = yield* RelayClientLedger.make({ stateDir }); |
| 105 | yield* restarted.reapOrphans; |
| 106 | |
| 107 | expect(isAlive(unrelated.pid)).toBe(true); |
| 108 | expect(yield* entryNames(stateDir)).toEqual([]); |
| 109 | }), |
| 110 | ), |
| 111 | ); |
| 112 | }); |