mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-08 12:32:43 +00:00
`buildCoachSnapshot()` reads only persisted data — ~10 measurement findManys plus the GLP-1 / mood / intake side-fetches — yet a single chat conversation sends 2-4 turns within a minute and rebuilds the same snapshot every time. Wrap the existing builder (renamed to `buildCoachSnapshotImpl`) with an in-memory cache keyed on `(userId, scope.window, sorted scope.sources)` with a 60 s TTL and a 64-entry LRU cap. Different scopes recompute; the cache evicts on the next eligible read. `.planning/round-v1433-audit-perf.md` §3.3 estimates 200-800 ms of server-side tail saved on the second+ turn — the bulk of the "blockt komplett" complaint between user-clicks-send and first SSE `token` frame. The `__resetCoachSnapshotCacheForTests` export gives the existing two snapshot-test suites a hook so each fixture is read fresh; both add a `beforeEach` reset alongside the existing `vi.clearAllMocks()` call. Two new tests pin the cache-hit (one Prisma findMany call across two `buildCoachSnapshot` calls with the same scope) and the scope-changed miss (different `window` → two findManys). Feature-flag tests gain a parallel "per-request memoisation" describe-block that pins the same behaviour for `getAssistantFlags()` against a stable mocked event reference.