Skip to content

feat(search): answer questions, not just match substrings - #25

Merged
matinsaurralde merged 1 commit into
masterfrom
feat/16-concept-map
Aug 4, 2026
Merged

matinsaurralde merged 1 commit into
masterfrom
feat/16-concept-map

Conversation

@matinsaurralde

Copy link
Copy Markdown
Owner

Today, asking mem "what did I use to fix the certificate" returns nothing. The words people use to describe a command are not the words in the command.

An audit measured recall@5 over 10 natural-language questions:

approach recall@5 latency
substring search, as shipped 0/10 9.6 ms
Apple Foundation Models expanding the query 2/10 2086 ms
hybrid ranker, no synonyms 2/10 2.4 ms
hybrid ranker + a synonym map 8/10 3.9 ms

A hand-written concept map is 4x more accurate and 500x faster than the on-device model — and deterministic, auditable, editable, translatable, and contributable, which no embedding gives you. The model also hallucinates without grounding: asked about fixing a certificate it proposed certutil /rebuild, a Windows tool.

NLContextualEmbedding was tested too and scored 0/5 top-1: shell commands are out of distribution for a natural-language BERT, and every similarity clustered in 0.81–0.86.

Verified independently

Against a 14-command fixture, before/after:

"certificate"                 -> openssl x509 -in cert.pem -noout -text
"what is listening on a port" -> lsof -i :8080
"disk space"                  -> df -h
"unpack an archive"           -> tar -xzf backup.tar.gz
"database"                    -> psql -h localhost -U app appdb

Every one of those returned nothing before.

The matching rule

Expansion is a fallback, not a blend. The literal pass runs first and completely unchanged; the concept map is consulted only when it returns nothing. So mem openssl returns openssl, and every literal result keeps its exact prior score and position — that is structural, not a matter of weights, so no frequency/recency combination can overtake a literal match.

Inside the fallback, a candidate must satisfy at least one concept through a synonym. Two alternatives were rejected with reasons:

  • All concepts — nothing in openssl x509 -in cert.pem satisfies the "fix" in "what did I use to fix the certificate", so one unsatisfiable concept would veto the right answer.
  • A merely-literal word subset — that is the AND search silently downgraded to an OR. mem docker zzzz-no-such-term must still return nothing, and it does.
  • A required literal anchor scores 0, for the reason the feature exists: the words people describe a command with are not in the command.

Ranking inside the fallback tier is base_score × idf-weighted coverage, with idf measured over the candidate set, so a synonym that matches everything is inert (below 0.2 at 50% document frequency).

Yours to edit

src/mem/concepts.json ships 224 concepts and 1,027 synonyms. ~/.mem/concepts.json layers on top; a malformed user file warns and falls back rather than crashing. mem concepts prints the active map.

That editability is the whole argument over an embedding: you can read it, grep it, fix a wrong synonym, add your team's vocabulary, or translate it.

Measured here

10/10 recall@5 on the test fixture (8 of them ranked first); the test asserts ≥8 for headroom. On a 20k-command corpus, 9/10, with the fallback path at ~60 ms against ~11 ms for the literal pass — paid only when the literal pass found nothing.

1338 passed, 91% branch coverage (master: 1292 / 89%); concepts.py, search.py and ranking.py at 100%.

Passed on from the author

  • JSON field names pollute the raw-line prefilter: "port" matches "imported":false, so those queries parse every line. Pre-existing, affects the literal pass too, and a performance bug rather than a correctness one. Fixing separately.
  • mem/tui.py could adopt the same fallback in the finder. concepts.py is stdlib-only and Pydantic-free by design so it would fit, but that file was off-limits here.

Ten questions phrased the way people actually ask them ("how do I see
what's listening on a port") scored recall@5 of 0/10 against a real
history. Not a ranking problem: the right command was never in the
result set, because "certificate" and `openssl x509` share no letters.

Ship a curated concept map — ~200 entries from what a person says to the
shell vocabulary that expresses it — and consult it when, and only when,
substring search finds nothing. The measured alternatives lost: the
on-device LLM scored 2/10 at 2086ms and hallucinated a Windows tool for
the certificate question, and NLContextualEmbedding scored 0/5 top-1
because shell commands are out of distribution for a natural-language
BERT. See ADR-011.

Expansion can only add. The literal pass is untouched and always ranked
first, so a query that works today returns the same results, in the same
order, with the same scores. Inside the fallback a candidate must earn at
least one concept through a synonym — matching only some of the literal
words would be the AND search quietly downgraded to an OR, which is the
defect fixed in #10 — and coverage is weighted by idf, so a synonym
appearing in half the history is inert rather than harmful.

~/.mem/concepts.json layers over the shipped map, `mem concepts` prints
it, and a malformed file warns and falls back instead of crashing. That
editability is the whole advantage over an embedding.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8bf84ffd73

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/mem/concepts.py
Comment on lines +309 to +310
phrase = " ".join(terms[index : index + width])
expansions = data.concepts.get(phrase)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Normalize sentence punctuation before concept lookup

When a quoted natural-language query ends with punctuation, this exact lookup treats the final token as an unknown concept: for example, the command I used to fix the certificate? produces a literal certificate? group rather than expanding certificate, so it does not find the advertised openssl command even though the same question without ? does. Commas and periods cause the same failure; normalize sentence punctuation for concept matching while preserving the original terms for literal shell-fragment matching.

Useful? React with 👍 / 👎.

Comment thread src/mem/search.py
Comment on lines +262 to +264
if group.literal_hit(lowered_command):
return ranking.LITERAL_CREDIT
if group.matched_expansions(lowered_command):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Track synonym hits independently from literal credit

When a command contains both the concept's literal text and one of its mapped synonyms, the literal branch returns before the synonym is recorded. Because _expanded_search admits a candidate only when its credit row contains EXPANDED_CREDIT, a query such as check certificate rejects openssl x509 -in certificate.pem: the literal pass misses check, while the expanded pass sees certificate literally and never records the simultaneous openssl synonym hit. Admission should track whether any synonym matched separately from the credit chosen for ranking.

Useful? React with 👍 / 👎.

@matinsaurralde
matinsaurralde merged commit 0a7a089 into master Aug 4, 2026
5 checks passed
@matinsaurralde
matinsaurralde deleted the feat/16-concept-map branch August 4, 2026 05:27
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