code wiki / (root) / nx_gen_mind_gate.nx

nx_gen_mind_gate.nx source

↩ module page · 101 lines · 6244 B

1// nx_gen_mind_gate.nx -- the REFEREE for companionchat CC0 (go_mind_context in nx_companion_mind.nx). 2// DONE-RULE (pre-declared in companionchat.plan): the composed chat prompt provably contains em_recall hits, 3// cm_ctx relationship lines and es_context typed state whenever those stores hold data for the session, proven 4// against the REAL promoted organs (forked from the serving root); a neg-control with empty stores must compose NO 5// fabricated memory or present lines. 6// FIXTURE LAW: scratch lives under /tmp/<gate>/ (never a production plane) and the session key is unique PER RUN, 7// so the gate is idempotent -- a memory planted by run N can never make run N+1's empty-session tooth vacuous. 8// license_tier: ORIGINAL expect_exit: 0 9import "nx_syscalls.nx" 10import "nx_gate_verdict.nx" 11import "nx_companion_mind.nx" 12 13const MG_DIR: *u8 = "/tmp/nx_gen_mind_gate" as *u8 14const MG_MODE_DIR: i64 = 511 15const MG_MODE_FILE: i64 = 420 16const MG_CAP: i64 = 16384 17const MG_TOKEN: *u8 = "qux9zebra" as *u8 18const MG_PLANT: *u8 = "my cat is named qux9zebra and she is grey" as *u8 19const MG_ASK: *u8 = "what is my cat qux9zebra called again" as *u8 20const MG_HELLO: *u8 = "hello there" as *u8 21 22func mg_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 23 24// per-run session key: fx + the low digits of the monotonic clock, [a-z0-9_] and well under nx_charmem's 32 cap 25func mg_session(out: *u8) -> i64 { 26 var o: i64 = 0 27 out[o] = 102 as u8; o = o + 1 28 out[o] = 120 as u8; o = o + 1 29 var v: i64 = sys_now_us() 30 if v < 0 { v = 0 - v } 31 let t: *u8 = sys_mmap(32) 32 var k: i64 = 0 33 while v > 0 { if k < 12 { t[k] = (48 + (v % 10)) as u8; k = k + 1 } v = v / 10 } 34 if k == 0 { t[0] = 48 as u8; k = 1 } 35 while k > 0 { k = k - 1; out[o] = t[k]; o = o + 1 } 36 out[o] = 0 as u8 37 return o 38} 39 40func mg_write_session_file(session: *u8) -> i64 { 41 let fd: i64 = sys_openat_wr(MIND_SESSION_FILE, MG_MODE_FILE) 42 if fd < 0 { return 0 - 1 } 43 sys_write(fd, session, mg_len(session)) 44 sys_close(fd) 45 return 0 46} 47 48func main() -> i64 { 49 gv_head("nx_gen_mind_gate -- CC0: memory, relationship and typed present-tense state compose into the chat turn; empty stores contribute nothing" as *u8) 50 let ctr: *i64 = gv_ctr() 51 52 // SETUP: scratch cwd so the organs' relative planes (knowledge/store/...) land under /tmp, never in production 53 sys_mkdir(MG_DIR, MG_MODE_DIR) 54 let cd: i64 = sys_chdir(MG_DIR) 55 gv_check("setup-scratch-cwd-entered" as *u8, cd == 0, ctr) 56 let session: *u8 = sys_mmap(64) 57 mg_session(session) 58 let wr: i64 = mg_write_session_file(session) 59 gv_check("setup-per-run-session-declared" as *u8, wr == 0, ctr) 60 let chk: *u8 = sys_mmap(64) 61 mind_session(chk) 62 gv_check("setup-lib-reads-the-declared-session" as *u8, mind_find(chk, mg_len(chk), 0, session) == 0, ctr) 63 64 // T1 NEG-CONTROLS on an EMPTY session: nothing remembered, nothing present -- and nothing invented 65 let out: *u8 = sys_mmap(MG_CAP) 66 let n0: i64 = go_mind_context(MG_HELLO, mg_len(MG_HELLO), out, MG_CAP) 67 gv_check("neg-control-empty-session-emits-no-remembered-line" as *u8, mind_find(out, n0, 0, "[REMEMBERED" as *u8) < 0, ctr) 68 gv_check("neg-control-empty-session-emits-no-present-line" as *u8, mind_find(out, n0, 0, "[PRESENT" as *u8) < 0, ctr) 69 gv_check("neg-control-empty-session-never-contains-the-token" as *u8, mind_find(out, n0, 0, MG_TOKEN) < 0, ctr) 70 // the relationship block for a fresh char IS the organ's declared default state -- present, and true 71 gv_check("relationship-state-block-composed-from-charmem" as *u8, mind_find(out, n0, 0, "[RELATIONSHIP STATE]" as *u8) >= 0, ctr) 72 73 // T2 PLANT through the SAME write lane the daemon uses, then ask with the token in the turn 74 go_mind_remember(MG_PLANT, mg_len(MG_PLANT)) 75 // fixture reached the condition: the ORGAN itself reports a hit for the token (not our parser) 76 let raw: *u8 = sys_mmap(MIND_CAP) 77 let rn: i64 = mind_run(MIND_ELF_MEM, 4, "recall" as *u8, session, MG_TOKEN, MIND_RECALL_MAX, raw) 78 gv_check("fixture-reached-condition-organ-recall-hits-nonzero" as *u8, (rn > 0) & (mind_find(raw, rn, 0, "\"hits\":0," as *u8) < 0), ctr) 79 let n1: i64 = go_mind_context(MG_ASK, mg_len(MG_ASK), out, MG_CAP) 80 gv_check("planted-memory-recalled-into-the-turn" as *u8, (mind_find(out, n1, 0, "[REMEMBERED" as *u8) >= 0) & (mind_find(out, n1, 0, MG_TOKEN) >= 0), ctr) 81 gv_check("recalled-line-carries-the-exact-detail" as *u8, mind_find(out, n1, 0, "she is grey" as *u8) >= 0, ctr) 82 83 // T3 PRESENT appears only once the typed state is SET (every narrative field, so no field reads unset) 84 let sb: *u8 = sys_mmap(MIND_CAP) 85 mind_run(MIND_ELF_STATE, 4, "set" as *u8, session, "outfit" as *u8, "sundress" as *u8, sb) 86 mind_run(MIND_ELF_STATE, 4, "set" as *u8, session, "location" as *u8, "kitchen" as *u8, sb) 87 mind_run(MIND_ELF_STATE, 4, "set" as *u8, session, "mood" as *u8, "happy" as *u8, sb) 88 mind_run(MIND_ELF_STATE, 4, "set" as *u8, session, "activity" as *u8, "making tea" as *u8, sb) 89 mind_run(MIND_ELF_STATE, 4, "set" as *u8, session, "dynamic" as *u8, "playful" as *u8, sb) 90 let n2: i64 = go_mind_context(MG_HELLO, mg_len(MG_HELLO), out, MG_CAP) 91 gv_check("present-block-composed-after-state-set" as *u8, mind_find(out, n2, 0, "[PRESENT" as *u8) >= 0, ctr) 92 gv_check("present-block-names-the-set-location" as *u8, mind_find(out, n2, 0, "kitchen" as *u8) >= 0, ctr) 93 // ORDER is part of the contract: what she IS, then who they ARE to each other, then what was SAID 94 let ip: i64 = mind_find(out, n2, 0, "[PRESENT" as *u8) 95 let ir: i64 = mind_find(out, n2, 0, "[RELATIONSHIP STATE]" as *u8) 96 gv_check("present-precedes-relationship-in-the-composed-block" as *u8, (ip >= 0) & (ir > ip), ctr) 97 // bounded: the composer never overruns the caller's buffer and always NUL-terminates 98 gv_check("composed-block-is-bounded-and-terminated" as *u8, (n2 < MG_CAP) & (out[n2] == (0 as u8)), ctr) 99 100 return gv_verdict("GEN-MIND-GATE" as *u8, ctr, "CC0 done-rule: recall, relationship and present compose into the turn from the real promoted organs; an empty session contributes zero fabricated lines" as *u8) 101}