nx_legal_compliance.nx source
↩ module page · 209 lines · 10296 B
1// nx_legal_compliance.nx -- LEGAL RUNG D0: the document-type -> legal-regime ->
2// validity-requirements engine. The s-class-exceed core that makes the whole
3// document/e-sign track produce LEGALLY VALID instruments BY CONSTRUCTION.
4//
5// module: nishi-core.legal.compliance
6// depends: (none -- pure classifier + regime table + validity machine)
7// capability: LEGAL_COMPLIANCE
8//
9// THE PROBLEM a naive DocuSign-clone gets wrong: ESIGN (15 U.S.C. 7003(a)(1))
10// and UETA (Section 3(b)(1)) EXCLUDE wills, codicils, and testamentary trusts
11// from electronic-signature coverage. A plain e-signature on a will is
12// LEGALLY VOID. A few states (Utah among the first, via the Uniform Electronic
13// Wills Act) permit electronic wills only under a STRICTER regime (testator's
14// e-signature + >=2 witnesses in electronic presence + notarization to be
15// self-proving). This engine routes every document to the correct regime and
16// NEVER returns VALID for an instrument that would be legally void --
17// the legal analog of the Rule 26 never-brick / never-poison / never-ignite
18// safety invariant: NEVER PRODUCE A LEGALLY VOID INSTRUMENT.
19//
20// Grounded facts (verified against the banked corpus, not assumed):
21// UETA Section 2(8) : e-signature = symbol/process LOGICALLY ASSOCIATED with
22// the record, executed WITH INTENT TO SIGN.
23// UETA Section 5(b) : applies only where each party AGREED to transact
24// electronically (consent).
25// UETA Section 8 : record must be RETAINABLE by the recipient.
26// UETA Section 9 : ATTRIBUTION via a security procedure.
27// UETA Section 12 : retention must accurately reflect + remain accessible.
28// UETA Section 3(b) / ESIGN 7003(a) : wills/codicils/testamentary trusts EXCLUDED.
29// Uniform Electronic Wills Act : e-will needs testator e-sign + >=2 witnesses
30// (electronic presence) + notarization (self-proving).
31// NOTE: a LIVING (inter-vivos/revocable) trust and a LIVING WILL / advance
32// directive are NOT testamentary -> they are e-signable under UETA. This
33// distinction (testamentary vs inter-vivos) is the precision a generic
34// e-sign product lacks.
35//
36// license_tier: ORIGINAL (statute is fact, not copyrightable; logic re-derived)
37// lineage_id: nishi_legal_compliance_d0
38//
39// nx_safety_envelope:
40// intended_use: "Classify a document, select its legal e-signature regime,
41// and decide VALID / NEEDS_MORE / INVALID_VOID with reason."
42// sil_target: SIL3 (a false VALID on a will = a void instrument = a
43// client's estate fails -- highest legal stakes)
44// evidence: [UETA_grounded_elements, testamentary_exclusion,
45// never_void_invariant_gate, living-vs-testamentary_classifier]
46// verdict: NOT_YET_EVALUATED
47
48// ---- Document types. ----
49const DT_GENERAL: i64 = 0 // generic business/commercial record
50const DT_CONTRACT: i64 = 1
51const DT_POA: i64 = 2 // power of attorney (e-signable; notary often advised)
52const DT_DEED: i64 = 3 // deed (e-signable with notarization)
53const DT_TRUST_LIVING: i64 = 4 // inter-vivos / revocable trust (e-signable)
54const DT_ADVANCE_DIRECTIVE: i64 = 5 // living will / healthcare directive (e-signable)
55const DT_WILL: i64 = 6 // last will & testament (EXCLUDED from UETA/ESIGN)
56const DT_CODICIL: i64 = 7 // amendment to a will (EXCLUDED)
57const DT_TESTAMENTARY_TRUST: i64 = 8 // testamentary trust (EXCLUDED)
58const DT_UNKNOWN: i64 = 9
59
60// ---- Legal regimes. ----
61const RG_UETA_GENERAL: i64 = 0 // general e-sign valid (UETA / ESIGN)
62const RG_ELECTRONIC_WILLS: i64 = 1 // special e-wills regime (jurisdiction permits)
63const RG_REQUIRES_WET: i64 = 2 // e-sign legally void; needs wet ink + witnesses
64const RG_UNKNOWN: i64 = 3
65
66// ---- Verdicts. ----
67const LV_VALID: i64 = 0 // meets all legal requirements -> enforceable
68const LV_NEEDS_MORE: i64 = 1 // in-scope but missing a required element
69const LV_INVALID_VOID: i64 = 2 // e-signature cannot validly execute this instrument
70
71// ---- Reason codes. ----
72const LR_OK_UETA: i64 = 0
73const LR_OK_EWILLS: i64 = 1
74const LR_NO_INTENT: i64 = 2
75const LR_NO_CONSENT: i64 = 3
76const LR_NO_ATTRIBUTION: i64 = 4
77const LR_NOT_RETAINABLE: i64 = 5
78const LR_NEED_2_WITNESS: i64 = 6
79const LR_NEED_NOTARY: i64 = 7
80const LR_EXCLUDED_WET: i64 = 8 // excluded doc; no e-wills statute -> wet required
81const LR_UNKNOWN: i64 = 9
82
83// ---- Required-element bitmask (drives the concierge "what's still needed" UX). ----
84const REQ_INTENT: i64 = 1
85const REQ_CONSENT: i64 = 2
86const REQ_ATTRIBUTION: i64 = 4
87const REQ_RETAINABLE: i64 = 8
88const REQ_WITNESS2: i64 = 16
89const REQ_NOTARY: i64 = 32
90
91func nx_legal_verdict_name(v: i64) -> *u8 {
92 if v == LV_VALID { return "VALID" }
93 if v == LV_NEEDS_MORE { return "NEEDS_MORE" }
94 if v == LV_INVALID_VOID { return "INVALID_VOID" }
95 return "?"
96}
97func nx_legal_regime_name(r: i64) -> *u8 {
98 if r == RG_UETA_GENERAL { return "UETA_GENERAL" }
99 if r == RG_ELECTRONIC_WILLS { return "ELECTRONIC_WILLS" }
100 if r == RG_REQUIRES_WET { return "REQUIRES_WET" }
101 return "UNKNOWN_REGIME"
102}
103
104// ---- byte helpers ----
105func lc_lower(c: i64) -> i64 { if c >= 65 { if c <= 90 { return c + 32 } } return c }
106func lc_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
107
108// case-insensitive substring test: does `hay` contain `needle` (needle lowercase)?
109func lc_ci_contains(hay: *u8, needle: *u8) -> i64 {
110 let hlen: i64 = lc_slen(hay)
111 let nlen: i64 = lc_slen(needle)
112 if nlen == 0 { return 1 }
113 if nlen > hlen { return 0 }
114 var i: i64 = 0
115 while i + nlen <= hlen {
116 var k: i64 = 0
117 var hit: i64 = 1
118 while k < nlen {
119 if lc_lower(hay[i + k] & 0xff) != (needle[k] as i64) { hit = 0; break }
120 k = k + 1
121 }
122 if hit == 1 { return 1 }
123 i = i + 1
124 }
125 return 0
126}
127
128// ---- Classify a document-type label into a doc type. ----
129//
130// Order matters: the most specific / highest-stakes patterns are tested first
131// so "living will" never falls through to DT_WILL and "testamentary trust"
132// never falls through to DT_TRUST_LIVING.
133func nx_legal_doc_classify(label: *u8) -> i64 {
134 if lc_ci_contains(label, "testamentary" as *u8) == 1 { return DT_TESTAMENTARY_TRUST }
135 if lc_ci_contains(label, "codicil" as *u8) == 1 { return DT_CODICIL }
136 if lc_ci_contains(label, "living will" as *u8) == 1 { return DT_ADVANCE_DIRECTIVE }
137 if lc_ci_contains(label, "advance directive" as *u8) == 1 { return DT_ADVANCE_DIRECTIVE }
138 if lc_ci_contains(label, "advance health" as *u8) == 1 { return DT_ADVANCE_DIRECTIVE }
139 if lc_ci_contains(label, "will" as *u8) == 1 { return DT_WILL }
140 if lc_ci_contains(label, "power of attorney" as *u8) == 1 { return DT_POA }
141 if lc_ci_contains(label, "deed" as *u8) == 1 { return DT_DEED }
142 if lc_ci_contains(label, "trust" as *u8) == 1 { return DT_TRUST_LIVING }
143 if lc_ci_contains(label, "contract" as *u8) == 1 { return DT_CONTRACT }
144 if lc_ci_contains(label, "agreement" as *u8) == 1 { return DT_CONTRACT }
145 return DT_GENERAL
146}
147
148// is this an excluded testamentary instrument?
149func nx_legal_is_excluded(doc_type: i64) -> i64 {
150 if doc_type == DT_WILL { return 1 }
151 if doc_type == DT_CODICIL { return 1 }
152 if doc_type == DT_TESTAMENTARY_TRUST { return 1 }
153 return 0
154}
155
156// ---- Select the legal regime for a doc type given the jurisdiction. ----
157func nx_legal_regime(doc_type: i64, e_wills_allowed: i64) -> i64 {
158 if nx_legal_is_excluded(doc_type) == 1 {
159 if e_wills_allowed == 1 { return RG_ELECTRONIC_WILLS }
160 return RG_REQUIRES_WET
161 }
162 return RG_UETA_GENERAL
163}
164
165// ---- The required elements for a regime (bitmask). ----
166func nx_legal_required_elements(regime: i64) -> i64 {
167 if regime == RG_UETA_GENERAL { return REQ_INTENT + REQ_CONSENT + REQ_ATTRIBUTION + REQ_RETAINABLE }
168 if regime == RG_ELECTRONIC_WILLS { return REQ_INTENT + REQ_ATTRIBUTION + REQ_WITNESS2 + REQ_NOTARY + REQ_RETAINABLE }
169 return 0
170}
171
172// ---- The validity verdict for a document + its execution facts. ----
173//
174// intent/consent/attribution/retainable/notarized are 0/1 flags; witnesses is
175// a count. *out_reason gets a reason code; *out_regime gets the chosen regime.
176// INVARIANT (proven by the gate): an excluded testamentary instrument NEVER
177// returns LV_VALID through RG_UETA_GENERAL -- it is always routed to the
178// stricter e-wills regime or REQUIRES_WET.
179func nx_legal_verdict(doc_type: i64, e_wills_allowed: i64,
180 intent: i64, consent: i64, attribution: i64,
181 retainable: i64, witnesses: i64, notarized: i64,
182 out_reason: *i64, out_regime: *i64) -> i64 {
183 let regime: i64 = nx_legal_regime(doc_type, e_wills_allowed)
184 *out_regime = regime
185
186 if regime == RG_REQUIRES_WET {
187 *out_reason = LR_EXCLUDED_WET
188 return LV_INVALID_VOID
189 }
190 if regime == RG_UETA_GENERAL {
191 if intent == 0 { *out_reason = LR_NO_INTENT; return LV_NEEDS_MORE }
192 if consent == 0 { *out_reason = LR_NO_CONSENT; return LV_NEEDS_MORE }
193 if attribution == 0 { *out_reason = LR_NO_ATTRIBUTION; return LV_NEEDS_MORE }
194 if retainable == 0 { *out_reason = LR_NOT_RETAINABLE; return LV_NEEDS_MORE }
195 *out_reason = LR_OK_UETA
196 return LV_VALID
197 }
198 if regime == RG_ELECTRONIC_WILLS {
199 if intent == 0 { *out_reason = LR_NO_INTENT; return LV_NEEDS_MORE }
200 if attribution == 0 { *out_reason = LR_NO_ATTRIBUTION; return LV_NEEDS_MORE }
201 if witnesses < 2 { *out_reason = LR_NEED_2_WITNESS; return LV_NEEDS_MORE }
202 if notarized == 0 { *out_reason = LR_NEED_NOTARY; return LV_NEEDS_MORE }
203 if retainable == 0 { *out_reason = LR_NOT_RETAINABLE; return LV_NEEDS_MORE }
204 *out_reason = LR_OK_EWILLS
205 return LV_VALID
206 }
207 *out_reason = LR_UNKNOWN
208 return LV_INVALID_VOID
209}