code wiki / (root) / nx_scenario_gate.nx

nx_scenario_gate.nx source

↩ module page · 120 lines · 5183 B

1// nx_scenario_gate.nx -- REFEREE for WRITING rung W-SCN-1 (nx_scenario). 2// 3// [T1] EXACT field extraction (kind / setting / goal) from a complete record. 4// [T2] completeness validation: complete -> 0 missing; record without a goal 5// key -> 1 missing (REJECT). 6// [T3] NEG-CONTROL whole-key matching: a record where the word "goal" appears 7// only inside a VALUE still counts goal as MISSING (no substring false hit). 8// [T4] BRIEF emit equals the expected one-line context string. 9// 10// Evidence -> stdout + knowledge/status/scenario_gate.log. Exit 0 GREEN / 1 RED. 11// Sovereign x86_64. license_tier: ORIGINAL 12import "nx_syscalls_x86_64.nx" 13import "nx_scenario.nx" 14 15func gp(logfd: i64, s: *u8) -> i64 { 16 var n: i64 = 0 17 while s[n] != (0 as u8) { n = n + 1 } 18 sys_write(1, s, n) 19 if logfd > 0 { sys_write(logfd, s, n) } 20 return 0 21} 22func gn(logfd: i64, v: i64) -> i64 { 23 var m: i64 = v 24 if m < 0 { 25 sys_write(1, "-\x00" as *u8, 1) 26 if logfd > 0 { sys_write(logfd, "-\x00" as *u8, 1) } 27 m = 0 - m 28 } 29 let bb: *u8 = sys_mmap(32) 30 let t: *u8 = sys_mmap(32) 31 var k: i64 = 0 32 if m == 0 { t[0] = 48 as u8; k = 1 } 33 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 34 var i: i64 = 0 35 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 36 sys_write(1, bb, k) 37 if logfd > 0 { sys_write(logfd, bb, k) } 38 return 0 39} 40func slen(s: *u8) -> i64 { 41 var n: i64 = 0 42 while s[n] != (0 as u8) { n = n + 1 } 43 return n 44} 45// 1 if null-terminated a equals null-terminated b 46func streq(a: *u8, b: *u8) -> i64 { 47 var i: i64 = 0 48 while a[i] != (0 as u8) { 49 if a[i] != b[i] { return 0 } 50 i = i + 1 51 } 52 if b[i] != (0 as u8) { return 0 } 53 return 1 54} 55func pr_kv(logfd: i64, label: *u8, got: i64, exp: i64) -> i64 { 56 gp(logfd, label) 57 gp(logfd, " got=\x00" as *u8); gn(logfd, got) 58 gp(logfd, " exp=\x00" as *u8); gn(logfd, exp) 59 if got == exp { gp(logfd, " OK\n\x00" as *u8); return 1 } 60 gp(logfd, " FAIL\n\x00" as *u8) 61 return 0 62} 63func pr_streq(logfd: i64, label: *u8, got: *u8, exp: *u8) -> i64 { 64 gp(logfd, label) 65 gp(logfd, " got=\"\x00" as *u8); gp(logfd, got); gp(logfd, "\"\x00" as *u8) 66 if streq(got, exp) == 1 { gp(logfd, " OK\n\x00" as *u8); return 1 } 67 gp(logfd, " FAIL\n\x00" as *u8) 68 return 0 69} 70 71func main() -> i64 { 72 let logfd: i64 = sys_openat_append("knowledge/status/scenario_gate.log\x00" as *u8, 0x1a4) 73 gp(logfd, "SCENARIO-GATE W-SCN-1 (parse / validate / extract / brief)\n\x00" as *u8) 74 75 let buf: *u8 = sys_mmap(512) 76 var ok: i64 = 1 77 78 // complete language-lesson scenario 79 let rec: *u8 = "kind: language\nsetting: a small cafe in Lyon\nroles: learner, waiter\ngoal: order lunch and ask for a recommendation\nconstraints: polite register; practice past tense\nlevel: A2\ntopic: food and dining\n\x00" as *u8 80 let rn: i64 = slen(rec) 81 82 // ---- T1: exact field extraction ---- 83 gp(logfd, " [T1] exact field extraction\n\x00" as *u8) 84 sc_copy_field(rec, rn, "kind\x00" as *u8, buf, 512) 85 if pr_streq(logfd, " kind\x00" as *u8, buf, "language\x00" as *u8) == 0 { ok = 0 } 86 sc_copy_field(rec, rn, "setting\x00" as *u8, buf, 512) 87 if pr_streq(logfd, " setting\x00" as *u8, buf, "a small cafe in Lyon\x00" as *u8) == 0 { ok = 0 } 88 sc_copy_field(rec, rn, "goal\x00" as *u8, buf, 512) 89 if pr_streq(logfd, " goal\x00" as *u8, buf, "order lunch and ask for a recommendation\x00" as *u8) == 0 { ok = 0 } 90 91 // ---- T2: completeness validation ---- 92 gp(logfd, " [T2] completeness validation\n\x00" as *u8) 93 if pr_kv(logfd, " complete record missing-count\x00" as *u8, sc_validate(rec, rn), 0) == 0 { ok = 0 } 94 let recng: *u8 = "kind: story\nsetting: a stadium\nroles: striker, keeper\n\x00" as *u8 95 if pr_kv(logfd, " record w/o goal missing-count\x00" as *u8, sc_validate(recng, slen(recng)), 1) == 0 { ok = 0 } 96 97 // ---- T3: NEG-CONTROL whole-key matching ---- 98 gp(logfd, " [T3] NEG-CONTROL: \"goal\" inside a value is NOT the goal key\n\x00" as *u8) 99 let recfm: *u8 = "kind: story\nsetting: a stadium\nroles: striker, keeper\nconstraints: the goal is to win\n\x00" as *u8 100 let fn: i64 = slen(recfm) 101 if pr_kv(logfd, " sc_has(goal)\x00" as *u8, sc_has(recfm, fn, "goal\x00" as *u8), 0) == 0 { ok = 0 } 102 if pr_kv(logfd, " missing-count (goal still missing)\x00" as *u8, sc_validate(recfm, fn), 1) == 0 { ok = 0 } 103 if pr_kv(logfd, " sc_has(constraints)\x00" as *u8, sc_has(recfm, fn, "constraints\x00" as *u8), 1) == 0 { ok = 0 } 104 105 // ---- T4: brief emit ---- 106 gp(logfd, " [T4] brief emit\n\x00" as *u8) 107 sc_emit_brief(rec, rn, buf, 512) 108 if pr_streq(logfd, " brief\x00" as *u8, buf, "language | a small cafe in Lyon | goal: order lunch and ask for a recommendation\x00" as *u8) == 0 { ok = 0 } 109 110 if ok == 1 { 111 gp(logfd, "SCENARIO-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8) 112 if logfd > 0 { sys_close(logfd) } 113 sys_exit(0) 114 return 0 115 } 116 gp(logfd, "SCENARIO-GATE result=FAIL verdict=RED\n\x00" as *u8) 117 if logfd > 0 { sys_close(logfd) } 118 sys_exit(1) 119 return 1 120}