Email/query: NOT of an AND holding an OR and a NOT returns nothing when no message matches the inner filter

Issue Description

An Email/query whose filter is NOT of an AND returns no ids when the AND holds an OR, a NOT and at least one other condition, and no message matches that AND. Every message should match, since none matches the inner filter. When at least one message matches the inner filter, the result is correct.

Writing the same filter by hand with De Morgan’s laws, as OR(NOT(subject: qqa), NOT(OR(subject: qqb, subject: qqc)), subject: qqd), returns the correct result. The same happens with a from condition in place of the first subject one.

Impact: webmail clients that build an “everything not in these tabs” view as NOT(tab filters) show an empty folder whenever the folder holds no messages for those tabs.

Expected Behavior

The query returns every message in the account, since no message matches the inner AND (RFC 8621 §4.4.1, FilterOperator NOT: none of the conditions match).

Actual Behavior

The query returns no ids.

Shapes tested on an account holding one message with subject “Hello there” (every word matches nothing):

Filter Result Expected
NOT(AND(subject: qqa, OR(subject: qqb, subject: qqc), NOT(subject: qqd))) 0 1
NOT(AND(subject: qqa, AND(OR(subject: qqb, subject: qqc), NOT(subject: qqd)))) 0 1
NOT(AND(subject: qqa, OR(subject: qqb, subject: qqc))) 1 1
NOT(AND(subject: qqa, subject: qqb, NOT(subject: qqd))) 1 1
NOT(AND(OR(subject: qqb, subject: qqc), NOT(subject: qqd))) 1 1

After adding a second message with subject “qqa qqb”, which matches the inner AND, the first query correctly returns 1 of the 2 messages.

Reproduction Steps

  1. Use an account holding one message, with subject “Hello there”.
  2. Run this query:
["Email/query", {
  "accountId": "<id>",
  "filter": {"operator": "NOT", "conditions": [
    {"operator": "AND", "conditions": [
      {"subject": "qqa"},
      {"operator": "OR", "conditions": [{"subject": "qqb"}, {"subject": "qqc"}]},
      {"operator": "NOT", "conditions": [{"subject": "qqd"}]}
    ]}
  ]}
}, "0"]
  1. It returns "ids": [] instead of the one message.

Relevant Log Output

No errors are logged; the query succeeds with an empty result.

Stalwart Version

v0.16.x

Installation Method

Docker

Database Backend

RocksDB

Blob Storage

RocksDB

Search Engine

Internal

Directory Backend

OIDC

Additional Context

Seen on 0.16.26 (Docker image stalwartlabs/stalwart:v0.16). Reproduced on a fresh test account; it also shows on a production server with real mail.

I have reviewed the documentation and FAQ and confirm that my issue is NOT addressed there.

on

I have searched this support forum (open and closed topics) and confirm this is not a duplicate.

on

I understand that topics in this category are triaged by a bot first but a human reply will follow up. If I’d prefer a human-only reply, I’ll add the no-ai tag to my topic.

on

This appears to be a genuine bug in the query engine’s “AND short-circuit” optimization, not an API or documentation issue. I traced it through crates/store/src/search/query.rs.

When a filter list is flattened to a stack machine (push a scope on AND/OR/NOT, pop on the matching End marker), there’s a short-circuit for AND: as soon as an AND’s running bitmap becomes empty, the code skips ahead through the remaining tokens in that AND by scanning forward until it sees the next End token:

if matches!(state.op, SearchFilter::And) && state.bm.as_ref().unwrap().is_empty() {
    while let Some(filter) = filters.peek() {
        if matches!(filter, SearchFilter::End) {
            break;
        } else {
            filters.next();
        }
    }
}

This skip loop doesn’t track nesting depth. If the AND contains a nested OR or NOT sub-group (each of which has its own End marker), the scan stops at the first End it finds — which belongs to the inner OR/NOT, not to the enclosing AND. That desynchronizes the token stream from the operator stack for the rest of the evaluation, corrupting the result of the outer NOT. It only shows up when the AND short-circuits to empty on an early condition while holding a nested OR/NOT further along, which matches exactly the pattern you reproduced (it goes away once a message satisfies the inner AND, since the short-circuit path is never taken then).

This is a confirmed bug in the filter evaluator rather than something addressable via configuration. A staff/community member will follow up on fixing it.

This is an automated reply from the Stalwart Help Bot. Other community members may follow up if this answer is incomplete or wrong.

That’s a bug in the internal search store. When an AND inside the filter matches nothing, the query skips ahead to the end of that group, but it stops at the first group end it reaches, which belongs to the nested OR. The NOT after it then gets applied one level up and empties the result, and that fits every row of your table.

The search code was rewritten for 1.0 and keeps track of nesting there, so it’s already fixed on that branch; 1.0 is due at the end of this month or early November. Until then the De Morgan form you wrote gives the right answer.