code wiki / _hdl_build / nx_game_synth.nx
nx_game_synth.nx source
↩ module page · 145 lines · 7269 B
1// nx_game_synth.nx -- the SOVEREIGN SYNTHESIS engine: the Nishi researcher does the research ANALYSIS itself
2// (no Claude deep-research in the loop), built hardware-up. This is the keystone rung = the CORROBORATION
3// engine: the >=2-source no-hearsay rule ([[feedback-no-wave-measured-exceed]]) made MECHANICAL. For a technique
4// term, it counts how many DISTINCT source files in the sovereign corpus (knowledge/fetched/*.raw) attest it ->
5// a term is CORROBORATED only if >=2 independent sources mention it (no single-source hearsay, no fabrication).
6// HONEST SCOPE: this is mechanical extraction + corroboration (real, gradeable) -- NOT LLM-quality semantic
7// synthesis (that = the no-float sovereign-LLM arc, the named destination). gs_* organs compose upward.
8// license_tier: ORIGINAL
9import "nx_syscalls.nx"
10import "nx_dirent.nx"
11const GS_MAGIC_131072: i64 = 131072
12const GS_MAGIC_1024: i64 = 1024
13
14const GS_CORROB_MIN: i64 = 2 // >=2 distinct sources = CORROBORATED (the no-hearsay floor)
15
16// substring presence: 1 iff `needle` occurs in hay[0..haylen). 'match' is reserved -> 'hit'.
17func gs_has(hay: *u8, haylen: i64, needle: *u8) -> i64 {
18 var nl: i64 = 0; while needle[nl] != (0 as u8) { nl = nl + 1 }
19 if nl == 0 { return 0 }
20 var i: i64 = 0
21 while i + nl <= haylen {
22 var hit: i64 = 1; var j: i64 = 0
23 while j < nl { if hay[i+j] != needle[j] { hit = 0; j = nl } else { j = j + 1 } }
24 if hit == 1 { return 1 }
25 i = i + 1
26 }
27 return 0
28}
29// GR-S-R3: WORD-BOUNDARY presence. 1 iff `needle` occurs in hay as a WHOLE word (bounded by non-alphanumeric
30// on both sides) -> "quest" no longer matches "request"/"question". Fixes the short-substring overcount. Case-
31// insensitive-ish is not needed (corpus + keys are lowercase). alnum = [0-9A-Za-z]; '-'/' ' inside a multi-word
32// needle are fine (they're part of the needle, only the OUTER boundaries are checked).
33func gs_alnum(c: i64) -> i64 {
34 if c >= 48 { if c <= 57 { return 1 } }
35 if c >= 65 { if c <= 90 { return 1 } }
36 if c >= 97 { if c <= 122 { return 1 } }
37 return 0
38}
39func gs_has_word(hay: *u8, haylen: i64, needle: *u8) -> i64 {
40 var nl: i64 = 0; while needle[nl] != (0 as u8) { nl = nl + 1 }
41 if nl == 0 { return 0 }
42 var i: i64 = 0
43 while i + nl <= haylen {
44 var hit: i64 = 1; var j: i64 = 0
45 while j < nl { if hay[i+j] != needle[j] { hit = 0; j = nl } else { j = j + 1 } }
46 if hit == 1 {
47 var lok: i64 = 1; if i > 0 { if gs_alnum(hay[i-1] & 0xff) == 1 { lok = 0 } }
48 var rok: i64 = 1; if i + nl < haylen { if gs_alnum(hay[i+nl] & 0xff) == 1 { rok = 0 } }
49 if lok == 1 { if rok == 1 { return 1 } }
50 }
51 i = i + 1
52 }
53 return 0
54}
55
56// lean file read (raw read into a big reused buffer; corpus files are < 1MB) -> presence test on disk.
57func gs_file_has(path: *u8, needle: *u8, buf: *u8, cap: i64) -> i64 {
58 let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 }
59 var total: i64 = 0
60 var more: i64 = 1
61 while more == 1 {
62 if total >= cap { more = 0 } else {
63 let got: i64 = sys_read(fd, (buf as i64 + total) as *u8, cap - total)
64 if got <= 0 { more = 0 } else { total = total + got }
65 }
66 }
67 sys_close(fd)
68 return gs_has_word(buf, total, needle)
69}
70func gs_ends_raw(name: *u8) -> i64 {
71 var l: i64 = 0; while name[l]!=(0 as u8){ l=l+1 }
72 if l < 4 { return 0 }
73 if name[l-4]==(46 as u8) { if name[l-3]==(114 as u8) { if name[l-2]==(97 as u8) { if name[l-1]==(119 as u8) { return 1 } } } } // ".raw"
74 return 0
75}
76func gs_join(dir: *u8, name: *u8, out: *u8) -> i64 {
77 var o: i64 = 0; var i: i64 = 0
78 while dir[i]!=(0 as u8){ out[o]=dir[i]; o=o+1; i=i+1 }
79 out[o]=47 as u8; o=o+1
80 i=0; while name[i]!=(0 as u8){ out[o]=name[i]; o=o+1; i=i+1 }
81 out[o]=0 as u8; return o
82}
83
84// 1 iff `name` starts with `pfx`.
85func gs_name_pfx(name: *u8, pfx: *u8) -> i64 { var i: i64=0; while pfx[i]!=(0 as u8){ if name[i]!=pfx[i]{ return 0 } i=i+1 } return 1 }
86
87// THE CORROBORATION COUNT, SCOPED to the relevant corpus: # of DISTINCT .raw source files in `dirpath` whose
88// name starts with pfx1 OR pfx2 AND that contain `needle`. SCOPING IS RESEARCH DISCIPLINE -- counting unrelated
89// corpora (financial/clinical/etc.) as game evidence is a CONTAMINATION the synthesis must refuse. Lean reads.
90// `all`=1 -> count every .raw (no prefix filter; avoids the unreliable empty-string literal). `all`=0 ->
91// count only files whose name starts with pfx1 OR pfx2. SCOPING IS RESEARCH DISCIPLINE -- counting unrelated
92// corpora (financial/clinical/etc.) as game evidence is a CONTAMINATION the synthesis must refuse. Lean reads.
93func gs_corroborate_in(dirpath: *u8, all: i64, pfx1: *u8, pfx2: *u8, needle: *u8, buf: *u8, cap: i64) -> i64 {
94 let fd: i64 = nx_openat(NX_AT_FDCWD, dirpath, NX_O_RDONLY | NX_O_DIRECTORY, 0)
95 if fd < 0 { return 0 }
96 let dbuf: *u8 = sys_mmap(GS_MAGIC_131072)
97 let dr: *NxDirent = sys_mmap(NX_DIRENT_BYTES) as *NxDirent
98 var sources: i64 = 0
99 var more: i64 = 1
100 while more == 1 {
101 let n: i64 = nx_dirent_read(fd, dbuf, GS_MAGIC_131072)
102 if n <= 0 { more = 0 } else {
103 var off: i64 = 0
104 while off < n {
105 let nextoff: i64 = nx_dirent_iter(dbuf, off, n, dr)
106 if nextoff <= off { off = n } else {
107 if gs_ends_raw(dr.name) == 1 {
108 var scoped: i64 = all
109 if scoped == 0 { scoped = gs_name_pfx(dr.name, pfx1) }
110 if scoped == 0 { scoped = gs_name_pfx(dr.name, pfx2) }
111 if scoped == 1 {
112 let full: *u8 = sys_mmap(GS_MAGIC_1024); gs_join(dirpath, dr.name, full)
113 if gs_file_has(full, needle, buf, cap) == 1 { sources = sources + 1 }
114 }
115 }
116 off = nextoff
117 }
118 }
119 }
120 }
121 sys_close(fd)
122 return sources
123}
124// back-compat: scan ALL .raw. Prefer gs_corroborate_game for real synthesis (scoped = no contamination).
125func gs_corroborate(dirpath: *u8, needle: *u8, buf: *u8, cap: i64) -> i64 {
126 return gs_corroborate_in(dirpath, 1, "x" as *u8, "x" as *u8, needle, buf, cap)
127}
128// scoped count over the GAME corpus (gr_ fetched + ge_ 5-pillar) -- the canonical synthesis count.
129func gs_corroborate_game(dirpath: *u8, needle: *u8, buf: *u8, cap: i64) -> i64 {
130 return gs_corroborate_in(dirpath, 0, "gr_" as *u8, "ge_" as *u8, needle, buf, cap)
131}
132// SOVEREIGN build-state check: 1 iff the file exists (the census verifies an implementing organ is REALLY on
133// disk -- no claimed PRESENT/EXCEEDS survives without the file actually opening). "-" = no mapping -> 0.
134func gs_file_exists(path: *u8) -> i64 {
135 if path[0] == (45 as u8) { if path[1] == (0 as u8) { return 0 } } // "-" sentinel = no organ
136 let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 }
137 sys_close(fd); return 1
138}
139
140// verdict: 2 = CORROBORATED (>=min), 1 = SINGLE-SOURCE (hearsay, do not cite alone), 0 = ABSENT.
141func gs_verdict(sources: i64) -> i64 {
142 if sources >= GS_CORROB_MIN { return 2 }
143 if sources >= 1 { return 1 }
144 return 0
145}