060-process-reaper-cron.sql
1 -- Migration 060: Process Reaper Cron Job 2 -- Adds a cron job to kill stale agent subprocesses and log zombie/memory metrics. 3 -- Runs every 5 minutes (Tier 1.5 - between Process Guardian and Pipeline Monitor). 4 -- 5 -- Why: The Claude Code Task tool spawns claude subprocesses for background agents. 6 -- Under memory pressure, git execSync calls can hang. When the parent exits, 7 -- orphaned git processes become zombies. This reaper kills stale parents and 8 -- surfaces memory warnings early. 9 -- 10 -- Env vars (all optional): 11 -- REAPER_MAX_AGENT_AGE_MINUTES default 120 - kill claude processes older than this 12 -- REAPER_ZOMBIE_WARN_THRESHOLD default 500 - warn if zombie count exceeds this 13 -- REAPER_FREE_MEM_WARN_MB default 1024 - warn if free RAM drops below this 14 -- REAPER_SWAP_WARN_PCT default 0.7 - warn if swap usage exceeds this fraction 15 16 INSERT OR REPLACE INTO cron_jobs 17 (name, task_key, description, handler_type, handler_value, 18 interval_value, interval_unit, enabled) 19 VALUES 20 ('Process Reaper', 'processReaper', 21 'Kill stale agent processes (>2h), log zombie count + memory pressure', 22 'function', 'processReaper', 5, 'minutes', 1);