code wiki / _hdl_build / nx_tutor_brief.nx
nx_tutor_brief.nx source
↩ module page · 146 lines · 6443 B
1// nx_tutor_brief.nx -- the TUTOR-VISIT CONTEXT PACK (token-efficiency organ):
2// a Claude session's dominant token cost is RE-ORIENTING (re-exploring the
3// repo, re-deriving landmines, re-finding the gate). This organ pre-builds
4// the visit: ONE landmine section (from the data registry) + ONE BRIEF block
5// per NOVEL queue row (id, advisory w, owner, deps, gate marker, title).
6// A session starts by reading the brief file -- not by exploring.
7// Batching is built in: ALL raised hands in one document = one amortized
8// visit (the E2 card's mechanism).
9// argv[1] = queue path override, argv[2] = landmines override,
10// argv[3] = out path override (default knowledge/status/tutor_briefs.log,
11// REWRITTEN each run -- briefs are derived state, not history).
12// license_tier: ORIGINAL
13import "nx_syscalls.nx"
14import "nx_assign_core.nx"
15const K_MAGIC_262144: i64 = 262144
16const K_MAGIC_262128: i64 = 262128
17const K_MAGIC_65536: i64 = 65536
18const K_MAGIC_65535: i64 = 65535
19
20func tbf_w(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 }
21func tbf_wn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m; sys_write(fd,"-" as *u8,1)}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(fd,bb,k); return 0 }
22
23// X-XL-004 Q+A join: the LAST channel ANSWER row for this id becomes the brief's
24// route line -- tutor knowledge rides the brief pack with no tutor present.
25// Graceful: a missing channel emits no route lines and never reddens the briefs.
26func tbf_route(fd: i64, tin: *u8, tn: i64, id: *u8) -> i64 {
27 var idl: i64 = 0
28 while id[idl] != (0 as u8) { idl = idl + 1 }
29 if idl == 0 { return 0 }
30 var pa: i64 = 0 - 1
31 var pb: i64 = 0 - 1
32 var i: i64 = 0
33 while i < tn {
34 var le: i64 = i
35 var s: i64 = 1
36 while s == 1 { if le >= tn { s = 0 } else { if tin[le] == (10 as u8) { s = 0 } else { le = le + 1 } } }
37 // "\tANSWER\t" = tab A N S W E R tab; id field follows, then a tab
38 var ap: i64 = 0 - 1
39 var j: i64 = i
40 while j + 7 < le {
41 if tin[j] == (9 as u8) { if tin[j+1] == (65 as u8) { if tin[j+6] == (82 as u8) { if tin[j+7] == (9 as u8) { ap = j + 8; j = le } } } }
42 j = j + 1
43 }
44 if ap >= 0 {
45 if ap + idl < le {
46 var k: i64 = 0
47 var hit: i64 = 1
48 while k < idl { if tin[ap+k] != id[k] { hit = 0; k = idl } else { k = k + 1 } }
49 if hit == 1 { if tin[ap+idl] == (9 as u8) { pa = ap + idl + 1; pb = le } }
50 }
51 }
52 i = le + 1
53 }
54 if pa >= 0 {
55 tbf_w(fd, " route: " as *u8)
56 let base: i64 = tin as i64
57 sys_write(fd, (base + pa) as *u8, pb - pa)
58 tbf_w(fd, "\n" as *u8)
59 return 1
60 }
61 return 0
62}
63
64func main(argc: i64, argv: *i64) -> i64 {
65 var qp: *u8 = "knowledge/registry/assignment_queue.tsv" as *u8
66 var lp: *u8 = "knowledge/registry/landmines.tsv" as *u8
67 var op: *u8 = "knowledge/status/tutor_briefs.log" as *u8
68 if argc >= 2 { qp = argv[1] as *u8 }
69 if argc >= 3 { lp = argv[2] as *u8 }
70 if argc >= 4 { op = argv[3] as *u8 }
71 let cx: *i64 = an_newcx()
72 let rows: i64 = an_load(qp, cx)
73 if rows <= 0 { tbf_w(1, "TUTORBRIEF verdict=RED reason=queue-missing\n" as *u8); sys_exit(2) }
74 // channel ANSWERs for the Q+A join (graceful absence)
75 let tinb: *u8 = sys_mmap(K_MAGIC_262144)
76 var tinn: i64 = 0
77 let tf: i64 = sys_openat_rd("knowledge/channel/tutor_in.tsv" as *u8)
78 if tf >= 0 {
79 var tr: i64 = sys_read(tf, tinb, K_MAGIC_262128)
80 while tr > 0 { tinn = tinn + tr; let tbase: i64 = tinb as i64; tr = sys_read(tf, (tbase + tinn) as *u8, K_MAGIC_262128 - tinn) }
81 sys_close(tf)
82 }
83 // REWRITTEN-each-run output: sibling tmp + renameat (openat_wr does NOT truncate
84 // -- the latent stale-tail bug, fixed 2026-06-12 under the RELOOP law)
85 let tmpp: *u8 = sys_mmap(512)
86 var to: i64 = 0
87 var ti2: i64 = 0
88 while op[ti2] != (0 as u8) { tmpp[ti2] = op[ti2]; ti2 = ti2 + 1 }
89 tmpp[ti2] = 46 as u8
90 tmpp[ti2+1] = 116 as u8
91 tmpp[ti2+2] = 109 as u8
92 tmpp[ti2+3] = 112 as u8
93 tmpp[ti2+4] = 0 as u8
94 let fd: i64 = sys_openat_wr(tmpp, 0x1a4)
95 if fd < 0 { tbf_w(1, "TUTORBRIEF verdict=RED reason=out-unwritable\n" as *u8); sys_exit(2) }
96 tbf_w(fd, "# tutor_briefs -- GENERATED by nx_tutor_brief; a session reads THIS, not the repo.\n" as *u8)
97 tbf_w(fd, "# Protocol: work the briefs below, flip statuses on gate markers, append a\n" as *u8)
98 tbf_w(fd, "# TOKENSPEND row (epoch, rows_closed, caps) to knowledge/status/token_spend.log.\n" as *u8)
99 // landmine section: echo the registry verbatim (it IS the one-page armor)
100 tbf_w(fd, "== LANDMINES (knowledge/registry/landmines.tsv) ==\n" as *u8)
101 let lb: *u8 = sys_mmap(K_MAGIC_65536)
102 let lf: i64 = sys_openat_rd(lp)
103 var ln: i64 = 0
104 if lf >= 0 {
105 var r: i64 = sys_read(lf, lb, K_MAGIC_65535)
106 while r > 0 { ln = ln + r; r = sys_read(lf, lb + ln, K_MAGIC_65535 - ln) }
107 sys_close(lf)
108 sys_write(fd, lb, ln)
109 } else {
110 tbf_w(fd, "LANDMINES-MISSING (fail-loud: registry absent)\n" as *u8)
111 }
112 // one brief block per NOVEL row
113 let wv: *i64 = cx[3] as *i64
114 let st: *i64 = cx[4] as *i64
115 var nb: i64 = 0
116 var r2: i64 = 0
117 while r2 < rows {
118 if st[r2] == 78 { // 'N'
119 tbf_w(fd, "== BRIEF id=" as *u8)
120 tbf_w(fd, an_id_at(cx, r2))
121 tbf_w(fd, " w=" as *u8)
122 tbf_wn(fd, wv[r2])
123 tbf_w(fd, " deps=" as *u8)
124 tbf_w(fd, an_deps_at(cx, r2))
125 tbf_w(fd, " ==\n" as *u8)
126 tbf_w(fd, " gate: " as *u8)
127 tbf_w(fd, an_gate_at(cx, r2))
128 tbf_w(fd, "\n what: " as *u8)
129 tbf_w(fd, an_title_at(cx, r2))
130 tbf_w(fd, "\n" as *u8)
131 tbf_route(fd, tinb, tinn, an_id_at(cx, r2))
132 nb = nb + 1
133 }
134 r2 = r2 + 1
135 }
136 tbf_w(fd, "== END briefs=" as *u8)
137 tbf_wn(fd, nb)
138 tbf_w(fd, " ==\n" as *u8)
139 sys_close(fd)
140 sys_renameat(tmpp, op)
141 tbf_w(1, "TUTORBRIEF briefs=" as *u8)
142 tbf_wn(1, nb)
143 tbf_w(1, " out-ok verdict=GREEN\n" as *u8)
144 sys_exit(0)
145 return 0
146}