import path from 'node:path';
export function loopRoot(root, missionId) {
    const missionsRoot = path.resolve(root, '.sneakoscope', 'missions');
    return containedJoin(missionsRoot, safeArtifactId('mission', missionId), 'loops');
}
export function loopNodeRoot(root, missionId, loopId) {
    return containedJoin(loopRoot(root, missionId), safeArtifactId('loop', loopId));
}
export function loopPlanPath(root, missionId) {
    return path.join(loopRoot(root, missionId), 'loop-plan.json');
}
export function loopStatePath(root, missionId, loopId) {
    return path.join(loopNodeRoot(root, missionId, loopId), 'loop-state.json');
}
export function loopRunLogPath(root, missionId, loopId) {
    return path.join(loopNodeRoot(root, missionId, loopId), 'loop-run-log.jsonl');
}
export function loopProofPath(root, missionId, loopId) {
    return path.join(loopNodeRoot(root, missionId, loopId), 'loop-proof.json');
}
export function loopBudgetPath(root, missionId, loopId) {
    return path.join(loopNodeRoot(root, missionId, loopId), 'loop-budget.json');
}
export function loopCheckpointPath(root, missionId, loopId, iteration, phase) {
    return path.join(loopNodeRoot(root, missionId, loopId), 'checkpoints', `${String(Math.max(1, Math.floor(iteration))).padStart(4, '0')}-${sanitizeArtifactPart(phase)}.json`);
}
export function loopLatestCheckpointPath(root, missionId, loopId) {
    return path.join(loopNodeRoot(root, missionId, loopId), 'checkpoint-latest.json');
}
export function loopGraphProofPath(root, missionId) {
    return path.join(loopRoot(root, missionId), 'loop-graph-proof.json');
}
export function loopIntegrationMergePath(root, missionId) {
    return path.join(loopRoot(root, missionId), 'integration-merge.json');
}
export function loopGptFinalArbiterPath(root, missionId) {
    return path.join(loopRoot(root, missionId), 'loop-gpt-final-arbiter.json');
}
export function loopFinalArbiterGateContractPath(root, missionId) {
    return path.join(loopRoot(root, missionId), 'gpt-final-arbiter-gate-contract.json');
}
export function loopFixturePolicyPath(root, missionId) {
    return path.join(loopRoot(root, missionId), 'fixture-policy.json');
}
export function loopMutationLedgerPath(root, missionId) {
    return path.join(loopRoot(root, missionId), 'mutation-ledger.jsonl');
}
export function loopSideEffectReportPath(root, missionId) {
    return path.join(loopRoot(root, missionId), 'loop-side-effect-report.json');
}
export function loopActiveWorkerHandlesPath(root, missionId) {
    return path.join(loopRoot(root, missionId), 'active-worker-handles.jsonl');
}
export function loopInterruptResultPath(root, missionId) {
    return path.join(loopRoot(root, missionId), 'interrupt-result.json');
}
export function loopConcurrencyBudgetPath(root, missionId) {
    return path.join(loopRoot(root, missionId), 'concurrency-budget.json');
}
export function loopKillRequestPath(root, missionId) {
    return path.join(loopRoot(root, missionId), 'kill-request.json');
}
export function loopGatePath(root, missionId, loopId, gateId) {
    return path.join(loopNodeRoot(root, missionId, loopId), 'gates', `${sanitizeArtifactPart(gateId)}.json`);
}
export function loopPatchPath(root, missionId, loopId, name) {
    return path.join(loopNodeRoot(root, missionId, loopId), 'patches', `${sanitizeArtifactPart(name)}.json`);
}
export function loopHandoffPath(root, missionId, loopId) {
    return path.join(loopNodeRoot(root, missionId, loopId), 'handoff.md');
}
export function loopOwnerLedgerPath(root, missionId) {
    return path.join(loopRoot(root, missionId), 'loop-owner-ledger.json');
}
export function sanitizeArtifactPart(value) {
    return String(value || 'artifact').replace(/[^a-zA-Z0-9._-]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 96) || 'artifact';
}
function safeArtifactId(kind, value) {
    const text = String(value || '').trim();
    const sanitized = sanitizeArtifactPart(text);
    if (!text || sanitized !== text)
        throw new Error(`invalid_loop_${kind}_id:${text || 'empty'}`);
    return sanitized;
}
function containedJoin(base, ...parts) {
    const resolvedBase = path.resolve(base);
    const target = path.resolve(resolvedBase, ...parts);
    if (target !== resolvedBase && !target.startsWith(`${resolvedBase}${path.sep}`)) {
        throw new Error(`loop_artifact_path_escape:${target}`);
    }
    return target;
}
//# sourceMappingURL=loop-artifacts.js.map