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 / synapse / core / review.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note protocol/synapse/core/review.om: exact-head review and landing policy engine: |
| 3 | note binds reviewer verdicts (MERGE, CHANGES_REQUESTED, REJECT) to exact 32-byte changeset digests, |
| 4 | note automatically invalidates prior verdicts when a new head arrives on a branch, refuses self-review, |
| 5 | note refuses stale-head verdicts, blocks landing on any non-MERGE verdict or missing exact-head MERGE, |
| 6 | note enforces strict fast-forward from main (never automerges), and logs transitions to ledger. |
| 7 | note Composed OM-2 library unit (no om2 directive, no main). |
| 8 | |
| 9 | const RV_MAX ulong 512 |
| 10 | const RV_FIELDS ulong 8 |
| 11 | |
| 12 | const RV_MERGE ulong 1 |
| 13 | const RV_CHANGES_REQUESTED ulong 2 |
| 14 | const RV_REJECT ulong 3 |
| 15 | |
| 16 | const RVF_CS_IDX ulong 0 |
| 17 | const RVF_REV_OFF ulong 1 |
| 18 | const RVF_REV_LEN ulong 2 |
| 19 | const RVF_VERDICT ulong 3 |
| 20 | const RVF_TS ulong 4 |
| 21 | const RVF_VALID ulong 5 |
| 22 | const RVF_BR_OFF ulong 6 |
| 23 | const RVF_BR_LEN ulong 7 |
| 24 | |
| 25 | global g_rv_tab ulong 4096 |
| 26 | global g_rv_head uchar 16384 |
| 27 | global g_rv_hdr ulong 8 |
| 28 | |
| 29 | text t_rv_merge "MERGE" |
| 30 | text t_rv_chg "CHANGES_REQUESTED" |
| 31 | text t_rv_rej "REJECT" |
| 32 | text t_rv_land "LAND_MAIN" |
| 33 | |
| 34 | fn rv_reset to void |
| 35 | put g_rv_hdr 0 0 |
| 36 | put g_rv_hdr 1 0 |
| 37 | end |
| 38 | |
| 39 | fn rv_count to ulong |
| 40 | ret get g_rv_hdr 0 |
| 41 | end |
| 42 | |
| 43 | fn rv_set_time ts ulong to void |
| 44 | put g_rv_hdr 1 ts |
| 45 | end |
| 46 | |
| 47 | fn rv_mget idx ulong f ulong to ulong |
| 48 | ret get g_rv_tab add mul idx RV_FIELDS f |
| 49 | end |
| 50 | |
| 51 | fn rv_mput idx ulong f ulong v ulong to void |
| 52 | put g_rv_tab add mul idx RV_FIELDS f v |
| 53 | end |
| 54 | |
| 55 | fn rv_head_matches ridx ulong target_id view uchar * to bool |
| 56 | if ne len target_id 32 |
| 57 | ret false |
| 58 | end |
| 59 | base = mul ridx 32 |
| 60 | for i ulong 32 |
| 61 | if ne get g_rv_head add base i get target_id i |
| 62 | ret false |
| 63 | end |
| 64 | end |
| 65 | ret true |
| 66 | end |
| 67 | |
| 68 | note Called automatically when a new changeset head is committed on a branch: |
| 69 | note invalidates all prior review verdicts whose branch matches and whose bound head differs. |
| 70 | fn rv_invalidate_branch bo ulong bl ulong new_cs_idx ulong to void |
| 71 | new_id = new uchar 32 0 |
| 72 | cs_get_id new_cs_idx new_id |
| 73 | cnt = get g_rv_hdr 0 |
| 74 | for i ulong cnt |
| 75 | if eq rv_mget i RVF_VALID 1 |
| 76 | if syn_arena_eq rv_mget i RVF_BR_OFF rv_mget i RVF_BR_LEN bo bl |
| 77 | if eq rv_head_matches i new_id false |
| 78 | rv_mput i RVF_VALID 0 |
| 79 | end |
| 80 | end |
| 81 | end |
| 82 | end |
| 83 | end |
| 84 | |
| 85 | note Counts valid exact-head MERGE verdicts for cs_idx. |
| 86 | fn rv_valid_merge_count cs_idx ulong to ulong |
| 87 | cur_id = new uchar 32 0 |
| 88 | if ge cs_idx cs_count |
| 89 | ret 0 |
| 90 | end |
| 91 | cs_get_id cs_idx cur_id |
| 92 | cnt = get g_rv_hdr 0 |
| 93 | merges = ulong 0 |
| 94 | for i ulong cnt |
| 95 | if eq rv_mget i RVF_VALID 1 |
| 96 | if eq rv_mget i RVF_CS_IDX cs_idx |
| 97 | if rv_head_matches i cur_id |
| 98 | if eq rv_mget i RVF_VERDICT RV_MERGE |
| 99 | merges = add merges 1 |
| 100 | end |
| 101 | end |
| 102 | end |
| 103 | end |
| 104 | end |
| 105 | ret merges |
| 106 | end |
| 107 | |
| 108 | note Counts valid exact-head blocking verdicts (CHANGES_REQUESTED or REJECT) for cs_idx. |
| 109 | fn rv_valid_block_count cs_idx ulong to ulong |
| 110 | cur_id = new uchar 32 0 |
| 111 | if ge cs_idx cs_count |
| 112 | ret 0 |
| 113 | end |
| 114 | cs_get_id cs_idx cur_id |
| 115 | cnt = get g_rv_hdr 0 |
| 116 | blocks = ulong 0 |
| 117 | for i ulong cnt |
| 118 | if eq rv_mget i RVF_VALID 1 |
| 119 | if eq rv_mget i RVF_CS_IDX cs_idx |
| 120 | if rv_head_matches i cur_id |
| 121 | if ne rv_mget i RVF_VERDICT RV_MERGE |
| 122 | blocks = add blocks 1 |
| 123 | end |
| 124 | end |
| 125 | end |
| 126 | end |
| 127 | end |
| 128 | ret blocks |
| 129 | end |
| 130 | |
| 131 | fn rv_log_verdict cs_id view uchar * ro ulong rl ulong verdict ulong ts ulong to ulong |
| 132 | lg_set_subject cs_id |
| 133 | if eq verdict RV_MERGE |
| 134 | ret lg_append_view 6 ts ro rl t_rv_merge |
| 135 | end |
| 136 | if eq verdict RV_CHANGES_REQUESTED |
| 137 | ret lg_append_view 6 ts ro rl t_rv_chg |
| 138 | end |
| 139 | ret lg_append_view 6 ts ro rl t_rv_rej |
| 140 | end |
| 141 | |
| 142 | note Internal commit of a verified review verdict for cs_idx (already validated against exact head). |
| 143 | fn rv_store_verdict cs_idx ulong reviewer view uchar * verdict ulong ts ulong to ulong |
| 144 | cur_id = new uchar 32 0 |
| 145 | if or eq len reviewer 0 gt len reviewer 128 |
| 146 | ret SYN_ERR_MALFORMED |
| 147 | end |
| 148 | if or lt verdict RV_MERGE gt verdict RV_REJECT |
| 149 | ret SYN_ERR_VERDICT_INVALID |
| 150 | end |
| 151 | st = cs_get_state cs_idx |
| 152 | if and ne st CS_STATE_PROPOSED ne st CS_STATE_REVIEWED |
| 153 | ret SYN_ERR_STATE_INVALID |
| 154 | end |
| 155 | note Self-review refusal: author cannot review their own changeset. |
| 156 | if syn_arena_eq_view cs_get_author_off cs_idx cs_get_author_len cs_idx reviewer |
| 157 | ret SYN_ERR_SELF_REVIEW |
| 158 | end |
| 159 | cnt = get g_rv_hdr 0 |
| 160 | if ge cnt RV_MAX |
| 161 | ret SYN_ERR_CAPACITY |
| 162 | end |
| 163 | ro = syn_arena_put reviewer |
| 164 | if eq ro SYN_NONE |
| 165 | ret SYN_ERR_CAPACITY |
| 166 | end |
| 167 | cs_get_id cs_idx cur_id |
| 168 | base = mul cnt 32 |
| 169 | for i ulong 32 |
| 170 | put g_rv_head add base i get cur_id i |
| 171 | end |
| 172 | rv_mput cnt RVF_CS_IDX cs_idx |
| 173 | rv_mput cnt RVF_REV_OFF ro |
| 174 | rv_mput cnt RVF_REV_LEN len reviewer |
| 175 | rv_mput cnt RVF_VERDICT verdict |
| 176 | rv_mput cnt RVF_TS ts |
| 177 | rv_mput cnt RVF_VALID 1 |
| 178 | rv_mput cnt RVF_BR_OFF cs_get_branch_off cs_idx |
| 179 | rv_mput cnt RVF_BR_LEN cs_get_branch_len cs_idx |
| 180 | put g_rv_hdr 0 add cnt 1 |
| 181 | seq = rv_log_verdict cur_id ro len reviewer verdict ts |
| 182 | if eq seq 0 |
| 183 | ret SYN_ERR_CAPACITY |
| 184 | end |
| 185 | if eq verdict RV_MERGE |
| 186 | if eq st CS_STATE_PROPOSED |
| 187 | if eq rv_valid_block_count cs_idx 0 |
| 188 | rc = cs_transition cs_idx CS_STATE_REVIEWED ts |
| 189 | if ne rc SYN_OK |
| 190 | ret rc |
| 191 | end |
| 192 | end |
| 193 | end |
| 194 | end |
| 195 | ret SYN_OK |
| 196 | end |
| 197 | |
| 198 | note Records a review verdict bound to exact 32-byte head_id. |
| 199 | note Refuses unknown changeset, stale branch head (superseded by newer head), self-review, |
| 200 | note invalid verdict, or non-PROPOSED/REVIEWED state. |
| 201 | fn rv_record head_id view uchar * reviewer view uchar * verdict ulong ts ulong to ulong |
| 202 | if ne len head_id 32 |
| 203 | ret SYN_ERR_MALFORMED |
| 204 | end |
| 205 | cs_idx = cs_find_by_id head_id |
| 206 | if eq cs_idx SYN_NONE |
| 207 | ret SYN_ERR_NOT_FOUND |
| 208 | end |
| 209 | note Check if this non-main branch already moved to a newer head (stale-head refusal). |
| 210 | bo = cs_get_branch_off cs_idx |
| 211 | bl = cs_get_branch_len cs_idx |
| 212 | if eq syn_arena_eq_view bo bl t_br_main false |
| 213 | bpos = gr_branch_find_arena bo bl |
| 214 | if ne bpos SYN_NONE |
| 215 | bhead = get g_gr_br add mul bpos 3 2 |
| 216 | if ne bhead cs_idx |
| 217 | ret SYN_ERR_STALE_HEAD |
| 218 | end |
| 219 | end |
| 220 | end |
| 221 | ret rv_store_verdict cs_idx reviewer verdict ts |
| 222 | end |
| 223 | |
| 224 | note Records a review verdict explicitly checking expected_head against cs_idx's exact digest. |
| 225 | note Uses timestamp from rv_set_time (or cs_ts + 1 if unset). Refuses with SYN_ERR_STALE_HEAD on mismatch. |
| 226 | fn rv_record_bound cs_idx ulong expected_head view uchar * reviewer view uchar * verdict ulong to ulong |
| 227 | cur_id = new uchar 32 0 |
| 228 | if ge cs_idx cs_count |
| 229 | ret SYN_ERR_NOT_FOUND |
| 230 | end |
| 231 | cs_get_id cs_idx cur_id |
| 232 | if eq syn_digest_eq cur_id expected_head false |
| 233 | ret SYN_ERR_STALE_HEAD |
| 234 | end |
| 235 | ts = get g_rv_hdr 1 |
| 236 | if eq ts 0 |
| 237 | ts = add cs_get_ts cs_idx 1 |
| 238 | end |
| 239 | ret rv_store_verdict cs_idx reviewer verdict ts |
| 240 | end |
| 241 | |
| 242 | note Strict landing policy check without mutating state. |
| 243 | note Requires: |
| 244 | note 1. cs_idx exists and is not already LANDED or ABANDONED |
| 245 | note 2. expected_head matches cs_id(cs_idx) byte-for-byte |
| 246 | note 3. At least one valid exact-head MERGE verdict and ZERO blocking verdicts |
| 247 | note 4. Current main head (if any) is an ancestor of cs_idx (strict fast-forward; never automerges) |
| 248 | fn rv_can_land cs_idx ulong expected_head view uchar * to ulong |
| 249 | cur_id = new uchar 32 0 |
| 250 | if ge cs_idx cs_count |
| 251 | ret SYN_ERR_NOT_FOUND |
| 252 | end |
| 253 | st = cs_get_state cs_idx |
| 254 | if or eq st CS_STATE_LANDED eq st CS_STATE_ABANDONED |
| 255 | ret SYN_ERR_STATE_INVALID |
| 256 | end |
| 257 | cs_get_id cs_idx cur_id |
| 258 | if eq syn_digest_eq cur_id expected_head false |
| 259 | ret SYN_ERR_STALE_HEAD |
| 260 | end |
| 261 | if gt rv_valid_block_count cs_idx 0 |
| 262 | ret SYN_ERR_REVIEW_BLOCKED |
| 263 | end |
| 264 | if eq rv_valid_merge_count cs_idx 0 |
| 265 | ret SYN_ERR_REVIEW_BLOCKED |
| 266 | end |
| 267 | mh = gr_main_head |
| 268 | if ne mh SYN_NONE |
| 269 | if eq gr_is_fast_forward mh cs_idx false |
| 270 | ret SYN_ERR_NON_FF |
| 271 | end |
| 272 | end |
| 273 | ret SYN_OK |
| 274 | end |
| 275 | |
| 276 | note Executes strict landing policy: verifies exact-head MERGE receipt, zero blocking verdicts, |
| 277 | note and fast-forward from main (never automerges). Advances state to LANDED, updates main head, |
| 278 | note and appends LG_EV_LAND (7) to the hash-chained ledger. |
| 279 | fn rv_land cs_idx ulong expected_head view uchar * ts ulong to ulong |
| 280 | cur_id = new uchar 32 0 |
| 281 | rc = rv_can_land cs_idx expected_head |
| 282 | if ne rc SYN_OK |
| 283 | ret rc |
| 284 | end |
| 285 | st = cs_get_state cs_idx |
| 286 | if eq st CS_STATE_PROPOSED |
| 287 | rc2 = cs_transition cs_idx CS_STATE_REVIEWED ts |
| 288 | if ne rc2 SYN_OK |
| 289 | ret rc2 |
| 290 | end |
| 291 | end |
| 292 | rc3 = cs_transition cs_idx CS_STATE_LANDED ts |
| 293 | if ne rc3 SYN_OK |
| 294 | ret rc3 |
| 295 | end |
| 296 | rc4 = gr_branch_set t_br_main cs_idx |
| 297 | if ne rc4 SYN_OK |
| 298 | ret rc4 |
| 299 | end |
| 300 | cs_get_id cs_idx cur_id |
| 301 | lg_set_subject cur_id |
| 302 | seq = lg_append_view 7 ts cs_get_author_off cs_idx cs_get_author_len cs_idx t_rv_land |
| 303 | if eq seq 0 |
| 304 | ret SYN_ERR_CAPACITY |
| 305 | end |
| 306 | ret SYN_OK |
| 307 | end |