@bobfrankston/mailx-imap 0.1.58 → 0.1.59

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 (3) hide show
  1. package/index.d.ts +35 -0
  2. package/index.js +120 -0
  3. package/package.json +3 -3
package/index.d.ts CHANGED
@@ -491,6 +491,41 @@ export declare class ImapManager extends EventEmitter {
491
491
  startOutboxWorker(): void;
492
492
  /** Stop Outbox worker */
493
493
  stopOutboxWorker(): void;
494
+ /** Periodic Sent sweep — reconciles optimistic local-inserts against
495
+ * the server's actual Sent folder for every IMAP-path account.
496
+ *
497
+ * Why: when a message is sent on an IMAP account, the local DB row is
498
+ * inserted with a PREDICTED UID (the Sent folder's UIDNEXT at submit
499
+ * time). The submission server's drain-Outbox sieve appends the real
500
+ * Sent copy at whatever UID it chooses, which may differ if anything
501
+ * else raced — another client, a sieve append from a different rule,
502
+ * or just the sieve picking its own UID. Without reconciliation:
503
+ * - On the next set-diff sync the local row's UID doesn't match
504
+ * any server UID → set-diff deletes it → "Sent copy missing".
505
+ * - Or the server's actual Sent copy gets reconciled as a NEW row
506
+ * → duplicate in the user's Sent view.
507
+ *
508
+ * What this sweep does:
509
+ * For each IMAP-path account, walk local Sent rows newer than
510
+ * SENT_SWEEP_WINDOW_MS, query the server's Sent folder by
511
+ * Message-ID, and:
512
+ * - if found at the local-known UID → no-op
513
+ * - if found at a different UID → rebind the local row to the
514
+ * real UID
515
+ * - if not found and the .eml is on disk → IMAP APPEND it
516
+ * from disk (no SMTP)
517
+ * - if not found and no .eml → leave it (set-diff will GC; the
518
+ * user has bigger problems than the Sent reconcile)
519
+ *
520
+ * Gmail-path accounts: skipped. Gmail's Sent is API-driven, the
521
+ * optimistic-insert path doesn't apply (gmail-api writes the row
522
+ * directly post-send with the real Gmail message ID, no UID race). */
523
+ private sentSweepInterval;
524
+ private readonly SENT_SWEEP_WINDOW_MS;
525
+ private readonly SENT_SWEEP_INTERVAL_MS;
526
+ startSentSweep(): void;
527
+ stopSentSweep(): void;
528
+ private sweepSentOnce;
494
529
  private configWatchers;
495
530
  private cloudPollTimers;
496
531
  /** Watch the local config files for external changes. On change, emit
package/index.js CHANGED
@@ -4371,6 +4371,126 @@ export class ImapManager extends EventEmitter {
4371
4371
  this.outboxInterval = null;
4372
4372
  }
4373
4373
  }
4374
+ /** Periodic Sent sweep — reconciles optimistic local-inserts against
4375
+ * the server's actual Sent folder for every IMAP-path account.
4376
+ *
4377
+ * Why: when a message is sent on an IMAP account, the local DB row is
4378
+ * inserted with a PREDICTED UID (the Sent folder's UIDNEXT at submit
4379
+ * time). The submission server's drain-Outbox sieve appends the real
4380
+ * Sent copy at whatever UID it chooses, which may differ if anything
4381
+ * else raced — another client, a sieve append from a different rule,
4382
+ * or just the sieve picking its own UID. Without reconciliation:
4383
+ * - On the next set-diff sync the local row's UID doesn't match
4384
+ * any server UID → set-diff deletes it → "Sent copy missing".
4385
+ * - Or the server's actual Sent copy gets reconciled as a NEW row
4386
+ * → duplicate in the user's Sent view.
4387
+ *
4388
+ * What this sweep does:
4389
+ * For each IMAP-path account, walk local Sent rows newer than
4390
+ * SENT_SWEEP_WINDOW_MS, query the server's Sent folder by
4391
+ * Message-ID, and:
4392
+ * - if found at the local-known UID → no-op
4393
+ * - if found at a different UID → rebind the local row to the
4394
+ * real UID
4395
+ * - if not found and the .eml is on disk → IMAP APPEND it
4396
+ * from disk (no SMTP)
4397
+ * - if not found and no .eml → leave it (set-diff will GC; the
4398
+ * user has bigger problems than the Sent reconcile)
4399
+ *
4400
+ * Gmail-path accounts: skipped. Gmail's Sent is API-driven, the
4401
+ * optimistic-insert path doesn't apply (gmail-api writes the row
4402
+ * directly post-send with the real Gmail message ID, no UID race). */
4403
+ sentSweepInterval = null;
4404
+ SENT_SWEEP_WINDOW_MS = 60 * 60 * 1000; // 1 hour
4405
+ SENT_SWEEP_INTERVAL_MS = 5 * 60 * 1000; // 5 minutes
4406
+ startSentSweep() {
4407
+ if (this.sentSweepInterval)
4408
+ return;
4409
+ const tick = async () => {
4410
+ for (const [accountId] of this.configs) {
4411
+ if (this.isGmailAccount(accountId))
4412
+ continue;
4413
+ try {
4414
+ await this.sweepSentOnce(accountId);
4415
+ }
4416
+ catch (e) {
4417
+ console.error(` [sent-sweep] ${accountId}: ${e?.message || e}`);
4418
+ }
4419
+ }
4420
+ };
4421
+ // First tick deferred 30 s so it doesn't race the daemon's
4422
+ // boot-time sync. Subsequent ticks every SENT_SWEEP_INTERVAL_MS.
4423
+ setTimeout(() => { tick().catch(() => { }); }, 30_000);
4424
+ this.sentSweepInterval = setInterval(() => { tick().catch(() => { }); }, this.SENT_SWEEP_INTERVAL_MS);
4425
+ }
4426
+ stopSentSweep() {
4427
+ if (this.sentSweepInterval) {
4428
+ clearInterval(this.sentSweepInterval);
4429
+ this.sentSweepInterval = null;
4430
+ }
4431
+ }
4432
+ async sweepSentOnce(accountId) {
4433
+ const sent = this.findFolder(accountId, "sent");
4434
+ if (!sent)
4435
+ return;
4436
+ // Pull recent local rows. Restrict to (a) account+folder, (b) cached
4437
+ // in the last hour, (c) have a message_id (no MID = can't reconcile).
4438
+ const cutoff = Date.now() - this.SENT_SWEEP_WINDOW_MS;
4439
+ const rows = this.db.getRecentMessagesByCachedAt(accountId, sent.id, cutoff);
4440
+ if (rows.length === 0)
4441
+ return;
4442
+ let reconciled = 0;
4443
+ let appended = 0;
4444
+ await this.withConnection(accountId, async (client) => {
4445
+ for (const row of rows) {
4446
+ const msgId = row.message_id;
4447
+ if (!msgId)
4448
+ continue;
4449
+ let serverUids = [];
4450
+ try {
4451
+ serverUids = await client.searchByHeader(sent.path, "Message-ID", msgId);
4452
+ }
4453
+ catch (e) {
4454
+ console.error(` [sent-sweep] searchByHeader ${msgId}: ${e?.message || e}`);
4455
+ continue;
4456
+ }
4457
+ if (serverUids.length === 0) {
4458
+ // Not on server. Re-APPEND from .eml if we have one.
4459
+ if (!row.body_path)
4460
+ continue;
4461
+ try {
4462
+ const buf = await this.bodyStore.readByPath(row.body_path);
4463
+ if (!buf)
4464
+ continue;
4465
+ await client.appendMessage(sent.path, buf, ["\\Seen"]);
4466
+ appended++;
4467
+ console.log(` [sent-sweep] ${accountId}: re-APPENDed ${msgId} (was missing on server)`);
4468
+ }
4469
+ catch (e) {
4470
+ console.error(` [sent-sweep] APPEND ${msgId}: ${e?.message || e}`);
4471
+ }
4472
+ }
4473
+ else if (!serverUids.includes(row.uid)) {
4474
+ // Server has it under a different UID — rebind local row by
4475
+ // patching the messages.uid in place. Cheap, no upsert
4476
+ // shuffle, no audit-trail noise.
4477
+ const realUid = serverUids[serverUids.length - 1];
4478
+ try {
4479
+ this.db.updateMessageUid?.(accountId, sent.id, row.uid, realUid);
4480
+ reconciled++;
4481
+ console.log(` [sent-sweep] ${accountId}: rebound ${msgId} ${row.uid} → ${realUid}`);
4482
+ }
4483
+ catch (e) {
4484
+ console.error(` [sent-sweep] rebind ${msgId}: ${e?.message || e}`);
4485
+ }
4486
+ }
4487
+ }
4488
+ }, { slow: true, timeoutMs: 120_000 });
4489
+ if (reconciled + appended > 0) {
4490
+ this.emit("folderCountsChanged", accountId, {});
4491
+ console.log(` [sent-sweep] ${accountId}: ${reconciled} rebound, ${appended} re-appended`);
4492
+ }
4493
+ }
4374
4494
  // ── Config file watcher ──
4375
4495
  configWatchers = [];
4376
4496
  cloudPollTimers = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bobfrankston/mailx-imap",
3
- "version": "0.1.58",
3
+ "version": "0.1.59",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -11,7 +11,7 @@
11
11
  "dependencies": {
12
12
  "@bobfrankston/mailx-types": "^0.1.18",
13
13
  "@bobfrankston/mailx-settings": "^0.1.22",
14
- "@bobfrankston/mailx-store": "^0.1.34",
14
+ "@bobfrankston/mailx-store": "^0.1.35",
15
15
  "@bobfrankston/iflow-direct": "^0.1.50",
16
16
  "@bobfrankston/tcp-transport": "^0.1.6",
17
17
  "@bobfrankston/smtp-direct": "^0.1.8",
@@ -39,7 +39,7 @@
39
39
  "dependencies": {
40
40
  "@bobfrankston/mailx-types": "^0.1.18",
41
41
  "@bobfrankston/mailx-settings": "^0.1.22",
42
- "@bobfrankston/mailx-store": "^0.1.34",
42
+ "@bobfrankston/mailx-store": "^0.1.35",
43
43
  "@bobfrankston/iflow-direct": "^0.1.50",
44
44
  "@bobfrankston/tcp-transport": "^0.1.6",
45
45
  "@bobfrankston/smtp-direct": "^0.1.8",