Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
feat(skills): deterministic relevance scorer — the paraphrase channel
Ruben Lütkeb524b110a969
diff --git a/jarvis/skills/relevance.py b/jarvis/skills/relevance.pynew file mode 100644index 0000000000..6adc845649--- /dev/null+++ b/jarvis/skills/relevance.py@@ -0,0 +1,881 @@+"""Deterministic skill relevance — the paraphrase channel.++The gap this closes: a skill fires only when the user hits an author-written+regex verbatim, or when the router LLM happens to notice it among twenty+undifferentiated bullets. There is no *partial* match channel, so every+rephrasing falls through. This module is that channel.++It needs no model, no network and no new dependency, because **the corpus+already contains the vocabulary**: authors write their trigger regexes as lists+of literal phrases, quote real utterances inside ``when_to_use``, and fill+``intent_verbs`` / ``intent_objects`` with the German and Spanish nouns a user+actually says. The scorer mines what is already written rather than asking+anyone to re-author 22 builtin skills.++Design commitments, each load-bearing:++* **Stdlib only, pure CPU.** No LLM call, no disk access, no network — this sits+ on the voice hot path beside ~30 regex gates that cost 5-20 µs each (AP-9,+ AP-11). Typical query is ~75 µs at 50 skills.+* **O(len(utterance)), not O(len(skills)).** A posting list means installing+ more skills does not slow the hot path.+* **Corpus IDF, PLUS a closed function-word list.** IDF alone was the original+ design and it was measured to be insufficient here — see the comment above+ ``_FUNCTION_WORDS`` for the failure and the numbers. IDF still does the heavy+ lifting for domain terms that happen to be common across the installed set;+ the word list only covers the closed class IDF structurally cannot see.+* **Zero ``jarvis.*`` imports outside this package.** Skills are duck-typed via+ ``getattr`` exactly as ``prompt_injection.py`` and ``local_search.py`` do, so+ the module is trivially testable and cannot close an import cycle.++Deliberately NOT here: embeddings (a network round trip per query, or a ~90 MB+local model plus torch — both break the headless 1-vCPU floor and AP-26) and+stemming (a dependency or ~400 lines of per-language rules, needs language+detection we do not have on this path, and is hostile to German compounds,+which are the actual problem). Character trigrams do the same work in far less+code, language-agnostically.++Bands and thresholds are applied by :mod:`jarvis.skills.match_eval`; this module+only scores and reports where its own corpus-derived cut-offs lie. That keeps+the dependency one-directional.+"""+from __future__ import annotations++import logging+import math+import re+import unicodedata+from dataclasses import dataclass+from dataclasses import field as dc_field+from typing import Any++log = logging.getLogger(__name__)++# ---------------------------------------------------------------------------+# Field weights+# ---------------------------------------------------------------------------++FIELD_NAME = "name"+FIELD_TRIGGER = "trigger"+FIELD_INTENT = "intent"+FIELD_TAG = "tag"+FIELD_WHEN = "when_to_use"+FIELD_DESC = "description"+FIELD_CATEGORY = "category"++#: ``trigger`` carries name-level weight on purpose: a regex literal is the+#: author's own statement of "this is what the user says", and it is the only+#: field reliably non-English across the corpus (descriptions are English by+#: repo policy; triggers and ``intent_objects`` carry the German and Spanish).+FIELD_WEIGHTS: dict[str, float] = {+ FIELD_NAME: 3.0,+ FIELD_TRIGGER: 3.0,+ FIELD_INTENT: 2.5,+ FIELD_TAG: 2.0,+ FIELD_WHEN: 1.6,+ FIELD_DESC: 1.0,+ FIELD_CATEGORY: 0.6,+}++# ---------------------------------------------------------------------------+# Tuning constants+# ---------------------------------------------------------------------------++#: Tokens shorter than this are dropped: below three characters a token carries+#: no discrimination and matches half the corpus by accident.+MIN_TOKEN_LEN = 3++#: Added to the query-mass denominator. Roughly "one distinctive word", which+#: stops a single-token utterance from producing an unbounded score.+QUERY_MASS_FLOOR = 3.0++#: A term present in this fraction of the corpus or more is treated as carrying+#: no information at all.+CORPUS_COMMON_FRACTION = 0.6++#: The corpus-common clamp only applies from this many skills upwards. Below+#: it the fraction is meaningless and actively harmful: on a fresh install with+#: three skills, "present in 60 % of the corpus" is TWO skills, so almost every+#: term would be clamped to zero and nothing could ever match. A fresh install+#: is precisely when the user first tries a skill, so failing there is the+#: worst possible place to fail.+CORPUS_COMMON_MIN_SKILLS = 6++#: How many top candidates get the (more expensive) trigram pass.+TRIGRAM_CANDIDATES = 8++#: Utterance tokens shorter than this contribute no trigrams — below five+#: characters the trigram set is noise.+TRIGRAM_MIN_TOKEN_LEN = 5++#: Weight of the trigram channel relative to the token channel.+TRIGRAM_WEIGHT = 0.55++#: Smoothing in the trigram containment denominator.+TRIGRAM_FLOOR = 8++#: Clear-winner rule: the leader must beat the runner-up by an absolute margin+#: OR a relative one, whichever is larger. The relative term is what forces a+#: genuine tie ("gmail 1.80 vs slack 1.70") to degrade instead of firing.+MARGIN_ABS = 0.12+MARGIN_REL = 0.25++#: Threshold coefficients, expressed against a corpus statistic rather than as+#: bare floats so they stay meaningful from a 5-skill fresh install to a+#: 60-skill power user (see :meth:`SkillMatchIndex.fire_threshold`).+#:+#: PLACEHOLDERS pending the offline calibration sweep, which picks the smallest+#: threshold with zero false positives over the hard-negative set and then+#: REPORTS the resulting recall. Do not hand-tune these to fix a single+#: over-fire; add a hard negative and re-run the sweep.+FIRE_COEFFICIENT = 0.62+HINT_COEFFICIENT = 0.22++# ---------------------------------------------------------------------------+# Hard caps — nothing a user installs may degrade the hot path+# ---------------------------------------------------------------------------++MAX_INDEXED_SKILLS = 300+MAX_TOKENS_PER_SKILL = 200+MAX_TRIGRAMS_PER_SKILL = 600++# ---------------------------------------------------------------------------+# Normalization+# ---------------------------------------------------------------------------++# Umlauts fold to digraphs BEFORE the NFKD pass, because the corpus is written+# in digraph form ("tages(ue|ue)berblick", "faehigkeit"). A plain NFKD strip+# would yield the bare vowel instead and silently disable the German+# vocabulary. Lifted from jarvis/brain/turn_planner.py for exactly that reason.+_UMLAUT_MAP = str.maketrans({+ "ä": "ae", "ö": "oe", "ü": "ue", # i18n-allow: speech-input normalization data+ "Ä": "ae", "Ö": "oe", "Ü": "ue", # i18n-allow: speech-input normalization data+ "ß": "ss", # i18n-allow: speech-input normalization data+})++_TOKEN_RE = re.compile(r"[a-z0-9]+")++# Function words in every supported language, in normalized (digraph) form.+#+# Corpus IDF was SUPPOSED to make this list unnecessary — a term common to the+# whole corpus earns ~0 weight on its own. Measured against the real 20 builtin+# skills, that reasoning is wrong here, and the failure is instructive: skill+# descriptions are English by repo policy while ``when_to_use`` and triggers+# quote German and Spanish. A German function word therefore appears in ONE+# skill, not twenty, so IDF scores it as maximally DISTINCTIVE. Live+# measurement: "die" and "ich" carried idf 2.64 of a possible 3.74 and made+# morning-routine win "ich brauch jetzt ruhe zum arbeiten"; the trigger literal+# "meine" (mined from "(meine[nm]? )?" in the calendar pattern) made+# plugin-google_calendar beat cli-gcloud on a Google Cloud billing question.+#+# So this list is a deliberate, bounded exception, not a maintenance burden:+# function words are a CLOSED class that does not change as skills are+# installed. Content words are never listed — only articles, pronouns,+# auxiliaries, prepositions, conjunctions and degree adverbs.+_FUNCTION_WORDS: frozenset[str] = frozenset(+ (+ # -- German ---------------------------------------------------+ "der die das den dem des ein eine einen einem einer eines " # i18n-allow+ "und oder aber ich du sie wir ihr mein meine meinem meinen " # i18n-allow+ "meiner meines mich mir dich dir sich uns euch ihm ihn ihnen " # i18n-allow+ "ist sind war waren bin bist sein seine habe hast hat haben " # i18n-allow+ "hatte hatten wird werden wurde muss musst muessen kann " # i18n-allow+ "kannst koennen koennte soll sollte sollen will willst wollen " # i18n-allow+ "darf duerfen mag moegen nicht kein keine nur auch noch schon " # i18n-allow+ "mal doch denn dann wenn weil dass als wie was wer wen wem " # i18n-allow+ "wann warum wieso welche welcher welches fuer mit von zu zum " # i18n-allow+ "zur aus bei nach ueber unter vor durch gegen ohne seit gerade " # i18n-allow+ "eigentlich jetzt bitte danke etwas alles nichts viel mehr " # i18n-allow+ "sehr ganz immer wieder hier dort " # i18n-allow+ # -- English --------------------------------------------------+ "the and but you your his her its our their they them him "+ "are was were been being have has had does did doing "+ "will would can could should shall may might must not "+ "only also still already just then when because that this these "+ "those with from for into over under after before between about "+ "there here very much more most some any each every "+ "yes please thank thanks "+ # -- Spanish --------------------------------------------------+ "los las una unos unas pero ella nosotros mis nos por para con " # i18n-allow+ "del son era eran ser estar tengo tienes tiene tener hacer solo " # i18n-allow+ "tambien ahora quien donde cuando como mas mucho poco todo nada " # i18n-allow+ "bien que sus esta este estos estas ese esa hay han desde hasta " # i18n-allow+ "entre sobre sin" # i18n-allow+ ).split()+)+++def _politeness_fillers() -> frozenset[str]:+ """The project's own curated address/courtesy vocabulary.++ ``trigger_matcher._FILLER_WORDS`` already encodes which German and English+ tokens are pure politeness glue ("bitte", "kurz", "mal", "mach"), reviewed+ against real utterances. Reusing it beats maintaining a second, drifting+ copy — and it covers the imperative glue ("mach", "kannst") that a plain+ function-word list misses.+ """+ try:+ from jarvis.skills.trigger_matcher import _FILLER_WORDS++ return frozenset(normalize_text(word) for word in _FILLER_WORDS)+ except Exception: # noqa: BLE001 — never let a refactor there break scoring+ return frozenset()+++def normalize_text(text: str) -> str:+ """Casefold, fold umlauts to digraphs, then strip remaining diacritics.++ The trailing NFKD pass costs nothing and makes Spanish accents and ñ fold+ correctly for free, which is why no language-specific branch is needed.+ """+ folded = str(text or "").casefold().translate(_UMLAUT_MAP)+ decomposed = unicodedata.normalize("NFKD", folded)+ return "".join(ch for ch in decomposed if not unicodedata.combining(ch))+++_STOPWORDS: frozenset[str] | None = None+++def _stopwords() -> frozenset[str]:+ global _STOPWORDS+ if _STOPWORDS is None:+ _STOPWORDS = _FUNCTION_WORDS | _politeness_fillers()+ return _STOPWORDS+++def is_content_token(token: str) -> bool:+ """True for a token that can discriminate between skills."""+ return len(token) >= MIN_TOKEN_LEN and token not in _stopwords()+++def tokenize(text: str) -> tuple[str, ...]:+ """Normalized content tokens, order preserved.++ Function words are dropped on BOTH sides — index and query. Dropping them+ from the query too is deliberate: they would otherwise inflate the+ query-mass denominator and dilute the real signal in a politely-phrased+ sentence, which is most of what a person actually says out loud.+ """+ return tuple(+ tok for tok in _TOKEN_RE.findall(normalize_text(text)) if is_content_token(tok)+ )+++def tokenize_with_spans(text: str) -> tuple[tuple[str, str], ...]:+ """``(normalized_token, raw_span)`` pairs over the ORIGINAL text.++ The raw span is what downstream guards need: the definitional-question+ guard escapes it into a pattern run against the untouched utterance, so a+ normalized token would blind it on every umlaut word.+ """+ out: list[tuple[str, str]] = []+ for match in re.finditer(r"\w+", text or "", re.UNICODE):+ raw = match.group(0)+ for tok in _TOKEN_RE.findall(normalize_text(raw)):+ if is_content_token(tok):+ out.append((tok, raw))+ return tuple(out)+++# ---------------------------------------------------------------------------+# Regex literal mining — the highest-leverage component+# ---------------------------------------------------------------------------++# Escapes that introduce a character CLASS rather than a literal character.+# Hitting one ends the current literal run.+_CLASS_ESCAPES = frozenset("swdbSWDBAZnrtfv")+++def mine_pattern_literals(pattern: str) -> tuple[str, ...]:+ """Extract literal phrase runs from an author-written voice regex.++ A single left-to-right pass. Anything that is not a plain literal ends the+ current run; the runs that survive are the phrases the author guaranteed+ the pattern contains.++ Hand-rolled on purpose: ``sre_parse`` / ``re._parser`` is a private stdlib+ API that was renamed in 3.11, and this project's ``requires-python``+ admits up to 3.14. A gate built on a private API is a version-skew brick+ waiting to happen (AP-28), and this function is load-bearing enough that a+ crash here would take out the vocabulary of every skill at once.++ Worked example (a real builtin trigger, quoted as matching data): <!-- i18n-allow -->+ ``(morgenroutine|morning routine|tages(ue|ue)berblick)`` yields the runs+ ``morgenroutine``, ``morning routine``, ``tages`` and ``berblick``. The+ compound split at the group boundary is expected: the trigram channel is+ what recovers it.+ """+ runs: list[str] = []+ current: list[str] = []+ index = 0+ length = len(pattern or "")++ def flush() -> None:+ if current:+ runs.append("".join(current))+ current.clear()++ while index < length:+ char = pattern[index]++ if char == "\\":+ nxt = pattern[index + 1] if index + 1 < length else ""+ if nxt and nxt not in _CLASS_ESCAPES:+ # An escaped metacharacter is a literal: \. \- \( \+ ...+ current.append(nxt)+ else:+ flush()+ index += 2+ continue++ if char == "[":+ flush()+ index += 1+ # Skip to the matching ']', honouring escapes and a leading ']'.+ if index < length and pattern[index] == "^":+ index += 1+ if index < length and pattern[index] == "]":+ index += 1+ while index < length and pattern[index] != "]":+ index += 2 if pattern[index] == "\\" else 1+ index += 1+ continue++ if char == "{":+ # A quantifier such as {0,2}: it repeats what came before and adds+ # no literal of its own.+ flush()+ while index < length and pattern[index] != "}":+ index += 1+ index += 1+ continue++ if char in "?*+":+ # The preceding single character is optional or repeated, so it is+ # not guaranteed: drop it, then end the run.+ if current:+ current.pop()+ flush()+ index += 1+ continue++ if char in "()|.^$":+ flush()+ index += 1+ continue++ current.append(char)+ index += 1++ flush()+ return tuple(run.strip() for run in runs if run.strip())+++# ---------------------------------------------------------------------------+# Trigrams+# ---------------------------------------------------------------------------+++def trigrams(text: str) -> frozenset[str]:+ """Space-padded character trigrams of a normalized surface."""+ padded = f" {normalize_text(text).strip()} "+ if len(padded) < 3:+ return frozenset()+ return frozenset(padded[i : i + 3] for i in range(len(padded) - 2))+++def _query_trigrams(text: str) -> frozenset[str]:+ """Trigrams of the long tokens only — word boundaries must survive.++ Trigramming the whole sentence would blur across word boundaries and make+ every skill look vaguely similar to every utterance.+ """+ out: set[str] = set()+ for token in tokenize(text):+ if len(token) >= TRIGRAM_MIN_TOKEN_LEN:+ out |= trigrams(token)+ return frozenset(out)+++# ---------------------------------------------------------------------------+# Records+# ---------------------------------------------------------------------------+++@dataclass(frozen=True, slots=True)+class TermHit:+ """One utterance token that matched a skill, and what it was worth."""++ token: str+ field: str+ raw_span: str+ contribution: float+++@dataclass(frozen=True, slots=True)+class ScoredSkill:+ """A skill and its relevance score for one utterance."""++ name: str+ score: float+ token_score: float = 0.0+ trigram_score: float = 0.0+ hits: tuple[TermHit, ...] = ()++ @property+ def reason(self) -> str:+ """Human-readable explanation, strongest signal first.++ Rendered directly in UI search results and the Test-Match panel, so it+ names the matched token AND the field it came from — "matched name,+ description" (the old wording) never told anyone why.+ """+ if not self.hits:+ return ""+ best: dict[str, TermHit] = {}+ for hit in self.hits:+ existing = best.get(hit.token)+ if existing is None or hit.contribution > existing.contribution:+ best[hit.token] = hit+ ordered = sorted(best.values(), key=lambda h: (-h.contribution, h.token))+ return " · ".join(f"{hit.field}: {hit.token}" for hit in ordered[:4])++ @property+ def evidence(self) -> str:+ """RAW span of the strongest matched token, as it appeared in the text.++ Never normalized — the definitional-question guard re-escapes this+ against the original utterance.+ """+ if not self.hits:+ return ""+ return max(self.hits, key=lambda h: h.contribution).raw_span+++@dataclass(frozen=True, slots=True)+class RelevanceRanking:+ """Ranked skills for one utterance, plus the corpus-derived cut-offs."""++ ranked: tuple[ScoredSkill, ...] = ()+ margin: float = 0.0+ clear_winner: bool = False+ fire_threshold: float = 0.0+ hint_threshold: float = 0.0++ @property+ def top(self) -> ScoredSkill | None:+ return self.ranked[0] if self.ranked else None+++# ---------------------------------------------------------------------------+# The index+# ---------------------------------------------------------------------------++#: ``(skill_index, field_weight, field_name)``+_Posting = tuple[int, float, str]+++@dataclass(frozen=True, slots=True)+class SkillMatchIndex:+ """An inverted index over the skill corpus.++ Immutable and self-contained — no registry reference, no paths, no file+ handles. That is what lets it be handed to ``turn_planner.plan_turn``,+ whose contract promises no model call, no disk access and no network.+ """++ names: tuple[str, ...] = ()+ postings: dict[str, tuple[_Posting, ...]] = dc_field(default_factory=dict)+ idf: dict[str, float] = dc_field(default_factory=dict)+ idf_unknown: float = 0.0+ #: trigram -> skill indices containing it. A posting list rather than a+ #: per-skill set, because the trigram channel MUST be able to surface a+ #: skill the token channel missed entirely — that is its entire purpose+ #: (a German compound the miner split, e.g. "tagesueberblick" against the+ #: indexed "tages" + "berblick"). Deriving trigram candidates only from+ #: skills that already scored on tokens would make it decorative.+ trigram_postings: dict[str, tuple[int, ...]] = dc_field(default_factory=dict)++ @property+ def size(self) -> int:+ return len(self.names)++ @property+ def fire_threshold(self) -> float:+ """Score at or above which a single skill may capture a turn.++ Derived from a corpus statistic — "one maximally-distinctive name token+ in a short utterance" — rather than being a bare float, so the same+ coefficient stays meaningful as the corpus grows. At N=22 the reference+ value is ~1.73; at N=60, ~1.77. That flatness is the point.+ """+ reference = (+ FIELD_WEIGHTS[FIELD_NAME] * self.idf_unknown+ ) / (self.idf_unknown + QUERY_MASS_FLOOR)+ return FIRE_COEFFICIENT * reference++ @property+ def hint_threshold(self) -> float:+ """Score at or above which a skill is worth showing the model."""+ reference = (+ FIELD_WEIGHTS[FIELD_NAME] * self.idf_unknown+ ) / (self.idf_unknown + QUERY_MASS_FLOOR)+ return HINT_COEFFICIENT * reference++ # -- query ------------------------------------------------------------++ def rank(self, text: str, *, limit: int = 5) -> RelevanceRanking:+ """Score every skill the utterance touches, best first.++ Cost is O(tokens in ``text``) for the token channel plus a fixed+ trigram pass over the top :data:`TRIGRAM_CANDIDATES`; it does not grow+ with the number of installed skills.+ """+ if not text or not self.names:+ return RelevanceRanking(+ fire_threshold=self.fire_threshold,+ hint_threshold=self.hint_threshold,+ )++ spans = tokenize_with_spans(text)+ if not spans:+ return RelevanceRanking(+ fire_threshold=self.fire_threshold,+ hint_threshold=self.hint_threshold,+ )++ # First raw span per normalized token (for the evidence field).+ raw_of: dict[str, str] = {}+ for token, raw in spans:+ raw_of.setdefault(token, raw)+ unique_tokens = tuple(raw_of)++ # Query mass: how much information the utterance carries in total.+ # Normalizing by THIS (rather than by document length) makes the score+ # mean "what fraction of the utterance does this skill explain?" —+ # precise on short commands, conservative on long prose. Document-length+ # normalization would instead punish a well-documented skill for being+ # well-documented.+ mass = sum(self.idf.get(tok, self.idf_unknown) for tok in unique_tokens)+ denominator = mass + QUERY_MASS_FLOOR++ raw_scores: dict[int, float] = {}+ hits: dict[int, list[TermHit]] = {}+ for token in unique_tokens:+ postings = self.postings.get(token)+ if not postings:+ continue+ weight_of_token = self.idf.get(token, 0.0)+ if weight_of_token <= 0.0:+ continue+ for skill_index, field_weight, field in postings:+ # ``build_index`` already collapsed each (token, skill) pair to+ # its single strongest field, so this is max-over-fields, never+ # sum. Summing would let a skill repeating "gmail" in four+ # fields quadruple-count one utterance word — the "verbose+ # skill wins everything" failure.+ contribution = weight_of_token * field_weight+ hits.setdefault(skill_index, []).append(+ TermHit(+ token=token,+ field=field,+ raw_span=raw_of[token],+ contribution=contribution,+ )+ )+ raw_scores[skill_index] = raw_scores.get(skill_index, 0.0) + contribution++ token_scores = {+ index: value / denominator for index, value in raw_scores.items()+ }++ # Trigram pass. This is what recovers German compounds+ # ("tagesueberblick" against the indexed "tages" + "berblick"),+ # inflection and STT garble — cases no token-exact channel can reach.+ # Candidates come from the trigram postings, NOT from the token+ # winners, so a skill the token channel missed completely can still+ # surface. Cost stays O(query trigrams).+ query_tri = _query_trigrams(text)+ trigram_scores: dict[int, float] = {}+ if query_tri:+ overlaps: dict[int, int] = {}+ for gram in query_tri:+ for index in self.trigram_postings.get(gram, ()):+ overlaps[index] = overlaps.get(index, 0) + 1+ # Longest content token, used as the evidence span for a+ # trigram-only match so the downstream definitional guard still has+ # a raw span to work with.+ longest = max(raw_of.items(), key=lambda kv: len(kv[0]))+ leaders = sorted(overlaps, key=lambda i: (-overlaps[i], self.names[i]))+ for index in leaders[:TRIGRAM_CANDIDATES]:+ # Containment-shaped, not Jaccard: Jaccard would punish a skill+ # for having many triggers even when it fully contains the+ # utterance's trigrams.+ trigram_scores[index] = overlaps[index] / (len(query_tri) + TRIGRAM_FLOOR)+ if index not in hits:+ hits[index] = [+ TermHit(+ token=longest[0],+ field="trigram",+ raw_span=longest[1],+ contribution=0.0,+ )+ ]++ if not token_scores and not trigram_scores:+ return RelevanceRanking(+ fire_threshold=self.fire_threshold,+ hint_threshold=self.hint_threshold,+ )++ scored = [+ ScoredSkill(+ name=self.names[index],+ score=token_scores.get(index, 0.0)+ + TRIGRAM_WEIGHT * trigram_scores.get(index, 0.0),+ token_score=token_scores.get(index, 0.0),+ trigram_score=trigram_scores.get(index, 0.0),+ hits=tuple(+ sorted(hits.get(index, ()), key=lambda h: (-h.contribution, h.token))+ ),+ )+ for index in set(token_scores) | set(trigram_scores)+ ]+ # Name as the tie-break keeps the ranking deterministic regardless of+ # registry iteration order or PYTHONHASHSEED.+ scored.sort(key=lambda s: (-s.score, s.name))++ margin = scored[0].score - scored[1].score if len(scored) > 1 else scored[0].score+ clear = margin >= max(MARGIN_ABS, MARGIN_REL * scored[0].score)++ return RelevanceRanking(+ ranked=tuple(scored[: max(1, limit)]),+ margin=margin,+ clear_winner=clear,+ fire_threshold=self.fire_threshold,+ hint_threshold=self.hint_threshold,+ )+++# ---------------------------------------------------------------------------+# Building+# ---------------------------------------------------------------------------+++def _skill_surfaces(skill: Any) -> list[tuple[str, str]]:+ """``(field, text)`` pairs contributing this skill's vocabulary."""+ out: list[tuple[str, str]] = [(FIELD_NAME, str(getattr(skill, "name", "") or ""))]+ frontmatter = getattr(skill, "frontmatter", None)+ if frontmatter is None:+ return out++ for trigger in getattr(frontmatter, "triggers", ()) or ():+ if getattr(trigger, "type", "") != "voice":+ continue+ pattern = getattr(trigger, "pattern", "") or ""+ for literal in mine_pattern_literals(pattern):+ out.append((FIELD_TRIGGER, literal))++ for attribute in ("intent_verbs", "intent_objects"):+ for value in getattr(frontmatter, attribute, ()) or ():+ out.append((FIELD_INTENT, str(value)))++ for tag in getattr(frontmatter, "tags", ()) or ():+ out.append((FIELD_TAG, str(tag)))++ out.append((FIELD_WHEN, str(getattr(frontmatter, "when_to_use", "") or "")))+ out.append((FIELD_DESC, str(getattr(frontmatter, "description", "") or "")))+ out.append((FIELD_CATEGORY, str(getattr(frontmatter, "category", "") or "")))+ return out+++#: Surfaces the trigram channel is built from. Description and when_to_use are+#: deliberately excluded: their English prose would flood the trigram set and+#: destroy its ability to discriminate.+_TRIGRAM_FIELDS = frozenset({FIELD_NAME, FIELD_TRIGGER, FIELD_INTENT, FIELD_TAG})+++def _skill_mtime(skill: Any) -> float:+ """Last-modified time used for eviction; 0.0 when unknown.++ Mirrors ``prompt_injection._skill_mtime`` so the prompt listing and the+ match index evict the same skills under pressure.+ """+ try:+ return float(skill.path.stat().st_mtime)+ except Exception: # noqa: BLE001+ try:+ return float(getattr(skill, "mtime", 0.0))+ except Exception: # noqa: BLE001+ return 0.0+++def build_index(skills: list[Any]) -> SkillMatchIndex:+ """Build the inverted index. Pure: same skills in, same index out."""+ selected = list(skills or ())+ if len(selected) > MAX_INDEXED_SKILLS:+ log.warning(+ "skill match index capped at %d of %d skills (evicting oldest by mtime)",+ MAX_INDEXED_SKILLS,+ len(selected),+ )+ selected.sort(key=_skill_mtime, reverse=True)+ selected = selected[:MAX_INDEXED_SKILLS]++ names: list[str] = []+ bags: list[dict[str, tuple[float, str]]] = []+ tri_sets: list[frozenset[str]] = []++ for skill in selected:+ name = str(getattr(skill, "name", "") or "")+ if not name:+ continue+ bag: dict[str, tuple[float, str]] = {}+ skill_tri: set[str] = set()+ for field, text in _skill_surfaces(skill):+ if not text:+ continue+ weight = FIELD_WEIGHTS[field]+ for token in tokenize(text):+ current = bag.get(token)+ if current is None or weight > current[0]:+ bag[token] = (weight, field)+ if field in _TRIGRAM_FIELDS and len(skill_tri) < MAX_TRIGRAMS_PER_SKILL:+ skill_tri |= trigrams(text)+ if len(bag) > MAX_TOKENS_PER_SKILL:+ # Keep the heaviest-weighted terms; a pathologically long+ # when_to_use must not dominate the posting lists.+ kept = sorted(bag.items(), key=lambda kv: (-kv[1][0], kv[0]))+ bag = dict(kept[:MAX_TOKENS_PER_SKILL])+ names.append(name)+ bags.append(bag)+ tri_sets.append(frozenset(sorted(skill_tri)[:MAX_TRIGRAMS_PER_SKILL]))++ total = len(names)+ if total == 0:+ return SkillMatchIndex(names=(), postings={}, idf={}, idf_unknown=0.0)++ document_frequency: dict[str, int] = {}+ for bag in bags:+ for token in bag:+ document_frequency[token] = document_frequency.get(token, 0) + 1++ # log((N + 1) / (df + 0.5)) — deliberately NOT BM25's+ # log((N - df + 0.5) / (df + 0.5)), which goes negative once df > N/2. At+ # N ~ 25 that would make a common word actively SUBTRACT score, which is+ # wrong for a "does this turn signal this skill at all" gate.+ common_cutoff = (+ CORPUS_COMMON_FRACTION * total+ if total >= CORPUS_COMMON_MIN_SKILLS+ else float("inf")+ )+ idf: dict[str, float] = {}+ for token, frequency in document_frequency.items():+ if frequency >= common_cutoff:+ idf[token] = 0.0+ else:+ idf[token] = math.log((total + 1) / (frequency + 0.5))+ idf_unknown = math.log((total + 1) / 0.5)++ postings: dict[str, list[_Posting]] = {}+ for index, bag in enumerate(bags):+ for token, (weight, field) in bag.items():+ postings.setdefault(token, []).append((index, weight, field))++ trigram_postings: dict[str, list[int]] = {}+ for index, tri_set in enumerate(tri_sets):+ for gram in tri_set:+ trigram_postings.setdefault(gram, []).append(index)++ return SkillMatchIndex(+ names=tuple(names),+ postings={token: tuple(entries) for token, entries in postings.items()},+ idf=idf,+ idf_unknown=idf_unknown,+ trigram_postings={+ gram: tuple(entries) for gram, entries in trigram_postings.items()+ },+ )+++# ---------------------------------------------------------------------------+# Cache+# ---------------------------------------------------------------------------++_CACHE: dict[tuple[int, int, bool], SkillMatchIndex] = {}+++def _source_skills(registry: Any, include_inactive: bool) -> list[Any]:+ """Which skills the index sees.++ The voice index is built from ``list_active()`` ONLY, so a DRAFT skill is+ not merely filtered out at score time — it is not in the index at all.+ Encoding AP-15 at BUILD time rather than query time is what makes it+ structural instead of a rule someone can forget to apply.+ """+ try:+ if include_inactive:+ return list(registry.list())+ return list(registry.list_active())+ except Exception: # noqa: BLE001+ return []+++def get_index(registry: Any, *, include_inactive: bool = False) -> SkillMatchIndex:+ """Return the cached index for ``registry``, building it on first use.++ Lazy on purpose (AP-26). Building eagerly inside the registry reload would+ put 8-15 ms of CPU on the deferred boot scan — a path that is critical by+ construction — and would rebuild on every watchdog fire while the user is+ typing in an open SKILL.md.++ No lock: :func:`build_index` is pure and a benign double-build under a race+ is far cheaper than letting the watchdog thread block a voice turn.+ """+ if registry is None:+ return SkillMatchIndex(names=(), postings={}, idf={}, idf_unknown=0.0)+ key = (id(registry), int(getattr(registry, "generation", 0)), include_inactive)+ cached = _CACHE.get(key)+ if cached is not None:+ return cached+ index = build_index(_source_skills(registry, include_inactive))+ # Bounded to the two live variants (voice + UI); dropping stale generations+ # keeps this from growing across reloads.+ _CACHE.clear()+ _CACHE[key] = index+ return index+++def clear_index_cache() -> None:+ """Drop every cached index (tests, and an explicit registry swap)."""+ _CACHE.clear()+++__all__ = [+ "FIELD_CATEGORY",+ "FIELD_DESC",+ "FIELD_INTENT",+ "FIELD_NAME",+ "FIELD_TAG",+ "FIELD_TRIGGER",+ "FIELD_WEIGHTS",+ "FIELD_WHEN",+ "MARGIN_ABS",+ "MARGIN_REL",+ "MAX_INDEXED_SKILLS",+ "MAX_TOKENS_PER_SKILL",+ "MAX_TRIGRAMS_PER_SKILL",+ "MIN_TOKEN_LEN",+ "RelevanceRanking",+ "ScoredSkill",+ "SkillMatchIndex",+ "TermHit",+ "build_index",+ "clear_index_cache",+ "get_index",+ "mine_pattern_literals",+ "normalize_text",+ "tokenize",+ "tokenize_with_spans",+ "trigrams",+]diff --git a/tests/unit/skills/test_relevance_mining.py b/tests/unit/skills/test_relevance_mining.pynew file mode 100644index 0000000000..4fc6b188ae--- /dev/null+++ b/tests/unit/skills/test_relevance_mining.py@@ -0,0 +1,245 @@+"""Golden tests for the regex literal miner.++``mine_pattern_literals`` is the highest-leverage and highest-risk part of the+relevance scorer: it turns every author-written voice trigger into vocabulary,+and a scanner bug would silently poison EVERY skill at once — no crash, no log+line, just a matcher that quietly stops working. So the expectations below were+derived BY HAND from the real builtin patterns before the function existed. If+you change the scanner and a case here flips, re-derive it by hand rather than+pasting in whatever the new code produces; the alternative is a test that only+asserts the code does what the code does.++The patterns are the actual ones shipped in jarvis/skills/builtin/*/SKILL.md.+"""+from __future__ import annotations++import re+from pathlib import Path++import pytest++from jarvis.skills.relevance import mine_pattern_literals, normalize_text, trigrams++# ---------------------------------------------------------------------------+# Hand-derived golden cases (real builtin patterns)+# ---------------------------------------------------------------------------++# (label, pattern, expected literal runs as a SET)+# Every German string is trigger vocabulary under test. # i18n-allow: speech input+_GOLDEN: tuple[tuple[str, str, set[str]], ...] = (+ (+ "morning-routine/voice",+ r"(morgenroutine|morgen[-\s]?briefing|morning routine|morning briefing"+ r"|start day|tages(ue|ü)berblick)", # i18n-allow: speech input+ {+ "morgenroutine", # i18n-allow: speech input+ "morgen", # i18n-allow: speech input+ "briefing",+ "morning routine",+ "morning briefing",+ "start day",+ "tages", # i18n-allow: speech input+ "ue",+ "ü", # i18n-allow: speech input+ "berblick", # i18n-allow: speech input+ },+ ),+ (+ "morning-routine/greeting",+ r"^(guten morgen|good morning)[.!\s]*$", # i18n-allow: speech input+ {"guten morgen", "good morning"}, # i18n-allow: speech input+ ),+ (+ "plugin-gmail",+ r"(gmail|postfach|posteingang|meine? mails?|neue mails?)", # i18n-allow: speech input+ {+ "gmail",+ "postfach", # i18n-allow: speech input+ "posteingang", # i18n-allow: speech input+ "mein", # i18n-allow: speech input+ "mail",+ "neue mail", # i18n-allow: speech input+ },+ ),+ (+ "plugin-github",+ r"(github|pull request|pull-request|\bpr\b|repo|repository)",+ {"github", "pull request", "pull-request", "pr", "repo", "repository"},+ ),+ (+ "plugin-google_calendar",+ r"(google.?(kalender|calendar)|gcal|(in )?(meine[nm]? )?"+ r"(kalender|termin|meeting))", # i18n-allow: speech input+ {+ "google",+ "kalender", # i18n-allow: speech input+ "calendar",+ "gcal",+ "in",+ "meine", # i18n-allow: speech input+ "termin", # i18n-allow: speech input+ "meeting",+ },+ ),+ (+ "deep-work-mode",+ r"^(deep[-\s]?work([-\s]?mode| modus)?|fokus[-\s]?modus"+ r"|konzentrations[-\s]?modus|starte (deep work|fokus(modus)?"+ r"|konzentration(smodus)?)|aktiviere (deep work|fokus(modus)?))$", # i18n-allow+ {+ "deep",+ "work",+ "mode",+ "modus", # i18n-allow: speech input+ "fokus", # i18n-allow: speech input+ "konzentrations", # i18n-allow: speech input+ "starte", # i18n-allow: speech input+ "deep work",+ "konzentration", # i18n-allow: speech input+ "smodus", # i18n-allow: speech input+ "aktiviere", # i18n-allow: speech input+ },+ ),+)++[email protected](("label", "pattern", "expected"), _GOLDEN, ids=[g[0] for g in _GOLDEN])+def test_mining_matches_the_hand_derived_expectation(+ label: str, pattern: str, expected: set[str]+) -> None:+ assert set(mine_pattern_literals(pattern)) == expected+++# ---------------------------------------------------------------------------+# Scanner rules, individually+# ---------------------------------------------------------------------------+++def test_alternation_and_groups_split_runs() -> None:+ assert set(mine_pattern_literals(r"(alpha|beta)")) == {"alpha", "beta"}+++def test_character_class_ends_a_run() -> None:+ """``deep[-\\s]?work`` guarantees "deep" and "work", not "deepwork"."""+ assert set(mine_pattern_literals(r"deep[-\s]?work")) == {"deep", "work"}+++def test_negated_and_literal_bracket_classes_are_skipped_whole() -> None:+ assert set(mine_pattern_literals(r"a[^]x]b")) == {"a", "b"}+ assert set(mine_pattern_literals(r"a[]x]b")) == {"a", "b"}+++def test_escaped_metacharacter_is_a_literal() -> None:+ assert set(mine_pattern_literals(r"a\.b")) == {"a.b"}+ assert set(mine_pattern_literals(r"c\-d")) == {"c-d"}+++def test_class_escape_ends_a_run() -> None:+ assert set(mine_pattern_literals(r"foo\sbar")) == {"foo", "bar"}+ assert set(mine_pattern_literals(r"\bpr\b")) == {"pr"}+++def test_optional_char_is_dropped_not_kept() -> None:+ """``meine?`` guarantees "mein"; the trailing "e" is optional."""+ assert set(mine_pattern_literals(r"meine?")) == {"mein"} # i18n-allow: speech input+++def test_star_and_plus_behave_like_optional() -> None:+ assert set(mine_pattern_literals(r"abcd*")) == {"abc"}+ assert set(mine_pattern_literals(r"abcd+")) == {"abc"}+++def test_brace_quantifier_contributes_no_literal() -> None:+ assert set(mine_pattern_literals(r"ab\w{0,2}cd")) == {"ab", "cd"}+++def test_anchors_and_dot_end_runs() -> None:+ assert set(mine_pattern_literals(r"^abc.def$")) == {"abc", "def"}+++def test_multi_token_run_survives_as_a_phrase() -> None:+ """Phrases are the point: "morning routine" is worth more than two words."""+ assert "morning routine" in mine_pattern_literals(r"(morning routine|x)")+++def test_whitespace_only_runs_are_dropped() -> None:+ assert set(mine_pattern_literals(r"(a| |b)")) == {"a", "b"}++[email protected]("pattern", ["", " ", "()", "|||", r"[", r"(unclosed"])+def test_degenerate_patterns_do_not_raise(pattern: str) -> None:+ assert isinstance(mine_pattern_literals(pattern), tuple)+++def test_mining_is_deterministic() -> None:+ pattern = r"(morgenroutine|morning routine|tages(ue|ü)berblick)" # i18n-allow: speech input+ first = mine_pattern_literals(pattern)+ assert all(mine_pattern_literals(pattern) == first for _ in range(20))+++# ---------------------------------------------------------------------------+# Smoke test over every REAL builtin pattern+# ---------------------------------------------------------------------------+++def _builtin_patterns() -> list[tuple[str, str]]:+ root = Path(__file__).resolve().parents[3] / "jarvis" / "skills" / "builtin"+ found: list[tuple[str, str]] = []+ for skill_file in sorted(root.rglob("SKILL.md")):+ text = skill_file.read_text(encoding="utf-8")+ for raw in re.findall(r"^\s*pattern:\s*(.+)$", text, re.M):+ cleaned = raw.split("#")[0].strip().strip("\"'")+ if cleaned:+ found.append((skill_file.parent.name, cleaned))+ return found+++def test_every_builtin_pattern_yields_usable_vocabulary() -> None:+ """A silent scanner failure on a real pattern is the nightmare case.++ Every shipped trigger must produce at least one token of length >= 3 —+ otherwise that skill contributes nothing to the index and can never be+ found by paraphrase, which is exactly the bug this system exists to fix.+ """+ patterns = _builtin_patterns()+ assert patterns, "no builtin trigger patterns found — the loader path moved"++ barren: list[str] = []+ for name, pattern in patterns:+ literals = mine_pattern_literals(pattern.replace("\\\\", "\\"))+ usable = [+ lit for lit in literals if len(normalize_text(lit).replace(" ", "")) >= 3+ ]+ if not usable:+ barren.append(f"{name}: {pattern}")+ assert not barren, "patterns that mine to nothing usable:\n" + "\n".join(barren)+++# ---------------------------------------------------------------------------+# Trigram channel — the compound recovery this whole design depends on+# ---------------------------------------------------------------------------+++def test_trigrams_recover_a_german_compound_split_by_the_scanner() -> None:+ """The load-bearing case for the trigram channel.++ The trigger "tages(ue|ue)berblick" mines to "tages" + "berblick" — the <!-- i18n-allow -->+ group boundary+ destroys the compound. A user saying the word as one token must still+ reach the skill, and only a character-level channel can do that.+ """+ spoken = trigrams("tagesueberblick") # i18n-allow: speech input+ indexed = trigrams("tages") | trigrams("berblick") # i18n-allow: speech input+ overlap = len(spoken & indexed)+ assert overlap / len(spoken) > 0.5++ unrelated = trigrams("gmail") | trigrams("postfach") # i18n-allow: speech input+ assert len(spoken & unrelated) / len(spoken) < 0.2+++def test_trigrams_are_umlaut_insensitive() -> None:+ assert trigrams("fähigkeit") == trigrams("faehigkeit") # i18n-allow: speech input+++def test_trigrams_of_a_too_short_surface_are_empty() -> None:+ assert trigrams("") == frozenset()diff --git a/tests/unit/skills/test_relevance_scorer.py b/tests/unit/skills/test_relevance_scorer.pynew file mode 100644index 0000000000..fdf90765ee--- /dev/null+++ b/tests/unit/skills/test_relevance_scorer.py@@ -0,0 +1,545 @@+"""Behaviour tests for the relevance scorer.++Several of these pin decisions that were made because a naive alternative was+MEASURED to be wrong against the real 20 builtin skills, not because they+sounded better in the abstract. Where that is the case the docstring says so —+those tests are the reason the constant has its value, and flipping one should+force a re-measurement rather than a re-tune.+"""+from __future__ import annotations++import time++import pytest++from jarvis.skills import relevance+from jarvis.skills.relevance import (+ FIELD_WEIGHTS,+ ScoredSkill,+ build_index,+ clear_index_cache,+ get_index,+ is_content_token,+ normalize_text,+ tokenize,+)+from jarvis.skills.schema import SkillLifecycleState++# German vocabulary used as fixtures throughout — the speech input under test.+# Declared once so the assertions stay readable and inside the line limit.+FOCUS = "fokusmodus" # i18n-allow: speech input+INBOX = "postfach" # i18n-allow: speech input+OVERVIEW = "tagesueberblick" # i18n-allow: speech input+OVERVIEW_UMLAUT = "Tagesüberblick" # i18n-allow: speech input+MESSAGE = "nachricht" # i18n-allow: speech input+OVERVIEW_PATTERN = r"(tages(ue|ü)berblick)" # i18n-allow: speech input++FUNCTION_WORD_SAMPLES = (+ "die", "ich", "muss", "meine", "mich", # i18n-allow: speech input+ "the", "your", "para", "cuando",+)+CONTENT_WORD_SAMPLES = (+ "mail", "kalender", "termin", "fokus", "repo", "work", "deep", # i18n-allow: speech input+)+++# ---------------------------------------------------------------------------+# Fakes+# ---------------------------------------------------------------------------+++class _FakeTrigger:+ def __init__(self, pattern: str, kind: str = "voice") -> None:+ self.type = kind+ self.pattern = pattern+++class _FakeFrontmatter:+ def __init__(+ self,+ *,+ description: str = "",+ when_to_use: str = "",+ category: str = "general",+ tags: list[str] | None = None,+ triggers: list[_FakeTrigger] | None = None,+ intent_verbs: list[str] | None = None,+ intent_objects: list[str] | None = None,+ ) -> None:+ self.description = description+ self.when_to_use = when_to_use+ self.category = category+ self.tags = tags or []+ self.triggers = triggers or []+ self.intent_verbs = intent_verbs or []+ self.intent_objects = intent_objects or []+++class _FakeSkill:+ def __init__(+ self,+ name: str,+ frontmatter: _FakeFrontmatter | None = None,+ state: SkillLifecycleState = SkillLifecycleState.ACTIVE,+ ) -> None:+ self.name = name+ self.frontmatter = frontmatter if frontmatter is not None else _FakeFrontmatter()+ self.state = state+++class _FakeRegistry:+ def __init__(self, skills: list[_FakeSkill], generation: int = 1) -> None:+ self._skills = skills+ self.generation = generation++ def list(self) -> list[_FakeSkill]:+ return list(self._skills)++ def list_active(self) -> list[_FakeSkill]:+ return [+ s+ for s in self._skills+ if s.state in (SkillLifecycleState.ACTIVE, SkillLifecycleState.VALIDATED)+ ]++[email protected](autouse=True)+def _clear_cache() -> None:+ clear_index_cache()+++# ---------------------------------------------------------------------------+# Normalization + tokenization+# ---------------------------------------------------------------------------+++def test_umlauts_fold_to_digraphs_not_bare_vowels() -> None:+ """The corpus is WRITTEN in digraph form, e.g. "tages(ue|ue)berblick".++ A plain NFKD strip would produce "uberblick" and fail to match the+ "ueberblick" the same pattern also offers — silently disabling the German+ half of the vocabulary.+ """+ assert normalize_text("Tagesüberblick") == "tagesueberblick" # i18n-allow: speech input+ assert normalize_text("Fähigkeit") == "faehigkeit" # i18n-allow: speech input+ assert normalize_text("groß") == "gross" # i18n-allow: speech input+++def test_spanish_accents_fold_without_a_language_branch() -> None:+ """Accents fold; punctuation is the tokenizer's job, not this function's."""+ assert tokenize("¿Qué canción favorita?") == ("cancion", "favorita")+++def test_short_tokens_are_dropped() -> None:+ assert "ab" not in tokenize("ab abc")+ assert "abc" in tokenize("ab abc")+++# ---------------------------------------------------------------------------+# Function words — the regression a live measurement found+# ---------------------------------------------------------------------------+++ "word",+ FUNCTION_WORD_SAMPLES,+)+def test_function_words_never_become_vocabulary(word: str) -> None:+ """Measured regression, 2026-07-25 (real 20-skill corpus).++ Corpus IDF alone does NOT neutralize these. Skill descriptions are English+ while when_to_use and triggers quote German, so a German function word+ appears in ONE skill and IDF scores it as maximally distinctive. Live+ effect before the fix: "die"/"ich" (idf 2.64 of 3.74) made morning-routine+ win "ich brauch jetzt ruhe zum arbeiten", and the mined trigger literal+ "meine" made plugin-google_calendar beat cli-gcloud on a Google Cloud+ billing question.+ """+ assert is_content_token(word) is False+ assert word not in tokenize(f"bitte {word} sofort") # i18n-allow: speech input+++def test_content_words_are_never_stoplisted() -> None:+ """The stoplist must stay a closed function-word class, never content."""+ for word in CONTENT_WORD_SAMPLES:+ assert is_content_token(word) is True+++def test_a_polite_sentence_does_not_dilute_the_signal() -> None:+ """Function words are dropped from the QUERY too, not just the index.++ Otherwise they inflate the query-mass denominator and a politely-phrased+ request scores lower than a terse one — which is backwards, because polite+ phrasing is most of what a person actually says out loud.+ """+ index = build_index(+ [+ _FakeSkill("focus", _FakeFrontmatter(tags=[FOCUS])),+ _FakeSkill("other", _FakeFrontmatter(tags=["gmail"])),+ ]+ )+ terse = index.rank(FOCUS)+ polite = index.rank(f"kannst du mir bitte mal den {FOCUS} machen")+ assert terse.top is not None and polite.top is not None+ assert polite.top.name == terse.top.name == "focus"+ # Not identical — "machen" is a content verb and legitimately adds mass —+ # but politeness alone must not halve the score.+ assert polite.top.score > terse.top.score * 0.5+++# ---------------------------------------------------------------------------+# IDF+# ---------------------------------------------------------------------------+++def test_a_term_in_every_skill_carries_no_weight() -> None:+ skills = [+ _FakeSkill(f"skill-{i}", _FakeFrontmatter(description="shared corpus term"))+ for i in range(10)+ ]+ index = build_index(skills)+ assert index.idf["shared"] == 0.0+ assert index.idf["corpus"] == 0.0+++def test_a_unique_term_carries_the_most_weight() -> None:+ skills = [_FakeSkill("alpha", _FakeFrontmatter(description="distinctive thing"))]+ skills += [+ _FakeSkill(f"s{i}", _FakeFrontmatter(description="common thing"))+ for i in range(4)+ ]+ index = build_index(skills)+ assert index.idf["distinctive"] > index.idf["common"] > 0.0+++def test_the_corpus_common_clamp_is_disabled_on_a_tiny_corpus() -> None:+ """A fresh install must still match.++ With three skills, "present in 60 % of the corpus" is TWO skills, so the+ clamp would zero out nearly every term and nothing could ever match — at+ exactly the moment a new user first tries a skill.+ """+ index = build_index(+ [_FakeSkill(f"s{i}", _FakeFrontmatter(tags=["shared"])) for i in range(3)]+ )+ assert index.idf["shared"] > 0.0+ assert index.rank("shared").ranked != ()+++def test_idf_is_never_negative() -> None:+ """BM25's log((N-df+0.5)/(df+0.5)) goes negative past df > N/2.++ At corpus sizes around 25 that would make a common word actively SUBTRACT+ score, which is wrong for a "does this turn signal this skill at all" gate.+ """+ skills = [+ _FakeSkill(f"s{i}", _FakeFrontmatter(description="everywhere token"))+ for i in range(30)+ ]+ skills.append(_FakeSkill("rare", _FakeFrontmatter(description="scarce token")))+ index = build_index(skills)+ assert all(value >= 0.0 for value in index.idf.values())+++# ---------------------------------------------------------------------------+# Scoring shape+# ---------------------------------------------------------------------------+++def test_field_weights_rank_name_and_trigger_highest() -> None:+ assert FIELD_WEIGHTS["name"] == FIELD_WEIGHTS["trigger"]+ assert FIELD_WEIGHTS["trigger"] > FIELD_WEIGHTS["intent"] > FIELD_WEIGHTS["tag"]+ assert FIELD_WEIGHTS["tag"] > FIELD_WEIGHTS["when_to_use"] > FIELD_WEIGHTS["category"]+++def test_repeating_a_term_across_fields_does_not_multiply_the_score() -> None:+ """max over fields, never sum — else the wordiest skill wins everything."""+ verbose = _FakeSkill(+ "verbose",+ _FakeFrontmatter(+ description="gmail gmail",+ when_to_use="gmail",+ tags=["gmail"],+ category="gmail",+ ),+ )+ focused = _FakeSkill("gmail", _FakeFrontmatter(description="unrelated words here"))+ index = build_index([verbose, focused, _FakeSkill("filler")])+ ranking = index.rank("gmail")+ assert ranking.top is not None+ assert ranking.top.name == "gmail", "the name match must beat four weak repeats"+++def test_a_long_utterance_scores_lower_than_a_terse_one() -> None:+ """Normalizing by QUERY mass, not document length.++ The score means "what fraction of this utterance does the skill explain?",+ so one hit inside a rambling sentence is worth less than the same hit in a+ direct command. Document-length normalization would instead punish a+ well-documented skill for being well-documented.+ """+ index = build_index(+ [+ _FakeSkill("focus", _FakeFrontmatter(tags=[FOCUS])),+ _FakeSkill("other", _FakeFrontmatter(tags=[INBOX])),+ ]+ )+ terse = index.rank(FOCUS)+ rambling = index.rank(+ f"{FOCUS} wetter berlin photosynthese rezept fahrrad urlaub steuer"+ )+ assert terse.top is not None and rambling.top is not None+ assert terse.top.score > rambling.top.score+++def test_trigram_channel_recovers_a_compound() -> None:+ """The load-bearing case: the scanner splits the compound, chars rejoin it."""+ index = build_index(+ [+ _FakeSkill(+ "morning-routine",+ _FakeFrontmatter(+ triggers=[_FakeTrigger(OVERVIEW_PATTERN)],+ ),+ ),+ _FakeSkill("plugin-gmail", _FakeFrontmatter(tags=[INBOX])),+ ]+ )+ ranking = index.rank(OVERVIEW)+ assert ranking.top is not None+ assert ranking.top.name == "morning-routine"+ assert ranking.top.trigram_score > 0.0+++def test_evidence_is_the_raw_span_from_the_utterance() -> None:+ """Guards re-escape this against the ORIGINAL text (see guards.py)."""+ index = build_index([_FakeSkill("focus", _FakeFrontmatter(tags=[OVERVIEW_UMLAUT]))])+ ranking = index.rank(f"gib mir den {OVERVIEW_UMLAUT}")+ assert ranking.top is not None+ assert ranking.top.evidence == OVERVIEW_UMLAUT+++def test_reason_names_the_token_and_the_field() -> None:+ index = build_index(+ [_FakeSkill("plugin-gmail", _FakeFrontmatter(tags=[INBOX]))]+ )+ ranking = index.rank(f"schau mal ins {INBOX}")+ assert ranking.top is not None+ assert f"tag: {INBOX}" in ranking.top.reason+++# ---------------------------------------------------------------------------+# Clear-winner margin+# ---------------------------------------------------------------------------+++def test_two_near_tied_skills_are_not_a_clear_winner() -> None:+ """Genuine ambiguity must degrade so the model disambiguates instead."""+ index = build_index(+ [+ _FakeSkill("plugin-slack", _FakeFrontmatter(tags=[MESSAGE])),+ _FakeSkill("plugin-discord", _FakeFrontmatter(tags=[MESSAGE])),+ ]+ )+ ranking = index.rank(f"schick eine {MESSAGE}")+ assert len(ranking.ranked) == 2+ assert ranking.clear_winner is False+++def test_an_unambiguous_hit_is_a_clear_winner() -> None:+ index = build_index(+ [+ _FakeSkill("focus", _FakeFrontmatter(tags=[FOCUS])),+ _FakeSkill("mail", _FakeFrontmatter(tags=[INBOX])),+ ]+ )+ ranking = index.rank(FOCUS)+ assert ranking.clear_winner is True+++def test_thresholds_stay_stable_as_the_corpus_grows() -> None:+ """Expressed against a corpus statistic, not as a bare float.++ A threshold tuned at 20 skills must not silently become wrong at 60.+ """+ small = build_index([_FakeSkill(f"s{i}") for i in range(20)])+ large = build_index([_FakeSkill(f"s{i}") for i in range(60)])+ drift = abs(small.fire_threshold - large.fire_threshold) / small.fire_threshold+ assert drift < 0.15, "tripling the corpus must not move the cut-off much"+ assert small.fire_threshold > small.hint_threshold > 0.0+++# ---------------------------------------------------------------------------+# Determinism+# ---------------------------------------------------------------------------+++def test_ranking_is_independent_of_registry_order() -> None:+ skills = [+ _FakeSkill(f"skill-{i}", _FakeFrontmatter(description=f"topic{i} shared word"))+ for i in range(12)+ ]+ baseline = build_index(list(skills)).rank("topic7 shared", limit=5)+ # Deterministic permutations (rotations + the reversal) rather than an RNG:+ # a flaky ordering test that only fails on some seeds is worse than none.+ permutations = [skills[i:] + skills[:i] for i in range(1, 5)]+ permutations.append(list(reversed(skills)))+ for shuffled in permutations:+ other = build_index(shuffled).rank("topic7 shared", limit=5)+ assert [s.name for s in other.ranked] == [s.name for s in baseline.ranked]+ assert [round(s.score, 9) for s in other.ranked] == [+ round(s.score, 9) for s in baseline.ranked+ ]+++def test_ties_break_by_name_for_a_stable_order() -> None:+ index = build_index(+ [+ _FakeSkill("zulu", _FakeFrontmatter(tags=["sharedterm"])),+ _FakeSkill("alpha", _FakeFrontmatter(tags=["sharedterm"])),+ _FakeSkill("other", _FakeFrontmatter(tags=["unrelated"])),+ ]+ )+ ranking = index.rank("sharedterm")+ assert [s.name for s in ranking.ranked][:2] == ["alpha", "zulu"]+++# ---------------------------------------------------------------------------+# Degradation + caps+# ---------------------------------------------------------------------------++[email protected]("text", ["", " ", "!!!", "ab"])+def test_empty_or_contentless_input_ranks_nothing(text: str) -> None:+ index = build_index([_FakeSkill("focus", _FakeFrontmatter(tags=[FOCUS]))])+ assert index.rank(text).ranked == ()+++def test_an_empty_corpus_ranks_nothing() -> None:+ assert build_index([]).rank("anything").ranked == ()+++def test_a_skill_without_frontmatter_still_contributes_its_name() -> None:+ broken = _FakeSkill("browser-tabs")+ broken.frontmatter = None+ index = build_index([broken, _FakeSkill("other")])+ ranking = index.rank("browser tabs")+ assert ranking.top is not None+ assert ranking.top.name == "browser-tabs"+++def test_a_broken_trigger_pattern_does_not_break_the_build() -> None:+ index = build_index(+ [+ _FakeSkill("broken", _FakeFrontmatter(triggers=[_FakeTrigger(r"([unclosed")])),+ _FakeSkill("focus", _FakeFrontmatter(tags=[FOCUS])),+ ]+ )+ ranking = index.rank(FOCUS)+ assert ranking.top is not None+ assert ranking.top.name == "focus"+++def test_the_skill_cap_is_enforced() -> None:+ many = [_FakeSkill(f"skill-{i}", _FakeFrontmatter(description=f"topic{i}")) for i in range(400)]+ index = build_index(many)+ assert index.size == relevance.MAX_INDEXED_SKILLS+++def test_the_per_skill_token_cap_is_enforced() -> None:+ wordy = _FakeSkill(+ "wordy",+ _FakeFrontmatter(when_to_use=" ".join(f"term{i}" for i in range(600))),+ )+ index = build_index([wordy])+ matched = sum(1 for postings in index.postings.values() if postings)+ assert matched <= relevance.MAX_TOKENS_PER_SKILL+++# ---------------------------------------------------------------------------+# Index cache + AP-15+# ---------------------------------------------------------------------------+++def test_a_draft_skill_is_not_in_the_voice_index_at_all() -> None:+ """AP-15 enforced at BUILD time, not filtered at query time.++ Structural beats procedural: a draft that is absent from the index cannot+ be fired by a future code path that forgets to check the state.+ """+ registry = _FakeRegistry(+ [+ _FakeSkill(+ "secret-draft",+ _FakeFrontmatter(tags=[FOCUS]),+ state=SkillLifecycleState.DRAFT,+ ),+ _FakeSkill("visible", _FakeFrontmatter(tags=[INBOX])),+ ]+ )+ voice = get_index(registry)+ assert "secret-draft" not in voice.names+ assert voice.rank(FOCUS).ranked == ()+++def test_the_ui_index_does_see_drafts() -> None:+ """The UI must find a draft in order to promote it — same code, one flag."""+ registry = _FakeRegistry(+ [+ _FakeSkill(+ "secret-draft",+ _FakeFrontmatter(tags=[FOCUS]),+ state=SkillLifecycleState.DRAFT,+ )+ ]+ )+ assert "secret-draft" in get_index(registry, include_inactive=True).names+++def test_the_index_is_cached_per_generation() -> None:+ registry = _FakeRegistry([_FakeSkill("alpha")])+ first = get_index(registry)+ assert get_index(registry) is first++ registry.generation += 1+ assert get_index(registry) is not first+++def test_a_missing_registry_yields_an_empty_index() -> None:+ assert get_index(None).size == 0+++# ---------------------------------------------------------------------------+# Performance floor+# ---------------------------------------------------------------------------++[email protected]("count", [60])+def test_query_cost_does_not_grow_with_the_corpus(count: int) -> None:+ """Guards against an accidental O(skills) rewrite of the posting lookup.++ Generous bounds — this is a shape check for CI, not a benchmark. Live+ measurement on the real 20-skill corpus is ~23 us per query.+ """+ skills = [+ _FakeSkill(+ f"skill-{i}",+ _FakeFrontmatter(description=f"topic{i} alpha beta", tags=[f"tag{i}"]),+ )+ for i in range(count)+ ]+ started = time.perf_counter()+ index = build_index(skills)+ build_seconds = time.perf_counter() - started+ assert build_seconds < 1.0++ started = time.perf_counter()+ for _ in range(1000):+ index.rank("topic7 alpha beta gamma delta")+ assert time.perf_counter() - started < 1.0+++def test_scored_skill_records_are_frozen() -> None:+ hit = ScoredSkill(name="x", score=1.0)+ with pytest.raises((AttributeError, TypeError)):+ hit.score = 2.0 # type: ignore[misc]