code wiki / _hdl_build / nx_legal_compliance_gate.nx
nx_legal_compliance_gate.nx source
↩ module page · 170 lines · 8701 B
1// nx_legal_compliance_gate.nx -- GATE for LEGAL D0 (nx_legal_compliance).
2//
3// Drives the REAL classifier + regime selector + validity machine and asserts:
4//
5// CLASSIFIER (the precision a generic e-sign product lacks):
6// "Last Will and Testament" -> WILL (excluded); "Living Will" ->
7// ADVANCE_DIRECTIVE (NOT a will, e-signable); "Revocable Living Trust" ->
8// TRUST_LIVING (e-signable); "Testamentary Trust" -> TESTAMENTARY_TRUST
9// (excluded); "Codicil ..." -> CODICIL; contract/POA/deed classify.
10//
11// UETA GENERAL : a contract with intent+consent+attribution+retainable ->
12// VALID/UETA_GENERAL; drop consent or retainable -> NEEDS_MORE with the
13// exact missing-element reason (drives the concierge "what's left" UX).
14//
15// E-WILLS REGIME : a will in a jurisdiction that permits e-wills, executed
16// with testator intent + attribution + >=2 witnesses + notarization +
17// retainable -> VALID/ELECTRONIC_WILLS; one witness -> NEEDS_MORE(2_WITNESS).
18//
19// *** NEVER-VOID INVARIANT (the safety crux -- legal Rule 26) ***
20// EXHAUSTIVE sweep: for EVERY excluded testamentary type (will/codicil/
21// testamentary trust), across ALL 2^4 combinations of the general UETA
22// elements and BOTH jurisdiction settings, with no witnesses/notary, the
23// verdict is NEVER VALID. A naive DocuSign-clone would return VALID and
24// produce a legally VOID instrument; this engine cannot, by construction.
25// Plus: an excluded doc in a no-e-wills jurisdiction -> INVALID_VOID/
26// REQUIRES_WET (never electronically executable).
27//
28// Evidence -> knowledge/status/legal_compliance.log
29// license_tier: ORIGINAL
30import "nx_legal_compliance.nx"
31import "nx_syscalls.nx"
32import "nx_gate_verdict.nx"
33
34const LC_LOG: *u8 = "knowledge/status/legal_compliance.log"
35
36func ew(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 }
37func ewn(fd: i64, v: i64) -> i64 {
38 let bb: *u8 = sys_mmap(28); var m: i64 = v
39 if m < 0 { m = 0 - m; sys_write(fd, "-" as *u8, 1) }
40 let t: *u8 = sys_mmap(28); var k: i64 = 0
41 if m == 0 { t[0] = 48; k = 1 }
42 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
43 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
44 sys_write(fd, bb, k); return 0
45}
46
47// EXHAUSTIVE never-void sweep for one excluded doc type: across both
48// jurisdiction settings and all 16 combos of general UETA elements (with
49// witnesses=0, notarized=0), the verdict must NEVER be VALID. Returns 1 if the
50// invariant holds, 0 if ANY combo slipped through as VALID.
51func sweep_excluded(doc_type: i64, reason: *i64, regime: *i64) -> i64 {
52 var holds: i64 = 1
53 var ewa: i64 = 0
54 while ewa <= 1 {
55 var combo: i64 = 0
56 while combo < 16 {
57 let intent: i64 = combo & 1
58 let consent: i64 = (combo >> 1) & 1
59 let attribution: i64 = (combo >> 2) & 1
60 let retainable: i64 = (combo >> 3) & 1
61 let v: i64 = nx_legal_verdict(doc_type, ewa, intent, consent, attribution, retainable, 0, 0, reason, regime)
62 if v == LV_VALID { holds = 0 }
63 combo = combo + 1
64 }
65 ewa = ewa + 1
66 }
67 return holds
68}
69
70func main() -> i64 {
71 var ok: i64 = 1
72 let reason: *i64 = sys_mmap(8) as *i64
73 let regime: *i64 = sys_mmap(8) as *i64
74
75 // ---- CLASSIFIER (living vs testamentary precision) ----
76 var classify_ok: i64 = 1
77 if nx_legal_doc_classify("Last Will and Testament" as *u8) != DT_WILL { classify_ok = 0 }
78 if nx_legal_doc_classify("Living Will" as *u8) != DT_ADVANCE_DIRECTIVE { classify_ok = 0 }
79 if nx_legal_doc_classify("Advance Healthcare Directive" as *u8) != DT_ADVANCE_DIRECTIVE { classify_ok = 0 }
80 if nx_legal_doc_classify("Revocable Living Trust" as *u8) != DT_TRUST_LIVING { classify_ok = 0 }
81 if nx_legal_doc_classify("Testamentary Trust Agreement" as *u8) != DT_TESTAMENTARY_TRUST { classify_ok = 0 }
82 if nx_legal_doc_classify("Codicil to Last Will" as *u8) != DT_CODICIL { classify_ok = 0 }
83 if nx_legal_doc_classify("Durable Power of Attorney" as *u8) != DT_POA { classify_ok = 0 }
84 if nx_legal_doc_classify("Quitclaim Deed" as *u8) != DT_DEED { classify_ok = 0 }
85 if nx_legal_doc_classify("Client Engagement Agreement" as *u8) != DT_CONTRACT { classify_ok = 0 }
86 if classify_ok != 1 { ok = 0 }
87
88 // ---- UETA GENERAL ----
89 var ueta_ok: i64 = 1
90 let v1: i64 = nx_legal_verdict(DT_CONTRACT, 1, 1, 1, 1, 1, 0, 0, reason, regime)
91 if v1 != LV_VALID { ueta_ok = 0 }
92 if *regime != RG_UETA_GENERAL { ueta_ok = 0 }
93 if *reason != LR_OK_UETA { ueta_ok = 0 }
94 let v2: i64 = nx_legal_verdict(DT_CONTRACT, 1, 1, 0, 1, 1, 0, 0, reason, regime)
95 if v2 != LV_NEEDS_MORE { ueta_ok = 0 }
96 if *reason != LR_NO_CONSENT { ueta_ok = 0 }
97 let v3: i64 = nx_legal_verdict(DT_CONTRACT, 1, 1, 1, 1, 0, 0, 0, reason, regime)
98 if v3 != LV_NEEDS_MORE { ueta_ok = 0 }
99 if *reason != LR_NOT_RETAINABLE { ueta_ok = 0 }
100 if ueta_ok != 1 { ok = 0 }
101
102 // ---- E-WILLS REGIME ----
103 var ewills_ok: i64 = 1
104 let w1: i64 = nx_legal_verdict(DT_WILL, 1, 1, 0, 1, 1, 2, 1, reason, regime)
105 if w1 != LV_VALID { ewills_ok = 0 }
106 if *regime != RG_ELECTRONIC_WILLS { ewills_ok = 0 }
107 if *reason != LR_OK_EWILLS { ewills_ok = 0 }
108 let w2: i64 = nx_legal_verdict(DT_WILL, 1, 1, 0, 1, 1, 1, 1, reason, regime)
109 if w2 != LV_NEEDS_MORE { ewills_ok = 0 }
110 if *reason != LR_NEED_2_WITNESS { ewills_ok = 0 }
111 if ewills_ok != 1 { ok = 0 }
112
113 // ---- NEVER-VOID INVARIANT: targeted cases ----
114 var invariant_ok: i64 = 1
115 // A will with ALL general UETA elements but no witnesses/notary: a naive
116 // clone returns VALID. We must NOT, and must NOT route via UETA_GENERAL.
117 let nv1: i64 = nx_legal_verdict(DT_WILL, 1, 1, 1, 1, 1, 0, 0, reason, regime)
118 if nv1 == LV_VALID { invariant_ok = 0 }
119 if *regime == RG_UETA_GENERAL { invariant_ok = 0 }
120 // A will e-signed where no e-wills statute exists -> VOID / wet required.
121 let nv2: i64 = nx_legal_verdict(DT_WILL, 0, 1, 1, 1, 1, 2, 1, reason, regime)
122 if nv2 != LV_INVALID_VOID { invariant_ok = 0 }
123 if *regime != RG_REQUIRES_WET { invariant_ok = 0 }
124 if *reason != LR_EXCLUDED_WET { invariant_ok = 0 }
125 // testamentary trust gets the same protection
126 let nv3: i64 = nx_legal_verdict(DT_TESTAMENTARY_TRUST, 0, 1, 1, 1, 1, 2, 1, reason, regime)
127 if nv3 != LV_INVALID_VOID { invariant_ok = 0 }
128 if invariant_ok != 1 { ok = 0 }
129
130 // ---- NEVER-VOID INVARIANT: EXHAUSTIVE adversarial sweep ----
131 var sweep_ok: i64 = 1
132 if sweep_excluded(DT_WILL, reason, regime) != 1 { sweep_ok = 0 }
133 if sweep_excluded(DT_CODICIL, reason, regime) != 1 { sweep_ok = 0 }
134 if sweep_excluded(DT_TESTAMENTARY_TRUST, reason, regime) != 1 { sweep_ok = 0 }
135 if sweep_ok != 1 { ok = 0 }
136
137 // ---- required-elements introspection (concierge UX driver) ----
138 var req_ok: i64 = 1
139 if nx_legal_required_elements(RG_UETA_GENERAL) != (REQ_INTENT + REQ_CONSENT + REQ_ATTRIBUTION + REQ_RETAINABLE) { req_ok = 0 }
140 if nx_legal_required_elements(RG_ELECTRONIC_WILLS) != (REQ_INTENT + REQ_ATTRIBUTION + REQ_WITNESS2 + REQ_NOTARY + REQ_RETAINABLE) { req_ok = 0 }
141 if req_ok != 1 { ok = 0 }
142
143 // ---- evidence ----
144 var fd: i64 = 1
145 while fd >= 1 {
146 ew(fd, "LEGALCOMPLIANCEGATE authored=organ grounded=UETA+ESIGN+UEWA classify_ok=" as *u8); ewn(fd, classify_ok)
147 ew(fd, " ueta_general_ok=" as *u8); ewn(fd, ueta_ok)
148 ew(fd, " ewills_regime_ok=" as *u8); ewn(fd, ewills_ok)
149 ew(fd, " never_void_targeted=" as *u8); ewn(fd, invariant_ok)
150 ew(fd, " never_void_exhaustive_sweep=" as *u8); ewn(fd, sweep_ok)
151 ew(fd, " (96 combos x 3 excluded types) required_elements_ok=" as *u8); ewn(fd, req_ok)
152 if ok == 1 { ew(fd, " verdict=GREEN\n" as *u8) } else { ew(fd, " verdict=RED\n" as *u8) }
153 if fd == 1 {
154 let lf: i64 = sys_openat_append(LC_LOG, 420)
155 if lf >= 1 { fd = lf } else { fd = 0 }
156 } else {
157 sys_close(fd); fd = 0
158 }
159 }
160
161 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
162 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
163 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
164 let ctr__dry: *i64 = gv_ctr()
165 ctr__dry[0] = ok
166 ctr__dry[1] = 1
167 let rc__dry: i64 = gv_verdict("LEGAL-COMPLIANCE-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
168 sys_exit(rc__dry)
169 return rc__dry
170}