import { spawn } from 'node:child_process';
const PROBE_TIMEOUT_MS = 5000;
export function createDefaultSupervisorProbe() {
    return () => new Promise(resolve => {
        const child = spawn('claude', ['agents', '--json'], { timeout: PROBE_TIMEOUT_MS });
        let stdout = '';
        let timedOut = false;
        child.stdout?.on('data', chunk => {
            stdout += chunk.toString();
        });
        child.on('error', () => {
            resolve({ exitCode: -1, stdout, timedOut });
        });
        child.on('close', (code, signal) => {
            if (signal === 'SIGTERM') {
                timedOut = true;
            }
            resolve({
                exitCode: code ?? -1,
                stdout,
                timedOut,
            });
        });
    });
}
export function createDefaultSupervisorSpawner() {
    return () => {
        try {
            const child = spawn('claude', ['agents'], {
                detached: true,
                stdio: 'ignore',
                cwd: process.env.HOME ?? '/',
            });
            child.unref();
            return { pid: child.pid ?? null, error: null };
        }
        catch (error) {
            const message = error instanceof Error ? error.message : 'unknown spawn error';
            return { pid: null, error: message };
        }
    };
}
export class SupervisorCliGateway {
    deps;
    constructor(deps) {
        this.deps = deps;
    }
    async probe() {
        const probeResult = await this.deps.probe();
        if (probeResult.timedOut) {
            return { state: 'down', reason: `probe timeout after ${PROBE_TIMEOUT_MS}ms` };
        }
        if (probeResult.exitCode !== 0) {
            return { state: 'down', reason: `claude agents --json exited with code ${probeResult.exitCode}` };
        }
        let parsed;
        try {
            parsed = JSON.parse(probeResult.stdout);
        }
        catch {
            return { state: 'down', reason: 'invalid JSON returned by claude agents --json' };
        }
        if (!Array.isArray(parsed)) {
            return { state: 'down', reason: 'claude agents --json did not return a JSON array' };
        }
        return { state: 'up', reason: null };
    }
    async spawnDetached() {
        const result = this.deps.spawn();
        if (result.error !== null) {
            return { state: 'failed', pid: null, reason: result.error };
        }
        if (result.pid === null) {
            return { state: 'failed', pid: null, reason: 'spawned process returned no pid' };
        }
        return { state: 'spawned', pid: result.pid, reason: null };
    }
}
//# sourceMappingURL=supervisor.cli.gateway.js.map