Agentic-RAG-Platform

STAR stories — incidents that actually happened, with artifacts

Rule: every story cites a file, a commit, or a test that exists. A story you cannot back with an artifact is a liability in an interview.

Story 1 — The RBAC query that never ran (bug found by reading, not failing)

S: Pre-retrieval RBAC used optional filters as :department IS NULL OR d.department = :department with a NULL bind. T: Make department filtering work for every query shape. A: Reading asyncpg’s error contract showed the pattern raises AmbiguousParameterError when the bind is NULL — the retrieval SQL had never successfully executed for unfiltered queries. Rewrote as predicate-assembly: append AND d.department = :department only when the filter is set (src/retrieval/sqlfilters.py, whose module docstring preserves the incident). R: Every unfiltered retrieval path works; the pattern is unit-tested (tests/unit/test_sqlfilters_*.py), and the story teaches: “a green test can still hide a query that never ran — read the driver’s error contract.”

Story 2 — Cross-event-loop connection poisoning

S: pytest-asyncio gave every test a fresh event loop, but the app cached a global SQLAlchemy engine; pooled connections outlived their loops. T: Stop intermittent “RuntimeError: Event loop is closed” cascades. A: An autouse fixture disposes the engine and clears the session maker after each test (tests/conftest.py::_fresh_db_engine). R: Stable suites; the fix is documented in the fixture docstring so nobody “optimizes” it away. Teaches: connection pools bind to the loop that created them.

Story 3 — The circular import nobody could ship

S: documents.py router imported document_service.py, which imported the router’s models back. T: Break the cycle without losing type hints. A: TYPE_CHECKING guard in the service, imports moved below model definitions (docs/engineering-journal.md, ingestion phase). R: Clean import graph; teaches runtime-vs-typing imports.

Story 4 — The 340 ms reranker nobody could afford

S: Cross-encoder reranking cost ~340 ms p95 on 4 cores for 50 pairs. T: Hit the latency budget without dropping reranking. A: Cut the candidate funnel from 100 to 50 and CPU inference moved off the event loop; the trade-off (tail recall vs latency) is recorded, not hidden (docs/engineering-journal.md). R: Budget met with a written trade-off. Teaches: measure, then cut the funnel, never the honesty.

Story 5 — Refresh tokens on the open web

S: The first auth implementation returned refresh tokens in JSON bodies. T: Production-grade session security. A: HttpOnly refresh cookie, rotation on every use, revocation list, logout that actually revokes (commit aa496c0; integration tests tests/integration/test_auth_refresh_rotation.py). R: Replay surface closed; teaches security is a series, not a flag.

Story 6 — The CI suite that lied about quality

S: A local run of the regression suite reported “60% unexpected failures” with no DB — degraded numbers indistinguishable from a real regression. T: Never let an environment gap masquerade as a product failure. A: Connectivity probes: tests skip with a reason when the stack is absent (this series; docs/reviews/test_matrix.md §14 philosophy). R: A red suite always means the product, never the plumbing. Teaches the meta-lesson: test the tests’ environment assumptions.