Skip to content

fix(stack-encrypt,eql-bindings): cut a query's short words only when it cuts no n-gram - #1753

Merged
coderdan merged 4 commits into
nextfrom
fix/match-query-word-tokens
Nov 23, 2026
Merged

coderdan merged 4 commits into
nextfrom
fix/match-query-word-tokens

Conversation

@coderdan

@coderdan coderdan commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The match index's short-words option makes two-letter words such as "Ng" searchable. With it on, a search for "Mike Ng" stopped finding "Mike Nguyen", and "san fr" stopped finding "San Francisco", because the query also demanded the short word as a whole word. Now a query uses its short words only when it cuts no 3-letter piece, which means the whole query is short words. "Ng" still finds "Ng" and "Mike Ng" and not "Nguyen". "Mike Ng", "ke Ng" and "san fr" find the longer names again. Stored values don't change, so nothing needs re-encrypting.

Changes

  • stack-encrypt tokenize: the rule lives in one place, Tokens::into_query, chosen by a crate-private Side::{Stored, Query}.
  • stack-encrypt sem:
    • QueryTokens cuts a query as a query, and verify cuts the row as stored.
    • match_term takes a Side.
    • New crate-private match_query.
  • stack-encrypt target:
    • New public target::matching_query (a query's filter) and Index::query_operation. query_operation defaults to operation, and Match overrides it. A field query now derives through query_operation.
    • New public Indexes::query_operations for a set of indexes. A Vec refuses an empty set before any key request, and wipes an owned plaintext when it does.
    • Breaking: Index::query_operation and Indexes::query_operations are required, so a wrapper index can't quietly send the stored filter. The CHANGELOG has upgrade steps.
    • matching_query and the other query cuts refuse text that keeps no token (TermError::EmptyTermText), instead of returning a filter that matches every row.
  • stack-encrypt dynamic: query_term and the term probe cut as a query, and the record path cuts as stored.
  • eql-bindings: new v4::terms::QueryBloomFilter, with the same JSON and schema as BloomFilter. Its EncryptTarget goes through matching_query, and it refuses an empty filter. TwinIndex forwards query derivation to the indexes whose terms its twin carries, through Indexes::query_operations. Since feat(eql-bindings): answer order queries from a field reference #1752 also lets the TextSearch and TextSearchOre twins answer eq, the orderings and matches, their matches sends the query-side filter too.
  • eql-codegen: every v4 query twin's bf is now a QueryBloomFilter. v4/text.rs and v4/answers.rs are regenerated, and eql_gen.go is unchanged.
  • Docs:
    • MatchOptions::short_words, the sem leakage section, the match_terms and dynamic::term docs, and the eql-bindings README.
    • Go encrypt/README.md and cmd/stashgen/README.md.
    • CHANGELOG entries for stack-encrypt, eql-bindings and Go.
    • The pending .changeset/eql-v4-match-short-words.md is edited, because its "Mike Ng does not find Mike Nguyen" sentence is now wrong.
    • The docs state the leakage plainly: one unique token per short word, in every stored value that holds one.
  • Tests:
    • Unit tests and proptests for the rule. One proptest checks that a query's filter is exactly the filter of the tokens it verifies by.
    • tests/match_short_words.rs: a table covering "Ng", "Mike Ng", "ke Ng", "san fr", "Li Wu" and "Mike Ngo", plus a typeahead proptest.
    • packages/stack-encrypt/tests/index.rs, given_a_set_holding_a_match_index_with_short_words: one index, a tuple and a Vec derive the query filter from query_operations and the stored filter from operations.
    • EQL typed_queries.rs, given_a_two_letter_word_in_a_match_query, over TextMatch, TextSearch and TextSearchOre: the field query and the data path keep the same rows and produce the same bytes. A stored "Mike Nguyen" filter contains the "Mike Ng" and "ke Ng" query filters. This test fails if TwinIndex sends the stored filter.
    • Go: Verify("Mike Ng") keeps "Mike Ng" and "Mike Nguyen".

Verification

  • cargo test -p stack-encrypt --all-features: all test binaries pass, including ui and doctests.
  • fmt, clippy -D warnings and doc -D warnings are clean for stack-encrypt.
  • packages/eql:
    • cargo test passes for eql-encryption-tests, eql-bindings, eql-domains and eql-codegen.
    • clippy is clean.
    • The TS bindings and JSON schemas are unchanged.
    • The generators regenerate cleanly.
  • Go: guests were rebuilt. go test ./... passes, go generate ./... produces no drift, and golangci-lint reports 0 issues.
  • cargo-mutants --in-diff: 29 mutants, 9 caught, 20 unviable, 0 missed.
  • PostgreSQL: the v4 bundle from tasks/build.sh, on a throwaway PostgreSQL 17, passes every suite in test:encryption:postgres (63 tests), including postgres_finds_two_letter_surnames.
  • The whole stack below this PR, checked again at this PR's head:
    • cargo test -p stack-encrypt --all-features: 1652 passed, 0 failed.
    • eql workspace tests: 630 passed.
    • Go tests and golangci-lint: pass.
    • The conformance script tests: 58 passed.
  • cargo doc -D warnings for eql-bindings fails with 10 broken links in v4/json_tree.rs. The same errors are on next, and none comes from this PR.

Related

Closes #1398. Stacked on #1752.

Review notes

  • One rule, applied once. The verifier, the typed chain, the dynamic path and the EQL twins all cut a query through Tokens::into_query.
  • Index::query_operation and Indexes::query_operations are required. An index that wraps others has to forward them, or it would quietly send the stored filter. Making them required turns that mistake into a compile error. TwinIndex forwards to the indexes whose terms its twin carries.
  • Why a separate QueryBloomFilter: the EQL data path builds query twins through the bf field type's EncryptTarget, so a query type is the clean place for the rule. It follows the existing SearchBloomFilter pattern. It changes public field types in the unreleased v4 module only, and its wire JSON and schema don't change.
  • Changelogs: short words already shipped in stack-encrypt 0.3.0 and Go rc.2, so those crates get new Unreleased entries rather than edits to released sections.
  • Vectors: no conformance vectors changed, because none pins short-word query tokens. Only test expectations for query results moved.

@coderdan
coderdan requested a review from a team as a code owner November 22, 2026 14:53
@changeset-bot

changeset-bot Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: b0d2a9d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes changesets to release 12 packages
Name Type
@cipherstash/eql Minor
stash Patch
@cipherstash/stack-prisma Patch
@cipherstash/basic-example Patch
@cipherstash/e2e Patch
@cipherstash/prisma-example Patch
@cipherstash/stack-drizzle Patch
@cipherstash/stack-supabase Patch
@cipherstash/stack Patch
@cipherstash/wizard Patch
@cipherstash/bench Patch
@cipherstash/test-kit Patch

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderdan
coderdan requested a balanced review from Copilot November 22, 2026 14:53
@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI; Enforced overrides: Organization
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: bd2a59ab-d345-488b-a386-09c03c14927d

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The empty-set failure paths drop owned plaintext without wiping it, violating the owned-mode wiping guarantee and requiring a wiping failure helper with regression coverage.

1 open issue
What changed in this PR

Restores partial-name matching with short-word indexing enabled across Stack Encrypt and EQL v4. Queries retain short-word tokens only when they produce no n-gram; stored filters remain unchanged, so no re-encryption is needed.

Changes:

  • Separates stored-value and query token derivation across typed and dynamic paths.
  • Adds QueryBloomFilter and forwards query-specific derivation through EQL query twins.
  • Adds Rust and Go regression coverage and updates documentation and release notes.
File Description
packages/​stack-encrypt/​tests/​match_short_words.rs Tests partial-name matching and token containment.
packages/​stack-encrypt/​tests/​index.rs Tests query derivation for individual and grouped indexes.
packages/​stack-encrypt/​src/​target/​operations.rs Adds the query-specific matching operation.
packages/​stack-encrypt/​src/​target/​mod.rs Exports matching_query.
packages/​stack-encrypt/​src/​target/​index.rs Adds query operations to index traits and implementations.
packages/​stack-encrypt/​src/​sem/​tokenize.rs Centralizes query-side short-word filtering.
packages/​stack-encrypt/​src/​sem/​mod.rs Aligns filter derivation and verification with query tokens.
packages/​stack-encrypt/​src/​op/​mod.rs Routes field queries through query operations.
packages/​stack-encrypt/​src/​dynamic/​term.rs Distinguishes stored terms from query probes.
packages/​stack-encrypt/​src/​dynamic/​query.rs Uses query-side term derivation.
packages/​stack-encrypt/​CHANGELOG.md Records matching behavior and new operations.
packages/​eql/​tests/​encryption/​tests/​v4_types.rs Tests query twins and PostgreSQL partial-name matching.
packages/​eql/​tests/​encryption/​tests/​typed_queries.rs Checks typed/data-path parity and verification.
packages/​eql/​crates/​eql-codegen/​src/​targets.rs Generates query twins using QueryBloomFilter.
packages/​eql/​crates/​eql-codegen/​src/​bindings.rs Selects query-specific Bloom filter field types.
packages/​eql/​crates/​eql-bindings/​src/​v4/​text.rs Updates generated query filter fields.
packages/​eql/​crates/​eql-bindings/​src/​v4/​mod.rs Defines QueryBloomFilter with the existing wire schema.
packages/​eql/​crates/​eql-bindings/​src/​v4/​answers.rs Updates generated query assembly types.
packages/​eql/​crates/​eql-bindings/​src/​encryption/​query.rs Forwards query derivation through TwinIndex.
packages/​eql/​crates/​eql-bindings/​src/​encryption.rs Implements query-filter encryption and validation.
packages/​eql/​crates/​eql-bindings/​README.md Explains revised matching and leakage.
packages/​eql/​crates/​eql-bindings/​CHANGELOG.md Records EQL query-filter changes.
languages/​golang/​encrypt/​README.md Updates Go matching guidance.
languages/​golang/​encrypt/​eql_v4_test.go Adds partial-name verification coverage.
languages/​golang/​cmd/​stashgen/​README.md Updates generated-field matching guidance.
languages/​golang/​CHANGELOG.md Records restored partial-name matching.
.changeset/​eql-v4-match-short-words.md Updates pending EQL release notes.

Review effort: Balanced

@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Mutation testing (cargo-mutants, --in-diff, stack-auth + stack-encrypt + stack-encrypt-jsonpath)

caught missed unviable timeout
11 0 29 0

Every mutant in the changed lines was caught by a test.

@cipherstash-agent cipherstash-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review attestation

Reviewed e73459fb · posted from Claude Code · claude-opus-5-5 · effort medium
Sources: code-review · claude-opus-5-5; rust-security agent · claude-opus-5-5; mattpocock code-review agent · claude-opus-5-5
Skills: code-review, rust-security, mattpocock-skills:code-review, review-cross-check, review-post

Findings (7): 7 minor, 4 inline, 3 below.

  • packages/eql/crates/eql-bindings/src/encryption.rs:407-417 (outside diff) minor The MatchVisitor/bloom_filter doc still says stored values and queries "take it from the same type, so they always agree" and that BloomFilter "is the filter of every match query"; every v4 query bf is now QueryBloomFilter. Same stale claim at v4/mod.rs:112-113 (SearchBloomFilter) and eql-codegen/src/bindings.rs:450-456 (stored_term_newtype). Name QueryBloomFilter as the query filter and say the two agree only on the tokens a query keeps.
  • packages/stack-encrypt/src/sem/mod.rs:2058-2096 (outside diff) minor The KeysetCipher impl doc ("These are the query-probe path", "The bytes are identical either way") still presents match_terms as a query probe, contradicting its own new doc at 2145-2155: with short_words on, match_terms::<ShortWords>("Mike Ng") keeps ng and misses "Mike Nguyen". Say it derives the stored filter; ADR-0004 decision 5 and the MatchConfig doc (819-824) need the same caveat.
  • packages/stack-encrypt/docs/adr/0009-operators-select-the-index-and-a-query-term-is-not-the-stored-term.md (outside diff) minor ADR-0009 governs query vs stored terms but gets no amendment for Index::query_operation, Indexes::query_operations, the wrapper-must-forward rule, matching_query, or the short-words query rule. Add an "Amended" section like the existing ones.

Generated by Claude Code

@coderdan
coderdan force-pushed the feat/eql-bindings-order-queries branch from 5b83b21 to ff7cbaf Compare November 22, 2026 16:03
@coderdan
coderdan force-pushed the fix/match-query-word-tokens branch from e73459f to a759749 Compare November 22, 2026 16:31
@coderdan

Copy link
Copy Markdown
Contributor Author

Review round 1, addressed in a759749. The branch was also rebased onto #1752's new head and force-pushed. The rebase hit conflicts in eql-codegen targets.rs and two CHANGELOGs. Every query twin's bf now comes from #1752's IndexKey API as QueryBloomFilter, and regenerating produced no diff.

  • Breaking for indexes defined outside the crate: Index::query_operation and Indexes::query_operations are now required, so a wrapper index can't quietly send the stored filter. The CHANGELOG has upgrade steps.
  • matching_query and the other query cuts now refuse text that keeps no token (TermError::EmptyTermText), instead of returning a filter that matches every row.
  • Stale eql-bindings docs (review body): MatchVisitor, SearchBloomFilter and stored_term_newtype no longer say a query uses BloomFilter.
  • KeysetCipher / match_terms / MatchConfig docs (review body): they now say match_terms derives the stored filter, and point queries at the field reference or matching_query.
  • ADR-0004 and ADR-0009 (review body): both carry an amendment for this change.
  • A possible follow-up, not done here: the public Encryption::failed, refused and ready still ignore an owned source. Nothing in the crate calls them in owned mode. Bounding or documenting them is a separate API decision.
  • The inline findings are answered on their threads.

Checks at a759749, on the whole stack:

  • stack-encrypt clippy with -D warnings: clean with all features, the default features and no default features.
  • cargo test -p stack-encrypt --all-features: 1671 passed. With no default features and -D warnings: 1066 passed.
  • rustdoc with -D warnings: clean.
  • EQL workspace: 634 passed, and clippy is clean. The generators produce no diff.
  • PostgreSQL 15: all 63 tests in test:encryption:postgres pass.
  • Go: all guests rebuilt. go test ./... passes in all three modules, go generate produces no drift, and golangci-lint reports 0 issues.
  • Conformance script tests: 58 of 58 pass.
  • cargo-mutants on the diff: 40 mutants, 11 caught, 29 unviable, 0 missed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The documentation incorrectly equates the no-n-gram condition with a single-word query and needs correction in both the source comment and changelog.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread packages/stack-encrypt/src/sem/mod.rs Resolved
@coderdan
coderdan force-pushed the fix/match-query-word-tokens branch 2 times, most recently from e8ff073 to b81001f Compare November 22, 2026 23:42
@coderdan
coderdan force-pushed the feat/eql-bindings-order-queries branch from eaf369a to a914822 Compare November 23, 2026 00:15
…it cuts no n-gram

With MatchOptions::short_words on, a query that held a short word also
asked for that word's token beside its n-grams, so the word had to be a
whole word of the row. "Mike Ng" no longer found "Mike Nguyen", and "san
fr" no longer found "San Francisco", which breaks typeahead and partial
names on EQL v4 text columns.

A query now keeps its short words only when it cuts no n-gram, which is
when the whole query is one short word. "Ng" is still its word token and
finds "Ng" and "Mike Ng" but not "Nguyen"; "Mike Ng" is its trigrams
alone. The stored side is unchanged, so no stored filter moves.

The rule lives once, in tokenize (Tokens::into_query, selected by
Side::Query), and every query derivation cuts through it:

- QueryTokens::under, so the typed verifier, dynamic::verify and the
  query preparation all keep the same tokens;
- match_term takes a Side; a new target::matching_query operation derives
  a query's filter, and Index::query_operation (defaulting to operation)
  is what a field query now derives through. Match overrides it, IndexSpec
  overrides it, and a set of indexes derives its query terms through the
  new Indexes::query_operations, each index's query_operation (one index,
  a tuple and a Vec);
- eql-bindings' TwinIndex forwards query_operation to the indexes whose
  terms its twin carries, so matches on TextMatch, TextSearch and
  TextSearchOre sends the query's filter, and every other operator the
  term its index stores;
- the dynamic derive takes a Side: query_term and the term probe cut a
  query, the record path a stored value;
- eql-bindings' v4 query twins carry a new QueryBloomFilter (BloomFilter's
  JSON and schema), whose EncryptTarget derives through matching_query,
  so TextMatchQuery and TextSearch(Ore)MatchQuery built directly or by the
  by-name query dispatch follow the rule. eql-codegen emits it for every
  v4 query twin's bf, and for the bf term of the twins' QueryTwin impls.

typed_queries.rs holds a field reference's matches on TextMatch,
TextSearch and TextSearchOre to the data path's bytes and verdicts, and
checks that "Mike Ng" and "ke Ng" find a stored "Mike Nguyen".

Docs, the pending EQL changeset and the CHANGELOGs state the new rule and
the leakage: one unique token per short word in every stored value that
holds one.

Closes #1398
An empty Vec of indexes refused with EmptyIndexes through
Encryption::failed, which ignores its source, so in Owned mode the
refusal freed the plaintext unwiped. Both Vec::operations and
Vec::query_operations now share one fold (each_index, keyed by Side),
and its empty branch refuses through a new crate-private
Encryption::failed_wiping, which hands the source to WipeSource::wipe
first. Two owned wipe-counter tests in tests/index.rs pin it.

Index::query_operation and Indexes::query_operations lose their
defaults. With a default of the stored term, a wrapper over a
short-words Match that forwarded only operation compiled and sent the
stored cut, missing "Mike Nguyen" for "Mike Ng". Every built-in index
now states its query term, and the CHANGELOG records the breaking
change for indexes defined elsewhere.

target::matching_query run on its own refuses text that keeps no token
with TermError::EmptyTermText (in sem::match_term, for Side::Query), as
a field query's prepare does, rather than deriving an empty filter that
matches every row.

Docs: the Index and Indexes docs point a query at query_operation; the
KeysetCipher, MatchConfig, MatchVisitor, SearchBloomFilter and
stored_term_newtype docs say match_terms and BloomFilter are the stored
cut and QueryBloomFilter the query's; ADR-0004 decision 5 gets a caveat
and ADR-0009 an amendment for the query cut.
…the n-gram

The docs said a query cuts no n-gram only when it is one short word.
That holds for trigrams, but the n-gram length is an option: under
6-grams "Ng Wu" cuts no n-gram and asks for both its words. Say what the
condition is instead. The EQL and Go docs keep "one two-letter word",
because both use 3-grams.
@coderdan
coderdan force-pushed the fix/match-query-word-tokens branch from b81001f to 931be62 Compare November 23, 2026 00:22
Base automatically changed from feat/eql-bindings-order-queries to next November 23, 2026 01:08
…n plain language

The docs this branch added or changed were hard to follow. They used
"cut" for several ideas, packed several facts into each sentence, and
mixed the value an app stores with the query it sends.

Each passage now leads with what the reader must do or must not do,
then gives the reason, with "Mike Ng" and "Mike Nguyen" as the example.
"n-gram", "short word" and "token" are defined where they first appear.
The rule is stated one way everywhere: a query keeps its short words
only when its whole text is shorter than one n-gram. In the EQL and Go
docs that is a query of one two-letter word.

The rewrite covers the rustdoc and comments in stack-encrypt,
eql-bindings and eql-codegen, the ADR-0004 and ADR-0009 amendments, the
stack-encrypt, eql-bindings and Go changelogs, the changeset, and the
eql-bindings and Go READMEs. Every fact is kept, including the leakage
note, the upgrade steps for the new required methods, and why
query_operation has no default. Rustdoc and Go docs no longer describe
what was visible before the option; they say what is visible without it.

No code changes.

@coderdan coderdan left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Human pass. LGTM.

@coderdan
coderdan merged commit f52f4d5 into next Nov 23, 2026
58 checks passed
@coderdan
coderdan deleted the fix/match-query-word-tokens branch November 23, 2026 01:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

match index: "Mike Ng" no longer finds "Mike Nguyen" with short words; cut query word tokens only when the query has no n-gram

3 participants