import * as path from 'path';
import { gcMailbox } from '../mailbox-gc.js';
import { mailboxIdForActiveSession } from '../mailbox-target.js';
import { getActiveSessions } from '../session/active.js';
import { loadLocalActiveSessions } from '../session/session-cache.js';
import { getRuntimeStateDir } from '../state.js';
import { runWatchdogTick } from './runner.js';
export async function loadWatchdogSessions() {
    const loaded = await loadLocalActiveSessions({
        gather: () => getActiveSessions({ localOnly: true }),
    });
    return loaded.sessions;
}
export function runWatchdogMailboxGc(sessions) {
    const activeBoxIds = new Set(sessions.map(mailboxIdForActiveSession).filter((id) => Boolean(id)));
    try {
        gcMailbox(activeBoxIds);
    }
    catch {
        // Housekeeping is retried by the next daemon tick.
    }
}
/** Execute one watchdog pass from the daemon or the explicit CLI command. */
export async function runWatchdogPass(opts) {
    const sessions = opts.sessions ?? await loadWatchdogSessions();
    const result = await runWatchdogTick({
        nudge: opts.nudge,
        nudgeText: opts.nudgeText,
        smartAgent: opts.smartAgent,
        thresholds: opts.thresholds,
        allowGhosttyFocus: opts.allowGhosttyFocus,
        stateDir: opts.stateDir ?? path.join(getRuntimeStateDir(), 'watchdog'),
        sessions,
    });
    if (opts.mailboxGc !== false)
        runWatchdogMailboxGc(sessions);
    return result;
}
