Skip to content

Memory search's key-match arm splits a key at whitespace before stemming it - #475

Merged
zlareb1 merged 4 commits into
mainfrom
fix/key-match-tokenize-keys
Nov 10, 2026
Merged

zlareb1 merged 4 commits into
mainfrom
fix/key-match-tokenize-keys

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in a release)

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

Memory search's key-match arm works in a namespace that holds a memory key with whitespace in it (communication style). Before, every key-match read in that namespace failed and the arm added nothing.

Why

The key-match arm splits each memory_key at _ and stems the parts with ClickHouse stem(t, 'en'). stem() refuses a word that holds ASCII whitespace (Code: 36. Function stem requires each input to be a single word without whitespace). All three statements stem every key in the namespace: the keys-only pre-check, the wide pass with its document frequencies, and the component-only pass. So one key such as communication style fails each of them, for every query in that namespace. The arm fails open, so search still answered, without the arm. A deployment logs key-match pre-check failed ... Code: 36 and then key-match arm failed on each such search.

The fix splits keys before they are stemmed: at _ and at the six ASCII whitespace characters stem() checks for (KEY_PARTS in server/memory/app/search/sql.py). translate maps each whitespace byte to _ before the split. A key without whitespace yields exactly the parts it did before. The Python mirror of the predicate (key_match_key_qualifies, which the gate uses) splits the same way through key_parts.

The query side already tokenizes in Python (lemma_tokens) and never reaches stem().

Decisions:

  • Whitespace only, not every punctuation mark. Splitting at /, - or . too would let pref/editor_theme match on pref. But it would also change the part count and the per-stem document frequencies of keys that work today, and with them the ranking. Whitespace is the one input stem() refuses, and no key that works today holds any.
  • translate, not splitByRegexp. translate maps bytes one for one, so the split reads nothing more. On clickhouse-local over two million generated keys (SELECT sum(length(arrayMap(t -> stem(t, 'en'), <split>))) FROM (SELECT concat('user_task_', toString(number % 1000), '_worktree_path') AS k FROM numbers(2000000))), splitByRegexp('[_[:space:]]', lower(k)) took 1.45 s against 0.73 s for splitByChar('_', lower(k)), and translate plus splitByChar took 0.77 s.

Scope and risk

  • Touches the three key-match statements, the Python predicate mirror, their tests and the analyzer's shape list.
  • Ranking: unchanged for every namespace whose keys hold no whitespace (see Tests). In a namespace with such a key, the arm now runs where it failed before. Those keys now match on their words, so communication style matches a query about "communication style".
  • Cost: the same as the _ split alone (see above).

Compatibility, rollout and rollback

  • Wire: none.
  • Operator steps: none.
  • Rollback: reverting brings back the failure; nothing is stored.

Tests

  • tests/unit/test_key_match_sql.py (clickhouse-local, the production table):
    • A namespace holding communication style and user_communication_style: the wide statement and the component statement each find both keys, and the pre-check answers, also on a namespace whose only key is communication style. On main each fails with the deployment's Code: 36 (the arm logs a failed pre-check and falls through to the wide statement).
    • key_parts in Python returns the statements' KEY_PARTS for ASCII keys with __, a leading _, a space, a tab, a newline, a vertical tab, a form feed, a carriage return and //-. The last one also holds the letters v, f and r, so each escape in the literal is pinned as whitespace, not as its letter.
    • Before and after, on a namespace of whitespace-free keys (__, a leading _, mixed case, /, -): for three queries, each statement's rows and order equal those of the same statement with the _-only split put back.
  • Ranking-neutral, by direct comparison at scale (a one-off script, not committed; the unit test above is its small form). On clickhouse-local, one namespace held 2,509 whitespace-free keys: every _-joined key-like string in tests/ and bench/, plus edge cases (a__b, _lead_x, trail_x_, mixed case, non-ASCII).
    • Both versions of all three statements ran for 141 queries of two or more stems: shuffled key words, extra words and stop words. The old version is the statement with KEY_PARTS replaced by splitByChar('_', lower(memory_key)).
    • The rows and their order were identical for each statement and query, and 58 of the queries returned rows.
    • Every row the wide statement returned was one the Python mirror qualifies.
  • tests/unit/test_key_match_arm.py: the PREWHERE assertion names KEY_PARTS.
  • tests/analyzer/shapes.txt: the nine key-match shapes are replaced (the pre-check, four of the wide statement and four of the component statement), regenerated with python -m tests.analyzer.corpus --write, which on this branch reproduces the file exactly.
  • Gates: make format lint typecheck; the key-match, read-scope, tag-filter, run-scope, handler-gate and public-copy unit tests; tests/analyzer.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code. The failing statement was reproduced on clickhouse-local, and the comparison above was run.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • make format-check, make lint, make typecheck and make test-unit pass; the integration suite passes if server behaviour changed. (Format, lint, typecheck and the affected unit tests were run; CI runs the rest.)
  • Generated files are regenerated in this PR, not by hand. (The shape list's key-match lines come from --write; see Tests.)
  • A new migration is additive and numbered after the latest on main; no merged migration is edited. (No migration.)
  • A behaviour change to any file a plugin ships bumps both plugin manifests' version. (No plugin files.)
  • The frontend builds and its tests pass, if frontend/ changed. (Unchanged.)
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers.
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code

zlareb1 and others added 4 commits November 10, 2026 11:12
…ing it

ClickHouse stem() refuses a word that holds ASCII whitespace, so one key
such as `communication style` failed the key-match pre-check, the wide
pass and the component pass for every query in its namespace. Keys are
now split at `_` and at the six ASCII whitespace characters (`translate`
then `splitByChar`); a key without whitespace splits as before, and the
Python mirror of the predicate splits the same way.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…curate docstrings

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ace key qualifies in the gate

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… whitespace

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 force-pushed the fix/key-match-tokenize-keys branch from fff6dd1 to 4624655 Compare November 10, 2026 11:12
@zlareb1
zlareb1 merged commit 653e47d into main Nov 10, 2026
37 of 38 checks passed
@zlareb1
zlareb1 deleted the fix/key-match-tokenize-keys branch November 10, 2026 12:35
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.

1 participant