-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsync-engine.ts
More file actions
1136 lines (1001 loc) · 33.7 KB
/
sync-engine.ts
File metadata and controls
1136 lines (1001 loc) · 33.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { AutomergeUrl, Repo, DocHandle } from "@automerge/automerge-repo";
import * as A from "@automerge/automerge";
import {
SyncSnapshot,
SyncResult,
FileDocument,
DirectoryDocument,
ChangeType,
MoveCandidate,
DirectoryConfig,
DetectedChange,
} from "../types";
import {
writeFileContent,
removePath,
getFileExtension,
normalizePath,
getEnhancedMimeType,
formatRelativePath,
findFileInDirectoryHierarchy,
} from "../utils";
import { isContentEqual } from "../utils/content";
import { waitForSync } from "../utils/network-sync";
import { SnapshotManager } from "./snapshot";
import { ChangeDetector } from "./change-detection";
import { MoveDetector } from "./move-detection";
import { out } from "../utils/output";
/**
* Post-sync delay constants for network propagation
* These delays allow the WebSocket protocol to propagate peer changes after
* our changes reach the server. waitForSync only ensures OUR changes reached
* the server, not that we've RECEIVED changes from other peers.
* TODO: remove need for this to exist.
*/
const POST_SYNC_DELAY_MS = 200; // After we pushed changes
/**
* Bidirectional sync engine implementing two-phase sync
*/
export class SyncEngine {
private snapshotManager: SnapshotManager;
private changeDetector: ChangeDetector;
private moveDetector: MoveDetector;
private handlesToWaitOn: DocHandle<unknown>[] = [];
private config: DirectoryConfig;
constructor(
private repo: Repo,
private rootPath: string,
config: DirectoryConfig
) {
this.config = config;
this.snapshotManager = new SnapshotManager(rootPath);
this.changeDetector = new ChangeDetector(
repo,
rootPath,
config.exclude_patterns
);
this.moveDetector = new MoveDetector(config.sync.move_detection_threshold);
}
/**
* Determine if content should be treated as text for Automerge text operations
* Note: This method checks the runtime type. File type detection happens
* during reading with isEnhancedTextFile() which now has better dev file support.
*/
private isTextContent(content: string | Uint8Array): boolean {
// Simply check the actual type of the content
return typeof content === "string";
}
/**
* Set the root directory URL in the snapshot
*/
async setRootDirectoryUrl(url: AutomergeUrl): Promise<void> {
let snapshot = await this.snapshotManager.load();
if (!snapshot) {
snapshot = this.snapshotManager.createEmpty();
}
snapshot.rootDirectoryUrl = url;
await this.snapshotManager.save(snapshot);
}
/**
* Commit local changes only (no network sync)
*/
async commitLocal(): Promise<SyncResult> {
const result: SyncResult = {
success: false,
filesChanged: 0,
directoriesChanged: 0,
errors: [],
warnings: [],
};
try {
// Load current snapshot
let snapshot = await this.snapshotManager.load();
if (!snapshot) {
snapshot = this.snapshotManager.createEmpty();
}
// Detect all changes
const changes = await this.changeDetector.detectChanges(snapshot);
// Detect moves
const { moves, remainingChanges } = await this.moveDetector.detectMoves(
changes,
snapshot
);
// Apply local changes only (no network sync)
const commitResult = await this.pushLocalChanges(
remainingChanges,
moves,
snapshot
);
result.filesChanged += commitResult.filesChanged;
result.directoriesChanged += commitResult.directoriesChanged;
result.errors.push(...commitResult.errors);
result.warnings.push(...commitResult.warnings);
// Touch root directory if any changes were made
const hasChanges =
result.filesChanged > 0 || result.directoriesChanged > 0;
if (hasChanges) {
await this.touchRootDirectory(snapshot);
}
// Save updated snapshot
await this.snapshotManager.save(snapshot);
result.success = result.errors.length === 0;
return result;
} catch (error) {
result.errors.push({
path: this.rootPath,
operation: "commitLocal",
error: error instanceof Error ? error : new Error(String(error)),
recoverable: true,
});
result.success = false;
return result;
}
}
/**
* Run full bidirectional sync
*/
async sync(): Promise<SyncResult> {
const result: SyncResult = {
success: false,
filesChanged: 0,
directoriesChanged: 0,
errors: [],
warnings: [],
timings: {},
};
// Reset handles to wait on
this.handlesToWaitOn = [];
try {
// Load current snapshot
const snapshot =
(await this.snapshotManager.load()) ||
this.snapshotManager.createEmpty();
// Detect all changes
const changes = await this.changeDetector.detectChanges(snapshot);
// Detect moves
const { moves, remainingChanges } = await this.moveDetector.detectMoves(
changes,
snapshot
);
// Phase 1: Push local changes to remote
const phase1Result = await this.pushLocalChanges(
remainingChanges,
moves,
snapshot
);
result.filesChanged += phase1Result.filesChanged;
result.directoriesChanged += phase1Result.directoriesChanged;
result.errors.push(...phase1Result.errors);
result.warnings.push(...phase1Result.warnings);
// Always wait for network sync when enabled (not just when local changes exist)
// This is critical for clone scenarios where we need to pull remote changes
if (this.config.sync_enabled) {
try {
// If we have a root directory URL, wait for it to sync
if (snapshot.rootDirectoryUrl) {
const rootDirUrl = snapshot.rootDirectoryUrl;
const rootHandle = await this.repo.find<DirectoryDocument>(
rootDirUrl
);
this.handlesToWaitOn.push(rootHandle);
}
if (this.handlesToWaitOn.length > 0) {
await waitForSync(
this.handlesToWaitOn,
this.config.sync_server_storage_id
);
// CRITICAL: Wait a bit after our changes reach the server to allow
// time for WebSocket to deliver OTHER peers' changes to us.
// waitForSync only ensures OUR changes reached the server, not that
// we've RECEIVED changes from other peers. This delay allows the
// WebSocket protocol to propagate peer changes before we re-detect.
// Without this, concurrent operations on different peers can miss
// each other due to timing races.
//
// Optimization: Only wait if we pushed changes (shorter delay if no changes)
await new Promise((resolve) =>
setTimeout(resolve, POST_SYNC_DELAY_MS)
);
}
} catch (error) {
out.taskLine(`Network sync failed: ${error}`, true);
result.warnings.push(`Network sync failed: ${error}`);
}
}
// Re-detect remote changes after network sync to ensure fresh state
// This fixes race conditions where we detect changes before server propagation
// NOTE: We DON'T update snapshot heads yet - that would prevent detecting remote changes!
const freshChanges = await this.changeDetector.detectChanges(snapshot);
const freshRemoteChanges = freshChanges.filter(
(c) =>
c.changeType === ChangeType.REMOTE_ONLY ||
c.changeType === ChangeType.BOTH_CHANGED
);
// Phase 2: Pull remote changes to local using fresh detection
const phase2Result = await this.pullRemoteChanges(
freshRemoteChanges,
snapshot
);
result.filesChanged += phase2Result.filesChanged;
result.directoriesChanged += phase2Result.directoriesChanged;
result.errors.push(...phase2Result.errors);
result.warnings.push(...phase2Result.warnings);
// CRITICAL FIX: Update snapshot heads AFTER pulling remote changes
// This ensures that change detection can find remote changes, and we only
// update the snapshot after the filesystem is in sync with the documents
// Update file document heads
for (const [filePath, snapshotEntry] of snapshot.files.entries()) {
try {
const handle = await this.repo.find(snapshotEntry.url);
const currentHeads = handle.heads();
if (!A.equals(currentHeads, snapshotEntry.head)) {
// Update snapshot with current heads after pulling changes
snapshot.files.set(filePath, {
...snapshotEntry,
head: currentHeads,
});
}
} catch (error) {
// Handle might not exist if file was deleted
}
}
// Update directory document heads
for (const [dirPath, snapshotEntry] of snapshot.directories.entries()) {
try {
const handle = await this.repo.find(snapshotEntry.url);
const currentHeads = handle.heads();
if (!A.equals(currentHeads, snapshotEntry.head)) {
// Update snapshot with current heads after pulling changes
snapshot.directories.set(dirPath, {
...snapshotEntry,
head: currentHeads,
});
}
} catch (error) {
// Handle might not exist if directory was deleted
}
}
// Touch root directory if any changes were made during sync
const hasChanges =
result.filesChanged > 0 || result.directoriesChanged > 0;
if (hasChanges) {
await this.touchRootDirectory(snapshot);
}
// Save updated snapshot if not dry run
await this.snapshotManager.save(snapshot);
result.success = result.errors.length === 0;
return result;
} catch (error) {
result.errors.push({
path: "sync",
operation: "full-sync",
error: error as Error,
recoverable: false,
});
return result;
}
}
/**
* Phase 1: Push local changes to Automerge documents
*/
private async pushLocalChanges(
changes: DetectedChange[],
moves: MoveCandidate[],
snapshot: SyncSnapshot
): Promise<SyncResult> {
const result: SyncResult = {
success: true,
filesChanged: 0,
directoriesChanged: 0,
errors: [],
warnings: [],
};
// Process moves first - all detected moves are applied
for (const move of moves) {
try {
await this.applyMoveToRemote(move, snapshot);
result.filesChanged++;
} catch (error) {
result.errors.push({
path: move.fromPath,
operation: "move",
error: error as Error,
recoverable: true,
});
}
}
// Process local changes
const localChanges = changes.filter(
(c) =>
c.changeType === ChangeType.LOCAL_ONLY ||
c.changeType === ChangeType.BOTH_CHANGED
);
for (const change of localChanges) {
try {
await this.applyLocalChangeToRemote(change, snapshot);
result.filesChanged++;
} catch (error) {
result.errors.push({
path: change.path,
operation: "local-to-remote",
error: error as Error,
recoverable: true,
});
}
}
return result;
}
/**
* Phase 2: Pull remote changes to local filesystem
*/
private async pullRemoteChanges(
changes: DetectedChange[],
snapshot: SyncSnapshot
): Promise<SyncResult> {
const result: SyncResult = {
success: true,
filesChanged: 0,
directoriesChanged: 0,
errors: [],
warnings: [],
};
// Process remote changes
const remoteChanges = changes.filter(
(c) =>
c.changeType === ChangeType.REMOTE_ONLY ||
c.changeType === ChangeType.BOTH_CHANGED
);
// Sort changes by dependency order (parents before children)
const sortedChanges = this.sortChangesByDependency(remoteChanges);
for (const change of sortedChanges) {
try {
await this.applyRemoteChangeToLocal(change, snapshot);
result.filesChanged++;
} catch (error) {
result.errors.push({
path: change.path,
operation: "remote-to-local",
error: error as Error,
recoverable: true,
});
}
}
return result;
}
/**
* Apply local file change to remote Automerge document
*/
private async applyLocalChangeToRemote(
change: DetectedChange,
snapshot: SyncSnapshot
): Promise<void> {
const snapshotEntry = snapshot.files.get(change.path);
// CRITICAL: Check for null explicitly, not falsy values
// Empty strings "" and empty Uint8Array are valid file content!
if (change.localContent === null) {
// File was deleted locally
if (snapshotEntry) {
await this.deleteRemoteFile(snapshotEntry.url, snapshot, change.path);
// Remove from directory document
await this.removeFileFromDirectory(snapshot, change.path);
this.snapshotManager.removeFileEntry(snapshot, change.path);
}
return;
}
if (!snapshotEntry) {
// New file
const handle = await this.createRemoteFile(change);
if (handle) {
await this.addFileToDirectory(snapshot, change.path, handle.url);
// CRITICAL FIX: Update snapshot with heads AFTER adding to directory
// The addFileToDirectory call above may have changed the document heads
this.snapshotManager.updateFileEntry(snapshot, change.path, {
path: normalizePath(this.rootPath + "/" + change.path),
url: handle.url,
head: handle.heads(),
extension: getFileExtension(change.path),
mimeType: getEnhancedMimeType(change.path),
});
}
} else {
// Update existing file
await this.updateRemoteFile(
snapshotEntry.url,
change.localContent,
snapshot,
change.path
);
}
}
/**
* Apply remote change to local filesystem
*/
private async applyRemoteChangeToLocal(
change: DetectedChange,
snapshot: SyncSnapshot
): Promise<void> {
const localPath = normalizePath(this.rootPath + "/" + change.path);
if (!change.remoteHead) {
throw new Error(
`No remote head found for remote change to ${change.path}`
);
}
// CRITICAL: Check for null explicitly, not falsy values
// Empty strings "" and empty Uint8Array are valid file content!
if (change.remoteContent === null) {
// File was deleted remotely
await removePath(localPath);
this.snapshotManager.removeFileEntry(snapshot, change.path);
return;
}
// Create or update local file
await writeFileContent(localPath, change.remoteContent);
// Update or create snapshot entry for this file
const snapshotEntry = snapshot.files.get(change.path);
if (snapshotEntry) {
// Update existing entry
snapshotEntry.head = change.remoteHead;
} else {
// Create new snapshot entry for newly discovered remote file
// We need to find the remote file's URL from the directory hierarchy
if (snapshot.rootDirectoryUrl) {
try {
const fileEntry = await findFileInDirectoryHierarchy(
this.repo,
snapshot.rootDirectoryUrl,
change.path
);
if (fileEntry) {
this.snapshotManager.updateFileEntry(snapshot, change.path, {
path: localPath,
url: fileEntry.url,
head: change.remoteHead,
extension: getFileExtension(change.path),
mimeType: getEnhancedMimeType(change.path),
});
}
} catch (error) {
// Failed to update snapshot - file may have been deleted
out.taskLine(
`Warning: Failed to update snapshot for remote file ${change.path}`,
true
);
}
}
}
}
/**
* Apply move to remote documents
*/
private async applyMoveToRemote(
move: MoveCandidate,
snapshot: SyncSnapshot
): Promise<void> {
const fromEntry = snapshot.files.get(move.fromPath);
if (!fromEntry) return;
// Parse paths
const toParts = move.toPath.split("/");
const toFileName = toParts.pop() || "";
const toDirPath = toParts.join("/");
// 1) Remove file entry from old directory document
if (move.fromPath !== move.toPath) {
await this.removeFileFromDirectory(snapshot, move.fromPath);
}
// 2) Ensure destination directory document exists and add file entry there
await this.ensureDirectoryDocument(snapshot, toDirPath);
await this.addFileToDirectory(snapshot, move.toPath, fromEntry.url);
// 3) Update the FileDocument name and content to match new location/state
try {
const handle = await this.repo.find<FileDocument>(fromEntry.url);
const heads = fromEntry.head;
// Update both name and content (if content changed during move)
if (heads && heads.length > 0) {
handle.changeAt(heads, (doc: FileDocument) => {
doc.name = toFileName;
// If new content is provided, update it (handles move + modification case)
if (move.newContent !== undefined) {
doc.content = move.newContent;
}
});
} else {
handle.change((doc: FileDocument) => {
doc.name = toFileName;
// If new content is provided, update it (handles move + modification case)
if (move.newContent !== undefined) {
doc.content = move.newContent;
}
});
}
// Track file handle for network sync
this.handlesToWaitOn.push(handle);
} catch (e) {
// Failed to update file name - file may have been deleted
out.taskLine(
`Warning: Failed to rename ${move.fromPath} to ${move.toPath}`,
true
);
}
// 4) Update snapshot entries
this.snapshotManager.removeFileEntry(snapshot, move.fromPath);
this.snapshotManager.updateFileEntry(snapshot, move.toPath, {
...fromEntry,
path: normalizePath(this.rootPath + "/" + move.toPath),
head: fromEntry.head, // will be updated later when heads advance
});
}
/**
* Create new remote file document
*/
private async createRemoteFile(
change: DetectedChange
): Promise<DocHandle<FileDocument> | null> {
// CRITICAL: Check for null explicitly, not falsy values
// Empty strings "" and empty Uint8Array are valid file content!
if (change.localContent === null) return null;
const isText = this.isTextContent(change.localContent);
// Create initial document structure
const fileDoc: FileDocument = {
"@patchwork": { type: "file" },
name: change.path.split("/").pop() || "",
extension: getFileExtension(change.path),
mimeType: getEnhancedMimeType(change.path),
content: change.localContent,
metadata: {
permissions: 0o644,
},
};
const handle = this.repo.create(fileDoc);
// For text files, set the actual content
if (isText && typeof change.localContent === "string") {
handle.change((doc: FileDocument) => {
doc.content = change.localContent as string;
});
}
// Always track newly created files for network sync
// (they always represent a change that needs to sync)
this.handlesToWaitOn.push(handle);
return handle;
}
/**
* Update existing remote file document
*/
private async updateRemoteFile(
url: AutomergeUrl,
content: string | Uint8Array,
snapshot: SyncSnapshot,
filePath: string
): Promise<void> {
const handle = await this.repo.find<FileDocument>(url);
// Check if content actually changed before tracking for sync
const doc = await handle.doc();
const currentContent = doc?.content;
const contentChanged = !isContentEqual(content, currentContent);
// CRITICAL FIX: Always update snapshot heads, even when content is identical
// This prevents stale head issues that cause false change detection
const snapshotEntry = snapshot.files.get(filePath);
if (snapshotEntry) {
// Update snapshot with current document heads
snapshot.files.set(filePath, {
...snapshotEntry,
head: handle.heads(),
});
}
if (!contentChanged) {
// Content is identical, but we've updated the snapshot heads above
// This prevents fresh change detection from seeing stale heads
return;
}
const heads = snapshotEntry?.head;
if (!heads) {
throw new Error(`No heads found for ${url}`);
}
handle.changeAt(heads, (doc: FileDocument) => {
doc.content = content;
});
// Update snapshot with new heads after content change
if (snapshotEntry) {
snapshot.files.set(filePath, {
...snapshotEntry,
head: handle.heads(),
});
}
// Only track files that actually changed content
this.handlesToWaitOn.push(handle);
}
/**
* Delete remote file document
*/
private async deleteRemoteFile(
url: AutomergeUrl,
snapshot?: SyncSnapshot,
filePath?: string
): Promise<void> {
// In Automerge, we don't actually delete documents
// They become orphaned and will be garbage collected
// For now, we just mark them as deleted by clearing content
const handle = await this.repo.find<FileDocument>(url);
// const doc = await handle.doc(); // no longer needed
let heads;
if (snapshot && filePath) {
heads = snapshot.files.get(filePath)?.head;
}
if (heads) {
handle.changeAt(heads, (doc: FileDocument) => {
doc.content = "";
});
} else {
handle.change((doc: FileDocument) => {
doc.content = "";
});
}
}
/**
* Add file entry to appropriate directory document (maintains hierarchy)
*/
private async addFileToDirectory(
snapshot: SyncSnapshot,
filePath: string,
fileUrl: AutomergeUrl
): Promise<void> {
if (!snapshot.rootDirectoryUrl) return;
const pathParts = filePath.split("/");
const fileName = pathParts.pop() || "";
const directoryPath = pathParts.join("/");
// Get or create the parent directory document
const parentDirUrl = await this.ensureDirectoryDocument(
snapshot,
directoryPath
);
const dirHandle = await this.repo.find<DirectoryDocument>(parentDirUrl);
let didChange = false;
const snapshotEntry = snapshot.directories.get(directoryPath);
const heads = snapshotEntry?.head;
if (heads) {
dirHandle.changeAt(heads, (doc: DirectoryDocument) => {
const existingIndex = doc.docs.findIndex(
(entry) => entry.name === fileName && entry.type === "file"
);
if (existingIndex === -1) {
doc.docs.push({
name: fileName,
type: "file",
url: fileUrl,
});
didChange = true;
}
});
} else {
dirHandle.change((doc: DirectoryDocument) => {
const existingIndex = doc.docs.findIndex(
(entry) => entry.name === fileName && entry.type === "file"
);
if (existingIndex === -1) {
doc.docs.push({
name: fileName,
type: "file",
url: fileUrl,
});
didChange = true;
}
});
}
if (didChange) {
this.handlesToWaitOn.push(dirHandle);
// CRITICAL FIX: Update snapshot with new directory heads immediately
// This prevents stale head issues that cause convergence problems
if (snapshotEntry) {
snapshotEntry.head = dirHandle.heads();
}
}
}
/**
* Ensure directory document exists for the given path, creating hierarchy as needed
* First checks for existing shared directories before creating new ones
*/
private async ensureDirectoryDocument(
snapshot: SyncSnapshot,
directoryPath: string
): Promise<AutomergeUrl> {
// Root directory case
if (!directoryPath || directoryPath === "") {
return snapshot.rootDirectoryUrl!;
}
// Check if we already have this directory in snapshot
const existingDir = snapshot.directories.get(directoryPath);
if (existingDir) {
return existingDir.url;
}
// Split path into parent and current directory name
const pathParts = directoryPath.split("/");
const currentDirName = pathParts.pop() || "";
const parentPath = pathParts.join("/");
// Ensure parent directory exists first (recursive)
const parentDirUrl = await this.ensureDirectoryDocument(
snapshot,
parentPath
);
// DISCOVERY: Check if directory already exists in parent on server
try {
const parentHandle = await this.repo.find<DirectoryDocument>(
parentDirUrl
);
const parentDoc = await parentHandle.doc();
if (parentDoc) {
const existingDirEntry = parentDoc.docs.find(
(entry: { name: string; type: string; url: AutomergeUrl }) =>
entry.name === currentDirName && entry.type === "folder"
);
if (existingDirEntry) {
// Resolve the actual directory handle and use its current heads
// Directory entries in parent docs may not carry valid heads
try {
const childDirHandle = await this.repo.find<DirectoryDocument>(
existingDirEntry.url
);
const childHeads = childDirHandle.heads();
// Update snapshot with discovered directory using validated heads
this.snapshotManager.updateDirectoryEntry(snapshot, directoryPath, {
path: normalizePath(this.rootPath + "/" + directoryPath),
url: existingDirEntry.url,
head: childHeads,
entries: [],
});
return existingDirEntry.url;
} catch (resolveErr) {
// Failed to resolve directory - fall through to create a fresh directory document
}
}
}
} catch (error) {
// Failed to check for existing directory - will create new one
}
// CREATE: Directory doesn't exist, create new one
const dirDoc: DirectoryDocument = {
"@patchwork": { type: "folder" },
docs: [],
};
const dirHandle = this.repo.create(dirDoc);
// Add this directory to its parent
const parentHandle = await this.repo.find<DirectoryDocument>(parentDirUrl);
let didChange = false;
parentHandle.change((doc: DirectoryDocument) => {
// Double-check that entry doesn't exist (race condition protection)
const existingIndex = doc.docs.findIndex(
(entry: { name: string; type: string; url: AutomergeUrl }) =>
entry.name === currentDirName && entry.type === "folder"
);
if (existingIndex === -1) {
doc.docs.push({
name: currentDirName,
type: "folder",
url: dirHandle.url,
});
didChange = true;
}
});
// Track directory handles for sync
this.handlesToWaitOn.push(dirHandle);
if (didChange) {
this.handlesToWaitOn.push(parentHandle);
// CRITICAL FIX: Update parent directory heads in snapshot immediately
// This prevents stale head issues when parent directory is modified
const parentSnapshotEntry = snapshot.directories.get(parentPath);
if (parentSnapshotEntry) {
parentSnapshotEntry.head = parentHandle.heads();
}
}
// Update snapshot with new directory
this.snapshotManager.updateDirectoryEntry(snapshot, directoryPath, {
path: normalizePath(this.rootPath + "/" + directoryPath),
url: dirHandle.url,
head: dirHandle.heads(),
entries: [],
});
return dirHandle.url;
}
/**
* Remove file entry from directory document
*/
private async removeFileFromDirectory(
snapshot: SyncSnapshot,
filePath: string
): Promise<void> {
if (!snapshot.rootDirectoryUrl) return;
const pathParts = filePath.split("/");
const fileName = pathParts.pop() || "";
const directoryPath = pathParts.join("/");
// Get the parent directory URL
let parentDirUrl: AutomergeUrl;
if (!directoryPath || directoryPath === "") {
parentDirUrl = snapshot.rootDirectoryUrl;
} else {
const existingDir = snapshot.directories.get(directoryPath);
if (!existingDir) {
// Directory not found - file may already be removed
return;
}
parentDirUrl = existingDir.url;
}
try {
const dirHandle = await this.repo.find<DirectoryDocument>(parentDirUrl);
// Track this handle for network sync waiting
this.handlesToWaitOn.push(dirHandle);
const snapshotEntry = snapshot.directories.get(directoryPath);
const heads = snapshotEntry?.head;
let didChange = false;
if (heads) {
dirHandle.changeAt(heads, (doc: DirectoryDocument) => {
const indexToRemove = doc.docs.findIndex(
(entry) => entry.name === fileName && entry.type === "file"
);
if (indexToRemove !== -1) {
doc.docs.splice(indexToRemove, 1);
didChange = true;
out.taskLine(
`Removed ${fileName} from ${
formatRelativePath(directoryPath) || "root"
}`
);
}
});
} else {
dirHandle.change((doc: DirectoryDocument) => {
const indexToRemove = doc.docs.findIndex(
(entry) => entry.name === fileName && entry.type === "file"
);
if (indexToRemove !== -1) {
doc.docs.splice(indexToRemove, 1);
didChange = true;
out.taskLine(
`Removed ${fileName} from ${
formatRelativePath(directoryPath) || "root"
}`
);
}
});
}
// CRITICAL FIX: Update snapshot with new directory heads immediately
// This prevents stale head issues that cause convergence problems
if (didChange && snapshotEntry) {
snapshotEntry.head = dirHandle.heads();
}
} catch (error) {
// Failed to remove from directory - re-throw for caller to handle
throw error;
}
}
/**
* Sort changes by dependency order
*/
private sortChangesByDependency(changes: DetectedChange[]): DetectedChange[] {
// Sort by path depth (shallower paths first)
return changes.sort((a, b) => {
const depthA = a.path.split("/").length;
const depthB = b.path.split("/").length;
return depthA - depthB;
});
}
/**
* Get sync status
*/
async getStatus(): Promise<{
snapshot: SyncSnapshot | null;
hasChanges: boolean;
changeCount: number;
lastSync: Date | null;
}> {
const snapshot = await this.snapshotManager.load();
if (!snapshot) {