import { writeJsonAtomic } from '../fsx.js';
import { loopFinalArbiterGateContractPath, loopGptFinalArbiterPath } from './loop-artifacts.js';
export async function writeLoopFinalArbiterGateContract(root, missionId) {
    const contract = buildLoopFinalArbiterGateContract(root, missionId);
    await writeJsonAtomic(loopFinalArbiterGateContractPath(root, missionId), { ...contract, generated_at: new Date().toISOString() });
    return contract;
}
export function buildLoopFinalArbiterGateContract(root, missionId) {
    return {
        schema: 'sks.loop-final-arbiter-gate-contract.v1',
        mission_id: missionId,
        gate_id: 'gpt:final-arbiter',
        handled_by: 'loop-finalizer',
        gate_runner_status: 'deferred',
        finalizer_artifact_path: relativeMissionArtifact(root, loopGptFinalArbiterPath(root, missionId)),
        required_when: ['source_mutation_exists', 'selected_gates_include_gpt_final_arbiter'],
        production_fixture_allowed: false
    };
}
export function loopFinalArbiterGateContractRelativePath(missionId) {
    return `.sneakoscope/missions/${missionId}/loops/gpt-final-arbiter-gate-contract.json`;
}
function relativeMissionArtifact(root, absolute) {
    const normalizedRoot = root.replace(/\\/g, '/').replace(/\/+$/, '');
    const normalized = absolute.replace(/\\/g, '/');
    return normalized.startsWith(`${normalizedRoot}/`) ? normalized.slice(normalizedRoot.length + 1) : normalized;
}
//# sourceMappingURL=loop-final-arbiter-contract.js.map