code wiki / _hdl_build / nx_compare_cite_gate.nx
nx_compare_cite_gate.nx source
↩ module page · 169 lines · 11935 B
1// nx_compare_cite_gate.nx -- END-TO-END PROOF of nx_compare_cite: the ref row and its inline mark land
2// together under compare-and-swap, the pin is COMPUTED from the mirror, exactly one row is marked, a
3// re-run is a no-op, and every refusal leaves both files byte-identical (hash-compared, never size).
4//
5// Runs the PROMOTED organ (/volume1/homes/elderwesto/nishihost/nx_compare_cite.elf) with cwd = a /tmp
6// fixture tree that mirrors the estate's shape (buildroot/knowledge/compare/<dom>.refs|.matrix and
7// knowledge/fetched/<mirror>), so production data is unreachable by construction. The organ composes
8// nx_atomic_publish by absolute path, so that promoted twin is a gv_need precondition, not an assumption.
9// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
10import "nx_syscalls.nx"
11import "nx_sha256.nx"
12import "nx_gate_verdict.nx"
13import "nx_gatekit_lib.nx"
14import "nx_tool_run.nx"
15
16const CG_FIX: *u8 = "/tmp/nx_compare_cite_gate"
17const CG_ELF: *u8 = "/volume1/homes/elderwesto/nishihost/nx_compare_cite.elf"
18const CG_PUB: *u8 = "/volume1/homes/elderwesto/nishihost/nx_atomic_publish.elf"
19const CG_REFS: *u8 = "/tmp/nx_compare_cite_gate/buildroot/knowledge/compare/fx.refs"
20const CG_MATRIX: *u8 = "/tmp/nx_compare_cite_gate/buildroot/knowledge/compare/fx.matrix"
21const CG_MIRROR: *u8 = "/tmp/nx_compare_cite_gate/knowledge/fetched/fx_mirror.html"
22const CG_MIRROR_REL: *u8 = "knowledge/fetched/fx_mirror.html"
23const CG_SPEC: *u8 = "/tmp/nx_compare_cite_gate/spec.txt"
24const CG_SPEC_ARG: *u8 = "@/tmp/nx_compare_cite_gate/spec.txt"
25const CG_OUTCAP: i64 = 65536
26const CG_PATHCAP: i64 = 512
27const CG_HEX: i64 = 64
28const CG_ARGV: i64 = 12
29
30func w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
31
32// sha256 hex of a whole file (sys_read_file sizes it); "-" when unreadable, so a missing file never equals anything
33func cg_hash(path: *u8, hex: *u8) -> i64 {
34 let szp: *i64 = sys_mmap(16) as *i64
35 let b: *u8 = sys_read_file(path, szp)
36 if (b as i64) == 0 { hex[0] = 45 as u8; hex[1] = 0 as u8; return 0 - 1 }
37 let dig: *u8 = sys_mmap(32)
38 sha256_digest(b, szp[0], dig)
39 let d: *u8 = "0123456789abcdef" as *u8
40 var i: i64 = 0
41 while i < 32 { let v: i64 = dig[i] as i64; hex[i * 2] = d[(v / 16) % 16]; hex[i * 2 + 1] = d[v % 16]; i = i + 1 }
42 hex[CG_HEX] = 0 as u8
43 return szp[0]
44}
45
46// run the organ with the 8 positional fields, cwd = fixture; returns the exit code, fills out/outn
47func cg_cite(dom: *u8, key: *u8, label: *u8, cite: *u8, url: *u8, mirror: *u8, cls: *u8, grounds: *u8, out: *u8, outn: *i64) -> i64 {
48 let argv: *i64 = sys_mmap(8 * CG_ARGV) as *i64
49 argv[0] = CG_ELF as i64
50 argv[1] = dom as i64
51 argv[2] = key as i64
52 argv[3] = label as i64
53 argv[4] = cite as i64
54 argv[5] = url as i64
55 argv[6] = mirror as i64
56 argv[7] = cls as i64
57 argv[8] = grounds as i64
58 argv[9] = 0
59 return tr_run_capture(CG_ELF, argv, out, CG_OUTCAP - 1, outn)
60}
61
62func main() -> i64 {
63 let ctr: *i64 = gv_ctr()
64 w("=== nx_compare_cite_gate -- a citation is ONE publish or NONE ===\n" as *u8)
65 let cwd0: *u8 = sys_mmap(CG_PATHCAP)
66 sys_getcwd(cwd0, CG_PATHCAP)
67 gk_mkdir(CG_FIX)
68 gk_mkdir("/tmp/nx_compare_cite_gate/buildroot" as *u8)
69 gk_mkdir("/tmp/nx_compare_cite_gate/buildroot/knowledge" as *u8)
70 gk_mkdir("/tmp/nx_compare_cite_gate/buildroot/knowledge/compare" as *u8)
71 gk_mkdir("/tmp/nx_compare_cite_gate/knowledge" as *u8)
72 gk_mkdir("/tmp/nx_compare_cite_gate/knowledge/fetched" as *u8)
73 // fixtures are REWRITTEN every run so the gate is idempotent (a leftover mark from a prior run would
74 // turn the CITED tooth into ALREADY-CITED and read like a regression)
75 gk_write(CG_REFS, "# fx.refs -- fixture register\nref|k0|Existing row.|https://example.test/k0|knowledge/fetched/fx_mirror.html|h0|2026-09-02|vendor-doc|Row Two\n" as *u8)
76 gk_write(CG_MATRIX, "# fixture matrix\n@title Fixture\n@cols A|B|C|D\nRow One|runtime/x.nx|sym_one|0|0|0|0|0|note one\nRow Two|runtime/y.nx|sym_two|0|1|1|1|1|note two [@k0]\nOther row|runtime/z.nx|sym_other|0|0|0|0|0|note other\n" as *u8)
77 gk_write(CG_MIRROR, "<html><body>fixture mirror body for the pin</body></html>\n" as *u8)
78 var t0: i64 = 1
79 if gk_exists(CG_REFS) == 0 { t0 = 0 }
80 if gk_exists(CG_MATRIX) == 0 { t0 = 0 }
81 if gk_exists(CG_MIRROR) == 0 { t0 = 0 }
82 if gk_count(CG_MATRIX, "Row One|" as *u8) != 1 { t0 = 0 }
83 gv_check("T0 fixture-reached-condition: refs, matrix and mirror written; exactly ONE row starts with the label under test" as *u8, t0, ctr)
84 if gv_need("promoted organ present at nishihost/nx_compare_cite.elf" as *u8, gk_exists(CG_ELF), ctr) == 1 {
85 if gv_need("promoted publisher present at nishihost/nx_atomic_publish.elf (the organ composes it by fork)" as *u8, gk_exists(CG_PUB), ctr) == 1 {
86 let mhex: *u8 = sys_mmap(CG_HEX + 2)
87 cg_hash(CG_MIRROR, mhex)
88 sys_chdir(CG_FIX)
89 let out: *u8 = sys_mmap(CG_OUTCAP)
90 let outn: *i64 = sys_mmap(16) as *i64
91 // ---- T1..T3 the positive path -----------------------------------------------------------
92 let rc1: i64 = cg_cite("fx" as *u8, "k1" as *u8, "Row One" as *u8, "Cite text one." as *u8, "https://example.test/one" as *u8, CG_MIRROR_REL, "vendor-doc" as *u8, "Row One grounds" as *u8, out, outn)
93 var t1: i64 = 0
94 if rc1 == 0 { if gk_out_has(out, outn[0], "CITED domain=fx key=k1 row=Row One" as *u8) == 1 { t1 = 1 } }
95 gv_check("T1 the positive path exits 0 with a CITED receipt naming domain, key and row" as *u8, t1, ctr)
96 let needle: *u8 = sys_mmap(1024)
97 var no: i64 = gk_cat(needle, 0, "ref|k1|Cite text one.|https://example.test/one|knowledge/fetched/fx_mirror.html|h" as *u8)
98 no = gk_cat(needle, no, mhex)
99 no = gk_cat(needle, no, "|20" as *u8)
100 var t2: i64 = 0
101 if gk_contains(CG_REFS, needle) == 1 { if gk_count(CG_REFS, "ref|k1|" as *u8) == 1 { t2 = 1 } }
102 gv_check("T2 the ref row landed ONCE with the pin COMPUTED from the mirror bytes (sha256 the gate recomputed) and a dated accessed field" as *u8, t2, ctr)
103 var t3: i64 = 0
104 if gk_contains(CG_MATRIX, "|note one [@k1]\n" as *u8) == 1 { if gk_contains(CG_MATRIX, "|note two [@k0]\n" as *u8) == 1 { if gk_count(CG_MATRIX, "[@k1]" as *u8) == 1 { t3 = 1 } } }
105 gv_check("T3 the mark landed at the END of the ONE matched row's note, exactly once, and the neighbouring rows are byte-identical" as *u8, t3, ctr)
106 // ---- T4 idempotent ------------------------------------------------------------------------
107 let rh1: *u8 = sys_mmap(CG_HEX + 2)
108 let mh1: *u8 = sys_mmap(CG_HEX + 2)
109 cg_hash(CG_REFS, rh1)
110 cg_hash(CG_MATRIX, mh1)
111 let rc2: i64 = cg_cite("fx" as *u8, "k1" as *u8, "Row One" as *u8, "Cite text one." as *u8, "https://example.test/one" as *u8, CG_MIRROR_REL, "vendor-doc" as *u8, "Row One grounds" as *u8, out, outn)
112 let rh2: *u8 = sys_mmap(CG_HEX + 2)
113 let mh2: *u8 = sys_mmap(CG_HEX + 2)
114 cg_hash(CG_REFS, rh2)
115 cg_hash(CG_MATRIX, mh2)
116 var t4: i64 = 0
117 if rc2 == 0 { if gk_out_has(out, outn[0], "ALREADY-CITED domain=fx key=k1" as *u8) == 1 { if gk_streq(rh1, rh2) == 1 { if gk_streq(mh1, mh2) == 1 { t4 = 1 } } } }
118 gv_check("T4 a re-run with the same arguments answers ALREADY-CITED, exits 0 and leaves both files HASH-identical (idempotent by content, not by size)" as *u8, t4, ctr)
119 // ---- T5..T8 neg-controls: every refusal is NAMED and writes NOTHING ------------------------
120 let rc5: i64 = cg_cite("fx" as *u8, "k2" as *u8, "Row One" as *u8, "Cite two." as *u8, "https://example.test/two" as *u8, "knowledge/fetched/absent_mirror.html" as *u8, "vendor-doc" as *u8, "Row One" as *u8, out, outn)
121 cg_hash(CG_REFS, rh2)
122 cg_hash(CG_MATRIX, mh2)
123 var t5: i64 = 0
124 if rc5 == 4 { if gk_out_has(out, outn[0], "REFUSED-MIRROR-ABSENT" as *u8) == 1 { if gk_streq(rh1, rh2) == 1 { if gk_streq(mh1, mh2) == 1 { t5 = 1 } } } }
125 gv_check("T5 neg-control-absent-mirror-cannot-be-cited: REFUSED-MIRROR-ABSENT exit 4, both files hash-identical (a reference nobody fetched never enters a register)" as *u8, t5, ctr)
126 let rc6: i64 = cg_cite("fx" as *u8, "k3" as *u8, "Row Nine" as *u8, "Cite three." as *u8, "https://example.test/three" as *u8, CG_MIRROR_REL, "vendor-doc" as *u8, "Row Nine" as *u8, out, outn)
127 cg_hash(CG_REFS, rh2)
128 cg_hash(CG_MATRIX, mh2)
129 var t6: i64 = 0
130 if rc6 == 6 { if gk_out_has(out, outn[0], "REFUSED-ROW-NOT-FOUND" as *u8) == 1 { if gk_streq(rh1, rh2) == 1 { if gk_streq(mh1, mh2) == 1 { t6 = 1 } } } }
131 gv_check("T6 neg-control-unknown-row: REFUSED-ROW-NOT-FOUND exit 6, both files hash-identical" as *u8, t6, ctr)
132 let rc7: i64 = cg_cite("fx" as *u8, "k4" as *u8, "Row " as *u8, "Cite four." as *u8, "https://example.test/four" as *u8, CG_MIRROR_REL, "vendor-doc" as *u8, "Row" as *u8, out, outn)
133 cg_hash(CG_REFS, rh2)
134 cg_hash(CG_MATRIX, mh2)
135 var t7: i64 = 0
136 if rc7 == 6 { if gk_out_has(out, outn[0], "REFUSED-ROW-AMBIGUOUS" as *u8) == 1 { if gk_out_has(out, outn[0], "matches=2" as *u8) == 1 { if gk_streq(rh1, rh2) == 1 { if gk_streq(mh1, mh2) == 1 { t7 = 1 } } } } }
137 gv_check("T7 neg-control-ambiguous-label: a prefix matching two rows is REFUSED-ROW-AMBIGUOUS with matches=2, nothing written (a mark can never land on the wrong row)" as *u8, t7, ctr)
138 let rc8: i64 = cg_cite("fx" as *u8, "k5" as *u8, "Row One" as *u8, "bad|cite" as *u8, "https://example.test/five" as *u8, CG_MIRROR_REL, "vendor-doc" as *u8, "Row One" as *u8, out, outn)
139 cg_hash(CG_REFS, rh2)
140 cg_hash(CG_MATRIX, mh2)
141 var t8: i64 = 0
142 if rc8 == 3 { if gk_out_has(out, outn[0], "REFUSED-PIPE-IN-FIELD" as *u8) == 1 { if gk_streq(rh1, rh2) == 1 { if gk_streq(mh1, mh2) == 1 { t8 = 1 } } } }
143 gv_check("T8 neg-control-pipe-in-prose: REFUSED-PIPE-IN-FIELD exit 3, nothing written (the row grammar has exactly nine fields)" as *u8, t8, ctr)
144 // ---- T9 the spec-file form (argv-mangling-safe) --------------------------------------------
145 gk_write(CG_SPEC, "fx\nk6\nOther row\nCite six via spec.\nhttps://example.test/six\nknowledge/fetched/fx_mirror.html\npublished-standard\nOther row grounds\n" as *u8)
146 let argv9: *i64 = sys_mmap(8 * 4) as *i64
147 argv9[0] = CG_ELF as i64
148 argv9[1] = CG_SPEC_ARG as i64
149 argv9[2] = 0
150 let rc9: i64 = tr_run_capture(CG_ELF, argv9, out, CG_OUTCAP - 1, outn)
151 var t9: i64 = 0
152 if rc9 == 0 { if gk_out_has(out, outn[0], "CITED domain=fx key=k6 row=Other row" as *u8) == 1 { if gk_contains(CG_MATRIX, "|note other [@k6]\n" as *u8) == 1 { if gk_count(CG_REFS, "ref|k6|Cite six via spec.|" as *u8) == 1 { t9 = 1 } } } }
153 gv_check("T9 the @specfile form (8 lines, the rail's mangling-safe idiom) cites a second row and leaves the first row's mark alone" as *u8, t9, ctr)
154 // ---- T10 usage refusal ---------------------------------------------------------------------
155 let argv10: *i64 = sys_mmap(8 * 4) as *i64
156 argv10[0] = CG_ELF as i64
157 argv10[1] = "fx" as *u8 as i64
158 argv10[2] = "k7" as *u8 as i64
159 argv10[3] = 0
160 let rc10: i64 = tr_run_capture(CG_ELF, argv10, out, CG_OUTCAP - 1, outn)
161 var t10: i64 = 0
162 if rc10 == 2 { if gk_out_has(out, outn[0], "REFUSED-USAGE fields=" as *u8) == 1 { t10 = 1 } }
163 gv_check("T10 neg-control-usage: fewer than eight fields is REFUSED-USAGE exit 2 naming the count it saw" as *u8, t10, ctr)
164 sys_chdir(cwd0)
165 } }
166 let rc__dry: i64 = gv_verdict("COMPARE-CITE-GATE" as *u8, ctr, "a citation is one publish or none; every refusal is named and writes nothing" as *u8)
167 sys_exit(rc__dry)
168 return rc__dry
169}