From 744907b928f4f03888724338b7657e544da5a221 Mon Sep 17 00:00:00 2001 From: James Long Date: Thu, 5 Nov 2026 20:21:22 +0000 Subject: [PATCH] fix(core): forget stale finished daemon processes in the shell sweep --- packages/core/src/shell.ts | 13 +++++++++++++ packages/core/test/shell-daemon.test.ts | 16 ++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/core/src/shell.ts b/packages/core/src/shell.ts index f3e8b0e032b..2844a7f05a6 100644 --- a/packages/core/src/shell.ts +++ b/packages/core/src/shell.ts @@ -211,6 +211,19 @@ export const cleanup = Effect.fn("Shell.cleanup")(function* () { return process ? pty.terminatePipe(process.id).pipe(Effect.ignore) : Effect.void }, ) + // Finished processes whose file is gone, or that ended long ago, are of no use to any Location: forget them. + const cutoff = Date.now() - Duration.toMillis(RETENTION) + yield* Effect.forEach( + processes.values(), + (process) => + Effect.gen(function* () { + if (process.lifecycle.status === "running") return + const stale = (process.exitedAt ?? process.startedAt) < cutoff + if (!stale && (yield* fs.exists(process.outputPath).pipe(Effect.orElseSucceed(() => true)))) return + yield* pty.terminatePipe(process.id).pipe(Effect.ignore) + }), + { concurrency: 8, discard: true }, + ) }) const cleanupLayer = Layer.effectDiscard( diff --git a/packages/core/test/shell-daemon.test.ts b/packages/core/test/shell-daemon.test.ts index d18f5b897b0..35b6d32a942 100644 --- a/packages/core/test/shell-daemon.test.ts +++ b/packages/core/test/shell-daemon.test.ts @@ -1,6 +1,6 @@ import { describe, expect } from "bun:test" import { existsSync, readdirSync, realpathSync } from "node:fs" -import { mkdir, utimes, writeFile } from "node:fs/promises" +import { mkdir, rm, utimes, writeFile } from "node:fs/promises" import path from "node:path" import { Effect, Layer } from "effect" import { TestClock } from "effect/testing" @@ -414,9 +414,19 @@ describe("Shell on the process daemon", () => { const finished = yield* shell.create({ shell: "/bin/sh", command: "finished" }) env.daemon.exit(2, 0) yield* shell.wait(finished.id) - return { running, finished } + const lostFile = yield* shell.create({ shell: "/bin/sh", command: "lost file" }) + env.daemon.exit(3, 0) + yield* shell.wait(lostFile.id) + const old = yield* shell.create({ shell: "/bin/sh", command: "old" }) + env.daemon.exit(4, 0) + yield* shell.wait(old.id) + return { running, finished, lostFile, old } }), ) + // A finished process whose file was deleted elsewhere, and one that ended more than 7 days ago. + yield* Effect.promise(() => rm(ids.lostFile.file)) + const ended = env.daemon.processes.find((process) => process.id === 4) + if (ended) ended.exited_at = Date.now() - 8 * 24 * 60 * 60 * 1_000 const expired = new Date(Date.now() - 8 * 24 * 60 * 60 * 1_000) yield* Effect.promise(() => utimes(ids.running.file, expired, expired)) yield* Effect.promise(() => utimes(ids.finished.file, expired, expired)) @@ -437,6 +447,8 @@ describe("Shell on the process daemon", () => { expect(existsSync(ids.finished.file)).toBe(false) expect(existsSync(orphan)).toBe(false) expect(env.daemon.processes.map((process) => process.id)).toEqual([1]) + // The old process's file is recent, so it stays. + expect(existsSync(ids.old.file)).toBe(true) })) })