nemu-cli 1.5.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,636 @@
1
+ import { randomBytes } from "node:crypto";
2
+ import { readFile } from "node:fs/promises";
3
+ import { join } from "node:path";
4
+ import { CliError } from "../errors.js";
5
+ import { RemoteError } from "./api.js";
6
+ import { delete_local, read_if, rename_local, scan_folder, write_atomic_file, } from "./files.js";
7
+ import { ignore_fingerprint, is_ignored, parse_ignore } from "./ignore.js";
8
+ import { acquire_lock } from "./lock.js";
9
+ import { merge_text } from "./merge.js";
10
+ import { check_path, conflict_path, path_key } from "./paths.js";
11
+ import { counted_deletes, mass_delete_blocked, plan_sync, } from "./plan.js";
12
+ import { empty_state, read_marker, read_state, write_state, } from "./state.js";
13
+ export const DEFAULT_MAX_FILE_BYTES = 4 * 1024 * 1024;
14
+ export const QUIET_MS = 2_000;
15
+ class GroupFailed extends Error {
16
+ retry;
17
+ constructor(retry) {
18
+ super("group failed");
19
+ this.retry = retry;
20
+ }
21
+ }
22
+ function wait(ms) {
23
+ return new Promise((resolve) => setTimeout(resolve, ms));
24
+ }
25
+ function decode(content) {
26
+ try {
27
+ return new TextDecoder("utf-8", { fatal: true }).decode(content);
28
+ }
29
+ catch {
30
+ return null;
31
+ }
32
+ }
33
+ function is_stop(error) {
34
+ return error instanceof RemoteError && (error.status === 401 || error.status === 403);
35
+ }
36
+ function is_storage_block(error) {
37
+ return (error instanceof RemoteError &&
38
+ (error.status === 402 || (error.status === 413 && error.code !== "file_too_large")));
39
+ }
40
+ export function group_of(action) {
41
+ switch (action.kind) {
42
+ case "delete_local":
43
+ case "forget":
44
+ return `key:${action.key}`;
45
+ case "rename_local":
46
+ return `local:${path_key(action.from)}`;
47
+ case "move_remote":
48
+ case "pull":
49
+ case "delete_remote":
50
+ return `note:${action.note_id}`;
51
+ case "push":
52
+ return action.note_id === null ? `local:${path_key(action.local.disk)}` : `note:${action.note_id}`;
53
+ case "merge":
54
+ return `note:${action.base.note_id}`;
55
+ case "keep_both":
56
+ case "adopt":
57
+ return `note:${action.remote.note_id}`;
58
+ }
59
+ }
60
+ function linked_groups(actions) {
61
+ const parent = new Map();
62
+ const find = (group) => {
63
+ let current = group;
64
+ while (parent.has(current) && parent.get(current) !== current)
65
+ current = parent.get(current);
66
+ return current;
67
+ };
68
+ const join_groups = (left, right) => {
69
+ const a = find(left);
70
+ const b = find(right);
71
+ if (a !== b)
72
+ parent.set(b, a);
73
+ if (!parent.has(a))
74
+ parent.set(a, a);
75
+ };
76
+ const key_owner = new Map();
77
+ for (const action of actions) {
78
+ const group = group_of(action);
79
+ if (!parent.has(group))
80
+ parent.set(group, group);
81
+ const keys = [];
82
+ if (action.kind === "rename_local")
83
+ keys.push(path_key(action.from), path_key(action.to));
84
+ if (action.kind === "forget" || action.kind === "delete_local")
85
+ keys.push(action.key);
86
+ if (action.kind === "push" || action.kind === "merge")
87
+ keys.push(path_key(action.local.disk), path_key(action.local.path));
88
+ if (action.kind === "pull")
89
+ keys.push(path_key(action.path));
90
+ if (action.kind === "keep_both")
91
+ keys.push(path_key(action.remote.path), path_key(action.copy_path));
92
+ if (action.kind === "adopt")
93
+ keys.push(path_key(action.remote.path));
94
+ if (action.kind === "move_remote")
95
+ keys.push(path_key(action.from), path_key(action.to));
96
+ for (const key of keys) {
97
+ const owner = key_owner.get(key);
98
+ if (owner === undefined)
99
+ key_owner.set(key, group);
100
+ else
101
+ join_groups(owner, group);
102
+ }
103
+ }
104
+ const resolved = new Map();
105
+ for (const group of parent.keys())
106
+ resolved.set(group, find(group));
107
+ return resolved;
108
+ }
109
+ async function load_ignore(root, settings) {
110
+ const source = await readFile(join(root, ".nemuignore"), "utf-8").catch(() => "");
111
+ const options = {
112
+ obsidian: settings.obsidian,
113
+ obsidian_plugins: settings.obsidian_plugins,
114
+ rules: parse_ignore(source),
115
+ };
116
+ return { options, fingerprint: ignore_fingerprint(options, source) };
117
+ }
118
+ function entry_of(note) {
119
+ return { path: note.path.normalize("NFC"), note_id: note.id, version: note.version, hash: note.hash };
120
+ }
121
+ async function full_view(api) {
122
+ const listing = await api.notes();
123
+ const entries = new Map();
124
+ for (const note of listing.notes) {
125
+ if (note.deleted)
126
+ continue;
127
+ entries.set(path_key(note.path), entry_of(note));
128
+ }
129
+ return { entries, revision: listing.revision, full: true };
130
+ }
131
+ async function remote_view(api, state) {
132
+ if (state.cursor === null || state.full_next)
133
+ return full_view(api);
134
+ const changed = [];
135
+ let since = state.cursor;
136
+ try {
137
+ for (let page = 0; page < 10_000; page += 1) {
138
+ const result = await api.changes(since);
139
+ changed.push(...result.notes);
140
+ since = result.revision;
141
+ if (!result.has_more)
142
+ break;
143
+ }
144
+ }
145
+ catch (error) {
146
+ if (error instanceof RemoteError && error.status === 410)
147
+ return full_view(api);
148
+ throw error;
149
+ }
150
+ const entries = new Map();
151
+ const key_of_id = new Map();
152
+ for (const base of Object.values(state.files)) {
153
+ const key = path_key(base.path);
154
+ entries.set(key, { path: base.path, note_id: base.note_id, version: base.version, hash: base.hash });
155
+ key_of_id.set(base.note_id, key);
156
+ }
157
+ changed.sort((left, right) => left.revision - right.revision);
158
+ for (const note of changed) {
159
+ const previous = key_of_id.get(note.id);
160
+ if (previous !== undefined && entries.get(previous)?.note_id === note.id) {
161
+ entries.delete(previous);
162
+ key_of_id.delete(note.id);
163
+ }
164
+ if (note.deleted)
165
+ continue;
166
+ const key = path_key(note.path);
167
+ const displaced = entries.get(key);
168
+ if (displaced !== undefined)
169
+ key_of_id.delete(displaced.note_id);
170
+ entries.set(key, entry_of(note));
171
+ key_of_id.set(note.id, key);
172
+ }
173
+ return { entries, revision: since, full: false };
174
+ }
175
+ export function new_report(vault_id, dry_run) {
176
+ return {
177
+ vault_id,
178
+ dry_run,
179
+ full: false,
180
+ cycles: 0,
181
+ pulled: [],
182
+ pushed: [],
183
+ merged: [],
184
+ conflicts: [],
185
+ moved: [],
186
+ deleted_local: [],
187
+ deleted_remote: [],
188
+ skipped: [],
189
+ notices: [],
190
+ waiting: [],
191
+ blocked: null,
192
+ planned: [],
193
+ };
194
+ }
195
+ export async function run_sync(options) {
196
+ const marker = await read_marker(options.root);
197
+ if (marker === null) {
198
+ throw new CliError(`${options.root} has no .nemu/vault.json, so it is not a linked vault folder. Nothing was synced. Run nemu vault link first, and check that the drive or folder is really there.`, "not_linked");
199
+ }
200
+ if (marker.vault_id !== options.api.vault_id) {
201
+ throw new CliError("This folder is linked to a different vault.", "vault_mismatch");
202
+ }
203
+ const lock = options.lock === false || options.dry_run === true
204
+ ? null
205
+ : await acquire_lock(options.root, randomBytes(8).toString("hex"));
206
+ try {
207
+ return await sync_locked(options);
208
+ }
209
+ finally {
210
+ await lock?.release();
211
+ }
212
+ }
213
+ async function sync_locked(options) {
214
+ const hasher = (content) => options.api.hash(content);
215
+ const dry_run = options.dry_run === true;
216
+ const report = new_report(options.api.vault_id, dry_run);
217
+ const emit = (event) => options.on_event?.(event);
218
+ const quiet_ms = options.quiet_ms ?? QUIET_MS;
219
+ const now = options.now ?? (() => new Date());
220
+ const stored = await read_state(options.state_path);
221
+ const state = stored !== null && stored.vault_id === options.api.vault_id
222
+ ? stored
223
+ : empty_state(options.api.vault_id, options.root, options.settings);
224
+ state.root = options.root;
225
+ if (options.settings?.conflicts !== undefined)
226
+ state.settings.conflicts = options.settings.conflicts;
227
+ if (options.settings?.obsidian !== undefined)
228
+ state.settings.obsidian = options.settings.obsidian;
229
+ if (options.settings?.obsidian_plugins !== undefined) {
230
+ state.settings.obsidian_plugins = options.settings.obsidian_plugins;
231
+ }
232
+ const ignore = await load_ignore(options.root, state.settings);
233
+ if (ignore.fingerprint !== state.ignore_fingerprint)
234
+ state.full_next = true;
235
+ state.ignore_fingerprint = ignore.fingerprint;
236
+ const save = async () => {
237
+ if (!dry_run)
238
+ await write_state(options.state_path, state);
239
+ };
240
+ let info;
241
+ try {
242
+ info = await options.api.info();
243
+ }
244
+ catch (error) {
245
+ if (is_stop(error))
246
+ throw stopped(error);
247
+ throw error;
248
+ }
249
+ if (info.limits !== undefined)
250
+ state.limits = info.limits;
251
+ const max_file_bytes = state.limits?.max_file_bytes ?? DEFAULT_MAX_FILE_BYTES;
252
+ const max_cycles = options.max_cycles ?? 4;
253
+ for (let cycle = 1; cycle <= max_cycles; cycle += 1) {
254
+ report.cycles = cycle;
255
+ let view;
256
+ try {
257
+ view = await remote_view(options.api, state);
258
+ }
259
+ catch (error) {
260
+ if (is_stop(error))
261
+ throw stopped(error);
262
+ throw error;
263
+ }
264
+ report.full = report.full || view.full;
265
+ const remote = new Map();
266
+ for (const [key, entry] of view.entries) {
267
+ if (is_ignored(entry.path, ignore.options))
268
+ continue;
269
+ if (!check_path(entry.path).ok)
270
+ continue;
271
+ remote.set(key, entry);
272
+ }
273
+ const scan_started = Date.now();
274
+ const base = new Map();
275
+ for (const entry of Object.values(state.files)) {
276
+ if (is_ignored(entry.path, ignore.options))
277
+ continue;
278
+ base.set(path_key(entry.path), entry);
279
+ }
280
+ const scan = await scan_folder(options.root, ignore.options, max_file_bytes, (key) => base.get(key)?.path, hasher);
281
+ if (cycle === 1) {
282
+ report.skipped.push(...scan.skipped);
283
+ for (const skipped of scan.skipped)
284
+ emit({ kind: "skipped", ...skipped });
285
+ }
286
+ const skipped_keys = new Set(scan.skipped.map((entry) => path_key(entry.path)));
287
+ const plan = plan_sync({
288
+ base,
289
+ local: scan.files,
290
+ remote,
291
+ skipped: skipped_keys,
292
+ conflicts: state.settings.conflicts,
293
+ device: options.device,
294
+ now: now(),
295
+ });
296
+ for (const notice of plan.notices) {
297
+ if (!report.notices.includes(notice)) {
298
+ report.notices.push(notice);
299
+ emit({ kind: "notice", message: notice });
300
+ }
301
+ }
302
+ const deletes = counted_deletes(plan);
303
+ if (!options.allow_deletes && mass_delete_blocked(deletes, base.size)) {
304
+ throw new CliError(`This sync would delete ${deletes} of the ${base.size} files in the vault, so it stopped and deleted nothing. If the folder is on a drive that is not mounted, or the path is wrong, fix that first. If you really deleted them, run nemu sync --allow-deletes.`, "mass_delete");
305
+ }
306
+ if (dry_run) {
307
+ report.planned = plan.actions;
308
+ return report;
309
+ }
310
+ const taken = new Set([...base.keys(), ...scan.files.keys(), ...remote.keys(), ...skipped_keys]);
311
+ for (const action of plan.actions) {
312
+ if (action.kind === "keep_both")
313
+ taken.add(path_key(action.copy_path));
314
+ if (action.kind === "rename_local")
315
+ taken.add(path_key(action.to));
316
+ }
317
+ const known = new Map();
318
+ for (const entry of remote.values())
319
+ known.set(entry.note_id, entry.version);
320
+ const groups = linked_groups(plan.actions);
321
+ const failed = new Set();
322
+ let retry = false;
323
+ let stale = plan.deferred;
324
+ let checkpoint = 0;
325
+ const record = (note) => {
326
+ known.set(note.note_id, note.version);
327
+ for (const [key, existing] of Object.entries(state.files)) {
328
+ if (existing.note_id === note.note_id && key !== path_key(note.path))
329
+ delete state.files[key];
330
+ }
331
+ state.files[path_key(note.path)] = {
332
+ path: note.path.normalize("NFC"),
333
+ note_id: note.note_id,
334
+ version: note.version,
335
+ hash: note.hash,
336
+ };
337
+ };
338
+ const record_note = (note) => record({ path: note.path, note_id: note.id, version: note.version, hash: note.hash });
339
+ const stable = async (local) => {
340
+ const ready_at = scan_started + quiet_ms;
341
+ if (Date.now() < ready_at)
342
+ await wait(ready_at - Date.now());
343
+ const content = await read_if(options.root, local.disk, local.hash, hasher);
344
+ if (content === null) {
345
+ if (!report.waiting.includes(local.path))
346
+ report.waiting.push(local.path);
347
+ throw new GroupFailed(true);
348
+ }
349
+ return content;
350
+ };
351
+ const log_merge = async (path, merged_version, remote_version, local) => {
352
+ if (merged_version <= remote_version)
353
+ return;
354
+ try {
355
+ await options.api.record_merge({ path, merged_version, remote_version, local });
356
+ }
357
+ catch (error) {
358
+ const reason = error instanceof Error ? error.message : String(error);
359
+ const message = `${path} was merged, but the merge log did not record it: ${reason}`;
360
+ report.notices.push(message);
361
+ emit({ kind: "notice", message });
362
+ }
363
+ };
364
+ const push_content = async (path, content, note_id) => {
365
+ if (report.blocked !== null)
366
+ throw new GroupFailed(false);
367
+ const version = note_id === null ? null : (known.get(note_id) ?? null);
368
+ try {
369
+ return await options.api.write(path, content, version);
370
+ }
371
+ catch (error) {
372
+ if (error instanceof RemoteError && error.status === 409) {
373
+ const current = error.current;
374
+ if (current !== null && current !== undefined && !current.deleted && current.hash === hasher(content)) {
375
+ record_note(current);
376
+ return current;
377
+ }
378
+ throw new GroupFailed(true);
379
+ }
380
+ if (is_storage_block(error)) {
381
+ const failure = error;
382
+ report.blocked = { code: failure.code, message: failure.message };
383
+ emit({ kind: "notice", message: `Uploads stopped: ${failure.message} Downloads continue.` });
384
+ throw new GroupFailed(false);
385
+ }
386
+ if (error instanceof RemoteError &&
387
+ (error.code === "file_too_large" || error.status === 400 || error.status === 422)) {
388
+ report.skipped.push({ path, reason: error.message });
389
+ emit({ kind: "skipped", path, reason: error.message });
390
+ throw new GroupFailed(false);
391
+ }
392
+ throw error;
393
+ }
394
+ };
395
+ const fetch_remote = async (path, note_id) => {
396
+ try {
397
+ const fetched = await options.api.read(path);
398
+ if (fetched.id !== note_id || hasher(fetched.content) !== fetched.hash) {
399
+ stale = true;
400
+ throw new GroupFailed(true);
401
+ }
402
+ return fetched;
403
+ }
404
+ catch (error) {
405
+ if (error instanceof RemoteError && error.status === 404) {
406
+ stale = true;
407
+ throw new GroupFailed(true);
408
+ }
409
+ throw error;
410
+ }
411
+ };
412
+ const keep_both = async (remote_entry, local, copy_path, local_content, remote_content) => {
413
+ if (!(await write_atomic_file(options.root, copy_path, local_content, null, hasher)))
414
+ throw new GroupFailed(true);
415
+ const fetched = remote_content ?? (await fetch_remote(remote_entry.path, remote_entry.note_id));
416
+ if (!(await write_atomic_file(options.root, remote_entry.path, fetched.content, { disk: local.disk, hash: local.hash }, hasher))) {
417
+ throw new GroupFailed(true);
418
+ }
419
+ record({ path: remote_entry.path, note_id: fetched.id, version: fetched.version, hash: fetched.hash });
420
+ report.conflicts.push({ path: remote_entry.path, copy: copy_path });
421
+ emit({ kind: "conflict", path: remote_entry.path, copy: copy_path });
422
+ const pushed = await push_content(copy_path, local_content, null).catch((error) => {
423
+ if (error instanceof GroupFailed)
424
+ return null;
425
+ throw error;
426
+ });
427
+ if (pushed !== null)
428
+ record_note(pushed);
429
+ };
430
+ const execute = async (action) => {
431
+ switch (action.kind) {
432
+ case "delete_local": {
433
+ if (!(await delete_local(options.root, action.disk, action.hash, hasher)))
434
+ throw new GroupFailed(true);
435
+ report.deleted_local.push(action.disk);
436
+ emit({ kind: "deleted_local", path: action.disk });
437
+ return;
438
+ }
439
+ case "rename_local": {
440
+ if (!(await rename_local(options.root, action.from, action.to)))
441
+ throw new GroupFailed(true);
442
+ if (action.reason !== "conflict") {
443
+ report.moved.push({ from: action.from, to: action.to });
444
+ emit({ kind: "moved", from: action.from, to: action.to });
445
+ }
446
+ return;
447
+ }
448
+ case "forget": {
449
+ delete state.files[action.key];
450
+ return;
451
+ }
452
+ case "move_remote": {
453
+ if (report.blocked !== null)
454
+ throw new GroupFailed(false);
455
+ const version = known.get(action.note_id);
456
+ if (version === undefined)
457
+ throw new GroupFailed(true);
458
+ try {
459
+ const note = await options.api.move(action.from, action.to, version);
460
+ delete state.files[path_key(action.from)];
461
+ record_note(note);
462
+ report.moved.push({ from: action.from, to: action.to });
463
+ emit({ kind: "moved", from: action.from, to: action.to });
464
+ }
465
+ catch (error) {
466
+ if (error instanceof RemoteError && (error.status === 409 || error.status === 404)) {
467
+ stale = true;
468
+ throw new GroupFailed(true);
469
+ }
470
+ if (is_storage_block(error)) {
471
+ report.blocked = { code: error.code, message: error.message };
472
+ throw new GroupFailed(false);
473
+ }
474
+ throw error;
475
+ }
476
+ return;
477
+ }
478
+ case "pull": {
479
+ const fetched = await fetch_remote(action.path, action.note_id);
480
+ const written = await write_atomic_file(options.root, action.path, fetched.content, action.replace === null ? null : { disk: action.replace.disk, hash: action.replace.hash }, hasher);
481
+ if (!written)
482
+ throw new GroupFailed(true);
483
+ record({ path: action.path, note_id: fetched.id, version: fetched.version, hash: fetched.hash });
484
+ report.pulled.push(action.path);
485
+ emit({ kind: "pulled", path: action.path });
486
+ return;
487
+ }
488
+ case "adopt": {
489
+ record({
490
+ path: action.remote.path,
491
+ note_id: action.remote.note_id,
492
+ version: known.get(action.remote.note_id) ?? action.remote.version,
493
+ hash: action.remote.hash,
494
+ });
495
+ return;
496
+ }
497
+ case "push": {
498
+ const content = await stable(action.local);
499
+ const note = await push_content(action.local.path, content, action.note_id);
500
+ if (note === null)
501
+ return;
502
+ record_note(note);
503
+ if (action.reason === "restore") {
504
+ report.pushed.push(action.local.path);
505
+ emit({ kind: "restored", path: action.local.path });
506
+ return;
507
+ }
508
+ if (action.reason === "conflict")
509
+ return;
510
+ report.pushed.push(action.local.path);
511
+ emit({ kind: "pushed", path: action.local.path });
512
+ return;
513
+ }
514
+ case "merge": {
515
+ const local_content = await stable(action.local);
516
+ const fetched = await fetch_remote(action.remote.path, action.remote.note_id);
517
+ let base_content = null;
518
+ try {
519
+ base_content = await options.api.read_version(action.base.note_id, action.base.version);
520
+ }
521
+ catch (error) {
522
+ if (!(error instanceof RemoteError) || error.status !== 404)
523
+ throw error;
524
+ }
525
+ const texts = [base_content, local_content, fetched.content].map((value) => value === null ? null : decode(value));
526
+ const merged = base_content !== null && hasher(base_content) === action.base.hash && texts.every((text) => text !== null)
527
+ ? merge_text(texts[0], texts[1], texts[2])
528
+ : { kind: "conflict" };
529
+ if (merged.kind === "conflict") {
530
+ const copy_path = conflict_path(action.remote.path, options.device, now(), (key) => taken.has(key));
531
+ taken.add(path_key(copy_path));
532
+ await keep_both(action.remote, action.local, copy_path, local_content, fetched);
533
+ return;
534
+ }
535
+ const bytes = Buffer.from(merged.text, "utf-8");
536
+ if (!(await write_atomic_file(options.root, action.remote.path, bytes, { disk: action.local.disk, hash: action.local.hash }, hasher))) {
537
+ throw new GroupFailed(true);
538
+ }
539
+ known.set(fetched.id, fetched.version);
540
+ if (hasher(bytes) === fetched.hash) {
541
+ record({ path: action.remote.path, note_id: fetched.id, version: fetched.version, hash: fetched.hash });
542
+ }
543
+ else {
544
+ const note = await push_content(action.remote.path, bytes, fetched.id);
545
+ if (note !== null) {
546
+ record_note(note);
547
+ await log_merge(action.remote.path, note.version, fetched.version, local_content);
548
+ }
549
+ }
550
+ report.merged.push(action.remote.path);
551
+ emit({ kind: "merged", path: action.remote.path });
552
+ return;
553
+ }
554
+ case "keep_both": {
555
+ const local_content = await stable(action.local);
556
+ await keep_both(action.remote, action.local, action.copy_path, local_content);
557
+ return;
558
+ }
559
+ case "delete_remote": {
560
+ if (report.blocked !== null)
561
+ throw new GroupFailed(false);
562
+ const version = known.get(action.note_id);
563
+ if (version === undefined)
564
+ throw new GroupFailed(true);
565
+ try {
566
+ await options.api.remove(action.path, version);
567
+ }
568
+ catch (error) {
569
+ if (error instanceof RemoteError && error.status === 404) {
570
+ delete state.files[path_key(action.path)];
571
+ return;
572
+ }
573
+ if (error instanceof RemoteError && error.status === 409) {
574
+ stale = true;
575
+ throw new GroupFailed(true);
576
+ }
577
+ throw error;
578
+ }
579
+ delete state.files[path_key(action.path)];
580
+ if (action.counted) {
581
+ report.deleted_remote.push(action.path);
582
+ emit({ kind: "deleted_remote", path: action.path });
583
+ }
584
+ return;
585
+ }
586
+ }
587
+ };
588
+ try {
589
+ for (const action of plan.actions) {
590
+ const group = groups.get(group_of(action)) ?? group_of(action);
591
+ if (failed.has(group))
592
+ continue;
593
+ try {
594
+ await execute(action);
595
+ }
596
+ catch (error) {
597
+ if (error instanceof GroupFailed) {
598
+ failed.add(group);
599
+ stale = true;
600
+ if (error.retry)
601
+ retry = true;
602
+ continue;
603
+ }
604
+ if (is_stop(error))
605
+ throw stopped(error);
606
+ throw error;
607
+ }
608
+ checkpoint += 1;
609
+ if (checkpoint % 50 === 0)
610
+ await save();
611
+ }
612
+ }
613
+ catch (error) {
614
+ state.full_next = true;
615
+ if (error instanceof CliError)
616
+ await save();
617
+ throw error;
618
+ }
619
+ state.cursor = view.revision;
620
+ state.full_next = stale;
621
+ state.synced_at = now().toISOString();
622
+ await save();
623
+ if (!retry)
624
+ break;
625
+ }
626
+ return report;
627
+ }
628
+ function stopped(error) {
629
+ const failure = error;
630
+ const reason = failure.code === "device_revoked"
631
+ ? "this device was revoked"
632
+ : failure.status === 401
633
+ ? "the sign in or key is no longer valid"
634
+ : "access to this vault was refused";
635
+ return new CliError(`Sync stopped because ${reason}. Nothing was deleted on this computer. Sign in again with nemu login (or nemu login key) and run nemu sync.`, "sync_stopped", { status: failure.status });
636
+ }