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 / graph.om

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