@bobfrankston/mailx-store 0.1.40 → 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 (4) hide show
  1. package/db.d.ts +20 -4
  2. package/db.js +63 -8
  3. package/package.json +1 -1
  4. package/store.js +1 -1
package/db.d.ts CHANGED
@@ -302,7 +302,15 @@ export declare class MailxDB {
302
302
  * Existing callers without folderId are tolerated (legacy) but should
303
303
  * be migrated. */
304
304
  getMessageBodyPath(accountId: string, uid: number, folderId?: number): string;
305
- updateMessageFlags(accountId: string, uid: number, flags: string[]): void;
305
+ /** Update flags for a message. folderId is REQUIRED — same UID-collision
306
+ * bug class as the deleteMessage one we fixed 2026-05-27. The
307
+ * WHERE-account-AND-uid query previously updated EVERY folder's row
308
+ * that shared the numeric UID, causing the "stars on letters I never
309
+ * starred" symptom (Bob 2026-05-27: flagging one INBOX message lit
310
+ * up stars on a same-numeric-UID row in Drafts/Sent/sub-folders).
311
+ * folderId === null is allowed as an explicit "every folder" override
312
+ * for code paths that mean it; everyone else must pass folderId. */
313
+ updateMessageFlags(accountId: string, folderId: number | null, uid: number, flags: string[]): void;
306
314
  updateMessageFolder(accountId: string, uid: number, targetFolderId: number): void;
307
315
  /** Local-first move of a message between folders. Updates BOTH the
308
316
  * legacy `messages.folder_id` column AND the `message_folders`
@@ -326,15 +334,23 @@ export declare class MailxDB {
326
334
  findPendingSyncAction(accountId: string, action: string, uid: number, folderId: number, targetFolderId?: number): {
327
335
  id: number;
328
336
  } | null;
329
- 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;
330
344
  /** Write the post-download metadata in one go: body file path, parsed
331
345
  * attachment flag, preview text, and the parse timestamp. Called from
332
346
  * prefetch after the body has been written to disk and parsed — without
333
347
  * this, IMAP metadata-only sync leaves has_attachments=0 and preview=""
334
348
  * forever (the paperclip 📎 never renders for non-Gmail accounts).
335
349
  * Pass bodyPath = "" to update only the parsed fields (used by the
336
- * backfill worker re-parsing existing .eml files on disk). */
337
- 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;
338
354
  /** Find rows whose body is on disk but was never run through extractPreview
339
355
  * (has_attachments/preview reflect "no body source at sync time", typical
340
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.
@@ -1815,8 +1849,21 @@ export class MailxDB {
1815
1849
  : this.db.prepare("SELECT body_path FROM messages WHERE account_id = ? AND uid = ?").get(accountId, uid);
1816
1850
  return r?.body_path || "";
1817
1851
  }
1818
- updateMessageFlags(accountId, uid, flags) {
1819
- this.db.prepare("UPDATE messages SET flags_json = ? WHERE account_id = ? AND uid = ?").run(JSON.stringify(flags), accountId, uid);
1852
+ /** Update flags for a message. folderId is REQUIRED — same UID-collision
1853
+ * bug class as the deleteMessage one we fixed 2026-05-27. The
1854
+ * WHERE-account-AND-uid query previously updated EVERY folder's row
1855
+ * that shared the numeric UID, causing the "stars on letters I never
1856
+ * starred" symptom (Bob 2026-05-27: flagging one INBOX message lit
1857
+ * up stars on a same-numeric-UID row in Drafts/Sent/sub-folders).
1858
+ * folderId === null is allowed as an explicit "every folder" override
1859
+ * for code paths that mean it; everyone else must pass folderId. */
1860
+ updateMessageFlags(accountId, folderId, uid, flags) {
1861
+ if (folderId != null) {
1862
+ this.db.prepare("UPDATE messages SET flags_json = ? WHERE account_id = ? AND folder_id = ? AND uid = ?").run(JSON.stringify(flags), accountId, folderId, uid);
1863
+ }
1864
+ else {
1865
+ this.db.prepare("UPDATE messages SET flags_json = ? WHERE account_id = ? AND uid = ?").run(JSON.stringify(flags), accountId, uid);
1866
+ }
1820
1867
  }
1821
1868
  updateMessageFolder(accountId, uid, targetFolderId) {
1822
1869
  // Idempotency: if a row already exists at (account, target_folder, uid)
@@ -1900,8 +1947,14 @@ export class MailxDB {
1900
1947
  const r = this.db.prepare(sql).get(...params);
1901
1948
  return r || null;
1902
1949
  }
1903
- updateBodyPath(accountId, uid, bodyPath) {
1904
- 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);
1905
1958
  }
1906
1959
  /** Write the post-download metadata in one go: body file path, parsed
1907
1960
  * attachment flag, preview text, and the parse timestamp. Called from
@@ -1909,13 +1962,15 @@ export class MailxDB {
1909
1962
  * this, IMAP metadata-only sync leaves has_attachments=0 and preview=""
1910
1963
  * forever (the paperclip 📎 never renders for non-Gmail accounts).
1911
1964
  * Pass bodyPath = "" to update only the parsed fields (used by the
1912
- * backfill worker re-parsing existing .eml files on disk). */
1913
- 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) {
1914
1969
  if (bodyPath) {
1915
- 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);
1916
1971
  }
1917
1972
  else {
1918
- 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);
1919
1974
  }
1920
1975
  }
1921
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.40",
3
+ "version": "0.1.42",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/store.js CHANGED
@@ -450,7 +450,7 @@ export class Store {
450
450
  * `folder:<id>` (auto fan-out)
451
451
  */
452
452
  updateFlags(accountId, uid, folderId, flags) {
453
- this.db.updateMessageFlags(accountId, uid, flags);
453
+ this.db.updateMessageFlags(accountId, folderId, uid, flags);
454
454
  const env = this.db.getMessageByUid(accountId, uid, folderId);
455
455
  const msgUuid = env?.uuid;
456
456
  if (msgUuid) {