@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
@@ -2,6 +2,7 @@ import { currentRepo } from "../store/job-try.mjs";
2
2
  import { fail } from "../contracts/result.mjs";
3
3
  import { contended, headOf, openRepo, repoAccess } from "../core/access.mjs";
4
4
  import { kindsOf } from "../core/ancestry.mjs";
5
+ import { readAsked } from "../core/asked.mjs";
5
6
  import { isRefName } from "../core/names.mjs";
6
7
  import { unseenRef } from "../core/rules/check.mjs";
7
8
  import { refViewIn } from "../core/ref-view.mjs";
@@ -12,16 +13,17 @@ import { hiddenUnder } from "../protocol/wire/lines.mjs";
12
13
  import { receiveRefs, sendPush } from "../client/remote.mjs";
13
14
  import { objectsFace, oneShotReads } from "../storage/calls.mjs";
14
15
  import { decodeRow, storeCall } from "../store/errors.mjs";
15
- import { MirrorRow } from "../core/mirror-rows.mjs";
16
+ import { MirrorRow, destinationText, mirrorJobKey } from "../core/mirror-rows.mjs";
17
+ import { notFailed } from "../core/events/deliver.mjs";
16
18
  import { collections, newChangeId, repoScope } from "../store/scopes.mjs";
17
19
  import { asSync, mirrorLimits } from "./bound.mjs";
18
20
  import { readImport } from "../core/imports.mjs";
19
- import { fromRemote, httpOf, openedFor, remoteOf, syncObjects, tried, unknownProvider } from "./tries.mjs";
20
- import { CoreParts, ObjectsParts, overStore } from "../core/parts.mjs";
21
+ import { fromRemote, httpOf, keptFieldsOf, openedFor, remoteOf, syncObjects, thrownOf, tried, unknownProvider } from "./tries.mjs";
22
+ import { CoreParts, ObjectsParts, QueueParts, overStore } from "../core/parts.mjs";
21
23
  import * as Clock$1 from "effect/Clock";
22
24
  import * as Effect from "effect/Effect";
23
25
  import * as Result from "effect/Result";
24
- import { CurrentJob, jsonBytes } from "@ref0/infra";
26
+ import { CurrentJob, isInfraError, jsonBytes } from "@ref0/infra";
25
27
  // src/sync/mirror.ts
26
28
  /** How long a mirror waits before it reads its repo's import again, while the import runs. */
27
29
  const importWait = 1e4;
@@ -50,7 +52,12 @@ const longestReceipt = {
50
52
  oid: "0".repeat(64),
51
53
  at: Number.MAX_SAFE_INTEGER
52
54
  };
53
- const sameSource = (a, b) => a.provider === b.provider && a.repo === b.repo;
55
+ const sameSource = (a, b) => destinationText(a) === destinationText(b);
56
+ /**
57
+ * A mirror's saver as one text, one a caller as `callerKey` has it: a decoded row holds only
58
+ * `SavedCaller`'s fields, in its order.
59
+ */
60
+ const saverOf = (row) => JSON.stringify(row.by);
54
61
  const stateOf = (repo, ref, row) => {
55
62
  const state = {
56
63
  repo,
@@ -58,16 +65,18 @@ const stateOf = (repo, ref, row) => {
58
65
  state: row.state
59
66
  };
60
67
  if (row.paused !== void 0) state.paused = row.paused;
68
+ if (row.deletes === true) state.deletes = true;
61
69
  if (row.lastPushed !== void 0) state.lastPushed = row.lastPushed;
62
70
  return state;
63
71
  };
64
72
  /**
65
- * `mirror`: the mirror's row, active, and its job due now, in one commit, once the caller may
66
- * update the repo, sees the ref, and the destination names a provider `Sync.layer` was given. Sent
73
+ * `mirror`: the mirror's row, active, and its destination's job due now, in one commit, then that
74
+ * job retried if it had failed, once the caller may update the repo, sees the ref, and the
75
+ * destination names a provider `Sync.layer` was given. Sent
67
76
  * again, it writes the same row, so a paused mirror resumes and an active one pushes again. A row
68
77
  * past `mirrorLimits.rowBytes` is refused, and so is a new mirror on a repo that holds
69
78
  * `mirrorLimits.mirrors`; a mirror the repo holds is saved again whatever the count, keeping its
70
- * `lastPushed` while its destination is the same.
79
+ * `lastPushed` while its destination is the same, and `deletes` only when it's sent again.
71
80
  */
72
81
  const mirrorWith = /* @__PURE__ */ Effect.fnUntraced(function* (bound, input) {
73
82
  const { destination } = input;
@@ -98,6 +107,7 @@ const mirrorWith = /* @__PURE__ */ Effect.fnUntraced(function* (bound, input) {
98
107
  state: "active",
99
108
  by: yield* savedCallerOf(actor.id, caller)
100
109
  };
110
+ if (input.deletes === true) saved.deletes = true;
101
111
  const largest = {
102
112
  ...saved,
103
113
  state: "paused",
@@ -106,9 +116,10 @@ const mirrorWith = /* @__PURE__ */ Effect.fnUntraced(function* (bound, input) {
106
116
  };
107
117
  const bytes = jsonBytes(mirrorRow(ref, largest));
108
118
  if (bytes > mirrorLimits.rowBytes) return yield* fail("INVALID_ARGUMENT", `The mirror of '${ref}' takes ${bytes} bytes: a mirror takes at most ${mirrorLimits.rowBytes}`, { hint: "Name a shorter destination or ref" });
119
+ const key = mirrorJobKey(saved.destination);
109
120
  const job = bound.mirrorJob.encodedJob({
110
- key: ref,
111
- data: { ref }
121
+ key,
122
+ data: { destination: saved.destination }
112
123
  });
113
124
  const { store } = yield* CoreParts;
114
125
  const scope = repoScope(internalId);
@@ -123,192 +134,232 @@ const mirrorWith = /* @__PURE__ */ Effect.fnUntraced(function* (bound, input) {
123
134
  });
124
135
  const row = { ...saved };
125
136
  if (held?.lastPushed !== void 0 && sameSource(held.destination, saved.destination)) row.lastPushed = held.lastPushed;
126
- if ((yield* storeCall(() => store.commit(scope, {
137
+ if (!(yield* storeCall(() => store.commit(scope, {
127
138
  id: newChangeId(),
128
139
  expect: read.version,
129
140
  put: [mirrorRow(ref, row)],
130
141
  jobs: [job]
131
- }))).ok) return stateOf(input.repo, ref, row);
142
+ }))).ok) continue;
143
+ const { jobs } = yield* QueueParts;
144
+ if ((yield* storeCall(() => jobs.retry(scope, job)).pipe(Effect.catchIf(notFailed, () => Effect.void), Effect.catchIf((error) => isInfraError(error.cause) && error.cause.reason === "compacted", () => Effect.succeed(false)))) === false) {
145
+ yield* storeCall(() => jobs.discard(scope, job)).pipe(Effect.catchIf(notFailed, () => Effect.void));
146
+ yield* storeCall(() => store.add(scope, { jobs: [job] }));
147
+ }
148
+ return stateOf(input.repo, ref, row);
132
149
  }
133
150
  return yield* contended();
134
151
  });
135
- /** The mirror's row and the read's version. */
136
- const mirrorOf = /* @__PURE__ */ Effect.fnUntraced(function* (attempt) {
137
- const { internalId, ref } = attempt;
138
- const { store } = yield* CoreParts;
139
- const snapshot = yield* storeCall(() => store.read(repoScope(internalId), [mirrorKey(ref)]));
140
- const [[own] = []] = snapshot.results;
141
- return {
142
- mirror: own === void 0 ? void 0 : yield* decodeRow(MirrorRow, own, `Mirror '${ref}'`),
143
- version: snapshot.version
144
- };
145
- });
146
- /** The mirror's row, whether the repo's import still runs, and the row read's version. */
147
- const readMirror = /* @__PURE__ */ Effect.fnUntraced(function* (attempt) {
148
- const { mirror, version } = yield* mirrorOf(attempt);
149
- const imported = yield* readImport((yield* CoreParts).store, attempt.internalId);
150
- return {
151
- mirror,
152
- importing: imported.row?.state === "running" && !imported.cancelled,
153
- version
154
- };
155
- });
156
- const again = { kind: "more" };
157
152
  /**
158
- * The mirror paused, committed at `version`, the read the try began with: a push that moved the
159
- * ref since, or a resume, moved it, so a stale try records nothing and its next read decides.
153
+ * The mirrors whose rows name `destination`, among every mirror row in `store`, which
154
+ * `mirrorLimits.mirrors` keeps within one read, and the read's version.
160
155
  */
161
- const paused = /* @__PURE__ */ Effect.fnUntraced(function* (attempt, version, mirror, why) {
162
- const { store } = yield* CoreParts;
163
- const row = {
164
- ...mirror,
165
- state: "paused",
166
- paused: why
156
+ const destinationRows = /* @__PURE__ */ Effect.fnUntraced(function* (store, internalId, destination) {
157
+ const snapshot = yield* storeCall(() => store.read(repoScope(internalId), [mirrorsRead]));
158
+ const [rows = []] = snapshot.results;
159
+ const mirrored = [];
160
+ for (const row of rows) {
161
+ const decoded = yield* decodeRow(MirrorRow, row, `Mirror '${row.id}'`);
162
+ if (sameSource(decoded.destination, destination)) mirrored.push({
163
+ ref: row.id,
164
+ row: decoded
165
+ });
166
+ }
167
+ return {
168
+ mirrored,
169
+ version: snapshot.version
167
170
  };
168
- return (yield* storeCall(() => store.commit(repoScope(attempt.internalId), {
169
- id: newChangeId(),
170
- expect: version,
171
- put: [mirrorRow(attempt.ref, row)]
172
- }))).ok ? void 0 : again;
173
171
  });
174
172
  /**
175
- * The receipt of a push the upstream took, `oid` at the try's time, on the mirror's row as it is:
176
- * a pause, a resume or a save since keeps it while the row names the same destination. A row
177
- * removed, or sent to another destination since, takes none, and so does one that `writeTries`
178
- * other writes beat: the push stands, so the try ends either way.
173
+ * The try's pauses and receipts, in one commit. The pauses commit only at `version`, the read the
174
+ * try began with: a push that moved a ref since, or a resume, moved the rows, so a stale try
175
+ * records no pause and its next read decides. A receipt, `oid` at the try's time, goes on the
176
+ * mirror's row as it is, read again after a commit another write beat: a pause, a resume or a save
177
+ * since keeps it while the row names the same destination, and a row removed or sent elsewhere
178
+ * takes none, nor does one that `writeTries` other writes beat: the push stands either way.
179
179
  */
180
- const pushedTo = /* @__PURE__ */ Effect.fnUntraced(function* (attempt, version, mirror, oid) {
181
- const { store } = yield* CoreParts;
182
- const lastPushed = {
183
- oid,
184
- at: yield* Clock$1.currentTimeMillis
185
- };
186
- let row = mirror;
187
- let expect = version;
180
+ const settle = /* @__PURE__ */ Effect.fnUntraced(function* (attempt, read, settled) {
181
+ const { store } = attempt.context;
182
+ const at = yield* Clock$1.currentTimeMillis;
183
+ let { mirrored, version } = read;
184
+ let { pauses } = settled;
188
185
  for (let tries = 0; tries < 5; tries += 1) {
189
- if (row === void 0 || !sameSource(row.destination, mirror.destination)) return void 0;
190
- const put = mirrorRow(attempt.ref, {
191
- ...row,
192
- lastPushed
193
- });
186
+ const put = [];
187
+ for (const { ref, row } of mirrored) {
188
+ const why = pauses.get(ref);
189
+ const oid = settled.receipts.get(ref);
190
+ if (why === void 0 && oid === void 0) continue;
191
+ const next = why === void 0 ? { ...row } : {
192
+ ...row,
193
+ state: "paused",
194
+ paused: why
195
+ };
196
+ if (oid !== void 0) next.lastPushed = {
197
+ oid,
198
+ at
199
+ };
200
+ put.push(mirrorRow(ref, next));
201
+ }
202
+ if (put.length === 0) break;
194
203
  if ((yield* storeCall(() => store.commit(repoScope(attempt.internalId), {
195
204
  id: newChangeId(),
196
- expect,
197
- put: [put]
198
- }))).ok) return void 0;
199
- const reread = yield* mirrorOf(attempt);
200
- row = reread.mirror;
201
- expect = reread.version;
205
+ expect: version,
206
+ put
207
+ }))).ok) break;
208
+ settled.more ||= pauses.size > 0;
209
+ pauses = /* @__PURE__ */ new Map();
210
+ ({mirrored, version} = yield* destinationRows(store, attempt.internalId, attempt.destination));
202
211
  }
212
+ return settled.more ? { kind: "more" } : void 0;
203
213
  });
204
- /** Whether the upstream's tip is behind the ref's: the walk the push decide's own kinds take. */
205
- const goesOn = /* @__PURE__ */ Effect.fnUntraced(function* (listed, edit) {
214
+ /** The edits whose upstream tip their ref goes on from: the walks the push decide's kinds take. */
215
+ const goingOn = /* @__PURE__ */ Effect.fnUntraced(function* (listed, edits) {
216
+ const going = /* @__PURE__ */ new Set();
217
+ if (edits.length === 0) return going;
206
218
  const kinds = yield* kindsOf({
207
219
  objects: oneShotReads(objectsFace(syncObjects(yield* ObjectsParts))),
208
220
  listed,
209
221
  pushed: () => Effect.succeed(void 0),
210
- edits: [edit]
222
+ edits
211
223
  });
212
- const [update] = kinds.updates;
213
- if (update === void 0 || kinds.refused.has(edit.ref)) return false;
214
- if (update.kind !== "force") return true;
215
- return (yield* kinds.exact(update)) === "fast-forward";
224
+ for (const update of kinds.updates) {
225
+ if (kinds.refused.has(update.ref)) continue;
226
+ if (update.kind !== "force" || (yield* kinds.exact(update)) === "fast-forward") going.add(update.ref);
227
+ }
228
+ return going;
216
229
  });
217
- const upstreamOf = /* @__PURE__ */ Effect.fnUntraced(function* (http) {
230
+ const upstreamOf = /* @__PURE__ */ Effect.fnUntraced(function* (http, heard) {
218
231
  const read = yield* Effect.result(receiveRefs(http));
219
232
  if (Result.isSuccess(read)) return {
220
233
  kind: "read",
221
234
  refs: read.success
222
235
  };
223
236
  const { failure } = read;
224
- if (!refusedCodes.has(failure.code)) return yield* Effect.fail(fromRemote("advertise")(failure));
237
+ if (!refusedCodes.has(failure.code)) return yield* Effect.fail(fromRemote("advertise", heard)(failure));
225
238
  return { kind: "refused" };
226
239
  });
227
240
  /**
228
- * Whether the caller who saved the mirror still sees its ref, asked again now for a fetch, as
229
- * `viewAgainOf` asks it, in the try's own context. `gone` for a repo deleted since. A host that
230
- * failed fails the try.
241
+ * The mirrors whose savers still see their refs, each saver asked again once, now, for a fetch,
242
+ * as `viewAgainOf` asks it, in the try's own context; the rest pause `unseen`. `undefined` for a
243
+ * repo deleted since. A host that failed fails the try.
231
244
  */
232
- const seenBy = /* @__PURE__ */ Effect.fnUntraced(function* (attempt, by) {
233
- const { internalId, ref, context } = attempt;
245
+ const seenOf = /* @__PURE__ */ Effect.fnUntraced(function* (attempt, active, settled) {
246
+ const { internalId, context } = attempt;
234
247
  const record = yield* hostRecordOf((yield* CoreParts).store, internalId);
235
- if (record === void 0) return "gone";
248
+ if (record === void 0) return void 0;
236
249
  const repo = repoAccess({
237
250
  name: record.name,
238
251
  record,
239
252
  internalId
240
253
  });
241
- const viewed = yield* viewAgainOf(by, repo, "fetch", context.signal);
242
- if (viewed.kind === "refused") return "unseen";
243
- return viewed.view.sees(ref) ? "seen" : "unseen";
254
+ const views = /* @__PURE__ */ new Map();
255
+ const seen = [];
256
+ for (const mirror of active) {
257
+ const saver = saverOf(mirror.row);
258
+ const viewed = views.get(saver) ?? (yield* viewAgainOf(mirror.row.by, repo, "fetch", context.signal));
259
+ views.set(saver, viewed);
260
+ if (viewed.kind !== "refused" && viewed.view.sees(mirror.ref)) seen.push(mirror);
261
+ else settled.pauses.set(mirror.ref, "unseen");
262
+ }
263
+ return seen;
244
264
  });
245
265
  /**
246
- * One mirror try: the mirror's row, its saver asked again, then the ref's tip and the upstream's,
247
- * and a push of the ref from the upstream's tip, so the upstream only ever moves from the ID it
248
- * advertised.
249
- * - a paused mirror, or one with no row, ends; one whose repo's import runs waits for it;
250
- * - a saver who no longer sees the ref pauses it `unseen`, before the tip is read;
251
- * - an upstream already at the tip ends, keeping its receipt when the row has none for it;
252
- * - an upstream tip the ref doesn't go on from pauses it `diverged`;
253
- * - a push the upstream takes keeps its receipt, `lastPushed`, on the mirror's row;
254
- * - a refused push whose upstream has moved since is stale, and reads again; one whose upstream
255
- * hasn't, or a credential or a POST the upstream refuses, pauses it `refused`.
266
+ * One mirror try, for every mirror of the job's destination: their rows, each saver asked again,
267
+ * then each ref's tip, the upstream's advertisement read once, and one push of every ref that
268
+ * differs, each from the upstream's tip, so the upstream only ever moves from the ID it advertised.
269
+ * - a destination with no active mirror ends; one whose repo's import runs waits for it;
270
+ * - a mirror whose saver no longer sees its ref pauses `unseen`, before the tip is read;
271
+ * - a ref with no tip is left out, and the upstream keeps its copy (Q393), unless its mirror
272
+ * `deletes`: then the upstream's ref goes in the same push, from the ID the mirror's receipt
273
+ * says it left there, and one pushed there since pauses it `diverged`, alone;
274
+ * - an upstream already at a ref's tip leaves it out, keeping its receipt when the row has none;
275
+ * - an upstream tip a ref doesn't go on from pauses that ref's mirror `diverged`, alone;
276
+ * - a credential, or a POST, the upstream refuses pauses every mirror the push held `refused`;
277
+ * - each update the upstream takes keeps its receipt, `lastPushed`, on its mirror's row, `null`
278
+ * for a delete;
279
+ * - a refused update whose upstream ref has moved since is stale, and the job reads again; one
280
+ * whose upstream ref hasn't pauses its mirror `refused`.
281
+ * Each try sends only what still differs, so one that dies mid-push and runs again pushes no ref
282
+ * twice.
256
283
  */
257
284
  const mirrorStep = /* @__PURE__ */ Effect.fnUntraced(function* (attempt) {
258
- const read = yield* readMirror(attempt);
259
- const { mirror } = read;
260
- if (mirror === void 0 || mirror.state !== "active") return void 0;
261
- if (read.importing) return {
285
+ const { store } = attempt.context;
286
+ const read = yield* destinationRows(store, attempt.internalId, attempt.destination);
287
+ const active = read.mirrored.filter(({ row }) => row.state === "active");
288
+ if (active.length === 0) return void 0;
289
+ const imported = yield* readImport(store, attempt.internalId);
290
+ if (imported.row?.state === "running" && !imported.cancelled) return {
262
291
  kind: "more",
263
292
  at: (yield* Clock$1.currentTimeMillis) + importWait
264
293
  };
265
- const seen = yield* seenBy(attempt, mirror.by);
266
- if (seen === "gone") return void 0;
267
- if (seen === "unseen") return yield* paused(attempt, read.version, mirror, "unseen");
268
- return yield* asSync(pushStep(attempt, read.version, mirror));
294
+ const settled = {
295
+ pauses: /* @__PURE__ */ new Map(),
296
+ receipts: /* @__PURE__ */ new Map(),
297
+ more: false
298
+ };
299
+ const seen = yield* seenOf(attempt, active, settled);
300
+ if (seen === void 0) return void 0;
301
+ if (seen.length > 0) yield* asSync(pushStep(attempt, seen, settled));
302
+ return yield* settle(attempt, read, settled);
269
303
  });
270
- /** The try past its saver's check: the tip, the upstream's, and the push between them. */
271
- const pushStep = /* @__PURE__ */ Effect.fnUntraced(function* (attempt, version, mirror) {
272
- const { bound, ref, context } = attempt;
304
+ /** The try past its savers' checks: the tips, the upstream's, and the one push between them. */
305
+ const pushStep = /* @__PURE__ */ Effect.fnUntraced(function* (attempt, seen, settled) {
306
+ const { bound, context } = attempt;
273
307
  const stored = yield* ObjectsParts;
274
308
  const opened = yield* openedFor(bound.caller, attempt.internalId);
275
- const tip = opened?.refs.get(ref);
276
- if (opened === void 0 || tip === void 0) return void 0;
277
- const remote = yield* remoteOf(bound, mirror.destination, context.signal, "write");
278
- const http = yield* httpOf(remote);
279
- const upstream = yield* upstreamOf(http);
280
- if (upstream.kind === "refused") return yield* paused(attempt, version, mirror, "refused");
281
- const from = upstream.refs.refs.get(ref) ?? null;
282
- if (from === tip) {
283
- if (mirror.lastPushed?.oid === tip) return void 0;
284
- return yield* pushedTo(attempt, version, mirror, tip);
309
+ if (opened === void 0) return;
310
+ const tipped = [];
311
+ for (const mirror of seen) {
312
+ const tip = opened.refs.get(mirror.ref) ?? null;
313
+ if (tip !== null || mirror.row.deletes === true) tipped.push({
314
+ ...mirror,
315
+ tip
316
+ });
285
317
  }
286
- const edit = {
287
- ref,
288
- from,
289
- to: tip
290
- };
291
- if (from !== null && !(yield* goesOn(opened.listed, edit))) return yield* paused(attempt, version, mirror, "diverged");
318
+ if (tipped.length === 0) return;
319
+ const remote = yield* remoteOf(bound, attempt.destination, context.signal, "write");
320
+ const { http, heard } = yield* httpOf(remote, keptFieldsOf(bound, attempt.destination));
321
+ const upstream = yield* upstreamOf(http, heard);
322
+ if (upstream.kind === "refused") {
323
+ for (const { ref } of tipped) settled.pauses.set(ref, "refused");
324
+ return;
325
+ }
326
+ const differ = [];
327
+ for (const { ref, row, tip } of tipped) {
328
+ const from = upstream.refs.refs.get(ref) ?? null;
329
+ if (from === tip) {
330
+ if (row.lastPushed?.oid !== tip) settled.receipts.set(ref, tip);
331
+ } else if (tip === null && row.lastPushed?.oid !== from) settled.pauses.set(ref, "diverged");
332
+ else differ.push({
333
+ ref,
334
+ from,
335
+ to: tip
336
+ });
337
+ }
338
+ const going = yield* goingOn(opened.listed, differ.filter(({ from, to }) => from !== null && to !== null));
339
+ const edits = [];
340
+ for (const edit of differ) if (edit.to === null || edit.from === null || going.has(edit.ref)) edits.push(edit);
341
+ else settled.pauses.set(edit.ref, "diverged");
342
+ if (edits.length === 0) return;
292
343
  const source = {
293
344
  objects: objectsFace(syncObjects(stored)),
294
345
  listed: opened.listed,
295
346
  limits: stored.fetchLimits
296
347
  };
297
- const pushed = yield* Effect.result(sendPush(http, source, upstream.refs, [edit], {}));
348
+ const pushed = yield* Effect.result(sendPush(http, source, upstream.refs, edits, {}));
298
349
  if (Result.isFailure(pushed)) {
299
350
  const { failure } = pushed;
300
- if (!refusedCodes.has(failure.code)) return yield* Effect.fail(fromRemote("take a push")(failure));
301
- return yield* paused(attempt, version, mirror, "refused");
351
+ if (!refusedCodes.has(failure.code)) return yield* Effect.fail(fromRemote("take a push", heard)(failure));
352
+ for (const { ref } of edits) settled.pauses.set(ref, "refused");
353
+ return;
302
354
  }
303
- const [update] = pushed.success.updates;
304
- if (update?.kind === "applied") return yield* pushedTo(attempt, version, mirror, tip);
305
- return yield* refusedPush(attempt, version, mirror, http, upstream.refs, ref);
306
- });
307
- /** A refused push: stale when the upstream's tip has moved since it was read, so read again. */
308
- const refusedPush = /* @__PURE__ */ Effect.fnUntraced(function* (attempt, version, mirror, http, first, ref) {
309
- const reread = yield* upstreamOf(http);
310
- if (reread.kind === "read" && reread.refs.refs.get(ref) !== first.refs.get(ref)) return again;
311
- return yield* paused(attempt, version, mirror, "refused");
355
+ const applied = new Set(pushed.success.updates.flatMap((update) => update.kind === "applied" ? [update.ref] : []));
356
+ const rejected = [];
357
+ for (const edit of edits) if (!applied.has(edit.ref)) rejected.push(edit);
358
+ else settled.receipts.set(edit.ref, edit.to);
359
+ if (rejected.length === 0) return;
360
+ const reread = yield* upstreamOf(http, heard);
361
+ for (const { ref } of rejected) if (reread.kind === "read" && reread.refs.refs.get(ref) !== upstream.refs.refs.get(ref)) settled.more = true;
362
+ else settled.pauses.set(ref, "refused");
312
363
  });
313
364
  /** `sync.mirror`'s handler over `bound`: one try, its failure thrown as `SyncTryError`. */
314
365
  const mirrorTry = /* @__PURE__ */ Effect.fnUntraced(function* (bound, data) {
@@ -316,12 +367,38 @@ const mirrorTry = /* @__PURE__ */ Effect.fnUntraced(function* (bound, data) {
316
367
  const attempt = {
317
368
  bound,
318
369
  internalId: yield* currentRepo,
319
- ref: data.ref,
370
+ destination: data.destination,
320
371
  context
321
372
  };
322
373
  return yield* tried(Effect.provideContext(overStore(context.store)(mirrorStep(attempt)), bound.needs));
323
374
  });
324
375
  /**
376
+ * Once a destination's job has failed for good, every active mirror its rows name paused
377
+ * `refused`, so `mirrors` says why it stopped and saving one again resumes it: read again after a
378
+ * commit another write beat, until the queue's bound on `onFailure` aborts the signal.
379
+ */
380
+ const mirrorFailed = /* @__PURE__ */ Effect.fnUntraced(function* (bound, data) {
381
+ const context = yield* CurrentJob.CurrentJob;
382
+ const internalId = yield* currentRepo;
383
+ const marked = Effect.gen(function* () {
384
+ for (;;) {
385
+ const read = yield* destinationRows(context.store, internalId, data.destination);
386
+ const put = read.mirrored.flatMap(({ ref, row }) => row.state === "active" ? [mirrorRow(ref, {
387
+ ...row,
388
+ state: "paused",
389
+ paused: "refused"
390
+ })] : []);
391
+ if (put.length === 0) return;
392
+ if ((yield* storeCall(() => context.store.commit(repoScope(internalId), {
393
+ id: newChangeId(),
394
+ expect: read.version,
395
+ put
396
+ }))).ok) return;
397
+ }
398
+ });
399
+ yield* Effect.provideContext(marked, bound.needs).pipe(Effect.mapError(thrownOf));
400
+ });
401
+ /**
325
402
  * `mirrors`: each of the repo's mirror rows, by ref, once the caller may read the repo: only those
326
403
  * whose ref the caller sees, so a private ref's mirror answers as no mirror.
327
404
  */
@@ -359,19 +436,22 @@ const removeMirrorWith = /* @__PURE__ */ Effect.fnUntraced(function* (_bound, in
359
436
  kind: "repo.update",
360
437
  repo: input.repo
361
438
  };
362
- const { internalId, name, record } = yield* openRepo(caller, operation, [], input.internalId);
439
+ const opened = yield* openRepo(caller, operation, [], input.internalId, "first-read");
440
+ const { internalId, name, record } = opened;
363
441
  const none = fail("NOT_FOUND", `'${input.repo}' has no mirror of '${ref}'`, { hint: "List the repo's mirrors with mirrors" });
364
- if (!(yield* refViewIn(caller, repoAccess({
365
- name,
366
- record,
367
- internalId
368
- }))).sees(ref)) return yield* none;
369
442
  const { store } = yield* CoreParts;
370
443
  const scope = repoScope(internalId);
444
+ let refView;
371
445
  for (let tries = 0; tries < 5; tries += 1) {
372
- const read = yield* storeCall(() => store.read(scope, [mirrorKey(ref)]));
446
+ const asked = tries === 0 ? opened : void 0;
447
+ const read = yield* readAsked(store, internalId, [mirrorKey(ref)], [], asked);
448
+ if (refView === void 0) refView = yield* refViewIn(caller, repoAccess({
449
+ name,
450
+ record,
451
+ internalId
452
+ }));
373
453
  const [[own] = []] = read.results;
374
- if (own === void 0) return yield* none;
454
+ if (own === void 0 || !refView.sees(ref)) return yield* none;
375
455
  if ((yield* storeCall(() => store.commit(scope, {
376
456
  id: newChangeId(),
377
457
  expect: read.version,
@@ -380,4 +460,4 @@ const removeMirrorWith = /* @__PURE__ */ Effect.fnUntraced(function* (_bound, in
380
460
  }
381
461
  return yield* contended();
382
462
  });
383
- export { mirrorTry, mirrorWith, mirrorsWith, removeMirrorWith };
463
+ export { mirrorFailed, mirrorTry, mirrorWith, mirrorsWith, removeMirrorWith };