|
1 | 1 | import { expect } from "bun:test" |
2 | | -import { Deferred, Effect, Fiber, Layer, Queue, Scope, Sink, Stream } from "effect" |
| 2 | +import { Deferred, Effect, Exit, Fiber, Layer, Queue, Scope, Sink, Stream } from "effect" |
3 | 3 | import { ChildProcessSpawner } from "effect/process" |
4 | 4 | import { ExitCode, makeHandle, ProcessId } from "effect/process/ChildProcessSpawner" |
5 | 5 | import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" |
@@ -242,3 +242,77 @@ it.live("keeps a retained shell and its flag across a server restart", () => |
242 | 242 | ) |
243 | 243 | }), |
244 | 244 | ) |
| 245 | + |
| 246 | +const exitedShells = (env: Effect.Success<ReturnType<typeof daemonShells>>, shell: Shell.Interface) => |
| 247 | + Effect.fnUntraced(function* (retain?: boolean) { |
| 248 | + const info = yield* shell.create({ |
| 249 | + shell: "/bin/sh", |
| 250 | + command: "echo hello", |
| 251 | + timeout: 0, |
| 252 | + ...(retain ? { retain: true } : {}), |
| 253 | + }) |
| 254 | + const id = env.created().length |
| 255 | + env.daemon.exit(id, 0) |
| 256 | + yield* shell.wait(info.id) |
| 257 | + return info |
| 258 | + }) |
| 259 | + |
| 260 | +it.live("keeps up to 25 exited shells and evicts only the oldest past them", () => |
| 261 | + Effect.gen(function* () { |
| 262 | + const env = yield* daemonShells() |
| 263 | + yield* env.withShell(env.directory, (shell) => |
| 264 | + Effect.gen(function* () { |
| 265 | + const complete = exitedShells(env, shell) |
| 266 | + const infos = yield* Effect.forEach(Array.from({ length: 25 }), () => complete()) |
| 267 | + const last = yield* complete() |
| 268 | + // Eviction runs after an exit's waiters resolve, so wait for the 26th exit to take the oldest. |
| 269 | + yield* until(shell.get(infos[0]!.id).pipe(Effect.exit, Effect.map(Exit.isFailure))) |
| 270 | + // With 13 to 25 exited shells, none went before it. |
| 271 | + for (const info of [...infos.slice(1), last]) |
| 272 | + expect(yield* shell.get(info.id)).toMatchObject({ status: "exited" }) |
| 273 | + }), |
| 274 | + ) |
| 275 | + }), |
| 276 | +) |
| 277 | + |
| 278 | +it.live("adopts 24 unretained and 1 retained exited shell without evicting any", () => |
| 279 | + Effect.gen(function* () { |
| 280 | + const env = yield* daemonShells() |
| 281 | + const infos = yield* env.withShell(env.directory, (shell) => |
| 282 | + Effect.gen(function* () { |
| 283 | + const complete = exitedShells(env, shell) |
| 284 | + const retained = yield* complete(true) |
| 285 | + return [retained, ...(yield* Effect.forEach(Array.from({ length: 24 }), () => complete()))] |
| 286 | + }), |
| 287 | + ) |
| 288 | + yield* env.withShell(env.directory, (shell) => |
| 289 | + Effect.gen(function* () { |
| 290 | + for (const info of infos) expect(yield* shell.get(info.id)).toMatchObject({ status: "exited" }) |
| 291 | + }), |
| 292 | + ) |
| 293 | + }), |
| 294 | +) |
| 295 | + |
| 296 | +it.live("saves a release interrupted while it stores, so a restart does not keep the shell", () => |
| 297 | + Effect.gen(function* () { |
| 298 | + const env = yield* daemonShells() |
| 299 | + const saved = () => env.daemon.processes[0]?.pipe?.metadata.retain === undefined |
| 300 | + const retained = yield* env.withShell(env.directory, (shell) => |
| 301 | + Effect.gen(function* () { |
| 302 | + const info = yield* exitedShells(env, shell)(true) |
| 303 | + // Interrupted as soon as it starts to save, like a job cancelled during a user's stop. |
| 304 | + const released = yield* shell.release(info.id).pipe(Effect.forkChild({ startImmediately: true })) |
| 305 | + yield* Fiber.interrupt(released) |
| 306 | + yield* until(saved) |
| 307 | + return info |
| 308 | + }), |
| 309 | + ) |
| 310 | + yield* env.withShell(env.directory, (shell) => |
| 311 | + Effect.gen(function* () { |
| 312 | + // Released, so 25 newer unretained shells take it past the cap. |
| 313 | + yield* Effect.forEach(Array.from({ length: 25 }), () => exitedShells(env, shell)(), { discard: true }) |
| 314 | + yield* until(shell.get(retained.id).pipe(Effect.exit, Effect.map(Exit.isFailure))) |
| 315 | + }), |
| 316 | + ) |
| 317 | + }), |
| 318 | +) |
0 commit comments