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
omega-mv / protocol / aien-protocols / core / action_event.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note protocol/aien-protocols/core/action_event.om: pure-Omega port of aien-action-protocol, |
| 3 | note aien-event-protocol, and aien-agent-state-abi execution/budget/containment invariants. |
| 4 | note Compose after si/directives/om2.om, om/tls/sha2.om, types.om, and provenance.om. |
| 5 | |
| 6 | const AE_OK ulong 0 |
| 7 | const AE_ERR_CONTAINMENT_WEAK ulong 1 |
| 8 | const AE_ERR_CAP_EXPIRED ulong 2 |
| 9 | const AE_ERR_CAP_BAD_WINDOW ulong 3 |
| 10 | const AE_ERR_CAP_EMPTY ulong 4 |
| 11 | const AE_ERR_BAD_TRANSITION ulong 5 |
| 12 | const AE_ERR_BAD_STATUS ulong 6 |
| 13 | const AE_ERR_SEQ_REGRESS ulong 7 |
| 14 | const AE_ERR_BUDGET_TOKENS ulong 8 |
| 15 | const AE_ERR_BUDGET_COST ulong 9 |
| 16 | const AE_ERR_BUDGET_DURATION ulong 10 |
| 17 | const AE_ERR_BUDGET_SUBAGENTS ulong 11 |
| 18 | |
| 19 | const SECCOMP_NONE ulong 0 |
| 20 | const SECCOMP_STRICT ulong 1 |
| 21 | const SECCOMP_PERMISSIVE ulong 2 |
| 22 | |
| 23 | const EXEC_IDLE ulong 0 |
| 24 | const EXEC_RUNNING ulong 1 |
| 25 | const EXEC_SUSPENDED ulong 2 |
| 26 | const EXEC_AWAITING_APPROVAL ulong 3 |
| 27 | const EXEC_TERMINATED ulong 4 |
| 28 | |
| 29 | note --- ContainmentState (strict default: isolated_network=true, read_only_fs=true, seccomp=strict) --- |
| 30 | |
| 31 | fn containment_is_strict iso_net bool ro_fs bool seccomp ulong subproc_cnt ulong to bool |
| 32 | if not iso_net |
| 33 | ret false |
| 34 | end |
| 35 | if not ro_fs |
| 36 | ret false |
| 37 | end |
| 38 | if ne seccomp SECCOMP_STRICT |
| 39 | ret false |
| 40 | end |
| 41 | ret eq subproc_cnt ulong 0 |
| 42 | end |
| 43 | |
| 44 | fn containment_require_strict iso_net bool ro_fs bool seccomp ulong subproc_cnt ulong to ulong |
| 45 | if containment_is_strict iso_net ro_fs seccomp subproc_cnt |
| 46 | ret AE_OK |
| 47 | end |
| 48 | ret AE_ERR_CONTAINMENT_WEAK |
| 49 | end |
| 50 | |
| 51 | note --- CapabilityGrant validation --- |
| 52 | |
| 53 | fn cap_grant_check cap view uchar * scope view uchar * to ulong |
| 54 | if eq len cap ulong 0 |
| 55 | ret AE_ERR_CAP_EMPTY |
| 56 | end |
| 57 | if eq len scope ulong 0 |
| 58 | ret AE_ERR_CAP_EMPTY |
| 59 | end |
| 60 | ret AE_OK |
| 61 | end |
| 62 | |
| 63 | fn cap_grant_window_check granted_us ulong has_exp bool exp_us ulong now_us ulong to ulong |
| 64 | if has_exp |
| 65 | if lt exp_us granted_us |
| 66 | ret AE_ERR_CAP_BAD_WINDOW |
| 67 | end |
| 68 | if gt now_us exp_us |
| 69 | ret AE_ERR_CAP_EXPIRED |
| 70 | end |
| 71 | end |
| 72 | ret AE_OK |
| 73 | end |
| 74 | |
| 75 | note --- AgentExecutionStatus state-machine invariants --- |
| 76 | note Valid transitions: |
| 77 | note Idle -> Running | Terminated |
| 78 | note Running -> Idle | Suspended | AwaitingApproval | Terminated |
| 79 | note Suspended -> Running | Terminated |
| 80 | note AwaitingApproval -> Running | Idle | Terminated |
| 81 | note Terminated -> (absorbing: no transitions allowed, not even to Terminated) |
| 82 | |
| 83 | fn exec_status_valid s ulong to bool |
| 84 | ret le s EXEC_TERMINATED |
| 85 | end |
| 86 | |
| 87 | fn exec_transition_check cur ulong nxt ulong to ulong |
| 88 | if not exec_status_valid cur |
| 89 | ret AE_ERR_BAD_STATUS |
| 90 | end |
| 91 | if not exec_status_valid nxt |
| 92 | ret AE_ERR_BAD_STATUS |
| 93 | end |
| 94 | if eq cur EXEC_TERMINATED |
| 95 | ret AE_ERR_BAD_TRANSITION |
| 96 | end |
| 97 | if eq cur EXEC_IDLE |
| 98 | if or eq nxt EXEC_RUNNING eq nxt EXEC_TERMINATED |
| 99 | ret AE_OK |
| 100 | end |
| 101 | ret AE_ERR_BAD_TRANSITION |
| 102 | end |
| 103 | if eq cur EXEC_RUNNING |
| 104 | if or eq nxt EXEC_IDLE or eq nxt EXEC_SUSPENDED or eq nxt EXEC_AWAITING_APPROVAL eq nxt EXEC_TERMINATED |
| 105 | ret AE_OK |
| 106 | end |
| 107 | ret AE_ERR_BAD_TRANSITION |
| 108 | end |
| 109 | if eq cur EXEC_SUSPENDED |
| 110 | if or eq nxt EXEC_RUNNING eq nxt EXEC_TERMINATED |
| 111 | ret AE_OK |
| 112 | end |
| 113 | ret AE_ERR_BAD_TRANSITION |
| 114 | end |
| 115 | if eq cur EXEC_AWAITING_APPROVAL |
| 116 | if or eq nxt EXEC_RUNNING or eq nxt EXEC_IDLE eq nxt EXEC_TERMINATED |
| 117 | ret AE_OK |
| 118 | end |
| 119 | ret AE_ERR_BAD_TRANSITION |
| 120 | end |
| 121 | ret AE_ERR_BAD_TRANSITION |
| 122 | end |
| 123 | |
| 124 | note --- EventEnvelope sequence monotonicity & canonical digest --- |
| 125 | |
| 126 | fn event_seq_check prev_seq ulong next_seq ulong to ulong |
| 127 | if le next_seq prev_seq |
| 128 | ret AE_ERR_SEQ_REGRESS |
| 129 | end |
| 130 | ret AE_OK |
| 131 | end |
| 132 | |
| 133 | fn event_hash_meta ctx edit ulong * eid view uchar 16 topic view uchar * to void |
| 134 | sha256_init ctx |
| 135 | sha256_update ctx eid |
| 136 | sha256_update ctx topic |
| 137 | end |
| 138 | |
| 139 | fn event_hash_body ctx edit ulong * producer view uchar * seq ulong ts_us ulong to void |
| 140 | buf = new uchar 16 0 |
| 141 | w1 = pt_put_u64_le buf ulong 0 seq |
| 142 | w2 = pt_put_u64_le buf ulong 8 ts_us |
| 143 | if or ne w1 ulong 8 ne w2 ulong 16 |
| 144 | trap 8 |
| 145 | end |
| 146 | sha256_update ctx producer |
| 147 | sha256_update ctx buf |
| 148 | end |
| 149 | |
| 150 | fn event_hash_finish ctx edit ulong * payload_dig view uchar 32 dst edit uchar 32 to void |
| 151 | sha256_update ctx payload_dig |
| 152 | sha256_final ctx dst |
| 153 | end |
| 154 | |
| 155 | note --- ResourceBudget enforcement --- |
| 156 | note A limit value of 0 means unbounded (None in Rust Option<u64>). |
| 157 | |
| 158 | fn budget_check_tokens used ulong max_lim ulong to ulong |
| 159 | if and gt max_lim ulong 0 gt used max_lim |
| 160 | ret AE_ERR_BUDGET_TOKENS |
| 161 | end |
| 162 | ret AE_OK |
| 163 | end |
| 164 | |
| 165 | fn budget_check_cost used_micro_usd ulong max_lim ulong to ulong |
| 166 | if and gt max_lim ulong 0 gt used_micro_usd max_lim |
| 167 | ret AE_ERR_BUDGET_COST |
| 168 | end |
| 169 | ret AE_OK |
| 170 | end |
| 171 | |
| 172 | fn budget_check_duration elapsed_s ulong max_lim ulong to ulong |
| 173 | if and gt max_lim ulong 0 gt elapsed_s max_lim |
| 174 | ret AE_ERR_BUDGET_DURATION |
| 175 | end |
| 176 | ret AE_OK |
| 177 | end |
| 178 | |
| 179 | fn budget_check_subagents active ulong max_lim ulong to ulong |
| 180 | if and gt max_lim ulong 0 gt active max_lim |
| 181 | ret AE_ERR_BUDGET_SUBAGENTS |
| 182 | end |
| 183 | ret AE_OK |
| 184 | end |