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 / graph.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note protocol/synapse/core/graph.om: changeset DAG and ancestry engine: |
| 3 | note parent/child links, cycle refusal, reachability / fast-forward check (gr_is_ancestor), |
| 4 | note lowest common ancestor (merge-base) finder (gr_lca), concurrent branch path-conflict |
| 5 | note detector (gr_branches_conflict), and branch head registry. |
| 6 | note Composed OM-2 library unit (no om2 directive, no main). |
| 7 | |
| 8 | const GR_MAX_PARENTS ulong 8 |
| 9 | const GR_MAX_CHILDREN ulong 32 |
| 10 | const GR_MAX_BRANCHES ulong 64 |
| 11 | |
| 12 | global g_gr_pcnt ulong 256 |
| 13 | global g_gr_pars ulong 2048 |
| 14 | global g_gr_ccnt ulong 256 |
| 15 | global g_gr_chld ulong 8192 |
| 16 | global g_gr_br ulong 192 |
| 17 | global g_gr_hdr ulong 8 |
| 18 | |
| 19 | fn gr_reset to void |
| 20 | for i ulong 256 |
| 21 | put g_gr_pcnt i 0 |
| 22 | put g_gr_ccnt i 0 |
| 23 | end |
| 24 | put g_gr_hdr 0 0 |
| 25 | put g_gr_hdr 1 SYN_NONE |
| 26 | put g_gr_hdr 2 SYN_NONE |
| 27 | end |
| 28 | |
| 29 | fn gr_parent_count idx ulong to ulong |
| 30 | if ge idx CS_MAX |
| 31 | ret 0 |
| 32 | end |
| 33 | ret get g_gr_pcnt idx |
| 34 | end |
| 35 | |
| 36 | fn gr_parent_at idx ulong k ulong to ulong |
| 37 | if ge idx CS_MAX |
| 38 | ret SYN_NONE |
| 39 | end |
| 40 | if ge k get g_gr_pcnt idx |
| 41 | ret SYN_NONE |
| 42 | end |
| 43 | ret get g_gr_pars add mul idx GR_MAX_PARENTS k |
| 44 | end |
| 45 | |
| 46 | fn gr_child_count idx ulong to ulong |
| 47 | if ge idx CS_MAX |
| 48 | ret 0 |
| 49 | end |
| 50 | ret get g_gr_ccnt idx |
| 51 | end |
| 52 | |
| 53 | fn gr_child_at idx ulong k ulong to ulong |
| 54 | if ge idx CS_MAX |
| 55 | ret SYN_NONE |
| 56 | end |
| 57 | if ge k get g_gr_ccnt idx |
| 58 | ret SYN_NONE |
| 59 | end |
| 60 | ret get g_gr_chld add mul idx GR_MAX_CHILDREN k |
| 61 | end |
| 62 | |
| 63 | note BFS reachability from desc_idx upward along parent links to test if anc_idx is an ancestor of desc_idx. |
| 64 | note Reflexive: gr_is_ancestor(x, x) is true for valid x. |
| 65 | fn gr_is_ancestor anc_idx ulong desc_idx ulong to bool |
| 66 | vis = new uchar 256 0 |
| 67 | que = new ulong 256 0 |
| 68 | cnt = cs_count |
| 69 | if or ge anc_idx cnt ge desc_idx cnt |
| 70 | ret false |
| 71 | end |
| 72 | if eq anc_idx desc_idx |
| 73 | ret true |
| 74 | end |
| 75 | put vis desc_idx 1 |
| 76 | put que 0 desc_idx |
| 77 | qh = ulong 0 |
| 78 | qt = ulong 1 |
| 79 | while lt qh qt bound 512 |
| 80 | cur = get que qh |
| 81 | qh = add qh 1 |
| 82 | pc = get g_gr_pcnt cur |
| 83 | base = mul cur GR_MAX_PARENTS |
| 84 | for i ulong pc |
| 85 | p = get g_gr_pars add base i |
| 86 | if eq p anc_idx |
| 87 | ret true |
| 88 | end |
| 89 | if lt p CS_MAX |
| 90 | if eq get vis p 0 |
| 91 | put vis p 1 |
| 92 | if lt qt 256 |
| 93 | put que qt p |
| 94 | qt = add qt 1 |
| 95 | end |
| 96 | end |
| 97 | end |
| 98 | end |
| 99 | end |
| 100 | ret false |
| 101 | end |
| 102 | |
| 103 | note Links child_idx -> parent_idx in the DAG, refusing self-links and any edge that would close a cycle. |
| 104 | fn gr_link_parent child_idx ulong parent_idx ulong to ulong |
| 105 | cnt = cs_count |
| 106 | if or ge child_idx cnt ge parent_idx cnt |
| 107 | ret SYN_ERR_NOT_FOUND |
| 108 | end |
| 109 | if eq child_idx parent_idx |
| 110 | ret SYN_ERR_CYCLE |
| 111 | end |
| 112 | pc = get g_gr_pcnt child_idx |
| 113 | pbase = mul child_idx GR_MAX_PARENTS |
| 114 | for i ulong pc |
| 115 | if eq get g_gr_pars add pbase i parent_idx |
| 116 | ret SYN_OK |
| 117 | end |
| 118 | end |
| 119 | note Cycle check: if child_idx is already an ancestor of parent_idx, linking child->parent creates a cycle. |
| 120 | if gr_is_ancestor child_idx parent_idx |
| 121 | ret SYN_ERR_CYCLE |
| 122 | end |
| 123 | cc = get g_gr_ccnt parent_idx |
| 124 | if or ge pc GR_MAX_PARENTS ge cc GR_MAX_CHILDREN |
| 125 | ret SYN_ERR_CAPACITY |
| 126 | end |
| 127 | put g_gr_pars add pbase pc parent_idx |
| 128 | put g_gr_pcnt child_idx add pc 1 |
| 129 | cbase = mul parent_idx GR_MAX_CHILDREN |
| 130 | put g_gr_chld add cbase cc child_idx |
| 131 | put g_gr_ccnt parent_idx add cc 1 |
| 132 | ret SYN_OK |
| 133 | end |
| 134 | |
| 135 | note Fast-forward check: true when base_idx is SYN_NONE (initial landing) or base_idx is an ancestor of head_idx. |
| 136 | fn gr_is_fast_forward base_idx ulong head_idx ulong to bool |
| 137 | if eq base_idx SYN_NONE |
| 138 | ret true |
| 139 | end |
| 140 | ret gr_is_ancestor base_idx head_idx |
| 141 | end |
| 142 | |
| 143 | note BFS from start_idx recording (distance + 1) in dist[0..256); 0 means not an ancestor. |
| 144 | fn gr_collect_ancestors start_idx ulong dist edit ulong 256 to void |
| 145 | que = new ulong 256 0 |
| 146 | for i ulong 256 |
| 147 | put dist i 0 |
| 148 | end |
| 149 | if ge start_idx cs_count |
| 150 | ret |
| 151 | end |
| 152 | put dist start_idx 1 |
| 153 | put que 0 start_idx |
| 154 | qh = ulong 0 |
| 155 | qt = ulong 1 |
| 156 | while lt qh qt bound 512 |
| 157 | cur = get que qh |
| 158 | qh = add qh 1 |
| 159 | dcur = get dist cur |
| 160 | pc = get g_gr_pcnt cur |
| 161 | base = mul cur GR_MAX_PARENTS |
| 162 | for i ulong pc |
| 163 | p = get g_gr_pars add base i |
| 164 | if lt p CS_MAX |
| 165 | if eq get dist p 0 |
| 166 | put dist p add dcur 1 |
| 167 | if lt qt 256 |
| 168 | put que qt p |
| 169 | qt = add qt 1 |
| 170 | end |
| 171 | end |
| 172 | end |
| 173 | end |
| 174 | end |
| 175 | end |
| 176 | |
| 177 | fn gr_has_lower_common u ulong cnt ulong da view ulong 256 db view ulong 256 to bool |
| 178 | for v ulong cnt |
| 179 | if ne v u |
| 180 | if gt get da v 0 |
| 181 | if gt get db v 0 |
| 182 | if gr_is_ancestor u v |
| 183 | ret true |
| 184 | end |
| 185 | end |
| 186 | end |
| 187 | end |
| 188 | end |
| 189 | ret false |
| 190 | end |
| 191 | |
| 192 | note Lowest Common Ancestor (merge-base) of idx_a and idx_b. |
| 193 | note Returns a common ancestor that is not a strict ancestor of any other common ancestor, |
| 194 | note breaking ties by minimal combined distance; returns SYN_NONE if disjoint. |
| 195 | fn gr_lca idx_a ulong idx_b ulong to ulong |
| 196 | da = new ulong 256 0 |
| 197 | db = new ulong 256 0 |
| 198 | cnt = cs_count |
| 199 | if or ge idx_a cnt ge idx_b cnt |
| 200 | ret SYN_NONE |
| 201 | end |
| 202 | gr_collect_ancestors idx_a da |
| 203 | gr_collect_ancestors idx_b db |
| 204 | best = SYN_NONE |
| 205 | best_sum = SYN_NONE |
| 206 | for u ulong cnt |
| 207 | if gt get da u 0 |
| 208 | if gt get db u 0 |
| 209 | if eq gr_has_lower_common u cnt da db false |
| 210 | s = add get da u get db u |
| 211 | if or eq best SYN_NONE lt s best_sum |
| 212 | best = u |
| 213 | best_sum = s |
| 214 | end |
| 215 | end |
| 216 | end |
| 217 | end |
| 218 | end |
| 219 | ret best |
| 220 | end |
| 221 | |
| 222 | fn gr_scan_conflict u ulong cnt ulong db view ulong 256 da view ulong 256 to ulong |
| 223 | best = ulong 0 |
| 224 | for v ulong cnt |
| 225 | if gt get db v 0 |
| 226 | if eq get da v 0 |
| 227 | ov = cs_changesets_overlap u v |
| 228 | if eq ov 1 |
| 229 | put g_gr_hdr 1 u |
| 230 | put g_gr_hdr 2 v |
| 231 | ret 1 |
| 232 | end |
| 233 | if eq ov 2 |
| 234 | if eq best 0 |
| 235 | best = 2 |
| 236 | put g_gr_hdr 1 u |
| 237 | put g_gr_hdr 2 v |
| 238 | end |
| 239 | end |
| 240 | end |
| 241 | end |
| 242 | end |
| 243 | ret best |
| 244 | end |
| 245 | |
| 246 | note Detects conflicting paths across concurrent branches head_a and head_b relative to their merge-base. |
| 247 | note Returns 0 (disjoint or non-concurrent), 1 (exact path conflict), or 2 (directory prefix conflict). |
| 248 | note Records the conflicting changeset indices in g_gr_hdr[1] and g_gr_hdr[2]. |
| 249 | fn gr_branches_conflict head_a ulong head_b ulong to ulong |
| 250 | da = new ulong 256 0 |
| 251 | db = new ulong 256 0 |
| 252 | cnt = cs_count |
| 253 | put g_gr_hdr 1 SYN_NONE |
| 254 | put g_gr_hdr 2 SYN_NONE |
| 255 | if or ge head_a cnt ge head_b cnt |
| 256 | ret 0 |
| 257 | end |
| 258 | if or gr_is_ancestor head_a head_b gr_is_ancestor head_b head_a |
| 259 | ret 0 |
| 260 | end |
| 261 | gr_collect_ancestors head_a da |
| 262 | gr_collect_ancestors head_b db |
| 263 | best = ulong 0 |
| 264 | for u ulong cnt |
| 265 | if gt get da u 0 |
| 266 | if eq get db u 0 |
| 267 | ov = gr_scan_conflict u cnt db da |
| 268 | if eq ov 1 |
| 269 | ret 1 |
| 270 | end |
| 271 | if eq ov 2 |
| 272 | best = 2 |
| 273 | end |
| 274 | end |
| 275 | end |
| 276 | end |
| 277 | ret best |
| 278 | end |
| 279 | |
| 280 | fn gr_conflict_cs_a to ulong |
| 281 | ret get g_gr_hdr 1 |
| 282 | end |
| 283 | |
| 284 | fn gr_conflict_cs_b to ulong |
| 285 | ret get g_gr_hdr 2 |
| 286 | end |
| 287 | |
| 288 | note ======== branch head registry ======== |
| 289 | |
| 290 | fn gr_branch_find_arena no ulong nl ulong to ulong |
| 291 | cnt = get g_gr_hdr 0 |
| 292 | for i ulong cnt |
| 293 | base = mul i 3 |
| 294 | if syn_arena_eq get g_gr_br base get g_gr_br add base 1 no nl |
| 295 | ret i |
| 296 | end |
| 297 | end |
| 298 | ret SYN_NONE |
| 299 | end |
| 300 | |
| 301 | fn gr_branch_get name view uchar * to ulong |
| 302 | cnt = get g_gr_hdr 0 |
| 303 | for i ulong cnt |
| 304 | base = mul i 3 |
| 305 | if syn_arena_eq_view get g_gr_br base get g_gr_br add base 1 name |
| 306 | ret get g_gr_br add base 2 |
| 307 | end |
| 308 | end |
| 309 | ret SYN_NONE |
| 310 | end |
| 311 | |
| 312 | fn gr_branch_set_arena no ulong nl ulong head_idx ulong to ulong |
| 313 | pos = gr_branch_find_arena no nl |
| 314 | if ne pos SYN_NONE |
| 315 | put g_gr_br add mul pos 3 2 head_idx |
| 316 | ret SYN_OK |
| 317 | end |
| 318 | cnt = get g_gr_hdr 0 |
| 319 | if ge cnt GR_MAX_BRANCHES |
| 320 | ret SYN_ERR_CAPACITY |
| 321 | end |
| 322 | base = mul cnt 3 |
| 323 | put g_gr_br base no |
| 324 | put g_gr_br add base 1 nl |
| 325 | put g_gr_br add base 2 head_idx |
| 326 | put g_gr_hdr 0 add cnt 1 |
| 327 | ret SYN_OK |
| 328 | end |
| 329 | |
| 330 | fn gr_branch_set name view uchar * head_idx ulong to ulong |
| 331 | cnt = get g_gr_hdr 0 |
| 332 | for i ulong cnt |
| 333 | base = mul i 3 |
| 334 | if syn_arena_eq_view get g_gr_br base get g_gr_br add base 1 name |
| 335 | put g_gr_br add base 2 head_idx |
| 336 | ret SYN_OK |
| 337 | end |
| 338 | end |
| 339 | if ge cnt GR_MAX_BRANCHES |
| 340 | ret SYN_ERR_CAPACITY |
| 341 | end |
| 342 | no = syn_arena_put name |
| 343 | if eq no SYN_NONE |
| 344 | ret SYN_ERR_CAPACITY |
| 345 | end |
| 346 | base = mul cnt 3 |
| 347 | put g_gr_br base no |
| 348 | put g_gr_br add base 1 len name |
| 349 | put g_gr_br add base 2 head_idx |
| 350 | put g_gr_hdr 0 add cnt 1 |
| 351 | ret SYN_OK |
| 352 | end |
| 353 | |
| 354 | fn gr_main_head to ulong |
| 355 | ret gr_branch_get t_br_main |
| 356 | end |