interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / aienux / aegis / aegis.om

970 lines · 23496 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/aegis/aegis.om: Pure-Omega AIENOS AEGIS Capability Graph, Effect Intent,
3note Operator Grant, J-Space World, Policy Evaluator, and Effect Broker.
4note Converted from aienux crates/aienos-aegis/src/{capability,effect,world,evaluator,broker}.rs.
5note Compose with: si/directives/om2.om om/tls/sha2.om aienux/aegis/aegis.om ...
6
7const AEGIS_MAX_CAPS ulong 32
8const AEGIS_MAX_DELEG_DEPTH uint 4
9const AEGIS_MAX_STR ulong 64
10const AEGIS_MAX_WORLDS ulong 8
11const AEGIS_MAX_AUDIT ulong 64
12
13const AEGIS_SCOPE_SYSADMIN uchar 1
14const AEGIS_SCOPE_FS uchar 2
15const AEGIS_SCOPE_NET uchar 3
16const AEGIS_SCOPE_DEVICE uchar 4
17const AEGIS_SCOPE_WORLD uchar 5
18const AEGIS_SCOPE_STANDING_FETCH uchar 6
19const AEGIS_SCOPE_CUSTOM uchar 7
20
21const AEGIS_DEC_APPROVED uchar 1
22const AEGIS_DEC_REQUIRES_OP uchar 2
23const AEGIS_DEC_REJECTED uchar 3
24
25const AEGIS_OK int 0
26const AEGIS_ERR_NOT_FOUND int -1
27const AEGIS_ERR_BAD_SIG int -2
28const AEGIS_ERR_REVOKED int -3
29const AEGIS_ERR_EXPIRED int -4
30const AEGIS_ERR_HOLDER int -5
31const AEGIS_ERR_SCOPE int -6
32const AEGIS_ERR_DEPTH int -7
33const AEGIS_ERR_MISSING_GRANT int -8
34const AEGIS_ERR_GRANT_INTENT int -9
35const AEGIS_ERR_GRANT_SIG int -10
36const AEGIS_ERR_WORLD_STATE int -11
37const AEGIS_ERR_CAPACITY int -12
38
39const AEGIS_WORLD_NONE uchar 0
40const AEGIS_WORLD_ACTIVE uchar 1
41const AEGIS_WORLD_COMMITTED uchar 2
42const AEGIS_WORLD_ROLLEDBACK uchar 3
43
44text aegis_dom_cap "AIENOS_CAP_V1"
45text aegis_dom_intent "AIENOS_INTENT_V1"
46text aegis_dom_grant "AIENOS_GRANT_V1"
47text aegis_act_fs_read "fs.read"
48text aegis_act_fs_write "fs.write"
49text aegis_act_fs_delete "fs.delete"
50text aegis_act_net_fetch "net.fetch"
51text aegis_act_net_read "net.read"
52text aegis_act_net_connect "net.connect"
53text aegis_act_net_send "net.send"
54text aegis_act_dev_read "device.read"
55text aegis_act_dev_write "device.write"
56text aegis_act_hw_inspect "hardware.inspect"
57text aegis_act_disk_format "disk.format"
58text aegis_act_git_pub "git.publish"
59text aegis_act_code_pub "code.publish"
60text aegis_act_kern_rep "kernel.replace"
61text aegis_act_sys_inst "system.install"
62text aegis_act_cred_mod "credential.modify"
63text aegis_act_fin_spend "finance.spend"
64text aegis_pfx_device "device."
65text aegis_pfx_world "world."
66
67global aegis_op_secret uchar 32
68global aegis_cap_secret uchar 32
69
70global aegis_cap_used uchar 32
71global aegis_cap_revoked uchar 32
72global aegis_cap_id uchar 512
73global aegis_cap_parent int 32
74global aegis_cap_depth uint 32
75global aegis_cap_holder ulong 32
76global aegis_cap_scope uchar 32
77global aegis_cap_ro uchar 32
78global aegis_cap_port uint 32
79global aegis_cap_exp ulong 32
80global aegis_cap_str uchar 2048
81global aegis_cap_slen ulong 32
82global aegis_cap_str2 uchar 2048
83global aegis_cap_s2len ulong 32
84global aegis_cap_sig uchar 1024
85
86global aegis_w_status uchar 8
87global aegis_w_id ulong 8
88global aegis_w_deltas uint 8
89global aegis_w_writes uint 8
90global aegis_w_deletes uint 8
91
92global aegis_audit_cnt ulong 1
93global aegis_audit_dec uchar 64
94global aegis_audit_rc int 64
95global aegis_audit_exec uchar 64
96global aegis_audit_ts ulong 64
97global aegis_audit_iid uchar 1024
98
99global aegis_cur_iid uchar 16
100global aegis_cur_grant_iid uchar 16
101global aegis_cur_grant_sig uchar 32
102note Grant meta: 0=present, 1=granted_at_utc, 2=op_id_len, 3=mutant_skip_grant
103global aegis_cur_grant_meta ulong 4
104global aegis_cur_grant_op uchar 64
105
106note Last eval result: 0=decision, 1=reason_code (cast via long)
107global aegis_last_dec uchar 1
108global aegis_last_rc int 1
109
110note Dispatch context: 0=world_id, 1=is_reversible, 2=now_utc, 3=net_port
111global aegis_dctx ulong 4
112
113note Scratch buffers for hashing (never passed as globals)
114global aegis_tmp_s1 uchar 64
115global aegis_tmp_s2 uchar 64
116global aegis_tmp_lens ulong 2
117
118fn aegis_dispatch_ctx world_id ulong is_rev ulong now_utc ulong port ulong to void
119put aegis_dctx 0 world_id
120put aegis_dctx 1 is_rev
121put aegis_dctx 2 now_utc
122put aegis_dctx 3 port
123end
124
125fn aegis_reset op_sec view uchar 32 cap_sec view uchar 32 to void
126for i ulong 32
127put aegis_op_secret i get op_sec i
128put aegis_cap_secret i get cap_sec i
129put aegis_cap_used i 0
130put aegis_cap_revoked i 0
131put aegis_cap_parent i -1
132put aegis_cap_depth i 0
133put aegis_cap_holder i 0
134put aegis_cap_scope i 0
135put aegis_cap_ro i 0
136put aegis_cap_port i 0
137put aegis_cap_exp i 0
138put aegis_cap_slen i 0
139put aegis_cap_s2len i 0
140end
141for w ulong 8
142put aegis_w_status w AEGIS_WORLD_NONE
143put aegis_w_id w 0
144put aegis_w_deltas w 0
145put aegis_w_writes w 0
146put aegis_w_deletes w 0
147end
148put aegis_audit_cnt 0 0
149put aegis_last_dec 0 0
150put aegis_last_rc 0 0
151for g ulong 4
152put aegis_cur_grant_meta g 0
153put aegis_dctx g 0
154end
155end
156
157fn aegis_set_mutant_skip_grant v ulong to void
158put aegis_cur_grant_meta 3 v
159end
160
161note ---- Byte/String & Lexical Path Helpers ----
162
163fn aegis_str_eq a view uchar * b view uchar * to bool
164if ne len a len b
165ret false
166end
167n = len a
168i = ulong 0
169while lt i n bound 256
170if ne get a i get b i
171ret false
172end
173i = add i 1
174end
175ret true
176end
177
178fn aegis_str_starts a view uchar * pfx view uchar * to bool
179if lt len a len pfx
180ret false
181end
182n = len pfx
183i = ulong 0
184while lt i n bound 256
185if ne get a i get pfx i
186ret false
187end
188i = add i 1
189end
190ret true
191end
192
193fn aegis_has_dotdot p view uchar * to bool
194n = len p
195i = ulong 0
196while lt i n bound 256
197if eq get p i 46
198if lt add i 1 n
199if eq get p add i 1 46
200prev_ok = eq i 0
201if gt i 0
202if eq get p sub i 1 47
203prev_ok = true
204end
205end
206next_ok = eq add i 2 n
207if lt add i 2 n
208if eq get p add i 2 47
209next_ok = true
210end
211end
212if and prev_ok next_ok
213ret true
214end
215end
216end
217end
218i = add i 1
219end
220ret false
221end
222
223note Lexical absolute path confinement (mirrors capability.rs path_within).
224note Both path and prefix must start with '/', contain no '..' component,
225note and path must equal prefix or be inside prefix/ (prefix "/" matches any absolute path).
226fn aegis_path_within path view uchar * prefix view uchar * to bool
227plen = len path
228xlen = len prefix
229if or eq plen 0 eq xlen 0
230ret false
231end
232if or ne get path 0 47 ne get prefix 0 47
233ret false
234end
235if or aegis_has_dotdot path aegis_has_dotdot prefix
236ret false
237end
238if and eq xlen 1 eq get prefix 0 47
239ret true
240end
241note Trim trailing '/' on prefix for comparison
242ex = xlen
243while and gt ex 1 eq get prefix sub ex 1 47 bound 64
244ex = sub ex 1
245end
246if lt plen ex
247ret false
248end
249i = ulong 0
250while lt i ex bound 256
251if ne get path i get prefix i
252ret false
253end
254i = add i 1
255end
256if eq plen ex
257ret true
258end
259ret eq get path ex 47
260end
261
262note ---- SHA-256 Encoding Helpers ----
263
264fn aegis_put_be64 buf edit uchar 8 v ulong to void
265put buf 0 uchar and shr v 56 0xff
266put buf 1 uchar and shr v 48 0xff
267put buf 2 uchar and shr v 40 0xff
268put buf 3 uchar and shr v 32 0xff
269put buf 4 uchar and shr v 24 0xff
270put buf 5 uchar and shr v 16 0xff
271put buf 6 uchar and shr v 8 0xff
272put buf 7 uchar and v 0xff
273end
274
275fn aegis_sha_u64 c edit ulong 40 v ulong to void
276b = new uchar 8 0
277aegis_put_be64 b v
278sha256_update c b
279end
280
281fn aegis_sha_field c edit ulong 40 f view uchar * to void
282aegis_sha_u64 c len f
283if gt len f 0
284sha256_update c f
285end
286end
287
288fn aegis_copy_cap_s1 slot ulong dst edit uchar 64 to ulong
289n = get aegis_cap_slen slot
290base = mul slot AEGIS_MAX_STR
291i = ulong 0
292while lt i n bound 64
293put dst i get aegis_cap_str add base i
294i = add i 1
295end
296ret n
297end
298
299fn aegis_copy_cap_s2 slot ulong dst edit uchar 64 to ulong
300n = get aegis_cap_s2len slot
301base = mul slot AEGIS_MAX_STR
302i = ulong 0
303while lt i n bound 64
304put dst i get aegis_cap_str2 add base i
305i = add i 1
306end
307ret n
308end
309
310note Compute 32-byte SHA-256 signature for capability at slot.
311fn aegis_cap_compute_sig slot ulong out_sig edit uchar 32 to void
312c = new ulong 40 0
313sec = new uchar 32 0
314s1 = new uchar 64 0
315s2 = new uchar 64 0
316for i ulong 32
317put sec i get aegis_cap_secret i
318end
319sha256_init c
320sha256_update c aegis_dom_cap
321sha256_update c sec
322aegis_sha_u64 c get aegis_cap_holder slot
323aegis_sha_u64 c ulong get aegis_cap_scope slot
324aegis_sha_u64 c ulong get aegis_cap_ro slot
325aegis_sha_u64 c ulong get aegis_cap_port slot
326aegis_sha_u64 c get aegis_cap_exp slot
327aegis_sha_u64 c ulong get aegis_cap_depth slot
328n1 = aegis_copy_cap_s1 slot s1
329aegis_sha_field c part s1 0 n1
330n2 = aegis_copy_cap_s2 slot s2
331aegis_sha_field c part s2 0 n2
332sha256_final c out_sig
333end
334
335fn aegis_eq32 a view uchar 32 b view uchar 32 to bool
336diff = uchar 0
337for i ulong 32
338diff = or diff xor get a i get b i
339end
340ret eq diff 0
341end
342
343fn aegis_eq16 a view uchar 16 b view uchar 16 to bool
344diff = uchar 0
345for i ulong 16
346diff = or diff xor get a i get b i
347end
348ret eq diff 0
349end
350
351note ---- Scope Attenuation & Action Authorization ----
352
353fn aegis_scope_contains parent_slot ulong child_scope uchar child_ro uchar child_port uint c1 view uchar * to bool
354ps = get aegis_cap_scope parent_slot
355if eq ps AEGIS_SCOPE_SYSADMIN
356ret true
357end
358if ne ps child_scope
359ret false
360end
361p1 = new uchar 64 0
362pn1 = aegis_copy_cap_s1 parent_slot p1
363pro = get aegis_cap_ro parent_slot
364pport = get aegis_cap_port parent_slot
365if eq ps AEGIS_SCOPE_FS
366if not aegis_path_within c1 part p1 0 pn1
367ret false
368end
369if and ne pro 0 eq child_ro 0
370ret false
371end
372ret true
373end
374if eq ps AEGIS_SCOPE_STANDING_FETCH
375if and eq pn1 1 eq get p1 0 42
376ret true
377end
378ret aegis_str_eq part p1 0 pn1 c1
379end
380if eq ps AEGIS_SCOPE_NET
381host_ok = false
382if and eq pn1 1 eq get p1 0 42
383host_ok = true
384else
385host_ok = aegis_str_eq part p1 0 pn1 c1
386end
387port_ok = or eq pport 0 eq pport child_port
388ret and host_ok port_ok
389end
390if eq ps AEGIS_SCOPE_DEVICE
391if not aegis_str_eq part p1 0 pn1 c1
392ret false
393end
394if and ne pro 0 eq child_ro 0
395ret false
396end
397ret true
398end
399if eq ps AEGIS_SCOPE_WORLD
400ret le child_port pport
401end
402ret false
403end
404
405note ---- Capability Graph Operations ----
406
407fn aegis_find_free_cap to long
408i = ulong 0
409while lt i AEGIS_MAX_CAPS bound 32
410if eq get aegis_cap_used i 0
411ret long i
412end
413i = add i 1
414end
415ret -1
416end
417
418fn aegis_store_cap_fields slot ulong holder ulong scope uchar ro uchar port uint exp ulong to void
419put aegis_cap_used slot 1
420put aegis_cap_revoked slot 0
421put aegis_cap_holder slot holder
422put aegis_cap_scope slot scope
423put aegis_cap_ro slot ro
424put aegis_cap_port slot port
425put aegis_cap_exp slot exp
426end
427
428fn aegis_store_cap_strs slot ulong s1 view uchar * s2 view uchar * to void
429n1 = len s1
430if gt n1 AEGIS_MAX_STR
431n1 = AEGIS_MAX_STR
432end
433put aegis_cap_slen slot n1
434b1 = mul slot AEGIS_MAX_STR
435i = ulong 0
436while lt i n1 bound 64
437put aegis_cap_str add b1 i get s1 i
438i = add i 1
439end
440n2 = len s2
441if gt n2 AEGIS_MAX_STR
442n2 = AEGIS_MAX_STR
443end
444put aegis_cap_s2len slot n2
445b2 = mul slot AEGIS_MAX_STR
446j = ulong 0
447while lt j n2 bound 64
448put aegis_cap_str2 add b2 j get s2 j
449j = add j 1
450end
451end
452
453fn aegis_finalize_cap slot ulong to void
454sig = new uchar 32 0
455aegis_cap_compute_sig slot sig
456sb = mul slot 32
457ib = mul slot 16
458for i ulong 32
459put aegis_cap_sig add sb i get sig i
460end
461for k ulong 16
462put aegis_cap_id add ib k get sig k
463end
464end
465
466note Issue a root capability token. Returns slot index (0..31) or negative AEGIS_ERR_*.
467fn aegis_cap_issue holder ulong scope uchar ro uchar port uint exp ulong to int
468s = aegis_find_free_cap
469if lt s 0
470ret AEGIS_ERR_CAPACITY
471end
472slot = ulong s
473put aegis_cap_parent slot -1
474put aegis_cap_depth slot 0
475aegis_store_cap_fields slot holder scope ro port exp
476put aegis_cap_slen slot 0
477put aegis_cap_s2len slot 0
478aegis_finalize_cap slot
479ret int slot
480end
481
482fn aegis_cap_set_strings slot ulong s1 view uchar * s2 view uchar * to void
483aegis_store_cap_strs slot s1 s2
484aegis_finalize_cap slot
485end
486
487fn aegis_cap_validate slot ulong now_utc ulong to int
488if ge slot AEGIS_MAX_CAPS
489ret AEGIS_ERR_NOT_FOUND
490end
491if eq get aegis_cap_used slot 0
492ret AEGIS_ERR_NOT_FOUND
493end
494if ne get aegis_cap_revoked slot 0
495ret AEGIS_ERR_REVOKED
496end
497exp = get aegis_cap_exp slot
498if and ne exp 0 gt now_utc exp
499ret AEGIS_ERR_EXPIRED
500end
501want = new uchar 32 0
502have = new uchar 32 0
503aegis_cap_compute_sig slot want
504sb = mul slot 32
505for i ulong 32
506put have i get aegis_cap_sig add sb i
507end
508if not aegis_eq32 want have
509ret AEGIS_ERR_BAD_SIG
510end
511ret AEGIS_OK
512end
513
514note Delegate an attenuated capability from parent_slot to new_holder.
515note Call aegis_set_tmp_strs(c1, c2) before calling, or use aegis_cap_delegate_strs.
516fn aegis_set_tmp_strs s1 view uchar * s2 view uchar * to void
517for z ulong 64
518put aegis_tmp_s1 z 0
519put aegis_tmp_s2 z 0
520end
521n1 = len s1
522if gt n1 64
523n1 = ulong 64
524end
525i = ulong 0
526while lt i n1 bound 64
527put aegis_tmp_s1 i get s1 i
528i = add i 1
529end
530n2 = len s2
531if gt n2 64
532n2 = ulong 64
533end
534j = ulong 0
535while lt j n2 bound 64
536put aegis_tmp_s2 j get s2 j
537j = add j 1
538end
539end
540
541fn aegis_cap_delegate parent_slot ulong new_holder ulong scope uchar ro uchar port uint exp ulong to int
542vrc = aegis_cap_validate parent_slot 0
543if ne vrc AEGIS_OK
544ret vrc
545end
546pdepth = get aegis_cap_depth parent_slot
547if ge pdepth AEGIS_MAX_DELEG_DEPTH
548ret AEGIS_ERR_DEPTH
549end
550note Expiry cannot exceed parent expiry when parent has one
551pexp = get aegis_cap_exp parent_slot
552if ne pexp 0
553if or eq exp 0 gt exp pexp
554ret AEGIS_ERR_SCOPE
555end
556end
557s = aegis_find_free_cap
558if lt s 0
559ret AEGIS_ERR_CAPACITY
560end
561slot = ulong s
562put aegis_cap_parent slot int parent_slot
563put aegis_cap_depth slot add pdepth 1
564aegis_store_cap_fields slot new_holder scope ro port exp
565put aegis_cap_slen slot 0
566put aegis_cap_s2len slot 0
567aegis_finalize_cap slot
568ret int slot
569end
570
571note Delegate with string scope (FS path, host, device) and strict attenuation check.
572fn aegis_cap_delegate_scoped parent_slot ulong new_holder ulong scope uchar ro uchar c1 view uchar * to int
573vrc = aegis_cap_validate parent_slot 0
574if ne vrc AEGIS_OK
575ret vrc
576end
577pdepth = get aegis_cap_depth parent_slot
578if ge pdepth AEGIS_MAX_DELEG_DEPTH
579ret AEGIS_ERR_DEPTH
580end
581if not aegis_scope_contains parent_slot scope ro 0 c1
582ret AEGIS_ERR_SCOPE
583end
584s = aegis_find_free_cap
585if lt s 0
586ret AEGIS_ERR_CAPACITY
587end
588slot = ulong s
589put aegis_cap_parent slot int parent_slot
590put aegis_cap_depth slot add pdepth 1
591aegis_store_cap_fields slot new_holder scope ro 0 get aegis_cap_exp parent_slot
592empty = new uchar 1 0
593aegis_store_cap_strs slot c1 part empty 0 0
594aegis_finalize_cap slot
595ret int slot
596end
597
598note Cascading subtree revocation: revoking slot revokes all descendants.
599fn aegis_cap_revoke slot ulong to int
600if ge slot AEGIS_MAX_CAPS
601ret AEGIS_ERR_NOT_FOUND
602end
603if eq get aegis_cap_used slot 0
604ret AEGIS_ERR_NOT_FOUND
605end
606put aegis_cap_revoked slot 1
607note Fixed-point pass over max 32 tokens (depth <= 4, so 5 passes always converge)
608pass = ulong 0
609while lt pass 6 bound 6
610i = ulong 0
611while lt i AEGIS_MAX_CAPS bound 32
612if and ne get aegis_cap_used i 0 eq get aegis_cap_revoked i 0
613p = get aegis_cap_parent i
614if ge p 0
615if ne get aegis_cap_revoked ulong p 0
616put aegis_cap_revoked i 1
617end
618end
619end
620i = add i 1
621end
622pass = add pass 1
623end
624ret AEGIS_OK
625end
626
627note ---- Effect Intent ID & Operator Grant Cryptography ----
628
629note Compute deterministic 16-byte EffectIntent ID with length-prefixed parameter boundaries.
630fn aegis_compute_intent_id agent_id ulong cap_slot ulong act view uchar * arg view uchar * to void
631c = new ulong 40 0
632dig = new uchar 32 0
633cid = new uchar 16 0
634if lt cap_slot AEGIS_MAX_CAPS
635cb = mul cap_slot 16
636for i ulong 16
637put cid i get aegis_cap_id add cb i
638end
639end
640sha256_init c
641sha256_update c aegis_dom_intent
642aegis_sha_u64 c agent_id
643aegis_sha_field c act
644sha256_update c cid
645aegis_sha_field c arg
646sha256_final c dig
647for k ulong 16
648put aegis_cur_iid k get dig k
649end
650end
651
652note Compute 16-byte intent ID for an explicit (key, value) pair to prove boundary separation.
653fn aegis_intent_hash_kv agent_id ulong pkey view uchar * pval view uchar * to void
654c = new ulong 40 0
655dig = new uchar 32 0
656sha256_init c
657sha256_update c aegis_dom_intent
658aegis_sha_u64 c agent_id
659aegis_sha_field c pkey
660aegis_sha_field c pval
661sha256_final c dig
662for k ulong 16
663put aegis_cur_iid k get dig k
664end
665end
666
667fn aegis_grant_compute_sig iid view uchar 16 op_id view uchar * ts ulong sec view uchar 32 out_sig edit uchar 32 to void
668c = new ulong 40 0
669sha256_init c
670sha256_update c aegis_dom_grant
671sha256_update c sec
672sha256_update c iid
673aegis_sha_field c op_id
674aegis_sha_u64 c ts
675sha256_final c out_sig
676end
677
678fn aegis_grant_clear to void
679put aegis_cur_grant_meta 0 0
680end
681
682note Issue an operator grant for the current intent ID (aegis_cur_iid) using the given secret.
683fn aegis_grant_issue_with_secret op_id view uchar * ts ulong sec view uchar 32 to void
684iid = new uchar 16 0
685sig = new uchar 32 0
686for i ulong 16
687put iid i get aegis_cur_iid i
688put aegis_cur_grant_iid i get aegis_cur_iid i
689end
690aegis_grant_compute_sig iid op_id ts sec sig
691for j ulong 32
692put aegis_cur_grant_sig j get sig j
693end
694n = len op_id
695if gt n 64
696n = ulong 64
697end
698k = ulong 0
699while lt k n bound 64
700put aegis_cur_grant_op k get op_id k
701k = add k 1
702end
703put aegis_cur_grant_meta 0 1
704put aegis_cur_grant_meta 1 ts
705put aegis_cur_grant_meta 2 n
706end
707
708fn aegis_grant_verify_current to int
709if eq get aegis_cur_grant_meta 0 0
710ret AEGIS_ERR_MISSING_GRANT
711end
712iid = new uchar 16 0
713giid = new uchar 16 0
714for i ulong 16
715put iid i get aegis_cur_iid i
716put giid i get aegis_cur_grant_iid i
717end
718if not aegis_eq16 iid giid
719ret AEGIS_ERR_GRANT_INTENT
720end
721op_buf = new uchar 64 0
722oplen = get aegis_cur_grant_meta 2
723k = ulong 0
724while lt k oplen bound 64
725put op_buf k get aegis_cur_grant_op k
726k = add k 1
727end
728sec = new uchar 32 0
729have = new uchar 32 0
730want = new uchar 32 0
731for s ulong 32
732put sec s get aegis_op_secret s
733put have s get aegis_cur_grant_sig s
734end
735aegis_grant_compute_sig giid part op_buf 0 oplen get aegis_cur_grant_meta 1 sec want
736if not aegis_eq32 want have
737ret AEGIS_ERR_GRANT_SIG
738end
739ret AEGIS_OK
740end
741
742note ---- J-Space Copy-on-Write Worlds ----
743
744fn aegis_world_create wid ulong to int
745i = ulong 0
746while lt i AEGIS_MAX_WORLDS bound 8
747if eq get aegis_w_status i AEGIS_WORLD_NONE
748put aegis_w_status i AEGIS_WORLD_ACTIVE
749put aegis_w_id i wid
750put aegis_w_deltas i 0
751put aegis_w_writes i 0
752put aegis_w_deletes i 0
753ret int i
754end
755i = add i 1
756end
757ret AEGIS_ERR_CAPACITY
758end
759
760fn aegis_world_find wid ulong to int
761i = ulong 0
762while lt i AEGIS_MAX_WORLDS bound 8
763if and ne get aegis_w_status i AEGIS_WORLD_NONE eq get aegis_w_id i wid
764ret int i
765end
766i = add i 1
767end
768ret AEGIS_ERR_NOT_FOUND
769end
770
771fn aegis_world_rollback wid ulong to int
772idx = aegis_world_find wid
773if lt idx 0
774ret AEGIS_ERR_NOT_FOUND
775end
776u = ulong idx
777put aegis_w_deltas u 0
778put aegis_w_writes u 0
779put aegis_w_deletes u 0
780put aegis_w_status u AEGIS_WORLD_ROLLEDBACK
781ret AEGIS_OK
782end
783
784fn aegis_world_commit wid ulong to int
785idx = aegis_world_find wid
786if lt idx 0
787ret AEGIS_ERR_NOT_FOUND
788end
789u = ulong idx
790if ne get aegis_w_status u AEGIS_WORLD_ACTIVE
791ret AEGIS_ERR_WORLD_STATE
792end
793put aegis_w_status u AEGIS_WORLD_COMMITTED
794ret AEGIS_OK
795end
796
797note ---- Policy Evaluator & Effect Broker ----
798
799fn aegis_is_high_impact act view uchar * to bool
800if or aegis_str_eq act aegis_act_disk_format aegis_str_eq act aegis_act_fs_delete
801ret true
802end
803if or aegis_str_eq act aegis_act_fs_write aegis_str_eq act aegis_act_git_pub
804ret true
805end
806if or aegis_str_eq act aegis_act_code_pub aegis_str_eq act aegis_act_kern_rep
807ret true
808end
809if or aegis_str_eq act aegis_act_sys_inst aegis_str_eq act aegis_act_cred_mod
810ret true
811end
812ret aegis_str_eq act aegis_act_fin_spend
813end
814
815fn aegis_scope_authorizes slot ulong act view uchar * arg view uchar * port uint to bool
816sc = get aegis_cap_scope slot
817if eq sc AEGIS_SCOPE_SYSADMIN
818ret true
819end
820p1 = new uchar 64 0
821pn1 = aegis_copy_cap_s1 slot p1
822ro = get aegis_cap_ro slot
823if eq sc AEGIS_SCOPE_FS
824if aegis_str_eq act aegis_act_fs_read
825ret aegis_path_within arg part p1 0 pn1
826end
827if or aegis_str_eq act aegis_act_fs_write aegis_str_eq act aegis_act_fs_delete
828if ne ro 0
829ret false
830end
831ret aegis_path_within arg part p1 0 pn1
832end
833ret false
834end
835if eq sc AEGIS_SCOPE_STANDING_FETCH
836if or aegis_str_eq act aegis_act_net_fetch aegis_str_eq act aegis_act_net_read
837if and eq pn1 1 eq get p1 0 42
838ret true
839end
840ret aegis_str_eq part p1 0 pn1 arg
841end
842ret false
843end
844if eq sc AEGIS_SCOPE_NET
845if or aegis_str_eq act aegis_act_net_connect aegis_str_eq act aegis_act_net_send
846host_ok = false
847if and eq pn1 1 eq get p1 0 42
848host_ok = true
849else
850host_ok = aegis_str_eq part p1 0 pn1 arg
851end
852pport = get aegis_cap_port slot
853port_ok = or eq pport 0 eq pport port
854ret and host_ok port_ok
855end
856ret false
857end
858if eq sc AEGIS_SCOPE_DEVICE
859if aegis_str_starts act aegis_pfx_device
860if not aegis_str_eq part p1 0 pn1 arg
861ret false
862end
863if and aegis_str_eq act aegis_act_dev_write ne ro 0
864ret false
865end
866ret true
867end
868ret false
869end
870if eq sc AEGIS_SCOPE_WORLD
871ret aegis_str_starts act aegis_pfx_world
872end
873ret false
874end
875
876fn aegis_record_audit dec uchar rc int executed uchar ts ulong to void
877cnt = get aegis_audit_cnt 0
878if lt cnt AEGIS_MAX_AUDIT
879put aegis_audit_dec cnt dec
880put aegis_audit_rc cnt rc
881put aegis_audit_exec cnt executed
882put aegis_audit_ts cnt ts
883ib = mul cnt 16
884for k ulong 16
885put aegis_audit_iid add ib k get aegis_cur_iid k
886end
887put aegis_audit_cnt 0 add cnt 1
888end
889put aegis_last_dec 0 dec
890put aegis_last_rc 0 rc
891end
892
893fn aegis_try_world_delta act view uchar * to bool
894world_id = get aegis_dctx 0
895if or eq world_id 0 eq get aegis_dctx 1 0
896ret false
897end
898is_wfs = or aegis_str_eq act aegis_act_fs_write aegis_str_eq act aegis_act_fs_delete
899if not is_wfs
900ret false
901end
902widx = aegis_world_find world_id
903if lt widx 0
904ret false
905end
906wu = ulong widx
907if ne get aegis_w_status wu AEGIS_WORLD_ACTIVE
908ret false
909end
910put aegis_w_deltas wu add get aegis_w_deltas wu 1
911if aegis_str_eq act aegis_act_fs_write
912put aegis_w_writes wu add get aegis_w_writes wu 1
913else
914put aegis_w_deletes wu add get aegis_w_deletes wu 1
915end
916ret true
917end
918
919note Evaluate and dispatch an EffectIntent through the AEGIS Broker.
920note Uses aegis_dctx: [0]=world_id, [1]=is_reversible, [2]=now_utc, [3]=net_port.
921note Returns AEGIS_OK if approved & executed, else negative AEGIS_ERR_*.
922fn aegis_broker_dispatch agent_id ulong cap_slot ulong act view uchar * arg view uchar * to int
923now_utc = get aegis_dctx 2
924aegis_compute_intent_id agent_id cap_slot act arg
925note Step 1: Validate capability token
926vrc = aegis_cap_validate cap_slot now_utc
927if ne vrc AEGIS_OK
928aegis_record_audit AEGIS_DEC_REJECTED vrc 0 now_utc
929ret vrc
930end
931note Step 2: Validate holder == proposing agent
932if ne get aegis_cap_holder cap_slot agent_id
933aegis_record_audit AEGIS_DEC_REJECTED AEGIS_ERR_HOLDER 0 now_utc
934ret AEGIS_ERR_HOLDER
935end
936note Step 3: Scope authorizes action + target arg
937if not aegis_scope_authorizes cap_slot act arg uint get aegis_dctx 3
938aegis_record_audit AEGIS_DEC_REJECTED AEGIS_ERR_SCOPE 0 now_utc
939ret AEGIS_ERR_SCOPE
940end
941note Step 4: Active broker-owned World delta fast-path for reversible fs.write/fs.delete
942if aegis_try_world_delta act
943aegis_record_audit AEGIS_DEC_APPROVED AEGIS_OK 1 now_utc
944ret AEGIS_OK
945end
946note Step 5: StandingFetch / read-only autonomous path when not high-impact
947is_stand = eq get aegis_cap_scope cap_slot AEGIS_SCOPE_STANDING_FETCH
948is_ro_act = or aegis_str_eq act aegis_act_fs_read aegis_str_eq act aegis_act_hw_inspect
949if and or is_stand is_ro_act not aegis_is_high_impact act
950aegis_record_audit AEGIS_DEC_APPROVED AEGIS_OK 1 now_utc
951ret AEGIS_OK
952end
953note Step 6: Irreversible / high-impact boundary requires verified OperatorGrant
954if ne get aegis_cur_grant_meta 3 0
955note Mutant: skip operator grant verification on high-impact boundary
956aegis_record_audit AEGIS_DEC_APPROVED AEGIS_OK 1 now_utc
957ret AEGIS_OK
958end
959grc = aegis_grant_verify_current
960if eq grc AEGIS_ERR_MISSING_GRANT
961aegis_record_audit AEGIS_DEC_REQUIRES_OP AEGIS_ERR_MISSING_GRANT 0 now_utc
962ret AEGIS_ERR_MISSING_GRANT
963end
964if ne grc AEGIS_OK
965aegis_record_audit AEGIS_DEC_REJECTED grc 0 now_utc
966ret grc
967end
968aegis_record_audit AEGIS_DEC_APPROVED AEGIS_OK 1 now_utc
969ret AEGIS_OK
970end