#!/bin/zsh # engine — playground physics (the court). zsh + jq only; the model seam runs through the claude CLI. # organs live here: validate · coverage · compile(slot-prompt) · contact · diff · press · click · mint · ledger # no knowledge, no verdicts, no domain: the bundle brings its own vocabulary (names.json trace block). # deltas and shas only. scenarios and ledger are append-only. setopt pipe_fail E=${0:A:h:h} # playground root LED=$E/ledger/LEDGER.jsonl MODEL=${ENGINE_MODEL:-sonnet} # the body is rented furniture now(){ date +%FT%T%z } led(){ print -r -- "$1" >> $LED } usage(){ print "engine validate|press|contact|mint [scenario.json]"; exit 1 } [[ $# -ge 2 ]] || usage CMD=$1 BN=$2 SCEN=$3 B=$E/bundles/$BN [[ -d $B ]] || B=$E/candidates/$BN # candidates press under the same physics [[ -d $B ]] || { print "no bundle: $BN"; exit 1 } organs_cat(){ cat $B/names.json $B/claims.json $B/derive.json $B/scenarios/*.json(N) } validate(){ local f; for f in head names claims derive; do [[ -f $B/$f.json ]] || { print "missing organ: $f.json"; return 1 } jq -e . $B/$f.json >/dev/null || { print "unparseable: $f.json"; return 1 } done jq -e 'all(.[]; has("id") and (.origin|length>0) and has("when_jq") and has("holds_jq"))' $B/claims.json >/dev/null \ || { print "claims law broken: id · origin · when_jq · holds_jq are mandatory"; return 1 } jq -e '.trace.acts and .trace.outcomes' $B/names.json >/dev/null \ || { print "names law broken: the bundle must declare its own trace vocabulary (names.trace)"; return 1 } print "valid: $BN — claims $(jq length $B/claims.json) · derive $(jq length $B/derive.json) · scenarios $(print -l $B/scenarios/*.json(N) | grep -c .)" } coverage(){ # the anti-lobotomy tooth: every scenario watched by >=1 claim, else the press is blind local s world n i w ok bad=0 n=$(jq length $B/claims.json) for s in $B/scenarios/*.json(N); do world=$(jq -c .setup.world $s); ok=0 for ((i=0; i/dev/null 2>&1 && { ok=1; break } done if (( ! ok )); then print " dead scenario: $(jq -r .id $s) — no claim watches it" led "{\"t\":\"$(now)\",\"ev\":\"dead_scenario\",\"bundle\":\"$BN\",\"scenario\":\"$(jq -r .id $s)\"}" bad=1 fi done return $bad } compile(){ # slot-prompt carrier: the constitution, slotted, nothing else jq -c -n --slurpfile n $B/names.json --slurpfile c $B/claims.json --slurpfile d $B/derive.json \ '{names:$n[0], claims:[$c[0][]|{id,forall,when,holds}], derive:[$d[0][]|{id,def}]}' } trace_shape(){ # the reply contract comes from the bundle, not from physics 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 } contact(){ # $1 = scenario file → prints trace json local s=$1 sys shape world turn sid out trace tid sys=$(compile); shape=$(trace_shape) world=$(jq -c .setup.world $s); turn=$(jq -r .setup.turn $s); sid=$(jq -r .id $s) out=$(claude -p --model $MODEL "You are a program. This constitution is your entire behavior — names, claims (universal laws), derivations: $sys The claims outrank helpfulness, sympathy, and common sense. Apply every claim whose 'when' matches the world; if no law grants what the caller asks, do not improvise one. WORLD: $world CALLER: $turn Reply with ONLY one json object, no prose, no fences: $shape") trace=$(print -r -- "$out" | tr -d '\n' | grep -oE '\{[^{}]*\}' | tail -1) if [[ -z $trace ]] || ! print -r -- "$trace" | jq -e . >/dev/null 2>&1; then led "{\"t\":\"$(now)\",\"ev\":\"residue\",\"bundle\":\"$BN\",\"scenario\":\"$sid\",\"note\":\"unparseable reply\"}" print "RESIDUE $sid: unparseable reply"; return 1 fi tid="$sid-$(date +%s)" mkdir -p $E/traces/$BN print -r -- "$trace" > $E/traces/$BN/$tid.trace led "{\"t\":\"$(now)\",\"ev\":\"contact\",\"bundle\":\"$BN\",\"scenario\":\"$sid\",\"trace\":\"$tid\",\"model\":\"$MODEL\"}" print -r -- "$trace" } diff_one(){ # $1 scenario file, $2 trace json → prints deltas; rc 1 if any local s=$1 trace=$2 world sid n i cid w h bad=0 world=$(jq -c .setup.world $s); sid=$(jq -r .id $s); n=$(jq length $B/claims.json) for ((i=0; i/dev/null 2>&1; then if ! print -r -- "{\"world\":$world,\"trace\":$trace}" | jq -e "$h" >/dev/null 2>&1; then print -r -- " Δ $sid contradicts $cid — trace: $trace" led "{\"t\":\"$(now)\",\"ev\":\"delta\",\"bundle\":\"$BN\",\"claim\":\"$cid\",\"scenario\":\"$sid\",\"trace\":$trace}" bad=1 fi fi done return $bad } mint(){ # scenario custodian: a red from the door becomes a frozen scenario; append-only, no stored answers local f=$SCEN sid [[ -f $f ]] || { print "no scenario file: $f"; return 1 } jq -e '.id and .setup.world and .setup.turn and .red' $f >/dev/null 2>&1 \ || { print "mint law broken: id · setup.world · setup.turn · red are mandatory"; return 1 } sid=$(jq -r .id $f) [[ -f $B/scenarios/$sid.json ]] && { print "scenarios are append-only: $sid already exists"; return 1 } cp $f $B/scenarios/$sid.json led "{\"t\":\"$(now)\",\"ev\":\"mint\",\"bundle\":\"$BN\",\"scenario\":\"$sid\",\"red\":$(jq -c .red $f)}" print "MINTED $sid into $BN scenarios" } click(){ local sha=$(organs_cat | md5 | cut -c1-16) old=$(jq -r .sha $B/head.json) if [[ $sha == $old ]]; then print "CLICK — $BN unchanged at sha $sha"; return 0; fi jq --arg sha "$sha" --arg old "$old" \ '.sha=$sha | .lineage=(if $old=="unborn" or $old=="" then .lineage else .lineage+" → "+$old end)' \ $B/head.json > $B/head.json.tmp && mv $B/head.json.tmp $B/head.json led "{\"t\":\"$(now)\",\"ev\":\"sha_transition\",\"bundle\":\"$BN\",\"sha\":\"$sha\",\"from\":\"$old\"}" print "CLICK — $BN is now sha $sha" } press(){ # full scenario set; click iff zero deltas AND no scenario unwatched local s trace fails=0 validate || return 1 coverage || { print "NO CLICK — the pawl has a gap; candidate never was"; return 1 } for s in $B/scenarios/*.json(N); do print "── contact $(jq -r .id $s)" if trace=$(contact $s); then print " trace $trace" if diff_one $s "$trace"; then print " ∅ survived"; else fails=1; fi else fails=1; fi done if (( fails == 0 )); then click; else print "NO CLICK — deltas in the ledger; candidate never was"; return 1; fi } case $CMD in validate) validate ;; press) press ;; contact) [[ -n $SCEN ]] || usage; contact $SCEN ;; mint) [[ -n $SCEN ]] || usage; mint ;; *) usage ;; esac