import { setCallbackActivityReporter } from "../../core/callback-activity.js";
import { writeRawStdoutControl } from "../../core/output-guard.js";
import { startParentProcessGuardian } from "../../utils/shell.js";
import { INTERACTIVE_ENGINE_PROTOCOL_VERSION, serializeInteractiveEngineMessage, } from "./protocol.js";
let activeLiveness;
export function startInteractiveEngineLiveness(write) {
    if (activeLiveness)
        return activeLiveness;
    if (process.env.ATOMIC_INTERACTIVE_ENGINE_CHILD !== "1")
        return { ready: () => { }, bound: () => { }, stop: () => { } };
    const hostPid = Number.parseInt(process.env.ATOMIC_INTERACTIVE_ENGINE_HOST_PID ?? "", 10);
    const stopGuardian = Number.isSafeInteger(hostPid) && hostPid > 0
        ? startParentProcessGuardian(hostPid, process.env.ATOMIC_INTERACTIVE_ENGINE_GUARD_FILE)
        : async () => { };
    const send = (message) => {
        write(serializeInteractiveEngineMessage(message));
    };
    setCallbackActivityReporter({
        started: (activity) => writeRawStdoutControl(serializeInteractiveEngineMessage({ type: "engine_activity_started", activity })),
        finished: (activityId) => send({ type: "engine_activity_finished", activityId }),
    });
    const heartbeat = setInterval(() => send({ type: "engine_heartbeat", at: performance.now() }), 50);
    heartbeat.unref?.();
    let readySent = false;
    let boundSent = false;
    const liveness = {
        ready: () => {
            if (readySent)
                return;
            readySent = true;
            send({ type: "engine_ready", protocolVersion: INTERACTIVE_ENGINE_PROTOCOL_VERSION, pid: process.pid });
        },
        bound: () => {
            if (boundSent)
                return;
            boundSent = true;
            send({ type: "engine_bound" });
        },
        stop: () => {
            clearInterval(heartbeat);
            setCallbackActivityReporter(undefined);
            void stopGuardian();
            if (activeLiveness === liveness)
                activeLiveness = undefined;
        },
    };
    activeLiveness = liveness;
    return liveness;
}
//# sourceMappingURL=engine-child-liveness.js.map