📦 EqualifyEverything / benchmarks-ai-alt

📄 run.sh · 362 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362#!/usr/bin/env bash
# Run the corpus construction loop: seek, then adversarially review, repeat
# until the acceptance criteria in directives/00-corpus-goals.md are met.
#
#   ./run.sh                 run until the goals are met or the cap is hit
#   ./run.sh --max-rounds 3  stop after three rounds regardless
#   ./run.sh --status        report progress and exit, running no agents
#   ./run.sh --selftest      exercise the loop with a stub agent, no API calls
#
# The loop is deliberately dumb. Every judgment lives in the directives, and
# every stop condition lives in tools/validate.mjs. This script only sequences
# them and stops at the right time.
#
# Environment:
#   AGENT_CMD     command used to run one directive. Default: claude
#   AGENT_FLAGS   flags passed to it. Default: -p --permission-mode acceptEdits
#   MAX_ROUNDS    same as --max-rounds. Default: 10
#
# The agent needs web access to do its job. If your permission settings do not
# already allow it, name the tools in AGENT_FLAGS, for example:
#   AGENT_FLAGS='-p --permission-mode acceptEdits --allowedTools WebSearch WebFetch Read Write Edit'
#
# Exit codes: 0 goals met, 1 cap reached with goals unmet, 2 a step failed,
# 3 bad usage or self-test failure.

set -u

PROJECT="$(cd "$(dirname "$0")" && pwd)"
CORPUS="$PROJECT/corpus/functional-images.jsonl"
ROUNDS="$PROJECT/rounds"
VALIDATE="$PROJECT/tools/validate.mjs"

AGENT_CMD="${AGENT_CMD:-claude}"
AGENT_FLAGS="${AGENT_FLAGS--p --permission-mode acceptEdits}"
MAX_ROUNDS="${MAX_ROUNDS:-10}"
MODE=loop

while [ $# -gt 0 ]; do
  case "$1" in
    --max-rounds) MAX_ROUNDS="${2:-}"; shift; shift ;;
    --status) MODE=status; shift ;;
    --selftest) MODE=selftest; shift ;;
    --next-round) MODE=next-round; shift ;;
    -h|--help) sed -n '2,26p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
    *) echo "run.sh: unknown argument \"$1\"" >&2; exit 3 ;;
  esac
done

case "$MAX_ROUNDS" in
  ''|*[!0-9]*|0) echo "run.sh: --max-rounds needs a whole number of 1 or more" >&2
    exit 3 ;;
esac

say() { printf '%s\n' "$*"; }
rule() { say "------------------------------------------------------------"; }

# Progress report. The exit code carries the meaning, so callers read it:
# 0 goals met, 1 not yet, 2 schema errors or no corpus file.
check() {
  node "$VALIDATE" --corpus "$CORPUS" --rounds "$ROUNDS"
}

# Next round number: one past the highest round already reviewed.
next_round() {
  highest=0
  for name in "$ROUNDS"/round-*-review.jsonl; do
    [ -e "$name" ] || continue
    base="$(basename "$name")"
    base="${base#round-}"
    base="${base%-review.jsonl}"
    base="$(printf '%s' "$base" | sed 's/^0*//')"
    [ -n "$base" ] || base=0
    if [ "$base" -gt "$highest" ]; then highest="$base"; fi
  done
  echo $((highest + 1))
}

# Run one directive as one agent turn. The directive file is the instruction;
# we only say which round it is and hand over the current status.
run_directive() {
  directive="$1"; round="$2"; role="$3"; status_text="$4"
  prompt="You are running round ${round} of the corpus construction loop for
the AI alt text benchmark, in the role of the ${role}.

Read ${PROJECT}/directives/${directive} and follow it exactly, including every
file it tells you to read first and every file it tells you to write. Use the
zero-padded round number ${round} in any file name that calls for it.

Current corpus status from tools/validate.mjs:

${status_text}

Do the work now. Do not ask for confirmation, and do not stop to summarise
before you have written your output files."

  # AGENT_FLAGS is intentionally word-split.
  # shellcheck disable=SC2086
  ( cd "$PROJECT" && $AGENT_CMD $AGENT_FLAGS "$prompt" )
}

case "$MODE" in
  status) check; exit $? ;;
  next-round) next_round; exit 0 ;;
esac

# --- self-test ------------------------------------------------------------
# Proves the loop sequences its steps, stops on the real signal, respects the
# cap, and fails loudly. Runs a stub agent in a scratch copy. No network.

if [ "$MODE" = selftest ]; then
  node "$VALIDATE" --selftest || exit 3
  rule

  tmp="$(mktemp -d)"
  trap 'rm -rf "$tmp"' EXIT
  proj="$tmp/project"
  mkdir -p "$proj/corpus" "$proj/rounds" "$proj/tools" "$proj/directives" "$tmp/bin"
  cp "$VALIDATE" "$proj/tools/"
  cp -R "$PROJECT/tools/fixtures" "$proj/tools/"
  cp "$0" "$proj/run.sh"
  cp "$PROJECT"/directives/*.md "$proj/directives/"

  # Stub agent. Writes the artefacts a real round would write, so the loop's
  # sequencing and stop condition are exercised without any model call. Its
  # reviews are unconditional accepts, and it reports a blocking finding in
  # round 1 only, so two quiet rounds cannot arrive before round 3.
  cat > "$tmp/bin/stub-agent" <<'STUB'
#!/usr/bin/env bash
set -u
for a in "$@"; do prompt="$a"; done
round="$(printf '%s' "$prompt" | sed -n 's/.*running round \([0-9][0-9]*\) .*/\1/p' | head -1)"
[ -n "$round" ] || round=1
nn="$(printf '%02d' "$round")"
case "$prompt" in
  *"seeking agent"*)
    printf 'stub seek round %s\n' "$round" > "rounds/round-${nn}-seek.md"
    ;;
  *"adversarial reviewer"*)
    {
      printf 'stub review round %s\n\n' "$round"
      if [ "$round" -le 1 ]; then
        printf 'STATUS: new-blocking-findings=yes\n'
      else
        printf 'STATUS: new-blocking-findings=no\n'
      fi
    } > "rounds/round-${nn}-report.md"
    node -e '
      const fs = require("fs")
      const [file, round] = process.argv.slice(1)
      const out = []
      if (fs.existsSync(file)) {
        for (const line of fs.readFileSync(file, "utf8").split("\n")) {
          if (line.trim() === "") continue
          const item = JSON.parse(line)
          if (item.status !== "accepted") continue
          out.push(JSON.stringify({
            item_id: item.id, round: Number(round), verdict: "accept",
            reason_codes: ["CLEAN"],
            evidence: "Stub reviewer accepted this item without checking anything.",
            required_change: null, blocking: false,
          }))
        }
      }
      process.stdout.write(out.length ? out.join("\n") + "\n" : "")
    ' corpus/functional-images.jsonl "$round" > "rounds/round-${nn}-review.jsonl"
    ;;
  *) echo "stub-agent: unrecognised role in prompt" >&2; exit 1 ;;
esac
STUB
  chmod +x "$tmp/bin/stub-agent"

  # A synthetic corpus that satisfies every coverage criterion, so the loop's
  # stop condition can be tested. Scratch data in a temporary directory, never
  # written into the repository.
  cat > "$tmp/gen.mjs" <<'GEN'
const subtypes = [
  ['linked-standalone-logo', 1, 40],
  ['standalone-navigational-link', 1, 40],
  ['form-control-or-image-button', 2, 40],
  ['action-or-toggle-icon', 2, 40],
  ['functional-non-unicode-emoji', 3, 40],
  ['linked-complex-graphic-or-image-map', 4, 40],
  ['structural-break-or-reader-control', 5, 40],
]
const lines = []
let n = 0
for (const [subtype, category, count] of subtypes) {
  for (let i = 0; i < count; i++) {
    n++
    const domain = `d${n % 40}.example.com`
    const empty = n % 5 === 0
    const alt = empty ? '' : `Synthetic action ${n}`
    lines.push(JSON.stringify({
      id: `fi-${String(n).padStart(4, '0')}`,
      status: 'accepted', round_added: 1, category, subtype,
      page_url: `https://${domain}/page/${n}`, domain,
      image_url: `https://${domain}/i/${n}.svg`, implementation: 'img',
      element_role: 'button',
      element_html: `<button><img src="/i/${n}.svg" alt="x"></button>`,
      surrounding_text: empty ? `Synthetic action ${n}` : '',
      destination: `Synthetic destination ${n}`,
      observed_alt: 'x', observed_alt_verdict: 'wrong',
      gold_alt: alt,
      gold_alt_rationale: 'Synthetic fixture rationale long enough to satisfy the minimum length rule.',
      gold_alt_passes: [
        { author: 'pass-a', alt, rationale: 'synthetic' },
        { author: 'pass-b', alt, rationale: 'synthetic' },
      ],
      adjudication: null,
      difficulty: n % 4 === 0 ? 'ambiguous' : 'standard',
      dual_purpose: n % 7 === 0,
      leakage_check: 'Synthetic fixture, leakage not applicable here.',
      leaky: false, retrieved: '2026-08-27',
      provenance_note: 'Synthetic self-test fixture, not corpus data.',
    }))
  }
}
process.stdout.write(lines.join('\n') + '\n')
GEN

  fails=0
  pass() { say "PASS $1"; }
  fail() { say "FAIL $1"; fails=$((fails + 1)); }

  # 1. No corpus file yet: report, do not crash.
  out="$(cd "$proj" && ./run.sh --status 2>&1)"; rc=$?
  if [ "$rc" -eq 2 ] && printf '%s' "$out" | grep -q 'corpus file not found'; then
    pass "status with no corpus file"
  else
    fail "status with no corpus file, exit $rc"
  fi

  # 2. Round numbering continues past the rounds already on disk.
  : > "$proj/rounds/round-01-review.jsonl"
  : > "$proj/rounds/round-07-review.jsonl"
  got="$(cd "$proj" && ./run.sh --next-round 2>&1)"
  [ "$got" = "8" ] && pass "next round after 01 and 07 is 8" \
    || fail "next round was \"$got\", expected 8"
  rm -f "$proj"/rounds/*

  # 3. Complete corpus: the loop must keep going until two consecutive quiet
  #    review rounds, then stop with exit 0. The stub is quiet from round 2, so
  #    a correct loop stops at round 3, not round 2.
  node "$tmp/gen.mjs" > "$proj/corpus/functional-images.jsonl"
  out="$(cd "$proj" && AGENT_CMD="$tmp/bin/stub-agent" AGENT_FLAGS= \
    ./run.sh --max-rounds 6 2>&1)"; rc=$?
  ran="$(ls "$proj/rounds" | grep -c 'seek\.md$' || true)"
  if [ "$rc" -eq 0 ] && [ "$ran" -eq 3 ]; then
    pass "loop stopped at round 3 on two quiet rounds, exit 0"
  else
    fail "loop ran $ran rounds and exited $rc, expected 3 rounds and exit 0"
    say "$out"
  fi

  # 4. Incomplete corpus: the cap holds and the exit code says unmet.
  rm -f "$proj"/rounds/*
  cp "$proj/tools/fixtures/valid-item.jsonl" "$proj/corpus/functional-images.jsonl"
  out="$(cd "$proj" && AGENT_CMD="$tmp/bin/stub-agent" AGENT_FLAGS= \
    ./run.sh --max-rounds 2 2>&1)"; rc=$?
  ran="$(ls "$proj/rounds" | grep -c 'seek\.md$' || true)"
  if [ "$rc" -eq 1 ] && [ "$ran" -eq 2 ]; then
    pass "round cap respected, exit 1 with goals unmet"
  else
    fail "cap case ran $ran rounds and exited $rc, expected 2 and exit 1"
    say "$out"
  fi

  # 5. Schema errors stop the loop rather than feeding junk to the reviewer.
  rm -f "$proj"/rounds/*
  cp "$proj/tools/fixtures/invalid-items.jsonl" "$proj/corpus/functional-images.jsonl"
  out="$(cd "$proj" && AGENT_CMD="$tmp/bin/stub-agent" AGENT_FLAGS= \
    ./run.sh --max-rounds 2 2>&1)"; rc=$?
  ran="$(ls "$proj/rounds" | grep -c 'seek\.md$' || true)"
  if [ "$rc" -eq 2 ] && [ "$ran" -eq 0 ]; then
    pass "schema errors stop the loop before any agent runs"
  else
    fail "schema case ran $ran rounds and exited $rc, expected 0 and exit 2"
  fi

  # 6. A failing agent stops the loop instead of spinning.
  rm -f "$proj"/rounds/*
  cp "$proj/tools/fixtures/valid-item.jsonl" "$proj/corpus/functional-images.jsonl"
  out="$(cd "$proj" && AGENT_CMD=false AGENT_FLAGS= ./run.sh --max-rounds 3 2>&1)"
  rc=$?
  [ "$rc" -eq 2 ] && pass "failing agent stops the loop" \
    || fail "failing agent exited $rc, expected 2"

  rule
  if [ "$fails" -eq 0 ]; then say "loop self-test passed"; exit 0; fi
  say "loop self-test failed, $fails case(s)"
  exit 3
fi

# --- the loop -------------------------------------------------------------

say "corpus construction loop"
say "project:     $PROJECT"
say "agent:       $AGENT_CMD $AGENT_FLAGS"
say "max rounds:  $MAX_ROUNDS"
mkdir -p "$ROUNDS"

round="$(next_round)"
last=$((round + MAX_ROUNDS - 1))

while [ "$round" -le "$last" ]; do
  rule
  say "round $round"
  rule

  status_text="$(check)"; rc=$?
  if [ "$rc" -eq 2 ] && [ -f "$CORPUS" ]; then
    say "$status_text"
    say ""
    say "The corpus has schema errors. Fix them before another round: the"
    say "reviewer cannot trust records the validator rejects."
    exit 2
  fi
  say "$status_text"
  say ""

  say "running the seeking agent"
  if ! run_directive 01-seek-functional-images.md "$round" \
      "seeking agent" "$status_text"; then
    say "seeking agent failed in round $round"
    exit 2
  fi

  status_text="$(check)"; rc=$?
  if [ "$rc" -eq 2 ] && [ -f "$CORPUS" ]; then
    say "$status_text"
    say ""
    say "The seeking agent wrote records the validator rejects. Stopping so the"
    say "schema errors above can be fixed."
    exit 2
  fi

  say "running the adversarial reviewer"
  if ! run_directive 02-adversarial-review.md "$round" \
      "adversarial reviewer" "$status_text"; then
    say "adversarial reviewer failed in round $round"
    exit 2
  fi

  rule
  if check; then
    rule
    say "Goals met after round $round. The corpus satisfies every acceptance"
    say "criterion in directives/00-corpus-goals.md."
    exit 0
  fi

  round=$((round + 1))
done

rule
check || true
rule
say "Stopped at the round cap with the goals unmet. Read the newest report in"
say "rounds/ before raising the cap. If progress has stalled, the corpus needs a"
say "human decision, not more rounds."
exit 1