Skip to content

CornerCore-AI/glass-box-system2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Glass Box System 2: Causal Neural Networks for Bug Detection

License Python

A Glass Box System 2 model that implements dual-pathway reasoning (System 1/System 2) with causal feature extraction for robust bug detection in code patches. This architecture is designed to learn causal representations that generalize across distribution shifts.

🎯 Key Contributions

  1. Dual-Pathway Architecture: Combines fast intuitive processing (System 1) with deliberate analysis (System 2) via learned metacognitive gating
  2. Causal Feature Extraction: Uses Variational Information Bottleneck (VIB) to separate causal code features from spurious confounders
  3. Theoretical Guarantees: Formal proofs for causal convergence, abstraction hierarchy, calibration bounds, and adaptive compute
  4. OOD Robustness: Demonstrated generalization across different code repositories and organizations

πŸ“Š Results Summary

In-Distribution (Django Dataset)

Model F1 Harmonic ROC-AUC Balanced Acc
Ours (GCS2-V5) 0.500 Β± 0.014 0.776 Β± 0.018 0.663 Β± 0.011
CodeBERT 0.512 Β± 0.011 0.823 Β± 0.006 0.714 Β± 0.016
GraphCodeBERT 0.532 Β± 0.008 0.840 Β± 0.003 0.712 Β± 0.007

Out-of-Distribution (Optimum Dataset)

Model F1 Harmonic ROC-AUC ECE Relative Drop
Ours (GCS2-V5) 0.500 ± 0.014 0.776 ± 0.018 0.148 ± 0.044 ~0% 🟒
CodeBERT 0.490 Β± 0.024 0.759 Β± 0.018 - ~4%
GraphCodeBERT 0.515 Β± 0.013 0.772 Β± 0.009 0.232 Β± 0.015 ~3%

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Glass Box System 2 (V5)                      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                 β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                             β”‚
β”‚  β”‚   Code      β”‚    β”‚ Confounders β”‚                             β”‚
β”‚  β”‚  (Patch)    β”‚    β”‚  (11 dim)   β”‚                             β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜                             β”‚
β”‚         β”‚                  β”‚                                    β”‚
β”‚         β–Ό                  β–Ό                                    β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                             β”‚
β”‚  β”‚    GNN +    β”‚    β”‚  Spurious   β”‚                             β”‚
β”‚  β”‚  CodeBERT   β”‚    β”‚   Encoder   β”‚                             β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜                             β”‚
β”‚         β”‚                  β”‚                                    β”‚
β”‚         β–Ό                  β–Ό                                    β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                             β”‚
β”‚  β”‚ VIB Causal  β”‚    β”‚VIB Spurious β”‚                             β”‚
β”‚  β”‚   z_causal  β”‚    β”‚  z_spurious β”‚                             β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜                             β”‚
β”‚         β”‚                  β”‚                                    β”‚
β”‚         β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                    β”‚
β”‚                  β”‚                                              β”‚
β”‚                  β–Ό                                              β”‚
β”‚         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                       β”‚
β”‚         β”‚ Learned Fusionβ”‚  z_fused = (1-Ξ»)Β·z_c + λ·z_s          β”‚
β”‚         β”‚    Gate (Ξ»)   β”‚                                       β”‚
β”‚         β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜                                       β”‚
β”‚                 β”‚                                               β”‚
β”‚     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                   β”‚
β”‚     β”‚                       β”‚                                   β”‚
β”‚     β–Ό                       β–Ό                                   β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”                               β”‚
β”‚ β”‚System 1 β”‚           β”‚System 2 β”‚                               β”‚
β”‚ β”‚ (Fast)  β”‚           β”‚ (GRU)   β”‚                               β”‚
β”‚ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜           β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜                               β”‚
β”‚      β”‚                     β”‚                                    β”‚
β”‚      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                    β”‚
β”‚                 β”‚                                               β”‚
β”‚                 β–Ό                                               β”‚
β”‚         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                       β”‚
β”‚         β”‚ Metacognitive β”‚  p = (1-Ξ³)Β·p₁ + Ξ³Β·pβ‚‚                  β”‚
β”‚         β”‚   Gate (Ξ³)    β”‚                                       β”‚
β”‚         β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜                                       β”‚
β”‚                 β”‚                                               β”‚
β”‚                 β–Ό                                               β”‚
β”‚           [Prediction]                                          β”‚
β”‚                                                                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“ Repository Structure

glass-box-system2/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ model.py              # GraphCausalSystem2 architecture
β”‚   β”œβ”€β”€ data_engineering.py   # Data loading, labeling, preprocessing
β”‚   β”œβ”€β”€ training.py           # Training loop with theorem validation
β”‚   └── baselines/
β”‚       β”œβ”€β”€ codebert.py       # CodeBERT baseline
β”‚       β”œβ”€β”€ graphcodebert.py  # GraphCodeBERT baseline
β”‚       └── causal_methods.py # IRM, GroupDRO, VREx
β”œβ”€β”€ configs/
β”‚   └── default.yaml          # Default hyperparameters
β”œβ”€β”€ experiments/
β”‚   β”œβ”€β”€ within_org.py         # Within-organization experiments
β”‚   └── cross_org.py          # Cross-organization experiments
β”œβ”€β”€ results/
β”‚   β”œβ”€β”€ ood/                   # Out-of-distribution results
β”‚   β”œβ”€β”€ baselines/             # Baseline method results
β”‚   β”œβ”€β”€ codebert/              # CodeBERT results per seed
β”‚   └── graphcodebert/         # GraphCodeBERT results per seed
β”œβ”€β”€ docs/
β”‚   β”œβ”€β”€ theorems.md            # Mathematical proofs summary
β”‚   └── labeling.md            # Labeling methodology
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ run_training.py        # Main training script
β”‚   └── run_ood_eval.py        # OOD evaluation script
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ setup.py
└── README.md

πŸ”¬ Theoretical Foundations

Our model is grounded in four key theorems (see docs/theorems.md for full proofs):

Theorem 1: Causal Convergence

Under weak independence assumptions, our model recovers causal features with error bounded by:

E[(f_c(x) - fΜ‚_c(x))Β²] ≀ O(V log n/n) + O(1/Ξ») + r·√(d_xΒ·d_c)Β·LΒ²

Theorem 2: Abstraction Hierarchy

The residual architecture provides exponential representational capacity:

R(L) = O(L^d) or O((d+1)^L) regions

Theorem 3: Calibration Bound

The metacognitive gating achieves improved calibration:

ECE(pΜ‚) ≀ min{ECE(p₁), ECE(pβ‚‚)} + O(√(log(1/Ξ΄)/n))

Theorem 4: Adaptive Compute

Reasoning depth scales with problem difficulty:

T* = Ξ©(log(1/Ξ΅_target) / (1-d))

πŸš€ Quick Start

Installation

git clone https://github.com/YOUR_USERNAME/glass-box-system2.git
cd glass-box-system2
pip install -e .

Training

from src.model import create_model
from src.data_engineering import load_and_label_data, System2Dataset
from src.training import train_system2

# Load data
samples, labels, analyses, prs = load_and_label_data('data/train.json')

# Create dataset
dataset = System2Dataset(samples, labels, tokenizer, pdg_extractor)

# Create model
model = create_model(
    model_name="microsoft/codebert-base",
    num_features=11,
    hidden_dim=256,
    bottleneck_dim=128
)

# Train
model, history, results = train_system2(
    model, train_loader, val_loader, test_loader,
    num_epochs=10, lr=3e-5
)

OOD Evaluation

from src.training import dual_robustness_test

# Test robustness to distribution shift
robustness_results = dual_robustness_test(
    model, ood_loader, device,
    strengths=[0.0, 0.1, 0.25, 0.5]
)

πŸ“ˆ Reproducing Results

Multi-Seed Training

python scripts/run_training.py \
    --data_path data/django_train.json \
    --seeds 42 123 456 789 1024 \
    --epochs 10 \
    --output_dir results/

Baseline Comparisons

python scripts/run_baselines.py \
    --model codebert \
    --data_path data/django_train.json \
    --ood_path data/optimum.json

πŸ“Š Confounders (11 Features)

Feature Description Range
auth_hash Author identity (hashed) [0, 1]
sin_hour sin(hour/24Γ—2Ο€) temporal [0, 1]
cos_hour cos(hour/24Γ—2Ο€) temporal [0, 1]
complexity Patch length / 10000 [0, 1]
centrality AST PageRank centrality [0, 1]
time_to_merge Days to merge / 30 [0, 1]
code_churn (adds+dels)/files / 1000 [0, 1]
non_code_ratio Comments/strings ratio [0, 1]
review_depth Review count / 10 [0, 1]
files_changed File count / 50 [0, 1]
degree AST graph degree [0, 1]

🏷️ Labeling Methodology

We use a semantic-focused distant supervision approach that combines:

  1. PR-level signals: Issue links, fix keywords, test coupling
  2. Code-level signals: Semantic pattern detection (identifier swaps, wrong calls, etc.)
  3. Strict intersection: Requires BOTH semantic evidence AND PR intent

See docs/labeling.md for detailed statistics.

πŸ“„ License

This project is licensed under the MIT License - see LICENSE for details.

πŸ™ Acknowledgments

  • CodeBERT and GraphCodeBERT from Microsoft Research
  • Django, HuggingFace, and other open-source projects for evaluation data

About

Glass Box System 2: Causal Neural Networks for Bug Detection

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages