// Forked-only entry point used by `native-runtime-guard.ts` to verify that
// the local speech native modules can be loaded on this machine without
// crashing the main daemon. If any stage SEGV's or throws a SEH exception,
// the child exits non-zero and the parent records "failed". The main
// daemon never imports the native modules itself.
//
// Stage logs go to a plain text file (passed as argv[2]) using fs.writeSync
// so they survive native crashes that would otherwise lose buffered output.
import { closeSync, openSync, writeSync } from "node:fs";
const logPath = process.argv[2];
let logFd = null;
function openLog() {
    if (!logPath)
        return;
    try {
        logFd = openSync(logPath, "w");
    }
    catch {
        logFd = null;
    }
}
function logStage(stage, extra) {
    const line = `${new Date().toISOString()} [probe] ${stage}${extra ? " " + JSON.stringify(extra) : ""}\n`;
    if (logFd !== null) {
        try {
            writeSync(logFd, line);
        }
        catch {
            // best effort
        }
    }
}
function closeLog() {
    if (logFd !== null) {
        try {
            closeSync(logFd);
        }
        catch {
            // ignore
        }
        logFd = null;
    }
}
async function probe() {
    openLog();
    logStage("start", { platform: process.platform, node: process.version, pid: process.pid });
    // Use the same loader the main daemon would use, so the probe verifies
    // the exact same DLL resolution path. Importing the loader itself does
    // not trigger native loading; calling loadSherpaOnnxNode() does.
    logStage("require-loader:start");
    const { loadSherpaOnnxNode } = await import("./providers/local/sherpa/sherpa-onnx-node-loader.js");
    logStage("require-loader:ok");
    logStage("load-sherpa:start");
    loadSherpaOnnxNode();
    logStage("load-sherpa:ok");
    logStage("done");
    closeLog();
    process.exit(0);
}
process.on("uncaughtException", (err) => {
    logStage("uncaughtException", {
        message: err instanceof Error ? err.message : String(err),
        stack: err instanceof Error ? err.stack : undefined,
    });
    closeLog();
    process.exit(1);
});
process.on("unhandledRejection", (reason) => {
    logStage("unhandledRejection", {
        reason: reason instanceof Error ? reason.message : String(reason),
    });
    closeLog();
    process.exit(1);
});
probe().catch((err) => {
    logStage("probe-rejected", {
        message: err instanceof Error ? err.message : String(err),
    });
    closeLog();
    process.exit(1);
});
//# sourceMappingURL=native-runtime-probe.js.map