The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import * as FileSystem from "effect/FileSystem"; |
| 3 | import * as Path from "effect/Path"; |
| 4 | import * as Logger from "effect/Logger"; |
| 5 | import * as PlatformError from "effect/PlatformError"; |
| 6 | import { assert, expect, it } from "@effect/vitest"; |
| 7 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Fiber from "effect/Fiber"; |
| 10 | |
| 11 | import { |
| 12 | SERVICE_LAUNCHER_CONTEXT_ENV, |
| 13 | SERVICE_LAUNCHER_PROTOCOL, |
| 14 | type ServiceLauncherChildMessage, |
| 15 | type ServiceLauncherParentMessage, |
| 16 | } from "./serviceProtocol.ts"; |
| 17 | import { posixLauncherScript } from "./pinnedRuntime.ts"; |
| 18 | import * as ServiceLauncherClient from "./serviceLauncherClient.ts"; |
| 19 | |
| 20 | class FakeLauncherProcess { |
| 21 | readonly connected: boolean; |
| 22 | readonly env: Record<string, string | undefined>; |
| 23 | readonly sent: ServiceLauncherChildMessage[] = []; |
| 24 | readonly #listeners = new Map<string, Set<(...args: ReadonlyArray<unknown>) => void>>(); |
| 25 | |
| 26 | constructor(context: unknown, connected = true) { |
| 27 | this.connected = connected; |
| 28 | this.env = { [SERVICE_LAUNCHER_CONTEXT_ENV]: JSON.stringify(context) }; |
| 29 | } |
| 30 | |
| 31 | send = (message: ServiceLauncherChildMessage, callback?: (error: Error | null) => void) => { |
| 32 | this.sent.push(message); |
| 33 | callback?.(null); |
| 34 | return true; |
| 35 | }; |
| 36 | |
| 37 | on = (event: "message" | "disconnect", listener: (...args: ReadonlyArray<unknown>) => void) => { |
| 38 | const listeners = this.#listeners.get(event) ?? new Set(); |
| 39 | listeners.add(listener); |
| 40 | this.#listeners.set(event, listeners); |
| 41 | }; |
| 42 | |
| 43 | off = (event: "message" | "disconnect", listener: (...args: ReadonlyArray<unknown>) => void) => { |
| 44 | this.#listeners.get(event)?.delete(listener); |
| 45 | }; |
| 46 | |
| 47 | emit(message: ServiceLauncherParentMessage) { |
| 48 | for (const listener of this.#listeners.get("message") ?? []) listener(message); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | const makeClient = (host: FakeLauncherProcess, currentVersion: string) => |
| 53 | ServiceLauncherClient.make({ currentVersion }).pipe( |
| 54 | Effect.provideService(ServiceLauncherClient.ServiceLauncherHostProcess, host), |
| 55 | Effect.provideService(HostProcess.Environment, host.env), |
| 56 | Effect.provide(NodeServices.layer), |
| 57 | ); |
| 58 | |
| 59 | // regression: #34502 a remote update never replaces the launcher, so an installed one stays protocol 3 |
| 60 | it.effect.each([3, SERVICE_LAUNCHER_PROTOCOL])( |
| 61 | "waits for a protocol-%i launcher to durably commit the trial update ID, then moves owned launchers", |
| 62 | (protocol) => |
| 63 | Effect.gen(function* () { |
| 64 | const fs = yield* FileSystem.FileSystem; |
| 65 | const path = yield* Path.Path; |
| 66 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-trial-" }); |
| 67 | const baseDir = path.join(root, ".t3"); |
| 68 | const exe = (version: string) => path.join(baseDir, "runtime/versions", version, "t3"); |
| 69 | const installed = path.join(root, ".local/bin/t3"); |
| 70 | for (const file of [exe("1.0.0"), exe("1.1.0")]) { |
| 71 | yield* fs.makeDirectory(path.dirname(file), { recursive: true }); |
| 72 | yield* fs.writeFileString(file, ""); |
| 73 | } |
| 74 | yield* fs.makeDirectory(path.dirname(installed), { recursive: true }); |
| 75 | yield* fs.symlink(exe("1.0.0"), installed); |
| 76 | |
| 77 | const pending = { |
| 78 | id: "update-1", |
| 79 | fromVersion: "1.0.0", |
| 80 | targetVersion: "1.1.0", |
| 81 | dbPath: "/tmp/state.sqlite", |
| 82 | status: "pending" as const, |
| 83 | }; |
| 84 | const host = new FakeLauncherProcess({ protocol, childVersion: "1.1.0", update: pending }); |
| 85 | host.env["HOME"] = root; |
| 86 | const client = yield* makeClient(host, "1.1.0"); |
| 87 | const prepared = yield* Effect.forkChild(client.prepareTrial({ baseDir }), { |
| 88 | startImmediately: true, |
| 89 | }); |
| 90 | yield* Effect.yieldNow; |
| 91 | expect(host.sent).toEqual([{ type: "prepared", updateId: "update-1" }]); |
| 92 | assert.equal(yield* fs.readLink(installed), exe("1.0.0")); |
| 93 | |
| 94 | const committed = { |
| 95 | id: pending.id, |
| 96 | fromVersion: pending.fromVersion, |
| 97 | targetVersion: pending.targetVersion, |
| 98 | status: "committed" as const, |
| 99 | }; |
| 100 | host.emit({ type: "committed", updateId: committed.id }); |
| 101 | expect(yield* Fiber.join(prepared)).toEqual(committed); |
| 102 | assert.equal(yield* fs.readFileString(installed), posixLauncherScript(exe("1.1.0"))); |
| 103 | |
| 104 | // A later start replays the stored outcome; user pins made since then stay put. |
| 105 | yield* fs.remove(installed); |
| 106 | yield* fs.symlink(exe("1.0.0"), installed); |
| 107 | const restarted = new FakeLauncherProcess({ |
| 108 | protocol, |
| 109 | childVersion: "1.1.0", |
| 110 | update: committed, |
| 111 | }); |
| 112 | restarted.env["HOME"] = root; |
| 113 | const restartedClient = yield* makeClient(restarted, "1.1.0"); |
| 114 | expect(yield* restartedClient.prepareTrial({ baseDir })).toEqual(committed); |
| 115 | assert.equal(yield* fs.readLink(installed), exe("1.0.0")); |
| 116 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 117 | ); |
| 118 | |
| 119 | it.effect("returns the launcher-generated ID only after update acceptance", () => |
| 120 | Effect.gen(function* () { |
| 121 | const host = new FakeLauncherProcess({ |
| 122 | protocol: SERVICE_LAUNCHER_PROTOCOL, |
| 123 | childVersion: "1.0.0", |
| 124 | }); |
| 125 | const client = yield* makeClient(host, "1.0.0"); |
| 126 | const requested = yield* Effect.forkChild( |
| 127 | client.requestUpdate({ targetVersion: "1.1.0", dbPath: "/tmp/state.sqlite" }), |
| 128 | { startImmediately: true }, |
| 129 | ); |
| 130 | yield* Effect.yieldNow; |
| 131 | host.emit({ |
| 132 | type: "update-accepted", |
| 133 | updateId: "launcher-id", |
| 134 | }); |
| 135 | expect(yield* Fiber.join(requested)).toBe("launcher-id"); |
| 136 | }), |
| 137 | ); |
| 138 | |
| 139 | it.effect("preserves a launcher rejection as a distinct error", () => |
| 140 | Effect.gen(function* () { |
| 141 | const host = new FakeLauncherProcess({ |
| 142 | protocol: SERVICE_LAUNCHER_PROTOCOL, |
| 143 | childVersion: "1.0.0", |
| 144 | }); |
| 145 | const client = yield* makeClient(host, "1.0.0"); |
| 146 | const requested = yield* Effect.forkChild( |
| 147 | client.requestUpdate({ targetVersion: "1.1.0", dbPath: "/tmp/state.sqlite" }), |
| 148 | { startImmediately: true }, |
| 149 | ); |
| 150 | yield* Effect.yieldNow; |
| 151 | host.emit({ type: "update-rejected", reason: "requires local update" }); |
| 152 | expect(yield* Fiber.join(requested).pipe(Effect.flip)).toMatchObject({ |
| 153 | _tag: "ServiceLauncherRejectedError", |
| 154 | targetVersion: "1.1.0", |
| 155 | reason: "requires local update", |
| 156 | }); |
| 157 | }), |
| 158 | ); |
| 159 | |
| 160 | it.effect("stops the server when the launcher asks over IPC", () => |
| 161 | Effect.gen(function* () { |
| 162 | const host = new FakeLauncherProcess({ |
| 163 | protocol: SERVICE_LAUNCHER_PROTOCOL, |
| 164 | childVersion: "1.0.0", |
| 165 | }); |
| 166 | const stopped = yield* Effect.forkChild( |
| 167 | ServiceLauncherClient.awaitServiceLauncherStop({ currentVersion: "1.0.0" }).pipe( |
| 168 | Effect.provideService(ServiceLauncherClient.ServiceLauncherHostProcess, host), |
| 169 | Effect.provideService(HostProcess.Environment, host.env), |
| 170 | ), |
| 171 | { startImmediately: true }, |
| 172 | ); |
| 173 | host.emit({ type: "stop" }); |
| 174 | yield* Fiber.join(stopped); |
| 175 | }), |
| 176 | ); |
| 177 | |
| 178 | it.effect("rejects contradictory trial context instead of leaving activation closed", () => |
| 179 | Effect.gen(function* () { |
| 180 | const host = new FakeLauncherProcess({ |
| 181 | protocol: SERVICE_LAUNCHER_PROTOCOL, |
| 182 | childVersion: "1.1.0", |
| 183 | update: { |
| 184 | id: "update-1", |
| 185 | fromVersion: "1.0.0", |
| 186 | targetVersion: "1.2.0", |
| 187 | dbPath: "/tmp/state.sqlite", |
| 188 | status: "pending", |
| 189 | }, |
| 190 | }); |
| 191 | const error = yield* makeClient(host, "1.1.0").pipe(Effect.flip); |
| 192 | expect(error.message).toBe("The service launcher supplied invalid startup context."); |
| 193 | }), |
| 194 | ); |
| 195 | |
| 196 | it.effect("ignores launcher context inherited without an IPC channel", () => |
| 197 | Effect.gen(function* () { |
| 198 | const host = new FakeLauncherProcess( |
| 199 | { protocol: SERVICE_LAUNCHER_PROTOCOL, childVersion: "1.0.0" }, |
| 200 | false, |
| 201 | ); |
| 202 | const client = yield* makeClient(host, "1.1.0"); |
| 203 | const error = yield* client |
| 204 | .requestUpdate({ targetVersion: "1.2.0", dbPath: "/tmp/state.sqlite" }) |
| 205 | .pipe(Effect.flip); |
| 206 | expect(error).toMatchObject({ _tag: "ServiceLauncherClientError", operation: "unmanaged" }); |
| 207 | expect(host.sent).toEqual([]); |
| 208 | }), |
| 209 | ); |
| 210 | |
| 211 | it.layer(NodeServices.layer)("self-update launchers", (it) => { |
| 212 | it.effect( |
| 213 | "updates custom install and PATH launchers, including versions older than fromVersion", |
| 214 | () => |
| 215 | Effect.gen(function* () { |
| 216 | const fs = yield* FileSystem.FileSystem; |
| 217 | const path = yield* Path.Path; |
| 218 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" }); |
| 219 | const baseDir = path.join(root, ".t3"); |
| 220 | const exe = (version: string) => path.join(baseDir, "runtime/versions", version, "t3"); |
| 221 | const installed = path.join(root, "custom/bin/t3"); |
| 222 | const onPath = path.join(root, "bin/t3"); |
| 223 | for (const file of [exe("1.0.0"), exe("1.5.0"), exe("2.0.0")]) { |
| 224 | yield* fs.makeDirectory(path.dirname(file), { recursive: true }); |
| 225 | yield* fs.writeFileString(file, ""); |
| 226 | } |
| 227 | yield* fs.makeDirectory(path.dirname(installed), { recursive: true }); |
| 228 | yield* fs.writeFileString(installed, posixLauncherScript(exe("1.5.0")), { mode: 0o755 }); |
| 229 | yield* fs.makeDirectory(path.dirname(onPath), { recursive: true }); |
| 230 | yield* fs.symlink(exe("1.0.0"), onPath); |
| 231 | |
| 232 | yield* ServiceLauncherClient.repointLaunchers({ baseDir, targetVersion: "2.0.0" }).pipe( |
| 233 | Effect.provideService(HostProcess.Environment, { |
| 234 | HOME: root, |
| 235 | T3CODE_INSTALL_BIN_DIR: path.dirname(installed), |
| 236 | PATH: path.dirname(onPath), |
| 237 | }), |
| 238 | ); |
| 239 | |
| 240 | // The script is rewritten and the legacy symlink becomes one. |
| 241 | assert.equal(yield* fs.readFileString(installed), posixLauncherScript(exe("2.0.0"))); |
| 242 | assert.equal(yield* fs.readFileString(onPath), posixLauncherScript(exe("2.0.0"))); |
| 243 | }).pipe(Effect.provideService(HostProcess.Platform, "linux")), |
| 244 | ); |
| 245 | |
| 246 | it.effect("still moves the PATH launcher when the install bin one cannot be rewritten", () => |
| 247 | Effect.gen(function* () { |
| 248 | const fs = yield* FileSystem.FileSystem; |
| 249 | const path = yield* Path.Path; |
| 250 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" }); |
| 251 | const baseDir = path.join(root, ".t3"); |
| 252 | const exe = (version: string) => path.join(baseDir, "runtime/versions", version, "t3"); |
| 253 | const installed = path.join(root, ".local/bin/t3"); |
| 254 | const onPath = path.join(root, "bin/t3"); |
| 255 | for (const file of [exe("1.0.0"), exe("2.0.0")]) { |
| 256 | yield* fs.makeDirectory(path.dirname(file), { recursive: true }); |
| 257 | yield* fs.writeFileString(file, ""); |
| 258 | } |
| 259 | for (const link of [installed, onPath]) { |
| 260 | yield* fs.makeDirectory(path.dirname(link), { recursive: true }); |
| 261 | yield* fs.symlink(exe("1.0.0"), link); |
| 262 | } |
| 263 | const warnings: unknown[] = []; |
| 264 | const logger = Logger.make(({ logLevel, message }) => { |
| 265 | if (logLevel === "Warn") warnings.push(message); |
| 266 | }); |
| 267 | const failingFs = FileSystem.FileSystem.of({ |
| 268 | ...fs, |
| 269 | rename: (source, destination) => |
| 270 | destination === installed |
| 271 | ? Effect.fail( |
| 272 | new PlatformError.PlatformError( |
| 273 | new PlatformError.SystemError({ |
| 274 | _tag: "PermissionDenied", |
| 275 | module: "FileSystem", |
| 276 | method: "rename", |
| 277 | pathOrDescriptor: destination, |
| 278 | }), |
| 279 | ), |
| 280 | ) |
| 281 | : fs.rename(source, destination), |
| 282 | }); |
| 283 | |
| 284 | yield* ServiceLauncherClient.repointLaunchers({ baseDir, targetVersion: "2.0.0" }).pipe( |
| 285 | Effect.provideService(HostProcess.Environment, { |
| 286 | HOME: root, |
| 287 | PATH: path.dirname(onPath), |
| 288 | }), |
| 289 | Effect.provideService(FileSystem.FileSystem, failingFs), |
| 290 | Effect.provide(Logger.layer([logger], { mergeWithExisting: false })), |
| 291 | ); |
| 292 | |
| 293 | assert.lengthOf(warnings, 1); |
| 294 | assert.include(JSON.stringify(warnings), installed); |
| 295 | assert.equal(yield* fs.readLink(installed), exe("1.0.0")); |
| 296 | assert.equal(yield* fs.readFileString(onPath), posixLauncherScript(exe("2.0.0"))); |
| 297 | }).pipe(Effect.provideService(HostProcess.Platform, "linux")), |
| 298 | ); |
| 299 | |
| 300 | it.effect("leaves unowned install files and PATH symlinks untouched after self-update", () => |
| 301 | Effect.gen(function* () { |
| 302 | const fs = yield* FileSystem.FileSystem; |
| 303 | const path = yield* Path.Path; |
| 304 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" }); |
| 305 | const baseDir = path.join(root, ".t3"); |
| 306 | const installed = path.join(root, ".local/bin/t3"); |
| 307 | const onPath = path.join(root, "bin/t3"); |
| 308 | const foreign = path.join(root, "other-home/runtime/versions/1.0.0/t3"); |
| 309 | const target = path.join(baseDir, "runtime/versions/2.0.0/t3"); |
| 310 | for (const file of [installed, foreign, target]) { |
| 311 | yield* fs.makeDirectory(path.dirname(file), { recursive: true }); |
| 312 | yield* fs.writeFileString(file, "user-owned file"); |
| 313 | } |
| 314 | yield* fs.makeDirectory(path.dirname(onPath), { recursive: true }); |
| 315 | yield* fs.symlink(foreign, onPath); |
| 316 | |
| 317 | yield* ServiceLauncherClient.repointLaunchers({ baseDir, targetVersion: "2.0.0" }).pipe( |
| 318 | Effect.provideService(HostProcess.Environment, { |
| 319 | HOME: root, |
| 320 | PATH: path.dirname(onPath), |
| 321 | }), |
| 322 | ); |
| 323 | |
| 324 | assert.equal(yield* fs.readFileString(installed), "user-owned file"); |
| 325 | assert.equal(yield* fs.readLink(onPath), foreign); |
| 326 | }).pipe(Effect.provideService(HostProcess.Platform, "linux")), |
| 327 | ); |
| 328 | }); |