@coderook/cli 0.46.3 → 0.46.5

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.
@@ -136,6 +136,9 @@ const signatures = new Map();
136
136
  const KEPT_LISTINGS = 3;
137
137
  /* The largest new file a one-request delta save carries as its own bytes. */
138
138
  const DELTA_SAVE_NEW_FILE_BYTES = 1024 * 1024;
139
+ /* With oneRoundTrip: an edit this small goes whole, up to this much in one save. */
140
+ const WHOLE_EDIT_BYTES = 256 * 1024;
141
+ const WHOLE_EDITS_BUDGET = 1024 * 1024;
139
142
  /** A delta save the service would not take; the save is sent the ordinary way. */
140
143
  class DeltaSaveRefused extends Error {
141
144
  }
@@ -521,27 +524,29 @@ class Uploader {
521
524
  truncated version of it would propose deleting everything the walk did
522
525
  not reach.
523
526
  */
524
- const everything = await (0, profile_js_1.timed)("survey the project", () => (0, worktree_js_1.surveyFiles)(request.localPath, rules, (found) =>
525
- /*
526
- The walk is the first minute of a large save and said nothing at
527
- all, so the panel could only show 0%. A running count of what has
528
- been seen is not a fraction of anything — it is the evidence that
529
- the thing is working.
530
- */
531
- report({
532
- stage: "plan",
533
- files: found,
534
- totalFiles: 0,
535
- bytes: 0,
536
- totalBytes: 0,
537
- path: "Looking through the folder",
527
+ const everything = request.survey
528
+ ? [...request.survey].sort((left, right) => left.path.localeCompare(right.path))
529
+ : await (0, profile_js_1.timed)("survey the project", () => (0, worktree_js_1.surveyFiles)(request.localPath, rules, (found) =>
538
530
  /*
539
- Between the key check and the reading, and it holds its own place
540
- on the bar rather than dropping back to nought between them.
531
+ The walk is the first minute of a large save and said nothing at
532
+ all, so the panel could only show 0%. A running count of what has
533
+ been seen is not a fraction of anything — it is the evidence that
534
+ the thing is working.
541
535
  */
542
- percent: 6,
543
- bytesPerSecond: 0,
544
- })));
536
+ report({
537
+ stage: "plan",
538
+ files: found,
539
+ totalFiles: 0,
540
+ bytes: 0,
541
+ totalBytes: 0,
542
+ path: "Looking through the folder",
543
+ /*
544
+ Between the key check and the reading, and it holds its own place
545
+ on the bar rather than dropping back to nought between them.
546
+ */
547
+ percent: 6,
548
+ bytesPerSecond: 0,
549
+ })));
545
550
  if (tracked) {
546
551
  const layers = await (0, worktree_js_1.collectLayers)(request.localPath, rules);
547
552
  this.trackedByGit = everything
@@ -719,6 +724,11 @@ class Uploader {
719
724
  Anything it does not answer is hashed here as before.
720
725
  */
721
726
  const ahead = new Map();
727
+ const surveyedTimes = new Map();
728
+ for (const one of request.survey ?? []) {
729
+ if (typeof one.mtimeMs === "number")
730
+ surveyedTimes.set(one.path, one.mtimeMs);
731
+ }
722
732
  {
723
733
  const unknown = sending.filter((file) => {
724
734
  const remembered = this.plannedDigests.get(file.path) ?? request.knownDigests?.get(file.path);
@@ -749,20 +759,32 @@ class Uploader {
749
759
  const full = node_path_1.default.join(request.localPath, file.path);
750
760
  let size;
751
761
  let mtimeMs;
752
- try {
753
- const info = await (0, promises_1.stat)(full);
754
- size = info.size;
755
- mtimeMs = info.mtimeMs;
756
- }
757
- catch (error) {
758
- // Unreadable now, though it was listed a moment ago. Skipping it
759
- // would publish a version quietly missing a file the person chose.
760
- vanished.push(file.path);
761
- readFailures.set(file.path, error instanceof Error ? error.message : String(error));
762
- hashed += 1;
763
- readDone += file.size;
764
- continue;
762
+ /*
763
+ The caller's walk of this folder (UploadRequest.survey) stated every
764
+ file a moment ago; statting all of them again here cost 0.4 s per
765
+ pass on a 14,000-file folder, twice a save. A file that moved since
766
+ is still caught: its bytes are hashed before they are sent.
767
+ */
768
+ const surveyed = surveyedTimes.get(file.path);
769
+ if (surveyed !== undefined) {
770
+ size = file.size;
771
+ mtimeMs = surveyed;
765
772
  }
773
+ else
774
+ try {
775
+ const info = await (0, promises_1.stat)(full);
776
+ size = info.size;
777
+ mtimeMs = info.mtimeMs;
778
+ }
779
+ catch (error) {
780
+ // Unreadable now, though it was listed a moment ago. Skipping it
781
+ // would publish a version quietly missing a file the person chose.
782
+ vanished.push(file.path);
783
+ readFailures.set(file.path, error instanceof Error ? error.message : String(error));
784
+ hashed += 1;
785
+ readDone += file.size;
786
+ continue;
787
+ }
766
788
  /*
767
789
  Hashing is the first thing that opens the file, and opening is what
768
790
  fails on a file another program holds — a database in use, a browser
@@ -1218,6 +1240,7 @@ class Uploader {
1218
1240
  ...(this.trackedByGit.length ? { trackedByGit: this.trackedByGit } : {}),
1219
1241
  ...(removed.length ? { removed } : {}),
1220
1242
  ...(executable.length ? { executable } : {}),
1243
+ ...(request.presence ? { presence: request.presence } : {}),
1221
1244
  }));
1222
1245
  const rest = new Uint8Array(4 + header.byteLength + plan.envelope.byteLength);
1223
1246
  new DataView(rest.buffer).setUint32(0, header.byteLength, false);
@@ -1384,6 +1407,7 @@ class Uploader {
1384
1407
  ...(completed.version.state === "held" ? { held: true } : {}),
1385
1408
  ...(completed.mergeTrack ? { mergeTrack: completed.mergeTrack } : {}),
1386
1409
  ...(completed.repeated ? { repeated: true } : {}),
1410
+ ...(completed.presence === true ? { presenceRecorded: true } : {}),
1387
1411
  sourceBytes: completed.version.sourceSize ?? sourceBytes,
1388
1412
  storedBytes: completed.version.storedSize ?? storedBytes,
1389
1413
  sentBytes: progress.transferred,
@@ -2220,10 +2244,57 @@ class Uploader {
2220
2244
  asks the service for the account's storage and allowance, with an empty
2221
2245
  object list, so what a person is shown before saving stays true.
2222
2246
  */
2247
+ /*
2248
+ When the save is likely to go as one request, the storage check is
2249
+ asked while the patches are made (and their signatures fetched), not
2250
+ after: two round trips at once instead of one after the other. If it
2251
+ turns out not to, the ordinary path prices the save again as it always
2252
+ has, and this answer is simply not used.
2253
+ */
2254
+ const quoting = this.planning &&
2255
+ !request.oneRoundTrip &&
2256
+ !this.deltaSaveRefused &&
2257
+ typeof request.baseVersionId === "string" &&
2258
+ prior.size > 0 &&
2259
+ !request.allowSecrets &&
2260
+ declarations.length <= delta_js_1.DELTA_BATCH_MAX_FILES &&
2261
+ (await this.serviceFeatures()).includes("delta-save-v1")
2262
+ ? this.call(`/v1/repositories/${repositoryId}/uploads/preflight`, {
2263
+ method: "POST",
2264
+ contentType: "application/json",
2265
+ body: JSON.stringify({
2266
+ sourceBytes: totalBytes,
2267
+ excludedBytes: 0,
2268
+ objects: [],
2269
+ track: request.track ?? "main",
2270
+ }),
2271
+ })
2272
+ : null;
2273
+ quoting?.catch(() => undefined);
2223
2274
  const oneRequest = this.deltaSaveRefused
2224
2275
  ? null
2225
2276
  : await this.deltaSavePlan(request, repositoryId, declarations, prior);
2226
- if (oneRequest && this.planning) {
2277
+ if (oneRequest && this.planning && request.oneRoundTrip) {
2278
+ /* Priced from its own numbers: the service checks storage when the save arrives. */
2279
+ this.plannedQuote = {
2280
+ quoteId: "",
2281
+ repositoryId,
2282
+ sourceBytes: totalBytes,
2283
+ excludedBytes: 0,
2284
+ compactedBytes: oneRequest.patchBytes,
2285
+ reusableBytes: 0,
2286
+ chargeableBytes: oneRequest.patchBytes,
2287
+ storage: { usedBytes: 0, quotaBytes: 0, remainingBytes: 0 },
2288
+ allowance: {
2289
+ exempt: true, stage: 0, unlockedPercent: 0, unlockedBytes: 0, chargedBytes: 0,
2290
+ reservedBytes: 0, remainingBytes: 0, periodStart: "", periodEnd: "", nextUnlockAt: null,
2291
+ },
2292
+ expiresAt: "",
2293
+ objects: {},
2294
+ estimated: true,
2295
+ };
2296
+ }
2297
+ else if (oneRequest && this.planning) {
2227
2298
  report({
2228
2299
  stage: "publish",
2229
2300
  files: declarations.length,
@@ -2234,7 +2305,7 @@ class Uploader {
2234
2305
  percent: 96,
2235
2306
  bytesPerSecond: 0,
2236
2307
  });
2237
- this.plannedQuote = await this.call(`/v1/repositories/${repositoryId}/uploads/preflight`, {
2308
+ this.plannedQuote = await (quoting ?? this.call(`/v1/repositories/${repositoryId}/uploads/preflight`, {
2238
2309
  method: "POST",
2239
2310
  contentType: "application/json",
2240
2311
  body: JSON.stringify({
@@ -2243,7 +2314,9 @@ class Uploader {
2243
2314
  objects: [],
2244
2315
  track: request.track ?? "main",
2245
2316
  }),
2246
- });
2317
+ }));
2318
+ }
2319
+ if (oneRequest && this.planning && this.plannedQuote) {
2247
2320
  return {
2248
2321
  repositoryId,
2249
2322
  versionId: "",
@@ -2818,6 +2891,13 @@ class Uploader {
2818
2891
  const targets = [];
2819
2892
  const held = [];
2820
2893
  const signed = [];
2894
+ /*
2895
+ For a client waiting on the save, a small edit goes whole: asking for
2896
+ its base's signatures is a round trip (0.4 s from Australia to the
2897
+ service), and a 20 KB source file compressed in the envelope costs far
2898
+ less than that to send. Bounded, so a save of many edits still asks.
2899
+ */
2900
+ let wholeBudget = request.oneRoundTrip ? WHOLE_EDITS_BUDGET : 0;
2821
2901
  for (const [index, declaration] of declarations.entries()) {
2822
2902
  let target;
2823
2903
  try {
@@ -2834,6 +2914,10 @@ class Uploader {
2834
2914
  held.push(base);
2835
2915
  if (base)
2836
2916
  continue;
2917
+ if (was && declaration.size <= WHOLE_EDIT_BYTES && declaration.size <= wholeBudget) {
2918
+ wholeBudget -= declaration.size;
2919
+ continue;
2920
+ }
2837
2921
  if (was && (0, delta_js_1.estimateDeltaSignatureBytes)(was.sourceSize, 2) < declaration.size * 0.8) {
2838
2922
  signed.push(index);
2839
2923
  }
@@ -3613,6 +3697,14 @@ class Uploader {
3613
3697
  this.chunkingSupported = (await this.serviceFeatures()).includes("chunked-files");
3614
3698
  return this.chunkingSupported;
3615
3699
  }
3700
+ /*
3701
+ Asks /health now, so the answer is in hand by the time a plan needs it.
3702
+ A save's planning used to stop for this round trip after the folder had
3703
+ been scanned; asked when the command starts, it overlaps the scan.
3704
+ */
3705
+ prefetchCapabilities() {
3706
+ void this.serviceCapabilities();
3707
+ }
3616
3708
  /** What /health says this deployment accepts. One request per uploader. */
3617
3709
  async serviceCapabilities() {
3618
3710
  if (this.capabilityRequest)
@@ -932,7 +932,20 @@ stats,
932
932
  /** Filled with how much of the folder the cap allowed through. */
933
933
  outcome,
934
934
  /** False for a read-only report: the listing's sizes and times may stand in for a stat (see engineScan). */
935
- exact = true) {
935
+ exact = true,
936
+ /*
937
+ Filled with every file the walk found that the rules would send, capped or
938
+ not. A caller asking which saved files are gone from the folder needs only
939
+ stat the ones missing from it, rather than every file the save holds.
940
+ */
941
+ seen,
942
+ /*
943
+ Told how many files have been read so far and how many need reading: the
944
+ ones whose size or time no longer match what was recorded. A folder with
945
+ no usable record reads everything, which on a large project is minutes,
946
+ and said nothing while it did.
947
+ */
948
+ onReading) {
936
949
  /* What git already tracks goes up whatever a rule says; see gitTracked. */
937
950
  const tracked = await gitTracked(root);
938
951
  /*
@@ -943,7 +956,7 @@ exact = true) {
943
956
  const native = await (0, native_engine_js_1.engineScan)(root, rules, tracked, baseline?.size ?? stats?.size, exact);
944
957
  const layers = native ? [] : await collectLayers(root, rules);
945
958
  const files = [];
946
- const present = new Set();
959
+ const present = seen ?? new Set();
947
960
  const pending = [root];
948
961
  /** Everything the rules would send, including what the cap keeps out. */
949
962
  const found = [];
@@ -964,7 +977,7 @@ exact = true) {
964
977
  */
965
978
  for (const one of native ?? []) {
966
979
  present.add(one.path);
967
- outcome?.sizes.push({ path: one.path, size: one.size });
980
+ outcome?.sizes.push({ path: one.path, size: one.size, mtimeMs: one.mtimeMs });
968
981
  found.push({ full: node_path_1.default.join(root, ...one.path.split("/")), relative: one.path, size: one.size, mtimeMs: one.mtimeMs });
969
982
  }
970
983
  for (let next = 0; !native && next < pending.length; next++) {
@@ -1029,7 +1042,7 @@ exact = true) {
1029
1042
  kept out — files sitting right there on disk.
1030
1043
  */
1031
1044
  present.add(one.relative);
1032
- outcome?.sizes.push({ path: one.relative, size: one.size });
1045
+ outcome?.sizes.push({ path: one.relative, size: one.size, mtimeMs: one.mtimeMs });
1033
1046
  found.push(one);
1034
1047
  }
1035
1048
  }
@@ -1056,6 +1069,8 @@ exact = true) {
1056
1069
  return one.size <= LINE_COUNT_LIMIT || (baseline?.has(one.relative) ?? false);
1057
1070
  };
1058
1071
  let readAheadTo = 0;
1072
+ const toRead = onReading ? order.filter(needsReading).length : 0;
1073
+ let read = 0;
1059
1074
  for (let at = 0; at < order.length; at += 1) {
1060
1075
  if (files.length >= limit)
1061
1076
  break;
@@ -1085,6 +1100,11 @@ exact = true) {
1085
1100
  }
1086
1101
  const one = order[at];
1087
1102
  const { full, relative, size, mtimeMs } = one;
1103
+ if (onReading && toRead && needsReading(one)) {
1104
+ read += 1;
1105
+ if (read === 1 || read % 25 === 0 || read === toRead)
1106
+ onReading(read, toRead);
1107
+ }
1088
1108
  const measured = readAhead.get(relative) ??
1089
1109
  (await measure(full, size, {
1090
1110
  mtimeMs,