interplane.Get Omega
Read in your language

omega-harness

Agent skills for operating, encoding and communicating in Omega (.om) and the AIEN repositories

Brought by drakestapleton

branch main Branches Tags History

omega-harness / tools / check.sh

235 lines · 13157 bytes Raw Blame History
1#!/bin/sh
2# check.sh OMC [OMEGA_MCP]: the guide gate. Run in the repository root, beside examples/gate.sh OMC.
3#
4# With OMC (its sha256 must be PIN's omc-sha256) it builds tools/jflat.om, tools/guides.om and
5# tools/rules.om, then requires:
6# data data/reference.tsv is tools/jflat's flattening of the omega-mcp data/reference.json that
7# data/SOURCE names (OMEGA_MCP: default the Interplane git repository)
8# guides `git ls-files | guides check` exits 0: every block matches its sources, nothing typed
9# rules `rules lint rules/rules.txt` exits 0, and rendering is deterministic
10# and that every check fails its negative control, on a scratch copy of the tracked files:
11# a stale block, a typed pin, a typed count, reference data for another pin, an unknown block,
12# a private path in the public rules, an unterminated rule, malformed JSON;
13# and that the pin rule compares words against PIN's values: with a fixture PIN whose revision and
14# sha256 start with 8 digits, 8-, 12- and 16-character, upper-case and full restatements are refused
15# while dates, counts and near misses are not, and five mutants of that comparison are each caught.
16# Prints one line per step and ends `CHECK PASS steps=N` (exit 0) or `CHECK FAIL ...` (exit 1);
17# exit 2 on a usage, pin or build error. This file is invocation glue around the Omega programs.
18set -u
19[ "$#" -ge 1 ] || { sed -n '2,14s/^# \{0,1\}//p' "$0" >&2; exit 2; }
20omc=$1
21mcp=${2:-}
22[ -f PIN ] && [ -d tools ] || { echo "CHECK FAIL run in the omega-harness root" >&2; exit 2; }
23want=$(sed -n 's/^omc-sha256 //p' PIN)
24got=$(sha256sum "$omc" 2>/dev/null | cut -d' ' -f1)
25[ -n "$want" ] && [ "$got" = "$want" ] || { echo "CHECK FAIL omc sha256 ${got:-unreadable}, PIN wants $want" >&2; exit 2; }
26unset GIT_DIR GIT_NAMESPACE GIT_WORK_TREE
27export GIT_TERMINAL_PROMPT=0
28tmp=$(mktemp -d) || exit 2
29trap 'rm -rf "$tmp"' EXIT
30for t in jflat guides rules; do
31 "$omc" < "tools/$t.om" > "$tmp/$t" && chmod +x "$tmp/$t" || { echo "CHECK FAIL tools/$t.om does not compile" >&2; exit 2; }
32done
33pass=0 fail=0
34ok() { echo "PASS $1"; pass=$((pass + 1)); }
35no() { echo "FAIL $1"; fail=$((fail + 1)); }
36# expect NAME RC: the last command's exit status $? must be RC
37expect() { if [ "$3" = "$2" ]; then ok "$1 (exit $3)"; else no "$1: exit $3, expected $2"; fi; }
38
39# data: reproduce data/reference.tsv from its recorded source
40set -- $(cat data/SOURCE 2>/dev/null)
41if [ "$#" -ge 6 ]; then
42 if [ -z "$mcp" ]; then
43 mcp=https://interplane.io/projects/omega-mcp/repo.git
44 fi
45 if git clone -q --bare "$mcp" "$tmp/mcp" 2>/dev/null && git --git-dir "$tmp/mcp" show "$3:$4" > "$tmp/ref.json" 2>/dev/null; then
46 [ "$(sha256sum "$tmp/ref.json" | cut -d' ' -f1)" = "$6" ] && "$tmp/jflat" < "$tmp/ref.json" | cmp -s - data/reference.tsv
47 expect "data: data/reference.tsv is jflat of omega-mcp $3 $4" 0 $?
48 else
49 no "data: cannot read omega-mcp $3 from $mcp"
50 fi
51else
52 no "data: data/SOURCE must name the omega-mcp commit, file and sha256"
53fi
54
55# guides and rules on the tree as it is
56git ls-files | "$tmp/guides" check > "$tmp/g.out"
57expect "guides check: $(tail -n 2 "$tmp/g.out" | head -n 1)" 0 $?
58[ "$fail" = 0 ] || sed 's/^/ /' "$tmp/g.out" | grep -v '^ guides:' | head -n 40
59"$tmp/rules" lint rules/rules.txt > "$tmp/r.out"
60expect "rules lint: $(cat "$tmp/r.out")" 0 $?
61{ "$tmp/rules" render rules/rules.txt /dev/null claude x > "$tmp/r1" && "$tmp/rules" render rules/rules.txt /dev/null claude x > "$tmp/r2" && cmp -s "$tmp/r1" "$tmp/r2"; }
62expect "rules render is deterministic" 0 $?
63
64# negative controls on a scratch copy of the tracked files
65copy() {
66 rm -rf "$tmp/c" && mkdir -p "$tmp/c" &&
67 git ls-files | while IFS= read -r f; do mkdir -p "$tmp/c/$(dirname "$f")" && cp -p "$f" "$tmp/c/$f"; done
68 git ls-files > "$tmp/c.list"
69}
70gcheck() { (cd "$tmp/c" && "$tmp/guides" check < "$tmp/c.list" > "$tmp/c.out" 2>&1); }
71blockfile=$(grep -l '^<!-- gen:pin -->$' $(git ls-files '*.md') | head -n 1)
72short=$(printf '%s' "$want" | cut -c1-8)
73
74copy && awk '{ print } /^<!-- gen:pin -->$/ && !d { print "| stale | row |"; d = 1 }' "$blockfile" > "$tmp/c/$blockfile.x" && mv "$tmp/c/$blockfile.x" "$tmp/c/$blockfile"
75gcheck; r=$?; grep -q 'block is stale' "$tmp/c.out" || r=99
76expect "control: a stale block in $blockfile is refused" 1 $r
77
78copy && printf '\nThe compiler is omc `%s`.\n' "$short" >> "$tmp/c/README.md"
79gcheck; r=$?; grep -q "restates PIN outside a generated block: $short" "$tmp/c.out" || r=99
80expect "control: a typed pin is refused" 1 $r
81
82# the pin rule on a fixture PIN whose revision and sha256 start with 8 digits (values built from short
83# pieces, so this file names no 7-character word of them); data/reference.tsv's first two lines follow it
84d8=$((2718 * 10000 + 2818))
85s8=$((3141 * 10000 + 5926))
86drev=$d8$(printf %s 0a1b 2c3d 4e5f 6a7b 8c9d 0e1f 2a3b 4c5d)
87dsha=$s8$(printf %s 5e6f 7a8b 9c0d 1e2f 3a4b 5c6d 7e8f 9a0b 1c2d 3e4f 5a6b 7c8d 9e0f 1a2b)
88r12=$(echo "$drev" | cut -c1-12)
89fyes="$d8 $s8 $r12 $(echo "$dsha" | cut -c1-16) $drev $dsha $(echo "$r12" | tr a-f A-F)"
90fno="$((2026 * 10000 + 1111)) $((1000 * 10000)) $((d8 + 1)) $((s8 + 1))"
91copy && printf 'omega-mv %s\nomc-sha256 %s\n' "$drev" "$dsha" > "$tmp/c/PIN" &&
92 { printf '/pin/omega_mv\t%s\n/pin/omc_sha256\t%s\n' "$drev" "$dsha"; sed 1,2d data/reference.tsv; } > "$tmp/c/data/reference.tsv" &&
93 { printf '\n## Pin fixture\n\n'; for w in $fyes $fno; do printf 'The word `%s` is here.\n' "$w"; done; } >> "$tmp/c/README.md"
94pinfix() { # pinfix GUIDES: the fixture words GUIDES judges wrongly (each of fyes refused, none of fno)
95 (cd "$tmp/c" && "$1" check < "$tmp/c.list" > "$tmp/f.out" 2>&1)
96 for w in $fyes; do grep -q "restates PIN outside a generated block: $w\$" "$tmp/f.out" || printf '%s ' "$w"; done
97 for w in $fno; do grep -q "restates PIN outside a generated block: $w\$" "$tmp/f.out" && printf '%s ' "$w"; done
98}
99bad=$(pinfix "$tmp/guides"); [ -z "$bad" ]
100expect "control: fixture pin with an all-digit start: 8-, 12-, 16-character, upper-case and full restatements refused, dates, counts and near misses not${bad:+ (wrong: $bad)}" 0 $?
101gmut() { # gmut NAME SED-ARGS...: tools/guides.om edited by sed must judge a fixture word wrongly
102 m=$1; shift
103 sed "$@" tools/guides.om > "$tmp/$m.om"
104 if cmp -s tools/guides.om "$tmp/$m.om"; then no "control: mutant $m: the edit did not apply"; return; fi
105 if ! "$omc" < "$tmp/$m.om" > "$tmp/$m" 2> "$tmp/$m.err" || [ -s "$tmp/$m.err" ]; then no "control: mutant $m does not compile: $(head -c 200 "$tmp/$m.err")"; return; fi
106 chmod +x "$tmp/$m"
107 bad=$(pinfix "$tmp/$m"); [ -n "$bad" ]
108 expect "control: mutant $m of the pin rule is caught${bad:+ by $bad}" 0 $?
109}
110gmut m-letter -e 's/^if pinpre s t$/if and ishexw s t pinpre s t/'
111gmut m-floor -e 's/^if ge sub t s 7$/if ge sub t s 9/'
112gmut m-revfirst -e 's/^for i ulong n bound 40$/for i ulong 1 bound 40/'
113gmut m-shafirst -e 's/^for j ulong n bound 64$/for j ulong 1 bound 64/'
114gmut m-nofold -e '/^fn fold c uchar to uchar$/,/^end$/s/^ret add c 32$/ret c/'
115
116n=$(git ls-files 'examples/*.om' | wc -l | tr -d ' ')
117copy && printf '\nThe gate runs %s examples.\n' "$n" >> "$tmp/c/README.md"
118gcheck; r=$?; grep -q "typed count outside a generated block: $n examples" "$tmp/c.out" || r=99
119expect "control: a typed count is refused" 1 $r
120
121copy && printf '\nOn 2026-11-11 the gate ran %s examples with omc `%s`.\n' "$n" "$short" >> "$tmp/c/README.md"
122gcheck
123expect "control: a dated observation naming the pin and a count is allowed" 0 $?
124
125copy && printf '<!-- gen:nosuch -->\n<!-- /gen:nosuch -->\n' >> "$tmp/c/README.md"
126gcheck; r=$?; grep -q 'unknown block' "$tmp/c.out" || r=99
127expect "control: an unknown block is refused" 1 $r
128
129copy && sed '1s/^omega-mv \(.......\)./omega-mv \10/' PIN > "$tmp/c/PIN"
130cmp -s PIN "$tmp/c/PIN" && sed -i '1s/^omega-mv \(.......\)./omega-mv \11/' "$tmp/c/PIN"
131gcheck
132expect "control: reference data for another pin is refused" 2 $?
133
134cp rules/rules.txt "$tmp/priv.txt" && printf 'rule leak\ndate 2026-11-11\nfor all\norder 1\n| read ~/secret/notes.md\nend\n' >> "$tmp/priv.txt"
135"$tmp/rules" lint "$tmp/priv.txt" > /dev/null
136expect "control: a home path in the public rules is refused" 1 $?
137
138# Tracker addresses and private issue identifiers belong only in the machine overlay.
139for marker in 'https://linear.app/private-workspace/issue/example' 'DRA-EXAMPLE'; do
140 cp rules/rules.txt "$tmp/tracker.txt" && printf 'rule tracker-leak\ndate 2026-11-16\nfor all\norder 1\n| read %s\nend\n' "$marker" >> "$tmp/tracker.txt"
141 "$tmp/rules" lint "$tmp/tracker.txt" > /dev/null
142 expect "control: a private tracker reference in public rules is refused" 1 $?
143 printf 'rule private-tracker\ndate 2026-11-16\nfor all\norder 1\n| read %s\nend\n' "$marker" > "$tmp/tracker-overlay.txt"
144 "$tmp/rules" lint rules/rules.txt "$tmp/tracker-overlay.txt" > /dev/null
145 expect "control: a private tracker reference in the overlay is allowed" 0 $?
146done
147
148cp rules/rules.txt "$tmp/open.txt" && printf 'rule open\ndate 2026-11-11\nfor all\norder 1\n| text\n' >> "$tmp/open.txt"
149"$tmp/rules" lint "$tmp/open.txt" > /dev/null
150expect "control: a rule without end is refused" 1 $?
151
152# A generic reader gets the complete agents policy unless its own cap requires a shorter file.
153cat > "$tmp/size.rules" <<'RULES'
154rule shared
155date 2026-11-18
156for all
157order 1
158| SHARED POLICY
159end
160rule only-agents
161date 2026-11-18
162for agents
163order 2
164| AGENTS ONLY
165end
166rule limited
167date 2026-11-18
168for generic
169order 3
170| LIMITED ééé
171end
172RULES
173# Make the shared text longer than the compact file's directions.
174for i in 1 2 3 4 5 6 7 8; do
175 sed -i '/^| SHARED POLICY$/a | The shared rules belong in the full agent file.' "$tmp/size.rules"
176done
177size_overlay() {
178 printf 'target agents /full agent.md\ntarget generic /small.md\ntarget generic /large.md\ntarget omp /omp.md\n'
179}
180size_overlay > "$tmp/base.ov"
181check_sizes() (
182 set -e
183 r=$1
184 full=$("$r" render "$tmp/size.rules" "$tmp/base.ov" generic HEAD)
185 printf '%s\n' "$full" | grep -qx 'AGENTS ONLY'
186 printf '%s\n' "$full" | grep -qx 'SHARED POLICY'
187 printf '%s\n' "$full" | grep -qx 'LIMITED ééé'
188 n=$(printf '%s\n' "$full" | LC_ALL=C.UTF-8 wc -m)
189 { size_overlay; printf 'cap %s /small.md\n' "$((n - 1))"; } > "$tmp/size.ov"
190 small=$("$r" render "$tmp/size.rules" "$tmp/size.ov" generic HEAD /small.md)
191 printf '%s\n' "$small" | grep -qx 'LIMITED ééé'
192 printf '%s\n' "$small" | grep -qF '/full agent.md'
193 if printf '%s\n' "$small" | grep -qx 'SHARED POLICY'; then exit 1; fi
194 test "$full" = "$("$r" render "$tmp/size.rules" "$tmp/size.ov" generic HEAD /large.md)"
195 s=$(printf '%s\n' "$small" | LC_ALL=C.UTF-8 wc -m)
196 { size_overlay; printf 'cap %s /small.md\n' "$s"; } > "$tmp/size.ov"
197 "$r" fit "$tmp/size.rules" "$tmp/size.ov" HEAD | grep -q " $s of $s ok$"
198 { size_overlay; printf 'cap %s /small.md\n' "$((s - 1))"; } > "$tmp/size.ov"
199 if "$r" fit "$tmp/size.rules" "$tmp/size.ov" HEAD > "$tmp/fit.out"; then exit 1; fi
200 grep -q ' OVER$' "$tmp/fit.out"
201 if "$r" render "$tmp/size.rules" "$tmp/size.ov" generic HEAD /small.md >/dev/null 2>&1; then exit 1; fi
202 omp=$("$r" render "$tmp/size.rules" "$tmp/base.ov" omp HEAD)
203 n=$(printf '%s\n' "$omp" | LC_ALL=C.UTF-8 wc -m)
204 { size_overlay; printf 'cap %s /omp.md\n' "$n"; } > "$tmp/size.ov"
205 "$r" fit "$tmp/size.rules" "$tmp/size.ov" HEAD | grep -q "fit omp /omp.md $n of $n ok$"
206 printf 'target generic /small.md\ncap %s /small.md\n' "$s" > "$tmp/alone.ov"
207 if "$r" fit "$tmp/size.rules" "$tmp/alone.ov" HEAD >/dev/null; then exit 1; fi
208 if "$r" render "$tmp/size.rules" "$tmp/base.ov" generic HEAD /omp.md >/dev/null 2>&1; then exit 1; fi
209 if "$r" render "$tmp/size.rules" "$tmp/base.ov" omp HEAD /unknown.md >/dev/null 2>&1; then exit 1; fi
210 size_overlay > "$tmp/missing.ov"
211 printf 'cap 100 /unknown.md\n' >> "$tmp/missing.ov"
212 if "$r" fit "$tmp/size.rules" "$tmp/missing.ov" HEAD >/dev/null; then exit 1; fi
213 for invalid in 'cap 2' 'cap 2 /small.md' 'cap -1 /small.md' 'target omp /small.md'; do
214 { size_overlay; printf '%s\n' "$invalid"; } > "$tmp/invalid.ov"
215 if "$r" lint "$tmp/size.rules" "$tmp/invalid.ov" >/dev/null; then exit 1; fi
216 done
217 # Exercise the published class in both generic readers from one overlay.
218 n=$("$r" render rules/rules.txt "$tmp/base.ov" generic HEAD | LC_ALL=C.UTF-8 wc -m)
219 { size_overlay; printf 'cap %s /small.md\n' "$((n - 1))"; } > "$tmp/real.ov"
220 for path in /large.md /small.md; do
221 "$r" render rules/rules.txt "$tmp/real.ov" generic HEAD "$path" > "$tmp/real.md"
222 grep -Fq 'Record every Omega math check under the class' "$tmp/real.md"
223 done
224 exit 0
225)
226check_sizes "$tmp/rules"
227expect "generic coverage, per-path caps, Unicode boundaries and malformed or ambiguous targets" 0 $?
228
229printf '{"a":[1,}' | "$tmp/jflat" > /dev/null 2>&1
230expect "control: malformed JSON is refused" 2 $?
231
232total=$((pass + fail))
233if [ "$fail" = 0 ]; then echo "CHECK PASS steps=$total"; exit 0; fi
234echo "CHECK FAIL steps=$total pass=$pass fail=$fail"
235exit 1