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

103 lines
3.6 KiB
Python

"""Resumable ingest: high-water mark in meta lets a stopped ingest rsync forward."""
from __future__ import annotations
from typing import Iterator
from arborist.document import Document
from arborist.ingest import ingest_source
from arborist.source import Source
from arborist.store import connect, get_meta, set_meta
class IndexedSource(Source):
"""Test source that exposes an `id` per doc and a start_id filter.
Imitates the WikipediaSqlDump resume contract.
"""
source_type = "indexed_test"
def __init__(self, n_docs: int = 10):
self.n_docs = n_docs
self.start_id = 0
self.last_id = 0
def iter_documents(self) -> Iterator[Document]:
for i in range(1, self.n_docs + 1):
if i <= self.start_id:
continue
self.last_id = i
yield Document(
uri=f"test://doc/{i}",
content=f"document number {i} content " * 10,
source_type=self.source_type,
title=f"Doc {i}",
extra={"indexed_test_id": str(i)},
)
def test_resume_writes_high_water(tmp_path):
db = tmp_path / "rh.db"
conn = connect(db)
try:
src = IndexedSource(n_docs=10)
ingest_source(conn, src, batch_size=5, resume=True)
# high-water now equals the last id seen
assert get_meta(conn, "source_high_water:indexed_test") == "10"
finally:
conn.close()
def test_resume_skips_processed(tmp_path):
db = tmp_path / "skip.db"
conn = connect(db)
try:
# Pre-seed a high-water as if a prior run got through doc 4.
with __import__("arborist").store.transaction(conn):
set_meta(conn, "source_high_water:indexed_test", "4")
src = IndexedSource(n_docs=10)
result = ingest_source(conn, src, batch_size=5, resume=True)
# Source should have skipped docs 1..4 entirely (start_id=4, then >4 emitted).
# Note: `seen` counts what the source yielded post-skip.
assert result.seen == 6
assert result.inserted == 6
# high-water now updated to 10
assert get_meta(conn, "source_high_water:indexed_test") == "10"
finally:
conn.close()
def test_resume_idempotent(tmp_path):
"""Running ingest twice with resume yields no new docs the second time."""
db = tmp_path / "idem.db"
conn = connect(db)
try:
ingest_source(conn, IndexedSource(n_docs=10), batch_size=3, resume=True)
n1 = conn.execute("SELECT COUNT(*) FROM documents").fetchone()[0]
ingest_source(conn, IndexedSource(n_docs=10), batch_size=3, resume=True)
n2 = conn.execute("SELECT COUNT(*) FROM documents").fetchone()[0]
assert n1 == n2 == 10
finally:
conn.close()
def test_resume_continues_after_kill(tmp_path):
"""Simulate a kill mid-batch: high-water is at the last successfully
flushed batch, restart picks up from there without dups or loss."""
db = tmp_path / "kill.db"
conn = connect(db)
try:
# First run hits 7 of 10, then "killed" — but with batch_size=5 the
# high-water should be 5 (one full batch flushed).
ingest_source(conn, IndexedSource(n_docs=5), batch_size=5, resume=True)
assert get_meta(conn, "source_high_water:indexed_test") == "5"
# Resume with the full source — should pick up at 6..10.
result = ingest_source(conn, IndexedSource(n_docs=10), batch_size=5, resume=True)
assert result.seen == 5
assert result.inserted == 5
n_total = conn.execute("SELECT COUNT(*) FROM documents").fetchone()[0]
assert n_total == 10
finally:
conn.close()