hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiBaf331ecAhmet Kaan Gümüş2 months ago451 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 | //! The two client-initiated request shapes are told apart by their required |
| 34 | //! field: an [`ApiRequest`] has `path`, a [`BlobRequest`] has `file`. |
| 35 | //! |
| 36 | //! # School scoping |
| 37 | //! |
| 38 | //! Every request frame names its school by slug, and every answer echoes it: |
| 39 | //! [`Request::school`], [`ApiRequest::school`], [`BlobRequest::school`] and |
| 40 | //! their responses. The AI fleet is *shared* across the deployment — one |
| 41 | //! service serves every school — so the school cannot be bound once at |
| 42 | //! handshake time and [`Hello`] deliberately carries none: a service pinned to |
| 43 | //! one school would have to be run once per customer. A frame without a |
| 44 | //! `school` is `malformed`, an unknown slug is `unknown_school`, a suspended |
| 45 | //! one `school_suspended`; there is no default and no fallback, because a read |
| 46 | //! answered out of the wrong school's database is the one failure this field |
| 47 | //! exists to make impossible. |
| 48 | //! |
| 49 | //! There is deliberately no correlation-id matching: QUIC stream IDs already |
| 50 | //! multiplex concurrent requests over the one connection, independently |
| 51 | //! flow-controlled, with no head-of-line blocking between them. [`Request::id`] |
| 52 | //! is a trace id for logs on both sides, nothing more. |
| 53 | //! |
| 54 | //! # Framing |
| 55 | //! |
| 56 | //! `u32` big-endian byte length, then that many bytes of JSON. JSON (not a |
| 57 | //! compact binary codec) because the AI services are expected to be written in |
| 58 | //! whatever language suits the model, and a length-prefixed JSON frame is |
| 59 | //! twenty lines in any of them. |
| 60 | |
| 61 | use ; |
| 62 | use Value; |
| 63 | use ; |
| 64 | |
| 65 | use crate; |
| 66 | |
| 67 | /// Why a frame could not be read or written. |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | /// The service's opening frame on the control stream. |
| 81 | /// |
| 82 | /// Carries **no** school on purpose: the fleet is shared, so one connection |
| 83 | /// serves every school on the deployment and each frame names its own (see |
| 84 | /// this module's "School scoping"). |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | /// The backend's answer on the control stream. |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | /// Why a handshake was refused. The service should not retry `Unauthorized` |
| 122 | /// or `UnsupportedProtocol` without a config change. |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | /// One unit of work, written by the backend on a fresh server-initiated |
| 133 | /// stream. |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | /// The service's single answer frame. `Err` is a *handled* failure (bad input, |
| 151 | /// model refused); a service that dies mid-request just drops the stream, |
| 152 | /// which surfaces as a transport error instead. |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | /// A read of the school's own API, written by the *service* on a fresh |
| 173 | /// client-initiated stream. |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | /// The backend's single answer frame. `Err` is a bridge-level refusal (path not |
| 197 | /// allowed, unknown user); an API call that ran and answered `404` is an `Ok` |
| 198 | /// carrying that status, because the service asked and the API replied. |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | /// A read of one course-note file's *bytes*, written by the *service* on a |
| 224 | /// fresh client-initiated stream. Distinguished from an [`ApiRequest`] by its |
| 225 | /// required `file` field. |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | /// The header frame that opens (or refuses) a blob stream. On `Ok` exactly |
| 243 | /// `size` raw bytes follow it, then FIN; on `Err` the stream is finished with |
| 244 | /// nothing after the frame. |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | /// Write one length-prefixed JSON frame. |
| 268 | pub async |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | /// Read one length-prefixed JSON frame. |
| 287 | /// |
| 288 | /// The length is checked *before* allocating, so a hostile or confused peer |
| 289 | /// cannot make the backend reserve gigabytes by lying in four bytes. |
| 290 | pub async |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | /// `read_exact` that reports a clean peer FIN as [`FrameError::Eof`] rather |
| 307 | /// than an opaque io error — a service closing its control stream is normal. |
| 308 | async |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | /// Does this [`Hello`] speak our protocol version? |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 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 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 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 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 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 |