# abhay-ugale-25/proctor-sandbox architecture

> Architecture diagram of the GitHub repository abhay-ugale-25/proctor-sandbox, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/abhay-ugale-25/proctor-sandbox
- Repository: https://github.com/abhay-ugale-25/proctor-sandbox

## Overview

#### Purpose and Entry Points
This is a FastAPI backend for proctored coding assessments: it judges untrusted multi-language submissions in Docker and records webcam-derived proctoring anomalies. The API starts at `main.py`; the independent webcam/MediaPipe process starts at `app/ml_engine/main.py`.

#### API and Session Orchestration
REST routes create users and sessions, accept code submissions and proctor logs, end sessions, and retrieve session history. The submission route synchronously bridges API input to the sandbox executor; the proctoring process independently POSTs events back to the API.
- `main.py`
- `app/api/routes.py`
- `app/api/schemas.py`

#### Persistent Domain Model
SQLAlchemy persists users, exam sessions, submission results, and proctoring events in local SQLite. `CodeSubmission` and `ProctoringLog` are associated with `Session`, making a session the aggregation boundary for judge history and anomaly timeline.
- `app/core/models.py`
- `app/core/database.py`
- `proctor.db`

#### Sandboxed Judge
`run_code()` dispatches Python, Java, C, or C++ source to a fresh Docker container. The container is disposable, network-disabled, memory-limited, and returns compile/run output plus pass/fail status to the API for persistence. Docker availability and the prebuilt `proctor-sandbox` image are required runtime dependencies.
- `app/sandbox/executor.py`
- `app/sandbox/Dockerfile`
- `app/api/routes.py`

#### Proctoring Capture and Detection Pipeline
A separate local process captures webcam frames and uses the bundled MediaPipe FaceLandmarker model. It calibrates an initial nose-position baseline, derives eye-aspect and gaze ratios, then emits threshold-based drowsiness, distraction, slouching, or no-face detections; this is heuristic CV rather than a trained classifier.
- `app/ml_engine/main.py`
- `app/ml_engine/face_mesh.py`
- `app/ml_engine/face_landmarker.task`

#### Event Computation and API Logging Boundary
Landmark calculations feed detection state in the camera loop. Detected anomalies pass through a logger that rate-limits each event type with a cooldown before HTTP POSTing to `/api/proctor-logs/`; the API owns validation and durable storage. The logger currently depends on a configured/hardcoded session identity, coupling the external ML process to an existing API session.
- `app/ml_engine/calculations.py`
- `app/ml_engine/proctor_logger.py`
- `app/api/routes.py`

#### Runtime Boundaries and External Services
The API runs on FastAPI/Uvicorn with Pydantic, SQLAlchemy, and SQLite. The judge requires the Docker daemon and an Ubuntu-based image containing language toolchains. The proctoring runtime requires a webcam, OpenCV, MediaPipe, and HTTP access to the local API; it is operationally separate from the API server.

Main entry points: [`app/api/routes.py`](https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/api/routes.py) (REST routes), [`app/ml_engine/main.py`](https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/ml_engine/main.py) (Camera loop), [`app/sandbox/executor.py`](https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/sandbox/executor.py) (Submission executor), [`app/ml_engine/face_mesh.py`](https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/ml_engine/face_mesh.py) (Face landmarks)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API service"]
  node_api_entry["FastAPI entry<br/>FastAPI/Uvicorn<br/>[main.py]"]
  node_routes["REST routes<br/>route handlers<br/>[routes.py]"]
  node_schemas["Request schemas<br/>Pydantic schemas<br/>[schemas.py]"]
end

subgraph group_persistence["Persistence"]
  node_models["Domain model<br/>SQLAlchemy models<br/>[models.py]"]
  node_database["Database access<br/>SQLAlchemy<br/>[database.py]"]
  node_sqlite[("Assessment data<br/>SQLite database<br/>[proctor.db]")]
end

subgraph group_judge["Sandboxed judge"]
  node_executor["Submission executor<br/>language dispatcher<br/>[executor.py]"]
  node_sandbox_image["Sandbox image<br/>Docker image"]
  node_docker{{"Docker daemon<br/>runtime dependency"}}
end

subgraph group_proctoring["Proctoring process"]
  node_ml_entry["Camera loop<br/>ML process entry<br/>[main.py]"]
  node_face_mesh["Face landmarks<br/>MediaPipe pipeline<br/>[face_mesh.py]"]
  node_landmarker_model["FaceLandmarker model<br/>MediaPipe task model"]
  node_calculations["Anomaly detection<br/>heuristic CV<br/>[calculations.py]"]
  node_proctor_logger["Event logger<br/>HTTP client<br/>[proctor_logger.py]"]
end

node_webcam{{"Webcam<br/>capture device"}}

node_api_entry -->|"serves"| node_routes
node_routes -->|"validates requests"| node_schemas
node_routes -->|"creates and queries"| node_models
node_models -->|"mapped through"| node_database
node_database -->|"persists"| node_sqlite
node_routes -->|"judges submissions"| node_executor
node_executor -->|"creates container"| node_docker
node_docker -->|"runs"| node_sandbox_image
node_executor -->|"compile/run result"| node_routes
node_webcam -->|"frames"| node_ml_entry
node_ml_entry -->|"extracts landmarks"| node_face_mesh
node_landmarker_model -->|"loaded by"| node_face_mesh
node_face_mesh -->|"landmarks and baseline"| node_calculations
node_calculations -->|"detection state"| node_ml_entry
node_ml_entry -->|"detected anomalies"| node_proctor_logger
node_proctor_logger -->|"POST /api/proctor-logs/"| node_routes

click node_api_entry "https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/main.py"
click node_routes "https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/api/routes.py"
click node_schemas "https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/api/schemas.py"
click node_models "https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/core/models.py"
click node_database "https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/core/database.py"
click node_sqlite "https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/proctor.db"
click node_executor "https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/sandbox/executor.py"
click node_sandbox_image "https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/sandbox/Dockerfile"
click node_ml_entry "https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/ml_engine/main.py"
click node_face_mesh "https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/ml_engine/face_mesh.py"
click node_landmarker_model "https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/ml_engine/face_landmarker.task"
click node_calculations "https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/ml_engine/calculations.py"
click node_proctor_logger "https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/ml_engine/proctor_logger.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_api_entry,node_routes,node_schemas toneBlue
class node_models,node_database,node_sqlite toneAmber
class node_executor,node_sandbox_image,node_docker toneMint
class node_ml_entry,node_face_mesh,node_landmarker_model,node_calculations,node_proctor_logger toneRose
class node_webcam toneNeutral
```

## Components

### API service

- [FastAPI entry](https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/main.py): FastAPI/Uvicorn · `main.py`
- [REST routes](https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/api/routes.py): route handlers · Sessions, submissions, proctor logs, and history. · `app/api/routes.py`
- [Request schemas](https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/api/schemas.py): Pydantic schemas · `app/api/schemas.py`

### Persistence

- [Domain model](https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/core/models.py): SQLAlchemy models · Session aggregates submissions and proctoring events. · `app/core/models.py`
- [Database access](https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/core/database.py): SQLAlchemy · `app/core/database.py`
- [Assessment data](https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/proctor.db): SQLite database · `proctor.db`

### Sandboxed judge

- [Submission executor](https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/sandbox/executor.py): language dispatcher · Runs Python, Java, C, and C++ submissions. · `app/sandbox/executor.py`
- [Sandbox image](https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/sandbox/Dockerfile): Docker image · Disposable network-disabled, memory-limited execution environment. · `app/sandbox/Dockerfile`
- **Docker daemon**: runtime dependency

### Proctoring process

- [Camera loop](https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/ml_engine/main.py): ML process entry · Independent webcam capture and event loop. · `app/ml_engine/main.py`
- [Face landmarks](https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/ml_engine/face_mesh.py): MediaPipe pipeline · Calibrates nose baseline and extracts face landmarks. · `app/ml_engine/face_mesh.py`
- [FaceLandmarker model](https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/ml_engine/face_landmarker.task): MediaPipe task model · `app/ml_engine/face_landmarker.task`
- [Anomaly detection](https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/ml_engine/calculations.py): heuristic CV · Uses eye-aspect, gaze, and posture ratios. · `app/ml_engine/calculations.py`
- [Event logger](https://github.com/abhay-ugale-25/proctor-sandbox/blob/main/app/ml_engine/proctor_logger.py): HTTP client · Cooldown-limits event types before posting with a configured session identity. · `app/ml_engine/proctor_logger.py`

### Other components

- **Webcam**: capture device

## Connections

- FastAPI entry → REST routes: serves
- REST routes → Request schemas: validates requests
- REST routes → Domain model: creates and queries
- Domain model → Database access: mapped through
- Database access → Assessment data: persists
- REST routes → Submission executor: judges submissions · Synchronous API-to-executor bridge.
- Submission executor → Docker daemon: creates container
- Docker daemon → Sandbox image: runs
- Submission executor → REST routes: compile/run result
- Webcam → Camera loop: frames
- Camera loop → Face landmarks: extracts landmarks
- FaceLandmarker model → Face landmarks: loaded by
- Face landmarks → Anomaly detection: landmarks and baseline
- Anomaly detection → Camera loop: detection state
- Camera loop → Event logger: detected anomalies
- Event logger → REST routes: POST /api/proctor-logs/ · Uses configured session identity.
