interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / protocol / synapse / core / lease.om

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