--- /dev/null
+Return-Path: <amdragon@mit.edu>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 13FF3429E42\r
+ for <notmuch@notmuchmail.org>; Thu, 24 Oct 2013 08:19:50 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id si1eabb+EMCc for <notmuch@notmuchmail.org>;\r
+ Thu, 24 Oct 2013 08:19:44 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-6.mit.edu (dmz-mailsec-scanner-6.mit.edu\r
+ [18.7.68.35])\r
+ by olra.theworths.org (Postfix) with ESMTP id EC773429E50\r
+ for <notmuch@notmuchmail.org>; Thu, 24 Oct 2013 08:19:26 -0700 (PDT)\r
+X-AuditID: 12074423-b7fc98e0000009a2-ab-52693a7eb51b\r
+Received: from mailhub-auth-3.mit.edu ( [18.9.21.43])\r
+ by dmz-mailsec-scanner-6.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id 3D.39.02466.E7A39625; Thu, 24 Oct 2013 11:19:26 -0400 (EDT)\r
+Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])\r
+ by mailhub-auth-3.mit.edu (8.13.8/8.9.2) with ESMTP id r9OFJF1u030433; \r
+ Thu, 24 Oct 2013 11:19:15 -0400\r
+Received: from drake.dyndns.org\r
+ (216-15-114-40.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com\r
+ [216.15.114.40]) (authenticated bits=0)\r
+ (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+ by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id r9OFJCXv012921\r
+ (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+ Thu, 24 Oct 2013 11:19:14 -0400\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
+ (envelope-from <amdragon@mit.edu>)\r
+ id 1VZMgq-0006e2-RH; Thu, 24 Oct 2013 11:19:12 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 08/11] search: Add stable queries to thread search results\r
+Date: Thu, 24 Oct 2013 11:19:08 -0400\r
+Message-Id: <1382627951-25252-9-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.8.4.rc3\r
+In-Reply-To: <1382627951-25252-1-git-send-email-amdragon@mit.edu>\r
+References: <1382627951-25252-1-git-send-email-amdragon@mit.edu>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=UTF-8\r
+Content-Transfer-Encoding: 8bit\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFuphleLIzCtJLcpLzFFi42IR4hTV1q2zygwyeDhJwqJpurPF6rk8Ftdv\r
+ zmR2YPbYOesuu8et+6/ZPZ6tusUcwBzFZZOSmpNZllqkb5fAldGzag9TwWfXiv6HW1gaGH+b\r
+ dzFyckgImEgcazzHDmGLSVy4t56ti5GLQ0hgH6PEwoMtrBDORkaJ7/PeMEI4d5gkjr39BlU2\r
+ l1Hi7LKjbCD9bAIaEtv2L2cEsUUEpCV23p3NCmIzC0RLHLk8A6xGWMBX4vDRc8wgNouAqsSt\r
+ s59ZQGxeAQeJ659eMULcoSSx8NQ2sF5OAUeJn6+vgcWFgGp+/T4DVS8ocXLmEyCbA2i+usT6\r
+ eUIQq+QlmrfOZp7AKDQLSdUshKpZSKoWMDKvYpRNya3SzU3MzClOTdYtTk7My0st0jXTy80s\r
+ 0UtNKd3ECAp0dhflHYx/DiodYhTgYFTi4dX4kB4kxJpYVlyZe4hRkoNJSZT3nmlmkBBfUn5K\r
+ ZUZicUZ8UWlOavEhRgkOZiUR3ml6QDnelMTKqtSifJiUNAeLkjjvLQ77ICGB9MSS1OzU1ILU\r
+ IpisDAeHkgTvP0ugRsGi1PTUirTMnBKENBMHJ8hwHqDh90BqeIsLEnOLM9Mh8qcYFaXEeXmB\r
+ qURIACSRUZoH1wtLRK8YxYFeEeb9ANLOA0xicN2vgAYzAQ2esiQNZHBJIkJKqoHxMuv2BSLZ\r
+ kZ/nzZ1/SW3Bg01Hwg4nO+bMDdW/V8+04UBxMpP14xcMj/IEl52uVDi9+eSRKH5uy6r27LqG\r
+ 18UXk+9lKBkqFF+Jc3We9GU2z3I5NsYtiyeot79rtIy4Nm/yy3cPT0z4XfjRervBBxPzjV8D\r
+ pOPU/5k8YbyRrBXNHGBbor2RrXKyEktxRqKhFnNRcSIARMMbFx8DAAA=\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://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: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Thu, 24 Oct 2013 15:19:50 -0000\r
+\r
+These queries will match exactly the set of messages currently in the\r
+thread, even if more messages later arrive. Two queries are provided:\r
+one for matched messages and one for unmatched messages.\r
+\r
+This can be used to fix race conditions with tagging threads from\r
+search results. While tagging based on a thread: query can affect\r
+messages that arrived after the search, tagging based on stable\r
+queries affects only the messages the user was shown in the search UI.\r
+\r
+Since we want clients to be able to depend on the presence of these\r
+queries, this ushers in schema version 2.\r
+---\r
+ devel/schemata | 22 ++++++++++++++++++--\r
+ notmuch-client.h | 2 +-\r
+ notmuch-search.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ test/json | 2 ++\r
+ test/missing-headers | 6 ++++--\r
+ test/sexp | 4 ++--\r
+ 6 files changed, 88 insertions(+), 7 deletions(-)\r
+\r
+diff --git a/devel/schemata b/devel/schemata\r
+index cdd0e43..41dc4a6 100644\r
+--- a/devel/schemata\r
++++ b/devel/schemata\r
+@@ -14,7 +14,17 @@ are interleaved. Keys are printed as keywords (symbols preceded by a\r
+ colon), e.g. (:id "123" :time 54321 :from "foobar"). Null is printed as\r
+ nil, true as t and false as nil.\r
+ \r
+-This is version 1 of the structured output format.\r
++This is version 2 of the structured output format.\r
++\r
++Version history\r
++---------------\r
++\r
++v1\r
++- First versioned schema release.\r
++- Added part.content-length and part.content-transfer-encoding fields.\r
++\r
++v2\r
++- Added the thread_summary.query field.\r
+ \r
+ Common non-terminals\r
+ --------------------\r
+@@ -145,7 +155,15 @@ thread_summary = {\r
+ authors: string, # comma-separated names with | between\r
+ # matched and unmatched\r
+ subject: string,\r
+- tags: [string*]\r
++ tags: [string*],\r
++\r
++ # Two stable query strings identifying exactly the matched and\r
++ # unmatched messages currently in this thread. The messages\r
++ # matched by these queries will not change even if more messages\r
++ # arrive in the thread. If there are no matched or unmatched\r
++ # messages, the corresponding query will be null (there is no\r
++ # query that matches nothing). (Added in schema version 2.)\r
++ query: [string|null, string|null],\r
+ }\r
+ \r
+ notmuch reply schema\r
+diff --git a/notmuch-client.h b/notmuch-client.h\r
+index 4ecb3ae..278b498 100644\r
+--- a/notmuch-client.h\r
++++ b/notmuch-client.h\r
+@@ -138,7 +138,7 @@ chomp_newline (char *str)\r
+ * this. New (required) map fields can be added without increasing\r
+ * this.\r
+ */\r
+-#define NOTMUCH_FORMAT_CUR 1\r
++#define NOTMUCH_FORMAT_CUR 2\r
+ /* The minimum supported structured output format version. Requests\r
+ * for format versions below this will return an error. */\r
+ #define NOTMUCH_FORMAT_MIN 1\r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index d9d39ec..7c973b3 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -20,6 +20,7 @@\r
+ \r
+ #include "notmuch-client.h"\r
+ #include "sprinter.h"\r
++#include "string-util.h"\r
+ \r
+ typedef enum {\r
+ OUTPUT_SUMMARY,\r
+@@ -46,6 +47,45 @@ sanitize_string (const void *ctx, const char *str)\r
+ return out;\r
+ }\r
+ \r
++/* Return two stable query strings that identify exactly the matched\r
++ * and unmatched messages currently in thread. If there are no\r
++ * matched or unmatched messages, the returned buffers will be\r
++ * NULL. */\r
++static int\r
++get_thread_query (notmuch_thread_t *thread,\r
++ char **matched_out, char **unmatched_out)\r
++{\r
++ notmuch_messages_t *messages;\r
++ char *escaped = NULL;\r
++ size_t escaped_len = 0;\r
++\r
++ *matched_out = *unmatched_out = NULL;\r
++\r
++ for (messages = notmuch_thread_get_messages (thread);\r
++ notmuch_messages_valid (messages);\r
++ notmuch_messages_move_to_next (messages))\r
++ {\r
++ notmuch_message_t *message = notmuch_messages_get (messages);\r
++ const char *mid = notmuch_message_get_message_id (message);\r
++ /* Determine which query buffer to extend */\r
++ char **buf = notmuch_message_get_flag (\r
++ message, NOTMUCH_MESSAGE_FLAG_MATCH) ? matched_out : unmatched_out;\r
++ /* Add this message's id: query. Since "id" is an exclusive\r
++ * prefix, it is implicitly 'or'd together, so we only need to\r
++ * join queries with a space. */\r
++ if (make_boolean_term (thread, "id", mid, &escaped, &escaped_len) < 0)\r
++ return -1;\r
++ if (*buf)\r
++ *buf = talloc_asprintf_append_buffer (*buf, " %s", escaped);\r
++ else\r
++ *buf = talloc_strdup (thread, escaped);\r
++ if (!*buf)\r
++ return -1;\r
++ }\r
++ talloc_free (escaped);\r
++ return 0;\r
++}\r
++\r
+ static int\r
+ do_search_threads (sprinter_t *format,\r
+ notmuch_query_t *query,\r
+@@ -131,6 +171,25 @@ do_search_threads (sprinter_t *format,\r
+ format->string (format, authors);\r
+ format->map_key (format, "subject");\r
+ format->string (format, subject);\r
++ if (notmuch_format_version >= 2) {\r
++ char *matched_query, *unmatched_query;\r
++ if (get_thread_query (thread, &matched_query,\r
++ &unmatched_query) < 0) {\r
++ fprintf (stderr, "Out of memory\n");\r
++ return 1;\r
++ }\r
++ format->map_key (format, "query");\r
++ format->begin_list (format);\r
++ if (matched_query)\r
++ format->string (format, matched_query);\r
++ else\r
++ format->null (format);\r
++ if (unmatched_query)\r
++ format->string (format, unmatched_query);\r
++ else\r
++ format->null (format);\r
++ format->end (format);\r
++ }\r
+ }\r
+ \r
+ talloc_free (ctx_quote);\r
+diff --git a/test/json b/test/json\r
+index b87b7f6..e07a290 100755\r
+--- a/test/json\r
++++ b/test/json\r
+@@ -26,6 +26,7 @@ test_expect_equal_json "$output" "[{\"thread\": \"XXX\",\r
+ \"total\": 1,\r
+ \"authors\": \"Notmuch Test Suite\",\r
+ \"subject\": \"json-search-subject\",\r
++ \"query\": [\"id:$gen_msg_id\", null],\r
+ \"tags\": [\"inbox\",\r
+ \"unread\"]}]"\r
+ \r
+@@ -59,6 +60,7 @@ test_expect_equal_json "$output" "[{\"thread\": \"XXX\",\r
+ \"total\": 1,\r
+ \"authors\": \"Notmuch Test Suite\",\r
+ \"subject\": \"json-search-utf8-body-sübjéct\",\r
++ \"query\": [\"id:$gen_msg_id\", null],\r
+ \"tags\": [\"inbox\",\r
+ \"unread\"]}]"\r
+ \r
+diff --git a/test/missing-headers b/test/missing-headers\r
+index f14b878..43e861b 100755\r
+--- a/test/missing-headers\r
++++ b/test/missing-headers\r
+@@ -43,7 +43,8 @@ test_expect_equal_json "$output" '\r
+ ],\r
+ "thread": "XXX",\r
+ "timestamp": 978709437,\r
+- "total": 1\r
++ "total": 1,\r
++ "query": ["id:notmuch-sha1-7a6e4eac383ef958fcd3ebf2143db71b8ff01161", null]\r
+ },\r
+ {\r
+ "authors": "Notmuch Test Suite",\r
+@@ -56,7 +57,8 @@ test_expect_equal_json "$output" '\r
+ ],\r
+ "thread": "XXX",\r
+ "timestamp": 0,\r
+- "total": 1\r
++ "total": 1,\r
++ "query": ["id:notmuch-sha1-ca55943aff7a72baf2ab21fa74fab3d632401334", null]\r
+ }\r
+ ]'\r
+ \r
+diff --git a/test/sexp b/test/sexp\r
+index 492a82f..be815e1 100755\r
+--- a/test/sexp\r
++++ b/test/sexp\r
+@@ -19,7 +19,7 @@ test_expect_equal "$output" "((((:id \"${gen_msg_id}\" :match t :excluded nil :f\r
+ test_begin_subtest "Search message: sexp"\r
+ add_message "[subject]=\"sexp-search-subject\"" "[date]=\"Sat, 01 Jan 2000 12:00:00 -0000\"" "[body]=\"sexp-search-message\""\r
+ output=$(notmuch search --format=sexp "sexp-search-message" | notmuch_search_sanitize)\r
+-test_expect_equal "$output" "((:thread \"0000000000000002\" :timestamp 946728000 :date_relative \"2000-01-01\" :matched 1 :total 1 :authors \"Notmuch Test Suite\" :subject \"sexp-search-subject\" :tags (\"inbox\" \"unread\")))"\r
++test_expect_equal "$output" "((:thread \"0000000000000002\" :timestamp 946728000 :date_relative \"2000-01-01\" :matched 1 :total 1 :authors \"Notmuch Test Suite\" :subject \"sexp-search-subject\" :query (\"id:$gen_msg_id\" nil) :tags (\"inbox\" \"unread\")))"\r
+ \r
+ test_begin_subtest "Show message: sexp, utf-8"\r
+ add_message "[subject]=\"sexp-show-utf8-body-sübjéct\"" "[date]=\"Sat, 01 Jan 2000 12:00:00 -0000\"" "[body]=\"jsön-show-méssage\""\r
+@@ -44,7 +44,7 @@ test_expect_equal "$output" "((((:id \"$id\" :match t :excluded nil :filename \"\r
+ test_begin_subtest "Search message: sexp, utf-8"\r
+ add_message "[subject]=\"sexp-search-utf8-body-sübjéct\"" "[date]=\"Sat, 01 Jan 2000 12:00:00 -0000\"" "[body]=\"jsön-search-méssage\""\r
+ output=$(notmuch search --format=sexp "jsön-search-méssage" | notmuch_search_sanitize)\r
+-test_expect_equal "$output" "((:thread \"0000000000000005\" :timestamp 946728000 :date_relative \"2000-01-01\" :matched 1 :total 1 :authors \"Notmuch Test Suite\" :subject \"sexp-search-utf8-body-sübjéct\" :tags (\"inbox\" \"unread\")))"\r
++test_expect_equal "$output" "((:thread \"0000000000000005\" :timestamp 946728000 :date_relative \"2000-01-01\" :matched 1 :total 1 :authors \"Notmuch Test Suite\" :subject \"sexp-search-utf8-body-sübjéct\" :query (\"id:$gen_msg_id\" nil) :tags (\"inbox\" \"unread\")))"\r
+ \r
+ \r
+ test_done\r
+-- \r
+1.8.4.rc3\r
+\r