Files
Dragan Spiridonov daa9fcf0a0 fix(learning): rehydrate lastUsedAt at kv seam to unblock consolidation (#493)
`LearningConsolidationWorker.getDomainPatterns()` mapped
`lastSeen: p.lastUsedAt`, but `lastUsedAt` arrives from the
`learning:pattern:*` kv as an ISO string (not a Date) after the JSON
round-trip. The two downstream consumers — `consolidatePatterns` and
`pruneIneffectivePatterns` — both call `.getTime()` on it, throwing
`TypeError: lastSeen.getTime is not a function` every cycle on installs
with non-empty pattern kv.

This was dead code until v3.9.32: #491 Bug 1+2 starved the pattern store
so `collectPatterns()` always threw `No learning patterns to consolidate
yet` before reaching `consolidatePatterns`. Fixing the #491 chain made
this line reachable for the first time and the same `Date`-lost-across-
kv-JSON-round-trip class of bug as #491 Bug 3 surfaced. Other call sites
were hardened in v3.9.32 (`pattern-store.ts:1301-1304`,
`pattern-lifecycle.ts:592-594`, `sqlite-persistence.ts:891`); these two
were missed because they were unreachable at the time.

Fix: rehydrate at the kv-read boundary — `lastSeen: new Date(p.lastUsedAt)`.
`new Date(x)` is safe for `Date | string | null | undefined` (yields
Invalid Date — whose `.getTime()` returns `NaN` rather than throwing — on
null-ish input). One-line change covers both downstream consumers.

Test: `learning-consolidation-date-coercion.test.ts` exercises the full
post-mining half of the loop (the gap Jordi flagged in his release-gate
suggestion). Seeds a `learning:pattern:*` entry with ISO-string
`lastUsedAt`, runs the worker through `getDomainPatterns` →
`consolidatePatterns` → `pruneIneffectivePatterns` → full `doExecute`,
and asserts:
  1. The seam rehydrates `lastSeen` to a real Date.
  2. `consolidatePatterns` completes without throwing.
  3. The recent-high-performer filter math actually counts the pattern
     (not just "doesn't throw" — catches silent-suppression regressions).
  4. `pruneIneffectivePatterns` completes and produces the stale finding.
  5. Full `doExecute` reaches `Learning consolidation complete` and
     `recordLoopHealth(success:true)` — the user-visible signals.

Verified pre-fix with 5/5 failing tests showing the exact error from
the issue; post-fix with 5/5 green, full learning-consolidation suite
(36 tests) green, and `tsc --noEmit` clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-18 07:40:37 +00:00
..