code wiki / _hdl_build / nx_drift_fix.nx
nx_drift_fix.nx source
↩ module page · 93 lines · 6174 B
1// nx_drift_fix.nx -- APPLY the verifiable dep-drift fix to a COPY (never the SSOT), so the CRITIC (nx_queue_health)
2// can CONFIRM the drift drops -- evidence a fix works, not a self-declaration. Operator 2026-07-04: "i only
3// believe when i see evidence; the critic decides if you got it fixed + you check in with it."
4//
5// THE VERIFIABLE FIX: a row whose DEPS field holds a STATUS-WORD (TODO/DONE/GEN/NOVEL/WIP/RED) is
6// definitionally mis-emitted (deps must be task-ids or "-"). The STATUS↔DEPS columns are swapped: swapping them
7// back makes deps = the old status (a real id that resolves, OR "-" for a root) and status = the status-word
8// (valid). This provably clears the 19 SWAP-class drifts. The 16 RENAME/REMOVED rows (deps looks like an id but
9// doesn't resolve) are NOT status-words -> NOT touched here (they need an owner REPOINT, not a swap).
10//
11// SAFE: reads the SSOT, writes a COPY (out path = argv[2], default assignment_queue.fixed.tsv). The SSOT is
12// NEVER modified -- the real edit stays council-gated (proposed wms-drift-fix, DENIED without quorum). This just
13// produces the fixed artifact so the critic can grade it + the owner/council can see the exact diff before
14// approving. license_tier: ORIGINAL lineage_id: nishi_drift_fix_v1 expect_exit: 0
15import "nx_syscalls.nx"
16
17const DF_BUF: i64 = 4194304
18
19func df_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 }
20func df_n(fd: i64, v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;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{b[i]=t[k-1-i];i=i+1} sys_write(fd,b,k); return 0 }
21
22// is buf[s,e) exactly one of the status-words? (the deps-field-holds-a-status-word test)
23func df_is_status_word(buf: *u8, s: i64, e: i64) -> i64 {
24 let n: i64 = e - s
25 if n == 4 { if buf[s]==(84 as u8) { if buf[s+1]==(79 as u8) { if buf[s+2]==(68 as u8) { if buf[s+3]==(79 as u8) { return 1 } } } } } // TODO
26 if n == 4 { if buf[s]==(68 as u8) { if buf[s+1]==(79 as u8) { if buf[s+2]==(78 as u8) { if buf[s+3]==(69 as u8) { return 1 } } } } } // DONE
27 if n == 3 { if buf[s]==(71 as u8) { if buf[s+1]==(69 as u8) { if buf[s+2]==(78 as u8) { return 1 } } } } // GEN
28 if n == 5 { if buf[s]==(78 as u8) { if buf[s+1]==(79 as u8) { if buf[s+2]==(86 as u8) { if buf[s+3]==(69 as u8) { if buf[s+4]==(76 as u8) { return 1 } } } } } } // NOVEL
29 if n == 3 { if buf[s]==(87 as u8) { if buf[s+1]==(73 as u8) { if buf[s+2]==(80 as u8) { return 1 } } } } // WIP
30 if n == 3 { if buf[s]==(82 as u8) { if buf[s+1]==(69 as u8) { if buf[s+2]==(68 as u8) { return 1 } } } } // RED
31 return 0
32}
33
34func main(argc: i64, argv: *i64) -> i64 {
35 var inp: *u8 = "knowledge/registry/assignment_queue.tsv\x00" as *u8
36 var outp: *u8 = "knowledge/registry/assignment_queue.fixed.tsv\x00" as *u8
37 if argc >= 2 { inp = argv[1] as *u8 }
38 if argc >= 3 { outp = argv[2] as *u8 }
39
40 let fd: i64 = sys_openat_rd(inp)
41 if fd < 0 { df_w(1, "drift-fix: cannot read input\n\x00" as *u8); sys_exit(1); return 1 }
42 let buf: *u8 = sys_mmap(DF_BUF)
43 var total: i64 = 0; var go: i64 = 1
44 while go == 1 { let r: i64 = sys_read(fd, (buf as i64 + total) as *u8, DF_BUF - total); if r <= 0 { go = 0 } else { total = total + r; if total >= DF_BUF { go = 0 } } }
45 sys_close(fd)
46
47 let ofd: i64 = sys_openat_wr(outp, 0x1a4)
48 if ofd < 0 { df_w(1, "drift-fix: cannot write output\n\x00" as *u8); sys_exit(1); return 1 }
49
50 var swaps: i64 = 0
51 var line: i64 = 0
52 while line < total {
53 // find line end
54 var eol: i64 = line
55 while eol < total { if buf[eol] == (10 as u8) { break } eol = eol + 1 }
56 // comment or too-short line -> copy verbatim (incl the newline if present)
57 if buf[line] == (35 as u8) {
58 var c: i64 = line; while c <= eol { if c < total { sys_write(ofd, (buf as i64 + c) as *u8, 1) } c = c + 1 }
59 line = eol + 1; continue
60 }
61 // find the first 7 tab positions within [line, eol)
62 let tab: *i64 = sys_mmap(8 * 12) as *i64
63 var nt: i64 = 0; var i: i64 = line
64 while i < eol { if buf[i] == (9 as u8) { if nt < 8 { tab[nt] = i; nt = nt + 1 } } i = i + 1 }
65 // need at least 7 tabs (>=8 fields) to have status(f5) + deps(f6)
66 if nt < 7 {
67 var c: i64 = line; while c <= eol { if c < total { sys_write(ofd, (buf as i64 + c) as *u8, 1) } c = c + 1 }
68 line = eol + 1; continue
69 }
70 // f5 = (tab[4]+1 .. tab[5]) ; f6 = (tab[5]+1 .. tab[6])
71 let f5s: i64 = tab[4] + 1; let f5e: i64 = tab[5]
72 let f6s: i64 = tab[5] + 1; let f6e: i64 = tab[6]
73 if df_is_status_word(buf, f6s, f6e) == 1 {
74 // SWAP: write [line .. tab[4]+1) + f6 + \t + f5 + [tab[6] .. eol]
75 sys_write(ofd, (buf as i64 + line) as *u8, tab[4] + 1 - line) // "f0\t..f4\t"
76 sys_write(ofd, (buf as i64 + f6s) as *u8, f6e - f6s) // old deps (the id / "-")
77 sys_write(ofd, "\t\x00" as *u8, 1)
78 sys_write(ofd, (buf as i64 + f5s) as *u8, f5e - f5s) // old status (the status-word)
79 var c: i64 = tab[6]; while c <= eol { if c < total { sys_write(ofd, (buf as i64 + c) as *u8, 1) } c = c + 1 } // "\tf7\tf8\n"
80 swaps = swaps + 1
81 } else {
82 var c: i64 = line; while c <= eol { if c < total { sys_write(ofd, (buf as i64 + c) as *u8, 1) } c = c + 1 }
83 }
84 line = eol + 1
85 }
86 sys_close(ofd)
87
88 df_w(1, "=== nx_drift_fix: status<->deps swap applied to a COPY (SSOT untouched) ===\n\x00" as *u8)
89 df_w(1, " rows swapped (deps held a status-word) = \x00" as *u8); df_n(1, swaps); df_w(1, "\n\x00" as *u8)
90 df_w(1, " fixed copy -> \x00" as *u8); df_w(1, outp); df_w(1, "\n\x00" as *u8)
91 df_w(1, " >> NOW CHECK IN WITH THE CRITIC: run nx_queue_health \x00" as *u8); df_w(1, outp); df_w(1, " -- it decides if the drift dropped.\n\x00" as *u8)
92 sys_exit(0); return 0
93}