// Arborist module graph — top-level packages & their dependencies.
//
// Arrows point in the import direction (A → B means A imports from B).
// Cluster boxes group modules with shared concerns (storage, retrieval,
// federation, etc.).
//
// Render: dot -Tsvg arborist-modules.dot -o arborist-modules.svg

digraph arborist_modules {
    rankdir=LR
    node [shape=box, style="rounded,filled", fontname="Helvetica", fontsize=10]
    edge [fontname="Helvetica", fontsize=9]
    bgcolor="white"

    // ---- Substrate (pure crypto / serialization, no SQL) ----
    subgraph cluster_substrate {
        label="substrate"
        style="rounded,dashed"
        color="#999999"
        merkle    [label="merkle.py\n(Python port of Go merkle.go)", fillcolor="#fff7e6"]
        document  [label="document.py\nDocument · Edge · Chunker", fillcolor="#fff7e6"]
        wikitext  [label="wikitext.py\nto_base() prose-strip", fillcolor="#fff7e6"]
    }

    // ---- Storage layer ----
    subgraph cluster_storage {
        label="storage"
        style="rounded,dashed"
        color="#999999"
        store     [label="store.py\nv9.8 SQLite schema +\naudit chain helpers", fillcolor="#e6f0ff"]
        ingest    [label="ingest.py\nnormalize → chunk → merkle → upsert", fillcolor="#e6f0ff"]
        evict     [label="evict.py\nhot ↔ cold tier", fillcolor="#e6f0ff"]
    }

    // ---- Sources (corpus producers) ----
    subgraph cluster_sources {
        label="sources/"
        style="rounded,dashed"
        color="#999999"
        wikipedia [label="wikipedia.py\ncur + old SQL dumps", fillcolor="#e7ffe7"]
        wiki_xml  [label="wikipedia_xml.py\nphase IV iterparse", fillcolor="#e7ffe7"]
        html_page [label="html_page.py\nselectolax + httpx", fillcolor="#e7ffe7"]
        crawler   [label="crawler/\nasync HTML BFS", fillcolor="#e7ffe7"]
        grok      [label="grok.py\nxAI export", fillcolor="#e7ffe7"]
        vcs       [label="vcs.py\ngit + Mercurial", fillcolor="#e7ffe7"]
    }

    // ---- Retrieval & verifier ----
    subgraph cluster_retrieval {
        label="retrieval & verifier"
        style="rounded,dashed"
        color="#999999"
        search    [label="search/\nFTS5 backend + ABC", fillcolor="#ffe6f0"]
        concepts  [label="concepts/\nsynonym & rivalry overlay\n(per-shard concept_relations)", fillcolor="#ffe6f0"]
        qa_query  [label="qa/query.py\nmulti-source RAG", fillcolor="#ffe6f0"]
        qa_verify [label="qa/verify.py\nquote/span/entity/paraphrase\n+ claim_lattice (7 hard checks)", fillcolor="#ffe6f0"]
        qa_warrant[label="qa/warrant.py\n5 anchor classes\n(proper-noun · date · count\n · entity-list · cause)", fillcolor="#ffe6f0"]
        qa_evidence[label="qa/evidence.py\nEvidenceObject + spotlight", fillcolor="#ffe6f0"]
        qa_runner [label="qa/runner.py\nask(): single-doc Q&A", fillcolor="#ffe6f0"]
        qa_dag    [label="qa/dag.py\nper-run Merkle DAG\n(7/8 quote · 9/10 CTI · 3 reject)\npreflight_node_hash + 5 clauses", fillcolor="#ffe6f0"]
        qa_keys   [label="qa/keys.py\n8-dim cache_key + question_hash", fillcolor="#ffe6f0"]
        qa_inspect[label="qa/inspect.py\nsidecar diagnostic\n(read-only)", fillcolor="#ffe6f0"]
        // Preflight modules (Tickets #000008 + #000010)
        qa_quantifier        [label="qa/quantifier.py\n10-rung intensity classifier\n(#000008)", fillcolor="#ffe6f0"]
        qa_model_profiles    [label="qa/model_profiles.py\nper-model claim-cap profiles\n(#000008)", fillcolor="#ffe6f0"]
        qa_quantifier_reminder[label="qa/quantifier_reminder.py\nbroad-query reminder text\n(#000008)", fillcolor="#ffe6f0"]
        qa_metacognition     [label="qa/metacognition.py\nQuestionState + 4 detectors\n(temporal · contradiction ·\n false-premise · out-of-corpus)\n(#000010)", fillcolor="#ffe6f0"]
    }

    // ---- Distill (surface → core) ----
    distill   [label="distill/\nDistiller ABC + tfidf + first_sentence", fillcolor="#fff0e0"]

    // ---- Federation (off by default) ----
    mesh      [label="mesh/\nfederation: identity, roster,\nAEAD epoch secret, gossip wire", fillcolor="#f0e6ff"]

    // ---- Entry point ----
    cli       [label="cli.py\nargparse entrypoint\n(make targets call into here)", fillcolor="#dddddd", shape=note]

    // Substrate dependencies
    document   -> merkle
    store      -> merkle
    store      -> document
    ingest     -> store
    ingest     -> document

    // Sources -> ingest path
    wikipedia  -> ingest
    wiki_xml   -> ingest
    html_page  -> ingest  [label="parse_html →\nedges rows"]
    crawler    -> html_page
    grok       -> ingest
    vcs        -> ingest

    // Retrieval pipeline
    qa_query   -> search
    qa_query   -> concepts  [label="synonym_expand /\nrivalry_excluded"]
    qa_query   -> qa_keys
    qa_query   -> qa_verify
    qa_query   -> qa_evidence
    qa_query   -> qa_dag
    qa_query   -> wikitext  [label="to_base()"]
    qa_verify  -> qa_warrant
    qa_verify  -> qa_evidence
    qa_runner  -> qa_query
    qa_inspect -> qa_query  [style=dashed, label="read-only sidecar"]

    // Preflight wiring (#000008 + #000010 → #000009 DAG binding)
    qa_query   -> qa_quantifier        [label="classify_question_quantifier"]
    qa_query   -> qa_model_profiles    [label="cap_for_intensity"]
    qa_query   -> qa_quantifier_reminder [label="broad_quantifier_reminder"]
    qa_query   -> qa_metacognition     [label="preflight_question"]
    qa_runner  -> qa_quantifier
    qa_runner  -> qa_model_profiles
    qa_runner  -> qa_quantifier_reminder
    qa_runner  -> qa_metacognition
    qa_dag     -> qa_quantifier        [style=dotted, label="preflight clause"]
    qa_dag     -> qa_metacognition     [style=dotted, label="preflight clause"]

    // Concepts reads existing edges
    concepts   -> store     [label="reads edges,\nwrites concept_relations"]

    // Search reads chunks
    search     -> store

    // Distill consumes store, writes derivations
    distill    -> store
    distill    -> document

    // Eviction
    evict      -> store

    // Mesh
    mesh       -> store

    // CLI dispatches everywhere
    cli        -> ingest
    cli        -> qa_runner
    cli        -> qa_query
    cli        -> qa_inspect
    cli        -> distill
    cli        -> evict
    cli        -> mesh
    cli        -> concepts
}
