HuFu 虎符
虎符相合,方为可信。 — In ancient China, a tiger tally was split in two; only when the halves matched was an order authentic. HuFu does the same for data: it checks whether two pieces of data fit together — a query and a retrieved passage, a memory and a memory, a statement and a document.
HuFu is a lightweight consistency checker. Vector similarity says two texts are close; HuFu decides whether they are consistent — with explainable evidence and provable polynomial-time complexity. Symbolic structure (linguistic hypergraphs, hypergraph simulation, quality rules) makes the call; small neural models (embeddings, NLI) serve only as measurable inputs to the rules.
Quick start
pip install hufu # dependency-free reference core
pip install "hufu[models]" # text-level checking (spaCy, embeddings, NLI)
python -m spacy download en_core_web_trf
import hufu
# 1. RAG retrieval post-check: does each retrieved passage fit the query?
verdicts = hufu.check_retrieval(
"Where was the author of 'The Old Man and the Sea' born?",
[
"Ernest Hemingway, who wrote The Old Man and the Sea, was born in Oak Park, Illinois.",
"Paris is the capital of France and a major European city.",
],
)
for v in verdicts:
print(v.consistent, v.distance)
# 2. Vector-memory conflicts: same object, opposing actions?
conflicts = hufu.detect_memory_conflicts([
("Turn on the light", {"object": "light", "action": "on"}),
("Turn off the light", {"object": "light", "action": "off"}),
("Water the plants", {"object": "plants", "action": "water"}),
])
print(conflicts[0].labels, conflicts[0].explanations)
# 3. Training-data outliers: good / bad / ugly by measured impact
from hufu.scenarios import classify_outliers
reports = classify_outliers(records, labels, evaluator=train_and_score)
Every verdict carries evidence provenance and the full decision chain — HuFu explains why it flags something.
What it does
- RAG retrieval post-checker — sits between retrieval and generation in
any pipeline, vector-database agnostic; labels every retrieved document
consistent / conflictwith per-pair evidence and distance. - Document contradiction detection — star-graph rules over a document's own statements: typed value-slot swaps, numeric mismatches (with unit conversion), role reversals, antonym pairs, polarity flips.
- Vector-memory conflict detection & trimming — CDR-style rules for memory banks (same object, opposing actions/states/values), conflict inference from retrieval judgments, and evidence/index-level trimming so retrieval stops surfacing conflicts.
- Training-data outlier management — detectors, the good / bad / ugly classification by measured downstream impact, and a repair family (KNN/RF label repair, value imputation, deletion) with a small pipeline search.
- Unified quality-rule language — one JSON rule schema (logic, NLI, similarity, numeric/temporal predicates), pluggable via a registry. Rules are data: hand-write, version, or discover them offline.
Scope and limitations
HuFu is deliberately precision-first: it certifies structural contradictions with explainable evidence rather than chasing every semantic nuance.
- Reliable subdomain (default
profile="strict"): conflicts provable from structure alone — typed value-slot swaps (born in X vs Y, 300 vs 3000 men, May vs June 1883), role reversals on asymmetric verbs, antonym pairs, and polarity flips on an otherwise identical frame. - Out of scope: narrative tension, perspective/emotion shifts, paraphrase-level disagreement, and temporal progressions (a story legitimately giving the same attribute different values at different times). Real long-form narrative contains the same surface signatures as injected conflicts — a methods boundary, not an engineering gap.
In short: use HuFu to confirm contradictions on structured / semi-structured content (knowledge bases, fact tables, records, curated documents), as a consistency post-checker for RAG retrieval, and for vector-memory conflict detection. Long-form narrative contradiction hunting is explicitly out of scope.
Installation
pip install hufu # dependency-free reference core (Python ≥ 3.10)
pip install "hufu[models]" # + text-level checking (spaCy, sentence-transformers, fastcoref)
pip install "hufu[outliers]" # + sklearn-backed outlier classification/repair
python -m spacy download en_core_web_trf
Text-level checking can also use the Rust fixed-point accelerator
(simulation-alg) when a Rust toolchain is available:
pip install ./rust/simulation
Model configuration (environment variables)
| Variable | Default | Purpose |
|---|---|---|
HUFU_EMBED_MODEL |
sentence-transformers/all-MiniLM-L6-v2 |
sentence embedding model |
HUFU_NLI_MODEL |
cross-encoder/nli-deberta-v3-xsmall |
NLI cross-encoder |
HUFU_COREF_MODEL |
biu-nlp/lingmess-coref |
coreference model (HUFU_COREF=off disables) |
HUFU_SPACY_MODEL |
en_core_web_trf |
spaCy pipeline |
Repository layout
src/hufu/
api.py Public facade: check_pair / check_retrieval + reference core
backends.py Model-backed EmbeddingBackend adapters (lazy)
engine/ HyperMatch core + star-graph document engine + bridge
rules/ Unified quality-rule DSL: model, registry, predicates, engine
scenarios/ Ready-made scenarios (memory, documents, outliers, trim)
hypergraph/ Linguistic hypergraph construction, traversal, fusion
core/ QueryInstance, logging, text utilities
rust/
graph-simulation/ Rust graph-simulation crate
simulation/ maturin Python binding (simulation-alg)
tests/ Dependency-free engine and rule tests (runnable anywhere)
Development
pip install -e ".[dev]"
python -m unittest discover -s tests -v # no models or network needed
ruff check src tests
Citation
HuFu integrates several peer-reviewed works. If you use it, please cite:
% Catching Inconsistencies in Vector Similarity Retrieval (HyperMatch)
% Conflict-Aware Memory for Embodied Agents: Enhancing Vector Data Quality via Detection Rules (CDRs, ACL 2026)
% Detecting Contradictions Within and Across Documents (DocChecker)
% Outliers: The Good, the Bad and the Ugly (OHunt)
(Full BibTeX entries will be added with publication metadata.)
License
Apache-2.0 (see LICENSE). Existing source copyright notices are retained.
Metadata
Release files for hufu 0.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| hufu-0.5.0.tar.gz | 1.7 MB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| hufu-0.5.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 1.9 MB
Release files / hufu-0.5.0.tar.gz
| Download URL | hufu-0.5.0.tar.gz |
|---|---|
| Size | 1.7 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
50e9b560f3985c3cf12f976ede0ce9b1a21150d88d66dd0765a6544a1cd952c9
|
|
BLAKE2b-256 checksum How to use checksums |
8e2c372cc2c54d0e9d70078f0e5f530b02b0a6726946b2d503ca89547cb680a6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 9, 2026.
Transparency logRelease files / hufu-0.5.0-py3-none-any.whl
| Download URL | hufu-0.5.0-py3-none-any.whl |
|---|---|
| Size | 212.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
154b7bc4c48c6d6d9679acff5175b63f54a46f73e255c16757f0222db174faa7
|
|
BLAKE2b-256 checksum How to use checksums |
2427548ae871b0e71d95d3f9ff2a88000b8f3cdd46d5c8d825798df7a09d6e9d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 9, 2026.
Transparency log