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 / lease.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note protocol/synapse/core/lease.om: exclusive coordination lease table over branch: and path: targets |
| 3 | note with monotonic timestamps, TTL expiry, strict overlap refusal (status 2), holder-only renewal, |
| 4 | note explicit release with final status word, and ledger recording. |
| 5 | note Composed OM-2 library unit (no om2 directive, no main). |
| 6 | |
| 7 | const LS_MAX ulong 256 |
| 8 | const LS_FIELDS ulong 14 |
| 9 | |
| 10 | const LS_KIND_BRANCH ulong 1 |
| 11 | const LS_KIND_PATH ulong 2 |
| 12 | |
| 13 | const LS_ST_ACTIVE ulong 1 |
| 14 | const LS_ST_RELEASED ulong 2 |
| 15 | const LS_ST_EXPIRED ulong 3 |
| 16 | |
| 17 | const LSF_ID ulong 0 |
| 18 | const LSF_KIND ulong 1 |
| 19 | const LSF_HOLD_OFF ulong 2 |
| 20 | const LSF_HOLD_LEN ulong 3 |
| 21 | const LSF_TGT_OFF ulong 4 |
| 22 | const LSF_TGT_LEN ulong 5 |
| 23 | const LSF_NORM_OFF ulong 6 |
| 24 | const LSF_NORM_LEN ulong 7 |
| 25 | const LSF_STATUS ulong 8 |
| 26 | const LSF_CREATED ulong 9 |
| 27 | const LSF_EXPIRES ulong 10 |
| 28 | const LSF_CLOSED ulong 11 |
| 29 | const LSF_FIN_OFF ulong 12 |
| 30 | const LSF_FIN_LEN ulong 13 |
| 31 | |
| 32 | global g_ls_tab ulong 3584 |
| 33 | global g_ls_hdr ulong 8 |
| 34 | |
| 35 | text t_ls_pbr "branch:" |
| 36 | text t_ls_ppath "path:" |
| 37 | text t_ls_wild "*" |
| 38 | |
| 39 | fn ls_reset to void |
| 40 | put g_ls_hdr 0 0 |
| 41 | put g_ls_hdr 1 SYN_NONE |
| 42 | put g_ls_hdr 2 0 |
| 43 | end |
| 44 | |
| 45 | fn ls_count to ulong |
| 46 | ret get g_ls_hdr 0 |
| 47 | end |
| 48 | |
| 49 | fn ls_last_id to ulong |
| 50 | ret get g_ls_hdr 2 |
| 51 | end |
| 52 | |
| 53 | fn ls_conflict_idx to ulong |
| 54 | ret get g_ls_hdr 1 |
| 55 | end |
| 56 | |
| 57 | fn ls_mget idx ulong f ulong to ulong |
| 58 | ret get g_ls_tab add mul idx LS_FIELDS f |
| 59 | end |
| 60 | |
| 61 | fn ls_mput idx ulong f ulong v ulong to void |
| 62 | put g_ls_tab add mul idx LS_FIELDS f v |
| 63 | end |
| 64 | |
| 65 | fn ls_find_by_id lid ulong to ulong |
| 66 | cnt = get g_ls_hdr 0 |
| 67 | for i ulong cnt |
| 68 | if eq ls_mget i LSF_ID lid |
| 69 | ret i |
| 70 | end |
| 71 | end |
| 72 | ret SYN_NONE |
| 73 | end |
| 74 | |
| 75 | fn ls_holder_off idx ulong to ulong |
| 76 | ret ls_mget idx LSF_HOLD_OFF |
| 77 | end |
| 78 | |
| 79 | fn ls_holder_len idx ulong to ulong |
| 80 | ret ls_mget idx LSF_HOLD_LEN |
| 81 | end |
| 82 | |
| 83 | fn ls_target_off idx ulong to ulong |
| 84 | ret ls_mget idx LSF_TGT_OFF |
| 85 | end |
| 86 | |
| 87 | fn ls_target_len idx ulong to ulong |
| 88 | ret ls_mget idx LSF_TGT_LEN |
| 89 | end |
| 90 | |
| 91 | fn ls_final_off idx ulong to ulong |
| 92 | ret ls_mget idx LSF_FIN_OFF |
| 93 | end |
| 94 | |
| 95 | fn ls_final_len idx ulong to ulong |
| 96 | ret ls_mget idx LSF_FIN_LEN |
| 97 | end |
| 98 | |
| 99 | fn ls_status idx ulong now ulong to ulong |
| 100 | if ge idx get g_ls_hdr 0 |
| 101 | ret 0 |
| 102 | end |
| 103 | st = ls_mget idx LSF_STATUS |
| 104 | if eq st LS_ST_ACTIVE |
| 105 | if ge now ls_mget idx LSF_EXPIRES |
| 106 | ret LS_ST_EXPIRED |
| 107 | end |
| 108 | end |
| 109 | ret st |
| 110 | end |
| 111 | |
| 112 | fn ls_is_active lid ulong now ulong to bool |
| 113 | idx = ls_find_by_id lid |
| 114 | if eq idx SYN_NONE |
| 115 | ret false |
| 116 | end |
| 117 | ret eq ls_status idx now LS_ST_ACTIVE |
| 118 | end |
| 119 | |
| 120 | fn ls_make_subj lid ulong dst edit uchar * to void |
| 121 | for i ulong 32 |
| 122 | put dst i 0 |
| 123 | end |
| 124 | put dst 0 76 |
| 125 | put dst 1 69 |
| 126 | put dst 2 65 |
| 127 | put dst 3 83 |
| 128 | put dst 4 69 |
| 129 | syn_put_u64_le dst 8 lid |
| 130 | end |
| 131 | |
| 132 | fn ls_has_prefix s view uchar * pref view uchar * to bool |
| 133 | if lt len s len pref |
| 134 | ret false |
| 135 | end |
| 136 | for i ulong len pref |
| 137 | if ne get s i get pref i |
| 138 | ret false |
| 139 | end |
| 140 | end |
| 141 | ret true |
| 142 | end |
| 143 | |
| 144 | note Parses and validates target: returns LS_KIND_BRANCH (1), LS_KIND_PATH (2), or 0 if invalid. |
| 145 | note Writes normalized suffix (off, len) into g_ls_hdr[4..5] (for path: strips trailing '/*' if present). |
| 146 | fn ls_parse_target target view uchar * to ulong |
| 147 | n = len target |
| 148 | if ls_has_prefix target t_ls_pbr |
| 149 | if or le n 7 gt n 135 |
| 150 | ret 0 |
| 151 | end |
| 152 | for i ulong sub n 7 |
| 153 | c = get target add 7 i |
| 154 | if or le c 32 gt c 126 |
| 155 | ret 0 |
| 156 | end |
| 157 | end |
| 158 | no = syn_arena_put part target 7 sub n 7 |
| 159 | if eq no SYN_NONE |
| 160 | ret 0 |
| 161 | end |
| 162 | put g_ls_hdr 4 no |
| 163 | put g_ls_hdr 5 sub n 7 |
| 164 | ret LS_KIND_BRANCH |
| 165 | end |
| 166 | if ls_has_prefix target t_ls_ppath |
| 167 | if or le n 5 gt n 261 |
| 168 | ret 0 |
| 169 | end |
| 170 | plen = sub n 5 |
| 171 | if and eq plen 1 eq get target 5 42 |
| 172 | no = syn_arena_put t_ls_wild |
| 173 | if eq no SYN_NONE |
| 174 | ret 0 |
| 175 | end |
| 176 | put g_ls_hdr 4 no |
| 177 | put g_ls_hdr 5 1 |
| 178 | ret LS_KIND_PATH |
| 179 | end |
| 180 | if ge plen 2 |
| 181 | if and eq get target sub n 2 47 eq get target sub n 1 42 |
| 182 | plen = sub plen 2 |
| 183 | end |
| 184 | end |
| 185 | if eq cs_path_valid part target 5 plen false |
| 186 | ret 0 |
| 187 | end |
| 188 | no = syn_arena_put part target 5 plen |
| 189 | if eq no SYN_NONE |
| 190 | ret 0 |
| 191 | end |
| 192 | put g_ls_hdr 4 no |
| 193 | put g_ls_hdr 5 plen |
| 194 | ret LS_KIND_PATH |
| 195 | end |
| 196 | ret 0 |
| 197 | end |
| 198 | |
| 199 | note Overlap test between two normalized targets (same kind). |
| 200 | fn ls_norm_overlap kind ulong ao ulong al ulong bo ulong bl ulong to bool |
| 201 | if eq kind LS_KIND_BRANCH |
| 202 | ret syn_arena_eq ao al bo bl |
| 203 | end |
| 204 | if eq kind LS_KIND_PATH |
| 205 | if or syn_arena_eq_view ao al t_ls_wild syn_arena_eq_view bo bl t_ls_wild |
| 206 | ret true |
| 207 | end |
| 208 | ret ne cs_paths_overlap_arena ao al bo bl 0 |
| 209 | end |
| 210 | ret false |
| 211 | end |
| 212 | |
| 213 | note Sweep and mark all expired active leases as LS_ST_EXPIRED at monotonic timestamp now. |
| 214 | fn ls_expire_sweep now ulong to ulong |
| 215 | cnt = get g_ls_hdr 0 |
| 216 | expired = ulong 0 |
| 217 | for i ulong cnt |
| 218 | if eq ls_mget i LSF_STATUS LS_ST_ACTIVE |
| 219 | if ge now ls_mget i LSF_EXPIRES |
| 220 | ls_mput i LSF_STATUS LS_ST_EXPIRED |
| 221 | ls_mput i LSF_CLOSED ls_mget i LSF_EXPIRES |
| 222 | expired = add expired 1 |
| 223 | end |
| 224 | end |
| 225 | end |
| 226 | ret expired |
| 227 | end |
| 228 | |
| 229 | note Scans for an active overlapping lease at timestamp now; expires stale leases along the way. |
| 230 | note Returns conflicting lease index or SYN_NONE. |
| 231 | fn ls_find_conflict kind ulong norm_off ulong norm_len ulong now ulong to ulong |
| 232 | cnt = get g_ls_hdr 0 |
| 233 | for i ulong cnt |
| 234 | if eq ls_mget i LSF_STATUS LS_ST_ACTIVE |
| 235 | if ge now ls_mget i LSF_EXPIRES |
| 236 | ls_mput i LSF_STATUS LS_ST_EXPIRED |
| 237 | ls_mput i LSF_CLOSED ls_mget i LSF_EXPIRES |
| 238 | else |
| 239 | if eq ls_mget i LSF_KIND kind |
| 240 | if ls_norm_overlap kind ls_mget i LSF_NORM_OFF ls_mget i LSF_NORM_LEN norm_off norm_len |
| 241 | ret i |
| 242 | end |
| 243 | end |
| 244 | end |
| 245 | end |
| 246 | end |
| 247 | ret SYN_NONE |
| 248 | end |
| 249 | |
| 250 | note Claims an exclusive lease on target (branch:... or path:...) for holder at now with ttl > 0. |
| 251 | note Strictly refuses with SYN_ERR_LEASE_CONFLICT (2) when an active overlapping lease exists. |
| 252 | note Expired leases are automatically transitioned to LS_ST_EXPIRED and reclaimed. |
| 253 | fn ls_claim holder view uchar * target view uchar * now ulong ttl ulong to ulong |
| 254 | subj = new uchar 32 0 |
| 255 | if or eq len holder 0 gt len holder 128 |
| 256 | ret SYN_ERR_MALFORMED |
| 257 | end |
| 258 | if eq ttl 0 |
| 259 | ret SYN_ERR_MALFORMED |
| 260 | end |
| 261 | kind = ls_parse_target target |
| 262 | if eq kind 0 |
| 263 | ret SYN_ERR_MALFORMED |
| 264 | end |
| 265 | put g_ls_hdr 1 SYN_NONE |
| 266 | cidx = ls_find_conflict kind get g_ls_hdr 4 get g_ls_hdr 5 now |
| 267 | if ne cidx SYN_NONE |
| 268 | put g_ls_hdr 1 cidx |
| 269 | ret SYN_ERR_LEASE_CONFLICT |
| 270 | end |
| 271 | cnt = get g_ls_hdr 0 |
| 272 | if ge cnt LS_MAX |
| 273 | ret SYN_ERR_CAPACITY |
| 274 | end |
| 275 | ho = syn_arena_put holder |
| 276 | if eq ho SYN_NONE |
| 277 | ret SYN_ERR_CAPACITY |
| 278 | end |
| 279 | to_off = syn_arena_put target |
| 280 | if eq to_off SYN_NONE |
| 281 | ret SYN_ERR_CAPACITY |
| 282 | end |
| 283 | lid = add cnt 1 |
| 284 | ls_mput cnt LSF_ID lid |
| 285 | ls_mput cnt LSF_KIND kind |
| 286 | ls_mput cnt LSF_HOLD_OFF ho |
| 287 | ls_mput cnt LSF_HOLD_LEN len holder |
| 288 | ls_mput cnt LSF_TGT_OFF to_off |
| 289 | ls_mput cnt LSF_TGT_LEN len target |
| 290 | ls_mput cnt LSF_NORM_OFF get g_ls_hdr 4 |
| 291 | ls_mput cnt LSF_NORM_LEN get g_ls_hdr 5 |
| 292 | ls_mput cnt LSF_STATUS LS_ST_ACTIVE |
| 293 | ls_mput cnt LSF_CREATED now |
| 294 | ls_mput cnt LSF_EXPIRES add now ttl |
| 295 | ls_mput cnt LSF_CLOSED 0 |
| 296 | ls_mput cnt LSF_FIN_OFF 0 |
| 297 | ls_mput cnt LSF_FIN_LEN 0 |
| 298 | put g_ls_hdr 0 lid |
| 299 | put g_ls_hdr 2 lid |
| 300 | ls_make_subj lid subj |
| 301 | lg_set_subject subj |
| 302 | seq = lg_append_arena 3 now ho len holder to_off len target |
| 303 | if eq seq 0 |
| 304 | ret SYN_ERR_CAPACITY |
| 305 | end |
| 306 | ret SYN_OK |
| 307 | end |
| 308 | |
| 309 | note Renews an active lease; strictly requires holder == current holder and lease not expired/released. |
| 310 | fn ls_renew lid ulong holder view uchar * now ulong ttl ulong to ulong |
| 311 | subj = new uchar 32 0 |
| 312 | if eq ttl 0 |
| 313 | ret SYN_ERR_MALFORMED |
| 314 | end |
| 315 | idx = ls_find_by_id lid |
| 316 | if eq idx SYN_NONE |
| 317 | ret SYN_ERR_NOT_FOUND |
| 318 | end |
| 319 | if ne ls_mget idx LSF_STATUS LS_ST_ACTIVE |
| 320 | ret SYN_ERR_LEASE_CLOSED |
| 321 | end |
| 322 | if ge now ls_mget idx LSF_EXPIRES |
| 323 | ls_mput idx LSF_STATUS LS_ST_EXPIRED |
| 324 | ls_mput idx LSF_CLOSED ls_mget idx LSF_EXPIRES |
| 325 | ret SYN_ERR_LEASE_CLOSED |
| 326 | end |
| 327 | if eq syn_arena_eq_view ls_mget idx LSF_HOLD_OFF ls_mget idx LSF_HOLD_LEN holder false |
| 328 | ret SYN_ERR_NOT_HOLDER |
| 329 | end |
| 330 | ls_mput idx LSF_EXPIRES add now ttl |
| 331 | ls_make_subj lid subj |
| 332 | lg_set_subject subj |
| 333 | seq = lg_append_arena 4 now ls_mget idx LSF_HOLD_OFF ls_mget idx LSF_HOLD_LEN ls_mget idx LSF_TGT_OFF ls_mget idx LSF_TGT_LEN |
| 334 | if eq seq 0 |
| 335 | ret SYN_ERR_CAPACITY |
| 336 | end |
| 337 | ret SYN_OK |
| 338 | end |
| 339 | |
| 340 | note Releases an active lease with an explicit final status word (e.g. MERGED, ABANDONED, DONE). |
| 341 | note Strictly requires holder == current holder. |
| 342 | fn ls_release lid ulong holder view uchar * final_st view uchar * now ulong to ulong |
| 343 | subj = new uchar 32 0 |
| 344 | if or eq len final_st 0 gt len final_st 64 |
| 345 | ret SYN_ERR_MALFORMED |
| 346 | end |
| 347 | idx = ls_find_by_id lid |
| 348 | if eq idx SYN_NONE |
| 349 | ret SYN_ERR_NOT_FOUND |
| 350 | end |
| 351 | if ne ls_mget idx LSF_STATUS LS_ST_ACTIVE |
| 352 | ret SYN_ERR_LEASE_CLOSED |
| 353 | end |
| 354 | if eq syn_arena_eq_view ls_mget idx LSF_HOLD_OFF ls_mget idx LSF_HOLD_LEN holder false |
| 355 | ret SYN_ERR_NOT_HOLDER |
| 356 | end |
| 357 | fo = syn_arena_put final_st |
| 358 | if eq fo SYN_NONE |
| 359 | ret SYN_ERR_CAPACITY |
| 360 | end |
| 361 | ls_mput idx LSF_STATUS LS_ST_RELEASED |
| 362 | ls_mput idx LSF_CLOSED now |
| 363 | ls_mput idx LSF_FIN_OFF fo |
| 364 | ls_mput idx LSF_FIN_LEN len final_st |
| 365 | ls_make_subj lid subj |
| 366 | lg_set_subject subj |
| 367 | seq = lg_append_arena 5 now ls_mget idx LSF_HOLD_OFF ls_mget idx LSF_HOLD_LEN fo len final_st |
| 368 | if eq seq 0 |
| 369 | ret SYN_ERR_CAPACITY |
| 370 | end |
| 371 | ret SYN_OK |
| 372 | end |