| # π ARCHITECTURE.md β System Design & Component Architecture |
|
|
| --- |
|
|
| ## π§ Overview |
|
|
| This document defines the **system architecture** for the AI Community Moderation Environment. |
|
|
| The system is designed to: |
|
|
| * comply with **OpenEnv specification** |
| * support **multi-step agent interaction** |
| * provide **deterministic evaluation** |
| * be easily deployable via **FastAPI + Docker** |
|
|
| --- |
|
|
| ## ποΈ High-Level Architecture |
|
|
| ### πΉ Updated β Agent layer added |
|
|
| ```text |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
| β Agent Layer β |
| β β |
| β ββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββ β |
| β β Gemini 2.5 Flash β β Rule-based Baseline Agent β β |
| β β (agent/gemini_ β β (baseline/agent.py) β β |
| β β agent.py) β β β β |
| β ββββββββββββ¬ββββββββββββ βββββββββββββββ¬βββββββββββββββββ β |
| β β /reset /step /agent/run β /baseline β |
| ββββββββββββββββΌβββββββββββββββββββββββββββββββΌββββββββββββββββββββ |
| β β |
| βΌ βΌ |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
| β FastAPI API (api/app.py) β |
| β /reset /step /state /tasks /grader /baseline /agent/run β |
| ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ |
| β |
| ββββββββββββββββββββββΌβββββββββββββββββββββ |
| β Environment Core (env/) β |
| βββββββββ¬ββββββββββββββββββ¬ββββββββββββββββ |
| β β |
| βββββββββββΌβββββββ βββββββββΌββββββββ |
| β State Manager β β Reward Engine β |
| βββββββββββ¬βββββββ βββββββββββββββββ |
| β |
| βββββββββββΌβββββββββββ |
| β Policy Engine β |
| βββββββββββ¬βββββββββββ |
| β |
| βββββββββββΌβββββββββββ |
| β Data Generator β |
| βββββββββββββββββββββββ |
| |
| βββββββββββββββββββββββββββββββββββββββ |
| β Grader Engine β |
| βββββββββββββββββββββββββββββββββββββββ |
| ``` |
|
|
| --- |
|
|
| ## π¦ Core Modules |
|
|
| --- |
|
|
| ## 1. π§ Environment Core (`env/`) |
|
|
| ### Responsibility |
|
|
| Implements OpenEnv interface: |
|
|
| * `reset()` |
| * `step(action)` |
| * `state()` |
|
|
| ### Key File |
|
|
| ```python id="f36n7z" |
| env/moderation_env.py |
| ``` |
|
|
| ### Responsibilities |
|
|
| * orchestrates entire flow |
| * maintains episode lifecycle |
| * integrates all sub-components |
|
|
| --- |
|
|
| ## 2. π State Manager (`env/state_manager.py`) |
| |
| ### Responsibility |
| |
| Handles: |
| |
| * current state representation |
| * updates after each action |
| |
| ### State Includes |
| |
| * post content |
| * user history |
| * reports |
| * geo |
| * context |
| |
| --- |
| |
| ## 3. π§Ύ Policy Engine (`env/policy_engine.py`) |
|
|
| ### Responsibility |
|
|
| * implements rules from `POLICY.md` |
| * computes: |
|
|
| * violation type |
| * severity |
| * expected action |
|
|
| ### Key Function |
|
|
| ```python id="0w8e3u" |
| def evaluate_policy(state) -> dict: |
| return { |
| "violation_type": ..., |
| "severity": ..., |
| "expected_action": ... |
| } |
| ``` |
|
|
| --- |
|
|
| ## 4. 𧬠Data Generator (`env/data_generator.py`) |
| |
| ### Responsibility |
| |
| * generates synthetic moderation scenarios |
| * ensures deterministic outputs |
| |
| ### Features |
| |
| * template-based post generation |
| * context simulation (history, reports, geo) |
| * seed-controlled reproducibility |
| |
| --- |
| |
| ## 5. π Reward Engine (`env/reward_engine.py`) |
|
|
| ### Responsibility |
|
|
| * computes step-wise rewards |
| * aligns with `REWARD.md` |
|
|
| ### Input |
|
|
| * current state |
| * action |
| * ground truth |
|
|
| --- |
|
|
| ## 6. π§ͺ Grader Engine (`graders/`) |
|
|
| ### Responsibility |
|
|
| * computes final episode score |
| * aligns with `GRADERS.md` |
|
|
| ### Key File |
|
|
| ```python id="4bqj7f" |
| graders/grader.py |
| ``` |
|
|
| --- |
|
|
| ## 7. π API Layer (`api/`) |
|
|
| ### Framework |
|
|
| * FastAPI |
|
|
| ### Responsibilities |
|
|
| Expose endpoints: |
|
|
| | Endpoint | Function | |
| | ------------- | -------------------------------- | |
| | `/reset` | start new episode | |
| | `/step` | take action | |
| | `/state` | current state | |
| | `/tasks` | list tasks | |
| | `/grader` | compute score | |
| | `/baseline` | run rule-based baseline agent | |
| | `/agent/run` | run Gemini 2.5 Flash agent πΉ | |
| | `/health` | liveness check | |
|
|
| --- |
|
|
| ## 8. π€ Baseline Agent (`baseline/`) |
|
|
| ### Responsibility |
|
|
| * runs simple rule-based heuristic agent |
| * produces reproducible benchmark without any LLM dependency |
|
|
| --- |
|
|
| ## 9. π§ Gemini Agent (`agent/`) β πΉ Added |
|
|
| ### Responsibility |
|
|
| * LLM-driven agent using **Gemini 2.5 Flash** (google-genai SDK) |
| * interacts with the environment via the same `/reset` and `/step` API |
| * uses multi-turn chat with a structured system prompt |
| * parses JSON action responses; falls back to `escalate` on parse failure |
|
|
| ### Key Files |
|
|
| ``` |
| agent/gemini_agent.py β GeminiAgent class |
| agent/prompts.py β SYSTEM_PROMPT + build_turn_prompt() |
| ``` |
|
|
| ### Design Constraint |
|
|
| The LLM is **only the decision-making layer**. Policy evaluation, reward |
| computation, and grading remain fully deterministic in the environment. |
|
|
| --- |
|
|
| ## π Data Flow |
|
|
| ### 1. Reset |
|
|
| ```text id="5v3sbh" |
| API β Environment.reset() |
| β DataGenerator |
| β StateManager |
| β Initial State returned |
| ``` |
|
|
| --- |
|
|
| ### 2. Step |
|
|
| ```text id="4cx7qf" |
| Agent Action β API |
| β Environment.step() |
| β StateManager update |
| β PolicyEngine evaluate |
| β RewardEngine compute |
| β New State returned |
| ``` |
|
|
| --- |
|
|
| ### 3. Grading |
|
|
| ```text id="yy9gsl" |
| Episode complete β GraderEngine |
| β Score computed |
| β Returned via API |
| ``` |
|
|
| --- |
|
|
| ## π§ Internal Interaction Flow |
|
|
| ```text id="4l9p6p" |
| Action |
| β |
| State Update |
| β |
| Policy Evaluation |
| β |
| Reward Calculation |
| β |
| Next State |
| ``` |
|
|
| --- |
|
|
| ## π§© Component Dependencies |
|
|
| | Component | Depends On | |
| | ------------- | --------------------- | |
| | Environment | State, Policy, Reward | |
| | State Manager | Data Generator | |
| | Reward Engine | Policy Engine | |
| | Grader | Policy + Trajectory | |
| | API | Environment | |
|
|
| --- |
|
|
| ## βοΈ Execution Model |
|
|
| * single-threaded environment (MVP) |
| * stateless API with session tracking (in-memory) |
| * reproducible seeds for all scenarios |
|
|
| --- |
|
|
| ## π³ Deployment Architecture |
|
|
| ```text id="0shdn3" |
| Docker Container |
| βββ FastAPI Server |
| βββ Environment Core |
| βββ Graders |
| βββ Baseline Agent |
| ``` |
|
|
| Runs on: |
|
|
| * Hugging Face Spaces |
| * local Docker |
|
|
| --- |
|
|
| ## π§ Design Principles |
|
|
| ### 1. Modularity |
|
|
| Each component is isolated and testable |
|
|
| --- |
|
|
| ### 2. Determinism |
|
|
| All outputs reproducible |
|
|
| --- |
|
|
| ### 3. Simplicity |
|
|
| Avoid unnecessary abstraction |
|
|
| --- |
|
|
| ### 4. Spec Compliance |
|
|
| Strict adherence to OpenEnv interface |
|
|
| --- |
|
|
| ## π§ One-Line Summary |
|
|
| > A modular, deterministic system where an environment core orchestrates policy evaluation, reward computation, and grading through well-defined components exposed via a FastAPI interface. |
|
|
| --- |
|
|