sweepSessions
uses node:fs
What sessions that ended without their server stopping (it was killed) left, cleared away when a session's server
starts: a branch a server made for itself (session/<date>-<id>) that no running server works on goes as
endSession would take it (main has all of it, and no scratch file holds unsaved work), and so does a session
directory whose branch is gone, unless a scratch file in it holds unsaved work (tcm status lists those). Each is
decided in a write transaction, as servers claim their branches (startSession): one claimed meanwhile stays.
What went: branches, and directories relative to the codebase.
import { sessionDirsGone, unsavedIn } from "#codebase/tcmts/cli/codebase"; import { endSession, isSessionBranch, withCodebase } from "#codebase/tcmts/cli/mcp"; import fs from "node:fs"; import path from "node:path"; export function sweepSessions(root: string): string[] { const gone = withCodebase(root, (cb) => cb.store.branches()).filter( (b) => isSessionBranch(b) && endSession(root, b), ); const left = withCodebase(root, (cb) => cb.store.transaction(() => { const dirs = sessionDirsGone(root, cb.store.branches()).filter( (dir) => !unsavedIn(dir).length, ); for (const dir of dirs) fs.rmSync(dir, { recursive: true, force: true }); return dirs; }), ); return [...gone, ...left.map((dir) => path.relative(root, dir))]; }
uses
- tcmts.cli.codebase.sessionDirsGone
- tcmts.cli.codebase.unsavedIn
- tcmts.cli.mcp.endSession
- tcmts.cli.mcp.isSessionBranch
- tcmts.cli.mcp.withCodebase