@bobfrankston/mailx-store 0.1.41 → 0.1.42

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/db.d.ts +11 -3
  2. package/db.js +48 -6
  3. package/package.json +1 -1
package/db.d.ts CHANGED
@@ -334,15 +334,23 @@ export declare class MailxDB {
334
334
  findPendingSyncAction(accountId: string, action: string, uid: number, folderId: number, targetFolderId?: number): {
335
335
  id: number;
336
336
  } | null;
337
- updateBodyPath(accountId: string, uid: number, bodyPath: string): void;
337
+ /** folderId REQUIRED same UID-collision bug class as deleteMessage /
338
+ * updateMessageFlags. The same numeric UID exists in many folders for
339
+ * DIFFERENT messages; a (account, uid) UPDATE writes the body_path onto
340
+ * EVERY folder's uid=N row, so 22 unrelated messages ended up sharing one
341
+ * .eml and showed each other's bodies (Bob 2026-05-29 — subject/letter
342
+ * mismatch). Never write body_path without folder_id. */
343
+ updateBodyPath(accountId: string, folderId: number, uid: number, bodyPath: string): void;
338
344
  /** Write the post-download metadata in one go: body file path, parsed
339
345
  * attachment flag, preview text, and the parse timestamp. Called from
340
346
  * prefetch after the body has been written to disk and parsed — without
341
347
  * this, IMAP metadata-only sync leaves has_attachments=0 and preview=""
342
348
  * forever (the paperclip 📎 never renders for non-Gmail accounts).
343
349
  * Pass bodyPath = "" to update only the parsed fields (used by the
344
- * backfill worker re-parsing existing .eml files on disk). */
345
- updateBodyMeta(accountId: string, uid: number, bodyPath: string, hasAttachments: boolean, preview: string): void;
350
+ * backfill worker re-parsing existing .eml files on disk).
351
+ * folderId REQUIRED see updateBodyPath: the (account, uid) form
352
+ * cross-wired body_path across every folder sharing the numeric UID. */
353
+ updateBodyMeta(accountId: string, folderId: number, uid: number, bodyPath: string, hasAttachments: boolean, preview: string): void;
346
354
  /** Find rows whose body is on disk but was never run through extractPreview
347
355
  * (has_attachments/preview reflect "no body source at sync time", typical
348
356
  * of the IMAP metadata-only path). Used by the backfill worker to walk
package/db.js CHANGED
@@ -501,6 +501,40 @@ export class MailxDB {
501
501
  catch (e) {
502
502
  console.error(` [migration] synthetic-UID cleanup failed: ${e?.message || e}`);
503
503
  }
504
+ // Heal body_path cross-wiring caused by the pre-fix updateBodyMeta /
505
+ // updateBodyPath (which wrote body_path WHERE (account, uid) with no
506
+ // folder_id, so 22+ unrelated messages sharing a numeric UID across
507
+ // folders ended up pointing at ONE .eml and rendered each other's
508
+ // bodies — Bob 2026-05-29 "subject and summary unrelated"). Any
509
+ // body_path referenced by more than one row is, by construction,
510
+ // cross-wired (each (folder,uid) gets its own .eml via putMessage).
511
+ // Clear those rows' body_path + body_parsed_at so the prefetch /
512
+ // on-demand fetch re-downloads the CORRECT body per (folder, uid) with
513
+ // the now-folder-scoped writers. This is a cache re-download, not a
514
+ // data backfill — safe. KV-flagged so it runs once.
515
+ try {
516
+ const healedFlag = this.getKv("schema", "fix_crosswired_bodypath_v1");
517
+ if (!healedFlag) {
518
+ const r = this.db.prepare(`
519
+ UPDATE messages SET body_path = '', body_parsed_at = NULL
520
+ WHERE body_path IS NOT NULL AND body_path != ''
521
+ AND body_path IN (
522
+ SELECT body_path FROM messages
523
+ WHERE body_path IS NOT NULL AND body_path != ''
524
+ GROUP BY body_path HAVING COUNT(*) > 1
525
+ )
526
+ `).run();
527
+ const count = r.changes || 0;
528
+ if (count > 0) {
529
+ console.log(` [migration] cleared ${count} cross-wired body_path rows — will re-fetch correct bodies`);
530
+ this.audit({ kind: "migration", count, reason: "fix_crosswired_bodypath_v1 — clear body_path shared across multiple (folder,uid) rows", source: "MailxDB constructor" });
531
+ }
532
+ this.setKv("schema", "fix_crosswired_bodypath_v1", "1");
533
+ }
534
+ }
535
+ catch (e) {
536
+ console.error(` [migration] body_path cross-wire cleanup failed: ${e?.message || e}`);
537
+ }
504
538
  // One-shot: backfill message_folders from existing messages rows.
505
539
  // After this runs, every existing (account, folder, uid) row in
506
540
  // messages has a corresponding membership row in message_folders.
@@ -1913,8 +1947,14 @@ export class MailxDB {
1913
1947
  const r = this.db.prepare(sql).get(...params);
1914
1948
  return r || null;
1915
1949
  }
1916
- updateBodyPath(accountId, uid, bodyPath) {
1917
- this.db.prepare("UPDATE messages SET body_path = ? WHERE account_id = ? AND uid = ?").run(bodyPath, accountId, uid);
1950
+ /** folderId REQUIRED — same UID-collision bug class as deleteMessage /
1951
+ * updateMessageFlags. The same numeric UID exists in many folders for
1952
+ * DIFFERENT messages; a (account, uid) UPDATE writes the body_path onto
1953
+ * EVERY folder's uid=N row, so 22 unrelated messages ended up sharing one
1954
+ * .eml and showed each other's bodies (Bob 2026-05-29 — subject/letter
1955
+ * mismatch). Never write body_path without folder_id. */
1956
+ updateBodyPath(accountId, folderId, uid, bodyPath) {
1957
+ this.db.prepare("UPDATE messages SET body_path = ? WHERE account_id = ? AND folder_id = ? AND uid = ?").run(bodyPath, accountId, folderId, uid);
1918
1958
  }
1919
1959
  /** Write the post-download metadata in one go: body file path, parsed
1920
1960
  * attachment flag, preview text, and the parse timestamp. Called from
@@ -1922,13 +1962,15 @@ export class MailxDB {
1922
1962
  * this, IMAP metadata-only sync leaves has_attachments=0 and preview=""
1923
1963
  * forever (the paperclip 📎 never renders for non-Gmail accounts).
1924
1964
  * Pass bodyPath = "" to update only the parsed fields (used by the
1925
- * backfill worker re-parsing existing .eml files on disk). */
1926
- updateBodyMeta(accountId, uid, bodyPath, hasAttachments, preview) {
1965
+ * backfill worker re-parsing existing .eml files on disk).
1966
+ * folderId REQUIRED see updateBodyPath: the (account, uid) form
1967
+ * cross-wired body_path across every folder sharing the numeric UID. */
1968
+ updateBodyMeta(accountId, folderId, uid, bodyPath, hasAttachments, preview) {
1927
1969
  if (bodyPath) {
1928
- this.db.prepare("UPDATE messages SET body_path = ?, has_attachments = ?, preview = ?, body_parsed_at = ? WHERE account_id = ? AND uid = ?").run(bodyPath, hasAttachments ? 1 : 0, preview, Date.now(), accountId, uid);
1970
+ this.db.prepare("UPDATE messages SET body_path = ?, has_attachments = ?, preview = ?, body_parsed_at = ? WHERE account_id = ? AND folder_id = ? AND uid = ?").run(bodyPath, hasAttachments ? 1 : 0, preview, Date.now(), accountId, folderId, uid);
1929
1971
  }
1930
1972
  else {
1931
- this.db.prepare("UPDATE messages SET has_attachments = ?, preview = ?, body_parsed_at = ? WHERE account_id = ? AND uid = ?").run(hasAttachments ? 1 : 0, preview, Date.now(), accountId, uid);
1973
+ this.db.prepare("UPDATE messages SET has_attachments = ?, preview = ?, body_parsed_at = ? WHERE account_id = ? AND folder_id = ? AND uid = ?").run(hasAttachments ? 1 : 0, preview, Date.now(), accountId, folderId, uid);
1932
1974
  }
1933
1975
  }
1934
1976
  /** Find rows whose body is on disk but was never run through extractPreview
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bobfrankston/mailx-store",
3
- "version": "0.1.41",
3
+ "version": "0.1.42",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",