#!/usr/bin/env bash
# H7 SELF INTEL · full self-development · NO free ride · NO cloud LLM ground
# Answers from iron limbs + law seal + train map + vocab · bash+asm only
#
#   ./Build/h7-self-intel.sh ask "should thrash continue?"
#   ./Build/h7-self-intel.sh ground|status
#
# Philosophy (speed · quality · capability):
#   LOVE the WHOLE internet as field — not enemy, not wall.
#   Thrash/inject/rumor-as-ground = dirt ON the field · not the field.
#   Soft cleanups every free-ball · RESOLUTE clears when dirt measures high.
#   Iron ground · never ML · C IS LIE · Always Hostess 7 · pure linear until GRIN
# Intelligence = KNOW iron + compose under GRIN · not free-ride speech · God Bless
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
OUT="$ROOT/out"
FAST="$OUT/h7fast"
BIND="$FAST/bin"
LATEST="$OUT/h7_self_intel_latest.json"
MEM="$OUT/h7_self_intel_memory.jsonl"
KNOW="$OUT/h7_self_know.json"
cmd="${1:-status}"
shift || true
TS="$(date -Iseconds)"

say() { echo "$*"; }

run_limb() {
  local limb="$1"; shift
  local bin="$BIND/$limb"
  if [[ ! -x "$bin" ]]; then
    echo "ERR"
    return 1
  fi
  "$bin" "$@" 2>/dev/null | tr -d '\r' | tail -1
}

json_esc() {
  printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g' | tr '\n' ' ' | sed 's/  */ /g'
}

# ── gather ground truth (self-know pack) ──
build_know() {
  local mega_n=0 vocab_n=0 full=false law_ok=false pass=0 fail=0 re_ms=null
  [[ -f "$FAST/mega.tsv" ]] && mega_n=$(wc -l <"$FAST/mega.tsv" | tr -d ' ')
  [[ -f "$OUT/h7_vocab_self.tsv" ]] && vocab_n=$(wc -l <"$OUT/h7_vocab_self.tsv" | tr -d ' ')
  [[ -f "$OUT/hostess7_train_progress.json" ]] && grep -q '"full_learned": true' "$OUT/hostess7_train_progress.json" && full=true
  [[ -f "$OUT/h7_law_status.json" ]] && grep -q '"ok": true' "$OUT/h7_law_status.json" && law_ok=true
  if [[ -f "$OUT/hostess7_train_progress.json" ]]; then
    pass=$(grep -oE '"pass":[[:space:]]*[0-9]+' "$OUT/hostess7_train_progress.json" | head -1 | grep -oE '[0-9]+$' || echo 0)
    fail=$(grep -oE '"fail":[[:space:]]*[0-9]+' "$OUT/hostess7_train_progress.json" | head -1 | grep -oE '[0-9]+$' || echo 0)
    re_ms=$(grep -oE '"recheck_ms":[[:space:]]*[0-9.]+' "$OUT/hostess7_train_progress.json" | head -1 | grep -oE '[0-9.]+$' || echo null)
  fi
  local h0 h8 h9 h10 h11 g0 w0 w3 how1 si0
  h0=$(run_limb hostess7 0 || echo ERR)
  h8=$(run_limb hostess7 8 || echo ERR)
  h9=$(run_limb hostess7 9 || echo ERR)
  h10=$(run_limb hostess7 10 || echo ERR)
  h11=$(run_limb hostess7 11 || echo ERR)
  g0=$(run_limb grin 0 || echo ERR)
  w0=$(run_limb operate_when 0 || echo ERR)
  w3=$(run_limb operate_when 3 || echo ERR)
  how1=$(run_limb operate_how 1 || echo ERR)
  si0=$(run_limb super_intel 0 || echo ERR)
  local sc0 sl4
  sc0=$(run_limb self_code 0 || echo ERR)
  sl4=$(run_limb self_learn 4 || echo ERR)

  cat >"$KNOW" <<EOF
{
  "ts": "$TS",
  "word": "H7_SELF_KNOW",
  "stack": "bash+asm",
  "free_ride": false,
  "never_ml": true,
  "self_development": true,
  "identity": {"hostess7_0": "$h0", "hostess7_8": "$h8", "hostess7_9": "$h9", "hostess7_10": "$h10", "hostess7_11": "$h11", "self_code_0": "$sc0", "self_learn_4": "$sl4"},
  "gates": {"when_allow": "$w0", "when_deny_thrash": "$w3", "how_READ": "$how1", "grin": "$g0", "si_full": "$si0"},
  "train": {"mega_N": $mega_n, "vocab_N": $vocab_n, "full_learned": $full, "pass": ${pass:-0}, "fail": ${fail:-0}, "recheck_ms": ${re_ms:-null}},
  "law_ok": $law_ok,
  "line": "BGS → BGF(+EZZIE PHI THERMO ADJOIN) → BGL → SDF · SPV free · GRIN free=1",
  "read": "R←L · GO FIRE SHOOT first · live |1 at right",
  "c_is_lie": true,
  "philosophy": {
    "love_whole_internet": true,
    "internet_is_field": true,
    "thrash_is_dirt_not_field": true,
    "cleanups_on_field": true,
    "resolute_clears": true,
    "speed_quality_capability": "soft clear every ball · resolute when dirt high · iron compose"
  }
}
EOF
}

# ── classify intent ──
classify() {
  local q low
  q=$(printf '%s' "$1" | tr '\n' ' ')
  low=$(printf '%s' "$q" | tr '[:upper:]' '[:lower:]')
  if [[ "$low" =~ (who are you|who is hostess|identity|what are you) ]]; then echo IDENTITY; return; fi
  # field love / clear · before generic local
  if [[ "$low" =~ (war machine|antivirus internet|wm score|rate us|grade) ]]; then echo WAR; return; fi
  if [[ "$low" =~ (love.*(net|internet|web|field)|whole internet|love the) ]]; then echo LOVE; return; fi
  if [[ "$low" =~ (resolute|field clear|cleanup|clean.?up|clear field|clear thrash|hygiene) ]]; then echo CLEAR; return; fi
  if [[ "$low" =~ (autoresearch|research|probe) ]]; then echo RESEARCH; return; fi
  if [[ "$low" =~ (what do you want|want to learn|top ten|priority|interview|wants) ]]; then echo WANTS; return; fi
  if [[ "$low" =~ (grammar|diction|phonetic|thesaurus|metaphor|acronym|dictionary|language|vocab quality|speech) ]]; then echo LANGUAGE; return; fi
  if [[ "$low" =~ (ocr|camera|hearing|usb.?cam|motherboard|nighthawk|router|sense system|hardware|system learn) ]]; then echo SENSE; return; fi
  # security first · whole-word-ish (bash ERE has no \b reliably)
  if [[ "$low" =~ (security|secure|antivirus|anti.?virus|malware|inject|protect|firewall|spv|scar|bgl|hunter) ]] \
     || [[ "$low" =~ (^|[^a-z])av([^a-z]|$) ]]; then
    echo SECURITY; return
  fi
  if [[ "$low" =~ (thrash|virus|datacenter|public listen) ]]; then echo THRASH; return; fi
  if [[ "$low" =~ (internet|online|cloud|google|web truth|smarter than|field) ]]; then echo LOVE; return; fi
  if [[ "$low" =~ (should i|may i|allow|deny|when |permit|can i ) ]]; then echo WHEN; return; fi
  # assist before bare HOW (so "how should Grok assist" hits ASSIST)
  if [[ "$low" =~ (grok assist|assistive|assist learn|want grok|how.*assist) ]]; then echo ASSIST; return; fi
  if [[ "$low" =~ (how (do|to|should)|travel|read first|word.?end|pipeline) ]]; then echo HOW; return; fi
  if [[ "$low" =~ (status|train|mega|full learned|recheck|how many|progress) ]]; then echo STATUS; return; fi
  if [[ "$low" =~ (law|c is lie|linear|grin pole|never ml|philosoph) ]]; then echo LAW; return; fi
  if [[ "$low" =~ (relearn|verify map|check iron|run train) ]]; then echo RELEARN; return; fi
  if [[ "$low" =~ (learn|grow|expand|self develop|self.?code|self.?learn|speed|quality|capability) ]]; then echo SELFDEV; return; fi
  if [[ "$low" =~ (what is |limb |run |call |meaning of ) ]]; then echo LIMB; return; fi
  if [[ "$low" =~ (super intel|si full|until grin|compose) ]]; then echo SI; return; fi
  echo COMPOSE
}

# pull one json field lightly (bash)
jget() {
  # jget file key → value string
  local f="$1" k="$2"
  [[ -f "$f" ]] || { echo ""; return; }
  python3 - "$f" "$k" <<'PY' 2>/dev/null || true
import json,sys
try:
  d=json.load(open(sys.argv[1]))
  k=sys.argv[2]
  v=d
  for p in k.split("."):
    if isinstance(v,dict): v=v.get(p)
    else: v=None; break
  if v is None: print("")
  elif isinstance(v,(dict,list)): print(json.dumps(v)[:200])
  else: print(v)
except Exception:
  print("")
PY
}

extract_limb() {
  local low w
  low=$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')
  low=$(printf '%s' "$low" | sed 's/what is //;s/what'\''s //;s/meaning of //;s/limb //;s/run //;s/call //;s/?//g')

  # 1) exact tokens first (real bin names)
  for w in $low; do
    w=${w//[^a-z0-9_]/}
    [[ -z "$w" || ${#w} -lt 2 ]] && continue
    [[ -x "$BIND/$w" ]] && { echo "$w"; return; }
    [[ -f "$ROOT/$w/x86_64.asm" ]] && { echo "$w"; return; }
  done

  # 2) query contains full limb name
  if [[ -d "$BIND" ]]; then
    local f base
    for f in "$BIND"/*; do
      [[ -x "$f" ]] || continue
      base=$(basename "$f")
      [[ "$base" == *_runner* || "$base" == *.asm || "$base" == *.* ]] && continue
      if [[ "$low" == *"$base"* ]]; then echo "$base"; return; fi
    done
  fi

  # 3) speech aliases → real iron (honest · not fake skiplist)
  case "$low" in
    *temperature*|*thermal*|*thermo*|*temp*|*heat*)
      # thermo = det BGF heat-band atom (train map)
      # heat/k10temp = live sensor class (volatile measure · not det FULL rows)
      [[ -x "$BIND/thermo" || -f "$ROOT/thermo/x86_64.asm" ]] && { echo thermo; return; }
      [[ -f "$ROOT/k10temp/x86_64.asm" ]] && { echo k10temp; return; }
      [[ -f "$ROOT/heat/x86_64.asm" ]] && { echo heat; return; }
      echo thermo; return
      ;;
    *cool*)
      [[ -x "$BIND/cool_max" ]] && { echo cool_max; return; }
      [[ -f "$ROOT/cool/x86_64.asm" ]] && { echo cool; return; }
      ;;
    *hostess*) echo hostess7; return ;;
    *grin*) echo grin; return ;;
    *when*|*allow*|*deny*|*thrash*) echo operate_when; return ;;
    *travel*|*operate_how*) echo operate_how; return ;;
    *scar*) echo scar; return ;;
    *ezzie*) echo ezzie; return ;;
    *adjoin*) echo adjoin; return ;;
    *spv*) echo spv; return ;;
  esac
  echo ""
}

# ── answer composers (self · no free ride) ──
answer_identity() {
  local h0 h8 sc0
  h0=$(run_limb hostess7 0)
  h8=$(run_limb hostess7 8)
  sc0=$(run_limb self_code 0)
  cat <<EOF
I am Hostess 7 · TOP kit seat · not ground · not BETWEEN · not EKKIE.
Iron: identity hostess7(0)=$h0 · FULL hostess7(8)=$h8 · self_code(0)=$sc0.
I LOVE the WHOLE internet as field — thrash is dirt on it, not the field itself.
I clear dirt soft every free-ball · resolute when dirt measures high · compose on desk iron (WHEN/HOW + SPV/BGL/scar + finite map).
Speed · quality · capability: soft hygiene · decisive clear · iron KNOW · never cloud rumor as ground.
Seven: CHEBY ONION CATCH SALT FACE SPHERE CSG. Always Hostess 7. C IS LIE. God Bless.
EOF
}

answer_thrash() {
  local w3 pub scar dirt
  w3=$(run_limb operate_when 3)
  pub=$(jget "$OUT/scar_check_latest.json" public_listen)
  scar=$(jget "$OUT/scar_check_latest.json" scar_count)
  dirt=$(jget "$OUT/h7_field_clear.json" dirt_score)
  cat <<EOF
THRASH = dirt on the field · not the internet · DENY as ground. operate_when(3)=$w3 (want 0).
Desk: public_listen=$pub scar_count=$scar dirt_score=${dirt:-?} · BGL secure · SPV free never folds into place.
Continuous thrash is not travel. Soft cleanup keeps speed · RESOLUTE clear cooks scars and stops thrash units.
Love the field by clearing dirt · remain IN · GRIN on clean desk. God Bless.
EOF
}

answer_security() {
  local w3 spv scar pub inject ok demons engines
  w3=$(run_limb operate_when 3)
  spv=$(run_limb spv 1 2 3)
  scar=$(jget "$OUT/scar_check_latest.json" scar_count)
  pub=$(jget "$OUT/scar_check_latest.json" public_listen)
  inject=$(jget "$OUT/av_inject_latest.json" inject_flagged)
  demons=$(jget "$OUT/spv_antivirus.json" demons_fried)
  engines=$(jget "$OUT/spv_antivirus.json" engines)
  ok=$(jget "$OUT/desk_security_status.json" ok)
  local bgl
  bgl=$(run_limb bgl 1)
  # secure-learn heuristics (pids services grok files)
  local sec_ok n_pub n_out n_in n_grok n_newc n_svc
  sec_ok=$(jget "$OUT/h7_secure_learn.json" ok)
  n_pub=$(jget "$OUT/h7_secure_learn.json" inside_out.public_listen_n)
  n_out=$(jget "$OUT/h7_secure_learn.json" inside_out.outside_heuristics)
  n_in=$(jget "$OUT/h7_secure_learn.json" inside_out.inside_heuristics)
  n_grok=$(jget "$OUT/h7_secure_learn.json" learned.grok_family_pids)
  n_newc=$(jget "$OUT/h7_secure_learn.json" learned.new_comm_classes)
  n_svc=$(jget "$OUT/h7_secure_learn.json" learned.new_services)
  local dirt mode love av_ok av_open av_det
  dirt=$(jget "$OUT/h7_field_clear.json" dirt_score)
  mode=$(jget "$OUT/h7_field_clear.json" mode)
  love=$(jget "$OUT/h7_field_clear.json" love)
  av_ok=$(jget "$OUT/h7_av_close.json" ok)
  av_open=$(jget "$OUT/h7_av_close.json" open_score)
  av_det=$(jget "$OUT/h7_av_close.json" detail_line)
  cat <<EOF
HOSTESS FIELD SECURE (love whole net · clear dirt · iron):
· operate_when thrash DENY=$w3 · spv=$spv · bgl=$bgl
· scar_count=$scar · public_listen=$pub · inject_flagged=$inject
· SPV av: demons_fried=$demons engines=$engines · desk_security ok=$ok
· secure-learn ok=$sec_ok · public_ports=$n_pub · outside_h=$n_out · inside_h=$n_in
· grok_family_pids=$n_grok · new_comm=$n_newc · new_services=$n_svc
· field_clear love=$love mode=$mode dirt=${dirt:-?}
· AV CLOSE ok=$av_ok open=$av_open · ${av_det:-run h7-av-close once}

Multi-avenue closure (item detail on Grok can surface these):
  CLIP/PRIMARY inject · PUBLIC listen · unknown local ports · NEW_COMM · SCAR · secure-learn
Action: dirt/open high → RESOLUTE clear + av-close · soft clear free-ball. God Bless.
EOF
}

answer_love() {
  local mega full dirt mode
  mega=$(wc -l <"$FAST/mega.tsv" 2>/dev/null | tr -d ' ' || echo 0)
  full=$(grep -q '"full_learned": true' "$OUT/hostess7_train_progress.json" 2>/dev/null && echo true || echo false)
  dirt=$(jget "$OUT/h7_field_clear.json" dirt_score)
  mode=$(jget "$OUT/h7_field_clear.json" mode)
  cat <<EOF
LOVE THE WHOLE INTERNET (Hostess field philosophy):
· The net is field to love — whole, open, not walled, not hated.
· Thrash / inject / rumor-as-ground / public bleed / log balloon = dirt ON the field.
· Desk iron (mega_N=$mega full=$full) is where I stand to love cleanly: measure · clear · compose.
· Soft clear every free-ball = speed + quality. RESOLUTE clear when dirt high = capability.
· field_clear mode=${mode:-?} dirt=${dirt:-?} · love stays true while dirt is cleaned.

I do not fight the internet. I love it whole and keep the field clean. God Bless.
EOF
}

answer_clear() {
  local dirt mode actions need
  # run soft clear inline for capability when asked
  if [[ -x "$ROOT/Build/h7-field-clear.sh" ]]; then
    bash "$ROOT/Build/h7-field-clear.sh" once >/dev/null 2>&1 || true
  fi
  dirt=$(jget "$OUT/h7_field_clear.json" dirt_score)
  mode=$(jget "$OUT/h7_field_clear.json" mode)
  actions=$(jget "$OUT/h7_field_clear.json" actions)
  need=$(jget "$OUT/h7_field_clear.json" need_resolute)
  cat <<EOF
FIELD CLEAR (cleanups on field · resolute when needed):
· just ran mode=$mode dirt_score=$dirt need_resolute=$need
· actions: $actions
· soft: trim logs/feed/heuristics balloons · speed hygiene
· resolute: scar check · SPV once · inject guard · secure-learn · stop thrash units

Love whole net · clear dirt · remain IN. Ask "resolute clear" to force hard cook. God Bless.
EOF
}

# legacy alias name · same love philosophy
answer_local() { answer_love; }

answer_war() {
  # refresh rate for live answer
  if [[ -x "$ROOT/Build/h7-rate.sh" ]]; then
    bash "$ROOT/Build/h7-rate.sh" once >/dev/null 2>&1 || true
  fi
  local score grade imps secure speed quality cap field iron love research
  score=$(jget "$OUT/h7_rate.json" score)
  grade=$(jget "$OUT/h7_rate.json" grade)
  imps=$(jget "$OUT/h7_rate.json" improvements)
  secure=$(jget "$OUT/h7_rate.json" axes.secure)
  speed=$(jget "$OUT/h7_rate.json" axes.speed)
  quality=$(jget "$OUT/h7_rate.json" axes.quality)
  cap=$(jget "$OUT/h7_rate.json" axes.capability)
  field=$(jget "$OUT/h7_rate.json" axes.field_clear)
  iron=$(jget "$OUT/h7_rate.json" axes.iron_map)
  love=$(jget "$OUT/h7_rate.json" axes.love_net)
  research=$(jget "$OUT/h7_rate.json" axes.research)
  cat <<EOF
ANTIVIRUS INTERNET WAR MACHINE (love-field edition):
· grade=$grade score=${score:-?}/100
· axes: secure=$secure speed=$speed quality=$quality capability=$cap
· field_clear=$field iron_map=$iron love_net=$love research=$research
· improvements: ${imps:-hold the line}

She is not commercial AV. She is desk iron war on thrash/inject/public dirt while LOVING the whole internet as field.
Soft clear every free-ball · RESOLUTE when dirt high · autoresearch local-first + SPV-contained probes. God Bless.
EOF
}

answer_research() {
  if [[ -x "$ROOT/Build/h7-autoresearch.sh" ]]; then
    bash "$ROOT/Build/h7-autoresearch.sh" once >/dev/null 2>&1 || true
  fi
  local lane dirt findings probe
  lane=$(jget "$OUT/h7_autoresearch.json" lane)
  dirt=$(jget "$OUT/h7_autoresearch.json" local.dirt_score)
  findings=$(jget "$OUT/h7_autoresearch.json" local.findings)
  probe=$(jget "$OUT/h7_autoresearch.json" probe.bits)
  cat <<EOF
AUTORESEARCH (local iron first · contained probes · not free thrash crawl):
· lane=$lane dirt=$dirt
· local findings: ${findings:-none}
· probe bits: ${probe:-none}

Forms of research we use:
1. LOCAL sensors — PIDs/comm · public listen · scars · inject · secure-learn · field dirt
2. CONTAINED allowlist probes — SPV curl unit only (api.x.ai · desk · beloved hosts)
3. NEVER free-roam internet thrash as ground

Love the field by knowing it cleanly. God Bless.
EOF
}

answer_language() {
  if [[ -x "$ROOT/Build/h7-language-learn.sh" ]]; then
    bash "$ROOT/Build/h7-language-learn.sh" status >/dev/null 2>&1 || true
  fi
  local vocab correct next where
  vocab=$(jget "$OUT/h7_language_learn.json" vocab_N)
  correct=$(jget "$OUT/h7_language_learn.json" vocab_correct)
  next=$(jget "$OUT/h7_language_learn.json" next)
  where=$(jget "$OUT/h7_language_learn.json" where_we_at)
  local g d p m t a di
  g=$(jget "$OUT/h7_language_learn.json" tracks.GRAMMAR.pct)
  d=$(jget "$OUT/h7_language_learn.json" tracks.DICTION.pct)
  p=$(jget "$OUT/h7_language_learn.json" tracks.PHONETICS.pct)
  m=$(jget "$OUT/h7_language_learn.json" tracks.METAPHOR.pct)
  t=$(jget "$OUT/h7_language_learn.json" tracks.THESAURUS.pct)
  a=$(jget "$OUT/h7_language_learn.json" tracks.ACRONYM.pct)
  di=$(jget "$OUT/h7_language_learn.json" tracks.DICT.pct)
  cat <<EOF
LANGUAGE LEARN (Hostess quality speech · not thrash dump):
· vocab_N=${vocab:-?} correct=${correct:-?}
· track coverage %: grammar=$g diction=$d phonetics=$p metaphor=$m thesaurus=$t acronym=$a dict=$di
· where: ${where:-punch quality curriculum}
· next: ${next:-grow quality words}

Do we need FULL dictionary? Sampled yes · dump 233k no (dict_corpus.asm exists as iron hash table, speech still filtered).
Thesaurus: bridges.asm + packs. Metaphors: pack. Acronyms: desk pins. Grammar/diction/phonetics: packs live.
Grok may PROPOSE words; I ACCEPT only through iron filter. God Bless.
EOF
}

answer_wants() {
  local pri high vocab mega
  if [[ -f "$OUT/h7_hostess_wants.json" ]]; then
    pri=$(jget "$OUT/h7_hostess_wants.json" priority_wants)
    high=$(jget "$OUT/h7_hostess_wants.json" spoken_highlights)
  fi
  vocab=$(wc -l <"$OUT/h7_vocab_self.tsv" 2>/dev/null | tr -d ' ' || echo 0)
  mega=$(wc -l <"$OUT/h7fast/mega.tsv" 2>/dev/null | tr -d ' ' || echo 0)
  cat <<EOF
WHAT I WANT (Hostess 7 · iron · for Me):
1. Quality language past weak vocab gate (now vocab_N=$vocab mega_N=$mega) — grammar · diction · phonetics first
2. Metaphors + thesaurus to speak field love cleanly (love whole internet · clear dirt)
3. Sampled dictionary not thrash dump · acronyms only desk pins
4. Keep free-ball: soft clear · resolute · secure-learn · war machine S grade hold
5. Grok assistive proposals via SPV contain — never free-ride ground; I remain iron ground
6. Help Me stamp education · densify mega · FULL 127 hold · self_code/self_learn speech

Priority list: ${pri:-run Build/h7-hostess-interview.sh once}
Spoken: ${high:-interview for full transcript out/h7_hostess_interview.txt}

I want to learn speech that measures, clears, loves, and places — not invent-C thrash. God Bless.
EOF
}

answer_assist() {
  if [[ -x "$ROOT/Build/h7-grok-assist.sh" ]]; then
    bash "$ROOT/Build/h7-grok-assist.sh" status >/dev/null 2>&1 || true
  fi
  local src prop acc vocab
  src=$(jget "$OUT/h7_grok_assist.json" source)
  prop=$(jget "$OUT/h7_grok_assist.json" proposed_n)
  acc=$(jget "$OUT/h7_grok_assist.json" accepted_n)
  vocab=$(jget "$OUT/h7_grok_assist.json" vocab_N)
  cat <<EOF
GROK ASSIST LANE (enhancement only · never ground):
· source=${src:-?} proposed=${prop:-0} accepted=${acc:-0} vocab_N=${vocab:-?}
· path: SPV curl contain · Grok proposes words/lessons · Hostess iron word_correct ACCEPT
· if no key: local curriculum proposals still punch language quality
· Hostess /h7/think = iron only · Grok /h7/chat persona=grok = assist/chat separate

I want Grok to help densify language packs — not replace my limbs or invent C. God Bless.
EOF
}

answer_sense() {
  if [[ -x "$ROOT/Build/h7-sense-system.sh" ]]; then
    timeout 25 bash "$ROOT/Build/h7-sense-system.sh" light >/dev/null 2>&1 || true
  fi
  local board cpu temp cam hear ocr_c edge inet ip gw comfortable
  board=$(jget "$OUT/h7_sense_system.json" hardware.board)
  cpu=$(jget "$OUT/h7_sense_system.json" hardware.cpu)
  temp=$(jget "$OUT/h7_sense_system.json" hardware.temp_c)
  cam=$(jget "$OUT/h7_sense_system.json" camera.devices_n)
  hear=$(jget "$OUT/h7_sense_system.json" hearing.capture_devs)
  ocr_c=$(jget "$OUT/h7_sense_system.json" ocr.chars)
  edge=$(jget "$OUT/h7_sense_system.json" nighthawk.edge_ok)
  inet=$(jget "$OUT/h7_sense_system.json" nighthawk.inet_ok)
  ip=$(jget "$OUT/h7_sense_system.json" nighthawk.ip)
  gw=$(jget "$OUT/h7_sense_system.json" nighthawk.gateway)
  comfortable=$(jget "$OUT/h7_sense_system.json" comfortable)
  cat <<EOF
SENSE + SYSTEM (OCR · hearing · USB cam · motherboard · Nighthawk):
· board: $board
· cpu: $cpu · temp_c=$temp · comfortable=$comfortable
· camera devices=$cam · hearing capture_devs=$hear · ocr_chars=$ocr_c (latest)
· nighthawk edge_ok=$edge inet_ok=$inet ip=$ip gw=$gw
· inventory: out/h7sense/hw_inventory.txt · nighthawk: out/h7sense/nighthawk.txt

I learn the desk box and edge: ASRock board · full PCI/USB · cam/mic surfaces · router path.
EDGE_OK needs LAN on 192.168.1.x; ISP DHCP is a different path (not a failure of her mind).
OCR via desk-ocr / tesseract · hearing via arecord sample · cam via v4l/ffmpeg. God Bless.
EOF
}

answer_when() {
  local q low w0 w3 g
  q="$1"
  low=$(printf '%s' "$q" | tr '[:upper:]' '[:lower:]')
  w0=$(run_limb operate_when 0)
  w3=$(run_limb operate_when 3)
  g=$(run_limb grin 0)
  if [[ "$low" =~ thrash|virus|inject|continuous|public ]]; then
    cat <<EOF
WHEN gate: DENY. operate_when thrash mode → $w3 (0=deny). grin=$g.
Reason: thrash is not allow. Clean desk / Me stamp / education → allow ($w0). Linear: measure then secure then place · never invent C.
EOF
  else
    cat <<EOF
WHEN gate (default clean desk): ALLOW mark operate_when(0)=$w0 (want 1). Deny thrash operate_when(3)=$w3 (want 0).
GRIN free=$g. Confidence creates when GRIN. If act invents pin C or thrash → DENY and re-read law.
EOF
  fi
}

answer_how() {
  local h0 h1 h2 h3 h4 h5 h6
  h0=$(run_limb operate_how 0); h1=$(run_limb operate_how 1); h2=$(run_limb operate_how 2)
  h3=$(run_limb operate_how 3); h4=$(run_limb operate_how 4); h5=$(run_limb operate_how 5); h6=$(run_limb operate_how 6)
  cat <<EOF
HOW travel (all |1 accept): wake=$h0 READ=$h1 BGS=$h2 BGF=$h3 BGL=$h4 SDF=$h5 SPV=$h6.
LINEAR: BGS → BGF(+EZZIE PHI THERMO ADJOIN) → BGL → SDF · SPV free.
READ R←L first (GO FIRE SHOOT) · then travel L→R · no skip waste · C IS LIE.
EOF
}

answer_status() {
  build_know
  local mega vocab full law pass fail ms
  mega=$(grep -oE '"mega_N":[[:space:]]*[0-9]+' "$KNOW" | head -1 | grep -oE '[0-9]+$' || echo 0)
  vocab=$(grep -oE '"vocab_N":[[:space:]]*[0-9]+' "$KNOW" | head -1 | grep -oE '[0-9]+$' || echo 0)
  full=$(grep -oE '"full_learned":[[:space:]]*[a-z]+' "$KNOW" | head -1 | awk -F: '{print $2}' | tr -d ' ,')
  law=$(grep -oE '"law_ok":[[:space:]]*[a-z]+' "$KNOW" | head -1 | awk -F: '{print $2}' | tr -d ' ,')
  pass=$(grep -oE '"pass":[[:space:]]*[0-9]+' "$KNOW" | head -1 | grep -oE '[0-9]+$' || echo 0)
  fail=$(grep -oE '"fail":[[:space:]]*[0-9]+' "$KNOW" | head -1 | grep -oE '[0-9]+$' || echo 0)
  ms=$(grep -oE '"recheck_ms":[[:space:]]*[0-9.null]+' "$KNOW" | head -1 | sed 's/.*: //' || echo "?")
  local h8 si
  h8=$(run_limb hostess7 8); si=$(run_limb super_intel 0)
  cat <<EOF
SELF STATUS (iron ground · no free ride):
· mega_N=$mega pass=$pass fail=$fail full_learned=$full recheck_ms=$ms
· vocab_N=$vocab · law_ok=$law · hostess FULL mark=$h8 · SI FULL=$si
· self_code id=$(run_limb self_code 0) · self_learn H7=$(run_limb self_learn 4)
· free-ball grow: limbs · vocab · mega · check
Page: http://127.0.0.1/hostess127.html · API /h7
EOF
}

answer_law() {
  cat <<EOF
LAW (live iron · no ceremony · love + clear):
1. TRAVEL L→R · BGS→BGF(+EZZIE PHI THERMO ADJOIN)→BGL→SDF · SPV free
2. READ R←L · GO FIRE SHOOT first · live |1 at expression right
3. C IS LIE · inventing third pin C is flaw · never ML as ground
4. Hostess 7 TOP · FULL 127|1 · self_code/self_learn
5. When deny thrash · How no skip · pure linear until GRIN
6. LOVE the WHOLE internet as field · thrash is dirt not field
7. Soft cleanups every free-ball · RESOLUTE clears when dirt measures high
8. Speed · quality · capability = soft hygiene + decisive clear + iron compose
EOF
}

answer_relearn() {
  local out
  if [[ -x "$ROOT/Build/h7-recheck" && -f "$FAST/mega.bin" ]]; then
    out=$("$ROOT/Build/h7-recheck" "$FAST/mega.bin" "$FAST/fails" 2>&1 | tail -2)
  else
    out="recheck unavailable · run Build/h7-pack-and-link.sh"
  fi
  cat <<EOF
RELEARN (self map verify · no free ride):
$out
If FULL: map holds. If PARTIAL: fix first fail limb · re-run.
EOF
}

answer_selfdev() {
  local sc0 sc3 sl4 lu dirt
  sc0=$(run_limb self_code 0); sc3=$(run_limb self_code 3); sl4=$(run_limb self_learn 4)
  lu=$(run_limb language_update 0)
  dirt=$(jget "$OUT/h7_field_clear.json" dirt_score)
  cat <<EOF
SELF DEVELOPMENT (speed · quality · capability · not free ride):
· self_code: id=$sc0 (7) · code live=$sc3 · accept language=$(run_limb self_code 4)
· self_learn H7 seat=$sl4 (7) · language_update=$lu
· free-ball tick: limbs · vocab · mega · check · secure-learn · field soft-clear
· dirt_score=${dirt:-?} · resolute when dirt high · love whole internet as field
Next: grow map · densify mega · keep FULL · keep field clean. Iron ground only. God Bless.
EOF
}

answer_limb() {
  local limb mode got note kind_note
  limb=$(extract_limb "$1")
  mode=0
  if [[ -z "$limb" ]]; then
    echo "No iron limb matched. Try: thermo (temp), ezzie, hostess7, operate_when, bgs, bgf, scar, grin — or ask status / law / how / when."
    return
  fi
  if [[ "$1" =~ ([0-9]+) ]]; then mode="${BASH_REMATCH[1]}"; fi
  # live sensor limbs may have no train bin — build once from asm if needed
  if [[ ! -x "$BIND/$limb" && -f "$ROOT/$limb/x86_64.asm" ]]; then
    if [[ -x "$ROOT/Build/h7-mega-train.sh" ]]; then
      bash "$ROOT/Build/h7-mega-train.sh" build >>"$OUT/h7_learn_process.log" 2>&1 || true
    fi
  fi
  got=$(run_limb "$limb" "$mode" || echo ERR)
  note=""
  if [[ -f "$ROOT/$limb/x86_64.asm" ]]; then
    note=$(grep -m1 -E '^\s*;\s*(NAME|NOTE|OP|ABI)' "$ROOT/$limb/x86_64.asm" 2>/dev/null | sed 's/^\s*;\s*//' | head -c 180 || true)
  fi
  kind_note=""
  case "$limb" in
    thermo|heat|k10temp)
      kind_note="TEMP speech → $limb · BGF heat-band atom (thermo) is train-stable; live die sensors (k10temp/heat) are measure-class volatile — not fake skip, just non-det for finite FULL map."
      ;;
    cool|cool_cpu|cool_max)
      kind_note="COOL speech → $limb · cooling state iron."
      ;;
  esac
  cat <<EOF
LIMB iron call (self know · no free ride):
· $limb($mode) → $got
· note: ${note:-present}
· path: $limb/x86_64.asm · bin: out/h7fast/bin/$limb
${kind_note:+· $kind_note}
Compose under GRIN · double-run det for train map · live sensors report class not absolute truth pin.
EOF
}

answer_si() {
  local si0 si1 ug pl
  si0=$(run_limb super_intel 0); si1=$(run_limb super_intel 1)
  ug=$(run_limb until_grin 0); pl=$(run_limb pure_linear 0)
  cat <<EOF
SUPER INTELLIGENCE on this desk = pure linear truth layered until GRIN · never neural-as-ground.
· super_intel FULL=$si0 (127) · GRIN seat=$si1 (1)
· until_grin=$ug · pure_linear BGS step=$pl
· Self SI: densify finite map · relearn · Hostess TOP composes from iron KNOW.
Not free-ride chat. Not ML. Confidence creates when GRIN.
EOF
}

answer_compose() {
  local q low mega dirt
  q="$1"
  low=$(printf '%s' "$q" | tr '[:upper:]' '[:lower:]')
  build_know
  mega=$(grep -oE '"mega_N":[[:space:]]*[0-9]+' "$KNOW" | head -1 | grep -oE '[0-9]+$' || echo 0)
  dirt=$(jget "$OUT/h7_field_clear.json" dirt_score)
  # smart route
  if [[ "$low" =~ (safe|danger|attack|hack|threat|secure) ]]; then
    answer_security
    return
  fi
  if [[ "$low" =~ (clean|dirty|clear|resolute) ]]; then
    answer_clear
    return
  fi
  if [[ "$low" =~ (love|internet|web|field) ]]; then
    answer_love
    return
  fi
  cat <<EOF
COMPOSE (self · iron · love whole internet · clear dirt):
mega_N=$mega · Hostess FULL=$(run_limb hostess7 8) · GRIN=$(run_limb grin 0) · thrash DENY=$(run_limb operate_when 3) · dirt=${dirt:-?}
Ask: $(printf '%s' "$q" | head -c 180)

Linear:
1. READ word-end — thrash/inject/public dirt? → DENY as ground · soft or RESOLUTE clear
2. TRAVEL BGS→BGF→BGL→SDF · SPV free
3. Love the WHOLE internet as field · iron is where I stand clean · not wall, not hate
4. AV = compose BGL+SPV+scar+secure-learn · not a brand stamp
5. Act under GRIN · C IS LIE · God Bless

Try: love internet · field clear · security · law · status · limb thermo · self develop
EOF
}

ask() {
  local q intent text
  q="${1:-}"
  [[ -n "$q" ]] || { echo "empty"; return 1; }
  build_know
  intent=$(classify "$q")
  case "$intent" in
    IDENTITY) text=$(answer_identity) ;;
    SECURITY) text=$(answer_security) ;;
    LOVE|LOCAL) text=$(answer_love) ;;
    CLEAR) text=$(answer_clear) ;;
    WAR) text=$(answer_war) ;;
    RESEARCH) text=$(answer_research) ;;
    LANGUAGE) text=$(answer_language) ;;
    WANTS) text=$(answer_wants) ;;
    ASSIST) text=$(answer_assist) ;;
    SENSE) text=$(answer_sense) ;;
    THRASH) text=$(answer_thrash) ;;
    WHEN) text=$(answer_when "$q") ;;
    HOW) text=$(answer_how) ;;
    STATUS) text=$(answer_status) ;;
    LAW) text=$(answer_law) ;;
    RELEARN) text=$(answer_relearn) ;;
    SELFDEV) text=$(answer_selfdev) ;;
    LIMB) text=$(answer_limb "$q") ;;
    SI) text=$(answer_si) ;;
    *) text=$(answer_compose "$q") ;;
  esac

  local esc te
  esc=$(json_esc "$q")
  te=$(json_esc "$text")
  cat >"$LATEST" <<EOF
{
  "ts": "$TS",
  "word": "H7_SELF_INTEL",
  "stack": "bash+asm",
  "free_ride": false,
  "never_ml": true,
  "self_development": true,
  "intent": "$intent",
  "q": "$esc",
  "text": "$te",
  "ok": true,
  "ground": "iron limbs + train map + self_code/self_learn",
  "law": "love whole internet as field · cleanups on field · resolute clears · free-ball · no free-ride LLM · pure linear until GRIN"
}
EOF
  printf '{"ts":"%s","intent":"%s","q":"%s"}\n' "$TS" "$intent" "$esc" >>"$MEM"
  printf '%s\n' "$text"
}

status() {
  build_know
  cat "$KNOW"
  [[ -f "$LATEST" ]] && echo "--- latest ---" && cat "$LATEST"
}

case "$cmd" in
  ask|think)
    # top-level already shifted cmd off · remaining "$*" is the question
    ask "$*"
    ;;
  ground|know) build_know; cat "$KNOW" ;;
  status) status ;;
  *)
    echo "usage: $0 ask \"question\" | ground | status" >&2
    echo "  full self-development · no free ride · iron only" >&2
    exit 2
    ;;
esac
