Commit Graph

2 Commits

Author SHA1 Message Date
Dragan Spiridonov b62eb83ad4 fix(learning): rehydrate Date fields at all kv-read seams (#493 follow-up)
#493 was not the last instance of the kv-Date hazard — it was the most
reachable. Audit of the rest of the learning pipeline found 11 more
unprotected kv-read sites where TypeScript declares `Date` but JSON
round-trip through `memory.get` yields an ISO string at runtime.

Two failure modes, both already documented at
learning-coordinator.ts:898-904 (#491 Bug 3):

  1. THROW — `record.someDate.getTime()` raises TypeError. Surfaces
     immediately on the first invocation once any record exists.
  2. SILENT — `timeRange.contains(record.someDate)` or
     `new Date() > record.expiresAt` coerces string vs Date to NaN; all
     comparisons return false; records are silently filtered out or
     expiry is never recognised. No error, just empty/stale analytics.

The silent variant is the worse one — #491 Bug 3 was the same class
and shipped for a release cycle undetected.

Strategic fix: a single `rehydrateDates<T>(record, dateFields[])` helper
at `src/shared/utils/kv-date-rehydrate.ts` applied at every kv-read seam
that touches Date-bearing types. Shallow-copies the record and coerces
named string fields back to Date; pass-through for already-Date, null,
undefined.

Sites patched (11 total):

  src/domains/learning-optimization/services/
    production-intel.ts       — 4 sites (HIGH #2-#5 throw, MEDIUM #8 silent)
                                 getMetricsHistory, getRecentIncidents (×2),
                                 resolveIncident, getRecentMilestones
    metrics-optimizer.ts      — 1 site (HIGH #1 throw)
                                 getMetricsHistory.sort
    learning-coordinator.ts   — 1 site (HIGH #6 throw)
                                 getReplayBuffer.sort
    transfer-specialist.ts    — 4 sites (HIGH #7 throw, MEDIUM #9 silent)
                                 queryKnowledge (×2), getKnowledgeById,
                                 bulkTransfer
  src/domains/learning-optimization/
    coordinator-helpers.ts    — 1 site (SILENT)
                                 getExperiencesForDomain
    coordinator.ts            — 1 site (SILENT, duplicate of above)
                                 getExperiencesForDomain (private)

Sites verified SAFE (kv-read but only access non-Date fields):
  - coordinator.ts:1045/1134/1175 — uses .domain/.confidence/.version
  - metrics-optimizer.ts:676      — uses .metrics map
  - learning-coordinator.ts:379/438/483/547/830/842 — uses
    .confidence/.successRate/.domain/.usageCount
  - learning-coordinator.ts:901 — already has #491 Bug 3 inline
    rehydration with instanceof guard
  - experience-capture.ts:520/563 — TaskExperience uses number
    timestamps, not Date

Sites NOT patched (deferred per scope):
  - coordination/protocols/learning-consolidation.ts — defined but
    never imported anywhere. Dead code; would resurrect the bug if
    reactivated. Documented in the audit but not fixed.

Regression tests (29 new, 6 files):
  tests/unit/shared/utils/kv-date-rehydrate.test.ts             (10)
  tests/unit/domains/learning-optimization/
    production-intel-date-coercion.test.ts                       (5)
    metrics-optimizer-date-coercion.test.ts                      (2)
    learning-coordinator-date-coercion.test.ts                   (2)
    transfer-specialist-date-coercion.test.ts                    (5)
    coordinator-helpers-date-coercion.test.ts                    (2)

Each service test seeds its mock memory backend with kv-shape records
(ISO strings instead of Date instances — matching the post-JSON.parse
shape that better-sqlite3-backed memory yields in production) and
exercises the kv-read → consumer path that would fail pre-fix. Both
throw and silent failure modes are pinned: throws are asserted not to
fire, silent failures are caught by behavioural assertions (e.g. the
expired-knowledge test asserts `expired.score < live.score * 0.5 ±`,
which would fail pre-fix because the 0.5 penalty never triggered).

Verification:
  - 29/29 new regression tests green
  - 135/135 existing tests for patched services green (production-intel,
    metrics-optimizer, transfer-specialist, learning-coordinator,
    learning-consolidation, learning-consolidation-mining,
    learning-consolidation-liveness)
  - `tsc --noEmit` clean
  - `npm run build` produces dist with rehydrateDates at all 17
    expected call sites across the 6 patched files

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-18 08:38:53 +00:00
Profa 2253d05618 feat: flatten project structure — archive v2, promote v3 to root
Remove the nested v3/ subdirectory by moving all active source code,
tests, configs, and assets directly to the project root. Archive v2/
(dead code, 847MB) to v2-archive.tar.gz.

Key changes:
- Move v3/src/ → src/, v3/tests/ → tests/, v3/packages/ → packages/
- Move v3/tsconfig.json, v3/vitest.config.ts to root
- Merge v3/package.json into root package.json (ESM, scripts, deps, exports)
- Merge v3/scripts/ into scripts/, v3/implementation/ into docs/implementation/
- Update all CI workflows to remove "cd v3 &&" and v3/ path prefixes
- Fix all init installer path resolution (3-level-up → 2-level-up)
- Fix hooks to use "node ./dist/cli/bundle.js" instead of bare "aqe"
- Update settings.json ADR/DDD directories to docs/implementation/
- Fix helper scripts (statusline, adr-compliance, ddd-tracker, guidance-hooks)
- Update 58 agent definitions: aqe/v3/ → aqe/ memory namespaces
- Update 15 guidance shards: v3/src/ → src/ path references
- Fix sync interfaces: remove v3/ and v2/ path references
- Fix test path calculations (process.cwd() + '..' no longer needed)
- Fix flaky perf test threshold (500ms → 1000ms for CI environments)
- Fix complexity-analyzer test to match ADR-051 keyword-based eligibility

Build passes, 17,901 tests pass, CLI and MCP bundles verified.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-01 11:35:41 +00:00