#!/usr/bin/env bash set -euo pipefail repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)" cd "$repo_root" if [[ "${PARTY2D_APPROVE_POSTESTIMATION:-}" != "YES" ]]; then echo "Refusing to read chains or run post-estimation without explicit approval." >&2 echo "After approval, rerun with PARTY2D_APPROVE_POSTESTIMATION=YES." >&2 exit 64 fi run_dir="${1:-$repo_root/_local/validation/blocked_party}" summary_dir="${2:-$repo_root/validation/outputs}" estimation_dir="$run_dir/estimations" if [[ ! -f "$run_dir/model_fit.complete" ]]; then echo "Refusing to finalize: the guarded fit is not marked complete." >&2 exit 1 fi if [[ ! -f "$run_dir/model_fit.exit_code" ]] || [[ "$(tr -dc '0-9' < "$run_dir/model_fit.exit_code")" != "0" ]]; then echo "Refusing to finalize: the guarded fit has no successful exit code." >&2 exit 1 fi mapfile -t model_runs < <(find "$run_dir/model_run/latest" -mindepth 1 -maxdepth 1 -type d -name 'run_*' 2>/dev/null | sort) if [[ "${#model_runs[@]}" -eq 0 ]]; then # The production saver currently writes its run under the repository-level # output directory even when --data-dir points at a validation workspace. # Match the completed fit by timestamp instead of relying on "latest" alone. fit_started="$(tr -d '\r\n' < "$run_dir/model_fit.started")" fit_completed="$(tr -d '\r\n' < "$run_dir/model_fit.complete")" mapfile -t model_runs < <(find "$repo_root/outputs/model_outputs/latest" -mindepth 1 -maxdepth 1 -type d -name 'run_*' -newermt "$fit_started" ! -newermt "$fit_completed" | sort) fi if [[ "${#model_runs[@]}" -ne 1 ]]; then echo "Expected exactly one blocked model run; found ${#model_runs[@]}." >&2 printf '%s\n' "${model_runs[@]}" >&2 exit 1 fi model_run="${model_runs[0]}" mkdir -p "$estimation_dir" "$summary_dir/blocked_fit_diagnostics" nice -n 10 julia --project=. src/julia/02_post_estimation.jl \ --run-dir "$model_run" --output-dir "$estimation_dir" \ 2>&1 | tee "$run_dir/post_estimation.log" mapfile -t position_files < <(find "$estimation_dir" -maxdepth 1 -type f -name 'party_positions_*.csv' | sort) if [[ "${#position_files[@]}" -ne 1 ]]; then echo "Expected exactly one blocked position file; found ${#position_files[@]}." >&2 exit 1 fi julia --project=. validation/summarize_blocked_validation.jl \ "${position_files[0]}" "$run_dir" "$summary_dir" \ 2>&1 | tee "$run_dir/blocked_summary.log" cp "$model_run/metadata.json" "$summary_dir/blocked_fit_diagnostics/metadata.json" cp "$model_run/diagnostics/run_metrics.json" "$summary_dir/blocked_fit_diagnostics/run_metrics.json" cp "$run_dir/blocked_validation_manifest.csv" "$summary_dir/blocked_fit_diagnostics/blocked_validation_manifest.csv" cp "$run_dir/blocked_validation_strata.csv" "$summary_dir/blocked_fit_diagnostics/blocked_validation_strata.csv" cp "$run_dir/blocked_parties.csv" "$summary_dir/blocked_fit_diagnostics/blocked_parties.csv" cp "$run_dir/input_sha256sums.txt" "$summary_dir/blocked_fit_diagnostics/input_sha256sums.txt" cp "$run_dir/model_fit.environment" "$summary_dir/blocked_fit_diagnostics/model_fit.environment" cp "$run_dir/stanc_version.txt" "$summary_dir/blocked_fit_diagnostics/stanc_version.txt" echo "Blocked validation finalized from: $model_run" echo "Position file: ${position_files[0]}" echo "Summary directory: $summary_dir"