# microsoft/monitors4codegen architecture

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

- Interactive diagram: https://gitdiagram.com/microsoft/monitors4codegen
- Repository: https://github.com/microsoft/monitors4codegen

## Overview

##### Project Type and Purpose
- This is a research-oriented Python library and artifact repository for “Monitor-Guided Decoding (MGD)” of code language models, combining static analysis (via LSP) with code generation.
- It is not a full-stack web app, but rather a modular toolkit and evaluation harness:
- Core Python package (`monitors4codegen`) containing MGD orchestrator, monitor implementations, and LSP client wrapper (`multilspy`).
- Datasets (Java repos and prompts).
- Evaluation scripts to score generated code and produce tables/figures.

A. Datasets & Artifacts
- `datasets/PragmaticCode` (JSON of repo file contents, CSV of repos)
- `datasets/DotPrompts` (CSV of prompt examples)
- `inference_results` (model outputs CSV)
B. Core Library (`src/monitors4codegen`)
1. MGD Orchestrator
- `hf_gen.py`: integrates with HuggingFace models as a `LogitsProcessor`
- `openai_gen.py`: wraps OpenAI API calls under MGD
2. Monitor Interface (`monitor.py`) & Implementations (`monitors/*.py`)
- Dereferences, NumArgs, Typestate, Switch-Enum, ClassInstantiation, JointMonitors
3. LSP Client Layer (`multilspy`)
- LanguageServer / SyncLanguageServer classes, protocol handlers, configs
- Language-specific adapters (Eclipse JDTLS, Jedi, Omnisharp, rust-analyzer directories)
C. External Services & Tools
- HuggingFace Transformers models
- OpenAI API
- Language Servers (Java, Python, C#, Rust)
D. Evaluation Pipeline
- `evaluation_scripts/eval_results.py` and `eval_utils.py`
- Input: `inference_results/*.csv` + `datasets/PragmaticCode/fileContentsByRepo.json`
- Output: metrics (CR, NIM, ISM, PM), figures, `Report.md`
E. Results & Reports
- `results` and `results_sample` directories (CSV/MD tables, figure PNGs)

##### Relationships & Data Flow
- Static Analysis Flow:
1. MGD Orchestrator → calls `multilspy` → starts Language Server → issues LSP requests (completion, hover, definitions) → receives type/symbol info → Monitor uses this to build token masks
2. MGD Orchestrator → injects masks into LM generation (HF or OpenAI) → produces code output
- Evaluation Flow:
1. Read inference CSV → for each sample, compute metrics using `eval_utils.py` (compilation check uses repo context JSON) → generate plots (matplotlib/Plotly) → dump tables & figures → assemble `Report.md`
- Artifact Flow:
- Datasets feed both the MGD process (for context) and evaluation scripts.
- Inference results are consumed by evaluation scripts to reproduce paper figures.

##### Architectural Patterns & Design Principles
- Modular Separation:
- Core MGD logic decoupled from LM backends (HF vs OpenAI) via adapter classes.
- Monitor implementations adhere to a common interface (`Monitor`).
- LSP communications abstracted into `multilspy`.
- Pipeline/Orchestration: Data-driven pipeline connecting static analysis → decoding → evaluation.
- Extensibility: New monitors or language servers can be plugged in by adding new classes under `monitors` or `language_servers`.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Top Layer: Data Artifacts and External Services
    subgraph "Data Artifacts"
        direction TB
        PC_JSON["fileContentsByRepo.json"]:::data
        PC_CSV["repos.csv"]:::data
        DP_CSV["dataset.csv"]:::data
        IR_DIR["inference_results/"]:::data
        IR_FULL["dotprompts_results.csv"]:::data
        IR_SAMPLE["dotprompts_results_sample.csv"]:::data
    end

    subgraph "External Services" 
        direction TB
        HF["HuggingFace Transformers"]:::ext
        OPENAI["OpenAI API"]:::ext
        subgraph "Language Servers"
            direction TB
            LS_JAVA["Eclipse JDTLS"]:::ext
            LS_PY["Jedi"]:::ext
            LS_CS["OmniSharp"]:::ext
            LS_RS["rust-analyzer"]:::ext
        end
    end

    %% Middle Layer: Core Library
    subgraph "Core Library" 
        direction TB

        subgraph "MGD Orchestrator & Generators"
            direction TB
            HF_GEN["hf_gen.py"]:::core
            OPENAI_GEN["openai_gen.py"]:::core
            MGD_UTILS["mgd_utils.py"]:::core
            TOKEN_WRAPPER["tokenizer_wrapper.py"]:::core
        end

        subgraph "Monitors"
            direction TB
            MONITOR_IF["monitor.py"]:::core
            DEREF_MON["dereferences_monitor.py"]:::core
            NUMARGS_MON["numargs_monitor.py"]:::core
            SWITCH_ENUM["switch_enum_monitor.py"]:::core
            CLASS_INST["class_instantiation_monitor.py"]:::core
        end

        subgraph "LSP Client (multilspy)"
            direction TB
            LS_CORE["language_server.py"]:::core
            CONFIGS["config/exceptions/logger/settings/types/utils"]:::core
            PROTOCOL["lsp_protocol_handler/"]:::core
            subgraph "Adapters"
                direction TB
                AD_JAVA["eclipse_jdtls.py"]:::core
                AD_PY["jedi_server.py"]:::core
                AD_CS["omnisharp.py"]:::core
                AD_RS["rust_analyzer.py"]:::core
            end
        end
    end

    %% Bottom Layer: Evaluation and Results
    subgraph "Evaluation Pipeline"
        direction TB
        EVAL_RUN["eval_results.py"]:::eval
        EVAL_UTIL["eval_utils.py"]:::eval
    end

    subgraph "Results & Reports"
        direction TB
        REPORT["Report.md"]:::eval
        METRICS_CSV["all_metrics_table.csv"]:::eval
        METRICS_MD["all_metrics_table.md"]:::eval
        FIGURES["figures/"]:::eval
        SAMPLE_RESULTS["results_sample/"]:::eval
    end

    %% Data Flow Arrows
    PC_JSON -->|"provides context"| HF_GEN
    PC_CSV -->|"provides list"| HF_GEN
    DP_CSV -->|"provides prompts"| OPENAI_GEN
    HF_GEN -->|"uses monitors & LSP"| MONITOR_IF
    OPENAI_GEN -->|"uses monitors & LSP"| MONITOR_IF
    MONITOR_IF -->|"calls LSP client"| LS_CORE
    LS_CORE -->|"requests"| LS_JAVA
    LS_CORE -->|"requests"| LS_PY
    LS_CORE -->|"requests"| LS_CS
    LS_CORE -->|"requests"| LS_RS
    MONITOR_IF -->|"builds masks"| HF_GEN
    MONITOR_IF -->|"builds masks"| OPENAI_GEN
    HF -->|"model inference"| HF_GEN
    OPENAI -->|"model inference"| OPENAI_GEN
    HF_GEN -->|"generates"| IR_FULL
    OPENAI_GEN -->|"generates"| IR_FULL

    IR_FULL -->|"consumed by"| EVAL_RUN
    IR_SAMPLE -->|"consumed by"| EVAL_RUN
    PC_JSON -->|"used by"| EVAL_UTIL

    EVAL_RUN -->|"computes metrics"| METRICS_CSV
    EVAL_RUN -->|"computes metrics"| METRICS_MD
    EVAL_RUN -->|"generates figures"| FIGURES
    EVAL_RUN -->|"assembles"| REPORT
    FIGURES -->|"sample set"| SAMPLE_RESULTS

    %% Click Events for component mapping
    click PC_JSON "https://github.com/microsoft/monitors4codegen/blob/main/datasets/PragmaticCode/fileContentsByRepo.json"
    click PC_CSV "https://github.com/microsoft/monitors4codegen/blob/main/datasets/PragmaticCode/repos.csv"
    click DP_CSV "https://github.com/microsoft/monitors4codegen/blob/main/datasets/DotPrompts/dataset.csv"
    click IR_DIR "https://github.com/microsoft/monitors4codegen/tree/main/inference_results/"
    click IR_FULL "https://github.com/microsoft/monitors4codegen/blob/main/inference_results/dotprompts_results.csv"
    click IR_SAMPLE "https://github.com/microsoft/monitors4codegen/blob/main/inference_results/dotprompts_results_sample.csv"
    click HF_GEN "https://github.com/microsoft/monitors4codegen/blob/main/src/monitors4codegen/monitor_guided_decoding/hf_gen.py"
    click OPENAI_GEN "https://github.com/microsoft/monitors4codegen/blob/main/src/monitors4codegen/monitor_guided_decoding/openai_gen.py"
    click MGD_UTILS "https://github.com/microsoft/monitors4codegen/blob/main/src/monitors4codegen/monitor_guided_decoding/mgd_utils.py"
    click TOKEN_WRAPPER "https://github.com/microsoft/monitors4codegen/blob/main/src/monitors4codegen/monitor_guided_decoding/tokenizer_wrapper.py"
    click MONITOR_IF "https://github.com/microsoft/monitors4codegen/blob/main/src/monitors4codegen/monitor_guided_decoding/monitor.py"
    click DEREF_MON "https://github.com/microsoft/monitors4codegen/blob/main/src/monitors4codegen/monitor_guided_decoding/monitors/dereferences_monitor.py"
    click NUMARGS_MON "https://github.com/microsoft/monitors4codegen/blob/main/src/monitors4codegen/monitor_guided_decoding/monitors/numargs_monitor.py"
    click SWITCH_ENUM "https://github.com/microsoft/monitors4codegen/blob/main/src/monitors4codegen/monitor_guided_decoding/monitors/switch_enum_monitor.py"
    click CLASS_INST "https://github.com/microsoft/monitors4codegen/blob/main/src/monitors4codegen/monitor_guided_decoding/monitors/class_instantiation_monitor.py"
    click LS_CORE "https://github.com/microsoft/monitors4codegen/blob/main/src/monitors4codegen/multilspy/language_server.py"
    click PROTOCOL "https://github.com/microsoft/monitors4codegen/tree/main/src/monitors4codegen/multilspy/lsp_protocol_handler/"
    click AD_JAVA "https://github.com/microsoft/monitors4codegen/blob/main/src/monitors4codegen/multilspy/language_servers/eclipse_jdtls/eclipse_jdtls.py"
    click AD_PY "https://github.com/microsoft/monitors4codegen/blob/main/src/monitors4codegen/multilspy/language_servers/jedi_language_server/jedi_server.py"
    click AD_CS "https://github.com/microsoft/monitors4codegen/blob/main/src/monitors4codegen/multilspy/language_servers/omnisharp/omnisharp.py"
    click AD_RS "https://github.com/microsoft/monitors4codegen/blob/main/src/monitors4codegen/multilspy/language_servers/rust_analyzer/rust_analyzer.py"
    click EVAL_RUN "https://github.com/microsoft/monitors4codegen/blob/main/evaluation_scripts/eval_results.py"
    click EVAL_UTIL "https://github.com/microsoft/monitors4codegen/blob/main/evaluation_scripts/eval_utils.py"
    click REPORT "https://github.com/microsoft/monitors4codegen/blob/main/results/Report.md"

    %% Styles
    classDef data fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef core fill:#90EE90,stroke:#333,stroke-width:1px
    classDef ext fill:#FFA500,stroke:#333,stroke-width:1px
    classDef eval fill:#FFFF99,stroke:#333,stroke-width:1px
```
