arborist/aborist/distill/base.py
russell@unturf.com 856b3116d7
phase 0 explore: aborist core + sources + distill + evict
A content-addressed, Merkle-committed document store implementing the
runtime spec from Merkle Providence Reverse RAG (April 2026 whitepaper)
and Merkle-AGI v9.8 admissibility ledger. Ports proxy.unturf.com Go
merkle conventions to Python: non-commutative HashCombine with 0x03
prefix, explicit IsLeft per sibling, self-duplicate odd elements.

What's in:

- merkle.py — proof generation/verification, JSON serialization
- store.py — v9.8 SQLite schema: 8-dim providence_cache key,
  falsification_state, append-only audit chain, surface/core kind,
  hot/warm/cold tier, derivations, edges
- ingest.py — Source -> normalize -> chunk -> merkle -> upsert,
  idempotent on document_root collision
- search/ — SearchBackend ABC with explicit AuditMode (STRICT/HYBRID/
  VISUAL), FTS5 backend returning VISUAL hits
- sources/ — wikipedia.py (streaming bz2/MySQL extended-INSERT parser
  for 2003-era cur dumps); html_page.py (selectolax + httpx, robots.txt
  honored automatically)
- distill/ — Distiller ABC + first-sentence-v1 stub. Runner generates
  per-contributing-chunk Merkle proofs binding cores back to source
  document_root.
- evict.py — hot->cold demote (NULLs content, drops FTS row, retains
  leaf_hash). rehydrate() refetches via source pipeline; matching root
  restores content, mismatching root marks providence stale and writes
  rehydrate_drift event. Cores never evict.
- cli.py — ingest / search / verify / stats / distill / evict /
  rehydrate
- 31 tests covering merkle round-trip, ingest+audit, chunker version
  binding, html parse, distillation proof verification, evict+
  rehydrate including drift detection.

Smoke: 503 Wikipedia 2003-05-16 + 3 fox-owned HTML pages ingested,
478 cores produced (24 surface->core merkle dedups), 7 chunks evicted
to cold and round-tripped via rehydrate, 987 audit events chained 0
breaks.
2026-04-27 07:53:18 -04:00

42 lines
1.2 KiB
Python

"""Distiller ABC.
A Distiller compresses a surface Document into a core Document. The runner
generates Merkle proofs for every contributing source chunk so the resulting
derivation row cryptographically binds the core back to its source.
"""
from __future__ import annotations
from abc import ABC, abstractmethod
from dataclasses import dataclass
from aborist.document import Document
@dataclass
class DistillationResult:
"""What a Distiller returns for one source.
contributing_chunk_indices: 0-based indices into source_chunks that fed
the core's content. The runner Merkle-proves each one against the
source's document_root and stores those proofs in derivations.proof_blob.
"""
core: Document
contributing_chunk_indices: list[int]
class Distiller(ABC):
"""Pure function: surface Document + its chunks -> core DistillationResult.
Distillers must be deterministic — same source bytes produce the same core
bytes. Bumping a distiller's algorithm requires bumping its `name`.
"""
name: str
@abstractmethod
def distill(
self, source: Document, source_chunks: list[str]
) -> DistillationResult:
...