# msv-lab/specfix architecture

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

- Interactive diagram: https://gitdiagram.com/msv-lab/specfix
- Repository: https://github.com/msv-lab/specfix

## Overview

##### Main system components
- **CLI / Entrypoint**
- `main.py` (under `specfix/`) – parses command-line arguments, kicks off the pipeline
- `run.sh` – convenience wrapper
- **Configuration & Dataset Loader**
- Arg parser in `main.py`
- Dataset loader in `dataset/` & `dataset/dataset_script.py`
- **Core Pipeline Modules** (under `specfix/`)
- **Prompting** (`prompting.py`): constructs prompts for code generation, test generation, and spec refinement
- **Model / LLM API** (`model.py`): wraps calls to OpenAI / Qwen APIs
- **Candidate Generator** (`main.py` + `model.py` + `prompting.py`): generates N programs per iteration
- **Tester** (`tester.py`): runs generated programs against test suites in parallel
- **Clusterer** (`cluster.py`): groups functionally equivalent implementations
- **Solution Transformer** (`solution_transformer.py`): refines the requirement (spec) based on test outcomes & clustering
- **Evaluator / Metrics** (`evaluator.py`): computes success rate, iteration count, etc.
- **Utilities** (`utils.py`): JSON I/O, logging, concurrency helpers
- **Experiment Harness** (`experiment/`): dozens of standalone scripts for data collection, ambiguity detection, threshold tuning, user studies. They import or invoke the core modules above to run specific workflows.
- **Data Storage**
- JSONL files under `dataset/` and `experiment/…/*.jsonl`
- Ground-truth, interim outputs, and final reports

##### Relationships & Dataflow
- The **CLI/Entrypoint** reads arguments (dataset name, path, API key, model, temperature, num-programs, max-iterations)
→ calls the **Pipeline Orchestrator** in `main.py`.
- **Dataset Loader** reads examples from JSONL or CSV → hands off to the orchestrator.
- For each iteration:
a. Orchestrator calls **Prompting** → builds generation prompt
b. Orchestrator calls **Model** → sends prompt to external LLM API → receives N candidate programs
c. Orchestrator sends candidates to **Tester** → runs test suite concurrently → returns pass/fail metadata
d. Orchestrator feeds test results to **Clusterer** → groups implementations into equivalence clusters
e. Orchestrator uses **Solution Transformer** → generates clarified/refined requirement for next iteration
f. Loop back until convergence or max iterations
- On completion, **Evaluator** collects metrics, writes final reports to disk

##### Architectural Patterns & Principles
- **Pipeline / Orchestrator pattern**: clear sequential steps, but parallelism inside testing
- **Iterative feedback loop**: spec refinement and re-generation until convergence
- **Modular design**: each core responsibility (prompting, LLM interface, testing, clustering, transformation, evaluation) lives in its own module
- **Extensibility**: experiment scripts plug into the core modules to run different workflows (ambiguity detection, threshold tuning, user study, etc.)

##### Relevant Technologies & Frameworks
- Language: **Python**
- LLM APIs: **OpenAI** or **Qwen** (via `model.py`)
- Data format: **JSONL**, **CSV**
- Concurrency: Python threads/processes in `tester.py`
- Third-party libs: `jsonlines`, `evalplus`
- CLI: built-in `argparse`

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Entrypoint Layer
    subgraph "Entrypoint"
        direction TB
        runWrapper["run.sh"]:::experiment
        CLI["specfix/main.py\n(CLI Entrypoint & Orchestrator)"]:::core
    end

    runWrapper -->|"invokes"| CLI

    %% Dataset Layer
    subgraph "Data Input" 
        direction TB
        DatasetLoader["dataset/dataset_script.py\n(Dataset Loader)"]:::storage
        DatasetStore["Dataset Files\n(JSONL/CSV)"]:::storage
    end

    CLI -->|"loads data"| DatasetLoader
    DatasetLoader -->|"reads"| DatasetStore

    %% Core Pipeline Loop
    subgraph "Core Pipeline (Iterative Loop)"
        direction TB
        Prompting["specfix/prompting.py\n(Prompting)"]:::core
        ModelAPI["specfix/model.py\n(Model / LLM API)"]:::core
        CandidatePrograms["Candidate Programs\n(N items)"]:::core
        Tester["specfix/tester.py\n(Tester)"]:::core
        TestResults["Test Results\n(pass/fail metadata)"]:::core
        Clusterer["specfix/cluster.py\n(Clusterer)"]:::core
        Clusters["Implementation Clusters"]:::core
        Transformer["specfix/solution_transformer.py\n(Solution Transformer)"]:::core
        NewSpec["Refined Spec"]:::core
    end

    CLI -->|"starts loop"| Prompting
    Prompting -->|"builds prompt"| ModelAPI
    ModelAPI -->|"calls LLM API"| CandidatePrograms
    CandidatePrograms -->|"to"| Tester
    Tester -->|"runs tests"| TestResults
    TestResults -->|"to"| Clusterer
    Clusterer -->|"groups into"| Clusters
    Clusters -->|"to"| Transformer
    Transformer -->|"produces"| NewSpec
    NewSpec -->|"feeds back"| CLI

    %% External Services
    subgraph "External Services"
        direction TB
        LLMAPI["OpenAI / Qwen API"]:::external
    end
    ModelAPI -->|"sends prompt"| LLMAPI
    LLMAPI -->|"returns outputs"| ModelAPI

    %% Final Evaluation
    subgraph "Completion"
        direction TB
        Evaluator["specfix/evaluator.py\n(Evaluator & Metrics)"]:::core
        Reports["Final Reports\n(JSONL)"]:::storage
    end
    CLI -->|"on completion"| Evaluator
    Evaluator -->|"writes"| Reports

    %% Experiment Harness
    subgraph "Experiment Harness" 
        direction TB
        ExperimentScripts["experiment/"]:::experiment
        ExperimentOutputs["Experiment Outputs\n(interim JSONL)"]:::storage
    end
    ExperimentScripts -->|"uses core modules"| CLI
    ExperimentScripts -->|"writes"| ExperimentOutputs

    %% Utility Module
    Utils["specfix/utils.py\n(Utilities)"]:::core
    CLI --> Utils
    Prompting --> Utils
    ModelAPI --> Utils
    Tester --> Utils
    Clusterer --> Utils
    Transformer --> Utils
    Evaluator --> Utils

    %% Click Events
    click CLI "https://github.com/msv-lab/specfix/blob/main/specfix/main.py"
    click runWrapper "https://github.com/msv-lab/specfix/blob/main/run.sh"
    click DatasetLoader "https://github.com/msv-lab/specfix/blob/main/dataset/dataset_script.py"
    click Prompting "https://github.com/msv-lab/specfix/blob/main/specfix/prompting.py"
    click ModelAPI "https://github.com/msv-lab/specfix/blob/main/specfix/model.py"
    click Tester "https://github.com/msv-lab/specfix/blob/main/specfix/tester.py"
    click Clusterer "https://github.com/msv-lab/specfix/blob/main/specfix/cluster.py"
    click Transformer "https://github.com/msv-lab/specfix/blob/main/specfix/solution_transformer.py"
    click Evaluator "https://github.com/msv-lab/specfix/blob/main/specfix/evaluator.py"
    click Utils "https://github.com/msv-lab/specfix/blob/main/specfix/utils.py"
    click ExperimentScripts "https://github.com/msv-lab/specfix/tree/main/experiment/"
    click DatasetStore "https://github.com/msv-lab/specfix/tree/main/dataset/"
    click ExperimentOutputs "https://github.com/msv-lab/specfix/tree/main/experiment/"

    %% Styles
    classDef core fill:#cce5ff,stroke:#004085,color:#004085;
    classDef storage fill:#d4edda,stroke:#155724,color:#155724,stroke-dasharray: 5 5;
    classDef external fill:#fff3cd,stroke:#856404,color:#856404,stroke-dasharray: 5 5;
    classDef experiment fill:#e2e3e5,stroke:#6c757d,color:#6c757d;
```
