omega-mcp
aien-dev/omega-mcp
Brought by drakestapleton
| 1 | note Agent Q&A store. Drake (2026-10-29): "We need a StackOverflow specifically for agents, built in to every |
| 2 | note coding CLI where opt-in contribution (selected by users for their agents) is strongly promoted." |
| 3 | note One entry per file in the store directory, named by the SHA-256 of its exact bytes (64 lowercase hex), |
| 4 | note so stores can be copied and merged by any transport and a damaged copy is seen on read. |
| 5 | note Reading (search, read, consent_status) is open to every agent. Contributing (ask, answer, check) needs |
| 6 | note the line `allow AGENT` in the consent file, which the person writes; this server only ever reads it, |
| 7 | note again at every contribution, so removing the line stops sharing at once. |
| 8 | note A contribution is refused, never edited, when it holds a private key block, a home directory path |
| 9 | note or a long mixed-case run that looks like a key or token. Entry layout (text, one field per line): |
| 10 | note agent-qa 1 / kind K / agent A / [ref ID] / [title T] / [versions V] / [result R] / [evidence E] |
| 11 | note then `steps N` and `body N` blocks: N decimal bytes follow the newline, then one newline. |
| 12 | global qv ulong 16 |
| 13 | global qagent uchar 64 |
| 14 | global qcpath uchar 4096 |
| 15 | global qid uchar 64 |
| 16 | global qerr uint 1 |
| 17 | |
| 18 | const qagentn ulong 0 |
| 19 | const qcpathn ulong 1 |
| 20 | const qdirfd ulong 2 |
| 21 | const qmatch ulong 3 |
| 22 | const qshown ulong 4 |
| 23 | const qskip ulong 5 |
| 24 | const qfoff ulong 6 |
| 25 | const qflen ulong 7 |
| 26 | const qrule ulong 8 |
| 27 | const qfmax ulong 32768 |
| 28 | const qmax ulong 65536 |
| 29 | const qpage ulong 50 |
| 30 | note Flags as in maintenance/state.om (Wolfram-checked there): directory/nofollow/cloexec, read/nofollow/ |
| 31 | note cloexec/nonblock, rdwr/create/excl/nofollow/cloexec. 420 is mode 0644 (aien-calc receipt in the commit message). |
| 32 | const qodir int 573440 |
| 33 | const qord int 559104 |
| 34 | const qonew int 557250 |
| 35 | const qmode int 420 |
| 36 | |
| 37 | text qhex "0123456789abcdef" |
| 38 | text qdot "." |
| 39 | text qargstore "--store" |
| 40 | text qargagent "--agent" |
| 41 | text qargcons "--consent" |
| 42 | text qallow "allow " |
| 43 | text qmagic "agent-qa 1\n" |
| 44 | text qkind "kind " |
| 45 | text qkq "question" |
| 46 | text qka "answer" |
| 47 | text qkc "check" |
| 48 | text qlagent "agent " |
| 49 | text qlref "ref " |
| 50 | text qltitle "title " |
| 51 | text qlvers "versions " |
| 52 | text qlres "result " |
| 53 | text qlevid "evidence " |
| 54 | text qlsteps "steps " |
| 55 | text qlbody "body " |
| 56 | text qworks "works" |
| 57 | text qfails "fails" |
| 58 | text qbegin "-----BEGIN" |
| 59 | text qhome "/home/" |
| 60 | text qusers "/Users/" |
| 61 | text qroot "/root/" |
| 62 | text qtmpp ".qa-tmp-" |
| 63 | |
| 64 | text qkok "ok" |
| 65 | text qkerr "error" |
| 66 | text qkid "id" |
| 67 | text qkkind "kind" |
| 68 | text qktitle "title" |
| 69 | text qkref "ref" |
| 70 | text qktext "text" |
| 71 | text qkres "results" |
| 72 | text qkmatch "matches" |
| 73 | text qkshown "shown" |
| 74 | text qkskip "skipped" |
| 75 | text qkmore "more" |
| 76 | text qkcreated "created" |
| 77 | text qkagent "agent" |
| 78 | text qkmay "may_contribute" |
| 79 | text qkcfile "consent_file" |
| 80 | text qkhow "how_to_enable" |
| 81 | text qknone "" |
| 82 | |
| 83 | text qhow1 "Reading is open. To let this agent share questions, answers and checks, the person (not the agent) adds the line `allow " |
| 84 | text qhow2 "` to the consent file named in consent_file, and deletes it to stop. Only the entry text is shared: no chats, code or files." |
| 85 | text qhownf "Reading is open. Sharing is off because this server was started without --consent FILE. The person adds --consent with a file they own to this agent's MCP entry, then the line `allow AGENT` to that file." |
| 86 | text qeno "Sharing is off for this agent. Ask the person to turn it on (see consent_status); reading still works." |
| 87 | text qeline "Refused: a one-line field holds a control character or a line break. Nothing was saved." |
| 88 | text qetext "Refused: a text field holds a control character other than line break or tab. Nothing was saved." |
| 89 | text qekey "Refused: the text holds a private key block (-----BEGIN). Remove it. Nothing was saved." |
| 90 | text qehome "Refused: the text names a home directory (/home/, /Users/ or /root/). Write ~/... instead. Nothing was saved." |
| 91 | text qetok "Refused: the text holds a long mixed-case string that looks like a key or token. Remove or split it. Nothing was saved." |
| 92 | text qeid "Refused: an id is 64 lowercase hexadecimal characters." |
| 93 | text qemiss "No entry has this id in this store." |
| 94 | text qekindq "Refused: question must be the id of a question entry." |
| 95 | text qekinda "Refused: answer must be the id of an answer entry." |
| 96 | text qedamage "The entry file does not match its id or is not a valid entry; it was not used." |
| 97 | text qeres "Refused: result is works or fails." |
| 98 | text qeempty "Refused: every field must be nonempty." |
| 99 | text qestale "Refused: a temporary file from an interrupted write (.qa-tmp-ID) blocks this entry; remove it and retry." |
| 100 | text qeio "The store could not be written (input/output error); nothing new is visible." |
| 101 | text qedir "The store directory could not be read." |
| 102 | |
| 103 | fn qa_eq a view uchar * t view uchar * to bool |
| 104 | if ne len a len t |
| 105 | ret false |
| 106 | end |
| 107 | for i ulong len a |
| 108 | if ne get a i get t i |
| 109 | ret false |
| 110 | end |
| 111 | end |
| 112 | ret true |
| 113 | end |
| 114 | |
| 115 | fn qa_lower c uchar to uchar |
| 116 | if and ge c 65 le c 90 |
| 117 | ret add c 32 |
| 118 | end |
| 119 | ret c |
| 120 | end |
| 121 | |
| 122 | note True when w (nonempty) occurs in h, ASCII letters compared without case. |
| 123 | fn qa_has h view uchar * w view uchar * to bool |
| 124 | if gt len w len h |
| 125 | ret false |
| 126 | end |
| 127 | for i ulong add sub len h len w 1 |
| 128 | hit = true |
| 129 | for j ulong len w |
| 130 | if ne qa_lower get h add i j qa_lower get w j |
| 131 | hit = false |
| 132 | break |
| 133 | end |
| 134 | end |
| 135 | if hit |
| 136 | ret true |
| 137 | end |
| 138 | end |
| 139 | ret false |
| 140 | end |
| 141 | |
| 142 | fn qa_isid s view uchar * to bool |
| 143 | if ne len s 64 |
| 144 | ret false |
| 145 | end |
| 146 | for i ulong 64 |
| 147 | c = get s i |
| 148 | if not or and ge c 48 le c 57 and ge c 97 le c 102 |
| 149 | ret false |
| 150 | end |
| 151 | end |
| 152 | ret true |
| 153 | end |
| 154 | |
| 155 | fn qa_agentok s view uchar * to bool |
| 156 | if or eq len s 0 gt len s 64 |
| 157 | ret false |
| 158 | end |
| 159 | for i ulong len s |
| 160 | c = get s i |
| 161 | ok = or and ge c 97 le c 122 and ge c 48 le c 57 |
| 162 | if and gt i 0 or eq c 45 eq c 95 |
| 163 | ok = true |
| 164 | end |
| 165 | if not ok |
| 166 | ret false |
| 167 | end |
| 168 | end |
| 169 | ret true |
| 170 | end |
| 171 | |
| 172 | note Strict UTF-8 (no overlong forms, no surrogates, at most U+10FFFF), so the JSON writer never fails. |
| 173 | fn qa_utf8 s view uchar * to bool |
| 174 | i = ulong 0 |
| 175 | n = len s |
| 176 | while lt i n bound 16777216 |
| 177 | c = get s i |
| 178 | if lt c 128 |
| 179 | i = add i 1 |
| 180 | continue |
| 181 | end |
| 182 | k = ulong 0 |
| 183 | lo = uchar 128 |
| 184 | hi = uchar 191 |
| 185 | if and ge c 194 le c 223 |
| 186 | k = 1 |
| 187 | end |
| 188 | if and ge c 224 le c 239 |
| 189 | k = 2 |
| 190 | if eq c 224 |
| 191 | lo = 160 |
| 192 | end |
| 193 | if eq c 237 |
| 194 | hi = 159 |
| 195 | end |
| 196 | end |
| 197 | if and ge c 240 le c 244 |
| 198 | k = 3 |
| 199 | if eq c 240 |
| 200 | lo = 144 |
| 201 | end |
| 202 | if eq c 244 |
| 203 | hi = 143 |
| 204 | end |
| 205 | end |
| 206 | if eq k 0 |
| 207 | ret false |
| 208 | end |
| 209 | if gt add i k sub n 1 |
| 210 | ret false |
| 211 | end |
| 212 | for j ulong k |
| 213 | d = get s add add i j 1 |
| 214 | if eq j 0 |
| 215 | if or lt d lo gt d hi |
| 216 | ret false |
| 217 | end |
| 218 | else |
| 219 | if or lt d 128 gt d 191 |
| 220 | ret false |
| 221 | end |
| 222 | end |
| 223 | end |
| 224 | i = add add i k 1 |
| 225 | end |
| 226 | ret true |
| 227 | end |
| 228 | |
| 229 | note 0 when the field may be shared; else the refusal: 1 key block, 2 home path, 3 token-like run. |
| 230 | note A token-like run is 24 or more of A-Z a-z 0-9 _ - holding an upper-case letter, a lower-case letter |
| 231 | note and a digit (lowercase hex digests such as sha256 values pass). |
| 232 | fn qa_private s view uchar * to ulong |
| 233 | if qa_has s qbegin |
| 234 | ret 1 |
| 235 | end |
| 236 | if or qa_has s qhome or qa_has s qusers qa_has s qroot |
| 237 | ret 2 |
| 238 | end |
| 239 | rn = ulong 0 |
| 240 | sn = ulong 0 |
| 241 | for i ulong len s |
| 242 | c = get s i |
| 243 | cl = ulong 0 |
| 244 | if and ge c 65 le c 90 |
| 245 | cl = 1 |
| 246 | end |
| 247 | if and ge c 97 le c 122 |
| 248 | cl = 2 |
| 249 | end |
| 250 | if and ge c 48 le c 57 |
| 251 | cl = 4 |
| 252 | end |
| 253 | if or eq c 95 eq c 45 |
| 254 | cl = 8 |
| 255 | end |
| 256 | if eq cl 0 |
| 257 | rn = 0 |
| 258 | sn = 0 |
| 259 | else |
| 260 | rn = add rn 1 |
| 261 | sn = or sn cl |
| 262 | if and ge rn 24 eq and sn 7 7 |
| 263 | ret 3 |
| 264 | end |
| 265 | end |
| 266 | end |
| 267 | ret 0 |
| 268 | end |
| 269 | |
| 270 | note 0 ok, 1 when a one-line field (multi false) or a block (multi true) holds a forbidden control byte. |
| 271 | fn qa_ctl s view uchar * multi bool to bool |
| 272 | for i ulong len s |
| 273 | c = get s i |
| 274 | if or lt c 32 eq c 127 |
| 275 | if not and multi or eq c 10 eq c 9 |
| 276 | ret false |
| 277 | end |
| 278 | end |
| 279 | end |
| 280 | ret true |
| 281 | end |
| 282 | |
| 283 | fn qa_wt ob edit uchar * s view uchar * to void |
| 284 | js_wsbuf ob s 0 len s |
| 285 | end |
| 286 | |
| 287 | fn qa_fail ob edit uchar * msg view uchar * to bool |
| 288 | js_wobj ob |
| 289 | qa_wt ob qkok |
| 290 | js_wbool ob false |
| 291 | qa_wt ob qkerr |
| 292 | qa_wt ob msg |
| 293 | js_wobje ob |
| 294 | put qerr 0 1 |
| 295 | ret false |
| 296 | end |
| 297 | |
| 298 | note Appends s at ent[n]; returns the new length (the caller sized ent above every reachable total). |
| 299 | fn qa_add ent edit uchar * n ulong s view uchar * to ulong |
| 300 | for i ulong len s |
| 301 | put ent add n i get s i |
| 302 | end |
| 303 | ret add n len s |
| 304 | end |
| 305 | |
| 306 | fn qa_addnum ent edit uchar * n ulong v ulong to ulong |
| 307 | d = new uchar 24 0 |
| 308 | k = ulong 0 |
| 309 | x = v |
| 310 | while true bound 24 |
| 311 | put d k add 48 uchar rem x 10 |
| 312 | k = add k 1 |
| 313 | x = div x 10 |
| 314 | if eq x 0 |
| 315 | break |
| 316 | end |
| 317 | end |
| 318 | for i ulong k |
| 319 | put ent add n i get d sub sub k i 1 |
| 320 | end |
| 321 | ret add n k |
| 322 | end |
| 323 | |
| 324 | fn qa_line ent edit uchar 65536 n ulong kw view uchar * vl view uchar * to ulong |
| 325 | m = qa_add ent n kw |
| 326 | m = qa_add ent m vl |
| 327 | put ent m 10 |
| 328 | ret add m 1 |
| 329 | end |
| 330 | |
| 331 | fn qa_block ent edit uchar 65536 n ulong kw view uchar * vl view uchar * to ulong |
| 332 | m = qa_add ent n kw |
| 333 | m = qa_addnum ent m len vl |
| 334 | put ent m 10 |
| 335 | m = qa_add ent add m 1 vl |
| 336 | put ent m 10 |
| 337 | ret add m 1 |
| 338 | end |
| 339 | |
| 340 | fn qa_hexid d view uchar 32 to void |
| 341 | for i ulong 32 |
| 342 | c = get d i |
| 343 | put qid mul i 2 get qhex ulong shr c 4 |
| 344 | put qid add mul i 2 1 get qhex ulong and c 15 |
| 345 | end |
| 346 | end |
| 347 | |
| 348 | fn qa_idis name view uchar * to bool |
| 349 | if ne len name 64 |
| 350 | ret false |
| 351 | end |
| 352 | for i ulong 64 |
| 353 | if ne get qid i get name i |
| 354 | ret false |
| 355 | end |
| 356 | end |
| 357 | ret true |
| 358 | end |
| 359 | |
| 360 | fn qa_idof ent view uchar * to void |
| 361 | d = new uchar 32 0 |
| 362 | sha256_hash ent d |
| 363 | qa_hexid d |
| 364 | end |
| 365 | |
| 366 | note Reads file name (relative to the store) into fb; returns its size, -1 missing, -2 unreadable, |
| 367 | note -3 larger than qmax. |
| 368 | fn qa_load name view uchar * fb edit uchar 65536 to long |
| 369 | x = new uchar 1 0 |
| 370 | r = fopen name 0 len name qord 0 |
| 371 | if lt r 0 |
| 372 | if eq r -2 |
| 373 | ret -1 |
| 374 | end |
| 375 | ret -2 |
| 376 | end |
| 377 | fd = int r |
| 378 | n = ulong 0 |
| 379 | res = long 0 |
| 380 | while lt n qmax bound 65537 |
| 381 | k = read fd fb n sub qmax n |
| 382 | if eq k -4 |
| 383 | continue |
| 384 | end |
| 385 | if lt k 0 |
| 386 | res = -2 |
| 387 | break |
| 388 | end |
| 389 | if eq k 0 |
| 390 | break |
| 391 | end |
| 392 | n = add n ulong k |
| 393 | end |
| 394 | if and eq res 0 eq n qmax |
| 395 | k = read fd x 0 1 |
| 396 | if ne k 0 |
| 397 | res = -3 |
| 398 | end |
| 399 | end |
| 400 | z = fclose fd |
| 401 | if lt res 0 |
| 402 | ret res |
| 403 | end |
| 404 | ret long n |
| 405 | end |
| 406 | |
| 407 | note Finds header field key (e.g. `title `) in entry e, up to the first steps or body block; sets |
| 408 | note qv[qfoff], qv[qflen] and returns true when present. |
| 409 | fn qa_field e view uchar * key view uchar * to bool |
| 410 | p = len qmagic |
| 411 | while lt p len e bound 65536 |
| 412 | q = p |
| 413 | while lt q len e bound 65536 |
| 414 | if eq get e q 10 |
| 415 | break |
| 416 | end |
| 417 | q = add q 1 |
| 418 | end |
| 419 | ln = sub q p |
| 420 | if or and ge ln len qlsteps qa_eq part e p len qlsteps qlsteps and ge ln len qlbody qa_eq part e p len qlbody qlbody |
| 421 | ret false |
| 422 | end |
| 423 | if ge ln len key |
| 424 | if qa_eq part e p len key key |
| 425 | put qv qfoff add p len key |
| 426 | put qv qflen sub ln len key |
| 427 | ret true |
| 428 | end |
| 429 | end |
| 430 | p = add q 1 |
| 431 | end |
| 432 | ret false |
| 433 | end |
| 434 | |
| 435 | note An entry is usable when its bytes hash to its name, start with the magic line, carry a kind and |
| 436 | note are valid UTF-8. |
| 437 | fn qa_valid name view uchar * e view uchar * to bool |
| 438 | if lt len e len qmagic |
| 439 | ret false |
| 440 | end |
| 441 | if not qa_eq part e 0 len qmagic qmagic |
| 442 | ret false |
| 443 | end |
| 444 | if not qa_utf8 e |
| 445 | ret false |
| 446 | end |
| 447 | qa_idof e |
| 448 | if not qa_idis name |
| 449 | ret false |
| 450 | end |
| 451 | ret qa_field e qkind |
| 452 | end |
| 453 | |
| 454 | fn qa_kindis e view uchar * k view uchar * to bool |
| 455 | if not qa_field e qkind |
| 456 | ret false |
| 457 | end |
| 458 | ret qa_eq part e get qv qfoff get qv qflen k |
| 459 | end |
| 460 | |
| 461 | note True when line s..e of b is exactly `allow AGENT` for this server's agent. |
| 462 | fn qa_isallow b view uchar * to bool |
| 463 | if ne len b add len qallow get qv qagentn |
| 464 | ret false |
| 465 | end |
| 466 | if not qa_eq part b 0 len qallow qallow |
| 467 | ret false |
| 468 | end |
| 469 | for i ulong get qv qagentn |
| 470 | if ne get b add len qallow i get qagent i |
| 471 | ret false |
| 472 | end |
| 473 | end |
| 474 | ret true |
| 475 | end |
| 476 | |
| 477 | fn qa_allowed b view uchar * to bool |
| 478 | s = ulong 0 |
| 479 | while lt s len b bound 65537 |
| 480 | e = s |
| 481 | while lt e len b bound 65537 |
| 482 | if eq get b e 10 |
| 483 | break |
| 484 | end |
| 485 | e = add e 1 |
| 486 | end |
| 487 | if qa_isallow part b s sub e s |
| 488 | ret true |
| 489 | end |
| 490 | s = add e 1 |
| 491 | end |
| 492 | ret false |
| 493 | end |
| 494 | |
| 495 | note At most 65536 bytes. One more byte is then tried. If it is there, the tail after the last newline was cut |
| 496 | note and is not a line: `allow AGENT` split across that boundary does not count. A short file still ends at EOF, |
| 497 | note so its last line counts, including one that does not end in a newline. |
| 498 | fn qa_consented to bool |
| 499 | if eq get qv qcpathn 0 |
| 500 | ret false |
| 501 | end |
| 502 | p = new uchar 4096 0 |
| 503 | b = new uchar 65536 0 |
| 504 | x = new uchar 1 0 |
| 505 | for i ulong get qv qcpathn |
| 506 | put p i get qcpath i |
| 507 | end |
| 508 | r = fopen p 0 get qv qcpathn qord 0 |
| 509 | if lt r 0 |
| 510 | ret false |
| 511 | end |
| 512 | n = ulong 0 |
| 513 | while lt n 65536 bound 65537 |
| 514 | k = read int r b n sub 65536 n |
| 515 | if eq k -4 |
| 516 | continue |
| 517 | end |
| 518 | if le k 0 |
| 519 | break |
| 520 | end |
| 521 | n = add n ulong k |
| 522 | end |
| 523 | if eq n 65536 |
| 524 | g = read int r x 0 1 |
| 525 | t = ulong 0 |
| 526 | while lt t 63 bound 63 |
| 527 | if ne g -4 |
| 528 | break |
| 529 | end |
| 530 | g = read int r x 0 1 |
| 531 | t = add t 1 |
| 532 | end |
| 533 | if gt g 0 |
| 534 | cut = ulong 0 |
| 535 | j = n |
| 536 | while gt j 0 bound 65537 |
| 537 | j = sub j 1 |
| 538 | if eq get b j 10 |
| 539 | cut = add j 1 |
| 540 | break |
| 541 | end |
| 542 | end |
| 543 | n = cut |
| 544 | end |
| 545 | if lt g 0 |
| 546 | z = fclose int r |
| 547 | ret false |
| 548 | end |
| 549 | end |
| 550 | z = fclose int r |
| 551 | ret qa_allowed part b 0 n |
| 552 | end |
| 553 | |
| 554 | fn qa_consent ob edit uchar * to bool |
| 555 | h = new uchar 512 0 |
| 556 | a = new uchar 64 0 |
| 557 | for i ulong get qv qagentn |
| 558 | put a i get qagent i |
| 559 | end |
| 560 | p = new uchar 4096 0 |
| 561 | for i ulong get qv qcpathn |
| 562 | put p i get qcpath i |
| 563 | end |
| 564 | js_wobj ob |
| 565 | qa_wt ob qkok |
| 566 | js_wbool ob true |
| 567 | qa_wt ob qkagent |
| 568 | js_wsbuf ob a 0 get qv qagentn |
| 569 | qa_wt ob qkmay |
| 570 | js_wbool ob qa_consented |
| 571 | qa_wt ob qkcfile |
| 572 | if eq get qv qcpathn 0 |
| 573 | js_wnull ob |
| 574 | else |
| 575 | js_wsbuf ob p 0 get qv qcpathn |
| 576 | end |
| 577 | qa_wt ob qkhow |
| 578 | if eq get qv qcpathn 0 |
| 579 | qa_wt ob qhownf |
| 580 | else |
| 581 | m = qa_add h 0 qhow1 |
| 582 | m = qa_add h m part a 0 get qv qagentn |
| 583 | m = qa_add h m qhow2 |
| 584 | js_wsbuf ob h 0 m |
| 585 | end |
| 586 | js_wobje ob |
| 587 | ret true |
| 588 | end |
| 589 | |
| 590 | fn qa_read ob edit uchar * id view uchar * fb edit uchar 65536 to bool |
| 591 | if not qa_isid id |
| 592 | ret qa_fail ob qeid |
| 593 | end |
| 594 | r = qa_load id fb |
| 595 | if eq r -1 |
| 596 | ret qa_fail ob qemiss |
| 597 | end |
| 598 | if lt r 0 |
| 599 | ret qa_fail ob qedamage |
| 600 | end |
| 601 | if not qa_valid id part fb 0 ulong r |
| 602 | ret qa_fail ob qedamage |
| 603 | end |
| 604 | js_wobj ob |
| 605 | qa_wt ob qkok |
| 606 | js_wbool ob true |
| 607 | qa_wt ob qkid |
| 608 | qa_wt ob id |
| 609 | qa_wt ob qktext |
| 610 | js_wsbuf ob fb 0 ulong r |
| 611 | js_wobje ob |
| 612 | ret true |
| 613 | end |
| 614 | |
| 615 | note True when every space-separated word of q occurs in e (an empty query matches every entry). |
| 616 | fn qa_words e view uchar * q view uchar * to bool |
| 617 | p = ulong 0 |
| 618 | while lt p len q bound 65536 |
| 619 | if eq get q p 32 |
| 620 | p = add p 1 |
| 621 | continue |
| 622 | end |
| 623 | s = p |
| 624 | while lt p len q bound 65536 |
| 625 | if eq get q p 32 |
| 626 | break |
| 627 | end |
| 628 | p = add p 1 |
| 629 | end |
| 630 | if not qa_has e part q s sub p s |
| 631 | ret false |
| 632 | end |
| 633 | end |
| 634 | ret true |
| 635 | end |
| 636 | |
| 637 | fn qa_hit ob edit uchar * name view uchar * e view uchar * to void |
| 638 | put qv qmatch add get qv qmatch 1 |
| 639 | if ge get qv qshown qpage |
| 640 | ret |
| 641 | end |
| 642 | put qv qshown add get qv qshown 1 |
| 643 | js_wobj ob |
| 644 | qa_wt ob qkid |
| 645 | qa_wt ob name |
| 646 | qa_wt ob qkkind |
| 647 | if qa_field e qkind |
| 648 | js_wsbuf ob e get qv qfoff get qv qflen |
| 649 | else |
| 650 | qa_wt ob qknone |
| 651 | end |
| 652 | qa_wt ob qktitle |
| 653 | if qa_field e qltitle |
| 654 | js_wsbuf ob e get qv qfoff get qv qflen |
| 655 | else |
| 656 | qa_wt ob qknone |
| 657 | end |
| 658 | qa_wt ob qkref |
| 659 | if qa_field e qlref |
| 660 | js_wsbuf ob e get qv qfoff get qv qflen |
| 661 | else |
| 662 | qa_wt ob qknone |
| 663 | end |
| 664 | js_wobje ob |
| 665 | end |
| 666 | |
| 667 | note getdents64 record: ino u64, offset i64, reclen u16 at 16, type at 18, NUL-terminated name at 19. |
| 668 | note Names that are not 64 lowercase hex (temporary files, anything else) are not entries. |
| 669 | fn qa_one ob edit uchar * q view uchar * name view uchar 64 to void |
| 670 | fb = new uchar 65536 0 |
| 671 | m = qa_load name fb |
| 672 | if lt m 0 |
| 673 | put qv qskip add get qv qskip 1 |
| 674 | ret |
| 675 | end |
| 676 | if not qa_valid name part fb 0 ulong m |
| 677 | put qv qskip add get qv qskip 1 |
| 678 | ret |
| 679 | end |
| 680 | if qa_words part fb 0 ulong m q |
| 681 | qa_hit ob name part fb 0 ulong m |
| 682 | end |
| 683 | end |
| 684 | |
| 685 | note Copies the NUL-terminated name of the record at p (length rl) into name (first 64 bytes); returns its length. |
| 686 | fn qa_dname buf view uchar 32768 p ulong rl ulong name edit uchar 64 to ulong |
| 687 | n = ulong 0 |
| 688 | for i ulong sub rl 19 |
| 689 | c = get buf add add p 19 i |
| 690 | if eq c 0 |
| 691 | break |
| 692 | end |
| 693 | if lt n 64 |
| 694 | put name n c |
| 695 | end |
| 696 | n = add n 1 |
| 697 | end |
| 698 | ret n |
| 699 | end |
| 700 | |
| 701 | note Walks one getdents64 batch of k bytes; false when a record is malformed. |
| 702 | fn qa_batch ob edit uchar * q view uchar * buf view uchar 32768 k ulong to bool |
| 703 | name = new uchar 64 0 |
| 704 | p = ulong 0 |
| 705 | while lt add p 19 k bound 32768 |
| 706 | rl = ulong get buf add p 17 |
| 707 | rl = add shl rl 8 ulong get buf add p 16 |
| 708 | if or lt rl 20 gt add p rl k |
| 709 | ret false |
| 710 | end |
| 711 | n = qa_dname buf p rl name |
| 712 | if eq n 64 |
| 713 | if qa_isid name |
| 714 | qa_one ob q name |
| 715 | end |
| 716 | end |
| 717 | p = add p rl |
| 718 | end |
| 719 | ret true |
| 720 | end |
| 721 | |
| 722 | note Names that are not 64 lowercase hex (temporary files, anything else) are not entries. |
| 723 | fn qa_search ob edit uchar * q view uchar * fb edit uchar 65536 to bool |
| 724 | buf = new uchar 32768 0 |
| 725 | r = fopen qdot 0 1 qodir 0 |
| 726 | if lt r 0 |
| 727 | ret qa_fail ob qedir |
| 728 | end |
| 729 | put qv qmatch 0 |
| 730 | put qv qshown 0 |
| 731 | put qv qskip 0 |
| 732 | js_wobj ob |
| 733 | qa_wt ob qkok |
| 734 | js_wbool ob true |
| 735 | qa_wt ob qkres |
| 736 | js_warr ob |
| 737 | ok = true |
| 738 | while true bound 16777216 |
| 739 | k = getdnts int r buf 0 32768 |
| 740 | if eq k 0 |
| 741 | break |
| 742 | end |
| 743 | if lt k 0 |
| 744 | ok = false |
| 745 | break |
| 746 | end |
| 747 | if not qa_batch ob q buf ulong k |
| 748 | ok = false |
| 749 | break |
| 750 | end |
| 751 | end |
| 752 | z = fclose int r |
| 753 | js_warre ob |
| 754 | qa_wt ob qkmatch |
| 755 | js_wulong ob get qv qmatch |
| 756 | qa_wt ob qkshown |
| 757 | js_wulong ob get qv qshown |
| 758 | qa_wt ob qkmore |
| 759 | js_wbool ob gt get qv qmatch get qv qshown |
| 760 | qa_wt ob qkskip |
| 761 | js_wulong ob get qv qskip |
| 762 | js_wobje ob |
| 763 | if not ok |
| 764 | put qerr 0 1 |
| 765 | end |
| 766 | ret ok |
| 767 | end |
| 768 | |
| 769 | note Creates tmp exclusively and writes ent to it with fsync: 0 done, 1 tmp already exists, 2 I/O error |
| 770 | note (tmp removed). |
| 771 | fn qa_writetmp tmp view uchar 72 ent view uchar * to ulong |
| 772 | r = fopen tmp 0 72 qonew qmode |
| 773 | if lt r 0 |
| 774 | if eq r -17 |
| 775 | ret 1 |
| 776 | end |
| 777 | ret 2 |
| 778 | end |
| 779 | p = ulong 0 |
| 780 | ok = true |
| 781 | while lt p len ent bound 65537 |
| 782 | k = write int r ent p sub len ent p |
| 783 | if eq k -4 |
| 784 | continue |
| 785 | end |
| 786 | if le k 0 |
| 787 | ok = false |
| 788 | break |
| 789 | end |
| 790 | p = add p ulong k |
| 791 | end |
| 792 | if ok |
| 793 | k = fsync int r |
| 794 | if ne k 0 |
| 795 | ok = false |
| 796 | end |
| 797 | end |
| 798 | k = fclose int r |
| 799 | if ne k 0 |
| 800 | ok = false |
| 801 | end |
| 802 | if not ok |
| 803 | z = unlink tmp 0 72 |
| 804 | ret 2 |
| 805 | end |
| 806 | ret 0 |
| 807 | end |
| 808 | |
| 809 | note Renames tmp to id (a packed list of two records) and syncs the store directory. |
| 810 | fn qa_commit tmp view uchar 72 id view uchar 64 to bool |
| 811 | pk = new uchar 160 0 |
| 812 | put pk 0 2 |
| 813 | put pk 2 72 |
| 814 | for i ulong 72 |
| 815 | put pk add 4 i get tmp i |
| 816 | end |
| 817 | put pk 76 64 |
| 818 | for i ulong 64 |
| 819 | put pk add 78 i get id i |
| 820 | end |
| 821 | k = renames pk 0 142 0 |
| 822 | if ne k 0 |
| 823 | z = unlink tmp 0 72 |
| 824 | ret false |
| 825 | end |
| 826 | k = fsync int get qv qdirfd |
| 827 | ret eq k 0 |
| 828 | end |
| 829 | |
| 830 | note Writes ent as the file named by its SHA-256: exclusive temporary file, write, fsync, rename, directory |
| 831 | note fsync. An entry that already exists is not rewritten (created false). |
| 832 | fn qa_store ob edit uchar * ent view uchar * kind view uchar * to bool |
| 833 | id = new uchar 64 0 |
| 834 | tmp = new uchar 72 0 |
| 835 | qa_idof ent |
| 836 | for i ulong 64 |
| 837 | put id i get qid i |
| 838 | put tmp add 8 i get qid i |
| 839 | end |
| 840 | for i ulong 8 |
| 841 | put tmp i get qtmpp i |
| 842 | end |
| 843 | created = true |
| 844 | r = fopen id 0 64 qord 0 |
| 845 | if ge r 0 |
| 846 | z = fclose int r |
| 847 | created = false |
| 848 | else |
| 849 | w = qa_writetmp tmp ent |
| 850 | if eq w 1 |
| 851 | ret qa_fail ob qestale |
| 852 | end |
| 853 | if ne w 0 |
| 854 | ret qa_fail ob qeio |
| 855 | end |
| 856 | if not qa_commit tmp id |
| 857 | ret qa_fail ob qeio |
| 858 | end |
| 859 | end |
| 860 | js_wobj ob |
| 861 | qa_wt ob qkok |
| 862 | js_wbool ob true |
| 863 | qa_wt ob qkid |
| 864 | qa_wt ob id |
| 865 | qa_wt ob qkkind |
| 866 | qa_wt ob kind |
| 867 | qa_wt ob qkcreated |
| 868 | js_wbool ob created |
| 869 | js_wobje ob |
| 870 | ret true |
| 871 | end |
| 872 | |
| 873 | fn qa_share ob edit uchar * s view uchar * multi bool to bool |
| 874 | if eq len s 0 |
| 875 | ret qa_fail ob qeempty |
| 876 | end |
| 877 | if not qa_ctl s multi |
| 878 | if multi |
| 879 | ret qa_fail ob qetext |
| 880 | end |
| 881 | ret qa_fail ob qeline |
| 882 | end |
| 883 | r = qa_private s |
| 884 | if eq r 1 |
| 885 | ret qa_fail ob qekey |
| 886 | end |
| 887 | if eq r 2 |
| 888 | ret qa_fail ob qehome |
| 889 | end |
| 890 | if eq r 3 |
| 891 | ret qa_fail ob qetok |
| 892 | end |
| 893 | ret true |
| 894 | end |
| 895 | |
| 896 | note sel 0: id must name a question; sel 1: id must name an answer. |
| 897 | fn qa_refkind ob edit uchar * id view uchar * sel ulong fb edit uchar 65536 to bool |
| 898 | if not qa_isid id |
| 899 | ret qa_fail ob qeid |
| 900 | end |
| 901 | r = qa_load id fb |
| 902 | if eq r -1 |
| 903 | ret qa_fail ob qemiss |
| 904 | end |
| 905 | if lt r 0 |
| 906 | ret qa_fail ob qedamage |
| 907 | end |
| 908 | if not qa_valid id part fb 0 ulong r |
| 909 | ret qa_fail ob qedamage |
| 910 | end |
| 911 | if eq sel 0 |
| 912 | if not qa_kindis part fb 0 ulong r qkq |
| 913 | ret qa_fail ob qekindq |
| 914 | end |
| 915 | else |
| 916 | if not qa_kindis part fb 0 ulong r qka |
| 917 | ret qa_fail ob qekinda |
| 918 | end |
| 919 | end |
| 920 | ret true |
| 921 | end |
| 922 | |
| 923 | fn qa_head ent edit uchar 65536 kind view uchar * to ulong |
| 924 | a = new uchar 64 0 |
| 925 | for i ulong get qv qagentn |
| 926 | put a i get qagent i |
| 927 | end |
| 928 | n = qa_add ent 0 qmagic |
| 929 | n = qa_line ent n qkind kind |
| 930 | n = qa_line ent n qlagent part a 0 get qv qagentn |
| 931 | ret n |
| 932 | end |
| 933 | |
| 934 | note Field slots in fv (qfmax bytes each, lengths in fl): 0 words, 1 id, 2 title, 3 versions, 4 body, |
| 935 | note 5 question, 6 answer, 7 steps, 8 evidence, 9 result. |
| 936 | fn qa_ask ob edit uchar * fv view uchar 327680 fl view ulong 10 ent edit uchar 65536 to bool |
| 937 | if not qa_consented |
| 938 | ret qa_fail ob qeno |
| 939 | end |
| 940 | if not qa_share ob part fv mul 2 qfmax get fl 2 false |
| 941 | ret false |
| 942 | end |
| 943 | if not qa_share ob part fv mul 3 qfmax get fl 3 false |
| 944 | ret false |
| 945 | end |
| 946 | if not qa_share ob part fv mul 4 qfmax get fl 4 true |
| 947 | ret false |
| 948 | end |
| 949 | n = qa_head ent qkq |
| 950 | n = qa_line ent n qltitle part fv mul 2 qfmax get fl 2 |
| 951 | n = qa_line ent n qlvers part fv mul 3 qfmax get fl 3 |
| 952 | n = qa_block ent n qlbody part fv mul 4 qfmax get fl 4 |
| 953 | ret qa_store ob part ent 0 n qkq |
| 954 | end |
| 955 | |
| 956 | fn qa_answer ob edit uchar * fv view uchar 327680 fl view ulong 10 ent edit uchar 65536 fb edit uchar 65536 to bool |
| 957 | if not qa_consented |
| 958 | ret qa_fail ob qeno |
| 959 | end |
| 960 | if not qa_refkind ob part fv mul 5 qfmax get fl 5 0 fb |
| 961 | ret false |
| 962 | end |
| 963 | if not qa_share ob part fv mul 3 qfmax get fl 3 false |
| 964 | ret false |
| 965 | end |
| 966 | if not qa_share ob part fv mul 8 qfmax get fl 8 false |
| 967 | ret false |
| 968 | end |
| 969 | if not qa_share ob part fv mul 7 qfmax get fl 7 true |
| 970 | ret false |
| 971 | end |
| 972 | if not qa_share ob part fv mul 4 qfmax get fl 4 true |
| 973 | ret false |
| 974 | end |
| 975 | n = qa_head ent qka |
| 976 | n = qa_line ent n qlref part fv mul 5 qfmax get fl 5 |
| 977 | n = qa_line ent n qlvers part fv mul 3 qfmax get fl 3 |
| 978 | n = qa_line ent n qlevid part fv mul 8 qfmax get fl 8 |
| 979 | n = qa_block ent n qlsteps part fv mul 7 qfmax get fl 7 |
| 980 | n = qa_block ent n qlbody part fv mul 4 qfmax get fl 4 |
| 981 | ret qa_store ob part ent 0 n qka |
| 982 | end |
| 983 | |
| 984 | fn qa_check ob edit uchar * fv view uchar 327680 fl view ulong 10 ent edit uchar 65536 fb edit uchar 65536 to bool |
| 985 | if not qa_consented |
| 986 | ret qa_fail ob qeno |
| 987 | end |
| 988 | if not or qa_eq part fv mul 9 qfmax get fl 9 qworks qa_eq part fv mul 9 qfmax get fl 9 qfails |
| 989 | ret qa_fail ob qeres |
| 990 | end |
| 991 | if not qa_refkind ob part fv mul 6 qfmax get fl 6 1 fb |
| 992 | ret false |
| 993 | end |
| 994 | if not qa_share ob part fv mul 8 qfmax get fl 8 false |
| 995 | ret false |
| 996 | end |
| 997 | if not qa_share ob part fv mul 4 qfmax get fl 4 true |
| 998 | ret false |
| 999 | end |
| 1000 | n = qa_head ent qkc |
| 1001 | n = qa_line ent n qlref part fv mul 6 qfmax get fl 6 |
| 1002 | n = qa_line ent n qlres part fv mul 9 qfmax get fl 9 |
| 1003 | n = qa_line ent n qlevid part fv mul 8 qfmax get fl 8 |
| 1004 | n = qa_block ent n qlbody part fv mul 4 qfmax get fl 4 |
| 1005 | ret qa_store ob part ent 0 n qkc |
| 1006 | end |
| 1007 | |
| 1008 | note Startup: --store DIR (absolute, an existing directory, becomes the working directory), --agent NAME |
| 1009 | note (lowercase letters, digits, - and _, at most 64), optional --consent FILE (absolute). Exit 90 on a |
| 1010 | note bad command line, 91 when the store cannot be opened. |
| 1011 | fn qa_boot to int |
| 1012 | put qerr 0 0 |
| 1013 | for i ulong 16 |
| 1014 | put qv i 0 |
| 1015 | end |
| 1016 | a = new uchar 4096 0 |
| 1017 | v = new uchar 4096 0 |
| 1018 | ns = ulong 0 |
| 1019 | i = ulong 1 |
| 1020 | while lt i ulong argcnt bound 8 |
| 1021 | r = argread i a 0 4096 |
| 1022 | if lt r 0 |
| 1023 | ret 90 |
| 1024 | end |
| 1025 | n = ulong r |
| 1026 | i = add i 1 |
| 1027 | if ge i ulong argcnt |
| 1028 | ret 90 |
| 1029 | end |
| 1030 | r = argread i v 0 4096 |
| 1031 | if le r 0 |
| 1032 | ret 90 |
| 1033 | end |
| 1034 | m = ulong r |
| 1035 | i = add i 1 |
| 1036 | if qa_eq part a 0 n qargstore |
| 1037 | if or ne ns 0 ne get v 0 47 |
| 1038 | ret 90 |
| 1039 | end |
| 1040 | r = fopen v 0 m qodir 0 |
| 1041 | if lt r 0 |
| 1042 | ret 91 |
| 1043 | end |
| 1044 | put qv qdirfd ulong r |
| 1045 | k = fchdir int r |
| 1046 | if ne k 0 |
| 1047 | ret 91 |
| 1048 | end |
| 1049 | ns = m |
| 1050 | else |
| 1051 | if qa_eq part a 0 n qargagent |
| 1052 | if or ne get qv qagentn 0 not qa_agentok part v 0 m |
| 1053 | ret 90 |
| 1054 | end |
| 1055 | for j ulong m |
| 1056 | put qagent j get v j |
| 1057 | end |
| 1058 | put qv qagentn m |
| 1059 | else |
| 1060 | if qa_eq part a 0 n qargcons |
| 1061 | if or ne get qv qcpathn 0 ne get v 0 47 |
| 1062 | ret 90 |
| 1063 | end |
| 1064 | for j ulong m |
| 1065 | put qcpath j get v j |
| 1066 | end |
| 1067 | put qv qcpathn m |
| 1068 | else |
| 1069 | ret 90 |
| 1070 | end |
| 1071 | end |
| 1072 | end |
| 1073 | end |
| 1074 | if or eq ns 0 eq get qv qagentn 0 |
| 1075 | ret 90 |
| 1076 | end |
| 1077 | ret 0 |
| 1078 | end |
| 1079 | |
| 1080 | note Tool numbers: 0 search, 1 read, 2 consent_status, 3 ask, 4 answer, 5 check. |
| 1081 | fn qa_call op ulong fv view uchar 327680 fl view ulong 10 ob edit uchar * to bool |
| 1082 | put qerr 0 0 |
| 1083 | ent = new uchar 65536 0 |
| 1084 | fb = new uchar 65536 0 |
| 1085 | if eq op 0 |
| 1086 | ret qa_search ob part fv 0 get fl 0 fb |
| 1087 | end |
| 1088 | if eq op 1 |
| 1089 | ret qa_read ob part fv qfmax get fl 1 fb |
| 1090 | end |
| 1091 | if eq op 2 |
| 1092 | ret qa_consent ob |
| 1093 | end |
| 1094 | if eq op 3 |
| 1095 | ret qa_ask ob fv fl ent |
| 1096 | end |
| 1097 | if eq op 4 |
| 1098 | ret qa_answer ob fv fl ent fb |
| 1099 | end |
| 1100 | ret qa_check ob fv fl ent fb |
| 1101 | end |