--- /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 E85A96DE02CA\r
+ for <notmuch@notmuchmail.org>; Thu, 31 Mar 2016 10:44:47 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.021\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.021 tagged_above=-999 required=5\r
+ tests=[AWL=-0.021] 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 7qPI6LgS_KEz for <notmuch@notmuchmail.org>;\r
+ Thu, 31 Mar 2016 10:44:39 -0700 (PDT)\r
+X-Greylist: delayed 584 seconds by postgrey-1.35 at arlo;\r
+ Thu, 31 Mar 2016 10:44:39 PDT\r
+Received: from che.mayfirst.org (che.mayfirst.org [209.234.253.108])\r
+ by arlo.cworth.org (Postfix) with ESMTP id 4EABF6DE014A\r
+ for <notmuch@notmuchmail.org>; Thu, 31 Mar 2016 10:44:39 -0700 (PDT)\r
+Received: from fifthhorseman.net (unknown [38.109.115.130])\r
+ by che.mayfirst.org (Postfix) with ESMTPSA id B3AE4F997\r
+ for <notmuch@notmuchmail.org>; Thu, 31 Mar 2016 13:34:53 -0400 (EDT)\r
+Received: by fifthhorseman.net (Postfix, from userid 1000)\r
+ id 0F95F2003D; Thu, 31 Mar 2016 13:34:53 -0400 (EDT)\r
+From: Daniel Kahn Gillmor <dkg@fifthhorseman.net>\r
+To: Notmuch Mail <notmuch@notmuchmail.org>\r
+Subject: [PATCH] test thread breakage when messages are removed and re-added\r
+Date: Thu, 31 Mar 2016 13:34:53 -0400\r
+Message-Id: <1459445693-3900-1-git-send-email-dkg@fifthhorseman.net>\r
+X-Mailer: git-send-email 2.8.0.rc3\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: Thu, 31 Mar 2016 17:44:48 -0000\r
+\r
+This test (T590-thread-breakage.sh) currently fails!\r
+\r
+If you have a two-message thread where message "B" is in-reply-to "A",\r
+notmuch rightly sees this as a single thread.\r
+\r
+But if you:\r
+\r
+ * remove "A" from the message store\r
+ * run "notmuch new"\r
+ * add "A" back into the message store\r
+ * re-run "notmuch new"\r
+\r
+Then notmuch sees the messages as distinct threads.\r
+\r
+I think this happens because if you insert "B" initially (before\r
+anything is known about "A"), then a "ghost message" gets added to the\r
+database in reference to "A" that is in the same thread, which "A"\r
+takes over when it appears.\r
+\r
+But if "A" is subsequently removed, no ghost message is retained, so\r
+when "A" appears, it is treated as a new thread.\r
+\r
+I don't know how to easily fix this, but i see a few options:\r
+\r
+ghost-on-removal\r
+----------------\r
+\r
+We could unilaterally add a ghost upon message removal. This has a\r
+few disadvantages: the message index would leak information about what\r
+messages the user has ever been exposed to, and we also create a\r
+perpetually-growing dataset -- the ghosts can never be removed.\r
+\r
+ghost-on-removal-when-shared-thread-exists\r
+------------------------------------------\r
+\r
+We could add a ghost upon message removal iff there are other\r
+non-ghost messages with the same thread ID.\r
+\r
+We'd also need to remove all ghost messages that share a thread when\r
+the last non-ghost message in that thread is removed.\r
+\r
+This still has a bit of information leakage, though: the message index\r
+would reveal that i've seen a newer message in a thread, even if i had\r
+deleted it from my message store\r
+\r
+track-dependencies\r
+------------------\r
+\r
+rather than a simple "ghost-message" we could store all the (A,B)\r
+message-reference pairs internally, showing which messages A reference\r
+which other messages B.\r
+\r
+Then removal of message X would require deleting all message-reference\r
+pairs (X,B), and only deleting a ghost message if no (A,X) reference\r
+pair exists.\r
+\r
+This requires modifying the database by adding a new and fairly weird\r
+table that would need to be indexed by both columns. I don't know\r
+whether xapian has nice ways to do that.\r
+\r
+scan-dependencies\r
+-----------------\r
+\r
+Without modifying the database, we could do something less efficient.\r
+\r
+Upon removal of message X, we could scan the headers of all non-ghost\r
+messages that share a thread with X. If any of those messages refers\r
+to X, we would add a ghost message. If none of them do, then we would\r
+just drop X entirely from the table.\r
+---\r
+ test/T590-thread-breakage.sh | 63 ++++++++++++++++++++++++++++++++++++++++++++\r
+ 1 file changed, 63 insertions(+)\r
+ create mode 100755 test/T590-thread-breakage.sh\r
+\r
+diff --git a/test/T590-thread-breakage.sh b/test/T590-thread-breakage.sh\r
+new file mode 100755\r
+index 0000000..704f504\r
+--- /dev/null\r
++++ b/test/T590-thread-breakage.sh\r
+@@ -0,0 +1,63 @@\r
++#!/usr/bin/env bash\r
++#\r
++# Copyright (c) 2016 Daniel Kahn Gillmor\r
++#\r
++\r
++test_description='thread breakage by reindexing (currently broken)'\r
++\r
++. ./test-lib.sh || exit 1\r
++\r
++message_a() {\r
++ mkdir -p ${MAIL_DIR}/cur\r
++ cat > ${MAIL_DIR}/cur/a <<EOF\r
++Subject: First message\r
++Message-ID: <a@example.net>\r
++From: Alice <alice@example.net>\r
++To: Bob <bob@example.net>\r
++Date: Thu, 31 Mar 2016 20:10:00 -0400\r
++\r
++This is the first message in the thread.\r
++EOF\r
++}\r
++\r
++message_b() {\r
++ mkdir -p ${MAIL_DIR}/cur\r
++ cat > ${MAIL_DIR}/cur/b <<EOF\r
++Subject: Second message\r
++Message-ID: <b@example.net>\r
++In-Reply-To: <a@example.net>\r
++References: <a@example.net>\r
++From: Bob <bob@example.net>\r
++To: Alice <alice@example.net>\r
++Date: Thu, 31 Mar 2016 20:15:00 -0400\r
++\r
++This is the second message in the thread.\r
++EOF\r
++}\r
++\r
++\r
++test_thread_count() {\r
++ notmuch new >/dev/null\r
++ test_begin_subtest "${2:-Expecting $1 thread(s)}"\r
++ count=$(notmuch count --output=threads)\r
++ test_expect_equal "$count" "$1"\r
++}\r
++\r
++test_thread_count 0 'There should be no threads initially'\r
++\r
++message_a\r
++test_thread_count 1 'One message in: one thread'\r
++\r
++message_b\r
++test_thread_count 1 'Second message in the same thread: one thread'\r
++\r
++rm -f ${MAIL_DIR}/cur/a\r
++test_thread_count 1 'First message removed: still only one thread'\r
++\r
++message_a\r
++# this is known to fail (it shows 2 threads) because no "ghost\r
++# message" was created for message A when it was removed from the\r
++# index, despite message B still pointing to it.\r
++test_thread_count 1 'First message reappears: should return to the same thread'\r
++\r
++test_done\r
+-- \r
+2.8.0.rc3\r
+\r