From b4e1313b22011f4a2a4b72d619e6cea109d0e574 Mon Sep 17 00:00:00 2001 From: Matthew Raymer Date: Mon, 1 Sep 2025 06:06:00 +0000 Subject: [PATCH] fix(activeDid): implement dual-write pattern with proper MASTER_SETTINGS_KEY usage - Fix $updateActiveDid() to use MASTER_SETTINGS_KEY constant instead of hardcoded "1" - Update migration plan to reflect current state after rollback - Ensure backward compatibility during activeDid migration transition The dual-write pattern now correctly updates both active_identity and settings tables using the proper MASTER_SETTINGS_KEY constant for settings table targeting. --- .cursor/rules/core/less_complex.mdc | 1 + doc/activeDid-migration-plan.md | 60 +++++++++++++---------------- src/utils/PlatformServiceMixin.ts | 3 +- 3 files changed, 30 insertions(+), 34 deletions(-) diff --git a/.cursor/rules/core/less_complex.mdc b/.cursor/rules/core/less_complex.mdc index 6c5ca71d..25e3e3a1 100644 --- a/.cursor/rules/core/less_complex.mdc +++ b/.cursor/rules/core/less_complex.mdc @@ -12,6 +12,7 @@ language: Match repository languages and conventions ## Rules +0. **Principle:** just the facts m'am. 1. **Default to the least complex solution.** Fix the problem directly where it occurs; avoid new layers, indirection, or patterns unless strictly necessary. diff --git a/doc/activeDid-migration-plan.md b/doc/activeDid-migration-plan.md index 23ca3fa9..0b3df4d9 100644 --- a/doc/activeDid-migration-plan.md +++ b/doc/activeDid-migration-plan.md @@ -1,8 +1,8 @@ # ActiveDid Migration Plan - Implementation Guide **Author**: Matthew Raymer -**Date**: 2025-08-31T03:34Z -**Status**: 🎯 **IMPLEMENTATION** - API Layer Incomplete +**Date**: 2025-09-01T05:09:47Z +**Status**: 🎯 **STABILITY** - Rollback Complete, Ready for Implementation ## Objective @@ -29,12 +29,12 @@ Follow this implementation checklist step-by-step to complete the migration. - [x] Include data migration from settings to active_identity table ### Phase 2: API Layer Updates ❌ INCOMPLETE -- [x] Implement `$getActiveIdentity()` method (exists but wrong return type) +- [x] Implement `$getActiveIdentity()` method (exists with correct return type) - [x] Fix `$getActiveIdentity()` return type to match documented interface -- [x] Update `$accountSettings()` to use new method -- [ ] Update `$updateActiveDid()` with dual-write pattern +- [ ] Update `$accountSettings()` to use new method (REVERTED - caused test failures) +- [x] Update `$updateActiveDid()` with dual-write pattern -**Status**: $accountSettings() now uses new API. Method combines settings with activeDid from active_identity table. Need to implement dual-write pattern. +**Status**: $updateActiveDid() now implements dual-write pattern. $accountSettings() reverted to original implementation due to test failures. ### Phase 3: Component Updates ❌ BLOCKED - [ ] Update 35+ components to use `$getActiveIdentity()` @@ -83,10 +83,10 @@ Follow this implementation checklist step-by-step to complete the migration. }, ``` -### 2. Fix $getActiveIdentity() Return Type +### 2. $getActiveIdentity() Method ✅ EXISTS ```typescript -// Update in PlatformServiceMixin.ts - Change return type to match documented interface +// Already exists in PlatformServiceMixin.ts with correct return type async $getActiveIdentity(): Promise<{ activeDid: string }> { try { const result = await this.$dbQuery( @@ -302,46 +302,41 @@ private async initializeSettings() { ## What Works (Evidence) - ✅ **Migration code exists** in MIGRATIONS array - - **Time**: 2025-08-31T03:34Z + - **Time**: 2025-09-01T05:09:47Z - **Evidence**: `src/db-sql/migration.ts:125` - `003_active_did_separate_table` migration defined - **Verify at**: Migration script contains proper table creation and data migration - ✅ **$getActiveIdentity() method exists** in PlatformServiceMixin - - **Time**: 2025-08-31T03:34Z + - **Time**: 2025-09-01T05:09:47Z - **Evidence**: `src/utils/PlatformServiceMixin.ts:555` - Method implemented with correct return type - **Verify at**: Method returns `{ activeDid: string }` as documented - ✅ **Database migration infrastructure** exists and mature - - **Time**: 2025-08-31T03:34Z + - **Time**: 2025-09-01T05:09:47Z - **Evidence**: `src/db-sql/migration.ts:31` - migration system in place - **Verify at**: Existing migration scripts and database versioning ## What Doesn't (Evidence & Hypotheses) -- ✅ **$getActiveIdentity() return type fixed** - now returns `{ activeDid: string }` as documented - - **Time**: 2025-08-31T03:34Z - - **Evidence**: `src/utils/PlatformServiceMixin.ts:555` - Method updated with correct return type - - **Status**: Ready for use in component updates +- ❌ **$accountSettings() reverted** due to test failures + - **Time**: 2025-09-01T05:09:47Z + - **Evidence**: Simplified implementation broke DID retrieval in tests + - **Hypothesis**: Original method handles complex DID-specific settings merging + - **Next probe**: Implement dual-write pattern first, then carefully update $accountSettings -- ✅ **$accountSettings() updated** to use new $getActiveIdentity() method - - **Time**: 2025-08-31T03:34Z - - **Evidence**: `src/utils/PlatformServiceMixin.ts:817` - Method now calls $getActiveIdentity and combines results - - **Status**: Maintains backward compatibility while using new API - -- ❌ **$updateActiveDid() not implemented** with dual-write pattern - - **Time**: 2025-08-31T03:34Z - - **Evidence**: `src/utils/PlatformServiceMixin.ts:200` - Method only updates internal tracking - - **Hypothesis**: Database updates not happening, only in-memory changes - - **Next probe**: Implement dual-write to both active_identity and settings tables +- ✅ **$updateActiveDid() dual-write implemented** + - **Time**: 2025-09-01T05:09:47Z + - **Evidence**: `src/utils/PlatformServiceMixin.ts:220` - Method now updates both active_identity and settings tables + - **Status**: Uses MASTER_SETTINGS_KEY constant for proper settings table targeting - ❌ **35 components still use old pattern** `this.activeDid = settings.activeDid` - - **Time**: 2025-08-31T03:34Z + - **Time**: 2025-09-01T05:09:47Z - **Evidence**: Grep search found 35 instances across views and components - **Hypothesis**: Components need updates but are blocked until API layer is ready - **Next probe**: Update components after API layer is implemented - ❌ **Database state unknown** - IndexedDB database not inspected - - **Time**: 2025-08-31T03:34Z + - **Time**: 2025-09-01T05:09:47Z - **Evidence**: Cannot check IndexedDB database without running application - **Hypothesis**: Migration exists in code but may not have run in IndexedDB - **Next probe**: Start application and check IndexedDB for active_identity table @@ -393,15 +388,14 @@ async function rollbackActiveDidMigration(): Promise { | Task | Exit Criteria | Priority | |------|---------------|----------| -| **Fix $getActiveIdentity() return type** | Method returns `{ activeDid: string }` as documented | ✅ COMPLETE | -| **Update $accountSettings() method** | Method calls $getActiveIdentity and combines with settings | ✅ COMPLETE | -| **Implement $updateActiveDid() dual-write** | Method updates both active_identity and settings tables | 🔴 HIGH | +| **Update $accountSettings() method** | Method calls $getActiveIdentity and combines with settings | 🔴 HIGH (REVERTED) | +| **Implement $updateActiveDid() dual-write** | Method updates both active_identity and settings tables | ✅ COMPLETE | | **Start application in browser** | Application loads and initializes IndexedDB database | 🟡 MEDIUM | | **Inspect IndexedDB via DevTools** | Verify active_identity table exists and contains data | 🟡 MEDIUM | | **Update first component** | One component successfully uses new API pattern | 🟢 LOW | | **Systematic component updates** | All 35 components use new API pattern | 🟢 LOW | -**Critical Blocker**: Need to implement $updateActiveDid() dual-write pattern before component updates can proceed. +**Critical Blocker**: Need to carefully update $accountSettings() without breaking existing functionality. ## Future Improvement: MASTER_SETTINGS_KEY Elimination @@ -434,8 +428,8 @@ async function rollbackActiveDidMigration(): Promise { - **Sign-off checklist**: - [ ] Migration script integrated with existing MIGRATIONS array - [x] $getActiveIdentity() method returns correct type - - [x] $accountSettings() method updated to use new API - - [ ] $updateActiveDid() method implements dual-write pattern + - [ ] $accountSettings() method updated to use new API (REVERTED) + - [x] $updateActiveDid() method implements dual-write pattern - [ ] All 35+ components updated to use new API - [ ] Rollback procedures validated - [ ] All platforms tested diff --git a/src/utils/PlatformServiceMixin.ts b/src/utils/PlatformServiceMixin.ts index 641912f7..e4001f6d 100644 --- a/src/utils/PlatformServiceMixin.ts +++ b/src/utils/PlatformServiceMixin.ts @@ -219,8 +219,9 @@ export const PlatformServiceMixin = { "UPDATE active_identity SET activeDid = ?, lastUpdated = datetime('now') WHERE id = 1", [newDid || ""], ); - await this.$dbExec("UPDATE settings SET activeDid = ? WHERE id = 1", [ + await this.$dbExec("UPDATE settings SET activeDid = ? WHERE id = ?", [ newDid || "", + MASTER_SETTINGS_KEY, ]); logger.debug( `[PlatformServiceMixin] ActiveDid dual-write completed for ${newDid}`,