nx_uxf_extract_avro.nx source
↩ module page · 111 lines · 6126 B
1// nx_uxf_extract_avro.nx -- UXF arc R1b-2: the sovereign EXTRACT pass over the
2// decoupling source. Chains proven organs end-to-end:
3// read R1a raw -> split header/body at CRLFCRLF -> nx_http_dechunk
4// -> nx_html_to_text_x -> pattern-extract Law-4 (data<->functionality
5// decoupling) signal (schema / evolution / reader / writer / compatib* / Avro).
6//
7// This is the researcher's MECHANICAL fact-extraction -- substring/count work the
8// team owns -- NOT semantic NLP (the honest "last sliver"). Robust by construction:
9// - if nx_http_dechunk fails, FALL BACK to the body as-is (it may already be
10// dechunked by nx_https_get_complete) -- we do not assume the wire framing.
11// - if the core terms are absent (e.g. gzip-encoded body), verdict=RED, no fake green.
12// Pattern helpers inlined as ux_* (mirror nx_research_extract's re_*) to stay
13// runtime/-local until the cross-dir import is confirmed (Rule 15 refactor later).
14// No hardware/persistent writes (Rule 26). expect_exit: 0 license_tier: ORIGINAL
15import "nx_syscalls.nx"
16import "nx_http_dechunk.nx"
17import "nx_html_to_text.nx"
18const K_MAGIC_4194304: i64 = 4194304
19
20func ux_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
21func ux_putn(v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 - m; sys_write(1, "-" as *u8, 1) } 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 { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0 }
22func ux_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
23
24// count non-overlapping occurrences of term in text[0..n) (mirrors re_count)
25func ux_count(text: *u8, n: i64, term: *u8) -> i64 {
26 let m: i64 = ux_slen(term)
27 if m == 0 { return 0 }
28 var c: i64 = 0
29 var i: i64 = 0
30 while (i + m) <= n {
31 var j: i64 = 0
32 var ok: i64 = 1
33 while j < m { if text[i + j] != term[j] { ok = 0; j = m } else { j = j + 1 } }
34 if ok == 1 { c = c + 1; i = i + m } else { i = i + 1 }
35 }
36 return c
37}
38
39// start of the HTTP body: byte after the first CRLFCRLF, or (0-1)
40func ux_body_off(raw: *u8, n: i64) -> i64 {
41 var i: i64 = 0
42 while (i + 4) <= n {
43 if raw[i] == (0x0D as u8) { if raw[i+1] == (0x0A as u8) { if raw[i+2] == (0x0D as u8) { if raw[i+3] == (0x0A as u8) { return i + 4 } } } }
44 i = i + 1
45 }
46 return 0 - 1
47}
48
49func ux_signal(text: *u8, n: i64, label: *u8, term: *u8) -> i64 {
50 let c: i64 = ux_count(text, n, term)
51 ux_puts(" " as *u8); ux_puts(label); ux_puts(" = " as *u8); ux_putn(c); ux_puts("\n" as *u8)
52 return c
53}
54
55func main() -> i64 {
56 // ---- 1. read the R1a raw fetch ----
57 let lenp: *i64 = sys_mmap(8) as *i64
58 let raw: *u8 = sys_read_file("knowledge/fetched/avro_decoupling_sovereign.raw\x00", lenp)
59 if (raw as i64) == 0 { ux_puts("EXTRACT: raw not found -- run nx_uxf_fetch_avro first\n" as *u8); sys_exit(1); return 1 }
60 let rawlen: i64 = lenp[0]
61 if rawlen <= 0 { ux_puts("EXTRACT: raw empty\n" as *u8); sys_exit(1); return 1 }
62 ux_puts("R1b-2 EXTRACT over the Avro decoupling source\n raw_bytes = " as *u8); ux_putn(rawlen); ux_puts("\n" as *u8)
63
64 // ---- 2. split header/body at CRLFCRLF ----
65 let boff: i64 = ux_body_off(raw, rawlen)
66 if boff < 0 { ux_puts(" no CRLFCRLF split found\n" as *u8); sys_exit(2); return 2 }
67 let body: *u8 = ((raw as i64) + boff) as *u8
68 let bodylen: i64 = rawlen - boff
69 ux_puts(" body_off = " as *u8); ux_putn(boff); ux_puts(" body_bytes = " as *u8); ux_putn(bodylen); ux_puts("\n" as *u8)
70
71 // ---- 3. dechunk (BK=2), with fallback if already-dechunked ----
72 let html: *u8 = sys_mmap(K_MAGIC_4194304)
73 var srcptr: *u8 = html
74 var srclen: i64 = nx_http_dechunk(body, bodylen, html, K_MAGIC_4194304)
75 if srclen < 0 {
76 ux_puts(" dechunk ret=" as *u8); ux_putn(srclen); ux_puts(" -> using body as-is (already dechunked)\n" as *u8)
77 srcptr = body
78 srclen = bodylen
79 } else {
80 ux_puts(" dechunked_html_bytes = " as *u8); ux_putn(srclen); ux_puts("\n" as *u8)
81 }
82
83 // ---- 4. render HTML -> text ----
84 let text: *u8 = sys_mmap(K_MAGIC_4194304)
85 let textlen: i64 = nx_html_to_text_x(srcptr, srclen, text, K_MAGIC_4194304, 0)
86 ux_puts(" rendered_text_bytes = " as *u8); ux_putn(textlen); ux_puts("\n" as *u8)
87 if textlen <= 0 { ux_puts(" render produced no text\n" as *u8); sys_exit(4); return 4 }
88
89 // persist the rendered text for the verify/synth rung (additive, durable)
90 let ofd: i64 = sys_openat_wr("knowledge/fetched/avro_decoupling_text.txt\x00", 0x1A4)
91 if ofd > 0 { sys_write(ofd, text, textlen); sys_close(ofd) }
92
93 // ---- 5. extract Law-4 decoupling signal by pattern (mechanical, not semantic) ----
94 ux_puts(" DECOUPLING SIGNAL (pattern counts in the rendered article):\n" as *u8)
95 let s_schema: i64 = ux_signal(text, textlen, "schema " as *u8, "schema" as *u8)
96 let s_evolu: i64 = ux_signal(text, textlen, "evolution" as *u8, "evolution" as *u8)
97 let s_reader: i64 = ux_signal(text, textlen, "reader " as *u8, "reader" as *u8)
98 let s_writer: i64 = ux_signal(text, textlen, "writer " as *u8, "writer" as *u8)
99 let s_compat: i64 = ux_signal(text, textlen, "compatib " as *u8, "compatib" as *u8)
100 let s_avro: i64 = ux_signal(text, textlen, "Avro " as *u8, "Avro" as *u8)
101
102 var present: i64 = 0
103 if s_schema > 0 { present = present + 1 }
104 if s_reader > 0 { present = present + 1 }
105 if s_writer > 0 { present = present + 1 }
106 if s_avro > 0 { present = present + 1 }
107 ux_puts(" core_terms_present = " as *u8); ux_putn(present); ux_puts("/4\n" as *u8)
108 if present >= 3 { ux_puts("R1b-2 verdict=GREEN (sovereign fetch->dechunk->render->extract over a REAL external source)\n" as *u8); sys_exit(0); return 0 }
109 ux_puts("R1b-2 verdict=RED (core decoupling terms absent -- render/extract suspect, e.g. gzip body)\n" as *u8)
110 sys_exit(5); return 5
111}