@ref0/git 0.12.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 (247) hide show
  1. package/dist/access/private-refs.mjs +3 -3
  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 +11 -1
  28. package/dist/contracts/git.d.mts +18 -7
  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 +23 -18
  42. package/dist/contracts/result.mjs +17 -4
  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 +79 -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.mjs +27 -5
  54. package/dist/core/ancestry.d.mts +9 -0
  55. package/dist/core/ancestry.mjs +6 -6
  56. package/dist/core/asked.mjs +42 -4
  57. package/dist/core/call-names.mjs +1 -0
  58. package/dist/core/call-services.mjs +2 -1
  59. package/dist/core/events/deliver.mjs +10 -2
  60. package/dist/core/forks.mjs +3 -3
  61. package/dist/core/imports.mjs +147 -9
  62. package/dist/core/index-job.mjs +9 -6
  63. package/dist/core/job-view.mjs +8 -9
  64. package/dist/core/mirror-rows.mjs +19 -4
  65. package/dist/core/objects/commits.mjs +16 -6
  66. package/dist/core/parts.d.mts +6 -1
  67. package/dist/core/parts.mjs +1 -4
  68. package/dist/core/purge.mjs +76 -6
  69. package/dist/core/push-decide.mjs +1 -1
  70. package/dist/core/push-files.d.mts +12 -0
  71. package/dist/core/push-files.mjs +225 -0
  72. package/dist/core/push-repo.d.mts +8 -0
  73. package/dist/core/push-repo.mjs +56 -0
  74. package/dist/core/push.d.mts +17 -0
  75. package/dist/core/push.mjs +98 -26
  76. package/dist/core/receipts.mjs +26 -10
  77. package/dist/core/ref-view.mjs +59 -5
  78. package/dist/core/refs.mjs +13 -8
  79. package/dist/core/repack.mjs +11 -8
  80. package/dist/core/repo-info.mjs +16 -9
  81. package/dist/core/repos.mjs +30 -14
  82. package/dist/core/seam.mjs +2 -2
  83. package/dist/core/view.d.mts +6 -0
  84. package/dist/core/view.mjs +45 -12
  85. package/dist/core/work.mjs +47 -22
  86. package/dist/effect.d.mts +3 -1
  87. package/dist/effect.mjs +3 -1
  88. package/dist/engine/reads.mjs +13 -6
  89. package/dist/engine/walk.mjs +4 -4
  90. package/dist/event-streams/event-streams.d.mts +1 -1
  91. package/dist/event-streams/event-streams.mjs +1 -1
  92. package/dist/event-streams/stream.d.mts +1 -1
  93. package/dist/event-streams/stream.mjs +1 -1
  94. package/dist/extend.d.mts +3 -3
  95. package/dist/extend.mjs +2 -2
  96. package/dist/http/answers.mjs +4 -2
  97. package/dist/http/core-parts.mjs +64 -15
  98. package/dist/http/handler.mjs +2 -2
  99. package/dist/http/node.mjs +5 -4
  100. package/dist/http/parts.d.mts +1 -0
  101. package/dist/http/push-upload-job.mjs +503 -0
  102. package/dist/http/push-uploads.d.mts +1 -0
  103. package/dist/http/push-uploads.mjs +114 -56
  104. package/dist/http/upload-pack.mjs +7 -6
  105. package/dist/index.d.mts +23 -24
  106. package/dist/index.mjs +4 -7
  107. package/dist/lfs/base.d.mts +7 -3
  108. package/dist/lfs/base.mjs +2 -1
  109. package/dist/lfs/copy.d.mts +46 -0
  110. package/dist/lfs/copy.mjs +26 -0
  111. package/dist/lfs/layer.mjs +4 -2
  112. package/dist/lfs/objects.d.mts +6 -0
  113. package/dist/lfs/objects.mjs +68 -41
  114. package/dist/lfs/rows.d.mts +13 -0
  115. package/dist/lfs/service.d.mts +1 -7
  116. package/dist/lfs/service.mjs +1 -9
  117. package/dist/lfs/upload-most.mjs +5 -5
  118. package/dist/node.d.mts +2 -2
  119. package/dist/protocol/bytes/sha256-hasher.d.mts +15 -0
  120. package/dist/protocol/bytes/sha256.d.mts +32 -0
  121. package/dist/protocol/bytes/sha256.mjs +105 -49
  122. package/dist/protocol/wire/advertisement.mjs +9 -4
  123. package/dist/protocol/wire/lines.d.mts +4 -2
  124. package/dist/protocol/wire/lines.mjs +11 -5
  125. package/dist/protocol/wire/push-uploads.mjs +13 -1
  126. package/dist/protocol/wire/v2.mjs +2 -2
  127. package/dist/protocol.d.mts +3 -1
  128. package/dist/protocol.mjs +3 -1
  129. package/dist/queue/advance.mjs +1 -1
  130. package/dist/queue/host.d.mts +1 -7
  131. package/dist/queue/host.mjs +1 -7
  132. package/dist/queue/layer.mjs +1 -1
  133. package/dist/queue/trim.mjs +1 -1
  134. package/dist/rest/blobs.d.mts +31 -23
  135. package/dist/rest/blobs.mjs +1 -1
  136. package/dist/rest/caller.d.mts +49 -9
  137. package/dist/rest/caller.mjs +1 -1
  138. package/dist/rest/client.d.mts +10 -3
  139. package/dist/rest/client.mjs +4 -0
  140. package/dist/rest/commits.d.mts +273 -240
  141. package/dist/rest/errors.d.mts +24 -4
  142. package/dist/rest/events.d.mts +9 -6
  143. package/dist/rest/events.mjs +1 -2
  144. package/dist/rest/files.d.mts +127 -115
  145. package/dist/rest/forks.d.mts +121 -97
  146. package/dist/rest/handler.mjs +4 -57
  147. package/dist/rest/handlers.d.mts +1 -1
  148. package/dist/rest/handlers.mjs +18 -4
  149. package/dist/rest/layer.mjs +2 -1
  150. package/dist/rest/me.d.mts +8 -4
  151. package/dist/rest/merges.d.mts +8 -4
  152. package/dist/rest/paths.mjs +1 -0
  153. package/dist/rest/queue.d.mts +282 -226
  154. package/dist/rest/refs.d.mts +205 -176
  155. package/dist/rest/repos.d.mts +108 -80
  156. package/dist/rest/tags.d.mts +24 -12
  157. package/dist/rest/tokens.d.mts +235 -211
  158. package/dist/rest/topics.d.mts +53 -33
  159. package/dist/rest/trees.d.mts +105 -89
  160. package/dist/rest/work.d.mts +467 -20
  161. package/dist/rest/work.mjs +8 -0
  162. package/dist/server/edge-node.d.mts +9 -13
  163. package/dist/server/edge-node.mjs +9 -12
  164. package/dist/server/git-layer.d.mts +1 -6
  165. package/dist/server/git-layer.mjs +2 -2
  166. package/dist/server/git-namespace.d.mts +3 -3
  167. package/dist/server/joined.d.mts +15 -22
  168. package/dist/server/joined.mjs +5 -29
  169. package/dist/server/lifecycle.mjs +107 -23
  170. package/dist/server/registry.mjs +9 -1
  171. package/dist/server/server.mjs +133 -212
  172. package/dist/storage/budget.mjs +2 -2
  173. package/dist/storage/clean-up.mjs +13 -10
  174. package/dist/storage/fetch.mjs +13 -8
  175. package/dist/storage/file-limits.mjs +24 -0
  176. package/dist/storage/index-build.d.mts +1 -0
  177. package/dist/storage/index-build.mjs +106 -62
  178. package/dist/storage/index-fetch.mjs +16 -3
  179. package/dist/storage/index-reach.mjs +33 -5
  180. package/dist/storage/index-remap.mjs +8 -1
  181. package/dist/storage/index-shallow.mjs +5 -3
  182. package/dist/storage/largest-files.mjs +48 -0
  183. package/dist/storage/lfs-pointers.mjs +10 -1
  184. package/dist/storage/loose.mjs +3 -1
  185. package/dist/storage/memory-budget.mjs +28 -4
  186. package/dist/storage/pack-reads.mjs +8 -8
  187. package/dist/storage/pack-receive.mjs +83 -25
  188. package/dist/storage/packs.mjs +37 -21
  189. package/dist/storage/prune-keep.mjs +203 -34
  190. package/dist/storage/prune-walk.mjs +54 -16
  191. package/dist/storage/prune.mjs +629 -196
  192. package/dist/storage/push-uploads.mjs +36 -12
  193. package/dist/storage/quarantine.mjs +9 -9
  194. package/dist/storage/ready.mjs +47 -19
  195. package/dist/storage/receive-bounds.mjs +29 -8
  196. package/dist/storage/receive-state.mjs +8 -0
  197. package/dist/storage/receive-steps.mjs +71 -18
  198. package/dist/storage/repack.d.mts +1 -1
  199. package/dist/storage/repack.mjs +1 -8
  200. package/dist/storage/rows.mjs +12 -1
  201. package/dist/storage/shallow.d.mts +1 -0
  202. package/dist/storage/shallow.mjs +26 -4
  203. package/dist/storage/stream-whole.mjs +5 -2
  204. package/dist/storage/visible.mjs +29 -1
  205. package/dist/storage/walk.mjs +2 -1
  206. package/dist/store/catalogue.mjs +1 -0
  207. package/dist/store/cursor-seal-namespace.d.mts +2 -0
  208. package/dist/store/cursor-seal-public.d.mts +5 -0
  209. package/dist/store/cursor-seal-public.mjs +5 -0
  210. package/dist/store/cursor-seal.d.mts +16 -2
  211. package/dist/store/cursor-seal.mjs +26 -14
  212. package/dist/store/errors.mjs +9 -1
  213. package/dist/store/records.d.mts +2 -0
  214. package/dist/store/records.mjs +9 -6
  215. package/dist/store/repo-record.d.mts +9 -7
  216. package/dist/store/repo-record.mjs +102 -19
  217. package/dist/store/scopes.mjs +10 -2
  218. package/dist/sync/bound.d.mts +2 -0
  219. package/dist/sync/bound.mjs +11 -2
  220. package/dist/sync/github.d.mts +6 -1
  221. package/dist/sync/github.mjs +55 -43
  222. package/dist/sync/http.mjs +36 -1
  223. package/dist/sync/import.mjs +1 -0
  224. package/dist/sync/layer.mjs +6 -2
  225. package/dist/sync/lfs-batch.mjs +119 -0
  226. package/dist/sync/lfs-copy.mjs +251 -0
  227. package/dist/sync/mirror.mjs +210 -137
  228. package/dist/sync/smart-http.mjs +4 -2
  229. package/dist/sync/step.mjs +25 -5
  230. package/dist/sync/tries.mjs +45 -14
  231. package/dist/sync.d.mts +2 -2
  232. package/dist/topics/host.mjs +1 -19
  233. package/dist/topics/layer.mjs +1 -1
  234. package/package.json +3 -8
  235. package/dist/auth/bridge.mjs +0 -30
  236. package/dist/auth/helpers.d.mts +0 -17
  237. package/dist/core/purge.d.mts +0 -8
  238. package/dist/core/registered-rows.d.mts +0 -5
  239. package/dist/core/repack.d.mts +0 -11
  240. package/dist/http/handler.d.mts +0 -2
  241. package/dist/rest/handler.d.mts +0 -21
  242. package/dist/rest.d.mts +0 -2
  243. package/dist/rest.mjs +0 -1
  244. package/dist/server/registry.d.mts +0 -16
  245. package/dist/server/server.d.mts +0 -36
  246. package/dist/storage/repack-job.d.mts +0 -3
  247. package/dist/topics/host.d.mts +0 -7
@@ -5,8 +5,8 @@ import { decodeRow } from "../store/errors.mjs";
5
5
  import { WholeRepo, wholeRef } from "../store/ref-rows.mjs";
6
6
  import { ReachedMark, markHolds } from "../store/records.mjs";
7
7
  import { collections, reachedKey, repoRootsKey, repoScope } from "../store/scopes.mjs";
8
- import { bucketDeleteMany, readWhole } from "./calls.mjs";
9
- import { putOnce } from "./index-build.mjs";
8
+ import { bucketDeleteMany, readWhole, sizedPutCalls } from "./calls.mjs";
9
+ import { putOnce, rootsKeyOf } from "./index-build.mjs";
10
10
  import { isPackId, objectCollections, runKey } from "./layout.mjs";
11
11
  import { upkeepRuns, upkeepTooLarge } from "./memory-budget.mjs";
12
12
  import { decodeRefused, refusedRead } from "./merge-fence.mjs";
@@ -14,9 +14,9 @@ import { decodeMerges, droppedStep, mergeKey, mergeRead, takenOf, writtenOf } fr
14
14
  import { mergeInputsMost } from "./merge-most.mjs";
15
15
  import { opensWithin, resumeMerge, streamMerge } from "./merge-stream.mjs";
16
16
  import { noRows, objectKey, rowsMoved } from "./object-rows.mjs";
17
- import { bitsBytes, hasBit, keepBits, keptAt, setBit } from "./prune-keep.mjs";
18
- import { WalkSaved, pruneWalk, walkKeys } from "./prune-walk.mjs";
19
- import { defaultRepack, leavingRows, loadAll, mergeWith, namedOf, replacing, wholeRows } from "./repack.mjs";
17
+ import { bitsBytes, eachHolders, hasBit, keepBitsOn, keepMergeCalls, setBit } from "./prune-keep.mjs";
18
+ import { WalkSaved, fits, pruneWalk, pruneWalkStartCalls, walkKeys } from "./prune-walk.mjs";
19
+ import { defaultRepack, leavingRows, mergeWith, namedOf, replacing, wholeRows } from "./repack.mjs";
20
20
  import { decodeHeld, pruneFloor, pruneWait } from "./rows.mjs";
21
21
  import { StoredRuns, dropRuns, mergeHeld, mergeRuns } from "./runs.mjs";
22
22
  import { lost } from "./walk.mjs";
@@ -49,7 +49,7 @@ const WalkCarry = /* @__PURE__ */ Schema.Union([Schema.Struct({
49
49
  * go on without reading them again: `'cruft'`, each fresh pack's objects no ref reaches, which its
50
50
  * cruft merge copies, or `'kept'`, each pack's objects its rewrite keeps. `n` names the object;
51
51
  * `packs` are the listed packs out of `refused` its bits were read over, and they hold while the
52
- * listing does; `reads` is the digest of the refs and object rows they were read at, as
52
+ * listing does; `reads` is the digest of the refs, object rows and roots they were read at, as
53
53
  * `readsDigest` takes them, since a push can land rows and let its ref go, the refs as they were.
54
54
  * `bytes` is the object's length, which its read charges: a fork's swap can unlist packs it holds
55
55
  * bits of, so the listing a later step reads doesn't bound it.
@@ -61,6 +61,60 @@ const PassClasses = /* @__PURE__ */ Schema.Struct({
61
61
  reads: Schema.String,
62
62
  bytes: Schema.Int
63
63
  });
64
+ /** A keep's object of bits, by its number and length, which its read charges. */
65
+ const KeptBits = /* @__PURE__ */ Schema.Struct({
66
+ n: Schema.Int,
67
+ bytes: Schema.Int
68
+ });
69
+ const Ranks = /* @__PURE__ */ Schema.Array(Schema.Int.check(Schema.isBetween({
70
+ minimum: 0,
71
+ maximum: 2 ** 32
72
+ })));
73
+ /**
74
+ * Where a keep's merge that alone passed a try stopped, between two IDs, for the next try to go
75
+ * on from (#2412 B2b-2): `merge` names which, the reach's bits `r`, the first holders merge
76
+ * `first`, the needed walk's bits `n` or the last holders merge `last`. Its object, `n` and
77
+ * `bytes` long, holds what the merge had found: the bits so far, the rows, the IDs a walk starts
78
+ * from and each wanted ID's holders. A bits merge's `places` are each of its `runs`' rows, `last`
79
+ * the ID it last visited and `ranks` each pack's next; a holders merge's `places` are each pack's
80
+ * next rank. `named` is the needed walk's runs' digest the `n` merge reads, and `missed` and
81
+ * `fits` the last merge's flags so far.
82
+ */
83
+ const KeepAt = /* @__PURE__ */ Schema.Struct({
84
+ ...KeptBits.fields,
85
+ merge: Schema.Literals([
86
+ "r",
87
+ "first",
88
+ "n",
89
+ "last"
90
+ ]),
91
+ places: Ranks,
92
+ runs: Schema.optional(StoredRuns),
93
+ last: Schema.optional(Schema.String.check(Schema.isPattern(/^[0-9a-f]{40}$/))),
94
+ ranks: Schema.optional(Ranks),
95
+ named: Schema.optional(Schema.String),
96
+ missed: Schema.optional(Schema.Boolean),
97
+ fits: Schema.optional(Schema.Boolean)
98
+ });
99
+ /**
100
+ * What a pass's merges found, kept on its mark when a try's calls end before its next merge
101
+ * (#2412), for the next try to read back in place of each merge. `over` is the digest of what
102
+ * they read, as `keepOver` takes it: the listed packs and their places in the pass, the refs,
103
+ * object rows and roots, a prune merge under way and the `r` walk's runs. `r` names the object of
104
+ * the reach's bits and rows, and `n` the needed walk's, `runs` the digest of the `n` walk's runs
105
+ * they were merged from. `first` says whether the first holders merge found where the needed walk
106
+ * starts, which it found nothing to catch up, copy or class.
107
+ */
108
+ const PassKeep = /* @__PURE__ */ Schema.Struct({
109
+ over: Schema.String,
110
+ r: Schema.optional(KeptBits),
111
+ first: Schema.optional(Schema.Struct({ starts: Schema.Boolean })),
112
+ n: Schema.optional(Schema.Struct({
113
+ ...KeptBits.fields,
114
+ runs: Schema.String
115
+ })),
116
+ at: Schema.optional(KeepAt)
117
+ });
64
118
  /**
65
119
  * A pass's mark, put in a step of its own before the pass first walks: when it began, and when
66
120
  * it waits until, if its walk has waited. A pass is under way while its mark is newer than the
@@ -68,10 +122,12 @@ const PassClasses = /* @__PURE__ */ Schema.Struct({
68
122
  * writes `waits`, the next period: till then no try walks, whatever its attempt. `walks` carries
69
123
  * the pass's two walks across its steps, `r` of what the refs reach and `n` of what the reach
70
124
  * leaves out names: each `saved` where a try stopped, or `done`, its reached runs under the pass's
71
- * scratch pack, till the pass's last step drops them. `classes` names the classes object its
125
+ * scratch pack, till the pass's last step drops them, and `roots`, `rootsKeyOf`'s digest of the
126
+ * roots they walk under, none in a repo with none. `classes` names the classes object its
72
127
  * merges read, and `redos` counts the rewrites it dropped as pushes moved its refs onto what they
73
128
  * were about to drop: the third waits for the next period, as `pruneStep` says, and that period's
74
- * walks start again with none (#1660). `periods` counts the periods in a row the pass has waited
129
+ * walks start again with none (#1660). `keep` holds what its merges found, as `PassKeep` says, its
130
+ * objects under the `r` walk's scratch too. `periods` counts the periods in a row the pass has waited
75
131
  * so, which a wait for any other reason ends, as `ObjectsStep.busy` says (#2233).
76
132
  */
77
133
  const PruneStarted = /* @__PURE__ */ Schema.Struct({
@@ -79,10 +135,12 @@ const PruneStarted = /* @__PURE__ */ Schema.Struct({
79
135
  waits: Schema.optional(Schema.Int),
80
136
  walks: Schema.optional(Schema.Struct({
81
137
  r: Schema.optional(WalkCarry),
82
- n: Schema.optional(WalkCarry)
138
+ n: Schema.optional(WalkCarry),
139
+ roots: Schema.optional(Schema.String)
83
140
  })),
84
141
  classes: Schema.optional(PassClasses),
85
142
  redos: Schema.optional(Schema.Int),
143
+ keep: Schema.optional(PassKeep),
86
144
  periods: Schema.optional(Schema.Int)
87
145
  });
88
146
  const pruneStartedKey = {
@@ -159,7 +217,8 @@ const walkPackOf = (repo, at, kind) => `p_${kind}${repo.slice(2)}${at.toString(3
159
217
  const passScratch = (input, at) => Effect.map(Effect.all([dropRuns(input.bucket, walkPackOf(input.listed.repo, at, "r")), dropRuns(input.bucket, walkPackOf(input.listed.repo, at, "n"))]), (drops) => drops.includes("more") ? "more" : "done");
160
218
  /**
161
219
  * The bucket keys `started` names under its pass's scratch packs: each walk's runs, saved or
162
- * done, and its classes object. Bounded by the mark's record, so a drop of them takes one call.
220
+ * done, its classes object and its keep's objects. Bounded by the mark's record, so a drop of them
221
+ * takes one call.
163
222
  */
164
223
  const markKeys = (started, repo, at) => {
165
224
  const keys = /* @__PURE__ */ new Set();
@@ -171,48 +230,58 @@ const markKeys = (started, repo, at) => {
171
230
  else for (const key of walkKeys(carry.saved)) keys.add(key);
172
231
  }
173
232
  if (started?.classes !== void 0) keys.add(classesKey(repo, at, started.classes.n));
233
+ for (const key of keptKeys(repo, at, started?.keep)) keys.add(key);
174
234
  return keys;
175
235
  };
176
236
  /**
177
- * A pass's reach, as a set: an ID is in it when the bits' rows hold it, or its first pack among
178
- * `packs` keeps it at its rank, as D3's `keptAt` says. Its IDs run each pack's set bits, then the
179
- * rows, so one held twice is there twice. `whole` is whether the keep has bits for every pack.
237
+ * A pass's reach, by an ID and the packs among `packs` that hold it, as a holders merge of `packs`
238
+ * finds them: an ID is in it when the bits' rows hold it, or its first holder keeps it at its
239
+ * rank, as D3's `keptAt` says, a pack the bits have none for keeping it whole.
180
240
  */
181
- const bitsOf = (bits, packs) => {
182
- const byPack = new Map(bits.packs.map((each) => [each.pack, each.bits]));
183
- return {
184
- whole: packs.every((at) => byPack.has(at.pack.id)),
185
- has: (oid) => {
186
- if (bits.rows.has(oid)) return true;
187
- for (const at of packs) {
188
- const rank = at.index.rankOf(oid);
189
- if (rank !== void 0) return keptAt(bits, at.pack.id, rank);
190
- }
191
- return false;
192
- },
193
- *[Symbol.iterator]() {
194
- for (const at of packs) {
195
- const set = byPack.get(at.pack.id);
196
- if (set === void 0) continue;
197
- for (let rank = 0; rank < at.index.count; rank += 1) if (hasBit(set, rank)) yield at.index.oidAt(rank);
198
- }
199
- yield* bits.rows;
200
- }
241
+ const reachOf = (bits, packs) => {
242
+ const byPack = packs.map((pack) => bits.packs.find((each) => each.pack === pack.id)?.bits);
243
+ return (oid, holders) => {
244
+ if (bits.rows.has(oid)) return true;
245
+ const first = holders[0];
246
+ if (first === void 0) return false;
247
+ const set = byPack[first.pack];
248
+ return set === void 0 || hasBit(set, first.rank);
201
249
  };
202
250
  };
203
251
  /**
252
+ * One holders merge of a keep's `packs`, as `eachHolders` charges and reads it: `visit` each ID
253
+ * they hold, by its hex. Answers the holders of each of `wanted` the packs hold, which no pack
254
+ * holding is left out of, and with `going`, where it stopped, as `mergeHolders` says.
255
+ */
256
+ const heldPass = /* @__PURE__ */ Effect.fnUntraced(function* (input, packs, wanted, visit = () => void 0, going = {}) {
257
+ const found = new Map(going.from?.found);
258
+ return {
259
+ found,
260
+ stopped: (yield* eachHolders(input, packs, (id, holders) => {
261
+ const oid = hexOf(id);
262
+ if (wanted.has(oid)) found.set(oid, [...holders]);
263
+ visit(oid, holders);
264
+ }, {
265
+ from: going.from?.places,
266
+ goesOn: going.goesOn
267
+ }))?.places
268
+ };
269
+ });
270
+ /**
204
271
  * A pass's walk under `scratch`, its runs outliving the step: from where `saved` stopped, or from
205
272
  * `start` with `known` reached already, as D2's walk carries it. A new walk, with neither, drops
206
273
  * what a pass that died left under `scratch` first. With `more`, where it stopped, for the mark.
274
+ * A walk `spent`, after a merge the try ran, stops with `more` where a fresh try's wouldn't.
207
275
  */
208
- const walkOn = /* @__PURE__ */ Effect.fnUntraced(function* (input, scratch, start, rows, missing, calls, roots, saved, known) {
276
+ const walkOn = /* @__PURE__ */ Effect.fnUntraced(function* (input, scratch, start, rows, missing, calls, roots, { saved, known, spent } = {}) {
209
277
  if (saved === void 0 && known === void 0) yield* dropRuns(input.bucket, scratch);
210
278
  const walked = yield* pruneWalk(input, scratch, start, missing, {
211
279
  rows: rows.length,
212
280
  calls,
213
281
  saved,
214
282
  known,
215
- roots
283
+ roots,
284
+ spent
216
285
  });
217
286
  return "more" in walked ? { more: walked.more } : {
218
287
  runs: walked.reached,
@@ -253,19 +322,6 @@ const refsOf = /* @__PURE__ */ Effect.fnUntraced(function* (input) {
253
322
  roots: yield* rootsOf(snapshot.results[1])
254
323
  };
255
324
  });
256
- /** Every ref's object, once each, with no roots: what a listing checks its bits were read at. */
257
- const refIdsOf = /* @__PURE__ */ Effect.fnUntraced(function* (input) {
258
- const snapshot = yield* input.store.read({
259
- scope: repoScope(input.listed.repo),
260
- reads: [{ collection: collections.refs }]
261
- });
262
- return yield* refIdsIn(snapshot.results[0]);
263
- });
264
- /** The objects of `packs` and `rows` that `reached` leaves out: where the needed walk starts. */
265
- const unreachedOf = (packs, rows, reached) => [...new Set([...oidsOf(packs), ...rows.map((row) => row.oid)].filter((oid) => !reached.has(oid)))];
266
- /** Every object `packs` hold. */
267
- const oidsOf = (packs) => packs.flatMap(({ index }) => Array.from({ length: index.count }, (_, rank) => index.oidAt(rank)));
268
- const holds = (packs) => (oid) => packs.some((indexed) => indexed.index.find(oid) !== void 0);
269
325
  /** The packs a geometric step leaves alone: each cruft pack, which only the prune pass merges. */
270
326
  const mergesAlone = (read) => (pack) => cruftOf(read, pack) !== void 0;
271
327
  /**
@@ -287,26 +343,28 @@ const passPacks = (input, prune, read, drops) => {
287
343
  };
288
344
  };
289
345
  /**
290
- * Whether a pass whose steps keep what `keep` says, over the packs out of young cruft and the
291
- * `rows`, leaves both halves of the repo's mark, as `ReachedMark` says: each object they keep is
292
- * one a ref reaches, and each a ref reaches is in them, none only in a young cruft pack, as one is
293
- * that a ref reached again after its cruft step, until the pass that finds its pack old (#647).
294
- * Exported for its test alone.
346
+ * Whether a pass whose steps keep what `keep` says leaves both halves of the repo's mark over its
347
+ * `rows`, as `ReachedMark` says: each row it keeps is one a ref reaches, and each the reach holds
348
+ * as a row, `reachedRows`, it keeps, in a row or a pack out of cruft, `inPacks`. A holders pass
349
+ * checks the same of the packs (#647). Exported for its test alone.
295
350
  */
296
- const reachesOnly = (packs, rows, reached, keep) => {
297
- const fits = (oid) => reached.has(oid) || !keep(oid);
298
- for (const { index } of packs) for (let rank = 0; rank < index.count; rank += 1) if (!fits(index.oidAt(rank))) return false;
299
- if (!rows.every(({ oid }) => fits(oid))) return false;
300
- const inPacks = holds(packs);
351
+ const reachesOnly = (rows, reached, reachedRows, keep, inPacks) => {
352
+ if (!rows.every(({ oid }) => reached(oid) || !keep(oid))) return false;
301
353
  const rowOids = new Set(rows.map(({ oid }) => oid));
302
- for (const oid of reached) if (!keep(oid) || !rowOids.has(oid) && !inPacks(oid)) return false;
354
+ for (const oid of reachedRows) if (!keep(oid) || !rowOids.has(oid) && !inPacks(oid)) return false;
303
355
  return true;
304
356
  };
357
+ /** SHA-256 over `lists`, each sorted, a line each of its own, with a blank line between. */
358
+ const listsDigest = (lists) => Effect.map(Effect.promise(() => crypto.subtle.digest("SHA-256", new TextEncoder().encode(lists.map((ids) => ids.toSorted().join("\n")).join("\n\n")))), (digest) => hexOf(new Uint8Array(digest)));
305
359
  /**
306
- * The digest of the repo's refs and object rows, as `PassClasses` keeps it: SHA-256 over their
307
- * sorted IDs, the refs', a blank line, then the rows'.
360
+ * The digest of the repo's refs, object rows and roots, as `PassClasses` keeps it: SHA-256 over
361
+ * their sorted IDs, the refs', a blank line, the rows', a blank line, then the roots'.
308
362
  */
309
- const readsDigest = (refs, rows) => Effect.map(Effect.promise(() => crypto.subtle.digest("SHA-256", new TextEncoder().encode(`${refs.toSorted().join("\n")}\n\n${rows.toSorted().join("\n")}`))), (digest) => hexOf(new Uint8Array(digest)));
363
+ const readsDigest = (refs, rows, roots) => listsDigest([
364
+ refs,
365
+ rows,
366
+ [...roots]
367
+ ]);
310
368
  /** The listed packs a pass's bits are read over, by ID: those out of `refused`. */
311
369
  const classedIds = (input, read) => input.listed.packs.filter((pack) => !read.refused.has(pack.id)).map((pack) => pack.id);
312
370
  /** Whether `a` and `b` name the same packs. */
@@ -316,25 +374,46 @@ const sameIds = (a, b) => {
316
374
  };
317
375
  /** The key of a pass's classes object `n`, under its `r` walk's scratch, which its last step drops. */
318
376
  const classesKey = (repo, at, n) => runKey(walkPackOf(repo, at, "r"), "classes", n);
319
- const ClassesHeader = /* @__PURE__ */ Schema.fromJsonString(Schema.Struct({ packs: Schema.Array(Schema.Struct({
320
- pack: Schema.String.pipe(Schema.refine(isPackId)),
321
- count: Schema.Int.check(Schema.isBetween({
322
- minimum: 0,
323
- maximum: 2 ** 32
324
- }))
325
- })) }));
377
+ const Hex = /* @__PURE__ */ Schema.String.check(Schema.isPattern(/^[0-9a-f]{40}$/));
378
+ const ClassesHeader = /* @__PURE__ */ Schema.fromJsonString(Schema.Struct({
379
+ packs: Schema.Array(Schema.Struct({
380
+ pack: Schema.String.pipe(Schema.refine(isPackId)),
381
+ count: Schema.Int.check(Schema.isBetween({
382
+ minimum: 0,
383
+ maximum: 2 ** 32
384
+ }))
385
+ })),
386
+ rows: Schema.optional(Schema.Array(Hex)),
387
+ ids: Schema.optional(Schema.Array(Hex)),
388
+ held: Schema.optional(Schema.Array(Schema.Struct({
389
+ oid: Hex,
390
+ holders: Ranks
391
+ })))
392
+ }));
326
393
  const decodeClassesHeader = /* @__PURE__ */ Schema.decodeUnknownOption(ClassesHeader);
327
394
  const encodeClassesHeader = /* @__PURE__ */ Schema.encodeEffect(ClassesHeader);
328
395
  /**
329
396
  * A pass's classes object: a 4-byte header length, the header, each pack and its object count as
330
- * JSON, then each pack's bits in its order. A try that died after its put leaves the key, and a
331
- * put never replaces one, so the put deletes it first. It returns the object's length.
397
+ * JSON, with a keep's `rows`, `ids` and `held`, then each pack's bits in its order. A try that
398
+ * died after its put leaves the key, and a put never replaces one, so the put deletes it first.
399
+ * The header is charged to the step's allotment while it's put, as the bits are, so a stopped
400
+ * merge's lists past it refuse the step too large, before the put. It returns the object's length.
332
401
  */
333
- const putClasses = /* @__PURE__ */ Effect.fnUntraced(function* (input, key, packs) {
334
- const text = yield* Effect.orDie(encodeClassesHeader({ packs: packs.map(({ pack, count }) => ({
402
+ const putClasses = /* @__PURE__ */ Effect.fnUntraced(function* (input, key, packs, lists = {}) {
403
+ const listed = packs.map(({ pack, count }) => ({
335
404
  pack,
336
405
  count
337
- })) }));
406
+ }));
407
+ const held = lists.held === void 0 ? void 0 : [...lists.held].map(([oid, holders]) => ({
408
+ oid,
409
+ holders: holders.flatMap(({ pack, rank }) => [pack, rank])
410
+ }));
411
+ const text = yield* Effect.orDie(encodeClassesHeader({
412
+ packs: listed,
413
+ rows: lists.rows,
414
+ ids: lists.ids,
415
+ held
416
+ }));
338
417
  const header = new TextEncoder().encode(text);
339
418
  const length = /* @__PURE__ */ new Uint8Array(4);
340
419
  new DataView(length.buffer).setUint32(0, header.length);
@@ -343,16 +422,21 @@ const putClasses = /* @__PURE__ */ Effect.fnUntraced(function* (input, key, pack
343
422
  header,
344
423
  ...packs.map((each) => each.bits)
345
424
  ];
346
- yield* bucketDeleteMany(input.bucket, [key]);
347
- yield* putOnce(input.bucket, key, parts);
348
- return parts.reduce((sum, part) => sum + part.length, 0);
425
+ return yield* Effect.scoped(Effect.gen(function* () {
426
+ yield* Effect.acquireRelease(input.allotment.grow(header.length, upkeepTooLarge), () => Effect.sync(() => input.allotment.shrink(header.length)));
427
+ yield* bucketDeleteMany(input.bucket, [key]);
428
+ yield* putOnce(input.bucket, key, parts);
429
+ return parts.reduce((sum, part) => sum + part.length, 0);
430
+ }));
349
431
  });
350
432
  /**
351
- * The bits of the pass's classes object at `key`, its `bytes` charged to the step's allotment
352
- * while its scope is open: `undefined` when it's gone, so the pass reads its bits again, and
353
- * `DATA_LOSS` when it's broken or isn't `bytes` long.
433
+ * The bits and rows of the pass's classes or keep object at `key`, its `bytes` charged to the
434
+ * step's allotment while its scope is open: `undefined` when it's gone, so the pass reads its bits
435
+ * again, and `DATA_LOSS` when it's broken or isn't `bytes` long.
354
436
  */
355
- const readClasses = /* @__PURE__ */ Effect.fnUntraced(function* (input, key, most) {
437
+ const readBits = (input, key, most) => Effect.map(readKept(input, key, most), (kept) => kept?.bits);
438
+ /** The object at `key` as `readBits` reads it, with a stopped merge's `ids` and `held` too. */
439
+ const readKept = /* @__PURE__ */ Effect.fnUntraced(function* (input, key, most) {
356
440
  yield* Effect.acquireRelease(input.allotment.grow(most, upkeepTooLarge), () => Effect.sync(() => input.allotment.shrink(most)));
357
441
  const bytes = yield* readWhole(input.bucket, key);
358
442
  if (bytes === void 0) return void 0;
@@ -373,31 +457,137 @@ const readClasses = /* @__PURE__ */ Effect.fnUntraced(function* (input, key, mos
373
457
  at = end;
374
458
  }
375
459
  if (at !== bytes.length) return yield* broken;
376
- return packs;
460
+ const held = /* @__PURE__ */ new Map();
461
+ for (const { oid, holders } of header.value.held ?? []) {
462
+ if (holders.length % 2 !== 0) return yield* broken;
463
+ const each = [];
464
+ for (let place = 0; place < holders.length; place += 2) each.push({
465
+ pack: holders[place] ?? 0,
466
+ rank: holders[place + 1] ?? 0
467
+ });
468
+ held.set(oid, each);
469
+ }
470
+ return {
471
+ bits: {
472
+ packs,
473
+ rows: new Set(header.value.rows)
474
+ },
475
+ ids: header.value.ids ?? [],
476
+ held
477
+ };
377
478
  });
479
+ /** The bits of the pass's classes object at `key`, as `readBits` reads them. */
480
+ const readClasses = (input, key, most) => Effect.map(readBits(input, key, most), (read) => read?.packs);
378
481
  /**
379
- * Each of `packs`' bits, set at each rank `has` says, charged to the step's allotment while its
380
- * scope is open.
482
+ * The key of a pass's keep object `n`, under its `r` walk's scratch: of the `kind` walk's bits, or
483
+ * `at`, what a stopped merge had found.
381
484
  */
382
- const classBits = /* @__PURE__ */ Effect.fnUntraced(function* (input, packs, has) {
383
- const bytes = packs.reduce((sum, { index }) => sum + bitsBytes(index.count), 0);
384
- yield* Effect.acquireRelease(input.allotment.grow(bytes, upkeepTooLarge), () => Effect.sync(() => input.allotment.shrink(bytes)));
385
- return packs.map((indexed) => {
386
- const bits = new Uint8Array(bitsBytes(indexed.index.count));
387
- let set = 0;
388
- for (let rank = 0; rank < indexed.index.count; rank += 1) {
389
- if (!has(indexed, rank)) continue;
390
- setBit(bits, rank);
391
- set += 1;
392
- }
485
+ const keepKey = (repo, at, kind, n) => runKey(walkPackOf(repo, at, "r"), `keep-${kind}`, n);
486
+ /** The keys of `keep`'s objects. */
487
+ const keptKeys = (repo, at, keep) => [
488
+ ...keep?.r === void 0 ? [] : [keepKey(repo, at, "r", keep.r.n)],
489
+ ...keep?.n === void 0 ? [] : [keepKey(repo, at, "n", keep.n.n)],
490
+ ...keep?.at === void 0 ? [] : [keepKey(repo, at, "at", keep.at.n)]
491
+ ];
492
+ /**
493
+ * The calls a keep object's put makes, its delete first, a forecast: of bits for packs of
494
+ * `counts` objects and `rows` row IDs, its header at 64 bytes a pack and 43 a row at the most,
495
+ * and `listed` more bytes of a stopped merge's lists.
496
+ */
497
+ const keptPutCalls = (input, counts, rows, listed = 0) => 1 + sizedPutCalls(input.bucket.multipart, 68 + counts.length * 64 + rows * 43 + listed + counts.reduce((sum, count) => sum + bitsBytes(count), 0));
498
+ /** The most header bytes a stopped holders merge's `held` takes: an ID's and each pack's ranks. */
499
+ const heldBytes = (wanted, packs) => wanted * (52 + packs * 24);
500
+ /** How many of `bits`' bits are set. */
501
+ const setCount = (bits) => {
502
+ let count = 0;
503
+ for (const byte of bits) for (let rest = byte; rest !== 0; rest &= rest - 1) count += 1;
504
+ return count;
505
+ };
506
+ /** `runs` as the mark stores them: a narrowed merge's runs are put, never held. */
507
+ const storedRuns = ({ kind, width, keys, lengths, rows }) => ({
508
+ kind,
509
+ width,
510
+ keys,
511
+ lengths,
512
+ rows
513
+ });
514
+ /**
515
+ * The digest a pass's keep holds over, as `PassKeep.over` says: the listed packs, the young, old
516
+ * and fenced among them, the reads' `digest`, the prune merge under way and the `r` walk's runs.
517
+ */
518
+ const keepOver = (input, packs, digest, merge, reached) => {
519
+ const placed = (place, list) => list.map((pack) => place + pack.id);
520
+ return listsDigest([
521
+ input.listed.packs.map((pack) => pack.id),
522
+ [...placed("young ", packs.young), ...placed("old ", packs.old)],
523
+ placed("fenced ", packs.fenced),
524
+ [digest],
525
+ [merge ?? ""],
526
+ reached.keys
527
+ ]);
528
+ };
529
+ /**
530
+ * The parts of the mark's `keep` that hold `over` and, for `n`, the needed walk's runs `named`,
531
+ * the objects of those that don't deleted first: `changed` when any part is let go.
532
+ */
533
+ const keptOf = /* @__PURE__ */ Effect.fnUntraced(function* (input, at, keep, over, named) {
534
+ if (keep === void 0) return {
535
+ keep: void 0,
536
+ changed: false
537
+ };
538
+ if (keep.over !== over) {
539
+ yield* bucketDeleteMany(input.bucket, keptKeys(input.listed.repo, at, keep));
393
540
  return {
394
- pack: indexed.pack.id,
395
- count: indexed.index.count,
396
- bits,
397
- set
541
+ keep: void 0,
542
+ changed: true
398
543
  };
399
- });
544
+ }
545
+ const staleN = keep.n !== void 0 && keep.n.runs !== named;
546
+ const staleAt = keep.at?.merge === "n" && keep.at.named !== named;
547
+ if (!staleN && !staleAt) return {
548
+ keep,
549
+ changed: false
550
+ };
551
+ const holding = { ...keep };
552
+ const keys = [];
553
+ if (staleN && keep.n !== void 0) {
554
+ keys.push(keepKey(input.listed.repo, at, "n", keep.n.n));
555
+ delete holding.n;
556
+ }
557
+ if (staleAt && keep.at !== void 0) {
558
+ keys.push(keepKey(input.listed.repo, at, "at", keep.at.n));
559
+ delete holding.at;
560
+ }
561
+ yield* bucketDeleteMany(input.bucket, keys);
562
+ return {
563
+ keep: holding,
564
+ changed: true
565
+ };
566
+ });
567
+ /**
568
+ * Bits for each of `packs` at `chosen`, by its place, none set, charged to the step's allotment
569
+ * while its scope is open: a holders pass sets them with `setClass`.
570
+ */
571
+ const classBitsOf = /* @__PURE__ */ Effect.fnUntraced(function* (input, packs, chosen) {
572
+ const bytes = chosen.reduce((sum, at) => sum + bitsBytes(packs[at]?.objects ?? 0), 0);
573
+ yield* Effect.acquireRelease(input.allotment.grow(bytes, upkeepTooLarge), () => Effect.sync(() => input.allotment.shrink(bytes)));
574
+ return new Map(chosen.map((at) => {
575
+ const count = packs[at]?.objects ?? 0;
576
+ return [at, {
577
+ pack: packs[at]?.id,
578
+ count,
579
+ bits: new Uint8Array(bitsBytes(count)),
580
+ set: 0
581
+ }];
582
+ }));
400
583
  });
584
+ /** Sets `classes`' bit at `holder`'s rank in its pack, when it has bits for that pack. */
585
+ const setClass = (classes, { pack, rank }) => {
586
+ const each = classes.get(pack);
587
+ if (each === void 0 || hasBit(each.bits, rank)) return;
588
+ setBit(each.bits, rank);
589
+ each.set += 1;
590
+ };
401
591
  /**
402
592
  * The cruft merge's listing: its packs listed, each a cruft pack with a `cruft` row of now. It
403
593
  * only adds, so a push beside it loses nothing, and the next step finds what it copied in young
@@ -424,11 +614,12 @@ const cruftListing = (landed) => Effect.fnUntraced(function* (input, merge) {
424
614
  /**
425
615
  * The rewrite's listing: its packs listed in place of its inputs, each an input a `leaving` row,
426
616
  * and each old cruft input's `cruft` row ended, so the step `drops`. None while the repo's refs,
427
- * its object rows, `rows`, or its listed packs aren't the ones its bits were read at, `seen`: the
617
+ * its object rows, `rows`, its roots or its listed packs aren't the ones its bits were read at, `seen`: the
428
618
  * merge waits at its commit pass, and the pass catches up first.
429
619
  */
430
620
  const keptListing = (read, seen, rows, landed) => Effect.fnUntraced(function* (input, merge) {
431
- if ((yield* readsDigest(yield* refIdsOf(input), rows)) !== seen.reads || !sameIds(seen.packs, classedIds(input, read))) return void 0;
621
+ const { refs, roots } = yield* refsOf(input);
622
+ if ((yield* readsDigest(refs, rows, roots)) !== seen.reads || !sameIds(seen.packs, classedIds(input, read))) return void 0;
432
623
  const removed = yield* takenOf(input, merge);
433
624
  const { packs, quarantines, first } = yield* writtenOf(merge);
434
625
  const dropped = removed.filter((pack) => read.cruft.has(pack.id));
@@ -587,9 +778,11 @@ const remarked = (started, patch) => {
587
778
  const walks = "walks" in patch ? patch.walks : started.walks;
588
779
  const classes = "classes" in patch ? patch.classes : started.classes;
589
780
  const redos = "redos" in patch ? patch.redos : started.redos;
781
+ const keep = "keep" in patch ? patch.keep : started.keep;
590
782
  if (walks !== void 0) value.walks = walks;
591
783
  if (classes !== void 0) value.classes = classes;
592
784
  if (redos !== void 0) value.redos = redos;
785
+ if (keep !== void 0) value.keep = keep;
593
786
  return value;
594
787
  };
595
788
  /**
@@ -606,35 +799,54 @@ const remarked = (started, patch) => {
606
799
  * holds it, and what it names stays, so the pass proves no mark while it holds what no ref
607
800
  * reaches. A rewrite whose refs, object rows or listed packs moved before its listing catches up,
608
801
  * and is dropped for its bits to be read again when it was about to drop what they reach now.
802
+ * With `calls`, a merge past what the try has left, after one it ran, ends the step with what
803
+ * they found on the mark's `keep`, which `keeping` hands `pruneStep` when the step lets it go.
609
804
  */
610
- const passSteps = /* @__PURE__ */ Effect.fnUntraced(function* (input, read, packs, objectRows, most, calls) {
805
+ const passSteps = /* @__PURE__ */ Effect.fnUntraced(function* (input, read, packs, objectRows, most, calls, keeping) {
611
806
  const rows = yield* wholeRows(objectRows);
612
807
  const rowOids = rows.map((row) => row.oid);
613
808
  const at = read.started?.at ?? input.at;
614
- const carried = read.started?.walks ?? {};
809
+ const { refs, roots } = yield* refsOf(input);
810
+ const rootsKey = rootsKeyOf(roots);
811
+ const carried = read.started?.walks?.roots === rootsKey ? read.started?.walks ?? {} : {};
615
812
  const classes = read.started?.classes;
616
813
  const redos = read.started?.redos ?? 0;
617
814
  const scratch = walkPackOf(input.listed.repo, at, "r");
618
- const saving = (walks) => ({
619
- step: {
620
- change: { rows: noRows },
621
- more: true
622
- },
623
- mark: { walks }
624
- });
625
- const { refs, roots } = yield* refsOf(input);
626
- const digest = yield* readsDigest(refs, rowOids);
815
+ const saving = (walks, keep) => {
816
+ const value = rootsKey === void 0 ? walks : {
817
+ ...walks,
818
+ roots: rootsKey
819
+ };
820
+ return {
821
+ step: {
822
+ change: { rows: noRows },
823
+ more: true
824
+ },
825
+ mark: keep === void 0 ? { walks: value } : {
826
+ walks: value,
827
+ keep
828
+ }
829
+ };
830
+ };
831
+ const digest = yield* readsDigest(refs, rowOids, roots);
627
832
  const ids = classedIds(input, read);
628
833
  const carry = carried.r;
629
834
  if (carry === void 0 || "saved" in carry) {
630
- const walked = yield* walkOn(input, scratch, refs, rows, lost, calls, roots, carry?.saved);
631
- return saving({ r: "more" in walked ? {
835
+ const walked = yield* walkOn(input, scratch, refs, rows, lost, calls, roots, { saved: carry?.saved });
836
+ const walkedOn = saving({ r: "more" in walked ? {
632
837
  saved: walked.more,
633
838
  refs: carry?.refs
634
839
  } : {
635
840
  done: walked.runs,
636
841
  refs: refsJoined(carry?.refs, walked.refs)
637
842
  } });
843
+ return carry === void 0 && read.started?.keep !== void 0 ? {
844
+ ...walkedOn,
845
+ mark: {
846
+ ...walkedOn.mark,
847
+ keep: void 0
848
+ }
849
+ } : walkedOn;
638
850
  }
639
851
  const merge = read.merges.prune;
640
852
  const landed = { done: false };
@@ -664,53 +876,184 @@ const passSteps = /* @__PURE__ */ Effect.fnUntraced(function* (input, read, pack
664
876
  const chosen = merge === void 0 && classes !== void 0 && (classes.kind === "cruft" ? sameIds(classes.packs, ids) : seenHolds(classes)) ? yield* readClasses(input, classesKey(input.listed.repo, at, classes.n), classes.bytes) : void 0;
665
877
  if (chosen !== void 0 && classes !== void 0 && !chosen.some((each) => youngIds.has(each.pack))) return went(yield* classMerge(input, read, chosen, most, calls, listingOf(classes)), classes.kind);
666
878
  const reachedRuns = carry.done;
667
- const bits = yield* keepBits(input, scratch, reachedRuns, rowOids, lost);
668
879
  const oldIds = new Set(packs.old.map((pack) => pack.id));
669
880
  const fencedIds = new Set(packs.fenced.map((pack) => pack.id));
670
- const sides = Effect.gen(function* () {
671
- const merging = yield* loadAll(input, packs.merging);
672
- const young = yield* loadAll(input, packs.young);
881
+ const sides = [...packs.merging, ...packs.young];
882
+ const isYoung = sides.map((pack) => youngIds.has(pack.id));
883
+ const isFenced = sides.map((pack) => fencedIds.has(pack.id));
884
+ const isOld = sides.map((pack) => oldIds.has(pack.id));
885
+ const isFresh = sides.map((_, at) => !isYoung[at] && !isOld[at] && !isFenced[at]);
886
+ const neededCarry = carried.n;
887
+ const neededDone = neededCarry !== void 0 && "done" in neededCarry ? neededCarry.done : void 0;
888
+ const neededKeys = neededDone === void 0 ? void 0 : yield* listsDigest([neededDone.keys]);
889
+ const keptOver = yield* keepOver(input, packs, digest, merge?.pack, reachedRuns);
890
+ const prior = yield* keptOf(input, at, read.started?.keep, keptOver, neededKeys);
891
+ const keepNow = { over: keptOver };
892
+ keeping.changed = prior.changed;
893
+ keeping.keep = keepNow;
894
+ const unsaved = [];
895
+ const started = read.started?.keep;
896
+ let numbered = Math.max(started?.r?.n ?? -1, started?.n?.n ?? -1, started?.at?.n ?? -1) + 1;
897
+ let ran = false;
898
+ const saveCalls = () => unsaved.reduce((sum, { bits }) => sum + keptPutCalls(input, bits.packs.map((each) => each.count), bits.rows.size), 0);
899
+ const goesOn = (needs) => !ran || fits(calls, needs + saveCalls());
900
+ const saved = Effect.fnUntraced(function* (kinds) {
901
+ for (const { kind, bits: each } of unsaved) {
902
+ if (!kinds.includes(kind)) continue;
903
+ const n = numbered;
904
+ numbered += 1;
905
+ const key = keepKey(input.listed.repo, at, kind, n);
906
+ const bytes = yield* putClasses(input, key, each.packs, { rows: [...each.rows] });
907
+ if (kind === "r") keepNow.r = {
908
+ n,
909
+ bytes
910
+ };
911
+ else keepNow.n = {
912
+ n,
913
+ bytes,
914
+ runs: neededKeys ?? ""
915
+ };
916
+ }
917
+ unsaved.length = 0;
918
+ return { ...keepNow };
919
+ });
920
+ const stopped = Effect.map(saved(["r", "n"]), (keep) => ({
921
+ step: {
922
+ change: { rows: noRows },
923
+ more: true
924
+ },
925
+ mark: { keep }
926
+ }));
927
+ const partial = prior.keep?.at;
928
+ keeping.changed ||= partial !== void 0;
929
+ const partialOf = (kind) => partial?.merge === kind ? partial : void 0;
930
+ const readPartial = (kept) => readKept(input, keepKey(input.listed.repo, at, "at", kept.n), kept.bytes);
931
+ const goesOnIn = (reserve) => calls === void 0 ? void 0 : (reads) => fits(calls, reads + saveCalls() + reserve() + 1);
932
+ const stoppedAt = Effect.fnUntraced(function* (place, found, lists) {
933
+ const n = numbered;
934
+ numbered += 1;
935
+ const bytes = yield* putClasses(input, keepKey(input.listed.repo, at, "at", n), found, lists);
936
+ keepNow.at = {
937
+ ...place,
938
+ n,
939
+ bytes
940
+ };
941
+ const step = yield* stopped;
942
+ if (partial !== void 0) yield* bucketDeleteMany(input.bucket, [keepKey(input.listed.repo, at, "at", partial.n)]);
943
+ return step;
944
+ });
945
+ const listedCounts = input.listed.packs.map((pack) => pack.objects);
946
+ const bitsCalls = () => keptPutCalls(input, listedCounts, rowOids.length);
947
+ const bitsFrom = Effect.fnUntraced(function* (kept) {
948
+ if (kept?.runs === void 0 || kept.last === void 0 || kept.ranks === void 0) return void 0;
949
+ const read = yield* readPartial(kept);
950
+ if (read === void 0) return void 0;
673
951
  return {
674
- merging,
675
- young,
676
- fenced: merging.filter(({ pack }) => fencedIds.has(pack.id)),
677
- fresh: merging.filter(({ pack }) => !oldIds.has(pack.id) && !fencedIds.has(pack.id)),
678
- reached: bitsOf(bits, [...merging, ...young])
952
+ place: {
953
+ runs: kept.runs,
954
+ places: kept.places,
955
+ last: kept.last,
956
+ ranks: kept.ranks
957
+ },
958
+ bits: read.bits
679
959
  };
680
960
  });
681
- const caught = yield* Effect.scoped(Effect.map(sides, ({ reached }) => refs.filter((oid) => !reached.has(oid))));
682
- if (caught.length > 0) {
683
- const walked = yield* walkOn(input, scratch, caught, rows, lost, calls, roots, void 0, reachedRuns);
684
- return saving({
685
- ...carried,
686
- r: "more" in walked ? {
687
- saved: walked.more,
688
- refs: carry.refs
689
- } : {
690
- done: walked.runs,
691
- refs: refsJoined(carry.refs, walked.refs)
692
- }
961
+ const stoppedBits = (kind, place, found) => stoppedAt({
962
+ merge: kind,
963
+ places: place.places,
964
+ runs: storedRuns(place.runs),
965
+ last: place.last,
966
+ ranks: place.ranks,
967
+ named: kind === "n" ? neededKeys ?? "" : void 0
968
+ }, found.packs, { rows: [...found.rows] });
969
+ const keptR = prior.keep?.r;
970
+ const savedR = keptR === void 0 ? void 0 : yield* readBits(input, keepKey(input.listed.repo, at, "r", keptR.n), keptR.bytes);
971
+ if (savedR !== void 0 && keptR !== void 0) keepNow.r = keptR;
972
+ keeping.changed ||= keptR !== void 0 && savedR === void 0;
973
+ const mergedR = savedR !== void 0 ? { bits: savedR } : yield* keepBitsOn(input, scratch, reachedRuns, rowOids, lost, {
974
+ from: yield* bitsFrom(partialOf("r")),
975
+ goesOn: goesOnIn(bitsCalls)
976
+ });
977
+ if ("stopped" in mergedR) return yield* stoppedBits("r", mergedR.stopped, mergedR.bits);
978
+ const { bits } = mergedR;
979
+ if (savedR === void 0) {
980
+ ran = true;
981
+ unsaved.push({
982
+ kind: "r",
983
+ bits
693
984
  });
694
985
  }
695
- const reachedBits = new Map(bits.packs.map((each) => [each.pack, each.bits]));
696
- const reachedAt = ({ pack, index }, rank) => {
697
- const set = reachedBits.get(pack.id);
698
- return set === void 0 || hasBit(set, rank) || bits.rows.has(index.oidAt(rank));
986
+ const pass = (wanted, visit, going) => heldPass(input, sides, wanted, visit, going);
987
+ const holdersFrom = Effect.fnUntraced(function* (kept, classes, ids) {
988
+ if (kept === void 0) return void 0;
989
+ return yield* Effect.scoped(Effect.gen(function* () {
990
+ const read = yield* readPartial(kept);
991
+ if (read === void 0) return void 0;
992
+ for (const each of read.bits.packs) {
993
+ const into = classes?.get(sides.findIndex((pack) => pack.id === each.pack));
994
+ if (into?.bits.length !== each.bits.length) return yield* fail("DATA_LOSS", `The prune pass's keep ${kept.n} names other classes`);
995
+ into.bits.set(each.bits);
996
+ into.set = setCount(each.bits);
997
+ }
998
+ ids.push(...read.ids);
999
+ return {
1000
+ places: kept.places,
1001
+ found: read.held
1002
+ };
1003
+ }));
1004
+ });
1005
+ const classPacks = (classes) => [...classes?.values() ?? []];
1006
+ const reached = reachOf(bits, sides);
1007
+ const apart = (holders) => holders.some(({ pack }) => isYoung[pack] === true || isFenced[pack] === true);
1008
+ const inFresh = (holders) => holders.some(({ pack }) => isFresh[pack] === true);
1009
+ const rowSet = new Set(rowOids);
1010
+ const reachedBits = sides.map((pack) => bits.packs.find((each) => each.pack === pack.id)?.bits);
1011
+ const ownBit = ({ pack, rank }) => {
1012
+ const set = reachedBits[pack];
1013
+ return set !== void 0 && hasBit(set, rank);
699
1014
  };
700
- const first = yield* Effect.scoped(Effect.gen(function* () {
701
- const { young, fenced, fresh, reached } = yield* sides;
702
- const start = packs.old.length === 0 ? [] : unreachedOf([
703
- ...young,
704
- ...fresh,
705
- ...fenced
706
- ], rows, reached);
707
- if (merge !== void 0) return { start };
708
- const inYoung = holds(young);
709
- const inFenced = holds(fenced);
710
- const apart = (oid) => inYoung(oid) || inFenced(oid);
711
- const cruftRows = rows.filter((row) => !reached.has(row.oid) && !apart(row.oid));
1015
+ const reachedAt = (oid, holder) => reachedBits[holder.pack] === void 0 || ownBit(holder) || bits.rows.has(oid);
1016
+ const starts = (oid, holders) => packs.old.length > 0 && !reached(oid, holders) && holders.some(({ pack }) => isOld[pack] !== true || isFenced[pack] === true);
1017
+ const startsRow = (oid, holders) => packs.old.length > 0 && !reached(oid, holders) && !starts(oid, holders);
1018
+ const keptFirst = packs.old.length > 0 && neededCarry === void 0 ? void 0 : prior.keep?.first;
1019
+ const sideCounts = sides.map((pack) => pack.objects);
1020
+ const walkStart = pruneWalkStartCalls(input.listed.packs.reduce((sum, pack) => sum + pack.objects, 0) + rows.length, sideCounts.reduce((sum, count) => sum + count, 0) + rows.length + refs.length);
1021
+ const holdersCalls = keepMergeCalls(input, sides) + keptPutCalls(input, sideCounts, 0);
1022
+ if (keptFirst === void 0 && !goesOn(holdersCalls + walkStart)) return yield* stopped;
1023
+ const none = { start: new Array() };
1024
+ ran ||= keptFirst === void 0;
1025
+ const first = keptFirst !== void 0 ? none : yield* Effect.scoped(Effect.gen(function* () {
1026
+ const fresh = sides.flatMap((_, at) => isFresh[at] === true ? [at] : []);
1027
+ const cruft = merge === void 0 ? yield* classBitsOf(input, sides, fresh) : void 0;
1028
+ const start = [];
1029
+ const wanted = /* @__PURE__ */ new Set([...rowSet, ...refs]);
1030
+ const cruftCounts = classPacks(cruft).map((each) => each.count);
1031
+ const passed = yield* pass(wanted, (oid, holders) => {
1032
+ if (starts(oid, holders)) start.push(oid);
1033
+ if (cruft === void 0 || apart(holders)) return;
1034
+ for (const holder of holders) if (!reachedAt(oid, holder)) setClass(cruft, holder);
1035
+ }, {
1036
+ from: yield* holdersFrom(partialOf("first"), cruft, start),
1037
+ goesOn: goesOnIn(() => keptPutCalls(input, cruftCounts, start.length, heldBytes(wanted.size, sides.length)) + walkStart)
1038
+ });
1039
+ if (passed.stopped !== void 0) return { stopped: yield* stoppedAt({
1040
+ merge: "first",
1041
+ places: passed.stopped
1042
+ }, classPacks(cruft), {
1043
+ ids: start,
1044
+ held: passed.found
1045
+ }) };
1046
+ const rowsBy = passed.found;
1047
+ const caught = refs.filter((oid) => !reached(oid, rowsBy.get(oid) ?? []));
1048
+ if (caught.length > 0) return { caught };
1049
+ start.push(...rowOids.filter((oid) => startsRow(oid, rowsBy.get(oid) ?? [])));
1050
+ if (cruft === void 0) return { start };
1051
+ const cruftRows = rows.filter((row) => {
1052
+ const holders = rowsBy.get(row.oid) ?? [];
1053
+ return !reached(row.oid, holders) && !apart(holders);
1054
+ });
712
1055
  if (cruftRows.length > 0) return { cruft: yield* rowsCruft(input, cruftRows, most) };
713
- const copies = (yield* classBits(input, fresh, (indexed, rank) => !reachedAt(indexed, rank) && !apart(indexed.index.oidAt(rank)))).filter((each) => each.set > 0);
1056
+ const copies = [...cruft.values()].filter((each) => each.set > 0);
714
1057
  if (copies.length === 0) return { start };
715
1058
  const n = (classes?.n ?? -1) + 1;
716
1059
  const bytes = yield* putClasses(input, classesKey(input.listed.repo, at, n), copies);
@@ -722,6 +1065,23 @@ const passSteps = /* @__PURE__ */ Effect.fnUntraced(function* (input, read, pack
722
1065
  bytes
723
1066
  } };
724
1067
  }));
1068
+ if ("stopped" in first) return first.stopped;
1069
+ if ("caught" in first) {
1070
+ const walked = yield* walkOn(input, scratch, first.caught, rows, lost, calls, roots, {
1071
+ known: reachedRuns,
1072
+ spent: ran
1073
+ });
1074
+ return saving({
1075
+ ...carried,
1076
+ r: "more" in walked ? {
1077
+ saved: walked.more,
1078
+ refs: carry.refs
1079
+ } : {
1080
+ done: walked.runs,
1081
+ refs: refsJoined(carry.refs, walked.refs)
1082
+ }
1083
+ });
1084
+ }
725
1085
  if ("cruft" in first) return first.cruft;
726
1086
  if ("classes" in first) return {
727
1087
  step: {
@@ -730,70 +1090,130 @@ const passSteps = /* @__PURE__ */ Effect.fnUntraced(function* (input, read, pack
730
1090
  },
731
1091
  mark: { classes: first.classes }
732
1092
  };
1093
+ const startsAny = keptFirst?.starts ?? first.start.length > 0;
1094
+ keepNow.first = { starts: startsAny };
733
1095
  const named = walkPackOf(input.listed.repo, at, "n");
734
- const neededCarry = carried.n;
735
1096
  if (packs.old.length > 0 && (neededCarry === void 0 || "saved" in neededCarry)) {
1097
+ const keep = calls === void 0 ? void 0 : yield* saved(["r"]);
736
1098
  const start = neededCarry === void 0 ? first.start : [];
737
- const walked = yield* walkOn(input, named, start, rows, () => void 0, calls, roots, neededCarry?.saved, neededCarry === void 0 ? reachedRuns : void 0);
1099
+ const walked = yield* walkOn(input, named, start, rows, () => void 0, calls, roots, {
1100
+ saved: neededCarry?.saved,
1101
+ known: neededCarry === void 0 ? reachedRuns : void 0,
1102
+ spent: ran
1103
+ });
738
1104
  return saving({
739
1105
  ...carried,
740
1106
  n: "more" in walked ? { saved: walked.more } : { done: walked.runs }
1107
+ }, keep);
1108
+ }
1109
+ const keptN = prior.keep?.n;
1110
+ const savedN = keptN === void 0 ? void 0 : yield* readBits(input, keepKey(input.listed.repo, at, "n", keptN.n), keptN.bytes);
1111
+ if (savedN !== void 0 && keptN !== void 0) keepNow.n = keptN;
1112
+ keeping.changed ||= keptN !== void 0 && savedN === void 0;
1113
+ const merges = neededDone === void 0 ? 0 : keepMergeCalls(input, input.listed.packs, neededDone) + keptPutCalls(input, listedCounts, rowOids.length);
1114
+ if (neededDone !== void 0 && savedN === void 0 && !goesOn(merges)) return yield* stopped;
1115
+ const mergedN = neededDone === void 0 || savedN !== void 0 ? savedN && { bits: savedN } : yield* keepBitsOn(input, named, neededDone, rowOids, () => void 0, {
1116
+ from: yield* bitsFrom(partialOf("n")),
1117
+ goesOn: goesOnIn(bitsCalls)
1118
+ });
1119
+ if (mergedN !== void 0 && "stopped" in mergedN) return yield* stoppedBits("n", mergedN.stopped, mergedN.bits);
1120
+ const neededRuns = mergedN?.bits;
1121
+ if (neededRuns !== void 0 && savedN === void 0) {
1122
+ ran = true;
1123
+ unsaved.push({
1124
+ kind: "n",
1125
+ bits: neededRuns
741
1126
  });
742
1127
  }
743
- const neededDone = neededCarry !== void 0 && "done" in neededCarry ? neededCarry.done : void 0;
744
- const neededRuns = neededDone === void 0 ? void 0 : yield* keepBits(input, named, neededDone, rowOids, () => void 0);
745
- const behind = neededRuns === void 0 ? [] : yield* Effect.scoped(Effect.map(sides, ({ merging, young }) => {
746
- const needed = bitsOf(neededRuns, [...merging, ...young]);
747
- return first.start.filter((oid) => !needed.has(oid));
1128
+ const needed = neededRuns === void 0 ? () => false : reachOf(neededRuns, sides);
1129
+ const keep = (oid, holders) => reached(oid, holders) || !apart(holders) && (inFresh(holders) || rowSet.has(oid) || needed(oid, holders));
1130
+ const moved = merge !== void 0 && classes !== void 0 ? merge : void 0;
1131
+ const before = moved === void 0 || classes === void 0 ? void 0 : yield* readClasses(input, classesKey(input.listed.repo, at, classes.n), classes.bytes);
1132
+ const byPack = new Map((before ?? []).map((each) => [each.pack, each.bits]));
1133
+ const inputs = new Set(moved?.value.inputs);
1134
+ const unclassed = ({ pack, rank }) => {
1135
+ const id = sides[pack]?.id;
1136
+ if (id === void 0 || isYoung[pack] === true || !inputs.has(id)) return false;
1137
+ const set = byPack.get(id);
1138
+ return set === void 0 || !hasBit(set, rank);
1139
+ };
1140
+ const rewriting = moved === void 0 ? [...sides.flatMap((_, at) => isOld[at] === true && isFenced[at] !== true ? [at] : []), ...sides.flatMap((_, at) => isFresh[at] === true ? [at] : [])] : [];
1141
+ const kept = yield* classBitsOf(input, sides, rewriting);
1142
+ const outside = sides.map((pack, at) => isYoung[at] !== true && cruftOf(read, pack) === void 0);
1143
+ const isOutside = (holders) => holders.some(({ pack }) => outside[pack] === true);
1144
+ const whole = (oid, holders) => keep(oid, holders) || holders.some(({ pack }) => isFenced[pack] === true);
1145
+ if (!goesOn(holdersCalls + walkStart)) return yield* stopped;
1146
+ const behind = [];
1147
+ const behindOf = neededRuns !== void 0 && startsAny;
1148
+ const lastAt = partialOf("last");
1149
+ let missed = lastAt?.missed ?? false;
1150
+ let markFits = lastAt?.fits ?? true;
1151
+ const wanted = /* @__PURE__ */ new Set([...rowSet, ...bits.rows]);
1152
+ const keptCounts = classPacks(kept).map((each) => each.count);
1153
+ const passed = yield* pass(wanted, (oid, holders) => {
1154
+ if (behindOf && starts(oid, holders) && !needed(oid, holders)) behind.push(oid);
1155
+ const keeps = keep(oid, holders);
1156
+ if (moved !== void 0) {
1157
+ missed ||= keeps && holders.some(unclassed);
1158
+ return;
1159
+ }
1160
+ for (const holder of holders) if (keeps || ownBit(holder)) setClass(kept, holder);
1161
+ if (!markFits) return;
1162
+ const wholly = keeps || holders.some(({ pack }) => isFenced[pack] === true);
1163
+ if (isOutside(holders) && wholly && !reached(oid, holders)) markFits = false;
1164
+ if (holders.some(ownBit) && !(wholly && (rowSet.has(oid) || isOutside(holders)))) markFits = false;
1165
+ }, {
1166
+ from: yield* holdersFrom(lastAt, kept, behind),
1167
+ goesOn: goesOnIn(() => keptPutCalls(input, keptCounts, behind.length, heldBytes(wanted.size, sides.length)) + walkStart)
1168
+ });
1169
+ if (passed.stopped !== void 0) return yield* stoppedAt({
1170
+ merge: "last",
1171
+ places: passed.stopped,
1172
+ missed,
1173
+ fits: markFits
1174
+ }, classPacks(kept), {
1175
+ ids: behind,
1176
+ held: passed.found
1177
+ });
1178
+ const rowsBy = passed.found;
1179
+ if (behindOf) behind.push(...rowOids.filter((oid) => {
1180
+ const holders = rowsBy.get(oid) ?? [];
1181
+ return startsRow(oid, holders) && !needed(oid, holders);
748
1182
  }));
749
1183
  if (neededDone !== void 0 && behind.length > 0) {
750
- const walked = yield* walkOn(input, named, behind, rows, () => void 0, calls, roots, void 0, neededDone);
1184
+ const keep = calls === void 0 ? void 0 : yield* saved(["r"]);
1185
+ const walked = yield* walkOn(input, named, behind, rows, () => void 0, calls, roots, {
1186
+ known: neededDone,
1187
+ spent: ran
1188
+ });
751
1189
  return saving({
752
1190
  ...carried,
753
1191
  n: "more" in walked ? { saved: walked.more } : { done: walked.runs }
754
- });
1192
+ }, keep);
755
1193
  }
756
- const { merging, young, fenced, fresh, reached } = yield* sides;
757
- const inYoung = holds(young);
758
- const inFenced = holds(fenced);
759
- const inFresh = holds(fresh);
760
- const needed = neededRuns === void 0 ? /* @__PURE__ */ new Set() : bitsOf(neededRuns, [...merging, ...young]);
761
- const apart = (oid) => inYoung(oid) || inFenced(oid);
762
- const inRows = new Set(rowOids);
763
- const keep = (oid) => reached.has(oid) || !apart(oid) && (inFresh(oid) || inRows.has(oid) || needed.has(oid));
764
- if (merge !== void 0 && classes !== void 0) {
765
- const before = yield* readClasses(input, classesKey(input.listed.repo, at, classes.n), classes.bytes);
766
- const byPack = new Map((before ?? []).map((each) => [each.pack, each.bits]));
767
- const inputs = new Set(merge.value.inputs);
768
- if (!merging.some(({ pack, index }) => {
769
- if (!inputs.has(pack.id)) return false;
770
- const set = byPack.get(pack.id);
771
- for (let rank = 0; rank < index.count; rank += 1) if ((set === void 0 || !hasBit(set, rank)) && keep(index.oidAt(rank))) return true;
772
- return false;
773
- })) {
1194
+ if (moved !== void 0) {
1195
+ if (!missed) {
774
1196
  const listing = keptListing(read, {
775
1197
  reads: digest,
776
1198
  packs: ids
777
1199
  }, rowOids, landed);
778
- return went(yield* resumeMerge(input, merge, most, calls, listing), "kept");
1200
+ return went(yield* resumeMerge(input, moved, most, calls, listing), "kept");
779
1201
  }
780
1202
  if (redos + 1 >= 3) return {
781
1203
  waits: `pushes moved its refs onto what its rewrite was about to drop, 3 times in a row`,
782
1204
  busy: true
783
1205
  };
784
1206
  return {
785
- step: droppedStep(merge),
1207
+ step: droppedStep(moved),
786
1208
  mark: {
787
1209
  classes: void 0,
788
1210
  redos: redos + 1
789
1211
  }
790
1212
  };
791
1213
  }
792
- if (rows.some((row) => !keep(row.oid))) return yield* rowsStep(input, rows, keep, most);
793
- const rewrites = (yield* classBits(input, [...merging.filter(({ pack }) => oldIds.has(pack.id) && !fencedIds.has(pack.id)), ...fresh], (indexed, rank) => {
794
- const set = reachedBits.get(indexed.pack.id);
795
- return set !== void 0 && hasBit(set, rank) || keep(indexed.index.oidAt(rank));
796
- })).filter((each) => oldIds.has(each.pack) || each.set < each.count);
1214
+ const rowKeep = (oid) => keep(oid, rowsBy.get(oid) ?? []);
1215
+ if (rows.some((row) => !rowKeep(row.oid))) return yield* rowsStep(input, rows, rowKeep, most);
1216
+ const rewrites = [...kept.values()].filter((each) => oldIds.has(each.pack) || each.set < each.count);
797
1217
  const emptied = rewrites.filter((each) => each.set === 0);
798
1218
  if (emptied.length > 0) return { step: yield* emptiedStep(input, read, emptied) };
799
1219
  if (rewrites.length > 0) {
@@ -831,9 +1251,10 @@ const passSteps = /* @__PURE__ */ Effect.fnUntraced(function* (input, read, pack
831
1251
  },
832
1252
  more: false
833
1253
  };
834
- const whole = (oid) => keep(oid) || inFenced(oid);
835
- const outside = merging.filter(({ pack }) => !read.cruft.has(pack.id));
836
- if (held && reached.whole && reachesOnly(outside, rows, reached, whole)) step.reaches = true;
1254
+ const rowsWhole = (oid) => whole(oid, rowsBy.get(oid) ?? []);
1255
+ const rowsReach = (oid) => reached(oid, rowsBy.get(oid) ?? []);
1256
+ const rowsOutside = (oid) => isOutside(rowsBy.get(oid) ?? []);
1257
+ if (held && reachedBits.every((each) => each !== void 0) && markFits && reachesOnly(rows, rowsReach, bits.rows, rowsWhole, rowsOutside)) step.reaches = true;
837
1258
  return {
838
1259
  step,
839
1260
  last: true
@@ -886,7 +1307,9 @@ const passDue = (input, prune, read, objectRows) => {
886
1307
  * dropped, and it looks again within the hour. A marked pass whose try is `retried`, after one
887
1308
  * that died or failed, waits for the next period, as does one whose walk is past what one walk
888
1309
  * holds or makes no step, and one whose rewrite pushes caught up with `redosMost` times: each
889
- * writes the wait to its mark and drops its merge under way, with a log line. A pass that waits
1310
+ * writes the wait to its mark, drops its merge under way and its keep's objects, with a log line.
1311
+ * A step whose try's calls end before the pass's next merge keeps what its merges found on its
1312
+ * mark, for the next try to go on from (#2412). A pass that waits
890
1313
  * before its walk logs too, and writes nothing but its mark's end of a count of periods pushes
891
1314
  * held it, as every wait for anything but pushes does. `step` is `undefined` when no pass is
892
1315
  * due; `next` is when the next one is, and `undefined` for a repo that holds no object.
@@ -999,16 +1422,26 @@ const pruneStep = /* @__PURE__ */ Effect.fnUntraced(function* (input, prune, rep
999
1422
  change,
1000
1423
  more: true
1001
1424
  };
1002
- return Effect.as(later(why), {
1425
+ const kept = keptKeys(input.listed.repo, started.at, started.keep);
1426
+ const dropped = kept.length === 0 ? Effect.void : bucketDeleteMany(input.bucket, kept);
1427
+ return Effect.andThen(dropped, Effect.as(later(why), {
1003
1428
  step,
1004
1429
  next: until
1005
- });
1430
+ }));
1006
1431
  };
1007
1432
  if (retried) return yield* waits("its last try died or failed with the pass under way");
1008
- const stepped = yield* passSteps(input, read, packs, rows, most, calls).pipe(Effect.catchTag("PruneWalkTooLarge", (refused) => fail("RESOURCE_EXHAUSTED", `The prune pass is past what one walk holds: ${refused.message}`, { context: { reason: "prune-walk-too-large" } })));
1433
+ const keeping = {
1434
+ changed: false,
1435
+ keep: void 0
1436
+ };
1437
+ const stepped = yield* passSteps(input, read, packs, rows, most, calls, keeping).pipe(Effect.catchTag("PruneWalkTooLarge", (refused) => fail("RESOURCE_EXHAUSTED", `The prune pass is past what one walk holds: ${refused.message}`, { context: { reason: "prune-walk-too-large" } })));
1009
1438
  if ("waits" in stepped) return yield* waits(stepped.waits, "busy" in stepped);
1010
1439
  const { change } = stepped.step;
1011
- const patch = "mark" in stepped ? stepped.mark : void 0;
1440
+ const marked = "mark" in stepped ? stepped.mark : void 0;
1441
+ const patch = keeping.changed && (marked === void 0 || !("keep" in marked)) ? {
1442
+ ...marked,
1443
+ keep: keeping.keep
1444
+ } : marked;
1012
1445
  const step = patch === void 0 ? stepped.step : {
1013
1446
  ...stepped.step,
1014
1447
  change: {
@@ -1043,4 +1476,4 @@ const pruneStep = /* @__PURE__ */ Effect.fnUntraced(function* (input, prune, rep
1043
1476
  next
1044
1477
  };
1045
1478
  });
1046
- export { CruftRow, PassClasses, PruneCleared, PrunePass, PruneStarted, bitsOf, cruftOf, mergesAlone, passDue, passScratch, pruneClearedKey, prunePassKey, pruneStartedKey, pruneStep, reachesOnly, readPrune, walkPackOf };
1479
+ export { CruftRow, PassClasses, PassKeep, PruneCleared, PrunePass, PruneStarted, cruftOf, mergesAlone, passDue, passScratch, pruneClearedKey, prunePassKey, pruneStartedKey, pruneStep, reachOf, reachesOnly, readPrune, walkPackOf };