@coderook/cli 0.46.7 → 0.46.9

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.
@@ -2,7 +2,7 @@
2
2
  "name": "coderook",
3
3
  "displayName": "CodeRook",
4
4
  "description": "Save, browse and restore whole-snapshot versions of a project on CodeRook, with cbx or plain git, from Claude Code.",
5
- "version": "0.46.7",
5
+ "version": "0.46.9",
6
6
  "author": {
7
7
  "name": "ACCA Gaming Productions",
8
8
  "url": "https://coderook.com"
package/README.md CHANGED
@@ -784,6 +784,10 @@ is reachable, and who this machine is signed in as.
784
784
  `0` on success, `1` on failure. Every command prints why it failed on standard
785
785
  error, so a script can branch on the status and log the reason.
786
786
 
787
+ `cbx submit` and `cbx workspace save` exit `3` when the save was kept as a
788
+ merge (`M-n`) instead of landing on the line, because somebody changed the same
789
+ lines first. Nothing is lost: settle it with `cbx merge M-n`.
790
+
787
791
  ## Source and licence
788
792
 
789
793
  The `cbx` command line and the CBX engine it is built on are open source under
@@ -23,6 +23,7 @@ const node_stream_1 = require("node:stream");
23
23
  const node_zlib_1 = require("node:zlib");
24
24
  const node_path_1 = __importDefault(require("node:path"));
25
25
  const safe_path_js_1 = require("../shared/safe_path.js");
26
+ const chunking_js_1 = require("../shared/chunking.js");
26
27
  const identify_js_1 = require("./identify.js");
27
28
  const faults_js_1 = require("./faults.js");
28
29
  const retry_js_1 = require("./retry.js");
@@ -70,13 +71,40 @@ async function digestOf(target) {
70
71
  * a directory holding ignored files with them, so this stops at the first
71
72
  * one that still has something in it.
72
73
  */
74
+ /*
75
+ A file's digest, from the folder's record when its size and time still match
76
+ it, otherwise by reading it. Catching up past 20,000 deleted files read every
77
+ one of them first to make sure nobody had edited it: 752 s (2026-11-11).
78
+ */
79
+ async function currentDigest(target, recorded) {
80
+ if (recorded) {
81
+ try {
82
+ const now = await (0, promises_1.stat)(target);
83
+ if (now.size === recorded.size && now.mtimeMs === recorded.mtimeMs)
84
+ return recorded.sha256;
85
+ }
86
+ catch {
87
+ return null;
88
+ }
89
+ }
90
+ return digestOf(target);
91
+ }
73
92
  async function pruneEmpty(root, removed) {
74
- let directory = node_path_1.default.dirname(removed);
75
- while (directory.startsWith(root) && directory !== root) {
93
+ /*
94
+ Compared resolved, and without case on Windows. The two arrived spelled
95
+ differently (B:/x and B:\x, or b: and B:), so the check failed at once and
96
+ no folder was ever removed: catching up past 20,000 deleted files left 101
97
+ empty folders behind (2026-11-11).
98
+ */
99
+ const fold = (value) => (process.platform === "win32" ? value.toLowerCase() : value);
100
+ const top = fold(node_path_1.default.resolve(root));
101
+ let directory = node_path_1.default.resolve(node_path_1.default.dirname(removed));
102
+ while (fold(directory).startsWith(top + node_path_1.default.sep) && fold(directory) !== top) {
76
103
  try {
77
104
  if ((await (0, promises_1.readdir)(directory)).length)
78
105
  return;
79
- await (0, promises_1.rm)(directory, { recursive: false, force: true });
106
+ /* rmdir, not rm: rm refuses any directory unless told to recurse, so this never removed one. */
107
+ await (0, promises_1.rmdir)(directory);
80
108
  }
81
109
  catch {
82
110
  return;
@@ -665,7 +693,59 @@ class Downloader {
665
693
  can say it happened.
666
694
  */
667
695
  recoveredFromGzip = [];
668
- async fetchInto(repositoryId, versionId, file, target, halt) {
696
+ /** Pieces of large files taken from the copy already in the folder rather than downloaded. */
697
+ piecesReused = 0;
698
+ /**
699
+ * Where each piece of `pieces` already sits in `localCopy`, by digest.
700
+ *
701
+ * Pieces are cut by content, so the parts of a large file a change did not
702
+ * touch cut into the same pieces again. Cutting the copy already in the
703
+ * folder the way the upload did finds them, and only the rest is fetched:
704
+ * a 1 MB edit inside a 1.2 GB file made every catch-up download all 1.2 GB
705
+ * again (2026-11-11 agent exercise, about 250 s each). Which profile the
706
+ * upload used is not in the listing, so the one whose target size is
707
+ * nearest the pieces' own median is taken; a wrong guess finds nothing and
708
+ * costs one read of the local file.
709
+ */
710
+ async localPieces(localCopy, pieces) {
711
+ const found = await (0, promises_1.stat)(localCopy).catch(() => null);
712
+ if (!found?.isFile() || pieces.length < 2)
713
+ return null;
714
+ const total = pieces.reduce((sum, piece) => sum + piece.sourceSize, 0);
715
+ const sizes = pieces.map((piece) => piece.sourceSize).sort((left, right) => left - right);
716
+ const median = sizes[sizes.length >> 1] ?? 0;
717
+ const candidates = [(0, chunking_js_1.microchunkProfileForFileSize)(total), (0, chunking_js_1.profileForFileSize)(total)].filter((one) => one !== null);
718
+ if (!candidates.length)
719
+ return null;
720
+ const profile = candidates.reduce((best, one) => Math.abs(one.targetSize - median) < Math.abs(best.targetSize - median) ? one : best);
721
+ const wanted = new Set(pieces.map((piece) => piece.sha256).filter(Boolean));
722
+ const where = new Map();
723
+ const note = (bytes, offset) => {
724
+ const digest = (0, node_crypto_1.createHash)("sha256").update(bytes).digest("hex");
725
+ if (wanted.has(digest) && !where.has(digest))
726
+ where.set(digest, { offset, length: bytes.length });
727
+ };
728
+ let pending = Buffer.alloc(0);
729
+ let pendingAt = 0;
730
+ for await (const block of (0, node_fs_1.createReadStream)(localCopy, { highWaterMark: 8 * 1024 * 1024 })) {
731
+ this.check();
732
+ const incoming = Buffer.from(block);
733
+ pending = pending.length ? Buffer.concat([pending, incoming]) : incoming;
734
+ let from = 0;
735
+ for (const cut of (0, chunking_js_1.cutPoints)(pending, profile)) {
736
+ note(pending.subarray(from, cut), pendingAt + from);
737
+ from = cut;
738
+ }
739
+ pendingAt += from;
740
+ pending = Buffer.from(pending.subarray(from));
741
+ }
742
+ if (pending.length)
743
+ note(pending, pendingAt);
744
+ return where.size ? where : null;
745
+ }
746
+ async fetchInto(repositoryId, versionId, file, target, halt,
747
+ /* The copy of this file already in the folder, whose unchanged pieces need not be fetched. */
748
+ localCopy) {
669
749
  /*
670
750
  A sealed file is read whole, by path, whatever shape it is stored in.
671
751
 
@@ -704,11 +784,40 @@ class Downloader {
704
784
  a 1.25 GB restore came down at 2.6 MB/s (2026-11-11).
705
785
  */
706
786
  const AHEAD = 4;
787
+ const onDisk = localCopy ? await this.localPieces(localCopy, pieces).catch(() => null) : null;
788
+ const reuse = async (piece) => {
789
+ const place = onDisk?.get(piece.sha256);
790
+ if (!place || !localCopy)
791
+ return null;
792
+ const handle = await (0, promises_1.open)(localCopy, "r");
793
+ try {
794
+ const body = Buffer.alloc(place.length);
795
+ const { bytesRead } = await handle.read(body, 0, place.length, place.offset);
796
+ if (bytesRead !== place.length)
797
+ return null;
798
+ /* Checked like a download: a file changed since it was cut is fetched instead. */
799
+ if ((0, node_crypto_1.createHash)("sha256").update(body).digest("hex") !== piece.sha256)
800
+ return null;
801
+ return { body, headers: new Headers() };
802
+ }
803
+ finally {
804
+ await handle.close();
805
+ }
806
+ };
807
+ const counted = () => {
808
+ this.piecesReused += 1;
809
+ };
707
810
  const source = (async function* () {
708
811
  const asked = [];
709
812
  const ask = (at) => {
710
813
  const piece = pieces[at];
711
- const reply = fetchPiece(`/v1/repositories/${repositoryId}/objects/${piece.objectId}`, `${file.path} (piece ${at + 1} of ${pieces.length})`);
814
+ const reply = reuse(piece).then((local) => {
815
+ if (local) {
816
+ counted();
817
+ return local;
818
+ }
819
+ return fetchPiece(`/v1/repositories/${repositoryId}/objects/${piece.objectId}`, `${file.path} (piece ${at + 1} of ${pieces.length})`);
820
+ });
712
821
  /* Awaited in turn below; this only stops an early stop leaving it unhandled. */
713
822
  reply.catch(() => undefined);
714
823
  asked[at] = reply;
@@ -866,7 +975,12 @@ class Downloader {
866
975
  version, which is what repairs a damaged one — and what discards
867
976
  anything unsaved.
868
977
  */
869
- mode = "replace") {
978
+ mode = "replace",
979
+ /*
980
+ What the folder's own record says each file was when last read: size,
981
+ time and digest. A file it still matches is known without opening it.
982
+ */
983
+ known) {
870
984
  const files = await this.files(repositoryId, versionId);
871
985
  if (!files.length)
872
986
  throw new Error("That version has no files");
@@ -886,6 +1000,10 @@ class Downloader {
886
1000
  try {
887
1001
  let written = 0;
888
1002
  let bytes = 0;
1003
+ /* Each file this run put in place: its size and time as written, and its digest. */
1004
+ const placedRecord = new Map();
1005
+ /* Files the version dropped that this run removed from the folder. */
1006
+ let removedHere = 0;
889
1007
  /*
890
1008
  Not fetched at all: a file the incoming version did not change, still
891
1009
  here. Reconciling never writes one of these (see the placing loop
@@ -895,12 +1013,24 @@ class Downloader {
895
1013
  14,125 files each spent twenty minutes re-downloading 306 MB apiece).
896
1014
  */
897
1015
  const kept = new Set();
1016
+ /*
1017
+ Files the version left alone that are gone from the folder: deleted
1018
+ here and not saved yet. Catching up put them back, which turned an
1019
+ unsaved rename into a copy (Opus55-1, 2026-11-11). They stay deleted,
1020
+ and the record still names them, so status shows the deletion waiting
1021
+ to be saved.
1022
+ */
1023
+ const deletedHere = new Set();
898
1024
  if (mode === "reconcile" && held) {
899
1025
  for (const file of files) {
900
1026
  if (!file.sha256 || held[file.path] !== file.sha256)
901
1027
  continue;
902
- if (!(await exists((0, safe_path_js_1.inside)(destination, file.path))))
1028
+ if (!(await exists((0, safe_path_js_1.inside)(destination, file.path)))) {
1029
+ deletedHere.add(file.path);
1030
+ kept.add(file.path);
1031
+ manifest[file.path] = file.sha256;
903
1032
  continue;
1033
+ }
904
1034
  kept.add(file.path);
905
1035
  manifest[file.path] = file.sha256;
906
1036
  written += 1;
@@ -1020,7 +1150,9 @@ class Downloader {
1020
1150
  const restoreFile = async (file) => {
1021
1151
  const full = (0, safe_path_js_1.inside)(staging, file.path);
1022
1152
  await (0, promises_1.mkdir)(node_path_1.default.dirname(full), { recursive: true });
1023
- const digest = await this.fetchInto(repositoryId, versionId, file, full, halt.signal);
1153
+ /* A file in pieces can take its unchanged pieces from the copy already here. */
1154
+ const localCopy = file.chunks?.length ? (0, safe_path_js_1.inside)(destination, file.path) : undefined;
1155
+ const digest = await this.fetchInto(repositoryId, versionId, file, full, halt.signal, localCopy);
1024
1156
  /*
1025
1157
  The whole file's digest, checked after every piece has already been
1026
1158
  checked individually. Both are needed: the pieces catch a corrupt or
@@ -1140,31 +1272,68 @@ class Downloader {
1140
1272
  (0, faults_js_1.maybeFail)("get:after-staging");
1141
1273
  await (0, promises_1.mkdir)(destination, { recursive: true });
1142
1274
  let placed = 0;
1143
- for (const file of files) {
1144
- const target = (0, safe_path_js_1.inside)(destination, file.path);
1145
- /* Never fetched, because the folder already holds it unchanged. */
1146
- if (kept.has(file.path))
1147
- continue;
1148
- /*
1149
- Reconciling leaves alone whatever the incoming version did not
1150
- change. Writing it anyway would replace an edit in progress with a
1151
- copy of what the folder already had — destroying work to achieve
1152
- nothing, which is the least defensible way to lose someone's file.
1153
- */
1154
- if (mode === "reconcile" &&
1155
- held &&
1156
- held[file.path] === manifest[file.path] &&
1157
- (await exists(target))) {
1158
- continue;
1275
+ /*
1276
+ One at a time, each folder made once. Placing several at once was
1277
+ tried and measured: on Windows 18,000 small files took 106-114 s with
1278
+ 4 or 16 lanes against 24.5 s with one (2026-11-11), because renames
1279
+ into the same folders contend. The lane shape stays so the count is
1280
+ one place to change.
1281
+ */
1282
+ const PLACING_LANES = 1;
1283
+ const madeFolders = new Map();
1284
+ const folderFor = (target) => {
1285
+ const folder = node_path_1.default.dirname(target);
1286
+ let made = madeFolders.get(folder);
1287
+ if (!made) {
1288
+ made = (0, promises_1.mkdir)(folder, { recursive: true });
1289
+ madeFolders.set(folder, made);
1159
1290
  }
1160
- await (0, promises_1.mkdir)(node_path_1.default.dirname(target), { recursive: true });
1161
- await (0, promises_1.rm)(target, { recursive: true, force: true });
1162
- await (0, promises_1.rename)((0, safe_path_js_1.inside)(staging, file.path), target);
1163
- await applyMode(target, file.executable === true);
1164
- placed += 1;
1165
- if (placed === 1)
1166
- (0, faults_js_1.maybeFail)("get:after-first-file");
1167
- }
1291
+ return made;
1292
+ };
1293
+ let nextToPlace = 0;
1294
+ let placingFailed = false;
1295
+ const placeLane = async () => {
1296
+ while (!placingFailed) {
1297
+ const at = nextToPlace;
1298
+ nextToPlace += 1;
1299
+ if (at >= files.length)
1300
+ return;
1301
+ const file = files[at];
1302
+ const target = (0, safe_path_js_1.inside)(destination, file.path);
1303
+ /* Never fetched, because the folder already holds it unchanged. */
1304
+ if (kept.has(file.path))
1305
+ continue;
1306
+ /*
1307
+ Reconciling leaves alone whatever the incoming version did not
1308
+ change. Writing it anyway would replace an edit in progress with a
1309
+ copy of what the folder already had — destroying work to achieve
1310
+ nothing, which is the least defensible way to lose someone's file.
1311
+ */
1312
+ if (mode === "reconcile" &&
1313
+ held &&
1314
+ held[file.path] === manifest[file.path] &&
1315
+ (await exists(target))) {
1316
+ continue;
1317
+ }
1318
+ try {
1319
+ await folderFor(target);
1320
+ await (0, promises_1.rm)(target, { recursive: true, force: true });
1321
+ await (0, promises_1.rename)((0, safe_path_js_1.inside)(staging, file.path), target);
1322
+ await applyMode(target, file.executable === true);
1323
+ /* What was just written, so the next status need not read it again. */
1324
+ const now = await (0, promises_1.stat)(target);
1325
+ placedRecord.set(file.path, { size: now.size, mtimeMs: now.mtimeMs, sha256: manifest[file.path] });
1326
+ placed += 1;
1327
+ if (placed === 1)
1328
+ (0, faults_js_1.maybeFail)("get:after-first-file");
1329
+ }
1330
+ catch (error) {
1331
+ placingFailed = true;
1332
+ throw error;
1333
+ }
1334
+ }
1335
+ };
1336
+ await Promise.all(Array.from({ length: PLACING_LANES }, () => placeLane()));
1168
1337
  /*
1169
1338
  A file this folder received but the version no longer holds is gone
1170
1339
  deliberately, so it goes. One it never received is not this fetch's
@@ -1183,9 +1352,10 @@ class Downloader {
1183
1352
  ones this folder received, somebody has been working on it, and
1184
1353
  somebody else's deletion is not permission to throw that away.
1185
1354
  */
1186
- if (mode === "reconcile" && (await digestOf(target)) !== digest)
1355
+ if (mode === "reconcile" && (await currentDigest(target, known?.get(gone))) !== digest)
1187
1356
  continue;
1188
1357
  await (0, promises_1.rm)(target, { force: true });
1358
+ removedHere += 1;
1189
1359
  await pruneEmpty(destination, target);
1190
1360
  (0, faults_js_1.maybeFail)("get:after-delete");
1191
1361
  }
@@ -1201,7 +1371,15 @@ class Downloader {
1201
1371
  percent: 100,
1202
1372
  });
1203
1373
  /* How many were already here and left alone, so a caller can say nothing was fetched. */
1204
- return { files: written, bytes, manifest, unchanged: kept.size };
1374
+ return {
1375
+ files: written,
1376
+ bytes,
1377
+ manifest,
1378
+ unchanged: kept.size - deletedHere.size,
1379
+ placedFiles: placedRecord,
1380
+ removed: removedHere,
1381
+ leftDeleted: deletedHere.size,
1382
+ };
1205
1383
  }
1206
1384
  catch (error) {
1207
1385
  await (0, promises_1.rm)(staging, { recursive: true, force: true });
@@ -1433,6 +1433,7 @@ class Uploader {
1433
1433
  storedBytes: completed.version.storedSize ?? storedBytes,
1434
1434
  sentBytes: progress.transferred,
1435
1435
  sentFiles: uploaded.length - progress.alreadyOnAccount,
1436
+ repackedFiles: this.foldPaths.size,
1436
1437
  reusedFiles: reused.length,
1437
1438
  /** Selected, but the service already held the content. */
1438
1439
  alreadyStoredFiles: progress.alreadyOnAccount,
@@ -2332,7 +2333,17 @@ class Uploader {
2332
2333
  : null;
2333
2334
  quoting?.catch(() => undefined);
2334
2335
  /* A save gathering scattered files has to store a new pack, which a patch cannot. */
2335
- const oneRequest = this.deltaSaveRefused || this.foldPaths.size
2336
+ /*
2337
+ Nor can it name a file kept under a new path. A rename whose bytes are
2338
+ already stored is a reused entry, not a declaration, and the one-request
2339
+ save carries only patches and removals: the service filled in the rest
2340
+ from the base, which has no such path, so the renamed file vanished from
2341
+ the version while its old name was removed (2026-11-11, found by
2342
+ GrokBot2, confirmed by Opus55-1: rename + --sync from a folder behind the
2343
+ newest version lost both renamed files). The ordinary path names it.
2344
+ */
2345
+ const movesKeptBytes = reused.some((one) => !prior.has(one.path));
2346
+ const oneRequest = this.deltaSaveRefused || this.foldPaths.size || movesKeptBytes
2336
2347
  ? null
2337
2348
  : await this.deltaSavePlan(request, repositoryId, declarations, prior);
2338
2349
  if (oneRequest && this.planning && request.oneRoundTrip) {
@@ -15,6 +15,7 @@
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.linesOf = linesOf;
17
17
  exports.diffLines = diffLines;
18
+ exports.diffLinesWithin = diffLinesWithin;
18
19
  exports.foldUnchanged = foldUnchanged;
19
20
  exports.conflictDraft = conflictDraft;
20
21
  exports.hasConflictMarkers = hasConflictMarkers;
@@ -57,8 +58,67 @@ function linesOf(text) {
57
58
  * make.
58
59
  */
59
60
  function diffLines(target, candidate) {
61
+ return diffLinesWithin(target, candidate, Number.POSITIVE_INFINITY);
62
+ }
63
+ /*
64
+ Past this many cells the table is not built: the middle is compared by the
65
+ shortest edit instead, whose cost follows the number of changed lines.
66
+ */
67
+ const MOST_TABLE_CELLS = 4_000_000;
68
+ /**
69
+ * `diffLines`, giving up (null) only when more than `maxEdits` lines differ.
70
+ *
71
+ * Lines both sides share at the start and the end are matched first: a longest
72
+ * common subsequence can always keep them, so the answer is the same and only
73
+ * the changed middle is compared. That middle goes through the exact table when
74
+ * it is small, and otherwise through the shortest edit (Myers), which is exact
75
+ * too and costs memory in the number of changes rather than in lines squared.
76
+ * `cbx diff` refused any file over 4,000 lines before, so a one-line change to
77
+ * the 4,937-line cli.ts could not be reviewed at all (2026-11-11).
78
+ */
79
+ function diffLinesWithin(target, candidate, maxEdits) {
60
80
  const left = linesOf(target);
61
81
  const right = linesOf(candidate);
82
+ let head = 0;
83
+ while (head < left.length && head < right.length && left[head] === right[head])
84
+ head += 1;
85
+ let tail = 0;
86
+ while (tail < left.length - head &&
87
+ tail < right.length - head &&
88
+ left[left.length - 1 - tail] === right[right.length - 1 - tail])
89
+ tail += 1;
90
+ const middleLeft = left.slice(head, left.length - tail);
91
+ const middleRight = right.slice(head, right.length - tail);
92
+ const middle = (middleLeft.length + 1) * (middleRight.length + 1) <= MOST_TABLE_CELLS
93
+ ? byTable(middleLeft, middleRight)
94
+ : shortestEdit(middleLeft, middleRight, maxEdits);
95
+ if (!middle)
96
+ return null;
97
+ if (middle.filter((line) => line.side !== "same").length > maxEdits)
98
+ return null;
99
+ const out = [];
100
+ for (let at = 0; at < head; at += 1) {
101
+ out.push({ side: "same", targetLine: at + 1, candidateLine: at + 1, text: left[at] });
102
+ }
103
+ for (const line of middle) {
104
+ out.push({
105
+ ...line,
106
+ targetLine: line.targetLine === null ? null : line.targetLine + head,
107
+ candidateLine: line.candidateLine === null ? null : line.candidateLine + head,
108
+ });
109
+ }
110
+ for (let at = tail; at > 0; at -= 1) {
111
+ out.push({
112
+ side: "same",
113
+ targetLine: left.length - at + 1,
114
+ candidateLine: right.length - at + 1,
115
+ text: left[left.length - at],
116
+ });
117
+ }
118
+ return out;
119
+ }
120
+ /** The exact table walk, for a middle small enough to tabulate. */
121
+ function byTable(left, right) {
62
122
  const table = commonRuns(left, right);
63
123
  const out = [];
64
124
  let a = 0;
@@ -103,6 +163,82 @@ function diffLines(target, candidate) {
103
163
  }
104
164
  return out;
105
165
  }
166
+ /**
167
+ * Myers' shortest edit between two line lists, or null past `maxEdits`.
168
+ *
169
+ * Each round d keeps only the furthest point reached on each diagonal, so the
170
+ * memory kept for walking back is about d squared over two entries rather than
171
+ * one per pair of lines.
172
+ */
173
+ function shortestEdit(left, right, maxEdits) {
174
+ const n = left.length;
175
+ const m = right.length;
176
+ const limit = Math.min(n + m, Number.isFinite(maxEdits) ? maxEdits : n + m);
177
+ const trace = [];
178
+ const at = (row, d, k) => row[k + d];
179
+ let done = false;
180
+ for (let d = 0; d <= limit && !done; d += 1) {
181
+ const previous = trace[d - 1];
182
+ const row = new Int32Array(2 * d + 1);
183
+ for (let k = -d; k <= d; k += 2) {
184
+ let x;
185
+ if (d === 0)
186
+ x = 0;
187
+ else if (k === -d || (k !== d && at(previous, d - 1, k - 1) < at(previous, d - 1, k + 1))) {
188
+ x = at(previous, d - 1, k + 1);
189
+ }
190
+ else {
191
+ x = at(previous, d - 1, k - 1) + 1;
192
+ }
193
+ let y = x - k;
194
+ while (x < n && y < m && left[x] === right[y]) {
195
+ x += 1;
196
+ y += 1;
197
+ }
198
+ row[k + d] = x;
199
+ if (x >= n && y >= m) {
200
+ done = true;
201
+ break;
202
+ }
203
+ }
204
+ trace.push(row);
205
+ }
206
+ if (!done)
207
+ return null;
208
+ const out = [];
209
+ let x = n;
210
+ let y = m;
211
+ for (let d = trace.length - 1; d >= 0; d -= 1) {
212
+ const k = x - y;
213
+ if (d === 0) {
214
+ while (x > 0 && y > 0) {
215
+ out.push({ side: "same", targetLine: x, candidateLine: y, text: left[x - 1] });
216
+ x -= 1;
217
+ y -= 1;
218
+ }
219
+ break;
220
+ }
221
+ const previous = trace[d - 1];
222
+ const down = k === -d || (k !== d && at(previous, d - 1, k - 1) < at(previous, d - 1, k + 1));
223
+ const fromK = down ? k + 1 : k - 1;
224
+ const fromX = at(previous, d - 1, fromK);
225
+ const fromY = fromX - fromK;
226
+ const startX = down ? fromX : fromX + 1;
227
+ const startY = down ? fromY + 1 : fromY;
228
+ while (x > startX && y > startY) {
229
+ out.push({ side: "same", targetLine: x, candidateLine: y, text: left[x - 1] });
230
+ x -= 1;
231
+ y -= 1;
232
+ }
233
+ if (down)
234
+ out.push({ side: "candidate", targetLine: null, candidateLine: fromY + 1, text: right[fromY] });
235
+ else
236
+ out.push({ side: "target", targetLine: fromX + 1, candidateLine: null, text: left[fromX] });
237
+ x = fromX;
238
+ y = fromY;
239
+ }
240
+ return out.reverse();
241
+ }
106
242
  function foldUnchanged(lines, context = 3) {
107
243
  const interesting = new Set();
108
244
  lines.forEach((line, index) => {