Files
NetMesh/application/convergentSyncReplica.ts
zhaolei 3c72efcb7f
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
[Init] Initial commit - NetMesh terminal manager
2026-09-13 18:24:01 +08:00

47 lines
1.8 KiB
TypeScript

import type { SyncPayload } from '../domain/sync';
import {
applyLegacySyncPayload,
materializeSyncPayloadFromConvergentState,
stripConvergentSyncEnvelope,
} from '../domain/convergentSync';
import { getCloudSyncManager } from '../infrastructure/services/CloudSyncManager';
import type { CloudSyncManager } from '../infrastructure/services/CloudSyncManager';
import { getConvergentSyncLocalConfig } from '../infrastructure/services/convergentSyncConfig';
export type CommitRestoredPayloadConvergentWrites = () => Promise<void>;
/**
* Local backups intentionally contain no active CRDT replica. Before applying
* a restore, validate the active replica and prepare ordinary local writes.
* The returned commit persists them only after the local import succeeds, so
* the replica never claims a restore that the local import rejected.
*/
export async function prepareRestoredPayloadConvergentWrites(
restoredPayload: SyncPayload,
now = Date.now(),
dependencies: {
manager?: CloudSyncManager;
initialized?: boolean;
} = {},
): Promise<CommitRestoredPayloadConvergentWrites> {
const initialized = dependencies.initialized
?? getConvergentSyncLocalConfig().initialized;
if (!initialized) return async () => {};
const manager = dependencies.manager ?? getCloudSyncManager();
const replica = await manager.loadConvergentReplica();
if (!replica) {
throw new Error('Convergent sync is initialized but its local replica is missing');
}
const baseline = materializeSyncPayloadFromConvergentState(replica.state, {
syncedAt: replica.updatedAt,
});
const state = applyLegacySyncPayload(
replica.state,
baseline,
stripConvergentSyncEnvelope(restoredPayload),
manager.getState().deviceId,
now,
);
return () => manager.saveConvergentReplica({ schemaVersion: 2, state, updatedAt: now });
}