openpod

An opencode plugin that spins up a dedicated virtual machine for each session to isolate agent work.
git clone https://unitehenry.com/git/openpod.git
Log | Files | Refs | README

cleanup.ts (1680B)


      1 import { access, rm } from "node:fs/promises";
      2 import * as path from "node:path";
      3 
      4 import { exec } from "./exec";
      5 import type { Logger } from "./logger";
      6 
      7 export type Cleanup = {
      8   execute: (sessionID: string) => Promise<void>;
      9 };
     10 
     11 export function initCleanup(openpodRoot: string, logger?: Logger): Cleanup {
     12   const log = (message: string, level?: string): void => {
     13     logger?.log?.(message, level);
     14   };
     15 
     16   async function stopLimaInstance(limaName: string): Promise<void> {
     17     // best-effort stop (ignore errors)
     18     await exec("limactl", ["stop", limaName, "--force"]);
     19     log(`[cleanup] stopped ${limaName} (if running)`);
     20   }
     21 
     22   async function deleteLimaInstance(limaName: string): Promise<void> {
     23     const result = await exec("limactl", ["delete", limaName, "--force"]);
     24     log(`[cleanup] limactl delete ${limaName} exited with ${result.status}`);
     25   }
     26 
     27   async function removePodDirectory(podDir: string): Promise<void> {
     28     try {
     29       await access(podDir);
     30     } catch {
     31       log(`[cleanup] directory did not exist: ${podDir}`);
     32       return;
     33     }
     34 
     35     try {
     36       await rm(podDir, { recursive: true, force: true });
     37       log(`[cleanup] removed directory ${podDir}`);
     38     } catch (err) {
     39       log(`[cleanup] failed to remove ${podDir}: ${err}`);
     40     }
     41   }
     42 
     43   async function execute(sessionID: string): Promise<void> {
     44     if (!sessionID || typeof sessionID !== "string") return;
     45 
     46     const limaName = sessionID;
     47     const podDir = path.join(openpodRoot, sessionID);
     48 
     49     log(`[cleanup] starting for ${sessionID}`);
     50 
     51     await stopLimaInstance(limaName);
     52     await deleteLimaInstance(limaName);
     53     await removePodDirectory(podDir);
     54   }
     55 
     56   return { execute };
     57 }