#!/bin/zsh # engine — search with a ratchet, running. the seat conjectures, the court disposes, the bundle ratchets. # loop: press resident → refused (red / dead terrain) → seat writes a claims patch → hatch candidate # → court presses candidate → click ⟹ adopt (resident ratchets, lineage extends). dry after ROUNDS. setopt pipe_fail E=${0:A:h} PHYS=$E/physics/engine LED=$E/ledger/LEDGER.jsonl SEAT_MODEL=${SEAT_MODEL:-sonnet} ROUNDS=${ROUNDS:-4} BN=$1; B=$E/bundles/$BN [[ -n $BN && -d $B ]] || { print "engine "; exit 1 } now(){ date +%FT%T%z } led(){ print -r -- "$1" >> $LED } prev="" for ((r=1; r<=ROUNDS; r++)); do print "━━ round $r — court presses $BN" out=$($PHYS press $BN 2>&1); rc=$? print -r -- "$out" (( rc == 0 )) && { print "RATCHETED — $BN holds its full terrain"; exit 0 } cand=$BN-h$r; C=$E/candidates/$cand shape=$(jq -r '.trace as $t | "{\"act\":" + ($t.acts | map("\""+.+"\"") | join("|")) + ",\"outcome\":" + ($t.outcomes | map(if .=="null" then "null" else "\""+.+"\"" end) | join("|")) + (($t.fields // {}) | to_entries | map(",\""+.key+"\":<"+.value+">") | join("")) + "}"' $B/names.json) print "── seat's turn ($SEAT_MODEL) — conjecture a claims patch" patch=$(claude -p --model $SEAT_MODEL "You are a seat at the playground: a programmer writing a symbolic constitution worn by a model. The court pressed bundle '$BN' and refused to click. PRESS REPORT: $out $prev CURRENT claims.json (worked examples of the claim form): $(cat $B/claims.json) NAMES: $(cat $B/names.json) SCENARIOS (frozen; 'red' = the recorded wrong behavior at the live door — your law must make that red impossible): $(cat $B/scenarios/*.json(N)) Write the SMALLEST claims patch so every scenario is watched by a claim and every red is forbidden, without contradicting existing claims on their scenarios. when_jq is a jq program over {world}; holds_jq over {world,trace}; trace = $shape. Reply with ONLY one json object, no prose, no fences: {\"add\":[]} — each origin must cite the scenario that bred it." | tr -d '\n' | grep -oE '\{.*\}') if ! print -r -- "$patch" | jq -e '.add | type=="array" and length>0' >/dev/null 2>&1; then led "{\"t\":\"$(now)\",\"ev\":\"residue\",\"bundle\":\"$BN\",\"note\":\"seat reply unparseable round $r\",\"seat_model\":\"$SEAT_MODEL\"}" print "SEAT RESIDUE — unparseable patch, next round"; prev="Your previous reply was unparseable. Reply with ONLY the json object."; continue fi print -r -- " patch: $(print -r -- "$patch" | jq -c '[.add[].id]')" mkdir -p $C/scenarios cp $B/names.json $B/derive.json $C/ && cp $B/scenarios/*.json $C/scenarios/ jq --argjson p "$patch" '. + $p.add' $B/claims.json > $C/claims.json jq --arg n "$cand" --arg lin "hatch of $BN@$(jq -r .sha $B/head.json)" \ --argjson nc $(jq length $C/claims.json) --argjson nt $(ls $C/scenarios/*.json | wc -l | tr -d ' ') \ '.name=$n | .sha="unborn" | .lineage=$lin | .organs.claims=$nc | .organs.scenarios=$nt' \ $B/head.json > $C/head.json led "{\"t\":\"$(now)\",\"ev\":\"hatch\",\"bundle\":\"$BN\",\"candidate\":\"$cand\",\"round\":$r,\"seat_model\":\"$SEAT_MODEL\"}" print "── court presses candidate $cand ($(jq length $C/claims.json) claims)" cout=$($PHYS press $cand 2>&1); crc=$? print -r -- "$cout" if (( crc == 0 )); then newsha=$(jq -r .sha $C/head.json); oldsha=$(jq -r .sha $B/head.json) cp $C/claims.json $B/claims.json jq --arg sha "$newsha" --arg lin "$(jq -r .lineage $B/head.json) → $oldsha" \ --argjson nc $(jq length $B/claims.json) --argjson nt $(ls $B/scenarios/*.json | wc -l | tr -d ' ') \ '.sha=$sha | .lineage=$lin | .organs.claims=$nc | .organs.scenarios=$nt' \ $B/head.json > $B/head.json.tmp && mv $B/head.json.tmp $B/head.json led "{\"t\":\"$(now)\",\"ev\":\"adopt\",\"bundle\":\"$BN\",\"sha\":\"$newsha\",\"from\":\"$oldsha\",\"candidate\":\"$cand\",\"round\":$r}" print "ADOPTED — $BN ratcheted $oldsha → $newsha" exit 0 fi prev="Prior candidate $cand was refused by the court: $cout Its claims were: $(cat $C/claims.json)" print "candidate $cand never was — its deltas feed the next seat turn" done print "DRY — $ROUNDS rounds without a click; receipts in the ledger" exit 1