import { readJson, writeJsonAtomic } from '../fsx.js';
import { loopKillRequestPath, loopProofPath, loopStatePath } from './loop-artifacts.js';
import { writeLoopCheckpoint } from './loop-checkpoint.js';
import { interruptLoopWorkers } from './loop-interrupt-registry.js';
export async function writeLoopKillRequest(root, missionId, target) {
    const request = { schema: 'sks.loop-kill-request.v1', mission_id: missionId, target, requested_at: new Date().toISOString() };
    await writeJsonAtomic(loopKillRequestPath(root, missionId), request);
    await interruptLoopWorkers({ root, missionId, target }).catch(() => undefined);
    return request;
}
export async function shouldKillLoop(root, missionId, loopId) {
    const request = await readJson(loopKillRequestPath(root, missionId), null);
    return request?.target === 'all' || request?.target === loopId;
}
export async function checkpointCancelledLoop(root, node, iteration, phase) {
    await writeLoopCheckpoint({
        root,
        mission_id: node.mission_id,
        loop_id: node.loop_id,
        iteration,
        phase,
        state_path: loopStatePath(root, node.mission_id, node.loop_id),
        proof_path: loopProofPath(root, node.mission_id, node.loop_id),
        resumable: true
    });
}
//# sourceMappingURL=loop-runtime-control.js.map