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, 04:45 UTC
| 1 | // @effect-diagnostics nodeBuiltinImport:off -- real processes stand in for servers and CLIs. |
| 2 | import * as NodeChildProcess from "node:child_process"; |
| 3 | import * as NodeCrypto from "node:crypto"; |
| 4 | import * as NodeFS from "node:fs"; |
| 5 | import * as NodeOS from "node:os"; |
| 6 | import * as NodePath from "node:path"; |
| 7 | |
| 8 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 9 | import { assert, describe, expect, it } from "@effect/vitest"; |
| 10 | import * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime"; |
| 11 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 12 | import * as Context from "effect/Context"; |
| 13 | import * as Deferred from "effect/Deferred"; |
| 14 | import * as Effect from "effect/Effect"; |
| 15 | import * as Layer from "effect/Layer"; |
| 16 | |
| 17 | import * as ServerConfig from "../config.ts"; |
| 18 | import * as ProviderProcesses from "./providerProcesses.ts"; |
| 19 | |
| 20 | // Real child processes stand in for servers and CLIs; only PIDs captured at spawn are signalled. |
| 21 | const ownedProcess = Effect.gen(function* () { |
| 22 | const ready = yield* Deferred.make<void>(); |
| 23 | const exited = yield* Deferred.make<void>(); |
| 24 | const child = yield* Effect.acquireRelease( |
| 25 | Effect.sync(() => { |
| 26 | const child = NodeChildProcess.spawn( |
| 27 | process.execPath, |
| 28 | ["-e", 'process.stdin.resume(); process.send("ready");'], |
| 29 | { stdio: ["pipe", "ignore", "ignore", "ipc"] }, |
| 30 | ); |
| 31 | child.once("message", () => Deferred.doneUnsafe(ready, Effect.void)); |
| 32 | child.once("error", (error) => Deferred.doneUnsafe(ready, Effect.die(error))); |
| 33 | child.once("exit", () => Deferred.doneUnsafe(exited, Effect.void)); |
| 34 | return child; |
| 35 | }), |
| 36 | (child) => |
| 37 | Effect.sync(() => child.kill("SIGKILL")).pipe(Effect.andThen(Deferred.await(exited))), |
| 38 | ); |
| 39 | yield* Deferred.await(ready); |
| 40 | const pid = child.pid; |
| 41 | assert(pid !== undefined); |
| 42 | const controller = AcpSessionRuntime.makePosixProcessTreeController("linux"); |
| 43 | const identity = controller.identity(pid); |
| 44 | assert(identity !== undefined); |
| 45 | const boot = NodeFS.readFileSync("/proc/sys/kernel/random/boot_id", "utf8").trim(); |
| 46 | return { |
| 47 | pid, |
| 48 | startTime: `${boot}:${identity.startTime}`, |
| 49 | pgid: 0, |
| 50 | running: () => { |
| 51 | const observed = controller.identity(pid); |
| 52 | return observed !== undefined && !AcpSessionRuntime.posixProcessIsZombie(observed); |
| 53 | }, |
| 54 | stop: Effect.sync(() => child.kill("SIGKILL")).pipe(Effect.andThen(Deferred.await(exited))), |
| 55 | }; |
| 56 | }); |
| 57 | |
| 58 | describe.runIf(HostProcess.Platform.defaultValue() === "linux")("ProviderProcesses", () => { |
| 59 | it.live("reaps only recorded CLIs of dead servers before startup completes", () => |
| 60 | Effect.gen(function* () { |
| 61 | const config = yield* ServerConfig.ServerConfig; |
| 62 | const directory = NodePath.join(config.stateDir, "provider-processes"); |
| 63 | NodeFS.mkdirSync(directory, { recursive: true }); |
| 64 | type Identity = { readonly pid: number; readonly startTime: string }; |
| 65 | const record = (owner: Identity, processes: ReadonlyArray<Identity>) => { |
| 66 | const instanceId = NodeCrypto.randomUUID(); |
| 67 | NodeFS.writeFileSync( |
| 68 | NodePath.join(directory, `${instanceId}.json`), |
| 69 | JSON.stringify({ |
| 70 | version: 1, |
| 71 | instanceId, |
| 72 | hostname: NodeOS.hostname(), |
| 73 | stateDir: config.stateDir, |
| 74 | owner: { pid: owner.pid, startTime: owner.startTime }, |
| 75 | processes: processes.map(({ pid, startTime }) => ({ pid, startTime, pgid: 0 })), |
| 76 | }), |
| 77 | ); |
| 78 | }; |
| 79 | const reused = (identity: Identity) => ({ |
| 80 | ...identity, |
| 81 | startTime: identity.startTime.replace(/:\d+$/, ":1"), |
| 82 | }); |
| 83 | const deadOwner = yield* ownedProcess; |
| 84 | const liveOwner = yield* ownedProcess; |
| 85 | const reusedOwner = yield* ownedProcess; |
| 86 | const liveProvider = yield* ownedProcess; |
| 87 | const reusedProvider = yield* ownedProcess; |
| 88 | const orphan = yield* ownedProcess; |
| 89 | const orphanOfReusedOwner = yield* ownedProcess; |
| 90 | const unrecorded = yield* ownedProcess; |
| 91 | record(liveOwner, [liveProvider]); |
| 92 | record(deadOwner, [reused(reusedProvider)]); |
| 93 | record(deadOwner, [orphan]); |
| 94 | record(reused(reusedOwner), [orphanOfReusedOwner]); |
| 95 | yield* deadOwner.stop; |
| 96 | |
| 97 | const context = yield* Layer.build(ProviderProcesses.layer); |
| 98 | expect(Context.get(context, ProviderProcesses.ProviderProcesses).hasOtherLiveOwner).toBe( |
| 99 | true, |
| 100 | ); |
| 101 | |
| 102 | expect({ |
| 103 | orphan: orphan.running(), |
| 104 | orphanOfReusedOwner: orphanOfReusedOwner.running(), |
| 105 | liveProvider: liveProvider.running(), |
| 106 | reusedProvider: reusedProvider.running(), |
| 107 | reusedOwner: reusedOwner.running(), |
| 108 | unrecorded: unrecorded.running(), |
| 109 | }).toEqual({ |
| 110 | orphan: false, |
| 111 | orphanOfReusedOwner: false, |
| 112 | liveProvider: true, |
| 113 | reusedProvider: true, |
| 114 | reusedOwner: true, |
| 115 | unrecorded: true, |
| 116 | }); |
| 117 | }).pipe( |
| 118 | Effect.scoped, |
| 119 | Effect.provideService(HostProcess.Platform, "linux"), |
| 120 | Effect.provide( |
| 121 | ServerConfig.layerTest(process.cwd(), { prefix: "t3-restart-ownership-" }).pipe( |
| 122 | Layer.provideMerge(NodeServices.layer), |
| 123 | ), |
| 124 | ), |
| 125 | ), |
| 126 | ); |
| 127 | }); |