@wirecat/cli-core 0.726.0 → 0.727.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.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,28 @@ Notable changes to `@wirecat/cli-core` (`@wirecat/cli-messaging` up to 0.261.0,
5
5
  minor version may break callers, and says how under "Changed — may break callers". `pnpm docs:check` checks the
6
6
  shape of this file.
7
7
 
8
+ ## 0.727.0 — 23.11.2026
9
+
10
+ ### Added
11
+
12
+ - **A file mirror works with a remote that rewrites the text it is sent.** A `FileTarget` with `rewrites: true` (Notion,
13
+ which keeps a page as blocks and gives back its own Markdown) no longer turns every upload into a conflict. When an
14
+ upload reads back with other bytes, the mirror keeps the local file's version, writes the remote's form into the
15
+ local file while it is unchanged, and logs it. Both sides then hold the same bytes, so the next sync compares and
16
+ merges against one base. `mirrors conflicts resolve` does the same with the final content. The result lists such
17
+ files in `rewritten` ([DOCUMENTS.md](docs/dev/DOCUMENTS.md)). Without `rewrites`, nothing changes (#3232).
18
+
19
+ ### Changed — may break callers
20
+
21
+ - **A file mirror keeps earlier versions for 365 days, not 30.** `fileVersions(dir, now, keepForDays)` takes the
22
+ period, so a CLI passes the profile's `keep_versions_for_days` (`keepVersionsForDays(settings)`), as documents
23
+ history does. Without it the default is 365 days, the owner's rule for every sync. A mirror's version store grows
24
+ for longer: yandex-disk-cli users keep a year of replaced files unless they set a shorter period.
25
+ - **`FileTarget.exists` answers false only for a real deletion.** A provider that cannot tell a deletion from lost
26
+ access throws instead, and the file is left alone as unknown. yandex-disk-cli's target already answers that way.
27
+ It also gets the file's id from the last sync, so a provider that keeps ids across a move can tell a file moved out
28
+ of the folder from a deleted one.
29
+
8
30
  ## 0.726.0 — 23.11.2026
9
31
 
10
32
  ### Added
@@ -43,13 +43,19 @@ export interface FileTarget {
43
43
  next?: string;
44
44
  } | undefined>;
45
45
  /**
46
- * Whether anything is at `path` now, read directly rather than from a listing; false only when the provider says
47
- * nothing is there. A file missing from a listing is deleted only once this says so.
46
+ * Whether anything is at `path` now, read directly rather than from a listing; false only when the provider says the
47
+ * file was deleted. A file missing from a listing is deleted only once this says so. An answer that cannot tell a
48
+ * deletion from lost access (a 404 that may mean "no longer shared") throws, and the file is left alone. `id` is
49
+ * the file's id at the last sync: a provider that keeps a file's id across a move asks by it, so a file moved out of
50
+ * the folder is still there, not deleted.
48
51
  */
49
- exists(path: string, signal?: AbortSignal): Promise<boolean>;
52
+ exists(path: string, signal?: AbortSignal, id?: string): Promise<boolean>;
50
53
  /** Streams the file into `into`, a new local file. */
51
54
  download(path: string, into: string, signal?: AbortSignal): Promise<void>;
52
- /** Streams the local file `from` to `path` and returns the file as the remote now has it. */
55
+ /**
56
+ * Streams the local file `from` to `path` and returns the file as the remote now has it. With `rewrites`, its hash
57
+ * is of the remote's own form of the text, the bytes `download` gives.
58
+ */
53
59
  upload(path: string, from: string, expect: Expect, signal?: AbortSignal): Promise<RemoteFile>;
54
60
  /** Moves the file to the remote's trash, where the owner can restore it. */
55
61
  trash(path: string, expect: Exclude<Expect, {
@@ -57,6 +63,12 @@ export interface FileTarget {
57
63
  }>, signal?: AbortSignal): Promise<void>;
58
64
  /** Creates the folder and its parents; a folder that exists is fine. */
59
65
  ensureFolder(path: string, signal?: AbortSignal): Promise<void>;
66
+ /**
67
+ * The remote keeps its own form of the text it is sent (Notion's Markdown), so an upload may read back with another
68
+ * hash. The mirror then writes that form into the local file, the earlier one kept, and both sides hold the same
69
+ * bytes again. Without it, such an upload is a conflict.
70
+ */
71
+ rewrites?: boolean;
60
72
  /** A value that moves with every change on the remote, when the provider has one: a watch reads it before listing. */
61
73
  revision?(signal?: AbortSignal): Promise<string>;
62
74
  }
@@ -106,25 +106,33 @@ export const resolveFileConflict = async (options) => {
106
106
  if (parent !== ".")
107
107
  await write("folder", "remote", parent, () => target.ensureFolder(remotePath(parent), signal));
108
108
  const sent = await write("absent" in expect ? "create" : "update", "remote", at, () => target.upload(remotePath(at), from, expect, signal));
109
- if (sent.sha256 !== undefined && sent.sha256 !== hash)
109
+ if (sent.sha256 === undefined || sent.sha256 === hash)
110
+ return { sent, settled: hash };
111
+ if (!target.rewrites)
110
112
  throw new CliError("conflict", "the file changed during the upload", { reason: "upload.mismatch" });
111
- return sent;
113
+ // The remote keeps its own form of the text: the local side takes that form, so both hold the same bytes.
114
+ const form = await versions.keep((temp) => target.download(remotePath(at), temp, signal));
115
+ if (form !== sent.sha256)
116
+ throw changed(`the ${options.remote} file`, "mirror.remote-changed");
117
+ return { sent, settled: form };
112
118
  };
113
119
  const expectRemote = found.remote
114
120
  ? { sha256: found.remote, ...(found.remoteFile?.md5 ? { md5: found.remoteFile.md5 } : {}) }
115
121
  : { absent: true };
116
122
  let sent = final === found.remote ? found.remoteFile : undefined;
123
+ /** What both sides hold once resolved: `final`, or the remote's own form of it. */
124
+ let settled = final;
117
125
  if (final !== found.remote) {
118
126
  if (final === undefined) {
119
127
  await write("delete", "remote", path, () => target.trash(remotePath(path), expectRemote, signal));
120
128
  }
121
129
  else
122
- sent = await send(path, "file" in choice ? choice.file : absolute, final, expectRemote);
130
+ ({ sent, settled } = await send(path, "file" in choice ? choice.file : absolute, final, expectRemote));
123
131
  }
124
- if (final !== found.local) {
125
- const placed = final === undefined
132
+ if (settled !== found.local) {
133
+ const placed = settled === undefined
126
134
  ? await write("delete", "local", path, () => trashLocal(root, path, found.local, date))
127
- : await write(found.local ? "update" : "create", "local", path, () => versions.write(final, absolute, found.local));
135
+ : await write(found.local ? "update" : "create", "local", path, () => versions.write(settled, absolute, found.local));
128
136
  if (!placed)
129
137
  throw changed("the local file", "mirror.local-changed");
130
138
  }
@@ -143,16 +151,20 @@ export const resolveFileConflict = async (options) => {
143
151
  syncedAt: new Date(now()).toISOString(),
144
152
  };
145
153
  };
146
- if (final === undefined)
154
+ if (settled === undefined)
147
155
  delete state.entries[path];
148
156
  else if (sent)
149
- await entry(path, sent, final);
157
+ await entry(path, sent, settled);
150
158
  if (keep === "both" && found.local && found.remote) {
151
159
  const name = copyName(path, options.remote, date, (at) => existsSync(join(root, at)) || at in state.entries);
152
160
  await assertNoLink(root, name);
153
161
  if (!(await write("create", "local", name, () => versions.write(found.remote, join(root, name)))))
154
162
  throw changed("the local folder", "mirror.local-changed");
155
- await entry(name, await send(name, join(root, name), found.remote, { absent: true }), found.remote);
163
+ const copied = await send(name, join(root, name), found.remote, { absent: true });
164
+ if (copied.settled !== found.remote &&
165
+ !(await write("update", "local", name, () => versions.write(copied.settled, join(root, name), found.remote))))
166
+ throw changed("the local folder", "mirror.local-changed");
167
+ await entry(name, copied.sent, copied.settled);
156
168
  copy = name;
157
169
  }
158
170
  if (state.conflicts) {
@@ -175,7 +187,9 @@ export const resolveFileConflict = async (options) => {
175
187
  ...(final ? { new: final } : {}),
176
188
  resolution,
177
189
  });
178
- return { path, resolution, sha256: final ?? null, ...(copy ? { copy } : {}) };
190
+ if (final && settled && settled !== final)
191
+ versions.log({ path, event: "changed", direction: "remote-to-local", old: final, from: "local", new: settled });
192
+ return { path, resolution, sha256: settled ?? null, ...(copy ? { copy } : {}) };
179
193
  }
180
194
  finally {
181
195
  await held.release();
@@ -89,6 +89,8 @@ export interface FileMirrorResult {
89
89
  downloaded: string[];
90
90
  /** Text files both sides changed, merged and written to both. */
91
91
  merged: string[];
92
+ /** Local files given the remote's own form of what was just uploaded, for a target that `rewrites`. */
93
+ rewritten?: string[];
92
94
  trashed: {
93
95
  path: string;
94
96
  side: Side;
@@ -287,7 +287,7 @@ export const syncFileMirror = async (options) => {
287
287
  const absent = steps.filter((step) => step.kind === "trash-local");
288
288
  for (let at = 0; at < absent.length; at += CHECKS)
289
289
  await Promise.all(absent.slice(at, at + CHECKS).map(async (step) => {
290
- const why = await target.exists(remotePath(step.path), signal).then((there) => (there ? `missing from the ${options.remote} listing, but still there` : undefined), (error) => {
290
+ const why = await target.exists(remotePath(step.path), signal, step.base?.id).then((there) => (there ? `missing from the ${options.remote} listing, but still there` : undefined), (error) => {
291
291
  if (signal?.aborted)
292
292
  throw error;
293
293
  return `the ${options.remote} could not say whether it was deleted: ${options.errorOf(error).message}`;
@@ -484,21 +484,56 @@ export const syncFileMirror = async (options) => {
484
484
  folders.add(up);
485
485
  }
486
486
  const sent = await write(created ? "create" : "update", "remote", path, () => target.upload(remotePath(path), from.absolute, expect, signal));
487
- if (sent.sha256 !== undefined && sent.sha256 !== from.hash)
487
+ const rewritten = sent.sha256 !== undefined && sent.sha256 !== from.hash;
488
+ if (rewritten && !target.rewrites)
488
489
  throw new CliError("conflict", "the local file changed during the upload", { reason: "upload.mismatch" });
490
+ if (!merged) {
491
+ versions.log({
492
+ path,
493
+ event: created ? "created" : "changed",
494
+ direction: "local-to-remote",
495
+ ...("sha256" in expect ? { old: expect.sha256, from: "remote" } : {}),
496
+ new: from.hash,
497
+ size: from.size,
498
+ });
499
+ result.uploaded.push(path);
500
+ }
501
+ if (rewritten)
502
+ return takeRewrite(path, sent, from);
489
503
  record(path, sent, from, from.hash);
490
504
  await keepBase(path, from.absolute, from.hash, from.size);
491
- if (merged)
505
+ };
506
+ /**
507
+ * Puts the remote's own form of an upload into the local file, so both sides hold the same bytes and the next
508
+ * merge has one base. The local file keeps its version first; one saved meanwhile stays, sent by the next sync.
509
+ */
510
+ const takeRewrite = async (path, sent, from) => {
511
+ const form = sent.sha256;
512
+ const kept = await versions.keep((temp) => target.download(remotePath(path), temp, signal));
513
+ if (kept !== form)
514
+ throw new CliError("conflict", `the ${options.remote} file changed right after the upload`, {
515
+ reason: "mirror.remote-changed",
516
+ });
517
+ const absolute = join(root, path);
518
+ await assertNoLink(root, path);
519
+ const old = await versions.keep(absolute);
520
+ const placed = old === from.hash && (await write("update", "local", path, () => versions.write(form, absolute, from.hash)));
521
+ if (!placed) {
522
+ // A local size no file has: the file is hashed again and sent, never taken for the remote's form.
523
+ record(path, sent, { size: -1, mtimeMs: -1 }, form);
492
524
  return;
525
+ }
526
+ record(path, sent, await stat(absolute), form);
493
527
  versions.log({
494
528
  path,
495
- event: created ? "created" : "changed",
496
- direction: "local-to-remote",
497
- ...("sha256" in expect ? { old: expect.sha256, from: "remote" } : {}),
498
- new: from.hash,
499
- size: from.size,
529
+ event: "changed",
530
+ direction: "remote-to-local",
531
+ old,
532
+ from: "local",
533
+ new: form,
534
+ size: sent.size,
500
535
  });
501
- result.uploaded.push(path);
536
+ result.rewritten = [...(result.rewritten ?? []), path];
502
537
  };
503
538
  /**
504
539
  * False when the local file changed (or appeared) during the sync, so nothing was written. Without `recorded`, the
@@ -75,11 +75,12 @@ export interface FileVersions {
75
75
  /**
76
76
  * A mirror's local version store: `<dir>/objects/<sha256>` holds each kept content once, `<dir>/log.jsonl` is
77
77
  * append-only. The store lives under the profile's state folder, never inside the mirrored folder. Retention: a
78
- * content stays while an event newer than 30 days names it; the last content of a path whose newest event is a
79
- * deletion, and every content a merge, a conflict or a resolution names, stay for good. A content no event names stays
80
- * 30 days after it was last kept, so a crash between keeping a file and logging the change loses nothing.
78
+ * content stays while an event newer than `keepForDays` (the profile's `keep_versions_for_days`, 365 by default) names
79
+ * it; the last content of a path whose newest event is a deletion, and every content a merge, a conflict or a
80
+ * resolution names, stay for good. A content no event names stays as long after it was last kept, so a crash between
81
+ * keeping a file and logging the change loses nothing.
81
82
  */
82
- export declare const fileVersions: (dir: string, now?: () => number) => FileVersions;
83
+ export declare const fileVersions: (dir: string, now?: () => number, keepForDays?: number) => FileVersions;
83
84
  /**
84
85
  * Puts a kept version of `path` back into the mirrored folder; the file there now is kept first. The next sync sends
85
86
  * it like any local change. False when the store no longer has the version or the file changed meanwhile. Runs under
@@ -7,7 +7,8 @@ import { pipeline } from "node:stream/promises";
7
7
  import { CliError } from "@wirecat/cli-base";
8
8
  import { heldCheck } from "../documents/mirrors.js";
9
9
  import { assertNoLink, hashFile, replaceLocal, safePath } from "../documents/push.js";
10
- const RETAIN_MS = 30 * 24 * 60 * 60 * 1000;
10
+ import { KEEP_VERSIONS_FOR_DAYS } from "../documents/sync.js";
11
+ const DAY_MS = 24 * 60 * 60 * 1000;
11
12
  /** Errors of the local file system: a writer that fails with one could not write the store, whatever it was reading. */
12
13
  const LOCAL_FAILURES = new Set(["ENOSPC", "EDQUOT", "EROFS", "EACCES", "EPERM", "EIO", "EFBIG", "EMFILE", "ENFILE"]);
13
14
  const notKept = (error) => {
@@ -50,11 +51,13 @@ const rolesOf = (event) => {
50
51
  /**
51
52
  * A mirror's local version store: `<dir>/objects/<sha256>` holds each kept content once, `<dir>/log.jsonl` is
52
53
  * append-only. The store lives under the profile's state folder, never inside the mirrored folder. Retention: a
53
- * content stays while an event newer than 30 days names it; the last content of a path whose newest event is a
54
- * deletion, and every content a merge, a conflict or a resolution names, stay for good. A content no event names stays
55
- * 30 days after it was last kept, so a crash between keeping a file and logging the change loses nothing.
54
+ * content stays while an event newer than `keepForDays` (the profile's `keep_versions_for_days`, 365 by default) names
55
+ * it; the last content of a path whose newest event is a deletion, and every content a merge, a conflict or a
56
+ * resolution names, stay for good. A content no event names stays as long after it was last kept, so a crash between
57
+ * keeping a file and logging the change loses nothing.
56
58
  */
57
- export const fileVersions = (dir, now = Date.now) => {
59
+ export const fileVersions = (dir, now = Date.now, keepForDays = KEEP_VERSIONS_FOR_DAYS) => {
60
+ const retainMs = keepForDays * DAY_MS;
58
61
  const objects = join(dir, "objects");
59
62
  const logPath = join(dir, "log.jsonl");
60
63
  const events = () => {
@@ -140,7 +143,7 @@ export const fileVersions = (dir, now = Date.now) => {
140
143
  const last = new Map();
141
144
  for (const event of all) {
142
145
  last.set(event.path, event);
143
- if (Date.parse(event.at) > now() - RETAIN_MS)
146
+ if (Date.parse(event.at) > now() - retainMs)
144
147
  for (const hash of [event.old, event.new])
145
148
  if (hash)
146
149
  keep.add(hash);
@@ -166,7 +169,7 @@ export const fileVersions = (dir, now = Date.now) => {
166
169
  return undefined;
167
170
  throw error;
168
171
  });
169
- if (kept && kept.mtimeMs <= now() - RETAIN_MS)
172
+ if (kept && kept.mtimeMs <= now() - retainMs)
170
173
  await rm(object, { force: true });
171
174
  }
172
175
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wirecat/cli-core",
3
- "version": "0.726.0",
3
+ "version": "0.727.0",
4
4
  "description": "The messenger-neutral half of a messaging CLI: domain model, message locators, rendering, name resolution, the SQLite seam, the command skeleton and shared read commands, the send guard, run records and the message store",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Viacheslav Ptsarev",