hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits
| 1 | //! The insight nest: the read surface over ZEKA's tables, the doors that ask |
| 2 | //! ZEKA to compute, and the storage doors its rows are written through. |
| 3 | //! |
| 4 | //! ZEKA (`hezarfen_zeka`) is an AI service that dials in over the QUIC bridge. |
| 5 | //! It **does not touch a school database** — no AI service on this deployment |
| 6 | //! does — so the `zeka_*` tables, created by the school migrator, are written |
| 7 | //! and read only by this process. Two surfaces, one implementation each: the |
| 8 | //! service calls the backend over the bridge (`insight.summary.upsert` and |
| 9 | //! the rest of the family — the wire contract is [`crate::ai::insight`], the |
| 10 | //! statements are [`crate::db::insight`]), and the same operations are |
| 11 | //! mounted here as HTTP doors for tooling. The backend never composes an |
| 12 | //! insight row; it stores what the service computed and hands it back. |
| 13 | //! |
| 14 | //! Reading is authorized the way every per-student report in this API is: |
| 15 | //! [`service::parent_link::ensure_can_observe`] decides whether the caller |
| 16 | //! may read *that student* at all (teacher+ clears it, a parent needs a live |
| 17 | //! link), and a teacher is then narrowed to the students they actually reach |
| 18 | //! — a shared class×course instance, asked through |
| 19 | //! [`service::instance::visible_instances`]. A student who is not the subject |
| 20 | //! reads `404`, never `403`: a foreign id is indistinguishable from a missing |
| 21 | //! one, the same posture `/marks/{user}` and `/rag/threads/{id}` take. |
| 22 | //! |
| 23 | //! What each door deliberately does **not** return is as much of the contract |
| 24 | //! as what it does (ZEKA's output contract, |
| 25 | //! `hezarfen_zeka/service/docs/CIKTI-SOZLESMESI.md` §5): |
| 26 | //! |
| 27 | //! * The attention list is a teacher's tool. It never appears in |
| 28 | //! [`my_insight`], and a linked parent reading their child does not get it |
| 29 | //! either — only staff at `teacher` or above do. |
| 30 | //! * A card's `about` (another student's id) never reaches a student caller. |
| 31 | //! * Segment rows with `confidence = 'none'` (fewer than 30 answers) are |
| 32 | //! filtered out: noise stays in the database, not on a screen. |
| 33 | //! * Dismissed and expired cards are filtered out of both card reads — the |
| 34 | //! application closes a card by stamping `dismissed_at`, and a closed card |
| 35 | //! must not come back. (The dismiss **write** is not built here: nothing in |
| 36 | //! this batch asked for it, and it needs its own decision on who may stamp |
| 37 | //! the audit trail. `deferred`, not half-done.) |
| 38 | //! |
| 39 | //! Sending is detached, like every other AI round trip in this API: the |
| 40 | //! compute doors answer `202` the moment the work is queued, because a |
| 41 | //! student's compute can outlast the request-timeout layer |
| 42 | //! ([`crate::constant::REQUEST_TIMEOUT_SECS`]). The client polls the stored |
| 43 | //! rows — the compute doors change nothing a reader cannot already see. |
| 44 | //! |
| 45 | //! The **report** doors are the one exception, and deliberately: building the |
| 46 | //! school's document is not a compute the service does on its own schedule — |
| 47 | //! it is one artifact the manager asked for, rendered from rows the backend |
| 48 | //! already holds, so `POST /runs/{run_day}/report` waits, stores the returned |
| 49 | //! HTML under the school's own blob directory, and answers `200` with what |
| 50 | //! was stored. `GET /runs/{run_day}/report` streams that document back. |
| 51 | |
| 52 | use Json; |
| 53 | use Body; |
| 54 | use ; |
| 55 | use HeaderValue; |
| 56 | use StatusCode; |
| 57 | use ; |
| 58 | use ; |
| 59 | use ; |
| 60 | use Value as JsonValue; |
| 61 | use Json as SqlJson; |
| 62 | use ToSchema; |
| 63 | use OpenApiRouter; |
| 64 | use routes; |
| 65 | |
| 66 | use crateAiError; |
| 67 | use crate |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | ; |
| 73 | use crate |
| 74 | |
| 75 | |
| 76 | ; |
| 77 | use crateDatabase; |
| 78 | use cratedb; |
| 79 | use crateClassGroupId; |
| 80 | use crateCourseId; |
| 81 | use crateRole; |
| 82 | use crate; |
| 83 | use crate; |
| 84 | use crateservice; |
| 85 | use crateensure_can_observe; |
| 86 | use crateAppState; |
| 87 | use crateResolvedTenant; |
| 88 | use crateState; |
| 89 | |
| 90 | use |
| 91 | |
| 92 | ; |
| 93 | |
| 94 | /// The single `now` every card read is filtered against. ZEKA stores unix |
| 95 | /// milliseconds, so "expired" is a comparison, not a projection. |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | /// The one door of this nest that is **not** school-scoped: the deployment's |
| 117 | /// active-school directory. |
| 118 | /// |
| 119 | /// ZEKA's storage operations are all scoped to the school a session belongs |
| 120 | /// to, and so are these doors — but the directory is not: it names every |
| 121 | /// customer. A school session must therefore never reach it, which is why |
| 122 | /// this route is mounted *outside* the school-scoped gate, on the builder |
| 123 | /// principal (the deployment operator), exactly like the vendor surface's own |
| 124 | /// `GET /schools`. A school cookie answers `401` here, as it does everywhere |
| 125 | /// the builder principal is required. |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | // ---- the compute doors ----------------------------------------------------- |
| 131 | |
| 132 | /// The receipt for a queued dispatch. It carries no id: what was started is |
| 133 | /// not a row of ours, and the work's own record is ZEKA's (`zeka_run` for a |
| 134 | /// refresh, a student's `computed_at` for a compute). |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | /// Ask ZEKA to recompute one student's insight, detached. `202` means queued, |
| 163 | /// not computed: the service works through the student's data and writes its |
| 164 | /// `zeka_*` rows on its own, and `GET /insights/students/{id}` shows them as |
| 165 | /// soon as they land. `503` when no service offers `insight.student` — and |
| 166 | /// nothing is queued. |
| 167 | /// |
| 168 | /// Authorization is [`student_insight`]'s, gate for gate: the caller must be |
| 169 | /// able to read the student before they may ask for a recompute, and the |
| 170 | /// refusal is a `404` (a foreign id and a missing one look the same). |
| 171 | /// |
| 172 | /// The dispatch names the **caller** as `requested_by`, not the student: the |
| 173 | /// service reads the student's marks and the rest as that caller, so a |
| 174 | /// teacher's or manager's own reach is what the reads answer against. |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | async |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 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 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | /// Ask ZEKA to recompute the whole school's insights, detached. `202` means |
| 233 | /// queued; the sweep's own record is `zeka_run`, which `GET /insights/runs` |
| 234 | /// reads — poll that to watch a run move from `running` to its verdict. |
| 235 | /// |
| 236 | /// Manager+ only: this is a school-wide batch, and the run it starts spends |
| 237 | /// the service's whole time budget. `503` when no service offers |
| 238 | /// `insight.refresh` — nothing is queued. |
| 239 | /// |
| 240 | /// The dispatch names the **caller** as `requested_by` — the principal the |
| 241 | /// service's own reads for the sweep run as — and always fills `user_ids`: a |
| 242 | /// named list passes through exactly as given, while an empty body is filled |
| 243 | /// from the school's own student roster (every user holding the `student` |
| 244 | /// role). The service's own roster discovery reads homework `assigned` lists, |
| 245 | /// so an empty body used to sweep nobody; the payload's `roster_source` records |
| 246 | /// which roster the list was. A school with no students dispatches an empty |
| 247 | /// list and answers `0` honestly. |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | async |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | // ---- the report doors ------------------------------------------------------ |
| 318 | |
| 319 | /// The receipt for a stored report. `generated_at` is this backend's clock the |
| 320 | /// moment the document was stored — the artifact's age is what a manager |
| 321 | /// re-clicking the button wants to see. |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | /// Where a school's generated report documents live under its blob directory: |
| 340 | /// one file per run day. The day is validated before it is joined (see |
| 341 | /// [`db::insight::run_day_ok`]), so the key can carry no separator and no |
| 342 | /// `..`. |
| 343 | const REPORT_DIR: &str = "zeka_report"; |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | /// Render the school's report for one run day, and store it. Synchronous, and |
| 350 | /// deliberately so: the manager clicked one button, and the document is what |
| 351 | /// they asked for — there is no queue to poll and no partial state, and the |
| 352 | /// receipt says what was stored. |
| 353 | /// |
| 354 | /// Manager+ only: the document is whole-school, like the refresh sweep and the |
| 355 | /// run ledger. The payload is read from this school's own `zeka_*` tables and |
| 356 | /// dispatched to the service that declares `insight.report`; the service may |
| 357 | /// not read a database, so every row the document needs travels in the |
| 358 | /// request — the same serde rows the storage surface writes. |
| 359 | /// |
| 360 | /// A run day with no ledger row, or one whose tables are empty, is still a |
| 361 | /// legal request: the service renders what it was given and says so in |
| 362 | /// `notes`. Refusals: `503` when no service offers the capability, or it did |
| 363 | /// not answer — nothing was stored; `429` when every worker is at capacity; |
| 364 | /// and the service's own typed refusals, mapped per class by |
| 365 | /// [`report_refusal`]. |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | async |
| 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 | /// The service's refusal vocabulary as this door's HTTP answer. |
| 462 | /// |
| 463 | /// The report service closes four codes (`hezarfen_zeka`'s handler) and a |
| 464 | /// client branches on the class each becomes: |
| 465 | /// |
| 466 | /// * `bad_request` — the service judged the composed request bad (an unserved |
| 467 | /// kind, a malformed day, a row field that is not a list, a school |
| 468 | /// disagreement). The payload is composed here, so this is a backend defect |
| 469 | /// and the caller can only be told the request was refused; the service's |
| 470 | /// own words go to the log. |
| 471 | /// * `insufficient_rows` — all four row lists were empty, so there is no |
| 472 | /// document to build. `409 report_empty`: no retry conjures data, a sweep |
| 473 | /// does. |
| 474 | /// * `document_too_large` — over the service's own HTML ceiling, which it |
| 475 | /// never truncates past. `413`. |
| 476 | /// * `internal` — the render failed on the service's side. `500`. |
| 477 | /// |
| 478 | /// Anything else is `409 report_refused` with the service's code and message |
| 479 | /// kept verbatim: a service may define codes this backend has never heard of, |
| 480 | /// and swallowing them would hide the one explanation a caller needs. |
| 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 | /// The stored report document, streamed. |
| 507 | /// |
| 508 | /// `409 report_missing` when nothing has been generated for the day — a |
| 509 | /// refusal that covers a day the ledger has never heard of too: generation |
| 510 | /// accepts such a day on purpose (the document's `notes` say it), so a `404` |
| 511 | /// here could refuse a day the POST door had just answered `200` for. |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | async |
| 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 | /// Store the rendered document under the school's own blob directory. |
| 563 | /// |
| 564 | /// Written to a temp name and renamed into place: the artifact appears |
| 565 | /// atomically or not at all, so a reader — or a second POST racing this one — |
| 566 | /// can never open a half-written document. The temp file is removed if |
| 567 | /// anything fails before the rename. |
| 568 | async |
| 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 | // ---- the storage doors ----------------------------------------------------- |
| 599 | // |
| 600 | // ZEKA's own tables are written and read by the backend, never by the service |
| 601 | // (`crate::db::insight` holds the statements, `src/ai/insight.rs` the bridge |
| 602 | // contract). These are the same nine operations over HTTP — one service |
| 603 | // function, two surfaces — so a deployment's tooling can seed, inspect and |
| 604 | // maintain what the service computed without holding a bridge connection. |
| 605 | // |
| 606 | // Manager+ on all of them: every one reads or writes rows about students |
| 607 | // school-wide, which is the same floor `GET /insights/runs` and the refresh |
| 608 | // door carry. The school is the caller's own, resolved by the nest's own |
| 609 | // extractors; no door here takes a school, a database or a query in its body. |
| 610 | |
| 611 | /// Store a batch of ZEKA's student summaries (and their attention items). |
| 612 | /// |
| 613 | /// This is what the AI service calls over the bridge as |
| 614 | /// `insight.summary.upsert`. One call is one batch and one transaction: the |
| 615 | /// batch lands whole or not at all, so a caller that retries cannot |
| 616 | /// half-apply it. Each student's attention items are **replaced**, never |
| 617 | /// merged — a fact the service stopped producing must not stay on screen. |
| 618 | /// At most [`MAX_INSIGHT_BATCH_ROWS`](crate::constant::MAX_INSIGHT_BATCH_ROWS) |
| 619 | /// rows per call. |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | async |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | /// Store a batch of ZEKA's recommendation cards. |
| 644 | /// |
| 645 | /// The bridge capability is `insight.recommendation.upsert`. The backend |
| 646 | /// mints each row's id. A row whose evidence carries nothing but a |
| 647 | /// `limitation` is **not written** and is counted `rejected` in the receipt — |
| 648 | /// the service's own rule, applied where the write happens instead of being |
| 649 | /// trusted to the writer. |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | async |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | /// Store a batch of ZEKA's question-segment labels (and each question's |
| 676 | /// dimension split). |
| 677 | /// |
| 678 | /// The bridge capability is `insight.segment.upsert`; the dimension rows are |
| 679 | /// replaced with the payload, like the attention items above. |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | async |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | /// Store a batch of ZEKA's student segment profiles. |
| 704 | /// |
| 705 | /// The bridge capability is `insight.profile.upsert`. `contrast` is the field |
| 706 | /// the service's rules fire on — the raw `accuracy` mostly measures a |
| 707 | /// student's general level and is stored for the evidence panel only. |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | async |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | |
| 731 | /// Store one compute run's ledger row (with its pending students and failed |
| 732 | /// modules, both replaced). |
| 733 | /// |
| 734 | /// The bridge capability is `insight.run.upsert`; the run is keyed by |
| 735 | /// `run_day` (`YYYY-MM-DD`), so a re-run of the same night overwrites rather |
| 736 | /// than duplicating. `GET /insights/runs` reads the ledger back. |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 750 | |
| 751 | async |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | /// The students the freshest compute run left pending — where the next run |
| 760 | /// starts. |
| 761 | /// |
| 762 | /// Manager+ only, like `GET /insights/runs`: `pending_students` is a list of |
| 763 | /// people, and this is the same list unpaged. The bridge capability is |
| 764 | /// `insight.pending.list`; the list is answered whole or refused past |
| 765 | /// [`MAX_INSIGHT_PENDING_STUDENTS`](crate::constant::MAX_INSIGHT_PENDING_STUDENTS), |
| 766 | /// never clipped, because the service resumes its next run from it. |
| 767 | |
| 768 | |
| 769 | |
| 770 | |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | |
| 779 | async |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 | /// Delete every `zeka_*` row whose retention window has closed. |
| 787 | /// |
| 788 | /// The bridge capability is `insight.retention.sweep`. Children go before |
| 789 | /// their parents (the foreign keys are `ON DELETE NO ACTION`), the backend's |
| 790 | /// own clock decides expiry, and each table answers its own verdict — a table |
| 791 | /// that could not be swept is reported `false` while the rest proceed, since |
| 792 | /// housekeeping must not lose a whole run over one locked table. |
| 793 | |
| 794 | |
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | |
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | async |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | /// Delete the derived rows of every student who is no longer on the roster. |
| 813 | /// |
| 814 | /// The bridge capability is `insight.departed.purge`; `students` is the |
| 815 | /// **active** roster, and every student not named loses their summaries, |
| 816 | /// profiles, attention items, cards about them, and pending-run entries. One |
| 817 | /// transaction, so the verdicts move together. An empty list is refused: an |
| 818 | /// empty roster is a fetch that failed, and obeying it would delete the whole |
| 819 | /// school's derived data. |
| 820 | |
| 821 | |
| 822 | |
| 823 | |
| 824 | |
| 825 | |
| 826 | |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | |
| 832 | |
| 833 | |
| 834 | |
| 835 | async |
| 836 | |
| 837 | |
| 838 | |
| 839 | |
| 840 | |
| 841 | |
| 842 | |
| 843 | |
| 844 | |
| 845 | /// The deployment's active schools, by school — the directory a shared AI fleet |
| 846 | /// schedules over. |
| 847 | /// |
| 848 | /// This is the one operation of the family that is not school-scoped, and the |
| 849 | /// one door of this nest that is not a school's: it names every customer, so |
| 850 | /// it runs on the **builder** principal (the deployment operator), mounted |
| 851 | /// outside the school gate exactly like the vendor surface's `GET /schools`. |
| 852 | /// A school session gets `401` here. The bridge capability is |
| 853 | /// `insight.schools.list`, which is the service's own deployment-scoped call: |
| 854 | /// the same function, two surfaces, and a school-scoped caller on neither. |
| 855 | |
| 856 | |
| 857 | |
| 858 | |
| 859 | |
| 860 | |
| 861 | |
| 862 | |
| 863 | |
| 864 | |
| 865 | async |
| 866 | |
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | /// Canonicalize the ids a refresh body names — through the same parser every |
| 873 | /// route uses for a wire id, so a client cannot hand the service a spelling |
| 874 | /// this backend itself would never produce. Anything that parses as no uuid |
| 875 | /// is a `400`: unlike a path segment (where garbage reads as the nil id and |
| 876 | /// matches no row), a batch naming students is a list this backend must not |
| 877 | /// forward half-understood. |
| 878 | |
| 879 | |
| 880 | |
| 881 | |
| 882 | |
| 883 | |
| 884 | |
| 885 | |
| 886 | |
| 887 | |
| 888 | |
| 889 | |
| 890 | |
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | |
| 896 | |
| 897 | /// The school's own student roster, as the refresh door fills an empty request |
| 898 | /// body with. |
| 899 | /// |
| 900 | /// The same set the office's student table lists: every `app_user` holding the |
| 901 | /// `student` role exactly, newest first ([`db::user::list_by_role`]). It is |
| 902 | /// what the service cannot assemble itself — its own roster discovery reads |
| 903 | /// homework `assigned` lists, which a live school rarely fills — so an empty |
| 904 | /// body must name the school here or the sweep computes nobody. |
| 905 | /// |
| 906 | /// Refused, never clipped, past |
| 907 | /// [`MAX_INSIGHT_REFRESH_STUDENTS`](crate::constant::MAX_INSIGHT_REFRESH_STUDENTS): |
| 908 | /// a clipped roster would leave students unanalysed while the run read as |
| 909 | /// complete. A school with no students answers an empty list — a sweep that |
| 910 | /// computed nobody is a true answer, not a failure. |
| 911 | async |
| 912 | |
| 913 | |
| 914 | |
| 915 | |
| 916 | |
| 917 | |
| 918 | |
| 919 | |
| 920 | |
| 921 | |
| 922 | |
| 923 | // ---- the reads ------------------------------------------------------------- |
| 924 | |
| 925 | /// One student's nightly summary. The four module objects are ZEKA's own |
| 926 | /// shapes (JSONB, by design): read a member by name, never by position. `null` |
| 927 | /// for a module the run could not compute — an empty object and a missing |
| 928 | /// module are different claims, and only one of them is true. |
| 929 | |
| 930 | |
| 931 | |
| 932 | |
| 933 | |
| 934 | |
| 935 | |
| 936 | |
| 937 | |
| 938 | |
| 939 | |
| 940 | |
| 941 | |
| 942 | |
| 943 | |
| 944 | |
| 945 | /// One entry of the attention list — a statement of fact about a window, not |
| 946 | /// a judgement. **Teacher-facing**: it is never returned to the student it is |
| 947 | /// about (see the module docs). |
| 948 | |
| 949 | |
| 950 | |
| 951 | |
| 952 | |
| 953 | |
| 954 | |
| 955 | |
| 956 | |
| 957 | |
| 958 | |
| 959 | |
| 960 | |
| 961 | |
| 962 | |
| 963 | |
| 964 | |
| 965 | /// One recommendation card addressed to the caller. It carries no |
| 966 | /// `dismissed_at`/`dismiss_by`: a dismissed card is filtered out of both card |
| 967 | /// reads, so the field could only ever be null — and a face that never |
| 968 | /// arrives is worse than no face. |
| 969 | |
| 970 | |
| 971 | |
| 972 | |
| 973 | |
| 974 | |
| 975 | |
| 976 | |
| 977 | |
| 978 | |
| 979 | |
| 980 | |
| 981 | |
| 982 | |
| 983 | |
| 984 | |
| 985 | |
| 986 | |
| 987 | |
| 988 | |
| 989 | |
| 990 | |
| 991 | |
| 992 | |
| 993 | |
| 994 | |
| 995 | |
| 996 | |
| 997 | |
| 998 | |
| 999 | /// One student × dimension × label row. `accuracy` is returned alongside |
| 1000 | /// `contrast` and never alone: raw accuracy carries general ability, and |
| 1001 | /// `contrast = accuracy − overall_accuracy` is the part specific to the |
| 1002 | /// segment. |
| 1003 | |
| 1004 | |
| 1005 | |
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | |
| 1010 | |
| 1011 | |
| 1012 | |
| 1013 | |
| 1014 | |
| 1015 | |
| 1016 | |
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | /// One student's whole readable insight: the stored summary, the caller's own |
| 1027 | /// cards about them, and the segment profile. |
| 1028 | |
| 1029 | |
| 1030 | |
| 1031 | |
| 1032 | |
| 1033 | |
| 1034 | |
| 1035 | |
| 1036 | |
| 1037 | |
| 1038 | |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 | /// The caller's own insight. Students get their summary, their own cards and |
| 1044 | /// their segment profile — **never** the attention list about themselves. |
| 1045 | /// Any other role gets the cards addressed to them (a teacher's `T4` cards |
| 1046 | /// about their students), with `summary`/`segments` empty. |
| 1047 | |
| 1048 | |
| 1049 | |
| 1050 | |
| 1051 | |
| 1052 | |
| 1053 | |
| 1054 | |
| 1055 | |
| 1056 | |
| 1057 | async |
| 1058 | |
| 1059 | |
| 1060 | |
| 1061 | |
| 1062 | |
| 1063 | |
| 1064 | |
| 1065 | |
| 1066 | |
| 1067 | |
| 1068 | |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | |
| 1073 | |
| 1074 | |
| 1075 | |
| 1076 | |
| 1077 | |
| 1078 | |
| 1079 | |
| 1080 | /// One student's insight, as an observer reads it. Teacher+ (narrowed to the |
| 1081 | /// students they reach) or a parent holding a live link to that student; |
| 1082 | /// everyone else — other students included — gets the same `404` a missing id |
| 1083 | /// gets. |
| 1084 | |
| 1085 | |
| 1086 | |
| 1087 | |
| 1088 | |
| 1089 | |
| 1090 | |
| 1091 | |
| 1092 | |
| 1093 | |
| 1094 | |
| 1095 | |
| 1096 | |
| 1097 | async |
| 1098 | |
| 1099 | |
| 1100 | |
| 1101 | |
| 1102 | |
| 1103 | |
| 1104 | |
| 1105 | |
| 1106 | |
| 1107 | |
| 1108 | |
| 1109 | |
| 1110 | |
| 1111 | |
| 1112 | |
| 1113 | |
| 1114 | |
| 1115 | |
| 1116 | |
| 1117 | |
| 1118 | /// One compute run, as the ledger keeps it. |
| 1119 | |
| 1120 | |
| 1121 | |
| 1122 | |
| 1123 | |
| 1124 | |
| 1125 | |
| 1126 | |
| 1127 | |
| 1128 | |
| 1129 | |
| 1130 | |
| 1131 | |
| 1132 | |
| 1133 | |
| 1134 | |
| 1135 | |
| 1136 | |
| 1137 | |
| 1138 | |
| 1139 | |
| 1140 | |
| 1141 | |
| 1142 | |
| 1143 | |
| 1144 | |
| 1145 | |
| 1146 | |
| 1147 | |
| 1148 | /// The run ledger, newest first. Manager+ only: `pending_students` is a list |
| 1149 | /// of people, and ZEKA's output contract confines it to the management view. |
| 1150 | /// Paged via `?limit=&offset=`. |
| 1151 | |
| 1152 | |
| 1153 | |
| 1154 | |
| 1155 | |
| 1156 | |
| 1157 | |
| 1158 | |
| 1159 | |
| 1160 | |
| 1161 | |
| 1162 | |
| 1163 | |
| 1164 | async |
| 1165 | |
| 1166 | |
| 1167 | |
| 1168 | |
| 1169 | |
| 1170 | |
| 1171 | |
| 1172 | |
| 1173 | |
| 1174 | |
| 1175 | |
| 1176 | |
| 1177 | |
| 1178 | |
| 1179 | |
| 1180 | |
| 1181 | |
| 1182 | |
| 1183 | |
| 1184 | |
| 1185 | |
| 1186 | |
| 1187 | |
| 1188 | |
| 1189 | |
| 1190 | |
| 1191 | |
| 1192 | |
| 1193 | |
| 1194 | |
| 1195 | |
| 1196 | |
| 1197 | |
| 1198 | |
| 1199 | |
| 1200 | |
| 1201 | |
| 1202 | |
| 1203 | |
| 1204 | |
| 1205 | |
| 1206 | /// The children of the runs on this page — one query for the whole page, not |
| 1207 | /// one per run, grouped by run day in `ord` order. |
| 1208 | async |
| 1209 | |
| 1210 | |
| 1211 | |
| 1212 | |
| 1213 | |
| 1214 | |
| 1215 | |
| 1216 | |
| 1217 | |
| 1218 | |
| 1219 | |
| 1220 | |
| 1221 | |
| 1222 | |
| 1223 | |
| 1224 | |
| 1225 | |
| 1226 | |
| 1227 | |
| 1228 | |
| 1229 | |
| 1230 | |
| 1231 | |
| 1232 | |
| 1233 | |
| 1234 | |
| 1235 | |
| 1236 | |
| 1237 | |
| 1238 | |
| 1239 | |
| 1240 | |
| 1241 | |
| 1242 | |
| 1243 | |
| 1244 | |
| 1245 | |
| 1246 | |
| 1247 | |
| 1248 | |
| 1249 | |
| 1250 | |
| 1251 | // ---- the queries ----------------------------------------------------------- |
| 1252 | |
| 1253 | /// The nightly summary, or `None` until ZEKA has computed this student once. |
| 1254 | async |
| 1255 | |
| 1256 | |
| 1257 | |
| 1258 | |
| 1259 | |
| 1260 | |
| 1261 | |
| 1262 | |
| 1263 | |
| 1264 | |
| 1265 | |
| 1266 | |
| 1267 | |
| 1268 | |
| 1269 | |
| 1270 | |
| 1271 | |
| 1272 | |
| 1273 | |
| 1274 | |
| 1275 | |
| 1276 | |
| 1277 | /// The attention list, in the writer's own order. Only staff at `teacher` or |
| 1278 | /// above ever call this — see the module docs. |
| 1279 | async |
| 1280 | |
| 1281 | |
| 1282 | |
| 1283 | |
| 1284 | |
| 1285 | |
| 1286 | |
| 1287 | |
| 1288 | |
| 1289 | |
| 1290 | |
| 1291 | |
| 1292 | |
| 1293 | |
| 1294 | |
| 1295 | |
| 1296 | |
| 1297 | |
| 1298 | |
| 1299 | |
| 1300 | |
| 1301 | |
| 1302 | |
| 1303 | |
| 1304 | |
| 1305 | /// Cards addressed to `audience`, filtered to the label the caller's own role |
| 1306 | /// reads (`audience_role`) and to cards that are still live: not expired, not |
| 1307 | /// dismissed. `about` narrows to one student — `None` is the caller's own |
| 1308 | /// card list, which for a teacher is the `T4` cards about their students. |
| 1309 | async |
| 1310 | |
| 1311 | |
| 1312 | |
| 1313 | |
| 1314 | |
| 1315 | |
| 1316 | |
| 1317 | |
| 1318 | |
| 1319 | |
| 1320 | |
| 1321 | |
| 1322 | |
| 1323 | |
| 1324 | |
| 1325 | |
| 1326 | |
| 1327 | |
| 1328 | |
| 1329 | |
| 1330 | |
| 1331 | |
| 1332 | |
| 1333 | |
| 1334 | |
| 1335 | |
| 1336 | |
| 1337 | |
| 1338 | |
| 1339 | |
| 1340 | |
| 1341 | |
| 1342 | |
| 1343 | |
| 1344 | |
| 1345 | |
| 1346 | |
| 1347 | |
| 1348 | |
| 1349 | |
| 1350 | |
| 1351 | |
| 1352 | |
| 1353 | |
| 1354 | |
| 1355 | |
| 1356 | |
| 1357 | |
| 1358 | |
| 1359 | /// The segment profile, noise filtered out: a row below 30 answers |
| 1360 | /// (`confidence = 'none'`) stays in the database and off every screen. |
| 1361 | async |
| 1362 | |
| 1363 | |
| 1364 | |
| 1365 | |
| 1366 | |
| 1367 | |
| 1368 | |
| 1369 | |
| 1370 | |
| 1371 | |
| 1372 | |
| 1373 | |
| 1374 | |
| 1375 | |
| 1376 | |
| 1377 | |
| 1378 | |
| 1379 | |
| 1380 | |
| 1381 | |
| 1382 | |
| 1383 | |
| 1384 | |
| 1385 | |
| 1386 | |
| 1387 | |
| 1388 | |
| 1389 | |
| 1390 | |
| 1391 | |
| 1392 | |
| 1393 | // ---- authorization --------------------------------------------------------- |
| 1394 | |
| 1395 | /// The one gate every per-student insight route shares, read and compute |
| 1396 | /// alike. |
| 1397 | /// |
| 1398 | /// [`ensure_can_observe`] settles the role question — teacher+ clears it, a |
| 1399 | /// parent needs a live link — and the student a caller may *reach* is then |
| 1400 | /// narrowed the same way `/marks/{user}` narrows a report: a teacher reads a |
| 1401 | /// student only when they run an instance that student sits in. The narrowing |
| 1402 | /// is a `404` rather than an empty body because the insight rows are not |
| 1403 | /// per-instance: there is nothing honest to narrow *to*, and a foreign |
| 1404 | /// student's id must be indistinguishable from a missing one. |
| 1405 | async |
| 1406 | |
| 1407 | |
| 1408 | |
| 1409 | |
| 1410 | |
| 1411 | |
| 1412 | |
| 1413 | |
| 1414 | |
| 1415 | |
| 1416 | |
| 1417 | |
| 1418 | |
| 1419 | |
| 1420 | |
| 1421 | /// Does this teacher actually reach this student — a shared class×course |
| 1422 | /// instance the teacher runs and the student sits in? The instances a teacher |
| 1423 | /// runs are [`service::instance::visible_instances`]'s `manages` half, which |
| 1424 | /// is the same D10 rule every instance-scoped route applies |
| 1425 | /// ([`service::class_course::ensure_instance_teacher`]): an assigned teacher |
| 1426 | /// or the class section's homeroom teacher, and nobody else. |
| 1427 | async |
| 1428 | |
| 1429 | |
| 1430 | |
| 1431 | |
| 1432 | |
| 1433 | |
| 1434 | |
| 1435 | |
| 1436 | |
| 1437 | |
| 1438 | |
| 1439 | |
| 1440 | |
| 1441 | |
| 1442 | |
| 1443 | |
| 1444 | |
| 1445 | |
| 1446 | |
| 1447 | |
| 1448 | |
| 1449 | |
| 1450 | |
| 1451 | |
| 1452 | |
| 1453 | |
| 1454 | |
| 1455 | /// The `audience_role` label a caller reads cards under. ZEKA's catalogue |
| 1456 | /// closes four labels (`student` | `teacher` | `parent` | `manager`); the two |
| 1457 | /// staff ranks above teacher fold into `manager`, and the bridge's own |
| 1458 | /// principal reads under a label no row can carry (the column's CHECK lists |
| 1459 | /// the human four) — so a service reading as itself gets the empty list an |
| 1460 | /// absent surface should give it. |
| 1461 | |
| 1462 | |
| 1463 | |
| 1464 | |
| 1465 | |
| 1466 | |
| 1467 | |
| 1468 | |
| 1469 |