xonecas.io
79113
Ma

mini-coding-agent / files

mini-coding-agent / tools / stress.sh

4.5 KB
1#!/usr/bin/env bash
2#
3# stress.sh - drive the real TUI in a real kitty window with a real prompt
4# while capturing the screen at a fixed rate, so rendering bugs and rare
5# crashes show up on a run that can be repeated. Frames land in
6# <outdir>/frame-NNNNN.png, the final pane text in <outdir>/final.txt.
7#
8#   tools/stress.sh [-d seconds] [-r fps] [-o outdir] [-C cwd]
9#                   [-b mini] [-n name] [-p prompt]
10
11set -euo pipefail
12
13fps=15
14seconds=300
15name="stress-$$"
16here=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
17bin=$here/../zig-out/bin/mini
18cwd=$PWD
19out=
20prompt="Research this repo then create /tmp/my-review.html"
21
22usage() {
23    cat <<'EOF'
24usage: stress.sh [options]
25  -d seconds  how long to capture (default 300)
26  -r fps      capture rate (default 15)
27  -o outdir   where frames go (default /tmp/mini-stress-<stamp>)
28  -C cwd      working directory mini runs in (default $PWD)
29  -b path     mini binary (default ../zig-out/bin/mini)
30  -n name     kitty session name (default stress-$$)
31  -p prompt   what to type (default: review this repo into /tmp/my-review.html)
32EOF
33}
34
35while getopts 'd:r:o:C:b:n:p:h' opt; do
36    case $opt in
37        d) seconds=$OPTARG ;;
38        r) fps=$OPTARG ;;
39        o) out=$OPTARG ;;
40        C) cwd=$OPTARG ;;
41        b) bin=$OPTARG ;;
42        n) name=$OPTARG ;;
43        p) prompt=$OPTARG ;;
44        h) usage; exit 0 ;;
45        *) usage; exit 2 ;;
46    esac
47done
48
49if (( fps <= 0 || seconds <= 0 )); then
50    echo "stress: fps and seconds must be positive" >&2
51    exit 2
52fi
53if [[ ! -x $bin ]]; then
54    echo "stress: no executable at $bin (run zig build first)" >&2
55    exit 2
56fi
57bin=$(readlink -f -- "$bin")
58cwd=$(cd -- "$cwd" && pwd)
59out=${out:-/tmp/mini-stress-$(date +%Y%m%d-%H%M%S)}
60mkdir -p -- "$out"
61out=$(readlink -f -- "$out")
62socket=/tmp/mini-$name.sock
63
64now_us() {
65    local t=${EPOCHREALTIME/,/.}
66    echo $(( ${t%.*} * 1000000 + 10#${t#*.} ))
67}
68
69kt() { kitty @ --to "unix:$socket" "$@"; }
70pane() { kt get-text -m "id:$wid" --extent="$1" --ansi 2>/dev/null; }
71
72wid=
73cleanup() {
74    if [[ -n $wid ]]; then
75        kt close-window -m "id:$wid" >/dev/null 2>&1 || true
76    fi
77    rm -f -- "$socket"
78}
79trap cleanup EXIT
80trap 'exit 1' INT TERM
81
82# The shell stays alive after mini exits so the window holds the crash for
83# inspection, and so the exit status can be read back off the pane.
84printf -v launch 'cd %q; %q; printf "\nMINI-EXIT %%d\n" $?; exec sleep 86400' "$cwd" "$bin"
85
86rm -f -- "$socket"
87kitty --detach --listen-on "unix:$socket" \
88    -o allow_remote_control=yes -o remember_window_size=no \
89    -o initial_window_width=100c -o initial_window_height=30c \
90    --title "mini-$name" \
91    bash --norc --noprofile -c "$launch"
92
93for _ in $(seq 100); do
94    wid=$(kt ls 2>/dev/null | grep -o '"id": [0-9]*' | tail -n1 | tr -dc '0-9' || true)
95    [[ -n $wid ]] && break
96    sleep 0.1
97done
98if [[ -z $wid ]]; then
99    echo "stress: kitty never reported a window on $socket" >&2
100    exit 1
101fi
102
103# The banner lands in scrollback, not the live region, so wait on all of it.
104for _ in $(seq 300); do
105    pane all | grep -q 'mini-coding-agent' && break
106    sleep 0.1
107done
108if ! pane all | grep -q 'mini-coding-agent'; then
109    echo "stress: mini did not start; pane says:" >&2
110    pane all >&2
111    exit 1
112fi
113
114echo "stress: $out"
115echo "stress: prompt: $prompt"
116kt send-text -m "id:$wid" "$prompt"
117kt send-key -m "id:$wid" enter
118
119end_reason=
120interval=$(( 1000000 / fps ))
121start=$(now_us)
122next=$start
123checked=$start
124frames=0
125while :; do
126    now=$(now_us)
127    (( now - start >= seconds * 1000000 )) && break
128    if ! kt screenshot -m "id:$wid" "$out/frame-$(printf '%05d' "$frames").png" >/dev/null 2>&1; then
129        end_reason="window vanished after $frames frames"
130        break
131    fi
132    frames=$(( frames + 1 ))
133    now=$(now_us)
134    if (( now - checked >= 1000000 )); then
135        checked=$now
136        if pane screen | grep -q 'MINI-EXIT'; then
137            end_reason=$(pane screen | grep -o 'MINI-EXIT -\?[0-9]*' | tail -n1)
138            break
139        fi
140    fi
141    next=$(( next + interval ))
142    if (( next > now )); then
143        wait_us=$(( next - now ))
144        sleep "$(( wait_us / 1000000 )).$(printf '%06d' $(( wait_us % 1000000 )))"
145    fi
146done
147
148elapsed=$(( $(now_us) - start ))
149pane all > "$out/final.txt" || true
150pane screen > "$out/final-screen.txt" || true
151
152echo "stress: $frames frames in $(( elapsed / 1000000 ))s (target ${fps}fps, got $(awk "BEGIN { printf \"%.1f\", $frames * 1000000 / $elapsed }")fps)"
153if [[ -n $end_reason ]]; then
154    echo "stress: ended early: $end_reason"
155    [[ $end_reason == 'MINI-EXIT 0' ]] || exit 1
156fi