/** Dead managed tmux session and orphan-helper cleanup under supervision. */
import { getDefaultSocketPath } from '../tmux/paths.js';
import { reapDeadTmuxPanes } from '../tmux/session.js';
import { BasePeriodicService } from './service.js';
const TMUX_REAP_TICK_MS = 5 * 60_000;
const TMUX_REAP_DEADLINE_MS = 2 * 60_000;
export class TmuxReapService extends BasePeriodicService {
    id = 'tmux-reap';
    intervalMs = TMUX_REAP_TICK_MS;
    deadlineMs = TMUX_REAP_DEADLINE_MS;
    async onStart(_ctx) { }
    async onStop() { }
    async onTick(ctx) {
        const result = await reapDeadTmuxPanes(getDefaultSocketPath());
        for (const warning of result.warnings)
            ctx.log('WARN', `Dead-pane reaper: ${warning}`);
        if (result.processes > 0) {
            ctx.log('INFO', `Dead-pane reaper: terminated ${result.processes} orphaned helper process(es)`);
            for (const detail of result.processDetails)
                ctx.log('INFO', `  ${detail}`);
        }
        if (result.reaped > 0) {
            ctx.log('INFO', `Dead-pane reaper: reaped ${result.reaped} session(s)`);
            for (const detail of result.details)
                ctx.log('INFO', `  killed ${detail}`);
        }
    }
}
