llm-bench

"""Easy-tier shell fixes (written 2026-10-12): two straightforward (unquoted paths with spaces in a
log rotator, string comparison of disk percentages), then two subtler ones (counters set inside
`cmd | while read` are lost in the subshell, and the test's last line has no trailing newline so
reading the file directly drops it; `read` without IFS= and -r, where -r alone still loses the
indentation). Tests source ./solution.sh; everything stays bash-3.2-safe for macOS /bin/bash."""

CONTEXT = "Target: macOS /bin/bash 3.2 as well as Linux bash 5."

ITEMS = [
    {
        "id": "sh-rotate-spaces", "lang": "sh", "cat": "bugfix", "context": CONTEXT,
        "symptom": "rotate_log '/srv/my logs/app server.log' 3 prints errors such as '[: too many arguments' and 'ambiguous redirect' and leaves every file where it was; paths without spaces rotate fine.",
        "buggy": '''# rotate_log FILE KEEP: shift FILE.1 .. FILE.(KEEP-1) up by one, move FILE to FILE.1,
# then start a new empty FILE.
rotate_log() {
    local log keep i
    log=$1
    keep=$2
    i=$((keep - 1))
    while [ $i -ge 1 ]; do
        if [ -f $log.$i ]; then
            mv $log.$i $log.$((i + 1))
        fi
        i=$((i - 1))
    done
    if [ -f $log ]; then
        mv $log $log.1
    fi
    : > $log
}
''',
        "fix": '''# rotate_log FILE KEEP: shift FILE.1 .. FILE.(KEEP-1) up by one, move FILE to FILE.1,
# then start a new empty FILE.
rotate_log() {
    local log keep i
    log=$1
    keep=$2
    i=$((keep - 1))
    while [ "$i" -ge 1 ]; do
        if [ -f "$log.$i" ]; then
            mv "$log.$i" "$log.$((i + 1))"
        fi
        i=$((i - 1))
    done
    if [ -f "$log" ]; then
        mv "$log" "$log.1"
    fi
    : > "$log"
}
''',
        "test": '''. ./solution.sh
status=0
fail() { echo "FAIL: $*"; status=1; }
base=$(mktemp -d "${TMPDIR:-/tmp}/sh-rotate.XXXXXX") || exit 2
dir="$base/my logs"
mkdir "$dir"
log="$dir/app server.log"
printf 'current\\n' > "$log"
printf 'one\\n' > "$log.1"
printf 'two\\n' > "$log.2"
rotate_log "$log" 3 2>/dev/null
{ [ -f "$log" ] && [ ! -s "$log" ]; } || fail "a new empty log should exist"
[ "$(cat "$log.1" 2>/dev/null)" = current ] || fail ".1 should hold the old log"
[ "$(cat "$log.2" 2>/dev/null)" = one ] || fail ".2 should hold the old .1"
[ "$(cat "$log.3" 2>/dev/null)" = two ] || fail ".3 should hold the old .2"
printf 'newer\\n' > "$log"
rotate_log "$log" 3 2>/dev/null
[ "$(cat "$log.1" 2>/dev/null)" = newer ] || fail "second rotation: .1"
[ "$(cat "$log.2" 2>/dev/null)" = current ] || fail "second rotation: .2"
[ "$(cat "$log.3" 2>/dev/null)" = one ] || fail "second rotation: .3"
[ ! -e "$log.4" ] || fail "KEEP=3 must not create .4"
plain="$base/plain.log"
rotate_log "$plain" 2 2>/dev/null
{ [ -f "$plain" ] && [ ! -e "$plain.1" ]; } || fail "a missing log should just be created"
printf 'x\\n' > "$plain"
rotate_log "$plain" 2 2>/dev/null
[ "$(cat "$plain.1" 2>/dev/null)" = x ] || fail "plain path rotation"
rm -f "$dir"/* "$base"/*.log "$base"/*.log.*
rmdir "$dir" "$base" 2>/dev/null
exit $status
''',
    },
    {
        "id": "sh-disk-threshold", "lang": "sh", "cat": "bugfix", "context": CONTEXT,
        "symptom": "check_disk 9% 80 prints 'ALERT 9% > 80%' and returns 1, while check_disk 100% 90 prints 'ok'.",
        "buggy": r'''# check_disk USED LIMIT: USED is a df capacity such as 93%, LIMIT a plain number.
# Print "ALERT <used>% > <limit>%" and return 1 when usage is above the limit,
# otherwise print "ok" and return 0.
check_disk() {
    local used limit
    used=${1%\%}
    limit=$2
    if [[ $used > $limit ]]; then
        echo "ALERT ${used}% > ${limit}%"
        return 1
    fi
    echo "ok"
    return 0
}
''',
        "fix": r'''# check_disk USED LIMIT: USED is a df capacity such as 93%, LIMIT a plain number.
# Print "ALERT <used>% > <limit>%" and return 1 when usage is above the limit,
# otherwise print "ok" and return 0.
check_disk() {
    local used limit
    used=${1%\%}
    limit=$2
    if [ "$used" -gt "$limit" ]; then
        echo "ALERT ${used}% > ${limit}%"
        return 1
    fi
    echo "ok"
    return 0
}
''',
        "test": '''. ./solution.sh
status=0
expect() {
    local want_rc want_out out rc
    want_rc=$1
    want_out=$2
    shift 2
    out=$(check_disk "$@")
    rc=$?
    if [ "$rc" != "$want_rc" ] || [ "$out" != "$want_out" ]; then
        echo "FAIL: check_disk $*: got rc=$rc out='$out', want rc=$want_rc out='$want_out'"
        status=1
    fi
}
expect 0 "ok" 9% 80
expect 1 "ALERT 100% > 90%" 100% 90
expect 1 "ALERT 85% > 80%" 85% 80
expect 0 "ok" 80% 80
expect 0 "ok" 5% 10
expect 1 "ALERT 79% > 8%" 79% 8
expect 0 "ok" 0% 0
exit $status
''',
    },
    {
        "id": "sh-failed-summary", "lang": "sh", "cat": "bugfix", "context": CONTEXT,
        "symptom": "For a job log with two FAILED lines (the last one for db-dump), failed_summary prints 'count=0 last=' instead of 'count=2 last=db-dump'.",
        "buggy": '''# failed_summary LOGFILE: log lines look like "2026-10-12 03:05:12 FAILED db-dump".
# Print "count=N last=JOB": N is the number of FAILED lines, JOB the job name (last field)
# of the last FAILED line, empty when there are none.
failed_summary() {
    local count=0 last="" line
    grep ' FAILED ' "$1" | while read -r line; do
        count=$((count + 1))
        last=${line##* }
    done
    echo "count=$count last=$last"
}
''',
        "fix": '''# failed_summary LOGFILE: log lines look like "2026-10-12 03:05:12 FAILED db-dump".
# Print "count=N last=JOB": N is the number of FAILED lines, JOB the job name (last field)
# of the last FAILED line, empty when there are none.
failed_summary() {
    local count=0 last="" line
    while read -r line; do
        count=$((count + 1))
        last=${line##* }
    done < <(grep ' FAILED ' "$1")
    echo "count=$count last=$last"
}
''',
        "test": '''. ./solution.sh
status=0
check() {
    local got
    got=$(failed_summary "$1" 2>&1)
    if [ "$got" != "$2" ]; then
        echo "FAIL: $(basename "$1"): got '$got', want '$2'"
        status=1
    fi
}
base=$(mktemp -d "${TMPDIR:-/tmp}/sh-failed.XXXXXX") || exit 2
printf '%s\\n' '2026-10-12 03:00:01 OK nightly-backup' '2026-10-12 03:05:12 FAILED cert-renew' \\
    '2026-10-12 03:10:00 OK rsync-offsite' > "$base/a.log"
printf '%s' '2026-10-12 03:15:42 FAILED db-dump' >> "$base/a.log"
check "$base/a.log" "count=2 last=db-dump"
printf '%s\\n' '2026-10-13 03:00:01 OK nightly-backup' '2026-10-13 03:10:00 OK rsync-offsite' > "$base/b.log"
check "$base/b.log" "count=0 last="
printf '%s\\n' '2026-10-14 03:00:01 OK nightly-backup' '2026-10-14 03:02:00 FAILED zfs-scrub' \\
    '2026-10-14 03:10:00 OK rsync-offsite' > "$base/c.log"
check "$base/c.log" "count=1 last=zfs-scrub"
check "$base/a.log" "count=2 last=db-dump"
rm -f "$base/a.log" "$base/b.log" "$base/c.log"
rmdir "$base" 2>/dev/null
exit $status
''',
    },
    {
        "id": "sh-comment-out", "lang": "sh", "cat": "bugfix", "context": CONTEXT,
        "symptom": r"Commenting out an nginx block turns the line 'location ~ \.php$ {' into '#location ~ .php$ {', so removing the '#' later does not give back the original config.",
        "buggy": '''# comment_out FILE: print FILE with "#" put in front of every line and the lines otherwise
# untouched, so stripping the first character of each line gives the original back.
comment_out() {
    local line
    while read line; do
        printf '#%s\\n' "$line"
    done < "$1"
}
''',
        "fix": '''# comment_out FILE: print FILE with "#" put in front of every line and the lines otherwise
# untouched, so stripping the first character of each line gives the original back.
comment_out() {
    local line
    while IFS= read -r line; do
        printf '#%s\\n' "$line"
    done < "$1"
}
''',
        "test": r'''. ./solution.sh
status=0
base=$(mktemp -d "${TMPDIR:-/tmp}/sh-comment.XXXXXX") || exit 2
f="$base/site.conf"
printf '%s\n' 'location ~ \.php$ {' '    fastcgi_pass unix:/run/php/php-fpm.sock;' > "$f"
printf '\tinclude fastcgi_params;\n' >> "$f"
printf '%s\n' '    rewrite ^/old/(.*)$ /new/$1 permanent;' '' \
    '    return 301 https://$host$request_uri;   ' '    set $path C:\new\temp;' '}' >> "$f"
comment_out "$f" > "$base/got" 2>&1
sed 's/^/#/' "$f" > "$base/want"
if ! cmp -s "$base/got" "$base/want"; then
    echo "FAIL: output differs from the input with '#' prepended:"
    diff "$base/want" "$base/got"
    status=1
fi
printf '%s\n' 'plain line' 'another one' > "$base/simple.conf"
if [ "$(comment_out "$base/simple.conf")" != "$(printf '#plain line\n#another one')" ]; then
    echo "FAIL: simple file"
    status=1
fi
rm -f "$f" "$base/got" "$base/want" "$base/simple.conf"
rmdir "$base" 2>/dev/null
exit $status
''',
    },
]