//! Runtime Violation infrastructure — runtime contract enforcement.
//!
//! Compile-time violations (E1xx–E5xx) are caught by Guardian at build time.
//! Runtime violations (R1xx–) catch contract breaches that only manifest at runtime,
//! such as retry count exceeded, token reuse beyond declared limits, etc.
//!
//! All runtime violations terminate the process via `std::process::abort()`.
//! We do NOT use `panic!()` because the framework's `catch_unwind` machinery
//! (used by Fatal propagate's `panic!("tx_abort")`) would intercept it,
//! causing incorrect error handling flow.

/// Emit runtime violation R101: retry count exceeded.
///
/// Called when a token's propagate/suppress usage count exceeds its declared
/// `retry_count()` from concept-map.json.
///
/// This function never returns — it logs the violation and aborts the process.
pub(crate) fn emit_r101(
    tx_name: &str,
    operation_name: &str,
    actual: u32,
    declared: u32,
) -> ! {
    let msg = format!(
        "\n\
        [MAD-HATTER] RUNTIME VIOLATION [R101]: retry count exceeded\n\
        \x20 | transaction: {tx_name}\n\
        \x20 | token: {operation_name}\n\
        \x20 | actual: {actual}, declared: {declared}\n\
        \x20 | A token was used more times than its declared retry_count in concept-map.json.\n\
        \x20 | Fix: either increase retry_count in concept-map.json, or fix the loop bound\n\
        \x20 |      to use `token.retry_count()` as the upper limit.\n\
        \x20 | See target/mad-hatter-guide.md\n"
    );

    // Log through tracing (captured by structured logging if configured)
    tracing::error!("{}", msg);

    // Also print to stderr directly (in case tracing is not initialized or buffered)
    eprintln!("{}", msg);

    // Abort the process. Unlike panic!(), abort() cannot be caught by catch_unwind,
    // so it won't interfere with the framework's Fatal propagate mechanism.
    std::process::abort();
}