@effect-agent/storage-memory 0.1.0-beta.9 → 0.1.0-beta.90

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 (34) hide show
  1. package/dist/MemoryMessageDeliveryStore.d.mts +15 -0
  2. package/dist/MemoryMessageDeliveryStore.mjs +147 -0
  3. package/dist/MemoryMessageDeliveryStore.mjs.map +1 -0
  4. package/dist/MemoryScheduleStore.d.mts +10 -0
  5. package/dist/MemoryScheduleStore.mjs +169 -0
  6. package/dist/MemoryScheduleStore.mjs.map +1 -0
  7. package/dist/MemorySemanticIndex.d.mts +21 -0
  8. package/dist/MemorySemanticIndex.mjs +222 -0
  9. package/dist/MemorySemanticIndex.mjs.map +1 -0
  10. package/dist/MemorySubmissionLedger.d.mts +24 -0
  11. package/dist/MemorySubmissionLedger.mjs +1058 -0
  12. package/dist/MemorySubmissionLedger.mjs.map +1 -0
  13. package/dist/MemorySubscriptionStore.d.mts +9 -0
  14. package/dist/MemorySubscriptionStore.mjs +849 -0
  15. package/dist/MemorySubscriptionStore.mjs.map +1 -0
  16. package/dist/MemoryThreadStore.d.mts +17 -0
  17. package/dist/MemoryThreadStore.mjs +377 -0
  18. package/dist/MemoryThreadStore.mjs.map +1 -0
  19. package/dist/index.d.mts +7 -30
  20. package/dist/index.mjs +7 -1298
  21. package/dist/rolldown-runtime-D7D4PA-g.mjs +13 -0
  22. package/package.json +1 -45
  23. package/src/MemoryMessageDeliveryStore.ts +293 -0
  24. package/src/MemoryScheduleStore.ts +328 -0
  25. package/src/MemorySemanticIndex.ts +357 -0
  26. package/src/{memory-ledger.ts → MemorySubmissionLedger.ts} +478 -86
  27. package/src/MemorySubscriptionStore.ts +1549 -0
  28. package/src/MemoryThreadStore.ts +766 -0
  29. package/src/index.ts +6 -2
  30. package/dist/index.mjs.map +0 -1
  31. package/dist/testing.d.mts +0 -2
  32. package/dist/testing.mjs +0 -2
  33. package/src/memory-storage.ts +0 -614
  34. package/src/testing.ts +0 -10
@@ -0,0 +1,849 @@
1
+ import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
2
+ import { Clock, Effect, Layer, Ref, Result, Schema } from "effect";
3
+ import { compareScheduleNames } from "effect-agent/schedule-transition";
4
+ import { Digest } from "effect-agent/records";
5
+ import { AcceptedEvent, DeliveryChange, SourcePartition, SubscriptionChange, SubscriptionDelivery, SubscriptionDeliveryKey, SubscriptionError, SubscriptionFailpoint, SubscriptionKey, SubscriptionLimits, SubscriptionName, SubscriptionRecord, SubscriptionRetentionPolicy, SubscriptionScanCursors, SubscriptionStore, subscriptionDeliveryKeyString, subscriptionKeyString } from "effect-agent/subscription";
6
+ import { applySubscriptionChange, applySubscriptionDeliveryChange, sameAcceptedEventIdentity, subscriptionCanSelect, subscriptionDeliveryCanSelect, validateEventRetention } from "effect-agent/subscription-transition";
7
+ //#region src/MemorySubscriptionStore.ts
8
+ var MemorySubscriptionStore_exports = /* @__PURE__ */ __exportAll({ memorySubscriptionStoreLayer: () => memorySubscriptionStoreLayer });
9
+ const error = (reason, code) => SubscriptionError.make({
10
+ reason,
11
+ code
12
+ });
13
+ const samePartition = (left, right) => left.tenantId === right.tenantId && left.address === right.address;
14
+ const sameSource = (left, right) => left.name === right.name && left.version === right.version;
15
+ const encode = (schema, value, code) => Result.try({
16
+ try: () => Schema.encodeSync(Schema.fromJsonString(schema))(value),
17
+ catch: () => error("corrupt", code)
18
+ });
19
+ const decode = (schema, value, code) => Result.try({
20
+ try: () => Schema.decodeSync(Schema.fromJsonString(schema))(value),
21
+ catch: () => error("corrupt", code)
22
+ });
23
+ const decodeEffect = (schema, value, code) => Effect.fromResult(decode(schema, value, code));
24
+ const validate = (schema, value, code) => Schema.decodeUnknownEffect(schema)(value).pipe(Effect.mapError(() => error("validation", code)));
25
+ const jsonBytes = (value) => new TextEncoder().encode(JSON.stringify(value)).byteLength;
26
+ const sameDeliveryIdentity = (left, right) => subscriptionDeliveryKeyString(left.key) === subscriptionDeliveryKeyString(right.key) && left.deliveryId === right.deliveryId && left.source.name === right.source.name && left.source.version === right.source.version && left.threadId === right.threadId && left.admissionKey === right.admissionKey && left.subscriptionFingerprint === right.subscriptionFingerprint && left.eventDigest === right.eventDigest;
27
+ const candidateIndexKey = (record) => JSON.stringify([
28
+ record.configuration.source.name,
29
+ record.configuration.source.version,
30
+ record.configuration.matchingKey
31
+ ]);
32
+ const eventCandidateIndexKey = (event) => JSON.stringify([
33
+ event.source.name,
34
+ event.source.version,
35
+ event.matchingKey
36
+ ]);
37
+ const sameEventIdentity = sameAcceptedEventIdentity;
38
+ const deliveryBelongsTo = (delivery, record, event) => subscriptionKeyString(delivery.key.subscription) === subscriptionKeyString(record.key) && delivery.key.eventId === event.eventId && sameSource(delivery.source, event.source);
39
+ const removeKeys = (keys, removed) => {
40
+ if (removed.size === 0) return keys;
41
+ const result = [...keys];
42
+ for (const key of removed) {
43
+ const index = upperBound(result, key) - 1;
44
+ if (result[index] === key) result.splice(index, 1);
45
+ }
46
+ return result;
47
+ };
48
+ const upperBound = (keys, after) => {
49
+ let low = 0;
50
+ let high = keys.length;
51
+ while (low < high) {
52
+ const middle = Math.floor((low + high) / 2);
53
+ const key = keys[middle];
54
+ if (key !== void 0 && compareScheduleNames(key, after) <= 0) low = middle + 1;
55
+ else high = middle;
56
+ }
57
+ return low;
58
+ };
59
+ const insertKey = (keys, key) => {
60
+ const at = upperBound(keys, key);
61
+ if (at > 0 && keys[at - 1] === key) return keys;
62
+ return [
63
+ ...keys.slice(0, at),
64
+ key,
65
+ ...keys.slice(at)
66
+ ];
67
+ };
68
+ const recoveryCountsAfter = (current, next, keys) => {
69
+ const counts = new Map(current.recoveryCounts);
70
+ for (const key of keys) {
71
+ const before = current.registrationIndex.get(key)?.recoveryKey;
72
+ const after = next.get(key)?.recoveryKey;
73
+ if (before === after) continue;
74
+ if (before !== null && before !== void 0) counts.set(before, (counts.get(before) ?? 0) - 1);
75
+ if (after !== null && after !== void 0) counts.set(after, (counts.get(after) ?? 0) + 1);
76
+ }
77
+ return counts;
78
+ };
79
+ const makeMemorySubscriptionStore = Effect.fn("makeMemorySubscriptionStore")(function* (ownedPartition) {
80
+ const partition = yield* validate(SourcePartition, ownedPartition, "partition");
81
+ const state = yield* Ref.make({
82
+ sequence: 0,
83
+ retentionDeadline: null,
84
+ tombstoneCount: 0,
85
+ eventKeys: [],
86
+ deliveryKeys: [],
87
+ maintenanceEvents: "",
88
+ maintenanceDeliveries: "",
89
+ recoveryCounts: /* @__PURE__ */ new Map(),
90
+ eventDeliveryCounts: /* @__PURE__ */ new Map(),
91
+ retentionHorizon: null,
92
+ registrations: /* @__PURE__ */ new Map(),
93
+ events: /* @__PURE__ */ new Map(),
94
+ deliveries: /* @__PURE__ */ new Map(),
95
+ registrationIndex: /* @__PURE__ */ new Map(),
96
+ candidateIndex: /* @__PURE__ */ new Map(),
97
+ ownerRegistrationCounts: /* @__PURE__ */ new Map(),
98
+ eventIndex: /* @__PURE__ */ new Map(),
99
+ deliveryIndex: /* @__PURE__ */ new Map(),
100
+ ownerDeliveryCounts: /* @__PURE__ */ new Map(),
101
+ scanCursors: {
102
+ events: "",
103
+ deliveries: "",
104
+ recovery: 0
105
+ }
106
+ });
107
+ const failpoint = yield* SubscriptionFailpoint;
108
+ const requirePartition = (value, code) => samePartition(value.partition, partition) ? Effect.succeed(value) : Effect.fail(error("validation", code));
109
+ const requireKey = (key, code) => validate(SubscriptionKey, key, code).pipe(Effect.flatMap((decoded) => requirePartition(decoded, code)));
110
+ const register = Effect.fn("MemorySubscriptionStore.register")(function* (input, inputLimits) {
111
+ const record = yield* validate(SubscriptionRecord, input, "register-record");
112
+ const limits = yield* validate(SubscriptionLimits, inputLimits, "register-limits");
113
+ yield* requirePartition(record.key, "register-partition");
114
+ yield* failpoint.hit("subscription:register:before");
115
+ const result = yield* Effect.uninterruptible(Ref.modify(state, (current) => {
116
+ const key = subscriptionKeyString(record.key);
117
+ const existingText = current.registrations.get(key);
118
+ if (existingText !== void 0) {
119
+ const existing = decode(SubscriptionRecord, existingText, "register-existing");
120
+ if (Result.isFailure(existing)) return [Result.fail(existing.failure), current];
121
+ return existing.success.creationFingerprint === record.creationFingerprint ? [Result.succeed(existing.success), current] : [Result.fail(error("conflict", "registration-identity")), current];
122
+ }
123
+ if (jsonBytes(record.configuration.context) > limits.maxContextBytes) return [Result.fail(error("capacity", "context-bytes")), current];
124
+ if (jsonBytes(record.configuration.parameters) > limits.maxPayloadBytes) return [Result.fail(error("capacity", "parameters-bytes")), current];
125
+ if (record.configuration.expiresAtMillis !== null && record.configuration.expiresAtMillis - record.createdAtMillis > limits.maxLifetimeMillis) return [Result.fail(error("capacity", "lifetime")), current];
126
+ if (current.registrations.size >= limits.maxRegistrations) return [Result.fail(error("capacity", "registrations")), current];
127
+ const ownerCount = current.ownerRegistrationCounts.get(record.key.ownerId) ?? 0;
128
+ if (ownerCount >= limits.maxRegistrationsPerOwner) return [Result.fail(error("capacity", "owner-registrations")), current];
129
+ const assigned = {
130
+ ...record,
131
+ ordinal: current.sequence + 1
132
+ };
133
+ const encoded = encode(SubscriptionRecord, assigned, "register-encode");
134
+ if (Result.isFailure(encoded)) return [Result.fail(encoded.failure), current];
135
+ const registrations = new Map(current.registrations);
136
+ registrations.set(key, encoded.success);
137
+ const registrationIndex = new Map(current.registrationIndex);
138
+ registrationIndex.set(key, {
139
+ key: assigned.key,
140
+ ordinal: assigned.ordinal,
141
+ state: assigned.state,
142
+ recoveryKey: assigned.recovery === null ? null : candidateIndexKey(assigned),
143
+ recoveryAt: assigned.recovery?.nextAttemptAtMillis ?? null
144
+ });
145
+ const candidateIndex = new Map(current.candidateIndex);
146
+ const candidateKey = candidateIndexKey(assigned);
147
+ candidateIndex.set(candidateKey, [...candidateIndex.get(candidateKey) ?? [], key]);
148
+ const ownerRegistrationCounts = new Map(current.ownerRegistrationCounts);
149
+ ownerRegistrationCounts.set(assigned.key.ownerId, ownerCount + 1);
150
+ return [Result.succeed(assigned), {
151
+ ...current,
152
+ sequence: assigned.ordinal,
153
+ registrations,
154
+ registrationIndex,
155
+ recoveryCounts: recoveryCountsAfter(current, registrationIndex, [key]),
156
+ candidateIndex,
157
+ ownerRegistrationCounts
158
+ }];
159
+ })).pipe(Effect.flatMap(Effect.fromResult));
160
+ yield* failpoint.hit("subscription:register:after");
161
+ return result;
162
+ });
163
+ const get = Effect.fn("MemorySubscriptionStore.get")(function* (input) {
164
+ const key = yield* requireKey(input, "get-key");
165
+ const text = (yield* Ref.get(state)).registrations.get(subscriptionKeyString(key));
166
+ return text === void 0 ? null : yield* decodeEffect(SubscriptionRecord, text, "get-record");
167
+ });
168
+ const list = Effect.fn("MemorySubscriptionStore.list")(function* (ownerId, after, limit) {
169
+ if (!Number.isSafeInteger(after) || after < 0 || !Number.isSafeInteger(limit) || limit <= 0) return yield* error("validation", "list-page");
170
+ const current = yield* Ref.get(state);
171
+ const records = [];
172
+ for (const [storageKey, indexed] of current.registrationIndex) {
173
+ if (indexed.key.ownerId !== ownerId || indexed.ordinal <= after) continue;
174
+ const text = current.registrations.get(storageKey);
175
+ if (text === void 0) return yield* error("corrupt", "list-index");
176
+ records.push(yield* decodeEffect(SubscriptionRecord, text, "list-record"));
177
+ }
178
+ records.sort((a, b) => a.ordinal - b.ordinal);
179
+ return records.slice(0, limit);
180
+ });
181
+ const change = Effect.fn("MemorySubscriptionStore.change")(function* (input, expectedRevision, inputChange) {
182
+ const key = yield* requireKey(input, "change-key");
183
+ const change = yield* validate(SubscriptionChange, inputChange, "change");
184
+ yield* validate(Schema.Int.check(Schema.isGreaterThan(0)), expectedRevision, "revision");
185
+ yield* failpoint.hit("subscription:change:before");
186
+ const updated = yield* Effect.uninterruptible(Ref.modify(state, (current) => {
187
+ const storageKey = subscriptionKeyString(key);
188
+ const text = current.registrations.get(storageKey);
189
+ if (text === void 0) return [Result.fail(error("not-found", "subscription")), current];
190
+ const existing = decode(SubscriptionRecord, text, "change-record");
191
+ if (Result.isFailure(existing)) return [existing, current];
192
+ const updated = applySubscriptionChange(existing.success, expectedRevision, change);
193
+ if (Result.isFailure(updated)) return [updated, current];
194
+ const revised = {
195
+ ...updated.success,
196
+ ordinal: current.sequence + 1
197
+ };
198
+ const encoded = encode(SubscriptionRecord, revised, "change-encode");
199
+ if (Result.isFailure(encoded)) return [Result.fail(encoded.failure), current];
200
+ const registrations = new Map(current.registrations);
201
+ registrations.set(storageKey, encoded.success);
202
+ const registrationIndex = new Map(current.registrationIndex);
203
+ registrationIndex.set(storageKey, {
204
+ key,
205
+ ordinal: revised.ordinal,
206
+ state: revised.state,
207
+ recoveryKey: revised.recovery === null ? null : candidateIndexKey(revised),
208
+ recoveryAt: revised.recovery?.nextAttemptAtMillis ?? null
209
+ });
210
+ const candidateIndex = new Map(current.candidateIndex);
211
+ const oldKey = candidateIndexKey(existing.success);
212
+ const nextKey = candidateIndexKey(revised);
213
+ candidateIndex.set(oldKey, (candidateIndex.get(oldKey) ?? []).filter((key) => key !== storageKey));
214
+ candidateIndex.set(nextKey, [...candidateIndex.get(nextKey) ?? [], storageKey].sort((a, b) => (registrationIndex.get(a)?.ordinal ?? 0) - (registrationIndex.get(b)?.ordinal ?? 0)));
215
+ return [Result.succeed(revised), {
216
+ ...current,
217
+ sequence: revised.ordinal,
218
+ registrations,
219
+ registrationIndex,
220
+ candidateIndex,
221
+ recoveryCounts: recoveryCountsAfter(current, registrationIndex, [storageKey])
222
+ }];
223
+ })).pipe(Effect.flatMap(Effect.fromResult));
224
+ yield* failpoint.hit("subscription:change:after");
225
+ return updated;
226
+ });
227
+ const cancel = Effect.fn("MemorySubscriptionStore.cancel")(function* (input, expectedRevision) {
228
+ const key = yield* requireKey(input, "cancel-key");
229
+ yield* failpoint.hit("subscription:cancel:before");
230
+ const result = yield* Effect.uninterruptible(Ref.modify(state, (current) => {
231
+ const storageKey = subscriptionKeyString(key);
232
+ const text = current.registrations.get(storageKey);
233
+ if (text === void 0) return [Result.fail(error("not-found", "subscription")), current];
234
+ const decoded = decode(SubscriptionRecord, text, "cancel-record");
235
+ if (Result.isFailure(decoded)) return [decoded, current];
236
+ if (expectedRevision !== void 0 && expectedRevision !== decoded.success.configurationRevision && !(decoded.success.state === "cancelled" && expectedRevision + 1 === decoded.success.configurationRevision)) return [Result.fail(SubscriptionError.make({
237
+ reason: "conflict",
238
+ code: "configuration-revision",
239
+ currentRevision: decoded.success.configurationRevision,
240
+ currentState: decoded.success.state
241
+ })), current];
242
+ if (decoded.success.state === "cancelled") return [Result.succeed(decoded.success), current];
243
+ const cancelled = {
244
+ ...decoded.success,
245
+ configurationRevision: decoded.success.configurationRevision + 1,
246
+ state: "cancelled",
247
+ recovery: null
248
+ };
249
+ const encoded = encode(SubscriptionRecord, cancelled, "cancel-encode");
250
+ if (Result.isFailure(encoded)) return [Result.fail(encoded.failure), current];
251
+ const registrations = new Map(current.registrations);
252
+ registrations.set(storageKey, encoded.success);
253
+ const registrationIndex = new Map(current.registrationIndex);
254
+ const indexed = registrationIndex.get(storageKey);
255
+ if (indexed === void 0) return [Result.fail(error("corrupt", "cancel-index")), current];
256
+ registrationIndex.set(storageKey, {
257
+ ...indexed,
258
+ state: "cancelled",
259
+ recoveryAt: null,
260
+ recoveryKey: null
261
+ });
262
+ return [Result.succeed(cancelled), {
263
+ ...current,
264
+ registrations,
265
+ registrationIndex,
266
+ recoveryCounts: recoveryCountsAfter(current, registrationIndex, [storageKey])
267
+ }];
268
+ })).pipe(Effect.flatMap(Effect.fromResult));
269
+ yield* failpoint.hit("subscription:cancel:after");
270
+ return result;
271
+ });
272
+ const accept = Effect.fn("MemorySubscriptionStore.accept")(function* (input, inputLimits) {
273
+ const event = yield* validate(AcceptedEvent, input, "accept-event");
274
+ const currentTimeMillis = yield* Clock.currentTimeMillis;
275
+ const limits = yield* validate(SubscriptionLimits, inputLimits, "accept-limits");
276
+ yield* requirePartition(event, "accept-partition");
277
+ yield* failpoint.hit("subscription:accept:before");
278
+ const result = yield* Effect.uninterruptible(Ref.modify(state, (current) => {
279
+ const existingText = current.events.get(event.eventId);
280
+ if (existingText !== void 0) {
281
+ const existing = decode(AcceptedEvent, existingText, "accept-existing");
282
+ if (Result.isFailure(existing)) return [Result.fail(existing.failure), current];
283
+ return sameEventIdentity(existing.success, event) ? [Result.succeed(existing.success), current] : [Result.fail(error("conflict", "event-identity")), current];
284
+ }
285
+ if (current.retentionHorizon !== null && current.retentionHorizon !== limits.retention?.replayHorizonMillis) return [Result.fail(error("conflict", "retention-horizon")), current];
286
+ const horizon = validateEventRetention(event, limits, currentTimeMillis);
287
+ if (Result.isFailure(horizon)) return [Result.fail(horizon.failure), current];
288
+ if (jsonBytes(event.payload) > limits.maxPayloadBytes) return [Result.fail(error("capacity", "payload-bytes")), current];
289
+ if (current.events.size - current.tombstoneCount >= limits.maxEvents) return [Result.fail(error("capacity", "events")), current];
290
+ const accepted = {
291
+ ...event,
292
+ cutoff: current.sequence + 1,
293
+ cursor: 0,
294
+ routingComplete: false,
295
+ routingFailure: null
296
+ };
297
+ const encoded = encode(AcceptedEvent, accepted, "accept-encode");
298
+ if (Result.isFailure(encoded)) return [Result.fail(encoded.failure), current];
299
+ const events = new Map(current.events);
300
+ events.set(accepted.eventId, encoded.success);
301
+ const eventIndex = new Map(current.eventIndex);
302
+ eventIndex.set(accepted.eventId, {
303
+ routingComplete: false,
304
+ nextAttemptAtMillis: accepted.nextAttemptAtMillis
305
+ });
306
+ return [Result.succeed(accepted), {
307
+ ...current,
308
+ sequence: accepted.cutoff,
309
+ events,
310
+ eventIndex,
311
+ eventKeys: insertKey(current.eventKeys, accepted.eventId),
312
+ retentionHorizon: limits.retention?.replayHorizonMillis ?? current.retentionHorizon,
313
+ retentionDeadline: limits.retention === void 0 ? current.retentionDeadline : accepted.acceptedAtMillis
314
+ }];
315
+ })).pipe(Effect.flatMap(Effect.fromResult));
316
+ yield* failpoint.hit("subscription:accept:after");
317
+ return result;
318
+ });
319
+ const event = Effect.fn("MemorySubscriptionStore.event")(function* (eventId) {
320
+ const text = (yield* Ref.get(state)).events.get(eventId);
321
+ return text === void 0 ? null : yield* decodeEffect(AcceptedEvent, text, "event-record");
322
+ });
323
+ const pendingEvents = Effect.fn("MemorySubscriptionStore.pendingEvents")(function* (nowMillis, after, limit) {
324
+ const events = [];
325
+ for (const [eventId, indexed] of (yield* Ref.get(state)).eventIndex) if (!indexed.routingComplete && indexed.nextAttemptAtMillis <= nowMillis && compareScheduleNames(eventId, after) > 0) events.push(eventId);
326
+ events.sort(compareScheduleNames);
327
+ return events.slice(0, limit);
328
+ });
329
+ const candidates = Effect.fn("MemorySubscriptionStore.candidates")(function* (input, limit) {
330
+ const accepted = yield* validate(AcceptedEvent, input, "candidates-event");
331
+ yield* requirePartition(accepted, "candidates-partition");
332
+ const stored = yield* event(accepted.eventId);
333
+ if (stored === null || !sameEventIdentity(stored, accepted)) return yield* error(stored === null ? "not-found" : "conflict", "event");
334
+ const current = yield* Ref.get(state);
335
+ const records = [];
336
+ for (const storageKey of current.candidateIndex.get(eventCandidateIndexKey(stored)) ?? []) {
337
+ const indexed = current.registrationIndex.get(storageKey);
338
+ if (indexed === void 0) return yield* error("corrupt", "candidate-index");
339
+ if (indexed.ordinal <= stored.cursor || indexed.ordinal > stored.cutoff) continue;
340
+ const text = current.registrations.get(storageKey);
341
+ if (text === void 0) return yield* error("corrupt", "candidate-record");
342
+ records.push(yield* decodeEffect(SubscriptionRecord, text, "candidate-record"));
343
+ }
344
+ records.sort((a, b) => a.ordinal - b.ordinal);
345
+ return records.slice(0, limit);
346
+ });
347
+ const select = Effect.fn("MemorySubscriptionStore.select")(function* (inputEvent, inputDeliveries, cursor, complete, nowMillis, inputLimits) {
348
+ const suppliedEvent = yield* validate(AcceptedEvent, inputEvent, "select-event");
349
+ const deliveries = yield* validate(Schema.Array(SubscriptionDelivery), inputDeliveries, "select-deliveries");
350
+ const limits = yield* validate(SubscriptionLimits, inputLimits, "select-limits");
351
+ yield* requirePartition(suppliedEvent, "select-partition");
352
+ yield* failpoint.hit("subscription:select:before");
353
+ const effectiveNowMillis = Math.max(nowMillis, yield* Clock.currentTimeMillis);
354
+ yield* Effect.uninterruptible(Ref.modify(state, (current) => {
355
+ const eventText = current.events.get(suppliedEvent.eventId);
356
+ if (eventText === void 0) return [Result.fail(error("not-found", "event")), current];
357
+ const decodedEvent = decode(AcceptedEvent, eventText, "select-event-record");
358
+ if (Result.isFailure(decodedEvent)) return [Result.fail(decodedEvent.failure), current];
359
+ const accepted = decodedEvent.success;
360
+ if (!sameEventIdentity(accepted, suppliedEvent) || suppliedEvent.cursor !== accepted.cursor) return [Result.fail(error("conflict", "event-cursor")), current];
361
+ if (accepted.routingComplete) return [complete && cursor === accepted.cursor ? Result.void : Result.fail(error("conflict", "routing-complete")), current];
362
+ if (!Number.isSafeInteger(cursor) || cursor < accepted.cursor || cursor > accepted.cutoff) return [Result.fail(error("validation", "cursor")), current];
363
+ const additions = [];
364
+ const updates = [];
365
+ const additionIndex = [];
366
+ const registrationUpdates = [];
367
+ const owners = new Map(current.ownerDeliveryCounts);
368
+ for (const delivery of deliveries) {
369
+ const recordText = current.registrations.get(subscriptionKeyString(delivery.key.subscription));
370
+ if (recordText === void 0) return [Result.fail(error("not-found", "subscription")), current];
371
+ const record = decode(SubscriptionRecord, recordText, "select-registration");
372
+ if (Result.isFailure(record)) return [Result.fail(record.failure), current];
373
+ if (!deliveryBelongsTo(delivery, record.success, accepted) || !subscriptionDeliveryCanSelect(delivery, record.success, accepted) || record.success.ordinal <= accepted.cursor || record.success.ordinal > cursor) return [Result.fail(error("conflict", "selection")), current];
374
+ if (!subscriptionCanSelect(record.success, accepted, effectiveNowMillis, false)) continue;
375
+ const deliveryKey = subscriptionDeliveryKeyString(delivery.key);
376
+ const existingText = current.deliveries.get(deliveryKey);
377
+ if (existingText !== void 0) {
378
+ const existing = decode(SubscriptionDelivery, existingText, "select-existing");
379
+ if (Result.isFailure(existing)) return [Result.fail(existing.failure), current];
380
+ if (!sameDeliveryIdentity(existing.success, delivery)) return [Result.fail(error("conflict", "delivery-identity")), current];
381
+ continue;
382
+ }
383
+ const encodedDelivery = encode(SubscriptionDelivery, delivery, "select-delivery-encode");
384
+ if (Result.isFailure(encodedDelivery)) return [Result.fail(encodedDelivery.failure), current];
385
+ additions.push([deliveryKey, encodedDelivery.success]);
386
+ additionIndex.push([
387
+ deliveryKey,
388
+ {
389
+ key: delivery.key,
390
+ state: delivery.state,
391
+ nextAttemptAtMillis: delivery.retry.nextAttemptAtMillis
392
+ },
393
+ record.success.key.ownerId
394
+ ]);
395
+ const ownerId = record.success.key.ownerId;
396
+ owners.set(ownerId, (owners.get(ownerId) ?? 0) + 1);
397
+ if ((owners.get(ownerId) ?? 0) > limits.maxDeliveriesPerOwner) return [Result.fail(error("capacity", "owner-deliveries")), current];
398
+ if (record.success.configuration.mode === "once") {
399
+ const consumed = {
400
+ ...record.success,
401
+ state: "consumed",
402
+ recovery: null
403
+ };
404
+ const encodedRecord = encode(SubscriptionRecord, consumed, "select-registration-encode");
405
+ if (Result.isFailure(encodedRecord)) return [Result.fail(encodedRecord.failure), current];
406
+ const consumedKey = subscriptionKeyString(consumed.key);
407
+ updates.push([consumedKey, encodedRecord.success]);
408
+ const indexed = current.registrationIndex.get(consumedKey);
409
+ if (indexed === void 0) return [Result.fail(error("corrupt", "selection-index")), current];
410
+ registrationUpdates.push([consumedKey, {
411
+ ...indexed,
412
+ state: "consumed",
413
+ recoveryAt: null,
414
+ recoveryKey: null
415
+ }]);
416
+ }
417
+ }
418
+ if (current.deliveries.size + additions.length > limits.maxDeliveries) return [Result.fail(error("capacity", "deliveries")), current];
419
+ const registrations = new Map(current.registrations);
420
+ for (const [key, value] of updates) registrations.set(key, value);
421
+ const nextDeliveries = new Map(current.deliveries);
422
+ for (const [key, value] of additions) nextDeliveries.set(key, value);
423
+ const deliveryIndex = new Map(current.deliveryIndex);
424
+ for (const [key, value] of additionIndex) deliveryIndex.set(key, value);
425
+ const registrationIndex = new Map(current.registrationIndex);
426
+ for (const [key, value] of registrationUpdates) registrationIndex.set(key, value);
427
+ const eventDeliveryCounts = new Map(current.eventDeliveryCounts);
428
+ eventDeliveryCounts.set(accepted.eventId, (eventDeliveryCounts.get(accepted.eventId) ?? 0) + additions.length);
429
+ const nextEvent = {
430
+ ...accepted,
431
+ cursor,
432
+ routingComplete: complete,
433
+ routingFailure: null
434
+ };
435
+ const encodedEvent = encode(AcceptedEvent, nextEvent, "select-event-encode");
436
+ if (Result.isFailure(encodedEvent)) return [Result.fail(encodedEvent.failure), current];
437
+ const events = new Map(current.events);
438
+ events.set(nextEvent.eventId, encodedEvent.success);
439
+ const eventIndex = new Map(current.eventIndex);
440
+ eventIndex.set(nextEvent.eventId, {
441
+ routingComplete: nextEvent.routingComplete,
442
+ nextAttemptAtMillis: nextEvent.nextAttemptAtMillis
443
+ });
444
+ return [Result.void, {
445
+ ...current,
446
+ registrations,
447
+ registrationIndex,
448
+ recoveryCounts: recoveryCountsAfter(current, registrationIndex, registrationUpdates.map(([key]) => key)),
449
+ eventDeliveryCounts,
450
+ events,
451
+ eventIndex,
452
+ deliveries: nextDeliveries,
453
+ deliveryKeys: additions.reduce((keys, [key]) => insertKey(keys, key), current.deliveryKeys),
454
+ deliveryIndex,
455
+ ownerDeliveryCounts: owners
456
+ }];
457
+ })).pipe(Effect.flatMap(Effect.fromResult));
458
+ yield* failpoint.hit("subscription:select:after");
459
+ });
460
+ const catchUp = Effect.fn("MemorySubscriptionStore.catchUp")(function* (inputEvent, inputDelivery, nowMillis, inputLimits) {
461
+ const suppliedEvent = yield* validate(AcceptedEvent, inputEvent, "catch-up-event");
462
+ const delivery = yield* validate(SubscriptionDelivery, inputDelivery, "catch-up-delivery");
463
+ const limits = yield* validate(SubscriptionLimits, inputLimits, "catch-up-limits");
464
+ yield* requirePartition(suppliedEvent, "catch-up-partition");
465
+ yield* failpoint.hit("subscription:catch-up:before");
466
+ const effectiveNowMillis = Math.max(nowMillis, yield* Clock.currentTimeMillis);
467
+ yield* Effect.uninterruptible(Ref.modify(state, (current) => {
468
+ const eventText = current.events.get(suppliedEvent.eventId);
469
+ const recordText = current.registrations.get(subscriptionKeyString(delivery.key.subscription));
470
+ if (eventText === void 0 || recordText === void 0) return [Result.fail(error("not-found", eventText === void 0 ? "event" : "subscription")), current];
471
+ const accepted = decode(AcceptedEvent, eventText, "catch-up-event-record");
472
+ if (Result.isFailure(accepted)) return [Result.fail(accepted.failure), current];
473
+ const record = decode(SubscriptionRecord, recordText, "catch-up-registration");
474
+ if (Result.isFailure(record)) return [Result.fail(record.failure), current];
475
+ if (!sameEventIdentity(accepted.success, suppliedEvent) || !deliveryBelongsTo(delivery, record.success, accepted.success) || !subscriptionDeliveryCanSelect(delivery, record.success, accepted.success) || record.success.configuration.mode !== "once") return [Result.fail(error("conflict", "catch-up-identity")), current];
476
+ const key = subscriptionDeliveryKeyString(delivery.key);
477
+ const existingText = current.deliveries.get(key);
478
+ if (existingText !== void 0) {
479
+ const existing = decode(SubscriptionDelivery, existingText, "catch-up-existing");
480
+ if (Result.isFailure(existing)) return [Result.fail(existing.failure), current];
481
+ return sameDeliveryIdentity(existing.success, delivery) ? [Result.void, current] : [Result.fail(error("conflict", "delivery-identity")), current];
482
+ }
483
+ if (!subscriptionCanSelect(record.success, accepted.success, effectiveNowMillis, true)) return [Result.fail(error("conflict", "catch-up-eligibility")), current];
484
+ if (current.deliveries.size >= limits.maxDeliveries) return [Result.fail(error("capacity", "deliveries")), current];
485
+ const ownerCount = current.ownerDeliveryCounts.get(record.success.key.ownerId) ?? 0;
486
+ if (ownerCount >= limits.maxDeliveriesPerOwner) return [Result.fail(error("capacity", "owner-deliveries")), current];
487
+ const encodedDelivery = encode(SubscriptionDelivery, delivery, "catch-up-delivery-encode");
488
+ if (Result.isFailure(encodedDelivery)) return [Result.fail(encodedDelivery.failure), current];
489
+ const consumed = {
490
+ ...record.success,
491
+ state: "consumed",
492
+ recovery: null
493
+ };
494
+ const encodedRecord = encode(SubscriptionRecord, consumed, "catch-up-registration-encode");
495
+ if (Result.isFailure(encodedRecord)) return [Result.fail(encodedRecord.failure), current];
496
+ const deliveries = new Map(current.deliveries);
497
+ deliveries.set(key, encodedDelivery.success);
498
+ const registrations = new Map(current.registrations);
499
+ const consumedKey = subscriptionKeyString(consumed.key);
500
+ registrations.set(consumedKey, encodedRecord.success);
501
+ const registrationIndex = new Map(current.registrationIndex);
502
+ const indexed = registrationIndex.get(consumedKey);
503
+ if (indexed === void 0) return [Result.fail(error("corrupt", "catch-up-index")), current];
504
+ registrationIndex.set(consumedKey, {
505
+ ...indexed,
506
+ state: "consumed",
507
+ recoveryAt: null,
508
+ recoveryKey: null
509
+ });
510
+ const deliveryIndex = new Map(current.deliveryIndex);
511
+ deliveryIndex.set(key, {
512
+ key: delivery.key,
513
+ state: delivery.state,
514
+ nextAttemptAtMillis: delivery.retry.nextAttemptAtMillis
515
+ });
516
+ const ownerDeliveryCounts = new Map(current.ownerDeliveryCounts);
517
+ const eventDeliveryCounts = new Map(current.eventDeliveryCounts);
518
+ eventDeliveryCounts.set(accepted.success.eventId, (eventDeliveryCounts.get(accepted.success.eventId) ?? 0) + 1);
519
+ ownerDeliveryCounts.set(consumed.key.ownerId, ownerCount + 1);
520
+ return [Result.void, {
521
+ ...current,
522
+ deliveries,
523
+ deliveryIndex,
524
+ deliveryKeys: insertKey(current.deliveryKeys, key),
525
+ ownerDeliveryCounts,
526
+ registrations,
527
+ registrationIndex,
528
+ recoveryCounts: recoveryCountsAfter(current, registrationIndex, [consumedKey]),
529
+ eventDeliveryCounts
530
+ }];
531
+ })).pipe(Effect.flatMap(Effect.fromResult));
532
+ yield* failpoint.hit("subscription:catch-up:after");
533
+ });
534
+ const deferEvent = Effect.fn("MemorySubscriptionStore.deferEvent")(function* (eventId, nextAttemptAtMillis, code) {
535
+ const routingFailure = code === void 0 ? "routing-failed" : yield* validate(SubscriptionName, code, "routing-failure");
536
+ yield* failpoint.hit("subscription:defer-event:before");
537
+ yield* Effect.uninterruptible(Ref.modify(state, (current) => {
538
+ const text = current.events.get(eventId);
539
+ if (text === void 0) return [Result.fail(error("not-found", "event")), current];
540
+ const accepted = decode(AcceptedEvent, text, "defer-event-record");
541
+ if (Result.isFailure(accepted)) return [Result.fail(accepted.failure), current];
542
+ const updated = {
543
+ ...accepted.success,
544
+ nextAttemptAtMillis,
545
+ routingFailure
546
+ };
547
+ const encoded = encode(AcceptedEvent, updated, "defer-event-encode");
548
+ if (Result.isFailure(encoded)) return [Result.fail(encoded.failure), current];
549
+ const events = new Map(current.events);
550
+ events.set(eventId, encoded.success);
551
+ const eventIndex = new Map(current.eventIndex);
552
+ const indexed = eventIndex.get(eventId);
553
+ if (indexed === void 0) return [Result.fail(error("corrupt", "defer-event-index")), current];
554
+ eventIndex.set(eventId, {
555
+ ...indexed,
556
+ nextAttemptAtMillis
557
+ });
558
+ return [Result.void, {
559
+ ...current,
560
+ events,
561
+ eventIndex
562
+ }];
563
+ })).pipe(Effect.flatMap(Effect.fromResult));
564
+ yield* failpoint.hit("subscription:defer-event:after");
565
+ });
566
+ const delivery = Effect.fn("MemorySubscriptionStore.delivery")(function* (input) {
567
+ const key = yield* validate(SubscriptionDeliveryKey, input, "delivery-key");
568
+ yield* requirePartition(key.subscription, "delivery-partition");
569
+ const text = (yield* Ref.get(state)).deliveries.get(subscriptionDeliveryKeyString(key));
570
+ return text === void 0 ? null : yield* decodeEffect(SubscriptionDelivery, text, "delivery-record");
571
+ });
572
+ const pendingDeliveries = Effect.fn("MemorySubscriptionStore.pendingDeliveries")(function* (nowMillis, after, limit) {
573
+ const items = [];
574
+ for (const [storageKey, item] of (yield* Ref.get(state)).deliveryIndex) if ((item.state !== "delivered" && item.state !== "refused" && item.parked !== true || item.state === "delivered" && item.observeSettlement === true) && item.nextAttemptAtMillis <= nowMillis && compareScheduleNames(storageKey, after) > 0) items.push(item.key);
575
+ items.sort((a, b) => compareScheduleNames(subscriptionDeliveryKeyString(a), subscriptionDeliveryKeyString(b)));
576
+ return items.slice(0, limit);
577
+ });
578
+ const listDeliveries = Effect.fn("MemorySubscriptionStore.listDeliveries")(function* (input, after, limit) {
579
+ const key = yield* requireKey(input, "list-deliveries-key");
580
+ const items = [];
581
+ for (const text of (yield* Ref.get(state)).deliveries.values()) {
582
+ const item = yield* decodeEffect(SubscriptionDelivery, text, "list-delivery");
583
+ const itemKey = subscriptionDeliveryKeyString(item.key);
584
+ if (subscriptionKeyString(item.key.subscription) === subscriptionKeyString(key) && compareScheduleNames(itemKey, after) > 0) items.push(item);
585
+ }
586
+ items.sort((a, b) => compareScheduleNames(subscriptionDeliveryKeyString(a.key), subscriptionDeliveryKeyString(b.key)));
587
+ return items.slice(0, limit);
588
+ });
589
+ const changeDelivery = Effect.fn("MemorySubscriptionStore.changeDelivery")(function* (inputKey, inputDeliveryId, inputChange) {
590
+ const key = yield* validate(SubscriptionDeliveryKey, inputKey, "change-delivery-key");
591
+ const deliveryId = yield* validate(Digest, inputDeliveryId, "change-delivery-id");
592
+ const change = yield* validate(DeliveryChange, inputChange, "change-delivery-change");
593
+ yield* requirePartition(key.subscription, "change-delivery-partition");
594
+ yield* failpoint.hit(`subscription:delivery-${change._tag.toLowerCase()}:before`);
595
+ const effectiveChange = change._tag === "Prepare" ? {
596
+ ...change,
597
+ nowMillis: Math.max(change.nowMillis, yield* Clock.currentTimeMillis)
598
+ } : change;
599
+ const result = yield* Effect.uninterruptible(Ref.modify(state, (current) => {
600
+ const storageKey = subscriptionDeliveryKeyString(key);
601
+ const text = current.deliveries.get(storageKey);
602
+ if (text === void 0) return [Result.fail(error("not-found", "delivery")), current];
603
+ const decoded = decode(SubscriptionDelivery, text, "change-delivery-record");
604
+ if (Result.isFailure(decoded)) return [decoded, current];
605
+ const registrationText = current.registrations.get(subscriptionKeyString(key.subscription));
606
+ if (registrationText === void 0) return [Result.fail(error("corrupt", "delivery-registration")), current];
607
+ const registration = decode(SubscriptionRecord, registrationText, "delivery-registration");
608
+ if (Result.isFailure(registration)) return [Result.fail(registration.failure), current];
609
+ const transition = applySubscriptionDeliveryChange(decoded.success, registration.success, deliveryId, effectiveChange);
610
+ if (Result.isFailure(transition)) return [Result.fail(transition.failure), current];
611
+ if (transition.success === decoded.success) return [Result.succeed(decoded.success), current];
612
+ const updated = transition.success;
613
+ const encoded = encode(SubscriptionDelivery, updated, "change-delivery-encode");
614
+ if (Result.isFailure(encoded)) return [Result.fail(encoded.failure), current];
615
+ const deliveries = new Map(current.deliveries);
616
+ deliveries.set(storageKey, encoded.success);
617
+ const deliveryIndex = new Map(current.deliveryIndex);
618
+ const indexed = deliveryIndex.get(storageKey);
619
+ if (indexed === void 0) return [Result.fail(error("corrupt", "delivery-index")), current];
620
+ deliveryIndex.set(storageKey, {
621
+ ...indexed,
622
+ state: updated.state,
623
+ parked: updated.retry.parked ?? false,
624
+ observeSettlement: updated.observeSettlement ?? false,
625
+ nextAttemptAtMillis: updated.retry.nextAttemptAtMillis
626
+ });
627
+ return [Result.succeed(updated), {
628
+ ...current,
629
+ deliveries,
630
+ deliveryIndex
631
+ }];
632
+ })).pipe(Effect.flatMap(Effect.fromResult));
633
+ yield* failpoint.hit(`subscription:delivery-${change._tag.toLowerCase()}:after`);
634
+ return result;
635
+ });
636
+ const recovering = Effect.fn("MemorySubscriptionStore.recovering")(function* (nowMillis, after, limit) {
637
+ const records = [];
638
+ for (const item of (yield* Ref.get(state)).registrationIndex.values()) if (item.ordinal > after && item.state === "active" && item.recoveryAt !== null && item.recoveryAt <= nowMillis) records.push({
639
+ key: item.key,
640
+ ordinal: item.ordinal
641
+ });
642
+ records.sort((a, b) => a.ordinal - b.ordinal);
643
+ return records.slice(0, limit);
644
+ });
645
+ const deferRecovery = Effect.fn("MemorySubscriptionStore.deferRecovery")(function* (input, expectedRevision, recovery) {
646
+ const key = yield* requireKey(input, "defer-recovery-key");
647
+ yield* failpoint.hit("subscription:defer-recovery:before");
648
+ yield* Effect.uninterruptible(Ref.modify(state, (current) => {
649
+ const storageKey = subscriptionKeyString(key);
650
+ const text = current.registrations.get(storageKey);
651
+ if (text === void 0) return [Result.fail(error("not-found", "subscription")), current];
652
+ const record = decode(SubscriptionRecord, text, "defer-recovery-record");
653
+ if (Result.isFailure(record)) return [Result.fail(record.failure), current];
654
+ if (record.success.configurationRevision !== expectedRevision) return [Result.void, current];
655
+ const updated = {
656
+ ...record.success,
657
+ recovery: record.success.state === "active" || record.success.state === "paused" ? recovery : null
658
+ };
659
+ const encoded = encode(SubscriptionRecord, updated, "defer-recovery-encode");
660
+ if (Result.isFailure(encoded)) return [Result.fail(encoded.failure), current];
661
+ const registrations = new Map(current.registrations);
662
+ registrations.set(storageKey, encoded.success);
663
+ const registrationIndex = new Map(current.registrationIndex);
664
+ const indexed = registrationIndex.get(storageKey);
665
+ if (indexed === void 0) return [Result.fail(error("corrupt", "recovery-index")), current];
666
+ registrationIndex.set(storageKey, {
667
+ ...indexed,
668
+ recoveryKey: updated.recovery === null ? null : candidateIndexKey(updated),
669
+ recoveryAt: updated.recovery?.nextAttemptAtMillis ?? null
670
+ });
671
+ return [Result.void, {
672
+ ...current,
673
+ registrations,
674
+ registrationIndex,
675
+ recoveryCounts: recoveryCountsAfter(current, registrationIndex, [storageKey])
676
+ }];
677
+ })).pipe(Effect.flatMap(Effect.fromResult));
678
+ yield* failpoint.hit("subscription:defer-recovery:after");
679
+ });
680
+ const readScanCursors = Ref.get(state).pipe(Effect.map((current) => current.scanCursors));
681
+ const advanceScanCursors = Effect.fn("MemorySubscriptionStore.advanceScanCursors")(function* (input) {
682
+ const cursors = yield* validate(SubscriptionScanCursors, input, "scan-cursors");
683
+ yield* failpoint.hit("subscription:advance-scan-cursors:before");
684
+ yield* Effect.uninterruptible(Ref.update(state, (current) => ({
685
+ ...current,
686
+ scanCursors: cursors
687
+ })));
688
+ yield* failpoint.hit("subscription:advance-scan-cursors:after");
689
+ });
690
+ const nextDeadline = Effect.gen(function* () {
691
+ let deadline = null;
692
+ const current = yield* Ref.get(state);
693
+ if (current.scanCursors.events !== "" || current.scanCursors.deliveries !== "" || current.scanCursors.recovery !== 0) return 0;
694
+ const consider = (value) => {
695
+ if (deadline === null || value < deadline) deadline = value;
696
+ };
697
+ if (current.retentionDeadline !== null) consider(current.retentionDeadline);
698
+ for (const accepted of current.eventIndex.values()) if (!accepted.routingComplete) consider(accepted.nextAttemptAtMillis);
699
+ for (const item of current.deliveryIndex.values()) if (item.state !== "delivered" && item.state !== "refused" && item.parked !== true || item.state === "delivered" && item.observeSettlement === true) consider(item.nextAttemptAtMillis);
700
+ for (const record of current.registrationIndex.values()) if (record.state === "active" && record.recoveryAt !== null) consider(record.recoveryAt);
701
+ return deadline;
702
+ }).pipe(Effect.withSpan("MemorySubscriptionStore.nextDeadline"));
703
+ const compact = Effect.fn("MemorySubscriptionStore.compact")(function* (nowMillis, inputPolicy, requestedLimit) {
704
+ nowMillis = Math.min(nowMillis, yield* Clock.currentTimeMillis);
705
+ const policy = yield* validate(SubscriptionRetentionPolicy, inputPolicy, "retention-policy");
706
+ const limit = yield* validate(Schema.Int.check(Schema.isBetween({
707
+ minimum: 1,
708
+ maximum: 100
709
+ })), requestedLimit, "maintenance-limit");
710
+ yield* failpoint.hit("subscription:compact:before");
711
+ let corruptCandidates = 0;
712
+ const result = yield* Effect.uninterruptible(Ref.modify(state, (current) => {
713
+ if (current.retentionHorizon !== null && current.retentionHorizon !== policy.replayHorizonMillis) return [Result.fail(error("conflict", "retention-horizon")), current];
714
+ const events = new Map(current.events);
715
+ const eventIndex = new Map(current.eventIndex);
716
+ const deliveries = new Map(current.deliveries);
717
+ const deliveryIndex = new Map(current.deliveryIndex);
718
+ const eventDeliveryCounts = new Map(current.eventDeliveryCounts);
719
+ const ownerDeliveryCounts = new Map(current.ownerDeliveryCounts);
720
+ const deliveryPage = current.deliveryKeys.slice(upperBound(current.deliveryKeys, current.maintenanceDeliveries), upperBound(current.deliveryKeys, current.maintenanceDeliveries) + limit);
721
+ const eventPage = current.eventKeys.slice(upperBound(current.eventKeys, current.maintenanceEvents), upperBound(current.eventKeys, current.maintenanceEvents) + limit);
722
+ const cutoff = nowMillis - policy.completedRetentionMillis;
723
+ let removed = 0;
724
+ const removedEvents = /* @__PURE__ */ new Set();
725
+ const removedDeliveries = /* @__PURE__ */ new Set();
726
+ for (const key of deliveryPage) {
727
+ const text = deliveries.get(key);
728
+ if (text === void 0) continue;
729
+ const decoded = decode(SubscriptionDelivery, text, "compact-delivery");
730
+ if (Result.isFailure(decoded)) {
731
+ corruptCandidates++;
732
+ continue;
733
+ }
734
+ const delivery = decoded.success;
735
+ if (subscriptionDeliveryKeyString(delivery.key) !== key || !samePartition(delivery.key.subscription.partition, partition)) {
736
+ corruptCandidates++;
737
+ continue;
738
+ }
739
+ if (delivery.state !== "refused" && (delivery.state !== "delivered" || delivery.settledAtMillis === void 0)) continue;
740
+ if ((delivery.settledAtMillis ?? delivery.completedAtMillis ?? delivery.selectedAtMillis) > cutoff) continue;
741
+ const eventText = events.get(delivery.key.eventId);
742
+ if (eventText === void 0) continue;
743
+ const accepted = decode(AcceptedEvent, eventText, "compact-delivery-event");
744
+ if (Result.isFailure(accepted)) {
745
+ corruptCandidates++;
746
+ continue;
747
+ }
748
+ const event = accepted.success;
749
+ if (event.eventId !== delivery.key.eventId || !samePartition(event.partition, partition)) {
750
+ corruptCandidates++;
751
+ continue;
752
+ }
753
+ if (!event.routingComplete || event.occurredAtMillis === void 0 || event.acceptedAtMillis > cutoff || (current.recoveryCounts.get(eventCandidateIndexKey(event)) ?? 0) > 0) continue;
754
+ deliveries.delete(key);
755
+ removedDeliveries.add(key);
756
+ deliveryIndex.delete(key);
757
+ eventDeliveryCounts.set(event.eventId, (eventDeliveryCounts.get(event.eventId) ?? 1) - 1);
758
+ const owner = delivery.key.subscription.ownerId;
759
+ ownerDeliveryCounts.set(owner, (ownerDeliveryCounts.get(owner) ?? 1) - 1);
760
+ }
761
+ let tombstones = current.tombstoneCount;
762
+ for (const key of eventPage) {
763
+ const text = events.get(key);
764
+ if (text === void 0) continue;
765
+ const decoded = decode(AcceptedEvent, text, "compact-event");
766
+ if (Result.isFailure(decoded)) {
767
+ corruptCandidates++;
768
+ continue;
769
+ }
770
+ const event = decoded.success;
771
+ if (event.eventId !== key || !samePartition(event.partition, partition)) {
772
+ corruptCandidates++;
773
+ continue;
774
+ }
775
+ if (!event.routingComplete || event.occurredAtMillis === void 0) continue;
776
+ const expired = event.occurredAtMillis <= nowMillis - policy.replayHorizonMillis;
777
+ if (event.tombstone === true && !expired) continue;
778
+ if (event.acceptedAtMillis > cutoff || (eventDeliveryCounts.get(key) ?? 0) > 0 || (current.recoveryCounts.get(eventCandidateIndexKey(event)) ?? 0) > 0) continue;
779
+ if (expired) {
780
+ events.delete(key);
781
+ removedEvents.add(key);
782
+ eventIndex.delete(key);
783
+ eventDeliveryCounts.delete(key);
784
+ if (event.tombstone === true) tombstones--;
785
+ } else {
786
+ if (tombstones >= policy.maxTombstones) continue;
787
+ const encoded = encode(AcceptedEvent, {
788
+ ...event,
789
+ payload: null,
790
+ tombstone: true
791
+ }, "compact-tombstone");
792
+ if (Result.isFailure(encoded)) continue;
793
+ events.set(key, encoded.success);
794
+ tombstones++;
795
+ }
796
+ removed++;
797
+ }
798
+ return [Result.succeed(removed), {
799
+ ...current,
800
+ events,
801
+ eventIndex,
802
+ deliveries,
803
+ deliveryIndex,
804
+ eventDeliveryCounts,
805
+ ownerDeliveryCounts,
806
+ eventKeys: removeKeys(current.eventKeys, removedEvents),
807
+ deliveryKeys: removeKeys(current.deliveryKeys, removedDeliveries),
808
+ tombstoneCount: tombstones,
809
+ maintenanceEvents: eventPage.length < limit ? "" : eventPage.at(-1) ?? "",
810
+ maintenanceDeliveries: deliveryPage.length < limit ? "" : deliveryPage.at(-1) ?? "",
811
+ retentionHorizon: policy.replayHorizonMillis,
812
+ retentionDeadline: events.size === 0 ? null : nowMillis + 6e4
813
+ }];
814
+ })).pipe(Effect.flatMap(Effect.fromResult));
815
+ if (corruptCandidates > 0) yield* Effect.logWarning("Subscription retention preserved corrupt candidates", { count: corruptCandidates });
816
+ yield* failpoint.hit("subscription:compact:after");
817
+ return result;
818
+ });
819
+ return SubscriptionStore.of({
820
+ partition,
821
+ compact,
822
+ register,
823
+ get,
824
+ list,
825
+ cancel,
826
+ change,
827
+ accept,
828
+ event,
829
+ pendingEvents,
830
+ candidates,
831
+ select,
832
+ catchUp,
833
+ deferEvent,
834
+ delivery,
835
+ pendingDeliveries,
836
+ listDeliveries,
837
+ changeDelivery,
838
+ recovering,
839
+ deferRecovery,
840
+ readScanCursors,
841
+ advanceScanCursors,
842
+ nextDeadline
843
+ });
844
+ });
845
+ const memorySubscriptionStoreLayer = (partition) => Layer.effect(SubscriptionStore, makeMemorySubscriptionStore(partition));
846
+ //#endregion
847
+ export { memorySubscriptionStoreLayer, MemorySubscriptionStore_exports as t };
848
+
849
+ //# sourceMappingURL=MemorySubscriptionStore.mjs.map