# khoasuythan/formal_verification-in-the-loop architecture

> Architecture diagram of the GitHub repository khoasuythan/formal_verification-in-the-loop, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/khoasuythan/formal_verification-in-the-loop
- Repository: https://github.com/khoasuythan/formal_verification-in-the-loop

## Overview

This is a Streamlit research application for generating Dafny code with LLMs and iteratively repairing it against Dafny/Z3 verification. The main workflow accepts a Dafny specification, detects its algorithmic topology, prompts a model, protects the original specification, normalizes and verifies generated code, then feeds diagnostics into subsequent repair attempts until success or the Pass@K limit. Separate product branches support cross-model benchmarking and research metrics. Relationships are limited to those visible in excerpts, SOURCE INDEX references, or README documentation; unsampled subsystems are represented only where documented.

Main entry points: [`core/pipeline_controller.py`](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/pipeline_controller.py) (Pass@K Controller), [`core/cross_model_evaluator.py`](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/cross_model_evaluator.py) (Cross-Model Evaluator), [`agents/llm_agent.py`](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/agents/llm_agent.py) (LLM Agent), [`app.py`](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/app.py) (Streamlit Studio)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interface["User Interface"]
  node_webapp["Streamlit Studio<br/>[app.py]"]
  node_pipeline_view["Pipeline View"]
  node_cross_view["Cross-Model View"]
  node_metrics_view["Metrics View"]
end

subgraph group_repair["Verified Repair"]
  node_pipeline["Pass@K Controller"]
  node_topology["Topology Detection"]
  node_spec_lock["Spec Integrity Guard<br/>[spec_locker.py]"]
  node_normalizer["Syntax Normalization"]
  node_template["Template Preservation"]
  node_localizer["AST Invariant Patching<br/>[ast_localizer.py]"]
  node_diagnostics["SMT Diagnostics"]
  node_dafny["Dafny Verification<br/>[dafny_engine.py]"]
end

subgraph group_model["Model Services"]
  node_agent["LLM Agent<br/>[llm_agent.py]"]
  node_cot["Reasoning Extraction<br/>[cot_extractor.py]"]
  node_tokens["Token Tracking<br/>[token_tracker.py]"]
end

subgraph group_evaluation["Research Evaluation"]
  node_cross_eval["Cross-Model Evaluator"]
  node_classifier["Hallucination Analysis"]
  node_benchmarks["Benchmark Specifications"]
  node_metrics["Experiment Metrics"]
  node_latex["LaTeX Export<br/>[latex_exporter.py]"]
end

subgraph group_support["Verification Support"]
  node_cache[("Verification Cache")]
  node_history[("Run Results and History")]
end

node_user(("Researcher"))
node_dafny_cli{{"Dafny and Z3"}}
node_llm_services{{"Local and Cloud LLMs"}}

node_user -->|"uses"| node_webapp
node_webapp -->|"routes to"| node_pipeline_view
node_webapp -->|"routes to"| node_cross_view
node_webapp -->|"routes to"| node_metrics_view
node_pipeline_view -->|"runs tasks"| node_pipeline
node_pipeline -->|"classifies spec"| node_topology
node_pipeline -->|"requests generation"| node_agent
node_pipeline -->|"checks integrity"| node_spec_lock
node_pipeline -->|"normalizes code"| node_normalizer
node_pipeline -->|"preserves template"| node_template
node_pipeline -->|"verifies candidate"| node_dafny
node_pipeline -->|"analyzes failures"| node_diagnostics
node_pipeline -->|"patches invariants"| node_localizer
node_dafny -->|"invokes verifier"| node_dafny_cli
node_dafny -->|"reads and writes"| node_cache
node_agent -->|"requests completion"| node_llm_services
node_agent -->|"extracts reasoning"| node_cot
node_agent -->|"records usage"| node_tokens
node_cross_view -->|"runs comparison"| node_cross_eval
node_cross_eval -->|"evaluates tasks"| node_pipeline
node_cross_eval -->|"selects models"| node_agent
node_cross_eval -->|"verifies results"| node_dafny
node_cross_eval -->|"analyzes hallucinations"| node_classifier
node_cross_eval -->|"configures cache"| node_cache
node_cross_eval -.->|"evaluates specs"| node_benchmarks
node_cross_eval -->|"stores checkpoints"| node_history
node_metrics_view -->|"presents metrics"| node_metrics
node_metrics_view -->|"exports tables"| node_latex
node_metrics_view -->|"reads results"| node_history
node_pipeline_view -->|"loads run history"| node_history

click node_webapp "https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/app.py"
click node_pipeline_view "https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/web_demo/tabs/tab_pipeline_view.py"
click node_cross_view "https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/web_demo/tabs/tab_cross_model_view.py"
click node_metrics_view "https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/web_demo/tabs/tab_metrics_view.py"
click node_pipeline "https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/pipeline_controller.py"
click node_topology "https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/topology_detector.py"
click node_spec_lock "https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/spec_locker.py"
click node_normalizer "https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/syntax_normalizer.py"
click node_template "https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/template_preserver.py"
click node_localizer "https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/ast_localizer.py"
click node_diagnostics "https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/diagnostic_parser.py"
click node_dafny "https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/dafny_engine.py"
click node_agent "https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/agents/llm_agent.py"
click node_cot "https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/cot_extractor.py"
click node_tokens "https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/token_tracker.py"
click node_cross_eval "https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/cross_model_evaluator.py"
click node_classifier "https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/hallucination_classifier.py"
click node_benchmarks "https://github.com/khoasuythan/formal_verification-in-the-loop/tree/main/data/benchmarks"
click node_metrics "https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/experiments/evaluate_metrics.py"
click node_latex "https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/latex_exporter.py"
click node_cache "https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/verification_cache.py"
click node_history "https://github.com/khoasuythan/formal_verification-in-the-loop/tree/main/artifacts/results"

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_webapp,node_pipeline_view,node_cross_view,node_metrics_view toneBlue
class node_pipeline,node_topology,node_spec_lock,node_normalizer,node_template,node_localizer,node_diagnostics,node_dafny toneAmber
class node_agent,node_cot,node_tokens toneMint
class node_cross_eval,node_classifier,node_benchmarks,node_metrics,node_latex toneRose
class node_cache,node_history,node_user,node_dafny_cli,node_llm_services toneIndigo
```

## Components

### User Interface

- [Streamlit Studio](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/app.py): `app.py`
- [Pipeline View](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/web_demo/tabs/tab_pipeline_view.py): `web_demo/tabs/tab_pipeline_view.py`
- [Cross-Model View](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/web_demo/tabs/tab_cross_model_view.py): `web_demo/tabs/tab_cross_model_view.py`
- [Metrics View](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/web_demo/tabs/tab_metrics_view.py): `web_demo/tabs/tab_metrics_view.py`

### Verified Repair

- [Pass@K Controller](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/pipeline_controller.py): `core/pipeline_controller.py`
- [Topology Detection](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/topology_detector.py): `core/topology_detector.py`
- [Spec Integrity Guard](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/spec_locker.py): `core/spec_locker.py`
- [Syntax Normalization](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/syntax_normalizer.py): `core/syntax_normalizer.py`
- [Template Preservation](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/template_preserver.py): `core/template_preserver.py`
- [AST Invariant Patching](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/ast_localizer.py): `core/ast_localizer.py`
- [SMT Diagnostics](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/diagnostic_parser.py): `core/diagnostic_parser.py`
- [Dafny Verification](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/dafny_engine.py): `core/dafny_engine.py`

### Model Services

- [LLM Agent](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/agents/llm_agent.py): `agents/llm_agent.py`
- [Reasoning Extraction](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/cot_extractor.py): `core/cot_extractor.py`
- [Token Tracking](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/token_tracker.py): `core/token_tracker.py`

### Research Evaluation

- [Cross-Model Evaluator](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/cross_model_evaluator.py): `core/cross_model_evaluator.py`
- [Hallucination Analysis](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/hallucination_classifier.py): `core/hallucination_classifier.py`
- [Benchmark Specifications](https://github.com/khoasuythan/formal_verification-in-the-loop/tree/main/data/benchmarks): `data/benchmarks`
- [Experiment Metrics](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/experiments/evaluate_metrics.py): `experiments/evaluate_metrics.py`
- [LaTeX Export](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/latex_exporter.py): `core/latex_exporter.py`

### Verification Support

- [Verification Cache](https://github.com/khoasuythan/formal_verification-in-the-loop/blob/main/core/verification_cache.py): `core/verification_cache.py`
- [Run Results and History](https://github.com/khoasuythan/formal_verification-in-the-loop/tree/main/artifacts/results): `artifacts/results`

### Other components

- **Researcher**
- **Dafny and Z3**
- **Local and Cloud LLMs**

## Connections

- Researcher → Streamlit Studio: uses (evidence: `README.md`)
- Streamlit Studio → Pipeline View: routes to (evidence: `app.py`)
- Streamlit Studio → Cross-Model View: routes to (evidence: `app.py`)
- Streamlit Studio → Metrics View: routes to (evidence: `app.py`)
- Pipeline View → Pass@K Controller: runs tasks (evidence: `web_demo/tabs/tab_pipeline_view.py`)
- Pass@K Controller → Topology Detection: classifies spec (evidence: `core/pipeline_controller.py`)
- Pass@K Controller → LLM Agent: requests generation (evidence: `core/pipeline_controller.py`)
- Pass@K Controller → Spec Integrity Guard: checks integrity (evidence: `core/pipeline_controller.py`)
- Pass@K Controller → Syntax Normalization: normalizes code (evidence: `core/pipeline_controller.py`)
- Pass@K Controller → Template Preservation: preserves template (evidence: `core/pipeline_controller.py`)
- Pass@K Controller → Dafny Verification: verifies candidate (evidence: `core/pipeline_controller.py`)
- Pass@K Controller → SMT Diagnostics: analyzes failures (evidence: `core/pipeline_controller.py`)
- Pass@K Controller → AST Invariant Patching: patches invariants (evidence: `core/pipeline_controller.py`)
- Dafny Verification → Dafny and Z3: invokes verifier (evidence: `core/dafny_engine.py`)
- Dafny Verification → Verification Cache: reads and writes (evidence: `core/dafny_engine.py`)
- LLM Agent → Local and Cloud LLMs: requests completion (evidence: `agents/llm_agent.py`)
- LLM Agent → Reasoning Extraction: extracts reasoning (evidence: `agents/llm_agent.py`)
- LLM Agent → Token Tracking: records usage (evidence: `agents/llm_agent.py`)
- Cross-Model View → Cross-Model Evaluator: runs comparison (evidence: `web_demo/tabs/tab_cross_model_view.py`)
- Cross-Model Evaluator → Pass@K Controller: evaluates tasks (evidence: `core/cross_model_evaluator.py`)
- Cross-Model Evaluator → LLM Agent: selects models (evidence: `core/cross_model_evaluator.py`)
- Cross-Model Evaluator → Dafny Verification: verifies results (evidence: `core/cross_model_evaluator.py`)
- Cross-Model Evaluator → Hallucination Analysis: analyzes hallucinations (evidence: `core/cross_model_evaluator.py`)
- Cross-Model Evaluator → Verification Cache: configures cache (evidence: `core/cross_model_evaluator.py`)
- Cross-Model Evaluator → Benchmark Specifications: evaluates specs (evidence: `README.md`)
- Cross-Model Evaluator → Run Results and History: stores checkpoints (evidence: `core/cross_model_evaluator.py`)
- Metrics View → Experiment Metrics: presents metrics (evidence: `web_demo/tabs/tab_metrics_view.py`)
- Metrics View → LaTeX Export: exports tables (evidence: `web_demo/tabs/tab_metrics_view.py`)
- Metrics View → Run Results and History: reads results (evidence: `web_demo/tabs/tab_metrics_view.py`)
- Pipeline View → Run Results and History: loads run history (evidence: `web_demo/tabs/tab_pipeline_view.py`)
