# nazib-riasat/graft_system architecture

> Architecture diagram of the GitHub repository nazib-riasat/graft_system, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nazib-riasat/graft_system
- Repository: https://github.com/nazib-riasat/graft_system

## Overview

#### Purpose and entry points
GRAFT is a provenance-preserving temporal graph-memory research system. It builds evidence graphs from conversational/corpus material, selects checker-valid evidence sets, and is designed to support retrieval, answerability gating, and later reading. Primary programmatic entry is the `graft` Python package; experiment entry points are phase scripts such as `scripts/phase3_calibrate.py`, `scripts/phase5_bakeoff.py`, and `scripts/phase7_retrieval.py`. Configuration starts through `graft/config/loader.py` with presets in `graft/config/presets/`.

#### Shared contracts, identity, and reproducibility
The repository centers its data model in `graft/schemas.py`, with canonical serialization and content-derived IDs in `graft/canonical.py` and `graft/ids.py`. Runs are deterministic and auditable: `graft/runtime.py` establishes seeds/manifests, `graft/eventlog.py` records append-only operations, and `graft/graphstore.py` exposes replay-backed graph snapshots. This is the main boundary between domain stages and durable/reproducible artifacts.

#### Deterministic evidence-set core
`graft/core/` defines the formal decision layer used by learning and search. `graft/core/checker.py` implements validity \(H\); `graft/core/obligations.py` represents required evidence/coverage and derives state obligations; `graft/core/masks.py` constrains legal additions and terminal decisions. `graft/core/utility.py` and `graft/core/reward.py` turn valid sets into utility and reward, while `graft/core/incremental.py` provides add/undo-compatible validity logic. `graft/ledger.py` supplies budget enforcement at this boundary.

#### Synthetic environment and learner training
The synthetic proof-lattice environment provides enumerable ground truth and exact evaluation for early-phase learning experiments: `graft/synth/lattice.py`, `graft/synth/exact.py`, and `graft/synth/audits.py`. The set-generation subsystem consumes these state/action semantics through policy, rollout, and trainer layers in `graft/setgen/policy.py`, `graft/setgen/rollout.py`, and `graft/setgen/trainer.py`. Multiple supervised, imitation, RL, trajectory-balance, and checker-conditioned learner arms live under `graft/setgen/learners/`; this stage requires the ML/PyTorch dependency boundary.

#### Evidence-set search and portfolio selection
Search is separated from learned generation so methods can be compared under the same checker and utility. `graft/setgen/search/base.py` defines the search interface; greedy, beam, submodular, PCST, and portfolio methods are in `graft/setgen/search/s1_greedy.py`, `graft/setgen/search/s2_beam.py`, and `graft/setgen/search/s5_portfolio.py`. `graft/setgen/search/gate3.py` is the comparative gate harness. Search operates on candidate evidence sets and must respect core masks/checker outcomes rather than independently redefining validity.

#### Ingestion: corpus material to grounded graph evidence
Stage-A ingestion transforms source material into structured, provenance-linked evidence. `graft/ingest/pipeline.py` orchestrates extraction and processing; `graft/ingest/extractor.py` produces candidate content; `graft/ingest/grounding.py` attaches graph grounding; and `graft/ingest/nli.py` verifies entailment/support. Obligation parsing and support gating are explicit downstream controls in `graft/ingest/oblparse.py` and `graft/ingest/support.py`. Annotation artifacts and finalized labels are stored under `data/phase2_5/`.

#### Graph construction, retrieval, and answerability boundaries
Later real-data stages are modularized behind distinct package boundaries. `graft/graphbuild/` builds and validates graph edges/candidates, notably `graft/graphbuild/candidates.py`, `graft/graphbuild/commit.py`, and `graft/graphbuild/validate.py`; it may use embeddings or LLM linking via `graft/graphbuild/embed.py` and `graft/graphbuild/llmlink.py`. `graft/retrieve/` forms and ranks evidence pools using BM25, dense, entity, temporal, and fused retrieval (`graft/retrieve/pool.py`, `graft/retrieve/fuse.py`, `graft/retrieve/scorer.py`). `graft/gate/decide.py` is the separate answerability/risk decision layer, preventing retrieval or graph construction from directly asserting answerability.

#### Runtime and infrastructure
The project targets Python 3.11/3.12, with baseline dependencies in `requirements.txt` and optional ML/GPU dependencies in `requirements-ml.txt`; CUDA/PyTorch is material for Phase 3 training. Bootstrap scripts support Windows and Unix environments: `scripts/bootstrap.ps1` and `scripts/bootstrap.sh`. External model and corpus integrations are implied by ingestion NLI/extraction, graph-building embeddings/LLM linking, and retrieval backends, but are contained in their respective stage packages rather than exposed as a web-service architecture.

Main entry points: [`graft/core/checker.py`](https://github.com/nazib-riasat/graft_system/blob/master/graft/core/checker.py) (Checker, obligations & masks), [`graft/setgen/trainer.py`](https://github.com/nazib-riasat/graft_system/blob/master/graft/setgen/trainer.py) (Policy, rollout & trainer), [`scripts/phase3_calibrate.py`](https://github.com/nazib-riasat/graft_system/blob/master/scripts/phase3_calibrate.py) (Phase experiment scripts), [`graft/core/utility.py`](https://github.com/nazib-riasat/graft_system/blob/master/graft/core/utility.py) (Utility, reward & ledger)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_foundation["Reproducible foundation"]
  node_package["graft package<br/>Python package"]
  node_config["Config loader &amp; presets<br/>configuration<br/>[loader.py]"]
  node_schemas["Domain schemas<br/>data contracts<br/>[schemas.py]"]
  node_identity["Canonical IDs<br/>identity layer<br/>[canonical.py]"]
  node_runtime["Deterministic runtime<br/>run control<br/>[runtime.py]"]
  node_graphstore[("Event log &amp; graph snapshots<br/>artifact store<br/>[eventlog.py]")]
end

subgraph group_decision["Evidence decision and learning"]
  node_core{{"Checker, obligations &amp; masks<br/>decision semantics<br/>[checker.py]"}}
  node_utility["Utility, reward &amp; ledger<br/>optimization boundary<br/>[utility.py]"]
  node_synthetic["Proof-lattice environment<br/>synthetic environment<br/>[lattice.py]"]
  node_training["Policy, rollout &amp; trainer<br/>ML training<br/>[trainer.py]"]
  node_search["Search &amp; portfolio methods<br/>evidence-set search<br/>[base.py]"]
  node_searchgate["Search comparison gate<br/>evaluation harness<br/>[gate3.py]"]
end

subgraph group_pipeline["Real-data evidence pipeline"]
  node_ingestion["Extraction &amp; grounding<br/>ingestion pipeline<br/>[pipeline.py]"]
  node_support{{"Obligation parse &amp; support gate<br/>evidence validation<br/>[oblparse.py]"}}
  node_labels[("Phase 2.5 annotations<br/>annotation artifacts")]
  node_graphbuild["Graph build &amp; validation<br/>graph construction<br/>[commit.py]"]
  node_retrieval["Evidence retrieval &amp; fusion<br/>retrieval pipeline<br/>[pool.py]"]
  node_answerability{{"Answerability decision<br/>risk gate<br/>[decide.py]"}}
end

node_experiments["Phase experiment scripts<br/>experiment entry points"]

node_package -->|"loads"| node_config
node_config -->|"configures"| node_runtime
node_schemas -->|"canonicalizes"| node_identity
node_runtime -->|"records runs"| node_graphstore
node_schemas -->|"defines evidence contracts"| node_core
node_core -->|"valid sets"| node_utility
node_synthetic -->|"uses state semantics"| node_core
node_core -->|"constrains actions"| node_training
node_utility -->|"provides rewards"| node_training
node_synthetic -->|"provides exact evaluation"| node_training
node_core -->|"enforces validity"| node_search
node_utility -->|"ranks candidates"| node_search
node_search -->|"compares methods"| node_searchgate
node_ingestion -->|"candidate evidence"| node_support
node_labels -->|"annotation input"| node_ingestion
node_support -->|"grounded supported evidence"| node_graphbuild
node_graphbuild -->|"commits graph operations"| node_graphstore
node_graphbuild -->|"graph evidence"| node_retrieval
node_retrieval -->|"ranked evidence pool"| node_answerability
node_core -.->|"checker-valid evidence"| node_answerability
node_experiments -->|"Phase 3"| node_training
node_experiments -->|"Phase 5"| node_searchgate
node_experiments -->|"Phase 7"| node_retrieval

click node_package "https://github.com/nazib-riasat/graft_system/tree/master/graft"
click node_config "https://github.com/nazib-riasat/graft_system/blob/master/graft/config/loader.py"
click node_schemas "https://github.com/nazib-riasat/graft_system/blob/master/graft/schemas.py"
click node_identity "https://github.com/nazib-riasat/graft_system/blob/master/graft/canonical.py"
click node_runtime "https://github.com/nazib-riasat/graft_system/blob/master/graft/runtime.py"
click node_graphstore "https://github.com/nazib-riasat/graft_system/blob/master/graft/eventlog.py"
click node_core "https://github.com/nazib-riasat/graft_system/blob/master/graft/core/checker.py"
click node_utility "https://github.com/nazib-riasat/graft_system/blob/master/graft/core/utility.py"
click node_synthetic "https://github.com/nazib-riasat/graft_system/blob/master/graft/synth/lattice.py"
click node_training "https://github.com/nazib-riasat/graft_system/blob/master/graft/setgen/trainer.py"
click node_search "https://github.com/nazib-riasat/graft_system/blob/master/graft/setgen/search/base.py"
click node_searchgate "https://github.com/nazib-riasat/graft_system/blob/master/graft/setgen/search/gate3.py"
click node_ingestion "https://github.com/nazib-riasat/graft_system/blob/master/graft/ingest/pipeline.py"
click node_support "https://github.com/nazib-riasat/graft_system/blob/master/graft/ingest/oblparse.py"
click node_graphbuild "https://github.com/nazib-riasat/graft_system/blob/master/graft/graphbuild/commit.py"
click node_retrieval "https://github.com/nazib-riasat/graft_system/blob/master/graft/retrieve/pool.py"
click node_answerability "https://github.com/nazib-riasat/graft_system/blob/master/graft/gate/decide.py"
click node_experiments "https://github.com/nazib-riasat/graft_system/blob/master/scripts/phase3_calibrate.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_package,node_config,node_schemas,node_identity,node_runtime,node_graphstore toneBlue
class node_core,node_utility,node_synthetic,node_training,node_search,node_searchgate toneAmber
class node_ingestion,node_support,node_labels,node_graphbuild,node_retrieval,node_answerability toneMint
class node_experiments toneNeutral
```

## Components

### Reproducible foundation

Shared contracts and durable run artifacts.

- [graft package](https://github.com/nazib-riasat/graft_system/tree/master/graft): Python package · Primary programmatic entry point. · `graft`
- [Config loader & presets](https://github.com/nazib-riasat/graft_system/blob/master/graft/config/loader.py): configuration · `graft/config/loader.py`
- [Domain schemas](https://github.com/nazib-riasat/graft_system/blob/master/graft/schemas.py): data contracts · Canonical evidence, graph, and run data model. · `graft/schemas.py`
- [Canonical IDs](https://github.com/nazib-riasat/graft_system/blob/master/graft/canonical.py): identity layer · Canonical serialization and content-derived identifiers. · `graft/canonical.py`
- [Deterministic runtime](https://github.com/nazib-riasat/graft_system/blob/master/graft/runtime.py): run control · Establishes seeds and manifests. · `graft/runtime.py`
- [Event log & graph snapshots](https://github.com/nazib-riasat/graft_system/blob/master/graft/eventlog.py): artifact store · Append-only operations with replay-backed graph snapshots. · `graft/eventlog.py`

### Evidence decision and learning

Formal validity semantics, synthetic evaluation, and set generation.

- [Checker, obligations & masks](https://github.com/nazib-riasat/graft_system/blob/master/graft/core/checker.py): decision semantics · Defines validity, required coverage, and legal evidence actions. · `graft/core/checker.py`
- [Utility, reward & ledger](https://github.com/nazib-riasat/graft_system/blob/master/graft/core/utility.py): optimization boundary · Scores valid sets and enforces budgets. · `graft/core/utility.py`
- [Proof-lattice environment](https://github.com/nazib-riasat/graft_system/blob/master/graft/synth/lattice.py): synthetic environment · Enumerable ground truth and exact audits for learning experiments. · `graft/synth/lattice.py`
- [Policy, rollout & trainer](https://github.com/nazib-riasat/graft_system/blob/master/graft/setgen/trainer.py): ML training · Trains learner arms against shared state and action semantics. · `graft/setgen/trainer.py`
- [Search & portfolio methods](https://github.com/nazib-riasat/graft_system/blob/master/graft/setgen/search/base.py): evidence-set search · Comparable greedy, beam, and portfolio selection methods. · `graft/setgen/search/base.py`
- [Search comparison gate](https://github.com/nazib-riasat/graft_system/blob/master/graft/setgen/search/gate3.py): evaluation harness · `graft/setgen/search/gate3.py`

### Real-data evidence pipeline

Corpus ingestion through graph, retrieval, and answerability stages.

- [Extraction & grounding](https://github.com/nazib-riasat/graft_system/blob/master/graft/ingest/pipeline.py): ingestion pipeline · Transforms corpus material into provenance-linked candidate evidence. · `graft/ingest/pipeline.py`
- [Obligation parse & support gate](https://github.com/nazib-riasat/graft_system/blob/master/graft/ingest/oblparse.py): evidence validation · Parses obligations and verifies entailment/support. · `graft/ingest/oblparse.py`
- **Phase 2.5 annotations**: annotation artifacts · Finalized labels and annotation artifacts.
- [Graph build & validation](https://github.com/nazib-riasat/graft_system/blob/master/graft/graphbuild/commit.py): graph construction · Builds, commits, and validates grounded graph edges. · `graft/graphbuild/commit.py`
- [Evidence retrieval & fusion](https://github.com/nazib-riasat/graft_system/blob/master/graft/retrieve/pool.py): retrieval pipeline · Builds and ranks BM25, dense, entity, temporal, and fused pools. · `graft/retrieve/pool.py`
- [Answerability decision](https://github.com/nazib-riasat/graft_system/blob/master/graft/gate/decide.py): risk gate · Separates answerability and risk decisions from retrieval. · `graft/gate/decide.py`

### Other components

- [Phase experiment scripts](https://github.com/nazib-riasat/graft_system/blob/master/scripts/phase3_calibrate.py): experiment entry points · Calibration, bakeoff, and retrieval experiment runners. · `scripts/phase3_calibrate.py`

## Connections

- graft package → Config loader & presets: loads
- Config loader & presets → Deterministic runtime: configures
- Domain schemas → Canonical IDs: canonicalizes
- Deterministic runtime → Event log & graph snapshots: records runs
- Domain schemas → Checker, obligations & masks: defines evidence contracts
- Checker, obligations & masks → Utility, reward & ledger: valid sets
- Proof-lattice environment → Checker, obligations & masks: uses state semantics
- Checker, obligations & masks → Policy, rollout & trainer: constrains actions
- Utility, reward & ledger → Policy, rollout & trainer: provides rewards
- Proof-lattice environment → Policy, rollout & trainer: provides exact evaluation
- Checker, obligations & masks → Search & portfolio methods: enforces validity
- Utility, reward & ledger → Search & portfolio methods: ranks candidates
- Search & portfolio methods → Search comparison gate: compares methods
- Extraction & grounding → Obligation parse & support gate: candidate evidence
- Phase 2.5 annotations → Extraction & grounding: annotation input
- Obligation parse & support gate → Graph build & validation: grounded supported evidence
- Graph build & validation → Event log & graph snapshots: commits graph operations
- Graph build & validation → Evidence retrieval & fusion: graph evidence
- Evidence retrieval & fusion → Answerability decision: ranked evidence pool
- Checker, obligations & masks → Answerability decision: checker-valid evidence
- Phase experiment scripts → Policy, rollout & trainer: Phase 3
- Phase experiment scripts → Search comparison gate: Phase 5
- Phase experiment scripts → Evidence retrieval & fusion: Phase 7
