@ref0/git 0.0.0 → 0.2.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 (503) hide show
  1. package/dist/LICENSE +202 -0
  2. package/dist/access/private-refs.d.mts +27 -0
  3. package/dist/access/private-refs.mjs +173 -0
  4. package/dist/access.d.mts +2 -0
  5. package/dist/access.mjs +1 -0
  6. package/dist/auth/access-jwt.d.mts +21 -0
  7. package/dist/auth/access-jwt.mjs +102 -0
  8. package/dist/auth/acting.mjs +15 -0
  9. package/dist/auth/authorize.d.mts +5 -0
  10. package/dist/auth/authorize.mjs +292 -0
  11. package/dist/auth/credentials.mjs +57 -0
  12. package/dist/auth/fences.d.mts +5 -0
  13. package/dist/auth/fences.mjs +65 -0
  14. package/dist/auth/grants.mjs +61 -0
  15. package/dist/auth/helpers.d.mts +17 -0
  16. package/dist/auth/helpers.mjs +25 -0
  17. package/dist/auth/host-function.d.mts +4 -0
  18. package/dist/auth/host-function.mjs +69 -0
  19. package/dist/auth/identify.d.mts +6 -0
  20. package/dist/auth/identify.mjs +97 -0
  21. package/dist/auth/token-rows.mjs +100 -0
  22. package/dist/auth/token-text.mjs +66 -0
  23. package/dist/auth/tokens.d.mts +10 -0
  24. package/dist/auth/tokens.mjs +347 -0
  25. package/dist/auth.d.mts +7 -0
  26. package/dist/auth.mjs +6 -0
  27. package/dist/client/answer.mjs +141 -0
  28. package/dist/client/client.d.mts +23 -0
  29. package/dist/client/client.mjs +51 -0
  30. package/dist/client/git-http.mjs +201 -0
  31. package/dist/client/helper-node.mjs +19 -0
  32. package/dist/client/local.mjs +160 -0
  33. package/dist/client/negotiate.mjs +170 -0
  34. package/dist/client/push-upload.mjs +53 -0
  35. package/dist/client/remote-helper.mjs +129 -0
  36. package/dist/client/remote.mjs +398 -0
  37. package/dist/client/run-remote-helper.d.mts +35 -0
  38. package/dist/client/run-remote-helper.mjs +259 -0
  39. package/dist/client/scrub.d.mts +18 -0
  40. package/dist/client/scrub.mjs +38 -0
  41. package/dist/client/transport.d.mts +2 -0
  42. package/dist/client/transport.mjs +211 -0
  43. package/dist/contracts/access.d.mts +87 -0
  44. package/dist/contracts/actors.d.mts +144 -0
  45. package/dist/contracts/blobs.d.mts +38 -0
  46. package/dist/contracts/calls-spent.d.mts +11 -0
  47. package/dist/contracts/calls-spent.mjs +150 -0
  48. package/dist/contracts/client.d.mts +90 -0
  49. package/dist/contracts/commits.d.mts +126 -0
  50. package/dist/contracts/engine.d.mts +134 -0
  51. package/dist/contracts/events.d.mts +276 -0
  52. package/dist/contracts/files.d.mts +75 -0
  53. package/dist/contracts/forks.d.mts +78 -0
  54. package/dist/contracts/grants.d.mts +60 -0
  55. package/dist/contracts/handlers.d.mts +10 -0
  56. package/dist/contracts/ids.d.mts +15 -0
  57. package/dist/contracts/ids.mjs +15 -0
  58. package/dist/contracts/lfs.d.mts +27 -0
  59. package/dist/contracts/merges.d.mts +33 -0
  60. package/dist/contracts/objects.d.mts +645 -0
  61. package/dist/contracts/objects.mjs +54 -0
  62. package/dist/contracts/operations.d.mts +133 -0
  63. package/dist/contracts/promises.d.mts +63 -0
  64. package/dist/contracts/push-uploads.d.mts +44 -0
  65. package/dist/contracts/queue.d.mts +401 -0
  66. package/dist/contracts/refs.d.mts +95 -0
  67. package/dist/contracts/remote.d.mts +50 -0
  68. package/dist/contracts/repos.d.mts +153 -0
  69. package/dist/contracts/result.d.mts +126 -0
  70. package/dist/contracts/result.mjs +34 -0
  71. package/dist/contracts/rules.d.mts +159 -0
  72. package/dist/contracts/server.d.mts +245 -0
  73. package/dist/contracts/services.d.mts +36 -0
  74. package/dist/contracts/services.mjs +13 -0
  75. package/dist/contracts/signin.d.mts +126 -0
  76. package/dist/contracts/store.d.mts +41 -0
  77. package/dist/contracts/sync.d.mts +210 -0
  78. package/dist/contracts/sync.mjs +8 -0
  79. package/dist/contracts/tags.d.mts +50 -0
  80. package/dist/contracts/tags.mjs +6 -0
  81. package/dist/contracts/tokens.d.mts +87 -0
  82. package/dist/contracts/tokens.mjs +15 -0
  83. package/dist/contracts/topics.d.mts +174 -0
  84. package/dist/contracts/trees.d.mts +84 -0
  85. package/dist/contracts/work.d.mts +31 -0
  86. package/dist/core/access.d.mts +22 -0
  87. package/dist/core/access.mjs +262 -0
  88. package/dist/core/ancestry.mjs +235 -0
  89. package/dist/core/clock.mjs +18 -0
  90. package/dist/core/events/deliver.mjs +468 -0
  91. package/dist/core/events/stream.d.mts +19 -0
  92. package/dist/core/events/stream.mjs +290 -0
  93. package/dist/core/forks.mjs +1027 -0
  94. package/dist/core/hooks/post-receive.mjs +204 -0
  95. package/dist/core/hooks/pre-receive.d.mts +5 -0
  96. package/dist/core/hooks/pre-receive.mjs +34 -0
  97. package/dist/core/index-job.mjs +198 -0
  98. package/dist/core/names.mjs +30 -0
  99. package/dist/core/objects/blobs.mjs +60 -0
  100. package/dist/core/objects/commit-writes.mjs +185 -0
  101. package/dist/core/objects/commits.mjs +172 -0
  102. package/dist/core/objects/files.mjs +211 -0
  103. package/dist/core/objects/lines.mjs +42 -0
  104. package/dist/core/objects/merges.mjs +83 -0
  105. package/dist/core/objects/peel.mjs +166 -0
  106. package/dist/core/objects/reading.mjs +284 -0
  107. package/dist/core/objects/tags.mjs +96 -0
  108. package/dist/core/objects/tree-edits.mjs +266 -0
  109. package/dist/core/objects/trees.mjs +229 -0
  110. package/dist/core/objects/writing.mjs +266 -0
  111. package/dist/core/own-writes.mjs +94 -0
  112. package/dist/core/promise.mjs +46 -0
  113. package/dist/core/publish.mjs +142 -0
  114. package/dist/core/push-decide.mjs +206 -0
  115. package/dist/core/push.mjs +1069 -0
  116. package/dist/core/receipts.d.mts +7 -0
  117. package/dist/core/receipts.mjs +234 -0
  118. package/dist/core/ref-conflicts.mjs +27 -0
  119. package/dist/core/refs.mjs +359 -0
  120. package/dist/core/repack.mjs +521 -0
  121. package/dist/core/repos.mjs +752 -0
  122. package/dist/core/rules/changes.mjs +57 -0
  123. package/dist/core/rules/check.mjs +101 -0
  124. package/dist/core/rules/commit-changes.mjs +216 -0
  125. package/dist/core/rules/context.d.mts +9 -0
  126. package/dist/core/rules/context.mjs +120 -0
  127. package/dist/core/rules/define.d.mts +5 -0
  128. package/dist/core/rules/define.mjs +13 -0
  129. package/dist/core/rules/facts.mjs +13 -0
  130. package/dist/core/rules/fetch.d.mts +5 -0
  131. package/dist/core/rules/fetch.mjs +59 -0
  132. package/dist/core/rules/file-globs.mjs +55 -0
  133. package/dist/core/rules/files.mjs +142 -0
  134. package/dist/core/rules/patterns.mjs +23 -0
  135. package/dist/core/rules/protect.d.mts +9 -0
  136. package/dist/core/rules/protect.mjs +116 -0
  137. package/dist/core/seam.d.mts +24 -0
  138. package/dist/core/seam.mjs +41 -0
  139. package/dist/core/sight.mjs +99 -0
  140. package/dist/core/tokens.mjs +95 -0
  141. package/dist/core/view.d.mts +9 -0
  142. package/dist/core/view.mjs +221 -0
  143. package/dist/core/work.mjs +92 -0
  144. package/dist/effect.d.mts +20 -0
  145. package/dist/effect.mjs +18 -0
  146. package/dist/engine/allotted-xdiff.d.mts +4 -0
  147. package/dist/engine/allotted-xdiff.mjs +80 -0
  148. package/dist/engine/attribute-patterns.mjs +163 -0
  149. package/dist/engine/attributes.mjs +226 -0
  150. package/dist/engine/combined.mjs +653 -0
  151. package/dist/engine/commits.mjs +90 -0
  152. package/dist/engine/content.d.mts +4 -0
  153. package/dist/engine/content.mjs +221 -0
  154. package/dist/engine/diff.mjs +49 -0
  155. package/dist/engine/engine.d.mts +25 -0
  156. package/dist/engine/engine.mjs +49 -0
  157. package/dist/engine/failures.d.mts +4 -0
  158. package/dist/engine/failures.mjs +23 -0
  159. package/dist/engine/line-count.mjs +23 -0
  160. package/dist/engine/merge-base-walk.d.mts +2 -0
  161. package/dist/engine/merge-base-walk.mjs +265 -0
  162. package/dist/engine/merge-bases.mjs +27 -0
  163. package/dist/engine/merge-renames.mjs +165 -0
  164. package/dist/engine/merge.mjs +1125 -0
  165. package/dist/engine/parts.d.mts +8 -0
  166. package/dist/engine/parts.mjs +9 -0
  167. package/dist/engine/reads.d.mts +5 -0
  168. package/dist/engine/reads.mjs +105 -0
  169. package/dist/engine/walk.mjs +328 -0
  170. package/dist/engine/xdiff/NOTICE.md +63 -0
  171. package/dist/engine/xdiff/compiled.d.mts +7 -0
  172. package/dist/engine/xdiff/compiled.mjs +9 -0
  173. package/dist/engine/xdiff/embedded.mjs +4 -0
  174. package/dist/engine/xdiff/node.d.mts +7 -0
  175. package/dist/engine/xdiff/node.mjs +10 -0
  176. package/dist/engine/xdiff/upstream/LGPL-2.1 +511 -0
  177. package/dist/engine/xdiff/xdiff.wasm +0 -0
  178. package/dist/engine/xdiff.d.mts +34 -0
  179. package/dist/engine/xdiff.mjs +380 -0
  180. package/dist/engine.d.mts +3 -0
  181. package/dist/engine.mjs +3 -0
  182. package/dist/extend.d.mts +3 -0
  183. package/dist/extend.mjs +1 -0
  184. package/dist/http/answers.mjs +74 -0
  185. package/dist/http/bodies.mjs +143 -0
  186. package/dist/http/core-parts.mjs +252 -0
  187. package/dist/http/fetch-depth.mjs +26 -0
  188. package/dist/http/handler.mjs +94 -0
  189. package/dist/http/node.d.mts +70 -0
  190. package/dist/http/node.mjs +178 -0
  191. package/dist/http/paths.d.mts +9 -0
  192. package/dist/http/paths.mjs +118 -0
  193. package/dist/http/push-uploads.mjs +286 -0
  194. package/dist/http/receive-pack.mjs +235 -0
  195. package/dist/http/upload-pack.mjs +229 -0
  196. package/dist/index.d.mts +46 -0
  197. package/dist/index.mjs +16 -0
  198. package/dist/lfs/calls.mjs +16 -0
  199. package/dist/lfs/locks.mjs +161 -0
  200. package/dist/lfs/objects.mjs +370 -0
  201. package/dist/lfs/pointers.mjs +141 -0
  202. package/dist/lfs/repo.mjs +42 -0
  203. package/dist/lfs/rows.mjs +108 -0
  204. package/dist/lfs/service.mjs +34 -0
  205. package/dist/lfs/upkeep.mjs +162 -0
  206. package/dist/lfs/wire.mjs +102 -0
  207. package/dist/node.d.mts +3 -0
  208. package/dist/node.mjs +3 -0
  209. package/dist/protocol/bytes/buffers.d.mts +38 -0
  210. package/dist/protocol/bytes/buffers.mjs +62 -0
  211. package/dist/protocol/bytes/crc32.d.mts +4 -0
  212. package/dist/protocol/bytes/crc32.mjs +14 -0
  213. package/dist/protocol/bytes/deflate.d.mts +78 -0
  214. package/dist/protocol/bytes/deflate.mjs +175 -0
  215. package/dist/protocol/bytes/errors.d.mts +20 -0
  216. package/dist/protocol/bytes/errors.mjs +19 -0
  217. package/dist/protocol/bytes/hex.mjs +16 -0
  218. package/dist/protocol/bytes/hmac-sha256.mjs +91 -0
  219. package/dist/protocol/bytes/inflate.d.mts +40 -0
  220. package/dist/protocol/bytes/inflate.mjs +109 -0
  221. package/dist/protocol/bytes/joined.d.mts +2 -0
  222. package/dist/protocol/bytes/joined.mjs +25 -0
  223. package/dist/protocol/bytes/sha1-hasher.d.mts +20 -0
  224. package/dist/protocol/bytes/sha1-hasher.mjs +72 -0
  225. package/dist/protocol/bytes/sha1.d.mts +35 -0
  226. package/dist/protocol/bytes/sha1.mjs +409 -0
  227. package/dist/protocol/bytes/sha256.mjs +156 -0
  228. package/dist/protocol/bytes/streams.mjs +11 -0
  229. package/dist/protocol/bytes/text.mjs +19 -0
  230. package/dist/protocol/diff/diff-raw.d.mts +29 -0
  231. package/dist/protocol/diff/diff-raw.mjs +73 -0
  232. package/dist/protocol/diff/patch.mjs +92 -0
  233. package/dist/protocol/diff/paths.mjs +67 -0
  234. package/dist/protocol/diff/read.d.mts +62 -0
  235. package/dist/protocol/diff/read.mjs +342 -0
  236. package/dist/protocol/diff/rename-similarity.mjs +428 -0
  237. package/dist/protocol/diff/text.mjs +12 -0
  238. package/dist/protocol/diff/tree-diff.d.mts +41 -0
  239. package/dist/protocol/diff/tree-diff.mjs +607 -0
  240. package/dist/protocol/framing/band.d.mts +10 -0
  241. package/dist/protocol/framing/band.mjs +16 -0
  242. package/dist/protocol/framing/pkt-line.d.mts +48 -0
  243. package/dist/protocol/framing/pkt-line.mjs +92 -0
  244. package/dist/protocol/objects/checked-blobs.mjs +127 -0
  245. package/dist/protocol/objects/commit.d.mts +25 -0
  246. package/dist/protocol/objects/commit.mjs +138 -0
  247. package/dist/protocol/objects/fsck-gitmodules.mjs +296 -0
  248. package/dist/protocol/objects/fsck-names.mjs +189 -0
  249. package/dist/protocol/objects/fsck.mjs +297 -0
  250. package/dist/protocol/objects/headers.mjs +109 -0
  251. package/dist/protocol/objects/held.mjs +56 -0
  252. package/dist/protocol/objects/iconv.mjs +145 -0
  253. package/dist/protocol/objects/ident.mjs +111 -0
  254. package/dist/protocol/objects/lfs-pointer.d.mts +8 -0
  255. package/dist/protocol/objects/lfs-pointer.mjs +63 -0
  256. package/dist/protocol/objects/links-raw.mjs +89 -0
  257. package/dist/protocol/objects/object-rules.mjs +75 -0
  258. package/dist/protocol/objects/object.d.mts +16 -0
  259. package/dist/protocol/objects/object.mjs +29 -0
  260. package/dist/protocol/objects/tag.d.mts +21 -0
  261. package/dist/protocol/objects/tag.mjs +96 -0
  262. package/dist/protocol/objects/tree.d.mts +43 -0
  263. package/dist/protocol/objects/tree.mjs +162 -0
  264. package/dist/protocol/objects/utf8-verify.mjs +71 -0
  265. package/dist/protocol/packs/delta.d.mts +57 -0
  266. package/dist/protocol/packs/delta.mjs +229 -0
  267. package/dist/protocol/packs/graph.mjs +78 -0
  268. package/dist/protocol/packs/idx.d.mts +54 -0
  269. package/dist/protocol/packs/idx.mjs +274 -0
  270. package/dist/protocol/packs/indexer.d.mts +167 -0
  271. package/dist/protocol/packs/indexer.mjs +507 -0
  272. package/dist/protocol/packs/parts.d.mts +40 -0
  273. package/dist/protocol/packs/parts.mjs +89 -0
  274. package/dist/protocol/packs/pos.mjs +51 -0
  275. package/dist/protocol/packs/reader.d.mts +180 -0
  276. package/dist/protocol/packs/reader.mjs +414 -0
  277. package/dist/protocol/packs/rev.d.mts +32 -0
  278. package/dist/protocol/packs/rev.mjs +99 -0
  279. package/dist/protocol/packs/step.d.mts +10 -0
  280. package/dist/protocol/packs/step.mjs +12 -0
  281. package/dist/protocol/packs/write-error.d.mts +12 -0
  282. package/dist/protocol/packs/write-error.mjs +27 -0
  283. package/dist/protocol/packs/writer.d.mts +157 -0
  284. package/dist/protocol/packs/writer.mjs +353 -0
  285. package/dist/protocol/wire/advertisement.d.mts +72 -0
  286. package/dist/protocol/wire/advertisement.mjs +161 -0
  287. package/dist/protocol/wire/fetch.d.mts +155 -0
  288. package/dist/protocol/wire/fetch.mjs +458 -0
  289. package/dist/protocol/wire/lines.mjs +107 -0
  290. package/dist/protocol/wire/push-uploads.mjs +43 -0
  291. package/dist/protocol/wire/receive-pack.d.mts +71 -0
  292. package/dist/protocol/wire/receive-pack.mjs +157 -0
  293. package/dist/protocol/wire/report-status.d.mts +47 -0
  294. package/dist/protocol/wire/report-status.mjs +119 -0
  295. package/dist/protocol/wire/v2.d.mts +90 -0
  296. package/dist/protocol/wire/v2.mjs +185 -0
  297. package/dist/protocol.d.mts +31 -0
  298. package/dist/protocol.mjs +31 -0
  299. package/dist/queue/advance.mjs +1036 -0
  300. package/dist/queue/callers.mjs +134 -0
  301. package/dist/queue/calls.mjs +1154 -0
  302. package/dist/queue/commit.mjs +85 -0
  303. package/dist/queue/host.d.mts +15 -0
  304. package/dist/queue/host.mjs +88 -0
  305. package/dist/queue/ids.d.mts +5 -0
  306. package/dist/queue/ids.mjs +21 -0
  307. package/dist/queue/lanes.mjs +86 -0
  308. package/dist/queue/limits.mjs +99 -0
  309. package/dist/queue/pages.mjs +76 -0
  310. package/dist/queue/push-option.mjs +62 -0
  311. package/dist/queue/rows.mjs +206 -0
  312. package/dist/queue/schedule.mjs +132 -0
  313. package/dist/queue/search.mjs +315 -0
  314. package/dist/queue/state.mjs +325 -0
  315. package/dist/queue/steps.mjs +317 -0
  316. package/dist/remote-helper.d.mts +3 -0
  317. package/dist/remote-helper.mjs +3 -0
  318. package/dist/rest/api.d.mts +23 -0
  319. package/dist/rest/api.mjs +27 -0
  320. package/dist/rest/blobs.d.mts +58 -0
  321. package/dist/rest/blobs.mjs +41 -0
  322. package/dist/rest/caller.d.mts +102 -0
  323. package/dist/rest/caller.mjs +18 -0
  324. package/dist/rest/client.d.mts +18 -0
  325. package/dist/rest/client.mjs +587 -0
  326. package/dist/rest/commits.d.mts +435 -0
  327. package/dist/rest/commits.mjs +140 -0
  328. package/dist/rest/errors.d.mts +44 -0
  329. package/dist/rest/errors.mjs +186 -0
  330. package/dist/rest/events.d.mts +38 -0
  331. package/dist/rest/events.mjs +74 -0
  332. package/dist/rest/files.d.mts +179 -0
  333. package/dist/rest/files.mjs +92 -0
  334. package/dist/rest/forks.d.mts +205 -0
  335. package/dist/rest/forks.mjs +51 -0
  336. package/dist/rest/handler.d.mts +17 -0
  337. package/dist/rest/handler.mjs +106 -0
  338. package/dist/rest/handlers.d.mts +8 -0
  339. package/dist/rest/handlers.mjs +592 -0
  340. package/dist/rest/listen.d.mts +24 -0
  341. package/dist/rest/listen.mjs +142 -0
  342. package/dist/rest/me.d.mts +55 -0
  343. package/dist/rest/me.mjs +21 -0
  344. package/dist/rest/merges.d.mts +90 -0
  345. package/dist/rest/merges.mjs +39 -0
  346. package/dist/rest/paths.mjs +86 -0
  347. package/dist/rest/queue.d.mts +486 -0
  348. package/dist/rest/queue.mjs +228 -0
  349. package/dist/rest/refs.d.mts +232 -0
  350. package/dist/rest/refs.mjs +86 -0
  351. package/dist/rest/repos.d.mts +223 -0
  352. package/dist/rest/repos.mjs +93 -0
  353. package/dist/rest/tags.d.mts +153 -0
  354. package/dist/rest/tags.mjs +72 -0
  355. package/dist/rest/tokens.d.mts +297 -0
  356. package/dist/rest/tokens.mjs +88 -0
  357. package/dist/rest/topics.d.mts +257 -0
  358. package/dist/rest/topics.mjs +116 -0
  359. package/dist/rest/trees.d.mts +158 -0
  360. package/dist/rest/trees.mjs +118 -0
  361. package/dist/rest/wire.mjs +57 -0
  362. package/dist/rest/work.d.mts +107 -0
  363. package/dist/rest/work.mjs +77 -0
  364. package/dist/rest.d.mts +8 -0
  365. package/dist/rest.mjs +5 -0
  366. package/dist/server/extension.d.mts +14 -0
  367. package/dist/server/extension.mjs +85 -0
  368. package/dist/server/routes.d.mts +25 -0
  369. package/dist/server/routes.mjs +67 -0
  370. package/dist/server/server.d.mts +2 -0
  371. package/dist/server/server.mjs +439 -0
  372. package/dist/signin/codes.mjs +47 -0
  373. package/dist/signin/context.d.mts +9 -0
  374. package/dist/signin/context.mjs +121 -0
  375. package/dist/signin/device.mjs +122 -0
  376. package/dist/signin/forms.mjs +157 -0
  377. package/dist/signin/grants.d.mts +5 -0
  378. package/dist/signin/grants.mjs +257 -0
  379. package/dist/signin/route.mjs +68 -0
  380. package/dist/signin/web.mjs +125 -0
  381. package/dist/signin/wire.d.mts +42 -0
  382. package/dist/signin/wire.mjs +43 -0
  383. package/dist/storage/allotted.mjs +102 -0
  384. package/dist/storage/budget.mjs +96 -0
  385. package/dist/storage/calls.d.mts +18 -0
  386. package/dist/storage/calls.mjs +273 -0
  387. package/dist/storage/clean-up.mjs +124 -0
  388. package/dist/storage/distinct-ids.mjs +39 -0
  389. package/dist/storage/ewah.mjs +97 -0
  390. package/dist/storage/fetch.d.mts +13 -0
  391. package/dist/storage/fetch.mjs +792 -0
  392. package/dist/storage/fsck.mjs +77 -0
  393. package/dist/storage/held-memo.mjs +188 -0
  394. package/dist/storage/index-bitmaps.d.mts +4 -0
  395. package/dist/storage/index-bitmaps.mjs +654 -0
  396. package/dist/storage/index-build.d.mts +15 -0
  397. package/dist/storage/index-build.mjs +1433 -0
  398. package/dist/storage/index-fetch.d.mts +9 -0
  399. package/dist/storage/index-fetch.mjs +756 -0
  400. package/dist/storage/index-files.mjs +232 -0
  401. package/dist/storage/index-graph.d.mts +3 -0
  402. package/dist/storage/index-graph.mjs +1049 -0
  403. package/dist/storage/index-layers.mjs +317 -0
  404. package/dist/storage/index-reach.mjs +216 -0
  405. package/dist/storage/index-remap.d.mts +8 -0
  406. package/dist/storage/index-remap.mjs +592 -0
  407. package/dist/storage/layout.mjs +69 -0
  408. package/dist/storage/lfs-pointers.d.mts +21 -0
  409. package/dist/storage/lfs-pointers.mjs +175 -0
  410. package/dist/storage/link-join.mjs +106 -0
  411. package/dist/storage/links.mjs +105 -0
  412. package/dist/storage/loose.d.mts +15 -0
  413. package/dist/storage/loose.mjs +120 -0
  414. package/dist/storage/memory-budget.d.mts +8 -0
  415. package/dist/storage/memory-budget.mjs +546 -0
  416. package/dist/storage/merge.mjs +416 -0
  417. package/dist/storage/object-rows.d.mts +6 -0
  418. package/dist/storage/object-rows.mjs +211 -0
  419. package/dist/storage/pack-order.d.mts +8 -0
  420. package/dist/storage/pack-order.mjs +779 -0
  421. package/dist/storage/pack-ranges.d.mts +5 -0
  422. package/dist/storage/pack-ranges.mjs +284 -0
  423. package/dist/storage/pack-reads.d.mts +11 -0
  424. package/dist/storage/pack-reads.mjs +990 -0
  425. package/dist/storage/pack-receive.d.mts +11 -0
  426. package/dist/storage/pack-receive.mjs +449 -0
  427. package/dist/storage/packs.d.mts +56 -0
  428. package/dist/storage/packs.mjs +600 -0
  429. package/dist/storage/prune.mjs +506 -0
  430. package/dist/storage/push-allotment.mjs +109 -0
  431. package/dist/storage/push-uploads.mjs +151 -0
  432. package/dist/storage/pushed-commits.mjs +136 -0
  433. package/dist/storage/quarantine.mjs +98 -0
  434. package/dist/storage/raw-ids.mjs +44 -0
  435. package/dist/storage/ready.d.mts +19 -0
  436. package/dist/storage/ready.mjs +328 -0
  437. package/dist/storage/receive-links.mjs +297 -0
  438. package/dist/storage/repack-job.mjs +48 -0
  439. package/dist/storage/repack.d.mts +31 -0
  440. package/dist/storage/repack.mjs +182 -0
  441. package/dist/storage/roots.mjs +42 -0
  442. package/dist/storage/rows.d.mts +3 -0
  443. package/dist/storage/rows.mjs +71 -0
  444. package/dist/storage/shallow.d.mts +5 -0
  445. package/dist/storage/shallow.mjs +128 -0
  446. package/dist/storage/stream-whole.mjs +236 -0
  447. package/dist/storage/unpack.mjs +236 -0
  448. package/dist/storage/visible.mjs +137 -0
  449. package/dist/storage/walk.d.mts +7 -0
  450. package/dist/storage/walk.mjs +170 -0
  451. package/dist/storage/wants.mjs +362 -0
  452. package/dist/store/call-count.d.mts +47 -0
  453. package/dist/store/call-count.mjs +207 -0
  454. package/dist/store/catalogue.d.mts +7 -0
  455. package/dist/store/catalogue.mjs +643 -0
  456. package/dist/store/crossing.mjs +66 -0
  457. package/dist/store/cursor-seal.d.mts +2 -0
  458. package/dist/store/cursor-seal.mjs +85 -0
  459. package/dist/store/errors.d.mts +12 -0
  460. package/dist/store/errors.mjs +129 -0
  461. package/dist/store/git-store.mjs +75 -0
  462. package/dist/store/list-job.mjs +35 -0
  463. package/dist/store/outbox.d.mts +7 -0
  464. package/dist/store/outbox.mjs +295 -0
  465. package/dist/store/queue-jobs.mjs +43 -0
  466. package/dist/store/random.mjs +28 -0
  467. package/dist/store/records.d.mts +4 -0
  468. package/dist/store/records.mjs +192 -0
  469. package/dist/store/ref-rows.d.mts +4 -0
  470. package/dist/store/ref-rows.mjs +30 -0
  471. package/dist/store/repo-record.mjs +100 -0
  472. package/dist/store/reservations.mjs +82 -0
  473. package/dist/store/scopes.mjs +170 -0
  474. package/dist/store/settle.mjs +63 -0
  475. package/dist/store/topic-jobs.mjs +62 -0
  476. package/dist/sync/bound.d.mts +7 -0
  477. package/dist/sync/bound.mjs +29 -0
  478. package/dist/sync/github.d.mts +12 -0
  479. package/dist/sync/github.mjs +297 -0
  480. package/dist/sync/import.d.mts +8 -0
  481. package/dist/sync/import.mjs +154 -0
  482. package/dist/sync/jobs.d.mts +9 -0
  483. package/dist/sync/jobs.mjs +80 -0
  484. package/dist/sync/mirror.d.mts +9 -0
  485. package/dist/sync/mirror.mjs +227 -0
  486. package/dist/sync/publish.d.mts +5 -0
  487. package/dist/sync/publish.mjs +117 -0
  488. package/dist/sync/records.mjs +73 -0
  489. package/dist/sync/smart-http.d.mts +8 -0
  490. package/dist/sync/smart-http.mjs +23 -0
  491. package/dist/sync/step.mjs +334 -0
  492. package/dist/sync/tries.mjs +126 -0
  493. package/dist/sync.d.mts +8 -0
  494. package/dist/sync.mjs +8 -0
  495. package/dist/topics/advance.mjs +554 -0
  496. package/dist/topics/calls.mjs +346 -0
  497. package/dist/topics/directory.mjs +107 -0
  498. package/dist/topics/host.d.mts +11 -0
  499. package/dist/topics/host.mjs +71 -0
  500. package/dist/topics/members.mjs +177 -0
  501. package/dist/topics/rows.mjs +99 -0
  502. package/dist/topics/topic.mjs +126 -0
  503. package/package.json +86 -5
@@ -0,0 +1,1433 @@
1
+ import { hexOf } from "../protocol/bytes/hex.mjs";
2
+ import { Sha1 } from "../protocol/bytes/sha1.mjs";
3
+ import { writeCommitGraph } from "../protocol/packs/graph.mjs";
4
+ import { IndexerCache } from "../protocol/packs/indexer.mjs";
5
+ import { oidBytes } from "../protocol/packs/idx.mjs";
6
+ import { rawLinks } from "../protocol/objects/links-raw.mjs";
7
+ import { gitError } from "../store/errors.mjs";
8
+ import { randomText } from "../store/random.mjs";
9
+ import { allottableOf } from "./budget.mjs";
10
+ import { bucketCall, bucketPut } from "./calls.mjs";
11
+ import { EdgeFiles, copyBitmaps, copyHeldBytes, copyRunBytes, edgesBytesOf, edgesFileOf, ewahMostWords, noNumber, runsMost, storedBitmaps, storedSize, writeBitmaps } from "./index-bitmaps.mjs";
12
+ import { BasesWriter, TypesWriter, indexBasesBytes, indexTypesBytes, openBases, openTypes, typeCodes } from "./index-files.mjs";
13
+ import { bitmapWords } from "./ewah.mjs";
14
+ import { graphChunk, mergeHeldBytes, mergeIndexGraphs, openGraphLayers, openIndexGraph, writeIndexGraph } from "./index-graph.mjs";
15
+ import { hasBit, idsOf, lookupCalls, openLayerBitmaps, openLayout, orIntoBytes, reachOf } from "./index-layers.mjs";
16
+ import { indexFiles, indexPrefix, indexRowKey, isPackId, packFiles } from "./layout.mjs";
17
+ import { joinBytes, joinLinks } from "./link-join.mjs";
18
+ import { outsideOn, readBusy } from "./memory-budget.mjs";
19
+ import { startPut } from "./merge.mjs";
20
+ import { eachPageAt, objectOfRow } from "./object-rows.mjs";
21
+ import { SliceLinks, SliceRefused, cutAtSpent, isSliceRefused, linksPastMemory, nothingSettled, readSlice, spentOf } from "./pack-order.mjs";
22
+ import { PackBlocks, packIndexerLimits, readWhole } from "./pack-reads.mjs";
23
+ import { rangeRanks, readExactly } from "./pack-ranges.mjs";
24
+ import { compareIds } from "./raw-ids.mjs";
25
+ import * as Effect from "effect/Effect";
26
+ import * as Result from "effect/Result";
27
+ import * as Schema from "effect/Schema";
28
+ import * as Option from "effect/Option";
29
+ import * as Exit from "effect/Exit";
30
+ // src/storage/index-build.ts
31
+ /**
32
+ * The reachability index's first build, `git.index`'s steps, one a try: the region's rows read,
33
+ * the commit graph merged, each pack read in its own order a slice at a time, the bitmaps walked
34
+ * from the slices' edges, the `.types` and `.bases` joined from their parts, then the `index` row
35
+ * for the core to commit. Each step's cursor is the job's data, so a try that dies is done again
36
+ * from where the last one ended. It covers the packs listed when it began: one listed after is an
37
+ * extra, outside its bitmaps until the update job folds it in, and only a covered pack that leaves
38
+ * the listing, as a repack's do, starts it again (Q735). A history past the graph's bound ends it
39
+ * with a refused row instead, so the repo keeps the bounded walk.
40
+ *
41
+ * Its files are under `index/<repo>/`, a build's own under its ID, so prune lists one repo's.
42
+ */
43
+ const lost = (message) => gitError("DATA_LOSS", message);
44
+ /** A build's ID, new for each: `i_` and 12 random bytes. */
45
+ const newBuildId = () => `i_${randomText(12)}`;
46
+ const Count = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0));
47
+ /** One read slice, by the order it was read: what its edges, types and bases parts cover. */
48
+ const IndexSlice = Schema.Struct({
49
+ first: Count,
50
+ positions: Count,
51
+ sources: Count,
52
+ links: Count,
53
+ deltas: Count
54
+ });
55
+ const indexSteps = [
56
+ "region",
57
+ "graph",
58
+ "read",
59
+ "bitmaps",
60
+ "files",
61
+ "merge",
62
+ "commit"
63
+ ];
64
+ /** One run of a layer's positions: its packs, in position order, then the rows it read. */
65
+ const IndexGroup = Schema.Struct({
66
+ packs: Schema.Array(Schema.String),
67
+ rows: Count
68
+ });
69
+ /**
70
+ * One layer of the index, a build of its own: its groups, its region's IDs' SHA-1, its first
71
+ * position and how many it holds, its first graph number and its commits, its deltas, and its
72
+ * bitmaps' count and table. A whole build is one layer from position 0. An update adds a layer
73
+ * over the packs and rows listed since, numbered after the layers below, so no position or
74
+ * bitmap below it changes; a merge makes the layers one, their positions as they were.
75
+ */
76
+ const IndexLayer = Schema.Struct({
77
+ build: Schema.String,
78
+ groups: Schema.Array(IndexGroup),
79
+ region: Schema.String,
80
+ first: Count,
81
+ positions: Count,
82
+ graphFirst: Count,
83
+ commits: Count,
84
+ deltas: Count,
85
+ bitmaps: Count,
86
+ table: Count
87
+ });
88
+ /**
89
+ * A build under way: its ID, the packs it covers in position order, its region's rows and their
90
+ * IDs' SHA-1, its step, the read's cursor, its slices, and what the graph and bitmaps wrote.
91
+ * `below` is the row's layers an update builds on, none for a whole build; a merge's are the
92
+ * layers it makes one of, and it covers no pack of its own.
93
+ */
94
+ const IndexData = Schema.Struct({
95
+ below: Schema.Array(IndexLayer),
96
+ merge: Schema.Boolean,
97
+ build: Schema.String,
98
+ packs: Schema.Array(Schema.String),
99
+ rows: Count,
100
+ region: Schema.String,
101
+ step: Schema.Literals(indexSteps),
102
+ pack: Count,
103
+ offset: Count,
104
+ position: Count,
105
+ slices: Schema.Array(IndexSlice),
106
+ commits: Count,
107
+ bitmaps: Count,
108
+ table: Count,
109
+ /** The read step's slices narrowed, as `narrowedAfter` moves them: none, in full slices. */
110
+ narrowed: Schema.optionalKey(Schema.suspend(() => Narrowed))
111
+ });
112
+ /** A build its row no longer names, kept readable until `at` and prune's grace pass. */
113
+ const ReplacedBuild = Schema.Struct({
114
+ build: Schema.String,
115
+ at: Count
116
+ });
117
+ /**
118
+ * A refusal for spent tries: when it was made, how many there have been in a row, and when a push
119
+ * may save the next round of tries.
120
+ */
121
+ const IndexTries = Schema.Struct({
122
+ at: Count,
123
+ refusals: Count,
124
+ next: Count
125
+ });
126
+ /**
127
+ * An update whose tries were spent, on the row it would have added a layer to: its reason and
128
+ * tries, as a refused row's. The layers stand, and reads answer from them; `status` says the
129
+ * refusal, and no push saves the job until its wait has passed. The next build's row has none.
130
+ */
131
+ const IndexRefusal = Schema.Struct({
132
+ reason: Schema.String,
133
+ tries: IndexTries
134
+ });
135
+ /**
136
+ * The repo's index, `index`/`current`: its layers, oldest first, at most `layersMost`, and the
137
+ * builds it replaced, still in prune's grace. A reader takes every layer from this one row, so
138
+ * it never mixes layers of two versions. Each layer's region IDs are its `.region` file, so a
139
+ * row that leaves after the build changes nothing. `rounds` is how many rounds of tries the repo
140
+ * had used once it was built, or once its update's were spent, as a refused row's.
141
+ */
142
+ const BuiltIndex = Schema.Struct({
143
+ rounds: Count,
144
+ layers: Schema.Array(IndexLayer),
145
+ replaced: Schema.Array(ReplacedBuild),
146
+ refusal: Schema.optionalKey(IndexRefusal)
147
+ });
148
+ /** How long a repo waits after its first refusal for spent tries before a push saves more. */
149
+ const triesWait = 864e5;
150
+ /** The longest a refusal for spent tries waits: each one after the first doubles it, to this. */
151
+ const triesWaitMost = 2592e6;
152
+ /** The wait after the `refusals`th refusal for spent tries in a row. */
153
+ const triesWaitOf = (refusals) => Math.min(triesWait * 2 ** Math.max(0, refusals - 1), triesWaitMost);
154
+ /**
155
+ * A repo whose build was refused: its commits then, the bound it was refused at, and the
156
+ * refusal's own words, such as the commit too wide and its parents. Past the graph's bound, no
157
+ * ref-moving commit saves `git.index` while the server's bound is the same; a host that raises
158
+ * `PacksOptions.memory` raises it, and the next one does (Q735). For spent tries, `tries` says
159
+ * when a push may save the next round: a day after the first, twice as long after each further
160
+ * one, up to `triesWaitMost` (the coordinator, 22 Nov). `rounds` is how many rounds of tries the
161
+ * repo has used, either refusal's: the next round a push saves is that one, a job key never used.
162
+ */
163
+ const RefusedIndex = Schema.Struct({
164
+ refused: Schema.Struct({
165
+ commits: Count,
166
+ most: Count,
167
+ reason: Schema.String,
168
+ rounds: Count,
169
+ tries: Schema.optionalKey(IndexTries)
170
+ }),
171
+ replaced: Schema.Array(ReplacedBuild)
172
+ });
173
+ const IndexRow = Schema.Union([BuiltIndex, RefusedIndex]);
174
+ const decodeRowOption = Schema.decodeUnknownOption(IndexRow);
175
+ /** `row`'s layers, when it has some: a row of none is as no row. */
176
+ const builtOf = (row) => row === void 0 || "refused" in row || row.layers.length === 0 ? void 0 : row;
177
+ /** An `index` row's value decoded, at the read's boundary: none for one unreadable. */
178
+ const decodeIndexValue = decodeRowOption;
179
+ /** A decoded `index` row as a built index, or `undefined` for none, a refusal or no layers. */
180
+ const builtIndexOf = (row) => builtOf(Option.getOrUndefined(row));
181
+ /** A prune's end, with when the next file it kept is due. */
182
+ const prunedAt = (due) => due === void 0 ? { kind: "pruned" } : {
183
+ kind: "pruned",
184
+ due
185
+ };
186
+ /** How far one read slice goes: 32 MiB of pack, or 256 K links. */
187
+ const sliceBounds = {
188
+ bytes: 33554432,
189
+ links: 262144
190
+ };
191
+ /** The memory one try takes: its base cache and blocks' floors, then what they may grow to. */
192
+ const floors = {
193
+ cache: 8388608,
194
+ blocks: 2097152
195
+ };
196
+ const most = {
197
+ cache: packIndexerLimits.cacheBytes,
198
+ blocks: 8388608
199
+ };
200
+ /**
201
+ * The most the bitmaps step keeps of edges files and of bitmaps made, past what a repo fills. At
202
+ * the graph's bound, 16 positions a commit, a try with both full fits what's allotted.
203
+ */
204
+ const edgesMost = 12582912;
205
+ const madeMost = 8388608;
206
+ /** The listing's packs in position order: biggest first, ties by ID. */
207
+ const positionOrder = (packs) => [...packs].sort((a, b) => b.bytes - a.bytes || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
208
+ /** A put of `bytes` at `key`, done again when a try that died had put it already. */
209
+ /** A put that found its key there: a try that died after its put landed, done again. */
210
+ const alreadyThere = (error) => error.code === "ALREADY_EXISTS";
211
+ const putOnce = (bucket, key, bytes) => bucketPut(bucket, (retrying) => bucket.put(key, streamOf(bytes), {
212
+ size: bytes.byteLength,
213
+ retrying
214
+ })).pipe(Effect.catchIf(alreadyThere, () => Effect.void));
215
+ /** `bytes` as a stream of one chunk, so a put holds no copy of them. */
216
+ const streamOf = (bytes) => new ReadableStream({ start: (controller) => {
217
+ controller.enqueue(bytes);
218
+ controller.close();
219
+ } });
220
+ /** The most an edges file of `links` takes, each link kept. */
221
+ const edgesMostOf = (links) => edgesBytesOf({
222
+ sources: links.sources,
223
+ links: links.size
224
+ });
225
+ /** What a region's `.graph` of `commits` takes: each one's ID and count, and its parents' IDs. */
226
+ const graphFileBytes = (commits) => commits.reduce((sum, commit) => sum + 24 + commit.parents.length * 20, 32);
227
+ const wordsBytes = (words) => {
228
+ const bytes = new Uint8Array(words.length * 4);
229
+ const view = new DataView(bytes.buffer);
230
+ for (let at = 0; at < words.length; at += 1) view.setUint32(at * 4, words[at] ?? 0);
231
+ return bytes;
232
+ };
233
+ /** Big-endian words, as the index's files keep them. */
234
+ const wordsOf = (bytes) => {
235
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
236
+ return Uint32Array.from({ length: bytes.byteLength / 4 }, (_, at) => view.getUint32(at * 4));
237
+ };
238
+ const readScratch = (bucket, key) => Effect.flatMap(readWhole(bucket, key), (bytes) => bytes === void 0 ? Effect.fail(lost(`${key} is missing`)) : Effect.succeed(bytes));
239
+ /** The packs every layer of `built` covers. */
240
+ const coveredPacks = (layers) => new Set(layers.flatMap((layer) => layer.groups.flatMap((group) => group.packs)));
241
+ /**
242
+ * An update of `built`: a layer over the packs listed since its layers were built, in position
243
+ * order, and the rows its region step finds none of them holds.
244
+ */
245
+ const startedUpdate = (listed, built) => {
246
+ const covered = coveredPacks(built.layers);
247
+ return {
248
+ ...started({
249
+ ...listed,
250
+ packs: listed.packs.filter((pack) => !covered.has(pack.id))
251
+ }),
252
+ below: built.layers
253
+ };
254
+ };
255
+ /**
256
+ * A merge of `built`'s layers into one, at `layersMost`: their positions as they were, under a
257
+ * build of its own. The update after it adds what was listed since.
258
+ */
259
+ const startedMerge = (listed, built) => ({
260
+ ...started({
261
+ ...listed,
262
+ packs: []
263
+ }),
264
+ below: built.layers,
265
+ merge: true,
266
+ step: "merge"
267
+ });
268
+ /** A fresh build of `listed`. */
269
+ const started = (listed) => ({
270
+ below: [],
271
+ merge: false,
272
+ build: newBuildId(),
273
+ packs: positionOrder(listed.packs).map(({ id }) => id),
274
+ rows: 0,
275
+ region: "",
276
+ step: "region",
277
+ pack: 0,
278
+ offset: 12,
279
+ position: 0,
280
+ slices: [],
281
+ commits: 0,
282
+ bitmaps: 0,
283
+ table: 0
284
+ });
285
+ /** The positions the layers below `data` hold, where its own start. */
286
+ const firstOf = (below) => {
287
+ const last = below[below.length - 1];
288
+ return last === void 0 ? 0 : last.first + last.positions;
289
+ };
290
+ /** The graph numbers the layers below `data` take, where its own commits' start. */
291
+ const graphFirstOf = (below) => {
292
+ const last = below[below.length - 1];
293
+ return last === void 0 ? 0 : last.graphFirst + last.commits;
294
+ };
295
+ /** The layer `data` is making, as far as its positions go. */
296
+ const spanOf = (data) => ({
297
+ build: data.build,
298
+ groups: [{
299
+ packs: data.packs,
300
+ rows: data.rows
301
+ }],
302
+ first: firstOf(data.below)
303
+ });
304
+ /**
305
+ * Every layer's positions, `data`'s own last: the layout across them, this layer's own packs'
306
+ * ranges, its first position, and where its rows start, after its packs.
307
+ */
308
+ const layoutOf = (bucket, listed, data) => Effect.gen(function* () {
309
+ const layout = yield* openLayout(bucket, listed, [...data.below, spanOf(data)]);
310
+ const own = layout.ranges.slice(layout.ranges.length - data.packs.length);
311
+ const first = firstOf(data.below);
312
+ return {
313
+ layout,
314
+ own,
315
+ first,
316
+ packed: own.reduce((sum, pack) => sum + pack.count, first)
317
+ };
318
+ });
319
+ /** `region` with `ids`, rows ascending, at the positions from `first`. */
320
+ const withRows = (region, first, ids) => {
321
+ const count = ids.length / 20;
322
+ if (count === 0) return region;
323
+ const merged = region.positions.length + count;
324
+ const order = Array.from({ length: merged }, (_, at) => at);
325
+ const idAt = (at) => at < region.positions.length ? [region.ids, at * 20] : [ids, (at - region.positions.length) * 20];
326
+ order.sort((a, b) => {
327
+ const [left, from] = idAt(a);
328
+ const [right, to] = idAt(b);
329
+ return compareIds(left, from, right, to);
330
+ });
331
+ const out = {
332
+ ids: new Uint8Array(merged * 20),
333
+ positions: new Uint32Array(merged)
334
+ };
335
+ for (const [index, at] of order.entries()) {
336
+ const [source, from] = idAt(at);
337
+ out.ids.set(source.subarray(from, from + 20), index * 20);
338
+ out.positions[index] = at < region.positions.length ? region.positions[at] ?? 0 : first + at - region.positions.length;
339
+ }
340
+ return out;
341
+ };
342
+ /**
343
+ * The most calls a read step makes after its slice, over packs of `counts` objects: its join's,
344
+ * each pack's `.idx` IDs read a chunk of `rangeRanks` at a time, and a chunk's positions when it
345
+ * holds a link, then the slice's three puts. No store call: the join reads the packs' files alone,
346
+ * and the job's own reads and settle aren't `packs()`'s to count. A slice's own calls leave this
347
+ * many of the try's bounds for them (#681).
348
+ */
349
+ const indexReadCallsAfter = (counts) => ({
350
+ bucket: counts.reduce((sum, count) => sum + 2 * Math.ceil(count / rangeRanks), 0) + 3,
351
+ store: 0
352
+ });
353
+ /**
354
+ * The refusal of a build whose read step needs `needs` calls of `kind` after its slice, at or past
355
+ * the try's `most` of them: no slice could read within the rest, so it refuses before any (#681).
356
+ */
357
+ const reservePast = (kind, needs, most) => {
358
+ const message = `Indexing needs ${needs} ${kind === "bucket" ? "bucket calls" : "subrequests"} to join and write, more than the limit of ${most}. Raise \`callLimits.${kind}\` in \`cloudflare()\`'s options, up to the platform's cap.`;
359
+ return gitError("RESOURCE_EXHAUSTED", message, {
360
+ cause: new SliceRefused(message),
361
+ context: { reason: "too-many-store-calls" }
362
+ });
363
+ };
364
+ /**
365
+ * The refusal of a read step whose join and puts ran out of the try's `most` calls past its
366
+ * slice's: `indexReadCallsAfter` fell short of them, which only a wrong reserve does.
367
+ */
368
+ const reserveShort = (pack, position, most) => {
369
+ const message = `Pack ${pack}'s slice at ${position} left its join and writes short of ${most} bucket calls: indexReadCallsAfter is too small`;
370
+ return gitError("RESOURCE_EXHAUSTED", message, {
371
+ cause: new SliceRefused(message),
372
+ context: { reason: "too-many-store-calls" }
373
+ });
374
+ };
375
+ /** A try's join of its links, its workspace grown into `allotment` while it runs. */
376
+ const joinCharged = (links, ranges, region, allotment) => {
377
+ const bytes = joinBytes(links.size);
378
+ return Effect.acquireUseRelease(allotment.grow(bytes), () => joinLinks(links, ranges, region), () => Effect.sync(() => allotment.shrink(bytes)));
379
+ };
380
+ /**
381
+ * The repo's object rows at `listed`'s version, each one a region position after the packs':
382
+ * their IDs, the SHA-1 of those, and, for the region step, their types, links and commits.
383
+ * `undefined` once the repo moved under the read.
384
+ */
385
+ const readRegion = (parts, listed, each, skip) => Effect.gen(function* () {
386
+ let ids = /* @__PURE__ */ new Uint8Array(1280);
387
+ let rows = 0;
388
+ if (!(yield* eachPageAt(parts.store, listed.repo, listed.version, (page) => Effect.gen(function* () {
389
+ for (const row of page) {
390
+ if (skip?.has(row.id) === true) continue;
391
+ if ((rows + 1) * 20 > ids.length) {
392
+ const grown = new Uint8Array(ids.length * 2);
393
+ grown.set(ids);
394
+ ids = grown;
395
+ }
396
+ ids.set(oidBytes(row.id), rows * 20);
397
+ if (each !== void 0) yield* each(row, rows);
398
+ rows += 1;
399
+ }
400
+ return true;
401
+ })))) return void 0;
402
+ const sha = new Sha1();
403
+ sha.update(ids.subarray(0, rows * 20));
404
+ return {
405
+ ids: ids.subarray(0, rows * 20),
406
+ rows,
407
+ digest: hexOf(sha.digest())
408
+ };
409
+ });
410
+ /**
411
+ * The region step: each row's type and links, its commits as a `.graph`, and its IDs, all held
412
+ * charged to `allotment`: its links as they grow, and each row's ID, type and commit entry and
413
+ * the row's own bytes while it's parsed.
414
+ */
415
+ const regionStep = (parts, listed, data, allotment) => Effect.gen(function* () {
416
+ const { bucket } = parts;
417
+ const files = indexFiles(listed.repo, data.build);
418
+ const { layout, first, packed } = yield* layoutOf(bucket, listed, data);
419
+ const held = data.below.length === 0 ? void 0 : yield* heldBelow(parts, listed, layout, first);
420
+ const links = new SliceLinks((bytes) => allotment.tryGrow(bytes));
421
+ const types = [];
422
+ const commits = [];
423
+ const parsed = (object, at) => Effect.gen(function* () {
424
+ types.push(typeCodes[object.type]);
425
+ if (object.type === "blob") return;
426
+ links.begin(packed + at, object.type);
427
+ const malformed = rawLinks(object.type, object.bytes, links.add);
428
+ if (malformed !== void 0) return yield* Effect.fail(lost(`Object ${object.oid} is malformed: ${malformed}`));
429
+ if (links.refused) return yield* Effect.fail(linksPastMemory());
430
+ if (object.type === "commit") {
431
+ const [from, to] = links.linksOf(links.sources - 1);
432
+ yield* allotment.grow((to - from) * 80);
433
+ const named = [];
434
+ rawLinks("commit", object.bytes, (bytes, from) => named.push(hexOf(bytes.subarray(from, from + 20))));
435
+ commits.push({
436
+ oid: object.oid,
437
+ parents: named.slice(1)
438
+ });
439
+ }
440
+ });
441
+ const read = yield* readRegion(parts, listed, (row, at) => Effect.gen(function* () {
442
+ const object = yield* objectOfRow(row);
443
+ yield* allotment.grow(regionRowBytes + object.bytes.byteLength);
444
+ yield* parsed(object, at).pipe(Effect.ensuring(Effect.sync(() => allotment.shrink(object.bytes.byteLength))));
445
+ }), held);
446
+ if (read === void 0) return data;
447
+ const done = {
448
+ ...data,
449
+ rows: read.rows,
450
+ region: read.digest,
451
+ step: "graph"
452
+ };
453
+ if (read.rows === 0) return done;
454
+ const region = withRows(layout.region, packed, read.ids);
455
+ const targets = yield* joinCharged(links, layout.ranges, region, allotment);
456
+ const slice = data.slices.length;
457
+ yield* allotment.grow(edgesMostOf(links) + read.rows + graphFileBytes(commits));
458
+ const edges = edgesFileOf(files.edges(slice), packed, read.rows, links, targets);
459
+ yield* putOnce(bucket, edges.slice.key, edges.bytes);
460
+ yield* putOnce(bucket, files.typesPart(slice), Uint8Array.from(types));
461
+ yield* putOnce(bucket, files.basesPart(slice), /* @__PURE__ */ new Uint8Array(0));
462
+ yield* putOnce(bucket, files.region, read.ids);
463
+ yield* putOnce(bucket, files.regionGraph, writeCommitGraph(commits));
464
+ return {
465
+ ...done,
466
+ slices: [...data.slices, {
467
+ ...edges.slice,
468
+ deltas: 0
469
+ }]
470
+ };
471
+ });
472
+ /**
473
+ * The rows the layers below hold, which an update's region leaves at their positions there: the
474
+ * repo's rows at `listed`'s version, each found among the layers, and kept by ID.
475
+ */
476
+ const heldBelow = (parts, listed, layout, first) => Effect.gen(function* () {
477
+ const read = yield* readRegion(parts, listed);
478
+ const held = /* @__PURE__ */ new Set();
479
+ if (read === void 0 || read.rows === 0) return held;
480
+ const positions = yield* layout.positionsOf(read.ids, read.rows);
481
+ for (let at = 0; at < read.rows; at += 1) if ((positions[at] ?? 4294967295) < first) held.add(hexOf(read.ids.subarray(at * 20, at * 20 + 20)));
482
+ return held;
483
+ });
484
+ /**
485
+ * What the region holds of each row past its links: its ID, twice while the IDs grow, its type,
486
+ * and a commit's entry and its ID as text, rounded up.
487
+ */
488
+ const regionRowBytes = 256;
489
+ /** Each of `layers`' graphs, opened at its first graph number. */
490
+ const belowGraphs = (bucket, repo, layers) => Effect.forEach(layers, (layer) => openIndexGraph(bucket, indexFiles(repo, layer.build).graph, layer.graphFirst));
491
+ /** The graph step: every pack's `.graph` and the region's, merged. */
492
+ const graphStep = (parts, listed, data, allotment) => Effect.gen(function* () {
493
+ const files = indexFiles(listed.repo, data.build);
494
+ const graphs = data.packs.map((id) => isPackId(id) ? packFiles(id).graph : id);
495
+ if (data.rows > 0) graphs.push(files.regionGraph);
496
+ const graphFirst = graphFirstOf(data.below);
497
+ const below = data.below.length === 0 ? void 0 : openGraphLayers(yield* belowGraphs(parts.bucket, listed.repo, data.below)).below(data.below.length);
498
+ const graphInput = {
499
+ bucket: parts.bucket,
500
+ graphs,
501
+ key: files.graph,
502
+ allotment,
503
+ most: parts.graphMost
504
+ };
505
+ if (below !== void 0) graphInput.below = below;
506
+ const written = (yield* storedSize(parts.bucket, files.graph)) === void 0 ? yield* writeIndexGraph(graphInput) : {
507
+ kind: "written",
508
+ commits: (yield* openIndexGraph(parts.bucket, files.graph, graphFirst)).commits
509
+ };
510
+ if (written.kind === "refused") return written;
511
+ const step = data.packs.length === 0 ? "bitmaps" : "read";
512
+ return {
513
+ ...data,
514
+ commits: written.commits,
515
+ step
516
+ };
517
+ });
518
+ /**
519
+ * The type codes `slices` stored at positions of the pack whose order starts at repo position
520
+ * `base`: for each slice that holds any, one ranged read of its `.types` part, from the first it
521
+ * holds to the last, `headReads` at once. A slice holds at most `sliceEntriesMost` positions, so each
522
+ * read is inside the room `headsBytes` charges for the header reads it stands in for.
523
+ */
524
+ const storedTypesOf = (bucket, files, slices, base) => (positions) => Effect.gen(function* () {
525
+ const codes = /* @__PURE__ */ new Map();
526
+ const reads = slices.flatMap((slice, number) => {
527
+ const held = positions.filter((position) => base + position >= slice.first && base + position < slice.first + slice.positions);
528
+ if (held.length === 0) return [];
529
+ const from = Math.min(...held) + base - slice.first;
530
+ const length = Math.max(...held) + base - slice.first - from + 1;
531
+ return [Effect.map(readExactly(bucket, files.typesPart(number), from, length), (types) => {
532
+ for (const position of held) {
533
+ const code = types[position + base - slice.first - from];
534
+ if (code !== void 0) codes.set(position, code);
535
+ }
536
+ })];
537
+ });
538
+ yield* Effect.all(reads, {
539
+ concurrency: 4,
540
+ discard: true
541
+ });
542
+ return codes;
543
+ });
544
+ /**
545
+ * The read step: one slice of the pack at the cursor, its links joined and its parts put. With
546
+ * `parts.calls`, all it reads before and in the slice is over a bucket that refuses calls past
547
+ * the try's bound less `indexReadCallsAfter`, then its join and puts over the whole bound; a slice
548
+ * that settles nothing is read again alone, and one alone that can't is refused (#681).
549
+ */
550
+ const readStep = (parts, listed, data, allotment) => Effect.gen(function* () {
551
+ const { counted } = parts;
552
+ const counts = data.packs.map((id) => listed.packs.find((each) => each.id === id)?.objects ?? 0);
553
+ const reserve = indexReadCallsAfter(counts);
554
+ const subrequestsAfter = reserve.bucket + reserve.store;
555
+ if (counted !== void 0 && reserve.bucket >= counted.most.bucket) return yield* Effect.fail(reservePast("bucket", reserve.bucket, counted.most.bucket));
556
+ if (counted !== void 0 && subrequestsAfter >= counted.most.subrequests) return yield* Effect.fail(reservePast("subrequests", subrequestsAfter, counted.most.subrequests));
557
+ const lower = counted === void 0 ? parts.bucket : counted.within({
558
+ bucket: counted.most.bucket - reserve.bucket,
559
+ subrequests: counted.most.subrequests - subrequestsAfter
560
+ }).bucket;
561
+ let through = lower;
562
+ const bucket = {
563
+ multipart: lower.multipart,
564
+ put: (key, body, options) => through.put(key, body, options),
565
+ read: (key, range, options) => through.read(key, range, options),
566
+ delete: (key, options) => through.delete(key, options),
567
+ list: (prefix, page, options) => through.list(prefix, page, options)
568
+ };
569
+ const whole = counted?.most.bucket ?? Number.POSITIVE_INFINITY;
570
+ const files = indexFiles(listed.repo, data.build);
571
+ const at = {
572
+ offset: data.offset,
573
+ position: data.position
574
+ };
575
+ const opened = yield* Effect.result(cutAtSpent(layoutOf(bucket, listed, data)));
576
+ const bounds = parts.bounds ?? sliceBounds;
577
+ const entries = data.narrowed?.entries ?? bounds.entries ?? 65536;
578
+ if (Result.isFailure(opened)) {
579
+ const spent = spentOf(opened.failure);
580
+ if (spent === void 0) return yield* Effect.fail(opened.failure);
581
+ if (entries > 1) return withNarrowed(data, {
582
+ entries: 1,
583
+ to: at.position + 1
584
+ });
585
+ return yield* Effect.fail(nothingSettled(data.packs[data.pack] ?? "", at.position, spent.most));
586
+ }
587
+ const { layout, own } = opened.success;
588
+ const { ranges, region } = layout;
589
+ const pack = own[data.pack];
590
+ if (pack === void 0) return yield* Effect.die(/* @__PURE__ */ new Error("a read past the last pack"));
591
+ const links = new SliceLinks((bytes) => allotment.tryGrow(bytes));
592
+ const slice = yield* readSlice({
593
+ bucket,
594
+ ranges: pack,
595
+ at,
596
+ bounds: {
597
+ ...bounds,
598
+ entries
599
+ },
600
+ blocks: new PackBlocks(bucket, allotment.room(floors.blocks, most.blocks)),
601
+ cache: new IndexerCache(allotment.room(floors.cache, most.cache)),
602
+ links,
603
+ allot: outsideOn(allotment),
604
+ storedTypes: storedTypesOf(bucket, files, data.slices, pack.base),
605
+ calls: counted === void 0 ? void 0 : {
606
+ made: () => counted.made().bucket,
607
+ most: counted.most.bucket - reserve.bucket
608
+ }
609
+ });
610
+ through = parts.bucket;
611
+ if (slice.spent !== void 0) return withNarrowed(data, narrowedAfter(data.narrowed, at.position, {
612
+ spent: slice.spent,
613
+ next: at.position
614
+ }));
615
+ const number = data.slices.length;
616
+ const first = pack.base + at.position;
617
+ const edges = yield* Effect.gen(function* () {
618
+ const targets = yield* joinCharged(links, ranges, region, allotment);
619
+ yield* allotment.grow(edgesMostOf(links) + slice.bases.length * 4);
620
+ const made = edgesFileOf(files.edges(number), first, slice.types.length, links, targets);
621
+ yield* putOnce(bucket, made.slice.key, made.bytes);
622
+ yield* putOnce(bucket, files.typesPart(number), slice.types);
623
+ yield* putOnce(bucket, files.basesPart(number), wordsBytes(slice.bases));
624
+ return made;
625
+ }).pipe(cutAtSpent, Effect.catchIf((error) => spentOf(error) !== void 0, () => Effect.fail(reserveShort(data.packs[data.pack] ?? "", at.position, whole))));
626
+ const slices = [...data.slices, {
627
+ ...edges.slice,
628
+ deltas: slice.bases.length / 2
629
+ }];
630
+ const { narrowed: _narrowed, ...past } = data;
631
+ if (slice.next !== void 0) {
632
+ const after = narrowedAfter(data.narrowed, at.position, { next: slice.next.position });
633
+ const moved = {
634
+ ...past,
635
+ slices,
636
+ offset: slice.next.offset,
637
+ position: slice.next.position
638
+ };
639
+ return withNarrowed(moved, after);
640
+ }
641
+ const last = data.pack + 1 >= data.packs.length;
642
+ return {
643
+ ...past,
644
+ slices,
645
+ step: last ? "bitmaps" : "read",
646
+ pack: last ? data.pack : data.pack + 1,
647
+ offset: 12,
648
+ position: 0
649
+ };
650
+ });
651
+ /**
652
+ * The read step's slices narrowed after one that settled nothing: `entries`, half the spent
653
+ * slice's, and `to`, where its span ended. Each further slice that settles nothing halves them
654
+ * again, its own span's end the new `to`; a slice that settles keeps them, until a read passes
655
+ * `to`, and then full slices again. So a hard entry is found as a binary search finds it, about
656
+ * twice log2 of the span in tries, and the entries past its span are read in full slices (the
657
+ * coordinator, 22 Nov). Only a slice of one that settles nothing is refused (#681).
658
+ */
659
+ const Narrowed = Schema.Struct({
660
+ entries: Count,
661
+ to: Count
662
+ });
663
+ /**
664
+ * The narrowing a try leaves, from `narrowed`, the one it read in, and how its slice at `at`
665
+ * ended: with `spent`, the entries a slice that settled nothing set out to read, or settled up to
666
+ * `next`.
667
+ */
668
+ const narrowedAfter = (narrowed, at, ended) => {
669
+ if (ended.spent !== void 0) return {
670
+ entries: Math.max(1, Math.floor(ended.spent / 2)),
671
+ to: at + ended.spent
672
+ };
673
+ return narrowed === void 0 || ended.next >= narrowed.to ? void 0 : narrowed;
674
+ };
675
+ /** `data` with its slices narrowed, or none. */
676
+ const withNarrowed = (data, narrowed) => {
677
+ const { narrowed: _narrowed, ...rest } = data;
678
+ return narrowed === void 0 ? rest : {
679
+ ...rest,
680
+ narrowed
681
+ };
682
+ };
683
+ /** The slices in position order, as the bitmaps and the files read them. */
684
+ const byPosition = (data) => [...data.slices.entries()].sort(([, a], [, b]) => a.first - b.first);
685
+ /**
686
+ * Whether a tip at `position` wants a bitmap: the layout holds it, and no layer has one for it.
687
+ * A build bitmaps each tip this says, and `upToDate` holds once it says it of none, so the two
688
+ * ask one question (#814).
689
+ */
690
+ const wantsBitmap = (position, bitmaps) => position !== 4294967295 && !bitmaps.has(position);
691
+ const chooseCommits = (rows, tips, first = 0) => {
692
+ const commits = rows.length / 3;
693
+ const walked = new Uint8Array(commits);
694
+ const chosen = /* @__PURE__ */ new Set();
695
+ for (const tip of tips) {
696
+ let number = tip;
697
+ if (tip >= first && tip - first < commits) chosen.add(tip);
698
+ for (let steps = 0; number !== 2147483647 && number >= first && number - first < commits && walked[number - first] === 0; steps += 1) {
699
+ walked[number - first] = 1;
700
+ if (steps % 100 === 0) chosen.add(number);
701
+ number = rows[(number - first) * 3] ?? 2147483647;
702
+ }
703
+ }
704
+ return [...chosen];
705
+ };
706
+ /**
707
+ * The bitmaps step: the tips' and chosen commits' positions, then a walk for each. An update
708
+ * makes a bitmap only for a tip no layer below has one for, and for every `bitmapEvery`th of its
709
+ * own commits down their first parents. Its walk ORs a stored bitmap it meets below its layer,
710
+ * and closes any other position there by reading its objects. `undefined` when one won't close
711
+ * within `walkMost`: the update gives way to a whole build.
712
+ */
713
+ const bitmapsStep = (parts, listed, data, input, allotment) => Effect.gen(function* () {
714
+ const { bucket } = parts;
715
+ const files = indexFiles(listed.repo, data.build);
716
+ const { layout, first } = yield* layoutOf(bucket, listed, data);
717
+ const { positions } = layout;
718
+ const graphFirst = graphFirstOf(data.below);
719
+ const own = yield* openIndexGraph(bucket, files.graph, graphFirst);
720
+ const graph = openGraphLayers([...yield* belowGraphs(bucket, listed.repo, data.below), own]);
721
+ yield* allotment.grow(own.commits * 12);
722
+ const rows = yield* own.rows;
723
+ const stored = yield* openLayerBitmaps(bucket, listed.repo, data.below);
724
+ const tips = [...new Set(input.tips)];
725
+ const tipIds = idsOf(tips);
726
+ const tipPositions = yield* layout.positionsOf(tipIds, tips.length);
727
+ const tipNumbers = yield* graph.numbersOf(tipIds, tips.length);
728
+ const wanting = (at) => wantsBitmap(tipPositions[at] ?? 4294967295, stored);
729
+ const numbers = chooseCommits(rows, [...tipNumbers].filter((number, at) => number !== 4294967295 && number >= graphFirst && wanting(at)), graphFirst);
730
+ const numberIds = yield* graph.idsAt(numbers);
731
+ const numberPositions = yield* layout.positionsOf(numberIds, numbers.length);
732
+ let latest = 0;
733
+ const chosen = [];
734
+ for (const [at, number] of numbers.entries()) {
735
+ const position = numberPositions[at] ?? 4294967295;
736
+ if (position === 4294967295) return yield* Effect.fail(lost(`The index's commit ${number} has no position`));
737
+ const generation = rows[(number - graphFirst) * 3 + 2] ?? 0;
738
+ latest = Math.max(latest, generation);
739
+ chosen.push({
740
+ position,
741
+ number,
742
+ generation
743
+ });
744
+ }
745
+ for (const [at, number] of tipNumbers.entries()) {
746
+ if (number === 4294967295 || number >= graphFirst || !wanting(at)) continue;
747
+ const { generation } = yield* graph.entryOf(number);
748
+ latest = Math.max(latest, generation);
749
+ chosen.push({
750
+ position: tipPositions[at] ?? 0,
751
+ number,
752
+ generation
753
+ });
754
+ }
755
+ for (const [at, number] of tipNumbers.entries()) if (number === 4294967295 && wanting(at)) chosen.push({
756
+ position: tipPositions[at] ?? 0,
757
+ number: noNumber,
758
+ generation: latest + 1
759
+ });
760
+ const reach = parts.objects === void 0 ? void 0 : {
761
+ layout,
762
+ bitmaps: stored,
763
+ read: yield* parts.objects(listed),
764
+ allotment
765
+ };
766
+ const below = data.below.length === 0 ? void 0 : {
767
+ first,
768
+ reach: (position) => Effect.gen(function* () {
769
+ const bitmap = yield* stored.bitmapAt(position);
770
+ if (bitmap !== void 0 || reach === void 0) return bitmap;
771
+ return yield* reachOf(reach, [{ position }]);
772
+ })
773
+ };
774
+ const slices = byPosition(data).map(([number, slice]) => ({
775
+ key: files.edges(number),
776
+ first: slice.first,
777
+ positions: slice.positions,
778
+ sources: slice.sources,
779
+ links: slice.links
780
+ }));
781
+ const edgesBytes = slices.reduce((sum, slice) => sum + edgesBytesOf(slice), 0);
782
+ const madeAll = new Set(chosen.map(({ position }) => position)).size * ewahMostWords(bitmapWords(positions)) * 4;
783
+ const edges = new EdgeFiles(bucket, slices, Math.min(edgesMost, edgesBytes), positions);
784
+ yield* allotment.grow(edges.room);
785
+ const bitmapsInput = {
786
+ bucket,
787
+ key: files.bitmaps,
788
+ positions,
789
+ edges,
790
+ chosen,
791
+ allotment,
792
+ kept: Math.min(madeMost, madeAll)
793
+ };
794
+ if (below !== void 0) bitmapsInput.below = below;
795
+ const already = yield* storedSize(bucket, files.bitmaps);
796
+ const written = already === void 0 ? yield* writeBitmaps(bitmapsInput) : yield* storedBitmaps(bucket, files.bitmaps, positions, already);
797
+ if (written === void 0) return void 0;
798
+ return {
799
+ ...data,
800
+ step: "files",
801
+ bitmaps: written.count,
802
+ table: written.table
803
+ };
804
+ });
805
+ /**
806
+ * The files step: `.types` and `.bases`, each its parts streamed in position order, and each
807
+ * found there on its own when a try that died after its put landed is done again. It holds a
808
+ * part as read, its words, and what it writes of it, charged to `allotment` first.
809
+ */
810
+ const filesStep = (parts, listed, data, allotment) => Effect.gen(function* () {
811
+ const { bucket } = parts;
812
+ const files = indexFiles(listed.repo, data.build);
813
+ const positions = coveredPositions(listed, data);
814
+ const every = firstOf(data.below) + positions;
815
+ const deltas = data.slices.reduce((sum, slice) => sum + slice.deltas, 0);
816
+ const ordered = byPosition(data);
817
+ const part = data.slices.reduce((most, slice) => Math.max(most, slice.positions, slice.deltas * 8), 0);
818
+ yield* allotment.grow(3 * part + Math.ceil(every / 4096) * 4 + 65536);
819
+ const types = Effect.gen(function* () {
820
+ const writer = new TypesWriter(positions);
821
+ const put = yield* startPut(bucket, files.types, { size: indexTypesBytes(positions) });
822
+ yield* Effect.addFinalizer((exit) => Exit.isFailure(exit) ? put.abort : Effect.void);
823
+ yield* put.write(writer.head());
824
+ for (const [number] of ordered) yield* put.write(yield* writer.part(yield* readScratch(bucket, files.typesPart(number))));
825
+ yield* put.write(yield* writer.end());
826
+ yield* put.finish;
827
+ });
828
+ const bases = Effect.gen(function* () {
829
+ const writer = new BasesWriter(every, deltas);
830
+ const put = yield* startPut(bucket, files.bases, { size: indexBasesBytes(every, deltas) });
831
+ yield* Effect.addFinalizer((exit) => Exit.isFailure(exit) ? put.abort : Effect.void);
832
+ yield* put.write(writer.head());
833
+ for (const [number] of ordered) yield* put.write(yield* writer.rows(wordsOf(yield* readScratch(bucket, files.basesPart(number)))));
834
+ yield* put.write(yield* writer.end());
835
+ yield* put.finish;
836
+ });
837
+ if ((yield* storedSize(bucket, files.types)) === void 0) yield* types;
838
+ if ((yield* storedSize(bucket, files.bases)) === void 0) yield* bases;
839
+ return {
840
+ ...data,
841
+ step: "commit"
842
+ };
843
+ });
844
+ /** How many positions, or `.bases` rows, the merge reads of a layer's file at a time. */
845
+ const mergeChunk = 65536;
846
+ /** What `.types` and `.bases` hold as the merge joins them: a chunk of each, and their writers. */
847
+ const mergeFilesBytes = 655360;
848
+ const rowsOfLayer = (layer) => layer.groups.reduce((sum, group) => sum + group.rows, 0);
849
+ /**
850
+ * The most bucket calls `mergeStep` makes once its layers' graphs are open, each bound from its
851
+ * merging layers alone, so a try that can't make them refuses before it reads (the core EM's condition 3
852
+ * on part 2). A file a try that died already put makes fewer.
853
+ */
854
+ const mergeCallsAfterOpen = (merging) => {
855
+ const { layers, octopus, packs, tips } = merging;
856
+ const chunks = (count, size) => Math.ceil(count / size);
857
+ const commits = layers.reduce((sum, layer) => sum + layer.commits, 0);
858
+ const passes = octopus.some((each) => each > 0) ? 4 : 3;
859
+ let calls = 8 + (layers.some((layer) => rowsOfLayer(layer) > 0) ? 1 : 0);
860
+ for (const [at, layer] of layers.entries()) {
861
+ const graphed = chunks(layer.commits, graphChunk);
862
+ if (rowsOfLayer(layer) > 0) calls += 2;
863
+ calls += graphed * 2 * passes + 3 * (octopus[at] ?? 0) + graphed;
864
+ if (layer.bitmaps > 0) calls += 3 + runsMost(layer.table, layer.bitmaps) + runsMost(layer.table, layer.bitmaps, copyRunBytes);
865
+ calls += 1 + chunks(layer.positions, mergeChunk) + 2 + chunks(layer.deltas, mergeChunk);
866
+ }
867
+ calls += 2 + chunks(commits, graphChunk);
868
+ calls += 2 * packs.length;
869
+ const lookups = lookupCalls(tips, packs);
870
+ calls += Math.min(lookups.searched, lookups.joined);
871
+ return calls;
872
+ };
873
+ /**
874
+ * What `liveBitmaps` holds beside the bitmaps' entries: the union and its reads, the tips' IDs
875
+ * and their positions, their join, and the layout's `rows`, 24 bytes each.
876
+ */
877
+ const liveHeldBytes = (words, tips, rows) => words * 4 + orIntoBytes(words) + tips * 24 + joinBytes(tips) + rows * 24;
878
+ /**
879
+ * The most `mergeStep` holds at once, beside its allotment's floor, charged before any phase
880
+ * holds it: the region's IDs; the graph merge's; the live tips' union, their IDs, their join and
881
+ * the layout's rows; the copy's; or `.types` and `.bases`' chunks. Each phase's go before the
882
+ * next's, and the bitmaps' entries stay through the union and the copy, `mergeEntryBytes` each.
883
+ */
884
+ const mergeHeldMost = (merging) => {
885
+ const { layers, tips } = merging;
886
+ const positions = firstOf(layers);
887
+ const words = bitmapWords(positions);
888
+ const rows = layers.reduce((sum, layer) => sum + rowsOfLayer(layer), 0);
889
+ const commits = layers.reduce((sum, layer) => sum + layer.commits, 0);
890
+ const bitmaps = layers.reduce((sum, layer) => sum + layer.bitmaps, 0);
891
+ const files = layers.map((layer) => layer.first + layer.positions);
892
+ const phases = [
893
+ rows * 40,
894
+ commits * 4 + mergeHeldBytes(layers.length),
895
+ liveHeldBytes(words, tips, rows),
896
+ copyHeldBytes(positions, bitmaps, files),
897
+ mergeFilesBytes
898
+ ];
899
+ return Math.max(...phases) + bitmaps * 640;
900
+ };
901
+ /**
902
+ * The refusal of a merge that can't fit a try's bound or its instance's memory: the next try
903
+ * would come to the same, so its row waits as one for spent tries does, its layers kept.
904
+ */
905
+ const mergePast = (needs, what, most, raise) => {
906
+ const message = `Merging the index's layers needs ${needs} ${what}, more than the limit of ${most}. ${raise}`;
907
+ return gitError("RESOURCE_EXHAUSTED", message, {
908
+ cause: new SliceRefused(message),
909
+ context: { reason: what === "bytes" ? "too-large" : "too-many-store-calls" }
910
+ });
911
+ };
912
+ const raiseCalls = (kind) => `Raise \`callLimits.${kind}\` in \`cloudflare()\`'s options, up to the platform's cap.`;
913
+ /** A refusal when `needs` more calls are past what the try has left of its bound. */
914
+ const callsPast = (parts, needs) => {
915
+ const { counted } = parts;
916
+ if (counted === void 0) return void 0;
917
+ const made = counted.made();
918
+ for (const kind of ["bucket", "subrequests"]) if (made[kind] + needs > counted.most[kind]) return mergePast(made[kind] + needs, `${kind === "bucket" ? "bucket calls" : "subrequests"}`, counted.most[kind], raiseCalls(kind));
919
+ };
920
+ /** The merged region's digest, its IDs each layer's in turn, put once. */
921
+ const mergedRegion = (bucket, repo, layers, key) => Effect.gen(function* () {
922
+ const regions = yield* Effect.forEach(layers, (layer) => rowsOfLayer(layer) === 0 ? Effect.succeed(/* @__PURE__ */ new Uint8Array(0)) : readScratch(bucket, indexFiles(repo, layer.build).region));
923
+ const region = new Uint8Array(regions.reduce((sum, ids) => sum + ids.byteLength, 0));
924
+ let at = 0;
925
+ for (const ids of regions) {
926
+ region.set(ids, at);
927
+ at += ids.byteLength;
928
+ }
929
+ const sha = new Sha1();
930
+ sha.update(region);
931
+ if (region.byteLength > 0) yield* putOnce(bucket, key, region);
932
+ return hexOf(sha.digest());
933
+ });
934
+ /**
935
+ * The stored bitmaps a merge keeps: those a current tip's stored bitmap reaches, ORed through
936
+ * each layer's runs, the union gone once they're chosen. Only a tip with a stored bitmap is
937
+ * ORed, so its lists are the bitmaps' at most.
938
+ */
939
+ const liveBitmaps = (layout, stored, tips, positions) => Effect.gen(function* () {
940
+ const live = new Uint32Array(bitmapWords(positions));
941
+ const lookups = lookupCalls(tips.length, layout.ranges.map((pack) => pack.count));
942
+ const searchedUpTo = lookups.searched <= lookups.joined ? tips.length : 0;
943
+ const found = yield* layout.positionsOf(idsOf(tips), tips.length, searchedUpTo);
944
+ const bitmapped = [];
945
+ for (const position of found) if (position !== 4294967295 && stored.has(position)) bitmapped.push(position);
946
+ yield* stored.orInto(bitmapped, live);
947
+ return stored.entries.filter(({ entry }) => hasBit(live, entry.position));
948
+ });
949
+ /**
950
+ * `kept`'s bitmaps copied to `files.bitmaps` over `positions`, each numbered again by the merged
951
+ * graph at `files.graph`, which `graphs` were merged into: its bits and its position as `move`
952
+ * moves them, or where they were. A file a try that died already put is kept. Its caller charges
953
+ * the bitmaps' entries, `mergeEntryBytes` each; the copy charges its own.
954
+ */
955
+ const copyMergedBitmaps = (input) => Effect.gen(function* () {
956
+ const { bucket, files, positions, kept } = input;
957
+ const stored = yield* storedSize(bucket, files.bitmaps);
958
+ if (stored !== void 0) return yield* storedBitmaps(bucket, files.bitmaps, positions, stored);
959
+ const numbered = kept.filter(({ entry }) => entry.number !== noNumber);
960
+ const merged = yield* openIndexGraph(bucket, files.graph);
961
+ const ids = yield* openGraphLayers(input.graphs).idsAt(numbered.map(({ entry }) => entry.number));
962
+ const numbers = yield* merged.numbersOf(ids, numbered.length);
963
+ const copied = [];
964
+ let place = 0;
965
+ for (const { from, entry } of kept) {
966
+ if (entry.number === 4294967295) {
967
+ copied.push({
968
+ from,
969
+ entry,
970
+ number: noNumber
971
+ });
972
+ continue;
973
+ }
974
+ const number = numbers[place++] ?? 4294967295;
975
+ if (number === 4294967295) return yield* Effect.fail(lost(`The merged graph lacks the commit at ${entry.position}`));
976
+ copied.push({
977
+ from,
978
+ entry,
979
+ number
980
+ });
981
+ }
982
+ return yield* copyBitmaps({
983
+ bucket,
984
+ key: files.bitmaps,
985
+ positions,
986
+ copied,
987
+ allotment: input.allotment,
988
+ move: input.move
989
+ });
990
+ });
991
+ /**
992
+ * The merge step (#596): `data.below`'s layers made one, under `data.build`, in one try. Each
993
+ * position stays where it was, so the files join: the `.region`s and the groups in layer order,
994
+ * the `.types` and the `.bases` rows each in turn. The graphs merge by ID, numbered again from 0,
995
+ * and each live bitmap is copied over every position, its number the merged graph's. A bitmap is
996
+ * live when a current tip's stored bitmap sets its position: a tip no longer a ref, with the
997
+ * commits only it reached, keeps none past the merge (the core EM's Q1, condition 3). A file a
998
+ * try that died already put is kept, as the build's steps do.
999
+ *
1000
+ * Its calls and memory are forecast once its graphs are open, from `mergeCallsAfterOpen` and
1001
+ * `mergeHeldMost`: past the try's bound, or past what the instance allots, it refuses before it
1002
+ * reads, and its row keeps the layers as a refusal for spent tries does (part 2's condition 3).
1003
+ */
1004
+ const mergeStep = (parts, listed, data, input, allotment) => Effect.gen(function* () {
1005
+ const { bucket } = parts;
1006
+ const { repo } = listed;
1007
+ const layers = data.below;
1008
+ const files = indexFiles(repo, data.build);
1009
+ const positions = firstOf(layers);
1010
+ const deltas = layers.reduce((sum, layer) => sum + layer.deltas, 0);
1011
+ const tips = [...new Set(input.tips)];
1012
+ const opening = callsPast(parts, 2 * layers.length);
1013
+ if (opening !== void 0) return yield* Effect.fail(opening);
1014
+ const graphs = yield* belowGraphs(bucket, repo, layers);
1015
+ const objectsOf = new Map(listed.packs.map((pack) => [pack.id, pack.objects]));
1016
+ const packs = [...coveredPacks(layers)].map((id) => objectsOf.get(id) ?? 0);
1017
+ const merging = {
1018
+ layers,
1019
+ octopus: graphs.map((graph) => graph.octopus),
1020
+ packs,
1021
+ tips: tips.length
1022
+ };
1023
+ const past = callsPast(parts, mergeCallsAfterOpen(merging));
1024
+ if (past !== void 0) return yield* Effect.fail(past);
1025
+ const held = mergeHeldMost(merging);
1026
+ const tooLarge = mergePast(held, "bytes", parts.budget.total, "Raise `PacksOptions.memory`.");
1027
+ yield* allotment.grow(held, tooLarge);
1028
+ allotment.shrink(held);
1029
+ const rows = layers.reduce((sum, layer) => sum + rowsOfLayer(layer), 0);
1030
+ yield* allotment.grow(rows * 40);
1031
+ const digest = yield* mergedRegion(bucket, repo, layers, files.region);
1032
+ allotment.shrink(rows * 40);
1033
+ const commits = (yield* storedSize(bucket, files.graph)) === void 0 ? (yield* mergeIndexGraphs({
1034
+ bucket,
1035
+ graphs,
1036
+ key: files.graph,
1037
+ allotment
1038
+ })).commits : (yield* openIndexGraph(bucket, files.graph)).commits;
1039
+ const words = bitmapWords(positions);
1040
+ const entriesBytes = layers.reduce((sum, layer) => sum + layer.bitmaps, 0) * 640;
1041
+ yield* allotment.grow(entriesBytes);
1042
+ const stored = yield* openLayerBitmaps(bucket, repo, layers);
1043
+ const liveBytes = liveHeldBytes(words, tips.length, rows);
1044
+ yield* allotment.grow(liveBytes);
1045
+ const kept = yield* liveBitmaps(yield* openLayout(bucket, listed, layers), stored, tips, positions);
1046
+ allotment.shrink(liveBytes);
1047
+ const bitmaps = yield* copyMergedBitmaps({
1048
+ bucket,
1049
+ graphs,
1050
+ files,
1051
+ positions,
1052
+ kept,
1053
+ allotment
1054
+ });
1055
+ allotment.shrink(entriesBytes);
1056
+ yield* allotment.grow(mergeFilesBytes);
1057
+ const types = Effect.gen(function* () {
1058
+ const writer = new TypesWriter(positions);
1059
+ const put = yield* startPut(bucket, files.types, { size: indexTypesBytes(positions) });
1060
+ yield* Effect.addFinalizer((exit) => Exit.isFailure(exit) ? put.abort : Effect.void);
1061
+ yield* put.write(writer.head());
1062
+ for (const layer of layers) {
1063
+ const types = yield* openTypes(bucket, indexFiles(repo, layer.build).types, layer.positions);
1064
+ for (let from = 0; from < layer.positions; from += mergeChunk) {
1065
+ const to = Math.min(layer.positions, from + mergeChunk);
1066
+ yield* put.write(yield* writer.part(yield* types.codesOf(from, to)));
1067
+ }
1068
+ }
1069
+ yield* put.write(yield* writer.end());
1070
+ yield* put.finish;
1071
+ });
1072
+ const bases = Effect.gen(function* () {
1073
+ const writer = new BasesWriter(positions, deltas);
1074
+ const put = yield* startPut(bucket, files.bases, { size: indexBasesBytes(positions, deltas) });
1075
+ yield* Effect.addFinalizer((exit) => Exit.isFailure(exit) ? put.abort : Effect.void);
1076
+ yield* put.write(writer.head());
1077
+ for (const layer of layers) {
1078
+ const key = indexFiles(repo, layer.build).bases;
1079
+ yield* openBases(bucket, key, layer.first + layer.positions);
1080
+ for (let from = 0; from < layer.deltas; from += mergeChunk) {
1081
+ const count = Math.min(layer.deltas - from, mergeChunk);
1082
+ const bytes = yield* readExactly(bucket, key, 16 + from * 8, count * 8);
1083
+ yield* put.write(yield* writer.rows(wordsOf(bytes)));
1084
+ }
1085
+ }
1086
+ yield* put.write(yield* writer.end());
1087
+ yield* put.finish;
1088
+ });
1089
+ if ((yield* storedSize(bucket, files.types)) === void 0) yield* Effect.scoped(types);
1090
+ if ((yield* storedSize(bucket, files.bases)) === void 0) yield* Effect.scoped(bases);
1091
+ return {
1092
+ ...data,
1093
+ region: digest,
1094
+ commits,
1095
+ bitmaps: bitmaps.count,
1096
+ table: bitmaps.table,
1097
+ step: "commit"
1098
+ };
1099
+ });
1100
+ /**
1101
+ * Deletes every file under the repo's prefix whose build `keep` doesn't name, once prune's grace
1102
+ * has passed since it was replaced, or, for one no row names, since it was written, and never
1103
+ * before `from`; and the scratch of a build kept for its stored files alone. Answers when the next
1104
+ * file it kept is due: `at` itself once it has deleted `pruneMost`, with more left to look at.
1105
+ */
1106
+ const pruneBuilds = (bucket, repo, keep, replaced, at, grace, from = 0) => Effect.gen(function* () {
1107
+ const prefix = indexPrefix(repo);
1108
+ const replacedAt = new Map(replaced.map((each) => [each.build, each.at]));
1109
+ let due;
1110
+ let after;
1111
+ let deleted = 0;
1112
+ for (;;) {
1113
+ const page = yield* bucketCall((signal) => bucket.list(prefix, after === void 0 ? {} : { after }, { signal }));
1114
+ for (const item of page.items) {
1115
+ const name = item.key.slice(prefix.length);
1116
+ const build = name.split(/[./]/)[0] ?? "";
1117
+ const kept = keep.get(build);
1118
+ const scratch = name.startsWith(`${build}/`);
1119
+ if (kept === "all" || kept === "stored" && !scratch) continue;
1120
+ const since = Math.max(replacedAt.get(build) ?? item.uploaded, from);
1121
+ if (kept !== "stored" && since + grace > at) {
1122
+ due = Math.min(due ?? Number.POSITIVE_INFINITY, since + grace);
1123
+ continue;
1124
+ }
1125
+ yield* bucketCall((signal) => bucket.delete(item.key, { signal }));
1126
+ deleted += 1;
1127
+ if (deleted >= 200) return at;
1128
+ }
1129
+ if (page.next === void 0) return due;
1130
+ after = page.next;
1131
+ }
1132
+ });
1133
+ /**
1134
+ * The builds the next row keeps in prune's grace: those the current row kept, and each of its
1135
+ * layers the next row doesn't name, `next`.
1136
+ */
1137
+ const replacedBy = (input, next = []) => {
1138
+ const built = builtOf(input.row);
1139
+ const named = new Set(next.map((layer) => layer.build));
1140
+ return [...(input.row?.replaced ?? []).filter((each) => each.at + input.grace > input.at), ...(built?.layers ?? []).filter((layer) => !named.has(layer.build)).map((layer) => ({
1141
+ build: layer.build,
1142
+ at: input.at
1143
+ }))];
1144
+ };
1145
+ /** The positions of the packs `data` covers, all listed, and its region's. */
1146
+ const coveredPositions = (listed, data) => {
1147
+ const covered = new Set(data.packs);
1148
+ return listed.packs.reduce((sum, pack) => covered.has(pack.id) ? sum + pack.objects : sum, 0) + data.rows;
1149
+ };
1150
+ /** The rounds a row written by `input`'s try records: its round and those before it. */
1151
+ const roundsAfter = (input) => (input.round ?? 0) + 1;
1152
+ /**
1153
+ * The commit step: the row, its layers those below the build and the build's own, over the packs
1154
+ * it began with and its region.
1155
+ */
1156
+ const commitStep = (listed, data, input) => Effect.sync(() => {
1157
+ if (data.merge) {
1158
+ const merged = {
1159
+ build: data.build,
1160
+ groups: data.below.flatMap((layer) => layer.groups),
1161
+ region: data.region,
1162
+ first: 0,
1163
+ positions: firstOf(data.below),
1164
+ graphFirst: 0,
1165
+ commits: data.commits,
1166
+ deltas: data.below.reduce((sum, layer) => sum + layer.deltas, 0),
1167
+ bitmaps: data.bitmaps,
1168
+ table: data.table
1169
+ };
1170
+ return {
1171
+ kind: "done",
1172
+ row: {
1173
+ rounds: roundsAfter(input),
1174
+ layers: [merged],
1175
+ replaced: replacedBy(input, [merged])
1176
+ },
1177
+ data
1178
+ };
1179
+ }
1180
+ const layer = {
1181
+ build: data.build,
1182
+ groups: [{
1183
+ packs: data.packs,
1184
+ rows: data.rows
1185
+ }],
1186
+ region: data.region,
1187
+ first: firstOf(data.below),
1188
+ positions: coveredPositions(listed, data),
1189
+ graphFirst: graphFirstOf(data.below),
1190
+ commits: data.commits,
1191
+ deltas: data.slices.reduce((sum, slice) => sum + slice.deltas, 0),
1192
+ bitmaps: data.bitmaps,
1193
+ table: data.table
1194
+ };
1195
+ const layers = [...data.below, layer];
1196
+ return {
1197
+ kind: "done",
1198
+ row: {
1199
+ rounds: roundsAfter(input),
1200
+ layers,
1201
+ replaced: replacedBy(input, layers)
1202
+ },
1203
+ data
1204
+ };
1205
+ });
1206
+ /**
1207
+ * Whether every pack `layers` cover is still listed: one listed since is an extra. A covered
1208
+ * pack that left, in any layer, as a repack's do, starts a whole build again.
1209
+ */
1210
+ const coversAll = (layers, listed) => {
1211
+ const ids = new Set(listed.packs.map((pack) => pack.id));
1212
+ return [...coveredPacks(layers)].every((id) => ids.has(id));
1213
+ };
1214
+ /** Whether every pack `data` and the layers below it cover is still listed. */
1215
+ const covers = (data, listed) => coversAll([...data.below, { groups: [{
1216
+ packs: data.packs,
1217
+ rows: data.rows
1218
+ }] }], listed);
1219
+ /**
1220
+ * Whether `built` holds all of `listed` and a bitmap for every tip: no pack or row listed since
1221
+ * its layers, and none of `tips` without a bitmap. A tip whose object no layer holds once every
1222
+ * pack and row is covered, such as a ref to an object the repo lacks, is left as it is: no update
1223
+ * would give it one. The try that finds it so only prunes.
1224
+ */
1225
+ const upToDate = (parts, listed, built, tips) => Effect.gen(function* () {
1226
+ const covered = coveredPacks(built.layers);
1227
+ if (listed.packs.some((pack) => !covered.has(pack.id))) return false;
1228
+ const layout = yield* openLayout(parts.bucket, listed, built.layers);
1229
+ const read = yield* readRegion(parts, listed);
1230
+ if (read === void 0) return false;
1231
+ if ((yield* layout.positionsOf(read.ids, read.rows)).some((position) => position === 4294967295)) return false;
1232
+ const bitmaps = yield* openLayerBitmaps(parts.bucket, listed.repo, built.layers);
1233
+ const unique = [...new Set(tips)];
1234
+ return (yield* layout.positionsOf(idsOf(unique), unique.length)).every((position) => !wantsBitmap(position, bitmaps));
1235
+ });
1236
+ /** The repo's `index` row decoded, or `undefined` for none or one unreadable, built again. */
1237
+ const indexRowOf = (row) => row === void 0 ? void 0 : Option.getOrUndefined(decodeRowOption(row.value));
1238
+ /**
1239
+ * Whether `row` is a built index of `round`, the one its status saves: that round's tries keep it
1240
+ * up, so its guard lets them past. One whose update `round`'s tries were spent on is that round's
1241
+ * refusal, and a later try of it only prunes.
1242
+ */
1243
+ const ownBuilt = (row, round) => !("refused" in row) && row.refusal === void 0 && row.rounds === round + 1;
1244
+ /** How many rounds of tries the repo had used when `row` was written. */
1245
+ const roundsOf = (row) => "refused" in row ? row.refused.rounds : row.rounds;
1246
+ /**
1247
+ * `GitObjects.index.status`: what the repo's `index` row says at `at`, one `IndexStatus`. A
1248
+ * built row's round is the one that built it, whose tries keep it up a layer at a time. One whose
1249
+ * update was refused for spent tries is `refused` until its wait passes, then `retry` at the next
1250
+ * round, though its layers stand.
1251
+ */
1252
+ const indexStatus = (row, graphMost, at) => {
1253
+ if (row === void 0) return { state: "unbuilt" };
1254
+ if (!("refused" in row)) {
1255
+ const { refusal, rounds } = row;
1256
+ if (refusal === void 0) return {
1257
+ state: "built",
1258
+ round: Math.max(0, rounds - 1)
1259
+ };
1260
+ const { reason, tries } = refusal;
1261
+ return at >= tries.next ? {
1262
+ state: "retry",
1263
+ reason,
1264
+ round: rounds
1265
+ } : {
1266
+ state: "refused",
1267
+ reason,
1268
+ retryAt: tries.next
1269
+ };
1270
+ }
1271
+ const { most, reason, rounds, tries } = row.refused;
1272
+ if (most !== graphMost || tries !== void 0 && at >= tries.next) return {
1273
+ state: "retry",
1274
+ reason,
1275
+ round: rounds
1276
+ };
1277
+ return tries === void 0 ? {
1278
+ state: "too-large",
1279
+ reason
1280
+ } : {
1281
+ state: "refused",
1282
+ reason,
1283
+ retryAt: tries.next
1284
+ };
1285
+ };
1286
+ /**
1287
+ * Whether a ref-moving commit saves `git.index` over `status`: none built, a built index to keep
1288
+ * up, or a refusal's hold ended.
1289
+ */
1290
+ const indexSaves = (status) => status.state === "unbuilt" || status.state === "built" || status.state === "retry";
1291
+ /**
1292
+ * One try of the build of `listed`: its next step, from `input.data`, or a new build when none
1293
+ * is under way, or when a pack it covers left the listing. Before its region is read, any change
1294
+ * to the packs starts it again, since its rows are read at the try's own version.
1295
+ */
1296
+ const indexStep = (parts, listed, input) => Effect.gen(function* () {
1297
+ const { row } = input;
1298
+ const keptOf = (building) => {
1299
+ const keep = /* @__PURE__ */ new Map();
1300
+ for (const layer of builtOf(row)?.layers ?? []) keep.set(layer.build, "stored");
1301
+ if (building !== void 0) keep.set(building, "all");
1302
+ return keep;
1303
+ };
1304
+ const prune = (building) => pruneBuilds(parts.bucket, listed.repo, keptOf(building), row?.replaced ?? [], input.at, input.grace);
1305
+ if (input.deleted !== void 0) {
1306
+ const due = yield* pruneBuilds(parts.bucket, listed.repo, /* @__PURE__ */ new Map(), [], input.at, input.grace, input.deleted);
1307
+ return prunedAt(due);
1308
+ }
1309
+ const round = input.round ?? 0;
1310
+ if (row !== void 0 && roundsOf(row) > round && !ownBuilt(row, round)) return prunedAt(yield* prune());
1311
+ const waiting = (reason) => {
1312
+ const rounds = round + 1;
1313
+ const kept = builtOf(row);
1314
+ const refusals = ((row === void 0 ? void 0 : "refused" in row ? row.refused.tries : row.refusal?.tries)?.refusals ?? 0) + 1;
1315
+ const tries = {
1316
+ at: input.at,
1317
+ refusals,
1318
+ next: input.at + triesWaitOf(refusals)
1319
+ };
1320
+ if (kept !== void 0) return {
1321
+ kind: "done",
1322
+ row: {
1323
+ rounds,
1324
+ layers: kept.layers,
1325
+ replaced: replacedBy(input, kept.layers),
1326
+ refusal: {
1327
+ reason,
1328
+ tries
1329
+ }
1330
+ }
1331
+ };
1332
+ return {
1333
+ kind: "done",
1334
+ row: {
1335
+ refused: {
1336
+ commits: input.data?.commits ?? 0,
1337
+ most: parts.graphMost,
1338
+ reason,
1339
+ rounds,
1340
+ tries
1341
+ },
1342
+ replaced: replacedBy(input)
1343
+ }
1344
+ };
1345
+ };
1346
+ if (input.failed !== void 0) return waiting(input.failed.reason);
1347
+ if (input.data === void 0 && row !== void 0 && !indexSaves(indexStatus(row, parts.graphMost, input.at))) return prunedAt(yield* prune());
1348
+ const built = builtOf(row);
1349
+ if (input.data === void 0 && built !== void 0 && coversAll(built.layers, listed) && (yield* upToDate(parts, listed, built, input.tips))) return prunedAt(yield* prune());
1350
+ const fresh = () => built === void 0 || !coversAll(built.layers, listed) ? started(listed) : built.layers.length < 8 ? startedUpdate(listed, built) : startedMerge(listed, built);
1351
+ const kept = input.data;
1352
+ const sameBelow = kept !== void 0 && (kept.below.length === 0 || kept.below.map((layer) => layer.build).join() === (built?.layers ?? []).map((layer) => layer.build).join());
1353
+ const again = (from) => from.below.length === 0 && from.merge !== true ? started(listed) : fresh();
1354
+ const data = kept === void 0 || !covers(kept, listed) || !sameBelow ? fresh() : kept.step === "region" ? again(kept) : kept;
1355
+ const more = (next) => ({
1356
+ kind: "more",
1357
+ data: next
1358
+ });
1359
+ if (data.step === "region") yield* prune(data.build);
1360
+ if (data.step === "commit") return yield* commitStep(listed, data, input);
1361
+ const allotment = yield* parts.budget.allot(floors.cache + floors.blocks, readBusy);
1362
+ if (data.step === "region") return more(yield* regionStep(parts, listed, data, allotment));
1363
+ if (data.step === "files") return more(yield* filesStep(parts, listed, data, allotment));
1364
+ if (data.step === "merge") {
1365
+ const merged = yield* Effect.result(mergeStep(parts, listed, data, input, allotment));
1366
+ if (Result.isSuccess(merged)) return more(merged.success);
1367
+ if (isSliceRefused(merged.failure)) return waiting(merged.failure.message);
1368
+ return yield* Effect.fail(merged.failure);
1369
+ }
1370
+ if (data.step === "graph") {
1371
+ const graphed = yield* graphStep(parts, listed, data, allotment);
1372
+ if ("step" in graphed) return more(graphed);
1373
+ return {
1374
+ kind: "done",
1375
+ row: {
1376
+ refused: {
1377
+ commits: graphed.commits,
1378
+ most: parts.graphMost,
1379
+ reason: graphed.refusal.message,
1380
+ rounds: round + 1
1381
+ },
1382
+ replaced: replacedBy(input)
1383
+ }
1384
+ };
1385
+ }
1386
+ if (data.step === "read") {
1387
+ const read = yield* Effect.result(readStep(parts, listed, data, allotment));
1388
+ if (Result.isSuccess(read)) return more(read.success);
1389
+ if (isSliceRefused(read.failure)) return waiting(read.failure.message);
1390
+ return yield* Effect.fail(read.failure);
1391
+ }
1392
+ return more((yield* bitmapsStep(parts, listed, data, input, allotment)) ?? started(listed));
1393
+ }).pipe(Effect.scoped);
1394
+ const decodeDataOption = Schema.decodeUnknownOption(IndexData);
1395
+ /**
1396
+ * The most the graph step holds in a `packs()` instance of `memory`: half what it allots, about
1397
+ * 48 MB in a Worker's 128 MB, at 16 bytes a commit about 3 M commits.
1398
+ */
1399
+ const graphMostOf = (memory) => Math.floor(allottableOf(memory) / 2);
1400
+ /**
1401
+ * `GitObjects.index`'s try over `indexStep`: its data and row decoded, and the row it ends with as
1402
+ * the `index`/`current` row to commit. An unreadable row or data, such as one of a shape a later
1403
+ * change wrote, is as none, as `indexStatus` reads it: the try starts a new build, and prune
1404
+ * deletes the old one's files once the grace has passed since they were written.
1405
+ */
1406
+ const indexTry = (parts, limits, listed, input) => Effect.gen(function* () {
1407
+ const step = {
1408
+ tips: [...new Set(input.tips.map((tip) => tip.oid))],
1409
+ at: input.at,
1410
+ grace: limits.grace
1411
+ };
1412
+ if (input.round !== void 0) step.round = input.round;
1413
+ const data = Option.getOrUndefined(decodeDataOption(input.data));
1414
+ if (data !== void 0) step.data = data;
1415
+ const row = Option.getOrUndefined(decodeRowOption(input.row));
1416
+ if (row !== void 0) step.row = row;
1417
+ if (input.deleted !== void 0) step.deleted = input.deleted;
1418
+ if (input.failed !== void 0) step.failed = input.failed;
1419
+ const ended = yield* indexStep({
1420
+ ...parts,
1421
+ graphMost: limits.graphMost
1422
+ }, listed, step);
1423
+ if (ended.kind !== "done") return ended;
1424
+ return {
1425
+ kind: "done",
1426
+ row: {
1427
+ ...indexRowKey,
1428
+ value: ended.row
1429
+ },
1430
+ data: ended.data
1431
+ };
1432
+ });
1433
+ export { BuiltIndex, IndexData, IndexGroup, IndexLayer, IndexRefusal, IndexRow, IndexSlice, IndexTries, Narrowed, RefusedIndex, ReplacedBuild, belowGraphs, builtIndexOf, chooseCommits, copyMergedBitmaps, coveredPacks, coversAll, decodeIndexValue, edgesMost, floors, graphMostOf, indexReadCallsAfter, indexRowOf, indexSaves, indexStatus, indexStep, indexSteps, indexTry, madeMost, mergeCallsAfterOpen, mergeChunk, mergeHeldMost, narrowedAfter, newBuildId, positionOrder, pruneBuilds, putOnce, rowsOfLayer, sliceBounds, storedTypesOf, triesWait, triesWaitMost, triesWaitOf, wordsOf };