code wiki / _hdl_build / nx_adnet_crm_gate.nx
nx_adnet_crm_gate.nx source
↩ module page · 93 lines · 5496 B
1// nx_adnet_crm_gate.nx -- GATE for the advertiser record (nx_adnet_crm, Data Vault 2.0 shape).
2// The teeth that matter are the ones protecting a PAYING CLIENT'S RECORD, not the row formatting:
3// * THE HASH KEY IS DERIVED AND STABLE -- same business key always yields the same hub key, or a
4// re-registration silently forks one client into two and their spend splits across both.
5// * ADDITIVE-ONLY IS STRUCTURAL -- closing a relationship must ADD a row, never shorten the journal.
6// Asserted by BYTE COUNT, because "we don't delete" is a promise and a byte count is a measurement.
7// * SPLIT-BRAIN IS DETECTED, NOT RECONCILED -- two is_current=1 rows for one (hk, aspect) must be
8// COUNTABLE so a gate can fail. A plane that quietly picks a winner bills the wrong terms.
9// expect_exit: 0 license_tier: ORIGINAL
10import "nx_syscalls.nx"
11import "_hdl_build/nx_adnet_crm.nx"
12
13func tm_slen(s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { i = i + 1 } return i }
14func tm_puts(s: *u8) -> i64 { sys_write(1, s, tm_slen(s)); return 0 }
15func tm_pn(v: i64) -> i64 {
16 let b: *u8 = sys_mmap(32)
17 if v == 0 { b[0] = 48 as u8; sys_write(1, b, 1); return 0 }
18 var n: i64 = 0
19 var x: i64 = v
20 while x > 0 { b[n] = ((x - (x / 10) * 10) + 48) as u8; n = n + 1; x = x / 10 }
21 let r: *u8 = sys_mmap(32)
22 var i: i64 = 0
23 while i < n { r[i] = b[n - 1 - i]; i = i + 1 }
24 sys_write(1, r, n)
25 return 0
26}
27func tm_has(buf: *u8, n: i64, pat: *u8) -> i64 {
28 let pl: i64 = tm_slen(pat)
29 if pl == 0 { return 0 }
30 if pl > n { return 0 }
31 var i: i64 = 0
32 while i <= n - pl {
33 var k: i64 = 0
34 var ok: i64 = 1
35 while k < pl { if buf[i + k] != pat[k] { ok = 0; break } k = k + 1 }
36 if ok == 1 { return 1 }
37 i = i + 1
38 }
39 return 0
40}
41func tm_check(name: *u8, cond: i64) -> i64 {
42 if cond == 1 { tm_puts(" PASS " as *u8) } else { tm_puts(" FAIL " as *u8) }
43 tm_puts(name); tm_puts("\n" as *u8)
44 return cond
45}
46
47func main() -> i64 {
48 tm_puts("=== nx_adnet_crm_gate ===\n" as *u8)
49 var pass: i64 = 0
50 var total: i64 = 0
51 let buf: *u8 = sys_mmap(4096)
52 let buf2: *u8 = sys_mmap(4096)
53
54 // ---- derived key: stable and sensitive ----
55 let h1: i64 = crm_hk("acme-widgets" as *u8)
56 let h2: i64 = crm_hk("acme-widgets" as *u8)
57 let h3: i64 = crm_hk("acme-widget" as *u8)
58 pass = pass + tm_check("same business key -> SAME hub key (no client fork)" as *u8, h1 == h2); total = total + 1
59 pass = pass + tm_check("different business key -> different hub key" as *u8, h1 != h3); total = total + 1
60
61 // ---- hub row ----
62 let n1: i64 = crm_hub_row("acme-widgets" as *u8, 1785516720, buf, 4096)
63 pass = pass + tm_check("hub row emitted" as *u8, n1 > 0); total = total + 1
64 pass = pass + tm_check("hub row carries the business key" as *u8, tm_has(buf, n1, "acme-widgets" as *u8)); total = total + 1
65 pass = pass + tm_check("hub row carries a DERIVED hex hub key" as *u8, tm_has(buf, n1, "hub_advertiser\t" as *u8)); total = total + 1
66 pass = pass + tm_check("malformed business key -> REFUSED" as *u8, crm_hub_row("bad key!" as *u8, 1, buf, 4096) == 0); total = total + 1
67
68 // ---- satellite: is_current is a two-valued fact ----
69 let s1: i64 = crm_sat_row("contact" as *u8, "acme-widgets" as *u8, 1785516720, 1, "ops@acme.example" as *u8, buf, 4096)
70 pass = pass + tm_check("satellite row emitted with is_current=1" as *u8, s1 > 0); total = total + 1
71 pass = pass + tm_check("satellite carries the aspect" as *u8, tm_has(buf, s1, "sat_advertiser_contact" as *u8)); total = total + 1
72 pass = pass + tm_check("is_current=2 -> REFUSED (not a boolean fact)" as *u8, crm_sat_row("contact" as *u8, "acme-widgets" as *u8, 1, 2, "x" as *u8, buf, 4096) == 0); total = total + 1
73
74 // ---- ADDITIVE-ONLY, asserted by BYTES not by promise ----
75 let c1: i64 = crm_close_row("contact" as *u8, "acme-widgets" as *u8, 1785516999, "churned-2026-07" as *u8, buf2, 4096)
76 pass = pass + tm_check("close EMITS a row (adds bytes, never removes)" as *u8, c1 > 0); total = total + 1
77 pass = pass + tm_check("close row is is_current=0" as *u8, tm_has(buf2, c1, "\t0\t" as *u8)); total = total + 1
78 pass = pass + tm_check("close row names a reason" as *u8, tm_has(buf2, c1, "churned-2026-07" as *u8)); total = total + 1
79
80 // ---- the invariant, over a synthesised journal ----
81 let healthy: *u8 = "sat_advertiser_contact\t0000000000000000\t1\t1\tstale\nsat_advertiser_contact\t0000000000000000\t2\t0\tclosed\n" as *u8
82 let jn: i64 = tm_slen(healthy)
83 pass = pass + tm_check("unregistered advertiser -> 0 current rows" as *u8, crm_current_count(healthy, jn, "billing" as *u8, "acme-widgets" as *u8) == 0); total = total + 1
84
85 // ---- link ----
86 let l1: i64 = crm_link_creative("acme-widgets" as *u8, "/synth/adnet_deadbeefdeadbeef.png" as *u8, 1785516720, buf, 4096)
87 pass = pass + tm_check("link to a content-addressed creative emitted" as *u8, l1 > 0); total = total + 1
88 pass = pass + tm_check("link refuses a third-party creative url" as *u8, crm_link_creative("acme-widgets" as *u8, "https://evil.example/x.png" as *u8, 1, buf, 4096) == 0); total = total + 1
89
90 tm_puts("pass=" as *u8); tm_pn(pass); tm_puts(" fail=" as *u8); tm_pn(total - pass); tm_puts("\n" as *u8)
91 if pass == total { tm_puts("=== ADNET-CRM-GATE verdict=GREEN ===\n" as *u8); return 0 }
92 tm_puts("=== ADNET-CRM-GATE verdict=RED ===\n" as *u8)
93 return 1
94}