#!/bin/bash # Validate a task: oracle must pass, nop must fail, static checks must pass. # The oracle and nop verdicts are read from each trial's result.json (the # reward Harbor scored, whether the verifier ran, the exception the trial # raised), through scripts/lib/trial_verdict.py; Harbor's console output # decides only when that record is missing. In a profile that ships # checks/check-verifier-output.py, the files each trial's verifier wrote # (reward.txt, reward.json, ctrf.json) are then checked against the reward # that agent must produce, with the profile's reward keys. # # Usage: scripts/validate.sh # Example: scripts/validate.sh tasks/compile-postgres-with-sanitizers set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" TOOLKIT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" source "$SCRIPT_DIR/lib/profile.sh" source "$SCRIPT_DIR/lib/toolkit-env.sh" source "$SCRIPT_DIR/lib/toml.sh" CI_CHECKS_DIR="$PROFILE_CHECKS_DIR" VERIFIER_OUTPUT_CHECK="$CI_CHECKS_DIR/check-verifier-output.py" TRIAL_VERDICT="$SCRIPT_DIR/lib/trial_verdict.py" # Harbor must run with CWD = the host-visible workspace path so its nested # container mounts resolve against the host Docker daemon. HARBOR_CWD="${HOST_WORKSPACE:-$TOOLKIT_ROOT}" if [ $# -lt 1 ]; then echo "Usage: $0 " >&2 exit 1 fi TASK_PATH="$1" if [ ! -d "$TASK_PATH" ]; then echo "ERROR: task directory not found: $TASK_PATH" >&2 exit 1 fi require_frontier_harbor # Load .env if present (for API keys Harbor may need). if [ -f "$TOOLKIT_ROOT/.env" ]; then set -a source "$TOOLKIT_ROOT/.env" set +a restore_profile_policy_values fi # Namespace Harbor output for clean.sh to pattern-match it per task. # All oracle+nop runs from this invocation land under validate--/. TASK_NAME="$(basename "$TASK_PATH")" TIMESTAMP="$(date +%Y%m%d-%H%M%S)" VALIDATE_DIR="harbor-jobs/validate-${TASK_NAME}-${TIMESTAMP}" FAIL_COUNT=0 # subrun_from_capture # # Harbor prints "Results written to harbor-jobs///result.json". # Extract the full result.json path and strip the filename. Keep backticks and # terminal punctuation out of the match; older parsing accidentally included # formatting backticks and then pointed workers at bogus `...`/job.log paths. subrun_from_capture() { local capture="$1" local result_path result_path=$(awk ' match($0, /harbor-jobs\/[^[:space:]`]+\/[^[:space:]`]+\/result\.json/) { print substr($0, RSTART, RLENGTH) } ' "$capture" 2>/dev/null | tail -n 1 || true) if [ -n "$result_path" ]; then echo "${result_path%/result.json}" return 0 fi # Last-ditch fallback: if the result line was absent or reformatted, pick # the newest top-level result.json under this validate invocation. if [ -d "$TOOLKIT_ROOT/$VALIDATE_DIR" ]; then result_path=$(find "$TOOLKIT_ROOT/$VALIDATE_DIR" -mindepth 2 -maxdepth 2 -type f -name result.json 2>/dev/null | sort | tail -n 1 || true) if [ -n "$result_path" ]; then result_path="${result_path#$TOOLKIT_ROOT/}" echo "${result_path%/result.json}" return 0 fi fi echo "" } resolve_harbor_path() { local rel="$1" if [ -e "$TOOLKIT_ROOT/$rel" ]; then echo "$TOOLKIT_ROOT/$rel" elif [ -e "$HARBOR_CWD/$rel" ]; then echo "$HARBOR_CWD/$rel" else echo "$TOOLKIT_ROOT/$rel" fi } print_common_harbor_diagnostics() { local capture="$1" local job_log="$2" local combined combined=$(mktemp) cat "$capture" > "$combined" 2>/dev/null || true if [ -n "$job_log" ] && [ -f "$job_log" ]; then cat "$job_log" >> "$combined" 2>/dev/null || true fi if grep -qiE 'Docker daemon is not running|Cannot connect to the Docker daemon|docker: Cannot connect' "$combined"; then echo " Diagnosis: Docker daemon is unavailable. Start Docker Desktop / dockerd, confirm \`docker ps\` works inside the dev container, then rerun validate." # Harbor 0.22.0 prints "Skipping image OS validation" and "docker inspect # returned 1" on every run, successful ones included (it inspects each # image before building it), so neither is a sign of a build failure. elif grep -qiE 'No such image|pull access denied|failed to solve|failed to build|docker build' "$combined"; then echo " Diagnosis: Harbor could not build or inspect the task image. The first Docker/Dockerfile error is usually in job.log above." echo " Check for missing COPY sources, failed package installs, removed images, or a Docker daemon restart, then rerun validate." fi rm -f "$combined" } # print_exception_diagnosis # # One line on what the exception Harbor recorded in the trial's result.json # means for the worker, printed before the log excerpts. Nothing for a failure # without a recorded exception (a reward verdict, or the console fallback). print_exception_diagnosis() { local exception_type="$1" case "$exception_type" in ""|none) ;; RewardFileNotFoundError) echo " Diagnosis: the verifier ran but wrote no reward file. tests/test.sh exited before writing /logs/verifier/reward.txt or reward.json; its output is in the job.log excerpt below. Make tests/test.sh write the fail-closed 0 to both files first, then rerun validate." ;; *) echo " Diagnosis: Harbor raised $exception_type during the trial; the message is above and the log excerpts follow. Fix what it names, then rerun validate." ;; esac } # dump_failure_logs [] # # When oracle or nop fails, the real error (pytest assertion, Docker build # failure, ModuleNotFoundError at collection) lives under # harbor-jobs///. Parse the subrun path from Harbor output and # tail the files that usually hold the cause. The exception type comes in as # the third argument (the two exception branches pass the VERDICT_EXCEPTION_TYPE # they just read; the reward verdicts and the console fallback pass none) # rather than being read from the global, so the diagnosis printed first is # keyed on the record the caller decided by. dump_failure_logs() { local capture="$1" local role="$2" local exception_type="${3:-}" print_exception_diagnosis "$exception_type" local subrun subrun=$(subrun_from_capture "$capture") if [ -z "$subrun" ]; then echo " (could not locate Harbor subrun path in captured output — logs unavailable)" print_common_harbor_diagnostics "$capture" "" return 0 fi local subrun_path subrun_path=$(resolve_harbor_path "$subrun") local job_log_path="$subrun_path/job.log" echo "" echo " --- $subrun/job.log (last 30 lines) ---" if [ -f "$job_log_path" ]; then tail -n 30 "$job_log_path" 2>/dev/null | sed 's/^/ /' || true else echo " (job.log not found at $job_log_path)" echo " Try: harbor view $VALIDATE_DIR" fi print_common_harbor_diagnostics "$capture" "$job_log_path" local verifier_log verifier_log=$(find "$subrun_path" -type f -path '*/verifier/test-stdout.txt' 2>/dev/null | head -n 1 || true) echo "" if [ -n "$verifier_log" ] && [ -f "$verifier_log" ]; then echo " --- ${verifier_log#$TOOLKIT_ROOT/} (last 40 lines) ---" tail -n 40 "$verifier_log" 2>/dev/null | sed 's/^/ /' || true else echo " --- verifier/test-stdout.txt ---" echo " (no verifier output — likely an environment/build failure; see job.log above)" echo " If job.log shows Docker/image setup failure, fix the Dockerfile or daemon issue first; pytest never ran." fi local agent_log agent_log=$(find "$subrun_path" -type f -path "*/agent/${role}.txt" 2>/dev/null | head -n 1 || true) echo "" if [ -n "$agent_log" ] && [ -f "$agent_log" ]; then echo " --- ${agent_log#$TOOLKIT_ROOT/} (last 20 lines) ---" tail -n 20 "$agent_log" 2>/dev/null | sed 's/^/ /' || true else echo " --- agent/${role}.txt ---" echo " (no ${role} agent log found)" fi echo "" } # check_trial_output # # The runtime companion to the static checks, for a profile that ships # checks/check-verifier-output.py (both profiles do, and require it with # VERIFIER_OUTPUT_CHECK_REQUIRED=1: a zip without the file fails the static # step below with a "repack" message; a profile without the key would skip # this step when the file is absent). The static checks read the verifier's source # and can only prove what they can parse; this reads what the verifier actually # wrote in the trial the Harbor output names: reward.txt must be exactly the # reward this agent must produce (1 for the oracle, 0 for the nop), reward.json # must be present as {"reward": N} and agree when REWARD_JSON_REQUIRED=1 (or be # absent otherwise), ctrf.json must be present when CTRF_REQUIRED=1 and, when # present, be a well-formed CTRF report whose summary agrees with the reward. check_trial_output() { local capture="$1" role="$2" expected="$3" local script="$VERIFIER_OUTPUT_CHECK" local subrun args=() # A missing required script was already counted once, in the static step. [ -f "$script" ] || return 0 echo ">> check-verifier-output.py ($role trial: reward.txt, reward.json, ctrf.json)" subrun=$(subrun_from_capture "$capture") if [ -z "$subrun" ]; then echo " FAIL: could not locate the $role trial's output under $VALIDATE_DIR, so the verifier's files were not checked" FAIL_COUNT=$((FAIL_COUNT + 1)) return 0 fi args=(--expect-reward "$expected") if [ "${CTRF_REQUIRED:-0}" = "1" ]; then args+=(--require-ctrf) fi if [ "${REWARD_JSON_REQUIRED:-0}" = "1" ]; then args+=(--require-reward-json) else args+=(--no-reward-json) fi if python3 "$script" "$(resolve_harbor_path "$subrun")" "${args[@]}" | sed 's/^/ /'; then echo " OK" else echo " FAIL" FAIL_COUNT=$((FAIL_COUNT + 1)) fi } # read_trial_record # # The verdict on a trial comes from the record Harbor writes beside it, # //result.json (scripts/lib/trial_verdict.py reads the reward # Harbor scored, whether the verifier ran, and the exception the trial raised), # not from the console: Harbor prints a zero mean for a trial that produced no # reward, so the console cannot tell a nop that scored 0 from a nop whose # verifier never ran. Sets VERDICT_RC (0: the record was read; 2: no record, # the caller prints one WARN and decides from the console output as older # versions did), VERDICT_RECORD (the result.json read or looked for, relative # to the toolkit root), VERDICT_REWARD (a number or none), VERDICT_VERIFIER_RAN # (0 or 1), VERDICT_EXCEPTION_TYPE and VERDICT_EXCEPTION_MESSAGE (none when the # trial raised nothing) and VERDICT_ERROR (why there is no record). read_trial_record() { local capture="$1" local subrun lines line VERDICT_RC=2 VERDICT_RECORD="$VALIDATE_DIR///result.json" VERDICT_REWARD=none VERDICT_VERIFIER_RAN=0 VERDICT_EXCEPTION_TYPE=none VERDICT_EXCEPTION_MESSAGE=none VERDICT_ERROR="Harbor's output named no result path" subrun=$(subrun_from_capture "$capture") [ -n "$subrun" ] || return 0 lines=$(python3 "$TRIAL_VERDICT" "$(resolve_harbor_path "$subrun")" 2>/dev/null) && VERDICT_RC=0 || VERDICT_RC=$? while IFS= read -r line; do case "$line" in record=*) VERDICT_RECORD="${line#record=}" ;; reward=*) VERDICT_REWARD="${line#reward=}" ;; verifier_ran=*) VERDICT_VERIFIER_RAN="${line#verifier_ran=}" ;; exception_type=*) VERDICT_EXCEPTION_TYPE="${line#exception_type=}" ;; exception_message=*) VERDICT_EXCEPTION_MESSAGE="${line#exception_message=}" ;; error=*) VERDICT_ERROR="${line#error=}" ;; esac done <> render-readme.sh --check" if "$SCRIPT_DIR/render-readme.sh" --check "$TASK_PATH"; then echo " OK" else echo " FAIL" FAIL_COUNT=$((FAIL_COUNT + 1)) fi fi for check in "$CI_CHECKS_DIR"/check-*.sh; do check_name=$(basename "$check") echo ">> $check_name" if bash "$check" "$TASK_PATH"; then echo " OK" else echo " FAIL" FAIL_COUNT=$((FAIL_COUNT + 1)) fi done # The runtime check must be present when the profile requires it: a zip that # lacks the file would otherwise skip the check silently and validate a task # whose verifier files were never inspected. if [ "${VERIFIER_OUTPUT_CHECK_REQUIRED:-0}" = "1" ]; then echo ">> check-verifier-output.py is present (required by this profile)" if [ -f "$VERIFIER_OUTPUT_CHECK" ]; then echo " OK" else echo " FAIL: this toolkit zip is missing ${VERIFIER_OUTPUT_CHECK#"$TOOLKIT_ROOT/"}; repack it (tools/pack.sh --profile $TOOLKIT_PROFILE) or re-download the toolkit. Without it the oracle and nop trials' reward.txt, reward.json and ctrf.json are not checked." FAIL_COUNT=$((FAIL_COUNT + 1)) fi fi # The network policy each trial runs under, from task.toml, as Harbor resolves # it (scripts/lib/toml.sh, toml_network_phases): the agent phase is # [environment].network_mode (absent: Harbor's default, public); the verifier # phase is [verifier.environment].network_mode, and when that block is absent # Harbor gives the separate verifier a copy of [environment], so the line says # it inherits the agent's value. Printed so a run's log says which policy the # egress sidecar enforced, instead of leaving it to be inferred. NETWORK_PHASES="$(toml_network_phases "$TASK_PATH/task.toml")" print_network_mode() { echo " network_mode: $NETWORK_PHASES (task.toml)" } # --- Oracle must pass (reward = 1.0) --- echo "" echo "[2/3] Oracle agent run (must produce reward = 1.0)" echo "------------------------------------------" print_network_mode ORACLE_OUTPUT=$(mktemp) (cd "$HARBOR_CWD" && harbor run -p "$TASK_PATH" -a oracle --yes -o "$VALIDATE_DIR") 2>&1 | tee "$ORACLE_OUTPUT" || true # The oracle passes only when the record scores 1 and the trial raised # nothing: an exception is an infrastructure verdict, whatever reward the # record shows beside it. read_trial_record "$ORACLE_OUTPUT" if [ "$VERDICT_RC" -eq 0 ]; then if [ "$VERDICT_EXCEPTION_TYPE" != none ]; then echo " FAIL: oracle run errored (infrastructure issue, not a test-strength issue)" echo " $VERDICT_EXCEPTION_TYPE: $VERDICT_EXCEPTION_MESSAGE" dump_failure_logs "$ORACLE_OUTPUT" oracle "$VERDICT_EXCEPTION_TYPE" FAIL_COUNT=$((FAIL_COUNT + 1)) elif [ "$VERDICT_REWARD" = 1 ]; then echo " OK" check_trial_output "$ORACLE_OUTPUT" oracle 1 else echo " FAIL: oracle did not achieve reward 1.0" print_record_reward dump_failure_logs "$ORACLE_OUTPUT" oracle FAIL_COUNT=$((FAIL_COUNT + 1)) fi else warn_no_record if grep -qE "Mean: 1\.0(00)?" "$ORACLE_OUTPUT"; then echo " OK" check_trial_output "$ORACLE_OUTPUT" oracle 1 else echo " FAIL: oracle did not achieve reward 1.0" dump_failure_logs "$ORACLE_OUTPUT" oracle FAIL_COUNT=$((FAIL_COUNT + 1)) fi fi rm -f "$ORACLE_OUTPUT" # --- Nop must fail (reward = 0.0) --- echo "" echo "[3/3] Nop agent run (must produce reward = 0.0)" echo "------------------------------------------" print_network_mode NOP_OUTPUT=$(mktemp) (cd "$HARBOR_CWD" && harbor run -p "$TASK_PATH" -a nop --yes -o "$VALIDATE_DIR") 2>&1 | tee "$NOP_OUTPUT" || true # A no-op agent should never achieve reward 1.0, and a zero it did not earn # proves nothing: the nop is clean only when the record scores 0, the verifier # ran, and the trial raised nothing. A trial that raised (a build failure, an # agent or verifier timeout, a missing reward file) is an infrastructure # verdict, whatever the console's mean says. read_trial_record "$NOP_OUTPUT" if [ "$VERDICT_RC" -eq 0 ]; then if [ "$VERDICT_EXCEPTION_TYPE" != none ]; then echo " FAIL: nop run errored (infrastructure issue, not a test-strength issue)" echo " $VERDICT_EXCEPTION_TYPE: $VERDICT_EXCEPTION_MESSAGE" dump_failure_logs "$NOP_OUTPUT" nop "$VERDICT_EXCEPTION_TYPE" FAIL_COUNT=$((FAIL_COUNT + 1)) elif [ "$VERDICT_REWARD" = 0 ] && [ "$VERDICT_VERIFIER_RAN" = 1 ]; then echo " OK (nop correctly received zero reward)" check_trial_output "$NOP_OUTPUT" nop 0 elif [ "$VERDICT_REWARD" = none ]; then echo " FAIL: nop run recorded no reward (infrastructure issue, not a test-strength issue)" print_record_reward dump_failure_logs "$NOP_OUTPUT" nop FAIL_COUNT=$((FAIL_COUNT + 1)) else echo " FAIL: nop received nonzero reward (tests may be trivially passable)" print_record_reward dump_failure_logs "$NOP_OUTPUT" nop FAIL_COUNT=$((FAIL_COUNT + 1)) fi else warn_no_record # The console fallback of older versions: a RuntimeError in the output # means the run errored, a zero mean means OK, anything else a nonzero # reward. if grep -qE "RuntimeError" "$NOP_OUTPUT"; then echo " FAIL: nop run errored (infrastructure issue, not a test-strength issue)" dump_failure_logs "$NOP_OUTPUT" nop FAIL_COUNT=$((FAIL_COUNT + 1)) elif grep -qE "Mean: 0\.0(00)?" "$NOP_OUTPUT"; then echo " OK (nop correctly received zero reward)" check_trial_output "$NOP_OUTPUT" nop 0 else echo " FAIL: nop received nonzero reward (tests may be trivially passable)" dump_failure_logs "$NOP_OUTPUT" nop FAIL_COUNT=$((FAIL_COUNT + 1)) fi fi rm -f "$NOP_OUTPUT" # --- Summary --- echo "" echo "==========================================" if [ "$FAIL_COUNT" -eq 0 ]; then echo "VALIDATION PASSED" echo "==========================================" # Suppress the "Next:" hint when invoked as a sub-step of submit.sh's # tarball-verify pass — the temp task path doesn't survive submit's trap # cleanup and pointing the worker at it would mislead. if [ "${VALIDATE_DELEGATED:-}" != "1" ]; then echo "" echo "Next: scripts/check-quality.sh $TASK_PATH" fi exit 0 else echo "VALIDATION FAILED: $FAIL_COUNT issue(s)" echo "==========================================" exit 1 fi