@ref0/git 0.11.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (262) hide show
  1. package/dist/access/private-refs.mjs +8 -7
  2. package/dist/auth/auth.d.mts +2 -2
  3. package/dist/auth/auth.mjs +3 -6
  4. package/dist/auth/authorize.mjs +1 -17
  5. package/dist/auth/helpers.mjs +3 -11
  6. package/dist/auth/host-services.mjs +11 -0
  7. package/dist/auth/policy.d.mts +27 -7
  8. package/dist/auth/policy.mjs +8 -4
  9. package/dist/auth/tokens-layer.d.mts +1 -2
  10. package/dist/auth/tokens-layer.mjs +2 -3
  11. package/dist/auth/tokens-public.d.mts +3 -3
  12. package/dist/auth/tokens.d.mts +1 -8
  13. package/dist/auth/tokens.mjs +1 -8
  14. package/dist/auth.d.mts +1 -4
  15. package/dist/auth.mjs +1 -3
  16. package/dist/client/git-http.mjs +18 -4
  17. package/dist/client/push-upload.mjs +33 -7
  18. package/dist/client/remote.mjs +94 -28
  19. package/dist/client/slice-push.d.mts +1 -1
  20. package/dist/contracts/access.d.mts +8 -8
  21. package/dist/contracts/actors.d.mts +2 -34
  22. package/dist/contracts/blobs.d.mts +2 -2
  23. package/dist/contracts/client.d.mts +8 -3
  24. package/dist/contracts/commits.d.mts +10 -2
  25. package/dist/contracts/commits.mjs +9 -1
  26. package/dist/contracts/files.d.mts +2 -2
  27. package/dist/contracts/forks.d.mts +14 -3
  28. package/dist/contracts/git.d.mts +41 -12
  29. package/dist/contracts/git.mjs +3 -2
  30. package/dist/contracts/lfs.d.mts +0 -7
  31. package/dist/contracts/objects.d.mts +80 -10
  32. package/dist/contracts/operations.d.mts +3 -0
  33. package/dist/contracts/operations.mjs +12 -2
  34. package/dist/contracts/push-uploads.d.mts +44 -5
  35. package/dist/contracts/push-uploads.mjs +7 -3
  36. package/dist/contracts/refs.d.mts +28 -6
  37. package/dist/contracts/remote.mjs +1 -3
  38. package/dist/contracts/repo-records.d.mts +1 -0
  39. package/dist/contracts/repos.d.mts +90 -2
  40. package/dist/contracts/repos.mjs +29 -2
  41. package/dist/contracts/result.d.mts +30 -23
  42. package/dist/contracts/result.mjs +24 -9
  43. package/dist/contracts/rules.d.mts +22 -22
  44. package/dist/contracts/server.d.mts +28 -77
  45. package/dist/contracts/signin.d.mts +1 -1
  46. package/dist/contracts/sync.d.mts +90 -19
  47. package/dist/contracts/sync.mjs +34 -10
  48. package/dist/contracts/tokens.d.mts +1 -6
  49. package/dist/contracts/tokens.mjs +1 -2
  50. package/dist/contracts/topics.d.mts +0 -5
  51. package/dist/contracts/work.d.mts +66 -2
  52. package/dist/contracts/work.mjs +8 -1
  53. package/dist/core/access.d.mts +1 -0
  54. package/dist/core/access.mjs +34 -13
  55. package/dist/core/ancestry.d.mts +9 -0
  56. package/dist/core/ancestry.mjs +6 -6
  57. package/dist/core/asked.mjs +68 -0
  58. package/dist/core/call-names.mjs +1 -0
  59. package/dist/core/call-services.mjs +2 -1
  60. package/dist/core/events/deliver.d.mts +1 -0
  61. package/dist/core/events/deliver.mjs +15 -3
  62. package/dist/core/forks.mjs +42 -16
  63. package/dist/core/imports.mjs +156 -13
  64. package/dist/core/index-job.mjs +9 -6
  65. package/dist/core/job-view.mjs +8 -9
  66. package/dist/core/mirror-rows.mjs +19 -4
  67. package/dist/core/objects/commits.mjs +16 -6
  68. package/dist/core/objects/reading.mjs +17 -9
  69. package/dist/core/parts.d.mts +8 -2
  70. package/dist/core/parts.mjs +1 -4
  71. package/dist/core/purge.mjs +76 -6
  72. package/dist/core/push-decide.mjs +1 -1
  73. package/dist/core/push-files.d.mts +12 -0
  74. package/dist/core/push-files.mjs +225 -0
  75. package/dist/core/push-repo.d.mts +8 -0
  76. package/dist/core/push-repo.mjs +56 -0
  77. package/dist/core/push.d.mts +17 -0
  78. package/dist/core/push.mjs +107 -32
  79. package/dist/core/receipts.mjs +26 -10
  80. package/dist/core/ref-view.mjs +59 -5
  81. package/dist/core/refs.mjs +18 -10
  82. package/dist/core/registered-rows.mjs +7 -5
  83. package/dist/core/repack.mjs +16 -11
  84. package/dist/core/repo-info.mjs +16 -9
  85. package/dist/core/repos.mjs +30 -14
  86. package/dist/core/saved-caller-views.d.mts +18 -0
  87. package/dist/core/saved-caller-views.mjs +17 -4
  88. package/dist/core/saved-callers-public.d.mts +3 -3
  89. package/dist/core/saved-callers-public.mjs +3 -2
  90. package/dist/core/saved-callers.d.mts +7 -0
  91. package/dist/core/seam.mjs +2 -2
  92. package/dist/core/view.d.mts +6 -0
  93. package/dist/core/view.mjs +45 -12
  94. package/dist/core/work.mjs +47 -22
  95. package/dist/effect.d.mts +3 -1
  96. package/dist/effect.mjs +3 -1
  97. package/dist/engine/reads.mjs +13 -6
  98. package/dist/engine/walk.mjs +4 -4
  99. package/dist/event-streams/event-streams.d.mts +1 -1
  100. package/dist/event-streams/event-streams.mjs +1 -1
  101. package/dist/event-streams/stream.d.mts +1 -1
  102. package/dist/event-streams/stream.mjs +1 -1
  103. package/dist/extend.d.mts +4 -3
  104. package/dist/extend.mjs +3 -2
  105. package/dist/http/answers.mjs +4 -2
  106. package/dist/http/core-parts.mjs +64 -15
  107. package/dist/http/handler.mjs +2 -2
  108. package/dist/http/node.mjs +5 -4
  109. package/dist/http/parts.d.mts +1 -0
  110. package/dist/http/push-upload-job.mjs +503 -0
  111. package/dist/http/push-uploads.d.mts +1 -0
  112. package/dist/http/push-uploads.mjs +120 -59
  113. package/dist/http/upload-pack.mjs +7 -6
  114. package/dist/index.d.mts +23 -24
  115. package/dist/index.mjs +4 -7
  116. package/dist/lfs/base.d.mts +7 -3
  117. package/dist/lfs/base.mjs +2 -1
  118. package/dist/lfs/copy.d.mts +46 -0
  119. package/dist/lfs/copy.mjs +26 -0
  120. package/dist/lfs/layer.mjs +4 -2
  121. package/dist/lfs/locks.mjs +14 -9
  122. package/dist/lfs/objects.d.mts +6 -0
  123. package/dist/lfs/objects.mjs +95 -61
  124. package/dist/lfs/rows.d.mts +13 -0
  125. package/dist/lfs/service.d.mts +1 -7
  126. package/dist/lfs/service.mjs +1 -9
  127. package/dist/lfs/upload-most.mjs +5 -5
  128. package/dist/node.d.mts +2 -2
  129. package/dist/protocol/bytes/sha256-hasher.d.mts +15 -0
  130. package/dist/protocol/bytes/sha256.d.mts +32 -0
  131. package/dist/protocol/bytes/sha256.mjs +105 -49
  132. package/dist/protocol/wire/advertisement.mjs +9 -4
  133. package/dist/protocol/wire/lines.d.mts +4 -2
  134. package/dist/protocol/wire/lines.mjs +11 -5
  135. package/dist/protocol/wire/push-uploads.mjs +13 -1
  136. package/dist/protocol/wire/v2.mjs +2 -2
  137. package/dist/protocol.d.mts +3 -1
  138. package/dist/protocol.mjs +3 -1
  139. package/dist/queue/advance.mjs +1 -1
  140. package/dist/queue/calls.mjs +21 -1
  141. package/dist/queue/host.d.mts +1 -7
  142. package/dist/queue/host.mjs +1 -7
  143. package/dist/queue/layer.mjs +1 -1
  144. package/dist/queue/trim.mjs +1 -1
  145. package/dist/rest/blobs.d.mts +16 -8
  146. package/dist/rest/blobs.mjs +1 -1
  147. package/dist/rest/caller.d.mts +49 -9
  148. package/dist/rest/caller.mjs +1 -1
  149. package/dist/rest/client.d.mts +10 -3
  150. package/dist/rest/client.mjs +4 -0
  151. package/dist/rest/commits.d.mts +272 -239
  152. package/dist/rest/errors.d.mts +24 -4
  153. package/dist/rest/events.d.mts +9 -6
  154. package/dist/rest/events.mjs +1 -2
  155. package/dist/rest/files.d.mts +112 -100
  156. package/dist/rest/forks.d.mts +146 -122
  157. package/dist/rest/handler.mjs +4 -57
  158. package/dist/rest/handlers.d.mts +1 -1
  159. package/dist/rest/handlers.mjs +18 -4
  160. package/dist/rest/layer.mjs +10 -2
  161. package/dist/rest/me.d.mts +8 -4
  162. package/dist/rest/merges.d.mts +8 -4
  163. package/dist/rest/paths.mjs +1 -0
  164. package/dist/rest/queue.d.mts +292 -236
  165. package/dist/rest/refs.d.mts +98 -69
  166. package/dist/rest/repos.d.mts +133 -105
  167. package/dist/rest/tags.d.mts +100 -88
  168. package/dist/rest/tokens.d.mts +192 -168
  169. package/dist/rest/topics.d.mts +154 -134
  170. package/dist/rest/trees.d.mts +32 -16
  171. package/dist/rest/work.d.mts +467 -20
  172. package/dist/rest/work.mjs +8 -0
  173. package/dist/server/edge-node.d.mts +9 -13
  174. package/dist/server/edge-node.mjs +9 -12
  175. package/dist/server/git-layer.d.mts +1 -6
  176. package/dist/server/git-layer.mjs +2 -2
  177. package/dist/server/git-namespace.d.mts +4 -4
  178. package/dist/server/joined.d.mts +15 -22
  179. package/dist/server/joined.mjs +5 -29
  180. package/dist/server/lifecycle.mjs +107 -23
  181. package/dist/server/registry.mjs +9 -1
  182. package/dist/server/server.mjs +139 -216
  183. package/dist/storage/budget.mjs +2 -2
  184. package/dist/storage/clean-up.mjs +13 -10
  185. package/dist/storage/fetch.mjs +13 -8
  186. package/dist/storage/file-limits.mjs +24 -0
  187. package/dist/storage/index-build.d.mts +1 -0
  188. package/dist/storage/index-build.mjs +106 -62
  189. package/dist/storage/index-fetch.mjs +16 -3
  190. package/dist/storage/index-reach.mjs +33 -5
  191. package/dist/storage/index-remap.mjs +8 -1
  192. package/dist/storage/index-shallow.mjs +5 -3
  193. package/dist/storage/largest-files.mjs +48 -0
  194. package/dist/storage/lfs-pointers.mjs +10 -1
  195. package/dist/storage/loose.mjs +3 -1
  196. package/dist/storage/memory-budget.mjs +154 -8
  197. package/dist/storage/merge-cut.mjs +5 -4
  198. package/dist/storage/pack-reads.mjs +246 -34
  199. package/dist/storage/pack-receive.mjs +83 -25
  200. package/dist/storage/packs.mjs +37 -21
  201. package/dist/storage/prune-keep.mjs +203 -34
  202. package/dist/storage/prune-seen.mjs +11 -6
  203. package/dist/storage/prune-walk.mjs +219 -81
  204. package/dist/storage/prune.mjs +629 -196
  205. package/dist/storage/push-uploads.mjs +36 -12
  206. package/dist/storage/quarantine.mjs +9 -7
  207. package/dist/storage/ready.mjs +47 -19
  208. package/dist/storage/receive-bounds.mjs +29 -8
  209. package/dist/storage/receive-state.mjs +8 -0
  210. package/dist/storage/receive-steps.mjs +71 -18
  211. package/dist/storage/repack.d.mts +1 -1
  212. package/dist/storage/repack.mjs +1 -8
  213. package/dist/storage/rows.mjs +12 -1
  214. package/dist/storage/shallow.d.mts +1 -0
  215. package/dist/storage/shallow.mjs +26 -4
  216. package/dist/storage/stream-whole.mjs +5 -2
  217. package/dist/storage/visible.mjs +29 -1
  218. package/dist/storage/walk.mjs +2 -1
  219. package/dist/store/catalogue.mjs +1 -0
  220. package/dist/store/cursor-seal-namespace.d.mts +2 -0
  221. package/dist/store/cursor-seal-public.d.mts +5 -0
  222. package/dist/store/cursor-seal-public.mjs +5 -0
  223. package/dist/store/cursor-seal.d.mts +16 -2
  224. package/dist/store/cursor-seal.mjs +26 -14
  225. package/dist/store/errors.mjs +9 -1
  226. package/dist/store/records.d.mts +2 -0
  227. package/dist/store/records.mjs +9 -6
  228. package/dist/store/repo-record.d.mts +27 -0
  229. package/dist/store/repo-record.mjs +141 -4
  230. package/dist/store/scopes.mjs +10 -2
  231. package/dist/sync/bound.d.mts +2 -0
  232. package/dist/sync/bound.mjs +11 -2
  233. package/dist/sync/github.d.mts +6 -1
  234. package/dist/sync/github.mjs +63 -42
  235. package/dist/sync/http.mjs +36 -1
  236. package/dist/sync/import.mjs +4 -2
  237. package/dist/sync/layer.mjs +6 -2
  238. package/dist/sync/lfs-batch.mjs +119 -0
  239. package/dist/sync/lfs-copy.mjs +251 -0
  240. package/dist/sync/mirror.mjs +227 -147
  241. package/dist/sync/said.mjs +204 -0
  242. package/dist/sync/smart-http.mjs +4 -2
  243. package/dist/sync/step.mjs +33 -13
  244. package/dist/sync/tries.mjs +71 -24
  245. package/dist/sync.d.mts +2 -2
  246. package/dist/topics/advance.mjs +10 -3
  247. package/dist/topics/host.mjs +1 -19
  248. package/dist/topics/layer.mjs +1 -1
  249. package/package.json +3 -8
  250. package/dist/auth/bridge.mjs +0 -30
  251. package/dist/auth/helpers.d.mts +0 -17
  252. package/dist/core/purge.d.mts +0 -8
  253. package/dist/core/registered-rows.d.mts +0 -4
  254. package/dist/core/repack.d.mts +0 -11
  255. package/dist/http/handler.d.mts +0 -2
  256. package/dist/rest/handler.d.mts +0 -21
  257. package/dist/rest.d.mts +0 -2
  258. package/dist/rest.mjs +0 -1
  259. package/dist/server/registry.d.mts +0 -16
  260. package/dist/server/server.d.mts +0 -36
  261. package/dist/storage/repack-job.d.mts +0 -3
  262. package/dist/topics/host.d.mts +0 -7
@@ -0,0 +1,503 @@
1
+ import { spentIn } from "../contracts/calls-spent.mjs";
2
+ import { gitError, isGitError } from "../contracts/result.mjs";
3
+ import { contended, keyedRecord, openRepo } from "../core/access.mjs";
4
+ import { CoreParts, ObjectsParts, overStore } from "../core/parts.mjs";
5
+ import { KeptError, KeptUpdate, heldError, heldUpdates, keptError, keptUpdates, refsAnswer } from "../core/receipts.mjs";
6
+ import { SavedCaller, askAgain, savedCallerOf } from "../core/saved-callers.mjs";
7
+ import { SealedObjects } from "../core/view.mjs";
8
+ import { bucketStream, effectOf, scopedReceiveStep } from "../storage/calls.mjs";
9
+ import { isPackId } from "../storage/layout.mjs";
10
+ import { isShallowRoot } from "../storage/links.mjs";
11
+ import { PushUploadRow, claimLapsed, completingMost, endUpload, pushUploadRead, pushUploadRowKey } from "../storage/push-uploads.mjs";
12
+ import { importTryTimeout, pushStepBytesOf } from "../storage/receive-bounds.mjs";
13
+ import { hostRecordOf } from "../store/catalogue.mjs";
14
+ import { bestEffort, decodeRow, storeCall } from "../store/errors.mjs";
15
+ import { decodePacks, gitStore } from "../store/git-store.mjs";
16
+ import { currentRepo, gitJob } from "../store/job-try.mjs";
17
+ import { keptKey } from "../store/records.mjs";
18
+ import { WholeRepo } from "../store/ref-rows.mjs";
19
+ import { collections, newChangeId, repoScope } from "../store/scopes.mjs";
20
+ import * as Clock$1 from "effect/Clock";
21
+ import * as Effect from "effect/Effect";
22
+ import * as Schema from "effect/Schema";
23
+ import * as Result from "effect/Result";
24
+ import { CurrentJob, Job } from "@ref0/infra";
25
+ // src/http/push-upload-job.ts
26
+ /**
27
+ * `git.push-upload`, a completed push upload's receive and landing (#1931), in the repo's scope, one
28
+ * step a try: its first try stages the uploaded pack, each next one receives a share of it, and the
29
+ * one that receives the last lands it as a push of the upload's refs, as the caller who completed
30
+ * it, asked again each try. The upload ends `ended` with its answer, which complete's report reads.
31
+ */
32
+ const pushUploadJobName = "git.push-upload";
33
+ /** The upload a try runs: its handle, which is also its row's key in each collection. */
34
+ const PushUploadJobData = /* @__PURE__ */ Schema.Struct({ upload: Schema.String });
35
+ /**
36
+ * A receiving upload's steps: who completed it, its landing's key as its receipt keeps it, the
37
+ * objects its pack's header counts, the receive's steps that paused, which complete's 202 tells,
38
+ * and once the first try staged it, its pack's ID. `restaged` once clean-up ended a stage and a
39
+ * try staged the pack again: a second loss ends the upload.
40
+ */
41
+ const PushUploadSteps = /* @__PURE__ */ Schema.Struct({
42
+ caller: SavedCaller,
43
+ key: Schema.String,
44
+ objects: Schema.Int,
45
+ step: Schema.Int,
46
+ pack: Schema.optionalKey(Schema.String),
47
+ restaged: Schema.optionalKey(Schema.Literal(true))
48
+ });
49
+ /**
50
+ * An ended upload's answer, as complete's report tells it: each ref's result and the lines for band
51
+ * 2, the pack's own receive refused, as Git's `unpack` failing, or the push refused whole.
52
+ */
53
+ const PushUploadAnswer = /* @__PURE__ */ Schema.Union([
54
+ Schema.Struct({
55
+ kind: Schema.Literal("updated"),
56
+ updates: Schema.Array(KeptUpdate),
57
+ notes: Schema.Array(Schema.String)
58
+ }),
59
+ Schema.Struct({
60
+ kind: Schema.Literal("unpack-failed"),
61
+ error: KeptError
62
+ }),
63
+ Schema.Struct({
64
+ kind: Schema.Literal("failed"),
65
+ error: KeptError
66
+ })
67
+ ]);
68
+ /** What a push's landing answered, as an upload's answer keeps it. */
69
+ const answerOf = (pushed) => {
70
+ if (Result.isFailure(pushed)) return {
71
+ kind: "failed",
72
+ error: keptError(pushed.failure)
73
+ };
74
+ const { success } = pushed;
75
+ if (success.kind === "unpack-failed") return {
76
+ kind: "unpack-failed",
77
+ error: keptError(success.error)
78
+ };
79
+ return {
80
+ kind: "updated",
81
+ updates: keptUpdates(success.updated),
82
+ notes: success.notes ?? []
83
+ };
84
+ };
85
+ /** An upload's answer as the push it lands as answered: what complete's report reads. */
86
+ const pushedOf = (answer) => {
87
+ if (answer.kind === "failed") return Result.fail(heldError(answer.error));
88
+ if (answer.kind === "unpack-failed") return Result.succeed({
89
+ kind: "unpack-failed",
90
+ error: heldError(answer.error)
91
+ });
92
+ const pushed = {
93
+ kind: "updated",
94
+ updated: heldUpdates(answer.updates)
95
+ };
96
+ if (answer.notes.length > 0) pushed.notes = answer.notes;
97
+ return Result.succeed(pushed);
98
+ };
99
+ const stepsKey = (upload) => ({
100
+ collection: collections.pushUploadSteps,
101
+ id: upload
102
+ });
103
+ const answerKey = (upload) => ({
104
+ collection: collections.pushUploadAnswers,
105
+ id: upload
106
+ });
107
+ /** The upload's answer as its row: what an end that refuses the upload before its steps puts. */
108
+ const answerRow = (upload, answer) => ({
109
+ ...answerKey(upload),
110
+ value: answer
111
+ });
112
+ /**
113
+ * Tries as an import's: one under `importTryTimeout`, which bounds when a step's keys may last be
114
+ * put, as `receiveStepGrace` says. A failed try backs off, from a second to a minute.
115
+ */
116
+ const PushUploadJob = /* @__PURE__ */ Job.make(pushUploadJobName, {
117
+ payload: PushUploadJobData,
118
+ timeout: importTryTimeout,
119
+ retry: {
120
+ limit: 8,
121
+ delay: "1 second",
122
+ backoff: "exponential",
123
+ maxDelay: "1 minute"
124
+ }
125
+ });
126
+ /**
127
+ * A try reads the repo's packs whole: its steps receive the upload's pack against every pack the
128
+ * repo holds, as the push it lands as does.
129
+ */
130
+ const uploadWhole = /* @__PURE__ */ WholeRepo.mint();
131
+ const more = { kind: "more" };
132
+ /** A failure the next try may mend: a busy store or bucket, or a try's calls spent. */
133
+ const passing = (error) => error.code === "UNAVAILABLE" || spentIn(error) !== void 0;
134
+ /** The upload's three rows, and the version they were read at: what complete answers from. */
135
+ const rowsOf = /* @__PURE__ */ Effect.fnUntraced(function* (repo, upload) {
136
+ const { store } = yield* CoreParts;
137
+ const snapshot = yield* storeCall(() => store.read(repoScope(repo), [
138
+ pushUploadRead(upload),
139
+ stepsKey(upload),
140
+ answerKey(upload)
141
+ ]));
142
+ const [[own] = [], [steps] = [], [answer] = []] = snapshot.results;
143
+ return {
144
+ version: snapshot.version,
145
+ row: own === void 0 ? void 0 : yield* decodeRow(PushUploadRow, own, `push upload ${upload}`),
146
+ steps: steps === void 0 ? void 0 : yield* decodeRow(PushUploadSteps, steps, `push upload ${upload}'s steps`),
147
+ answer: answer === void 0 ? void 0 : yield* decodeRow(PushUploadAnswer, answer, `push upload ${upload}'s answer`)
148
+ };
149
+ });
150
+ /**
151
+ * The upload ended with `answer`: its parts aborted and its key deleted, then its row `ended`, its
152
+ * answer put and its steps deleted, in one commit at the version its rows were read. An upload
153
+ * upkeep deleted, or one another try ended, is left as it is. A bucket that refuses the end leaves
154
+ * the key to upkeep, at the row's `removeAt`.
155
+ */
156
+ const ended = /* @__PURE__ */ Effect.fnUntraced(function* (parts, repo, upload, answer) {
157
+ const { store } = yield* CoreParts;
158
+ for (let tries = 0; tries < 5; tries += 1) {
159
+ const read = yield* rowsOf(repo, upload);
160
+ const { row } = read;
161
+ if (row === void 0 || row.state !== "receiving" || read.answer !== void 0) return;
162
+ yield* bestEffort(endUpload(parts.bucket, parts.uploads, row), (error) => Effect.logWarning("An upload's end was refused: upkeep ends it at its removeAt").pipe(Effect.annotateLogs({
163
+ repo,
164
+ upload,
165
+ key: row.key,
166
+ code: error.code,
167
+ reason: error.message
168
+ })));
169
+ if ((yield* storeCall(() => store.commit(repoScope(repo), {
170
+ id: newChangeId(),
171
+ expect: read.version,
172
+ put: [{
173
+ ...pushUploadRowKey(upload),
174
+ value: {
175
+ ...row,
176
+ state: "ended"
177
+ }
178
+ }, {
179
+ ...answerKey(upload),
180
+ value: answer
181
+ }],
182
+ delete: [stepsKey(upload)]
183
+ }))).ok) return;
184
+ }
185
+ return yield* contended();
186
+ });
187
+ const endedWith = (parts, repo, upload, error) => ended(parts, repo, upload, {
188
+ kind: "failed",
189
+ error: keptError(error)
190
+ });
191
+ /**
192
+ * The upload's steps moved from `was` to `next`, in the store as it holds it now: the stage and the
193
+ * step write the receive's rows in the repo's scope since the try's read. `false` when another
194
+ * write ended the upload or moved its steps.
195
+ */
196
+ const stepsSaved = /* @__PURE__ */ Effect.fnUntraced(function* (at, next) {
197
+ const { store } = yield* CoreParts;
198
+ const now = yield* rowsOf(at.internalId, at.upload);
199
+ const was = at.steps;
200
+ if (!(now.row?.state === "receiving" && now.steps?.pack === was.pack && now.steps?.restaged === was.restaged)) return false;
201
+ return (yield* storeCall(() => store.commit(repoScope(at.internalId), {
202
+ id: newChangeId(),
203
+ expect: now.version,
204
+ put: [{
205
+ ...stepsKey(at.upload),
206
+ value: next
207
+ }]
208
+ }))).ok;
209
+ });
210
+ const stepsRaced = () => gitError("UNAVAILABLE", "Another write moved the push upload's rows while its try ran", { hint: "The job tries again" });
211
+ const notStepped = () => gitError("FAILED_PRECONDITION", "The host's objects don't receive in steps", {
212
+ hint: "Push again",
213
+ hostHint: "Give Git packs() objects, whose receive steps, to take push uploads"
214
+ });
215
+ /** A stage clean-up ended twice: its steps wait out the quarantine's `prune` each time. */
216
+ const stageGoneAgain = () => gitError("FAILED_PRECONDITION", "Clean-up ended the push upload's staged pack twice before its steps received it", {
217
+ hint: "Push again",
218
+ hostHint: "Give Objects.layer({ prune }) a prune longer than a push upload's tries",
219
+ context: { reason: "stage-gone" }
220
+ });
221
+ const notAPack = () => gitError("INVALID_ARGUMENT", "the push's pack is missing or isn't a pack");
222
+ const repoGone = () => gitError("NOT_FOUND", "The push upload's repo is gone");
223
+ /** A host whose tries' CPU can't carry `pushStepLeast`: no step of the pack would fit in one. */
224
+ const cpuTooSmall = (cpuMs) => gitError("FAILED_PRECONDITION", `This host gives a request ${cpuMs} ms of CPU, under the 500 ms a push upload's step needs`, {
225
+ hint: "Push to a host that gives more CPU: this one can't take a push upload",
226
+ hostHint: `Give requests at least 500 ms of CPU, as requestLimits.cpuMs says`
227
+ });
228
+ const unsteppedPack = (pack) => gitError("INTERNAL", `The host's objects staged ${pack}, which is no pack ID`, { hint: "Report it" });
229
+ /** The repo by the name its record holds now, opened for `caller` to push, with its packs. */
230
+ const openedFor = /* @__PURE__ */ Effect.fnUntraced(function* (caller, internalId) {
231
+ const { store } = yield* CoreParts;
232
+ const record = yield* hostRecordOf(store, internalId);
233
+ if (record === void 0) return void 0;
234
+ const operation = {
235
+ kind: "push",
236
+ repo: record.name,
237
+ updates: []
238
+ };
239
+ const resolved = yield* openRepo(caller, operation, [packsRead], internalId);
240
+ if (resolved.internalId !== internalId) return void 0;
241
+ const packs = yield* decodePacks(resolved.snapshot.results[1] ?? []);
242
+ const listed = {
243
+ repo: internalId,
244
+ version: resolved.snapshot.version,
245
+ packs
246
+ };
247
+ return {
248
+ name: record.name,
249
+ listed
250
+ };
251
+ });
252
+ const packsRead = { collection: collections.packs };
253
+ /** The landing's write key, the upload's own. */
254
+ const landingKey = (upload) => `push-upload/${upload}`;
255
+ /** What the landing's key is kept with: the upload it lands. */
256
+ const landingInput = (upload) => ["push-upload", upload];
257
+ /** `landingInput` as a receipt keeps it, as `keptKey` writes it. */
258
+ const keptInput = (upload) => JSON.stringify(landingInput(upload));
259
+ /**
260
+ * The landing's receipt, once a try landed the upload, as its answer: its refs' results, with no
261
+ * lines for band 2. Else its key and lookup, for the landing to keep, held to this repo (Q705). It's
262
+ * the upload's own record of its push, read for its report, so it's read before the caller is asked
263
+ * again: a landing whose end was lost answers as it landed, even once the caller's token ends.
264
+ */
265
+ const receiptOf = /* @__PURE__ */ Effect.fnUntraced(function* (name, at) {
266
+ const { store } = yield* CoreParts;
267
+ const kept = {
268
+ key: at.steps.key,
269
+ input: keptInput(at.upload),
270
+ at: yield* Clock$1.currentTimeMillis
271
+ };
272
+ const keyed = yield* keyedRecord(store, name, kept, () => Effect.void, at.internalId);
273
+ if (keyed === void 0) return yield* Effect.die(noKey);
274
+ if (keyed.kind === "answered") {
275
+ const updated = yield* refsAnswer(keyed.receipt);
276
+ return {
277
+ kind: "answered",
278
+ answer: {
279
+ kind: "updated",
280
+ updates: keptUpdates(updated),
281
+ notes: []
282
+ }
283
+ };
284
+ }
285
+ return {
286
+ kind: "missed",
287
+ key: {
288
+ key: kept,
289
+ missed: keyed
290
+ }
291
+ };
292
+ });
293
+ const noKey = /* @__PURE__ */ new TypeError("A push upload's landing has a key");
294
+ /**
295
+ * The upload's answer once a try landed it, from its receipt, for a try that reads it again or the
296
+ * job's last failure: `undefined` before its pack is staged, or while it hasn't landed.
297
+ */
298
+ const landedAnswer = /* @__PURE__ */ Effect.fnUntraced(function* (at) {
299
+ if (at.steps.pack === void 0) return void 0;
300
+ const { store } = yield* CoreParts;
301
+ const record = yield* hostRecordOf(store, at.internalId);
302
+ if (record === void 0) return void 0;
303
+ return yield* receiptOf(record.name, at);
304
+ });
305
+ /**
306
+ * The upload's first step: its pack staged from the bucket, its shallow lines with it, and its
307
+ * steps row given the pack's ID, at the version the rows were read. A row another write moved
308
+ * leaves the staged pack to the quarantine's clean-up.
309
+ */
310
+ const staged = /* @__PURE__ */ Effect.fnUntraced(function* (parts, stage, listed, at, signal) {
311
+ const { row, steps } = at;
312
+ const pack = yield* bucketStream(parts.bucket, row.key);
313
+ if (pack === void 0) return yield* Effect.fail(notAPack());
314
+ const input = {
315
+ pack,
316
+ objects: steps.objects,
317
+ length: row.bytes,
318
+ signal
319
+ };
320
+ if (row.shallow.length > 0) input.shallow = row.shallow;
321
+ const stagedPack = yield* Effect.result(effectOf(stage)(listed, input));
322
+ if (Result.isFailure(stagedPack)) return yield* unpackFailed(parts, at, stagedPack.failure);
323
+ if (!(yield* stepsSaved(at, {
324
+ ...steps,
325
+ pack: stagedPack.success.pack
326
+ }))) return yield* Effect.fail(stepsRaced());
327
+ return more;
328
+ });
329
+ /**
330
+ * A refusal of the pack's receive: one the next try may mend fails this one, and any other ends
331
+ * the upload as Git's `unpack` failing.
332
+ */
333
+ const unpackFailed = (parts, at, failure) => passing(failure) ? Effect.fail(failure) : ended(parts, at.internalId, at.upload, {
334
+ kind: "unpack-failed",
335
+ error: keptError(failure)
336
+ });
337
+ /**
338
+ * The pack, received, or refused for a shallow root, landed as a push of the upload's refs, as
339
+ * `caller`, under the upload's key; then the upload ended with what it answered. A refusal the next
340
+ * try may mend fails this one, and the receipt answers that try as this landing did.
341
+ */
342
+ const landed = /* @__PURE__ */ Effect.fnUntraced(function* (parts, caller, name, at, stepped, key, signal) {
343
+ const { row } = at;
344
+ const receiving = yield* parts.http().receive(caller, name);
345
+ const input = {
346
+ edits: row.updates,
347
+ atomic: row.atomic,
348
+ pushOptions: row.pushOptions,
349
+ stepped,
350
+ key,
351
+ signal
352
+ };
353
+ if (row.shallow.length > 0) input.shallow = row.shallow;
354
+ const pushed = yield* Effect.result(receiving.push(input));
355
+ if (Result.isFailure(pushed) && passing(pushed.failure)) return yield* Effect.fail(pushed.failure);
356
+ return yield* ended(parts, at.internalId, at.upload, answerOf(pushed));
357
+ });
358
+ /**
359
+ * A staged upload's next step: one share of its receive, which pauses for the next try, or ends
360
+ * with the pack received, and lands it. Clean-up that ended the stage between steps stages it
361
+ * again from the bucket, once. A refusal of the pack's own ends the upload as Git's `unpack`
362
+ * failing, but a shallow root's, which lands as a push that receives no pack.
363
+ */
364
+ const stepped = /* @__PURE__ */ Effect.fnUntraced(function* (parts, receiveStep, caller, opened, at, pack, key, signal, share) {
365
+ const input = {
366
+ store: gitStore((yield* CoreParts).store),
367
+ pack,
368
+ objects: at.steps.objects,
369
+ share,
370
+ largest: true
371
+ };
372
+ const step = yield* Effect.result(scopedReceiveStep(receiveStep, opened.listed, input));
373
+ if (Result.isFailure(step)) {
374
+ const { failure } = step;
375
+ if (failure.context?.reason === "stage-gone") {
376
+ if (at.steps.restaged === true) return yield* Effect.fail(stageGoneAgain());
377
+ const { pack: _pack, ...steps } = at.steps;
378
+ if (!(yield* stepsSaved(at, {
379
+ ...steps,
380
+ restaged: true
381
+ }))) return yield* Effect.fail(stepsRaced());
382
+ return more;
383
+ }
384
+ if (isShallowRoot(failure)) return yield* landed(parts, caller, opened.name, at, { rooted: failure }, key, signal);
385
+ return yield* unpackFailed(parts, at, failure);
386
+ }
387
+ const answer = step.success;
388
+ if ("paused" in answer) {
389
+ yield* stepsSaved(at, {
390
+ ...at.steps,
391
+ step: at.steps.step + 1
392
+ });
393
+ return more;
394
+ }
395
+ const received = { received: answer.received };
396
+ return yield* landed(parts, caller, opened.name, at, received, key, signal);
397
+ });
398
+ /** One try's step of `upload`, in the repo `internalId`: `more` while it receives. */
399
+ const tryStep = /* @__PURE__ */ Effect.fnUntraced(function* (parts, internalId, upload, signal) {
400
+ const { row, steps } = yield* rowsOf(internalId, upload);
401
+ if (row === void 0 || row.state !== "receiving" || steps === void 0) return;
402
+ const cpuMs = parts.cpuMs ?? 3e4;
403
+ const share = pushStepBytesOf(cpuMs);
404
+ if (share < 1048576) return yield* Effect.fail(cpuTooSmall(cpuMs));
405
+ const at = {
406
+ internalId,
407
+ upload,
408
+ row,
409
+ steps
410
+ };
411
+ const landing = yield* landedAnswer(at);
412
+ if (landing?.kind === "answered") return yield* ended(parts, internalId, upload, landing.answer);
413
+ const asked = yield* askAgain(steps.caller, yield* Clock$1.currentTimeMillis, signal);
414
+ if (asked.kind === "refused") return yield* Effect.fail(asked.error);
415
+ const { caller } = asked;
416
+ const opened = yield* openedFor(caller, internalId);
417
+ if (opened === void 0) return yield* Effect.fail(repoGone());
418
+ const { stage, receiveStep } = SealedObjects.whole((yield* ObjectsParts).objects, uploadWhole);
419
+ if (stage === void 0 || receiveStep === void 0) return yield* Effect.fail(notStepped());
420
+ if (steps.pack === void 0) return yield* staged(parts, stage, opened.listed, at, signal);
421
+ if (!isPackId(steps.pack)) return yield* Effect.fail(unsteppedPack(steps.pack));
422
+ if (landing === void 0) return yield* Effect.fail(repoGone());
423
+ return yield* stepped(parts, receiveStep, caller, opened, at, steps.pack, landing.key, signal, share);
424
+ });
425
+ /**
426
+ * `git.push-upload`'s try over `parts`: a refusal no try mends, as a revoked caller's or a repo
427
+ * gone, ends the upload with it, and the queue's retries take the rest, whose last `onFailure` ends.
428
+ */
429
+ const pushUploadTry = (parts) => Effect.fnUntraced(function* ({ upload }) {
430
+ const context = yield* CurrentJob.CurrentJob;
431
+ const internalId = yield* currentRepo;
432
+ const step = tryStep(parts, internalId, upload, context.signal).pipe(Effect.catch((error) => passing(error) ? Effect.fail(error) : endedWith(parts, internalId, upload, error)), Effect.scoped);
433
+ return yield* Effect.provideContext(overStore(context.store)(step), parts.needs);
434
+ });
435
+ /** The last failed try's error, as the upload's answer keeps it: one the job threw, else its own. */
436
+ const lastError = ({ error }) => isGitError(error) ? error : gitError("UNAVAILABLE", "The push upload's tries failed", { hint: "Push again" });
437
+ /** `git.push-upload`, over `parts`: its tries, and the end its last failure gives the upload. */
438
+ const pushUploadJob = (parts) => gitJob(PushUploadJob, pushUploadTry(parts), { onFailure: Effect.fnUntraced(function* ({ upload }, failure) {
439
+ const context = yield* CurrentJob.CurrentJob;
440
+ const internalId = yield* currentRepo;
441
+ const end = Effect.gen(function* () {
442
+ const { row, steps } = yield* rowsOf(internalId, upload);
443
+ if (row === void 0 || steps === void 0) return;
444
+ const landing = yield* landedAnswer({
445
+ internalId,
446
+ upload,
447
+ row,
448
+ steps
449
+ });
450
+ if (landing?.kind === "answered") return yield* ended(parts, internalId, upload, landing.answer);
451
+ return yield* endedWith(parts, internalId, upload, lastError(failure));
452
+ });
453
+ return yield* Effect.provideContext(overStore(context.store)(end), parts.needs);
454
+ }) });
455
+ /**
456
+ * A completing upload handed to `git.push-upload`'s steps: its row `receiving`, its steps row
457
+ * keeping `caller` as `SavedCaller` does and the `objects` its pack's header counts, and the job,
458
+ * in one commit at the version the row was read, still the claim made at `claimedAt` and before it
459
+ * lapses at `completingMost`. Complete calls it once its parts are joined. A claim that moved on
460
+ * or lapsed is refused as a `Result`, which complete ends the upload with, as it ends one whose
461
+ * parts it refused; a call that fails, fails.
462
+ */
463
+ const startSteps = /* @__PURE__ */ Effect.fnUntraced(function* (internalId, upload, actor, caller, objects, claimedAt) {
464
+ const core = yield* CoreParts;
465
+ const job = core.jobOf?.(pushUploadJobName);
466
+ if (job === void 0) return yield* Effect.fail(notStepped());
467
+ const saved = yield* savedCallerOf(actor, caller);
468
+ const at = yield* Clock$1.currentTimeMillis;
469
+ const kept = yield* keptKey(caller, landingKey(upload), landingInput(upload), at);
470
+ if (kept === void 0) return yield* Effect.die(noKey);
471
+ const steps = {
472
+ caller: saved,
473
+ key: kept.key,
474
+ objects,
475
+ step: 0
476
+ };
477
+ for (let tries = 0; tries < 5; tries += 1) {
478
+ const read = yield* rowsOf(internalId, upload);
479
+ const { row } = read;
480
+ if (row === void 0 || row.actor !== actor || row.state !== "completing" || row.claimedAt !== claimedAt) return Result.fail(gitError("FAILED_PRECONDITION", `The push upload ${upload} has ended, or isn't yours`, { hint: "Push again" }));
481
+ if ((yield* Clock$1.currentTimeMillis) >= (claimedAt ?? 0) + completingMost) return Result.fail(claimLapsed);
482
+ if ((yield* storeCall(() => core.store.commit(repoScope(internalId), {
483
+ id: newChangeId(),
484
+ expect: read.version,
485
+ put: [{
486
+ ...pushUploadRowKey(upload),
487
+ value: {
488
+ ...row,
489
+ state: "receiving"
490
+ }
491
+ }, {
492
+ ...stepsKey(upload),
493
+ value: steps
494
+ }],
495
+ jobs: [job.job({
496
+ key: upload,
497
+ data: { upload }
498
+ })]
499
+ }))).ok) return Result.void;
500
+ }
501
+ return yield* contended();
502
+ });
503
+ export { PushUploadAnswer, PushUploadJobData, PushUploadSteps, answerOf, answerRow, pushUploadJob, pushUploadJobName, pushedOf, rowsOf, startSteps };
@@ -1,3 +1,4 @@
1
+ import "../contracts/authorize.mjs";
1
2
  import "../contracts/actors.mjs";
2
3
  import "../contracts/ids.mjs";
3
4
  import { Bucket, Uploads } from "../contracts/objects.mjs";