aegis-runtime-mv
Native Rust agent runtime: Axum WebSocket/SSE/REST gateway, heartbeat scheduler, dependency-aware task workflow, SQLite storage. Attachment of AienOS. Pre-alpha.
Brought by drakestapleton
aegis-runtime-mv / om / heartbeat.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note core/aegis/heartbeat.om: Deterministic heartbeat and lease scheduler (pure OM-2 library unit). |
| 3 | note Converted from aegis-runtime-mv/src/heartbeat.rs and src/persistence/task_lifecycle.rs. |
| 4 | note Agent registration, monotonic tick evaluation, missed-heartbeat liveness expiry, |
| 5 | note exclusive task lease assignment/renewal/release, fencing stale-generation refusal, |
| 6 | note and bounded global + per-agent concurrency cap. |
| 7 | |
| 8 | const HB_MAX_AGENTS ulong 64 |
| 9 | const HB_MAX_LEASES ulong 128 |
| 10 | |
| 11 | const AGENT_UNUSED ulong 0 |
| 12 | const AGENT_ALIVE ulong 1 |
| 13 | const AGENT_EXPIRED ulong 2 |
| 14 | const AGENT_DEREGISTERED ulong 3 |
| 15 | |
| 16 | const LEASE_FREE ulong 0 |
| 17 | const LEASE_ACTIVE ulong 1 |
| 18 | const LEASE_RELEASED ulong 2 |
| 19 | const LEASE_EXPIRED ulong 3 |
| 20 | |
| 21 | const HB_OK ulong 0 |
| 22 | const HB_ERR_FULL ulong 1 |
| 23 | const HB_ERR_DUPLICATE ulong 2 |
| 24 | const HB_ERR_UNKNOWN_AGENT ulong 3 |
| 25 | const HB_ERR_AGENT_EXPIRED ulong 4 |
| 26 | const HB_ERR_CONCURRENCY_CAP ulong 5 |
| 27 | const HB_ERR_ALREADY_LEASED ulong 6 |
| 28 | const HB_ERR_UNKNOWN_LEASE ulong 7 |
| 29 | const HB_ERR_STALE_GEN ulong 8 |
| 30 | const HB_ERR_WRONG_OWNER ulong 9 |
| 31 | const HB_ERR_NON_MONOTONIC ulong 10 |
| 32 | const HB_ERR_INVALID_PARAM ulong 11 |
| 33 | |
| 34 | note hb_meta: |
| 35 | note [0]=current_tick, [1]=agent_cnt, [2]=lease_cnt, [3]=max_conc, [4]=active_leases, |
| 36 | note [5]=agent_ttl, [6]=lease_ttl, [7]=next_gen, [8]=pulse_cnt, |
| 37 | note [9]=last_reclaimed, [10]=last_expired_agents, [11]=total_reclaimed, [12]=degraded |
| 38 | global hb_meta ulong 16 |
| 39 | |
| 40 | global hb_agent_id ulong 64 |
| 41 | global hb_agent_state ulong 64 |
| 42 | global hb_agent_last_tick ulong 64 |
| 43 | global hb_agent_missed ulong 64 |
| 44 | global hb_agent_cap ulong 64 |
| 45 | global hb_agent_active ulong 64 |
| 46 | global hb_agent_epoch ulong 64 |
| 47 | |
| 48 | global hb_lease_task ulong 128 |
| 49 | global hb_lease_agent ulong 128 |
| 50 | global hb_lease_gen ulong 128 |
| 51 | global hb_lease_state ulong 128 |
| 52 | global hb_lease_expires ulong 128 |
| 53 | global hb_lease_acquired ulong 128 |
| 54 | |
| 55 | global hb_reclaimed_tasks ulong 128 |
| 56 | |
| 57 | fn hb_init max_conc ulong agent_ttl ulong lease_ttl ulong to ulong |
| 58 | if or eq max_conc 0 or eq agent_ttl 0 eq lease_ttl 0 |
| 59 | ret HB_ERR_INVALID_PARAM |
| 60 | end |
| 61 | if gt max_conc HB_MAX_LEASES |
| 62 | ret HB_ERR_INVALID_PARAM |
| 63 | end |
| 64 | for i ulong 16 |
| 65 | put hb_meta i 0 |
| 66 | end |
| 67 | put hb_meta 3 max_conc |
| 68 | put hb_meta 5 agent_ttl |
| 69 | put hb_meta 6 lease_ttl |
| 70 | put hb_meta 7 1 |
| 71 | for i ulong 64 |
| 72 | put hb_agent_id i 0 |
| 73 | put hb_agent_state i AGENT_UNUSED |
| 74 | put hb_agent_last_tick i 0 |
| 75 | put hb_agent_missed i 0 |
| 76 | put hb_agent_cap i 0 |
| 77 | put hb_agent_active i 0 |
| 78 | put hb_agent_epoch i 0 |
| 79 | end |
| 80 | for i ulong 128 |
| 81 | put hb_lease_task i 0 |
| 82 | put hb_lease_agent i 0 |
| 83 | put hb_lease_gen i 0 |
| 84 | put hb_lease_state i LEASE_FREE |
| 85 | put hb_lease_expires i 0 |
| 86 | put hb_lease_acquired i 0 |
| 87 | put hb_reclaimed_tasks i 0 |
| 88 | end |
| 89 | ret HB_OK |
| 90 | end |
| 91 | |
| 92 | fn hb_current_tick to ulong |
| 93 | ret get hb_meta 0 |
| 94 | end |
| 95 | |
| 96 | fn hb_max_concurrency to ulong |
| 97 | ret get hb_meta 3 |
| 98 | end |
| 99 | |
| 100 | fn hb_active_leases to ulong |
| 101 | ret get hb_meta 4 |
| 102 | end |
| 103 | |
| 104 | fn hb_pulse_count to ulong |
| 105 | ret get hb_meta 8 |
| 106 | end |
| 107 | |
| 108 | fn hb_last_reclaimed to ulong |
| 109 | ret get hb_meta 9 |
| 110 | end |
| 111 | |
| 112 | fn hb_last_expired_agents to ulong |
| 113 | ret get hb_meta 10 |
| 114 | end |
| 115 | |
| 116 | fn hb_total_reclaimed to ulong |
| 117 | ret get hb_meta 11 |
| 118 | end |
| 119 | |
| 120 | fn hb_is_degraded to bool |
| 121 | ret ne get hb_meta 12 0 |
| 122 | end |
| 123 | |
| 124 | fn hb_get_reclaimed idx ulong to ulong |
| 125 | if ge idx get hb_meta 9 |
| 126 | ret 0 |
| 127 | end |
| 128 | ret get hb_reclaimed_tasks idx |
| 129 | end |
| 130 | |
| 131 | fn hb_find_agent agent_id ulong to long |
| 132 | if eq agent_id 0 |
| 133 | ret -1 |
| 134 | end |
| 135 | n = get hb_meta 1 |
| 136 | for i ulong n bound 64 |
| 137 | if eq get hb_agent_id i agent_id |
| 138 | ret long i |
| 139 | end |
| 140 | end |
| 141 | ret -1 |
| 142 | end |
| 143 | |
| 144 | fn hb_find_lease task_id ulong to long |
| 145 | if eq task_id 0 |
| 146 | ret -1 |
| 147 | end |
| 148 | n = get hb_meta 2 |
| 149 | for i ulong n bound 128 |
| 150 | if eq get hb_lease_task i task_id |
| 151 | ret long i |
| 152 | end |
| 153 | end |
| 154 | ret -1 |
| 155 | end |
| 156 | |
| 157 | fn hb_agent_status agent_id ulong to ulong |
| 158 | s = hb_find_agent agent_id |
| 159 | if lt s 0 |
| 160 | ret AGENT_UNUSED |
| 161 | end |
| 162 | ret get hb_agent_state ulong s |
| 163 | end |
| 164 | |
| 165 | fn hb_lease_owner task_id ulong to ulong |
| 166 | s = hb_find_lease task_id |
| 167 | if lt s 0 |
| 168 | ret 0 |
| 169 | end |
| 170 | slot = ulong s |
| 171 | if ne get hb_lease_state slot LEASE_ACTIVE |
| 172 | ret 0 |
| 173 | end |
| 174 | ret get hb_lease_agent slot |
| 175 | end |
| 176 | |
| 177 | fn hb_lease_current_gen task_id ulong to ulong |
| 178 | s = hb_find_lease task_id |
| 179 | if lt s 0 |
| 180 | ret 0 |
| 181 | end |
| 182 | ret get hb_lease_gen ulong s |
| 183 | end |
| 184 | |
| 185 | fn hb_register_agent agent_id ulong agent_cap ulong to ulong |
| 186 | if or eq agent_id 0 eq agent_cap 0 |
| 187 | ret HB_ERR_INVALID_PARAM |
| 188 | end |
| 189 | s = hb_find_agent agent_id |
| 190 | if ge s 0 |
| 191 | slot = ulong s |
| 192 | if eq get hb_agent_state slot AGENT_ALIVE |
| 193 | ret HB_ERR_DUPLICATE |
| 194 | end |
| 195 | note Re-registering an expired/deregistered agent bumps its epoch; prior lease generations stay fenced. |
| 196 | put hb_agent_state slot AGENT_ALIVE |
| 197 | put hb_agent_last_tick slot get hb_meta 0 |
| 198 | put hb_agent_missed slot 0 |
| 199 | put hb_agent_cap slot agent_cap |
| 200 | put hb_agent_active slot 0 |
| 201 | put hb_agent_epoch slot add get hb_agent_epoch slot 1 |
| 202 | ret HB_OK |
| 203 | end |
| 204 | n = get hb_meta 1 |
| 205 | if ge n HB_MAX_AGENTS |
| 206 | ret HB_ERR_FULL |
| 207 | end |
| 208 | put hb_agent_id n agent_id |
| 209 | put hb_agent_state n AGENT_ALIVE |
| 210 | put hb_agent_last_tick n get hb_meta 0 |
| 211 | put hb_agent_missed n 0 |
| 212 | put hb_agent_cap n agent_cap |
| 213 | put hb_agent_active n 0 |
| 214 | put hb_agent_epoch n 1 |
| 215 | put hb_meta 1 add n 1 |
| 216 | ret HB_OK |
| 217 | end |
| 218 | |
| 219 | fn hb_record_reclaim task_id ulong to void |
| 220 | rc = get hb_meta 9 |
| 221 | if lt rc HB_MAX_LEASES |
| 222 | put hb_reclaimed_tasks rc task_id |
| 223 | put hb_meta 9 add rc 1 |
| 224 | end |
| 225 | put hb_meta 11 add get hb_meta 11 1 |
| 226 | put hb_meta 12 1 |
| 227 | end |
| 228 | |
| 229 | fn hb_reclaim_lease_slot lslot ulong to void |
| 230 | if ne get hb_lease_state lslot LEASE_ACTIVE |
| 231 | ret |
| 232 | end |
| 233 | aid = get hb_lease_agent lslot |
| 234 | tid = get hb_lease_task lslot |
| 235 | put hb_lease_state lslot LEASE_EXPIRED |
| 236 | act = get hb_meta 4 |
| 237 | if gt act 0 |
| 238 | put hb_meta 4 sub act 1 |
| 239 | end |
| 240 | aslot = hb_find_agent aid |
| 241 | if ge aslot 0 |
| 242 | aidx = ulong aslot |
| 243 | aa = get hb_agent_active aidx |
| 244 | if gt aa 0 |
| 245 | put hb_agent_active aidx sub aa 1 |
| 246 | end |
| 247 | end |
| 248 | hb_record_reclaim tid |
| 249 | end |
| 250 | |
| 251 | fn hb_reclaim_agent_leases agent_id ulong to void |
| 252 | lcnt = get hb_meta 2 |
| 253 | for j ulong lcnt bound 128 |
| 254 | if eq get hb_lease_state j LEASE_ACTIVE |
| 255 | if eq get hb_lease_agent j agent_id |
| 256 | hb_reclaim_lease_slot j |
| 257 | end |
| 258 | end |
| 259 | end |
| 260 | end |
| 261 | |
| 262 | fn hb_heartbeat agent_id ulong tick ulong to ulong |
| 263 | if lt tick get hb_meta 0 |
| 264 | ret HB_ERR_NON_MONOTONIC |
| 265 | end |
| 266 | s = hb_find_agent agent_id |
| 267 | if lt s 0 |
| 268 | ret HB_ERR_UNKNOWN_AGENT |
| 269 | end |
| 270 | slot = ulong s |
| 271 | if ne get hb_agent_state slot AGENT_ALIVE |
| 272 | ret HB_ERR_AGENT_EXPIRED |
| 273 | end |
| 274 | put hb_agent_last_tick slot tick |
| 275 | put hb_agent_missed slot 0 |
| 276 | ret HB_OK |
| 277 | end |
| 278 | |
| 279 | note Deterministic monotonic tick evaluation: |
| 280 | note 1) Expire any ALIVE agent whose silence (new_tick - last_tick) exceeds agent_ttl, |
| 281 | note reclaiming all leases held by that expired agent. |
| 282 | note 2) Expire and reclaim any remaining ACTIVE lease whose deadline <= new_tick. |
| 283 | fn hb_tick new_tick ulong to ulong |
| 284 | if lt new_tick get hb_meta 0 |
| 285 | ret HB_ERR_NON_MONOTONIC |
| 286 | end |
| 287 | put hb_meta 0 new_tick |
| 288 | put hb_meta 8 add get hb_meta 8 1 |
| 289 | put hb_meta 9 0 |
| 290 | put hb_meta 10 0 |
| 291 | put hb_meta 12 0 |
| 292 | attl = get hb_meta 5 |
| 293 | acnt = get hb_meta 1 |
| 294 | for i ulong acnt bound 64 |
| 295 | if eq get hb_agent_state i AGENT_ALIVE |
| 296 | last = get hb_agent_last_tick i |
| 297 | gap = ulong 0 |
| 298 | if gt new_tick last |
| 299 | gap = sub new_tick last |
| 300 | end |
| 301 | put hb_agent_missed i gap |
| 302 | if gt gap attl |
| 303 | put hb_agent_state i AGENT_EXPIRED |
| 304 | put hb_meta 10 add get hb_meta 10 1 |
| 305 | put hb_meta 12 1 |
| 306 | hb_reclaim_agent_leases get hb_agent_id i |
| 307 | end |
| 308 | end |
| 309 | end |
| 310 | lcnt = get hb_meta 2 |
| 311 | for j ulong lcnt bound 128 |
| 312 | if eq get hb_lease_state j LEASE_ACTIVE |
| 313 | if ge new_tick get hb_lease_expires j |
| 314 | hb_reclaim_lease_slot j |
| 315 | end |
| 316 | end |
| 317 | end |
| 318 | ret HB_OK |
| 319 | end |
| 320 | |
| 321 | note Acquire an exclusive task lease for agent_id. Writes strictly monotonic generation into dst_gen[0]. |
| 322 | fn hb_acquire_lease task_id ulong agent_id ulong dst_gen edit ulong 1 to ulong |
| 323 | put dst_gen 0 0 |
| 324 | if or eq task_id 0 eq agent_id 0 |
| 325 | ret HB_ERR_INVALID_PARAM |
| 326 | end |
| 327 | aslot = hb_find_agent agent_id |
| 328 | if lt aslot 0 |
| 329 | ret HB_ERR_UNKNOWN_AGENT |
| 330 | end |
| 331 | aidx = ulong aslot |
| 332 | if ne get hb_agent_state aidx AGENT_ALIVE |
| 333 | ret HB_ERR_AGENT_EXPIRED |
| 334 | end |
| 335 | if ge get hb_meta 4 get hb_meta 3 |
| 336 | ret HB_ERR_CONCURRENCY_CAP |
| 337 | end |
| 338 | if ge get hb_agent_active aidx get hb_agent_cap aidx |
| 339 | ret HB_ERR_CONCURRENCY_CAP |
| 340 | end |
| 341 | lfind = hb_find_lease task_id |
| 342 | lslot = ulong 0 |
| 343 | if ge lfind 0 |
| 344 | lslot = ulong lfind |
| 345 | if eq get hb_lease_state lslot LEASE_ACTIVE |
| 346 | ret HB_ERR_ALREADY_LEASED |
| 347 | end |
| 348 | else |
| 349 | lslot = get hb_meta 2 |
| 350 | if ge lslot HB_MAX_LEASES |
| 351 | ret HB_ERR_FULL |
| 352 | end |
| 353 | put hb_meta 2 add lslot 1 |
| 354 | end |
| 355 | gen = get hb_meta 7 |
| 356 | put hb_meta 7 add gen 1 |
| 357 | now = get hb_meta 0 |
| 358 | put hb_lease_task lslot task_id |
| 359 | put hb_lease_agent lslot agent_id |
| 360 | put hb_lease_gen lslot gen |
| 361 | put hb_lease_state lslot LEASE_ACTIVE |
| 362 | put hb_lease_acquired lslot now |
| 363 | put hb_lease_expires lslot add now get hb_meta 6 |
| 364 | put hb_meta 4 add get hb_meta 4 1 |
| 365 | put hb_agent_active aidx add get hb_agent_active aidx 1 |
| 366 | put dst_gen 0 gen |
| 367 | ret HB_OK |
| 368 | end |
| 369 | |
| 370 | note Verify an actor still owns the active lease at the exact fencing generation. |
| 371 | fn hb_verify_lease task_id ulong agent_id ulong gen ulong to ulong |
| 372 | lfind = hb_find_lease task_id |
| 373 | if lt lfind 0 |
| 374 | ret HB_ERR_UNKNOWN_LEASE |
| 375 | end |
| 376 | lslot = ulong lfind |
| 377 | if ne get hb_lease_state lslot LEASE_ACTIVE |
| 378 | ret HB_ERR_STALE_GEN |
| 379 | end |
| 380 | if ne get hb_lease_gen lslot gen |
| 381 | ret HB_ERR_STALE_GEN |
| 382 | end |
| 383 | if ne get hb_lease_agent lslot agent_id |
| 384 | ret HB_ERR_WRONG_OWNER |
| 385 | end |
| 386 | aslot = hb_find_agent agent_id |
| 387 | if lt aslot 0 |
| 388 | ret HB_ERR_UNKNOWN_AGENT |
| 389 | end |
| 390 | if ne get hb_agent_state ulong aslot AGENT_ALIVE |
| 391 | ret HB_ERR_AGENT_EXPIRED |
| 392 | end |
| 393 | ret HB_OK |
| 394 | end |
| 395 | |
| 396 | fn hb_renew_lease task_id ulong agent_id ulong gen ulong to ulong |
| 397 | chk = hb_verify_lease task_id agent_id gen |
| 398 | if ne chk HB_OK |
| 399 | ret chk |
| 400 | end |
| 401 | lfind = hb_find_lease task_id |
| 402 | lslot = ulong lfind |
| 403 | now = get hb_meta 0 |
| 404 | put hb_lease_expires lslot add now get hb_meta 6 |
| 405 | ret HB_OK |
| 406 | end |
| 407 | |
| 408 | fn hb_release_lease task_id ulong agent_id ulong gen ulong to ulong |
| 409 | chk = hb_verify_lease task_id agent_id gen |
| 410 | if ne chk HB_OK |
| 411 | ret chk |
| 412 | end |
| 413 | lfind = hb_find_lease task_id |
| 414 | lslot = ulong lfind |
| 415 | put hb_lease_state lslot LEASE_RELEASED |
| 416 | act = get hb_meta 4 |
| 417 | if gt act 0 |
| 418 | put hb_meta 4 sub act 1 |
| 419 | end |
| 420 | aslot = hb_find_agent agent_id |
| 421 | if ge aslot 0 |
| 422 | aidx = ulong aslot |
| 423 | aa = get hb_agent_active aidx |
| 424 | if gt aa 0 |
| 425 | put hb_agent_active aidx sub aa 1 |
| 426 | end |
| 427 | end |
| 428 | ret HB_OK |
| 429 | end |