@web-ts-toolkit/email-service 0.72.0 → 0.74.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.
package/index.mjs CHANGED
@@ -257,6 +257,12 @@ function scopeToken(value, code = "invalid_command") {
257
257
  function identifier(value, code) {
258
258
  return opaqueToken(value, MESSAGE_LIMITS.identifierBytes, code);
259
259
  }
260
+ function uuidV4(value, code) {
261
+ if (typeof value !== "string" || value.length !== 36 || !/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(value)) {
262
+ invalid(code);
263
+ }
264
+ return value;
265
+ }
260
266
  function hashToken(value, code) {
261
267
  if (typeof value !== "string" || value.length !== 64 || !/^[0-9a-f]{64}$/.test(value)) invalid(code);
262
268
  return value;
@@ -578,9 +584,9 @@ function syncStage(budget, code, run) {
578
584
  // src/internal/canonical-json.ts
579
585
  import { createHash } from "crypto";
580
586
  var treeLimits = { depth: MESSAGE_LIMITS.jsonDepth, values: MESSAGE_LIMITS.jsonValues };
581
- function canonicalJson(input, maxBytes, code = "invalid_snapshot", limits = treeLimits) {
587
+ function canonicalJson(input, maxBytes, code = "invalid_snapshot", limits2 = treeLimits) {
582
588
  let bytesLeft = maxBytes;
583
- let valuesLeft = limits.values;
589
+ let valuesLeft = limits2.values;
584
590
  const chunks = [];
585
591
  const ancestors = /* @__PURE__ */ new Set();
586
592
  function append(text, bytes = text.length) {
@@ -599,7 +605,7 @@ function canonicalJson(input, maxBytes, code = "invalid_snapshot", limits = tree
599
605
  append(JSON.stringify(text), bytes);
600
606
  }
601
607
  function visit(value2, depth) {
602
- if (depth > limits.depth || --valuesLeft < 0 || bytesLeft < 1) invalid(code);
608
+ if (depth > limits2.depth || --valuesLeft < 0 || bytesLeft < 1) invalid(code);
603
609
  switch (typeof value2) {
604
610
  case "string":
605
611
  appendString(value2);
@@ -623,7 +629,7 @@ function canonicalJson(input, maxBytes, code = "invalid_snapshot", limits = tree
623
629
  if (ancestors.has(value2)) invalid(code);
624
630
  ancestors.add(value2);
625
631
  try {
626
- const maxChildren = depth === limits.depth ? 0 : valuesLeft;
632
+ const maxChildren = depth === limits2.depth ? 0 : valuesLeft;
627
633
  if (isArray(value2, code)) {
628
634
  const length = denseArrayLength(value2, maxChildren, code);
629
635
  if (length && 2 * length + 1 > bytesLeft) invalid(code);
@@ -666,10 +672,83 @@ function sha256(input) {
666
672
  function hashJson(input, maxBytes, code = "invalid_snapshot") {
667
673
  return sha256(canonicalJson(input, maxBytes, code).encoded);
668
674
  }
675
+ function encodeEvidence(input, code, capacity) {
676
+ return canonicalJson(input, capacity?.limits.evidenceBytes ?? 8192, code, capacity?.limits.evidenceTree).encoded;
677
+ }
669
678
 
670
679
  // src/internal/message-codecs.ts
671
680
  import { isIP } from "net";
672
681
  import { types as types4 } from "util";
682
+
683
+ // src/internal/smtp-capacity.ts
684
+ var MIN_RECIPIENTS = MESSAGE_LIMITS.recipients + 1;
685
+ var MAX_SMTP_RECIPIENTS = 1e3;
686
+ var KIB = 1024;
687
+ var limits = Object.freeze({
688
+ snapshotBytes: 2 * KIB * KIB,
689
+ snapshotTree: Object.freeze({ depth: 4, values: 4096 }),
690
+ evidenceBytes: 64 * KIB,
691
+ evidenceTree: Object.freeze({ depth: 5, values: 4096 }),
692
+ jobBsonBytes: 4 * KIB * KIB,
693
+ activityBsonBytes: 128 * KIB,
694
+ registryCache: Object.freeze({ min: 0, default: 8, max: 16 }),
695
+ registryPending: Object.freeze({ min: 1, default: 2, max: 4 }),
696
+ servicePending: 4,
697
+ workerProfiles: 16,
698
+ workerConcurrency: Object.freeze({ min: 1, default: 1, max: 2 }),
699
+ runClaims: Object.freeze({ min: 1, default: 4, max: 16 }),
700
+ runMaintenance: Object.freeze({ min: 0, default: 4, max: 16 }),
701
+ adminMaintenance: Object.freeze({ min: 1, default: 4, max: 16 }),
702
+ page: Object.freeze({ min: 1, default: 8, max: 16 }),
703
+ memoryJobs: Object.freeze({ min: 1, default: 8, max: 16 }),
704
+ mongoInFlight: 4,
705
+ dueCandidates: 4
706
+ });
707
+ function readBinding(input, code) {
708
+ const { format, namespace, maxRecipients } = readRecord(input, ["format", "namespace", "maxRecipients"], [], code);
709
+ if (format !== "smtp-v2") invalid(code);
710
+ return Object.freeze({
711
+ format,
712
+ namespace: uuidV4(namespace, code),
713
+ maxRecipients: integer(maxRecipients, MIN_RECIPIENTS, MAX_SMTP_RECIPIENTS, code)
714
+ });
715
+ }
716
+ function readCapacityOption(raw) {
717
+ if (!Object.hasOwn(raw, "smtpCapacity")) return;
718
+ return Object.freeze({ binding: readBinding(raw.smtpCapacity, "invalid_config"), limits });
719
+ }
720
+ function capacityTag(capacity) {
721
+ return capacity ? { smtpCapacity: capacity.binding } : {};
722
+ }
723
+ function recipientLimit(capacity) {
724
+ return capacity?.binding.maxRecipients ?? MESSAGE_LIMITS.recipients;
725
+ }
726
+ function matchesBinding(input, capacity, code) {
727
+ const binding = readBinding(input, code);
728
+ if (binding.namespace !== capacity.binding.namespace || binding.maxRecipients !== capacity.binding.maxRecipients) {
729
+ invalid(code);
730
+ }
731
+ }
732
+ function assertCapacityTag(raw, capacity, code) {
733
+ const tagged = Object.hasOwn(raw, "smtpCapacity");
734
+ if (capacity) {
735
+ if (!tagged) invalid(code);
736
+ matchesBinding(raw.smtpCapacity, capacity, code);
737
+ } else if (tagged) {
738
+ invalid(code);
739
+ }
740
+ }
741
+ function assertPortCapacity(port, capacity) {
742
+ const code = "invalid_config";
743
+ const binding = readPortMember(port, "smtpCapacity");
744
+ if (capacity) matchesBinding(binding, capacity, code);
745
+ else if (binding !== void 0) invalid(code);
746
+ }
747
+ function assertCapacityProvider(provider, capacity, code) {
748
+ if (capacity && provider.kind !== "smtp") invalid(code);
749
+ }
750
+
751
+ // src/internal/message-codecs.ts
673
752
  var L = MESSAGE_LIMITS;
674
753
  var SNAPSHOT = "invalid_snapshot";
675
754
  var DOT_ATOM = /^[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*$/;
@@ -706,7 +785,8 @@ function validateMailbox(input, code = SNAPSHOT) {
706
785
  ...Object.hasOwn(mailbox, "name") ? { name: headerString(mailbox.name, L.displayNameBytes, code) } : {}
707
786
  });
708
787
  }
709
- function validateEnvelope(input, kind, code = SNAPSHOT) {
788
+ function validateEnvelope(input, kind, code = SNAPSHOT, capacity) {
789
+ const limit = recipientLimit(capacity);
710
790
  const envelope = readRecord(input, ["from", "to", "cc", "bcc"], ["replyTo"], code);
711
791
  const seen = /* @__PURE__ */ new Set();
712
792
  const recipient = (raw) => {
@@ -717,9 +797,9 @@ function validateEnvelope(input, kind, code = SNAPSHOT) {
717
797
  return mailbox;
718
798
  };
719
799
  const from = validateMailbox(envelope.from, code);
720
- const to = readArray(envelope.to, L.recipients, code, recipient);
721
- const cc = readArray(envelope.cc, L.recipients - to.length, code, recipient);
722
- const bcc = readArray(envelope.bcc, L.recipients - to.length - cc.length, code, recipient);
800
+ const to = readArray(envelope.to, limit, code, recipient);
801
+ const cc = readArray(envelope.cc, limit - to.length, code, recipient);
802
+ const bcc = readArray(envelope.bcc, limit - to.length - cc.length, code, recipient);
723
803
  if (seen.size === 0 || kind === "sendgrid" && to.length === 0) invalid(code);
724
804
  return Object.freeze({
725
805
  from,
@@ -807,12 +887,17 @@ function validateExpiry(input, preparedAt, scheduleAt, code = SNAPSHOT) {
807
887
  }
808
888
  return expiresAt;
809
889
  }
810
- function hashSnapshot(snapshot) {
890
+ function hashSnapshot(snapshot, capacity) {
811
891
  return sha256(
812
- canonicalJson(snapshot, L.snapshotBytes, SNAPSHOT, { depth: L.jsonDepth + 1, values: L.jsonValues + 256 }).encoded
892
+ canonicalJson(
893
+ snapshot,
894
+ capacity?.limits.snapshotBytes ?? L.snapshotBytes,
895
+ SNAPSHOT,
896
+ capacity?.limits.snapshotTree ?? { depth: L.jsonDepth + 1, values: L.jsonValues + 256 }
897
+ ).encoded
813
898
  );
814
899
  }
815
- function readSnapshot(input) {
900
+ function readSnapshot(input, capacity) {
816
901
  const dto = readRecord(
817
902
  input,
818
903
  [
@@ -827,17 +912,19 @@ function readSnapshot(input) {
827
912
  "attachments",
828
913
  "correlation"
829
914
  ],
830
- ["subject", "html", "text", "templateId", "data"],
915
+ ["subject", "html", "text", "templateId", "data", "smtpCapacity"],
831
916
  SNAPSHOT
832
917
  );
833
918
  if (dto.version !== EMAIL_FORMAT_VERSION || dto.kind !== "smtp" && dto.kind !== "sendgrid") invalid(SNAPSHOT);
919
+ assertCapacityTag(dto, capacity, SNAPSHOT);
834
920
  const provider = validateProviderRef(dto.provider);
835
921
  if (provider.kind !== dto.kind) invalid("provider_mismatch");
922
+ assertCapacityProvider(provider, capacity, "provider_mismatch");
836
923
  const preparedAt = unixMs(dto.preparedAt, SNAPSHOT);
837
924
  const common = {
838
925
  version: EMAIL_FORMAT_VERSION,
839
926
  template: validateTemplateIdentity(dto.template),
840
- envelope: validateEnvelope(dto.envelope, provider.kind),
927
+ envelope: validateEnvelope(dto.envelope, provider.kind, SNAPSHOT, capacity),
841
928
  preparedAt,
842
929
  expiresAt: validateExpiry(dto.expiresAt, preparedAt, null),
843
930
  priority: integer(dto.priority, PRIORITY.min, PRIORITY.max, SNAPSHOT),
@@ -851,7 +938,7 @@ function readSnapshot(input) {
851
938
  ...Object.hasOwn(dto, "html") ? { html: dto.html } : {},
852
939
  ...Object.hasOwn(dto, "text") ? { text: dto.text } : {}
853
940
  });
854
- return Object.freeze({ ...common, kind: provider.kind, provider, ...content });
941
+ return Object.freeze({ ...common, kind: provider.kind, provider, ...capacityTag(capacity), ...content });
855
942
  }
856
943
  if (Object.hasOwn(dto, "subject") || Object.hasOwn(dto, "html") || Object.hasOwn(dto, "text")) invalid(SNAPSHOT);
857
944
  return Object.freeze({
@@ -862,9 +949,9 @@ function readSnapshot(input) {
862
949
  data: copyJsonObject(dto.data, L.dataBytes, SNAPSHOT)
863
950
  });
864
951
  }
865
- function validateSnapshot(input) {
866
- const snapshot = readSnapshot(input);
867
- return { snapshot, snapshotHash: hashSnapshot(snapshot) };
952
+ function validateSnapshot(input, capacity) {
953
+ const snapshot = readSnapshot(input, capacity);
954
+ return { snapshot, snapshotHash: hashSnapshot(snapshot, capacity) };
868
955
  }
869
956
 
870
957
  // src/internal/command-codecs.ts
@@ -888,25 +975,35 @@ function encodeAdmissionKey(key) {
888
975
  function sameCommandMetadata(key, templateKey, provider, scheduleAt, expected) {
889
976
  return encodeAdmissionKey(key) === encodeAdmissionKey(expected.key) && templateKey === expected.templateKey && provider.kind === expected.provider.kind && provider.key === expected.provider.key && provider.mode === expected.provider.mode && scheduleAt === expected.scheduleAt;
890
977
  }
891
- function fingerprintFor(identity) {
978
+ function fingerprintFor(identity, capacity) {
892
979
  const { scope, key, templateKey, provider, scheduleAt, intentHash } = identity;
893
980
  return hashJson(
894
- [EMAIL_FORMAT_VERSION, scope, key, templateKey, provider, scheduleAt, intentHash],
981
+ [
982
+ ...capacity ? ["smtp-v2-admission", capacity.binding] : [],
983
+ EMAIL_FORMAT_VERSION,
984
+ scope,
985
+ key,
986
+ templateKey,
987
+ provider,
988
+ scheduleAt,
989
+ intentHash
990
+ ],
895
991
  MESSAGE_LIMITS.intentBytes
896
992
  );
897
993
  }
898
- function parseCommandIdentity(scope, command) {
994
+ function parseCommandIdentity(scope, command, capacity) {
899
995
  const code = "invalid_command";
900
996
  const parsedScope = scopeToken(scope, code);
901
997
  const raw = readRecord(command, ["key", "templateKey", "input", "provider", "intent"], ["scheduleAt"], code);
902
998
  const key = validateAdmissionKey(raw.key, code);
903
999
  const templateKey = identifier(raw.templateKey, code);
904
1000
  const provider = validateProviderRef(raw.provider, code);
1001
+ assertCapacityProvider(provider, capacity, "provider_mismatch");
905
1002
  const scheduleAt = raw.scheduleAt === void 0 ? null : unixMs(raw.scheduleAt, code);
906
1003
  const intent = canonicalJson(raw.intent, MESSAGE_LIMITS.intentBytes, code);
907
1004
  const identity = { scope: parsedScope, key, templateKey, provider, scheduleAt, intentHash: sha256(intent.encoded) };
908
1005
  return Object.freeze({
909
- identity: Object.freeze({ ...identity, fingerprint: fingerprintFor(identity) }),
1006
+ identity: Object.freeze({ ...identity, fingerprint: fingerprintFor(identity, capacity) }),
910
1007
  command: Object.freeze({
911
1008
  key,
912
1009
  templateKey,
@@ -917,35 +1014,40 @@ function parseCommandIdentity(scope, command) {
917
1014
  })
918
1015
  });
919
1016
  }
920
- function validatePreparedCommand(scope, input) {
1017
+ function validatePreparedCommand(scope, input, capacity) {
921
1018
  const expectedScope = scopeToken(scope);
922
1019
  const code = "invalid_snapshot";
923
1020
  const dto = readRecord(
924
1021
  input,
925
1022
  ["version", "scope", "key", "scheduleAt", "intentHash", "fingerprint", "snapshotHash", "snapshot"],
926
- [],
1023
+ ["smtpCapacity"],
927
1024
  code
928
1025
  );
929
1026
  if (dto.version !== EMAIL_FORMAT_VERSION || scopeToken(dto.scope, code) !== expectedScope) invalid(code);
1027
+ assertCapacityTag(dto, capacity, code);
930
1028
  const key = validateAdmissionKey(dto.key, code);
931
1029
  const scheduleAt = dto.scheduleAt === null ? null : unixMs(dto.scheduleAt, code);
932
1030
  const intentHash = hashToken(dto.intentHash, code);
933
1031
  const fingerprint = hashToken(dto.fingerprint, code);
934
1032
  const storedSnapshotHash = hashToken(dto.snapshotHash, code);
935
- const { snapshot, snapshotHash } = validateSnapshot(dto.snapshot);
1033
+ const { snapshot, snapshotHash } = validateSnapshot(dto.snapshot, capacity);
936
1034
  validateExpiry(snapshot.expiresAt, snapshot.preparedAt, scheduleAt);
937
- if (snapshotHash !== storedSnapshotHash || fingerprint !== fingerprintFor({
938
- scope: expectedScope,
939
- key,
940
- templateKey: snapshot.template.key,
941
- provider: snapshot.provider,
942
- scheduleAt,
943
- intentHash
944
- })) {
1035
+ if (snapshotHash !== storedSnapshotHash || fingerprint !== fingerprintFor(
1036
+ {
1037
+ scope: expectedScope,
1038
+ key,
1039
+ templateKey: snapshot.template.key,
1040
+ provider: snapshot.provider,
1041
+ scheduleAt,
1042
+ intentHash
1043
+ },
1044
+ capacity
1045
+ )) {
945
1046
  invalid(code);
946
1047
  }
947
1048
  return Object.freeze({
948
1049
  version: EMAIL_FORMAT_VERSION,
1050
+ ...capacityTag(capacity),
949
1051
  scope: expectedScope,
950
1052
  key,
951
1053
  scheduleAt,
@@ -956,6 +1058,11 @@ function validatePreparedCommand(scope, input) {
956
1058
  });
957
1059
  }
958
1060
 
1061
+ // src/internal/smtp-attachments.ts
1062
+ function assertSmtpAttachmentTypes(refs) {
1063
+ if (refs.some(({ contentType }) => /^(?:message|multipart)\//.test(contentType))) invalid("invalid_snapshot");
1064
+ }
1065
+
959
1066
  // src/templates/local-sources.ts
960
1067
  import { Buffer as Buffer2 } from "buffer";
961
1068
  import { addAbortListener as addAbortListener3 } from "events";
@@ -1179,7 +1286,7 @@ function readDefinition(value) {
1179
1286
  }
1180
1287
  return invalid(code);
1181
1288
  }
1182
- function captureFields(raw, definition, context, scheduleAt, budget, slot) {
1289
+ function captureFields(raw, definition, context, scheduleAt, budget, slot, capacity) {
1183
1290
  const code = "invalid_snapshot";
1184
1291
  const fields = readRecord(
1185
1292
  raw,
@@ -1197,7 +1304,9 @@ function captureFields(raw, definition, context, scheduleAt, budget, slot) {
1197
1304
  bcc: fields.bcc === void 0 ? [] : fields.bcc,
1198
1305
  ...replyTo === void 0 ? {} : { replyTo }
1199
1306
  },
1200
- context.provider.kind
1307
+ context.provider.kind,
1308
+ code,
1309
+ capacity
1201
1310
  );
1202
1311
  const expiresAt = validateExpiry(
1203
1312
  fields.expiresAt === void 0 ? context.now + DEFAULT_EXPIRY_MS : fields.expiresAt,
@@ -1206,6 +1315,7 @@ function captureFields(raw, definition, context, scheduleAt, budget, slot) {
1206
1315
  );
1207
1316
  const priority = boundedOption(fields.priority, PRIORITY, code);
1208
1317
  const attachments = validateAttachmentRefs(fields.attachments === void 0 ? [] : fields.attachments);
1318
+ if (context.provider.kind === "smtp") assertSmtpAttachmentTypes(attachments);
1209
1319
  const correlation = validateCorrelation(fields.correlation === void 0 ? {} : fields.correlation);
1210
1320
  const values = copyJsonObject(fields.values, MESSAGE_LIMITS.dataBytes, "invalid_data");
1211
1321
  const { parseData } = definition;
@@ -1220,18 +1330,25 @@ function findRegistryPreparation(registry) {
1220
1330
  }
1221
1331
  function createEmailTemplateRegistry(definitions, options = {}) {
1222
1332
  const code = "invalid_config";
1223
- const config = readRecord(options, [], ["clock", "cacheEntries", "maxPending", "helpers"], code);
1333
+ const config = readRecord(options, [], ["clock", "cacheEntries", "maxPending", "helpers", "smtpCapacity"], code);
1334
+ const capacity = readCapacityOption(config);
1224
1335
  const clock = readClock(config.clock);
1225
- const maxPending = boundedOption(config.maxPending, TEMPLATE_MAX_PENDING);
1226
- const renderer = createLocalRenderer(boundedOption(config.cacheEntries, TEMPLATE_CACHE_ENTRIES), config.helpers);
1336
+ const maxPending = boundedOption(config.maxPending, capacity?.limits.registryPending ?? TEMPLATE_MAX_PENDING);
1337
+ const renderer = createLocalRenderer(
1338
+ boundedOption(config.cacheEntries, capacity?.limits.registryCache ?? TEMPLATE_CACHE_ENTRIES),
1339
+ config.helpers
1340
+ );
1227
1341
  const registered = /* @__PURE__ */ new Map();
1228
1342
  for (const key of plainKeys(definitions, Infinity, code)) {
1229
- registered.set(identifier(key, code), readDefinition(ownData(definitions, key, code)));
1343
+ const definition = readDefinition(ownData(definitions, key, code));
1344
+ if (capacity && definition.kind !== "handlebars") invalid(code);
1345
+ registered.set(identifier(key, code), definition);
1230
1346
  }
1231
1347
  let pending = 0;
1232
1348
  async function prepareParsed({ identity, command: safe }, budget) {
1233
1349
  budget.check();
1234
1350
  const { templateKey, provider, key, scheduleAt } = identity;
1351
+ assertCapacityProvider(provider, capacity, "provider_mismatch");
1235
1352
  const definition = registered.get(templateKey);
1236
1353
  if (!definition) invalid("template_not_found");
1237
1354
  if ((definition.kind === "handlebars" ? "smtp" : "sendgrid") !== provider.kind) invalid("provider_mismatch");
@@ -1255,7 +1372,7 @@ function createEmailTemplateRegistry(definitions, options = {}) {
1255
1372
  slot,
1256
1373
  "preparation_failed",
1257
1374
  () => prepare(input, context),
1258
- (raw) => captureFields(raw, definition, context, scheduleAt, budget, slot)
1375
+ (raw) => captureFields(raw, definition, context, scheduleAt, budget, slot, capacity)
1259
1376
  );
1260
1377
  budget.check();
1261
1378
  const base = {
@@ -1267,17 +1384,18 @@ function createEmailTemplateRegistry(definitions, options = {}) {
1267
1384
  let snapshot;
1268
1385
  if (provider.kind === "smtp" && definition.kind === "handlebars") {
1269
1386
  const content = await renderer.render(templateKey, definition, data, budget, slot);
1270
- snapshot = Object.freeze({ ...base, kind: provider.kind, provider, ...content });
1387
+ snapshot = Object.freeze({ ...base, kind: provider.kind, provider, ...capacityTag(capacity), ...content });
1271
1388
  } else if (provider.kind === "sendgrid" && definition.kind === "sendgrid") {
1272
1389
  snapshot = Object.freeze({ ...base, kind: provider.kind, provider, templateId: definition.templateId, data });
1273
1390
  } else {
1274
1391
  return invalid("provider_mismatch");
1275
1392
  }
1276
1393
  budget.check();
1277
- const snapshotHash = hashSnapshot(snapshot);
1394
+ const snapshotHash = hashSnapshot(snapshot, capacity);
1278
1395
  budget.check();
1279
1396
  return Object.freeze({
1280
1397
  version: EMAIL_FORMAT_VERSION,
1398
+ ...capacityTag(capacity),
1281
1399
  scope: identity.scope,
1282
1400
  key,
1283
1401
  scheduleAt,
@@ -1291,6 +1409,7 @@ function createEmailTemplateRegistry(definitions, options = {}) {
1291
1409
  }
1292
1410
  }
1293
1411
  const registry = Object.freeze({
1412
+ ...capacityTag(capacity),
1294
1413
  invalidate(key) {
1295
1414
  if (key !== void 0) identifier(key, code);
1296
1415
  renderer.invalidate(key);
@@ -1300,7 +1419,7 @@ function createEmailTemplateRegistry(definitions, options = {}) {
1300
1419
  const budget = createBudget(clock, call.timeoutMs, call.signal);
1301
1420
  try {
1302
1421
  budget.check();
1303
- return await prepareParsed(parseCommandIdentity(scope, command), budget);
1422
+ return await prepareParsed(parseCommandIdentity(scope, command, capacity), budget);
1304
1423
  } finally {
1305
1424
  budget.close();
1306
1425
  }
@@ -1368,12 +1487,73 @@ function attemptOutcome(reason) {
1368
1487
  function isSafeTransient(reason) {
1369
1488
  return evidence[reason][1] === "safe_transient";
1370
1489
  }
1490
+ var recipientGroups = ["to", "cc", "bcc"];
1371
1491
  function recipientCounts(envelope) {
1372
1492
  return Object.freeze({ to: envelope.to.length, cc: envelope.cc.length, bcc: envelope.bcc.length });
1373
1493
  }
1494
+ function readRecipientCounts(input, code, capacity) {
1495
+ const limit = recipientLimit(capacity);
1496
+ const counts = readRecord(input, recipientGroups, [], code);
1497
+ const to = integer(counts.to, 0, limit, code);
1498
+ const cc = integer(counts.cc, 0, limit - to, code);
1499
+ const bcc = integer(counts.bcc, 0, limit - to - cc, code);
1500
+ if (to + cc + bcc === 0) invalid(code);
1501
+ return Object.freeze({ to, cc, bcc });
1502
+ }
1374
1503
  function smtpMessageId(attemptId) {
1375
1504
  return `<${attemptId}@email.invalid>`;
1376
1505
  }
1506
+ function readRecipientResults(input, code, { limit, allowAccepted, ordered }) {
1507
+ const seen = /* @__PURE__ */ new Set();
1508
+ let previous = -1;
1509
+ return readArray(input, limit, code, (value) => {
1510
+ const { group, index: rawIndex, status } = readRecord(value, ["group", "index", "status"], [], code);
1511
+ if (group !== "to" && group !== "cc" && group !== "bcc") invalid(code);
1512
+ if (status !== "rejected" && (status !== "accepted" || !allowAccepted)) invalid(code);
1513
+ const index = integer(rawIndex, 0, limit - 1, code);
1514
+ if (ordered) {
1515
+ const position = recipientGroups.indexOf(group) * limit + index;
1516
+ if (position <= previous) invalid(code);
1517
+ previous = position;
1518
+ } else {
1519
+ const key = `${group}:${index}`;
1520
+ if (seen.has(key)) invalid(code);
1521
+ seen.add(key);
1522
+ }
1523
+ return Object.freeze({ group, index, status });
1524
+ });
1525
+ }
1526
+ function allAccepted(recipients) {
1527
+ return recipients.length !== 0 && recipients.every(({ status }) => status === "accepted");
1528
+ }
1529
+ function createSmtpAcceptanceResult(recipients, messageId) {
1530
+ const code = "invalid_command";
1531
+ if (messageId !== void 0 && (typeof messageId !== "string" || messageId.length !== 52 || messageId !== smtpMessageId(uuidV4(messageId.slice(1, 37), code)))) {
1532
+ invalid(code);
1533
+ }
1534
+ const checked = readRecipientResults(recipients, code, {
1535
+ limit: MAX_SMTP_RECIPIENTS,
1536
+ allowAccepted: true,
1537
+ ordered: false
1538
+ });
1539
+ const data = { retry: "never", stage: "data", ...messageId === void 0 ? {} : { messageId } };
1540
+ if (allAccepted(checked)) {
1541
+ return Object.freeze({ ...data, reason: "smtp_accepted", outcome: "accepted", recipients: checked });
1542
+ }
1543
+ if (!checked.some(({ status }) => status === "accepted")) invalid(code);
1544
+ return Object.freeze({
1545
+ ...data,
1546
+ reason: "smtp_partial",
1547
+ outcome: "partially_accepted",
1548
+ recipients: checked
1549
+ });
1550
+ }
1551
+ function uncertainProviderResult(stage, reason) {
1552
+ const base = { outcome: "unknown", retry: "never", stage };
1553
+ if (reason === "provider_timeout") return { ...base, reason };
1554
+ if (reason === "provider_aborted") return { ...base, reason };
1555
+ return { ...base, reason };
1556
+ }
1377
1557
  function readHint(input, reason, code) {
1378
1558
  if (reason !== "rate_deferred" && reason !== "sendgrid_throttled") invalid(code);
1379
1559
  const raw = readRecord(input, [], ["delayMs", "notBefore"], code);
@@ -1387,7 +1567,7 @@ function readHint(input, reason, code) {
1387
1567
  ...hasFloor ? { notBefore: unixMs(raw.notBefore, code) } : {}
1388
1568
  });
1389
1569
  }
1390
- function readAttemptResult(input, code = "invalid_command", projected = false) {
1570
+ function readAttemptResult(input, code = "invalid_command", projected = false, capacity) {
1391
1571
  const raw = readRecord(
1392
1572
  input,
1393
1573
  ["outcome", "retry", "stage", "reason"],
@@ -1398,6 +1578,7 @@ function readAttemptResult(input, code = "invalid_command", projected = false) {
1398
1578
  const [outcome, retry, stages2, origin] = evidence[reason];
1399
1579
  const stage = stages2.find((value) => value === raw.stage);
1400
1580
  if (raw.outcome !== outcome || raw.retry !== retry || !stage) invalid(code);
1581
+ if (capacity && (origin === "sendgrid" || stage === "http")) invalid(code);
1401
1582
  const hasHint = Object.hasOwn(raw, "retryHint");
1402
1583
  if (projected && hasHint || !projected && reason === "rate_deferred" && !hasHint) invalid(code);
1403
1584
  const smtpSuccess = reason === "smtp_accepted" || reason === "smtp_partial";
@@ -1406,16 +1587,10 @@ function readAttemptResult(input, code = "invalid_command", projected = false) {
1406
1587
  let recipients;
1407
1588
  if (hasRecipients) {
1408
1589
  if (origin !== "smtp" && origin !== "provider" || stage !== "envelope" && stage !== "data") invalid(code);
1409
- const seen = /* @__PURE__ */ new Set();
1410
- recipients = readArray(raw.recipients, MESSAGE_LIMITS.recipients, code, (value) => {
1411
- const { group, index: rawIndex, status } = readRecord(value, ["group", "index", "status"], [], code);
1412
- if (group !== "to" && group !== "cc" && group !== "bcc") invalid(code);
1413
- if (status !== "rejected" && (status !== "accepted" || !smtpSuccess)) invalid(code);
1414
- const index = integer(rawIndex, 0, MESSAGE_LIMITS.recipients - 1, code);
1415
- const key = `${group}:${index}`;
1416
- if (seen.has(key)) invalid(code);
1417
- seen.add(key);
1418
- return Object.freeze({ group, index, status });
1590
+ recipients = readRecipientResults(raw.recipients, code, {
1591
+ limit: recipientLimit(capacity),
1592
+ allowAccepted: smtpSuccess,
1593
+ ordered: Boolean(capacity)
1419
1594
  });
1420
1595
  const accepted = recipients.filter(({ status }) => status === "accepted").length;
1421
1596
  if (reason === "smtp_accepted" && (!accepted || accepted !== recipients.length) || reason === "smtp_partial" && (!accepted || accepted === recipients.length)) {
@@ -1429,7 +1604,7 @@ function readAttemptResult(input, code = "invalid_command", projected = false) {
1429
1604
  if (hasProviderId && (reason !== "sendgrid_accepted" || typeof providerMessageId !== "string" || providerMessageId.length > 128 || !/^[A-Za-z0-9][A-Za-z0-9._=-]{0,127}$/.test(providerMessageId))) {
1430
1605
  invalid(code);
1431
1606
  }
1432
- return Object.freeze({
1607
+ const result = Object.freeze({
1433
1608
  outcome,
1434
1609
  retry,
1435
1610
  stage,
@@ -1439,6 +1614,8 @@ function readAttemptResult(input, code = "invalid_command", projected = false) {
1439
1614
  ...typeof messageId === "string" ? { messageId } : {},
1440
1615
  ...typeof providerMessageId === "string" ? { providerMessageId } : {}
1441
1616
  });
1617
+ if (capacity && !projected) encodeEvidence(result, code, capacity);
1618
+ return result;
1442
1619
  }
1443
1620
  function assertEvidencePhase(reason, phase, source, code) {
1444
1621
  const origin = evidence[reason][3];
@@ -1464,9 +1641,9 @@ function assertEvidenceContext(result, provider, attemptId, counts, code) {
1464
1641
  }
1465
1642
  }
1466
1643
  }
1467
- function readProviderResult(input, provider, attemptId, envelope) {
1644
+ function readProviderResult(input, provider, attemptId, envelope, capacity) {
1468
1645
  const code = "invalid_command";
1469
- const result = readAttemptResult(input, code);
1646
+ const result = readAttemptResult(input, code, false, capacity);
1470
1647
  const origin = evidence[result.reason][3];
1471
1648
  if (origin !== "smtp" && origin !== "sendgrid" && origin !== "provider") invalid(code);
1472
1649
  assertEvidenceContext(result, provider, attemptId, recipientCounts(envelope), code);
@@ -1497,10 +1674,7 @@ function attemptEffect(result, attemptId, ordinal, manual, policy, expiresAt, no
1497
1674
  var REQUEST = "invalid_command";
1498
1675
  var LEASE = WORKER_LIMIT_BOUNDS.leaseMs;
1499
1676
  function readStoreId(value, code = REQUEST) {
1500
- if (typeof value !== "string" || value.length !== 36 || !/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(value)) {
1501
- invalid(code);
1502
- }
1503
- return value;
1677
+ return uuidV4(value, code);
1504
1678
  }
1505
1679
  function readLeaseToken(value, code = REQUEST) {
1506
1680
  if (typeof value !== "string" || value.length < 22 || value.length > 128 || !/^[A-Za-z0-9_-]+$/.test(value) || /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value)) {
@@ -1514,16 +1688,20 @@ function readLeaseMs(value) {
1514
1688
  function timeAfter(start, delayMs) {
1515
1689
  return delayMs <= MAX_UNIX_MS - start ? start + delayMs : void 0;
1516
1690
  }
1517
- function readStoreCall(input, worker = false) {
1691
+ function readStoreCall(input, worker = false, capacity) {
1518
1692
  const code = "invalid_config";
1519
- const raw = readRecord(input, ["signal", "timeoutMs"], [], code);
1693
+ const { smtpCapacity, ...raw } = readRecord(input, ["signal", "timeoutMs"], capacity ? ["smtpCapacity"] : [], code);
1694
+ if (capacity) assertCapacityTag({ smtpCapacity }, capacity, "store_incompatible");
1520
1695
  const max = worker ? WORKER_LIMIT_BOUNDS.storeTimeoutMs.max : CALL_BUDGETS.admissionOrAdmin.max;
1521
1696
  const timeoutMs = integer(raw.timeoutMs, 1, max, code);
1522
1697
  const call = readCallOptions(raw, { default: STORE_PRIMITIVE_TIMEOUT_MS, min: 1, max });
1523
1698
  if (!call.signal) invalid(code);
1524
- return { signal: call.signal, timeoutMs: worker ? timeoutMs : Math.min(timeoutMs, STORE_PRIMITIVE_TIMEOUT_MS) };
1699
+ return {
1700
+ signal: call.signal,
1701
+ timeoutMs: worker && !capacity ? timeoutMs : Math.min(timeoutMs, STORE_PRIMITIVE_TIMEOUT_MS)
1702
+ };
1525
1703
  }
1526
- function readStoreNext(value, read) {
1704
+ function readStoreNext(value, read, kind) {
1527
1705
  const code = "store_incompatible";
1528
1706
  const raw = readRecord(value, ["status"], ["value"], code);
1529
1707
  if (raw.status === "applied" && Object.hasOwn(raw, "value")) {
@@ -1532,6 +1710,9 @@ function readStoreNext(value, read) {
1532
1710
  if ((raw.status === "none" || raw.status === "contended") && !Object.hasOwn(raw, "value")) {
1533
1711
  return Object.freeze({ status: raw.status });
1534
1712
  }
1713
+ if (kind === "claim" && raw.status === "expiry_maintenance_required" && !Object.hasOwn(raw, "value")) {
1714
+ return Object.freeze({ status: raw.status });
1715
+ }
1535
1716
  return invalid(code);
1536
1717
  }
1537
1718
  function readRetryPolicy(input, code = REQUEST) {
@@ -1558,9 +1739,9 @@ function readRetentionPolicy(input, code = REQUEST) {
1558
1739
  idempotencyMs: integer(idempotencyMs, bounds.idempotencyMs.min, bounds.idempotencyMs.max, code)
1559
1740
  });
1560
1741
  }
1561
- function readStoreAdmission(scope, input) {
1742
+ function readStoreAdmission(scope, input, capacity) {
1562
1743
  const raw = readRecord(input, ["command", "jobId", "retry", "retention"], [], REQUEST);
1563
- const command = validatePreparedCommand(scope, raw.command);
1744
+ const command = validatePreparedCommand(scope, raw.command, capacity);
1564
1745
  return Object.freeze({
1565
1746
  command,
1566
1747
  admissionKey: encodeAdmissionKey(command.key),
@@ -1581,7 +1762,7 @@ function readClaim(input) {
1581
1762
  leaseMs: readLeaseMs(leaseMs)
1582
1763
  });
1583
1764
  }
1584
- function readFinish(input) {
1765
+ function readFinish(input, capacity) {
1585
1766
  const { fence, phase, operationId, result } = readRecord(
1586
1767
  input,
1587
1768
  ["fence", "phase", "operationId", "result"],
@@ -1589,7 +1770,7 @@ function readFinish(input) {
1589
1770
  REQUEST
1590
1771
  );
1591
1772
  if (phase !== "pre_handoff" && phase !== "handed_off") invalid(REQUEST);
1592
- const evidence2 = readAttemptResult(result);
1773
+ const evidence2 = readAttemptResult(result, REQUEST, false, capacity);
1593
1774
  assertEvidencePhase(evidence2.reason, phase, "owner", REQUEST);
1594
1775
  return Object.freeze({ fence: readFence(fence), phase, operationId: readStoreId(operationId), result: evidence2 });
1595
1776
  }
@@ -1606,7 +1787,6 @@ function readResolution(input) {
1606
1787
  }
1607
1788
 
1608
1789
  // src/internal/activity-codecs.ts
1609
- var ACTIVITY_BYTES = 8192;
1610
1790
  var dispositions = [
1611
1791
  "accepted",
1612
1792
  "partially_accepted",
@@ -1633,7 +1813,7 @@ function assertActivityDisposition(result, disposition, code) {
1633
1813
  invalid(code);
1634
1814
  }
1635
1815
  }
1636
- function readActivity(scope, input, code = "store_incompatible") {
1816
+ function captureActivity(scope, input, code, capacity) {
1637
1817
  const raw = readRecord(
1638
1818
  input,
1639
1819
  [
@@ -1650,26 +1830,32 @@ function readActivity(scope, input, code = "store_incompatible") {
1650
1830
  "result",
1651
1831
  "disposition"
1652
1832
  ],
1653
- ["handedOffAt"],
1833
+ ["handedOffAt", "smtpCapacity", "recipientCounts"],
1654
1834
  code
1655
1835
  );
1656
1836
  if (raw.version !== EMAIL_FORMAT_VERSION || scopeToken(raw.scope, code) !== scope) invalid(code);
1837
+ assertCapacityTag(raw, capacity, code);
1838
+ if (!capacity && Object.hasOwn(raw, "recipientCounts")) invalid(code);
1839
+ const counts = capacity ? readRecipientCounts(raw.recipientCounts, code, capacity) : void 0;
1657
1840
  const jobId = readStoreId(raw.jobId, code);
1658
1841
  const attemptId = readStoreId(raw.attemptId, code);
1659
1842
  const provider = validateProviderRef(raw.provider, code);
1843
+ assertCapacityProvider(provider, capacity, code);
1660
1844
  const startedAt = unixMs(raw.startedAt, code);
1661
1845
  const finishedAt = unixMs(raw.finishedAt, code);
1662
1846
  const handedOffAt = Object.hasOwn(raw, "handedOffAt") ? unixMs(raw.handedOffAt, code) : void 0;
1663
1847
  if (finishedAt < startedAt || handedOffAt !== void 0 && (handedOffAt < startedAt || handedOffAt > finishedAt)) {
1664
1848
  invalid(code);
1665
1849
  }
1666
- const result = readAttemptResult(raw.result, code, true);
1850
+ const result = readAttemptResult(raw.result, code, true, capacity);
1667
1851
  assertEvidencePhase(result.reason, handedOffAt === void 0 ? "pre_handoff" : "handed_off", "ledger", code);
1668
- assertEvidenceContext(result, provider, attemptId, void 0, code);
1852
+ assertEvidenceContext(result, provider, attemptId, counts, code);
1669
1853
  const disposition = readDisposition(raw.disposition, code);
1670
1854
  assertActivityDisposition(result, disposition, code);
1671
1855
  return Object.freeze({
1672
1856
  version: EMAIL_FORMAT_VERSION,
1857
+ ...capacityTag(capacity),
1858
+ ...counts ? { recipientCounts: counts } : {},
1673
1859
  scope,
1674
1860
  jobId,
1675
1861
  attemptId,
@@ -1684,50 +1870,62 @@ function readActivity(scope, input, code = "store_incompatible") {
1684
1870
  disposition
1685
1871
  });
1686
1872
  }
1687
- function activityFor(job, commit) {
1873
+ function readActivity(scope, input, code = "store_incompatible", capacity) {
1874
+ const activity = captureActivity(scope, input, code, capacity);
1875
+ if (capacity) encodeEvidence(activity, code, capacity);
1876
+ return activity;
1877
+ }
1878
+ function activityFor(job, commit, capacity) {
1688
1879
  const { outcome, retry, stage, reason, recipients, messageId, providerMessageId } = commit.result;
1689
- return readActivity(job.scope, {
1690
- version: EMAIL_FORMAT_VERSION,
1691
- scope: job.scope,
1692
- jobId: job.jobId,
1693
- attemptId: commit.attemptId,
1694
- ordinal: commit.ordinal,
1695
- provider: job.provider,
1696
- template: job.template,
1697
- correlation: job.correlation,
1698
- startedAt: commit.startedAt,
1699
- ...commit.handedOffAt === void 0 ? {} : { handedOffAt: commit.handedOffAt },
1700
- finishedAt: commit.finishedAt,
1701
- disposition: commit.disposition,
1702
- result: {
1703
- outcome,
1704
- retry,
1705
- stage,
1706
- reason,
1707
- ...recipients === void 0 ? {} : { recipients },
1708
- ...messageId === void 0 ? {} : { messageId },
1709
- ...providerMessageId === void 0 ? {} : { providerMessageId }
1710
- }
1711
- });
1880
+ return captureActivity(
1881
+ job.scope,
1882
+ {
1883
+ version: EMAIL_FORMAT_VERSION,
1884
+ ...capacityTag(capacity),
1885
+ ...capacity ? { recipientCounts: job.recipientCounts } : {},
1886
+ scope: job.scope,
1887
+ jobId: job.jobId,
1888
+ attemptId: commit.attemptId,
1889
+ ordinal: commit.ordinal,
1890
+ provider: job.provider,
1891
+ template: job.template,
1892
+ correlation: job.correlation,
1893
+ startedAt: commit.startedAt,
1894
+ ...commit.handedOffAt === void 0 ? {} : { handedOffAt: commit.handedOffAt },
1895
+ finishedAt: commit.finishedAt,
1896
+ disposition: commit.disposition,
1897
+ result: {
1898
+ outcome,
1899
+ retry,
1900
+ stage,
1901
+ reason,
1902
+ ...recipients === void 0 ? {} : { recipients },
1903
+ ...messageId === void 0 ? {} : { messageId },
1904
+ ...providerMessageId === void 0 ? {} : { providerMessageId }
1905
+ }
1906
+ },
1907
+ "store_incompatible",
1908
+ capacity
1909
+ );
1712
1910
  }
1713
- function activityDigest(activity) {
1714
- return hashJson(activity, ACTIVITY_BYTES, "store_incompatible");
1911
+ function activityDigest(activity, capacity) {
1912
+ return sha256(encodeEvidence(activity, "store_incompatible", capacity));
1715
1913
  }
1716
- function readStoredActivity(scope, input, code = "store_incompatible") {
1914
+ function readStoredActivity(scope, input, code = "store_incompatible", capacity) {
1717
1915
  const raw = readRecord(input, ["activity", "digest"], [], code);
1718
- const activity = readActivity(scope, raw.activity, code);
1916
+ const activity = captureActivity(scope, raw.activity, code, capacity);
1719
1917
  const digest = hashToken(raw.digest, code);
1720
- if (activityDigest(activity) !== digest) invalid(code);
1918
+ if (activityDigest(activity, capacity) !== digest) invalid(code);
1721
1919
  return Object.freeze({ activity, digest });
1722
1920
  }
1723
- function readProjectionPermit(scope, input) {
1921
+ function readProjectionPermit(scope, input, capacity) {
1724
1922
  const code = "invalid_command";
1725
1923
  const {
1726
1924
  activity,
1727
1925
  digest,
1728
1926
  permitUntil: rawUntil
1729
1927
  } = readRecord(input, ["activity", "digest", "permitUntil"], [], code);
1730
- const stored = readStoredActivity(scope, { activity, digest }, code);
1928
+ const stored = readStoredActivity(scope, { activity, digest }, code, capacity);
1731
1929
  const permitUntil = unixMs(rawUntil, code);
1732
1930
  if (permitUntil - stored.activity.finishedAt < PROJECTION_PERMIT_MS) invalid(code);
1733
1931
  return Object.freeze({ ...stored, permitUntil });
@@ -1739,13 +1937,15 @@ function compareActivity(a, b) {
1739
1937
  return b.finishedAt - a.finishedAt || compareAscii(a.jobId, b.jobId) || compareAscii(a.attemptId, b.attemptId);
1740
1938
  }
1741
1939
  function cursorPayload(criteria, position) {
1940
+ const binding = criteria.capacity?.binding;
1742
1941
  return {
1743
1942
  v: EMAIL_FORMAT_VERSION,
1744
1943
  s: criteria.scope,
1745
1944
  j: criteria.jobId,
1746
1945
  f: criteria.from,
1747
1946
  b: criteria.before,
1748
- p: [position.finishedAt, position.jobId, position.attemptId]
1947
+ p: [position.finishedAt, position.jobId, position.attemptId],
1948
+ ...binding ? { c: [binding.format, binding.namespace, binding.maxRecipients] } : {}
1749
1949
  };
1750
1950
  }
1751
1951
  function encodeCursor(criteria, position) {
@@ -1766,7 +1966,7 @@ function decodeCursor(criteria, cursor) {
1766
1966
  } catch {
1767
1967
  invalid(code);
1768
1968
  }
1769
- const { p } = readRecord(input, ["v", "s", "j", "f", "b", "p"], [], code);
1969
+ const { p } = readRecord(input, ["v", "s", "j", "f", "b", "p"], criteria.capacity ? ["c"] : [], code);
1770
1970
  const tuple = readArray(p, 3, code, (entry) => entry);
1771
1971
  const position = Object.freeze({
1772
1972
  finishedAt: unixMs(tuple[0], code),
@@ -1778,15 +1978,16 @@ function decodeCursor(criteria, cursor) {
1778
1978
  }
1779
1979
  return position;
1780
1980
  }
1781
- function readActivityCriteria(scope, input) {
1981
+ function readActivityCriteria(scope, input, capacity) {
1782
1982
  const code = "invalid_command";
1783
1983
  const raw = readRecord(input, [], ["jobId", "from", "before", "limit", "cursor"], code);
1784
1984
  const jobId = Object.hasOwn(raw, "jobId") ? readStoreId(raw.jobId, code) : null;
1785
1985
  const from = Object.hasOwn(raw, "from") ? unixMs(raw.from, code) : null;
1786
1986
  const before = Object.hasOwn(raw, "before") ? unixMs(raw.before, code) : null;
1787
1987
  if (from !== null && before !== null && from > before) invalid(code);
1788
- const limit = Object.hasOwn(raw, "limit") ? integer(raw.limit, ACTIVITY_PAGE_LIMIT.min, ACTIVITY_PAGE_LIMIT.max, code) : ACTIVITY_PAGE_LIMIT.default;
1789
- const criteria = { scope, jobId, from, before, limit };
1988
+ const page = capacity?.limits.page ?? ACTIVITY_PAGE_LIMIT;
1989
+ const limit = Object.hasOwn(raw, "limit") ? integer(raw.limit, page.min, page.max, code) : page.default;
1990
+ const criteria = { scope, jobId, from, before, limit, ...capacity ? { capacity } : {} };
1790
1991
  return Object.freeze({
1791
1992
  ...criteria,
1792
1993
  ...Object.hasOwn(raw, "cursor") ? { after: decodeCursor(criteria, raw.cursor) } : {}
@@ -1806,7 +2007,7 @@ function readOrderedActivities(criteria, input, maxRows) {
1806
2007
  const code = "store_incompatible";
1807
2008
  let previous = criteria.after;
1808
2009
  return readArray(input, maxRows, code, (row) => {
1809
- const activity = readActivity(criteria.scope, row);
2010
+ const activity = readActivity(criteria.scope, row, code, criteria.capacity);
1810
2011
  if (previous && compareActivity(previous, activity) >= 0 || criteria.jobId !== null && criteria.jobId !== activity.jobId || criteria.from !== null && activity.finishedAt < criteria.from || criteria.before !== null && activity.finishedAt >= criteria.before) {
1811
2012
  invalid(code);
1812
2013
  }
@@ -1856,14 +2057,18 @@ function readUnknownResolution(input) {
1856
2057
  if (action !== "dismiss" && action !== "redrive") invalid(STORED);
1857
2058
  return Object.freeze({ attemptId: readStoreId(attemptId, STORED), at: unixMs(at, STORED), action });
1858
2059
  }
1859
- function readJobSummary(scope, input) {
2060
+ function readJobSummary(scope, input, capacity) {
1860
2061
  const raw = readRecord(
1861
2062
  input,
1862
2063
  COMMON,
1863
- ["latestAttempt", "nextAt", "phase", "disposition", "terminalReason", "terminalAt"],
2064
+ ["latestAttempt", "nextAt", "phase", "disposition", "terminalReason", "terminalAt", "smtpCapacity"],
1864
2065
  STORED
1865
2066
  );
1866
2067
  if (raw.version !== EMAIL_FORMAT_VERSION || scopeToken(raw.scope, STORED) !== scope) invalid(STORED);
2068
+ assertCapacityTag(raw, capacity, STORED);
2069
+ const optional = capacity ? ["latestAttempt", "smtpCapacity"] : ["latestAttempt"];
2070
+ const provider = validateProviderRef(raw.provider, STORED);
2071
+ assertCapacityProvider(provider, capacity, STORED);
1867
2072
  const createdAt = unixMs(raw.createdAt, STORED);
1868
2073
  const updatedAt = unixMs(raw.updatedAt, STORED);
1869
2074
  const expiresAt = unixMs(raw.expiresAt, STORED);
@@ -1872,16 +2077,16 @@ function readJobSummary(scope, input) {
1872
2077
  if (updatedAt < createdAt || firstDue >= expiresAt) invalid(STORED);
1873
2078
  let state;
1874
2079
  if (raw.state === "queued") {
1875
- readRecord(raw, [...COMMON, "nextAt"], ["latestAttempt"], STORED);
2080
+ readRecord(raw, [...COMMON, "nextAt"], optional, STORED);
1876
2081
  const nextAt = unixMs(raw.nextAt, STORED);
1877
2082
  if (nextAt < firstDue || nextAt >= expiresAt) invalid(STORED);
1878
2083
  state = { state: raw.state, nextAt };
1879
2084
  } else if (raw.state === "leased") {
1880
- readRecord(raw, [...COMMON, "phase"], ["latestAttempt"], STORED);
2085
+ readRecord(raw, [...COMMON, "phase"], optional, STORED);
1881
2086
  if (raw.phase !== "pre_handoff" && raw.phase !== "handed_off") invalid(STORED);
1882
2087
  state = { state: raw.state, phase: raw.phase };
1883
2088
  } else if (raw.state === "terminal") {
1884
- readRecord(raw, [...COMMON, "disposition", "terminalAt", "terminalReason"], ["latestAttempt"], STORED);
2089
+ readRecord(raw, [...COMMON, "disposition", "terminalAt", "terminalReason"], optional, STORED);
1885
2090
  const terminalAt = unixMs(raw.terminalAt, STORED);
1886
2091
  const disposition = readDisposition(raw.disposition, STORED);
1887
2092
  if (terminalAt < createdAt || terminalAt > updatedAt || disposition === "retry_scheduled") invalid(STORED);
@@ -1906,10 +2111,11 @@ function readJobSummary(scope, input) {
1906
2111
  const resolutions = readArray(raw.resolutions, Math.min(2, attemptCount - active), STORED, readUnknownResolution);
1907
2112
  return Object.freeze({
1908
2113
  version: EMAIL_FORMAT_VERSION,
2114
+ ...capacityTag(capacity),
1909
2115
  scope,
1910
2116
  jobId: readStoreId(raw.jobId, STORED),
1911
2117
  key: validateAdmissionKey(raw.key, STORED),
1912
- provider: validateProviderRef(raw.provider, STORED),
2118
+ provider,
1913
2119
  template: validateTemplateIdentity(raw.template, STORED),
1914
2120
  correlation: validateCorrelation(raw.correlation, STORED),
1915
2121
  scheduleAt,
@@ -1961,7 +2167,7 @@ async function runStoreCall(clock, parent, options) {
1961
2167
  budget.check();
1962
2168
  invalid("deadline");
1963
2169
  }
1964
- const call = Object.freeze({ timeoutMs, signal: budget.signal });
2170
+ const call = Object.freeze({ timeoutMs, signal: budget.signal, ...capacityTag(options.capacity) });
1965
2171
  let raw;
1966
2172
  try {
1967
2173
  const pending = options.invoke(call, dispatchedAt);
@@ -2023,16 +2229,18 @@ var serviceAdmissions = /* @__PURE__ */ new WeakMap();
2023
2229
  function findServiceAdmission(service) {
2024
2230
  return serviceAdmissions.get(service);
2025
2231
  }
2232
+ function createEmailRelay(options) {
2233
+ const raw = readRecord(options, ["scope", "store"], ["retry", "retention", "smtpCapacity"], CONFIG);
2234
+ const { enqueuePrepared } = createAdmissionCore(raw, systemEmailClock);
2235
+ return Object.freeze({ enqueuePrepared });
2236
+ }
2026
2237
  function createEmailService(options) {
2027
2238
  return createEmailServiceWithClock(options, systemEmailClock);
2028
2239
  }
2029
2240
  function createEmailServiceWithClock(options, clock) {
2030
- const raw = readRecord(options, ["scope", "store", "registry"], ["retry", "retention"], CONFIG);
2031
- const scope = scopeToken(raw.scope, CONFIG);
2032
- const retry = readRetryPolicy(readPolicy(raw.retry, DEFAULT_RETRY_POLICY), CONFIG);
2033
- const retention = readRetentionPolicy(readPolicy(raw.retention, DEFAULT_RETENTION_POLICY), CONFIG);
2034
- const find = bindMethod(raw.store, "findAdmission");
2035
- const insert = bindMethod(raw.store, "insert");
2241
+ const raw = readRecord(options, ["scope", "store", "registry"], ["retry", "retention", "smtpCapacity"], CONFIG);
2242
+ const { scope, capacity, enqueuePrepared, admitParsed, inOperation, withAdmission } = createAdmissionCore(raw, clock);
2243
+ assertPortCapacity(raw.registry, capacity);
2036
2244
  if (typeof raw.registry !== "object" || raw.registry === null || types6.isProxy(raw.registry)) invalid(CONFIG);
2037
2245
  const registry = raw.registry;
2038
2246
  const prepare = bindMethod(registry, "prepare");
@@ -2040,7 +2248,7 @@ function createEmailServiceWithClock(options, clock) {
2040
2248
  async function prepareMiss(parsed, parent) {
2041
2249
  const { identity, command } = parsed;
2042
2250
  const accept = (value) => {
2043
- const staged = validatePreparedCommand(scope, value);
2251
+ const staged = validatePreparedCommand(scope, value, capacity);
2044
2252
  if (!sameCommandMetadata(
2045
2253
  staged.key,
2046
2254
  staged.snapshot.template.key,
@@ -2069,7 +2277,7 @@ function createEmailServiceWithClock(options, clock) {
2069
2277
  slots = { pending: 0 };
2070
2278
  injectedRegistrySlots.set(registry, slots);
2071
2279
  }
2072
- if (slots.pending >= TEMPLATE_MAX_PENDING.default) invalid("busy");
2280
+ if (slots.pending >= (capacity?.limits.registryPending.default ?? TEMPLATE_MAX_PENDING.default)) invalid("busy");
2073
2281
  const budget = createChildBudget(clock, parent, CALL_BUDGETS.prepare.default);
2074
2282
  const occupied = slots;
2075
2283
  slots.pending++;
@@ -2096,15 +2304,37 @@ function createEmailServiceWithClock(options, clock) {
2096
2304
  slot.close();
2097
2305
  }
2098
2306
  }
2307
+ const enqueueParsed = (parsed, budget) => admitParsed(parsed, budget, prepareMiss);
2308
+ const service = Object.freeze({
2309
+ scope,
2310
+ ...capacityTag(capacity),
2311
+ enqueue(command, call = {}) {
2312
+ return inOperation(call, (budget) => enqueueParsed(parseCommandIdentity(scope, command, capacity), budget));
2313
+ },
2314
+ enqueuePrepared
2315
+ });
2316
+ serviceAdmissions.set(service, (parsed, budget) => withAdmission(() => enqueueParsed(parsed, budget)));
2317
+ return service;
2318
+ }
2319
+ function createAdmissionCore(raw, clock) {
2320
+ const capacity = readCapacityOption(raw);
2321
+ assertPortCapacity(raw.store, capacity);
2322
+ const scope = scopeToken(raw.scope, CONFIG);
2323
+ const retry = readRetryPolicy(readPolicy(raw.retry, DEFAULT_RETRY_POLICY), CONFIG);
2324
+ const retention = readRetentionPolicy(readPolicy(raw.retention, DEFAULT_RETENTION_POLICY), CONFIG);
2325
+ const find = bindMethod(raw.store, "findAdmission");
2326
+ const insert = bindMethod(raw.store, "insert");
2327
+ let admissions = 0;
2099
2328
  function findWinner(key, budget) {
2100
2329
  return runStoreCall(clock, budget, {
2101
2330
  maxMs: STORE_PRIMITIVE_TIMEOUT_MS,
2331
+ capacity,
2102
2332
  effect: "read",
2103
2333
  invoke: (call) => find(scope, key, call),
2104
2334
  accept(value) {
2105
2335
  if (value === null) return null;
2106
2336
  const raw2 = readRecord(value, ["job", "fingerprint"], [], STORED2);
2107
- const job = readJobSummary(scope, raw2.job);
2337
+ const job = readJobSummary(scope, raw2.job, capacity);
2108
2338
  if (encodeAdmissionKey(job.key) !== encodeAdmissionKey(key)) invalid(STORED2);
2109
2339
  return { job, fingerprint: hashToken(raw2.fingerprint, STORED2) };
2110
2340
  }
@@ -2118,10 +2348,12 @@ function createEmailServiceWithClock(options, clock) {
2118
2348
  }
2119
2349
  async function insertOrConfirm(command, identity, budget) {
2120
2350
  budget.check();
2351
+ if (command.snapshot.kind === "smtp") assertSmtpAttachmentTypes(command.snapshot.attachments);
2121
2352
  const admission = Object.freeze({ command, jobId: randomUUID(), retry, retention });
2122
2353
  try {
2123
2354
  const job = await runStoreCall(clock, budget, {
2124
2355
  maxMs: STORE_PRIMITIVE_TIMEOUT_MS,
2356
+ capacity,
2125
2357
  effect: "mutation",
2126
2358
  definite: INSERT_REJECTIONS,
2127
2359
  invoke: (call) => insert(scope, admission, call),
@@ -2129,7 +2361,7 @@ function createEmailServiceWithClock(options, clock) {
2129
2361
  const raw2 = readRecord(value, ["status"], ["job"], STORED2);
2130
2362
  if (raw2.status === "duplicate" && !Object.hasOwn(raw2, "job")) return null;
2131
2363
  if (raw2.status !== "inserted") invalid(STORED2);
2132
- const job2 = readJobSummary(scope, raw2.job);
2364
+ const job2 = readJobSummary(scope, raw2.job, capacity);
2133
2365
  const { snapshot } = command;
2134
2366
  if (job2.jobId !== admission.jobId || !matchesAdmissionJob(job2, identity, "enqueued") || job2.template.revision !== snapshot.template.revision || snapshot.preparedAt - job2.createdAt > MAX_PREPARED_AT_SKEW_MS || job2.expiresAt !== snapshot.expiresAt || job2.priority !== snapshot.priority || !sameCorrelation(job2.correlation, snapshot.correlation)) {
2135
2367
  invalid(STORED2);
@@ -2149,46 +2381,51 @@ function createEmailServiceWithClock(options, clock) {
2149
2381
  }
2150
2382
  return invalid("write_unconfirmed");
2151
2383
  }
2152
- async function enqueueParsed(parsed, budget) {
2384
+ async function admitParsed(parsed, budget, prepareMiss) {
2153
2385
  if (parsed.identity.scope !== scope) invalid("invalid_command");
2154
2386
  const winner = await findWinner(parsed.identity.key, budget);
2155
2387
  if (winner) return replay(winner, parsed.identity);
2156
2388
  return insertOrConfirm(await prepareMiss(parsed, budget), parsed.identity, budget);
2157
2389
  }
2158
- async function inOperation(call, run) {
2159
- const { timeoutMs, signal } = readCallOptions(call, CALL_BUDGETS.admissionOrAdmin);
2160
- const budget = createBudget(clock, timeoutMs, signal);
2390
+ async function withAdmission(run) {
2391
+ if (!capacity) return run();
2392
+ if (admissions >= capacity.limits.servicePending) invalid("busy");
2393
+ admissions++;
2161
2394
  try {
2162
- budget.check();
2163
- return await run(budget);
2395
+ return await run();
2164
2396
  } finally {
2165
- budget.close();
2397
+ admissions--;
2166
2398
  }
2167
2399
  }
2168
- const service = Object.freeze({
2169
- scope,
2170
- enqueue(command, call = {}) {
2171
- return inOperation(call, (budget) => enqueueParsed(parseCommandIdentity(scope, command), budget));
2172
- },
2173
- enqueuePrepared(input, call = {}) {
2174
- return inOperation(call, async (budget) => {
2175
- const command = validatePreparedCommand(scope, input);
2176
- const identity = {
2177
- scope,
2178
- key: command.key,
2179
- templateKey: command.snapshot.template.key,
2180
- provider: command.snapshot.provider,
2181
- scheduleAt: command.scheduleAt,
2182
- intentHash: command.intentHash,
2183
- fingerprint: command.fingerprint
2184
- };
2185
- const winner = await findWinner(identity.key, budget);
2186
- return winner ? replay(winner, identity) : insertOrConfirm(command, identity, budget);
2187
- });
2188
- }
2189
- });
2190
- serviceAdmissions.set(service, enqueueParsed);
2191
- return service;
2400
+ function inOperation(call, run) {
2401
+ return withAdmission(async () => {
2402
+ const { timeoutMs, signal } = readCallOptions(call, CALL_BUDGETS.admissionOrAdmin);
2403
+ const budget = createBudget(clock, timeoutMs, signal);
2404
+ try {
2405
+ budget.check();
2406
+ return await run(budget);
2407
+ } finally {
2408
+ budget.close();
2409
+ }
2410
+ });
2411
+ }
2412
+ function enqueuePrepared(input, call = {}) {
2413
+ return inOperation(call, async (budget) => {
2414
+ const command = validatePreparedCommand(scope, input, capacity);
2415
+ const identity = {
2416
+ scope,
2417
+ key: command.key,
2418
+ templateKey: command.snapshot.template.key,
2419
+ provider: command.snapshot.provider,
2420
+ scheduleAt: command.scheduleAt,
2421
+ intentHash: command.intentHash,
2422
+ fingerprint: command.fingerprint
2423
+ };
2424
+ const winner = await findWinner(identity.key, budget);
2425
+ return winner ? replay(winner, identity) : insertOrConfirm(command, identity, budget);
2426
+ });
2427
+ }
2428
+ return { scope, capacity, enqueuePrepared, admitParsed, inOperation, withAdmission };
2192
2429
  }
2193
2430
  function readPolicy(input, defaults) {
2194
2431
  const result = { ...defaults };
@@ -2212,30 +2449,36 @@ import { types as types7 } from "util";
2212
2449
 
2213
2450
  // src/internal/worker-config.ts
2214
2451
  var CONFIG2 = "invalid_config";
2215
- function readLimits(input) {
2452
+ function readLimits(input, capacity) {
2216
2453
  const keys = Object.keys(WORKER_LIMIT_BOUNDS);
2217
2454
  const raw = readRecord(input === void 0 ? {} : input, [], keys, CONFIG2);
2218
- const limits = {};
2219
- for (const key of keys) limits[key] = boundedOption(raw[key], WORKER_LIMIT_BOUNDS[key]);
2220
- const { leaseMs, storeTimeoutMs, providerTimeoutMs, finalizeGraceMs } = limits;
2455
+ const limits2 = {};
2456
+ for (const key of keys) {
2457
+ const bounds = key === "maxConcurrency" ? capacity?.limits.workerConcurrency : void 0;
2458
+ limits2[key] = boundedOption(raw[key], bounds ?? WORKER_LIMIT_BOUNDS[key]);
2459
+ }
2460
+ const { leaseMs, storeTimeoutMs, providerTimeoutMs, finalizeGraceMs } = limits2;
2221
2461
  if (leaseMs < Math.max(
2222
2462
  3 * storeTimeoutMs + CLOCK_MARGIN_MS,
2223
2463
  storeTimeoutMs + providerTimeoutMs + finalizeGraceMs + CLOCK_MARGIN_MS
2224
2464
  )) {
2225
2465
  invalid(CONFIG2);
2226
2466
  }
2227
- return Object.freeze(limits);
2467
+ return Object.freeze(limits2);
2228
2468
  }
2229
2469
  function sameProvider(a, b) {
2230
2470
  return a.kind === b.kind && a.key === b.key && a.mode === b.mode;
2231
2471
  }
2232
- function readProfiles(input) {
2472
+ function readProfiles(input, capacity) {
2233
2473
  const profiles = /* @__PURE__ */ new Map();
2234
- readArray(input, Number.MAX_SAFE_INTEGER, CONFIG2, (provider) => {
2474
+ readArray(input, capacity?.limits.workerProfiles ?? Number.MAX_SAFE_INTEGER, CONFIG2, (provider) => {
2235
2475
  const ref = validateProviderRef(readPortMember(provider, "ref"), CONFIG2);
2476
+ assertPortCapacity(provider, capacity);
2477
+ assertCapacityProvider(ref, capacity, CONFIG2);
2236
2478
  if (profiles.has(ref.key)) invalid(CONFIG2);
2237
2479
  profiles.set(ref.key, Object.freeze({ ref, prepare: bindMethod(provider, "prepare") }));
2238
2480
  });
2481
+ if (profiles.size === 0) invalid(CONFIG2);
2239
2482
  return profiles;
2240
2483
  }
2241
2484
  function readResolvers(input) {
@@ -2273,15 +2516,18 @@ function readWorkerConfig(input) {
2273
2516
  const raw = readRecord(
2274
2517
  input,
2275
2518
  ["scope", "store", "providers"],
2276
- ["attachmentResolvers", "rateLimiter", "clock", "limits"],
2519
+ ["attachmentResolvers", "rateLimiter", "clock", "limits", "smtpCapacity"],
2277
2520
  CONFIG2
2278
2521
  );
2279
2522
  const { store } = raw;
2523
+ const capacity = readCapacityOption(raw);
2524
+ assertPortCapacity(store, capacity);
2280
2525
  return Object.freeze({
2526
+ capacity,
2281
2527
  scope: scopeToken(raw.scope, CONFIG2),
2282
- limits: readLimits(raw.limits),
2528
+ limits: readLimits(raw.limits, capacity),
2283
2529
  clock: readWorkerClock(raw.clock),
2284
- profiles: readProfiles(raw.providers),
2530
+ profiles: readProfiles(raw.providers, capacity),
2285
2531
  resolvers: readResolvers(raw.attachmentResolvers),
2286
2532
  limiter: readLimiter(raw.rateLimiter),
2287
2533
  store: Object.freeze({
@@ -2295,7 +2541,7 @@ function readWorkerConfig(input) {
2295
2541
  })
2296
2542
  });
2297
2543
  }
2298
- function readRunOptions(input, limits) {
2544
+ function readRunOptions(input, limits2, capacity) {
2299
2545
  const { limit, concurrency, maintenanceLimit, ...call } = readRecord(
2300
2546
  input,
2301
2547
  [],
@@ -2304,9 +2550,9 @@ function readRunOptions(input, limits) {
2304
2550
  );
2305
2551
  return Object.freeze({
2306
2552
  ...readCallOptions(call, CALL_BUDGETS.batchOrRun),
2307
- limit: boundedOption(limit, RUN_CLAIM_LIMIT),
2308
- concurrency: boundedOption(concurrency, { min: 1, max: limits.maxConcurrency, default: limits.maxConcurrency }),
2309
- maintenanceLimit: boundedOption(maintenanceLimit, RUN_MAINTENANCE_LIMIT)
2553
+ limit: boundedOption(limit, capacity?.limits.runClaims ?? RUN_CLAIM_LIMIT),
2554
+ concurrency: boundedOption(concurrency, { min: 1, max: limits2.maxConcurrency, default: limits2.maxConcurrency }),
2555
+ maintenanceLimit: boundedOption(maintenanceLimit, capacity?.limits.runMaintenance ?? RUN_MAINTENANCE_LIMIT)
2310
2556
  });
2311
2557
  }
2312
2558
  function readShutdownOptions(input) {
@@ -2322,10 +2568,10 @@ function readGrant(times, leaseMs) {
2322
2568
  if (leaseEndsAt - grantedAt !== leaseMs) invalid(STORED3);
2323
2569
  return Object.freeze({ grantedAt, leaseEndsAt });
2324
2570
  }
2325
- function readClaimed(value, scope, expected) {
2571
+ function readClaimed(value, scope, expected, capacity) {
2326
2572
  const raw = readRecord(value, ["job", "snapshot", "fence", "lease", "ordinal", "manual"], [], STORED3);
2327
- const job = readJobSummary(scope, raw.job);
2328
- const { snapshot } = validateSnapshot(raw.snapshot);
2573
+ const job = readJobSummary(scope, raw.job, capacity);
2574
+ const { snapshot } = validateSnapshot(raw.snapshot, capacity);
2329
2575
  const fence = readFence(raw.fence);
2330
2576
  const lease = readGrant(readRecord(raw.lease, ["grantedAt", "leaseEndsAt"], [], STORED3), expected.leaseMs);
2331
2577
  const ordinal = integer(raw.ordinal, 1, MAX_ATTEMPT_LEDGER, STORED3);
@@ -2360,7 +2606,7 @@ function readHandoff(value, last, claim, leaseMs, times) {
2360
2606
  assertLaterGrant(grant, last, claim.job.expiresAt, times);
2361
2607
  return Object.freeze({ ...grant, handoffAt });
2362
2608
  }
2363
- function readCommit(value) {
2609
+ function readCommit(value, capacity) {
2364
2610
  const raw = readRecord(
2365
2611
  value,
2366
2612
  ["jobId", "attemptId", "ordinal", "operationId", "startedAt", "finishedAt", "result", "disposition", "reason"],
@@ -2371,7 +2617,7 @@ function readCommit(value) {
2371
2617
  const finishedAt = unixMs(raw.finishedAt, STORED3);
2372
2618
  const handedOffAt = Object.hasOwn(raw, "handedOffAt") ? unixMs(raw.handedOffAt, STORED3) : void 0;
2373
2619
  const retryAt = Object.hasOwn(raw, "retryAt") ? unixMs(raw.retryAt, STORED3) : void 0;
2374
- const result = readAttemptResult(raw.result, STORED3);
2620
+ const result = readAttemptResult(raw.result, STORED3, false, capacity);
2375
2621
  const disposition = readDisposition(raw.disposition, STORED3);
2376
2622
  const reason = raw.reason === "attempts_exhausted" ? raw.reason : readAttemptReason(raw.reason, STORED3);
2377
2623
  assertEvidencePhase(result.reason, handedOffAt === void 0 ? "pre_handoff" : "handed_off", "ledger", STORED3);
@@ -2394,23 +2640,23 @@ function readCommit(value) {
2394
2640
  ...retryAt === void 0 ? {} : { retryAt }
2395
2641
  });
2396
2642
  }
2397
- function readRecovery(value) {
2398
- const commit = readCommit(value);
2643
+ function readRecovery(value, capacity) {
2644
+ const commit = readCommit(value, capacity);
2399
2645
  const { result, startedAt, handedOffAt, finishedAt } = commit;
2400
2646
  if (result.reason !== "pre_handoff_lease_expired" && result.reason !== "post_handoff_lease_expired" || finishedAt - (handedOffAt ?? startedAt) < WORKER_LIMIT_BOUNDS.leaseMs.min) {
2401
2647
  invalid(STORED3);
2402
2648
  }
2403
2649
  return commit;
2404
2650
  }
2405
- function readQueuedExpiry(value, scope) {
2406
- const job = readJobSummary(scope, value);
2651
+ function readQueuedExpiry(value, scope, capacity) {
2652
+ const job = readJobSummary(scope, value, capacity);
2407
2653
  if (job.state !== "terminal" || job.disposition !== "expired" || job.terminalReason !== "expired" || job.terminalAt < job.expiresAt || job.updatedAt !== job.terminalAt) {
2408
2654
  invalid(STORED3);
2409
2655
  }
2410
2656
  return job;
2411
2657
  }
2412
- function readOwnedCommit(value, { claim, lease, handoff }, request, providerInvoked, direct) {
2413
- const commit = readCommit(value);
2658
+ function readOwnedCommit(value, { claim, lease, handoff }, request, providerInvoked, direct, capacity) {
2659
+ const commit = readCommit(value, capacity);
2414
2660
  const { attemptId, startedAt, handedOffAt, finishedAt, result, disposition, retryAt } = commit;
2415
2661
  const { job, snapshot, fence, ordinal, manual } = claim;
2416
2662
  const { expiresAt } = job;
@@ -2423,7 +2669,7 @@ function readOwnedCommit(value, { claim, lease, handoff }, request, providerInvo
2423
2669
  assertEvidenceContext(result, snapshot.provider, attemptId, recipientCounts(snapshot.envelope), STORED3);
2424
2670
  if (request && commit.operationId === request.operationId) {
2425
2671
  const phase = handedOffAt === void 0 ? "pre_handoff" : "handed_off";
2426
- if (phase !== request.phase || finishedAt >= last.leaseEndsAt || canonicalJson(result, 8192, STORED3).encoded !== canonicalJson(request.result, 8192, STORED3).encoded) {
2672
+ if (phase !== request.phase || finishedAt >= last.leaseEndsAt || encodeEvidence(result, STORED3, capacity) !== encodeEvidence(request.result, STORED3, capacity)) {
2427
2673
  invalid(STORED3);
2428
2674
  }
2429
2675
  } else if (direct) {
@@ -2435,7 +2681,7 @@ function readOwnedCommit(value, { claim, lease, handoff }, request, providerInvo
2435
2681
  // src/internal/worker-finish.ts
2436
2682
  import { randomUUID as randomUUID2 } from "crypto";
2437
2683
  async function finishAttempt(run, evidence2, providerInvoked, result) {
2438
- const { clock, store, scope, limits } = run.config;
2684
+ const { clock, store, scope, limits: limits2, capacity } = run.config;
2439
2685
  const {
2440
2686
  claim: { fence },
2441
2687
  handoff,
@@ -2444,13 +2690,13 @@ async function finishAttempt(run, evidence2, providerInvoked, result) {
2444
2690
  const now = monotonicNow(clock);
2445
2691
  const request = result && handoff !== "unconfirmed" ? Object.freeze({ fence, phase: handoff ? "handed_off" : "pre_handoff", operationId: randomUUID2(), result }) : void 0;
2446
2692
  const leaseEnd = safeLeaseEnd(handoff && handoff !== "unconfirmed" ? handoff : lease);
2447
- const grace = createBudgetUntil(clock, Math.min(now, run.budget.deadline) + limits.finalizeGraceMs);
2693
+ const grace = createBudgetUntil(clock, Math.min(now, run.budget.deadline) + limits2.finalizeGraceMs);
2448
2694
  async function confirm() {
2449
2695
  try {
2450
2696
  return await run.callStore(grace, {
2451
2697
  effect: "read",
2452
2698
  invoke: (call) => store.findAttempt(scope, fence.jobId, fence.attemptId, call),
2453
- accept: (value) => value === null ? null : readOwnedCommit(value, evidence2, request, providerInvoked, false)
2699
+ accept: (value) => value === null ? null : readOwnedCommit(value, evidence2, request, providerInvoked, false, capacity)
2454
2700
  });
2455
2701
  } catch {
2456
2702
  return null;
@@ -2461,7 +2707,7 @@ async function finishAttempt(run, evidence2, providerInvoked, result) {
2461
2707
  effect: "mutation",
2462
2708
  deadline: leaseEnd,
2463
2709
  invoke: (call) => store.finish(scope, exact, call),
2464
- accept: (value) => value === null ? null : readOwnedCommit(value, evidence2, exact, providerInvoked, true)
2710
+ accept: (value) => value === null ? null : readOwnedCommit(value, evidence2, exact, providerInvoked, true, capacity)
2465
2711
  });
2466
2712
  }
2467
2713
  try {
@@ -2638,8 +2884,8 @@ var AttemptOwner = class {
2638
2884
  }
2639
2885
  }
2640
2886
  async renewWhile(budget, stopped) {
2641
- const { clock, limits } = this.run.config;
2642
- const cadence = Math.floor(limits.leaseMs / 3);
2887
+ const { clock, limits: limits2 } = this.run.config;
2888
+ const cadence = Math.floor(limits2.leaseMs / 3);
2643
2889
  const waitSignal = AbortSignal.any([stopped, this.run.noLaunch]);
2644
2890
  while (!waitSignal.aborted) {
2645
2891
  try {
@@ -2709,7 +2955,7 @@ var AttemptOwner = class {
2709
2955
  }
2710
2956
  };
2711
2957
  async function launch(run, owner, mark, submit, preflight) {
2712
- const { clock, limits } = run.config;
2958
+ const { clock, limits: limits2, capacity } = run.config;
2713
2959
  const { provider, envelope, expiresAt } = owner.claim.snapshot;
2714
2960
  const { attemptId } = owner.claim.fence;
2715
2961
  const launchEnd = Math.min(
@@ -2717,7 +2963,7 @@ async function launch(run, owner, mark, submit, preflight) {
2717
2963
  mark.dispatchedAt + (expiresAt - mark.grant.handoffAt) - CLOCK_MARGIN_MS
2718
2964
  );
2719
2965
  const stage = provider.kind === "smtp" ? "data" : "http";
2720
- const unknown = (reason) => Object.freeze({ outcome: "unknown", retry: "never", stage, reason });
2966
+ const unknown = (reason) => Object.freeze(uncertainProviderResult(stage, reason));
2721
2967
  let io;
2722
2968
  try {
2723
2969
  run.checkLaunch();
@@ -2727,7 +2973,7 @@ async function launch(run, owner, mark, submit, preflight) {
2727
2973
  io = createChildBudgetUntil(
2728
2974
  clock,
2729
2975
  run.budget,
2730
- Math.min(now + limits.providerTimeoutMs, safeLeaseEnd(mark) - limits.finalizeGraceMs)
2976
+ Math.min(now + limits2.providerTimeoutMs, safeLeaseEnd(mark) - limits2.finalizeGraceMs)
2731
2977
  );
2732
2978
  run.checkLaunch();
2733
2979
  io.check();
@@ -2743,7 +2989,7 @@ async function launch(run, owner, mark, submit, preflight) {
2743
2989
  if (!types7.isPromise(returned)) return unknown("transport_uncertain");
2744
2990
  let result;
2745
2991
  try {
2746
- result = readProviderResult(raw, provider, attemptId, envelope);
2992
+ result = readProviderResult(raw, provider, attemptId, envelope, capacity);
2747
2993
  } catch {
2748
2994
  return unknown("transport_uncertain");
2749
2995
  }
@@ -2767,8 +3013,8 @@ async function launch(run, owner, mark, submit, preflight) {
2767
3013
  }
2768
3014
  async function processOwned(owner, slot) {
2769
3015
  const { run, claim, lease } = owner;
2770
- const { clock, limits } = run.config;
2771
- const deadline = lease.dispatchedAt + limits.preHandoffTimeoutMs;
3016
+ const { clock, limits: limits2 } = run.config;
3017
+ const deadline = lease.dispatchedAt + limits2.preHandoffTimeoutMs;
2772
3018
  let budget;
2773
3019
  try {
2774
3020
  budget = createChildBudgetUntil(clock, run.budget, deadline);
@@ -2847,11 +3093,16 @@ var WorkerRun = class {
2847
3093
  if (this.noLaunch.aborted) invalid("aborted");
2848
3094
  }
2849
3095
  callStore(budget, operation) {
2850
- const { clock, limits } = this.config;
3096
+ const { clock, limits: limits2, capacity } = this.config;
2851
3097
  let invoked = false;
2852
3098
  return runStoreCall(clock, budget, {
2853
3099
  ...operation,
2854
- maxMs: Math.min(operation.maxMs ?? limits.storeTimeoutMs, limits.storeTimeoutMs),
3100
+ capacity,
3101
+ maxMs: Math.min(
3102
+ operation.maxMs ?? limits2.storeTimeoutMs,
3103
+ limits2.storeTimeoutMs,
3104
+ capacity ? STORE_PRIMITIVE_TIMEOUT_MS : Infinity
3105
+ ),
2855
3106
  beforeInvoke: () => {
2856
3107
  if (operation.authorize) this.checkLaunch();
2857
3108
  operation.beforeInvoke?.();
@@ -2929,25 +3180,29 @@ async function sweep(run, limit, items, invoke, read, entry) {
2929
3180
  return false;
2930
3181
  }
2931
3182
  function claimNext(run) {
2932
- const { clock, scope, store, limits } = run.config;
3183
+ const { clock, scope, store, limits: limits2, capacity } = run.config;
2933
3184
  const request = Object.freeze({
2934
3185
  attemptId: randomUUID3(),
2935
3186
  token: randomBytes(16).toString("base64url"),
2936
- leaseMs: limits.leaseMs
3187
+ leaseMs: limits2.leaseMs
2937
3188
  });
2938
3189
  let dispatchedAt;
2939
3190
  return run.callStore(run.budget, {
2940
3191
  effect: "mutation",
2941
3192
  authorize: true,
2942
- maxMs: limits.preHandoffTimeoutMs,
3193
+ maxMs: limits2.preHandoffTimeoutMs,
2943
3194
  invoke(call, start) {
2944
3195
  dispatchedAt = start;
2945
3196
  return store.claimNext(scope, request, call);
2946
3197
  },
2947
- accept: (value) => readStoreNext(value, (raw) => {
2948
- const claim = readClaimed(raw, scope, request);
2949
- return { claim, dispatchedAt, confirmedAt: monotonicNow(clock) };
2950
- })
3198
+ accept: (value) => readStoreNext(
3199
+ value,
3200
+ (raw) => {
3201
+ const claim = readClaimed(raw, scope, request, capacity);
3202
+ return { claim, dispatchedAt, confirmedAt: monotonicNow(clock) };
3203
+ },
3204
+ "claim"
3205
+ )
2951
3206
  });
2952
3207
  }
2953
3208
  async function drainClaims(run, options, items, slots) {
@@ -2983,7 +3238,7 @@ async function drainClaims(run, options, items, slots) {
2983
3238
  }
2984
3239
  if (next.status !== "applied") {
2985
3240
  slot.close();
2986
- stop = next.status === "contended" ? "contended" : "idle";
3241
+ stop = next.status === "none" ? "idle" : next.status;
2987
3242
  break;
2988
3243
  }
2989
3244
  const { claim } = next.value;
@@ -3023,13 +3278,13 @@ async function drainClaims(run, options, items, slots) {
3023
3278
  }
3024
3279
  async function runQueue(run, options, slots, shutdown) {
3025
3280
  const items = [];
3026
- const { scope, store } = run.config;
3281
+ const { scope, store, capacity } = run.config;
3027
3282
  const contended = await sweep(
3028
3283
  run,
3029
3284
  options.maintenanceLimit,
3030
3285
  items,
3031
3286
  (call) => store.recoverNext(scope, call),
3032
- readRecovery,
3287
+ (value) => readRecovery(value, capacity),
3033
3288
  (commit) => ({
3034
3289
  operation: "recover",
3035
3290
  jobId: commit.jobId,
@@ -3043,7 +3298,7 @@ async function runQueue(run, options, slots, shutdown) {
3043
3298
  options.maintenanceLimit,
3044
3299
  items,
3045
3300
  (call) => store.expireNext(scope, call),
3046
- (value) => readQueuedExpiry(value, scope),
3301
+ (value) => readQueuedExpiry(value, scope, capacity),
3047
3302
  (job) => ({
3048
3303
  operation: "expire",
3049
3304
  jobId: job.jobId,
@@ -3060,7 +3315,8 @@ async function runQueue(run, options, slots, shutdown) {
3060
3315
  else if (allowedErrorCode(run.budget.signal.reason, ["deadline"])) stop = "deadline";
3061
3316
  else if (run.persistenceFailures) stop = "store_error";
3062
3317
  else if (run.budget.signal.aborted) return invalid("invalid_config");
3063
- else if (queueStop !== "contended" && slots.size === run.config.limits.maxConcurrency) stop = "callback_capacity";
3318
+ else if (queueStop === "contended" || queueStop === "expiry_maintenance_required") stop = queueStop;
3319
+ else if (slots.size === run.config.limits.maxConcurrency) stop = "callback_capacity";
3064
3320
  else if (queueStop === "idle" && timeLeft < 1)
3065
3321
  stop = "deadline";
3066
3322
  else stop = queueStop;
@@ -3068,16 +3324,17 @@ async function runQueue(run, options, slots, shutdown) {
3068
3324
  }
3069
3325
  function createEmailWorker(options) {
3070
3326
  const config = readWorkerConfig(options);
3071
- const { clock, scope, limits } = config;
3327
+ const { clock, scope, limits: limits2, capacity } = config;
3072
3328
  const shutdown = new AbortController();
3073
3329
  const slots = /* @__PURE__ */ new Set();
3074
3330
  let active;
3075
3331
  const worker = {
3076
3332
  scope,
3333
+ ...capacityTag(capacity),
3077
3334
  async run(options2 = {}) {
3078
3335
  if (shutdown.signal.aborted) invalid("closed");
3079
3336
  if (active) invalid("busy");
3080
- const settings = readRunOptions(options2, limits);
3337
+ const settings = readRunOptions(options2, limits2, capacity);
3081
3338
  const done = new AbortController();
3082
3339
  active = done;
3083
3340
  try {
@@ -3144,7 +3401,7 @@ var ADMISSION_ERRORS = [
3144
3401
  function boundScope(port) {
3145
3402
  return scopeToken(readPortMember(port, "scope"), "invalid_config");
3146
3403
  }
3147
- function readBatch(scope, input) {
3404
+ function readBatch(scope, input, capacity) {
3148
3405
  const code = "invalid_command";
3149
3406
  const raw = readRecord(input, ["batchId", "items"], [], code);
3150
3407
  const batchId = identifier(raw.batchId, code);
@@ -3155,7 +3412,10 @@ function readBatch(scope, input) {
3155
3412
  if (seen.has(itemId)) invalid(code);
3156
3413
  seen.add(itemId);
3157
3414
  const command = readRecord(item.command, ["templateKey", "input", "provider", "intent"], ["scheduleAt"], code);
3158
- return { itemId, parsed: parseCommandIdentity(scope, { ...command, key: { kind: "batch", batchId, itemId } }) };
3415
+ return {
3416
+ itemId,
3417
+ parsed: parseCommandIdentity(scope, { ...command, key: { kind: "batch", batchId, itemId } }, capacity)
3418
+ };
3159
3419
  });
3160
3420
  if (!items.length) invalid(code);
3161
3421
  return { batchId, items };
@@ -3164,25 +3424,49 @@ function stopReason(budget) {
3164
3424
  if (budget.remainingMs() > 0) return;
3165
3425
  return allowedErrorCode(budget.signal.reason, STOP_ERRORS2) ?? invalid("invalid_config");
3166
3426
  }
3167
- function readAdmissionResult(input, identity) {
3427
+ function readAdmissionResult(input, identity, capacity) {
3168
3428
  const code = "write_unconfirmed";
3169
3429
  const { status, job: raw } = readRecord(input, ["status", "job"], [], code);
3170
3430
  if (status !== "enqueued" && status !== "replayed") invalid(code);
3171
- const job = readJobSummary(identity.scope, raw);
3431
+ const job = readJobSummary(identity.scope, raw, capacity);
3172
3432
  if (!matchesAdmissionJob(job, identity, status)) invalid(code);
3173
3433
  return { status, job };
3174
3434
  }
3175
- function createEmailBatchService(options) {
3176
- return createEmailBatchServiceWithClock(options, systemEmailClock);
3435
+ function createEmailBatchAdmission(options) {
3436
+ return createEmailBatchAdmissionWithClock(options, systemEmailClock);
3177
3437
  }
3178
- function createEmailBatchServiceWithClock(options, clock) {
3179
- const { service, worker } = readRecord(options, ["service", "worker"], [], "invalid_config");
3438
+ function createEmailBatchAdmissionWithClock(options, clock) {
3439
+ const raw = readRecord(options, ["service"], ["smtpCapacity"], "invalid_config");
3440
+ const { service } = raw;
3441
+ const capacity = readCapacityOption(raw);
3442
+ assertPortCapacity(service, capacity);
3443
+ return createBatchAdmission(service, boundScope(service), capacity, clock);
3444
+ }
3445
+ function createEmailBatchService(options) {
3446
+ const raw = readRecord(options, ["service", "worker"], ["smtpCapacity"], "invalid_config");
3447
+ const { service, worker } = raw;
3448
+ const capacity = readCapacityOption(raw);
3449
+ assertPortCapacity(service, capacity);
3450
+ assertPortCapacity(worker, capacity);
3180
3451
  const scope = boundScope(service);
3181
3452
  if (scope !== boundScope(worker)) invalid("invalid_config");
3182
3453
  const run = bindMethod(worker, "run");
3454
+ const { enqueueBatch } = createBatchAdmission(service, scope, capacity, systemEmailClock);
3455
+ const batch = {
3456
+ enqueueBatch,
3457
+ async run(options2) {
3458
+ if (boundScope(worker) !== scope) invalid("invalid_config");
3459
+ assertPortCapacity(worker, capacity);
3460
+ return run(options2);
3461
+ }
3462
+ };
3463
+ return Object.freeze(batch);
3464
+ }
3465
+ function createBatchAdmission(service, scope, capacity, clock) {
3183
3466
  const enqueue = bindMethod(service, "enqueue");
3184
3467
  const admit = findServiceAdmission(service) ?? (async ({ command, identity }, budget) => {
3185
3468
  if (boundScope(service) !== scope) invalid("invalid_config");
3469
+ assertPortCapacity(service, capacity);
3186
3470
  const bounds = CALL_BUDGETS.admissionOrAdmin;
3187
3471
  const timeoutMs = Math.max(bounds.min, Math.min(bounds.max, Math.floor(budget.remainingMs())));
3188
3472
  budget.check();
@@ -3197,17 +3481,20 @@ function createEmailBatchServiceWithClock(options, clock) {
3197
3481
  throw error;
3198
3482
  }
3199
3483
  try {
3200
- return readAdmissionResult(raw, identity);
3484
+ return readAdmissionResult(raw, identity, capacity);
3201
3485
  } catch {
3202
3486
  return invalid("write_unconfirmed");
3203
3487
  }
3204
3488
  });
3489
+ let admitting = false;
3205
3490
  const batch = {
3206
3491
  async enqueueBatch(input, call = {}) {
3492
+ if (capacity && admitting) invalid("busy");
3207
3493
  const { signal, timeoutMs } = readCallOptions(call, CALL_BUDGETS.batchOrRun);
3208
3494
  const budget = createBudget(clock, timeoutMs, signal);
3495
+ admitting = true;
3209
3496
  try {
3210
- const { batchId, items: commands } = readBatch(scope, input);
3497
+ const { batchId, items: commands } = readBatch(scope, input, capacity);
3211
3498
  const items = [];
3212
3499
  const counts = { enqueued: 0, replayed: 0, failed: 0, notStarted: 0, persistenceFailures: 0 };
3213
3500
  for (const { itemId, parsed } of commands) {
@@ -3229,12 +3516,9 @@ function createEmailBatchServiceWithClock(options, clock) {
3229
3516
  }
3230
3517
  return Object.freeze({ batchId, items: Object.freeze(items), counts: Object.freeze(counts) });
3231
3518
  } finally {
3519
+ admitting = false;
3232
3520
  budget.close();
3233
3521
  }
3234
- },
3235
- async run(options2) {
3236
- if (boundScope(worker) !== scope) invalid("invalid_config");
3237
- return run(options2);
3238
3522
  }
3239
3523
  };
3240
3524
  return Object.freeze(batch);
@@ -3254,7 +3538,10 @@ function createEmailAdmin(options) {
3254
3538
  return createEmailAdminWithClock(options, systemEmailClock);
3255
3539
  }
3256
3540
  function createEmailAdminWithClock(options, clock) {
3257
- const { scope: input, store, onActivity: callback } = readRecord(options, ["scope", "store"], ["onActivity"], CONFIG3);
3541
+ const raw = readRecord(options, ["scope", "store"], ["onActivity", "smtpCapacity"], CONFIG3);
3542
+ const { scope: input, store, onActivity: callback } = raw;
3543
+ const capacity = readCapacityOption(raw);
3544
+ assertPortCapacity(store, capacity);
3258
3545
  const scope = scopeToken(input, CONFIG3);
3259
3546
  const findJob = bindMethod(store, "findJob");
3260
3547
  const cancel = bindMethod(store, "cancel");
@@ -3266,6 +3553,7 @@ function createEmailAdminWithClock(options, clock) {
3266
3553
  const cleanupNext = bindMethod(store, "cleanupNext");
3267
3554
  const onActivity = callback === void 0 ? void 0 : requireCallback(callback);
3268
3555
  let observers = 0;
3556
+ let active = false;
3269
3557
  function observe(activity, counts) {
3270
3558
  if (!onActivity) return;
3271
3559
  if (observers >= MAX_OBSERVER_PENDING) {
@@ -3284,29 +3572,35 @@ function createEmailAdminWithClock(options, clock) {
3284
3572
  }
3285
3573
  }
3286
3574
  function readJob(jobId, value) {
3287
- const job = readJobSummary(scope, value);
3575
+ const job = readJobSummary(scope, value, capacity);
3288
3576
  if (job.jobId !== jobId) invalid(STORED4);
3289
3577
  return job;
3290
3578
  }
3291
3579
  function callStore(budget, operation) {
3292
- return runStoreCall(clock, budget, { ...operation, maxMs: STORE_PRIMITIVE_TIMEOUT_MS });
3580
+ return runStoreCall(clock, budget, { ...operation, maxMs: STORE_PRIMITIVE_TIMEOUT_MS, capacity });
3293
3581
  }
3294
3582
  function write(budget, invoke, accept) {
3295
3583
  return callStore(budget, { effect: "mutation", invoke, accept });
3296
3584
  }
3297
3585
  async function inOperation(options2, run) {
3298
- const { timeoutMs, signal: source } = readCallOptions(options2, CALL_BUDGETS.admissionOrAdmin);
3299
- const signal = source && AbortSignal.any([source]);
3300
- const budget = createBudget(clock, timeoutMs, signal);
3586
+ if (capacity && active) invalid("busy");
3587
+ active = true;
3301
3588
  try {
3302
- return await run(budget, signal);
3589
+ const { timeoutMs, signal: source } = readCallOptions(options2, CALL_BUDGETS.admissionOrAdmin);
3590
+ const signal = source && AbortSignal.any([source]);
3591
+ const budget = createBudget(clock, timeoutMs, signal);
3592
+ try {
3593
+ return await run(budget, signal);
3594
+ } finally {
3595
+ budget.close();
3596
+ }
3303
3597
  } finally {
3304
- budget.close();
3598
+ active = false;
3305
3599
  }
3306
3600
  }
3307
3601
  async function maintain(options2, step) {
3308
3602
  const { limit: input2, ...call } = readRecord(options2, [], ["limit", "signal", "timeoutMs"], CONFIG3);
3309
- const limit = boundedOption(input2, ADMIN_MAINTENANCE_LIMIT);
3603
+ const limit = boundedOption(input2, capacity?.limits.adminMaintenance ?? ADMIN_MAINTENANCE_LIMIT);
3310
3604
  return inOperation(call, async (budget, signal) => {
3311
3605
  let stop = "limit";
3312
3606
  for (let i = 0; i < limit; i++) {
@@ -3349,11 +3643,11 @@ function createEmailAdminWithClock(options, clock) {
3349
3643
  budget,
3350
3644
  (call) => cancel(scope, id, call),
3351
3645
  (value) => {
3352
- const raw = readRecord(value, ["status"], ["job"], STORED4);
3353
- const { status } = raw;
3354
- if (status === "not_found" && !Object.hasOwn(raw, "job")) return Object.freeze({ status });
3646
+ const raw2 = readRecord(value, ["status"], ["job"], STORED4);
3647
+ const { status } = raw2;
3648
+ if (status === "not_found" && !Object.hasOwn(raw2, "job")) return Object.freeze({ status });
3355
3649
  if (status !== "cancelled" && status !== "too_late") invalid(STORED4);
3356
- const job = readJob(id, raw.job);
3650
+ const job = readJob(id, raw2.job);
3357
3651
  if (status === "cancelled" !== (job.state === "terminal" && job.disposition === "cancelled")) {
3358
3652
  invalid(STORED4);
3359
3653
  }
@@ -3370,8 +3664,8 @@ function createEmailAdminWithClock(options, clock) {
3370
3664
  definite: RESOLUTION_REJECTIONS,
3371
3665
  invoke: (call) => resolveUnknown(scope, request, call),
3372
3666
  accept(value) {
3373
- const { status, job: raw } = readRecord(value, ["status", "job"], [], STORED4);
3374
- const job = readJob(request.jobId, raw);
3667
+ const { status, job: raw2 } = readRecord(value, ["status", "job"], [], STORED4);
3668
+ const job = readJob(request.jobId, raw2);
3375
3669
  const decision = job.resolutions.find(({ attemptId }) => attemptId === request.attemptId);
3376
3670
  if (decision?.action !== request.action || status !== "resolved" && status !== "replayed") {
3377
3671
  invalid(STORED4);
@@ -3386,7 +3680,7 @@ function createEmailAdminWithClock(options, clock) {
3386
3680
  },
3387
3681
  listActivities(query = {}, options2 = {}) {
3388
3682
  return inOperation(options2, (budget) => {
3389
- const criteria = readActivityCriteria(scope, query);
3683
+ const criteria = readActivityCriteria(scope, query, capacity);
3390
3684
  const request = activityQuery(criteria);
3391
3685
  return callStore(budget, {
3392
3686
  effect: "read",
@@ -3401,7 +3695,7 @@ function createEmailAdminWithClock(options, clock) {
3401
3695
  const next = await write(
3402
3696
  budget,
3403
3697
  (call) => reserve(scope, call),
3404
- (value) => readStoreNext(value, (permit2) => readProjectionPermit(scope, permit2))
3698
+ (value) => readStoreNext(value, (permit2) => readProjectionPermit(scope, permit2, capacity))
3405
3699
  );
3406
3700
  if (next.status !== "applied") return next.status === "none" ? "idle" : "contended";
3407
3701
  counts.reserved++;
@@ -3516,6 +3810,7 @@ function jobSummary(job) {
3516
3810
  const latest = job.attempts.at(-1)?.commit;
3517
3811
  return Object.freeze({
3518
3812
  version: EMAIL_FORMAT_VERSION,
3813
+ ...job.smtpCapacity ? { smtpCapacity: job.smtpCapacity } : {},
3519
3814
  scope: job.scope,
3520
3815
  jobId: job.jobId,
3521
3816
  key: Object.freeze({ ...job.key }),
@@ -3540,13 +3835,13 @@ function jobSummary(job) {
3540
3835
  ...summaryState
3541
3836
  });
3542
3837
  }
3543
- function copyCommit(commit) {
3544
- return Object.freeze({ ...commit, result: readAttemptResult(commit.result, STORED5) });
3838
+ function copyCommit(commit, capacity) {
3839
+ return Object.freeze({ ...commit, result: readAttemptResult(commit.result, STORED5, false, capacity) });
3545
3840
  }
3546
- function readStoredSnapshot(job, input) {
3841
+ function readStoredSnapshot(job, input, capacity) {
3547
3842
  let validated;
3548
3843
  try {
3549
- validated = validateSnapshot(input);
3844
+ validated = validateSnapshot(input, capacity);
3550
3845
  } catch {
3551
3846
  return invalid(STORED5);
3552
3847
  }
@@ -3581,15 +3876,18 @@ function replace(job, time, patch, value, before, pendingAck) {
3581
3876
  write: { kind: "replace", guard: pendingAck ? { ...expect, pendingAck } : expect, next }
3582
3877
  };
3583
3878
  }
3584
- function newJob(admission, time) {
3879
+ function newJob(admission, time, capacity) {
3585
3880
  const { command, jobId, retry, retention, admissionKey } = admission;
3586
3881
  const { scope, key, scheduleAt, fingerprint, snapshotHash, snapshot } = command;
3587
3882
  const now = unixMs(time.now, "store_unavailable");
3588
3883
  if (snapshot.preparedAt - now > MAX_PREPARED_AT_SKEW_MS) invalid("invalid_snapshot");
3589
3884
  const nextAt = Math.max(now, scheduleAt ?? now);
3590
3885
  if (nextAt >= snapshot.expiresAt) invalid("expired");
3886
+ if (snapshot.kind === "smtp") assertSmtpAttachmentTypes(snapshot.attachments);
3591
3887
  return Object.freeze({
3592
3888
  version: EMAIL_FORMAT_VERSION,
3889
+ ...capacityTag(capacity),
3890
+ ...capacity ? { intentHash: command.intentHash } : {},
3593
3891
  scope,
3594
3892
  jobId,
3595
3893
  key,
@@ -3628,14 +3926,18 @@ function liveLease(job, fence, phase, now) {
3628
3926
  }
3629
3927
  return lease;
3630
3928
  }
3631
- function claimJob(job, claim, time) {
3929
+ function claimEligibility(job, now) {
3930
+ if (job.state.state !== "queued" || job.state.nextAt > now) return "not_due";
3931
+ return now >= job.expiresAt ? "expired" : "eligible";
3932
+ }
3933
+ function claimJob(job, claim, time, capacity) {
3632
3934
  const { now } = time;
3633
- if (job.state.state !== "queued" || job.state.nextAt > now || now >= job.expiresAt) return null;
3935
+ if (claimEligibility(job, now) !== "eligible") return null;
3634
3936
  const manual = hasRedrive(job);
3635
3937
  const ordinal = job.attempts.length + 1;
3636
3938
  if (ordinal > MAX_ATTEMPT_LEDGER || !manual && ordinal > job.retry.maxAttempts) invalid("store_incompatible");
3637
3939
  if (job.attempts.some(({ commit }) => commit.attemptId === claim.attemptId)) invalid("invalid_command");
3638
- const snapshot = readStoredSnapshot(job, job.snapshot);
3940
+ const snapshot = readStoredSnapshot(job, job.snapshot, capacity);
3639
3941
  const lease = Object.freeze({
3640
3942
  attemptId: claim.attemptId,
3641
3943
  token: claim.token,
@@ -3658,6 +3960,20 @@ function claimJob(job, claim, time) {
3658
3960
  Math.min(job.expiresAt, lease.leaseEndsAt)
3659
3961
  );
3660
3962
  }
3963
+ var EXPIRY_MAINTENANCE_REQUIRED = Object.freeze({ status: "expiry_maintenance_required" });
3964
+ function claimWindow(claim, candidateLimit, capacity) {
3965
+ let expired = 0;
3966
+ return {
3967
+ decide(job, time) {
3968
+ const decision = claimJob(job, claim, time, capacity);
3969
+ if (decision === null && claimEligibility(job, time.now) === "expired") expired++;
3970
+ return decision;
3971
+ },
3972
+ result(next) {
3973
+ return next.status !== "applied" && expired === candidateLimit ? EXPIRY_MAINTENANCE_REQUIRED : next;
3974
+ }
3975
+ };
3976
+ }
3661
3977
  function renewJob(job, fence, leaseMs, time) {
3662
3978
  const lease = liveLease(job, fence, "pre_handoff", time.now);
3663
3979
  if (!lease || time.now >= job.expiresAt) return null;
@@ -3695,7 +4011,7 @@ function markJob(job, fence, leaseMs, time) {
3695
4011
  Math.min(job.expiresAt, lease.leaseEndsAt, grant.leaseEndsAt)
3696
4012
  );
3697
4013
  }
3698
- function finalize(job, lease, operationId, result, time, owner) {
4014
+ function finalize(job, lease, operationId, result, time, owner, capacity) {
3699
4015
  const { now } = time;
3700
4016
  const { attemptId, startedAt, handedOffAt } = lease;
3701
4017
  const ordinal = job.attempts.length + 1;
@@ -3713,7 +4029,7 @@ function finalize(job, lease, operationId, result, time, owner) {
3713
4029
  });
3714
4030
  const entry = Object.freeze({
3715
4031
  commit,
3716
- digest: activityDigest(activityFor(job, commit)),
4032
+ digest: activityDigest(activityFor(job, commit, capacity), capacity),
3717
4033
  ...owner ? { ownerDigest: sha256(lease.token) } : {}
3718
4034
  });
3719
4035
  const state = effect.disposition === "retry_scheduled" ? Object.freeze({ state: "queued", nextAt: effect.retryAt }) : terminalState(effect.disposition, effect.reason, now);
@@ -3725,11 +4041,11 @@ function finalize(job, lease, operationId, result, time, owner) {
3725
4041
  job,
3726
4042
  time,
3727
4043
  { ...job, state, attempts: Object.freeze([...job.attempts, entry]) },
3728
- () => copyCommit(commit),
4044
+ () => copyCommit(commit, capacity),
3729
4045
  before
3730
4046
  );
3731
4047
  }
3732
- function finishJob(job, request, time) {
4048
+ function finishJob(job, request, time, capacity) {
3733
4049
  const { fence, phase, operationId, result } = request;
3734
4050
  if (job.jobId !== fence.jobId) return null;
3735
4051
  assertEvidencePhase(result.reason, phase, "owner", "invalid_command");
@@ -3740,28 +4056,28 @@ function finishJob(job, request, time) {
3740
4056
  if (previous.ownerDigest !== sha256(fence.token) || commit.operationId !== operationId || previousPhase !== phase) {
3741
4057
  return null;
3742
4058
  }
3743
- if (hashJson(result, 8192, "invalid_command") !== hashJson(commit.result, 8192, "store_incompatible")) {
4059
+ if (sha256(encodeEvidence(result, "invalid_command", capacity)) !== sha256(encodeEvidence(commit.result, "store_incompatible", capacity))) {
3744
4060
  invalid("invalid_command");
3745
4061
  }
3746
- return { value: copyCommit(commit) };
4062
+ return { value: copyCommit(commit, capacity) };
3747
4063
  }
3748
4064
  const lease = liveLease(job, fence, phase, time.now);
3749
4065
  if (!lease) return null;
3750
4066
  if (result.reason === "expired" && time.now < job.expiresAt) invalid("invalid_command");
3751
4067
  assertEvidenceContext(result, job.provider, fence.attemptId, job.recipientCounts, "invalid_command");
3752
- return finalize(job, lease, operationId, result, time, true);
4068
+ return finalize(job, lease, operationId, result, time, true, capacity);
3753
4069
  }
3754
- function recoverJob(job, operationId, time) {
4070
+ function recoverJob(job, operationId, time, capacity) {
3755
4071
  if (job.state.state !== "leased" || time.now < job.state.lease.leaseEndsAt) return null;
3756
4072
  const { lease } = job.state;
3757
4073
  const reason = lease.handedOffAt === void 0 ? "pre_handoff_lease_expired" : "post_handoff_lease_expired";
3758
- return finalize(job, lease, operationId, lifecycleEvidence(reason), time, false);
4074
+ return finalize(job, lease, operationId, lifecycleEvidence(reason), time, false, capacity);
3759
4075
  }
3760
4076
  function expireJob(job, time) {
3761
4077
  if (job.state.state !== "queued" || time.now < job.expiresAt) return null;
3762
4078
  return replace(job, time, { ...job, state: terminalState("expired", "expired", time.now) }, jobSummary);
3763
4079
  }
3764
- function cancelJob(job, operationId, time) {
4080
+ function cancelJob(job, operationId, time, capacity) {
3765
4081
  const { state } = job;
3766
4082
  if (state.state === "terminal") {
3767
4083
  return {
@@ -3775,7 +4091,7 @@ function cancelJob(job, operationId, time) {
3775
4091
  return { value: Object.freeze({ status: "too_late", job: jobSummary(job) }) };
3776
4092
  }
3777
4093
  if (state.state === "leased") {
3778
- const decision = finalize(job, state.lease, operationId, lifecycleEvidence("cancelled"), time, false);
4094
+ const decision = finalize(job, state.lease, operationId, lifecycleEvidence("cancelled"), time, false, capacity);
3779
4095
  const { write } = decision;
3780
4096
  if (write?.kind !== "replace") invalid("store_incompatible");
3781
4097
  return { write, value: Object.freeze({ status: "cancelled", job: jobSummary(write.next) }) };
@@ -3820,11 +4136,11 @@ function resolveJob(job, request, time) {
3820
4136
  function replaceAttempt(job, previous, entry) {
3821
4137
  return Object.freeze(job.attempts.map((attempt) => attempt === previous ? Object.freeze(entry) : attempt));
3822
4138
  }
3823
- function reserveJobProjection(job, time) {
4139
+ function reserveJobProjection(job, time, capacity) {
3824
4140
  const entry = pendingAttempt(job);
3825
4141
  if (job.retiring || !entry || entry.commit.finishedAt > time.now) return null;
3826
4142
  const permitUntil = timeAfter(time.now, PROJECTION_PERMIT_MS) ?? invalid("store_unavailable");
3827
- const activity = activityFor(job, entry.commit);
4143
+ const activity = activityFor(job, entry.commit, capacity);
3828
4144
  const permit = Object.freeze({ activity, digest: entry.digest, permitUntil });
3829
4145
  const attempts = replaceAttempt(job, entry, { ...entry, permitUntil });
3830
4146
  return replace(
@@ -3968,12 +4284,14 @@ var TIME_MISS = /* @__PURE__ */ Symbol("time_miss");
3968
4284
  var NONE = Object.freeze({ status: "none" });
3969
4285
  var CONTENDED = Object.freeze({ status: "contended" });
3970
4286
  function createMemoryEmailStore(options = {}) {
3971
- const config = readRecord(options, [], ["clock", "maxJobs"], "invalid_config");
4287
+ const config = readRecord(options, [], ["clock", "maxJobs", "smtpCapacity"], "invalid_config");
4288
+ const capacity = readCapacityOption(config);
3972
4289
  const clock = readClock(config.clock);
3973
- const maxJobs = boundedOption(config.maxJobs, MEMORY_MAX_JOBS);
4290
+ const maxJobs = boundedOption(config.maxJobs, capacity?.limits.memoryJobs ?? MEMORY_MAX_JOBS);
3974
4291
  const scopes = /* @__PURE__ */ new Map();
3975
4292
  let count = 0;
3976
4293
  let active = false;
4294
+ let ready = false;
3977
4295
  let lastWall = 0;
3978
4296
  function wallNow() {
3979
4297
  let now;
@@ -4014,8 +4332,13 @@ function createMemoryEmailStore(options = {}) {
4014
4332
  active = false;
4015
4333
  }
4016
4334
  }
4335
+ function primitive(call, worker, run) {
4336
+ const safe = readStoreCall(call, worker, capacity);
4337
+ if (capacity && !ready) invalid("store_unavailable");
4338
+ return atomic(safe, run);
4339
+ }
4017
4340
  function read(call, worker, get) {
4018
- return atomic(readStoreCall(call, worker), (check) => {
4341
+ return primitive(call, worker, (check) => {
4019
4342
  const value = get();
4020
4343
  check();
4021
4344
  return value;
@@ -4080,12 +4403,14 @@ function createMemoryEmailStore(options = {}) {
4080
4403
  return misses ? CONTENDED : NONE;
4081
4404
  }
4082
4405
  const store = {
4406
+ ...capacityTag(capacity),
4083
4407
  async ready(options2 = {}) {
4084
4408
  const call = readCallOptions(options2, CALL_BUDGETS.admissionOrAdmin);
4085
4409
  atomic({ ...call, timeoutMs: Math.min(call.timeoutMs, STORE_PRIMITIVE_TIMEOUT_MS) }, (check) => {
4086
4410
  wallNow();
4087
4411
  check();
4088
4412
  });
4413
+ ready = true;
4089
4414
  },
4090
4415
  async findAdmission(scope, key, call) {
4091
4416
  return read(call, false, () => {
@@ -4097,15 +4422,15 @@ function createMemoryEmailStore(options = {}) {
4097
4422
  });
4098
4423
  },
4099
4424
  async insert(scope, admission, call) {
4100
- return atomic(readStoreCall(call), (check) => {
4101
- const parsed = readStoreAdmission(scope, admission);
4425
+ return primitive(call, false, (check) => {
4426
+ const parsed = readStoreAdmission(scope, admission, capacity);
4102
4427
  const rows = scopes.get(parsed.command.scope);
4103
4428
  if (rows?.identities.has(parsed.admissionKey)) {
4104
4429
  check();
4105
4430
  return Object.freeze({ status: "duplicate" });
4106
4431
  }
4107
4432
  if (rows?.jobs.has(parsed.jobId)) invalid("invalid_command");
4108
- const row = newJob(parsed, { now: wallNow(), revision: randomUUID4() });
4433
+ const row = newJob(parsed, { now: wallNow(), revision: randomUUID4() }, capacity);
4109
4434
  if (count >= maxJobs) invalid("store_capacity");
4110
4435
  const result = Object.freeze({ status: "inserted", job: jobSummary(row) });
4111
4436
  check();
@@ -4132,62 +4457,66 @@ function createMemoryEmailStore(options = {}) {
4132
4457
  const job = readStoreId(jobId);
4133
4458
  const id = readStoreId(attemptId);
4134
4459
  const entry = rows?.jobs.get(job)?.attempts.find(({ commit }) => commit.attemptId === id);
4135
- return entry ? copyCommit(entry.commit) : null;
4460
+ return entry ? copyCommit(entry.commit, capacity) : null;
4136
4461
  });
4137
4462
  },
4138
4463
  async claimNext(scope, claim, call) {
4139
- return atomic(readStoreCall(call, true), (check) => {
4140
- const safe = readClaim(claim);
4141
- return next(scope, "nextAt", check, (row, time) => claimJob(row, safe, time));
4464
+ return primitive(call, true, (check) => {
4465
+ const window = claimWindow(readClaim(claim), MAX_STORE_CANDIDATES, capacity);
4466
+ return window.result(next(scope, "nextAt", check, window.decide));
4142
4467
  });
4143
4468
  },
4144
4469
  async renew(scope, fence, leaseMs, call) {
4145
- return atomic(readStoreCall(call, true), (check) => {
4470
+ return primitive(call, true, (check) => {
4146
4471
  const safe = readFence(fence);
4147
4472
  const duration = readLeaseMs(leaseMs);
4148
4473
  return point(scope, safe.jobId, check, (row, time) => renewJob(row, safe, duration, time));
4149
4474
  });
4150
4475
  },
4151
4476
  async markHandoff(scope, fence, leaseMs, call) {
4152
- return atomic(readStoreCall(call, true), (check) => {
4477
+ return primitive(call, true, (check) => {
4153
4478
  const safe = readFence(fence);
4154
4479
  const duration = readLeaseMs(leaseMs);
4155
4480
  return point(scope, safe.jobId, check, (row, time) => markJob(row, safe, duration, time));
4156
4481
  });
4157
4482
  },
4158
4483
  async finish(scope, finish, call) {
4159
- return atomic(readStoreCall(call, true), (check) => {
4160
- const safe = readFinish(finish);
4161
- return point(scope, safe.fence.jobId, check, (row, time) => finishJob(row, safe, time));
4484
+ return primitive(call, true, (check) => {
4485
+ const safe = readFinish(finish, capacity);
4486
+ return point(scope, safe.fence.jobId, check, (row, time) => finishJob(row, safe, time, capacity));
4162
4487
  });
4163
4488
  },
4164
4489
  async recoverNext(scope, call) {
4165
- return atomic(readStoreCall(call, true), (check) => {
4490
+ return primitive(call, true, (check) => {
4166
4491
  const operationId = randomUUID4();
4167
- return next(scope, "leaseEndsAt", check, (row, time) => recoverJob(row, operationId, time));
4492
+ return next(scope, "leaseEndsAt", check, (row, time) => recoverJob(row, operationId, time, capacity));
4168
4493
  });
4169
4494
  },
4170
4495
  async expireNext(scope, call) {
4171
- return atomic(readStoreCall(call, true), (check) => next(scope, "queuedExpiresAt", check, expireJob));
4496
+ return primitive(call, true, (check) => next(scope, "queuedExpiresAt", check, expireJob));
4172
4497
  },
4173
4498
  async cancel(scope, jobId, call) {
4174
- return atomic(readStoreCall(call), (check) => {
4499
+ return primitive(call, false, (check) => {
4175
4500
  const operationId = randomUUID4();
4176
- return point(scope, readStoreId(jobId), check, (row, time) => cancelJob(row, operationId, time)) ?? Object.freeze({ status: "not_found" });
4501
+ return point(scope, readStoreId(jobId), check, (row, time) => cancelJob(row, operationId, time, capacity)) ?? Object.freeze({ status: "not_found" });
4177
4502
  });
4178
4503
  },
4179
4504
  async resolveUnknown(scope, command, call) {
4180
- return atomic(readStoreCall(call), (check) => {
4505
+ return primitive(call, false, (check) => {
4181
4506
  const safe = readResolution(command);
4182
4507
  return point(scope, safe.jobId, check, (row, time) => resolveJob(row, safe, time)) ?? invalid("not_found");
4183
4508
  });
4184
4509
  },
4185
4510
  async reserveProjection(scope, call) {
4186
- return atomic(readStoreCall(call), (check) => next(scope, "pendingAt", check, reserveJobProjection));
4511
+ return primitive(
4512
+ call,
4513
+ false,
4514
+ (check) => next(scope, "pendingAt", check, (row, time) => reserveJobProjection(row, time, capacity))
4515
+ );
4187
4516
  },
4188
4517
  async putActivity(scope, permit, call) {
4189
- return atomic(readStoreCall(call), (check) => {
4190
- const safe = readProjectionPermit(scopeToken(scope), permit);
4518
+ return primitive(call, false, (check) => {
4519
+ const safe = readProjectionPermit(scopeToken(scope), permit, capacity);
4191
4520
  const rows = scopes.get(scope);
4192
4521
  const { jobId, attemptId } = safe.activity;
4193
4522
  const existing = rows?.byJob.get(jobId)?.byId.get(attemptId);
@@ -4203,8 +4532,8 @@ function createMemoryEmailStore(options = {}) {
4203
4532
  });
4204
4533
  },
4205
4534
  async ackProjection(scope, permit, call) {
4206
- return atomic(readStoreCall(call), (check) => {
4207
- const safe = readProjectionPermit(scopeToken(scope), permit);
4535
+ return primitive(call, false, (check) => {
4536
+ const safe = readProjectionPermit(scopeToken(scope), permit, capacity);
4208
4537
  const { jobId, attemptId } = safe.activity;
4209
4538
  const proof = scopes.get(scope)?.byJob.get(jobId)?.byId.get(attemptId);
4210
4539
  return point(scope, jobId, check, (row, time) => acknowledgeJobProjection(row, safe, proof, time)) ?? "stale";
@@ -4212,7 +4541,7 @@ function createMemoryEmailStore(options = {}) {
4212
4541
  },
4213
4542
  async listActivities(scope, query, call) {
4214
4543
  return read(call, false, () => {
4215
- const criteria = readActivityCriteria(scopeToken(scope), query);
4544
+ const criteria = readActivityCriteria(scopeToken(scope), query, capacity);
4216
4545
  const rows = scopes.get(scope);
4217
4546
  const { jobId, before, from, after, limit } = criteria;
4218
4547
  const index = (jobId === null ? rows?.activities : rows?.byJob.get(jobId)?.ordered) ?? [];
@@ -4226,7 +4555,7 @@ function createMemoryEmailStore(options = {}) {
4226
4555
  });
4227
4556
  },
4228
4557
  async cleanupNext(scope, call) {
4229
- return atomic(readStoreCall(call), (check) => next(scope, "nextCleanupAt", check, cleanupJob));
4558
+ return primitive(call, false, (check) => next(scope, "nextCleanupAt", check, cleanupJob));
4230
4559
  }
4231
4560
  };
4232
4561
  return Object.freeze(store);
@@ -4234,11 +4563,14 @@ function createMemoryEmailStore(options = {}) {
4234
4563
  export {
4235
4564
  EmailError,
4236
4565
  createEmailAdmin,
4566
+ createEmailBatchAdmission,
4237
4567
  createEmailBatchService,
4568
+ createEmailRelay,
4238
4569
  createEmailService,
4239
4570
  createEmailTemplateRegistry,
4240
4571
  createEmailWorker,
4241
4572
  createMemoryEmailStore,
4573
+ createSmtpAcceptanceResult,
4242
4574
  defineLocalTemplate,
4243
4575
  defineSendGridTemplate
4244
4576
  };