code wiki / _hdl_build / nx_galx_sortserve_gate.nx
nx_galx_sortserve_gate.nx source
↩ module page · 122 lines · 8341 B
1// nx_galx_sortserve_gate.nx -- SOVEREIGN gate for the gallery's sort-by-key serve handlers (ss_emit_size /
2// ss_emit_runtime in nx_galx_sortindex.nx). Calls the ACTUAL handlers the daemon calls and asserts the
3// response BYTES -- no socket, no bash, no curl. Isolates into /tmp/ssg (chdir) so it NEVER touches the
4// real repo's knowledge/status data. Proves: size desc/asc order, discovery-offset (pct=) composition,
5// runtime desc/asc order, and the "building" fallback before the duration batch exists. license_tier: ORIGINAL
6import "nx_syscalls.nx"
7import "nx_galx_sortindex.nx"
8
9func g_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
10func g_n(v: i64) -> i64 {
11 let bb: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
12 let t: *u8 = sys_mmap(28); var k: i64 = 0; if m == 0 { t[0] = 48 as u8; k = 1 }
13 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
14 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0
15}
16func g_strlen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
17func g_writefile_n(path: *u8, n: i64) -> i64 {
18 let fd: i64 = sys_openat_wr(path, 0x1a4); if fd < 0 { return 0 - 1 }
19 let buf: *u8 = sys_mmap(n + 16); var i: i64 = 0; while i < n { buf[i] = 65 as u8; i = i + 1 }
20 sys_write(fd, buf, n); sys_close(fd); return 0
21}
22func g_writestr(path: *u8, s: *u8) -> i64 {
23 let fd: i64 = sys_openat_wr(path, 0x1a4); if fd < 0 { return 0 - 1 }
24 sys_write(fd, s, g_strlen(s)); sys_close(fd); return 0
25}
26func g_contains(hay: *u8, hlen: i64, needle: *u8) -> i64 {
27 let nl: i64 = g_strlen(needle)
28 if nl == 0 { return 1 }
29 var i: i64 = 0
30 while i + nl <= hlen {
31 var j: i64 = 0; var ok: i64 = 1
32 while j < nl { if hay[i + j] != needle[j] { ok = 0; j = nl } else { j = j + 1 } }
33 if ok == 1 { return 1 }
34 i = i + 1
35 }
36 return 0
37}
38func g_assert(label: *u8, hay: *u8, hlen: i64, needle: *u8, pass: *i64, fail: *i64) -> i64 {
39 if g_contains(hay, hlen, needle) == 1 { pass[0] = pass[0] + 1; g_p(" ok " as *u8); g_p(label); g_p("\n" as *u8) }
40 else { fail[0] = fail[0] + 1; g_p(" FAIL " as *u8); g_p(label); g_p(" resp=" as *u8); sys_write(1, hay, hlen); g_p("\n" as *u8) }
41 return 0
42}
43
44func main(argc: i64, argv: *i64) -> i64 {
45 let pass: *i64 = sys_mmap(16) as *i64; pass[0] = 0
46 let fail: *i64 = sys_mmap(16) as *i64; fail[0] = 0
47 // isolate into a UNIQUE scratch dir per run (mono-clock-seeded) so we NEVER touch the real repo data AND
48 // never inherit a stale index/galx_dur.raw from a prior run (/tmp is not reliably cleared). 0x1ed = 0755.
49 let ts: *i64 = sys_mmap(16) as *i64; sys_clock_gettime_mono(ts)
50 var seed: i64 = ts[1]; if seed < 0 { seed = 0 - seed }
51 let dir: *u8 = sys_mmap(64); var dl: i64 = si_cat(dir, 0, "/tmp/ssg_" as *u8); dl = si_u(dir, dl, seed); dir[dl] = 0 as u8
52 sys_mkdir(dir, 0x1ed)
53 let d2: *u8 = sys_mmap(96); var l2: i64 = si_cat(d2, 0, dir); l2 = si_cat(d2, l2, "/knowledge" as *u8); d2[l2] = 0 as u8
54 sys_mkdir(d2, 0x1ed)
55 let d3: *u8 = sys_mmap(96); var l3: i64 = si_cat(d3, 0, dir); l3 = si_cat(d3, l3, "/knowledge/status" as *u8); d3[l3] = 0 as u8
56 sys_mkdir(d3, 0x1ed)
57 if sys_chdir(dir) != 0 { g_p("FAIL chdir\n" as *u8); sys_exit(1) }
58
59 // 4 videos: distinct sizes AND basenames whose alphabetical order (a,k,m,z) != id order, so the NAME test
60 // discriminates a real sort from a no-op. Sizes stay per-id so the size/runtime asserts are unchanged.
61 g_writefile_n("m_movie.bin" as *u8, 5000) // id0
62 g_writefile_n("z_movie.bin" as *u8, 1000) // id1
63 g_writefile_n("a_movie.bin" as *u8, 9000) // id2
64 g_writefile_n("k_movie.bin" as *u8, 3000) // id3
65 g_writestr("knowledge/status/galx_vid_paths.tsv" as *u8, "m_movie.bin\nz_movie.bin\na_movie.bin\nk_movie.bin\n" as *u8)
66
67 let rbuf: *u8 = sys_mmap(65536)
68 let req0: *u8 = sys_mmap(64); let rn0: i64 = si_cat(req0, 0, "x" as *u8)
69 let reqp: *u8 = sys_mmap(64); let rnp: i64 = si_cat(reqp, 0, "pct=50" as *u8)
70
71 // SIZE: A=5000/0 B=1000/1 C=9000/2 D=3000/3
72 var L: i64 = ss_emit_size(rbuf, req0, rn0, 114, 1, 0, 10)
73 g_assert("size-desc (largest first 2,0,3,1)" as *u8, rbuf, L, "\"items\":[\"2\",\"0\",\"3\",\"1\"]" as *u8, pass, fail)
74 L = ss_emit_size(rbuf, req0, rn0, 114, 0, 0, 10)
75 g_assert("size-asc (smallest first 1,3,0,2)" as *u8, rbuf, L, "\"items\":[\"1\",\"3\",\"0\",\"2\"]" as *u8, pass, fail)
76 L = ss_emit_size(rbuf, reqp, rnp, 114, 1, 0, 10)
77 g_assert("size-desc + pct=50 -> middle 3,1" as *u8, rbuf, L, "\"items\":[\"3\",\"1\"]" as *u8, pass, fail)
78
79 // R3 HIDE: hide id2 -> rebuild excludes it (size-desc 2,0,3,1 -> 0,3,1, total 3); UNHIDE -> restored (reversible).
80 g_writestr("knowledge/status/galx_hidden.log" as *u8, "2 1\n" as *u8)
81 L = ss_emit_size(rbuf, req0, rn0, 114, 1, 0, 10)
82 g_assert("hide id2 -> size-desc 0,3,1" as *u8, rbuf, L, "\"items\":[\"0\",\"3\",\"1\"]" as *u8, pass, fail)
83 g_assert("hide id2 -> total 3" as *u8, rbuf, L, "\"total\":3" as *u8, pass, fail)
84 g_writestr("knowledge/status/galx_hidden.log" as *u8, "2 1\n2 0\n" as *u8)
85 L = ss_emit_size(rbuf, req0, rn0, 114, 1, 0, 10)
86 g_assert("unhide id2 -> size-desc 2,0,3,1" as *u8, rbuf, L, "\"items\":[\"2\",\"0\",\"3\",\"1\"]" as *u8, pass, fail)
87
88 // RUNTIME: building fallback before the duration batch exists
89 L = ss_emit_runtime(rbuf, req0, rn0, 114, 1, 0, 10)
90 g_assert("runtime-building (no galx_dur.raw)" as *u8, rbuf, L, "\"building\":1" as *u8, pass, fail)
91 // drop the duration batch in the REAL nx_ts_dur format ("size=.. first_pcr90=.. last_pcr90=.. dur_ms=..").
92 // sizes (5000,99999,100,50000) are ADVERSARIAL: ordered DIFFERENTLY than the durations, so if the parser
93 // regressed to first-int (=size) these asserts would fail. durations id0=120000 id1=30000 id2=300000 id3=90000.
94 g_writestr("knowledge/status/galx_dur.raw" as *u8, "size=5000 first_pcr90=0 last_pcr90=10800000 dur_ms=120000\nsize=99999 first_pcr90=0 last_pcr90=2700000 dur_ms=30000\nsize=100 first_pcr90=0 last_pcr90=27000000 dur_ms=300000\nsize=50000 first_pcr90=0 last_pcr90=8100000 dur_ms=90000\n" as *u8)
95 L = ss_emit_runtime(rbuf, req0, rn0, 114, 1, 0, 10)
96 g_assert("runtime-desc (longest first 2,0,3,1)" as *u8, rbuf, L, "\"items\":[\"2\",\"0\",\"3\",\"1\"]" as *u8, pass, fail)
97 L = ss_emit_runtime(rbuf, req0, rn0, 114, 0, 0, 10)
98 g_assert("runtime-asc (shortest first 1,3,0,2)" as *u8, rbuf, L, "\"items\":[\"1\",\"3\",\"0\",\"2\"]" as *u8, pass, fail)
99
100 // NAME: basenames a_movie(id2) k_movie(id3) m_movie(id0) z_movie(id1) -> asc 2,3,0,1 ; desc 1,0,3,2
101 L = ss_emit_name(rbuf, req0, rn0, 114, 0, 0, 10)
102 g_assert("name-asc (A->Z 2,3,0,1)" as *u8, rbuf, L, "\"items\":[\"2\",\"3\",\"0\",\"1\"]" as *u8, pass, fail)
103 L = ss_emit_name(rbuf, req0, rn0, 114, 1, 0, 10)
104 g_assert("name-desc (Z->A 1,0,3,2)" as *u8, rbuf, L, "\"items\":[\"1\",\"0\",\"3\",\"2\"]" as *u8, pass, fail)
105
106 // R3 cross-key hide: hiding id0 excludes it from ALL of size/runtime/name (each desc/asc full was [2,0,3,1]/
107 // [2,0,3,1]/[2,3,0,1] -> without id0 all become [2,3,1]); then unhide restores it.
108 g_writestr("knowledge/status/galx_hidden.log" as *u8, "0 1\n" as *u8)
109 L = ss_emit_size(rbuf, req0, rn0, 114, 1, 0, 10)
110 g_assert("hide id0 -> size 2,3,1" as *u8, rbuf, L, "\"items\":[\"2\",\"3\",\"1\"]" as *u8, pass, fail)
111 L = ss_emit_runtime(rbuf, req0, rn0, 114, 1, 0, 10)
112 g_assert("hide id0 -> runtime 2,3,1" as *u8, rbuf, L, "\"items\":[\"2\",\"3\",\"1\"]" as *u8, pass, fail)
113 L = ss_emit_name(rbuf, req0, rn0, 114, 0, 0, 10)
114 g_assert("hide id0 -> name 2,3,1" as *u8, rbuf, L, "\"items\":[\"2\",\"3\",\"1\"]" as *u8, pass, fail)
115 g_writestr("knowledge/status/galx_hidden.log" as *u8, "0 1\n0 0\n" as *u8)
116 L = ss_emit_size(rbuf, req0, rn0, 114, 1, 0, 10)
117 g_assert("unhide id0 -> size 2,0,3,1" as *u8, rbuf, L, "\"items\":[\"2\",\"0\",\"3\",\"1\"]" as *u8, pass, fail)
118
119 g_p("\nsortserve gate pass=" as *u8); g_n(pass[0]); g_p(" fail=" as *u8); g_n(fail[0]); g_p("\n" as *u8)
120 if fail[0] == 0 { g_p("GREEN\n" as *u8); sys_exit(0) } else { g_p("RED\n" as *u8); sys_exit(1) }
121 return 0
122}