@coderook/cli 0.46.6 → 0.46.7

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.6",
5
+ "version": "0.46.7",
6
6
  "author": {
7
7
  "name": "ACCA Gaming Productions",
8
8
  "url": "https://coderook.com"
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Uploader = exports.UploadCancelled = void 0;
7
+ exports.packsToFold = packsToFold;
7
8
  /**
8
9
  * Sending a version to CodeRook.
9
10
  *
@@ -158,6 +159,8 @@ class Uploader {
158
159
  deltaBatchSupported = null;
159
160
  /* Set once the service refuses a one-request delta save; later saves go the ordinary way. */
160
161
  deltaSaveRefused = false;
162
+ /** Files this save sends again only to gather them into one pack (see packsToFold). */
163
+ foldPaths = new Set();
161
164
  /* Digests the plan asked about and heard were not held; see storedInBulk. */
162
165
  plannedAbsent = new Set();
163
166
  /** Batch signatures fetched while pricing a save, used once by the pass that sends it. */
@@ -568,6 +571,22 @@ class Uploader {
568
571
  const prior = request.repositoryId
569
572
  ? await this.versionFiles(request.repositoryId, request.baseVersionId, request.listingRoot ?? request.localPath)
570
573
  : new Map();
574
+ /*
575
+ Small files gathered back together when the version has scattered them
576
+ (see packsToFold): the files on disk from those packs join this save,
577
+ and runPass sends them again as one pack.
578
+ */
579
+ const folded = (await this.packingAllowed()) ? packsToFold(prior) : new Set();
580
+ const foldIn = new Set();
581
+ if (folded.size) {
582
+ for (const file of everything) {
583
+ const held = prior.get(file.path);
584
+ if (!held || !folded.has(foldKey(held)) || ticked.has(file.path))
585
+ continue;
586
+ foldIn.add(file.path);
587
+ ticked.add(file.path);
588
+ }
589
+ }
571
590
  // Ticked files are sent; everything else the project still has keeps the
572
591
  // copy already stored, and a file that is new but unticked is left out.
573
592
  const sending = everything.filter((file) => ticked.has(file.path));
@@ -623,6 +642,8 @@ class Uploader {
623
642
  }
624
643
  return {
625
644
  rules,
645
+ folded,
646
+ foldIn,
626
647
  everything,
627
648
  unticked,
628
649
  ticked,
@@ -1575,7 +1596,9 @@ class Uploader {
1575
1596
  is left after this — the large files — is where bandwidth actually
1576
1597
  matters, and those still go one per request through the lanes below.
1577
1598
  */
1578
- const missing = declarations.filter((one) => !alreadyHeld.has(one.sha256));
1599
+ const missing = declarations.filter(
1600
+ /* Gathered files go into the new pack even though their bytes are held loose. */
1601
+ (one) => !alreadyHeld.has(one.sha256) || this.foldPaths.has(one.path));
1579
1602
  const smallOnes = missing.filter((one) => one.size <= BATCH_FILE_LIMIT);
1580
1603
  /* The first file with each digest is sent; the rest are counted when it lands. */
1581
1604
  const copiesOf = new Map();
@@ -1728,7 +1751,7 @@ class Uploader {
1728
1751
  })))
1729
1752
  : null;
1730
1753
  const packedBytes = built ? this.frameSolidPack(built).byteLength : 0;
1731
- const delta = built
1754
+ const delta = built && !this.foldPaths.size
1732
1755
  ? await (0, profile_js_1.timed)("price changed-file batch", () => this.putDeltaBatch(repositoryId, request.baseVersionId, taking, prior, packedBytes, request.localBase))
1733
1756
  : null;
1734
1757
  if (delta) {
@@ -2040,7 +2063,7 @@ class Uploader {
2040
2063
  project held before it, and what a ticked path that is no longer on disk
2041
2064
  means. Nothing below changes any of it.
2042
2065
  */
2043
- const { everything, ticked, prior, sending, reused, deletions, readFailures, vanished, } = await this.selectFiles(request, report);
2066
+ const { folded, foldIn, everything, ticked, prior, sending, reused, deletions, readFailures, vanished, } = await this.selectFiles(request, report);
2044
2067
  /*
2045
2068
  Read once, here. Everything below works from the declarations rather
2046
2069
  than from the disk.
@@ -2089,7 +2112,25 @@ class Uploader {
2089
2112
  return was;
2090
2113
  return priorByDigest.get(declaration.sha256) ?? null;
2091
2114
  };
2092
- const keptAsItWas = (declaration) => storedAs(declaration) !== null;
2115
+ /*
2116
+ Small files gathered back together when the version has scattered them.
2117
+
2118
+ Each save packs only what it changed, so a project saved thousands of
2119
+ times ends up naming a separate tiny pack for nearly every file anybody
2120
+ touched: 47 stored objects at v36 and 1,038 at v1,136 in one 3,000-file
2121
+ project (2026-11-11). Every save, listing and restore then pays per
2122
+ object, which is why saves into a long-lived project slowed from 14 s
2123
+ to 26 s over 4,500 versions. Past a few dozen tiny packs, the unchanged
2124
+ files in them are sent again, together, as one pack. Same bytes, so the
2125
+ version is unchanged; only where it keeps them is.
2126
+ */
2127
+ const reuseOf = (declaration) => {
2128
+ const held = storedAs(declaration);
2129
+ if (held && folded.has(foldKey(held)))
2130
+ return null;
2131
+ return held;
2132
+ };
2133
+ const keptAsItWas = (declaration) => reuseOf(declaration) !== null;
2093
2134
  /*
2094
2135
  A copy left stale by somebody else's save is not an edit.
2095
2136
 
@@ -2119,7 +2160,24 @@ class Uploader {
2119
2160
  staleHere.add(one.path);
2120
2161
  }
2121
2162
  }
2163
+ /*
2164
+ A file joined only to be gathered into one pack is sent only if it is
2165
+ exactly what the version holds. Anything else here is not this save's
2166
+ to send — an edit somebody left unticked, or an old copy — so the
2167
+ version keeps its own.
2168
+ */
2169
+ for (const one of hashed.declarations) {
2170
+ if (foldIn.has(one.path) && prior.get(one.path)?.sha256 !== one.sha256)
2171
+ staleHere.add(one.path);
2172
+ }
2122
2173
  const declarations = hashed.declarations.filter((one) => !staleHere.has(one.path) && !keptAsItWas(one));
2174
+ /* The files sent again only to be gathered: same bytes as the version, held loose or in a tiny pack. */
2175
+ this.foldPaths = new Set(declarations
2176
+ .filter((one) => {
2177
+ const held = storedAs(one);
2178
+ return !!held && folded.has(foldKey(held));
2179
+ })
2180
+ .map((one) => one.path));
2123
2181
  let totalBytes = hashed.totalBytes;
2124
2182
  for (const one of hashed.declarations) {
2125
2183
  if (!staleHere.has(one.path))
@@ -2147,7 +2205,7 @@ class Uploader {
2147
2205
  for (const one of hashed.declarations) {
2148
2206
  if (staleHere.has(one.path))
2149
2207
  continue;
2150
- const held = storedAs(one);
2208
+ const held = reuseOf(one);
2151
2209
  if (!held)
2152
2210
  continue;
2153
2211
  totalBytes -= one.size;
@@ -2166,6 +2224,8 @@ class Uploader {
2166
2224
  });
2167
2225
  }
2168
2226
  (0, profile_js_1.counted)("files kept as they were", hashed.declarations.length - declarations.length);
2227
+ if (folded.size)
2228
+ (0, profile_js_1.counted)("tiny packs gathered into one", folded.size);
2169
2229
  // 2. The project needs somewhere on the account to live.
2170
2230
  const repositoryId = await this.ensureRepository(request);
2171
2231
  // Learn the wire contract once, before the two section pipelines begin.
@@ -2271,7 +2331,8 @@ class Uploader {
2271
2331
  })
2272
2332
  : null;
2273
2333
  quoting?.catch(() => undefined);
2274
- const oneRequest = this.deltaSaveRefused
2334
+ /* A save gathering scattered files has to store a new pack, which a patch cannot. */
2335
+ const oneRequest = this.deltaSaveRefused || this.foldPaths.size
2275
2336
  ? null
2276
2337
  : await this.deltaSavePlan(request, repositoryId, declarations, prior);
2277
2338
  if (oneRequest && this.planning && request.oneRoundTrip) {
@@ -2515,10 +2576,10 @@ class Uploader {
2515
2576
  : null;
2516
2577
  if (cached)
2517
2578
  return cached;
2518
- const body = await this.call(`/v1/repositories/${repositoryId}/versions/${versionId}/files`, {
2519
- method: "GET",
2520
- });
2521
- const rows = body.files ?? [];
2579
+ const rows = (localPath ? await this.patchedListing(repositoryId, versionId, localPath) : null) ??
2580
+ ((await this.call(`/v1/repositories/${repositoryId}/versions/${versionId}/files`, {
2581
+ method: "GET",
2582
+ })).files ?? []);
2522
2583
  if (localPath)
2523
2584
  await writeListing(localPath, repositoryId, versionId, rows);
2524
2585
  return rows;
@@ -2527,6 +2588,40 @@ class Uploader {
2527
2588
  reading.catch(() => versionListings.delete(key));
2528
2589
  return reading;
2529
2590
  }
2591
+ /**
2592
+ * A version's listing made from the newest one this folder already keeps,
2593
+ * plus only what changed since it. Null when there is nothing kept to start
2594
+ * from, or the service answers in full (one that predates `since`), or the
2595
+ * result does not add up — the caller then asks for the whole listing.
2596
+ *
2597
+ * With several people or agents saving, the version a save starts from is
2598
+ * nearly always somebody else's, so the folder's kept listing was never the
2599
+ * one wanted and each save downloaded the whole listing again: 4.66 MB for
2600
+ * 14,125 files, on every save (2026-11-11).
2601
+ */
2602
+ async patchedListing(repositoryId, versionId, localPath) {
2603
+ try {
2604
+ const base = await newestListing(localPath, repositoryId);
2605
+ if (!base || base.versionId === versionId)
2606
+ return null;
2607
+ const body = await this.call(`/v1/repositories/${repositoryId}/versions/${versionId}/files?since=${encodeURIComponent(base.versionId)}`, { method: "GET" });
2608
+ if (body.since !== base.versionId || !Array.isArray(body.changed) || !Array.isArray(body.removed)) {
2609
+ return body.files ?? null;
2610
+ }
2611
+ const byPath = new Map(base.files.map((row) => [String(row.path), row]));
2612
+ for (const gone of body.removed)
2613
+ byPath.delete(gone);
2614
+ for (const row of body.changed)
2615
+ byPath.set(String(row.path), row);
2616
+ if (byPath.size !== body.fileCount)
2617
+ return null;
2618
+ return [...byPath.values()];
2619
+ }
2620
+ catch {
2621
+ /* Anything unexpected costs one full listing, nothing more. */
2622
+ return null;
2623
+ }
2624
+ }
2530
2625
  /** The account's repository with this slug, if it has one. */
2531
2626
  async findBySlug(slug) {
2532
2627
  try {
@@ -3849,6 +3944,26 @@ async function readListing(localPath, repositoryId, versionId) {
3849
3944
  return null;
3850
3945
  }
3851
3946
  }
3947
+ /** The most recently written listing this folder keeps for the project, or null. */
3948
+ async function newestListing(localPath, repositoryId) {
3949
+ const folder = node_path_1.default.join(localPath, ".coderook", "versions");
3950
+ try {
3951
+ const entries = await Promise.all((await (0, promises_1.readdir)(folder))
3952
+ .filter((name) => name.endsWith(".json"))
3953
+ .map(async (name) => ({ name, at: (await (0, promises_1.stat)(node_path_1.default.join(folder, name))).mtimeMs })));
3954
+ entries.sort((left, right) => right.at - left.at);
3955
+ for (const entry of entries) {
3956
+ const versionId = entry.name.slice(0, -".json".length);
3957
+ const files = await readListing(localPath, repositoryId, versionId);
3958
+ if (files)
3959
+ return { versionId, files };
3960
+ }
3961
+ }
3962
+ catch {
3963
+ /* No kept listings yet. */
3964
+ }
3965
+ return null;
3966
+ }
3852
3967
  async function writeListing(localPath, repositoryId, versionId, files) {
3853
3968
  const folder = node_path_1.default.join(localPath, ".coderook", "versions");
3854
3969
  try {
@@ -3870,6 +3985,54 @@ async function writeListing(localPath, repositoryId, versionId, files) {
3870
3985
  /* A cache that cannot be written costs one download next time, nothing more. */
3871
3986
  }
3872
3987
  }
3988
+ /*
3989
+ When a version names this many small stored objects (a loose small file, or a
3990
+ pack holding only a few of the version's files), a save gathers their files
3991
+ into one pack. At most this many bytes are sent again in one save, smallest
3992
+ first.
3993
+ */
3994
+ const FOLD_AT_OBJECTS = 64;
3995
+ const TINY_PACK_FILES = 8;
3996
+ const SMALL_OBJECT_BYTES = 256 * 1024;
3997
+ const MOST_BYTES_FOLDED = 32 * 1024 * 1024;
3998
+ /** Text, source and markup: what solid packs compress well. */
3999
+ function textLike(mediaType) {
4000
+ return /^text\/|json|javascript|typescript|xml|yaml|toml|csv|svg|x-sh|x-python|sql|markdown/i.test(mediaType);
4001
+ }
4002
+ /** Which stored object a file's bytes live in, for packsToFold; empty for a file in pieces. */
4003
+ function foldKey(held) {
4004
+ return held.pack?.objectId ?? (held.chunks?.length ? "" : held.objectId ?? "");
4005
+ }
4006
+ /** The stored objects whose files this save should send again together; empty almost always. */
4007
+ function packsToFold(prior) {
4008
+ const objects = new Map();
4009
+ /* Only what always packs well: text. Compressed media sent again would just land loose again. */
4010
+ const unpackable = new Set();
4011
+ for (const held of prior.values()) {
4012
+ const id = foldKey(held);
4013
+ if (!id)
4014
+ continue;
4015
+ if (held.mediaType !== undefined && !textLike(held.mediaType))
4016
+ unpackable.add(id);
4017
+ const one = objects.get(id) ?? { files: 0, bytes: 0 };
4018
+ one.files += 1;
4019
+ one.bytes += held.sourceSize;
4020
+ objects.set(id, one);
4021
+ }
4022
+ const small = [...objects].filter(([id, one]) => !unpackable.has(id) && one.files <= TINY_PACK_FILES && one.bytes <= SMALL_OBJECT_BYTES);
4023
+ if (small.length < FOLD_AT_OBJECTS)
4024
+ return new Set();
4025
+ small.sort((left, right) => left[1].bytes - right[1].bytes);
4026
+ const chosen = new Set();
4027
+ let bytes = 0;
4028
+ for (const [id, one] of small) {
4029
+ if (bytes + one.bytes > MOST_BYTES_FOLDED)
4030
+ break;
4031
+ bytes += one.bytes;
4032
+ chosen.add(id);
4033
+ }
4034
+ return chosen;
4035
+ }
3873
4036
  /** A file the previous version holds, in the shape a save names it. */
3874
4037
  function wireFromPrior(filePath, held) {
3875
4038
  return {
@@ -1918,7 +1918,7 @@ held, mode = "replace",
1918
1918
  */
1919
1919
  trackName) {
1920
1920
  const downloader = new download_js_1.Downloader(config_js_1.credentials);
1921
- const all = await downloader.versions(repositoryId);
1921
+ let all = await downloader.versions(repositoryId);
1922
1922
  /*
1923
1923
  The head of this folder's line, not the newest version in the project.
1924
1924
 
@@ -1943,7 +1943,18 @@ trackName) {
1943
1943
  console.error(red(`The line ${trackName} has nothing saved to it yet.`));
1944
1944
  return 1;
1945
1945
  }
1946
- const head = all.find((version) => version.id === chosen.headVersionId);
1946
+ let head = all.find((version) => version.id === chosen.headVersionId);
1947
+ /*
1948
+ A save that lands between reading the versions and reading the line
1949
+ moves the head past the list just read. With several people or agents
1950
+ saving that is routine, and it ended clones with "Could not read the
1951
+ current version" (2026-11-11). Read both again, the line first.
1952
+ */
1953
+ for (let attempt = 0; !head && attempt < 3; attempt += 1) {
1954
+ const again = (await new tracks_js_1.Tracks(config_js_1.credentials).list(repositoryId)).find((candidate) => candidate.name === trackName && candidate.kind === "line");
1955
+ all = await downloader.versions(repositoryId);
1956
+ head = all.find((version) => version.id === again?.headVersionId);
1957
+ }
1947
1958
  if (!head) {
1948
1959
  console.error(red(`Could not read the current version of ${trackName}.`));
1949
1960
  return 1;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coderook/cli",
3
- "version": "0.46.6",
3
+ "version": "0.46.7",
4
4
  "description": "The command line for CodeRook (coderook.com): version control and project hosting for projects of any size. Every save is a compressed snapshot, private or public, never used to train AI.",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://coderook.com",