interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / research / tests / passages.sh
| 1 | #!/bin/sh |
| 2 | # Offline oracle: real sync/text -> build quote -> actual agent --gate --split. |
| 3 | # --split exposes the same builder spans, including unselected passages (max three selected/work). |
| 4 | # Python checks bytes/JSON; it neither implements splitting nor substitutes a passage builder. |
| 5 | set -eu |
| 6 | export LC_ALL=C |
| 7 | cd "$(dirname "$0")/../.." |
| 8 | OMC=$HOME/work/flux/omega-mv/seed/out/omc |
| 9 | PIN=3ad9f10f64309e7acf8a725082b28e23a2a4aeb07e98322fb4dc0fcd8fc93194 |
| 10 | [ "$(sha256sum "$OMC" | cut -d' ' -f1)" = "$PIN" ] || { echo 'passages: compiler is not the pin'; exit 2; } |
| 11 | T=$(mktemp -d /tmp/research-passages.XXXXXX) |
| 12 | trap 'rm -rf "$T"' EXIT HUP INT TERM |
| 13 | sh research/build-tools.sh "$T/bin" |
| 14 | "$OMC" < research/build.om > "$T/build" |
| 15 | "$OMC" < research/agent.om > "$T/agent" |
| 16 | chmod 700 "$T/build" "$T/agent" |
| 17 | python3 -I - "$T" <<'PY' |
| 18 | from pathlib import Path |
| 19 | import sys |
| 20 | p=Path(sys.argv[1]) |
| 21 | # Each fixture produces at most three actual builder spans. ASCII and spaced fixtures score all |
| 22 | # spans; exact requested UTF-8 fixture scores quantum only, so --split checks its unselected span too. |
| 23 | for name,text,question in [ |
| 24 | ('utf8','é'*800+' quantum '+'é'*1000,'quantum'), |
| 25 | ('ascii','a'*3609,'aaa'), |
| 26 | ('three','€'*1200+' quantum','quantum'), |
| 27 | ('four','😀'*900+' quantum','quantum'), |
| 28 | ('spaced','quantum '*450,'quantum'), |
| 29 | ]: |
| 30 | d=p/name; d.mkdir() |
| 31 | (d/'raw').write_bytes((text+'\n').encode('utf-8')) |
| 32 | (d/'question').write_text(question) |
| 33 | PY |
| 34 | for f in utf8 ascii three four spaced; do |
| 35 | "$T/bin/research-sync" text < "$T/$f/raw" > "$T/$f/text" |
| 36 | "$T/build" quote < "$T/$f/text" > "$T/$f/quoted" |
| 37 | { printf '@@\tpassage-fixture\tPassage fixture\n'; cat "$T/$f/quoted"; } > "$T/$f/corpus" |
| 38 | done |
| 39 | # Shared unchanged by the correct agent and both compiled source mutants. |
| 40 | cat > "$T/check.py" <<'PY' |
| 41 | import json,re,sys |
| 42 | from pathlib import Path |
| 43 | p=Path(sys.argv[1]); d=Path(sys.argv[2]); name=sys.argv[3] |
| 44 | def fail(reason): |
| 45 | raise SystemExit(reason) |
| 46 | def utf(data): |
| 47 | try: return data.decode('utf-8','strict') |
| 48 | except UnicodeDecodeError: fail('INVALID_UTF8') |
| 49 | def load(data): |
| 50 | try: return json.loads(utf(data)) |
| 51 | except json.JSONDecodeError: fail('INVALID_JSON') |
| 52 | def nosep(data): return data.translate(None,b' \t\r\n') |
| 53 | raw=(p/'raw').read_bytes(); admitted=(p/'text').read_bytes(); quoted=(p/'quoted').read_bytes() |
| 54 | utf(admitted) |
| 55 | if nosep(admitted)!=nosep(raw): fail('SYNC_LOST_BYTES') |
| 56 | if quoted!=b''.join(b'> '+line for line in admitted.splitlines(keepends=True)): fail('QUOTE_LOST_BYTES') |
| 57 | records=[load(line) for line in (d/'split.jsonl').read_bytes().splitlines()] |
| 58 | if not 1<len(records)<=3: fail('FIXTURE_SPANS') |
| 59 | corpus=(p/'corpus').read_bytes() |
| 60 | start=corpus.index(b'\n')+1 |
| 61 | joined=b'' |
| 62 | for r in records: |
| 63 | text=r['text'].encode('utf-8') |
| 64 | if text!=corpus[r['s']:r['e']]: fail('DUMP_NOT_ACTUAL_SPAN') |
| 65 | if r['s']<start or nosep(corpus[start:r['s']]): fail('LOST_BYTES') |
| 66 | joined+=text |
| 67 | start=r['e'] |
| 68 | if nosep(corpus[start:]): fail('LOST_BYTES') |
| 69 | if nosep(joined)!=nosep(quoted): fail('LOST_BYTES') |
| 70 | req=load((d/'request.json').read_bytes()) |
| 71 | content=req['messages'][1]['content'] |
| 72 | # Selected spans come from the JSON-decoded actual request, never a second splitter. |
| 73 | selected=re.findall(r'\[\d+\] Passage fixture\n(.*?)\n\n',content,re.S) |
| 74 | if not selected: fail('NO_SELECTED_PASSAGES') |
| 75 | texts=[r['text'].replace('\n',' ') for r in records] |
| 76 | if any(t not in texts for t in selected): fail('SELECTED_NOT_BUILDER_SPAN') |
| 77 | if name in ('ascii','spaced'): |
| 78 | if len(selected)!=len(records): fail('NOT_ALL_SELECTED') |
| 79 | if sorted(selected)!=sorted(texts): fail('LOST_BYTES') |
| 80 | else: |
| 81 | if not any('quantum' in t for t in selected): fail('QUANTUM_NOT_SELECTED') |
| 82 | answer=load(next((d/'ask').glob('*.json')).read_bytes()) |
| 83 | if answer['answer']!='Fixture answer. [1]': fail('NOT_GATE_ANSWER') |
| 84 | print('PASSAGE_CHECK '+name+' spans='+str(len(records))+' selected='+str(len(selected))+' UTF8_JSON_RECONSTRUCT=PASS') |
| 85 | PY |
| 86 | run_case() { |
| 87 | bin=$1; tag=$2; f=$3 |
| 88 | d=$T/$tag-$f |
| 89 | mkdir -p "$d/ask" "$d/state" |
| 90 | printf '10000000000000000000000000000001\t%s\n' "$(cat "$T/$f/question")" > "$d/ask/queue" |
| 91 | "$bin" "$d/ask/" "$T/$f/corpus" "$d/state/off" --gate --split > "$d/request.json" 2> "$d/split.jsonl" |
| 92 | python3 -I "$T/check.py" "$T/$f" "$d" "$f" |
| 93 | } |
| 94 | for f in utf8 ascii three four spaced; do run_case "$T/agent" good "$f"; done |
| 95 | # Each mutant must compile/run. Only the byte oracle can kill it, not a compile error/missing output. |
| 96 | python3 -I - research/agent.om "$T" <<'PY' |
| 97 | from pathlib import Path |
| 98 | import sys |
| 99 | s=Path(sys.argv[1]).read_text(); p=Path(sys.argv[2]) |
| 100 | a='s = p\nif sep\ns = add p 1\nend' |
| 101 | assert s.count(a)==1 |
| 102 | (p/'skip.om').write_text(s.replace(a,'s = add p 1')) |
| 103 | a='while eq and get gc p 192 128 bound 4\np = sub p 1\nend' |
| 104 | assert s.count(a)==1 |
| 105 | (p/'mid.om').write_text(s.replace(a,'p = add p 1')) |
| 106 | PY |
| 107 | for mutant in skip mid; do |
| 108 | "$OMC" < "$T/$mutant.om" > "$T/$mutant" |
| 109 | chmod 700 "$T/$mutant" |
| 110 | done |
| 111 | if run_case "$T/skip" skip ascii > "$T/skip.log" 2>&1; then |
| 112 | echo 'FAIL mutant skip survived'; exit 1 |
| 113 | fi |
| 114 | grep -qx LOST_BYTES "$T/skip.log" || { cat "$T/skip.log"; echo 'FAIL skip wrong reason'; exit 1; } |
| 115 | if run_case "$T/mid" mid utf8 > "$T/mid.log" 2>&1; then |
| 116 | echo 'FAIL mutant mid survived'; exit 1 |
| 117 | fi |
| 118 | grep -qx INVALID_UTF8 "$T/mid.log" || { cat "$T/mid.log"; echo 'FAIL mid wrong reason'; exit 1; } |
| 119 | echo 'PASSAGE_MUTANT skip=LOST_BYTES mid=INVALID_UTF8 compile=PASS' |
| 120 | echo 'PASSAGES_RESULT cases=5 mutants=2 fail=0 model=NOT_RUN' |
| 121 | # Entire sibling prompt module with the same compiled agent; no second build. |
| 122 | pass=0; fail=0 |
| 123 | ok(){ pass=$((pass+1)); echo "PASS $*"; } |
| 124 | bad(){ fail=$((fail+1)); echo "FAIL $*"; } |
| 125 | . research/tests/prompt.sh |
| 126 | [ "$fail" = 0 ] |