A Python-based platform for interactive technical mastery via AST mutation and shadow-worktree isolation.
Experience the engine immediately without writing code. This demo uses Developer Mode (pre-loaded reference implementations) to simulate a user solving the curriculum.
# 1. Clone and Enter Repository
git clone https://github.com/ImmortalDemonGod/mastery-engine.git
cd mastery-engine
# 2. Activate Developer Mode (REQUIRED: Creates cs336_basics symlink)
./scripts/mode switch developer
# 3. Install Dependencies
pip install uv
uv sync
uv pip install -e .
# 4. Initialize a Curriculum
uv run mastery init cs336_a1
# 5. View Current Module
uv run mastery show
# Output: Displays build prompt with problem description
# 6. Run Build Validation (Watch the engine validate the reference implementation)
uv run mastery submit
# Output: โ
All tests passed! (25/25 test cases in 0.15s)
# 7. Answer Justify Questions (Auto-passes in mock mode without OpenAI key)
uv run mastery submit
# Output: ๐ญ MOCK MODE: Auto-passing justify stage
# 8. Inject Runtime Bug (Watch the engine mutate the code)
uv run mastery start-challenge
# Output: ๐ Injecting semantic bug 'off_by_one_loop'...
# 9. See the Bug Fail
uv run mastery submit
# Output: โ Test failed: Expected 8, got 7 (off-by-one error)What Just Happened?
You witnessed the complete Build-Justify-Harden loop:
- Build: Validated a working implementation
- Justify: Evaluated conceptual understanding (mocked without API key)
- Harden: Injected a semantic bug and detected the failure
No OpenAI API key required for demo - the engine operates in mock mode automatically.
| Feature | Description | Implementation |
|---|---|---|
| Runtime AST Mutation | Parses Python code into an Abstract Syntax Tree, identifies semantic patterns (e.g., loop termination conditions), and surgically injects logic bugsโnot just syntax errors. Students debug realistic mistakes. | engine/ast_harden/generic_injector.py |
| Process Isolation via Shadow Worktrees | Solves the "dirty state" problem by executing user code in ephemeral Git shadow worktrees. Each harden challenge runs in an isolated filesystem copy, preventing data corruption and enabling safe rollback. | engine/workspace.py |
| Socratic LLM Evaluation | Uses GPT-4o with Chain-of-Thought prompting to evaluate natural language justifications against technical rubrics. Detects surface-level answers via keyword filtering before calling the LLM. | engine/services/llm_service.py |
| Automated Content Pipeline | Generated 38+ competitive programming problems by parsing unstructured LeetCode data into structured JSON schemas. Demonstrates data engineering at scale. | scripts/generate_module.py |
| Curriculum-Agnostic Architecture | Supports multiple curricula (Deep Learning, Competitive Programming, Job Prep) with both LINEAR (sequential) and LIBRARY (freeform) modes. Curricula are hot-swappable via manifest files. | engine/curriculum.py |
1. cs336_a1 - Stanford CS336: Language Modeling (LINEAR)
- Target: Deep learning practitioners building transformers from scratch
- Modules: 21 modules (BPE Tokenizer โ Full Training Loop)
- Tech Stack: PyTorch, einops, tiktoken
- Completion Time: ~60 hours
- Target: Software engineers preparing for algorithmic interviews
- Problems: 38 LeetCode problems across 19 patterns
- Demonstration: Showcases automated content generation pipeline
- Status: Breadth-first scaffolding complete
- Target: Candidates preparing for coding assessments
- Modules: 3 modules (HTTP, HTML Parsing, 2D Grids)
- Focus: Forensic pedagogy - each module targets a documented failure mode
- Completion Time: ~3 hours
- Target: Competitive programmers learning Python idioms
- Module:
std_lib_augmentation(deque, heapq, bisect) - Status: Priority module complete
The engine's pedagogical core is a three-stage loop designed for deep, resilient mastery:
Write code that passes functional tests.
uv run mastery submit
# โ Runs validator.sh, checks correctness and performanceAnswer Socratic questions to prove you understand why your code works.
uv run mastery submit
# โ Opens $EDITOR for natural language responses
# โ LLM evaluates depth of understanding (or auto-passes in mock mode)Debug a bug that was surgically injected into your correct code via AST transformation.
uv run mastery start-challenge
# โ Creates shadow worktree with mutated code
# โ Common mistakes: off-by-one, missing base case, type confusion
uv run mastery submit
# โ Validates the fix in isolated environmentWhy This Works:
- Build ensures functional competence
- Justify prevents "interview syndrome" (can code but can't explain)
- Harden builds debugging intuition for production systems
Main Repository
โโโ modes/student/ โ Your workspace (stubs)
โโโ modes/developer/ โ Reference implementations
Shadow Worktree (ephemeral)
โโโ .mastery_engine_worktree/
โโโ modes/student/ โ Isolated copy with injected bug
Workflow:
- User completes build + justify in main repo
start-challengecreates shadow worktree from main repo- AST injector mutates code in shadow copy
- User debugs in shadow worktree
- On success, shadow is deleted (clean slate for next bug)
Original Code:
def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right: # โ
Correct termination
...After Injection (off_by_one bug):
def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left < right: # โ Misses single-element case
...The AST injector:
- Parses code into syntax tree
- Locates
Comparenodes with<=operator - Replaces with
<operator - Regenerates Python code
Result: Syntactically valid but semantically broken code.
- Python 3.10+
- uv (fast Python package manager)
- Git
Platform: Linux, macOS, or Windows WSL2
Why? This engine uses POSIX features (bash scripts, symlinks via ln -s, git worktree).
Windows Users: Install WSL2 (click to expand)
# PowerShell (as Administrator)
wsl --install
wsl --set-default-version 2Then continue installation inside WSL.
# Clone the repository
git clone https://github.com/ImmortalDemonGod/mastery-engine.git
cd mastery-engine
# Install dependencies
pip install uv
uv sync
# Install in editable mode
uv pip install -e .
# Verify installation
uv run mastery --version# Set in environment
export OPENAI_API_KEY="sk-..."
# Or create .env file
echo "OPENAI_API_KEY=sk-..." > .envNote: The engine operates in mock mode without an API key (auto-passes justify stage).
# 1. Choose a mode
./scripts/mode switch student # Start with stubs (typical user)
./scripts/mode switch developer # Use reference implementations (demo)
# 2. Initialize a curriculum
uv run mastery init cs336_a1
# 3. View current assignment
uv run mastery show
# 4. Work through the BJH loop
uv run mastery submit # Build stage
uv run mastery submit # Justify stage
uv run mastery start-challenge # Harden stage (injects bug)
uv run mastery submit # Fix the bug# List all modules in curriculum
uv run mastery curriculum-list
# Reset a specific module
uv run mastery progress-reset <module_id>
# Check current status
uv run mastery status
# Cleanup artifacts
uv run mastery cleanupThe engine has 78% test coverage (145 passing tests):
# Run all tests
uv run pytest
# Run with coverage
uv run pytest --cov=engine --cov-report=html
# Run specific test file
uv run pytest tests/engine/test_curriculum.py -v- Unit Tests: Engine components (
test_curriculum.py,test_workspace.py) - Integration Tests: Full workflows (
test_main_workflows_real.py) - E2E Tests: Error handling (
test_error_handling.py)
mastery-engine/
โโโ engine/ # Core engine
โ โโโ curriculum.py # Curriculum loading and validation
โ โโโ workspace.py # Shadow worktree management
โ โโโ main.py # CLI entry point
โ โโโ ast_harden/ # AST mutation engine
โ โ โโโ generic_injector.py
โ โโโ services/
โ โโโ llm_service.py # LLM evaluation (with mock mode)
โโโ curricula/ # Curriculum content
โ โโโ cs336_a1/ # Deep learning curriculum
โ โโโ cp_accelerator/ # Competitive programming (LIBRARY mode)
โ โโโ job_prep_data_annotation/ # Job prep curriculum
โ โโโ python_for_cp/ # Python for CP (partial)
โโโ modes/
โ โโโ student/ # Stub implementations
โ โโโ developer/ # Reference solutions
โโโ scripts/
โ โโโ mode # Mode switcher script
โ โโโ generate_module.py # Content generation pipeline
โ โโโ generate_manifest.py # Manifest generator
โโโ tests/ # Test suite (145 tests, 78% coverage)
โโโ docs/ # Documentation
Every claim is executable. "You'll learn X" โ "Run this validator to prove you learned X."
The engine aggressively validates at each stage. Passing justify โ memorization; it requires explaining trade-offs.
Harden bugs simulate real mistakes (not contrived errors). Example: using list.pop(0) in BFS instead of deque.popleft() - syntactically correct, O(nยฒ) performance trap.
The engine doesn't care if you're learning transformers or competitive programming. Curricula are data, not code.
job_prep_data_annotation was designed by analyzing actual assessment failures and creating modules that surgically address each failure mode.
See docs/current/BUG_INJECTION_GUIDE.md for curriculum authoring guidelines.
Adding a New Curriculum:
- Create
curricula/<name>/manifest.json - Define modules with build prompts, validators, justify questions
- Create harden bugs as
.patchfiles - Test with
uv run mastery init <name>
This project is a dual-composition of original systems engineering and adapted educational content.
The core architecture is original engineering work licensed under the MIT License:
- CLI Orchestration: Command-line interface and workflow management (
engine/main.py) - AST Mutation Engine: Runtime code transformation system (
engine/ast_harden/) - Shadow Worktree System: Process isolation via git internals (
engine/workspace.py) - Validation Pipeline: Test harness and LLM evaluation (
engine/validator.py,engine/services/) - Curriculum Generation: Automated content pipeline (
scripts/generate_module.py)
Educational materials are adapted from the following sources:
-
Deep Learning (cs336_a1): Problem statements and mathematical specifications adapted from Stanford CS336 (Spring 2024). Reference implementations derive from course assignments. The "Justify" questions and "Harden" bug patterns were engineered specifically for this platform.
-
Competitive Programming (cp_accelerator): Problem taxonomies derived from LeetCode and DSA Taxonomies. Problem descriptions remain property of respective authors.
-
Python Basics (python_for_cp, job_prep_data_annotation): Concepts adapted from 30 Days of Python by Asabeneh Yetayeh (MIT License).
See individual curriculum READMEs for detailed attribution.
Our Contribution: The engineering achievement is the system architecture that enables runtime AST mutation, shadow worktree isolation, and cost-optimized LLM validationโnot the specific curriculum problems.
License: See LICENSE and NOTICE files for complete terms.
GitHub: @ImmortalDemonGod
Project: mastery-engine
Issues: Report bugs or request features
Built with Python ๐ | Powered by uv โก | Engineered for Mastery ๐ฏ