code wiki / _hdl_build / nx_hr_graph_access.nx
nx_hr_graph_access.nx source
↩ module page · 131 lines · 8966 B
1// nx_hr_graph_access.nx -- NISHI HR's new capability (operator 2026-07-16): "HR gains the capability to use
2// the FROM-GOD family graph (source of truth) to ASSIGN + COORDINATE access privileges, in partnership with
3// the PM who guides priorities." HR is the accountable role for manage_users/access; this makes the ecosystem
4// GRAPH the root of trust for who-can-access-what, instead of a hand-maintained ACL that drifts.
5//
6// ★THE NOVEL SOVEREIGN INVARIANT (security-by-construction): PROVENANCE OUTRANKS AUTHORITY. A capability that
7// is NOT from-god-rooted (an orphan / island in the family tree -- no traced lineage to the root) is
8// UNGRANTABLE -- even the HR operator (level-3 god within the realm) CANNOT grant access to it. Rationale:
9// granting access to something with no provenance is unsafe by construction (you cannot vouch for what you
10// cannot trace to god). Root it first (nx_genesis_trace / from-god rooting), THEN access can be assigned.
11// This is strictly stronger than Zanzibar/ReBAC alone (which would let an admin grant anything): here the
12// GRAPH gates the admin. Composes -- does NOT rebuild -- nx_rebac (the tuple engine) for the actual grant.
13//
14// PM PARTNERSHIP: every assignment carries a priority (from nishi_project / the PM board); the verdict passes
15// it through so the PM's ranking orders which grants land first -- HR executes, PM prioritizes.
16//
17// This organ ships the PURE, adversarially-GATED verdict (the invariant, un-gameable) + a real from-god
18// rooted-set check (reads the graph's rooted projection). The live grant-emit = compose hga_verdict ->
19// rb_store_tuple (nx_rebac) -- the named next wiring; the DECISION is proven here.
20// nx_hr_graph_access gate -- adversarial self-test (default; expect_exit 0)
21// nx_hr_graph_access check <object> <hr_level> <rooted-set-file> -- live verdict for a real object
22// license_tier: ORIGINAL module: nishi-core.hr.graph_access
23import "nx_syscalls.nx"
24import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
25const HGA_MAGIC_1048576: i64 = 1048576
26const HGA_MAGIC_1048575: i64 = 1048575
27
28// verdicts
29const HGA_GRANT: i64 = 1 // rooted + authorized -> emit the rebac grant (additive tuple)
30const HGA_DENY_AUTHZ: i64 = 2 // rooted, but subject lacks HR authority -> deny-by-default
31const HGA_REFUSE_UNROOTED: i64 = 3 // object not from-god-rooted -> UNGRANTABLE by construction (the invariant)
32const HGA_OPERATOR_LEVEL: i64 = 3 // HR level that is site-operator/god-in-realm (mirrors nx_rebac RB_OPERATOR)
33
34func hga_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
35// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
36// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
37// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
38// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
39func hga_pn(v: i64) -> i64 { nxi_out(v); return 0 }
40func hga_streq(a: *u8, b: *u8) -> i64 { var i: i64 = 0; while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } if b[i] != (0 as u8) { return 0 } return 1 }
41func hga_atoi(s: *u8) -> i64 { var v: i64 = 0; var i: i64 = 0; var go: i64 = 1; while go == 1 { let c: i64 = s[i] as i64; if c < 48 { go = 0 } else { if c > 57 { go = 0 } else { v = v * 10 + (c - 48); i = i + 1 } } } return v }
42func hga_vname(v: i64) -> *u8 {
43 if v == HGA_GRANT { return "GRANT" as *u8 }
44 if v == HGA_DENY_AUTHZ { return "DENY-AUTHZ" as *u8 }
45 return "REFUSE-UNROOTED" as *u8
46}
47
48// ★THE PURE INVARIANT (un-gameable, gated). rooted: 1 iff object traces to god in the family graph (else 0,
49// INCLUDING any non-0/1 = fail-closed). hr_level: subject's HR level. The order MATTERS: provenance is
50// checked FIRST, so an un-rooted object is refused before authority is even consulted -> authority can never
51// bypass provenance. Returns a verdict code.
52func hga_verdict(rooted: i64, hr_level: i64) -> i64 {
53 if rooted != 1 { return HGA_REFUSE_UNROOTED } // provenance gate -- BEFORE authority (the whole point)
54 if hr_level >= HGA_OPERATOR_LEVEL { return HGA_GRANT }
55 return HGA_DENY_AUTHZ
56}
57
58// from-god rooted check: is <object> present in the graph's rooted-set projection (one name per line)?
59// The rooted-set is the family graph's answer to "what has traced lineage to god" (the complement of the
60// honesty audit's [I] islands / un-rooted orphans). Comment(35)/blank lines skipped. 1 = rooted, 0 = orphan.
61func hga_is_rooted(object: *u8, path: *u8) -> i64 {
62 let fd: i64 = sys_openat_rd(path)
63 if fd < 0 { return 0 } // no rooted-set -> fail-closed (treat as orphan)
64 let buf: *u8 = sys_mmap(HGA_MAGIC_1048576)
65 var n: i64 = 0; var r: i64 = 1
66 while r > 0 { r = sys_read(fd, (buf as i64 + n) as *u8, HGA_MAGIC_1048575 - n); if r > 0 { n = n + r } if n >= HGA_MAGIC_1048575 { r = 0 } }
67 sys_close(fd)
68 var ls: i64 = 0; var i: i64 = 0
69 var found: i64 = 0
70 while i <= n {
71 var eol: i64 = 0
72 if i >= n { eol = 1 } else { if buf[i] == (10 as u8) { eol = 1 } }
73 if eol == 1 {
74 if i > ls { if buf[ls] != (35 as u8) {
75 // compare buf[ls..i) to object (trim trailing CR)
76 var e: i64 = i
77 if e > ls { if buf[e-1] == (13 as u8) { e = e - 1 } }
78 var k: i64 = 0
79 var m: i64 = 1
80 var p: i64 = ls
81 while p < e { if object[k] == (0 as u8) { m = 0; p = e } else { if buf[p] != object[k] { m = 0; p = e } else { k = k + 1; p = p + 1 } } }
82 if m == 1 { if object[k] == (0 as u8) { found = 1; i = n } }
83 } }
84 ls = i + 1
85 }
86 i = i + 1
87 }
88 return found
89}
90
91// ---- adversarial gate: the invariant must hold, and PROVENANCE MUST OUTRANK AUTHORITY ----
92func hga_gate() -> i64 {
93 hga_p("=== nx_hr_graph_access GATE -- provenance outranks authority (from-god access invariant) ===\n" as *u8)
94 var fail: i64 = 0
95 // T1 rooted + operator -> GRANT
96 if hga_verdict(1, 3) == HGA_GRANT { hga_p(" [PASS] rooted + HR-operator -> GRANT\n" as *u8) } else { hga_p(" [FAIL] rooted+op\n" as *u8); fail = 1 }
97 // T2 ★the teeth: orphan + operator(god) -> STILL REFUSE (authority cannot bypass provenance)
98 if hga_verdict(0, 3) == HGA_REFUSE_UNROOTED { hga_p(" [PASS] orphan + HR-operator(god) -> REFUSE-UNROOTED (authority CANNOT grant an un-rooted orphan)\n" as *u8) } else { hga_p(" [FAIL] orphan+op should refuse\n" as *u8); fail = 1 }
99 // T3 rooted + non-operator -> DENY-AUTHZ (deny-by-default)
100 if hga_verdict(1, 1) == HGA_DENY_AUTHZ { hga_p(" [PASS] rooted + non-operator -> DENY-AUTHZ\n" as *u8) } else { hga_p(" [FAIL] rooted+low\n" as *u8); fail = 1 }
101 // T4 orphan + non-operator -> REFUSE-UNROOTED (provenance gate fires first)
102 if hga_verdict(0, 1) == HGA_REFUSE_UNROOTED { hga_p(" [PASS] orphan + non-operator -> REFUSE-UNROOTED (provenance checked before authority)\n" as *u8) } else { hga_p(" [FAIL] orphan+low\n" as *u8); fail = 1 }
103 // T5 NEG-CONTROL: a corrupt/invalid rooted flag (2) must FAIL-CLOSED to REFUSE (never accidentally grant)
104 if hga_verdict(2, 3) == HGA_REFUSE_UNROOTED { hga_p(" [PASS] neg-control: invalid rooted flag -> fail-closed REFUSE\n" as *u8) } else { hga_p(" [FAIL] invalid-flag not fail-closed\n" as *u8); fail = 1 }
105 if fail == 0 { hga_p("=== GATE GREEN: the graph gates the admin -- un-rooted = ungrantable, by construction ===\n" as *u8); sys_exit(0); return 0 }
106 hga_p("=== GATE RED ===\n" as *u8); sys_exit(1); return 1
107}
108
109func main(argc: i64, argv: *i64) -> i64 {
110 if argc < 2 { return hga_gate() }
111 let verb: *u8 = argv[1] as *u8
112 if hga_streq(verb, "gate" as *u8) == 1 { return hga_gate() }
113 if hga_streq(verb, "check" as *u8) == 1 {
114 if argc < 5 { hga_p("usage: nx_hr_graph_access check <object> <hr_level> <rooted-set-file>\n" as *u8); return 2 }
115 let object: *u8 = argv[2] as *u8
116 let lvl: i64 = hga_atoi(argv[3] as *u8)
117 let rooted: i64 = hga_is_rooted(object, argv[4] as *u8)
118 let v: i64 = hga_verdict(rooted, lvl)
119 hga_p("HR-graph-access: object=" as *u8); hga_p(object)
120 hga_p(" rooted=" as *u8); hga_pn(rooted)
121 hga_p(" hr_level=" as *u8); hga_pn(lvl)
122 hga_p(" -> " as *u8); hga_p(hga_vname(v))
123 if v == HGA_GRANT { hga_p(" (HR emits the rebac tuple; PM priority orders it)\n" as *u8) }
124 else { if v == HGA_REFUSE_UNROOTED { hga_p(" (root it in the family tree first -- access requires provenance)\n" as *u8) }
125 else { hga_p(" (only HR/operator assigns access)\n" as *u8) } }
126 if v == HGA_GRANT { return 0 }
127 return v
128 }
129 hga_p("usage: nx_hr_graph_access gate | check <object> <hr_level> <rooted-set-file>\n" as *u8)
130 return 2
131}