arborist/tests/test_ingest.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

119 lines
3.9 KiB
Python

"""End-to-end ingest test using a hand-rolled in-memory Source."""
from __future__ import annotations
from typing import Iterator
from arborist.document import Document, Edge
from arborist.ingest import ingest_source, verify_random_sample
from arborist.search import FTS5Backend
from arborist.search.base import AuditMode
from arborist.source import Source
from arborist.store import connect, stats
class FakeSource(Source):
source_type = "fake"
def __init__(self, docs: list[Document]):
self.docs = docs
def iter_documents(self) -> Iterator[Document]:
yield from self.docs
def _doc(uri: str, content: str, *, edges: list[Edge] | None = None) -> Document:
return Document(
uri=uri,
content=content,
source_type="fake",
title=uri.rsplit("/", 1)[-1],
edges=edges or [],
)
def test_ingest_basic_round_trip(tmp_path):
db_path = tmp_path / "test.db"
src = FakeSource([
_doc("test://a", "alpha bravo charlie delta echo foxtrot golf hotel"),
_doc("test://b", "the quick brown fox jumps over the lazy dog"),
_doc(
"test://c",
"merkle providence reverse rag verifies provenance",
edges=[Edge(edge_type="wikilink", dst_uri="test://a")],
),
])
conn = connect(db_path)
try:
result = ingest_source(conn, src)
assert result.seen == 3
assert result.inserted == 3
assert result.skipped_duplicate == 0
# Verify Merkle round-trip.
v = verify_random_sample(conn, n=3)
assert v["sampled"] == 3
assert v["passed"] == 3
assert v["failed"] == 0
# Idempotent re-ingest.
result2 = ingest_source(conn, src)
assert result2.inserted == 0
assert result2.skipped_duplicate == 3
# FTS5 search returns UNGROUNDED hits.
backend = FTS5Backend(conn)
hits = backend.search("merkle")
assert len(hits) >= 1
assert hits[0].audit_mode == AuditMode.UNGROUNDED
assert "merkle" in hits[0].snippet.lower()
# Edge resolution: c -> a should be backfilled (a was ingested first).
row = conn.execute(
"SELECT dst_root FROM edges WHERE dst_uri = ?", ("test://a",)
).fetchone()
assert row is not None
assert row["dst_root"] != "" # backfilled (was '' before resolution)
# Stats reflect ingest.
s = stats(conn)
assert s["documents_total"] == 3
assert s["documents_surface"] == 3
assert s["documents_core"] == 0
assert s["chunks_total"] >= 3
assert s["audit_events_total"] == 3 # one ingest event per doc
finally:
conn.close()
def test_audit_chain_links_correctly(tmp_path):
"""Each audit event chains to the previous via prev_event_hash."""
db_path = tmp_path / "audit.db"
src = FakeSource([_doc(f"test://{i}", f"document number {i} content") for i in range(5)])
conn = connect(db_path)
try:
ingest_source(conn, src)
rows = conn.execute(
"SELECT seq, event_hash, prev_event_hash FROM audit_events ORDER BY seq"
).fetchall()
assert len(rows) == 5
assert rows[0]["prev_event_hash"] is None # genesis
for i in range(1, len(rows)):
assert rows[i]["prev_event_hash"] == rows[i - 1]["event_hash"]
finally:
conn.close()
def test_chunker_version_persisted(tmp_path):
db_path = tmp_path / "chunker.db"
conn = connect(db_path)
try:
ingest_source(conn, FakeSource([_doc("test://x", "alpha beta gamma")]))
row = conn.execute(
"SELECT chunking_version, canonicalization_version, schema_version FROM documents"
).fetchone()
assert row["chunking_version"] == "tok-512-v1"
assert row["canonicalization_version"] == "norm-v1"
assert row["schema_version"] == "v9.8.0"
finally:
conn.close()