hezarfen_backend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_backend.git
0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits
| 1 | //! Wire format for the AI bridge ("hab/2" — hezarfen ai bridge). |
| 2 | //! |
| 3 | //! # Shape |
| 4 | //! |
| 5 | //! The backend is the QUIC **server**: AI services dial in, so a service can |
| 6 | //! live behind NAT, restart freely, and scale by simply opening a second |
| 7 | //! connection. Every service holds exactly one QUIC connection. |
| 8 | //! |
| 9 | //! On that connection: |
| 10 | //! |
| 11 | //! * The **control stream** is the first *client-initiated* bidirectional |
| 12 | //! stream. The service writes one [`Hello`]; the backend answers one |
| 13 | //! [`Greeting`] and then leaves the stream open forever. Its death is the |
| 14 | //! deregistration signal — no heartbeat frame needed. |
| 15 | //! * Each request is one *server-initiated* bidirectional stream: the backend |
| 16 | //! opens it, writes one [`Request`], finishes its send side, and reads one |
| 17 | //! [`Response`]. The stream is then closed. |
| 18 | //! |
| 19 | //! * Each **api read** goes the other way: the service opens a *client-initiated* |
| 20 | //! bidirectional stream (any after the control one), writes one |
| 21 | //! [`ApiRequest`], finishes its send side, and reads one [`ApiResponse`]. |
| 22 | //! That mirrors the REST API — the backend dispatches the path internally — |
| 23 | //! so a service asks for school data instead of being handed it. |
| 24 | //! |
| 25 | //! * Each **blob read** goes the same way as an api read, on its own |
| 26 | //! client-initiated bidirectional stream: the service writes one |
| 27 | //! [`BlobRequest`], finishes its send side, and reads one [`BlobResponse`] |
| 28 | //! header frame. On [`BlobResponse::Ok`] exactly `size` **raw** bytes follow |
| 29 | //! the header — not a frame, so [`AI_MAX_FRAME_BYTES`] does not bound them — |
| 30 | //! and then the stream is finished. That is how a service gets a course |
| 31 | //! note's file bytes, which no JSON frame could carry. |
| 32 | //! |
| 33 | //! * Each **capability call** also goes the service's way, on its own |
| 34 | //! client-initiated bidirectional stream: the service writes one |
| 35 | //! [`CapabilityRequest`], finishes its send side, and reads one |
| 36 | //! [`CapabilityResponse`]. This is the reverse of the [`Request`]/ |
| 37 | //! [`Response`] pair above, and it exists for the same reason the api read |
| 38 | //! does: capabilities live on both sides. The backend *dispatches* |
| 39 | //! `chat.reply` and `insight.student` to a service; a service *calls* |
| 40 | //! `insight.summary.upsert` and the rest of the backend's storage surface |
| 41 | //! to have the backend write rows an AI service may not write itself. |
| 42 | //! |
| 43 | //! The three client-initiated request shapes are told apart by their |
| 44 | //! required field: an [`ApiRequest`] has `path`, a [`BlobRequest`] has |
| 45 | //! `file`, a [`CapabilityRequest`] has `capability`. |
| 46 | //! |
| 47 | //! * Each **blob upload** is the blob read's mirror image: the service writes |
| 48 | //! one [`BlobUploadRequest`], then exactly `size` **raw** bytes — again not a |
| 49 | //! frame — and finishes its send side; the backend stores them and answers |
| 50 | //! one [`BlobUploadResponse`] header frame. That is how a service hands back |
| 51 | //! an artifact it produced (today the podcast service's finished mp3), which |
| 52 | //! no JSON frame could carry. Distinguished from a [`BlobRequest`] by its |
| 53 | //! required `upload` marker and `job_id`. |
| 54 | //! |
| 55 | //! # Refusal codes |
| 56 | //! |
| 57 | //! [`ApiResponse::Err`], [`BlobResponse::Err`] and [`CapabilityResponse::Err`] |
| 58 | //! all carry a `code`: a stable, machine-readable string a service branches |
| 59 | //! on, never a message. The vocabulary, in one place: |
| 60 | //! |
| 61 | //! * `malformed` — the frame is not this shape (a missing or mistyped field). |
| 62 | //! * `unknown_capability` — no operation this backend serves has that name. |
| 63 | //! * `unknown_school` — the named id is not a school on this deployment. |
| 64 | //! * `school_suspended` — it exists and is switched off; worth retrying later. |
| 65 | //! * `not_permitted` — the school's own feature set refuses the operation. |
| 66 | //! * `invalid_payload` — the payload does not fit the operation's contract |
| 67 | //! (`message` names the field). |
| 68 | //! * `too_many_rows` — a count bound was exceeded (`message` names it); the |
| 69 | //! operation was refused whole, never applied in part. |
| 70 | //! * `unavailable` — the school's database could not be reached. |
| 71 | //! * `timed_out` — the operation outlived the bridge's deadline; whether it |
| 72 | //! landed is unknown, so retrying is the caller's decision. |
| 73 | //! * `internal` — the backend failed while doing the work. |
| 74 | //! * `path_not_allowed`, `unknown_user`, `method_not_allowed` — api-read only. |
| 75 | //! |
| 76 | //! # School scoping |
| 77 | //! |
| 78 | //! Every request frame names its school by uuid, and every answer echoes it: |
| 79 | //! [`Request::school`], [`ApiRequest::school`], [`BlobRequest::school`] and |
| 80 | //! their responses. The AI fleet is *shared* across the deployment — one |
| 81 | //! service serves every school — so the school cannot be bound once at |
| 82 | //! handshake time and [`Hello`] deliberately carries none: a service pinned to |
| 83 | //! one school would have to be run once per customer. A frame without a |
| 84 | //! `school` is `malformed`, an unknown id is `unknown_school`, a suspended |
| 85 | //! one `school_suspended`; there is no default and no fallback, because a read |
| 86 | //! answered out of the wrong school's database is the one failure this field |
| 87 | //! exists to make impossible. |
| 88 | //! |
| 89 | //! There is deliberately no correlation-id matching: QUIC stream IDs already |
| 90 | //! multiplex concurrent requests over the one connection, independently |
| 91 | //! flow-controlled, with no head-of-line blocking between them. [`Request::id`] |
| 92 | //! is a trace id for logs on both sides, nothing more. |
| 93 | //! |
| 94 | //! # Framing |
| 95 | //! |
| 96 | //! `u32` big-endian byte length, then that many bytes of JSON. JSON (not a |
| 97 | //! compact binary codec) because the AI services are expected to be written in |
| 98 | //! whatever language suits the model, and a length-prefixed JSON frame is |
| 99 | //! twenty lines in any of them. |
| 100 | |
| 101 | use ; |
| 102 | use Value; |
| 103 | use ; |
| 104 | |
| 105 | use crate; |
| 106 | |
| 107 | /// Why a frame could not be read or written. |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | /// The service's opening frame on the control stream. |
| 121 | /// |
| 122 | /// Carries **no** school on purpose: the fleet is shared, so one connection |
| 123 | /// serves every school on the deployment and each frame names its own (see |
| 124 | /// this module's "School scoping"). |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | /// The backend's answer on the control stream. |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | /// Why a handshake was refused. The service should not retry `Unauthorized` |
| 162 | /// or `UnsupportedProtocol` without a config change. |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | /// One unit of work, written by the backend on a fresh server-initiated |
| 173 | /// stream. |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | /// The service's single answer frame. `Err` is a *handled* failure (bad input, |
| 192 | /// model refused); a service that dies mid-request just drops the stream, |
| 193 | /// which surfaces as a transport error instead. |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | /// A read of the school's own API, written by the *service* on a fresh |
| 214 | /// client-initiated stream. |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | /// The backend's single answer frame. `Err` is a bridge-level refusal (path not |
| 239 | /// allowed, unknown user); an API call that ran and answered `404` is an `Ok` |
| 240 | /// carrying that status, because the service asked and the API replied. |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | /// A read of one course-note file's *bytes*, written by the *service* on a |
| 266 | /// fresh client-initiated stream. Distinguished from an [`ApiRequest`] by its |
| 267 | /// required `file` field. |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | /// The header frame that opens (or refuses) a blob stream. On `Ok` exactly |
| 286 | /// `size` raw bytes follow it, then FIN; on `Err` the stream is finished with |
| 287 | /// nothing after the frame. |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | /// A call of one capability the *backend* serves, written by the service on a |
| 311 | /// fresh client-initiated stream. The mirror image of [`Request`], which the |
| 312 | /// backend writes when it calls a capability the *service* serves. |
| 313 | /// |
| 314 | /// Distinguished from an [`ApiRequest`] by its required `capability` field and |
| 315 | /// from a [`BlobRequest`] by its required `school` and `payload`. There is no |
| 316 | /// path and no method: an operation is named, never addressed — the backend's |
| 317 | /// storage surface is a list of operations, and a frame that could name a |
| 318 | /// table, a schema or a query would be the door this shape exists to not have. |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | /// The backend's single answer frame, mirroring [`Response`] field for field: |
| 337 | /// `status` is the discriminator, and both variants echo the school the call |
| 338 | /// named so a service can tell which of its in-flight calls was answered. |
| 339 | /// |
| 340 | /// `Err` is a *handled* refusal (unknown capability, a payload that does not |
| 341 | /// fit, the school's database unreachable). A service that dies mid-call just |
| 342 | /// drops the stream, which surfaces as a transport error instead. |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | /// A write of one produced artifact's *bytes*, written by the *service* on a |
| 365 | /// fresh client-initiated stream. The mirror image of [`BlobRequest`]: the |
| 366 | /// service names what it is handing over, exactly `size` raw bytes follow this |
| 367 | /// frame, and the backend stores them somewhere that belongs to the school — |
| 368 | /// never to the service's own volume. |
| 369 | /// |
| 370 | /// Distinguished from a [`BlobRequest`] (the read) by the required `upload` |
| 371 | /// marker and its `job_id`: an upload is always *about* one backend-minted |
| 372 | /// job, and the backend links the stored blob to that job's row. |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | /// The single answer frame an upload gets. There are no bytes after it: the |
| 399 | /// artifact travels service → backend only, and `Ok.key` is the handle the |
| 400 | /// read doors publish (`GET /podcast/jobs/{id}/audio` resolves it). A refusal |
| 401 | /// carries nothing after the frame, and the backend resets the stream rather |
| 402 | /// than reading a body it will not store. |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | /// Write one length-prefixed JSON frame. |
| 431 | pub async |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | /// Read one length-prefixed JSON frame. |
| 450 | /// |
| 451 | /// The length is checked *before* allocating, so a hostile or confused peer |
| 452 | /// cannot make the backend reserve gigabytes by lying in four bytes. |
| 453 | pub async |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | /// `read_exact` that reports a clean peer FIN as [`FrameError::Eof`] rather |
| 470 | /// than an opaque io error — a service closing its control stream is normal. |
| 471 | async |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | /// Does this [`Hello`] speak our protocol version? |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | |
| 761 | |
| 762 | |
| 763 | |
| 764 | |
| 765 | |
| 766 | |
| 767 | |
| 768 | |
| 769 | |
| 770 | |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | |
| 791 | |
| 792 | |
| 793 | |
| 794 | |
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | |
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | |
| 813 | |
| 814 | |
| 815 | |
| 816 | |
| 817 | |
| 818 | |
| 819 | |
| 820 | |
| 821 | |
| 822 |