core/chat/pack_resolver.py
Shay a376a30bf8 feat(packs): en_core_meta_v1 — conversational substrate (73 lemmas)
Workstream 1 (pack content scale-up) first load-bearing step.

Adds a new ratified content pack covering the conversational vocabulary
en_core_cognition_v1 deliberately omits — speech acts, mental states,
perception, self-reference, and discourse-object nouns.  These are the
lemmas that show up in nearly every model response and that previously
fell through to the OOV invitation surface.

Pack composition (73 entries, 49 VERB + 24 NOUN):

  meta.speech_act.*     (20 verbs)  say tell speak reply claim state
                                    describe express name mention note
                                    observe declare assert deny confirm
                                    suggest propose articulate respond
  meta.mental_state.*   (18 verbs)  know believe think suppose assume
                                    expect hope want prefer doubt wonder
                                    guess recognize realize consider intend
                                    decide hold
  meta.perception.*     (11 verbs)  see hear feel sense perceive watch
                                    look listen find detect notice
  meta.self_reference.* (10 nouns)  self mind view perspective position
                                    role agent model system speaker
  meta.discourse.*      (14 nouns)  response reply statement fact idea
                                    point argument proposal suggestion
                                    case instance example kind type

Files:
  language_packs/data/en_core_meta_v1/
    lexicon.jsonl   — 73 entries, SHA-256 checksum-sealed
    manifest.json   — operational_base / D0 / checksum-verified
  chat/pack_resolver.py
    Appended en_core_meta_v1 to DEFAULT_RESOLVABLE_PACK_IDS after
    en_core_cognition_v1 so cognition lemma resolution stays first-
    match-wins on any future collision (preserves cognition-lane
    byte-identity invariant).
  core/config.py
    Added en_core_meta_v1 to RuntimeConfig.input_packs default mount.
  tests/test_en_core_meta_v1_pack.py
    11 contract tests: checksum-verified load, POS split, primary-
    domain namespace, no-collision-with-cognition-v1 regression gate,
    pack registration order, resolver routing, and cognition-lemma
    resolution unchanged.
  tests/test_procedure_surface.py
    Swapped two test fixtures from "claim" to "hypothesis".  ``claim``
    is now correctly pack-resident (meta.speech_act.claim) so the
    procedure composer's object-first selector picks it over the verb
    — the new behavior is semantically correct.  ``hypothesis`` is
    genuinely OOV across all mounted packs and preserves the verb-
    fallback contract these tests pin.

Authoring methodology:
  Four parallel subagents authored one cluster each from a strict
  exemplar + word list + forbidden-lemma list (every en_core_cognition_v1
  lemma listed explicitly to prevent collision).  Each subagent wrote
  only its cluster JSONL; the main pass assembled, validated, computed
  the SHA-256 over bytes-on-disk, and wrote the manifest.

Verification:
  Full lane: 2148 passed, 2 skipped, 0 failed (+11 new tests).
  Cognition eval byte-identical on both splits:
    public  100 / 100 / 91.7 / 100
    holdout 100 / 100 / 83.3 / 100
  Live runtime probes: fresh ChatRuntime() for "What is X?" with
  X ∈ {fact, doubt, statement, model, self} all emit a
  pack-grounded sentence from en_core_meta_v1.
  OOV path still honest for genuinely-unknown terms (e.g. hypothesis).

Scope note:
  This is one pack of ~70 lemmas, not "the model now articulates
  open-domain English."  The architecturally-honest articulation
  story still requires more pack and teaching-chain content; this
  pack moves the conversational-substrate boundary forward by ~70
  lemmas in one ratifiable, replay-stable step.
2026-05-19 05:38:12 -07:00

138 lines
5 KiB
Python

"""chat/pack_resolver.py — ADR-0063 cross-pack surface resolver.
The cold-start grounding composers in :mod:`chat.pack_grounding` were
hardcoded to a single lexicon pack (``en_core_cognition_v1``). That
asymmetry blocked ``en_core_relations_v1`` from being mounted on the
default runtime: mounting it would silently widen vault recall and
intent classification without a corresponding ratified surface
composer for kinship lemmas.
This module supplies the missing abstraction: a *deterministic*,
*first-match-wins* resolver that maps a lemma to ``(pack_id, semantic_domains)``
across an ordered tuple of mounted lexicon packs. Surface composers
consult the resolver instead of a single pack; the surface trust-boundary
tag follows the *resolving* pack id.
Design constraints (CLAUDE.md / Reconstruction-over-storage):
- The resolver loads each pack lexicon at most once per process via
:func:`functools.lru_cache`. Ratified packs are immutable, so caching
is sound.
- A pack that fails to load (missing file, unreadable JSON) contributes
an empty index — callers cannot distinguish "pack absent" from "lemma
absent in mounted packs". Both produce ``None``.
- First-match-wins on collision. The orthogonality test in
``tests/test_en_core_relations_v1_pack.py`` enforces zero overlap
today; this rule documents the deterministic resolution if a future
pack pair collides.
- No mutation of pack data is performed anywhere in this module. All
return types are tuples/frozensets/dicts of plain strings.
"""
from __future__ import annotations
import json
from functools import lru_cache
from pathlib import Path
# Default mounted lexicon-pack ids that ADR-0063 surface composers
# consult. Order matters: earlier packs win on lemma collision. This
# tuple is intentionally narrow — it lists only ratified ``en_core_*``
# *content* packs. Identity/safety/ethics packs are propositional
# overlays and never carry vocabulary; they are deliberately excluded.
DEFAULT_RESOLVABLE_PACK_IDS: tuple[str, ...] = (
"en_core_cognition_v1",
"en_core_meta_v1",
"en_core_relations_v1",
"en_core_relations_v2",
)
_PACK_ROOT = Path(__file__).resolve().parent.parent / "language_packs" / "data"
@lru_cache(maxsize=16)
def _pack_lexicon_for(pack_id: str) -> dict[str, tuple[str, ...]]:
"""Return ``{lemma_lower: semantic_domains}`` for *pack_id*, cached.
Returns an empty dict if the pack's lexicon file is missing,
unreadable, or contains no entries with ``semantic_domains``.
Ratified packs are immutable so the cache survives the process
lifetime.
"""
lexicon_path = _PACK_ROOT / pack_id / "lexicon.jsonl"
if not lexicon_path.exists():
return {}
out: dict[str, tuple[str, ...]] = {}
for line in lexicon_path.read_text(encoding="utf-8").splitlines():
line = line.strip()
if not line:
continue
try:
entry = json.loads(line)
except json.JSONDecodeError:
continue
if not isinstance(entry, dict):
continue
lemma = entry.get("lemma") or entry.get("surface")
if not lemma or not isinstance(lemma, str):
continue
domains = entry.get("semantic_domains", ())
if not isinstance(domains, (list, tuple)) or not domains:
continue
out[lemma.lower()] = tuple(str(d) for d in domains)
return out
def resolve_lemma(
lemma: str,
pack_ids: tuple[str, ...] = DEFAULT_RESOLVABLE_PACK_IDS,
) -> tuple[str, tuple[str, ...]] | None:
"""Return ``(pack_id, semantic_domains)`` for the first pack in
*pack_ids* whose lexicon contains *lemma*, else ``None``.
First-match-wins on collision. ``pack_ids`` defaults to
:data:`DEFAULT_RESOLVABLE_PACK_IDS`.
"""
if not lemma or not isinstance(lemma, str):
return None
key = lemma.strip().lower()
if not key:
return None
for pack_id in pack_ids:
index = _pack_lexicon_for(pack_id)
domains = index.get(key)
if domains:
return (pack_id, domains)
return None
def is_resolvable(
lemma: str,
pack_ids: tuple[str, ...] = DEFAULT_RESOLVABLE_PACK_IDS,
) -> bool:
"""Return True iff *lemma* resolves in any of *pack_ids*."""
return resolve_lemma(lemma, pack_ids) is not None
def mounted_lemmas(
pack_ids: tuple[str, ...] = DEFAULT_RESOLVABLE_PACK_IDS,
) -> frozenset[str]:
"""Return the frozen union of lemmas across *pack_ids*.
Used by topic extractors that iterate over user-text tokens and
need an O(1) "is this token any mounted lemma?" check.
"""
out: set[str] = set()
for pack_id in pack_ids:
out.update(_pack_lexicon_for(pack_id).keys())
return frozenset(out)
def clear_resolver_cache() -> None:
"""Drop the lru_cache for :func:`_pack_lexicon_for`.
Test-only escape hatch: enables fixture-based pack mutation
scenarios. Production code never calls this — ratified packs are
immutable.
"""
_pack_lexicon_for.cache_clear()