code wiki / _hdl_build / nx_worklog_ingest_gate.nx

nx_worklog_ingest_gate.nx source

↩ module page · 117 lines · 6438 B

1import "nx_gate_gn.nx" 2// nx_worklog_ingest_gate.nx -- hermetic gate for the transcript ingestor. Builds a fixture .jsonl 3// (two real tool_use lines + a stop_reason:"tool_use" DECOY + a deliberately INCOMPLETE trailing 4// line) and proves the load-bearing properties mechanically: 5// T1 captures exactly the complete tool_use actions (granular, decoy excluded, partial line held). 6// T2 tool + target extracted correctly from input (file_path / command). 7// T3 (NEG/idempotence) a second run captures 0 -- the cursor prevents duplicate lines (this is what 8// lets PostToolUse + Stop + Cron all fire safely). THE load-bearing test. 9// T4 completing the partial line -> next run captures exactly it (incremental, line-boundary cursor). 10// T5 (NEG) the decoy ("tool_use" only as a stop_reason / bare value) is NEVER captured. 11// Sovereign: imports nx_syscalls + the ingest lib only. license_tier: ORIGINAL 12import "nx_syscalls.nx" 13import "nx_worklog_ingest_lib.nx" 14 15const GDIR: *u8 = "/tmp/nx_wli_gate" 16const GT: *u8 = "/tmp/nx_wli_gate/transcript.jsonl" 17const GC: *u8 = "/tmp/nx_wli_gate/cursor.cur" 18const GW: *u8 = "/tmp/nx_wli_gate/worklog.tsv" 19 20func gp(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 21func gstrlen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 22 23func gtrunc(path: *u8, content: *u8) -> i64 { 24 let fd: i64 = sys_openat_wr(path, 420) 25 if fd < 0 { return 0 - 1 } 26 let n: i64 = gstrlen(content) 27 if n > 0 { sys_write(fd, content, n) } 28 sys_close(fd) 29 return 0 30} 31func gappend(path: *u8, content: *u8) -> i64 { 32 let fd: i64 = sys_openat_append(path, 420) 33 if fd < 0 { return 0 - 1 } 34 sys_write(fd, content, gstrlen(content)) 35 sys_close(fd) 36 return 0 37} 38func gread(path: *u8, buf: *u8, cap: i64) -> i64 { 39 let fd: i64 = sys_openat_rd(path) 40 if fd < 0 { return 0 - 1 } 41 var total: i64 = 0 42 var nrd: i64 = sys_read(fd, buf, cap) 43 while nrd > 0 { total = total + nrd; if total >= cap { nrd = 0 } else { nrd = sys_read(fd, ((buf as i64) + total) as *u8, cap - total) } } 44 sys_close(fd) 45 return total 46} 47func gfind(buf: *u8, n: i64, pat: *u8) -> i64 { 48 var pl: i64 = 0; while pat[pl] != (0 as u8) { pl = pl + 1 } 49 if pl == 0 { return 0 - 1 } 50 var i: i64 = 0 51 while i + pl <= n { 52 var j: i64 = 0; var ok: i64 = 1 53 while j < pl { if buf[i + j] != pat[j] { ok = 0; j = pl } else { j = j + 1 } } 54 if ok == 1 { return i } 55 i = i + 1 56 } 57 return 0 - 1 58} 59func gcount(buf: *u8, n: i64, c: i64) -> i64 { var cnt: i64 = 0; var i: i64 = 0; while i < n { if (buf[i] as i64) == c { cnt = cnt + 1 } i = i + 1 } return cnt } 60 61func main(argc: i64, argv: *i64) -> i64 { 62 gp("=== nx_worklog_ingest_gate (transcript capture + idempotent cursor) ===\n" as *u8) 63 sys_mkdir(GDIR, 511) 64 65 // clean slate: empty worklog + empty cursor (absent/empty -> offset 0). 66 gtrunc(GW, "" as *u8) 67 gtrunc(GC, "" as *u8) 68 69 // fixture: two real tool_use lines, a decoy, then an INCOMPLETE 4th line (no newline). 70 gtrunc(GT, "{\"message\":{\"content\":[{\"type\":\"tool_use\",\"id\":\"toolu_a\",\"name\":\"Read\",\"input\":{\"file_path\":\"/tmp/foo.md\"}}]},\"type\":\"assistant\",\"timestamp\":\"t1\"}\n" as *u8) 71 gappend(GT, "{\"message\":{\"content\":[{\"type\":\"tool_use\",\"id\":\"toolu_b\",\"name\":\"Bash\",\"input\":{\"command\":\"echo hi\",\"description\":\"d\"}}]},\"type\":\"assistant\",\"timestamp\":\"t2\"}\n" as *u8) 72 gappend(GT, "{\"message\":{\"role\":\"assistant\",\"stop_reason\":\"tool_use\",\"content\":[{\"type\":\"text\",\"text\":\"no tool here\"}]},\"type\":\"assistant\",\"timestamp\":\"t3\"}\n" as *u8) 73 gappend(GT, "{\"message\":{\"content\":[{\"type\":\"tool_use\",\"id\":\"toolu_d\",\"name\":\"Write\"," as *u8) 74 75 var pass: i64 = 0 76 var fail: i64 = 0 77 78 let wbuf: *u8 = sys_mmap(65536) 79 80 // T1: ingest captures exactly the 2 complete tool_use actions (decoy + partial excluded). 81 let c1: i64 = wli_ingest(GT, GC, GW) 82 if c1 == 2 { pass = pass + 1; gp(" T1 capture-count==2 PASS\n" as *u8) } else { fail = fail + 1; gp(" T1 capture-count FAIL got=" as *u8); gn(c1); gp("\n" as *u8) } 83 84 // T2: tool + target extracted from input (file_path for Read, command for Bash). 85 let w1: i64 = gread(GW, wbuf, 65536) 86 var t2: i64 = 1 87 if gfind(wbuf, w1, "Read" as *u8) < 0 { t2 = 0 } 88 if gfind(wbuf, w1, "/tmp/foo.md" as *u8) < 0 { t2 = 0 } 89 if gfind(wbuf, w1, "Bash" as *u8) < 0 { t2 = 0 } 90 if gfind(wbuf, w1, "echo hi" as *u8) < 0 { t2 = 0 } 91 if gfind(wbuf, w1, "ingest" as *u8) < 0 { t2 = 0 } 92 if t2 == 1 { pass = pass + 1; gp(" T2 tool+target-extracted PASS\n" as *u8) } else { fail = fail + 1; gp(" T2 extraction FAIL\n" as *u8) } 93 94 // T3 (NEG / idempotence): a second run captures NOTHING (cursor) and the log is unchanged. 95 let nl1: i64 = gcount(wbuf, w1, 10) 96 let c2: i64 = wli_ingest(GT, GC, GW) 97 let w2: i64 = gread(GW, wbuf, 65536) 98 let nl2: i64 = gcount(wbuf, w2, 10) 99 if c2 == 0 { if nl2 == nl1 { pass = pass + 1; gp(" T3 idempotent-second-run(0) PASS\n" as *u8) } else { fail = fail + 1; gp(" T3 log-grew FAIL\n" as *u8) } } else { fail = fail + 1; gp(" T3 re-captured FAIL got=" as *u8); gn(c2); gp("\n" as *u8) } 100 101 // T4: complete the partial line -> next run captures exactly it (incremental, line-boundary). 102 gappend(GT, "\"input\":{\"file_path\":\"/tmp/bar.md\"}}]},\"type\":\"assistant\",\"timestamp\":\"t4\"}\n" as *u8) 103 let c3: i64 = wli_ingest(GT, GC, GW) 104 let w3: i64 = gread(GW, wbuf, 65536) 105 var t4: i64 = 1 106 if c3 != 1 { t4 = 0 } 107 if gfind(wbuf, w3, "Write" as *u8) < 0 { t4 = 0 } 108 if gfind(wbuf, w3, "/tmp/bar.md" as *u8) < 0 { t4 = 0 } 109 if t4 == 1 { pass = pass + 1; gp(" T4 incremental-capture(Write) PASS\n" as *u8) } else { fail = fail + 1; gp(" T4 incremental FAIL c3=" as *u8); gn(c3); gp("\n" as *u8) } 110 111 // T5 (NEG): the decoy is never captured (anchor is "type":"tool_use", not the bare value). 112 if gfind(wbuf, w3, "no tool here" as *u8) < 0 { pass = pass + 1; gp(" T5 decoy-never-captured PASS\n" as *u8) } else { fail = fail + 1; gp(" T5 decoy-leaked FAIL\n" as *u8) } 113 114 gp("RESULT pass=" as *u8); gn(pass); gp(" fail=" as *u8); gn(fail) 115 if fail == 0 { gp(" verdict=GREEN\n" as *u8); sys_exit(0); return 0 } 116 gp(" verdict=RED\n" as *u8); sys_exit(1); return 1 117}