/**
 * pm2 ecosystem entry for legal-reaper — a host-side daemon that periodically
 * reaps orphaned, runaway processes that saturate the Nautilus box:
 *   - task-master-mcp (Node) orphaned to ppid=1, ballooning to ~3GB each
 *     (memory: project_taskmaster_mcp_memory_leak).
 *   - camoufox-bin (Firefox) leftover from a crashed/killed X13 court fetch.
 * Only ppid=1 orphans are killed — live, parented processes are never touched.
 * See scripts/reap_orphan_procs.py for the safety rationale.
 *
 * Install (once):
 *     pm2 start /home/chaim/legal-ai/scripts/legal-reaper.config.cjs
 *     pm2 save
 * Logs:
 *     pm2 logs legal-reaper --lines 50
 *
 * Interval defaults to 180s; override with REAP_INTERVAL_S.
 */
const interval = process.env.REAP_INTERVAL_S || "180";

module.exports = {
  apps: [
    {
      name: "legal-reaper",
      cwd: "/home/chaim/legal-ai",
      script: "/home/chaim/legal-ai/mcp-server/.venv/bin/python",
      args: `scripts/reap_orphan_procs.py --loop ${interval}`,
      env: { HOME: "/home/chaim", PYTHONUNBUFFERED: "1" },
      autorestart: true,
      max_restarts: 20,
      restart_delay: 5000,
      // The reaper itself is tiny and must never be the thing that leaks.
      max_memory_restart: "100M",
    },
  ],
};
