code wiki / _hdl_build / nx_brand_book_gate.nx
nx_brand_book_gate.nx source
↩ module page · 116 lines · 6274 B
1// nx_brand_book_gate.nx -- R5 gate: brand-book generator + off-token brand audit.
2// BB: the brand book is emitted FROM the token SSOT (swatches + MEASURED WCAG contrast + AA/AAA badges, rendered
3// in-brand). BA: the audit returns 0 for a token-driven builder page (no drift) and flags a page that hardcodes a
4// colour outside the token block. 100% sovereign. license_tier: ORIGINAL expect_exit: 0
5import "nx_syscalls.nx"
6import "nx_sitegate_emit_lib.nx"
7import "nx_brand_book.nx"
8import "nx_brand_tokens.nx"
9
10
11func g_has(s: *u8, n: i64, lit: *u8) -> i64 { if bt_find(s, n, lit, bt_len(lit)) >= 0 { return 1 } return 0 }
12func g_read(path: *u8, lp: *i64) -> *u8 { return sys_read_file(path, lp) }
13func inband5(v: i64, lo: i64, hi: i64) -> i64 { if v < lo { return 0 } if v > hi { return 0 } return 1 }
14
15func main() -> i64 {
16 let tot: *i64 = sys_mmap(32) as *i64
17 tot[0]=0; tot[1]=0
18 let d: *u8 = bt_default_brand()
19 let bn: i64 = bt_len(d)
20 let lp: *i64 = sys_mmap(16) as *i64
21 gw("=== nx_brand_book_gate -- R5 brand-book generator + off-token audit ===\n" as *u8)
22
23 // emit the brand book to /tmp
24 let bfd: i64 = sys_openat_wr("/tmp/nx_brandbook.html" as *u8, 0x1a4)
25 bb_emit(d, bn, bfd)
26 sys_close(bfd)
27 let book: *u8 = g_read("/tmp/nx_brandbook.html" as *u8, lp)
28 let bln: i64 = lp[0]
29
30 // BB1: book is generated from the token SSOT, rendered in-brand
31 var t1: i64 = 0
32 if bln > 0 {
33 if g_has(book, bln, "Brand Guidelines" as *u8) == 1 {
34 if g_has(book, bln, "--nx-color-primary: #0b2545" as *u8) == 1 {
35 if g_has(book, bln, "#0b2545" as *u8) == 1 { if g_has(book, bln, "Contrast vs bg" as *u8) == 1 { t1 = 1 } }
36 }
37 }
38 }
39 t_row("BB1 brand book emitted FROM tokens, rendered in-brand (swatches + hex + contrast col)" as *u8, t1, tot)
40
41 // BB2: measured contrast + AA/AAA badge present
42 var t2: i64 = 0
43 if g_has(book, bln, ":1 " as *u8) == 1 { if g_has(book, bln, "AAA" as *u8) == 1 { t2 = 1 } }
44 t_row("BB2 measured WCAG contrast ratios + AAA badge shown in the book" as *u8, t2, tot)
45
46 // BA1: a token-driven builder page has NO off-token colour drift
47 let cfd: i64 = sys_openat_wr("/tmp/nx_clean.html" as *u8, 0x1a4)
48 wb_doc_open_branded(cfd, "Clean" as *u8, d, bn)
49 wb_w(cfd, "<p>clean page, all colour via tokens</p>\n" as *u8)
50 wb_doc_close(cfd)
51 sys_close(cfd)
52 let clean: *u8 = g_read("/tmp/nx_clean.html" as *u8, lp)
53 let cln: i64 = lp[0]
54 let off_clean: i64 = ba_audit(clean, cln)
55 gw(" audit(clean builder page) off-token hex = " as *u8); gn(off_clean); gw("\n" as *u8)
56 t_row("BA1 audit: token-driven page has 0 off-token colour drift" as *u8, inband5(off_clean, 0, 0), tot)
57
58 // BA2: a page that hardcodes a colour outside the token block is FLAGGED
59 let dfd: i64 = sys_openat_wr("/tmp/nx_dirty.html" as *u8, 0x1a4)
60 wb_doc_open_branded(dfd, "Dirty" as *u8, d, bn)
61 wb_w(dfd, "<div style=\"color:#ff0000\">rogue hardcoded colour</div>\n" as *u8)
62 wb_doc_close(dfd)
63 sys_close(dfd)
64 let dirty: *u8 = g_read("/tmp/nx_dirty.html" as *u8, lp)
65 let dln: i64 = lp[0]
66 let off_dirty: i64 = ba_audit(dirty, dln)
67 gw(" audit(page with a rogue #ff0000) off-token hex = " as *u8); gn(off_dirty); gw("\n" as *u8)
68 t_row("BA2 audit: a hardcoded off-token colour is FLAGGED (>=1)" as *u8, inband5(off_dirty, 1, 999999), tot)
69
70 // BB3: determinism
71 let b2fd: i64 = sys_openat_wr("/tmp/nx_brandbook2.html" as *u8, 0x1a4)
72 bb_emit(d, bn, b2fd)
73 sys_close(b2fd)
74 let book2: *u8 = g_read("/tmp/nx_brandbook2.html" as *u8, lp)
75 let bln2: i64 = lp[0]
76 var t5: i64 = 0
77 if bln == bln2 { if bln > 0 { var eq: i64 = 1; var i: i64 = 0; while i < bln { if book[i] != book2[i] { eq = 0 } i = i + 1 } t5 = eq } }
78 t_row("BB3 determinism: brand book emits byte-identical (portable/reproducible)" as *u8, t5, tot)
79
80 // ---- MUTATION-DERIVED TOOTH: THE WCAG BANDING ITSELF (2026-08-01, debt 1785604588) ----
81 // Survivor at nx_brand_book.nx:21, `if cx >= 700 { "AAA" }` -- the top band of bb_label, which
82 // classifies a measured contrast ratio as AAA / AA / FAIL. Inverting that comparison mislabels
83 // ACCESSIBILITY COMPLIANCE: a failing contrast can be stamped AAA and shipped as conformant.
84 // It survived because every existing row checks that the generated HTML CONTAINS expected tokens
85 // and that measured contrast values fall inside a band -- but nothing asserts that a GIVEN contrast
86 // maps to the RIGHT LABEL. The generator and the classifier were tested; the mapping between them
87 // was not.
88 // LAW: A CLASSIFIER IS ONLY TESTED BY DRIVING EVERY BAND, INCLUDING THE BOUNDARY. One sample lands
89 // in one band and proves nothing about the thresholds that separate them -- and a threshold is the
90 // entire content of a classifier.
91 // Values chosen to sit clearly inside each band AND on the exact boundaries (700 and 450), because
92 // an inverted comparison shows first at the boundary.
93 let lfd: i64 = sys_openat_wr("/tmp/nx_bb_label.txt" as *u8, 0x1a4)
94 if lfd >= 0 {
95 bb_label(lfd, 900) // deep AAA
96 bb_label(lfd, 700) // AAA boundary (>= is inclusive)
97 bb_label(lfd, 500) // mid AA
98 bb_label(lfd, 450) // AA boundary
99 bb_label(lfd, 300) // FAIL
100 sys_close(lfd)
101 }
102 let llp: *i64 = sys_mmap(16) as *i64
103 let lbuf: *u8 = g_read("/tmp/nx_bb_label.txt" as *u8, llp)
104 var band_ok: i64 = 0
105 if (lbuf as i64) != 0 {
106 // Expected concatenation, in order: AAA AAA AA AA FAIL
107 if g_has(lbuf, llp[0], "AAAAAAAAAAFAIL" as *u8) == 1 { band_ok = 1 }
108 }
109 if band_ok == 1 { tot[0]=tot[0]+1; gw(" [PASS] WCAG banding: 900/700 -> AAA, 500/450 -> AA, 300 -> FAIL (thresholds and boundaries exact)\n" as *u8) }
110 if band_ok == 0 { tot[1]=tot[1]+1; gw(" [FAIL] WCAG banding wrong -- a contrast ratio is mapped to the wrong conformance label\n" as *u8) }
111
112 gw("\nrows pass=" as *u8); gn(tot[0]); gw(" fail=" as *u8); gn(tot[1]); gw("\n" as *u8)
113 if tot[1] == 0 { gw("VERDICT=GREEN -- brand book generated from the SSOT + off-token drift audit works\n" as *u8); return 0 }
114 gw("VERDICT=RED\n" as *u8)
115 return 1
116}