mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-16 20:11:08 +00:00
* feat(plugins): add sync providers and encrypted sidecar sync
Implement PR 8 of the plugin platform (#2269): namespaced sync Providers
with provider.sync, encrypted-object storage operations, WebDAV through the
shared storage surface, dynamic cloud provider IDs, and non-cascade sidecars
for sync:true plugin settings plus account/CRDT baselines.
Related to #2269
* fix(plugins): wire sync providers and sidecars into production paths
Close PR 8 integration gaps: dynamic plugin provider registry and
getConnectedAdapter createPluginStorage, WebDAV through EncryptedObjectStorage,
cloud payload collect/apply for pluginSidecars, and IPC/preload bridges.
Related to #2269
* fix(plugins): preserve WebDAV auth reuse and resourceId through EOS wrap
encryptedObjectStorageAsCloudAdapter now reports initiallyAuthenticated when
config exists (matching raw WebDAVAdapter) so getConnectedAdapter reuses the
cached instance, and preserves/refreshes resourceId from the backing adapter
instead of always forcing the default object key.
Related to #2269
* fix(plugins): address Codex PR 8 review findings
Carry pluginSidecars through merge/convergent uploads, lazy-connect before
object I/O with revision-aware writes, fail closed on sidecar collect errors,
expose connectPluginProvider, preserve remote timestamps, and drop deleted
installed-plugin settings from collection.
Related to #2269
* fix(plugins): close remaining Codex P1/P2 sync issues
Lazy stream cancel for inline sync reads, must-not-exist conditional writes,
three-way sidecar merge for local resets, validated apply path for settings,
and merge remote sidecars into convergent uploads.
Related to #2269
* fix(plugins): harden sidecar collect/apply and write verification
Preserve last-known sidecars when the host is offline, apply remote
deletions for installed plugins, force convergent upload on sidecar
divergence, verify plugin object writes, and pick plugin providers in
auto-sync startup checks.
Related to #2269
* fix(plugins): close remaining Codex sidecar/sync path gaps
Include sidecars in auto-sync payload and hash, keep explicit empty bundles,
fail apply when host is offline, require getCapabilities, and carry merged
sidecars into convergent local apply/upload decisions.
Related to #2269
* fix(plugins): close Codex P1 sidecar collect/apply/merge gaps
Treat null host responses as unavailable, persist collected settings into
the non-cascade table, retain decoded convergent payloads, use three-way
sidecar merge for deletions, and fail apply only on operational errors.
Also treat non-empty plugin sidecars as meaningful sync data and re-run
auto-sync when plugin contributions change.
Related to #2269
* fix(plugins): close remaining Codex P1 sidecar and provider gaps
Replay offline-cached sidecars before collect, keep explicit empty
sidecar fields after three-way merge, thread sidecars through convergent
conflict/downgrade materialization, keep gated-off plugin providers from
joining sync as connected, and rebind plugin EOS sessions after runtime
replacement.
Related to #2269
* fix(plugins): separate pending remote sidecars from last-known cache
Replay only host-offline remote applies into the DB, keep last-known as
upload fallback, use contribution reset for deleted settings, probe real
plugin host readiness, rebind storage events for plugin providers, clear
plugin bases on reset, and swallow disconnect rejections.
Related to #2269
* fix(plugins): hydrate retained sidecars and gate missing providers
Materialize installed-plugin settings from retained sidecars before
collect pruning, clear sidecars on setting reset, fail closed when
pending remote replay fails, and only restore plugin providers that are
contribution-available. Also initialize dynamic provider counters on
connect.
Related to #2269
* fix(plugins): surface plugin sync providers and preserve conflict sidecars
Collect live sidecars before convergent conflict apply/upload, include
dynamic providers in status and disconnect paths, and list plugin sync
providers in Cloud Sync settings for connect/disconnect.
Related to #2269
* fix(plugins): per-provider sidecar baselines and safer collect/UI
Merge convergent sidecars against each provider's own baseline, keep
retained settings in sidecars without unvalidated plugin_settings writes,
guard isConnectDisabled for never-connected plugins, and fall back plugin
provider labels/icons in the status popup.
Related to #2269
* fix(plugins): correct plugin provider discovery and sidecar reverify
Read nested contribution metadata for sync provider cards, gate empty-config
connects, enforce single-provider disconnect for plugins, and re-verify after
sidecar-only convergent uploads so baselines track the post-write payload.
Related to #2269
* fix(plugins): refresh sync provider availability from live contributions
Replace the contribution-available provider ID set when plugins change so
disabled or uninstalled sync providers leave the auto-sync ready set and
drop to disconnected with config retained.
Related to #2269
* fix(plugins): preserve sidecars in backups, pending queue, and migration
Attach last-known plugin sidecars to protective local vault backups, fail
operationally when pending/last-known storage writes are rejected, carry
sidecars through convergent migration publish, and reject trailing garbage
on encrypted object parse.
Related to #2269
* fix(plugins): abort conflict collect failures and merge migration sidecars
Rethrow operational sidecar collection errors during convergent conflict
resolution, LWW-union all local/provider sidecars during convergent
migration, re-collect last-known after apply, and prefer live host collect
for protective local vault backups.
Related to #2269
* fix(plugins): restore pluginBridge tests, fix import path, union downgrade sidecars
Correct SettingsSyncTab dynamic import path so production builds resolve,
add ipcMain.on to the plugin bridge test double, union all provider
sidecars on convergent downgrade, keep empty last-known resets, and skip
force-delete when contribution reset validation rejects.
Related to #2269
* fix(plugins): abort backup collect failures and honor empty remote wipes
Only fall back to last-known sidecars when the host is unavailable during
protective backups, treat explicit empty remote sidecar bundles as
authoritative wipes, remove lint-blocking anys from harness tests, and
use a stable equal-timestamp sidecar tie-break.
Related to #2269
* fix(plugins): include sidecars in equality, preserve merge base on reconnect
Compare pluginSidecars in cloudSyncPayloadsEqual for migration freshness,
clear plugin merge state only when account/resource changes on reconnect,
cancel accepted sync read streams on failure, and stop re-connecting the
plugin EOS adapter on every I/O.
Related to #2269
* fix(plugins): drop cached adapters on contribution refresh
Invalidate plugin sync adapters when live contributions change so a
restarted runtime gets a fresh connect, take offline any non-disconnected
missing providers, and latch version-change backups only after cancellation
checks pass.
Related to #2269
* fix(plugins): preserve omitted sidecars and hydrate on plugin enable
Treat missing pluginSidecars as legacy/unsupported in three-way merge
(only explicit empty is a wipe), hydrate retained settings when a plugin
enables, and re-enable retained provider configs when contributions return.
Related to #2269
* fix(plugins): keep newer retained sidecars over older stored settings
When collection merges existing sidecars with plugin_settings rows, prefer
the newer updatedAt so a schema-rejected remote value retained only in
the sidecar table is not overwritten by a stale local setting.
Related to #2269
* fix(plugins): seal plugin provider config and clear base on config change
Encrypt opaque plugin provider configuration before persistence, clear
merge state when reconnect configuration changes, and stream sync objects
below a base64-safe inline threshold so control-plane JSON stays in budget.
Related to #2269
* fix(plugins): seal plugin configs by provider id and hydrate before start
Encrypt plugin provider configuration only for non-builtin provider IDs
so field-name collisions cannot skip sealing, hydrate retained sidecars
before plugin enable starts the runtime, and stop tearing down live
adapters on no-op availability refreshes.
Related to #2269
* fix(plugins): seal scalar configs, safe inline reads, stable config fingerprint
Encrypt any non-builtin plugin configuration JSON shape, enforce the
base64-safe inline read cutoff, and compare reconnect configurations with
order-independent fingerprints so merge bases are not cleared spuriously.
Related to #2269
* fix(plugins): thread sync credentials and harden availability checks
Pass SyncConnectPayload.credential through the object-storage and IPC host
path, treat only null/undefined config as missing, and avoid wiping the
plugin provider availability catalog on transient discovery failures.
Related to #2269
* fix(plugins): unblock schema-required sync connect and scalar configs
Add a JSON config dialog for plugin sync providers that declare a
required configurationSchema, reuse retained configs on reconnect, and
treat falsy scalar configs as present in readiness and adapter lookup.
Related to #2269
* fix(plugins): rebind sync adapters on contribution refresh
Drop cached plugin sync adapters when live contributions still include
the provider so replaced runtimes get a fresh connect, and seal falsy
scalar plugin configs with nullish presence checks end-to-end.
Related to #2269
* fix(plugins): apply sidecars under current declared setting scopes
When a plugin update changes a syncable setting scope, write under the
live declaration and drop the obsolete scope row so collection does not
republish duplicates.
Related to #2269
* fix(plugins): seal config envelopes and stabilize provider counters
Use an unambiguous host-owned sealed-config envelope, initialize
dynamic-provider sequence counters before cross-window decrypt, keep
three-way sidecar deletions during convergent migration, and create the
sidecar table for existing schema-1 plugin databases.
Related to #2269
* fix(plugins): avoid mid-sync adapter drops and sidecar resurrection
Keep plugin adapters across setting-only contribution refreshes and
rebind sessions on ensureConnected; stop preserving local-only missing
plugin sidecars when remote already mentions the plugin; compare configs
with nullish semantics across windows; swallow quick-connect failures
after toasting.
Related to #2269
* fix(plugins): restore remoteKeys for installed sidecar resets
Reintroduce the remote key set used when dropping installed-plugin
settings the remote no longer carries so applyFromSync cannot throw
after replacing the sidecar table.
Related to #2269
* fix(plugins): honor conflict policy and re-key scoped sidecars
Pass preferCloud/preferLocal into three-way sidecar merge, use
per-provider baselines on convergent downgrade so local resets are not
resurrected, and re-key applied settings under the current declared
scope coordinates.
Related to #2269
* fix(plugins): keep strategy on final sidecar merge and block dual legacy
Pass preferCloud/preferLocal through the final verified sidecar merge,
avoid auto-reactivating a retained plugin while another legacy provider
is already connected, cache plugin adapters only after initializeSync,
and preserve explicit null configuration values.
Related to #2269
* fix(plugins): strip plugin avatars and protect last-known sidecars
Do not load plugin-supplied avatar URLs in the main renderer, and avoid
overwriting last-known sidecars with an unmerged remote bundle when the
post-apply collect fails or is non-authoritative.
Related to #2269
* fix(plugins): strip top-bar plugin avatars and stop always-rebind
Hide plugin avatar URLs in the sync status popover, drop always-on
rebindSession for plugin adapters, and read convergent mode from the
local config module when deciding whether to auto-reactivate providers.
Related to #2269
* fix(plugins): use window.netcatty for sync IPC and allow null configs
Read plugin sync and sidecar APIs from the production preload global,
and treat an explicit config property (including JSON null) as present
for readiness and adapter lookup.
Related to #2269
* fix(plugins): close residual Codex P2 sync edge cases
Three-way merge sidecars during v2 convergent migration so local
resets are not resurrected, preserve explicit null plugin configs on
adapter rebuild, rebind EOS sessions after I/O failure for replaced
runtimes, and accept the public inlineObjectBytes limit on readObject.
Related to #2269
* fix(plugins): keep inline read cutoff under control-plane budget
Restore the base64-safe inline cutoff so advertised-size reads cannot
overflow the 128 KiB provider JSON envelope; providers near the public
inlineObjectBytes limit must stream.
Related to #2269
* fix(plugins): harden sidecar empty-vault, hydrate LWW, and credential seals
Close remaining Codex/review gaps for PR8: keep empty sidecar shells from
bypassing upload guards, skip startup hydrate when local settings are newer,
seal/validate durable plugin credential refs, and cover schema 1→2 migration.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): rebind plugin sync sessions and keep null configs
Address Codex P2 on 8ae52c54: re-issue connect after runtime replacement,
and treat stored config:null as present in the dashboard reconnect path.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): defer empty sidecar last-known until sync succeeds
Keep prior non-empty last-known across an authoritative empty collect so the
empty-vault guard can still recognize a plugin-only reset, then commit the
empty cache after a successful upload.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): avoid restricted localStorage global in sidecar test
Bind the mock to a local binding so eslint no-restricted-globals stays clean.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): commit applied sidecar cache and reset orphan settings
Prefer merged payload sidecars when updating last-known after sync, and clear
syncable plugin_settings that lack sidecar rows on authoritative remote deletes.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): reseal marker-shaped plugin configs instead of skipping
Exact __netcatty_plugin_config_v1 collisions no longer bypass encryption; trusted
host envelopes are unwrapped then resealed, and false collisions are sealed whole.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): keep merged sidecars in last-known after apply collect fails
Return applyFromSync entries from IPC and use them when follow-up collect fails,
so preserved missing-plugin rows are not dropped from the offline upload cache.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): delete stale-scope settings at stored coordinates on reset
After resetSetting against the current declared scope, also remove the row at
the sidecar's stored scope so scope migrations cannot republish remote deletes.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): close sync-provider credential, stream, and sidecar gaps
Make SecretRef usable end-to-end via lease-bound network.request, keep
renderer/main transfers abortable and bounded, and stop sidecar
last-known/pending from corrupting backups and conflict apply.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): harden single-provider, secret schema, and conditional conflicts
Keep legacy restart from dual-connecting providers, extract writeOnly schema
secrets into OS storage, and surface conditional-write precondition failures
as conflict UI instead of a generic sync error.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): dedupe sync type imports for lint
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix: address Codex review on PR #2713
* fix(plugins): wait for sidecar host before version backup latch
Defer the one-shot upgrade snapshot until the plugin sidecar host is ready (or a short grace), and catch getDataHash failures so pending/baseline paths do not reject unhandled.
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix(plugins): require sidecar grace before version backup
Always wait for contributions/grace tick before the upgrade snapshot, and only latch after a non-cancelled attempt so cleanup cannot suppress retries.
Co-authored-by: Cursor <cursoragent@cursor.com>
---------
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: netcatty-bot <308658023+netcatty-bot@users.noreply.github.com>
603 lines
21 KiB
TypeScript
603 lines
21 KiB
TypeScript
/**
|
|
* Three-Way Merge for Cloud Sync Payloads
|
|
*
|
|
* Implements a Git-style three-way merge using a stored "base" snapshot
|
|
* (the last successfully synced payload) to detect per-entity changes
|
|
* on both the local and remote sides.
|
|
*
|
|
* Algorithm:
|
|
* For each entity (identified by `id`):
|
|
* - Only in local → local addition → keep
|
|
* - Only in remote → remote addition → keep
|
|
* - In base, removed locally → local deletion → remove (unless remote modified)
|
|
* - In base, removed remotely → remote deletion → remove (unless local modified)
|
|
* - Modified only locally → keep local version
|
|
* - Modified only remotely → keep remote version
|
|
* - Modified on both sides → prefer local (conflict logged)
|
|
*
|
|
* When no base is available (first sync), falls back to a set-union
|
|
* merge by entity ID, preferring local for duplicates.
|
|
*/
|
|
|
|
import { carryForwardSyncDeletions, getDeletedEntityIds } from './syncReliability';
|
|
import type { CloudSyncPayloadEntityKey, SyncPayload } from './sync';
|
|
import { mergePluginSyncSidecarsThreeWay } from './pluginSyncSidecar';
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Public types
|
|
// ---------------------------------------------------------------------------
|
|
|
|
interface MergeSummary {
|
|
added: { local: number; remote: number };
|
|
deleted: { local: number; remote: number };
|
|
modified: { local: number; remote: number; conflicts: number };
|
|
}
|
|
|
|
interface MergeResult {
|
|
payload: SyncPayload;
|
|
/** True when both sides modified the same entity (resolved by preferring local) */
|
|
hadConflicts: boolean;
|
|
summary: MergeSummary;
|
|
}
|
|
|
|
const OPTIONAL_ENTITY_KEYS = new Set<CloudSyncPayloadEntityKey>([
|
|
'identities',
|
|
'proxyProfiles',
|
|
'snippetPackages',
|
|
'notes',
|
|
'noteGroups',
|
|
'portForwardingRules',
|
|
'groupConfigs',
|
|
]);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Helpers
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Deterministic JSON string for content comparison.
|
|
* Sorts object keys to avoid false diffs from key ordering.
|
|
*/
|
|
function fingerprint(value: unknown): string {
|
|
return JSON.stringify(value, (_key, v) => {
|
|
if (v && typeof v === 'object' && !Array.isArray(v)) {
|
|
return Object.keys(v).sort().reduce<Record<string, unknown>>((acc, k) => {
|
|
acc[k] = (v as Record<string, unknown>)[k];
|
|
return acc;
|
|
}, {});
|
|
}
|
|
return v;
|
|
});
|
|
}
|
|
|
|
function entityArray<T>(
|
|
payload: SyncPayload,
|
|
key: CloudSyncPayloadEntityKey,
|
|
fallback: T[],
|
|
): T[] {
|
|
if (
|
|
OPTIONAL_ENTITY_KEYS.has(key)
|
|
&& !Object.prototype.hasOwnProperty.call(payload, key)
|
|
) {
|
|
return fallback;
|
|
}
|
|
const value = payload[key];
|
|
return Array.isArray(value) ? value as T[] : [];
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Entity-array merge (hosts, keys, identities, snippets, etc.)
|
|
// ---------------------------------------------------------------------------
|
|
|
|
interface EntityMergeResult<T> {
|
|
merged: T[];
|
|
conflicts: number;
|
|
added: { local: number; remote: number };
|
|
deleted: { local: number; remote: number };
|
|
modified: { local: number; remote: number };
|
|
}
|
|
|
|
function mergeEntityArrays<T extends { id: string }>(
|
|
base: T[],
|
|
local: T[],
|
|
remote: T[],
|
|
tombstones?: { local: Set<string>; remote: Set<string> },
|
|
): EntityMergeResult<T> {
|
|
const baseMap = new Map(base.map((e) => [e.id, e]));
|
|
const localMap = new Map(local.map((e) => [e.id, e]));
|
|
const remoteMap = new Map(remote.map((e) => [e.id, e]));
|
|
|
|
const allIds = new Set([
|
|
...baseMap.keys(),
|
|
...localMap.keys(),
|
|
...remoteMap.keys(),
|
|
]);
|
|
|
|
const merged: T[] = [];
|
|
let conflicts = 0;
|
|
const added = { local: 0, remote: 0 };
|
|
const deleted = { local: 0, remote: 0 };
|
|
const modified = { local: 0, remote: 0 };
|
|
|
|
for (const id of allIds) {
|
|
const baseItem = baseMap.get(id);
|
|
const localItem = localMap.get(id);
|
|
const remoteItem = remoteMap.get(id);
|
|
|
|
const inBase = baseItem !== undefined;
|
|
const inLocal = localItem !== undefined;
|
|
const inRemote = remoteItem !== undefined;
|
|
|
|
if (!inBase && inLocal && !inRemote && tombstones?.remote.has(id)) {
|
|
// Remote explicitly records this entity as deleted. When no base is
|
|
// available, this tombstone is the only durable signal that absence is
|
|
// intentional rather than an old client omitting the entity.
|
|
deleted.remote++;
|
|
} else if (!inBase && !inLocal && inRemote && tombstones?.local.has(id)) {
|
|
deleted.local++;
|
|
} else if (!inBase && inLocal && !inRemote) {
|
|
// Local addition
|
|
merged.push(localItem);
|
|
added.local++;
|
|
} else if (!inBase && !inLocal && inRemote) {
|
|
// Remote addition
|
|
merged.push(remoteItem);
|
|
added.remote++;
|
|
} else if (!inBase && inLocal && inRemote) {
|
|
// Both added same ID — prefer local
|
|
merged.push(localItem);
|
|
if (fingerprint(localItem) !== fingerprint(remoteItem)) {
|
|
conflicts++;
|
|
}
|
|
} else if (inBase && inLocal && inRemote) {
|
|
// Exists in all three — compare changes
|
|
const localChanged = fingerprint(localItem) !== fingerprint(baseItem);
|
|
const remoteChanged = fingerprint(remoteItem) !== fingerprint(baseItem);
|
|
|
|
if (!localChanged && !remoteChanged) {
|
|
merged.push(baseItem);
|
|
} else if (localChanged && !remoteChanged) {
|
|
merged.push(localItem);
|
|
modified.local++;
|
|
} else if (!localChanged && remoteChanged) {
|
|
merged.push(remoteItem);
|
|
modified.remote++;
|
|
} else {
|
|
// Both changed — prefer local
|
|
merged.push(localItem);
|
|
if (fingerprint(localItem) !== fingerprint(remoteItem)) {
|
|
conflicts++;
|
|
}
|
|
modified.local++;
|
|
modified.remote++;
|
|
}
|
|
} else if (inBase && !inLocal && inRemote) {
|
|
// Local deleted
|
|
const remoteChanged = fingerprint(remoteItem) !== fingerprint(baseItem);
|
|
if (remoteChanged) {
|
|
// Remote modified + local deleted → keep modification (safer)
|
|
merged.push(remoteItem);
|
|
conflicts++;
|
|
} else {
|
|
deleted.local++;
|
|
}
|
|
} else if (inBase && inLocal && !inRemote) {
|
|
// Remote deleted
|
|
const localChanged = fingerprint(localItem) !== fingerprint(baseItem);
|
|
if (localChanged) {
|
|
// Local modified + remote deleted → keep modification (safer)
|
|
merged.push(localItem);
|
|
conflicts++;
|
|
} else {
|
|
deleted.remote++;
|
|
}
|
|
}
|
|
// inBase && !inLocal && !inRemote → both deleted → gone
|
|
}
|
|
|
|
return { merged, conflicts, added, deleted, modified };
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// String-array merge (customGroups, snippetPackages)
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function mergeStringArrays(
|
|
base: string[],
|
|
local: string[],
|
|
remote: string[],
|
|
tombstones?: { local: Set<string>; remote: Set<string> },
|
|
): string[] {
|
|
const baseSet = new Set(base);
|
|
const localSet = new Set(local);
|
|
const remoteSet = new Set(remote);
|
|
|
|
const result = new Set<string>();
|
|
|
|
// Start with base items, then apply additions/deletions
|
|
const allValues = new Set([...baseSet, ...localSet, ...remoteSet]);
|
|
|
|
for (const value of allValues) {
|
|
const inBase = baseSet.has(value);
|
|
const inLocal = localSet.has(value);
|
|
const inRemote = remoteSet.has(value);
|
|
|
|
if (!inBase && inLocal && !inRemote && tombstones?.remote.has(value)) {
|
|
// Remote tombstone wins over a stale local value when no base exists.
|
|
} else if (!inBase && !inLocal && inRemote && tombstones?.local.has(value)) {
|
|
// Local tombstone wins over a stale remote value when no base exists.
|
|
} else if (!inBase) {
|
|
// Addition — keep if either side added it
|
|
if (inLocal || inRemote) result.add(value);
|
|
} else {
|
|
// Was in base — keep unless both sides deleted
|
|
const localDeleted = !inLocal;
|
|
const remoteDeleted = !inRemote;
|
|
if (localDeleted && remoteDeleted) {
|
|
// Both deleted — gone
|
|
} else if (localDeleted || remoteDeleted) {
|
|
// Only one side deleted — honour the deletion
|
|
// (If the other side didn't touch it, it's still in their set from base)
|
|
} else {
|
|
result.add(value);
|
|
}
|
|
}
|
|
}
|
|
|
|
return [...result];
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Settings merge (flat key-value)
|
|
// ---------------------------------------------------------------------------
|
|
|
|
type SettingsObj = NonNullable<SyncPayload['settings']>;
|
|
|
|
/** Check if an array contains objects with `id` fields (for entity merge). */
|
|
function isIdArray(arr: unknown[]): boolean {
|
|
return arr.length > 0 && typeof arr[0] === 'object' && arr[0] !== null && 'id' in arr[0];
|
|
}
|
|
|
|
/** Treat an explicit empty object as a reset marker during the first cloud merge. */
|
|
function isEmptyPlainObject(value: unknown): value is Record<string, never> {
|
|
return value !== null &&
|
|
typeof value === 'object' &&
|
|
!Array.isArray(value) &&
|
|
Object.keys(value).length === 0;
|
|
}
|
|
|
|
/** Recursively merge two plain objects against a base using three-way logic. */
|
|
function mergeSettingsDeep(
|
|
base: Record<string, unknown>,
|
|
local: Record<string, unknown>,
|
|
remote: Record<string, unknown>,
|
|
preferRemoteOnConflict: boolean,
|
|
): Record<string, unknown> {
|
|
const allKeys = new Set([
|
|
...Object.keys(base),
|
|
...Object.keys(local),
|
|
...Object.keys(remote),
|
|
]);
|
|
const merged: Record<string, unknown> = {};
|
|
for (const key of allKeys) {
|
|
const bVal = base[key];
|
|
const lVal = local[key];
|
|
const rVal = remote[key];
|
|
const lChanged = fingerprint(lVal) !== fingerprint(bVal);
|
|
const rChanged = fingerprint(rVal) !== fingerprint(bVal);
|
|
|
|
if (!lChanged && !rChanged) {
|
|
if (bVal !== undefined) merged[key] = bVal;
|
|
} else if (lChanged && !rChanged) {
|
|
if (lVal !== undefined) merged[key] = lVal;
|
|
} else if (!lChanged && rChanged) {
|
|
if (rVal !== undefined) merged[key] = rVal;
|
|
} else {
|
|
// Both changed — recurse if both are plain objects, else prefer local
|
|
if (
|
|
lVal && rVal &&
|
|
typeof lVal === 'object' && !Array.isArray(lVal) &&
|
|
typeof rVal === 'object' && !Array.isArray(rVal)
|
|
) {
|
|
merged[key] = preferRemoteOnConflict && isEmptyPlainObject(rVal)
|
|
? rVal
|
|
: mergeSettingsDeep(
|
|
(bVal && typeof bVal === 'object' && !Array.isArray(bVal) ? bVal : {}) as Record<string, unknown>,
|
|
lVal as Record<string, unknown>,
|
|
rVal as Record<string, unknown>,
|
|
preferRemoteOnConflict,
|
|
);
|
|
} else if (
|
|
preferRemoteOnConflict &&
|
|
Array.isArray(lVal) && Array.isArray(rVal) &&
|
|
(isIdArray(lVal) || isIdArray(rVal) || isIdArray(Array.isArray(bVal) ? bVal as unknown[] : []))
|
|
) {
|
|
const bArr = Array.isArray(bVal) ? bVal as Array<{ id: string }> : [];
|
|
const result = mergeEntityArrays(
|
|
bArr,
|
|
rVal as Array<{ id: string }>,
|
|
lVal as Array<{ id: string }>,
|
|
);
|
|
merged[key] = result.merged;
|
|
} else if (preferRemoteOnConflict && rVal !== undefined) {
|
|
merged[key] = rVal;
|
|
} else if (lVal !== undefined) {
|
|
merged[key] = lVal;
|
|
}
|
|
}
|
|
}
|
|
return merged;
|
|
}
|
|
|
|
function mergeSettings(
|
|
base: SettingsObj | undefined,
|
|
local: SettingsObj | undefined,
|
|
remote: SettingsObj | undefined,
|
|
preferRemoteOnConflict: boolean,
|
|
): SettingsObj | undefined {
|
|
if (!local && !remote) return undefined;
|
|
if (!local) return remote;
|
|
if (!remote) return local;
|
|
|
|
const b = base ?? {};
|
|
const allKeys = new Set([
|
|
...Object.keys(b),
|
|
...Object.keys(local),
|
|
...Object.keys(remote),
|
|
]);
|
|
|
|
const merged: Record<string, unknown> = {};
|
|
|
|
for (const key of allKeys) {
|
|
const bVal = (b as Record<string, unknown>)[key];
|
|
const lVal = (local as Record<string, unknown>)[key];
|
|
const rVal = (remote as Record<string, unknown>)[key];
|
|
|
|
const lChanged = fingerprint(lVal) !== fingerprint(bVal);
|
|
const rChanged = fingerprint(rVal) !== fingerprint(bVal);
|
|
|
|
if (!lChanged && !rChanged) {
|
|
if (bVal !== undefined) merged[key] = bVal;
|
|
} else if (lChanged && !rChanged) {
|
|
if (lVal !== undefined) merged[key] = lVal;
|
|
} else if (!lChanged && rChanged) {
|
|
if (rVal !== undefined) merged[key] = rVal;
|
|
} else {
|
|
// Both changed — deep merge if both are plain objects, else prefer local
|
|
if (
|
|
lVal && rVal &&
|
|
typeof lVal === 'object' && !Array.isArray(lVal) &&
|
|
typeof rVal === 'object' && !Array.isArray(rVal)
|
|
) {
|
|
merged[key] = preferRemoteOnConflict && isEmptyPlainObject(rVal)
|
|
? rVal
|
|
: mergeSettingsDeep(
|
|
(bVal && typeof bVal === 'object' && !Array.isArray(bVal) ? bVal : {}) as Record<string, unknown>,
|
|
lVal as Record<string, unknown>,
|
|
rVal as Record<string, unknown>,
|
|
preferRemoteOnConflict,
|
|
);
|
|
} else if (
|
|
Array.isArray(lVal) && Array.isArray(rVal) &&
|
|
(isIdArray(lVal) || isIdArray(rVal) || isIdArray(Array.isArray(bVal) ? bVal as unknown[] : []))
|
|
) {
|
|
// Array of objects with `id` (e.g. customTerminalThemes) — entity merge
|
|
const bArr = Array.isArray(bVal) ? bVal as Array<{ id: string }> : [];
|
|
const preferred = preferRemoteOnConflict ? rVal : lVal;
|
|
const other = preferRemoteOnConflict ? lVal : rVal;
|
|
const result = mergeEntityArrays(
|
|
bArr,
|
|
preferred as Array<{ id: string }>,
|
|
other as Array<{ id: string }>,
|
|
);
|
|
merged[key] = result.merged;
|
|
} else if (preferRemoteOnConflict && rVal !== undefined) {
|
|
merged[key] = rVal;
|
|
} else if (lVal !== undefined) {
|
|
merged[key] = lVal;
|
|
}
|
|
}
|
|
}
|
|
|
|
return Object.keys(merged).length > 0 ? (merged as SettingsObj) : undefined;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Main merge function
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Three-way merge of sync payloads.
|
|
*
|
|
* @param base - The last successfully synced payload (null if unavailable)
|
|
* @param local - The current device's data
|
|
* @param remote - The other device's data (downloaded from cloud)
|
|
*/
|
|
export function mergeSyncPayloads(
|
|
base: SyncPayload | null,
|
|
local: SyncPayload,
|
|
remote: SyncPayload,
|
|
): MergeResult {
|
|
const emptyBase: SyncPayload = {
|
|
hosts: [],
|
|
keys: [],
|
|
identities: [],
|
|
proxyProfiles: [],
|
|
snippets: [],
|
|
customGroups: [],
|
|
snippetPackages: [],
|
|
notes: [],
|
|
noteGroups: [],
|
|
portForwardingRules: [],
|
|
settings: undefined,
|
|
syncedAt: 0,
|
|
};
|
|
const b = base ?? emptyBase;
|
|
|
|
const summary: MergeSummary = {
|
|
added: { local: 0, remote: 0 },
|
|
deleted: { local: 0, remote: 0 },
|
|
modified: { local: 0, remote: 0, conflicts: 0 },
|
|
};
|
|
const tombstones = (entityType: CloudSyncPayloadEntityKey) => ({
|
|
local: getDeletedEntityIds(local, entityType),
|
|
remote: getDeletedEntityIds(remote, entityType),
|
|
});
|
|
|
|
// Merge each entity type
|
|
const hosts = mergeEntityArrays(b.hosts ?? [], local.hosts ?? [], remote.hosts ?? [], tombstones('hosts'));
|
|
const keys = mergeEntityArrays(b.keys ?? [], local.keys ?? [], remote.keys ?? [], tombstones('keys'));
|
|
const baseIdentities = b.identities ?? [];
|
|
const identities = mergeEntityArrays(
|
|
baseIdentities,
|
|
entityArray(local, 'identities', baseIdentities),
|
|
entityArray(remote, 'identities', baseIdentities),
|
|
tombstones('identities'),
|
|
);
|
|
const baseProxyProfiles = b.proxyProfiles ?? [];
|
|
const proxyProfiles = mergeEntityArrays(
|
|
baseProxyProfiles,
|
|
entityArray(local, 'proxyProfiles', baseProxyProfiles),
|
|
entityArray(remote, 'proxyProfiles', baseProxyProfiles),
|
|
tombstones('proxyProfiles'),
|
|
);
|
|
const snippets = mergeEntityArrays(b.snippets ?? [], local.snippets ?? [], remote.snippets ?? [], tombstones('snippets'));
|
|
const baseNotes = b.notes ?? [];
|
|
const notes = mergeEntityArrays(
|
|
baseNotes,
|
|
entityArray(local, 'notes', baseNotes),
|
|
entityArray(remote, 'notes', baseNotes),
|
|
tombstones('notes'),
|
|
);
|
|
const basePortForwardingRules = b.portForwardingRules ?? [];
|
|
const portForwardingRules = mergeEntityArrays(
|
|
basePortForwardingRules,
|
|
entityArray(local, 'portForwardingRules', basePortForwardingRules),
|
|
entityArray(remote, 'portForwardingRules', basePortForwardingRules),
|
|
tombstones('portForwardingRules'),
|
|
);
|
|
|
|
// Merge group configs (keyed by path — wrap with virtual id for entity merge)
|
|
type GCWithId = import('./models').GroupConfig & { id: string };
|
|
const wrapGC = (arr: import('./models').GroupConfig[] | undefined): GCWithId[] =>
|
|
(arr ?? []).map(gc => ({ ...gc, id: gc.path }));
|
|
const unwrapGC = (arr: GCWithId[]): import('./models').GroupConfig[] =>
|
|
arr.map(({ id: _id, ...rest }) => rest as import('./models').GroupConfig);
|
|
const baseGroupConfigs = b.groupConfigs ?? [];
|
|
const groupConfigsResult = mergeEntityArrays(
|
|
wrapGC(baseGroupConfigs),
|
|
wrapGC(entityArray(local, 'groupConfigs', baseGroupConfigs)),
|
|
wrapGC(entityArray(remote, 'groupConfigs', baseGroupConfigs)),
|
|
tombstones('groupConfigs'),
|
|
);
|
|
|
|
// Aggregate stats
|
|
const entityResults: Pick<EntityMergeResult<unknown>, 'added' | 'deleted' | 'modified' | 'conflicts'>[] =
|
|
[hosts, keys, identities, proxyProfiles, snippets, notes, portForwardingRules, groupConfigsResult];
|
|
for (const r of entityResults) {
|
|
summary.added.local += r.added.local;
|
|
summary.added.remote += r.added.remote;
|
|
summary.deleted.local += r.deleted.local;
|
|
summary.deleted.remote += r.deleted.remote;
|
|
summary.modified.local += r.modified.local;
|
|
summary.modified.remote += r.modified.remote;
|
|
summary.modified.conflicts += r.conflicts;
|
|
}
|
|
|
|
// Merge string arrays
|
|
const customGroups = mergeStringArrays(
|
|
b.customGroups ?? [],
|
|
local.customGroups ?? [],
|
|
remote.customGroups ?? [],
|
|
tombstones('customGroups'),
|
|
);
|
|
const baseSnippetPackages = b.snippetPackages ?? [];
|
|
const snippetPackages = mergeStringArrays(
|
|
baseSnippetPackages,
|
|
entityArray<string>(local, 'snippetPackages', baseSnippetPackages),
|
|
entityArray<string>(remote, 'snippetPackages', baseSnippetPackages),
|
|
tombstones('snippetPackages'),
|
|
);
|
|
const baseNoteGroups = b.noteGroups ?? [];
|
|
const noteGroups = mergeStringArrays(
|
|
baseNoteGroups,
|
|
entityArray<string>(local, 'noteGroups', baseNoteGroups),
|
|
entityArray<string>(remote, 'noteGroups', baseNoteGroups),
|
|
tombstones('noteGroups'),
|
|
);
|
|
|
|
// Merge settings
|
|
// With no trusted base, the remote payload represents the established
|
|
// cloud replica while a newly installed client has already persisted its
|
|
// initial defaults. Treating both as additions and preferring local would
|
|
// keep those defaults and make settings appear not to sync at all. Prefer
|
|
// the cloud value only for same-field conflicts on this first merge; fields
|
|
// present on just one side are still preserved. Once a base exists, retain
|
|
// the existing local-wins three-way conflict policy.
|
|
const settings = mergeSettings(
|
|
b.settings,
|
|
local.settings,
|
|
remote.settings,
|
|
base === null,
|
|
);
|
|
|
|
// Deduplicate global SFTP bookmarks by path (IDs are random per device)
|
|
if (settings?.sftpGlobalBookmarks && settings.sftpGlobalBookmarks.length > 0) {
|
|
const seenPaths = new Set<string>();
|
|
settings.sftpGlobalBookmarks = settings.sftpGlobalBookmarks.filter((bm) => {
|
|
if (seenPaths.has(bm.path)) return false;
|
|
seenPaths.add(bm.path);
|
|
return true;
|
|
});
|
|
}
|
|
|
|
const groupConfigs = unwrapGC(groupConfigsResult.merged);
|
|
|
|
// Plugin sidecars: three-way merge so local resets propagate and baselines
|
|
// from missing plugins are preserved. An *omitted* pluginSidecars field means
|
|
// legacy/unsupported (treat as base for that side). Explicit { entries: [] }
|
|
// is an authoritative wipe.
|
|
const baseHasSidecars = Object.prototype.hasOwnProperty.call(b, 'pluginSidecars');
|
|
const localHasSidecars = Object.prototype.hasOwnProperty.call(local, 'pluginSidecars');
|
|
const remoteHasSidecars = Object.prototype.hasOwnProperty.call(remote, 'pluginSidecars');
|
|
const baseSidecars = Array.isArray(b.pluginSidecars?.entries) ? b.pluginSidecars.entries : [];
|
|
const localSidecars = localHasSidecars
|
|
? (Array.isArray(local.pluginSidecars?.entries) ? local.pluginSidecars.entries : [])
|
|
: baseSidecars;
|
|
const remoteSidecars = remoteHasSidecars
|
|
? (Array.isArray(remote.pluginSidecars?.entries) ? remote.pluginSidecars.entries : [])
|
|
: baseSidecars;
|
|
const mergedSidecarEntries = mergePluginSyncSidecarsThreeWay({
|
|
base: baseSidecars,
|
|
local: localSidecars,
|
|
remote: remoteSidecars,
|
|
});
|
|
// Emit the field when any side explicitly carried it (including empty reset).
|
|
const anySideHadPluginSidecars = baseHasSidecars || localHasSidecars || remoteHasSidecars;
|
|
|
|
const payload: SyncPayload = carryForwardSyncDeletions({
|
|
hosts: hosts.merged,
|
|
keys: keys.merged,
|
|
identities: identities.merged,
|
|
proxyProfiles: proxyProfiles.merged,
|
|
snippets: snippets.merged,
|
|
customGroups,
|
|
snippetPackages,
|
|
notes: notes.merged,
|
|
noteGroups,
|
|
portForwardingRules: portForwardingRules.merged,
|
|
groupConfigs,
|
|
settings,
|
|
...(mergedSidecarEntries.length > 0 || anySideHadPluginSidecars
|
|
? { pluginSidecars: { version: 1 as const, entries: mergedSidecarEntries } }
|
|
: {}),
|
|
syncedAt: Date.now(),
|
|
}, [local, remote]);
|
|
|
|
return {
|
|
payload,
|
|
hadConflicts: summary.modified.conflicts > 0,
|
|
summary,
|
|
};
|
|
}
|