The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { assert, expect } from "@alchemy.run/test"; |
| 2 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 3 | import * as Cause from "effect/Cause"; |
| 4 | import * as Clock from "effect/Clock"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Exit from "effect/Exit"; |
| 7 | import * as Fiber from "effect/Fiber"; |
| 8 | import * as FileSystem from "effect/FileSystem"; |
| 9 | import * as ChildProcess from "effect/process/ChildProcess"; |
| 10 | import * as Redacted from "effect/Redacted"; |
| 11 | import * as Scope from "effect/Scope"; |
| 12 | import * as pathe from "pathe"; |
| 13 | import * as Command from "@/Command"; |
| 14 | import type { ScopedPlanStatusSession } from "@/Report.ts"; |
| 15 | import { assertDead, lifecycleFixture, pgid, pidAlive } from "./fixture/lifecycle-support.ts"; |
| 16 | const { test } = Test.make({ providers: Command.providers() }); |
| 17 | |
| 18 | const session = (notes: Array<string>) => |
| 19 | ({ |
| 20 | note: (note: string) => |
| 21 | Effect.sync(() => { |
| 22 | notes.push(note); |
| 23 | }), |
| 24 | }) as unknown as ScopedPlanStatusSession; |
| 25 | |
| 26 | const withExecutor = <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 27 | effect.pipe(Effect.provide(Command.CommandExecutorLive())); |
| 28 | |
| 29 | test( |
| 30 | "returns stdout and stderr for a successful command", |
| 31 | withExecutor( |
| 32 | Effect.gen(function* () { |
| 33 | const executor = yield* Command.CommandExecutor; |
| 34 | const notes: Array<string> = []; |
| 35 | const result = yield* executor.run( |
| 36 | { |
| 37 | command: "printf 'hello'; printf 'warning' >&2", |
| 38 | shell: true, |
| 39 | }, |
| 40 | session(notes), |
| 41 | ); |
| 42 | |
| 43 | expect(result).toEqual({ |
| 44 | exitCode: 0, |
| 45 | stdout: "hello", |
| 46 | stderr: "warning", |
| 47 | }); |
| 48 | expect(notes).toEqual(expect.arrayContaining(["hello", "warning"])); |
| 49 | }), |
| 50 | ), |
| 51 | { tags: ["unit", "local"], timeout: 30_000 }, |
| 52 | ); |
| 53 | |
| 54 | test( |
| 55 | "returns a typed failure for a non-zero exit", |
| 56 | withExecutor( |
| 57 | Effect.gen(function* () { |
| 58 | const executor = yield* Command.CommandExecutor; |
| 59 | const error = yield* executor |
| 60 | .run( |
| 61 | { |
| 62 | command: "printf 'plain failure' >&2; exit 7", |
| 63 | shell: true, |
| 64 | }, |
| 65 | session([]), |
| 66 | ) |
| 67 | .pipe(Effect.flip); |
| 68 | |
| 69 | assert(Command.isCommandError(error)); |
| 70 | assert(error.reason._tag === "UnexpectedExit"); |
| 71 | expect(error.reason.exitCode).toBe(7); |
| 72 | expect(error.reason.stderr).toBe("plain failure"); |
| 73 | expect(error.message).toContain("exited with code 7"); |
| 74 | }), |
| 75 | ), |
| 76 | { tags: ["unit", "local"], timeout: 30_000 }, |
| 77 | ); |
| 78 | |
| 79 | test( |
| 80 | "times out and terminates descendants that ignore SIGTERM", |
| 81 | withExecutor( |
| 82 | Effect.gen(function* () { |
| 83 | const fs = yield* FileSystem.FileSystem; |
| 84 | const executor = yield* Command.CommandExecutor; |
| 85 | const cwd = yield* fs.makeTempDirectoryScoped(); |
| 86 | const pidFile = pathe.join(cwd, "child.pid"); |
| 87 | |
| 88 | const invalid = yield* executor |
| 89 | .run({ command: "true", shell: true, timeout: 0 }, session([])) |
| 90 | .pipe(Effect.flip); |
| 91 | assert(Command.isCommandError(invalid)); |
| 92 | assert(invalid.reason._tag === "BadArgument"); |
| 93 | expect(invalid.message).toContain("positive finite duration"); |
| 94 | |
| 95 | const error = yield* executor |
| 96 | .run( |
| 97 | { |
| 98 | command: 'trap \'\' TERM; sleep 30 & echo "$!" > "$PID_FILE"; wait', |
| 99 | cwd, |
| 100 | env: { PID_FILE: pidFile }, |
| 101 | shell: true, |
| 102 | timeout: "100 millis", |
| 103 | }, |
| 104 | session([]), |
| 105 | ) |
| 106 | .pipe(Effect.flip); |
| 107 | |
| 108 | assert(Command.isCommandError(error)); |
| 109 | assert(error.reason._tag === "CommandTimedOut"); |
| 110 | expect(error.reason.timeout).toBe("100ms"); |
| 111 | expect(error.message).toContain("process group was terminated"); |
| 112 | |
| 113 | const childPid = Number((yield* fs.readFileString(pidFile)).trim()); |
| 114 | expect(Number.isSafeInteger(childPid)).toBe(true); |
| 115 | |
| 116 | let alive = true; |
| 117 | for (let attempt = 0; attempt < 20; attempt++) { |
| 118 | alive = yield* Effect.sync(() => { |
| 119 | try { |
| 120 | process.kill(childPid, 0); |
| 121 | return true; |
| 122 | } catch (cause) { |
| 123 | if ( |
| 124 | typeof cause === "object" && |
| 125 | cause !== null && |
| 126 | "code" in cause && |
| 127 | cause.code === "ESRCH" |
| 128 | ) { |
| 129 | return false; |
| 130 | } |
| 131 | throw cause; |
| 132 | } |
| 133 | }); |
| 134 | if (!alive) break; |
| 135 | yield* Effect.sleep("50 millis"); |
| 136 | } |
| 137 | expect(alive).toBe(false); |
| 138 | }), |
| 139 | ), |
| 140 | { tags: ["unit", "local"], timeout: 30_000 }, |
| 141 | ); |
| 142 | |
| 143 | test( |
| 144 | "redacts Redacted env values across chunks, notes, results, and errors", |
| 145 | withExecutor( |
| 146 | Effect.gen(function* () { |
| 147 | const executor = yield* Command.CommandExecutor; |
| 148 | const secret = "super-secret-value"; |
| 149 | const notes: Array<string> = []; |
| 150 | const splitSecret = |
| 151 | "printf 'super-secret-'; sleep 0.05; printf 'value\\n'; " + |
| 152 | "printf 'super-secret-' >&2; sleep 0.05; printf 'value\\n' >&2"; |
| 153 | const props = { |
| 154 | command: splitSecret, |
| 155 | shell: true, |
| 156 | env: { SECRET: Redacted.make(secret) }, |
| 157 | } as const; |
| 158 | |
| 159 | const result = yield* executor.run(props, session(notes)); |
| 160 | expect(result.stdout).toBe("[REDACTED]\n"); |
| 161 | expect(result.stderr).toBe("[REDACTED]\n"); |
| 162 | expect(notes.join("\n")).not.toContain(secret); |
| 163 | expect(notes.join("\n")).toContain("[REDACTED]"); |
| 164 | |
| 165 | const error = yield* executor |
| 166 | .run( |
| 167 | { |
| 168 | command: "printf '%s\\n' \"$SECRET\" >&2; exit 9", |
| 169 | shell: true, |
| 170 | env: { SECRET: Redacted.make(secret) }, |
| 171 | }, |
| 172 | session(notes), |
| 173 | ) |
| 174 | .pipe(Effect.flip); |
| 175 | |
| 176 | assert(Command.isCommandError(error)); |
| 177 | assert(error.reason._tag === "UnexpectedExit"); |
| 178 | expect(error.reason.stderr).toBe("[REDACTED]\n"); |
| 179 | expect(error.message).not.toContain(secret); |
| 180 | expect(JSON.stringify(error)).not.toContain(secret); |
| 181 | expect(notes.join("\n")).not.toContain(secret); |
| 182 | |
| 183 | const spawnError = yield* executor |
| 184 | .run( |
| 185 | { |
| 186 | command: secret, |
| 187 | env: { SECRET: Redacted.make(secret) }, |
| 188 | }, |
| 189 | session(notes), |
| 190 | ) |
| 191 | .pipe(Effect.flip); |
| 192 | assert(Command.isCommandError(spawnError)); |
| 193 | expect(spawnError.message).not.toContain(secret); |
| 194 | expect(JSON.stringify(spawnError)).not.toContain(secret); |
| 195 | |
| 196 | // Even the conventional marker itself can be a legitimate secret. The |
| 197 | // replacement must never reproduce that exact value. |
| 198 | const markerCollision = "[REDACTED]"; |
| 199 | const collisionNotes: Array<string> = []; |
| 200 | const collision = yield* executor.run( |
| 201 | { |
| 202 | command: "printf '%s' \"$SECRET\"", |
| 203 | shell: true, |
| 204 | env: { SECRET: Redacted.make(markerCollision) }, |
| 205 | }, |
| 206 | session(collisionNotes), |
| 207 | ); |
| 208 | expect(collision.stdout).not.toContain(markerCollision); |
| 209 | expect(collisionNotes.join("\n")).not.toContain(markerCollision); |
| 210 | }), |
| 211 | ), |
| 212 | { tags: ["unit", "local"], timeout: 30_000 }, |
| 213 | ); |
| 214 | |
| 215 | for (const mode of ["cooperative", "stubborn", "early-exit"]) { |
| 216 | test.skipIf(process.platform === "win32")( |
| 217 | `scoped cleanup uses TERM before bounded escalation (${mode})`, |
| 218 | withExecutor( |
| 219 | Effect.gen(function* () { |
| 220 | const fixture = yield* lifecycleFixture(mode); |
| 221 | const executor = yield* Command.CommandExecutor; |
| 222 | const scope = yield* Effect.acquireRelease(Scope.make(), (scope) => |
| 223 | Scope.close(scope, Exit.void), |
| 224 | ); |
| 225 | yield* executor.spawn(fixture.props).pipe(Scope.provide(scope)); |
| 226 | const pids = yield* fixture.ready; |
| 227 | expect(yield* pgid(pids.leaf)).toBe(mode === "cooperative" ? pids.leaf : pids.wrapper); |
| 228 | const started = yield* Clock.currentTimeMillis; |
| 229 | yield* Scope.close(scope, Exit.void); |
| 230 | expect((yield* Clock.currentTimeMillis) - started).toBeLessThan(3500); |
| 231 | expect(yield* fixture.has("wrapper.term")).toBe(true); |
| 232 | expect(yield* fixture.has("wrapper.clean")).toBe(mode === "cooperative"); |
| 233 | yield* assertDead(pids.wrapper); |
| 234 | yield* assertDead(pids.leaf); |
| 235 | }), |
| 236 | ), |
| 237 | { tags: ["unit", "local"], timeout: 20_000 }, |
| 238 | ); |
| 239 | } |
| 240 | |
| 241 | test.skipIf(process.platform === "win32")( |
| 242 | "a nonzero leader exit hard-cleans its TERM-ignoring descendant before scope closure", |
| 243 | withExecutor( |
| 244 | Effect.gen(function* () { |
| 245 | const fixture = yield* lifecycleFixture("crash"); |
| 246 | const executor = yield* Command.CommandExecutor; |
| 247 | const fiber = yield* executor |
| 248 | .run(fixture.props, session([])) |
| 249 | .pipe(Effect.flip, Effect.forkScoped); |
| 250 | const pids = yield* fixture.ready; |
| 251 | expect(yield* pgid(pids.leaf)).toBe(pids.wrapper); |
| 252 | yield* fixture.crash; |
| 253 | const error = yield* Fiber.join(fiber).pipe(Effect.timeout("3 seconds")); |
| 254 | expect(error.reason._tag).toBe("UnexpectedExit"); |
| 255 | yield* assertDead(pids.wrapper); |
| 256 | yield* assertDead(pids.leaf); |
| 257 | }), |
| 258 | ), |
| 259 | { tags: ["unit", "local"], timeout: 20_000 }, |
| 260 | ); |
| 261 | |
| 262 | test.skipIf(process.platform === "win32")( |
| 263 | "negative control: immediate KILL prevents wrapper cleanup and leaves its detached child", |
| 264 | Effect.gen(function* () { |
| 265 | const fixture = yield* lifecycleFixture(); |
| 266 | const scope = yield* Effect.acquireRelease(Scope.make(), (scope) => |
| 267 | Scope.close(scope, Exit.void), |
| 268 | ); |
| 269 | yield* ChildProcess.make("bun", ["run", fixture.entry], { |
| 270 | env: fixture.props.env, |
| 271 | extendEnv: true, |
| 272 | killSignal: "SIGKILL", |
| 273 | }).pipe(Scope.provide(scope)); |
| 274 | const pids = yield* fixture.ready; |
| 275 | yield* Scope.close(scope, Exit.void); |
| 276 | expect(yield* fixture.has("wrapper.clean")).toBe(false); |
| 277 | expect(yield* pidAlive(pids.leaf)).toBe(true); |
| 278 | }), |
| 279 | { tags: ["unit", "local"], timeout: 20_000 }, |
| 280 | ); |
| 281 | |
| 282 | test.skipIf(process.platform === "win32")( |
| 283 | "cancelling run without a timeout gracefully cleans a wrapper and detached child", |
| 284 | withExecutor( |
| 285 | Effect.gen(function* () { |
| 286 | const fixture = yield* lifecycleFixture(); |
| 287 | const executor = yield* Command.CommandExecutor; |
| 288 | const fiber = yield* executor.run(fixture.props, session([])).pipe(Effect.forkScoped); |
| 289 | const pids = yield* fixture.ready; |
| 290 | yield* Fiber.interrupt(fiber); |
| 291 | const exit = yield* Fiber.await(fiber); |
| 292 | assert(Exit.isFailure(exit)); |
| 293 | expect(Cause.hasInterruptsOnly(exit.cause)).toBe(true); |
| 294 | expect(yield* fixture.has("wrapper.clean")).toBe(true); |
| 295 | yield* assertDead(pids.wrapper); |
| 296 | yield* assertDead(pids.leaf); |
| 297 | }), |
| 298 | ), |
| 299 | { tags: ["unit", "local"], timeout: 20_000 }, |
| 300 | ); |
| 301 | |
| 302 | test.skipIf(process.platform === "win32")( |
| 303 | "finite timeout preserves its typed error after graceful wrapper cleanup", |
| 304 | withExecutor( |
| 305 | Effect.gen(function* () { |
| 306 | const fixture = yield* lifecycleFixture(); |
| 307 | const executor = yield* Command.CommandExecutor; |
| 308 | const fiber = yield* executor |
| 309 | .run({ ...fixture.props, timeout: "2 seconds" }, session([])) |
| 310 | .pipe(Effect.flip, Effect.forkScoped); |
| 311 | const pids = yield* fixture.ready; |
| 312 | const error = yield* Fiber.join(fiber); |
| 313 | expect(error.reason._tag).toBe("CommandTimedOut"); |
| 314 | expect(yield* fixture.has("wrapper.clean")).toBe(true); |
| 315 | yield* assertDead(pids.wrapper); |
| 316 | yield* assertDead(pids.leaf); |
| 317 | }), |
| 318 | ), |
| 319 | { tags: ["unit", "local"], timeout: 20_000 }, |
| 320 | ); |
| 321 | |
| 322 | test.skipIf(process.platform === "win32")( |
| 323 | "negative control: TERM without escalation leaves stubborn processes alive", |
| 324 | Effect.gen(function* () { |
| 325 | const fixture = yield* lifecycleFixture("stubborn"); |
| 326 | const child = yield* ChildProcess.make("bun", ["run", fixture.entry], { |
| 327 | env: fixture.props.env, |
| 328 | extendEnv: true, |
| 329 | forceKillAfter: "1 second", |
| 330 | }); |
| 331 | const pids = yield* fixture.ready; |
| 332 | yield* child.kill({ killSignal: "SIGTERM" }).pipe(Effect.timeoutOption("200 millis")); |
| 333 | expect(yield* fixture.has("wrapper.term")).toBe(true); |
| 334 | expect(yield* pidAlive(pids.wrapper)).toBe(true); |
| 335 | expect(yield* pidAlive(pids.leaf)).toBe(true); |
| 336 | }), |
| 337 | { tags: ["unit", "local"], timeout: 20_000 }, |
| 338 | ); |