@yielded/agent-storage-memory 0.1.0-beta.167 → 0.1.0-beta.169

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.
@@ -1,8 +1,8 @@
1
1
  import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
2
2
  import { compareScheduleNames } from "@yielded/agent/schedule-transition";
3
3
  import { Clock, Effect, Layer, Ref, Result, Schema } from "effect";
4
- import { Digest } from "@yielded/agent/records";
5
4
  import { AcceptedEvent, DeliveryChange, SourcePartition, SubscriptionChange, SubscriptionDelivery, SubscriptionDeliveryKey, SubscriptionError, SubscriptionFailpoint, SubscriptionKey, SubscriptionLimits, SubscriptionName, SubscriptionRecord, SubscriptionRetentionPolicy, SubscriptionScanCursors, SubscriptionStore, subscriptionDeliveryKeyString, subscriptionKeyString } from "@yielded/agent/subscription";
5
+ import { Digest } from "@yielded/agent/records";
6
6
  import { applySubscriptionChange, applySubscriptionDeliveryChange, sameAcceptedEventIdentity, subscriptionCanSelect, subscriptionDeliveryCanSelect, validateEventRetention } from "@yielded/agent/subscription-transition";
7
7
  //#region src/MemorySubscriptionStore.ts
8
8
  var MemorySubscriptionStore_exports = /* @__PURE__ */ __exportAll({ memorySubscriptionStoreLayer: () => memorySubscriptionStoreLayer });
@@ -76,7 +76,7 @@ const recoveryCountsAfter = (current, next, keys) => {
76
76
  }
77
77
  return counts;
78
78
  };
79
- const makeMemorySubscriptionStore = Effect.fn("makeMemorySubscriptionStore")(function* (ownedPartition) {
79
+ const makeMemorySubscriptionStore = Effect.fnUntraced(function* (ownedPartition) {
80
80
  const partition = yield* validate(SourcePartition, ownedPartition, "partition");
81
81
  const state = yield* Ref.make({
82
82
  sequence: 0,
@@ -107,7 +107,7 @@ const makeMemorySubscriptionStore = Effect.fn("makeMemorySubscriptionStore")(fun
107
107
  const failpoint = yield* SubscriptionFailpoint;
108
108
  const requirePartition = (value, code) => samePartition(value.partition, partition) ? Effect.succeed(value) : Effect.fail(error("validation", code));
109
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) {
110
+ const register = Effect.fnUntraced(function* (input, inputLimits) {
111
111
  const record = yield* validate(SubscriptionRecord, input, "register-record");
112
112
  const limits = yield* validate(SubscriptionLimits, inputLimits, "register-limits");
113
113
  yield* requirePartition(record.key, "register-partition");
@@ -160,12 +160,12 @@ const makeMemorySubscriptionStore = Effect.fn("makeMemorySubscriptionStore")(fun
160
160
  yield* failpoint.hit("subscription:register:after");
161
161
  return result;
162
162
  });
163
- const get = Effect.fn("MemorySubscriptionStore.get")(function* (input) {
163
+ const get = Effect.fnUntraced(function* (input) {
164
164
  const key = yield* requireKey(input, "get-key");
165
165
  const text = (yield* Ref.get(state)).registrations.get(subscriptionKeyString(key));
166
166
  return text === void 0 ? null : yield* decodeEffect(SubscriptionRecord, text, "get-record");
167
167
  });
168
- const list = Effect.fn("MemorySubscriptionStore.list")(function* (ownerId, after, limit) {
168
+ const list = Effect.fnUntraced(function* (ownerId, after, limit) {
169
169
  if (!Number.isSafeInteger(after) || after < 0 || !Number.isSafeInteger(limit) || limit <= 0) return yield* error("validation", "list-page");
170
170
  const current = yield* Ref.get(state);
171
171
  const records = [];
@@ -178,7 +178,7 @@ const makeMemorySubscriptionStore = Effect.fn("makeMemorySubscriptionStore")(fun
178
178
  records.sort((a, b) => a.ordinal - b.ordinal);
179
179
  return records.slice(0, limit);
180
180
  });
181
- const change = Effect.fn("MemorySubscriptionStore.change")(function* (input, expectedRevision, inputChange) {
181
+ const change = Effect.fnUntraced(function* (input, expectedRevision, inputChange) {
182
182
  const key = yield* requireKey(input, "change-key");
183
183
  const change = yield* validate(SubscriptionChange, inputChange, "change");
184
184
  yield* validate(Schema.Int.check(Schema.isGreaterThan(0)), expectedRevision, "revision");
@@ -224,7 +224,7 @@ const makeMemorySubscriptionStore = Effect.fn("makeMemorySubscriptionStore")(fun
224
224
  yield* failpoint.hit("subscription:change:after");
225
225
  return updated;
226
226
  });
227
- const cancel = Effect.fn("MemorySubscriptionStore.cancel")(function* (input, expectedRevision) {
227
+ const cancel = Effect.fnUntraced(function* (input, expectedRevision) {
228
228
  const key = yield* requireKey(input, "cancel-key");
229
229
  yield* failpoint.hit("subscription:cancel:before");
230
230
  const result = yield* Effect.uninterruptible(Ref.modify(state, (current) => {
@@ -269,7 +269,7 @@ const makeMemorySubscriptionStore = Effect.fn("makeMemorySubscriptionStore")(fun
269
269
  yield* failpoint.hit("subscription:cancel:after");
270
270
  return result;
271
271
  });
272
- const accept = Effect.fn("MemorySubscriptionStore.accept")(function* (input, inputLimits) {
272
+ const accept = Effect.fnUntraced(function* (input, inputLimits) {
273
273
  const event = yield* validate(AcceptedEvent, input, "accept-event");
274
274
  const currentTimeMillis = yield* Clock.currentTimeMillis;
275
275
  const limits = yield* validate(SubscriptionLimits, inputLimits, "accept-limits");
@@ -316,17 +316,17 @@ const makeMemorySubscriptionStore = Effect.fn("makeMemorySubscriptionStore")(fun
316
316
  yield* failpoint.hit("subscription:accept:after");
317
317
  return result;
318
318
  });
319
- const event = Effect.fn("MemorySubscriptionStore.event")(function* (eventId) {
319
+ const event = Effect.fnUntraced(function* (eventId) {
320
320
  const text = (yield* Ref.get(state)).events.get(eventId);
321
321
  return text === void 0 ? null : yield* decodeEffect(AcceptedEvent, text, "event-record");
322
322
  });
323
- const pendingEvents = Effect.fn("MemorySubscriptionStore.pendingEvents")(function* (nowMillis, after, limit) {
323
+ const pendingEvents = Effect.fnUntraced(function* (nowMillis, after, limit) {
324
324
  const events = [];
325
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
326
  events.sort(compareScheduleNames);
327
327
  return events.slice(0, limit);
328
328
  });
329
- const candidates = Effect.fn("MemorySubscriptionStore.candidates")(function* (input, limit) {
329
+ const candidates = Effect.fnUntraced(function* (input, limit) {
330
330
  const accepted = yield* validate(AcceptedEvent, input, "candidates-event");
331
331
  yield* requirePartition(accepted, "candidates-partition");
332
332
  const stored = yield* event(accepted.eventId);
@@ -344,7 +344,7 @@ const makeMemorySubscriptionStore = Effect.fn("makeMemorySubscriptionStore")(fun
344
344
  records.sort((a, b) => a.ordinal - b.ordinal);
345
345
  return records.slice(0, limit);
346
346
  });
347
- const select = Effect.fn("MemorySubscriptionStore.select")(function* (inputEvent, inputDeliveries, cursor, complete, nowMillis, inputLimits) {
347
+ const select = Effect.fnUntraced(function* (inputEvent, inputDeliveries, cursor, complete, nowMillis, inputLimits) {
348
348
  const suppliedEvent = yield* validate(AcceptedEvent, inputEvent, "select-event");
349
349
  const deliveries = yield* validate(Schema.Array(SubscriptionDelivery), inputDeliveries, "select-deliveries");
350
350
  const limits = yield* validate(SubscriptionLimits, inputLimits, "select-limits");
@@ -457,7 +457,7 @@ const makeMemorySubscriptionStore = Effect.fn("makeMemorySubscriptionStore")(fun
457
457
  })).pipe(Effect.flatMap(Effect.fromResult));
458
458
  yield* failpoint.hit("subscription:select:after");
459
459
  });
460
- const catchUp = Effect.fn("MemorySubscriptionStore.catchUp")(function* (inputEvent, inputDelivery, nowMillis, inputLimits) {
460
+ const catchUp = Effect.fnUntraced(function* (inputEvent, inputDelivery, nowMillis, inputLimits) {
461
461
  const suppliedEvent = yield* validate(AcceptedEvent, inputEvent, "catch-up-event");
462
462
  const delivery = yield* validate(SubscriptionDelivery, inputDelivery, "catch-up-delivery");
463
463
  const limits = yield* validate(SubscriptionLimits, inputLimits, "catch-up-limits");
@@ -531,7 +531,7 @@ const makeMemorySubscriptionStore = Effect.fn("makeMemorySubscriptionStore")(fun
531
531
  })).pipe(Effect.flatMap(Effect.fromResult));
532
532
  yield* failpoint.hit("subscription:catch-up:after");
533
533
  });
534
- const deferEvent = Effect.fn("MemorySubscriptionStore.deferEvent")(function* (eventId, nextAttemptAtMillis, code) {
534
+ const deferEvent = Effect.fnUntraced(function* (eventId, nextAttemptAtMillis, code) {
535
535
  const routingFailure = code === void 0 ? "routing-failed" : yield* validate(SubscriptionName, code, "routing-failure");
536
536
  yield* failpoint.hit("subscription:defer-event:before");
537
537
  yield* Effect.uninterruptible(Ref.modify(state, (current) => {
@@ -563,19 +563,19 @@ const makeMemorySubscriptionStore = Effect.fn("makeMemorySubscriptionStore")(fun
563
563
  })).pipe(Effect.flatMap(Effect.fromResult));
564
564
  yield* failpoint.hit("subscription:defer-event:after");
565
565
  });
566
- const delivery = Effect.fn("MemorySubscriptionStore.delivery")(function* (input) {
566
+ const delivery = Effect.fnUntraced(function* (input) {
567
567
  const key = yield* validate(SubscriptionDeliveryKey, input, "delivery-key");
568
568
  yield* requirePartition(key.subscription, "delivery-partition");
569
569
  const text = (yield* Ref.get(state)).deliveries.get(subscriptionDeliveryKeyString(key));
570
570
  return text === void 0 ? null : yield* decodeEffect(SubscriptionDelivery, text, "delivery-record");
571
571
  });
572
- const pendingDeliveries = Effect.fn("MemorySubscriptionStore.pendingDeliveries")(function* (nowMillis, after, limit) {
572
+ const pendingDeliveries = Effect.fnUntraced(function* (nowMillis, after, limit) {
573
573
  const items = [];
574
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
575
  items.sort((a, b) => compareScheduleNames(subscriptionDeliveryKeyString(a), subscriptionDeliveryKeyString(b)));
576
576
  return items.slice(0, limit);
577
577
  });
578
- const listDeliveries = Effect.fn("MemorySubscriptionStore.listDeliveries")(function* (input, after, limit) {
578
+ const listDeliveries = Effect.fnUntraced(function* (input, after, limit) {
579
579
  const key = yield* requireKey(input, "list-deliveries-key");
580
580
  const items = [];
581
581
  for (const text of (yield* Ref.get(state)).deliveries.values()) {
@@ -586,7 +586,7 @@ const makeMemorySubscriptionStore = Effect.fn("makeMemorySubscriptionStore")(fun
586
586
  items.sort((a, b) => compareScheduleNames(subscriptionDeliveryKeyString(a.key), subscriptionDeliveryKeyString(b.key)));
587
587
  return items.slice(0, limit);
588
588
  });
589
- const changeDelivery = Effect.fn("MemorySubscriptionStore.changeDelivery")(function* (inputKey, inputDeliveryId, inputChange) {
589
+ const changeDelivery = Effect.fnUntraced(function* (inputKey, inputDeliveryId, inputChange) {
590
590
  const key = yield* validate(SubscriptionDeliveryKey, inputKey, "change-delivery-key");
591
591
  const deliveryId = yield* validate(Digest, inputDeliveryId, "change-delivery-id");
592
592
  const change = yield* validate(DeliveryChange, inputChange, "change-delivery-change");
@@ -633,7 +633,7 @@ const makeMemorySubscriptionStore = Effect.fn("makeMemorySubscriptionStore")(fun
633
633
  yield* failpoint.hit(`subscription:delivery-${change._tag.toLowerCase()}:after`);
634
634
  return result;
635
635
  });
636
- const recovering = Effect.fn("MemorySubscriptionStore.recovering")(function* (nowMillis, after, limit) {
636
+ const recovering = Effect.fnUntraced(function* (nowMillis, after, limit) {
637
637
  const records = [];
638
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
639
  key: item.key,
@@ -642,7 +642,7 @@ const makeMemorySubscriptionStore = Effect.fn("makeMemorySubscriptionStore")(fun
642
642
  records.sort((a, b) => a.ordinal - b.ordinal);
643
643
  return records.slice(0, limit);
644
644
  });
645
- const deferRecovery = Effect.fn("MemorySubscriptionStore.deferRecovery")(function* (input, expectedRevision, recovery) {
645
+ const deferRecovery = Effect.fnUntraced(function* (input, expectedRevision, recovery) {
646
646
  const key = yield* requireKey(input, "defer-recovery-key");
647
647
  yield* failpoint.hit("subscription:defer-recovery:before");
648
648
  yield* Effect.uninterruptible(Ref.modify(state, (current) => {
@@ -678,7 +678,7 @@ const makeMemorySubscriptionStore = Effect.fn("makeMemorySubscriptionStore")(fun
678
678
  yield* failpoint.hit("subscription:defer-recovery:after");
679
679
  });
680
680
  const readScanCursors = Ref.get(state).pipe(Effect.map((current) => current.scanCursors));
681
- const advanceScanCursors = Effect.fn("MemorySubscriptionStore.advanceScanCursors")(function* (input) {
681
+ const advanceScanCursors = Effect.fnUntraced(function* (input) {
682
682
  const cursors = yield* validate(SubscriptionScanCursors, input, "scan-cursors");
683
683
  yield* failpoint.hit("subscription:advance-scan-cursors:before");
684
684
  yield* Effect.uninterruptible(Ref.update(state, (current) => ({
@@ -699,8 +699,8 @@ const makeMemorySubscriptionStore = Effect.fn("makeMemorySubscriptionStore")(fun
699
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
700
  for (const record of current.registrationIndex.values()) if (record.state === "active" && record.recoveryAt !== null) consider(record.recoveryAt);
701
701
  return deadline;
702
- }).pipe(Effect.withSpan("MemorySubscriptionStore.nextDeadline"));
703
- const compact = Effect.fn("MemorySubscriptionStore.compact")(function* (nowMillis, inputPolicy, requestedLimit) {
702
+ });
703
+ const compact = Effect.fnUntraced(function* (nowMillis, inputPolicy, requestedLimit) {
704
704
  nowMillis = Math.min(nowMillis, yield* Clock.currentTimeMillis);
705
705
  const policy = yield* validate(SubscriptionRetentionPolicy, inputPolicy, "retention-policy");
706
706
  const limit = yield* validate(Schema.Int.check(Schema.isBetween({