Skip to main content

Module lookup

Module lookup 

Source
Expand description

lookup records at run time (CMF_V2_SPEC §9.5.2; the lookup spec §3): an explicit key → card table appended to a sealed genome, reached through the request-level resonance router.

  • LookupTable — the four tensors skill.{id}.lookup.* of one record as the runtime reads them: sorted key hashes (binary search), entry per key, slot offsets, and the UTF-8 card blob left in the file’s mapping (zero-copy).
  • extract_key — O(len) key extraction from a user message: a Latin binomial in parentheses (the innermost group first), then a capitalised Genus species pair anywhere in the ORIGINAL message (capitalised_binomials), then every 1..=MAX_NGRAM-word n-gram of the cmf-key-v2-normalised message against the exact key index (the longest match wins, ties → the first occurrence), then the same n-grams STEMMED (stem_key: a light, deterministic Russian / English suffix stripper) against the table’s StemIndex. A STRONG candidate (is_strong_key: ≥ 2 real words, no digits) wins over any weak one, in every path — the router’s pick, key_first, the conversation memory; without one, the first weak candidate (a weak stem only when nothing matched exactly). The file stores only key hashes, so the stem index is built at open from the key texts the cards themselves spell (recover_key_texts): no format change.
  • LookupTable::find_answer_turns — conversation memory: the key is taken from the most recent of up to MEMORY_TURNS user turns that holds one; the field and the language come from the LAST turn.
  • select_field / pick_lang — the card field the question asks for (keyword rules) and the language of the answer (script).
  • LookupMode — answer (the table answers, no generation), context (the card — or, when a field was selected, its first sentence plus that field — is prepended to the message and the backbone generates), off (the table is ignored); flag or CMF_LOOKUP_MODE.
  • resolve_lookup — the one step every caller shares: a RouteDecision whose target is a lookup record becomes the lookup outcome; no key in the message, an empty card in every language, or mode off sends the request to the BACKBONE unchanged. A lookup record never has a lane of its own: the backbone pipeline — the same object F0 runs — serves it, so the logits of every non-hit request are bit-identical to F0’s.
  • LookupPolicy / resolve_lookup_gated — the record’s routing policy (LookupInfo.policy): router_and_key (the default) looks a key up only in what the φ router sent; key_first lets a STRONG key of the message (is_strong_key) take a request the router sent to the backbone — only on a file whose router could pick a skill at all (no fail-closed state). A one-word key (чай, мята) still needs the router; a message without a strong key keeps the router’s decision untouched. An unknown policy value reads as router_and_key.

Structs§

Card
One slot of the text blob: {"card": "...", "fields": {name: text}}.
KeyFirstGate
What the key_first policy needs to take a decision (resolve_lookup_gated): that the decision was the ROUTER’s own (a caller that pinned the target passes no gate — --route backbone, --skill none and a forced record are never overridden) and the conditions the router’s own pick of the record would have met.
KeyHit
A key found in a user message.
LookupAnswer
What the table answered.
LookupTable
One mounted lookup record.
LookupTables
The lookup tables of a file, opened on first use and kept (Sync: serve shares one behind its router).
StemIndex
The second, sorted hash index of a table: hashes of the STEMMED key texts (stem_key), each mapped to its entry. A stem two keys of DIFFERENT entries share is ambiguous and is left out (ambiguous); the same stem from several keys of one entry (ромашка аптечная, ромашки аптечной) is one row. Consulted only when the exact index finds nothing.

Enums§

DecidedBy
Who sent a request to a lookup record — decided_by in the route summary.
Found
LookupTable::find_answer: what the table found for a message.
KeySource
Where a key hit came from.
LookupMode
What a request routed to a lookup record does with the card.
LookupOutcome
What a routed request does once its target is known.
LookupPolicy
How a request reaches a lookup record (LookupInfo.policy, spec §9.5.2).
MatchVia
Which index answered: the exact cmf-key-v2 hashes (primary), or the stem index (StemIndex, consulted only when the primary found nothing). Reported as lookup_match in the route summary.

Constants§

CONTEXT_HEADER
The header line of a context-mode prompt.
EN_STEM_MIN
An English stem keeps at least this many letters.
FIELDS
The card fields select_field can name, in rule order (the first rule that matches wins): the specific ones before uses, which almost every question mentions in passing (safe to use, parts used, in what form is it used).
FIRST_SENTENCE_MAX
Card::first_sentence: the cap when the card has no terminator.
FIRST_SENTENCE_MIN
Card::first_sentence: a terminator this early is an abbreviation.
LATIN_STEM_MIN_WORD
A Latin-script word shorter than this is never stemmed (sage, uses, wort, Pinus stay as they are).
LOOKUP_MODE_ENV
Environment default of LookupMode (answer | context | off).
MAX_NGRAM
Longest n-gram (in words of the normalised message) tried as a key — LOOKUP_MAX_NGRAM of the core, the number the builder drops longer keys by.
MEMORY_TURNS
Conversation memory of serve (LookupTable::find_answer_turns): the key is searched in the last user message and up to this many user turns in total, most recent first.
RU_STEM_MIN
A Russian stem keeps at least this many letters (чай, вид, дуб are never shortened).
RU_SUFFIXES
Russian inflectional endings stem_word strips, longest first (adjective and noun endings of every case and number; й and ь so that зверобой / зверобоя and полынь / полыни meet).
STEM_UNIGRAM_MIN
A ONE-word stem candidate shorter than this (in letters) is not looked up: виды → вид, чая → чая never match by stem. Exact one-word matches are unaffected.
STRONG_KEY_MIN_WORDS
A strong key has at least this many words of ≥ STRONG_WORD_MIN_LETTERS letters (is_strong_key_text).
STRONG_WORD_MIN_LETTERS
Letters a word needs to count towards STRONG_KEY_MIN_WORDS (st, s, b, pb never do).

Functions§

capitalised_binomials
Every Genus species pair of the ORIGINAL (case-kept) message, in order: a capitalised Latin word of ≥ 3 letters followed by a lowercase Latin word of ≥ 3 letters, optionally hyphenated (nux-vomica: ≥ 3 and ≥ 2 letters around the hyphen); punctuation around the words is ignored ((Abies balsamea)?). The pairs are tried as EXACT keys only, before the n-grams; a genus alone is never tried (ambiguous). A capitalised English pair (What plant) is a candidate too — harmless, it is a key or it is not.
context_excerpt
The text context mode prepends for a card: the whole card when no field was selected; otherwise its first sentence and the field as {label}: {text} — a short excerpt that keeps the fact next to the question. A bounded-attention genome (swa_sink, window 128) would otherwise see the family line of a 1 000-char card hundreds of tokens before the question, beyond its window.
context_prompt
The user message the backbone generates from in context mode.
extract_key
extract_key_with against the exact index only (no stem index).
extract_key_with
The key of message, O(len): the STRONGEST key when the message holds one (extract_strong_key_with), else the first key of any strength. One rule for every path — the φ router’s pick of a table, the key_first policy and the conversation memory resolve one message to one entry (review KF-3: чай из ромашки аптечной is chamomile whoever decided, not tea on the router’s path and chamomile on key_first’s).
extract_strong_key_with
The STRONGEST key of message for the key_first policy: the same search as extract_key_with (the parenthesised group, the capitalised binomial, the exact n-grams, then the stem n-grams) over strong candidates only (is_strong_key_text) — so a one-word exact key never hides a two-word stem key of the same message (чай из ромашки аптечной). None = no strong key; the result always passes is_strong_key.
field_label
The human label of a card field in the slot language (context mode writes {label}: {text}); an unknown field or language keeps the field name.
has_cyrillic
Any Cyrillic letter in s.
is_strong_key
Is hit a STRONG key — one the key_first policy acts on without the router, and one every search prefers over a weak key of the same message? Its text passes is_strong_key_text, whatever its source: an exact or stem n-gram of ≥ 2 real words (ромашки аптечной, pot marigold), a parenthesised Latin group, a capitalised Genus species pair. The source does not make a key strong: every exact key a capitalised pair finds is the same text as an n-gram of the message, so a pair rule stricter than the n-gram rule would protect nothing (Common box in Why is Common box cutter so popular? is the n-gram key common box too — only a stop list removes it) and would only reorder candidates (a binomial the cards spell over the common name typed first: Spring vetchling (Lathyrus vernus (L.) Bernh.) would then answer from a stub entry of the corpus). A one-word key — чай, мята, календулы, a lone (Calendula) — never is strong: such words occur in general chat, and only the router’s decision may send them to a table.
is_strong_key_text
Is the normalised key text norm STRONG — a name the key_first policy may act on without the router (review KF-5)? No word with a digit (sts 135, pti 2, a 41988, 5f pb 22), and at least STRONG_KEY_MIN_WORDS purely alphabetic words of ≥ STRONG_WORD_MIN_LETTERS letters (ромашки аптечной, pot marigold, st john s wort; not thc b, not чай). A general phrase that happens to be a plant’s common name (scrambled eggs, gas plant, live forever) passes this rule — only the builder’s general probe and a stop list remove such a key (lookup-build --general-prompts, --drop-keys).
pick_lang
The slot language of the answer: ru for a Cyrillic question, else en; a language the record lacks falls back to en, then to the record’s first language. Returns the index into langs.
recover_key_texts
The key texts a table’s cards spell — the file stores only key hashes, so this is where the stem index gets its words from: every 1..=MAX_NGRAM-word n-gram of every slot’s text (the raw JSON of the card and its fields) whose hash the exact index knows, once per key, with the entry the INDEX maps it to (a card that mentions another entry’s plant recovers that plant’s key correctly). A key no card spells — a synonym only the corpus knew — is not recovered and has no stem; the exact index still serves it. O(blob) at open.
resolve_lookup
The lookup step after the routing decision: a target that is a lookup record becomes its outcome under mode. No key (or mode off) turns the decision into the BACKBONE with the reason kept; a hit keeps the skill target and notes the key. Any other target passes through as LookupOutcome::NotLookup. One user message, no memory. The decision is taken as given — the key_first policy needs to know how it was made (resolve_lookup_gated).
resolve_lookup_gated
resolve_lookup_turns with the records’ routing policy. key_first is Some when decision is the router’s own (see KeyFirstGate).
resolve_lookup_turns
resolve_lookup with conversation memory: turns as LookupTable::find_answer_turns takes them — the message routed on first, then the earlier user turns (serve passes up to MEMORY_TURNS).
select_field
The field a question asks for, by keyword (lookup spec §3), in FIELDS order: семейств|family → family; части|часть|частей|частям|частях|частью| частями|part|parts → parts (whole words: частуха is a plant, часто an adverb); веществ|соединен|состав|компонент|ингредиент| compound|constituent|ingredient|chemical → compounds; противопоказ| безопас|побочн|опасн|ядовит|токсич|contraindic|toxic|poison|safety| safe|risk|risks|side effect → safety; доказ|исследован|evidence| study|studies|research|clinical|trial|trials → evidence; форм| препарат|дозиров|preparation|dosage|form|forms|dose|doses → preparations; примен|use|uses|used|usage → uses. Stems match a word prefix, the short Latin words match whole words (so because is not use, information is not form). None = no field named: the whole card answers.
stem_key
stem_word of every word of a normalised key or message, joined by single spaces (the form both sides of the stem index hash).
stem_word
A light, deterministic stem of one normalised word — the same function on the keys (at open) and on the message (per query), so an inflected form meets its nominative key. Cyrillic: the longest ending of RU_SUFFIXES is stripped, repeatedly, while ≥ RU_STEM_MIN letters remain (тойона → тойон, магнолии → магнол, лекарственной → лекарственн, зверобоя → зверобо → звероб). Latin script, ≥ LATIN_STEM_MIN_WORD letters: ies → i, es after ss/x/z/ch/sh, or a final s (not after ss/us/is) is dropped when ≥ EN_STEM_MIN letters remain; otherwise a final y → i so that berry meets berries. Latin binomials mostly stay as they are (pinus, officinalis, balsamea). A word with a digit or another script is returned unchanged.