--- /dev/null
+Return-Path: <dkg@fifthhorseman.net>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by arlo.cworth.org (Postfix) with ESMTP id 695456DE0360\r
+ for <notmuch@notmuchmail.org>; Sat, 2 Apr 2016 07:16:11 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.314\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.314 tagged_above=-999 required=5\r
+ tests=[AWL=-0.314] autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id mWQiHFtSQ_J5 for <notmuch@notmuchmail.org>;\r
+ Sat, 2 Apr 2016 07:16:03 -0700 (PDT)\r
+Received: from che.mayfirst.org (che.mayfirst.org [209.234.253.108])\r
+ by arlo.cworth.org (Postfix) with ESMTP id 25C5B6DE02D9\r
+ for <notmuch@notmuchmail.org>; Sat, 2 Apr 2016 07:15:48 -0700 (PDT)\r
+Received: from fifthhorseman.net (dhcp-a244.meeting.ietf.org [31.133.162.68])\r
+ by che.mayfirst.org (Postfix) with ESMTPSA id D20DCF99C\r
+ for <notmuch@notmuchmail.org>; Sat, 2 Apr 2016 10:15:46 -0400 (EDT)\r
+Received: by fifthhorseman.net (Postfix, from userid 1000)\r
+ id A9EBC20898; Sat, 2 Apr 2016 11:15:41 -0300 (BRT)\r
+From: Daniel Kahn Gillmor <dkg@fifthhorseman.net>\r
+To: Notmuch Mail <notmuch@notmuchmail.org>\r
+Subject: [PATCH v2 3/7] fix thread breakage via ghost-on-removal\r
+Date: Sat, 2 Apr 2016 11:15:37 -0300\r
+Message-Id: <1459606541-23889-3-git-send-email-dkg@fifthhorseman.net>\r
+X-Mailer: git-send-email 2.8.0.rc3\r
+In-Reply-To: <1459606541-23889-1-git-send-email-dkg@fifthhorseman.net>\r
+References: <1459445693-3900-1-git-send-email-dkg@fifthhorseman.net>\r
+ <1459606541-23889-1-git-send-email-dkg@fifthhorseman.net>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.20\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sat, 02 Apr 2016 14:16:11 -0000\r
+\r
+ghost-on-removal the solution to T590-thread-breakage.sh that just\r
+adds a ghost message after removing each message.\r
+\r
+It leaks information about whether we've ever seen a given message id,\r
+but it's a fairly simple implementation.\r
+\r
+Note that _resolve_message_id_to_thread_id also introduces new\r
+message_ids to the database, so i think just searching for a given\r
+message ID may introduce the same metadata leakage.\r
+\r
+This differs from v1 of this changeset in that we implement the change\r
+in _notmuch_message_delete, a more "internal" function.\r
+---\r
+ lib/database.cc | 2 +-\r
+ lib/message.cc | 29 ++++++++++++++++++++++++++---\r
+ 2 files changed, 27 insertions(+), 4 deletions(-)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index 3b342f1..d5733c9 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -2557,7 +2557,7 @@ notmuch_database_remove_message (notmuch_database_t *notmuch,\r
+ \r
+ if (status == NOTMUCH_STATUS_SUCCESS && message) {\r
+ status = _notmuch_message_remove_filename (message, filename);\r
+- if (status == NOTMUCH_STATUS_SUCCESS)\r
++ if (status == NOTMUCH_STATUS_SUCCESS) \r
+ _notmuch_message_delete (message);\r
+ else if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)\r
+ _notmuch_message_sync (message);\r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index 8d72ea2..e414e9c 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -1037,20 +1037,43 @@ _notmuch_message_sync (notmuch_message_t *message)\r
+ message->modified = FALSE;\r
+ }\r
+ \r
+-/* Delete a message document from the database. */\r
++/* Delete a message document from the database, leaving a ghost\r
++ * message in its place */\r
+ notmuch_status_t\r
+ _notmuch_message_delete (notmuch_message_t *message)\r
+ {\r
+ notmuch_status_t status;\r
+ Xapian::WritableDatabase *db;\r
++ const char *mid, *tid;\r
++ notmuch_message_t *ghost;\r
++ notmuch_private_status_t private_status;\r
++ notmuch_database_t *notmuch;\r
++ \r
++ mid = notmuch_message_get_message_id (message);\r
++ tid = notmuch_message_get_thread_id (message);\r
++ notmuch = message->notmuch;\r
+ \r
+ status = _notmuch_database_ensure_writable (message->notmuch);\r
+ if (status)\r
+ return status;\r
+ \r
+- db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);\r
++ db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);\r
+ db->delete_document (message->doc_id);\r
+- return NOTMUCH_STATUS_SUCCESS;\r
++ \r
++ /* and reintroduce a ghost in its place */\r
++ ghost = _notmuch_message_create_for_message_id (notmuch, mid, &private_status);\r
++ if (private_status == NOTMUCH_PRIVATE_STATUS_SUCCESS) {\r
++ /* this is deeply weird, and we should not have gotten into\r
++ this state. is there a better error message to return\r
++ here? */\r
++ return NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;\r
++ } else if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {\r
++ private_status = _notmuch_message_initialize_ghost (ghost, tid);\r
++ if (! private_status)\r
++ _notmuch_message_sync (ghost);\r
++ }\r
++ notmuch_message_destroy (ghost);\r
++ return COERCE_STATUS (private_status, "Error converting to ghost message");\r
+ }\r
+ \r
+ /* Transform a blank message into a ghost message. The caller must\r
+-- \r
+2.8.0.rc3\r
+\r