Something went wrong. Try again.
Exploring the idea of generated web rendering engine.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136#!/usr/bin/env bash# Runs one model batch so that it cannot end without a ledger line: every job's batch# stage goes through here (specs/job-batch-wrapper.md).## jobs/scripts/run-batch.sh --job <name> --measure-dir <dir> -- <command...>## <command...> is the batch itself, from the job's manifest (for example# `cargo run --release -p generation --bin table_real_model_run`). Run from the worktree# root; the bench/ scripts it calls (bench/batch-lane.sh, bench/ledger.py) are read from# that worktree, as the other batch scripts read them. In order:## 1. NEV_PROVIDER (or its old name GENERA_PROVIDER) set to anything but claude-code:# exit 2, the command never runs. The metered Anthropic API is never used unattended.# 2. Not on a lane: re-runs itself there through bench/on-lane.sh --lane-only, pulling# <dir> back; exit 3 when no lane answers. The batch never runs on the development box.# 3. Loads .env under whatever the environment already sets (nothing from it is printed),# and repeats check 1 against what it loaded.# 4. Resolves the ledger directory the way bench/ledger.py ledger_dir() does# (NEV_LEDGER_DIR, else <NEV_LANE_SHARED or GENERA_LANE_SHARED>/ledger when that# directory exists); with neither, exit 4 before the command runs, since nothing has# been spent yet and the job can be resubmitted once the store is mounted.# 5. Exports NEV_LEDGER_TRACK=<name> and records the ledger file's line count.# 6. Starts the CPU sampler, runs the command with its output stamped, and stops the# sampler and appends the ledger line on every exit: a finished command, a failed one,# or the wrapper interrupted (INT, TERM), so a batch that failed halfway still leaves# the line for what it spent.# 7. Ledger file not longer than before: one line saying so, and exit 5 (a batch ran and# its cost is unrecorded), unless the command itself failed, whose status then wins.# Otherwise the command's exit status.set -uo pipefail
usage() { echo "usage: jobs/scripts/run-batch.sh --job <name> --measure-dir <dir> -- <command...>" >&2 exit 2}
ORIGINAL_ARGS=("$@")JOB=""MEASURE_DIR=""while [ $# -gt 0 ]; do case "$1" in --job) [ $# -ge 2 ] || usage; JOB="$2"; shift 2 ;; --measure-dir) [ $# -ge 2 ] || usage; MEASURE_DIR="$2"; shift 2 ;; --) shift; break ;; *) usage ;; esacdone[ -n "$JOB" ] && [ -n "$MEASURE_DIR" ] && [ $# -gt 0 ] || usagecase "$JOB" in *..*|*[!A-Za-z0-9._-]*) echo "run-batch: unusable job name: $JOB" >&2; exit 2 ;;esac
refuse_provider() { local name value for name in NEV_PROVIDER GENERA_PROVIDER; do value="${!name-}" if [ -n "${!name+x}" ] && [ "$value" != claude-code ]; then echo "run-batch: $name is set to \"$value\"; only claude-code may be set, and the metered API is never used unattended; not running the batch" >&2 exit 2 fi done}refuse_provider
WORKTREE="$PWD"BENCH_DIR="${JOBS_BENCH_DIR:-$WORKTREE/bench}"if [ ! -f "$BENCH_DIR/batch-lane.sh" ]; then echo "run-batch: no bench/batch-lane.sh under $WORKTREE; run from the worktree root" >&2 exit 2fi# shellcheck source=/dev/nullsource "$BENCH_DIR/batch-lane.sh"
batch_on_lane jobs/scripts/run-batch.sh --pull "${MEASURE_DIR%/}/" -- "${ORIGINAL_ARGS[@]}"batch_load_envrefuse_provider
ledger_dir() { if [ -n "${NEV_LEDGER_DIR:-}" ]; then printf '%s' "$NEV_LEDGER_DIR" return 0 fi local shared="${NEV_LANE_SHARED:-${GENERA_LANE_SHARED:-}}" if [ -n "$shared" ] && [ -d "$shared" ]; then printf '%s/ledger' "$shared" fi}LEDGER_DIR="$(ledger_dir)"if [ -z "$LEDGER_DIR" ]; then echo "run-batch: no ledger directory (NEV_LEDGER_DIR, or NEV_LANE_SHARED naming a directory); not running the batch" >&2 exit 4fiexport NEV_LEDGER_TRACK="$JOB"LEDGER_FILE="$LEDGER_DIR/$JOB.jsonl"ledger_lines() { if [ -f "$LEDGER_FILE" ]; then wc -l < "$LEDGER_FILE" | tr -d ' '; else echo 0; fi}LINES_BEFORE="$(ledger_lines)"
# The command runs in its own process group (job control on), so an interrupt can stop# the whole pipeline and still leave this shell to write the ledger line.set -mCHILD=""FINISHED=0finish() { local status="$1" [ "$FINISHED" = 1 ] && return FINISHED=1 batch_measure_stop if [ "$(ledger_lines)" -le "$LINES_BEFORE" ]; then echo "run-batch: no ledger line was written for $JOB in $LEDGER_FILE" >&2 if [ "$status" -eq 0 ]; then status=5; fi fi exit "$status"}interrupted() { local signal="$1" code="$2" echo "run-batch: interrupted by $signal; stopping the batch and recording what it spent" >&2 if [ -n "$CHILD" ]; then kill -TERM -- "-$CHILD" 2>/dev/null || kill -TERM "$CHILD" 2>/dev/null || true wait "$CHILD" 2>/dev/null || true fi finish "$code"}trap 'interrupted INT 130' INTtrap 'interrupted TERM 143' TERM
batch_measure_start "$MEASURE_DIR"batch_phase batch( "$@" 2>&1 | batch_stamp; exit "${PIPESTATUS[0]}" ) &CHILD=$!wait "$CHILD"STATUS=$?CHILD=""finish "$STATUS"