interplane.Get Omega
Read in your language

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

branch main Branches Tags History

aegis-runtime-mv / om / heartbeat.om

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