aeat.application.command_search._index module¶
Hybrid command index: per-column BM25 lexical + model2vec semantic, RRF-fused.
Ranks a free-text query against the command corpus so the search meta-tool
bridges the operator’s natural vocabulary to a command’s own tokens. Three
cooperating parts, the same shape the corpus grounding search uses (ADR
mcp-progressive-discovery P2):
a per-column FTS5 lexical index that weights the command KEY and TOOL NAME above curated OUTCOME ALIASES above the human DESCRIPTION above the per-verb HELP, so a homonym token in a low-value column no longer outranks the correct command whose key carries it (the
importmis-rank the review found);a model2vec semantic side (behind the capability-gated
aeat-cli[search]extra, reused through thecorpus_searchpublic facade) that embeds the command docs and the query into one vector space and cosine-ranks them, so a concept query reaches the right verb across a Spanish/English or concept/verb vocabulary gap the stemmer cannot bridge;Reciprocal Rank Fusion (RRF,
k=60) over the two rankings.
The semantic side is a strict enhancement: when the search extra (or SQLite
FTS5) is absent, CommandIndex.search() degrades cleanly — FTS5 BM25 alone,
then a pure-Python token-overlap scorer — so a minimal install always ranks
better than a bare substring match and never hard-fails.
The index is SDK-independent and pure (it takes plain command documents), so it is unit-tested directly without the MCP transport.
- RRF_K¶
RRF damping constant. The canonical k=60 keeps a strong hit on one ranker from being drowned by the other ranker’s long tail.
- class CommandDoc(**data)[source]¶
Bases:
BaseModelOne command’s searchable document, split into weighted columns.
The four tiers rank a query hit by WHERE it lands:
key_and_name(the command key tokens and tool name) is the most discriminative,aliasescarries curated outcome vocabulary for composite verbs,descriptionis the human summary, andhelpis the per-verb CLI help — the noisiest tier. Each tier becomes its own BM25-weighted FTS5 column; a query token hitting the key ranks above the same token hitting the help.- Parameters:
- command_key: str¶
- tool_name: str¶
- key_and_name: str¶
- description: str¶
- aliases: str¶
- help: str¶
- class CommandHit(**data)[source]¶
Bases:
BaseModelOne ranked command match.
- command_key: str¶
- tool_name: str¶
- rank: int¶
- score: float¶
- class CommandIndex(docs, *, query_embedder=None, enable_semantic=True)[source]¶
Bases:
objectA hybrid searchable index over the command corpus.
Fuses a per-column FTS5 BM25 lexical ranking with a model2vec semantic cosine ranking via RRF; both degrade cleanly (FTS5-only, then token-overlap only) so a minimal install keeps a working
search.search()returns rankedCommandHitrecords for a free-text query.- Parameters:
docs (Sequence[CommandDoc])
query_embedder (QueryEmbedder | None)
enable_semantic (bool)
- search(query, *, limit=20)[source]¶
Return up to
limitranked command hits forquery.A blank query or one with no searchable terms returns no hits. The lexical side ranks the matching commands (per-column BM25, or token-overlap when FTS5 is absent); the semantic side re-ranks that candidate set by cosine similarity when the
searchextra is present; the two rankings are RRF-fused. When the semantic side is unavailable the fused order is exactly the lexical order.- Return type:
- Parameters:
- build_command_index(docs, *, query_embedder=None, enable_semantic=True)[source]¶
Build a
CommandIndexfrom the command documents.- Return type:
- Parameters:
docs (Iterable[CommandDoc])
query_embedder (QueryEmbedder | None)
enable_semantic (bool)