arborist/tests/test_concepts.py
russell@unturf.com 8d6961fcc1
aborist/arborist
modified:   .gitlab-ci.yml
	modified:   bench/qa_questions.txt
	modified:   bench/qa_sweep.py
	modified:   bench/run.sh
	modified:   docs/TICKETS.md
	modified:   docs/_source/README.md
	modified:   docs/_source/_ext/makefile_targets.py
	modified:   docs/_source/api/cli.rst
	modified:   docs/_source/api/distill.rst
	modified:   docs/_source/api/mesh.rst
	modified:   docs/_source/api/qa.rst
	modified:   docs/_source/api/retrieval.rst
	modified:   docs/_source/api/storage.rst
	modified:   docs/_source/api/substrate.rst
	modified:   docs/_source/concepts.rst
	modified:   docs/_source/conf.py
	modified:   docs/_source/cookbook.rst
	modified:   docs/_source/index.rst
	modified:   docs/_source/license.rst
	modified:   docs/_source/quickstart.rst
	modified:   docs/bench-maxing.md
	modified:   docs/benchmarks.md
	modified:   docs/cti-architecture.md
	modified:   docs/diagrams/aborist-modules.dot
	modified:   docs/diagrams/aborist-modules.svg
	modified:   docs/diagrams/mesh-data-flow.dot
	modified:   docs/diagrams/mesh-epoch-lifecycle.dot
	modified:   docs/diagrams/mesh-epoch-lifecycle.svg
	modified:   docs/diagrams/mesh-group-decisions.dot
	modified:   docs/diagrams/mesh-group-decisions.svg
	modified:   docs/diagrams/mesh-identity-stack.dot
	modified:   docs/diagrams/mesh-secret-envelope.dot
	modified:   docs/mesh.md
	modified:   docs/qa-modes-bench.md
	modified:   docs/seven-point-program.md
	modified:   docs/tickets/ticket-000001-retrieval-keywords-audit-gap.md
	modified:   docs/tickets/ticket-000002-reference-frame-polarity-contract.md
	modified:   docs/tickets/ticket-000003-anchor-class-warrant.md
	modified:   docs/tickets/ticket-000005-label-ladder-migration.md
	modified:   docs/tickets/ticket-000006-bench-emergent-findings.md
	modified:   docs/tickets/ticket-000007-query-layer-hyphen-fold.md
	modified:   docs/tickets/ticket-000008-broad-quantifier-preflight-guard.md
	modified:   docs/tickets/ticket-000009-quantifier-preflight-dag-binding.md
	modified:   docs/tickets/ticket-000010-metacognition-preflight-guard.md
	modified:   docs/tickets/ticket-000011-soft-preflight-hint-sidecar.md
	modified:   scripts/backfill_concepts.py
	modified:   scripts/bench_emergent.py
	modified:   tests/crawler/test_async_web_fetcher.py
	modified:   tests/crawler/test_bridge.py
	modified:   tests/crawler/test_web_fetch.py
	modified:   tests/test_bench_qa_sweep.py
	modified:   tests/test_burn.py
	modified:   tests/test_burn_doc.py
	modified:   tests/test_claim_lattice.py
	modified:   tests/test_cli_render.py
	modified:   tests/test_compress.py
	modified:   tests/test_concepts.py
	modified:   tests/test_dag.py
	modified:   tests/test_directives.py
	modified:   tests/test_distill.py
	modified:   tests/test_distill_recursive.py
	modified:   tests/test_evict.py
	modified:   tests/test_frame.py
	modified:   tests/test_grok_source.py
	modified:   tests/test_html_source.py
	modified:   tests/test_ingest.py
	modified:   tests/test_inspect.py
	modified:   tests/test_journal.py
	modified:   tests/test_keys.py
	modified:   tests/test_llm_context_base.py
	modified:   tests/test_merkle.py
	modified:   tests/test_mesh.py
	modified:   tests/test_mesh_aead.py
	modified:   tests/test_mesh_chain.py
	modified:   tests/test_mesh_cli.py
	modified:   tests/test_mesh_cli_pull.py
	modified:   tests/test_mesh_wire.py
	modified:   tests/test_mesh_wire_e2e.py
	modified:   tests/test_metacognition.py
	modified:   tests/test_migration_audit_mode.py
	modified:   tests/test_providence_source.py
	modified:   tests/test_qa.py
	modified:   tests/test_qa_quality_live.py
	modified:   tests/test_quantifier_caps.py
	modified:   tests/test_quantifier_classifier.py
	modified:   tests/test_quantifier_phase4.py
	modified:   tests/test_quantifier_reminder.py
	modified:   tests/test_query.py
	modified:   tests/test_reclassify.py
	modified:   tests/test_repair.py
	modified:   tests/test_resume.py
	modified:   tests/test_snapshot.py
	modified:   tests/test_soft_preflight.py
	modified:   tests/test_tfidf.py
	modified:   tests/test_vcs_source.py
	modified:   tests/test_verify.py
	modified:   tests/test_verify_json.py
	modified:   tests/test_versioned_ingest.py
	modified:   tests/test_warrant.py
	modified:   tests/test_wikipedia_old.py
	modified:   tests/test_wikipedia_xml.py
	modified:   tests/test_wikitext.py
2026-05-07 09:31:49 -04:00

176 lines
6.1 KiB
Python

"""Concept overlay: synonym expansion + rivalry exclusion.
Pre-2026-05-01: data lived as Python frozensets, tests called the
helpers directly. Post-2026-05-01: data lives in per-shard SQLite
tables, the helpers walk shards via shards_dir. Each test seeds
a temporary shard dir using the legacy seed so the assertions stay
identical.
"""
from __future__ import annotations
from pathlib import Path
import pytest
from arborist.concepts import invalidate_cache as _invalidate_cache
from arborist.concepts.query import invalidate_cache
from arborist.concepts.seed import seed_legacy_concepts
from arborist.qa.concepts import (
has_compare_phrasing,
rivalry_excluded,
synonym_expand,
)
from arborist.store import connect
# ---------------------------------------------------------------------------
# Fixture: a tmp shards_dir containing one shard pre-seeded with legacy data.
# ---------------------------------------------------------------------------
@pytest.fixture
def seeded_shards(tmp_path: Path) -> Path:
shards_dir = tmp_path / "shards"
shards_dir.mkdir()
shard_path = shards_dir / "000.db"
conn = connect(shard_path)
seed_legacy_concepts(conn)
conn.close()
invalidate_cache()
return shards_dir
# ---------------------------------------------------------------------------
# synonym_expand
# ---------------------------------------------------------------------------
def test_synonym_expand_amd_pulls_athlon_and_back(seeded_shards):
expanded = synonym_expand({"athlon"}, shards_dir=seeded_shards)
assert "amd" in expanded
assert "duron" in expanded
assert "thunderbird" in expanded
expanded = synonym_expand({"amd"}, shards_dir=seeded_shards)
assert "athlon" in expanded
def test_synonym_expand_unrelated_token_unchanged(seeded_shards):
expanded = synonym_expand({"banana"}, shards_dir=seeded_shards)
assert expanded == {"banana"}
def test_synonym_expand_doesnt_cross_groups(seeded_shards):
"""AMD and Intel are in different groups — expanding one shouldn't
pull in the other."""
expanded = synonym_expand({"amd"}, shards_dir=seeded_shards)
assert "intel" not in expanded
assert "pentium" not in expanded
def test_synonym_expand_no_shards_dir_is_noop():
"""Without a shards_dir, expand returns the input unchanged.
Same degenerate behavior the legacy frozenset code had when
a query token didn't hit any group."""
assert synonym_expand({"athlon"}) == {"athlon"}
# ---------------------------------------------------------------------------
# rivalry_excluded
# ---------------------------------------------------------------------------
def test_rivalry_excluded_amd_query_drops_intel(seeded_shards):
excluded = rivalry_excluded({"amd"}, compare_phrasing=False, shards_dir=seeded_shards)
assert "intel" in excluded
assert "pentium" in excluded
def test_rivalry_excluded_intel_query_drops_amd(seeded_shards):
excluded = rivalry_excluded({"intel"}, compare_phrasing=False, shards_dir=seeded_shards)
assert "amd" in excluded
assert "athlon" in excluded
def test_rivalry_excluded_both_sides_no_exclusion(seeded_shards):
excluded = rivalry_excluded(
{"amd", "intel"}, compare_phrasing=False, shards_dir=seeded_shards
)
assert excluded == set()
def test_rivalry_excluded_compare_phrasing_suppresses(seeded_shards):
excluded = rivalry_excluded(
{"amd"}, compare_phrasing=True, shards_dir=seeded_shards
)
assert excluded == set()
def test_rivalry_excluded_no_shards_dir_is_noop():
"""Without shards_dir, no exclusion (caller hasn't loaded the
concept layer)."""
assert rivalry_excluded({"amd"}, compare_phrasing=False) == set()
# ---------------------------------------------------------------------------
# Compare-phrasing detection (independent of shards)
# ---------------------------------------------------------------------------
def test_compare_phrasing_detection():
assert has_compare_phrasing("compare AMD and Intel")
assert has_compare_phrasing("AMD vs Intel")
assert has_compare_phrasing("difference between Mac and Windows")
assert not has_compare_phrasing("what is the fastest AMD CPU?")
assert not has_compare_phrasing("tell me about Athlon")
def test_mac_windows_rivalry(seeded_shards):
"""Independent rivalry pair — Mac vs Windows."""
excluded = rivalry_excluded(
{"macintosh"}, compare_phrasing=False, shards_dir=seeded_shards
)
assert "windows" in excluded
excluded = rivalry_excluded(
{"windows"}, compare_phrasing=False, shards_dir=seeded_shards
)
assert "macintosh" in excluded
assert "macos" in excluded
# ---------------------------------------------------------------------------
# Brain-tech group (added 2026-05-01)
# ---------------------------------------------------------------------------
def test_brain_tech_synonym_group_includes_telepathy_and_thoughts(seeded_shards):
"""Run 1 retrieval-fix group: 'thoughts' should pull 'telepathy',
'neurotechnology', etc. so the title-relevance accept path admits
brain-tech pages."""
expanded = synonym_expand({"thoughts"}, shards_dir=seeded_shards)
assert "telepathy" in expanded
assert "neurotechnology" in expanded
assert "mind" in expanded
# ---------------------------------------------------------------------------
# Cache invalidation
# ---------------------------------------------------------------------------
def test_invalidate_cache_does_not_break_lookup(seeded_shards):
"""After explicit cache invalidation, a fresh load reproduces
the same answers."""
expanded_1 = synonym_expand({"amd"}, shards_dir=seeded_shards)
invalidate_cache()
expanded_2 = synonym_expand({"amd"}, shards_dir=seeded_shards)
assert expanded_1 == expanded_2
def test_export_invalidate_cache_alias(seeded_shards):
"""The package-level ``invalidate_cache`` is the same callable as
``arborist.concepts.query.invalidate_cache`` — exposed at top level
for callers that don't want to import the implementation module."""
assert _invalidate_cache is invalidate_cache