Repository navigation
feat(search): answer questions, not just match substrings - #25
Conversation
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.
There was a problem hiding this comment.
💡 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".
| phrase = " ".join(terms[index : index + width]) | ||
| expansions = data.concepts.get(phrase) |
There was a problem hiding this comment.
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 👍 / 👎.
| if group.literal_hit(lowered_command): | ||
| return ranking.LITERAL_CREDIT | ||
| if group.matched_expansions(lowered_command): |
There was a problem hiding this comment.
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 👍 / 👎.
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:
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.NLContextualEmbeddingwas 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:
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 opensslreturns 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:
openssl x509 -in cert.pemsatisfies the "fix" in "what did I use to fix the certificate", so one unsatisfiable concept would veto the right answer.mem docker zzzz-no-such-termmust still return nothing, and it does.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.jsonships 224 concepts and 1,027 synonyms.~/.mem/concepts.jsonlayers on top; a malformed user file warns and falls back rather than crashing.mem conceptsprints 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.pyandranking.pyat 100%.Passed on from the author
"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.pycould adopt the same fallback in the finder.concepts.pyis stdlib-only and Pydantic-free by design so it would fit, but that file was off-limits here.