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
omega-mv / protocol / rad-id-sync / main.om
| 1 | note protocol/rad-id-sync/main.om: CLI entrypoint for rad-id-sync |
| 2 | note Compose with: si/directives/om2.om protocol/rad-id-sync/lib.om protocol/rad-id-sync/main.om |
| 3 | |
| 4 | text cmd_status "status" |
| 5 | text cmd_sync "sync" |
| 6 | text cmd_json "json" |
| 7 | text cmd_check_rid "check-rid" |
| 8 | text cmd_check_did "check-did" |
| 9 | text cmd_diff "diff" |
| 10 | |
| 11 | text hdr_anchor "=== Radicle Identity Anchor ===\n" |
| 12 | text lbl_did "DID: " |
| 13 | text lbl_name "Name: " |
| 14 | text lbl_type "Type: autonomous_agent\n" |
| 15 | text lbl_radk "Radicle Key: " |
| 16 | text lbl_sshk "SSH Key: " |
| 17 | text lbl_hist "Key History: " |
| 18 | text lbl_recs " records\n" |
| 19 | text lbl_none "<none>\n" |
| 20 | text lbl_nl "\n" |
| 21 | |
| 22 | text msg_valid_rid "VALID RID\n" |
| 23 | text msg_invalid_rid "INVALID RID\n" |
| 24 | text msg_valid_did "VALID DID\n" |
| 25 | text msg_invalid_did "INVALID DID\n" |
| 26 | text msg_usage "Usage: rad-id-sync [status|sync|json|check-rid <rid>|check-did <did>|diff]\n" |
| 27 | |
| 28 | text k_rid "rid=" |
| 29 | text k_bth "base_threshold=" |
| 30 | text k_bdid "base_did=" |
| 31 | text k_rth "remote_threshold=" |
| 32 | text k_rdid "remote_did=" |
| 33 | text k_appr "approvals=" |
| 34 | text k_allow "allow_add=" |
| 35 | |
| 36 | text dj_open "{\"code\":" |
| 37 | text dj_dec ",\"decision\":" |
| 38 | text dj_add ",\"added\":" |
| 39 | text dj_rem ",\"removed\":" |
| 40 | text dj_com ",\"common\":" |
| 41 | text dj_tch ",\"threshold_changed\":" |
| 42 | text dj_close "}\n" |
| 43 | |
| 44 | fn print_status to int |
| 45 | buf = new uchar 1024 0 |
| 46 | p = ulong 0 |
| 47 | p = append_bytes buf p hdr_anchor |
| 48 | p = append_bytes buf p lbl_did |
| 49 | dl = get g_anc_did_len 0 |
| 50 | i = ulong 0 |
| 51 | while lt i dl bound 64 |
| 52 | p = append_byte buf p get g_anc_did i |
| 53 | i = add i ulong 1 |
| 54 | end |
| 55 | p = append_bytes buf p lbl_nl |
| 56 | p = append_bytes buf p lbl_name |
| 57 | nl = get g_anc_name_len 0 |
| 58 | j = ulong 0 |
| 59 | while lt j nl bound 64 |
| 60 | p = append_byte buf p get g_anc_name j |
| 61 | j = add j ulong 1 |
| 62 | end |
| 63 | p = append_bytes buf p lbl_nl |
| 64 | p = append_bytes buf p lbl_type |
| 65 | p = append_bytes buf p lbl_radk |
| 66 | rl = get g_anc_rad_len 0 |
| 67 | if eq rl ulong 0 |
| 68 | p = append_bytes buf p lbl_none |
| 69 | else |
| 70 | k = ulong 0 |
| 71 | while lt k rl bound 256 |
| 72 | p = append_byte buf p get g_anc_rad_key k |
| 73 | k = add k ulong 1 |
| 74 | end |
| 75 | p = append_bytes buf p lbl_nl |
| 76 | end |
| 77 | p = append_bytes buf p lbl_sshk |
| 78 | sl = get g_anc_ssh_len 0 |
| 79 | if eq sl ulong 0 |
| 80 | p = append_bytes buf p lbl_none |
| 81 | else |
| 82 | m = ulong 0 |
| 83 | while lt m sl bound 256 |
| 84 | p = append_byte buf p get g_anc_ssh_key m |
| 85 | m = add m ulong 1 |
| 86 | end |
| 87 | p = append_bytes buf p lbl_nl |
| 88 | end |
| 89 | p = append_bytes buf p lbl_hist |
| 90 | p = append_u64 buf p get g_anc_hist_n 0 |
| 91 | p = append_bytes buf p lbl_recs |
| 92 | ok = write_all int 1 part buf 0 p |
| 93 | if not ok |
| 94 | ret 1 |
| 95 | end |
| 96 | ret 0 |
| 97 | end |
| 98 | |
| 99 | fn read_stdin_all dst edit uchar * to ulong |
| 100 | cap = len dst |
| 101 | pos = ulong 0 |
| 102 | note bound 4096 caps stdin reads for CLI descriptor input |
| 103 | while lt pos cap bound 4096 |
| 104 | remn = sub cap pos |
| 105 | k = read int 0 dst pos remn |
| 106 | if le k long 0 |
| 107 | break |
| 108 | end |
| 109 | pos = add pos ulong k |
| 110 | end |
| 111 | ret pos |
| 112 | end |
| 113 | |
| 114 | fn parse_diff_line line view uchar * appr edit ulong 1 allow edit ulong 1 to ulong |
| 115 | s = trim_start line |
| 116 | e = trim_end line |
| 117 | if ge s e |
| 118 | ret RAD_OK |
| 119 | end |
| 120 | n = sub e s |
| 121 | ok = new ulong 1 0 |
| 122 | if slice_starts_with part line s n k_rid |
| 123 | r1 = base_doc_set_rid part line add s ulong 4 sub n ulong 4 |
| 124 | if ne r1 RAD_OK |
| 125 | ret r1 |
| 126 | end |
| 127 | ret remote_doc_set_rid part line add s ulong 4 sub n ulong 4 |
| 128 | end |
| 129 | if slice_starts_with part line s n k_bth |
| 130 | v = parse_u64_strict part line add s ulong 15 sub n ulong 15 ok |
| 131 | if eq get ok 0 ulong 0 |
| 132 | ret RAD_ERR_PARSE |
| 133 | end |
| 134 | base_doc_set_threshold v |
| 135 | ret RAD_OK |
| 136 | end |
| 137 | if slice_starts_with part line s n k_rth |
| 138 | v = parse_u64_strict part line add s ulong 17 sub n ulong 17 ok |
| 139 | if eq get ok 0 ulong 0 |
| 140 | ret RAD_ERR_PARSE |
| 141 | end |
| 142 | remote_doc_set_threshold v |
| 143 | ret RAD_OK |
| 144 | end |
| 145 | if slice_starts_with part line s n k_bdid |
| 146 | ret base_doc_add_delegate part line add s ulong 9 sub n ulong 9 |
| 147 | end |
| 148 | if slice_starts_with part line s n k_rdid |
| 149 | ret remote_doc_add_delegate part line add s ulong 11 sub n ulong 11 |
| 150 | end |
| 151 | if slice_starts_with part line s n k_appr |
| 152 | v = parse_u64_strict part line add s ulong 10 sub n ulong 10 ok |
| 153 | if eq get ok 0 ulong 0 |
| 154 | ret RAD_ERR_PARSE |
| 155 | end |
| 156 | put appr 0 v |
| 157 | ret RAD_OK |
| 158 | end |
| 159 | if slice_starts_with part line s n k_allow |
| 160 | v = parse_u64_strict part line add s ulong 10 sub n ulong 10 ok |
| 161 | if eq get ok 0 ulong 0 |
| 162 | ret RAD_ERR_PARSE |
| 163 | end |
| 164 | put allow 0 v |
| 165 | ret RAD_OK |
| 166 | end |
| 167 | ret RAD_ERR_PARSE |
| 168 | end |
| 169 | |
| 170 | fn run_diff_stdin to int |
| 171 | inp = new uchar 4096 0 |
| 172 | jbuf = new uchar 512 0 |
| 173 | appr = new ulong 1 0 |
| 174 | allow = new ulong 1 0 |
| 175 | diff = new RadDiff 0 0 0 0 0 0 |
| 176 | base_doc_reset |
| 177 | remote_doc_reset |
| 178 | n = read_stdin_all inp |
| 179 | pos = ulong 0 |
| 180 | while lt pos n bound 4096 |
| 181 | ls = pos |
| 182 | while lt pos n bound 4096 |
| 183 | if eq get inp pos uchar 10 |
| 184 | break |
| 185 | end |
| 186 | pos = add pos ulong 1 |
| 187 | end |
| 188 | ll = sub pos ls |
| 189 | if gt ll ulong 0 |
| 190 | rc = parse_diff_line part inp ls ll appr allow |
| 191 | if ne rc RAD_OK |
| 192 | put diff code rc |
| 193 | put diff decision SYNC_REFUSED |
| 194 | break |
| 195 | end |
| 196 | end |
| 197 | if lt pos n |
| 198 | pos = add pos ulong 1 |
| 199 | end |
| 200 | end |
| 201 | if eq get diff code RAD_OK |
| 202 | rad_doc_diff_sync get appr 0 get allow 0 diff |
| 203 | end |
| 204 | p = ulong 0 |
| 205 | p = append_bytes jbuf p dj_open |
| 206 | p = append_u64 jbuf p get diff code |
| 207 | p = append_bytes jbuf p dj_dec |
| 208 | p = append_u64 jbuf p get diff decision |
| 209 | p = append_bytes jbuf p dj_add |
| 210 | p = append_u64 jbuf p get diff added |
| 211 | p = append_bytes jbuf p dj_rem |
| 212 | p = append_u64 jbuf p get diff removed |
| 213 | p = append_bytes jbuf p dj_com |
| 214 | p = append_u64 jbuf p get diff common |
| 215 | p = append_bytes jbuf p dj_tch |
| 216 | p = append_u64 jbuf p get diff thresh_chg |
| 217 | p = append_bytes jbuf p dj_close |
| 218 | ok = write_all int 1 part jbuf 0 p |
| 219 | if not ok |
| 220 | ret 1 |
| 221 | end |
| 222 | if ne get diff code RAD_OK |
| 223 | ret 1 |
| 224 | end |
| 225 | ret 0 |
| 226 | end |
| 227 | |
| 228 | fn main to int |
| 229 | arg1 = new uchar 256 0 |
| 230 | arg2 = new uchar 256 0 |
| 231 | jbuf = new uchar 1024 0 |
| 232 | st = new AnchorSync 0 0 0 0 0 0 |
| 233 | anchor_init_default |
| 234 | ac = argcnt |
| 235 | if le ac long 1 |
| 236 | ret print_status |
| 237 | end |
| 238 | n1 = argread ulong 1 arg1 ulong 0 ulong 256 |
| 239 | if le n1 long 0 |
| 240 | ret 1 |
| 241 | end |
| 242 | u1 = ulong n1 |
| 243 | if slice_eq part arg1 0 u1 cmd_status |
| 244 | ret print_status |
| 245 | end |
| 246 | if or slice_eq part arg1 0 u1 cmd_sync slice_eq part arg1 0 u1 cmd_json |
| 247 | anchor_sync_keys part arg1 0 0 part arg2 0 0 st |
| 248 | jl = anchor_render_json st jbuf |
| 249 | ok = write_all int 1 part jbuf 0 jl |
| 250 | if not ok |
| 251 | ret 1 |
| 252 | end |
| 253 | ret 0 |
| 254 | end |
| 255 | if slice_eq part arg1 0 u1 cmd_check_rid |
| 256 | if le ac long 2 |
| 257 | ok = write_all int 2 msg_usage |
| 258 | if not ok |
| 259 | ret 2 |
| 260 | end |
| 261 | ret 2 |
| 262 | end |
| 263 | n2 = argread ulong 2 arg2 ulong 0 ulong 256 |
| 264 | if le n2 long 0 |
| 265 | ok = write_all int 1 msg_invalid_rid |
| 266 | if not ok |
| 267 | ret 1 |
| 268 | end |
| 269 | ret 1 |
| 270 | end |
| 271 | rc = validate_rid part arg2 0 ulong n2 |
| 272 | if eq rc RAD_OK |
| 273 | ok = write_all int 1 msg_valid_rid |
| 274 | if not ok |
| 275 | ret 1 |
| 276 | end |
| 277 | ret 0 |
| 278 | end |
| 279 | ok = write_all int 1 msg_invalid_rid |
| 280 | if not ok |
| 281 | ret 1 |
| 282 | end |
| 283 | ret 1 |
| 284 | end |
| 285 | if slice_eq part arg1 0 u1 cmd_check_did |
| 286 | if le ac long 2 |
| 287 | ok = write_all int 2 msg_usage |
| 288 | if not ok |
| 289 | ret 2 |
| 290 | end |
| 291 | ret 2 |
| 292 | end |
| 293 | n2 = argread ulong 2 arg2 ulong 0 ulong 256 |
| 294 | if le n2 long 0 |
| 295 | ok = write_all int 1 msg_invalid_did |
| 296 | if not ok |
| 297 | ret 1 |
| 298 | end |
| 299 | ret 1 |
| 300 | end |
| 301 | rc = validate_delegate_did part arg2 0 ulong n2 |
| 302 | if eq rc RAD_OK |
| 303 | ok = write_all int 1 msg_valid_did |
| 304 | if not ok |
| 305 | ret 1 |
| 306 | end |
| 307 | ret 0 |
| 308 | end |
| 309 | ok = write_all int 1 msg_invalid_did |
| 310 | if not ok |
| 311 | ret 1 |
| 312 | end |
| 313 | ret 1 |
| 314 | end |
| 315 | if slice_eq part arg1 0 u1 cmd_diff |
| 316 | ret run_diff_stdin |
| 317 | end |
| 318 | ok = write_all int 1 msg_usage |
| 319 | if not ok |
| 320 | ret 1 |
| 321 | end |
| 322 | ret 0 |
| 323 | end |