Skip to content

Commit cf1cee9

Browse files
authored
Merge pull request #582 from OpenCoven/feat/scripts-agent-tools
feat(scripts): agent tools for PR readiness, local gates and Current state
2 parents 4facdb3 + 7f3af11 commit cf1cee9

9 files changed

Lines changed: 2138 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Agent tools tests
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- 'scripts/agent/**'
7+
- '.github/workflows/agent-tools-tests.yml'
8+
# test_gates.py checks gates.sh against the cargo commands in Rust CI.
9+
- '.github/workflows/rust-ci.yml'
10+
workflow_dispatch:
11+
12+
concurrency:
13+
group: agent-tools-tests-${{ github.ref }}
14+
cancel-in-progress: true
15+
16+
permissions:
17+
contents: read
18+
19+
jobs:
20+
agent-tools:
21+
name: Agent tools offline tests
22+
runs-on: ubuntu-latest
23+
timeout-minutes: 5
24+
steps:
25+
- uses: actions/checkout@v5
26+
with:
27+
persist-credentials: false
28+
# The oldest Python the tools support.
29+
- uses: actions/setup-python@v6
30+
with:
31+
python-version: '3.10'
32+
- name: Require shellcheck
33+
run: shellcheck --version
34+
- name: Test pr-ready, replace-state and gates without GitHub or cargo
35+
env:
36+
REQUIRE_SHELLCHECK: '1'
37+
run: python3 -B -m unittest discover -s scripts/agent -p 'test_*.py' -v

‎AGENTS.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ For parallel-agent or batch work:
102102
- Each worker prompt must include the base branch/commit, assigned files or ownership boundary, non-goals, verification recipe, and PR body requirements.
103103
- Each worker final response must include the branch, PR URL, summary, verification evidence, and known gaps. Use a bare `PR: <url>` line when an orchestrator needs to parse results.
104104

105+
## Agent Tools
106+
107+
`scripts/agent/` holds the tools parallel agent runs share. None of them merges, pushes or edits anything unless told to. They need Python 3.10 or later and `gh` 2.48 or later.
108+
109+
- `scripts/agent/pr-ready.sh <pr> [--repo OWNER/NAME] [--min-checks N] [--require-workflow NAME]...`: REST only (`gh api`). Prints the head SHA, whether the PR's base branch on GitHub (`main`) is an ancestor of it, `mergeable` and `mergeable_state` (retried for about two minutes while unknown), the latest run for each check name (keyed by workflow), the latest run of each workflow (so a workflow whose jobs have not started still counts) and the latest commit status per context, then `READY` or `NOT READY: <reasons>`. A head that moves during the check is NOT READY. Exits 0 only when READY. READY means mergeable with every check green, not approved: `main` has no required reviews. A head with no check runs is NOT READY unless `--min-checks 0`. READY covers only the runs that exist: right after a push GitHub may not have created them all, so pass `--require-workflow` for each workflow the change must run (for example `"Rust CI"` and `"TUI Tests"` for `src-rust/`). A dispatched run is kept apart from the PR's own run, and runs for another PR on the same head are left out (a run GitHub lists under several PRs is NOT READY, to be checked by hand).
110+
- `scripts/agent/gates.sh [--dry-run] <worktree> <outdir>`: the Rust CI gates run locally (fmt, check, both clippy runs, workspace tests, the `next_frame_perf` allocation gate; the computer-use clippy run only on macOS, as in CI), one log per gate, `<outdir>/summary.txt` with each exit code and the test counts, and `identity.txt` with the head and changes tested. Exit 0 means every gate that ran passed on an unchanged worktree (the `result` line names any gate skipped, such as computer-use off macOS or `GATES_ONLY_TESTS=1`); 1 is a failed gate, 2 a usage error (including a non-empty `<outdir>` that is not an earlier gates outdir), and 3 a pass during which the worktree changed. The toolchain defaults to the pin in `rust-ci.yml` (`COVEN_TOOLCHAIN` overrides it) and `CARGO_TARGET_DIR` to the worktree's own `src-rust/target`. It clears variables CI does not set (`RUSTFLAGS`, `RUSTC_BOOTSTRAP`, `CARGO_PROFILE_*`, `COVEN_*`, provider keys and similar), so these gates mean what they mean in CI; settings in `$CARGO_HOME/config.toml` or a `.cargo/config.toml` above the worktree still apply. It does not run the other Rust CI jobs (headless schemas, installer checksums, the macOS, Windows and Coven contract jobs); hosted CI does. Set `CARGO_BUILD_JOBS` and check the load before a real run; `--dry-run` only prints the commands.
111+
- `scripts/agent/replace-state.py <issue> <section-file> [--apply] [--insert] [--repo R] [--out-dir D]`: rewrites an issue's `## Current state: YYYY-MM-DD` section, and refuses whenever the section's end is not certain rather than risk dropping original text. The old section and the section file must both be the heading (after a blank line, with no code fence or HTML anywhere before it), plain lines (paragraphs, lists, tables), and a single-line `**Next action:**` paragraph with a blank line before it and a blank line or the end after it. A line starting a heading, rule or code fence (also inside a quote or list item), HTML anywhere in a line, `]:` anywhere (a link or footnote definition, even in a quote or list), a footnote reference `[^`, `$$`, a near-miss marker such as `**Next Action:**`, `**Next actions:**` or `Next action:`, a lone carriage return, or indented text right after the section is refused. An old section with no marker at all runs on to the first exact `**Next action:**` line below it, so read the preview diff before `--apply`. Text outside the section is kept as it was, apart from CRLF becoming LF, a leading byte-order mark being dropped, and extra blank lines right after the section collapsing to one. By default it previews: a diff, plus timestamped `<issue>.<time>.before.md` and `.after.md` copies in `--out-dir` (default: the current directory, refused inside a git work tree). Only `--apply` writes the body, with `gh api -X PATCH`; it re-reads the body just before writing and refuses if it changed. `--insert` adds the section to a body that has none.
112+
- Tests: `python3 -B -m unittest discover -s scripts/agent -p 'test_*.py' -v`, offline, with a fake `gh` and a fake `rustup`. The Agent tools tests workflow runs them for changes under `scripts/agent/`, to itself, or to `rust-ci.yml`.
113+
105114
## Providers
106115

107116
Coven Code supports exactly two providers: **Claude** (Anthropic) and **Codex**

‎scripts/agent/gates.sh‎

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
#!/usr/bin/env bash
2+
# usage: scripts/agent/gates.sh [--dry-run] <worktree> <outdir>
3+
#
4+
# Runs the local gates that Rust CI runs, from <worktree>/src-rust:
5+
# fmt cargo fmt --all -- --check
6+
# check cargo check --workspace --locked
7+
# clippy cargo clippy --workspace --all-targets --locked -- -D warnings
8+
# clippy-computer-use the same for claurst-tools with --features computer-use
9+
# (macOS only, as in CI; its dependencies need extra
10+
# libraries elsewhere)
11+
# test cargo test --workspace --locked --no-fail-fast
12+
# alloc the next_frame_perf allocation gate, serial
13+
# Each gate's output goes to <outdir>/<gate>.log. <outdir>/summary.txt gets one
14+
# line per gate ("<gate> rc=<n> <seconds>s", or "<gate> skipped") and the
15+
# workspace test counts;
16+
# <outdir>/identity.txt records the head, tree, a fingerprint of the changes
17+
# and untracked files, and the dirty files tested.
18+
# --dry-run prints the commands and writes nothing.
19+
# <outdir> must be new, empty, or an earlier gates outdir (one holding
20+
# identity.txt), since a run replaces the logs it names.
21+
#
22+
# Environment (default):
23+
# COVEN_TOOLCHAIN toolchain for `rustup run` (the dtolnay/rust-toolchain pin
24+
# in <worktree>/.github/workflows/rust-ci.yml)
25+
# CARGO_TARGET_DIR build directory (<worktree>/src-rust/target). Never share
26+
# one between worktrees.
27+
# CARGO_BUILD_JOBS passed to cargo when set; set it on a shared host
28+
# RUSTUP_HOME, CARGO_HOME ($HOME/.rustup, $HOME/.cargo) kept for the test
29+
# gates, which run with a scratch HOME under <outdir>
30+
# RUSTUP rustup binary (rustup on PATH, else $CARGO_HOME/bin/rustup)
31+
# GATES_ONLY_TESTS=1 run only the test and alloc gates; the result line
32+
# then names the gates skipped
33+
# GATES_COMPUTER_USE 1 or 0 to force the computer-use clippy gate on or off
34+
# (on when uname -s is Darwin)
35+
#
36+
# Not run here: the headless-schemas job (release version preflight and the
37+
# headless JSON Schema fixtures), the installer-checksums jobs, the macOS
38+
# job's computer_use::tests and workspace_policy steps, the Windows job, the
39+
# contract fixtures by name, and the Coven engine contract. Hosted CI runs
40+
# those, so a local pass covers only the gates listed above.
41+
#
42+
# Exit status: 0 when every gate passed, 1 when any failed, 2 on usage errors,
43+
# 3 when the gates passed but the worktree's head or dirty files changed
44+
# during the run, so the pass does not describe one tree.
45+
set -u
46+
47+
usage() { echo "usage: $0 [--dry-run] <worktree> <outdir>" >&2; exit 2; }
48+
49+
DRY_RUN=0
50+
if [ "${1:-}" = "--dry-run" ]; then DRY_RUN=1; shift; fi
51+
[ $# -eq 2 ] || usage
52+
for arg in "$1" "$2"; do case "$arg" in -*) usage ;; esac; done
53+
54+
WORKTREE="$(cd "$1" 2>/dev/null && pwd -P)" || { echo "gates: no such worktree: $1" >&2; exit 2; }
55+
[ -d "$WORKTREE/src-rust" ] || { echo "gates: $WORKTREE has no src-rust directory" >&2; exit 2; }
56+
case "$2" in /*) OUT="$2" ;; *) OUT="$PWD/$2" ;; esac
57+
# Resolve symlinks and .. in the existing part of the path.
58+
OUT_PARENT="$(dirname "$OUT")"
59+
if [ -d "$OUT_PARENT" ]; then
60+
OUT="$(cd "$OUT_PARENT" && pwd -P)/$(basename "$OUT")"
61+
else
62+
case "/$OUT/" in */../*|*/./*) echo "gates: give <outdir> without . or .. when its parent does not exist" >&2; exit 2 ;; esac
63+
fi
64+
# Inside the worktree the logs would show up in identity.txt's git status.
65+
case "$OUT/" in "$WORKTREE"/*) echo "gates: put <outdir> outside the worktree" >&2; exit 2 ;; esac
66+
# A run removes and rewrites the logs it names; never do that to an unrelated
67+
# directory that happens to hold a test.log.
68+
if [ -e "$OUT" ] && [ ! -e "$OUT/identity.txt" ] && [ -n "$(ls -A "$OUT" 2>/dev/null || echo x)" ]; then
69+
echo "gates: <outdir> exists and is not empty or an earlier gates outdir; give a new one" >&2
70+
exit 2
71+
fi
72+
73+
TOOLCHAIN="${COVEN_TOOLCHAIN:-}"
74+
if [ -z "$TOOLCHAIN" ]; then
75+
TOOLCHAIN="$(grep -m1 -oE 'dtolnay/rust-toolchain@[0-9][0-9.]*' \
76+
"$WORKTREE/.github/workflows/rust-ci.yml" 2>/dev/null | cut -d@ -f2)"
77+
fi
78+
[ -n "$TOOLCHAIN" ] || { echo "gates: no toolchain pin found in rust-ci.yml; set COVEN_TOOLCHAIN" >&2; exit 2; }
79+
80+
export RUSTUP_HOME="${RUSTUP_HOME:-$HOME/.rustup}"
81+
export CARGO_HOME="${CARGO_HOME:-$HOME/.cargo}"
82+
export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$WORKTREE/src-rust/target}"
83+
# CI sets none of these. A caller's RUSTFLAGS=--cap-lints=allow or
84+
# CARGO_PROFILE_TEST_DEBUG_ASSERTIONS=false would let a gate pass that fails
85+
# in CI, and a Coven agent shell's COVEN_HOME would point the tests at a real
86+
# home instead of the scratch one. A CARGO_TARGET_<triple>_RUNNER of `true`
87+
# would skip every test, and XDG_* would send test caches to the caller's real
88+
# directories on Linux. RUSTC_BOOTSTRAP=1 would let #![feature] code build on
89+
# the stable pin, and a workspace wrapper could rewrite every rustc call.
90+
# Settings in $CARGO_HOME/config.toml, and in a .cargo/config.toml in any
91+
# directory above the worktree, still apply. (COVEN_TOOLCHAIN was read above.)
92+
unset RUSTFLAGS CARGO_ENCODED_RUSTFLAGS RUSTDOCFLAGS CARGO_ENCODED_RUSTDOCFLAGS \
93+
CARGO_BUILD_RUSTFLAGS CARGO_BUILD_RUSTDOCFLAGS CARGO_BUILD_TARGET RUSTC CLIPPY_CONF_DIR \
94+
RUSTC_BOOTSTRAP RUSTC_WORKSPACE_WRAPPER CARGO_BUILD_RUSTC_WORKSPACE_WRAPPER
95+
for var in $(compgen -e); do
96+
case "$var" in
97+
CARGO_TARGET_*_RUSTFLAGS | CARGO_TARGET_*_RUNNER | CARGO_PROFILE_* | XDG_* | \
98+
COVEN_* | CLAURST_* | ANTHROPIC_* | OPENAI_* | CODEX_*)
99+
unset "$var" ;;
100+
esac
101+
done
102+
# Colour codes would break the test-count summary.
103+
export CARGO_TERM_COLOR=never
104+
COMPUTER_USE="${GATES_COMPUTER_USE:-}"
105+
case "$COMPUTER_USE" in ""|0|1) ;; *) echo "gates: GATES_COMPUTER_USE must be 0 or 1" >&2; exit 2 ;; esac
106+
if [ -z "$COMPUTER_USE" ]; then
107+
if [ "$(uname -s)" = Darwin ]; then COMPUTER_USE=1; else COMPUTER_USE=0; fi
108+
fi
109+
RUSTUP="${RUSTUP:-$(command -v rustup || echo "$CARGO_HOME/bin/rustup")}"
110+
CARGO=("$RUSTUP" run "$TOOLCHAIN" cargo)
111+
112+
# The head, the tracked changes and the untracked files' contents, so an edit
113+
# to an already dirty file during the run is noticed too. Writes no objects.
114+
fingerprint() {
115+
{
116+
git -C "$WORKTREE" rev-parse HEAD
117+
git -C "$WORKTREE" diff HEAD --binary --no-ext-diff --no-textconv
118+
# One file at a time: a path git cannot hash (a nested repository shows
119+
# up as "dir/") must not hide the files listed after it.
120+
git -C "$WORKTREE" ls-files -z -o --exclude-standard | while IFS= read -r -d '' path; do
121+
printf '%s ' "$path"
122+
git -C "$WORKTREE" hash-object -- "$path" 2>/dev/null || echo unhashable
123+
done
124+
} 2>&1 | cksum
125+
}
126+
127+
if [ "$DRY_RUN" = 1 ]; then
128+
SCRATCH_HOME="$OUT/home.XXXXXX"
129+
else
130+
# Without git the change check below would compare two identical errors.
131+
git -C "$WORKTREE" rev-parse --verify -q HEAD >/dev/null || {
132+
echo "gates: $WORKTREE is not a git worktree with a commit" >&2
133+
exit 2
134+
}
135+
"$RUSTUP" run "$TOOLCHAIN" cargo --version >/dev/null 2>&1 || {
136+
echo "gates: '$RUSTUP run $TOOLCHAIN cargo' does not run; install the toolchain or set RUSTUP" >&2
137+
exit 2
138+
}
139+
mkdir -p "$OUT" || exit 2
140+
SCRATCH_HOME="$(mktemp -d "$OUT/home.XXXXXX")" || exit 2
141+
trap 'rm -rf "$SCRATCH_HOME"' EXIT
142+
# Logs from an earlier run into the same outdir would look current.
143+
rm -f "$OUT"/fmt.log "$OUT"/check.log "$OUT"/clippy.log "$OUT"/clippy-computer-use.log \
144+
"$OUT"/test.log "$OUT"/alloc.log
145+
: >"$OUT/summary.txt"
146+
{
147+
echo "head $(git -C "$WORKTREE" rev-parse HEAD) tree $(git -C "$WORKTREE" rev-parse 'HEAD^{tree}')"
148+
echo "toolchain $TOOLCHAIN target $CARGO_TARGET_DIR jobs ${CARGO_BUILD_JOBS:-default}"
149+
IDENTITY="$(fingerprint)"
150+
echo "fingerprint $IDENTITY"
151+
git -C "$WORKTREE" status --porcelain --untracked-files=all
152+
} >"$OUT/identity.txt"
153+
fi
154+
TEST_ENV=(env HOME="$SCRATCH_HOME")
155+
156+
cd "$WORKTREE/src-rust" || exit 2
157+
FAILED=0
158+
run() {
159+
local name="$1" start rc
160+
shift
161+
if [ "$DRY_RUN" = 1 ]; then
162+
printf '%s:' "$name"
163+
printf ' %q' "$@"
164+
printf '\n'
165+
return
166+
fi
167+
start=$(date +%s)
168+
"$@" >"$OUT/$name.log" 2>&1
169+
rc=$?
170+
[ "$rc" -eq 0 ] || FAILED=1
171+
echo "$name rc=$rc $(($(date +%s) - start))s" | tee -a "$OUT/summary.txt"
172+
}
173+
174+
if [ "$DRY_RUN" = 1 ]; then
175+
echo "cd $(printf '%q' "$PWD")"
176+
echo "toolchain=$TOOLCHAIN CARGO_TARGET_DIR=$CARGO_TARGET_DIR CARGO_BUILD_JOBS=${CARGO_BUILD_JOBS:-}"
177+
fi
178+
SKIPPED=""
179+
if [ "${GATES_ONLY_TESTS:-}" = 1 ]; then
180+
SKIPPED="fmt check clippy clippy-computer-use"
181+
for gate in fmt check clippy clippy-computer-use; do
182+
if [ "$DRY_RUN" = 1 ]; then
183+
echo "$gate: skipped (GATES_ONLY_TESTS=1)"
184+
else
185+
echo "$gate skipped (GATES_ONLY_TESTS=1)" | tee -a "$OUT/summary.txt"
186+
fi
187+
done
188+
else
189+
run fmt "${CARGO[@]}" fmt --all -- --check
190+
run check "${CARGO[@]}" check --workspace --locked
191+
run clippy "${CARGO[@]}" clippy --workspace --all-targets --locked -- -D warnings
192+
if [ "$COMPUTER_USE" = 1 ]; then
193+
run clippy-computer-use "${CARGO[@]}" clippy --package claurst-tools --all-targets \
194+
--features computer-use --locked -- -D warnings
195+
elif [ "$DRY_RUN" = 1 ]; then
196+
echo "clippy-computer-use: skipped (not macOS; set GATES_COMPUTER_USE=1 to run it)"
197+
else
198+
echo "clippy-computer-use skipped" | tee -a "$OUT/summary.txt"
199+
SKIPPED="clippy-computer-use"
200+
fi
201+
fi
202+
run test "${TEST_ENV[@]}" "${CARGO[@]}" test --workspace --locked --no-fail-fast
203+
run alloc "${TEST_ENV[@]}" "${CARGO[@]}" test -p claurst-tui --locked --test next_frame_perf \
204+
-- --ignored --skip measure_active_frames --test-threads=1
205+
[ "$DRY_RUN" = 1 ] && exit 0
206+
207+
# A workspace test gate that passes without running a single test has not
208+
# tested anything (a cargo runner or config override); count it as failed.
209+
python3 -I - "$OUT/test.log" >>"$OUT/summary.txt" <<'PY' || FAILED=1
210+
import re
211+
import sys
212+
213+
passed = failed = ignored = 0
214+
failures = []
215+
with open(sys.argv[1], errors="replace") as log:
216+
for line in log:
217+
m = re.search(r"test result: \w+\. (\d+) passed; (\d+) failed; (\d+) ignored;", line)
218+
if m:
219+
passed += int(m[1])
220+
failed += int(m[2])
221+
ignored += int(m[3])
222+
if re.match(r"^test .* FAILED$", line.strip()):
223+
failures.append(line.strip())
224+
print(f"tests passed={passed} failed={failed} ignored={ignored}")
225+
for failure in failures:
226+
print(" ", failure)
227+
if passed == 0:
228+
print("tests: no test passed, so the test gate is counted as failed")
229+
sys.exit(1)
230+
PY
231+
if [ "$FAILED" != 0 ]; then
232+
echo "result FAIL" | tee -a "$OUT/summary.txt"
233+
exit 1
234+
fi
235+
if [ "$(fingerprint)" != "$IDENTITY" ]; then
236+
echo "result CHANGED (the worktree's head or files changed during the run)" | tee -a "$OUT/summary.txt"
237+
exit 3
238+
fi
239+
echo "result pass${SKIPPED:+ (skipped: $SKIPPED)}" | tee -a "$OUT/summary.txt"
240+
exit 0

‎scripts/agent/pr-ready.sh‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
# usage: scripts/agent/pr-ready.sh <pr> [--repo OWNER/NAME] [--min-checks N] [--require-workflow NAME]...
3+
#
4+
# Prints a pull request's head SHA, whether its base branch on GitHub is an
5+
# ancestor of it, mergeable and mergeable_state (retried for about two
6+
# minutes while GitHub reports them as unknown), the latest run for each check
7+
# name, the latest run of each workflow and the latest commit status per
8+
# context, then READY or NOT READY with the reasons. REST only (`gh api`); it never merges,
9+
# pushes or edits anything. Exit 0 only when READY. See pr_ready.py.
10+
set -u
11+
exec python3 -I "$(dirname "${BASH_SOURCE[0]}")/pr_ready.py" "$@"

0 commit comments

Comments
 (0)