@ref0/git 0.4.0 → 0.6.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 (167) hide show
  1. package/dist/access/private-refs.mjs +3 -4
  2. package/dist/auth/authorize.mjs +9 -4
  3. package/dist/auth/identify.d.mts +5 -3
  4. package/dist/auth/tokens.mjs +12 -11
  5. package/dist/client/local.mjs +2 -0
  6. package/dist/client/slice-push.d.mts +1 -1
  7. package/dist/contracts/access.d.mts +6 -2
  8. package/dist/contracts/authorize.d.mts +14 -10
  9. package/dist/contracts/events.d.mts +32 -5
  10. package/dist/contracts/events.mjs +14 -3
  11. package/dist/contracts/git.d.mts +68 -3
  12. package/dist/contracts/grants.d.mts +6 -1
  13. package/dist/contracts/lfs.d.mts +5 -2
  14. package/dist/contracts/objects.d.mts +7 -2
  15. package/dist/contracts/promises.d.mts +2 -14
  16. package/dist/contracts/push-uploads.d.mts +1 -1
  17. package/dist/contracts/queue.d.mts +15 -7
  18. package/dist/contracts/refs.d.mts +1 -1
  19. package/dist/contracts/result.d.mts +9 -5
  20. package/dist/contracts/result.mjs +5 -0
  21. package/dist/contracts/server.d.mts +13 -6
  22. package/dist/contracts/work.d.mts +1 -1
  23. package/dist/core/access.mjs +17 -1
  24. package/dist/core/call-names.d.mts +3 -1
  25. package/dist/core/claim-rows.d.mts +4 -0
  26. package/dist/core/claim-rows.mjs +53 -0
  27. package/dist/core/events/deliver.d.mts +12 -0
  28. package/dist/core/events/deliver.mjs +367 -145
  29. package/dist/core/events/seen.mjs +17 -3
  30. package/dist/core/forks.mjs +82 -20
  31. package/dist/core/hooks/post-receive.mjs +20 -17
  32. package/dist/core/index-job.mjs +23 -17
  33. package/dist/core/parts.d.mts +6 -0
  34. package/dist/core/purge.d.mts +8 -0
  35. package/dist/core/purge.mjs +16 -21
  36. package/dist/core/push-decide.mjs +26 -4
  37. package/dist/core/push.mjs +57 -20
  38. package/dist/core/ref-index-job.mjs +9 -14
  39. package/dist/core/ref-index.d.mts +1 -0
  40. package/dist/core/ref-index.mjs +28 -8
  41. package/dist/core/ref-view.mjs +14 -9
  42. package/dist/core/refs.mjs +13 -11
  43. package/dist/core/repack.d.mts +2 -1
  44. package/dist/core/repack.mjs +66 -118
  45. package/dist/core/repos.mjs +5 -5
  46. package/dist/core/rules/check.mjs +7 -6
  47. package/dist/core/rules/context.d.mts +1 -1
  48. package/dist/core/seam.d.mts +56 -9
  49. package/dist/core/seam.mjs +7 -1
  50. package/dist/core/still-takes.mjs +75 -0
  51. package/dist/core/view.d.mts +1 -0
  52. package/dist/core/view.mjs +8 -9
  53. package/dist/core/work.mjs +1 -1
  54. package/dist/extend.d.mts +2 -1
  55. package/dist/http/core-parts.mjs +19 -11
  56. package/dist/http/handler.mjs +2 -2
  57. package/dist/http/node.d.mts +2 -23
  58. package/dist/http/parts.d.mts +1 -0
  59. package/dist/http/push-uploads.d.mts +10 -2
  60. package/dist/http/push-uploads.mjs +31 -34
  61. package/dist/http/receive-pack.mjs +5 -3
  62. package/dist/http/upload-pack.mjs +2 -2
  63. package/dist/index.d.mts +11 -7
  64. package/dist/index.mjs +5 -3
  65. package/dist/lfs/base.d.mts +32 -0
  66. package/dist/lfs/base.mjs +8 -0
  67. package/dist/lfs/calls.mjs +21 -12
  68. package/dist/lfs/layer.d.mts +27 -0
  69. package/dist/lfs/layer.mjs +64 -0
  70. package/dist/lfs/locks.mjs +13 -27
  71. package/dist/lfs/namespace.d.mts +2 -0
  72. package/dist/lfs/objects.d.mts +23 -4
  73. package/dist/lfs/objects.mjs +65 -81
  74. package/dist/lfs/pointers.mjs +8 -6
  75. package/dist/lfs/public.d.mts +5 -0
  76. package/dist/lfs/public.mjs +8 -0
  77. package/dist/lfs/purge.mjs +19 -25
  78. package/dist/lfs/rows.mjs +22 -10
  79. package/dist/lfs/service.d.mts +9 -4
  80. package/dist/lfs/service.mjs +3 -12
  81. package/dist/lfs/upkeep.mjs +34 -49
  82. package/dist/node.d.mts +3 -2
  83. package/dist/node.mjs +1 -1
  84. package/dist/protocol/wire/advertisement.d.mts +4 -10
  85. package/dist/protocol/wire/advertisement.mjs +16 -12
  86. package/dist/protocol/wire/lines.d.mts +14 -2
  87. package/dist/protocol/wire/lines.mjs +32 -21
  88. package/dist/protocol/wire/v2.d.mts +4 -15
  89. package/dist/protocol/wire/v2.mjs +16 -14
  90. package/dist/queue/advance.mjs +78 -55
  91. package/dist/queue/calls.mjs +4 -2
  92. package/dist/queue/commit.mjs +20 -13
  93. package/dist/queue/host.d.mts +2 -2
  94. package/dist/queue/layer.d.mts +4 -2
  95. package/dist/queue/layer.mjs +8 -3
  96. package/dist/queue/limits.mjs +36 -6
  97. package/dist/queue/rows.mjs +14 -5
  98. package/dist/queue/state.mjs +19 -5
  99. package/dist/queue/steps.mjs +10 -4
  100. package/dist/queue/trim.mjs +284 -0
  101. package/dist/rest/blobs.d.mts +19 -19
  102. package/dist/rest/caller.d.mts +4 -4
  103. package/dist/rest/commits.d.mts +96 -96
  104. package/dist/rest/errors.d.mts +2 -2
  105. package/dist/rest/events.d.mts +2 -2
  106. package/dist/rest/files.d.mts +54 -54
  107. package/dist/rest/forks.d.mts +87 -87
  108. package/dist/rest/handler.mjs +13 -5
  109. package/dist/rest/layer.d.mts +14 -0
  110. package/dist/rest/layer.mjs +52 -0
  111. package/dist/rest/me.d.mts +2 -2
  112. package/dist/rest/merges.d.mts +2 -2
  113. package/dist/rest/namespace.d.mts +2 -0
  114. package/dist/rest/queue.d.mts +208 -208
  115. package/dist/rest/refs.d.mts +131 -131
  116. package/dist/rest/repos.d.mts +12 -12
  117. package/dist/rest/tags.d.mts +70 -70
  118. package/dist/rest/tokens.d.mts +23 -23
  119. package/dist/rest/topics.d.mts +111 -111
  120. package/dist/rest/trees.d.mts +57 -57
  121. package/dist/rest/wire.d.mts +2 -0
  122. package/dist/rest/wire.mjs +2 -2
  123. package/dist/rest/work.d.mts +6 -6
  124. package/dist/server/edge-node.d.mts +20 -0
  125. package/dist/server/edge-node.mjs +20 -0
  126. package/dist/server/edge.d.mts +10 -0
  127. package/dist/server/edge.mjs +23 -0
  128. package/dist/server/extension.mjs +1 -1
  129. package/dist/server/git-layer.d.mts +15 -4
  130. package/dist/server/git-layer.mjs +13 -2
  131. package/dist/server/git-namespace.d.mts +5 -4
  132. package/dist/server/git-namespace.mjs +6 -3
  133. package/dist/server/registry.d.mts +3 -0
  134. package/dist/server/registry.mjs +58 -7
  135. package/dist/server/server.d.mts +3 -0
  136. package/dist/server/server.mjs +81 -39
  137. package/dist/server/uploads.d.mts +14 -0
  138. package/dist/server/uploads.mjs +15 -0
  139. package/dist/storage/calls.d.mts +7 -8
  140. package/dist/storage/calls.mjs +14 -14
  141. package/dist/storage/clean-up.mjs +59 -29
  142. package/dist/storage/host-bucket.mjs +16 -1
  143. package/dist/storage/index-build.d.mts +1 -1
  144. package/dist/storage/layout.mjs +5 -0
  145. package/dist/storage/merge.d.mts +1 -1
  146. package/dist/storage/push-uploads.mjs +12 -16
  147. package/dist/storage/quarantine.d.mts +1 -1
  148. package/dist/storage/quarantine.mjs +51 -12
  149. package/dist/storage/receive-bounds.d.mts +1 -1
  150. package/dist/storage/repack-job.d.mts +1 -1
  151. package/dist/storage/repack-job.mjs +7 -14
  152. package/dist/storage/rows.mjs +42 -3
  153. package/dist/storage/runs.d.mts +1 -1
  154. package/dist/store/job-try.mjs +26 -0
  155. package/dist/store/list-job.mjs +22 -16
  156. package/dist/store/outbox.mjs +38 -12
  157. package/dist/store/promises.d.mts +6 -1
  158. package/dist/store/promises.mjs +74 -89
  159. package/dist/store/queue-jobs.mjs +22 -1
  160. package/dist/store/scopes.mjs +0 -10
  161. package/dist/sync/mirror.d.mts +1 -1
  162. package/dist/sync/mirror.mjs +1 -1
  163. package/dist/sync/publish.mjs +12 -7
  164. package/dist/sync/step.mjs +4 -0
  165. package/package.json +3 -3
  166. package/dist/lfs/repo.mjs +0 -42
  167. package/dist/storage/promise-calls.mjs +0 -29
@@ -1,22 +1,29 @@
1
1
  import { millis } from "../../contracts/promises.mjs";
2
2
  import { isInternalId } from "../../contracts/ids.mjs";
3
3
  import { fail, gitError } from "../../contracts/result.mjs";
4
+ import { isWellFormed, utf8 } from "../../protocol/bytes/text.mjs";
4
5
  import { decodeRecord } from "../../store/catalogue.mjs";
5
6
  import { settleName } from "../../store/settle.mjs";
6
7
  import { decodeRow, storeCall } from "../../store/errors.mjs";
7
- import { runJobTry } from "../../store/crossing.mjs";
8
- import { FailedTry, Follower, appendEvents, cursorId, decodeEvents, decodeOutbox, eventBytes, eventId, eventLimits, failedKey, headKey, outboxCollections, outboxReads, repoEnvelope, shownEvent } from "../../store/outbox.mjs";
8
+ import { Delivered, FailedTry, Follower, appendEvents, cursorId, cursorRead, decodeEvents, decodeOutbox, deliveryCursorId, eventBytes, eventId, eventLimits, failedKeyOf, failedPrefix, headKey, outboxCollections, outboxReads, repoEnvelope, shownEvent } from "../../store/outbox.mjs";
9
+ import { madeJob, settledCall } from "../../store/job-try.mjs";
9
10
  import { newChangeId, repoScope, scopeRepo, selfKey } from "../../store/scopes.mjs";
10
- import { defineJob, isInfraError } from "@ref0/infra";
11
+ import { CurrentJob, Job, isInfraError, limits } from "@ref0/infra";
11
12
  import * as Schema from "effect/Schema";
12
13
  import * as Effect from "effect/Effect";
13
14
  import * as Predicate from "effect/Predicate";
14
15
  // src/core/events/deliver.ts
15
- const deliverJobName = "git.deliver";
16
- /** One row per scope, so its events go one batch at a time, in `seq` order. */
16
+ /** What each delivery's job name starts with: `git.deliver.<name>`. */
17
+ const deliverJobPrefix = "git.deliver.";
18
+ /** `delivery`'s job, `git.deliver.<delivery>`: one a scope, beside each other delivery's. */
19
+ const deliverJobName = (delivery) => `${deliverJobPrefix}${delivery}`;
20
+ /** The delivery a job named `name` runs, or `undefined` for a job no delivery's. */
21
+ const deliveryOf = (name) => name.startsWith(deliverJobPrefix) && name.length > 12 ? name.slice(12) : void 0;
22
+ /** One row per scope and delivery, so its events go one batch at a time, in `seq` order. */
17
23
  const deliverJobKey = "deliver";
18
24
  const DeliverData = /* @__PURE__ */ Schema.Struct({});
19
- const deliverJobSchema = /* @__PURE__ */ Schema.toStandardSchemaV1(DeliverData);
25
+ /** The delivery a server ships with: its host's `events`. */
26
+ const eventsDelivery = "events";
20
27
  /** How long a delivered event is kept for replay, unless the kept budget trims it first. */
21
28
  const keptFor = 864e5;
22
29
  /** How long a scope waits before it looks again at a scope that held its name's sequence. */
@@ -37,6 +44,11 @@ const defaultDeliverRetry = {
37
44
  };
38
45
  /** The host's `events`, as one shape. */
39
46
  const eventDelivery = (events) => Predicate.isFunction(events) ? { handler: events } : events;
47
+ /** The deliveries a server runs for the host's `events`: the one, `events`, until D2 adds more. */
48
+ const deliveriesOf = (events) => [{
49
+ ...eventDelivery(events),
50
+ name: eventsDelivery
51
+ }];
40
52
  /** A read infra refused because its answer passed 16 MiB. */
41
53
  const tooLarge = (error) => error.code === "RESOURCE_EXHAUSTED" && error.context?.reason === "too-large";
42
54
  /**
@@ -51,6 +63,78 @@ const readEvents = (store, scope, reads, limit) => {
51
63
  })));
52
64
  return read(limit).pipe(Effect.catchIf(tooLarge, () => read(largeReadRows)));
53
65
  };
66
+ /**
67
+ * Where a delivery stands in a scope it has no cursor row in yet: the retained beginning, just
68
+ * before the oldest event kept, whose earlier events' bytes the head counts as trimmed, since a
69
+ * trim deletes the oldest first. With none kept, past the last. One read, of one event.
70
+ */
71
+ const retainedStart = /* @__PURE__ */ Effect.fnUntraced(function* (store, scope, head) {
72
+ const read = {
73
+ collection: outboxCollections.events,
74
+ limit: 1
75
+ };
76
+ const snapshot = yield* storeCall(() => store.read(scope, [read]));
77
+ const [oldest] = yield* decodeEvents(snapshot.results[0] ?? []);
78
+ return oldest === void 0 ? {
79
+ seq: head.next - 1,
80
+ bytes: head.bytes
81
+ } : {
82
+ seq: oldest.seq - 1,
83
+ bytes: head.trimmed
84
+ };
85
+ });
86
+ /** A cursor row, read by `cursorRead`, or none: a delivery that hasn't moved in the scope yet. */
87
+ const decodeCursor = (rows) => {
88
+ const [row] = rows ?? [];
89
+ return row === void 0 ? Effect.succeed(void 0) : decodeRow(Delivered, row, "The delivery cursor");
90
+ };
91
+ /** A delivery's cursor in `scope`, from its `cursorRead` rows, or its retained beginning. */
92
+ const cursorOf = /* @__PURE__ */ Effect.fnUntraced(function* (store, scope, head, rows) {
93
+ const cursor = yield* decodeCursor(rows);
94
+ if (cursor !== void 0) return cursor;
95
+ return yield* retainedStart(store, scope, head);
96
+ });
97
+ /** Each delivery's cursor, from their `cursorRead`s' results, in the order the reads named them. */
98
+ const decodeCursors = /* @__PURE__ */ Effect.fnUntraced(function* (deliveries, results) {
99
+ const cursors = /* @__PURE__ */ new Map();
100
+ for (const [index, delivery] of deliveries.entries()) cursors.set(delivery, yield* decodeCursor(results[index]));
101
+ return cursors;
102
+ });
103
+ /** The slowest of `places`, or `undefined` when one is missing: a delivery that hasn't moved. */
104
+ const slowest = (places) => {
105
+ let least;
106
+ for (const place of places) {
107
+ if (place === void 0) return void 0;
108
+ if (least === void 0 || place.seq < least.seq) least = place;
109
+ }
110
+ return least;
111
+ };
112
+ /**
113
+ * Moves `delivery`'s cursor to `value`, in a new row, and then the low-water mark to the slowest
114
+ * active delivery, when each has a cursor and the slowest has passed the mark, as `read` showed
115
+ * them. A low-water row that a crash between the two leaves behind only holds more: the next move
116
+ * writes it. Answers the low-water mark after.
117
+ */
118
+ const moveCursor = /* @__PURE__ */ Effect.fnUntraced(function* (store, scope, delivery, value, read) {
119
+ const own = read.cursors.get(delivery);
120
+ const moved = own === void 0 || own.seq < value.seq ? value : own;
121
+ const id = deliveryCursorId(delivery, value.seq);
122
+ if (moved === value) yield* addCursor(store, scope, {
123
+ collection: outboxCollections.cursors,
124
+ id,
125
+ value
126
+ });
127
+ const places = [...read.cursors].map(([name, at]) => name === delivery ? moved : at);
128
+ const floor = slowest(places);
129
+ if (floor === void 0 || floor.seq <= read.delivered.seq) return read.delivered;
130
+ const mark = {
131
+ collection: outboxCollections.delivered,
132
+ id: cursorId(floor.seq),
133
+ value: floor
134
+ };
135
+ yield* addCursor(store, scope, mark);
136
+ return floor;
137
+ });
54
138
  const again = (at) => ({
55
139
  kind: "settle",
56
140
  outcome: at === void 0 ? { kind: "more" } : {
@@ -63,22 +147,28 @@ const settle = (outcome) => ({
63
147
  outcome
64
148
  });
65
149
  /**
66
- * Which of the scopes that held this scope's names' sequences before still owe theirs: each that
67
- * hasn't delivered up to where this one goes on. One whose `git.deliver` failed still owes them,
68
- * until a retry delivers them or a discard skips them, so a name's `seq` only goes up. Drops the
69
- * rest from `after`, and answers the first `seq` here they hold, or `undefined` when none does.
150
+ * Which of the scopes that held this scope's names' sequences before still owe theirs to this
151
+ * try's delivery: each whose cursor for it hasn't reached where this one goes on. One whose
152
+ * delivery failed still owes them, until a retry delivers them or a discard skips them, so a
153
+ * name's `seq` only goes up, for each delivery. Drops from `after` only those every delivery is
154
+ * past, by the earlier scope's low-water mark, and answers the first `seq` here they hold for this
155
+ * delivery, or `undefined` when none does.
70
156
  */
71
157
  const followed = /* @__PURE__ */ Effect.fnUntraced(function* (parts, store, scope, version, head) {
72
158
  const { after } = head;
73
159
  if (after === void 0) return void 0;
74
160
  const owing = [];
161
+ const kept = [];
75
162
  const waiter = scopeRepo(scope);
76
163
  for (const follows of after) {
77
164
  const before = repoScope(follows.internalId);
78
- const snapshot = yield* storeCall(() => parts.store.read(before, [...outboxReads]));
165
+ const reads = [...outboxReads, cursorRead(parts.delivery)];
166
+ const snapshot = yield* storeCall(() => parts.store.read(before, reads));
79
167
  const outbox = yield* decodeOutbox(snapshot.results);
168
+ const cursor = yield* cursorOf(parts.store, before, outbox.head, snapshot.results[2]);
80
169
  const owed = Math.min(follows.seq, outbox.head.next - 1);
81
- if (outbox.delivered.seq < owed) owing.push(follows);
170
+ if (cursor.seq < owed) owing.push(follows);
171
+ if (cursor.seq < owed || outbox.delivered.seq < owed) kept.push(follows);
82
172
  if (owing.at(-1) !== follows || waiter === void 0) continue;
83
173
  const row = {
84
174
  collection: outboxCollections.followers,
@@ -87,12 +177,12 @@ const followed = /* @__PURE__ */ Effect.fnUntraced(function* (parts, store, scop
87
177
  };
88
178
  yield* addNew(parts.store, before, row);
89
179
  }
90
- if (owing.length < after.length) {
180
+ if (kept.length < after.length) {
91
181
  const left = {
92
182
  ...head,
93
- after: owing
183
+ after: kept
94
184
  };
95
- if (owing.length === 0) delete left.after;
185
+ if (kept.length === 0) delete left.after;
96
186
  const change = {
97
187
  id: newChangeId(),
98
188
  expect: version,
@@ -111,9 +201,10 @@ const followersRead = {
111
201
  limit: 20
112
202
  };
113
203
  /**
114
- * Makes `git.deliver` due now in each scope whose `followers` row, `snapshot`'s last result, has a
115
- * `seq` the cursor at `delivered` has passed, then deletes their rows in one commit at its
116
- * version: one that loses leaves them for the next send, which wakes them again.
204
+ * Makes each delivery's job due now in each scope whose `followers` row, `snapshot`'s last result, has a
205
+ * `seq` the low-water mark at `delivered` has passed, so every delivery has, then deletes their
206
+ * rows in one commit at its version: one that loses leaves them for the next send, which wakes
207
+ * them again. A waiter a slower delivery still holds looks again after `followWait`.
117
208
  */
118
209
  const wakeFollowers = /* @__PURE__ */ Effect.fnUntraced(function* (parts, store, scope, snapshot, delivered) {
119
210
  const woken = [];
@@ -121,7 +212,7 @@ const wakeFollowers = /* @__PURE__ */ Effect.fnUntraced(function* (parts, store,
121
212
  const { id } = row;
122
213
  const { seq } = yield* decodeRow(Follower, row, `The follower '${id}'`);
123
214
  if (seq > delivered.seq) continue;
124
- if (isInternalId(id)) yield* storeCall(() => parts.store.add(repoScope(id), { jobs: [parts.wake()] }));
215
+ if (isInternalId(id)) yield* storeCall(() => parts.store.add(repoScope(id), { jobs: [...parts.wake()] }));
125
216
  woken.push({
126
217
  collection: row.collection,
127
218
  id
@@ -136,13 +227,47 @@ const wakeFollowers = /* @__PURE__ */ Effect.fnUntraced(function* (parts, store,
136
227
  yield* storeCall(() => store.commit(scope, change));
137
228
  });
138
229
  /**
139
- * Deletes what's delivered and old: each delivered event older than `keptFor`, and the oldest
140
- * past the kept budget whatever their age, with the cursor rows behind the newest, at most
141
- * `trimBatch` of each. Answers the bytes of events it deleted, and `at`: when the oldest event left
142
- * is due to be trimmed, now when there's more to trim, or `undefined` when none is left. `lost`
143
- * says a write since its read beat its commit, so it trimmed nothing and should run again.
230
+ * Moves the low-water mark up to the slowest active cursor, and wakes the followers it passes, for
231
+ * a drained try whose own cursor is past the mark: a try that stopped between `moveCursor`'s two
232
+ * writes left it behind, and no send is left to move it.
144
233
  */
145
- const trim = /* @__PURE__ */ Effect.fnUntraced(function* (store, scope, now) {
234
+ const settleMark = /* @__PURE__ */ Effect.fnUntraced(function* (parts, store, scope) {
235
+ const reads = [
236
+ ...outboxReads,
237
+ ...parts.deliveries.map(cursorRead),
238
+ followersRead
239
+ ];
240
+ const snapshot = yield* storeCall(() => store.read(scope, reads));
241
+ const { delivered } = yield* decodeOutbox(snapshot.results);
242
+ const cursors = yield* decodeCursors(parts.deliveries, snapshot.results.slice(2));
243
+ const own = cursors.get(parts.delivery);
244
+ if (own === void 0) return;
245
+ const mark = yield* moveCursor(store, scope, parts.delivery, own, {
246
+ delivered,
247
+ cursors
248
+ });
249
+ yield* wakeFollowers(parts, store, scope, snapshot, mark);
250
+ });
251
+ /**
252
+ * Deletes what every active delivery has passed and is old: each event behind the slowest cursor
253
+ * older than `keptFor`, and the oldest past the kept budget whatever their age, with the low-water
254
+ * and cursor rows behind each newest, and a removed delivery's cursor and `failed` rows, at most
255
+ * `trimBatch` of each. A delivery with no cursor yet holds every event. Answers the bytes of events
256
+ * it deleted, and `at`: now when `delivery` has events left to send or there's more to trim; when
257
+ * the oldest event left is due to be trimmed; or `undefined` when none is left, or a slower
258
+ * delivery holds it, whose own tries trim once they pass it. `lost` says a write since its read
259
+ * beat its commit, so it trimmed nothing and should run again.
260
+ */
261
+ const trim = /* @__PURE__ */ Effect.fnUntraced(function* (parts, store, scope, now) {
262
+ const { delivery, deliveries } = parts;
263
+ const sweep = [{
264
+ collection: outboxCollections.cursors,
265
+ limit: 1001
266
+ }, {
267
+ collection: outboxCollections.outbox,
268
+ prefix: failedPrefix,
269
+ limit: 1001
270
+ }];
146
271
  const reads = (limit) => [
147
272
  {
148
273
  collection: headKey.collection,
@@ -155,22 +280,35 @@ const trim = /* @__PURE__ */ Effect.fnUntraced(function* (store, scope, now) {
155
280
  {
156
281
  collection: outboxCollections.events,
157
282
  limit
158
- }
283
+ },
284
+ ...deliveries.map(cursorRead),
285
+ ...sweep
159
286
  ];
160
287
  const { snapshot, limit } = yield* readEvents(store, scope, reads, trimBatch);
161
- const [headRows = [], cursorRows = [], eventRows = []] = snapshot.results;
162
- const { head, delivered } = yield* decodeOutbox([headRows, cursorRows]);
288
+ const [headRows = [], markRows = [], eventRows = []] = snapshot.results;
289
+ const newest = snapshot.results.slice(3, 3 + deliveries.length);
290
+ const [cursorRows = [], failedRows = []] = snapshot.results.slice(3 + deliveries.length);
291
+ const { head } = yield* decodeOutbox([headRows, markRows]);
163
292
  const events = yield* decodeEvents(eventRows);
164
- let kept = delivered.bytes - head.trimmed;
165
- let trimmed = 0;
166
- const gone = cursorRows.slice(1).map(({ collection, id }) => ({
293
+ const cursors = yield* decodeCursors(deliveries, newest);
294
+ const floor = slowest([...cursors.values()]);
295
+ const own = cursors.get(delivery);
296
+ const keep = new Set(newest.flatMap((rows) => rows.slice(0, 1).map((row) => row.id)));
297
+ const failing = new Set(deliveries.map((name) => failedKeyOf(name).id));
298
+ const gone = [
299
+ ...markRows.slice(1),
300
+ ...cursorRows.filter((row) => !keep.has(row.id)),
301
+ ...failedRows.filter((row) => !failing.has(row.id))
302
+ ].map(({ collection, id }) => ({
167
303
  collection,
168
304
  id
169
305
  }));
306
+ let kept = floor === void 0 ? 0 : floor.bytes - head.trimmed;
307
+ let trimmed = 0;
170
308
  let left;
171
309
  for (const event of events) {
172
310
  const old = event.at + 864e5 <= now || kept > eventLimits.kept;
173
- if (event.seq > delivered.seq || !old) {
311
+ if (floor === void 0 || event.seq > floor.seq || !old) {
174
312
  left = event;
175
313
  break;
176
314
  }
@@ -204,11 +342,21 @@ const trim = /* @__PURE__ */ Effect.fnUntraced(function* (store, scope, now) {
204
342
  lost: true
205
343
  };
206
344
  }
207
- if (left !== void 0) return {
208
- trimmed,
209
- at: left.seq > delivered.seq ? now : left.at + keptFor
210
- };
211
- const more = events.length === limit || cursorRows.length > trimBatch;
345
+ if (left !== void 0) {
346
+ if (own === void 0 || left.seq > own.seq) return {
347
+ trimmed,
348
+ at: now
349
+ };
350
+ if (floor === void 0 || left.seq > floor.seq) return {
351
+ trimmed,
352
+ at: void 0
353
+ };
354
+ return {
355
+ trimmed,
356
+ at: left.at + keptFor
357
+ };
358
+ }
359
+ const more = events.length === limit || markRows.length > trimBatch || cursorRows.length > trimBatch || failedRows.length > trimBatch;
212
360
  return {
213
361
  trimmed,
214
362
  at: more ? now : void 0
@@ -228,13 +376,19 @@ const trimsFirst = (head, delivered) => delivered.bytes - head.trimmed > eventLi
228
376
  /** How many times a try reads its scope while each read settles a name first. */
229
377
  const settleReads = 2;
230
378
  /**
231
- * Reads the scope's outbox once its name is settled: a create's or a rename's first try settles
232
- * it, reads again and goes on, so it sends in the same try (#1506). Answers `undefined` when each
233
- * of `settleReads` reads found a name to settle, a write racing the settle, for the next try.
379
+ * Reads the scope's outbox, with `delivery`'s cursor, once its name is settled: a create's or a
380
+ * rename's first try settles it, reads again and goes on, so it sends in the same try (#1506).
381
+ * Answers `undefined` when each of `settleReads` reads found a name to settle, a write racing the
382
+ * settle, for the next try.
234
383
  */
235
- const readSettled = /* @__PURE__ */ Effect.fnUntraced(function* (store, scope) {
384
+ const readSettled = /* @__PURE__ */ Effect.fnUntraced(function* (store, scope, delivery) {
236
385
  for (let reads = 0; reads < settleReads; reads += 1) {
237
- const snapshot = yield* storeCall(() => store.read(scope, [selfKey, ...outboxReads]));
386
+ const all = [
387
+ selfKey,
388
+ ...outboxReads,
389
+ cursorRead(delivery)
390
+ ];
391
+ const snapshot = yield* storeCall(() => store.read(scope, all));
238
392
  const record = yield* decodeRecord(snapshot.results[0]?.[0]);
239
393
  const { head, delivered } = yield* decodeOutbox(snapshot.results.slice(1));
240
394
  const read = {
@@ -242,10 +396,12 @@ const readSettled = /* @__PURE__ */ Effect.fnUntraced(function* (store, scope) {
242
396
  head,
243
397
  record
244
398
  };
245
- if (!(yield* settleName(store, scope, read))) return {
399
+ if (yield* settleName(store, scope, read)) continue;
400
+ return {
246
401
  snapshot,
247
402
  head,
248
- delivered
403
+ delivered,
404
+ cursor: yield* cursorOf(store, scope, head, snapshot.results[3])
249
405
  };
250
406
  }
251
407
  });
@@ -257,17 +413,17 @@ const readSettled = /* @__PURE__ */ Effect.fnUntraced(function* (store, scope) {
257
413
  */
258
414
  const prepare = /* @__PURE__ */ Effect.fnUntraced(function* (parts, job, store) {
259
415
  const { scope } = job;
260
- const settled = yield* readSettled(store, scope);
416
+ const settled = yield* readSettled(store, scope, parts.delivery);
261
417
  if (settled === void 0) return again();
262
- const { snapshot, head, delivered } = settled;
418
+ const { snapshot, head, delivered, cursor } = settled;
263
419
  if (trimsFirst(head, delivered)) {
264
- const { trimmed, lost } = yield* trim(store, scope, yield* parts.now);
420
+ const { trimmed, lost } = yield* trim(parts, store, scope, yield* parts.now);
265
421
  if (trimmed > 0 || lost === true) return again();
266
422
  }
267
423
  const held = yield* followed(parts, store, scope, snapshot.version, head);
268
424
  const waited = again((yield* parts.now) + followWait);
269
- if (held !== void 0 && held <= delivered.seq + 1) return waited;
270
- const after = eventId(delivered.seq);
425
+ if (held !== void 0 && held <= cursor.seq + 1) return waited;
426
+ const after = eventId(cursor.seq);
271
427
  const reads = (limit) => [{
272
428
  collection: outboxCollections.events,
273
429
  after,
@@ -281,23 +437,24 @@ const prepare = /* @__PURE__ */ Effect.fnUntraced(function* (parts, job, store)
281
437
  const sent = events.slice(0, limit - 1);
282
438
  return {
283
439
  kind: "send",
284
- cursor: delivered,
440
+ cursor,
285
441
  events: sent,
286
442
  last: fetched.length < limit && sent.length === events.length
287
443
  };
288
444
  }
289
445
  if (held !== void 0) return waited;
290
446
  if (fetched.length > 0) return again();
291
- const { at } = yield* trim(store, scope, yield* parts.now);
447
+ if (cursor.seq > delivered.seq) yield* settleMark(parts, store, scope);
448
+ const { at } = yield* trim(parts, store, scope, yield* parts.now);
292
449
  return settle(at === void 0 ? void 0 : {
293
450
  kind: "more",
294
451
  at
295
452
  });
296
453
  });
297
454
  /**
298
- * A try's last part, once the host took the batch: the cursor moves past it, in a new row the
299
- * try's claim fences, so a stale try moves nothing. A skip a discard recorded at the same `seq`
300
- * already moved it. A batch that sent the head's last event trims and settles as a drained
455
+ * A try's last part, once the host took the batch: the delivery's cursor moves past it, in a new
456
+ * row the try's claim fences, so a stale try moves nothing, and the low-water mark with it when
457
+ * it was the slowest. A skip a discard recorded at the same `seq` already moved it. A batch that sent the head's last event trims and settles as a drained
301
458
  * `prepare` would, when `sentLast` holds. Otherwise one that sent the last event it read trims
302
459
  * past the kept budget, up to `trimBatch`, and each try after trims on. Steady traffic whose batches outweigh the smaller
303
460
  * events a trim removes can hold kept past the budget while it lasts, under `drainCeiling`. Past
@@ -308,35 +465,43 @@ const advance = /* @__PURE__ */ Effect.fnUntraced(function* (parts, job, store,
308
465
  const last = batch.events.at(-1);
309
466
  const again = { kind: "more" };
310
467
  if (last === void 0) return again;
311
- const snapshot = yield* storeCall(() => store.read(scope, [...outboxReads, followersRead]));
468
+ const { deliveries } = parts;
469
+ const reads = [
470
+ ...outboxReads,
471
+ ...deliveries.map(cursorRead),
472
+ followersRead
473
+ ];
474
+ const snapshot = yield* storeCall(() => store.read(scope, reads));
312
475
  const outbox = yield* decodeOutbox(snapshot.results);
476
+ const cursors = yield* decodeCursors(deliveries, snapshot.results.slice(2));
477
+ const own = cursors.get(parts.delivery);
313
478
  const bytes = batch.events.reduce((sum, event) => sum + eventBytes(event), batch.cursor.bytes);
314
479
  const value = {
315
480
  seq: last.seq,
316
481
  bytes
317
482
  };
318
- const row = {
319
- collection: outboxCollections.delivered,
320
- id: cursorId(last.seq),
321
- value
483
+ const read = {
484
+ delivered: outbox.delivered,
485
+ cursors
322
486
  };
323
- if (outbox.delivered.seq < last.seq) yield* addCursor(store, scope, row);
324
- yield* wakeFollowers(parts, store, scope, snapshot, value);
325
- if (sentLast(outbox, last, value)) {
326
- const { at } = yield* trim(store, scope, yield* parts.now);
487
+ const mark = yield* moveCursor(store, scope, parts.delivery, value, read);
488
+ yield* wakeFollowers(parts, store, scope, snapshot, mark);
489
+ if (sentLast(outbox, own, last, mark)) {
490
+ const { at } = yield* trim(parts, store, scope, yield* parts.now);
327
491
  return at === void 0 ? void 0 : {
328
492
  kind: "more",
329
493
  at
330
494
  };
331
495
  }
332
- if (batch.last && bytes - outbox.head.trimmed > eventLimits.kept) yield* trim(store, scope, yield* parts.now);
496
+ if (batch.last && mark.bytes - outbox.head.trimmed > eventLimits.kept) yield* trim(parts, store, scope, yield* parts.now);
333
497
  return again;
334
498
  });
335
499
  /**
336
500
  * Whether the try that sent `last` leaves its scope so the next try's `prepare` would only trim
337
- * and settle, as `outbox`, read before the cursor moved to `moved`, shows. Then the try trims and
338
- * settles itself, with the same answer, and saves the next try (#1506). Any doubt is `false`, and
339
- * the next try runs. Each part is a step `prepare` would take first, or one that finds more:
501
+ * and settle, as `outbox` and the delivery's cursor `own`, read before it moved and the low-water
502
+ * mark moved to `mark`, show. Then the try trims and settles itself, with the same answer, and
503
+ * saves the next try (#1506). Any doubt is `false`, and the next try runs. Each part is a step
504
+ * `prepare` would take first, or one that finds more:
340
505
  * - the cursor moves to the head's last event, so a read past it finds no event to send;
341
506
  * - no name waits to be settled, which `settleName` would write first;
342
507
  * - no scope it follows, which `followed` would read, and which may hold it;
@@ -344,7 +509,7 @@ const advance = /* @__PURE__ */ Effect.fnUntraced(function* (parts, job, store,
344
509
  * A write since the read adds events and saves the job while this try holds it, so the queue runs
345
510
  * it again by the earlier of the save and the settle's `at`, as it does for a drained `prepare`.
346
511
  */
347
- const sentLast = (outbox, last, moved) => outbox.delivered.seq < last.seq && outbox.head.next === last.seq + 1 && outbox.head.unconfirmed === void 0 && outbox.head.after === void 0 && !trimsFirst(outbox.head, moved);
512
+ const sentLast = (outbox, own, last, mark) => (own === void 0 || own.seq < last.seq) && outbox.head.next === last.seq + 1 && outbox.head.unconfirmed === void 0 && outbox.head.after === void 0 && !trimsFirst(outbox.head, mark);
348
513
  /** Adds a row, unless one is there by its key: then that one stays. */
349
514
  const addNew = (store, scope, row) => storeCall(() => store.add(scope, { put: [row] })).pipe(Effect.catchIf((error) => error.code === "ALREADY_EXISTS", () => Effect.void));
350
515
  /** Adds a cursor row: one already at its `seq`, a discard's skip, moved the cursor there first. */
@@ -354,20 +519,22 @@ const addCursor = (store, scope, row) => addNew(store, scope, row);
354
519
  * once the row is deleted and added again.
355
520
  */
356
521
  const newerThan = (note, job) => note.claim > job.claim;
357
- /** Drops the scope's note when it's `job`'s try's or an older one's, whose `onFailure` has run. */
522
+ /** Drops the note when it's `job`'s try's or an older one's, whose `onFailure` has run. */
358
523
  const dropNote = (sending, job) => {
359
524
  const note = sending.get(job.scope);
360
525
  if (note !== void 0 && !newerThan(note, job)) sending.delete(job.scope);
361
526
  };
362
527
  /**
363
- * `git.deliver`'s try, in one scope: up to 100 events past its cursor, to the host's `events`. A
528
+ * A delivery's try, in one scope: up to 100 events past its cursor, to the host's `events`. A
364
529
  * throw or a timeout sends the same batch again, so later events wait behind one that fails; once
365
530
  * the host took it, the cursor moves past it. When none is left to send, the try trims and sleeps
366
531
  * until the oldest event kept is due to go. `sending` notes the batch while the host has it.
367
532
  */
368
- const deliverHandler = (parts, sending) => async (job, context) => {
533
+ const deliverTry = (parts, sending) => Effect.fnUntraced(function* () {
534
+ const job = yield* CurrentJob.current;
535
+ const store = yield* CurrentJob.store;
369
536
  dropNote(sending, job);
370
- const prepared = await runJobTry(prepare(parts, job, context.store));
537
+ const prepared = yield* prepare(parts, job, store);
371
538
  if (prepared.kind === "settle") return prepared.outcome;
372
539
  const seq = prepared.events[0]?.seq;
373
540
  dropNote(sending, job);
@@ -376,41 +543,44 @@ const deliverHandler = (parts, sending) => async (job, context) => {
376
543
  seq
377
544
  });
378
545
  const events = prepared.events.map(shownEvent);
379
- await parts.handler({
546
+ const signal = yield* CurrentJob.signal;
547
+ yield* settledCall(async () => parts.handler({
380
548
  events,
381
549
  attempt: job.attempt,
382
- signal: context.signal
383
- });
550
+ signal
551
+ }));
384
552
  dropNote(sending, job);
385
- return runJobTry(advance(parts, job, context.store, prepared));
386
- };
553
+ return yield* advance(parts, job, store, prepared);
554
+ });
387
555
  /**
388
- * The `failed` row for `job`'s last try: the batch `sending` noted for that try, by its `claim`,
389
- * or none. A claim out of tries runs no try, so its `onFailure` finds a note of an older try, or
390
- * none, and names no event.
556
+ * `delivery`'s `failed` row for `job`'s last try: the batch `sending` noted for that try, by its
557
+ * `claim`, or none. A claim out of tries runs no try, so its `onFailure` finds a note of an older
558
+ * try, or none, and names no event.
391
559
  */
392
- const failedTry = (sending, job) => {
560
+ const failedTry = (sending, delivery, job) => {
393
561
  const noted = sending.get(job.scope);
394
562
  dropNote(sending, job);
395
- return noted?.claim === job.claim ? {
563
+ const value = noted?.claim === job.claim ? {
396
564
  claim: noted.claim,
397
565
  seq: noted.seq
398
566
  } : { claim: job.claim };
567
+ return {
568
+ ...failedKeyOf(delivery),
569
+ value
570
+ };
399
571
  };
400
572
  /**
401
573
  * Writes `repo.work.failed` when one of a repo's jobs fails its last try, behind the events it
402
- * held. It passes the events budget. A failed `git.deliver` saves no `git.deliver` beside it,
403
- * which would run the job it reports again, and passes `tried`, which names the event its last
404
- * try sent, for the `failed` row a discard skips by, in the same commit. It writes through the
574
+ * held. It passes the events budget. A failed delivery's job saves every delivery's job but its
575
+ * own, which would run the job it reports again: the queue keeps any other failed job failed, so
576
+ * each stays as its own tries left it. It passes `tried`, which names the event its last try sent,
577
+ * for the `failed` row a discard skips by, in the same commit. It writes through the
405
578
  * try's `context.store`, fenced by its claim, so a failure whose lease has ended writes nothing.
406
579
  */
407
580
  const workFailed = (log, now, tried) => {
408
581
  const write = Effect.fnUntraced(function* (job, failure, store) {
409
582
  if (scopeRepo(job.scope) === void 0) return;
410
- const failed = tried === void 0 ? [] : [{
411
- ...failedKey,
412
- value: tried(job)
413
- }];
583
+ const failed = tried === void 0 ? [] : [tried(job)];
414
584
  for (let attempt = 0; attempt < failedTries; attempt += 1) {
415
585
  const snapshot = yield* storeCall(() => store.read(job.scope, [selfKey, ...outboxReads]));
416
586
  const record = yield* decodeRecord(snapshot.results[0]?.[0]);
@@ -437,7 +607,7 @@ const workFailed = (log, now, tried) => {
437
607
  exempt: true
438
608
  };
439
609
  const appended = yield* appendEvents(log, outbox, [event], options);
440
- const jobs = job.name === "git.deliver" ? [] : appended.jobs;
610
+ const jobs = appended.jobs.filter((saved) => !sameJob(saved, job));
441
611
  const change = {
442
612
  id: newChangeId(),
443
613
  expect: snapshot.version,
@@ -451,26 +621,29 @@ const workFailed = (log, now, tried) => {
451
621
  }))).ok) return;
452
622
  }
453
623
  });
454
- const handler = (job, failure, context) => runJobTry(write(job, failure, context.store));
455
- return handler;
624
+ return (_data, failure) => Effect.flatMap(Effect.all([CurrentJob.current, CurrentJob.store]), ([job, store]) => write(job, failure, store));
456
625
  };
457
- /** Whether `job` is a scope's `git.deliver`. */
458
- const isDeliverJob = (job) => job.name === "git.deliver" && job.key === "deliver";
626
+ /** Whether `a` and `b` are one job of a scope: its name and its key. */
627
+ const sameJob = (a, b) => a.name === b.name && a.key === b.key;
628
+ /** Whether `job` is a scope's job of a delivery, `git.deliver.<name>`. */
629
+ const isDeliverJob = (job) => deliveryOf(job.name) !== void 0 && job.key === "deliver";
459
630
  /**
460
- * A discard's event that has gone since it read the cursor: delivery passed it, by a retry, and a
461
- * trim deleted it, so there is nothing to skip. A trim deletes only delivered events, so one gone
462
- * that the cursor hasn't passed is `DATA_LOSS`.
631
+ * A discard's event that has gone since it read the cursor: `delivery` passed it, by a retry, and
632
+ * a trim deleted it, so there is nothing to skip. A trim deletes only events every delivery has
633
+ * passed, so one gone that the cursor hasn't passed is `DATA_LOSS`.
463
634
  */
464
- const passed = /* @__PURE__ */ Effect.fnUntraced(function* (store, scope, seq) {
465
- const snapshot = yield* storeCall(() => store.read(scope, [...outboxReads]));
466
- const { delivered } = yield* decodeOutbox(snapshot.results);
467
- if (delivered.seq < seq) return yield* fail("DATA_LOSS", `The event ${seq} has gone`);
635
+ const passed = /* @__PURE__ */ Effect.fnUntraced(function* (store, scope, delivery, seq) {
636
+ const snapshot = yield* storeCall(() => store.read(scope, [...outboxReads, cursorRead(delivery)]));
637
+ const { head } = yield* decodeOutbox(snapshot.results);
638
+ if ((yield* cursorOf(store, scope, head, snapshot.results[2])).seq < seq) return yield* fail("DATA_LOSS", `The event ${seq} has gone`);
468
639
  });
469
640
  /** A retry that found the job no longer failed: a discard sent at once, or a retry, ran first. */
470
641
  const notFailed = (error) => error.code === "FAILED_PRECONDITION" && isInfraError(error.cause) && error.cause.reason === "not-failed" || error.code === "NOT_FOUND";
471
642
  /**
472
- * A discard of a failed `git.deliver`: a skip past the event its last try sent, then the job due
473
- * again for the rest. The queue makes a failed job due only by its own `retry`, so the two are
643
+ * A discard of `target`, a delivery's failed job, of the active `deliveries`: a skip of its
644
+ * delivery's cursor past the event its last try sent, with the low-water mark when it was the
645
+ * slowest, then that job due again for the rest. Every other delivery's job, failed or not, and
646
+ * its cursor, stay as they are. The queue makes a failed job due only by its own `retry`, so the two are
474
647
  * separate writes. The skip is the cursor row past the cursor, added only when the `failed` row
475
648
  * `workFailed` wrote names the try the job lists, by its `claim`, and that event: a try can fail
476
649
  * before it sends one, and a row from an older failure names an older claim, even one in the same
@@ -478,18 +651,31 @@ const notFailed = (error) => error.code === "FAILED_PRECONDITION" && isInfraErro
478
651
  * discard sent again finds the cursor past that event, or the job no longer failed, so it skips
479
652
  * once. `verification/Discards.tla` checks it.
480
653
  */
481
- const skipFailed = /* @__PURE__ */ Effect.fnUntraced(function* (store, jobs, scope) {
482
- const failed = (yield* storeCall(() => jobs.list(scope, { state: "failed" }))).items.find(isDeliverJob);
654
+ const skipFailed = /* @__PURE__ */ Effect.fnUntraced(function* (store, jobs, scope, target, deliveries) {
655
+ const delivery = deliveryOf(target.name);
656
+ if (delivery === void 0 || !deliveries.includes(delivery)) return;
657
+ const request = {
658
+ state: "failed",
659
+ name: target.name
660
+ };
661
+ const failed = (yield* storeCall(() => jobs.list(scope, request))).items.find((item) => sameJob(item, target));
483
662
  if (failed === void 0) return;
484
- const reads = [...outboxReads, {
485
- collection: failedKey.collection,
486
- id: failedKey.id
487
- }];
663
+ const failedRow = failedKeyOf(delivery);
664
+ const reads = [
665
+ ...outboxReads,
666
+ {
667
+ collection: failedRow.collection,
668
+ id: failedRow.id
669
+ },
670
+ ...deliveries.map(cursorRead)
671
+ ];
488
672
  const snapshot = yield* storeCall(() => store.read(scope, reads));
489
- const { delivered } = yield* decodeOutbox(snapshot.results);
673
+ const { head, delivered } = yield* decodeOutbox(snapshot.results);
490
674
  const [row] = snapshot.results[2] ?? [];
491
675
  const tried = row === void 0 ? void 0 : yield* decodeRow(FailedTry, row, "The failed try");
492
- const seq = delivered.seq + 1;
676
+ const cursors = yield* decodeCursors(deliveries, snapshot.results.slice(3));
677
+ const cursor = cursors.get(delivery) ?? (yield* retainedStart(store, scope, head));
678
+ const seq = cursor.seq + 1;
493
679
  if (tried?.seq === seq && tried.claim === failed.claim) {
494
680
  const read = {
495
681
  collection: outboxCollections.events,
@@ -497,23 +683,21 @@ const skipFailed = /* @__PURE__ */ Effect.fnUntraced(function* (store, jobs, sco
497
683
  };
498
684
  const next = yield* storeCall(() => store.read(scope, [read]));
499
685
  const [event] = yield* decodeEvents(next.results[0] ?? []);
500
- if (event === void 0) yield* passed(store, scope, seq);
686
+ if (event === void 0) yield* passed(store, scope, delivery, seq);
501
687
  else {
502
688
  const value = {
503
689
  seq,
504
- bytes: delivered.bytes + eventBytes(event)
505
- };
506
- const cursor = {
507
- collection: outboxCollections.delivered,
508
- id: cursorId(seq),
509
- value
690
+ bytes: cursor.bytes + eventBytes(event)
510
691
  };
511
- yield* addCursor(store, scope, cursor);
692
+ yield* moveCursor(store, scope, delivery, value, {
693
+ delivered,
694
+ cursors
695
+ });
512
696
  }
513
697
  }
514
698
  const job = {
515
- name: deliverJobName,
516
- key: deliverJobKey
699
+ name: failed.name,
700
+ key: failed.key
517
701
  };
518
702
  yield* storeCall(() => jobs.retry(scope, job)).pipe(Effect.catchIf(notFailed, () => Effect.void));
519
703
  });
@@ -529,33 +713,71 @@ const checkDeliveryTimeout = ({ timeout }) => {
529
713
  throw gitError("INVALID_ARGUMENT", message);
530
714
  };
531
715
  /**
532
- * `git.deliver`, over the host's `events`: tried again as the host's `retry` says, or for three
533
- * days at most an hour apart. Its last failure writes `repo.work.failed`, which waits behind it.
716
+ * Throws an `INVALID_ARGUMENT` `GitError` unless there are 1 to `deliveryLimit` deliveries, each
717
+ * named, with no name twice, each job name within a job name's 256 bytes as UTF-8 and holding no
718
+ * U+0000 or lone surrogate, and each timeout within `checkDeliveryTimeout`'s. A store that writes a
719
+ * lone surrogate as U+FFFD would give two such names one cursor.
534
720
  */
535
- const deliverJob = (parts) => {
536
- const log = { deliverJob: () => definition.job({
537
- key: deliverJobKey,
538
- data: {}
539
- }) };
540
- const sending = /* @__PURE__ */ new Map();
541
- const options = {
542
- name: deliverJobName,
543
- schema: deliverJobSchema,
544
- retry: parts.delivery.retry ?? defaultDeliverRetry,
545
- handler: deliverHandler({
546
- ...parts,
547
- handler: parts.delivery.handler,
548
- wake: log.deliverJob
549
- }, sending),
550
- onFailure: workFailed(log, parts.now, (job) => failedTry(sending, job))
721
+ const checkDeliveries = (deliveries) => {
722
+ const names = new Set(deliveries.map((delivery) => delivery.name));
723
+ if (deliveries.length === 0 || deliveries.length > 16) {
724
+ const message = `A server runs 1 to 16 deliveries, not ${deliveries.length}`;
725
+ throw gitError("INVALID_ARGUMENT", message);
726
+ }
727
+ if (names.size < deliveries.length) throw gitError("INVALID_ARGUMENT", "Each delivery has a name of its own");
728
+ for (const delivery of deliveries) {
729
+ const name = deliverJobName(delivery.name);
730
+ if (delivery.name === "" || name.includes("\0") || !isWellFormed(name) || utf8(name).length > limits.nameBytes) {
731
+ const message = `A delivery's name is set, holds no U+0000 or lone surrogate, and keeps '${deliverJobPrefix}<name>' within ${limits.nameBytes} bytes as UTF-8`;
732
+ throw gitError("INVALID_ARGUMENT", message);
733
+ }
734
+ checkDeliveryTimeout(delivery);
735
+ }
736
+ };
737
+ /**
738
+ * A job for each of `parts.deliveries`, `git.deliver.<name>`, over its host's handler: tried again
739
+ * as its `retry` says, or for three days at most an hour apart, within its own `timeout`, with its
740
+ * own cursor. Its last failure writes `repo.work.failed`, which waits behind it, and makes each
741
+ * other delivery's job due. The deliveries are checked before any job is made.
742
+ */
743
+ const deliverJobs = (parts) => {
744
+ checkDeliveries(parts.deliveries);
745
+ const names = parts.deliveries.map((delivery) => delivery.name);
746
+ const log = {
747
+ deliveries: names,
748
+ deliverJobs: () => definitions.map((definition) => definition.job({
749
+ key: deliverJobKey,
750
+ data: {}
751
+ }))
551
752
  };
552
- const { timeout } = parts.delivery;
553
- checkDeliveryTimeout(parts.delivery);
554
- if (timeout !== void 0) options.timeout = millis(timeout);
555
- const definition = defineJob(options);
753
+ const definitions = parts.deliveries.map((delivery) => {
754
+ const sending = /* @__PURE__ */ new Map();
755
+ const { timeout } = delivery;
756
+ const policy = {
757
+ payload: DeliverData,
758
+ retry: delivery.retry ?? defaultDeliverRetry
759
+ };
760
+ const DeliverJob = Job.make(deliverJobName(delivery.name), timeout === void 0 ? policy : {
761
+ ...policy,
762
+ timeout: millis(timeout)
763
+ });
764
+ const tryParts = {
765
+ ...parts,
766
+ delivery: delivery.name,
767
+ deliveries: names
768
+ };
769
+ const handler = deliverTry({
770
+ ...tryParts,
771
+ handler: delivery.handler,
772
+ wake: log.deliverJobs
773
+ }, sending);
774
+ const tried = (job) => failedTry(sending, delivery.name, job);
775
+ const onFailure = workFailed(log, parts.now, tried);
776
+ return madeJob(DeliverJob.definition(handler, { onFailure }));
777
+ });
556
778
  return {
557
- definition,
779
+ definitions,
558
780
  log
559
781
  };
560
782
  };
561
- export { checkDeliveryTimeout, defaultDeliverRetry, deliverHandler, deliverJob, deliverJobKey, deliverJobName, drainCeiling, eventDelivery, isDeliverJob, keptFor, notFailed, skipFailed, workFailed };
783
+ export { DeliverData, checkDeliveries, checkDeliveryTimeout, defaultDeliverRetry, deliverJobKey, deliverJobName, deliverJobs, deliverTry, deliveriesOf, drainCeiling, eventDelivery, eventsDelivery, isDeliverJob, keptFor, notFailed, skipFailed, workFailed };