Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Refactor vault push scope to be determined by the writing repository instead of by caller (#2159)

Leendert de Borst70f65da7474b
diff --git a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
index 125930e1e..48cb0c49b 100644
--- a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
+++ b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts
@@ -19,7 +19,7 @@ import { hasSyncError, syncResult, VaultSync, type FullVaultSyncResult, type Sha
import { type IVaultSyncEngineHost, type VaultSyncOptions, type VaultSyncPhase as EngineSyncPhase, type VaultSyncStoreOutcome, type VaultSyncStoreRequest } from '@aliasvault/client/sync/VaultSyncEngine';
import { getVaultSyncHoldReason } from '@aliasvault/client/sync/VaultSyncHold';
import { base64ToBytes, bytesToBase64 } from '@aliasvault/client/utilities/Base64';
-import { FieldKey, ItemTypes, VaultDataBucketCategory, createSystemField, type Item, type PasswordSettings } from '@aliasvault/models/vault';
+import { FieldKey, ItemTypes, createSystemField, type Item, type PasswordSettings } from '@aliasvault/models/vault';
import { storage } from 'wxt/utils/storage';
import { clearAllSavePromptState } from '@/entrypoints/background/SavePromptStateHandler';
@@ -672,17 +672,17 @@ export async function handleClearPersistedFormValues(): Promise<void> {
* This is tolerant to server being offline (in which case the vault state will be stored locally for next sync).
* @param sqliteClient - the mutated vault
* @param encryptionKey - the key the local blob is stored under
- * @param scope - what the mutation touched; bucket-scoped mutations (e.g. 'Stats') let the sync push just
- * that data bucket instead of the whole manifest. Defaults to a full manifest push.
*/
-async function persistLocalVaultMutation(sqliteClient: SqliteClient, encryptionKey: string, scope?: VaultMutationScope) : Promise<void> {
+async function persistLocalVaultMutation(sqliteClient: SqliteClient, encryptionKey: string) : Promise<void> {
const encryptedVault = await encryptVaultBlob(sqliteClient.exportToBytes(), encryptionKey);
- await handleStoreEncryptedVault({ vaultBlob: encryptedVault, markDirty: true, scope });
-
- /*
- * The stored blob is exactly this client's content, so re-adopt the pair as the cache (the store just cleared
- * it): the sync that follows can then read the vault without another decrypt + database load.
- */
+ const scopes = sqliteClient.takeMutationScopes();
+ try {
+ await handleStoreEncryptedVault({ vaultBlob: encryptedVault, markDirty: true, scopes });
+ } catch (error) {
+ // The write is still in the local database, so put the scopes back for whichever persist carries it next.
+ scopes.forEach(scope => sqliteClient.recordMutationScope(scope));
+ throw error;
+ }
cachedSqliteClient = sqliteClient;
cachedVaultBlob = encryptedVault;
@@ -753,7 +753,7 @@ export async function handleStoreEncryptedVault(request: {
vaultBlob: string;
markDirty?: boolean;
expectedMutationSeq?: number;
- scope?: VaultMutationScope;
+ scopes?: VaultMutationScope[];
}): Promise<{ success: boolean; mutationSequence: number }> {
let mutationSequence = await storage.getItem(StorageKeys.MUTATION_SEQUENCE) as number | null ?? 0;
@@ -770,8 +770,12 @@ export async function handleStoreEncryptedVault(request: {
mutationSequence++;
}
- // Track what changed so the next sync can choose a cheap bucket-only push (e.g. a settings toggle) over a full manifest push.
- const dirtyScopeKey = dirtyScopeStorageKey(request.scope ?? DEFAULT_VAULT_MUTATION_SCOPE);
+ /*
+ * Track what changed so the next sync can choose a cheap bucket-only push (e.g. a settings toggle) over a
+ * full manifest push. A store that names no scope changed something the repositories do not account for
+ * (a raw statement, a migration, a merged vault), which only a full manifest push covers.
+ */
+ const dirtyScopes = request.scopes?.length ? request.scopes : [DEFAULT_VAULT_MUTATION_SCOPE];
// Build items to store.
if (request.markDirty) {
@@ -779,7 +783,7 @@ export async function handleStoreEncryptedVault(request: {
{ key: StorageKeys.ENCRYPTED_VAULT, value: request.vaultBlob },
{ key: StorageKeys.MUTATION_SEQUENCE, value: mutationSequence },
{ key: StorageKeys.IS_DIRTY, value: true },
- { key: dirtyScopeKey, value: true }
+ ...dirtyScopes.map(scope => ({ key: dirtyScopeStorageKey(scope), value: true }))
]);
} else {
await storage.setItem(StorageKeys.ENCRYPTED_VAULT, request.vaultBlob);
@@ -1456,7 +1460,7 @@ export async function handleRecordItemUsage(
return { success: false };
}
- await persistLocalVaultMutation(sqliteClient, encryptionKey, VaultDataBucketCategory.Stats);
+ await persistLocalVaultMutation(sqliteClient, encryptionKey);
return { success: true };
} catch (error) {
console.error('Failed to record item usage:', error);
diff --git a/apps/browser-extension/src/entrypoints/popup/hooks/useVaultMutate.ts b/apps/browser-extension/src/entrypoints/popup/hooks/useVaultMutate.ts
index a94b09b66..29a5d7b88 100644
--- a/apps/browser-extension/src/entrypoints/popup/hooks/useVaultMutate.ts
+++ b/apps/browser-extension/src/entrypoints/popup/hooks/useVaultMutate.ts
@@ -1,5 +1,5 @@
import { encryptVaultBlob } from '@aliasvault/client/crypto/VaultBlob';
-import { isSilentScope, type VaultMutationOptions, type VaultMutationScope } from '@aliasvault/client/sync/VaultMutationScope';
+import { hasUserVisibleScope, type VaultMutationScope } from '@aliasvault/client/sync/VaultMutationScope';
import { hasSyncError } from '@aliasvault/client/sync/VaultSync';
import { useCallback, useRef } from 'react';
@@ -25,7 +25,7 @@ import { sendMessage } from '@/utils/messaging/ExtensionMessaging';
* even if the popup closes. This ensures vault changes are always synced to the server.
*/
export function useVaultMutate(): {
- executeVaultMutationAsync: (operation: () => Promise<void>, options?: VaultMutationOptions) => Promise<void>;
+ executeVaultMutationAsync: (operation: () => Promise<void>) => Promise<void>;
} {
const dbContext = useDb();
const pollIntervalRef = useRef<NodeJS.Timeout | null>(null);
@@ -33,28 +33,43 @@ export function useVaultMutate(): {
/**
* Execute the provided operation and save locally.
* Atomically increments mutation sequence and marks dirty.
+ * @returns The scopes the operation wrote into
*/
- const saveLocally = useCallback(async (operation: () => Promise<void>, scope?: VaultMutationScope): Promise<void> => {
+ const saveLocally = useCallback(async (operation: () => Promise<void>): Promise<VaultMutationScope[]> => {
// Execute the provided operation (e.g. create/update/delete credential)
await operation();
- // Export and encrypt the updated vault
- const encryptionKey = await sendMessage('GET_ENCRYPTION_KEY') as string;
- const encryptedVaultBlob = await encryptVaultBlob(dbContext.sqliteClient!.exportToBytes(), encryptionKey);
-
/*
- * Store the updated vault locally, mark dirty, increment mutation sequence. The scope records what
- * changed: bucket-scoped mutations (e.g. 'Settings') let the background sync push just that data bucket
- * instead of the full vault manifest.
+ * Take what the operation wrote into, as recorded by the repositories it ran through. A mutation that
+ * only touched a bucket-scoped repository (e.g. 'Settings') lets the background sync push just that data
+ * bucket instead of the full vault manifest.
*/
- await sendMessage('STORE_ENCRYPTED_VAULT', {
- vaultBlob: encryptedVaultBlob,
- markDirty: true,
- scope
- });
+ const scopes = dbContext.sqliteClient!.takeMutationScopes();
+
+ try {
+ // Export and encrypt the updated vault
+ const encryptionKey = await sendMessage('GET_ENCRYPTION_KEY') as string;
+ const encryptedVaultBlob = await encryptVaultBlob(dbContext.sqliteClient!.exportToBytes(), encryptionKey);
+
+ // Store the updated vault locally, mark dirty, increment mutation sequence.
+ await sendMessage('STORE_ENCRYPTED_VAULT', {
+ vaultBlob: encryptedVaultBlob,
+ markDirty: true,
+ scopes
+ });
+ } catch (error) {
+ /*
+ * Storing failed, but the write itself is still in the local database and the next mutation that does
+ * reach storage carries it along. Put the scopes back, or that mutation could store the change under a
+ * bucket scope alone and a bucket-only push would leave it behind.
+ */
+ scopes.forEach(scope => dbContext.sqliteClient!.recordMutationScope(scope));
+ throw error;
+ }
// Refresh the sync state in React
await dbContext.refreshSyncState();
+ return scopes;
}, [dbContext]);
/**
@@ -111,13 +126,13 @@ export function useVaultMutate(): {
* and continues even if the popup closes.
*
* Always polls to detect completion since background sync may queue additional
- * syncs that we cannot directly observe from the popup context. A silent scope
- * (e.g. item usage statistics) syncs the same way but shows no indicator: the
- * user did not ask for that write and expects no feedback on it.
- * @param scope - what the mutation touched
+ * syncs that we cannot directly observe from the popup context. A mutation that
+ * only wrote into silent scopes (e.g. item usage statistics) syncs the same way but
+ * shows no indicator: the user did not ask for that write and expects no feedback on it.
+ * @param scopes - what the mutation wrote into
*/
- const triggerBackgroundSync = useCallback((scope?: VaultMutationScope): void => {
- const silent = scope !== undefined && isSilentScope(scope);
+ const triggerBackgroundSync = useCallback((scopes: VaultMutationScope[]): void => {
+ const silent = !hasUserVisibleScope(scopes);
if (!silent) {
dbContext.setIsUploading(true);
@@ -160,18 +175,15 @@ export function useVaultMutate(): {
/**
* Execute a vault mutation asynchronously: save locally immediately, then
* trigger sync in background. This doesn't block the UI.
- * Pass `options.scope` for mutations that only touch bucket-scoped data (e.g. 'Settings') so the sync
- * pushes just that data bucket instead of the full vault.
+ * What the mutation touched follows from the repositories it wrote through, so a mutation that only writes
+ * bucket-scoped data (e.g. through `settings`) syncs as a bucket push without saying so here.
*/
- const executeVaultMutationAsync = useCallback(async (
- operation: () => Promise<void>,
- options?: VaultMutationOptions
- ): Promise<void> => {
+ const executeVaultMutationAsync = useCallback(async (operation: () => Promise<void>): Promise<void> => {
// 1. Execute mutation and save locally (fast, doesn't block)
- await saveLocally(operation, options?.scope);
+ const scopes = await saveLocally(operation);
// 2. Trigger sync in background (fire-and-forget, continues even if popup closes)
- triggerBackgroundSync(options?.scope);
+ triggerBackgroundSync(scopes);
}, [saveLocally, triggerBackgroundSync]);
return {
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx b/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx
index 56ede6bf1..7c18cbd42 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/items/ItemsList.tsx
@@ -883,7 +883,7 @@ const ItemsList: React.FC = () => {
// Save to settings and trigger vault sync
await executeVaultMutationAsync(async () => {
dbContext.sqliteClient?.settings.setCredentialsSortOrder(option.value);
- }, { scope: 'Settings' });
+ });
}}
className={`w-full text-left px-4 py-2 text-sm hover:bg-gray-100 dark:hover:bg-gray-700 flex items-center gap-2 ${
sortOrder === option.value ? 'text-orange-600 dark:text-orange-400' : 'text-gray-700 dark:text-gray-300'
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/settings/IdentityGeneratorSettings.tsx b/apps/browser-extension/src/entrypoints/popup/pages/settings/IdentityGeneratorSettings.tsx
index f6832806a..0de69103a 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/settings/IdentityGeneratorSettings.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/settings/IdentityGeneratorSettings.tsx
@@ -69,7 +69,7 @@ const IdentityGeneratorSettings: React.FC = () => {
if (dbContext?.sqliteClient) {
await executeVaultMutationAsync(async () => {
dbContext.sqliteClient!.settings.updateSetting('DefaultIdentityLanguage', newLanguage);
- }, { scope: 'Settings' });
+ });
}
}, [dbContext?.sqliteClient, executeVaultMutationAsync]);
@@ -81,7 +81,7 @@ const IdentityGeneratorSettings: React.FC = () => {
if (dbContext?.sqliteClient) {
await executeVaultMutationAsync(async () => {
dbContext.sqliteClient!.settings.updateSetting('DefaultIdentityGender', newGender);
- }, { scope: 'Settings' });
+ });
}
}, [dbContext?.sqliteClient, executeVaultMutationAsync]);
@@ -93,7 +93,7 @@ const IdentityGeneratorSettings: React.FC = () => {
if (dbContext?.sqliteClient) {
await executeVaultMutationAsync(async () => {
dbContext.sqliteClient!.settings.updateSetting('DefaultIdentityAgeRange', newAgeRange);
- }, { scope: 'Settings' });
+ });
}
}, [dbContext?.sqliteClient, executeVaultMutationAsync]);
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/settings/PasswordGeneratorSettings.tsx b/apps/browser-extension/src/entrypoints/popup/pages/settings/PasswordGeneratorSettings.tsx
index e71227054..97c744bec 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/settings/PasswordGeneratorSettings.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/settings/PasswordGeneratorSettings.tsx
@@ -107,7 +107,7 @@ const PasswordGeneratorSettings: React.FC = () => {
if (dbContext?.sqliteClient) {
void executeVaultMutationAsync(async () => {
dbContext.sqliteClient!.settings.setPasswordSettings(next);
- }, { scope: 'Settings' });
+ });
}
}, [dbContext?.sqliteClient, executeVaultMutationAsync]);
diff --git a/apps/browser-extension/src/utils/messaging/ExtensionMessaging.ts b/apps/browser-extension/src/utils/messaging/ExtensionMessaging.ts
index a189d533e..588bfae24 100644
--- a/apps/browser-extension/src/utils/messaging/ExtensionMessaging.ts
+++ b/apps/browser-extension/src/utils/messaging/ExtensionMessaging.ts
@@ -115,7 +115,7 @@ export interface IExtensionMessageProtocol {
SET_CLIPBOARD_CLEAR_TIMEOUT(data: number): boolean;
SET_LOGIN_SAVE_ENABLED(data: boolean): BoolResponse;
SET_RECENTLY_SELECTED(data: { itemId: string; domain: string }): { success: boolean };
- STORE_ENCRYPTED_VAULT(data: { vaultBlob: string; markDirty?: boolean; expectedMutationSeq?: number; scope?: VaultMutationScope }): { success: boolean; mutationSequence: number };
+ STORE_ENCRYPTED_VAULT(data: { vaultBlob: string; markDirty?: boolean; expectedMutationSeq?: number; scopes?: VaultMutationScope[] }): { success: boolean; mutationSequence: number };
STORE_UNLOCK_KEY(data: string): BoolResponse;
STORE_UNLOCK_KEY_DERIVATION_PARAMS(data: UnlockKeyDerivationParams): BoolResponse;
STORE_LAST_AUTOFILLED(data: LastAutofilledCredential): { success: boolean };
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.kt
index 2880ba19d..82aae5459 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.kt
@@ -2040,8 +2040,11 @@ class NativeVaultManager(reactContext: ReactApplicationContext) :
override fun getSyncState(promise: Promise) {
try {
val syncState = vaultStore.getSyncState()
+ val dirtyScopes = Arguments.createArray()
+ syncState.dirtyScopes.forEach { dirtyScopes.pushString(it) }
val result = Arguments.createMap()
result.putBoolean("isDirty", syncState.isDirty)
+ result.putArray("dirtyScopes", dirtyScopes)
result.putInt("mutationSequence", syncState.mutationSequence)
result.putInt("serverRevision", syncState.serverRevision)
result.putBoolean("isSyncing", syncState.isSyncing)
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultMetadataManager.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultMetadataManager.kt
index ea3c082b3..8e5ccb2f0 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultMetadataManager.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultMetadataManager.kt
@@ -249,6 +249,7 @@ class VaultMetadataManager(
fun getSyncState(): SyncState {
return SyncState(
isDirty = getIsDirty(),
+ dirtyScopes = getDirtyScopes(),
mutationSequence = getMutationSequence(),
serverRevision = getVaultRevisionNumber(),
isSyncing = getIsSyncing(),
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/models/SyncState.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/models/SyncState.kt
index 5a2412fc7..691bf4509 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/models/SyncState.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/models/SyncState.kt
@@ -9,6 +9,11 @@ data class SyncState(
*/
val isDirty: Boolean = false,
+ /**
+ * What the pending changes touched, so a caller can tell a change the user made from a silent one.
+ */
+ val dirtyScopes: List<String> = emptyList(),
+
/**
* Counter that increments on each local mutation.
* Used for race detection during sync operations.
diff --git a/apps/mobile-app/components/ServerSyncIndicator.tsx b/apps/mobile-app/components/ServerSyncIndicator.tsx
index a4a79725e..ce5792e09 100644
--- a/apps/mobile-app/components/ServerSyncIndicator.tsx
+++ b/apps/mobile-app/components/ServerSyncIndicator.tsx
@@ -194,7 +194,7 @@ export function ServerSyncIndicator(): React.ReactNode {
) : (
<>
<Ionicons name="cloud-offline" size={18} color={colors.warning} />
- {dbContext.isDirty && (
+ {dbContext.hasUnsyncedUserChanges && (
<View style={styles.badge} />
)}
</>
@@ -233,7 +233,7 @@ export function ServerSyncIndicator(): React.ReactNode {
}
// Priority 4: Pending indicator (tappable to force sync)
- if (dbContext.isDirty) {
+ if (dbContext.hasUnsyncedUserChanges) {
return (
<RobustPressable
style={[styles.container, styles.pending]}
diff --git a/apps/mobile-app/context/DbContext.tsx b/apps/mobile-app/context/DbContext.tsx
index 13384bb23..a59e9ef7b 100644
--- a/apps/mobile-app/context/DbContext.tsx
+++ b/apps/mobile-app/context/DbContext.tsx
@@ -1,6 +1,7 @@
import React, { createContext, useContext, useState, useEffect, useCallback, useMemo } from 'react';
import type { UnlockKeyDerivationParams, VaultMetadata } from '@aliasvault/models/metadata';
+import { hasUserVisibleScope, type VaultMutationScope } from '@aliasvault/client/sync/VaultMutationScope';
import EncryptionUtility from '@/utils/EncryptionUtility';
import SqliteClient from '@/utils/SqliteClient';
@@ -12,6 +13,12 @@ type DbContextType = {
dbAvailable: boolean;
// Sync state tracking
isDirty: boolean;
+ /**
+ * Whether the pending changes are worth telling the user about. A vault that is only dirty from silent
+ * scopes (e.g. item usage statistics recorded while autofilling) syncs like any other but reports false
+ * here, so the UI stays quiet about writes the user never asked for.
+ */
+ hasUnsyncedUserChanges: boolean;
isSyncing: boolean;
isUploading: boolean;
isOffline: boolean;
@@ -64,6 +71,11 @@ export const DbProvider: React.FC<{ children: React.ReactNode }> = ({ children }
*/
const [isDirty, setIsDirty] = useState(false);
+ /**
+ * Sync state tracking - the scopes those local changes belong to, which decides what the UI shows.
+ */
+ const [dirtyScopes, setDirtyScopes] = useState<VaultMutationScope[]>([]);
+
/**
* Sync state tracking - isSyncing indicates a download sync operation is in progress.
*/
@@ -202,6 +214,7 @@ export const DbProvider: React.FC<{ children: React.ReactNode }> = ({ children }
const syncState = await NativeVaultManager.getSyncState();
const offline = await NativeVaultManager.getOfflineMode();
setIsDirty(syncState.isDirty);
+ setDirtyScopes(syncState.dirtyScopes as VaultMutationScope[]);
setIsOfflineState(offline);
} catch (error) {
console.error('Failed to refresh sync state:', error);
@@ -304,6 +317,7 @@ export const DbProvider: React.FC<{ children: React.ReactNode }> = ({ children }
dbAvailable,
// Sync state
isDirty,
+ hasUnsyncedUserChanges: isDirty && hasUserVisibleScope(dirtyScopes),
isSyncing,
isUploading,
isOffline,
@@ -323,7 +337,7 @@ export const DbProvider: React.FC<{ children: React.ReactNode }> = ({ children }
storeUnlockKeyDerivationParams,
checkStoredVault,
setDatabaseAvailable,
- }), [sqliteClient, dbInitialized, dbAvailable, isDirty, isSyncing, isUploading, isOffline, setIsSyncing, setIsUploading, setIsOffline, shouldSuppressEmailErrors, refreshSyncState, requiresLegacySqliteBlobMigration, hasPendingMigrations, clearDatabase, getVaultMetadata, testDatabaseConnection, verifyUnlockKey, unlockVault, storeUnlockKey, storeUnlockKeyDerivationParams, checkStoredVault, setDatabaseAvailable]);
+ }), [sqliteClient, dbInitialized, dbAvailable, isDirty, dirtyScopes, isSyncing, isUploading, isOffline, setIsSyncing, setIsUploading, setIsOffline, shouldSuppressEmailErrors, refreshSyncState, requiresLegacySqliteBlobMigration, hasPendingMigrations, clearDatabase, getVaultMetadata, testDatabaseConnection, verifyUnlockKey, unlockVault, storeUnlockKey, storeUnlockKeyDerivationParams, checkStoredVault, setDatabaseAvailable]);
return (
<DbContext.Provider value={contextValue}>
diff --git a/apps/mobile-app/hooks/useLogout.ts b/apps/mobile-app/hooks/useLogout.ts
index 8b315ee76..19c29b43c 100644
--- a/apps/mobile-app/hooks/useLogout.ts
+++ b/apps/mobile-app/hooks/useLogout.ts
@@ -26,7 +26,7 @@ type UseLogoutReturn = {
/**
* Hook for handling logout across the app.
* Provides consistent logout behavior with:
- * - Warning about unsynced changes (isDirty check)
+ * - Warning about unsynced changes the user made (silent scopes should not trigger this warning)
* - Confirmation dialog for user-initiated logout
* - Token revocation
* - Complete auth and vault data clearance
@@ -45,7 +45,7 @@ type UseLogoutReturn = {
export function useLogout(): UseLogoutReturn {
const { t } = useTranslation();
const { clearAuthUserInitiated } = useAuth();
- const { isDirty } = useDb();
+ const { hasUnsyncedUserChanges } = useDb();
const { showConfirm } = useDialog();
const webApi = useWebApi();
@@ -77,7 +77,7 @@ export function useLogout(): UseLogoutReturn {
* Shows warning if there are unsynced changes, otherwise shows normal confirmation.
*/
const logoutUserInitiated = useCallback(async (): Promise<void> => {
- if (isDirty) {
+ if (hasUnsyncedUserChanges) {
// Show warning about unsynced changes
showConfirm(
t('logout.unsyncedChangesTitle'),
@@ -96,7 +96,7 @@ export function useLogout(): UseLogoutReturn {
{ confirmStyle: 'destructive' }
);
}
- }, [isDirty, showConfirm, t, performLogout]);
+ }, [hasUnsyncedUserChanges, showConfirm, t, performLogout]);
return {
logoutUserInitiated,
diff --git a/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift b/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift
index acceb0a27..804f1cb4a 100644
--- a/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift
+++ b/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift
@@ -1455,6 +1455,7 @@ public class VaultManager: NSObject {
let syncState = vaultStore.getSyncState()
let result: [String: Any] = [
"isDirty": syncState.isDirty,
+ "dirtyScopes": syncState.dirtyScopes,
"mutationSequence": syncState.mutationSequence,
"serverRevision": syncState.serverRevision,
"isSyncing": syncState.isSyncing
diff --git a/apps/mobile-app/ios/VaultStoreKit/Models/SyncModels.swift b/apps/mobile-app/ios/VaultStoreKit/Models/SyncModels.swift
index 2a22593e2..9ce03d56b 100644
--- a/apps/mobile-app/ios/VaultStoreKit/Models/SyncModels.swift
+++ b/apps/mobile-app/ios/VaultStoreKit/Models/SyncModels.swift
@@ -3,12 +3,14 @@ import Foundation
/// The vault's local sync bookkeeping.
public struct SyncStateResult {
public let isDirty: Bool
+ public let dirtyScopes: [String]
public let mutationSequence: Int
public let serverRevision: Int
public let isSyncing: Bool
- public init(isDirty: Bool, mutationSequence: Int, serverRevision: Int, isSyncing: Bool) {
+ public init(isDirty: Bool, dirtyScopes: [String] = [], mutationSequence: Int, serverRevision: Int, isSyncing: Bool) {
self.isDirty = isDirty
+ self.dirtyScopes = dirtyScopes
self.mutationSequence = mutationSequence
self.serverRevision = serverRevision
self.isSyncing = isSyncing
diff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Metadata.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Metadata.swift
index 0f4f5f2a3..239644545 100644
--- a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Metadata.swift
+++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Metadata.swift
@@ -210,6 +210,7 @@ extension VaultStore {
public func getSyncState() -> SyncStateResult {
return SyncStateResult(
isDirty: getIsDirty(),
+ dirtyScopes: getDirtyScopes(),
mutationSequence: getMutationSequence(),
serverRevision: getCurrentVaultRevisionNumber(),
isSyncing: getIsSyncing()
diff --git a/apps/mobile-app/specs/NativeVaultManager.ts b/apps/mobile-app/specs/NativeVaultManager.ts
index 56a1fa1b0..f080f9bc7 100644
--- a/apps/mobile-app/specs/NativeVaultManager.ts
+++ b/apps/mobile-app/specs/NativeVaultManager.ts
@@ -48,7 +48,7 @@ export interface Spec extends TurboModule {
getVaultSyncLogs(): Promise<string>;
// Sync state management
- getSyncState(): Promise<{isDirty: boolean; mutationSequence: number; serverRevision: number; isSyncing: boolean}>;
+ getSyncState(): Promise<{isDirty: boolean; dirtyScopes: string[]; mutationSequence: number; serverRevision: number; isSyncing: boolean}>;
markVaultClean(mutationSeqAtStart: number, newServerRevision: number): Promise<boolean>;
clearEncryptedVaultForFreshDownload(): Promise<void>;
diff --git a/core/client/src/database/BaseRepository.ts b/core/client/src/database/BaseRepository.ts
index 7f408106e..209fde8c4 100644
--- a/core/client/src/database/BaseRepository.ts
+++ b/core/client/src/database/BaseRepository.ts
@@ -1,3 +1,4 @@
+import { DEFAULT_VAULT_MUTATION_SCOPE } from '../sync/VaultMutationScope';
import * as dateFormatter from '../utilities/DateFormatter';
import { runAsync } from './DbOp';
@@ -24,6 +25,7 @@ export interface IDatabaseClient {
isInTransaction(): boolean;
getActiveManifestId(): string | null;
getPersonalManifestId(): string | null | Promise<string | null>;
+ recordMutationScope?(scope: VaultMutationScope): void;
}
/**
@@ -42,12 +44,26 @@ export interface ISyncDatabaseClient extends IDatabaseClient {
* Base repository class with common database operations.
*/
export abstract class BaseRepository {
+ /**
+ * What this repository's writes touch. Set by the wrapper that binds the repository to a client, and read
+ * back on every write so that async methods report the same scope the wrapper does.
+ */
+ private mutationScope: VaultMutationScope = DEFAULT_VAULT_MUTATION_SCOPE;
+
/**
* Constructor for the BaseRepository class.
* @param client - The database client to use for the repository
*/
public constructor(protected client: IDatabaseClient) {}
+ /**
+ * Tell this repository which mutation scope its writes belong to.
+ * @param scope - The scope
+ */
+ public setMutationScope(scope: VaultMutationScope): void {
+ this.mutationScope = scope;
+ }
+
/**
* Run a SELECT and return its rows.
* @param sql - The statement
@@ -149,7 +165,7 @@ export abstract class BaseRepository {
* @returns The op's result
*/
protected run<T>(op: DbOp<T>): Promise<T> {
- return runAsync(op, this.client);
+ return runAsync(op, this.client, this.mutationScope);
}
/**
@@ -162,7 +178,7 @@ export abstract class BaseRepository {
await this.client.beginTransaction();
try {
const result = await fn();
- await this.client.commitTransaction();
+ await this.client.commitTransaction(this.mutationScope);
return result;
} catch (error) {
await this.client.rollbackTransaction();
diff --git a/core/client/src/database/DbOp.ts b/core/client/src/database/DbOp.ts
index 4c5992544..c93fd3529 100644
--- a/core/client/src/database/DbOp.ts
+++ b/core/client/src/database/DbOp.ts
@@ -1,3 +1,5 @@
+import { DEFAULT_VAULT_MUTATION_SCOPE } from '../sync/VaultMutationScope';
+
import type { IDatabaseClient, ISyncDatabaseClient, SqliteBindValue } from './BaseRepository';
import type { VaultMutationScope } from '../sync/VaultMutationScope';
@@ -27,6 +29,13 @@ export type DbEffect =
*/
export type DbOp<T> = Generator<DbEffect, T, unknown>;
+/**
+ * A repository that can be told which mutation scope its writes belong to (every BaseRepository can).
+ */
+type ScopedRepository = {
+ setMutationScope?(scope: VaultMutationScope): void;
+};
+
/**
* A repository as a synchronous host sees it: DbOp methods return their value, everything else is unchanged.
*/
@@ -42,24 +51,40 @@ export type AsyncRepository<R> = {
};
/**
- * Run an op to completion on a synchronous client.
+ * Run an op to completion on a synchronous client. An op that writes records its scope on the client; a
+ * read-only op records nothing.
* @param op - The op to run
* @param client - The client that performs its steps
+ * @param scope - What the op's writes touch. Defaults to a full-manifest change.
* @returns The op's result
*/
-export function runSync<T>(op: DbOp<T>, client: ISyncDatabaseClient): T {
- let step = op.next();
- while (!step.done) {
- let result: unknown;
- try {
- result = performSync(step.value, client);
- } catch (error) {
- step = op.throw(error);
- continue;
+export function runSync<T>(op: DbOp<T>, client: ISyncDatabaseClient, scope?: VaultMutationScope): T {
+ let wrote = false;
+ try {
+ let step = op.next();
+ while (!step.done) {
+ let result: unknown;
+ try {
+ if (step.value.kind === 'execute') {
+ wrote = true;
+ }
+ result = performSync(step.value, client);
+ } catch (error) {
+ step = op.throw(error);
+ continue;
+ }
+ step = op.next(result);
+ }
+ return step.value;
+ } finally {
+ /*
+ * Recorded even when the op threw. The synchronous path runs no transaction of its own, so a write that
+ * already happened stays in the database and still has to reach the server.
+ */
+ if (wrote) {
+ client.recordMutationScope?.(scope ?? DEFAULT_VAULT_MUTATION_SCOPE);
}
- step = op.next(result);
}
- return step.value;
}
/**
@@ -72,14 +97,18 @@ export function runSync<T>(op: DbOp<T>, client: ISyncDatabaseClient): T {
*/
export async function runAsync<T>(op: DbOp<T>, client: IDatabaseClient, scope?: VaultMutationScope): Promise<T> {
let ownsTransaction = false;
+ let wrote = false;
try {
let step = op.next();
while (!step.done) {
let result: unknown;
try {
- if (client.persistsOnCommit && step.value.kind === 'execute' && !ownsTransaction && !client.isInTransaction()) {
- await client.beginTransaction();
- ownsTransaction = true;
+ if (step.value.kind === 'execute') {
+ wrote = true;
+ if (client.persistsOnCommit && !ownsTransaction && !client.isInTransaction()) {
+ await client.beginTransaction();
+ ownsTransaction = true;
+ }
}
result = await performAsync(step.value, client);
} catch (error) {
@@ -103,6 +132,11 @@ export async function runAsync<T>(op: DbOp<T>, client: IDatabaseClient, scope?:
}
}
throw error;
+ } finally {
+ // Only a client that persists nothing per write records here; one that persists on commit got the scope there.
+ if (wrote) {
+ client.recordMutationScope?.(scope ?? DEFAULT_VAULT_MUTATION_SCOPE);
+ }
}
}
@@ -110,10 +144,12 @@ export async function runAsync<T>(op: DbOp<T>, client: IDatabaseClient, scope?:
* Wrap a repository for a synchronous client.
* @param repository - The repository
* @param client - The client its ops run on
+ * @param scope - What this repository's writes touch; a bucket-scoped repository lets the sync push just that
+ * data bucket instead of the full vault manifest. Defaults to a full-manifest change.
* @returns The repository with its DbOp methods returning plain values
*/
-export function syncRepository<R extends object>(repository: R, client: ISyncDatabaseClient): SyncRepository<R> {
- return bindRepository(repository, (op) => runSync(op, client)) as SyncRepository<R>;
+export function syncRepository<R extends object>(repository: R, client: ISyncDatabaseClient, scope?: VaultMutationScope): SyncRepository<R> {
+ return bindRepository(scopeRepository(repository, scope), (op) => runSync(op, client, scope)) as SyncRepository<R>;
}
/**
@@ -125,7 +161,21 @@ export function syncRepository<R extends object>(repository: R, client: ISyncDat
* @returns The repository with its DbOp methods returning Promises
*/
export function asyncRepository<R extends object>(repository: R, client: IDatabaseClient, scope?: VaultMutationScope): AsyncRepository<R> {
- return bindRepository(repository, (op) => runAsync(op, client, scope)) as AsyncRepository<R>;
+ return bindRepository(scopeRepository(repository, scope), (op) => runAsync(op, client, scope)) as AsyncRepository<R>;
+}
+
+/**
+ * Tell a repository which scope its writes belong to, so the methods it runs itself (the async ones, which
+ * never pass through the wrapper below) report the same scope as the ops the wrapper runs.
+ * @param repository - The repository
+ * @param scope - The scope, or undefined to leave the repository on its default
+ * @returns The same repository
+ */
+function scopeRepository<R extends object>(repository: R, scope?: VaultMutationScope): R {
+ if (scope) {
+ (repository as ScopedRepository).setMutationScope?.(scope);
+ }
+ return repository;
}
/**
diff --git a/core/client/src/database/SqliteClient.ts b/core/client/src/database/SqliteClient.ts
index 3f18e5864..442064719 100644
--- a/core/client/src/database/SqliteClient.ts
+++ b/core/client/src/database/SqliteClient.ts
@@ -1,3 +1,4 @@
+import { VaultDataBucketCategory } from '@aliasvault/models/vault';
import { VaultSqlGenerator, checkVersionCompatibility, extractVersionFromMigrationId } from '@aliasvault/vault';
import { VaultVersionIncompatibleError } from '../api/errors/VaultVersionIncompatibleError';
@@ -22,6 +23,7 @@ import {
import type { ISyncDatabaseClient, SqliteBindValue } from './BaseRepository';
import type { SyncRepository } from './DbOp';
import type { ISqliteDatabase } from '../platform/SqliteEngine';
+import type { VaultMutationScope } from '../sync/VaultMutationScope';
import type { VaultVersion } from '@aliasvault/vault';
/** Minimum number of free pages before a VACUUM is worth the full database rewrite on export. */
@@ -38,6 +40,12 @@ export class SqliteClient implements ISyncDatabaseClient {
private db: ISqliteDatabase | null = null;
private transactionOpen: boolean = false;
+ /**
+ * The mutation scopes written into since the host last drained them. Writes on this client persist nothing
+ * by themselves: the host exports the whole database afterwards, and reads this to know what changed.
+ */
+ private readonly pendingMutationScopes = new Set<VaultMutationScope>();
+
/**
* The manifest this client writes new rows into, when the user has switched to one explicitly.
* Null means "not chosen" and the user's personal manifest applies.
@@ -102,7 +110,7 @@ export class SqliteClient implements ISyncDatabaseClient {
*/
public get itemStats(): SyncRepository<ItemStatsRepository> {
if (!this._itemStats) {
- this._itemStats = syncRepository(new ItemStatsRepository(this), this);
+ this._itemStats = syncRepository(new ItemStatsRepository(this), this, VaultDataBucketCategory.Stats);
}
return this._itemStats;
}
@@ -132,7 +140,7 @@ export class SqliteClient implements ISyncDatabaseClient {
*/
public get settings(): SyncRepository<SettingsRepository> {
if (!this._settings) {
- this._settings = syncRepository(new SettingsRepository(this), this);
+ this._settings = syncRepository(new SettingsRepository(this), this, VaultDataBucketCategory.Settings);
}
return this._settings;
}
@@ -257,6 +265,27 @@ export class SqliteClient implements ISyncDatabaseClient {
}
}
+ /**
+ * Record that a repository wrote into a mutation scope.
+ * @param scope - The scope that was written into
+ */
+ public recordMutationScope(scope: VaultMutationScope): void {
+ this.pendingMutationScopes.add(scope);
+ }
+
+ /**
+ * Take the scopes written into since the last drain, and clear them. Called by the host right after the
+ * operation it is about to persist, so the sync knows whether a bucket-only push covers the change. A host
+ * whose persist then fails puts them back through {@link recordMutationScope}: the write stays in the
+ * database either way, and an unrecorded scope can strand it behind a later bucket-only push.
+ * @returns The scopes written into, empty when nothing was written
+ */
+ public takeMutationScopes(): VaultMutationScope[] {
+ const scopes = [...this.pendingMutationScopes];
+ this.pendingMutationScopes.clear();
+ return scopes;
+ }
+
/**
* Rollback the current transaction.
*/
diff --git a/core/client/src/database/__tests__/MutationScopes.test.ts b/core/client/src/database/__tests__/MutationScopes.test.ts
new file mode 100644
index 000000000..a16b3ab34
--- /dev/null
+++ b/core/client/src/database/__tests__/MutationScopes.test.ts
@@ -0,0 +1,73 @@
+import { ItemTypes, VaultDataBucketCategory } from '@aliasvault/models/vault';
+import { VaultSqlGenerator } from '@aliasvault/vault';
+import { beforeEach, describe, expect, it } from 'vitest';
+
+import { StorageKeys } from '../../constants/StorageKeys';
+import { getPlatform } from '../../platform/ClientPlatform';
+import { SqliteClient } from '../SqliteClient';
+
+import type { DraftItem } from '../ItemRef';
+
+const PERSONAL = '11111111-1111-4111-8111-111111111111';
+
+/**
+ * A client over an empty vault on the real schema, with the repositories wired as the hosts get them.
+ * @returns The opened client
+ */
+async function openClient(): Promise<SqliteClient> {
+ const db = await getPlatform().sqlite.open();
+ db.exec(new VaultSqlGenerator().getCompleteSchemaSql());
+ const bytes = db.export();
+
+ await getPlatform().storage.set(StorageKeys.VAULT_PERSONAL_MANIFEST_ID, PERSONAL);
+ const client = new SqliteClient();
+ await client.initializeFromBytes(bytes);
+ return client;
+}
+
+/**
+ * A minimal item as the form hands it over.
+ * @param name - The item's name
+ * @returns The draft
+ */
+function draftItem(name: string): DraftItem {
+ return { Name: name, ItemType: ItemTypes.Note, Fields: [], CreatedAt: '', UpdatedAt: '' } as unknown as DraftItem;
+}
+
+describe('mutation scopes', () => {
+ let client: SqliteClient;
+
+ beforeEach(async () => {
+ client = await openClient();
+ });
+
+ it('records nothing for a read', () => {
+ client.settings.getSetting('CredentialsSortOrder');
+ expect(client.takeMutationScopes()).toEqual([]);
+ });
+
+ it('records the bucket a scoped repository writes into', () => {
+ client.settings.updateSetting('DefaultIdentityLanguage', 'nl');
+ expect(client.takeMutationScopes()).toEqual([VaultDataBucketCategory.Settings]);
+ });
+
+ it('records a full manifest change for an unscoped repository', async () => {
+ await client.items.create(draftItem('Note'));
+ expect(client.takeMutationScopes()).toEqual(['Main']);
+ });
+
+ it('records every scope a single mutation touched, so a bucket-only push cannot swallow an item change', async () => {
+ client.settings.updateSetting('DefaultIdentityLanguage', 'nl');
+ await client.items.create(draftItem('Note'));
+
+ const scopes = client.takeMutationScopes();
+ expect(scopes).toContain(VaultDataBucketCategory.Settings);
+ expect(scopes).toContain('Main');
+ });
+
+ it('clears what it hands out', () => {
+ client.settings.updateSetting('DefaultIdentityLanguage', 'nl');
+ expect(client.takeMutationScopes()).toHaveLength(1);
+ expect(client.takeMutationScopes()).toEqual([]);
+ });
+});
diff --git a/core/client/src/sync/VaultMutationScope.ts b/core/client/src/sync/VaultMutationScope.ts
index 9fa972867..d7141fc0d 100644
--- a/core/client/src/sync/VaultMutationScope.ts
+++ b/core/client/src/sync/VaultMutationScope.ts
@@ -52,11 +52,3 @@ export const isSilentScope = (scope: VaultMutationScope): boolean => SILENT_VAUL
*/
export const hasUserVisibleScope = (scopes: readonly VaultMutationScope[]): boolean =>
scopes.length === 0 || scopes.some(scope => !isSilentScope(scope));
-
-/**
- * Options for executing a vault mutation.
- */
-export type VaultMutationOptions = {
- /** What the mutation touches. Defaults to 'Main' (full manifest push) when omitted. */
- scope?: VaultMutationScope;
-};