Skip to content

Computer use CLI for AI agents

7.1 KB
Raw Blame
#!/usr/bin/env bash
#
# Enforces the two source-shape rules no compiler checks: the per-file line cap
# and the ban on non-doc comments.
#
# Both halves are only a gate if they can run, and if they still fire once they
# do. This script therefore probes for an interpreter instead of hardcoding one
# - `python3` does not exist on a Windows developer box, and hardcoding it made
# the comment half exit without checking anything while violations reached CI -
# and it runs a self-test over its own cap rule before scanning the tree, so a
# rule that has stopped detecting anything fails the gate rather than passing it
# in silence.

set -euo pipefail

cd "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

limit=400

# The single owner of the cap rule. The tree scan and `self_test` both call it,
# so the fixture exercises the shipped rule instead of a copy that can drift
# away from it.
#
# Counting and the `@generated` exemption share one awk pass, for two reasons.
# `wc -l` counts newlines rather than lines, so a file whose final line is
# unterminated read one short and a 401-line file without a trailing newline
# slipped the cap; awk counts records, and an unterminated final line is a
# record. And reading the marker through `head | grep` hangs the verdict on a
# pipeline whose exit status depends on which side finishes first. A generated
# file reports -1, which no limit can exceed.
oversize_report() {
    local file="$1"
    local lines
    lines="$(
        awk '
            NR <= 5 && index($0, "@generated") { generated = 1 }
            END { print (generated ? -1 : NR) }
        ' "$file"
    )"
    if [ "$lines" -gt "$limit" ]; then
        printf '%s: %s lines (limit %s)\n' "$file" "$lines" "$limit"
        return 1
    fi
    return 0
}

# The lookup order is a variable so the self-test below can drive the probe
# against candidates it controls, instead of emptying PATH - which needs a real
# empty directory on disk, and so needs creating and deleting.
INTERPRETER_CANDIDATES=(python3 python py)

resolve_interpreter() {
    local candidate
    for candidate in "${INTERPRETER_CANDIDATES[@]}"; do
        if command -v "$candidate" >/dev/null 2>&1 &&
            "$candidate" -c 'import sys; raise SystemExit(0 if sys.version_info[0] >= 3 else 1)' \
                >/dev/null 2>&1; then
            printf '%s\n' "$candidate"
            return 0
        fi
    done
    return 1
}

# Finding nothing is a failure, never a skip: a comment rule that quietly stops
# running is indistinguishable from a clean tree.
require_interpreter() {
    local found
    if ! found="$(resolve_interpreter)"; then
        printf 'no working Python 3 interpreter on PATH (tried python3, python, py)\n' >&2
        return 1
    fi
    printf '%s\n' "$found"
}

rust_lines() {
    awk -v count="$1" 'BEGIN { for (i = 1; i <= count; i++) print "fn line() {}" }'
}

# Both directions are driven, because a rule that rejects everything is as
# useless as one that rejects nothing, and only one of the two announces itself.
#
# The fixtures are streams rather than files. `oversize_report` reads its
# argument in one forward awk pass and never seeks, so a process substitution
# hands it a working path with nothing written to disk, nothing to clean up and
# no cleanup trap that a later edit can leave stale. Only exit status is
# asserted, so the `/dev/fd/N` name that reaches the report text is immaterial.
self_test() {
    local failures interpreter
    failures=0

    if oversize_report <(rust_lines "$((limit + 1))") >/dev/null 2>&1; then
        printf 'self-test FAIL (missed): a %s-line file did not trip the %s-line cap\n' \
            "$((limit + 1))" "$limit" >&2
        failures=1
    fi
    if oversize_report <(
        rust_lines "$limit"
        printf 'fn unterminated_last_line() {}'
    ) >/dev/null 2>&1; then
        printf 'self-test FAIL (missed): a %s-line file with no trailing newline did not trip the cap\n' \
            "$((limit + 1))" >&2
        failures=1
    fi
    if ! oversize_report <(rust_lines "$limit") >/dev/null 2>&1; then
        printf 'self-test FAIL (false positive): a %s-line file tripped the %s-line cap\n' \
            "$limit" "$limit" >&2
        failures=1
    fi
    if ! oversize_report <(
        printf '// @generated by a tool\n'
        rust_lines "$((limit + 1))"
    ) >/dev/null 2>&1; then
        printf 'self-test FAIL (false positive): an @generated file tripped the cap it is exempt from\n' >&2
        failures=1
    fi

    # A machine with no interpreter must fail the gate, never skip it quietly:
    # that silent skip is how 27 comment violations across 7 files reached CI.
    # The probe is driven through an injected candidate list rather than an
    # emptied PATH, so no directory has to be created to hold nothing. Both
    # rejection branches are driven: a name nothing on the machine can resolve,
    # and a command that does resolve but answers the version probe with a
    # failure, which is what a Python 2 would do.
    if (
        INTERPRETER_CANDIDATES=(agent-desktop-no-such-interpreter)
        require_interpreter
    ) >/dev/null 2>&1; then
        printf 'self-test FAIL: the interpreter probe reported success with no resolvable candidate\n' >&2
        failures=1
    fi
    if (
        INTERPRETER_CANDIDATES=(false)
        require_interpreter
    ) >/dev/null 2>&1; then
        printf 'self-test FAIL: the interpreter probe accepted a command that is not a Python 3\n' >&2
        failures=1
    fi

    # The cap rule's fixtures are above; the comment rule's fixtures live in
    # scripts/check_rust_comments_test.py, and nothing ran them. The fmt lane's
    # unittest discovery is scoped to tests/e2e with a `test_*.py` pattern that
    # this filename does not match, so the scanner that enforces the no-comment
    # rule over every .rs file in the tree shipped with an unexecuted test
    # suite - a rule that could stop detecting comments with nothing to say so.
    # They run here because this is the script that runs the scanner.
    #
    # A missing interpreter is not skipped past: `require_interpreter` below
    # fails the gate outright on the same machine, so the condition cannot hide
    # a rule that never ran.
    if interpreter="$(resolve_interpreter)"; then
        if ! "$interpreter" scripts/check_rust_comments_test.py >/dev/null 2>&1; then
            printf 'self-test FAIL: the comment scanner does not satisfy its own fixtures\n' >&2
            printf '  rerun for detail: %s scripts/check_rust_comments_test.py\n' "$interpreter" >&2
            failures=1
        fi
    fi

    if [ "$failures" -ne 0 ]; then
        printf 'The Rust source-shape rules do not behave as documented.\n' >&2
        return 1
    fi
    return 0
}

failed=0

self_test || failed=1

while IFS= read -r file; do
    [ -f "$file" ] || continue
    oversize_report "$file" >&2 || failed=1
done < <(git ls-files --cached --others --exclude-standard -- '*.rs')

if interpreter="$(require_interpreter)"; then
    if ! git ls-files -z --cached --others --exclude-standard -- '*.rs' |
        "$interpreter" scripts/check_rust_comments.py; then
        failed=1
    fi
else
    failed=1
fi

if [ "$failed" -ne 0 ]; then
    exit 1
fi