#!/usr/bin/env bash # Shared gates of the laptop GPU runs (source it; inside WSL2): the fairness flag, the build and memory gates, the lock, the preflight and the # guard. The protocol is the owner's safety protocol of 2026-11-13; lap_stretch.sh carries an older # copy of the same logic and is to be reduced to a user of this file once no stretch is waiting on it. # # lap_want_gpu LABEL create the flag C:\...\.git\laptop-gpu-wanted.flag (only at a moment the stretch can start at once; lap_done and the lock remove it) # lap_wait_and_lock LABEL wait for: no cargo/rustc/link in WSL or Windows, Windows available RAM >= 8 GB (no builds), no lock; then take the lock # lap_preflight RESULT_DIR refuse (return 4) at a power limit < 100 W, SW cap / thermal slowdown at idle, P5, a bad turbo watchdog, WSL RAM < 10 GB # lap_done remove the lock and the flag (call from a trap) # lap_gpu_idle_check print the GPU state line LAP_REPO=${LAP_REPO:-/mnt/c/Users/pavel/work/leleka} LAP_LOCK="$LAP_REPO/.git/laptop-gpu-run.lock" LAP_FLAG="$LAP_REPO/.git/laptop-gpu-wanted.flag" LAP_AGENT=${LAP_AGENT:-laptop-3080-live} LAP_HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" lap_want_gpu() { echo "$LAP_AGENT $(date -u +%FT%TZ) $1" > "$LAP_FLAG"; } lap_builds_running() { { pgrep -x cargo || pgrep -x rustc || pgrep -x ld || pgrep -x cc1plus; } >/dev/null 2>&1 && return 0 [ "$(powershell.exe -NoProfile -Command 'Get-Process cargo,rustc,link,lld-link -ErrorAction SilentlyContinue | Measure-Object | % Count' | tr -dc '0-9')" != 0 ] } # Windows "available" physical memory (free + standby), whole GB; the WSL VM's page cache is held by vmmemWSL and does not count. lap_windows_avail_gb() { powershell.exe -NoProfile -Command 'Add-Type -AssemblyName Microsoft.VisualBasic; [math]::Floor((New-Object Microsoft.VisualBasic.Devices.ComputerInfo).AvailablePhysicalMemory/1GB)' | tr -dc '0-9' } # The engine's own start check (footprint 25.2 GiB for this profile + soft floor and reserve 5 GiB in the dedicated preset) refuses below ~30.2 GiB of VM # MemAvailable ("REFUSING TO START: memory budget refused (host_ram)"); waiting for it is cheaper than a refused `leleka up`. LAP_MIN_WSL_GB=${LAP_MIN_WSL_GB:-31} lap_wsl_avail_gb() { awk '$1=="MemAvailable:" {printf "%d", $2/1048576}' /proc/meminfo; } lap_power_limit_w() { nvidia-smi -q -d POWER | awk -F: '/Current Power Limit/ && $2 !~ /N\/A/ {gsub(/[ W]/, "", $2); printf "%d", $2; exit}'; } # Rule of the coordinator (2026-11-14): poll every 2 minutes WITHOUT the flag (it would stall the refactor agents for nothing); at a moment when no # cargo/rustc/link runs and Windows has >= LAP_MIN_WIN_GB (8) available, raise the flag, take the lock and drop the flag again at once (the lock keeps the # others out for the length of the stretch). Waits as long as it takes; the caller bounds it with `timeout` when it must report. LAP_MIN_WIN_GB=${LAP_MIN_WIN_GB:-8} lap_wait_and_lock() { while true; do if [ ! -e "$LAP_LOCK" ] && ! lap_builds_running && [ "$(lap_windows_avail_gb)" -ge "$LAP_MIN_WIN_GB" ] && [ "$(lap_wsl_avail_gb)" -ge "$LAP_MIN_WSL_GB" ]; then lap_want_gpu "$1" if (set -C; echo "$LAP_AGENT $(date -u +%FT%TZ) $1" > "$LAP_LOCK") 2>/dev/null; then rm -f "$LAP_FLAG"; return 0; fi rm -f "$LAP_FLAG" fi sleep 120 done } lap_done() { rm -f "$LAP_LOCK" "$LAP_FLAG"; } lap_preflight() { local res="$1" lim q avail wfree turbo lim=$(lap_power_limit_w); q=$(nvidia-smi -q -d PERFORMANCE,POWER) avail=$(awk '$1=="MemAvailable:" {printf "%d", $2/1048576}' /proc/meminfo); wfree=$(lap_windows_avail_gb) turbo=$(bash "$LAP_HERE/turbo_status.sh") echo "preflight: limit=${lim:-?} W $(nvidia-smi --query-gpu=pstate,temperature.gpu,clocks.sm --format=csv,noheader) wsl_avail=${avail} GB win_avail=${wfree} GB turbo=${turbo}" | tee "$res/preflight.txt" [ "${lim:-0}" -ge 100 ] || return 4 ! grep -E "SW Power Cap|SW Thermal Slowdown" <<< "$q" | grep -q ": Active" || return 4 ! grep "Performance State" <<< "$q" | grep -q P5 || return 4 [ "${turbo%% *}" != BAD ] && [ "$avail" -ge "$LAP_MIN_WSL_GB" ] && [ "${wfree:-0}" -ge "$LAP_MIN_WIN_GB" ] }