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, 06:00 UTC
| 1 | import { spawn } from "node:child_process"; |
| 2 | import { resolve } from "node:path"; |
| 3 | import { assert, describe, expect, it, layer } from "@alchemy.run/test"; |
| 4 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 5 | import * as Clock from "effect/Clock"; |
| 6 | import * as Deferred from "effect/Deferred"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Exit from "effect/Exit"; |
| 9 | import * as Fiber from "effect/Fiber"; |
| 10 | import * as FileSystem from "effect/FileSystem"; |
| 11 | import * as Logger from "effect/Logger"; |
| 12 | import * as Option from "effect/Option"; |
| 13 | import * as Path from "effect/Path"; |
| 14 | import * as Schedule from "effect/Schedule"; |
| 15 | import { sanitizeLockKey, withLock } from "@/Auth/Lock.ts"; |
| 16 | import { rootDir } from "@/Auth/Paths.ts"; |
| 17 | import { recordOf } from "@/Util/LogRecord.ts"; |
| 18 | |
| 19 | describe("sanitizeLockKey", { tags: ["unit", "local"] }, () => { |
| 20 | it("leaves conventional keys untouched", () => { |
| 21 | expect(sanitizeLockKey("default-Cloudflare")).toBe("default-Cloudflare"); |
| 22 | expect(sanitizeLockKey("my_profile.v2-AWS")).toBe("my_profile.v2-AWS"); |
| 23 | }); |
| 24 | |
| 25 | it("neutralises unexpanded shell placeholders", () => { |
| 26 | // Seen verbatim in production: EINVAL mkdir |
| 27 | // '...\${ALCHEMY_PROFILE:-default}-Cloudflare.lock.lock' on Windows, |
| 28 | // where `:`/`$`/`{`/`}` are invalid in file names. |
| 29 | const sanitized = sanitizeLockKey("${ALCHEMY_PROFILE:-default}-Cloudflare"); |
| 30 | expect(sanitized).toBe("__ALCHEMY_PROFILE_-default_-Cloudflare"); |
| 31 | expect(sanitized).not.toMatch(/[<>:"/\\|?*${}]/); |
| 32 | }); |
| 33 | |
| 34 | it("replaces path separators so keys cannot escape the lock dir", () => { |
| 35 | expect(sanitizeLockKey("../../etc/passwd")).toBe(".._.._etc_passwd"); |
| 36 | }); |
| 37 | }); |
| 38 | |
| 39 | // Scenarios adapted from proper-lockfile's suite |
| 40 | // (https://github.com/moxystudio/node-proper-lockfile/tree/master/test) to |
| 41 | // the Effect lock: withLock is scoped acquire+release, the lockfile is a |
| 42 | // directory with an owner marker, staleness is 30s, the heartbeat is 10s. |
| 43 | layer(NodeServices.layer, { excludeTestServices: true })("withLock", (it) => { |
| 44 | const lockPathOf = Effect.fn(function* (key: string) { |
| 45 | const path = yield* Path.Path; |
| 46 | return path.join(rootDir(), "lock", `${sanitizeLockKey(key)}.lock`); |
| 47 | }); |
| 48 | |
| 49 | /** Create a foreign lock as another process would have left it. */ |
| 50 | const plantForeignLock = Effect.fn(function* ( |
| 51 | lockPath: string, |
| 52 | options?: { readonly ageMillis?: number }, |
| 53 | ) { |
| 54 | const fs = yield* FileSystem.FileSystem; |
| 55 | const path = yield* Path.Path; |
| 56 | yield* fs.makeDirectory(lockPath, { recursive: true }); |
| 57 | yield* fs.writeFileString(path.join(lockPath, "owner"), "foreign-owner"); |
| 58 | if (options?.ageMillis !== undefined) { |
| 59 | const stamp = new Date((yield* Clock.currentTimeMillis) - options.ageMillis); |
| 60 | yield* fs.utimes(lockPath, stamp, stamp); |
| 61 | } |
| 62 | }); |
| 63 | |
| 64 | const removeLock = Effect.fn(function* (lockPath: string) { |
| 65 | const fs = yield* FileSystem.FileSystem; |
| 66 | yield* fs.remove(lockPath, { recursive: true, force: true }); |
| 67 | }); |
| 68 | |
| 69 | it.effect( |
| 70 | "acquires and releases a lock whose key contains characters invalid in file names", |
| 71 | () => |
| 72 | Effect.gen(function* () { |
| 73 | // Regression for the production EINVAL: this key is unusable as a |
| 74 | // raw Windows file name without sanitisation. |
| 75 | const result = yield* withLock( |
| 76 | "${ALCHEMY_PROFILE:-default}-LockTest", |
| 77 | Effect.succeed("ran"), |
| 78 | ); |
| 79 | expect(result).toBe("ran"); |
| 80 | }), |
| 81 | { tags: ["unit", "local"] }, |
| 82 | ); |
| 83 | |
| 84 | it.effect( |
| 85 | "creates the lock directory while held and removes it on release", |
| 86 | () => |
| 87 | Effect.gen(function* () { |
| 88 | const fs = yield* FileSystem.FileSystem; |
| 89 | const key = "lock-test-lifecycle"; |
| 90 | const lockPath = yield* lockPathOf(key); |
| 91 | const seen = yield* withLock( |
| 92 | key, |
| 93 | Effect.all({ |
| 94 | lock: fs.exists(lockPath), |
| 95 | owner: fs |
| 96 | .readFileString(`${lockPath}/owner`) |
| 97 | .pipe(Effect.map((owner) => owner.length > 0)), |
| 98 | }), |
| 99 | ); |
| 100 | expect(seen).toEqual({ lock: true, owner: true }); |
| 101 | expect(yield* fs.exists(lockPath)).toBe(false); |
| 102 | }), |
| 103 | { tags: ["unit", "local"] }, |
| 104 | ); |
| 105 | |
| 106 | it.effect( |
| 107 | "serialises same-key critical sections in-process", |
| 108 | () => |
| 109 | Effect.gen(function* () { |
| 110 | const order: number[] = []; |
| 111 | const critical = (i: number) => |
| 112 | withLock( |
| 113 | "lock-test-serialise", |
| 114 | Effect.gen(function* () { |
| 115 | order.push(i); |
| 116 | yield* Effect.sleep("50 millis"); |
| 117 | order.push(i); |
| 118 | }), |
| 119 | ); |
| 120 | yield* Effect.all([critical(1), critical(2)], { concurrency: "unbounded" }); |
| 121 | // Each critical section's two entries must be adjacent — no |
| 122 | // interleaving between holders. |
| 123 | expect(order.slice(0, 2)).toEqual([order[0], order[0]]); |
| 124 | expect(order.slice(2)).toEqual([order[2], order[2]]); |
| 125 | }), |
| 126 | { tags: ["unit", "local"] }, |
| 127 | ); |
| 128 | |
| 129 | it.effect( |
| 130 | "allows different-key critical sections to overlap", |
| 131 | () => |
| 132 | Effect.gen(function* () { |
| 133 | const firstEntered = yield* Deferred.make<void>(); |
| 134 | const releaseFirst = yield* Deferred.make<void>(); |
| 135 | const first = yield* withLock( |
| 136 | "lock-test-independent-a", |
| 137 | Deferred.succeed(firstEntered, undefined).pipe( |
| 138 | Effect.andThen(Deferred.await(releaseFirst)), |
| 139 | ), |
| 140 | ).pipe(Effect.forkScoped); |
| 141 | |
| 142 | yield* Deferred.await(firstEntered); |
| 143 | const secondRan = yield* withLock("lock-test-independent-b", Effect.succeed(true)); |
| 144 | expect(secondRan).toBe(true); |
| 145 | |
| 146 | yield* Deferred.succeed(releaseFirst, undefined); |
| 147 | yield* Fiber.await(first); |
| 148 | }), |
| 149 | { tags: ["unit", "local"] }, |
| 150 | ); |
| 151 | |
| 152 | it.effect( |
| 153 | "excludes another process and recovers its lock after a crash", |
| 154 | () => |
| 155 | Effect.gen(function* () { |
| 156 | const fs = yield* FileSystem.FileSystem; |
| 157 | const key = `lock-test-process-${crypto.randomUUID()}`; |
| 158 | const lockPath = yield* lockPathOf(key); |
| 159 | const child = spawn(process.execPath, ["test/Auth/fixtures/lock-holder.ts", key], { |
| 160 | cwd: resolve(import.meta.dir, "../.."), |
| 161 | stdio: ["ignore", "pipe", "inherit"], |
| 162 | }); |
| 163 | |
| 164 | yield* Effect.addFinalizer(() => |
| 165 | Effect.sync(() => { |
| 166 | if (child.exitCode === null) child.kill("SIGKILL"); |
| 167 | }), |
| 168 | ); |
| 169 | |
| 170 | yield* Effect.tryPromise( |
| 171 | () => |
| 172 | new Promise<void>((resolve, reject) => { |
| 173 | child.once("error", reject); |
| 174 | child.once("exit", (code) => |
| 175 | reject(new Error(`lock holder exited before ready: ${code}`)), |
| 176 | ); |
| 177 | child.stdout.once("data", (chunk) => { |
| 178 | if (chunk.toString().includes("ready")) resolve(); |
| 179 | }); |
| 180 | }), |
| 181 | ); |
| 182 | |
| 183 | const blocked = yield* withLock(key, Effect.void, { timeout: "250 millis" }).pipe( |
| 184 | Effect.exit, |
| 185 | ); |
| 186 | assert(Exit.isFailure(blocked)); |
| 187 | expect(String(blocked.cause)).toContain("Timed out waiting"); |
| 188 | |
| 189 | child.kill("SIGKILL"); |
| 190 | yield* Effect.tryPromise( |
| 191 | () => |
| 192 | new Promise<void>((resolve) => { |
| 193 | if (child.exitCode !== null) resolve(); |
| 194 | else child.once("exit", () => resolve()); |
| 195 | }), |
| 196 | ); |
| 197 | expect(yield* fs.exists(lockPath)).toBe(true); |
| 198 | |
| 199 | const stale = new Date((yield* Clock.currentTimeMillis) - 60_000); |
| 200 | yield* fs.utimes(lockPath, stale, stale); |
| 201 | expect(yield* withLock(key, Effect.succeed("recovered"), { timeout: "2 seconds" })).toBe( |
| 202 | "recovered", |
| 203 | ); |
| 204 | expect(yield* fs.exists(lockPath)).toBe(false); |
| 205 | }).pipe(Effect.scoped), |
| 206 | { tags: ["unit", "local"], timeout: 10_000 }, |
| 207 | ); |
| 208 | |
| 209 | it.effect( |
| 210 | "releases the lock when the critical section fails", |
| 211 | () => |
| 212 | Effect.gen(function* () { |
| 213 | const fs = yield* FileSystem.FileSystem; |
| 214 | const key = "lock-test-release-on-failure"; |
| 215 | const lockPath = yield* lockPathOf(key); |
| 216 | const exit = yield* withLock(key, Effect.fail("boom")).pipe(Effect.exit); |
| 217 | assert(Exit.isFailure(exit)); |
| 218 | expect(String(exit.cause)).toContain("boom"); |
| 219 | expect(yield* fs.exists(lockPath)).toBe(false); |
| 220 | }), |
| 221 | { tags: ["unit", "local"] }, |
| 222 | ); |
| 223 | |
| 224 | it.effect( |
| 225 | "releases the lock when the holder is interrupted", |
| 226 | () => |
| 227 | Effect.gen(function* () { |
| 228 | const fs = yield* FileSystem.FileSystem; |
| 229 | const key = "lock-test-release-on-interrupt"; |
| 230 | const lockPath = yield* lockPathOf(key); |
| 231 | const entered = yield* Deferred.make<void>(); |
| 232 | const fiber = yield* withLock( |
| 233 | key, |
| 234 | Deferred.succeed(entered, undefined).pipe(Effect.andThen(Effect.never)), |
| 235 | ).pipe(Effect.forkScoped); |
| 236 | yield* Deferred.await(entered); |
| 237 | yield* Fiber.interrupt(fiber); |
| 238 | expect(yield* fs.exists(lockPath)).toBe(false); |
| 239 | }), |
| 240 | { tags: ["unit", "local"] }, |
| 241 | ); |
| 242 | |
| 243 | it.effect( |
| 244 | "releases the lock when interrupted during acquisition", |
| 245 | () => |
| 246 | Effect.gen(function* () { |
| 247 | const fs = yield* FileSystem.FileSystem; |
| 248 | const key = "lock-test-interrupt-acquisition"; |
| 249 | const lockPath = yield* lockPathOf(key); |
| 250 | const created = yield* Deferred.make<void>(); |
| 251 | const resume = yield* Deferred.make<void>(); |
| 252 | yield* Effect.addFinalizer(() => removeLock(lockPath).pipe(Effect.orDie)); |
| 253 | const acquisitionFs: FileSystem.FileSystem = { |
| 254 | ...fs, |
| 255 | makeDirectory: (directory, options) => |
| 256 | fs |
| 257 | .makeDirectory(directory, options) |
| 258 | .pipe( |
| 259 | Effect.andThen( |
| 260 | directory === lockPath |
| 261 | ? Deferred.succeed(created, undefined).pipe( |
| 262 | Effect.andThen(Deferred.await(resume)), |
| 263 | ) |
| 264 | : Effect.void, |
| 265 | ), |
| 266 | ), |
| 267 | }; |
| 268 | const holder = yield* withLock(key, Effect.never).pipe( |
| 269 | Effect.provideService(FileSystem.FileSystem, acquisitionFs), |
| 270 | Effect.forkScoped, |
| 271 | ); |
| 272 | yield* Deferred.await(created); |
| 273 | const interrupted = yield* Fiber.interrupt(holder).pipe( |
| 274 | Effect.forkScoped({ startImmediately: true }), |
| 275 | ); |
| 276 | yield* Deferred.succeed(resume, undefined); |
| 277 | yield* Fiber.join(interrupted); |
| 278 | expect(yield* fs.exists(lockPath)).toBe(false); |
| 279 | }).pipe(Effect.scoped), |
| 280 | { tags: ["unit", "local"] }, |
| 281 | ); |
| 282 | |
| 283 | it.effect( |
| 284 | "dies with a timeout while a live foreign lock is held", |
| 285 | () => |
| 286 | Effect.gen(function* () { |
| 287 | const fs = yield* FileSystem.FileSystem; |
| 288 | const key = "lock-test-foreign-holder"; |
| 289 | const lockPath = yield* lockPathOf(key); |
| 290 | yield* plantForeignLock(lockPath); |
| 291 | |
| 292 | const exit = yield* withLock(key, Effect.void, { timeout: "500 millis" }).pipe(Effect.exit); |
| 293 | assert(Exit.isFailure(exit)); |
| 294 | expect(String(exit.cause)).toMatch(/Timed out waiting/); |
| 295 | // A fresh (non-stale) foreign lock must survive our failed attempt. |
| 296 | expect(yield* fs.readFileString(`${lockPath}/owner`)).toBe("foreign-owner"); |
| 297 | yield* removeLock(lockPath); |
| 298 | }), |
| 299 | { tags: ["unit", "local"] }, |
| 300 | ); |
| 301 | |
| 302 | it.effect( |
| 303 | "reaps and acquires over a stale lock", |
| 304 | () => |
| 305 | Effect.gen(function* () { |
| 306 | const fs = yield* FileSystem.FileSystem; |
| 307 | const key = "lock-test-stale"; |
| 308 | const lockPath = yield* lockPathOf(key); |
| 309 | // A holder that crashed 60s ago: owner file present, mtime past the |
| 310 | // 30s stale threshold, nobody refreshing. |
| 311 | yield* plantForeignLock(lockPath, { ageMillis: 60_000 }); |
| 312 | |
| 313 | const result = yield* withLock(key, Effect.succeed("recovered"), { timeout: "5 seconds" }); |
| 314 | expect(result).toBe("recovered"); |
| 315 | expect(yield* fs.exists(lockPath)).toBe(false); |
| 316 | }), |
| 317 | { tags: ["unit", "local"] }, |
| 318 | ); |
| 319 | |
| 320 | it.effect( |
| 321 | "retries until a foreign lock is released", |
| 322 | () => |
| 323 | Effect.gen(function* () { |
| 324 | const key = "lock-test-wait-for-release"; |
| 325 | const lockPath = yield* lockPathOf(key); |
| 326 | yield* plantForeignLock(lockPath); |
| 327 | yield* removeLock(lockPath).pipe(Effect.delay("300 millis"), Effect.forkScoped); |
| 328 | const result = yield* withLock(key, Effect.succeed("ran"), { timeout: "5 seconds" }); |
| 329 | expect(result).toBe("ran"); |
| 330 | }), |
| 331 | { tags: ["unit", "local"] }, |
| 332 | ); |
| 333 | |
| 334 | it.effect( |
| 335 | "does not remove a lock it no longer owns on release", |
| 336 | () => |
| 337 | Effect.gen(function* () { |
| 338 | const fs = yield* FileSystem.FileSystem; |
| 339 | const key = "lock-test-compromised-release"; |
| 340 | const lockPath = yield* lockPathOf(key); |
| 341 | const release = yield* Deferred.make<void>(); |
| 342 | const fiber = yield* withLock(key, Deferred.await(release)).pipe(Effect.forkScoped); |
| 343 | yield* fs.exists(lockPath).pipe( |
| 344 | Effect.repeat({ |
| 345 | schedule: Schedule.spaced("25 millis"), |
| 346 | until: (exists) => exists, |
| 347 | times: 100, |
| 348 | }), |
| 349 | ); |
| 350 | // Another process reaped our lock as stale and took it over. |
| 351 | yield* fs.writeFileString(`${lockPath}/owner`, "foreign-owner"); |
| 352 | yield* Deferred.succeed(release, undefined); |
| 353 | yield* Fiber.await(fiber); |
| 354 | // Release must leave the usurper's lock in place. |
| 355 | expect(yield* fs.readFileString(`${lockPath}/owner`)).toBe("foreign-owner"); |
| 356 | yield* removeLock(lockPath); |
| 357 | }), |
| 358 | { tags: ["unit", "local"] }, |
| 359 | ); |
| 360 | |
| 361 | it.effect.skipIf(process.env.FAST)( |
| 362 | "refreshes the lock mtime while held", |
| 363 | () => |
| 364 | Effect.gen(function* () { |
| 365 | const fs = yield* FileSystem.FileSystem; |
| 366 | const key = "lock-test-heartbeat"; |
| 367 | const lockPath = yield* lockPathOf(key); |
| 368 | const release = yield* Deferred.make<void>(); |
| 369 | const fiber = yield* withLock(key, Deferred.await(release)).pipe(Effect.forkScoped); |
| 370 | yield* fs.exists(lockPath).pipe( |
| 371 | Effect.repeat({ |
| 372 | schedule: Schedule.spaced("25 millis"), |
| 373 | until: (exists) => exists, |
| 374 | times: 100, |
| 375 | }), |
| 376 | ); |
| 377 | // Age the lock past the heartbeat interval; the next refresh must |
| 378 | // bump the mtime back to the present so it never appears stale. |
| 379 | const before = new Date((yield* Clock.currentTimeMillis) - 25_000); |
| 380 | yield* fs.utimes(lockPath, before, before); |
| 381 | const refreshed = yield* fs.stat(lockPath).pipe( |
| 382 | Effect.map((info) => |
| 383 | Option.exists(info.mtime, (mtime) => mtime.getTime() > before.getTime() + 20_000), |
| 384 | ), |
| 385 | Effect.repeat({ |
| 386 | schedule: Schedule.spaced("500 millis"), |
| 387 | until: (bumped) => bumped, |
| 388 | times: 25, |
| 389 | }), |
| 390 | ); |
| 391 | expect(refreshed).toBe(true); |
| 392 | yield* Deferred.succeed(release, undefined); |
| 393 | yield* Fiber.await(fiber); |
| 394 | }), |
| 395 | { tags: ["unit", "local"], timeout: 30_000 }, |
| 396 | ); |
| 397 | /** |
| 398 | * A locked operation that never completes used to be entirely invisible: |
| 399 | * the deploy sat there with no output at any log level (#1231). The stall |
| 400 | * notice is the backstop that makes a hang self-describing, so it has to |
| 401 | * name the operation and it must stay quiet for flows that legitimately |
| 402 | * block on the user. |
| 403 | */ |
| 404 | /** Collects warnings logged while `self` runs. */ |
| 405 | const captureConsole = () => { |
| 406 | const messages: Array<string> = []; |
| 407 | return { |
| 408 | messages, |
| 409 | provide: <A, E, R>(self: Effect.Effect<A, E, R>) => |
| 410 | Effect.provide( |
| 411 | self, |
| 412 | Logger.layer([ |
| 413 | Logger.make<unknown, void>((options) => { |
| 414 | const record = recordOf(options); |
| 415 | if (record.kind === "log" && record.level === "warn") { |
| 416 | messages.push(record.data.message); |
| 417 | } |
| 418 | }), |
| 419 | ]), |
| 420 | ), |
| 421 | }; |
| 422 | }; |
| 423 | |
| 424 | it.effect( |
| 425 | "names the stalled operation in a warning", |
| 426 | () => |
| 427 | Effect.gen(function* () { |
| 428 | const console = captureConsole(); |
| 429 | yield* console.provide( |
| 430 | withLock("stall-notice-key", Effect.sleep("250 millis"), { |
| 431 | label: "AWS.read (profile 'probe')", |
| 432 | stallInterval: "50 millis", |
| 433 | }), |
| 434 | ); |
| 435 | expect(console.messages.length).toBeGreaterThan(0); |
| 436 | expect(console.messages[0]).toContain("AWS.read (profile 'probe')"); |
| 437 | expect(console.messages[0]).toContain("stall-notice-key.lock"); |
| 438 | yield* removeLock(yield* lockPathOf("stall-notice-key")); |
| 439 | }), |
| 440 | { tags: ["unit", "local"] }, |
| 441 | ); |
| 442 | |
| 443 | it.effect( |
| 444 | "stays silent when the watchdog is disabled", |
| 445 | () => |
| 446 | Effect.gen(function* () { |
| 447 | const console = captureConsole(); |
| 448 | yield* console.provide( |
| 449 | withLock("stall-quiet-key", Effect.sleep("250 millis"), { |
| 450 | watchdog: false, |
| 451 | stallInterval: "50 millis", |
| 452 | }), |
| 453 | ); |
| 454 | expect(console.messages).toEqual([]); |
| 455 | yield* removeLock(yield* lockPathOf("stall-quiet-key")); |
| 456 | }), |
| 457 | { tags: ["unit", "local"] }, |
| 458 | ); |
| 459 | }); |