ADR 0001 — AuditContext and the service HTTP boundary
Status: Accepted
Version: 1.0
Last Updated: 2026-07-17 16:51:04
Deciders: LynCareOS remediation programme (quality-first)
Context
Section titled “Context”LynCareOS rules require:
- Services must not handle HTTP (
Request,Response,HTTPException, file responses). - Clinical mutations must write an
audit_logrow with client IP and User-Agent (HIPAA §164.312(b)).
Historically write_audit(..., request: Request) forced services to accept FastAPI Request, which systematically violated rule (1). Clinical hot-path services (patient_scripts_service, medication_groups_service, care_shifts_service) were the worst offenders.
Decision
Section titled “Decision”Introduce a framework-free AuditContext value object:
@dataclass(frozen=True, slots=True)class AuditContext: performed_by: int ip_address: str | None = None user_agent: str | None = None- Routers call
audit_context_from_request(request, performed_by=...)and passaudit=into services. - Services accept
AuditContext | Noneonly — neverRequest. write_audittakesaudit: AuditContext(legacyrequest=/performed_by=accepted only during migration).- Domain errors are plain exceptions (or a small domain-error hierarchy). Routers map them to
HTTPExceptionat the boundary.services/http_errors.pyremains the intentional adapter for that mapping when a thin helper is useful at the boundary — not inside domain logic.
Consequences
Section titled “Consequences”- Phase C can remove
Request/HTTPException/FileResponsefrom services without losing audit IP capture. - Tests can construct
AuditContext(performed_by=1, ip_address="127.0.0.1")without a FastAPI app. - Cursor rules (
.cursor/rules/rules.mdc) documentAuditContextas the only sanctioned cross-boundary audit metadata.
Alternatives considered
Section titled “Alternatives considered”- Keep
Requestin services — rejected (layering rule becomes fiction). - Drop IP from audit — rejected (HIPAA / compliance).
- Middleware that stashes IP in a contextvar — deferred (harder to test; implicit).