@ref0/git 0.11.0 → 0.14.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.
Files changed (262) hide show
  1. package/dist/access/private-refs.mjs +8 -7
  2. package/dist/auth/auth.d.mts +2 -2
  3. package/dist/auth/auth.mjs +3 -6
  4. package/dist/auth/authorize.mjs +1 -17
  5. package/dist/auth/helpers.mjs +3 -11
  6. package/dist/auth/host-services.mjs +11 -0
  7. package/dist/auth/policy.d.mts +27 -7
  8. package/dist/auth/policy.mjs +8 -4
  9. package/dist/auth/tokens-layer.d.mts +1 -2
  10. package/dist/auth/tokens-layer.mjs +2 -3
  11. package/dist/auth/tokens-public.d.mts +3 -3
  12. package/dist/auth/tokens.d.mts +1 -8
  13. package/dist/auth/tokens.mjs +1 -8
  14. package/dist/auth.d.mts +1 -4
  15. package/dist/auth.mjs +1 -3
  16. package/dist/client/git-http.mjs +18 -4
  17. package/dist/client/push-upload.mjs +33 -7
  18. package/dist/client/remote.mjs +94 -28
  19. package/dist/client/slice-push.d.mts +1 -1
  20. package/dist/contracts/access.d.mts +8 -8
  21. package/dist/contracts/actors.d.mts +2 -34
  22. package/dist/contracts/blobs.d.mts +2 -2
  23. package/dist/contracts/client.d.mts +8 -3
  24. package/dist/contracts/commits.d.mts +10 -2
  25. package/dist/contracts/commits.mjs +9 -1
  26. package/dist/contracts/files.d.mts +2 -2
  27. package/dist/contracts/forks.d.mts +14 -3
  28. package/dist/contracts/git.d.mts +41 -12
  29. package/dist/contracts/git.mjs +3 -2
  30. package/dist/contracts/lfs.d.mts +0 -7
  31. package/dist/contracts/objects.d.mts +80 -10
  32. package/dist/contracts/operations.d.mts +3 -0
  33. package/dist/contracts/operations.mjs +12 -2
  34. package/dist/contracts/push-uploads.d.mts +44 -5
  35. package/dist/contracts/push-uploads.mjs +7 -3
  36. package/dist/contracts/refs.d.mts +28 -6
  37. package/dist/contracts/remote.mjs +1 -3
  38. package/dist/contracts/repo-records.d.mts +1 -0
  39. package/dist/contracts/repos.d.mts +90 -2
  40. package/dist/contracts/repos.mjs +29 -2
  41. package/dist/contracts/result.d.mts +30 -23
  42. package/dist/contracts/result.mjs +24 -9
  43. package/dist/contracts/rules.d.mts +22 -22
  44. package/dist/contracts/server.d.mts +28 -77
  45. package/dist/contracts/signin.d.mts +1 -1
  46. package/dist/contracts/sync.d.mts +90 -19
  47. package/dist/contracts/sync.mjs +34 -10
  48. package/dist/contracts/tokens.d.mts +1 -6
  49. package/dist/contracts/tokens.mjs +1 -2
  50. package/dist/contracts/topics.d.mts +0 -5
  51. package/dist/contracts/work.d.mts +66 -2
  52. package/dist/contracts/work.mjs +8 -1
  53. package/dist/core/access.d.mts +1 -0
  54. package/dist/core/access.mjs +34 -13
  55. package/dist/core/ancestry.d.mts +9 -0
  56. package/dist/core/ancestry.mjs +6 -6
  57. package/dist/core/asked.mjs +68 -0
  58. package/dist/core/call-names.mjs +1 -0
  59. package/dist/core/call-services.mjs +2 -1
  60. package/dist/core/events/deliver.d.mts +1 -0
  61. package/dist/core/events/deliver.mjs +15 -3
  62. package/dist/core/forks.mjs +42 -16
  63. package/dist/core/imports.mjs +156 -13
  64. package/dist/core/index-job.mjs +9 -6
  65. package/dist/core/job-view.mjs +8 -9
  66. package/dist/core/mirror-rows.mjs +19 -4
  67. package/dist/core/objects/commits.mjs +16 -6
  68. package/dist/core/objects/reading.mjs +17 -9
  69. package/dist/core/parts.d.mts +8 -2
  70. package/dist/core/parts.mjs +1 -4
  71. package/dist/core/purge.mjs +76 -6
  72. package/dist/core/push-decide.mjs +1 -1
  73. package/dist/core/push-files.d.mts +12 -0
  74. package/dist/core/push-files.mjs +225 -0
  75. package/dist/core/push-repo.d.mts +8 -0
  76. package/dist/core/push-repo.mjs +56 -0
  77. package/dist/core/push.d.mts +17 -0
  78. package/dist/core/push.mjs +107 -32
  79. package/dist/core/receipts.mjs +26 -10
  80. package/dist/core/ref-view.mjs +59 -5
  81. package/dist/core/refs.mjs +18 -10
  82. package/dist/core/registered-rows.mjs +7 -5
  83. package/dist/core/repack.mjs +16 -11
  84. package/dist/core/repo-info.mjs +16 -9
  85. package/dist/core/repos.mjs +30 -14
  86. package/dist/core/saved-caller-views.d.mts +18 -0
  87. package/dist/core/saved-caller-views.mjs +17 -4
  88. package/dist/core/saved-callers-public.d.mts +3 -3
  89. package/dist/core/saved-callers-public.mjs +3 -2
  90. package/dist/core/saved-callers.d.mts +7 -0
  91. package/dist/core/seam.mjs +2 -2
  92. package/dist/core/view.d.mts +6 -0
  93. package/dist/core/view.mjs +45 -12
  94. package/dist/core/work.mjs +47 -22
  95. package/dist/effect.d.mts +3 -1
  96. package/dist/effect.mjs +3 -1
  97. package/dist/engine/reads.mjs +13 -6
  98. package/dist/engine/walk.mjs +4 -4
  99. package/dist/event-streams/event-streams.d.mts +1 -1
  100. package/dist/event-streams/event-streams.mjs +1 -1
  101. package/dist/event-streams/stream.d.mts +1 -1
  102. package/dist/event-streams/stream.mjs +1 -1
  103. package/dist/extend.d.mts +4 -3
  104. package/dist/extend.mjs +3 -2
  105. package/dist/http/answers.mjs +4 -2
  106. package/dist/http/core-parts.mjs +64 -15
  107. package/dist/http/handler.mjs +2 -2
  108. package/dist/http/node.mjs +5 -4
  109. package/dist/http/parts.d.mts +1 -0
  110. package/dist/http/push-upload-job.mjs +503 -0
  111. package/dist/http/push-uploads.d.mts +1 -0
  112. package/dist/http/push-uploads.mjs +120 -59
  113. package/dist/http/upload-pack.mjs +7 -6
  114. package/dist/index.d.mts +23 -24
  115. package/dist/index.mjs +4 -7
  116. package/dist/lfs/base.d.mts +7 -3
  117. package/dist/lfs/base.mjs +2 -1
  118. package/dist/lfs/copy.d.mts +46 -0
  119. package/dist/lfs/copy.mjs +26 -0
  120. package/dist/lfs/layer.mjs +4 -2
  121. package/dist/lfs/locks.mjs +14 -9
  122. package/dist/lfs/objects.d.mts +6 -0
  123. package/dist/lfs/objects.mjs +95 -61
  124. package/dist/lfs/rows.d.mts +13 -0
  125. package/dist/lfs/service.d.mts +1 -7
  126. package/dist/lfs/service.mjs +1 -9
  127. package/dist/lfs/upload-most.mjs +5 -5
  128. package/dist/node.d.mts +2 -2
  129. package/dist/protocol/bytes/sha256-hasher.d.mts +15 -0
  130. package/dist/protocol/bytes/sha256.d.mts +32 -0
  131. package/dist/protocol/bytes/sha256.mjs +105 -49
  132. package/dist/protocol/wire/advertisement.mjs +9 -4
  133. package/dist/protocol/wire/lines.d.mts +4 -2
  134. package/dist/protocol/wire/lines.mjs +11 -5
  135. package/dist/protocol/wire/push-uploads.mjs +13 -1
  136. package/dist/protocol/wire/v2.mjs +2 -2
  137. package/dist/protocol.d.mts +3 -1
  138. package/dist/protocol.mjs +3 -1
  139. package/dist/queue/advance.mjs +1 -1
  140. package/dist/queue/calls.mjs +21 -1
  141. package/dist/queue/host.d.mts +1 -7
  142. package/dist/queue/host.mjs +1 -7
  143. package/dist/queue/layer.mjs +1 -1
  144. package/dist/queue/trim.mjs +1 -1
  145. package/dist/rest/blobs.d.mts +16 -8
  146. package/dist/rest/blobs.mjs +1 -1
  147. package/dist/rest/caller.d.mts +49 -9
  148. package/dist/rest/caller.mjs +1 -1
  149. package/dist/rest/client.d.mts +10 -3
  150. package/dist/rest/client.mjs +4 -0
  151. package/dist/rest/commits.d.mts +272 -239
  152. package/dist/rest/errors.d.mts +24 -4
  153. package/dist/rest/events.d.mts +9 -6
  154. package/dist/rest/events.mjs +1 -2
  155. package/dist/rest/files.d.mts +112 -100
  156. package/dist/rest/forks.d.mts +146 -122
  157. package/dist/rest/handler.mjs +4 -57
  158. package/dist/rest/handlers.d.mts +1 -1
  159. package/dist/rest/handlers.mjs +18 -4
  160. package/dist/rest/layer.mjs +10 -2
  161. package/dist/rest/me.d.mts +8 -4
  162. package/dist/rest/merges.d.mts +8 -4
  163. package/dist/rest/paths.mjs +1 -0
  164. package/dist/rest/queue.d.mts +292 -236
  165. package/dist/rest/refs.d.mts +98 -69
  166. package/dist/rest/repos.d.mts +133 -105
  167. package/dist/rest/tags.d.mts +100 -88
  168. package/dist/rest/tokens.d.mts +192 -168
  169. package/dist/rest/topics.d.mts +154 -134
  170. package/dist/rest/trees.d.mts +32 -16
  171. package/dist/rest/work.d.mts +467 -20
  172. package/dist/rest/work.mjs +8 -0
  173. package/dist/server/edge-node.d.mts +9 -13
  174. package/dist/server/edge-node.mjs +9 -12
  175. package/dist/server/git-layer.d.mts +1 -6
  176. package/dist/server/git-layer.mjs +2 -2
  177. package/dist/server/git-namespace.d.mts +4 -4
  178. package/dist/server/joined.d.mts +15 -22
  179. package/dist/server/joined.mjs +5 -29
  180. package/dist/server/lifecycle.mjs +107 -23
  181. package/dist/server/registry.mjs +9 -1
  182. package/dist/server/server.mjs +139 -216
  183. package/dist/storage/budget.mjs +2 -2
  184. package/dist/storage/clean-up.mjs +13 -10
  185. package/dist/storage/fetch.mjs +13 -8
  186. package/dist/storage/file-limits.mjs +24 -0
  187. package/dist/storage/index-build.d.mts +1 -0
  188. package/dist/storage/index-build.mjs +106 -62
  189. package/dist/storage/index-fetch.mjs +16 -3
  190. package/dist/storage/index-reach.mjs +33 -5
  191. package/dist/storage/index-remap.mjs +8 -1
  192. package/dist/storage/index-shallow.mjs +5 -3
  193. package/dist/storage/largest-files.mjs +48 -0
  194. package/dist/storage/lfs-pointers.mjs +10 -1
  195. package/dist/storage/loose.mjs +3 -1
  196. package/dist/storage/memory-budget.mjs +154 -8
  197. package/dist/storage/merge-cut.mjs +5 -4
  198. package/dist/storage/pack-reads.mjs +246 -34
  199. package/dist/storage/pack-receive.mjs +83 -25
  200. package/dist/storage/packs.mjs +37 -21
  201. package/dist/storage/prune-keep.mjs +203 -34
  202. package/dist/storage/prune-seen.mjs +11 -6
  203. package/dist/storage/prune-walk.mjs +219 -81
  204. package/dist/storage/prune.mjs +629 -196
  205. package/dist/storage/push-uploads.mjs +36 -12
  206. package/dist/storage/quarantine.mjs +9 -7
  207. package/dist/storage/ready.mjs +47 -19
  208. package/dist/storage/receive-bounds.mjs +29 -8
  209. package/dist/storage/receive-state.mjs +8 -0
  210. package/dist/storage/receive-steps.mjs +71 -18
  211. package/dist/storage/repack.d.mts +1 -1
  212. package/dist/storage/repack.mjs +1 -8
  213. package/dist/storage/rows.mjs +12 -1
  214. package/dist/storage/shallow.d.mts +1 -0
  215. package/dist/storage/shallow.mjs +26 -4
  216. package/dist/storage/stream-whole.mjs +5 -2
  217. package/dist/storage/visible.mjs +29 -1
  218. package/dist/storage/walk.mjs +2 -1
  219. package/dist/store/catalogue.mjs +1 -0
  220. package/dist/store/cursor-seal-namespace.d.mts +2 -0
  221. package/dist/store/cursor-seal-public.d.mts +5 -0
  222. package/dist/store/cursor-seal-public.mjs +5 -0
  223. package/dist/store/cursor-seal.d.mts +16 -2
  224. package/dist/store/cursor-seal.mjs +26 -14
  225. package/dist/store/errors.mjs +9 -1
  226. package/dist/store/records.d.mts +2 -0
  227. package/dist/store/records.mjs +9 -6
  228. package/dist/store/repo-record.d.mts +27 -0
  229. package/dist/store/repo-record.mjs +141 -4
  230. package/dist/store/scopes.mjs +10 -2
  231. package/dist/sync/bound.d.mts +2 -0
  232. package/dist/sync/bound.mjs +11 -2
  233. package/dist/sync/github.d.mts +6 -1
  234. package/dist/sync/github.mjs +63 -42
  235. package/dist/sync/http.mjs +36 -1
  236. package/dist/sync/import.mjs +4 -2
  237. package/dist/sync/layer.mjs +6 -2
  238. package/dist/sync/lfs-batch.mjs +119 -0
  239. package/dist/sync/lfs-copy.mjs +251 -0
  240. package/dist/sync/mirror.mjs +227 -147
  241. package/dist/sync/said.mjs +204 -0
  242. package/dist/sync/smart-http.mjs +4 -2
  243. package/dist/sync/step.mjs +33 -13
  244. package/dist/sync/tries.mjs +71 -24
  245. package/dist/sync.d.mts +2 -2
  246. package/dist/topics/advance.mjs +10 -3
  247. package/dist/topics/host.mjs +1 -19
  248. package/dist/topics/layer.mjs +1 -1
  249. package/package.json +3 -8
  250. package/dist/auth/bridge.mjs +0 -30
  251. package/dist/auth/helpers.d.mts +0 -17
  252. package/dist/core/purge.d.mts +0 -8
  253. package/dist/core/registered-rows.d.mts +0 -4
  254. package/dist/core/repack.d.mts +0 -11
  255. package/dist/http/handler.d.mts +0 -2
  256. package/dist/rest/handler.d.mts +0 -21
  257. package/dist/rest.d.mts +0 -2
  258. package/dist/rest.mjs +0 -1
  259. package/dist/server/registry.d.mts +0 -16
  260. package/dist/server/server.d.mts +0 -36
  261. package/dist/storage/repack-job.d.mts +0 -3
  262. package/dist/topics/host.d.mts +0 -7
@@ -7,10 +7,10 @@ import { readerFloors, readerMost } from "./budget.mjs";
7
7
  import { readWhole } from "./calls.mjs";
8
8
  import { putOnce } from "./index-build.mjs";
9
9
  import { runKey } from "./layout.mjs";
10
- import { pruneSeenBytes, upkeepMemoryOf, upkeepRuns, upkeepTooLarge } from "./memory-budget.mjs";
11
- import { blockBytes, blocksHeld, indexBytes, packReader } from "./pack-reads.mjs";
10
+ import { pruneIndexFloor, pruneSeenBytes, upkeepMemoryOf, upkeepRuns, upkeepTooLarge } from "./memory-budget.mjs";
11
+ import { blockBytes, blocksHeld, indexBytes, isIndexReloaded, isIndexShort, packReader } from "./pack-reads.mjs";
12
12
  import { SeenSaved, seenSet } from "./prune-seen.mjs";
13
- import { StoredRuns, dedupeTableBytes, dropRuns, mergeHeld, mergeRuns, narrowed, narrowedCalls, runWriter, writerHeld } from "./runs.mjs";
13
+ import { StoredRuns, dedupeTableBytes, dropRuns, mergeHeld, mergeRuns, narrowed, narrowedCalls, runWriter, wholeRows, writerHeld } from "./runs.mjs";
14
14
  import { countedReads } from "./walk.mjs";
15
15
  import * as Effect from "effect/Effect";
16
16
  import * as Schema from "effect/Schema";
@@ -80,6 +80,15 @@ const pruneWalkSizes = (objects, blocks) => {
80
80
  blocks: blocksHeld(Math.min(8, Math.max(1, blocks)))
81
81
  };
82
82
  };
83
+ /**
84
+ * The calls a walk `spent` makes, at most, to save where it starts, from at most `start` IDs over
85
+ * `objects` objects: its refs' put, and the save its first admission reserves, `5` and the IDs'
86
+ * puts. A try reserves them before the merge a walk follows, so the walk's start save fits.
87
+ */
88
+ const pruneWalkStartCalls = (objects, start) => {
89
+ const { runBytes } = pruneWalkSizes(objects, 1);
90
+ return 6 + 2 * Math.ceil(Math.max(1, start) * rowBytes / runBytes);
91
+ };
83
92
  /** A walk's sizes at its bounds, the most any repo's take. */
84
93
  const boundSizes = /* @__PURE__ */ pruneWalkSizes(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
85
94
  /**
@@ -122,6 +131,10 @@ const WalkSaved = /* @__PURE__ */ Schema.Union([Schema.Struct({
122
131
  frontier: StoredRuns,
123
132
  at: Schema.optional(Schema.Struct({
124
133
  last: LastId,
134
+ merge: Schema.Struct({
135
+ runs: StoredRuns,
136
+ places: Schema.Array(Count)
137
+ }),
125
138
  reached: StoredRuns,
126
139
  frontier: StoredRuns,
127
140
  trees: StoredRuns
@@ -152,7 +165,8 @@ const walkKeys = (saved) => {
152
165
  saved.frontier,
153
166
  saved.at?.reached,
154
167
  saved.at?.frontier,
155
- saved.at?.trees
168
+ saved.at?.trees,
169
+ saved.at?.merge.runs
156
170
  ]).flatMap((each) => each?.keys ?? []);
157
171
  return saved.phase === "history" ? [...keys, saved.seen.key] : keys;
158
172
  };
@@ -169,18 +183,20 @@ const droppedOnRefusal = (walk, input, pack, _start, _missing, options = {}) =>
169
183
  * frontier merged against the runs reached so far, which the merge carries on as the next
170
184
  * level's, so a tree is read once and a blob put once, by the ID its tree names and never read.
171
185
  * `missing` says what an object the repo doesn't hold does to the walk: a tree it passes over is
172
- * marked reached, unread, so a keep of these runs is given the same `missing` (`keepBits`), or
173
- * fails on the mark. The empty tree is the reader's, held or not, as `keepBits` holds it.
186
+ * marked reached, unread, so a keep of these runs is given the same `missing` (`keepBitsOn`), or
187
+ * fails on the mark. The empty tree is the reader's, held or not, as `keepBitsOn` holds it.
174
188
  *
175
189
  * With `calls`, a step whose forecast passes what the try has left isn't started: the walk ends
176
190
  * with `more`, where it stopped, a history batch, a tree level or a tree partway through one, and a
177
191
  * try given it as `saved` goes on from there, never from `start`. A batch reads as many objects as
178
- * their delta chains' gets fit. A step that passes a whole try is refused by name.
192
+ * their delta chains' gets fit. A step that passes a whole try is refused by name, but in a walk
193
+ * `spent`, after the try's other calls: it ends with `more`, where it was given or where it
194
+ * starts, for a fresh try to go on from.
179
195
  * `known`'s rows, from an earlier walk, are reached already: a tree they hold isn't read again.
180
196
  * A refused fresh walk drops its runs; one given `saved` or `known` keeps them. A test sets
181
197
  * `treesAtOnce` below its bound.
182
198
  */
183
- const pruneWalk = /* @__PURE__ */ Effect.fnUntraced(function* (input, pack, start, missing, { rows = 0, treesAtOnce = pruneTreesAtOnce, calls, saved, known, roots } = {}) {
199
+ const pruneWalk = /* @__PURE__ */ Effect.fnUntraced(function* (input, pack, start, missing, { rows = 0, treesAtOnce = pruneTreesAtOnce, calls, saved, known, roots, spent = false } = {}) {
184
200
  const { allotment, listed } = input;
185
201
  const objects = listed.packs.reduce((sum, each) => sum + each.objects, 0) + rows;
186
202
  const blocks = listed.packs.reduce((sum, each) => sum + Math.ceil(each.bytes / blockBytes), 0);
@@ -196,14 +212,34 @@ const pruneWalk = /* @__PURE__ */ Effect.fnUntraced(function* (input, pack, star
196
212
  const outsideMost = rows + named;
197
213
  const namesBytes = (saved === void 0 ? named : history?.pending.rows ?? 0) * 70;
198
214
  const seenBytes = saved?.phase === "trees" ? 0 : pruneSeenBytes(listed.packs, outsideMost);
215
+ const past = listed.packs.find((each) => indexBytes(each) > pruneIndexFloor);
216
+ if (past !== void 0) return yield* new PruneWalkTooLarge({ message: `pack ${past.id}'s index takes ${indexBytes(past)} bytes, past the ${pruneIndexFloor} a prune walk's reader holds` });
199
217
  const indexes = listed.packs.reduce((sum, each) => sum + indexBytes(each), 0);
200
- const charge = pruneWalkHeld(sizes) + namesBytes + seenBytes + indexes;
218
+ let loadNeeds = () => 3;
219
+ let shortBy;
220
+ let loads = 0;
221
+ const canLoad = () => {
222
+ const needs = loadNeeds();
223
+ loads += 1;
224
+ if (loads === 1 && !spent || fits(calls, needs)) return true;
225
+ shortBy = needs;
226
+ return false;
227
+ };
228
+ const room = {
229
+ bytes: Math.min(indexes, pruneIndexFloor),
230
+ unlisted: outsideMost,
231
+ canLoad
232
+ };
233
+ const evicts = room.bytes < indexes;
234
+ const unlistedBytes = outsideMost * 70;
235
+ const charge = pruneWalkHeld(sizes) + namesBytes + seenBytes + room.bytes + unlistedBytes;
201
236
  if (charge - own > allotment.growable) return yield* new PruneWalkTooLarge({ message: `its walk charges ${charge} bytes with its packs' indexes, past the ${own + allotment.growable} its allotment holds: the repo is past what one prune walk holds` });
202
- const heldBytes = namesBytes + seenBytes;
237
+ const heldBytes = namesBytes + seenBytes + unlistedBytes;
203
238
  yield* Effect.acquireRelease(allotment.grow(heldBytes, upkeepTooLarge), () => Effect.sync(() => allotment.shrink(heldBytes)));
204
239
  const reader = packReader({
205
240
  ...input,
206
- memory
241
+ memory,
242
+ indexFloor: room
207
243
  });
208
244
  let held = 0;
209
245
  yield* Effect.addFinalizer(() => Effect.sync(() => allotment.shrink(held)));
@@ -218,25 +254,43 @@ const pruneWalk = /* @__PURE__ */ Effect.fnUntraced(function* (input, pack, star
218
254
  ...reader,
219
255
  roots
220
256
  }, objects + 1e6, charged, void 0, release);
221
- /** Where each of `oids` is for the seen set: its first listed pack and its rank there. */
222
- const ranksOf = Effect.fnUntraced(function* (oids) {
223
- const at = /* @__PURE__ */ new Map();
224
- let left = [...oids];
225
- for (const pack of listed.packs) {
226
- if (left.length === 0) break;
227
- const { index } = yield* reader.indexed(pack);
228
- left = left.filter((oid) => {
229
- const rank = index.rankOf(oid);
230
- if (rank === void 0) return true;
231
- at.set(oid, {
232
- pack: pack.id,
233
- rank
234
- });
235
- return false;
236
- });
237
- }
238
- return at;
257
+ /**
258
+ * `lookup`'s answer, or `undefined` once it needs a pack whose index the reader's room let go
259
+ * of, or a load past `loadNeeds`, which ends the try where it is, as one short of calls does
260
+ * (#2230).
261
+ */
262
+ const unlessReloaded = (lookup) => lookup.pipe(Effect.catchIf((error) => isIndexReloaded(error) || isIndexShort(error), () => Effect.succeed(void 0)));
263
+ /** A level stopped before `trees` on a lookup `unlessReloaded` ended: past the room or the calls. */
264
+ const stoppedOn = (trees) => shortBy === void 0 ? new WalkStopped({
265
+ trees,
266
+ needs: 0,
267
+ reloaded: true
268
+ }) : new WalkStopped({
269
+ trees,
270
+ needs: shortBy
239
271
  });
272
+ /** `groups` as one list, and the end of each in it, so a batch takes from one group. */
273
+ const grouped = (groups) => {
274
+ const ends = [];
275
+ let end = 0;
276
+ for (const group of groups) {
277
+ end += group.length;
278
+ ends.push(end);
279
+ }
280
+ return {
281
+ oids: groups.flat(),
282
+ ends
283
+ };
284
+ };
285
+ /**
286
+ * `ids` split in two, those to look up now and those after: a try's first alone while the room
287
+ * may let packs go, so its lookups need one pack's index, or every pack's once for an ID none
288
+ * holds, and the try moves. Many at once may let go of a pack they're in, which a try that
289
+ * moved ends on, to go on in the next (#2230).
290
+ */
291
+ const alone = (ids) => !moved && evicts && ids.length > 1 ? [ids.slice(0, 1), ids.slice(1)] : [ids, []];
292
+ /** The end of the group `at` is in, of `ends`. */
293
+ const groupEnd = (ends, at) => ends.find((end) => end > at) ?? at;
240
294
  /** Lets go of what `found` holds of `batch`, once each of it is put. */
241
295
  const letGo = (found, batch) => {
242
296
  for (const object of found.values()) reads.letGo(object);
@@ -282,6 +336,17 @@ const pruneWalk = /* @__PURE__ */ Effect.fnUntraced(function* (input, pack, star
282
336
  const reads = merged.lengths.reduce((sum, length) => sum + Math.ceil(length / readBytes), 0);
283
337
  return reads + (merged.keys.length > ways ? reads + Math.ceil(merged.keys.length / ways) : 0) + putCalls(objects) + 1;
284
338
  };
339
+ /**
340
+ * What a merge going on from its save's places calls yet, a forecast: each run's reads from its
341
+ * place's read, as `mergeRuns` seeks it, none narrowing, and its reached writer's puts (#2231).
342
+ */
343
+ const restCalls = (merge) => {
344
+ const readBytes = wholeRows(rowBytes, sizes.runs.readBytes);
345
+ return merge.runs.lengths.reduce((sum, length, run) => {
346
+ const start = (merge.places[run] ?? 0) * rowBytes;
347
+ return start >= length ? sum : sum + Math.ceil((length - start + start % readBytes) / readBytes);
348
+ }, 0) + putCalls(objects) + 1;
349
+ };
285
350
  const saveCalls = 3 + putCalls(atOnce);
286
351
  const findCalls = 2;
287
352
  const headerGets = 2;
@@ -346,6 +411,8 @@ const pruneWalk = /* @__PURE__ */ Effect.fnUntraced(function* (input, pack, star
346
411
  let moved = false;
347
412
  const most = calls === void 0 ? 0 : Math.min(calls.most.bucket, calls.most.subrequests);
348
413
  const tooMany = (step, needs) => new PruneWalkTooLarge({ message: `${step} needs about ${needs} calls, past the ${most} a try makes` });
414
+ const given = spent && saved !== void 0 ? { more: saved } : void 0;
415
+ const pastRoom = (step) => new PruneWalkTooLarge({ message: `${step} needs more packs' indexes than the ${room.bytes} bytes its reader holds` });
349
416
  let refs;
350
417
  if (saved === void 0) {
351
418
  const refsWriter = writer("refs", sizes.runBytes);
@@ -367,8 +434,8 @@ const pruneWalk = /* @__PURE__ */ Effect.fnUntraced(function* (input, pack, star
367
434
  let prior;
368
435
  let seenFrom;
369
436
  if (history !== void 0) {
370
- const rebuild = narrowedCalls(history.reached.lengths, rowBytes, sizes.runs, input.bucket.multipart).calls + narrowedCalls(history.frontier.lengths, rowBytes, sizes.runs, input.bucket.multipart).calls + mergeCalls(history.pending) + 1;
371
- if (!fits(calls, rebuild + 6)) return yield* tooMany("a history resume", rebuild + 6);
437
+ const rebuild = narrowedCalls(history.reached.lengths, rowBytes, sizes.runs, input.bucket.multipart).calls + narrowedCalls(history.frontier.lengths, rowBytes, sizes.runs, input.bucket.multipart).calls + mergeCalls(history.pending) + 1 + (history.seen.outside === 0 ? 0 : 3 * listed.packs.filter(({ id }) => !history.seen.packs.some((each) => each.id === id)).length);
438
+ if (!fits(calls, rebuild + 6)) return given ?? (yield* tooMany("a history resume", rebuild + 6));
372
439
  const bytes = yield* readWhole(input.bucket, history.seen.key);
373
440
  if (bytes === void 0) return yield* Effect.fail(gitError("DATA_LOSS", `The walk's seen object ${history.seen.key} is gone`));
374
441
  seenFrom = {
@@ -388,59 +455,108 @@ const pruneWalk = /* @__PURE__ */ Effect.fnUntraced(function* (input, pack, star
388
455
  }
389
456
  const seen = yield* seenSet(listed.packs, outsideMost, seenFrom === void 0 ? void 0 : {
390
457
  ...seenFrom,
391
- place: ranksOf
458
+ place: (oids) => reader.ranks(oids, new Set(seenFrom?.saved.packs.map(({ id }) => id)))
392
459
  });
460
+ if (seenFrom !== void 0) reader.release();
461
+ loadNeeds = () => 8 + putCalls(names.length + parents.length);
393
462
  const reached = writer("reached-0", sizes.runBytes);
394
463
  const frontier = frontierWriter("frontier-0");
464
+ /** Where the history is, saved for the next try: its runs, the IDs yet to read, and its seen set. */
465
+ const savedHistory = Effect.fnUntraced(function* () {
466
+ const reachedRuns = yield* reached.finish;
467
+ const frontierRuns = yield* frontier.finish;
468
+ const pending = writer("pending", sizes.runBytes);
469
+ for (const oid of names) yield* put(pending, oidBytes(oid), tags.tree);
470
+ for (const oid of parents) yield* put(pending, oidBytes(oid), tags.reached);
471
+ const key = runKey(pack, `seen.${tried}`, 0);
472
+ const { layout, bytes } = seen.saved(key);
473
+ yield* putOnce(input.bucket, key, bytes);
474
+ return { more: {
475
+ phase: "history",
476
+ count: seen.size(),
477
+ seen: layout,
478
+ refs,
479
+ reached: after(prior?.reached, reachedRuns),
480
+ frontier: after(prior?.frontier, frontierRuns),
481
+ pending: yield* pending.finish,
482
+ known: knownRuns
483
+ } };
484
+ });
485
+ /** The try ended where it is: saved, or refused if it walked nothing. */
486
+ const stopHere = () => moved || spent ? given !== void 0 && !moved ? Effect.succeed(given) : savedHistory() : Effect.fail(shortBy === void 0 ? pastRoom("a history batch") : tooMany("a history batch", shortBy));
487
+ /**
488
+ * The next IDs to read, the names' before the parents': those the first pack that holds some
489
+ * of them holds, as `nextPack` finds it, or all of them once no listed pack holds any;
490
+ * `undefined` once only packs the room let go of may hold what's left, which the next try
491
+ * reads.
492
+ */
493
+ const nextLevel = Effect.fnUntraced(function* () {
494
+ for (const fromNames of [true, false]) {
495
+ const pool = fromNames ? names : parents;
496
+ if (pool.length === 0) continue;
497
+ const { placed, unlisted } = yield* reader.nextPack(pool);
498
+ if (placed.size === 0 && !unlisted) continue;
499
+ const level = unlisted ? [...new Set(pool)] : [...placed.keys()];
500
+ const rest = unlisted ? [] : pool.filter((oid) => !placed.has(oid));
501
+ if (fromNames) names = rest;
502
+ else parents = rest;
503
+ return {
504
+ fromNames,
505
+ level,
506
+ placed
507
+ };
508
+ }
509
+ });
395
510
  while (names.length > 0 || parents.length > 0) {
396
- const fromNames = names.length > 0;
397
- const level = fromNames ? names : parents;
398
- if (fromNames) names = [];
399
- else parents = [];
511
+ const step = yield* unlessReloaded(nextLevel());
512
+ if (step === void 0) return yield* stopHere();
513
+ const { fromNames, level, placed } = step;
514
+ const back = (at) => {
515
+ if (fromNames) names = [...level.slice(at), ...names];
516
+ else parents = [...level.slice(at), ...parents];
517
+ };
400
518
  for (let at = 0; at < level.length;) {
401
519
  const left = level.length - at + names.length + parents.length;
402
- const { count: admits, needs } = yield* admitted(level.slice(at, at + 64), 5 + putCalls(left));
520
+ const admission = yield* unlessReloaded(admitted(level.slice(at, at + 64), 5 + putCalls(left)));
521
+ if (admission === void 0) {
522
+ back(at);
523
+ return yield* stopHere();
524
+ }
525
+ const { count: admits, needs } = admission;
403
526
  if (admits === 0) {
404
- if (!moved) return yield* tooMany("a history batch", needs);
405
- const reachedRuns = yield* reached.finish;
406
- const frontierRuns = yield* frontier.finish;
407
- const pending = writer("pending", sizes.runBytes);
408
- const rest = level.slice(at);
409
- for (const oid of fromNames ? [...rest, ...names] : names) yield* put(pending, oidBytes(oid), tags.tree);
410
- for (const oid of fromNames ? parents : [...rest, ...parents]) yield* put(pending, oidBytes(oid), tags.reached);
411
- const key = runKey(pack, `seen.${tried}`, 0);
412
- const { layout, bytes } = seen.saved(key);
413
- yield* putOnce(input.bucket, key, bytes);
414
- return { more: {
415
- phase: "history",
416
- count: seen.size(),
417
- seen: layout,
418
- refs,
419
- reached: after(prior?.reached, reachedRuns),
420
- frontier: after(prior?.frontier, frontierRuns),
421
- pending: yield* pending.finish,
422
- known: knownRuns
423
- } };
527
+ if (!moved && given !== void 0) return given;
528
+ if (!moved && !spent) return yield* tooMany("a history batch", needs);
529
+ back(at);
530
+ return yield* savedHistory();
424
531
  }
425
- moved = true;
426
532
  const slice = level.slice(at, at + admits);
533
+ const looked = yield* unlessReloaded(Effect.gen(function* () {
534
+ const blobs = fromNames ? yield* reader.wholeBlobs(slice) : /* @__PURE__ */ new Set();
535
+ const unread = [...new Set(slice)].filter((oid) => !blobs.has(oid) && !seen.has(oid, placed.get(oid)));
536
+ return {
537
+ blobs,
538
+ unread,
539
+ found: unread.length === 0 ? /* @__PURE__ */ new Map() : yield* reads(unread)
540
+ };
541
+ }));
542
+ if (looked === void 0) {
543
+ back(at);
544
+ return yield* stopHere();
545
+ }
546
+ const { blobs, unread, found } = looked;
547
+ moved = true;
427
548
  at += admits;
428
- const blobs = fromNames ? yield* reader.wholeBlobs(slice) : /* @__PURE__ */ new Set();
429
- const ranked = yield* ranksOf(slice);
430
- const batch = [];
431
549
  for (const oid of slice) {
432
- const id = oidBytes(oid);
433
- const added = seen.add(oid, ranked.get(oid));
550
+ const added = seen.add(oid, placed.get(oid));
434
551
  if (added === "full") return yield* Effect.fail(gitError("DATA_LOSS", `The walk reaches more than ${outsideMost} objects no listed pack holds: past its rows and refs`));
435
552
  if (!added) continue;
436
- if (blobs.has(oid)) yield* put(reached, id, tags.reached);
437
- else batch.push(oid);
438
- }
439
- const found = batch.length === 0 ? /* @__PURE__ */ new Map() : yield* reads(batch);
440
- for (const oid of batch) {
553
+ const id = oidBytes(oid);
554
+ if (blobs.has(oid)) {
555
+ yield* put(reached, id, tags.reached);
556
+ continue;
557
+ }
441
558
  const object = yield* foundOf(found, oid);
442
559
  if (object === void 0) continue;
443
- const id = oidBytes(oid);
444
560
  if (object.type === "tree") {
445
561
  yield* put(frontier, id, tags.tree);
446
562
  continue;
@@ -450,7 +566,7 @@ const pruneWalk = /* @__PURE__ */ Effect.fnUntraced(function* (input, pack, star
450
566
  if (object.type === "tag") names.push(...object.history);
451
567
  else parents.push(...object.history);
452
568
  }
453
- if (batch.length > 0) yield* letGo(found, batch);
569
+ if (unread.length > 0) yield* letGo(found, unread);
454
570
  }
455
571
  }
456
572
  runs = after(prior?.reached, yield* reached.finish);
@@ -461,12 +577,14 @@ const pruneWalk = /* @__PURE__ */ Effect.fnUntraced(function* (input, pack, star
461
577
  let within = saved?.phase === "trees" ? saved.at : void 0;
462
578
  for (let depth = from + 1; next.rows > 0; depth += 1) {
463
579
  const merged = joined(runs, next, `join-${depth}.${tried}`);
464
- const reserve = mergeCalls(merged);
465
580
  const goesOn = within;
466
581
  within = void 0;
582
+ const reserve = goesOn === void 0 ? mergeCalls(merged) : restCalls(goesOn.merge);
467
583
  const needs = reserve + 2 + saveCalls;
584
+ loadNeeds = () => 3 + reserve + saveCalls;
468
585
  if (!fits(calls, needs)) {
469
- if (!moved) return yield* tooMany(`level ${depth}`, needs);
586
+ if (!moved && given !== void 0) return given;
587
+ if (!moved && !spent) return yield* tooMany(`level ${depth}`, needs);
470
588
  return { more: {
471
589
  phase: "trees",
472
590
  commits,
@@ -480,18 +598,32 @@ const pruneWalk = /* @__PURE__ */ Effect.fnUntraced(function* (input, pack, star
480
598
  const frontier = frontierWriter(`frontier-${depth}`);
481
599
  let trees = [];
482
600
  const last = goesOn === void 0 ? /* @__PURE__ */ new Uint8Array(20) : oidBytes(goesOn.last);
601
+ const few = goesOn?.merge.runs ?? (yield* narrowed(input.bucket, pack, merged, sizes.runs));
602
+ let placesOf;
483
603
  const readTrees = Effect.fnUntraced(function* () {
484
- const ordered = yield* reader.inPackOrder(trees);
485
- trees = [];
604
+ while (trees.length > 0) {
605
+ const [asked, rest] = alone(trees);
606
+ trees = rest;
607
+ yield* readSome(asked);
608
+ }
609
+ });
610
+ const readSome = Effect.fnUntraced(function* (asked) {
611
+ const groups = yield* unlessReloaded(reader.inPackGroups(asked));
612
+ if (groups === void 0) return yield* stoppedOn(asked);
613
+ const { oids: ordered, ends } = grouped(groups);
614
+ const stoppedAt = (at) => stoppedOn(ordered.slice(at));
486
615
  for (let at = 0; at < ordered.length;) {
487
- const next = ordered.slice(at, at + 64);
488
- const { count, needs } = yield* admitted(next, reserve + saveCalls);
616
+ const next = ordered.slice(at, Math.min(at + 64, groupEnd(ends, at)));
617
+ const admission = yield* unlessReloaded(admitted(next, reserve + saveCalls));
618
+ if (admission === void 0) return yield* stoppedAt(at);
619
+ const { count, needs } = admission;
489
620
  if (count === 0) return yield* new WalkStopped({
490
621
  trees: ordered.slice(at),
491
622
  needs
492
623
  });
493
624
  const batch = next.slice(0, count);
494
- const found = yield* reads(batch);
625
+ const found = yield* unlessReloaded(reads(batch));
626
+ if (found === void 0) return yield* stoppedAt(at);
495
627
  for (const [index, oid] of batch.entries()) {
496
628
  const object = yield* foundOf(found, oid);
497
629
  if (object === void 0) continue;
@@ -521,7 +653,8 @@ const pruneWalk = /* @__PURE__ */ Effect.fnUntraced(function* (input, pack, star
521
653
  yield* eachOf(goesOn.trees, (id) => void trees.push(hexOf(id)));
522
654
  yield* readTrees();
523
655
  }
524
- yield* mergeRuns(input.bucket, pack, merged, (bytes, at) => {
656
+ yield* mergeRuns(input.bucket, pack, few, (bytes, at, _run, places) => {
657
+ placesOf = places;
525
658
  if (!pastId(bytes, at, last)) return void 0;
526
659
  const id = bytes.subarray(at, at + 20);
527
660
  last.set(id);
@@ -531,18 +664,23 @@ const pruneWalk = /* @__PURE__ */ Effect.fnUntraced(function* (input, pack, star
531
664
  if (tag !== tags.tree) return marked;
532
665
  trees.push(hexOf(id));
533
666
  return trees.length < atOnce ? marked : Effect.andThen(marked, readTrees());
534
- }, sizes.runs);
667
+ }, sizes.runs, void 0, goesOn?.merge.places);
535
668
  if (trees.length > 0) yield* readTrees();
536
669
  });
537
670
  const stopped = yield* Effect.as(walked, void 0).pipe(Effect.catchTag("WalkStopped", (stop) => Effect.succeed(stop)));
538
671
  if (stopped !== void 0) {
539
- if (!moved) return yield* tooMany(`a tree at level ${depth}`, stopped.needs);
672
+ if (!moved && given !== void 0) return given;
673
+ if (!moved && !spent) return yield* stopped.reloaded === true ? pastRoom(`a tree at level ${depth}`) : tooMany(`a tree at level ${depth}`, stopped.needs);
540
674
  const levelRuns = after(goesOn?.reached, yield* level.finish);
541
675
  const frontierRuns = after(goesOn?.frontier, yield* frontier.finish);
542
676
  const pending = writer(`trees-${depth}`, sizes.runBytes);
543
677
  for (const oid of [...stopped.trees, ...trees]) yield* put(pending, oidBytes(oid), tags.tree);
544
678
  const at = {
545
679
  last: hexOf(last),
680
+ merge: {
681
+ runs: few,
682
+ places: placesOf?.() ?? goesOn?.merge.places ?? few.keys.map(() => 0)
683
+ },
546
684
  reached: levelRuns,
547
685
  frontier: frontierRuns,
548
686
  trees: yield* pending.finish
@@ -566,4 +704,4 @@ const pruneWalk = /* @__PURE__ */ Effect.fnUntraced(function* (input, pack, star
566
704
  refs
567
705
  };
568
706
  }, droppedOnRefusal);
569
- export { PruneWalkTooLarge, WalkSaved, pruneTreesAtOnce, pruneWalk, pruneWalkHeld, pruneWalkSizes, pruneWalkTables, reachedRunBytes, walkKeys };
707
+ export { PruneWalkTooLarge, WalkSaved, fits, pruneTreesAtOnce, pruneWalk, pruneWalkHeld, pruneWalkSizes, pruneWalkStartCalls, pruneWalkTables, reachedRunBytes, walkKeys };