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