# ๐Ÿ“„ 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. ---