agent-inbox 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/AGENTS.md +18 -0
  2. package/CLAUDE.md +92 -1
  3. package/README.md +73 -6
  4. package/dist/federation/connection-manager.d.ts +8 -0
  5. package/dist/federation/connection-manager.d.ts.map +1 -1
  6. package/dist/federation/connection-manager.js +12 -0
  7. package/dist/federation/connection-manager.js.map +1 -1
  8. package/dist/federation/delivery-queue.d.ts +11 -3
  9. package/dist/federation/delivery-queue.d.ts.map +1 -1
  10. package/dist/federation/delivery-queue.js +38 -8
  11. package/dist/federation/delivery-queue.js.map +1 -1
  12. package/dist/federation/queue-store.d.ts +42 -0
  13. package/dist/federation/queue-store.d.ts.map +1 -0
  14. package/dist/federation/queue-store.js +87 -0
  15. package/dist/federation/queue-store.js.map +1 -0
  16. package/dist/index.d.mts +2 -0
  17. package/dist/index.d.ts +17 -0
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +98 -0
  20. package/dist/index.js.map +1 -1
  21. package/dist/index.mjs +1 -0
  22. package/dist/index.mjs.map +1 -0
  23. package/dist/mail/address-book.d.ts +43 -0
  24. package/dist/mail/address-book.d.ts.map +1 -0
  25. package/dist/mail/address-book.js +95 -0
  26. package/dist/mail/address-book.js.map +1 -0
  27. package/dist/mail/attachment-store.d.ts +31 -0
  28. package/dist/mail/attachment-store.d.ts.map +1 -0
  29. package/dist/mail/attachment-store.js +74 -0
  30. package/dist/mail/attachment-store.js.map +1 -0
  31. package/dist/mail/email-mapper.d.ts +41 -0
  32. package/dist/mail/email-mapper.d.ts.map +1 -0
  33. package/dist/mail/email-mapper.js +216 -0
  34. package/dist/mail/email-mapper.js.map +1 -0
  35. package/dist/mail/fs-attachment-store.d.ts +38 -0
  36. package/dist/mail/fs-attachment-store.d.ts.map +1 -0
  37. package/dist/mail/fs-attachment-store.js +165 -0
  38. package/dist/mail/fs-attachment-store.js.map +1 -0
  39. package/dist/mail/mail-gateway.d.ts +114 -0
  40. package/dist/mail/mail-gateway.d.ts.map +1 -0
  41. package/dist/mail/mail-gateway.js +402 -0
  42. package/dist/mail/mail-gateway.js.map +1 -0
  43. package/dist/mail/provider-transport.d.ts +138 -0
  44. package/dist/mail/provider-transport.d.ts.map +1 -0
  45. package/dist/mail/provider-transport.js +434 -0
  46. package/dist/mail/provider-transport.js.map +1 -0
  47. package/dist/mail/rate-limiter.d.ts +20 -0
  48. package/dist/mail/rate-limiter.d.ts.map +1 -0
  49. package/dist/mail/rate-limiter.js +56 -0
  50. package/dist/mail/rate-limiter.js.map +1 -0
  51. package/dist/mail/smtp-transport.d.ts +141 -0
  52. package/dist/mail/smtp-transport.d.ts.map +1 -0
  53. package/dist/mail/smtp-transport.js +415 -0
  54. package/dist/mail/smtp-transport.js.map +1 -0
  55. package/dist/mail/types.d.ts +177 -0
  56. package/dist/mail/types.d.ts.map +1 -0
  57. package/dist/mail/types.js +11 -0
  58. package/dist/mail/types.js.map +1 -0
  59. package/dist/router/destination.d.ts +69 -0
  60. package/dist/router/destination.d.ts.map +1 -0
  61. package/dist/router/destination.js +106 -0
  62. package/dist/router/destination.js.map +1 -0
  63. package/dist/router/message-router.d.ts +15 -0
  64. package/dist/router/message-router.d.ts.map +1 -1
  65. package/dist/router/message-router.js +25 -3
  66. package/dist/router/message-router.js.map +1 -1
  67. package/dist/storage/interface.d.ts +9 -0
  68. package/dist/storage/interface.d.ts.map +1 -1
  69. package/dist/storage/memory.d.ts +4 -0
  70. package/dist/storage/memory.d.ts.map +1 -1
  71. package/dist/storage/memory.js +12 -0
  72. package/dist/storage/memory.js.map +1 -1
  73. package/dist/storage/sqlite.d.ts +6 -0
  74. package/dist/storage/sqlite.d.ts.map +1 -1
  75. package/dist/storage/sqlite.js +28 -0
  76. package/dist/storage/sqlite.js.map +1 -1
  77. package/dist/types.d.ts +79 -0
  78. package/dist/types.d.ts.map +1 -1
  79. package/docs/DESIGN.md +15 -0
  80. package/docs/MAIL-INTEROP-PLAN.md +660 -0
  81. package/package.json +28 -3
  82. package/renovate.json5 +6 -0
  83. package/rules/agent-inbox.md +1 -0
  84. package/src/federation/connection-manager.ts +12 -0
  85. package/src/federation/delivery-queue.ts +38 -8
  86. package/src/federation/queue-store.ts +124 -0
  87. package/src/index.ts +148 -0
  88. package/src/mail/address-book.ts +111 -0
  89. package/src/mail/attachment-store.ts +90 -0
  90. package/src/mail/email-mapper.ts +288 -0
  91. package/src/mail/fs-attachment-store.ts +163 -0
  92. package/src/mail/mail-gateway.ts +505 -0
  93. package/src/mail/provider-transport.ts +577 -0
  94. package/src/mail/rate-limiter.ts +51 -0
  95. package/src/mail/smtp-transport.ts +589 -0
  96. package/src/mail/types.ts +221 -0
  97. package/src/router/destination.ts +140 -0
  98. package/src/router/message-router.ts +41 -4
  99. package/src/storage/interface.ts +11 -0
  100. package/src/storage/memory.ts +15 -0
  101. package/src/storage/sqlite.ts +36 -0
  102. package/src/types.ts +73 -0
  103. package/test/federation/delivery-queue-sqlite.test.ts +158 -0
  104. package/test/load.test.ts +1 -1
  105. package/test/mail/address-book.test.ts +111 -0
  106. package/test/mail/attachment-store-contract.test.ts +92 -0
  107. package/test/mail/attachment-store.test.ts +69 -0
  108. package/test/mail/destination.test.ts +115 -0
  109. package/test/mail/dsn-parse.test.ts +239 -0
  110. package/test/mail/email-mapper.test.ts +341 -0
  111. package/test/mail/external-id.test.ts +43 -0
  112. package/test/mail/fs-attachment-store.test.ts +134 -0
  113. package/test/mail/full-flow-e2e.test.ts +200 -0
  114. package/test/mail/mail-gateway.test.ts +419 -0
  115. package/test/mail/mail-transport-contract.test.ts +134 -0
  116. package/test/mail/mock-mail.ts +161 -0
  117. package/test/mail/mock-postmark.ts +66 -0
  118. package/test/mail/provider-transport.test.ts +381 -0
  119. package/test/mail/rate-limiter.test.ts +48 -0
  120. package/test/mail/router-mail-integration.test.ts +138 -0
  121. package/test/mail/smtp-e2e.test.ts +98 -0
  122. package/test/mail/smtp-transport.test.ts +138 -0
@@ -0,0 +1,114 @@
1
+ /**
2
+ * MailGateway — the only mail-aware component the rest of the system touches.
3
+ *
4
+ * Ingress: transport.onReceive → policy (size / allow-list / DMARC) → dedup →
5
+ * bounce-detect → map → commit via router.routeMessage (single ingestion path).
6
+ * The transport ACKs only after this resolves (commit-before-ACK); a throw
7
+ * NACKs so the sender retries.
8
+ *
9
+ * Egress: router calls send() for mail-class recipients. The message is rendered
10
+ * and attempted; transient failures are queued durably and retried; permanent
11
+ * failures (and inbound DSNs) converge on one config-gated bounce handler.
12
+ *
13
+ * See docs/MAIL-INTEROP-PLAN.md §7, §8.
14
+ */
15
+ import { EventEmitter } from "node:events";
16
+ import type { Storage } from "../storage/interface.js";
17
+ import type { MessageRouter, MailEgress } from "../router/message-router.js";
18
+ import { DeliveryQueue } from "../federation/delivery-queue.js";
19
+ import type { QueueStore } from "../federation/queue-store.js";
20
+ import type { Message, MailIdentityConfig, DeliveryQueueConfig } from "../types.js";
21
+ import type { MailTransport, InboundMail } from "./types.js";
22
+ export interface MailGatewayConfig {
23
+ identity: MailIdentityConfig;
24
+ /** Domains we send to without erroring; combined with identity.localDomains. */
25
+ routableDomains?: string[];
26
+ /** Override the transport's maxMessageBytes for inbound rejection. */
27
+ maxMessageBytes?: number;
28
+ /** Inbound sender allow-list (domains). Empty/undefined = allow all. */
29
+ allowedSenderDomains?: string[];
30
+ /** Drop inbound mail whose DMARC verdict is "fail". */
31
+ rejectDmarcFail?: boolean;
32
+ /** Max attachments on an inbound message before it is rejected. */
33
+ maxAttachments?: number;
34
+ /**
35
+ * Inbound rate limiting (abuse control). Over-limit inbound is NACKed
36
+ * (transient) so legitimate senders back off and retry. Disabled if unset.
37
+ */
38
+ rateLimit?: {
39
+ /** Sliding window, ms (default 60000). */
40
+ windowMs?: number;
41
+ /** Max inbound per sender domain per window. */
42
+ perSenderDomain?: number;
43
+ /** Max inbound across all senders per window. */
44
+ global?: number;
45
+ };
46
+ /** Bounce behavior; both paths default on. */
47
+ bounce?: {
48
+ emitEvent?: boolean;
49
+ synthesizeInboxMessage?: boolean;
50
+ };
51
+ /** Domain used to mint outbound Message-IDs (defaults to primary local domain). */
52
+ messageIdDomain?: string;
53
+ /** Default inbox scope for inbound mail without plus-addressing. */
54
+ defaultScope?: string;
55
+ /** Delivery queue config for outbound retries. */
56
+ queue?: Partial<DeliveryQueueConfig>;
57
+ }
58
+ /** Thrown to NACK over-limit inbound so the sender backs off and retries. */
59
+ export declare class RateLimitedError extends Error {
60
+ readonly scope: string;
61
+ constructor(scope: string);
62
+ }
63
+ export declare class MailGateway implements MailEgress {
64
+ private transport;
65
+ private storage;
66
+ private router;
67
+ private events;
68
+ private config;
69
+ private addressBook;
70
+ private mailDomains;
71
+ private senderLimiter;
72
+ private globalLimiter;
73
+ readonly queue: DeliveryQueue;
74
+ private retryTimer?;
75
+ private retrying;
76
+ constructor(opts: {
77
+ transport: MailTransport;
78
+ storage: Storage;
79
+ router: MessageRouter;
80
+ events: EventEmitter;
81
+ config: MailGatewayConfig;
82
+ /** Durable queue store for outbound retries (sqlite). Memory if omitted. */
83
+ queueStore?: QueueStore;
84
+ });
85
+ /** Start the underlying transport and the retry/TTL timers. */
86
+ start(intervalMs?: number): Promise<void>;
87
+ stop(): Promise<void>;
88
+ /** True if this recipient's domain is one we route mail for. */
89
+ ownsRecipient(agentId: string): boolean;
90
+ /** Render and deliver the mail-class recipients of a message. */
91
+ send(message: Message): Promise<void>;
92
+ /**
93
+ * Handle one inbound message. Resolves on ACK; throws on NACK (transient
94
+ * failure the sender should retry). Policy rejections ACK-and-drop so the
95
+ * sender does not retry forever.
96
+ */
97
+ handleInbound(mail: InboundMail): Promise<void>;
98
+ /** Attempt delivery; classify the result and queue/bounce accordingly. */
99
+ private attemptDelivery;
100
+ /** Re-attempt all retryable queued mail. Call from a timer or deterministically. */
101
+ processRetries(): Promise<void>;
102
+ private markDelivered;
103
+ private handleInboundBounce;
104
+ private bounce;
105
+ /** Inject a bounce notification into the original sender's inbox. */
106
+ private synthesizeBounce;
107
+ private reject;
108
+ /** Throw RateLimitedError (→ NACK) when an inbound limiter is exceeded. */
109
+ private enforceRateLimits;
110
+ private senderAllowed;
111
+ /** Dedup key: RFC Message-ID when present, else a synthetic content hash. */
112
+ private externalIdFor;
113
+ }
114
+ //# sourceMappingURL=mail-gateway.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mail-gateway.d.ts","sourceRoot":"","sources":["../../src/mail/mail-gateway.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,KAAK,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAG7E,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAQ/D,OAAO,KAAK,EAAE,OAAO,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AACpF,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE7D,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,kBAAkB,CAAC;IAC7B,gFAAgF;IAChF,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC;IAC3B,sEAAsE;IACtE,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,wEAAwE;IACxE,oBAAoB,CAAC,EAAE,MAAM,EAAE,CAAC;IAChC,uDAAuD;IACvD,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,mEAAmE;IACnE,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB;;;OAGG;IACH,SAAS,CAAC,EAAE;QACV,0CAA0C;QAC1C,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,gDAAgD;QAChD,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,CAAC;IAQjD,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAS3B,gEAAgE;IAChE,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAMvC,iEAAiE;IAC3D,IAAI,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAU3C;;;;OAIG;IACG,aAAa,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAiFrD,0EAA0E;YAC5D,eAAe;IAgF7B,oFAAoF;IAC9E,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC;IAiBrC,OAAO,CAAC,aAAa;YAYP,mBAAmB;YAWnB,MAAM;IAgDpB,qEAAqE;YACvD,gBAAgB;IA4B9B,OAAO,CAAC,MAAM;IAQd,2EAA2E;IAC3E,OAAO,CAAC,iBAAiB;IAgBzB,OAAO,CAAC,aAAa;IAQrB,6EAA6E;IAC7E,OAAO,CAAC,aAAa;CAYtB"}
@@ -0,0 +1,402 @@
1
+ /**
2
+ * MailGateway — the only mail-aware component the rest of the system touches.
3
+ *
4
+ * Ingress: transport.onReceive → policy (size / allow-list / DMARC) → dedup →
5
+ * bounce-detect → map → commit via router.routeMessage (single ingestion path).
6
+ * The transport ACKs only after this resolves (commit-before-ACK); a throw
7
+ * NACKs so the sender retries.
8
+ *
9
+ * Egress: router calls send() for mail-class recipients. The message is rendered
10
+ * and attempted; transient failures are queued durably and retried; permanent
11
+ * failures (and inbound DSNs) converge on one config-gated bounce handler.
12
+ *
13
+ * See docs/MAIL-INTEROP-PLAN.md §7, §8.
14
+ */
15
+ import { createHash } from "node:crypto";
16
+ import { parseAddress } from "../federation/address.js";
17
+ import { domainMatches } from "../router/destination.js";
18
+ import { DeliveryQueue } from "../federation/delivery-queue.js";
19
+ import { AddressBook } from "./address-book.js";
20
+ import { RateLimiter } from "./rate-limiter.js";
21
+ import { inboundMailToMessage, messageToOutboundMail, UnknownRecipientError, } from "./email-mapper.js";
22
+ /** Single queue bucket for outbound mail retries. */
23
+ const MAIL_QUEUE = "mail";
24
+ /** Thrown to NACK over-limit inbound so the sender backs off and retries. */
25
+ export class RateLimitedError extends Error {
26
+ scope;
27
+ constructor(scope) {
28
+ super(`Inbound rate limit exceeded (${scope})`);
29
+ this.scope = scope;
30
+ this.name = "RateLimitedError";
31
+ }
32
+ }
33
+ export class MailGateway {
34
+ transport;
35
+ storage;
36
+ router;
37
+ events;
38
+ config;
39
+ addressBook;
40
+ mailDomains;
41
+ senderLimiter = null;
42
+ globalLimiter = null;
43
+ queue;
44
+ retryTimer;
45
+ retrying = false;
46
+ constructor(opts) {
47
+ this.transport = opts.transport;
48
+ this.storage = opts.storage;
49
+ this.router = opts.router;
50
+ this.events = opts.events;
51
+ this.config = opts.config;
52
+ this.addressBook = new AddressBook(opts.config.identity);
53
+ if (this.addressBook.conflicts.length > 0) {
54
+ this.events.emit("mail.config.conflict", {
55
+ kind: "address-mapping",
56
+ conflicts: this.addressBook.conflicts,
57
+ });
58
+ }
59
+ this.mailDomains = [
60
+ ...opts.config.identity.localDomains,
61
+ ...(opts.config.routableDomains ?? []),
62
+ ];
63
+ this.queue = new DeliveryQueue(opts.events, opts.config.queue, opts.queueStore ? { store: opts.queueStore } : undefined);
64
+ const rl = opts.config.rateLimit;
65
+ if (rl) {
66
+ const windowMs = rl.windowMs ?? 60_000;
67
+ if (rl.perSenderDomain && rl.perSenderDomain > 0) {
68
+ this.senderLimiter = new RateLimiter(windowMs, rl.perSenderDomain);
69
+ }
70
+ if (rl.global && rl.global > 0) {
71
+ this.globalLimiter = new RateLimiter(windowMs, rl.global);
72
+ }
73
+ }
74
+ this.transport.onReceive((mail) => this.handleInbound(mail));
75
+ }
76
+ /** Start the underlying transport and the retry/TTL timers. */
77
+ async start(intervalMs = 30_000) {
78
+ await this.transport.start();
79
+ this.queue.startTicking();
80
+ this.retryTimer = setInterval(() => {
81
+ void this.processRetries();
82
+ }, intervalMs);
83
+ }
84
+ async stop() {
85
+ if (this.retryTimer)
86
+ clearInterval(this.retryTimer);
87
+ this.retryTimer = undefined;
88
+ this.queue.stopTicking();
89
+ await this.transport.stop();
90
+ }
91
+ // -- MailEgress ----------------------------------------------------------
92
+ /** True if this recipient's domain is one we route mail for. */
93
+ ownsRecipient(agentId) {
94
+ if (agentId.indexOf("@") === -1)
95
+ return false;
96
+ const addr = parseAddress(agentId);
97
+ return addr.system ? domainMatches(addr.system, this.mailDomains) : false;
98
+ }
99
+ /** Render and deliver the mail-class recipients of a message. */
100
+ async send(message) {
101
+ const mailRecipients = message.recipients.filter((r) => this.ownsRecipient(r.agent_id));
102
+ if (mailRecipients.length === 0)
103
+ return;
104
+ await this.attemptDelivery({ ...message, recipients: mailRecipients });
105
+ }
106
+ // -- Ingress -------------------------------------------------------------
107
+ /**
108
+ * Handle one inbound message. Resolves on ACK; throws on NACK (transient
109
+ * failure the sender should retry). Policy rejections ACK-and-drop so the
110
+ * sender does not retry forever.
111
+ */
112
+ async handleInbound(mail) {
113
+ const cap = this.config.maxMessageBytes ?? this.transport.capabilities.maxMessageBytes;
114
+ if (mail.sizeBytes > cap) {
115
+ this.reject(mail, "size-exceeded");
116
+ return;
117
+ }
118
+ // Rate limiting (abuse control) — applies to all inbound including bounces.
119
+ // Over-limit NACKs (throws) so legitimate senders back off and retry.
120
+ this.enforceRateLimits(mail);
121
+ // Bounces/DSNs correlate to a prior send rather than routing as new mail.
122
+ if (mail.bounce) {
123
+ await this.handleInboundBounce(mail);
124
+ return;
125
+ }
126
+ if (!this.senderAllowed(mail)) {
127
+ this.reject(mail, "sender-not-allowed");
128
+ return;
129
+ }
130
+ if (this.config.rejectDmarcFail && mail.authResults?.dmarc === "fail") {
131
+ this.reject(mail, "dmarc-fail");
132
+ return;
133
+ }
134
+ const maxAtt = this.config.maxAttachments;
135
+ if (maxAtt !== undefined && (mail.attachments?.length ?? 0) > maxAtt) {
136
+ this.reject(mail, "too-many-attachments");
137
+ return;
138
+ }
139
+ const externalId = this.externalIdFor(mail);
140
+ if (this.storage.hasSeenExternalId(externalId)) {
141
+ this.events.emit("mail.duplicate", { externalId });
142
+ return; // already ingested — ACK without re-injecting
143
+ }
144
+ let mapped;
145
+ try {
146
+ mapped = inboundMailToMessage(mail, {
147
+ addressBook: this.addressBook,
148
+ defaultScope: this.config.defaultScope,
149
+ lookupInboxIdByRfcMessageId: (id) => this.storage.getMessageIdByExternalId(id),
150
+ });
151
+ }
152
+ catch (err) {
153
+ if (err instanceof UnknownRecipientError) {
154
+ this.reject(mail, "no-local-recipient");
155
+ return;
156
+ }
157
+ throw err; // unexpected — NACK
158
+ }
159
+ // Single ingestion path: routeMessage stores, marks delivery, fires events.
160
+ // When the reply resolves to a known parent, inherit the parent's thread_tag
161
+ // (keeps threads consistent even for clients that send In-Reply-To without a
162
+ // full References chain). Fall back to our derived tag if the parent is gone.
163
+ const parentThreadTag = mapped.in_reply_to
164
+ ? this.storage.getMessage(mapped.in_reply_to)?.thread_tag
165
+ : undefined;
166
+ const stored = await this.router.routeMessage({
167
+ from: mapped.sender_id,
168
+ to: mapped.recipients.map((r) => ({ agent_id: r.agent_id, kind: r.kind })),
169
+ payload: mapped.content,
170
+ scope: mapped.scope,
171
+ subject: mapped.subject,
172
+ threadTag: parentThreadTag ?? mapped.thread_tag,
173
+ inReplyTo: mapped.in_reply_to,
174
+ importance: mapped.importance,
175
+ metadata: mapped.metadata,
176
+ });
177
+ this.storage.recordExternalId(externalId, stored.id);
178
+ this.events.emit("mail.received", { messageId: stored.id, externalId });
179
+ }
180
+ // -- Egress delivery -----------------------------------------------------
181
+ /** Attempt delivery; classify the result and queue/bounce accordingly. */
182
+ async attemptDelivery(message, queuedId) {
183
+ const outbound = messageToOutboundMail(message, {
184
+ addressBook: this.addressBook,
185
+ messageIdDomain: this.config.messageIdDomain,
186
+ lookupRfcMessageId: (id) => this.storage.getMessage(id)?.metadata.rfcMessageId,
187
+ lookupReferences: (id) => this.storage.getMessage(id)?.metadata.rfcReferences,
188
+ });
189
+ let disposition;
190
+ let detail;
191
+ let code;
192
+ let remoteMessageId;
193
+ try {
194
+ const res = await this.transport.send(outbound);
195
+ disposition = res.disposition;
196
+ detail = res.detail;
197
+ code = res.code;
198
+ remoteMessageId = res.remoteMessageId;
199
+ }
200
+ catch (err) {
201
+ // Transport threw (e.g. not ready) — treat as transient, keep retrying.
202
+ disposition = "transient";
203
+ detail = err instanceof Error ? err.message : String(err);
204
+ }
205
+ if (disposition === "delivered") {
206
+ // Record our Message-ID so an inbound DSN or reply can correlate back.
207
+ this.storage.recordExternalId(outbound.headers.messageId, message.id);
208
+ // Also record the provider's id — some backends (Postmark) reference their
209
+ // own message id in bounce webhooks rather than the RFC Message-ID.
210
+ if (remoteMessageId) {
211
+ this.storage.recordExternalId(remoteMessageId, message.id);
212
+ }
213
+ this.markDelivered(message);
214
+ if (queuedId)
215
+ this.queue.removeEntry(MAIL_QUEUE, queuedId);
216
+ this.events.emit("mail.sent", { messageId: message.id, remoteMessageId });
217
+ return;
218
+ }
219
+ if (disposition === "transient") {
220
+ if (queuedId) {
221
+ // recordAttempt returns false once retryMaxAttempts is exhausted (the
222
+ // entry is then removed). Bounce instead of silently dropping it.
223
+ const kept = this.queue.recordAttempt(MAIL_QUEUE, queuedId);
224
+ if (!kept) {
225
+ await this.bounce({
226
+ message,
227
+ recipient: outbound.to.map((t) => t.address).join(", "),
228
+ status: code ? String(code) : undefined,
229
+ diagnostic: detail ?? "retries exhausted",
230
+ action: "failed",
231
+ });
232
+ return;
233
+ }
234
+ }
235
+ else {
236
+ this.queue.enqueue(MAIL_QUEUE, message);
237
+ }
238
+ this.events.emit("mail.queued", { messageId: message.id, detail });
239
+ return;
240
+ }
241
+ // permanent
242
+ if (queuedId)
243
+ this.queue.removeEntry(MAIL_QUEUE, queuedId);
244
+ await this.bounce({
245
+ message,
246
+ recipient: outbound.to.map((t) => t.address).join(", "),
247
+ status: code ? String(code) : undefined,
248
+ diagnostic: detail,
249
+ action: "failed",
250
+ });
251
+ }
252
+ /** Re-attempt all retryable queued mail. Call from a timer or deterministically. */
253
+ async processRetries() {
254
+ // Guard against overlapping ticks: a slow send must not be re-selected and
255
+ // re-sent by the next interval while it is still in flight.
256
+ if (this.retrying)
257
+ return;
258
+ this.retrying = true;
259
+ // Opportunistically reclaim idle rate-limiter keys (bounded memory).
260
+ this.senderLimiter?.prune();
261
+ this.globalLimiter?.prune();
262
+ try {
263
+ for (const entry of this.queue.getRetryable(MAIL_QUEUE)) {
264
+ await this.attemptDelivery(entry.message, entry.id);
265
+ }
266
+ }
267
+ finally {
268
+ this.retrying = false;
269
+ }
270
+ }
271
+ markDelivered(message) {
272
+ const stored = this.storage.getMessage(message.id);
273
+ if (!stored)
274
+ return;
275
+ const now = new Date().toISOString();
276
+ for (const r of stored.recipients) {
277
+ if (this.ownsRecipient(r.agent_id))
278
+ r.delivered_at = now;
279
+ }
280
+ this.storage.putMessage(stored);
281
+ }
282
+ // -- Bounce (shared by outbound permanent + inbound DSN) -----------------
283
+ async handleInboundBounce(mail) {
284
+ const b = mail.bounce;
285
+ await this.bounce({
286
+ originalMessageId: b.originalMessageId,
287
+ recipient: b.recipient,
288
+ status: b.status,
289
+ diagnostic: b.diagnostic,
290
+ action: b.action,
291
+ });
292
+ }
293
+ async bounce(opts) {
294
+ let original = opts.message;
295
+ if (!original && opts.originalMessageId) {
296
+ const inboxId = this.storage.getMessageIdByExternalId(opts.originalMessageId);
297
+ if (inboxId)
298
+ original = this.storage.getMessage(inboxId);
299
+ }
300
+ // A delayed DSN means the peer MTA is still retrying — surface, don't bounce.
301
+ if (opts.action === "delayed") {
302
+ this.events.emit("mail.queued", {
303
+ messageId: original?.id,
304
+ recipient: opts.recipient,
305
+ delayed: true,
306
+ });
307
+ return;
308
+ }
309
+ // A positive DSN ("delivered") is informational only.
310
+ if (opts.action === "delivered")
311
+ return;
312
+ // Dedup: a replayed/spoofed DSN echoing a known message id must not
313
+ // repeatedly emit events or synthesize inbox notices (flood protection).
314
+ const bounceKey = `bounce:${original?.id ?? opts.originalMessageId ?? "?"}:${opts.recipient}`;
315
+ if (this.storage.hasSeenExternalId(bounceKey))
316
+ return;
317
+ this.storage.recordExternalId(bounceKey, original?.id ?? bounceKey);
318
+ if (this.config.bounce?.emitEvent !== false) {
319
+ this.events.emit("mail.bounced", {
320
+ messageId: original?.id,
321
+ recipient: opts.recipient,
322
+ status: opts.status,
323
+ diagnostic: opts.diagnostic,
324
+ });
325
+ }
326
+ if (this.config.bounce?.synthesizeInboxMessage !== false && original) {
327
+ await this.synthesizeBounce(original, opts);
328
+ }
329
+ }
330
+ /** Inject a bounce notification into the original sender's inbox. */
331
+ async synthesizeBounce(original, opts) {
332
+ const parts = [`Delivery to ${opts.recipient} failed`];
333
+ if (opts.status)
334
+ parts.push(`(${opts.status})`);
335
+ if (opts.diagnostic)
336
+ parts.push(`- ${opts.diagnostic}`);
337
+ await this.router.routeMessage({
338
+ from: "mailer-daemon",
339
+ to: [{ agent_id: original.sender_id, kind: "to" }],
340
+ subject: `Delivery failed: ${original.subject ?? "(no subject)"}`,
341
+ payload: { type: "text", text: parts.join(" ") },
342
+ inReplyTo: original.id,
343
+ importance: "high",
344
+ scope: original.scope,
345
+ metadata: {
346
+ bounce: {
347
+ recipient: opts.recipient,
348
+ status: opts.status,
349
+ diagnostic: opts.diagnostic,
350
+ },
351
+ },
352
+ });
353
+ }
354
+ // -- Helpers -------------------------------------------------------------
355
+ reject(mail, reason) {
356
+ this.events.emit("mail.rejected", {
357
+ reason,
358
+ from: mail.from.address,
359
+ to: mail.to.map((t) => t.address),
360
+ });
361
+ }
362
+ /** Throw RateLimitedError (→ NACK) when an inbound limiter is exceeded. */
363
+ enforceRateLimits(mail) {
364
+ if (this.globalLimiter && !this.globalLimiter.tryAcquire("*")) {
365
+ this.events.emit("mail.rate_limited", { scope: "global" });
366
+ throw new RateLimitedError("global");
367
+ }
368
+ if (this.senderLimiter) {
369
+ const from = mail.from.address || mail.envelopeFrom || "unknown";
370
+ const at = from.lastIndexOf("@");
371
+ const domain = at === -1 ? from : from.slice(at + 1).toLowerCase();
372
+ if (!this.senderLimiter.tryAcquire(domain)) {
373
+ this.events.emit("mail.rate_limited", { scope: "sender", domain });
374
+ throw new RateLimitedError(`sender:${domain}`);
375
+ }
376
+ }
377
+ }
378
+ senderAllowed(mail) {
379
+ const allow = this.config.allowedSenderDomains;
380
+ if (!allow || allow.length === 0)
381
+ return true;
382
+ const at = mail.from.address.lastIndexOf("@");
383
+ if (at === -1)
384
+ return false;
385
+ return domainMatches(mail.from.address.slice(at + 1), allow);
386
+ }
387
+ /** Dedup key: RFC Message-ID when present, else a synthetic content hash. */
388
+ externalIdFor(mail) {
389
+ if (mail.headers.messageId)
390
+ return mail.headers.messageId;
391
+ const h = createHash("sha256");
392
+ h.update(mail.from.address);
393
+ h.update("\0");
394
+ h.update(mail.subject ?? "");
395
+ h.update("\0");
396
+ h.update(mail.receivedAt);
397
+ h.update("\0");
398
+ h.update((mail.text ?? mail.html ?? "").slice(0, 4096));
399
+ return "syn:" + h.digest("hex");
400
+ }
401
+ }
402
+ //# sourceMappingURL=mail-gateway.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mail-gateway.js","sourceRoot":"","sources":["../../src/mail/mail-gateway.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAGH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGzC,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAEhE,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EACL,oBAAoB,EACpB,qBAAqB,EACrB,qBAAqB,GACtB,MAAM,mBAAmB,CAAC;AAsC3B,qDAAqD;AACrD,MAAM,UAAU,GAAG,MAAM,CAAC;AAE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