"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.RuntimeProbe = void 0;
const child_process = __importStar(require("child_process"));
const util_1 = require("util");
const ExecutorHealth_1 = require("./ExecutorHealth");
const execFile = (0, util_1.promisify)(child_process.execFile);
class RuntimeProbe {
    executorHealth;
    commands;
    timeoutMs;
    constructor(options = {}) {
        this.executorHealth = new ExecutorHealth_1.ExecutorHealth();
        this.commands = options.commands ?? {};
        this.timeoutMs = options.timeoutMs ?? 15000;
    }
    async probeAll() {
        const executors = ['codex', 'gemini', 'claude', 'openclaude'];
        const results = [];
        for (const executor of executors) {
            results.push(await this.probe(executor));
        }
        return results;
    }
    async probe(executor) {
        try {
            const command = this.commandFor(executor);
            const { stdout, stderr } = await execFile(command[0], command.slice(1), { timeout: this.timeoutMs, killSignal: 'SIGKILL', maxBuffer: 1024 * 1024 * 5 });
            const detail = (stdout || stderr || 'ok').toString().trim().slice(0, 200) || 'ok';
            this.executorHealth.set(executor, true, detail);
            return {
                executor,
                available: true,
                reason: detail,
                category: 'ok',
                updatedAt: new Date().toISOString(),
                command
            };
        }
        catch (error) {
            const probeErr = error;
            const reason = probeErr.stderr || probeErr.stdout || probeErr.message || 'probe failed';
            this.executorHealth.set(executor, false, String(reason));
            const state = this.executorHealth.get(executor);
            return {
                executor,
                available: false,
                reason: String(reason).slice(0, 300),
                category: state.category,
                updatedAt: new Date().toISOString(),
                command: this.commandFor(executor)
            };
        }
    }
    commandFor(executor) {
        return this.commands[executor] ?? this.defaultCommandFor(executor);
    }
    defaultCommandFor(executor) {
        switch (executor) {
            case 'gemini':
                return ['gemini', '--version'];
            case 'claude':
                return ['claude', '--version'];
            case 'openclaude':
                return ['openclaude', '--version'];
            default:
                return ['codex', '--version'];
        }
    }
}
exports.RuntimeProbe = RuntimeProbe;
