# lmcrc/newclid architecture

> Architecture diagram of the GitHub repository lmcrc/newclid, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lmcrc/newclid
- Repository: https://github.com/lmcrc/newclid

## Overview

##### Project type and purpose
- Newclid is an open-source Python library and CLI tool (with optional web interface) for solving Olympiad‐style geometry problems. It provides both a command‐line front end (“newclid”), a Python API (GeometricSolverBuilder/GeometricSolver), and a minimal webapp. Internally it implements multiple reasoning engines (algebraic, numerical), an agent framework, dependency graph builders, and proof writers.

##### File structure
- Top-level files: packaging (pyproject.toml, setup.py, requirements.txt), CI/config (GitHub workflows, .pre-commit, devcontainer).
- docs/: Sphinx source for online documentation and static assets.
- scripts/: utilities for profiling and build checks.
- problems_datasets/: plain-text problem definitions.
- src/newclid/:
– __main__.py / api.py: CLI entrypoint and user API.
– configs.py + default_configs/: global configuration and fallback rule sets.
– agent/: agent interfaces and implementations (ddarn, human).
– algebraic_reasoning/: algebraic manipulator and tables.
– dependencies/: building and traversing the dependency graph and symbols graph.
– formulations/: converting text definitions to internal problem, clause, rule, definition objects.
– numerical/: numerical checkers, figure sketching, geometry primitives.
– predicates/: geometric predicates (collinearity, perpendicularity, etc.).
– proof & proof_writing: assembling and formatting proof steps.
– run_loop.py: orchestrates the solver loop.
– webapp.py: minimal web server exposing the solver over HTTP.
- tests/ & tests-perfs/: unit tests for agents, reasoning engines, CLI, geogebra loader, and performance benchmarks.

##### README for additional insights
- Describes two main usage modes: CLI (newclid --problem-name …) and Python interface (GeometricSolverBuilder → .build() → run()).
- Mentions nature of the agentic support, plug-in of different agents.
- Points to web documentation and examples in “examples” (folder in repo).
- References external reliance on Geogebra exports and optional uv packaging.

a. Main components
- CLI / Command‐line Front End
- Python API Layer (GeometricSolverBuilder, GeometricSolver)
- Core Orchestrator (run_loop.py)
- Formulation Engine (parsing problem text → internal Problem, Clause, Rule objects)
- Dependency Graph Builder (dependency_graph.py, symbols_graph.py)
- Reasoning Engines:
– Algebraic Reasoner
– Numerical Reasoner
- Agent Layer (agents_interface.py → ddarn, human_agent)
- Proof Writer (proof_writing.py)
- Configuration & Rule Store (configs.py, default_configs)
- Data Store (problems_datasets/)
- Optional Web Interface (webapp.py)
- External Service: Geogebra integration (load_geogebra.py)
b. Relationships & data flow
1. CLI/API → Formulation Engine (input text or file)
2. Formulation → Dependency Graph Builder → Graph structures
3. Core Orchestrator calls Agent Layer → selects next reasoning action
4. Agent Layer invokes one of the Reasoning Engines → returns new clauses or proof steps
5. Proof Writer accumulates steps → final proof output
6. Orchestrator loops until solve or timeout
7. Web Interface wraps API calls behind HTTP endpoints
8. Configuration & default rules feed into Formulation and Reasoning Engines
9. Profiling scripts and CI pipeline hook into test modules and scripts
c. Architectural patterns & principles
- Layered architecture (presentation → API → orchestrator → domain engines → data)
- Builder pattern for solver construction (GeometricSolverBuilder)
- Plugin/strategy pattern for agents and reasoning engines
- Separation of concerns: parsing, graph construction, reasoning, proof writing
- Declarative problem specification → imperative solve loop
d. Technologies & frameworks
- Python 3.9+ package managed via pyproject.toml / setuptools (setup.py)
- GitHub Actions for CI (tests, coverage, PyPI publishing)
- Sphinx for documentation
- optional uv for dev-dependency management
- Geogebra file parsing (load_geogebra.py)
- pre-commit, ruff for linting

– Green: Presentation layer (CLI, API, Webapp)
– Blue: Core orchestrator & domain logic (run_loop, formulation, dependency)
– Orange: Reasoning engines & agents
– Gray: Configuration & data

##### Detail
- Every sub-component, even if nested. For instance, show “default_configs” as part of Config Store.
- Distinguish between the two agent types (ddarn vs. human) under the Agent Layer.
- Testing and profiling pipelines branching off the core modules.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    CLI["CLI Front End"]:::green
    API["Python API (GeometricSolverBuilder)"]:::green
    Webapp["Web Interface"]:::green

    %% Core Orchestrator & Domain Logic
    subgraph "Core Logic" 
        direction TB
        Formulation["Formulation Engine"]:::blue
        Dependency["Dependency Graph Builder"]:::blue
        Orchestrator["Core Orchestrator (run_loop)"]:::blue
        ProofWriter["Proof Writer"]:::blue
    end

    %% Agents & Reasoning Engines
    subgraph "Agents & Reasoners"
        direction TB
        AgentInterface["Agent Interface"]:::orange
        Ddarn["ddarn Agent"]:::orange
        Human["Human Agent"]:::orange
        Algebraic["Algebraic Reasoner"]:::orange
        Numerical["Numerical Reasoner"]:::orange
    end

    %% Configuration & Data
    Config["Configuration Store"]:::gray
    DefaultConfigs["Default Configs"]:::gray
    DataStore["Problem Datasets"]:::gray
    Geogebra["Geogebra Loader"]:::gray

    %% Presentation to Core
    CLI -->|"ProblemText"| Formulation
    API -->|".load_problem_from_txt() → .build()"| Formulation
    Webapp -->|"HTTP Request"| API

    %% Core Flow
    Formulation -->|"ProblemObject"| Dependency
    Dependency -->|"Graph"| Orchestrator
    Orchestrator -->|"nextAction"| AgentInterface
    AgentInterface -->|"invoke"| Ddarn
    AgentInterface -->|"invoke"| Human
    Ddarn -->|"ClauseSet"| Orchestrator
    Human -->|"ClauseSet"| Orchestrator
    Orchestrator -->|"selectEngine"| Algebraic
    Orchestrator -->|"selectEngine"| Numerical
    Algebraic -->|"ProofSteps"| ProofWriter
    Numerical -->|"ProofSteps"| ProofWriter
    ProofWriter -->|"FinalProof"| CLI
    ProofWriter -->|"FinalProof"| API
    ProofWriter -->|"FinalProof"| Webapp

    %% Config & Data feeding
    Config --> Formulation
    DefaultConfigs --> Formulation
    Config --> Algebraic
    Config --> Numerical
    DataStore --> Formulation

    %% External Integration
    Geogebra -.->|"optional parse"| Formulation

    %% Click Events
    click CLI "https://github.com/lmcrc/newclid/blob/main/src/newclid/__main__.py"
    click API "https://github.com/lmcrc/newclid/blob/main/src/newclid/api.py"
    click Webapp "https://github.com/lmcrc/newclid/blob/main/src/newclid/webapp.py"
    click Formulation "https://github.com/lmcrc/newclid/tree/main/src/newclid/formulations/"
    click Dependency "https://github.com/lmcrc/newclid/tree/main/src/newclid/dependencies/"
    click Orchestrator "https://github.com/lmcrc/newclid/blob/main/src/newclid/run_loop.py"
    click ProofWriter "https://github.com/lmcrc/newclid/blob/main/src/newclid/proof_writing.py"
    click AgentInterface "https://github.com/lmcrc/newclid/blob/main/src/newclid/agent/agents_interface.py"
    click Ddarn "https://github.com/lmcrc/newclid/blob/main/src/newclid/agent/ddarn.py"
    click Human "https://github.com/lmcrc/newclid/blob/main/src/newclid/agent/human_agent.py"
    click Algebraic "https://github.com/lmcrc/newclid/blob/main/src/newclid/algebraic_reasoning/algebraic_manipulator.py"
    click Numerical "https://github.com/lmcrc/newclid/tree/main/src/newclid/numerical/"
    click Config "https://github.com/lmcrc/newclid/blob/main/src/newclid/configs.py"
    click DefaultConfigs "https://github.com/lmcrc/newclid/tree/main/src/newclid/default_configs/"
    click DataStore "https://github.com/lmcrc/newclid/tree/main/problems_datasets/"
    click Geogebra "https://github.com/lmcrc/newclid/blob/main/src/newclid/load_geogebra.py"

    %% Styles
    classDef green fill:#ccffcc,stroke:#333,stroke-width:1px
    classDef blue fill:#cce5ff,stroke:#333,stroke-width:1px
    classDef orange fill:#ffe5cc,stroke:#333,stroke-width:1px
    classDef gray fill:#dddddd,stroke:#333,stroke-width:1px
```
