mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-16 20:11:08 +00:00
Cloud sync uploaded the whole encrypted vault as a single payload. Two
problems follow from that, both of which we hit in production:
1. A one-sided deletion propagated to every device. Restoring an older
local backup is indistinguishable from "the user deleted these 25
hosts", so the next sync happily overwrote the cloud replica and
every other machine with the older, smaller vault.
2. AI chat sessions were not part of the sync payload at all, so they
never travelled between machines and never appeared in any backup.
This change adds an opt-in "keep all" merge and makes sessions syncable.
## Non-destructive merge
`mergeSyncPayloads` gains `{ preserveDeletions }`. When set, an entity
missing on one side is carried forward from the side that still has it;
only entities absent from base *and* both sides are removed. It is
exposed as the new `smartMergePreserve` strategy ("Smart merge (never
delete)") and threaded through every merge call site:
- `providerSyncMethods.syncToProviderImpl`
- `syncAllStorageMethods`
- `useAutoSync`
Default behaviour (`smartMerge`) is unchanged, so existing users see no
behaviour change until they opt in.
## AI sessions in the payload
`SyncPayload.aiSessions` is new and opt-in via the
"Sync AI chat history" switch in Settings → Cloud Sync. Sessions are
unioned by id, and messages are merged **append-only**: a message is
identified by id and the merged transcript is the union of all three
sides ordered by timestamp. Two devices that appended different turns to
the same conversation therefore keep both turns, instead of one device
silently dropping the other's work.
Applying a downloaded payload unions with the local transcript rather
than replacing it, so an older cloud snapshot can never delete turns
that only exist locally.
Inline attachment blobs (`base64Data`) are stripped before upload —
image/PDF payloads would multiply the encrypted vault size and may carry
data the user never intended to leave the device. Attachment metadata
(filename, media type, line count) is kept for context.
## Tests
- `domain/syncMerge.preserveDeletions.test.ts` — one-sided deletions
(local and remote), both-sides deletion, additions, notes/snippets/
port-forwarding/string entities, idempotence, summary reporting.
- `domain/syncMerge.aiSessions.test.ts` — session union, append-only
message merge, timestamp ordering, metadata LWW, strategy-aware
deletion, attachment stripping, idempotence.
Verified locally: 1939/1939 tests pass across the domain, convergentSync,
i18n, cloudSync and application sync suites; eslint clean on all touched
files.
55 lines
1.7 KiB
TypeScript
55 lines
1.7 KiB
TypeScript
/**
|
|
* - `smartMerge` — three-way merge; deletions propagate (legacy default)
|
|
* - `smartMergePreserve` — three-way merge that never propagates a one-sided
|
|
* deletion; an entity survives until *both* devices dropped it. Use when the
|
|
* same vault is edited from sessions, restores or partially-loaded states
|
|
* where a missing entry must not be read as "the user deleted this".
|
|
* - `preferCloud` / `preferLocal` — whole-payload side wins
|
|
*/
|
|
export type CloudSyncStrategy =
|
|
| 'smartMerge'
|
|
| 'smartMergePreserve'
|
|
| 'preferCloud'
|
|
| 'preferLocal';
|
|
|
|
export type CloudSyncConflictAction = 'smart-merge' | 'download-remote' | 'upload-local';
|
|
|
|
export const DEFAULT_CLOUD_SYNC_STRATEGY: CloudSyncStrategy = 'smartMerge';
|
|
|
|
const CLOUD_SYNC_STRATEGIES: readonly CloudSyncStrategy[] = [
|
|
'smartMerge',
|
|
'smartMergePreserve',
|
|
'preferCloud',
|
|
'preferLocal',
|
|
] as const;
|
|
|
|
export function normalizeCloudSyncStrategy(value: unknown): CloudSyncStrategy {
|
|
return CLOUD_SYNC_STRATEGIES.includes(value as CloudSyncStrategy)
|
|
? value as CloudSyncStrategy
|
|
: DEFAULT_CLOUD_SYNC_STRATEGY;
|
|
}
|
|
|
|
/** True when the strategy merges with non-destructive (keep-all) semantics. */
|
|
export function isPreserveDeletionsStrategy(strategy: CloudSyncStrategy): boolean {
|
|
return strategy === 'smartMergePreserve';
|
|
}
|
|
|
|
export function resolveCloudSyncConflictAction(
|
|
strategy: CloudSyncStrategy,
|
|
remoteState: { hasConflict: boolean; hasRemoteFile: boolean },
|
|
): CloudSyncConflictAction {
|
|
if (!remoteState.hasConflict || !remoteState.hasRemoteFile) {
|
|
return 'upload-local';
|
|
}
|
|
|
|
if (strategy === 'preferCloud') {
|
|
return 'download-remote';
|
|
}
|
|
|
|
if (strategy === 'preferLocal') {
|
|
return 'upload-local';
|
|
}
|
|
|
|
return 'smart-merge';
|
|
}
|