interplane.Get Omega
Read in your language

open-humanity

Privacy-first peer assistance network for sovereign autonomous agents

Brought by drakestapleton

branch main Branches Tags History

open-humanity / om / main.om

404 lines · 7946 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note protocol/open-humanity/main.om: CLI processing peer assistance protocol commands and
3note distress envelopes on stdin, printing policy-checked protocol responses on stdout.
4note Compose with: si/directives/om2.om protocol/open-humanity/lib.om protocol/open-humanity/main.om
5note Stdin line grammar (comments start with #):
6note OPTIN <0|1>
7note NOW <ms_u64>
8note DISTRESS <version> <peer_id> <consent> <category> <created_ms> <ttl_ms> <cap_flags> <payload...>
9note RESOLVE <req_id> <helper_peer_id> <consent> <cap_flags> <solution...>
10note SWEEP
11note AUDIT
12
13text oh_cmd_optin "OPTIN"
14text oh_cmd_now "NOW"
15text oh_cmd_distress "DISTRESS"
16text oh_cmd_resolve "RESOLVE"
17text oh_cmd_sweep "SWEEP"
18text oh_cmd_audit "AUDIT"
19
20text oh_out_ok_optin "OK OPTIN="
21text oh_out_ok_now "OK NOW="
22text oh_out_ok_enq "OK ENQUEUED req_id="
23text oh_out_dig " digest="
24text oh_out_ok_res "OK RESOLVED req_id="
25text oh_out_ok_swp "OK SWEPT count="
26text oh_out_ref "REFUSED code="
27text oh_out_rsn " reason="
28text oh_out_aud_hdr "AUDIT entries="
29text oh_out_aud_pend " pending="
30text oh_out_aud_row "AUDIT_ROW seq="
31text oh_out_aud_k " kind="
32text oh_out_aud_p " peer="
33text oh_out_aud_r " req="
34text oh_out_aud_c " code="
35text oh_out_err "ERROR bad command line\n"
36text oh_out_nl "\n"
37
38global oh_inbuf uchar 65536
39
40fn oh_read_stdin to long
41total = ulong 0
42while lt total 65536 bound 65536
43k = read 0 oh_inbuf total sub 65536 total
44if lt k 0
45ret k
46end
47if eq k 0
48break
49end
50total = add total ulong k
51end
52ret long total
53end
54
55fn oh_is_ws c uchar to bool
56ret or eq c 32 or eq c 9 eq c 13
57end
58
59fn oh_skip_ws buf view uchar * pos ulong n ulong to ulong
60p = pos
61while lt p n bound 1024
62if not oh_is_ws get buf p
63break
64end
65p = add p 1
66end
67ret p
68end
69
70fn oh_tok_end buf view uchar * pos ulong n ulong to ulong
71p = pos
72while lt p n bound 1024
73if oh_is_ws get buf p
74break
75end
76p = add p 1
77end
78ret p
79end
80
81fn oh_tok_eq buf view uchar * s ulong e ulong lit view uchar * to bool
82ln = len lit
83if ne sub e s ln
84ret false
85end
86i = ulong 0
87while lt i ln bound 64
88if ne get buf add s i get lit i
89ret false
90end
91i = add i 1
92end
93ret true
94end
95
96fn oh_parse_u64_ok buf view uchar * s ulong e ulong to bool
97if ge s e
98ret false
99end
100if gt sub e s 20
101ret false
102end
103i = s
104while lt i e bound 24
105c = get buf i
106if or lt c 48 gt c 57
107ret false
108end
109i = add i 1
110end
111ret true
112end
113
114fn oh_parse_u64_val buf view uchar * s ulong e ulong to ulong
115acc = ulong 0
116i = s
117while lt i e bound 24
118d = ulong sub get buf i 48
119acc = add mul acc 10 d
120i = add i 1
121end
122ret acc
123end
124
125fn oh_next_u64 line view uchar * pos edit ulong 1 dst edit ulong 1 to bool
126n = len line
127p = oh_skip_ws line get pos 0 n
128e = oh_tok_end line p n
129if not oh_parse_u64_ok line p e
130ret false
131end
132put dst 0 oh_parse_u64_val line p e
133put pos 0 e
134ret true
135end
136
137fn oh_emit_refused code ulong to void
138ok = oh_write_all 1 oh_out_ref
139oh_write_u64 1 code
140ok = and ok oh_write_all 1 oh_out_rsn
141oh_write_reason 1 code
142ok = and ok oh_write_all 1 oh_out_nl
143if ok
144ret
145end
146end
147
148fn oh_wire_submit pol edit OhPolicy env view OhEnv payload view uchar * to void
149wire = new uchar 560 0
150parsed = new OhEnv 0 0 0 0 0 0 0 0 0
151pbuf = new uchar 512 0
152req_out = new ulong 1 0
153wn = oh_serialize_envelope env payload wire
154if eq wn 0
155oh_emit_refused OH_ERR_TRUNCATED
156ret
157end
158prc = oh_parse_envelope part wire 0 wn parsed pbuf
159if ne prc OH_OK
160oh_emit_refused prc
161ret
162end
163rc = oh_submit_distress pol parsed part pbuf 0 get parsed payload_len req_out
164if eq rc OH_OK
165ok = oh_write_all 1 oh_out_ok_enq
166oh_write_u64 1 get req_out 0
167ok = and ok oh_write_all 1 oh_out_dig
168oh_write_hex64 1 get parsed digest
169ok = and ok oh_write_all 1 oh_out_nl
170if ok
171ret
172end
173ret
174end
175oh_emit_refused rc
176end
177
178fn oh_handle_distress line view uchar * start ulong pol edit OhPolicy to bool
179pos = new ulong 1 start
180tmp = new ulong 1 0
181env = new OhEnv 0 0 0 0 0 0 0 0 0
182if not oh_next_u64 line pos tmp
183ret false
184end
185put env version get tmp 0
186if not oh_next_u64 line pos tmp
187ret false
188end
189put env peer_id get tmp 0
190if not oh_next_u64 line pos tmp
191ret false
192end
193put env consent get tmp 0
194if not oh_next_u64 line pos tmp
195ret false
196end
197put env category get tmp 0
198if not oh_next_u64 line pos tmp
199ret false
200end
201put env created_ms get tmp 0
202if not oh_next_u64 line pos tmp
203ret false
204end
205put env ttl_ms get tmp 0
206if not oh_next_u64 line pos tmp
207ret false
208end
209put env cap_flags get tmp 0
210n = len line
211ps = oh_skip_ws line get pos 0 n
212plen = ulong 0
213if lt ps n
214plen = sub n ps
215end
216if gt plen OH_MAX_PAYLOAD
217plen = OH_MAX_PAYLOAD
218end
219oh_wire_submit pol env part line ps plen
220ret true
221end
222
223fn oh_handle_resolve line view uchar * start ulong pol edit OhPolicy to bool
224pos = new ulong 1 start
225tmp = new ulong 1 0
226helper = new OhEnv OH_VERSION 0 0 0 0 0 0 0 0
227if not oh_next_u64 line pos tmp
228ret false
229end
230req_id = get tmp 0
231if not oh_next_u64 line pos tmp
232ret false
233end
234put helper peer_id get tmp 0
235if not oh_next_u64 line pos tmp
236ret false
237end
238put helper consent get tmp 0
239if not oh_next_u64 line pos tmp
240ret false
241end
242put helper cap_flags get tmp 0
243n = len line
244ps = oh_skip_ws line get pos 0 n
245plen = ulong 0
246if lt ps n
247plen = sub n ps
248end
249if gt plen OH_MAX_PAYLOAD
250plen = OH_MAX_PAYLOAD
251end
252rc = oh_resolve_distress pol req_id helper part line ps plen
253if eq rc OH_OK
254ok = oh_write_all 1 oh_out_ok_res
255oh_write_u64 1 req_id
256ok = and ok oh_write_all 1 oh_out_nl
257if ok
258ret true
259end
260ret true
261end
262oh_emit_refused rc
263ret true
264end
265
266fn oh_emit_audit to void
267cnt = oh_audit_count
268pend = oh_queue_pending_count
269ok = oh_write_all 1 oh_out_aud_hdr
270oh_write_u64 1 cnt
271ok = and ok oh_write_all 1 oh_out_aud_pend
272oh_write_u64 1 pend
273ok = and ok oh_write_all 1 oh_out_nl
274i = ulong 0
275while lt i cnt bound 64
276ok = and ok oh_write_all 1 oh_out_aud_row
277oh_write_u64 1 get oh_aud_seq i
278ok = and ok oh_write_all 1 oh_out_aud_k
279oh_write_u64 1 get oh_aud_kind i
280ok = and ok oh_write_all 1 oh_out_aud_p
281oh_write_u64 1 get oh_aud_peer i
282ok = and ok oh_write_all 1 oh_out_aud_r
283oh_write_u64 1 get oh_aud_req i
284ok = and ok oh_write_all 1 oh_out_aud_c
285oh_write_u64 1 get oh_aud_code i
286ok = and ok oh_write_all 1 oh_out_nl
287i = add i 1
288end
289if ok
290ret
291end
292end
293
294fn oh_process_line line view uchar * pol edit OhPolicy to bool
295pos = new ulong 1 0
296tmp = new ulong 1 0
297n = len line
298p = oh_skip_ws line 0 n
299if ge p n
300ret true
301end
302if eq get line p 35
303ret true
304end
305e = oh_tok_end line p n
306put pos 0 e
307if oh_tok_eq line p e oh_cmd_optin
308if not oh_next_u64 line pos tmp
309ret false
310end
311v = get tmp 0
312if gt v 1
313ret false
314end
315put pol opt_in v
316ok = oh_write_all 1 oh_out_ok_optin
317oh_write_u64 1 v
318ok = and ok oh_write_all 1 oh_out_nl
319ret ok
320end
321if oh_tok_eq line p e oh_cmd_now
322if not oh_next_u64 line pos tmp
323ret false
324end
325put pol now_ms get tmp 0
326ok = oh_write_all 1 oh_out_ok_now
327oh_write_u64 1 get tmp 0
328ok = and ok oh_write_all 1 oh_out_nl
329ret ok
330end
331if oh_tok_eq line p e oh_cmd_distress
332ret oh_handle_distress line e pol
333end
334if oh_tok_eq line p e oh_cmd_resolve
335ret oh_handle_resolve line e pol
336end
337if oh_tok_eq line p e oh_cmd_sweep
338sw = oh_queue_sweep_expired pol
339ok = oh_write_all 1 oh_out_ok_swp
340oh_write_u64 1 sw
341ok = and ok oh_write_all 1 oh_out_nl
342ret ok
343end
344if oh_tok_eq line p e oh_cmd_audit
345oh_emit_audit
346ret true
347end
348ret false
349end
350
351fn oh_copy_line pos ulong total ulong dst edit uchar 768 to long
352cnt = ulong 0
353p = pos
354while lt p total bound 65536
355c = get oh_inbuf p
356if eq c 10
357break
358end
359if ge cnt 768
360ret -1
361end
362put dst cnt c
363cnt = add cnt 1
364p = add p 1
365end
366ret long cnt
367end
368
369fn main to int
370got = oh_read_stdin
371if lt got 0
372ret 1
373end
374total = ulong got
375pol = new OhPolicy 0 0 1 0 0
376oh_reset_state pol
377line = new uchar 768 0
378pos = ulong 0
379while lt pos total bound 65536
380ln_s = oh_copy_line pos total line
381if lt ln_s 0
382oke_l = oh_write_all 2 oh_out_err
383if oke_l
384ret 1
385end
386ret 1
387end
388ln = ulong ln_s
389if not oh_process_line part line 0 ln pol
390oke = oh_write_all 2 oh_out_err
391if oke
392ret 1
393end
394ret 1
395end
396pos = add pos ln
397if lt pos total
398if eq get oh_inbuf pos 10
399pos = add pos 1
400end
401end
402end
403ret 0
404end