# jcthecoder200/pygym architecture

> Architecture diagram of the GitHub repository jcthecoder200/pygym, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jcthecoder200/pygym
- Repository: https://github.com/jcthecoder200/pygym

## Overview

pygym is an offline-first Python practice tool: students choose a built-in or library function, write an exercise solution in a terminal, CLI, tkinter, or notebook front end, and receive per-test feedback. The shared Session handles commands, the Grader evaluates code in a separate Sandbox process, and progress is tracked locally. Optional AI generation resolves targets, prompts a provider, validates exercises, and caches usable results. README and source-index references support the main connections; some front ends and AI internals were not excerpted, so those links rely on documented behavior or index references.

Main entry points: [`src/pygym/session.py`](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/session.py) (Session rules), [`src/pygym/ai/engine.py`](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/ai/engine.py) (Generation loop), [`src/pygym/menu.py`](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/menu.py) (CLI menu), [`src/pygym/ui.py`](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/ui.py) (UI dispatch)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry and front ends"]
  node_menu["CLI menu<br/>[menu.py]"]
  node_api["Python API<br/>[__init__.py]"]
  node_ui["UI dispatch<br/>[ui.py]"]
  node_editor["Terminal editor<br/>[editor.py]"]
  node_jupyter["Jupyter magics<br/>[jupyter.py]"]
end

subgraph group_practice["Exercise workflow"]
  node_session["Session rules<br/>[session.py]"]
  node_loader["Preset exercises<br/>[loader.py]"]
  node_models["Exercise contracts<br/>[models.py]"]
  node_grader["Test grader<br/>[grader.py]"]
  node_progress[("Local progress<br/>[progress.py]")]
  node_tips["Error tips<br/>[tips.py]"]
  node_config[("Settings<br/>[config.py]")]
end

subgraph group_ai["AI generation"]
  node_ai_engine["Generation loop<br/>[engine.py]"]
  node_targets["Target resolver<br/>[targets.py]"]
  node_prompt["Prompt builder<br/>[prompt.py]"]
  node_levels["Level rules<br/>[levels.py]"]
  node_validation["AI validation<br/>[validate.py]"]
  node_cache[("Exercise cache<br/>[cache.py]")]
  node_providers["Model providers<br/>[providers.py]"]
end

subgraph group_runtime["Execution and safety"]
  node_sandbox["Sandbox<br/>[sandbox.py]"]
  node_runner["Runner process<br/>[_runner.py]"]
end

node_student(("Student"))
node_provider_service{{"Model service"}}

node_student -->|"chooses exercise"| node_menu
node_student -->|"starts practice"| node_api
node_menu -->|"starts UI"| node_ui
node_api -->|"launches UI"| node_ui
node_ui -->|"runs editor"| node_editor
node_ui -->|"registers magics"| node_jupyter
node_editor -->|"sends commands"| node_session
node_jupyter -.->|"handles commands"| node_session
node_student -->|"submits code"| node_session
node_session -->|"loads exercises"| node_loader
node_session -->|"requests grading"| node_grader
node_session -->|"updates progress"| node_progress
node_session -->|"uses contracts"| node_models
node_session -->|"loads settings"| node_config
node_session -->|"requests generation"| node_ai_engine
node_grader -->|"runs checks"| node_sandbox
node_sandbox -->|"spawns process"| node_runner
node_student -->|"gets error tips"| node_tips
node_menu -->|"reads settings"| node_config
node_menu -->|"shows tips"| node_tips
node_ai_engine -->|"resolves target"| node_targets
node_ai_engine -->|"builds prompt"| node_prompt
node_ai_engine -->|"requests completion"| node_providers
node_providers -->|"calls model"| node_provider_service
node_ai_engine -->|"validates reply"| node_validation
node_ai_engine -->|"stores exercise"| node_cache
node_prompt -->|"uses level rules"| node_levels
node_validation -->|"checks solution"| node_grader
node_validation -->|"checks safety"| node_sandbox
node_targets -->|"checks imports"| node_sandbox

click node_menu "https://github.com/jcthecoder200/pygym/blob/main/src/pygym/menu.py"
click node_api "https://github.com/jcthecoder200/pygym/blob/main/src/pygym/__init__.py"
click node_ui "https://github.com/jcthecoder200/pygym/blob/main/src/pygym/ui.py"
click node_editor "https://github.com/jcthecoder200/pygym/blob/main/src/pygym/editor.py"
click node_jupyter "https://github.com/jcthecoder200/pygym/blob/main/src/pygym/jupyter.py"
click node_session "https://github.com/jcthecoder200/pygym/blob/main/src/pygym/session.py"
click node_loader "https://github.com/jcthecoder200/pygym/blob/main/src/pygym/loader.py"
click node_models "https://github.com/jcthecoder200/pygym/blob/main/src/pygym/models.py"
click node_grader "https://github.com/jcthecoder200/pygym/blob/main/src/pygym/grader.py"
click node_progress "https://github.com/jcthecoder200/pygym/blob/main/src/pygym/progress.py"
click node_tips "https://github.com/jcthecoder200/pygym/blob/main/src/pygym/tips.py"
click node_config "https://github.com/jcthecoder200/pygym/blob/main/src/pygym/config.py"
click node_ai_engine "https://github.com/jcthecoder200/pygym/blob/main/src/pygym/ai/engine.py"
click node_targets "https://github.com/jcthecoder200/pygym/blob/main/src/pygym/ai/targets.py"
click node_prompt "https://github.com/jcthecoder200/pygym/blob/main/src/pygym/ai/prompt.py"
click node_levels "https://github.com/jcthecoder200/pygym/blob/main/src/pygym/ai/levels.py"
click node_validation "https://github.com/jcthecoder200/pygym/blob/main/src/pygym/ai/validate.py"
click node_cache "https://github.com/jcthecoder200/pygym/blob/main/src/pygym/ai/cache.py"
click node_providers "https://github.com/jcthecoder200/pygym/blob/main/src/pygym/ai/providers.py"
click node_sandbox "https://github.com/jcthecoder200/pygym/blob/main/src/pygym/sandbox.py"
click node_runner "https://github.com/jcthecoder200/pygym/blob/main/src/pygym/_runner.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_menu,node_api,node_ui,node_editor,node_jupyter toneBlue
class node_session,node_loader,node_models,node_grader,node_progress,node_tips,node_config toneAmber
class node_ai_engine,node_targets,node_prompt,node_levels,node_validation,node_cache,node_providers toneMint
class node_sandbox,node_runner toneRose
class node_student,node_provider_service toneIndigo
```

## Components

### Entry and front ends

- [CLI menu](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/menu.py): `src/pygym/menu.py`
- [Python API](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/__init__.py): `src/pygym/__init__.py`
- [UI dispatch](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/ui.py): `src/pygym/ui.py`
- [Terminal editor](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/editor.py): `src/pygym/editor.py`
- [Jupyter magics](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/jupyter.py): `src/pygym/jupyter.py`

### Exercise workflow

- [Session rules](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/session.py): `src/pygym/session.py`
- [Preset exercises](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/loader.py): `src/pygym/loader.py`
- [Exercise contracts](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/models.py): `src/pygym/models.py`
- [Test grader](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/grader.py): `src/pygym/grader.py`
- [Local progress](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/progress.py): `src/pygym/progress.py`
- [Error tips](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/tips.py): `src/pygym/tips.py`
- [Settings](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/config.py): `src/pygym/config.py`

### AI generation

- [Generation loop](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/ai/engine.py): `src/pygym/ai/engine.py`
- [Target resolver](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/ai/targets.py): `src/pygym/ai/targets.py`
- [Prompt builder](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/ai/prompt.py): `src/pygym/ai/prompt.py`
- [Level rules](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/ai/levels.py): `src/pygym/ai/levels.py`
- [AI validation](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/ai/validate.py): `src/pygym/ai/validate.py`
- [Exercise cache](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/ai/cache.py): `src/pygym/ai/cache.py`
- [Model providers](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/ai/providers.py): `src/pygym/ai/providers.py`

### Execution and safety

- [Sandbox](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/sandbox.py): `src/pygym/sandbox.py`
- [Runner process](https://github.com/jcthecoder200/pygym/blob/main/src/pygym/_runner.py): `src/pygym/_runner.py`

### Other components

- **Student**
- **Model service**

## Connections

- Student → CLI menu: chooses exercise
- Student → Python API: starts practice
- CLI menu → UI dispatch: starts UI (evidence: `src/pygym/menu.py`)
- Python API → UI dispatch: launches UI (evidence: `src/pygym/__init__.py`)
- UI dispatch → Terminal editor: runs editor (evidence: `src/pygym/ui.py`)
- UI dispatch → Jupyter magics: registers magics (evidence: `src/pygym/ui.py`)
- Terminal editor → Session rules: sends commands (evidence: `src/pygym/editor.py`)
- Jupyter magics → Session rules: handles commands
- Student → Session rules: submits code
- Session rules → Preset exercises: loads exercises (evidence: `src/pygym/session.py`)
- Session rules → Test grader: requests grading (evidence: `src/pygym/session.py`)
- Session rules → Local progress: updates progress (evidence: `src/pygym/session.py`)
- Session rules → Exercise contracts: uses contracts (evidence: `src/pygym/session.py`)
- Session rules → Settings: loads settings (evidence: `src/pygym/session.py`)
- Session rules → Generation loop: requests generation (evidence: `src/pygym/session.py`)
- Test grader → Sandbox: runs checks (evidence: `src/pygym/grader.py`)
- Sandbox → Runner process: spawns process (evidence: `src/pygym/sandbox.py`)
- Student → Error tips: gets error tips
- CLI menu → Settings: reads settings (evidence: `src/pygym/menu.py`)
- CLI menu → Error tips: shows tips (evidence: `src/pygym/menu.py`)
- Generation loop → Target resolver: resolves target (evidence: `src/pygym/ai/engine.py`)
- Generation loop → Prompt builder: builds prompt (evidence: `src/pygym/ai/engine.py`)
- Generation loop → Model providers: requests completion (evidence: `src/pygym/ai/engine.py`)
- Model providers → Model service: calls model (evidence: `src/pygym/ai/providers.py`)
- Generation loop → AI validation: validates reply (evidence: `src/pygym/ai/engine.py`)
- Generation loop → Exercise cache: stores exercise (evidence: `src/pygym/ai/engine.py`)
- Prompt builder → Level rules: uses level rules (evidence: `src/pygym/ai/prompt.py`)
- AI validation → Test grader: checks solution (evidence: `src/pygym/ai/validate.py`)
- AI validation → Sandbox: checks safety (evidence: `src/pygym/ai/validate.py`)
- Target resolver → Sandbox: checks imports (evidence: `src/pygym/ai/targets.py`)
