Skip to content

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)

LynCareOS rules require:

  1. Services must not handle HTTP (Request, Response, HTTPException, file responses).
  2. Clinical mutations must write an audit_log row 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.

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 pass audit= into services.
  • Services accept AuditContext | None only — never Request.
  • write_audit takes audit: AuditContext (legacy request= / performed_by= accepted only during migration).
  • Domain errors are plain exceptions (or a small domain-error hierarchy). Routers map them to HTTPException at the boundary. services/http_errors.py remains the intentional adapter for that mapping when a thin helper is useful at the boundary — not inside domain logic.
  • Phase C can remove Request / HTTPException / FileResponse from 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) document AuditContext as the only sanctioned cross-boundary audit metadata.
  • Keep Request in 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).