# ritam-05/cpu-scheduler-rl architecture

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

- Interactive diagram: https://gitdiagram.com/ritam-05/cpu-scheduler-rl
- Repository: https://github.com/ritam-05/cpu-scheduler-rl

## Overview

1. **What this repository is**
   - This is a small benchmark service for **CPU scheduling under RL / agent control**.
   - The core artifact is a **deterministic scheduling environment** plus a **grading layer** that scores agent decisions on three tasks.

2. **Primary runtime shape**
   - The system is centered on a **FastAPI application** exposed on port `7860`.
   - It is meant to run both **locally** and in **Docker / Hugging Face Spaces**.
   - `app.py` is the local entrypoint, while `server/app.py` and `openenv.yaml` support the hosted deployment path.

3. **Environment subsystem**
   - `env.py` is the heart of the benchmark: it models the CPU scheduling state and transitions.
   - The environment is deterministic, which matters for reproducible grading and for comparing policies fairly.
   - The expected interaction pattern is the standard control loop: **reset → inspect state → choose action → step → observe next state**.

4. **Task and benchmark definition**
   - `tasks.py` defines the **three graded tasks** referenced in the README.
   - These tasks likely parameterize the scheduling scenario and define what counts as a successful policy for each benchmark variant.
   - This is the main benchmark boundary: the same environment machinery is reused, but task configuration changes the evaluation target.

5. **Grading subsystem**
   - `grader.py` evaluates submissions against the task definitions and environment behavior.
   - The `GET /grade/{task_id}` endpoint exposes this scoring path over HTTP.
   - This is separate from the interactive environment API, so grading can be treated as a read-only evaluation layer.

6. **Service/API boundary**
   - The FastAPI service exposes the operational surface of the benchmark:
     - `GET /health`
     - `GET /tasks`
     - `POST /reset`
     - `POST /step`
     - `GET /state`
     - `GET /grade/{task_id}`
     - `GET /validate`
   - This creates a clean split between **clients** and the **simulation engine**: clients never manipulate internal state directly, only through API calls.

7. **Baseline agent / inference path**
   - `inference.py` is the baseline runner for exercising the benchmark with an OpenAI-compatible client.
   - It supports external model APIs such as **DeepSeek** via `API_BASE_URL`, `MODEL_NAME`, and `API_KEY` / `HF_TOKEN`.
   - If no credentials are present, it falls back to a **shortest-job-first heuristic**, which keeps the repo runnable for smoke tests without any model service.

8. **Agent abstraction**
   - `agent.py` likely contains the decision-making wrapper used by the baseline or evaluation harness.
   - Architecturally, this is the “policy” side of the system: it consumes state and emits scheduling actions.
   - That keeps agent logic decoupled from the environment and from the HTTP service.

9. **Testing boundary**
   - `tests/test_env.py` focuses on the environment behavior, which is the most important deterministic contract in the repo.
   - The test emphasis suggests the environment is treated as the source of truth, with correctness validated at the state-transition level rather than only through end-to-end API calls.

10. **Packaging and deployment**
   - `Dockerfile`, `requirements.txt`, `pyproject.toml`, and `uv.lock` define a reproducible Python runtime.
   - `openenv.yaml` and the README’s Hugging Face instructions indicate the project is designed to be deployed as an **OpenEnv-style benchmark service**.
   - `app_port: 7860` is a key deployment assumption for diagramming the runtime.

11. **Data flow to show in a diagram**
   - The main flow is: **client or agent → FastAPI endpoint → environment state machine → updated state/score → client**.
   - For grading, the flow is: **task selection → grader → environment/task definitions → score result**.
   - For baseline inference, the flow is: **inference.py → model API or SJF fallback → environment API → benchmark outcome**.

12. **Important architectural boundary**
   - The most important separation is between:
     - **benchmark logic** (`env.py`, `tasks.py`, `grader.py`)
     - **service transport** (`app.py`, `server/app.py`)
     - **client/agent execution** (`agent.py`, `inference.py`)
   - That boundary is what makes the repository reusable both as a local simulator and as a hosted benchmark endpoint.

Main entry points: [`inference.py`](https://github.com/ritam-05/cpu-scheduler-rl/blob/main/inference.py) (Inference runner), [`env.py`](https://github.com/ritam-05/cpu-scheduler-rl/blob/main/env.py) (Env state), [`app.py`](https://github.com/ritam-05/cpu-scheduler-rl/blob/main/app.py) (Local app), [`server/app.py`](https://github.com/ritam-05/cpu-scheduler-rl/blob/main/server/app.py) (Hosted app)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_benchmark["Benchmark logic"]
  node_core_benchmark["Benchmark core<br/>system boundary"]
  node_env_py["Env state<br/>deterministic env<br/>[env.py]"]
  node_tasks_py["Tasks<br/>task config<br/>[tasks.py]"]
  node_grader_py["Grader<br/>scorer<br/>[grader.py]"]
end

subgraph group_service["Service transport"]
  node_api_surface["API surface<br/>HTTP interface"]
  node_app_py["Local app<br/>entrypoint<br/>[app.py]"]
  node_server_app_py["Hosted app<br/>entrypoint<br/>[app.py]"]
end

subgraph group_clients["Client execution"]
  node_agent_py["Agent<br/>policy wrapper<br/>[agent.py]"]
  node_inference_py["Inference runner<br/>baseline runner<br/>[inference.py]"]
  node_model_api(("Model API<br/>external model"))
  node_sjf_fallback(("SJF fallback<br/>heuristic policy"))
end

subgraph group_deploy["Deployment"]
  node_dockerfile["Container build<br/>runtime image"]
  node_openenv_yaml["OpenEnv config<br/>deployment config<br/>[openenv.yaml]"]
  node_package_meta["Package setup<br/>python packaging"]
end

subgraph group_tests["Tests"]
  node_env_tests["Env tests<br/>test suite<br/>[test_env.py]"]
end

node_core_benchmark -->|"contains"| node_env_py
node_core_benchmark -->|"defines"| node_tasks_py
node_core_benchmark -->|"scores"| node_grader_py
node_api_surface -->|"drives"| node_env_py
node_api_surface -->|"lists"| node_tasks_py
node_api_surface -->|"exposes"| node_grader_py
node_app_py -->|"serves"| node_api_surface
node_server_app_py -->|"serves"| node_api_surface
node_agent_py -->|"acts on"| node_env_py
node_inference_py -->|"uses"| node_agent_py
node_inference_py -->|"calls"| node_model_api
node_inference_py -->|"falls back to"| node_sjf_fallback
node_inference_py -->|"drives"| node_api_surface
node_env_tests -->|"verifies"| node_env_py
node_dockerfile -->|"packages"| node_app_py
node_openenv_yaml -->|"deploys"| node_server_app_py
node_package_meta -.->|"supports"| node_app_py
node_package_meta -.->|"supports"| node_server_app_py

click node_env_py "https://github.com/ritam-05/cpu-scheduler-rl/blob/main/env.py"
click node_tasks_py "https://github.com/ritam-05/cpu-scheduler-rl/blob/main/tasks.py"
click node_grader_py "https://github.com/ritam-05/cpu-scheduler-rl/blob/main/grader.py"
click node_app_py "https://github.com/ritam-05/cpu-scheduler-rl/blob/main/app.py"
click node_server_app_py "https://github.com/ritam-05/cpu-scheduler-rl/blob/main/server/app.py"
click node_agent_py "https://github.com/ritam-05/cpu-scheduler-rl/blob/main/agent.py"
click node_inference_py "https://github.com/ritam-05/cpu-scheduler-rl/blob/main/inference.py"
click node_env_tests "https://github.com/ritam-05/cpu-scheduler-rl/blob/main/tests/test_env.py"
click node_dockerfile "https://github.com/ritam-05/cpu-scheduler-rl/tree/main/Dockerfile"
click node_openenv_yaml "https://github.com/ritam-05/cpu-scheduler-rl/blob/main/openenv.yaml"

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_core_benchmark,node_env_py,node_tasks_py,node_grader_py toneBlue
class node_api_surface,node_app_py,node_server_app_py toneAmber
class node_agent_py,node_inference_py,node_model_api,node_sjf_fallback toneMint
class node_dockerfile,node_openenv_yaml,node_package_meta toneRose
class node_env_tests toneIndigo
```

## Components

### Benchmark logic

Deterministic scheduling state, task definitions, and grading.

- **Benchmark core**: system boundary · The reusable CPU scheduling benchmark composed of environment, tasks, and grading.
- [Env state](https://github.com/ritam-05/cpu-scheduler-rl/blob/main/env.py): deterministic env · CPU scheduling state machine and reset/step transitions. · `env.py`
- [Tasks](https://github.com/ritam-05/cpu-scheduler-rl/blob/main/tasks.py): task config · Definitions for the three graded benchmark tasks. · `tasks.py`
- [Grader](https://github.com/ritam-05/cpu-scheduler-rl/blob/main/grader.py): scorer · Evaluates task submissions against the environment and task definitions. · `grader.py`

### Service transport

FastAPI entrypoints that expose the benchmark over HTTP.

- **API surface**: HTTP interface · The exposed benchmark operations for health, task listing, reset, step, state, grading, and validation.
- [Local app](https://github.com/ritam-05/cpu-scheduler-rl/blob/main/app.py): entrypoint · Local FastAPI entrypoint for running the service directly. · `app.py`
- [Hosted app](https://github.com/ritam-05/cpu-scheduler-rl/blob/main/server/app.py): entrypoint · Hosted deployment FastAPI app used by the server package. · `server/app.py`

### Client execution

Policy wrappers and baseline inference paths that drive the environment.

- [Agent](https://github.com/ritam-05/cpu-scheduler-rl/blob/main/agent.py): policy wrapper · Decision-making wrapper that turns state observations into scheduling actions. · `agent.py`
- [Inference runner](https://github.com/ritam-05/cpu-scheduler-rl/blob/main/inference.py): baseline runner · Exercises the benchmark with an OpenAI-compatible client or a heuristic fallback. · `inference.py`
- **Model API**: external model · External LLM endpoint used by the baseline runner when credentials are available.
- **SJF fallback**: heuristic policy · Short-job-first heuristic used when no model credentials are present.

### Deployment

Container and hosted-runtime configuration.

- [Container build](https://github.com/ritam-05/cpu-scheduler-rl/tree/main/Dockerfile): runtime image · Defines the reproducible containerized runtime for local or hosted execution. · `Dockerfile`
- [OpenEnv config](https://github.com/ritam-05/cpu-scheduler-rl/blob/main/openenv.yaml): deployment config · Hosted deployment configuration for the benchmark service. · `openenv.yaml`
- **Package setup**: python packaging · Project metadata and dependency lockfiles for reproducible installation.

### Tests

Deterministic contract checks for the environment.

- [Env tests](https://github.com/ritam-05/cpu-scheduler-rl/blob/main/tests/test_env.py): test suite · Validates the deterministic environment contract and state transitions. · `tests/test_env.py`

## Connections

- Benchmark core → Env state: contains · The benchmark core is implemented by the environment state machine.
- Benchmark core → Tasks: defines · Task variants parameterize the benchmark.
- Benchmark core → Grader: scores · The grader turns environment/task behavior into benchmark results.
- API surface → Env state: drives · HTTP calls reset, inspect, and advance the deterministic environment.
- API surface → Tasks: lists · The service exposes the available benchmark tasks.
- API surface → Grader: exposes · The grading endpoint invokes the scoring layer.
- Local app → API surface: serves · Local entrypoint wiring for the FastAPI service.
- Hosted app → API surface: serves · Hosted deployment wiring for the FastAPI service.
- Agent → Env state: acts on · The policy wrapper consumes observations and emits actions.
- Inference runner → Agent: uses · Baseline inference routes decisions through the agent abstraction.
- Inference runner → Model API: calls · Primary path uses an OpenAI-compatible external model endpoint.
- Inference runner → SJF fallback: falls back to · Heuristic scheduling is used when no credentials are available.
- Inference runner → API surface: drives · Baseline runner interacts with the benchmark through HTTP.
- Env tests → Env state: verifies · Tests focus on deterministic state transitions and reset/step behavior.
- Container build → Local app: packages · Container build runs the local application entrypoint.
- OpenEnv config → Hosted app: deploys · Hosted configuration targets the server package app.
- Package setup → Local app: supports · Packaging and dependency metadata support local execution.
- Package setup → Hosted app: supports · Packaging and dependency metadata support hosted execution.
