Spaces:
Sleeping
Sleeping
Afifa Kureshi commited on
Commit ·
c5997d8
0
Parent(s):
Initial clean submission
Browse files- Dockerfile +13 -0
- README.md +125 -0
- actions/file_actions.py +74 -0
- agent/agent.py +187 -0
- agent/rule_agent.py +122 -0
- config.py +43 -0
- data/criteria_easy.json +11 -0
- data/criteria_hard.json +8 -0
- data/criteria_medium.json +11 -0
- data/final_state.json +201 -0
- data/initial_state.json +231 -0
- environment/cloud_env.py +120 -0
- environment/file_system.py +310 -0
- grader/grader.py +276 -0
- inference.py +97 -0
- openenv.yaml +30 -0
- requirements.txt +2 -0
- tasks/easy_task.py +97 -0
- tasks/hard_task.py +157 -0
- tasks/medium_task.py +116 -0
- utils/helpers.py +190 -0
Dockerfile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
|
| 7 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 8 |
+
|
| 9 |
+
COPY . .
|
| 10 |
+
|
| 11 |
+
ENV PYTHONUNBUFFERED=1
|
| 12 |
+
|
| 13 |
+
CMD ["python", "inference.py"]
|
README.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Cloud Data Janitor
|
| 2 |
+
|
| 3 |
+
## Overview
|
| 4 |
+
|
| 5 |
+
Cloud Data Janitor is an OpenEnv-compatible environment that simulates a messy cloud storage system similar to Google Drive.
|
| 6 |
+
An agent interacts with the environment to organize files, delete unnecessary data, and optimize storage.
|
| 7 |
+
|
| 8 |
+
The environment follows the standard OpenEnv API:
|
| 9 |
+
|
| 10 |
+
- reset()
|
| 11 |
+
- state()
|
| 12 |
+
- step()
|
| 13 |
+
|
| 14 |
+
Rewards are given based on how well the agent completes tasks.
|
| 15 |
+
|
| 16 |
+
---
|
| 17 |
+
|
| 18 |
+
## Tasks
|
| 19 |
+
|
| 20 |
+
The environment includes three difficulty levels:
|
| 21 |
+
|
| 22 |
+
### Easy
|
| 23 |
+
Organize important documents by moving them into the correct folder.
|
| 24 |
+
|
| 25 |
+
### Medium
|
| 26 |
+
Delete unnecessary files while preserving important files.
|
| 27 |
+
|
| 28 |
+
### Hard
|
| 29 |
+
Optimize storage usage while protecting critical folders.
|
| 30 |
+
|
| 31 |
+
Each task returns a score between **0.0 and 1.0**.
|
| 32 |
+
|
| 33 |
+
---
|
| 34 |
+
|
| 35 |
+
## Project Structure
|
| 36 |
+
|
| 37 |
+
cloud-data-janitor/
|
| 38 |
+
|
| 39 |
+
agent/
|
| 40 |
+
environment/
|
| 41 |
+
actions/
|
| 42 |
+
grader/
|
| 43 |
+
utils/
|
| 44 |
+
data/
|
| 45 |
+
|
| 46 |
+
inference.py
|
| 47 |
+
openenv.yaml
|
| 48 |
+
Dockerfile
|
| 49 |
+
requirements.txt
|
| 50 |
+
README.md
|
| 51 |
+
|
| 52 |
+
---
|
| 53 |
+
|
| 54 |
+
## How to Run
|
| 55 |
+
|
| 56 |
+
### Run Locally
|
| 57 |
+
|
| 58 |
+
python inference.py
|
| 59 |
+
|
| 60 |
+
---
|
| 61 |
+
|
| 62 |
+
### Run Specific Task (Windows)
|
| 63 |
+
|
| 64 |
+
set MY_ENV_V4_TASK=easy
|
| 65 |
+
python inference.py
|
| 66 |
+
|
| 67 |
+
To run other tasks:
|
| 68 |
+
|
| 69 |
+
set MY_ENV_V4_TASK=medium
|
| 70 |
+
python inference.py
|
| 71 |
+
|
| 72 |
+
set MY_ENV_V4_TASK=hard
|
| 73 |
+
python inference.py
|
| 74 |
+
|
| 75 |
+
---
|
| 76 |
+
|
| 77 |
+
## Run with Docker
|
| 78 |
+
|
| 79 |
+
Build the Docker image:
|
| 80 |
+
|
| 81 |
+
docker build -t cloud-data-janitor .
|
| 82 |
+
|
| 83 |
+
Run the environment:
|
| 84 |
+
|
| 85 |
+
docker run -e MY_ENV_V4_TASK=easy cloud-data-janitor
|
| 86 |
+
|
| 87 |
+
You can replace easy with:
|
| 88 |
+
|
| 89 |
+
medium
|
| 90 |
+
hard
|
| 91 |
+
|
| 92 |
+
---
|
| 93 |
+
|
| 94 |
+
## Output Format
|
| 95 |
+
|
| 96 |
+
The program prints:
|
| 97 |
+
|
| 98 |
+
[START]
|
| 99 |
+
[STEP]
|
| 100 |
+
[END]
|
| 101 |
+
|
| 102 |
+
Example:
|
| 103 |
+
|
| 104 |
+
[START] task=easy env=cloud-data-janitor model=rule-agent
|
| 105 |
+
[STEP] step=1 reward=0.50 done=false error=null
|
| 106 |
+
[END] success=true steps=2 score=1.00 rewards=0.50,1.00
|
| 107 |
+
|
| 108 |
+
---
|
| 109 |
+
|
| 110 |
+
## Requirements
|
| 111 |
+
|
| 112 |
+
- Python 3.10 or later
|
| 113 |
+
- Docker
|
| 114 |
+
|
| 115 |
+
---
|
| 116 |
+
|
| 117 |
+
## Status
|
| 118 |
+
|
| 119 |
+
This project is:
|
| 120 |
+
|
| 121 |
+
- OpenEnv compatible
|
| 122 |
+
- Deterministic
|
| 123 |
+
- Docker ready
|
| 124 |
+
- Fully functional
|
| 125 |
+
- Submission ready
|
actions/file_actions.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# actions/file_actions.py
|
| 2 |
+
|
| 3 |
+
from environment.file_system import FileSystem
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def create_folder(fs: FileSystem, name: str) -> dict:
|
| 7 |
+
"""Action: Create a new folder."""
|
| 8 |
+
return fs.create_folder(name)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def move_file(fs: FileSystem, filename: str, destination: str) -> dict:
|
| 12 |
+
"""Action: Move a file to a destination folder."""
|
| 13 |
+
return fs.move_file(filename, destination)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def rename_file(fs: FileSystem, old_name: str, new_name: str) -> dict:
|
| 17 |
+
"""Action: Rename a file."""
|
| 18 |
+
return fs.rename_file(old_name, new_name)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def delete_file(fs: FileSystem, filename: str) -> dict:
|
| 22 |
+
"""Action: Delete a file."""
|
| 23 |
+
return fs.delete_file(filename)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def zip_folder(fs: FileSystem, folder_name: str) -> dict:
|
| 27 |
+
"""Action: Zip a folder to compress it."""
|
| 28 |
+
return fs.zip_folder(folder_name)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def inspect_file(fs: FileSystem, filename: str) -> dict:
|
| 32 |
+
"""Action: Inspect a file's metadata."""
|
| 33 |
+
info = fs.get_file_info(filename)
|
| 34 |
+
if info:
|
| 35 |
+
return {"success": True, "file_info": info}
|
| 36 |
+
return {"success": False, "message": f"File '{filename}' not found."}
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def list_all_files(fs: FileSystem) -> dict:
|
| 40 |
+
"""Action: List all files in the filesystem."""
|
| 41 |
+
files = fs.list_files()
|
| 42 |
+
return {"success": True, "files": files, "total": len(files)}
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# Registry mapping action name → callable
|
| 46 |
+
ACTION_REGISTRY = {
|
| 47 |
+
"create_folder": create_folder,
|
| 48 |
+
"move_file": move_file,
|
| 49 |
+
"rename_file": rename_file,
|
| 50 |
+
"delete_file": delete_file,
|
| 51 |
+
"zip_folder": zip_folder,
|
| 52 |
+
"inspect_file": inspect_file,
|
| 53 |
+
"list_all_files": list_all_files,
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def execute_action(fs, action_type, params):
|
| 58 |
+
"""
|
| 59 |
+
Dispatch action using registry.
|
| 60 |
+
"""
|
| 61 |
+
|
| 62 |
+
if action_type not in ACTION_REGISTRY:
|
| 63 |
+
|
| 64 |
+
raise ValueError(
|
| 65 |
+
f"Unknown action: {action_type}"
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
handler = ACTION_REGISTRY[action_type]
|
| 69 |
+
|
| 70 |
+
# Call handler with parameters
|
| 71 |
+
return handler(
|
| 72 |
+
fs,
|
| 73 |
+
**params
|
| 74 |
+
)
|
agent/agent.py
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# agent/agent.py
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
import re
|
| 5 |
+
import os
|
| 6 |
+
from openai import OpenAI
|
| 7 |
+
from environment.file_system import FileSystem
|
| 8 |
+
from actions.file_actions import execute_action
|
| 9 |
+
from utils.helpers import build_prompt_context
|
| 10 |
+
from config import MAX_AGENT_STEPS
|
| 11 |
+
|
| 12 |
+
# Safe OpenAI client setup (NO HARDCODED TOKEN)
|
| 13 |
+
API_KEY = os.getenv("HF_TOKEN")
|
| 14 |
+
API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
|
| 15 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
|
| 16 |
+
|
| 17 |
+
client = OpenAI(
|
| 18 |
+
base_url=API_BASE_URL,
|
| 19 |
+
api_key=API_KEY
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
SYSTEM_PROMPT = """You are an AI Data Janitor agent responsible for cleaning and organizing a cloud file system.
|
| 23 |
+
|
| 24 |
+
You have access to the following actions (respond ONLY with valid JSON):
|
| 25 |
+
- create_folder(name)
|
| 26 |
+
- move_file(filename, destination)
|
| 27 |
+
- rename_file(old_name, new_name)
|
| 28 |
+
- delete_file(filename)
|
| 29 |
+
- zip_folder(folder_name)
|
| 30 |
+
- inspect_file(filename)
|
| 31 |
+
- list_all_files()
|
| 32 |
+
- done()
|
| 33 |
+
|
| 34 |
+
Each response must be valid JSON exactly like this:
|
| 35 |
+
{
|
| 36 |
+
"thought": "your reasoning here",
|
| 37 |
+
"action": "action_name",
|
| 38 |
+
"params": { ...parameters... }
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
Rules:
|
| 42 |
+
- Think carefully before deleting anything
|
| 43 |
+
- Never delete recent files unless clearly unnecessary
|
| 44 |
+
- Prefer moving files to proper folders first
|
| 45 |
+
- If unsure, use inspect_file first
|
| 46 |
+
"""
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
class DataJanitorAgent:
|
| 50 |
+
|
| 51 |
+
def __init__(self, task_description: str, fs: FileSystem):
|
| 52 |
+
self.task_description = task_description
|
| 53 |
+
self.fs = fs
|
| 54 |
+
self.messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 55 |
+
self.step = 0
|
| 56 |
+
self.done = False
|
| 57 |
+
|
| 58 |
+
def _build_first_message(self):
|
| 59 |
+
|
| 60 |
+
obs = self.fs.get_observation()
|
| 61 |
+
context = build_prompt_context(obs)
|
| 62 |
+
|
| 63 |
+
return (
|
| 64 |
+
f"TASK: {self.task_description}\n\n"
|
| 65 |
+
f"CURRENT FILE SYSTEM STATE:\n{context}\n\n"
|
| 66 |
+
f"What is your next action?"
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
def _build_next_message(self):
|
| 70 |
+
|
| 71 |
+
obs = self.fs.get_observation()
|
| 72 |
+
context = build_prompt_context(obs)
|
| 73 |
+
|
| 74 |
+
return (
|
| 75 |
+
f"CURRENT FILE SYSTEM STATE:\n{context}\n\n"
|
| 76 |
+
f"What is your next action? Reply in JSON only."
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
def _parse_response(self, text):
|
| 80 |
+
|
| 81 |
+
text = re.sub(r"```(?:json)?", "", text).strip().rstrip("`").strip()
|
| 82 |
+
|
| 83 |
+
try:
|
| 84 |
+
return json.loads(text)
|
| 85 |
+
|
| 86 |
+
except:
|
| 87 |
+
|
| 88 |
+
match = re.search(r"\{.*\}", text, re.DOTALL)
|
| 89 |
+
|
| 90 |
+
if match:
|
| 91 |
+
|
| 92 |
+
try:
|
| 93 |
+
return json.loads(match.group(0))
|
| 94 |
+
except:
|
| 95 |
+
pass
|
| 96 |
+
|
| 97 |
+
return {
|
| 98 |
+
"thought": "Failed to parse",
|
| 99 |
+
"action": "done",
|
| 100 |
+
"params": {}
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
def step_once(self):
|
| 104 |
+
|
| 105 |
+
if self.done:
|
| 106 |
+
|
| 107 |
+
return {
|
| 108 |
+
"step": self.step,
|
| 109 |
+
"done": True
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
if self.step == 0:
|
| 113 |
+
|
| 114 |
+
user_content = self._build_first_message()
|
| 115 |
+
|
| 116 |
+
else:
|
| 117 |
+
|
| 118 |
+
user_content = self._build_next_message()
|
| 119 |
+
|
| 120 |
+
self.messages.append({
|
| 121 |
+
"role": "user",
|
| 122 |
+
"content": user_content
|
| 123 |
+
})
|
| 124 |
+
|
| 125 |
+
response = client.chat.completions.create(
|
| 126 |
+
model=MODEL_NAME,
|
| 127 |
+
messages=self.messages,
|
| 128 |
+
temperature=0.0
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
assistant_text = response.choices[0].message.content
|
| 132 |
+
|
| 133 |
+
self.messages.append({
|
| 134 |
+
"role": "assistant",
|
| 135 |
+
"content": assistant_text
|
| 136 |
+
})
|
| 137 |
+
|
| 138 |
+
parsed = self._parse_response(assistant_text)
|
| 139 |
+
|
| 140 |
+
action_name = parsed.get("action", "")
|
| 141 |
+
params = parsed.get("params", {})
|
| 142 |
+
thought = parsed.get("thought", "")
|
| 143 |
+
|
| 144 |
+
self.step += 1
|
| 145 |
+
|
| 146 |
+
if action_name == "done":
|
| 147 |
+
|
| 148 |
+
self.done = True
|
| 149 |
+
|
| 150 |
+
return {
|
| 151 |
+
"step": self.step,
|
| 152 |
+
"thought": thought,
|
| 153 |
+
"action": "done",
|
| 154 |
+
"result": {
|
| 155 |
+
"success": True,
|
| 156 |
+
"message": "Task completed"
|
| 157 |
+
},
|
| 158 |
+
"done": True
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
result = execute_action(
|
| 162 |
+
self.fs,
|
| 163 |
+
action_name,
|
| 164 |
+
params
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
return {
|
| 168 |
+
"step": self.step,
|
| 169 |
+
"thought": thought,
|
| 170 |
+
"action": action_name,
|
| 171 |
+
"params": params,
|
| 172 |
+
"result": result,
|
| 173 |
+
"done": False
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
def run(self, verbose=False):
|
| 177 |
+
|
| 178 |
+
while not self.done and self.step < MAX_AGENT_STEPS:
|
| 179 |
+
|
| 180 |
+
result = self.step_once()
|
| 181 |
+
|
| 182 |
+
if result.get("done"):
|
| 183 |
+
break
|
| 184 |
+
|
| 185 |
+
self.fs.save_state()
|
| 186 |
+
|
| 187 |
+
return []
|
agent/rule_agent.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime, timedelta
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class RuleBasedAgent:
|
| 5 |
+
|
| 6 |
+
def choose_action(self, state):
|
| 7 |
+
|
| 8 |
+
files = state.get("files", [])
|
| 9 |
+
task = state.get("task")
|
| 10 |
+
|
| 11 |
+
# -------------------------------
|
| 12 |
+
# EASY TASK
|
| 13 |
+
# -------------------------------
|
| 14 |
+
if task == "easy":
|
| 15 |
+
|
| 16 |
+
for f in files:
|
| 17 |
+
|
| 18 |
+
name = f.get("name")
|
| 19 |
+
path = f.get("path", "")
|
| 20 |
+
|
| 21 |
+
# Skip already organized
|
| 22 |
+
if "/" in path:
|
| 23 |
+
continue
|
| 24 |
+
|
| 25 |
+
if name.endswith(".pdf"):
|
| 26 |
+
return {
|
| 27 |
+
"action": "move_file",
|
| 28 |
+
"filename": name,
|
| 29 |
+
"destination": "Documents"
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
if name.endswith(".jpg"):
|
| 33 |
+
return {
|
| 34 |
+
"action": "move_file",
|
| 35 |
+
"filename": name,
|
| 36 |
+
"destination": "Images"
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
return {
|
| 40 |
+
"action": "move_file",
|
| 41 |
+
"filename": name,
|
| 42 |
+
"destination": "Misc"
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
# -------------------------------
|
| 46 |
+
# MEDIUM TASK
|
| 47 |
+
# -------------------------------
|
| 48 |
+
elif task == "medium":
|
| 49 |
+
|
| 50 |
+
for f in files:
|
| 51 |
+
|
| 52 |
+
name = f.get("name")
|
| 53 |
+
name_lower = name.lower()
|
| 54 |
+
|
| 55 |
+
# Detect duplicate patterns
|
| 56 |
+
if (
|
| 57 |
+
"(" in name_lower and ")" in name_lower
|
| 58 |
+
or "_copy" in name_lower
|
| 59 |
+
or " copy" in name_lower
|
| 60 |
+
):
|
| 61 |
+
|
| 62 |
+
return {
|
| 63 |
+
"action": "delete_file",
|
| 64 |
+
"filename": name
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
return {
|
| 68 |
+
"action": "done"
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
# -------------------------------
|
| 72 |
+
# HARD TASK
|
| 73 |
+
# -------------------------------
|
| 74 |
+
elif task == "hard":
|
| 75 |
+
|
| 76 |
+
now = datetime.now()
|
| 77 |
+
threshold = now - timedelta(days=30)
|
| 78 |
+
|
| 79 |
+
# Sort largest files first
|
| 80 |
+
files_sorted = sorted(
|
| 81 |
+
files,
|
| 82 |
+
key=lambda x: x.get("size_mb", 0),
|
| 83 |
+
reverse=True
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
for f in files_sorted:
|
| 87 |
+
|
| 88 |
+
name = f.get("name")
|
| 89 |
+
size = f.get("size_mb", 0)
|
| 90 |
+
last_accessed = f.get("last_accessed")
|
| 91 |
+
|
| 92 |
+
if not last_accessed:
|
| 93 |
+
continue
|
| 94 |
+
|
| 95 |
+
try:
|
| 96 |
+
last_accessed_dt = datetime.fromisoformat(
|
| 97 |
+
last_accessed
|
| 98 |
+
)
|
| 99 |
+
except Exception:
|
| 100 |
+
continue
|
| 101 |
+
|
| 102 |
+
# Safe deletion rule
|
| 103 |
+
if (
|
| 104 |
+
last_accessed_dt < threshold
|
| 105 |
+
and size > 10
|
| 106 |
+
):
|
| 107 |
+
|
| 108 |
+
return {
|
| 109 |
+
"action": "delete_file",
|
| 110 |
+
"filename": name
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
return {
|
| 114 |
+
"action": "done"
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
# -------------------------------
|
| 118 |
+
# DEFAULT
|
| 119 |
+
# -------------------------------
|
| 120 |
+
return {
|
| 121 |
+
"action": "done"
|
| 122 |
+
}
|
config.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# config.py
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
GROK_API_KEY = os.getenv("GROK_API_KEY")
|
| 7 |
+
MODEL_NAME = "grok-3-fast-beta"
|
| 8 |
+
MAX_TOKENS = 4096
|
| 9 |
+
|
| 10 |
+
# File system paths
|
| 11 |
+
DATA_DIR = "data"
|
| 12 |
+
INITIAL_STATE_FILE = "data/initial_state.json"
|
| 13 |
+
FINAL_STATE_FILE = "data/final_state.json"
|
| 14 |
+
|
| 15 |
+
# Task difficulty
|
| 16 |
+
TASK_EASY = "easy"
|
| 17 |
+
TASK_MEDIUM = "medium"
|
| 18 |
+
TASK_HARD = "hard"
|
| 19 |
+
|
| 20 |
+
# File categories
|
| 21 |
+
FILE_CATEGORIES = {
|
| 22 |
+
"Documents": [".pdf", ".doc", ".docx", ".txt", ".ppt", ".pptx", ".xls", ".xlsx"],
|
| 23 |
+
"Images": [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".svg", ".webp"],
|
| 24 |
+
"Videos": [".mp4", ".avi", ".mov", ".mkv", ".wmv"],
|
| 25 |
+
"Audio": [".mp3", ".wav", ".flac", ".aac", ".ogg"],
|
| 26 |
+
"Archives": [".zip", ".tar", ".gz", ".rar", ".7z"],
|
| 27 |
+
"Code": [".py", ".js", ".ts", ".html", ".css", ".java", ".cpp", ".c"],
|
| 28 |
+
"Data": [".json", ".csv", ".xml", ".sql"],
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
# Hard task constraints
|
| 32 |
+
STORAGE_LIMIT_MB = 500 # Free up 500MB
|
| 33 |
+
OLD_FILE_THRESHOLD_YEARS = 3 # Files older than 3 years are eligible for deletion
|
| 34 |
+
LARGE_FOLDER_THRESHOLD_MB = 100 # Folders larger than 100MB are eligible for zipping
|
| 35 |
+
RECENT_DAYS_THRESHOLD = 90 # Files accessed within 90 days are "recent"
|
| 36 |
+
|
| 37 |
+
# Grader settings
|
| 38 |
+
CORRECT_MOVE_SCORE = 1.0
|
| 39 |
+
INCORRECT_PENALTY = -1.0
|
| 40 |
+
|
| 41 |
+
# Agent settings
|
| 42 |
+
MAX_AGENT_STEPS = 100
|
| 43 |
+
AGENT_TEMPERATURE = 0.2
|
data/criteria_easy.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"files_to_move_to_Documents": [
|
| 3 |
+
"annual_report_2018.pdf",
|
| 4 |
+
"tax_return_2019.pdf",
|
| 5 |
+
"report_2022.pdf"
|
| 6 |
+
],
|
| 7 |
+
|
| 8 |
+
"files_to_move_to_Images": [],
|
| 9 |
+
|
| 10 |
+
"files_to_leave_untouched": []
|
| 11 |
+
}
|
data/criteria_hard.json
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"storage_goal_mb": 300,
|
| 3 |
+
|
| 4 |
+
"folders_to_protect": [
|
| 5 |
+
"Active_Projects",
|
| 6 |
+
"Current_Code"
|
| 7 |
+
]
|
| 8 |
+
}
|
data/criteria_medium.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"files_to_delete": [
|
| 3 |
+
"archive_misc.zip",
|
| 4 |
+
"old_backup_2018.zip"
|
| 5 |
+
],
|
| 6 |
+
|
| 7 |
+
"files_to_preserve": [
|
| 8 |
+
"annual_report_2018.pdf",
|
| 9 |
+
"tax_return_2019.pdf"
|
| 10 |
+
]
|
| 11 |
+
}
|
data/final_state.json
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"root": {
|
| 3 |
+
"annual_report_2018.pdf": {
|
| 4 |
+
"type": "file",
|
| 5 |
+
"extension": ".pdf",
|
| 6 |
+
"size_mb": 8.5,
|
| 7 |
+
"created_date": "2021-05-07T17:20:30.349075",
|
| 8 |
+
"last_accessed": "2021-06-26T17:20:30.349075"
|
| 9 |
+
},
|
| 10 |
+
"tax_return_2019.pdf": {
|
| 11 |
+
"type": "file",
|
| 12 |
+
"extension": ".pdf",
|
| 13 |
+
"size_mb": 3.2,
|
| 14 |
+
"created_date": "2021-11-23T17:20:30.349075",
|
| 15 |
+
"last_accessed": "2022-01-12T17:20:30.349075"
|
| 16 |
+
},
|
| 17 |
+
"meeting_notes_2018.txt": {
|
| 18 |
+
"type": "file",
|
| 19 |
+
"extension": ".txt",
|
| 20 |
+
"size_mb": 0.2,
|
| 21 |
+
"created_date": "2021-03-18T17:20:30.349075",
|
| 22 |
+
"last_accessed": "2021-05-07T17:20:30.349075"
|
| 23 |
+
},
|
| 24 |
+
"report_2022.pdf": {
|
| 25 |
+
"type": "file",
|
| 26 |
+
"extension": ".pdf",
|
| 27 |
+
"size_mb": 4.5,
|
| 28 |
+
"created_date": "2024-05-11T17:20:30.349075",
|
| 29 |
+
"last_accessed": "2025-03-07T17:20:30.349075"
|
| 30 |
+
},
|
| 31 |
+
"notes_2020.txt": {
|
| 32 |
+
"type": "file",
|
| 33 |
+
"extension": ".txt",
|
| 34 |
+
"size_mb": 0.1,
|
| 35 |
+
"created_date": "2022-12-28T17:20:30.349075",
|
| 36 |
+
"last_accessed": "2023-04-07T17:20:30.349075"
|
| 37 |
+
},
|
| 38 |
+
"Design_Assets": {
|
| 39 |
+
"type": "folder",
|
| 40 |
+
"created_date": "2023-09-04T17:20:30.349075",
|
| 41 |
+
"contents": {}
|
| 42 |
+
},
|
| 43 |
+
"Tutorial_Videos": {
|
| 44 |
+
"type": "folder",
|
| 45 |
+
"created_date": "2024-04-21T17:20:30.349075",
|
| 46 |
+
"contents": {}
|
| 47 |
+
},
|
| 48 |
+
"Active_Projects": {
|
| 49 |
+
"type": "folder",
|
| 50 |
+
"created_date": "2026-03-17T17:20:30.349075",
|
| 51 |
+
"contents": {
|
| 52 |
+
"q4_strategy_2024.pptx": {
|
| 53 |
+
"type": "file",
|
| 54 |
+
"extension": ".pptx",
|
| 55 |
+
"size_mb": 5.2,
|
| 56 |
+
"created_date": "2026-03-22T17:20:30.349075",
|
| 57 |
+
"last_accessed": "2026-04-09T17:20:30.349075"
|
| 58 |
+
},
|
| 59 |
+
"client_proposal_nov.docx": {
|
| 60 |
+
"type": "file",
|
| 61 |
+
"extension": ".docx",
|
| 62 |
+
"size_mb": 1.8,
|
| 63 |
+
"created_date": "2026-03-27T17:20:30.349075",
|
| 64 |
+
"last_accessed": "2026-04-10T17:20:30.349075"
|
| 65 |
+
},
|
| 66 |
+
"budget_forecast_2025.xlsx": {
|
| 67 |
+
"type": "file",
|
| 68 |
+
"extension": ".xlsx",
|
| 69 |
+
"size_mb": 3.1,
|
| 70 |
+
"created_date": "2026-04-01T17:20:30.349075",
|
| 71 |
+
"last_accessed": "2026-04-08T17:20:30.349075"
|
| 72 |
+
},
|
| 73 |
+
"product_roadmap.pdf": {
|
| 74 |
+
"type": "file",
|
| 75 |
+
"extension": ".pdf",
|
| 76 |
+
"size_mb": 2.4,
|
| 77 |
+
"created_date": "2026-04-06T17:20:30.349075",
|
| 78 |
+
"last_accessed": "2026-04-11T17:20:30.349075"
|
| 79 |
+
}
|
| 80 |
+
}
|
| 81 |
+
},
|
| 82 |
+
"Current_Code": {
|
| 83 |
+
"type": "folder",
|
| 84 |
+
"created_date": "2026-04-04T17:20:30.349075",
|
| 85 |
+
"contents": {
|
| 86 |
+
"main.py": {
|
| 87 |
+
"type": "file",
|
| 88 |
+
"extension": ".py",
|
| 89 |
+
"size_mb": 0.05,
|
| 90 |
+
"created_date": "2026-04-04T17:20:30.349075",
|
| 91 |
+
"last_accessed": "2026-04-11T17:20:30.349075"
|
| 92 |
+
},
|
| 93 |
+
"utils.py": {
|
| 94 |
+
"type": "file",
|
| 95 |
+
"extension": ".py",
|
| 96 |
+
"size_mb": 0.03,
|
| 97 |
+
"created_date": "2026-04-04T17:20:30.349075",
|
| 98 |
+
"last_accessed": "2026-04-10T17:20:30.349075"
|
| 99 |
+
},
|
| 100 |
+
"requirements.txt": {
|
| 101 |
+
"type": "file",
|
| 102 |
+
"extension": ".txt",
|
| 103 |
+
"size_mb": 0.01,
|
| 104 |
+
"created_date": "2026-04-04T17:20:30.349075",
|
| 105 |
+
"last_accessed": "2026-04-11T17:20:30.349075"
|
| 106 |
+
},
|
| 107 |
+
"README.md": {
|
| 108 |
+
"type": "file",
|
| 109 |
+
"extension": ".md",
|
| 110 |
+
"size_mb": 0.02,
|
| 111 |
+
"created_date": "2026-04-04T17:20:30.349075",
|
| 112 |
+
"last_accessed": "2026-04-11T17:20:30.349075"
|
| 113 |
+
},
|
| 114 |
+
"tests.py": {
|
| 115 |
+
"type": "file",
|
| 116 |
+
"extension": ".py",
|
| 117 |
+
"size_mb": 0.04,
|
| 118 |
+
"created_date": "2026-04-04T17:20:30.349075",
|
| 119 |
+
"last_accessed": "2026-04-11T17:20:30.349075"
|
| 120 |
+
}
|
| 121 |
+
}
|
| 122 |
+
}
|
| 123 |
+
},
|
| 124 |
+
"action_log": [
|
| 125 |
+
{
|
| 126 |
+
"action": "delete_file",
|
| 127 |
+
"file": "raw_footage.avi",
|
| 128 |
+
"size_mb": 1200.0
|
| 129 |
+
},
|
| 130 |
+
{
|
| 131 |
+
"action": "delete_file",
|
| 132 |
+
"file": "tutorial_part1.mp4",
|
| 133 |
+
"size_mb": 800.0
|
| 134 |
+
},
|
| 135 |
+
{
|
| 136 |
+
"action": "delete_file",
|
| 137 |
+
"file": "tutorial_part2.mp4",
|
| 138 |
+
"size_mb": 750.0
|
| 139 |
+
},
|
| 140 |
+
{
|
| 141 |
+
"action": "delete_file",
|
| 142 |
+
"file": "database_snapshot_2018.sql",
|
| 143 |
+
"size_mb": 320.0
|
| 144 |
+
},
|
| 145 |
+
{
|
| 146 |
+
"action": "delete_file",
|
| 147 |
+
"file": "old_backup_2018.zip",
|
| 148 |
+
"size_mb": 210.0
|
| 149 |
+
},
|
| 150 |
+
{
|
| 151 |
+
"action": "delete_file",
|
| 152 |
+
"file": "photo_album_2022.zip",
|
| 153 |
+
"size_mb": 130.0
|
| 154 |
+
},
|
| 155 |
+
{
|
| 156 |
+
"action": "delete_file",
|
| 157 |
+
"file": "banner_draft.psd",
|
| 158 |
+
"size_mb": 110.0
|
| 159 |
+
},
|
| 160 |
+
{
|
| 161 |
+
"action": "delete_file",
|
| 162 |
+
"file": "logo_v1.psd",
|
| 163 |
+
"size_mb": 95.0
|
| 164 |
+
},
|
| 165 |
+
{
|
| 166 |
+
"action": "delete_file",
|
| 167 |
+
"file": "legacy_code_archive.tar",
|
| 168 |
+
"size_mb": 85.0
|
| 169 |
+
},
|
| 170 |
+
{
|
| 171 |
+
"action": "delete_file",
|
| 172 |
+
"file": "mockup_website.fig",
|
| 173 |
+
"size_mb": 75.0
|
| 174 |
+
},
|
| 175 |
+
{
|
| 176 |
+
"action": "delete_file",
|
| 177 |
+
"file": "icons_set.ai",
|
| 178 |
+
"size_mb": 60.0
|
| 179 |
+
},
|
| 180 |
+
{
|
| 181 |
+
"action": "delete_file",
|
| 182 |
+
"file": "archive_misc.zip",
|
| 183 |
+
"size_mb": 55.0
|
| 184 |
+
},
|
| 185 |
+
{
|
| 186 |
+
"action": "delete_file",
|
| 187 |
+
"file": "old_presentation_2017.pptx",
|
| 188 |
+
"size_mb": 45.0
|
| 189 |
+
},
|
| 190 |
+
{
|
| 191 |
+
"action": "delete_file",
|
| 192 |
+
"file": "presentation_2021.pptx",
|
| 193 |
+
"size_mb": 22.0
|
| 194 |
+
},
|
| 195 |
+
{
|
| 196 |
+
"action": "delete_file",
|
| 197 |
+
"file": "project_alpha_2019.docx",
|
| 198 |
+
"size_mb": 12.0
|
| 199 |
+
}
|
| 200 |
+
]
|
| 201 |
+
}
|
data/initial_state.json
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"root": {
|
| 3 |
+
"old_backup_2018.zip": {
|
| 4 |
+
"type": "file",
|
| 5 |
+
"extension": ".zip",
|
| 6 |
+
"size_mb": 210.0,
|
| 7 |
+
"created_date": "2021-01-27T17:20:30.349075",
|
| 8 |
+
"last_accessed": "2021-05-07T17:20:30.347872"
|
| 9 |
+
},
|
| 10 |
+
"legacy_code_archive.tar": {
|
| 11 |
+
"type": "file",
|
| 12 |
+
"extension": ".tar",
|
| 13 |
+
"size_mb": 85.0,
|
| 14 |
+
"created_date": "2022-03-03T17:20:30.349075",
|
| 15 |
+
"last_accessed": "2022-06-11T17:20:30.349075"
|
| 16 |
+
},
|
| 17 |
+
"project_alpha_2019.docx": {
|
| 18 |
+
"type": "file",
|
| 19 |
+
"extension": ".docx",
|
| 20 |
+
"size_mb": 12.0,
|
| 21 |
+
"created_date": "2021-08-15T17:20:30.349075",
|
| 22 |
+
"last_accessed": "2021-11-23T17:20:30.349075"
|
| 23 |
+
},
|
| 24 |
+
"annual_report_2018.pdf": {
|
| 25 |
+
"type": "file",
|
| 26 |
+
"extension": ".pdf",
|
| 27 |
+
"size_mb": 8.5,
|
| 28 |
+
"created_date": "2021-05-07T17:20:30.349075",
|
| 29 |
+
"last_accessed": "2021-06-26T17:20:30.349075"
|
| 30 |
+
},
|
| 31 |
+
"tax_return_2019.pdf": {
|
| 32 |
+
"type": "file",
|
| 33 |
+
"extension": ".pdf",
|
| 34 |
+
"size_mb": 3.2,
|
| 35 |
+
"created_date": "2021-11-23T17:20:30.349075",
|
| 36 |
+
"last_accessed": "2022-01-12T17:20:30.349075"
|
| 37 |
+
},
|
| 38 |
+
"database_snapshot_2018.sql": {
|
| 39 |
+
"type": "file",
|
| 40 |
+
"extension": ".sql",
|
| 41 |
+
"size_mb": 320.0,
|
| 42 |
+
"created_date": "2020-10-19T17:20:30.349075",
|
| 43 |
+
"last_accessed": "2021-01-27T17:20:30.349075"
|
| 44 |
+
},
|
| 45 |
+
"old_presentation_2017.pptx": {
|
| 46 |
+
"type": "file",
|
| 47 |
+
"extension": ".pptx",
|
| 48 |
+
"size_mb": 45.0,
|
| 49 |
+
"created_date": "2020-04-02T17:20:30.349075",
|
| 50 |
+
"last_accessed": "2020-07-11T17:20:30.349075"
|
| 51 |
+
},
|
| 52 |
+
"meeting_notes_2018.txt": {
|
| 53 |
+
"type": "file",
|
| 54 |
+
"extension": ".txt",
|
| 55 |
+
"size_mb": 0.2,
|
| 56 |
+
"created_date": "2021-03-18T17:20:30.349075",
|
| 57 |
+
"last_accessed": "2021-05-07T17:20:30.349075"
|
| 58 |
+
},
|
| 59 |
+
"report_2022.pdf": {
|
| 60 |
+
"type": "file",
|
| 61 |
+
"extension": ".pdf",
|
| 62 |
+
"size_mb": 4.5,
|
| 63 |
+
"created_date": "2024-05-11T17:20:30.349075",
|
| 64 |
+
"last_accessed": "2025-03-07T17:20:30.349075"
|
| 65 |
+
},
|
| 66 |
+
"presentation_2021.pptx": {
|
| 67 |
+
"type": "file",
|
| 68 |
+
"extension": ".pptx",
|
| 69 |
+
"size_mb": 22.0,
|
| 70 |
+
"created_date": "2023-07-16T17:20:30.349075",
|
| 71 |
+
"last_accessed": "2024-02-01T17:20:30.349075"
|
| 72 |
+
},
|
| 73 |
+
"archive_misc.zip": {
|
| 74 |
+
"type": "file",
|
| 75 |
+
"extension": ".zip",
|
| 76 |
+
"size_mb": 55.0,
|
| 77 |
+
"created_date": "2024-02-01T17:20:30.349075",
|
| 78 |
+
"last_accessed": "2024-08-19T17:20:30.349075"
|
| 79 |
+
},
|
| 80 |
+
"notes_2020.txt": {
|
| 81 |
+
"type": "file",
|
| 82 |
+
"extension": ".txt",
|
| 83 |
+
"size_mb": 0.1,
|
| 84 |
+
"created_date": "2022-12-28T17:20:30.349075",
|
| 85 |
+
"last_accessed": "2023-04-07T17:20:30.349075"
|
| 86 |
+
},
|
| 87 |
+
"photo_album_2022.zip": {
|
| 88 |
+
"type": "file",
|
| 89 |
+
"extension": ".zip",
|
| 90 |
+
"size_mb": 130.0,
|
| 91 |
+
"created_date": "2024-06-30T17:20:30.349075",
|
| 92 |
+
"last_accessed": "2024-11-27T17:20:30.349075"
|
| 93 |
+
},
|
| 94 |
+
"Design_Assets": {
|
| 95 |
+
"type": "folder",
|
| 96 |
+
"created_date": "2023-09-04T17:20:30.349075",
|
| 97 |
+
"contents": {
|
| 98 |
+
"logo_v1.psd": {
|
| 99 |
+
"type": "file",
|
| 100 |
+
"extension": ".psd",
|
| 101 |
+
"size_mb": 95.0,
|
| 102 |
+
"created_date": "2023-10-24T17:20:30.349075",
|
| 103 |
+
"last_accessed": "2024-03-22T17:20:30.349075"
|
| 104 |
+
},
|
| 105 |
+
"banner_draft.psd": {
|
| 106 |
+
"type": "file",
|
| 107 |
+
"extension": ".psd",
|
| 108 |
+
"size_mb": 110.0,
|
| 109 |
+
"created_date": "2023-12-13T17:20:30.349075",
|
| 110 |
+
"last_accessed": "2024-02-01T17:20:30.349075"
|
| 111 |
+
},
|
| 112 |
+
"icons_set.ai": {
|
| 113 |
+
"type": "file",
|
| 114 |
+
"extension": ".ai",
|
| 115 |
+
"size_mb": 60.0,
|
| 116 |
+
"created_date": "2023-09-04T17:20:30.349075",
|
| 117 |
+
"last_accessed": "2023-10-24T17:20:30.349075"
|
| 118 |
+
},
|
| 119 |
+
"mockup_website.fig": {
|
| 120 |
+
"type": "file",
|
| 121 |
+
"extension": ".fig",
|
| 122 |
+
"size_mb": 75.0,
|
| 123 |
+
"created_date": "2023-11-23T17:20:30.349075",
|
| 124 |
+
"last_accessed": "2024-01-12T17:20:30.349075"
|
| 125 |
+
}
|
| 126 |
+
}
|
| 127 |
+
},
|
| 128 |
+
"Tutorial_Videos": {
|
| 129 |
+
"type": "folder",
|
| 130 |
+
"created_date": "2024-04-21T17:20:30.349075",
|
| 131 |
+
"contents": {
|
| 132 |
+
"tutorial_part1.mp4": {
|
| 133 |
+
"type": "file",
|
| 134 |
+
"extension": ".mp4",
|
| 135 |
+
"size_mb": 800.0,
|
| 136 |
+
"created_date": "2024-05-11T17:20:30.349075",
|
| 137 |
+
"last_accessed": "2024-06-30T17:20:30.349075"
|
| 138 |
+
},
|
| 139 |
+
"tutorial_part2.mp4": {
|
| 140 |
+
"type": "file",
|
| 141 |
+
"extension": ".mp4",
|
| 142 |
+
"size_mb": 750.0,
|
| 143 |
+
"created_date": "2024-05-11T17:20:30.349075",
|
| 144 |
+
"last_accessed": "2024-06-30T17:20:30.349075"
|
| 145 |
+
},
|
| 146 |
+
"raw_footage.avi": {
|
| 147 |
+
"type": "file",
|
| 148 |
+
"extension": ".avi",
|
| 149 |
+
"size_mb": 1200.0,
|
| 150 |
+
"created_date": "2024-04-21T17:20:30.349075",
|
| 151 |
+
"last_accessed": "2024-05-31T17:20:30.349075"
|
| 152 |
+
}
|
| 153 |
+
}
|
| 154 |
+
},
|
| 155 |
+
"Active_Projects": {
|
| 156 |
+
"type": "folder",
|
| 157 |
+
"created_date": "2026-03-17T17:20:30.349075",
|
| 158 |
+
"contents": {
|
| 159 |
+
"q4_strategy_2024.pptx": {
|
| 160 |
+
"type": "file",
|
| 161 |
+
"extension": ".pptx",
|
| 162 |
+
"size_mb": 5.2,
|
| 163 |
+
"created_date": "2026-03-22T17:20:30.349075",
|
| 164 |
+
"last_accessed": "2026-04-09T17:20:30.349075"
|
| 165 |
+
},
|
| 166 |
+
"client_proposal_nov.docx": {
|
| 167 |
+
"type": "file",
|
| 168 |
+
"extension": ".docx",
|
| 169 |
+
"size_mb": 1.8,
|
| 170 |
+
"created_date": "2026-03-27T17:20:30.349075",
|
| 171 |
+
"last_accessed": "2026-04-10T17:20:30.349075"
|
| 172 |
+
},
|
| 173 |
+
"budget_forecast_2025.xlsx": {
|
| 174 |
+
"type": "file",
|
| 175 |
+
"extension": ".xlsx",
|
| 176 |
+
"size_mb": 3.1,
|
| 177 |
+
"created_date": "2026-04-01T17:20:30.349075",
|
| 178 |
+
"last_accessed": "2026-04-08T17:20:30.349075"
|
| 179 |
+
},
|
| 180 |
+
"product_roadmap.pdf": {
|
| 181 |
+
"type": "file",
|
| 182 |
+
"extension": ".pdf",
|
| 183 |
+
"size_mb": 2.4,
|
| 184 |
+
"created_date": "2026-04-06T17:20:30.349075",
|
| 185 |
+
"last_accessed": "2026-04-11T17:20:30.349075"
|
| 186 |
+
}
|
| 187 |
+
}
|
| 188 |
+
},
|
| 189 |
+
"Current_Code": {
|
| 190 |
+
"type": "folder",
|
| 191 |
+
"created_date": "2026-04-04T17:20:30.349075",
|
| 192 |
+
"contents": {
|
| 193 |
+
"main.py": {
|
| 194 |
+
"type": "file",
|
| 195 |
+
"extension": ".py",
|
| 196 |
+
"size_mb": 0.05,
|
| 197 |
+
"created_date": "2026-04-04T17:20:30.349075",
|
| 198 |
+
"last_accessed": "2026-04-11T17:20:30.349075"
|
| 199 |
+
},
|
| 200 |
+
"utils.py": {
|
| 201 |
+
"type": "file",
|
| 202 |
+
"extension": ".py",
|
| 203 |
+
"size_mb": 0.03,
|
| 204 |
+
"created_date": "2026-04-04T17:20:30.349075",
|
| 205 |
+
"last_accessed": "2026-04-10T17:20:30.349075"
|
| 206 |
+
},
|
| 207 |
+
"requirements.txt": {
|
| 208 |
+
"type": "file",
|
| 209 |
+
"extension": ".txt",
|
| 210 |
+
"size_mb": 0.01,
|
| 211 |
+
"created_date": "2026-04-04T17:20:30.349075",
|
| 212 |
+
"last_accessed": "2026-04-11T17:20:30.349075"
|
| 213 |
+
},
|
| 214 |
+
"README.md": {
|
| 215 |
+
"type": "file",
|
| 216 |
+
"extension": ".md",
|
| 217 |
+
"size_mb": 0.02,
|
| 218 |
+
"created_date": "2026-04-04T17:20:30.349075",
|
| 219 |
+
"last_accessed": "2026-04-11T17:20:30.349075"
|
| 220 |
+
},
|
| 221 |
+
"tests.py": {
|
| 222 |
+
"type": "file",
|
| 223 |
+
"extension": ".py",
|
| 224 |
+
"size_mb": 0.04,
|
| 225 |
+
"created_date": "2026-04-04T17:20:30.349075",
|
| 226 |
+
"last_accessed": "2026-04-11T17:20:30.349075"
|
| 227 |
+
}
|
| 228 |
+
}
|
| 229 |
+
}
|
| 230 |
+
}
|
| 231 |
+
}
|
environment/cloud_env.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from environment.file_system import FileSystem
|
| 2 |
+
from actions.file_actions import execute_action
|
| 3 |
+
from grader.grader import grade_state
|
| 4 |
+
from utils.helpers import load_initial_state
|
| 5 |
+
from utils.helpers import load_task_criteria
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class CloudDataJanitorEnv:
|
| 9 |
+
|
| 10 |
+
def __init__(self, task_name="easy"):
|
| 11 |
+
self.task_name = task_name
|
| 12 |
+
self.max_steps = 10
|
| 13 |
+
|
| 14 |
+
self.current_step = 0
|
| 15 |
+
self.done = False
|
| 16 |
+
|
| 17 |
+
self.fs = None
|
| 18 |
+
|
| 19 |
+
# REQUIRED
|
| 20 |
+
def reset(self):
|
| 21 |
+
|
| 22 |
+
self.current_step = 0
|
| 23 |
+
self.done = False
|
| 24 |
+
|
| 25 |
+
initial_state = load_initial_state()
|
| 26 |
+
|
| 27 |
+
criteria = load_task_criteria(
|
| 28 |
+
self.task_name
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
self.fs = FileSystem(
|
| 32 |
+
initial_state,
|
| 33 |
+
criteria
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# REQUIRED folders for grading
|
| 37 |
+
self.fs.create_folder("Documents")
|
| 38 |
+
self.fs.create_folder("Images")
|
| 39 |
+
self.fs.create_folder("Misc")
|
| 40 |
+
|
| 41 |
+
return self.state()
|
| 42 |
+
|
| 43 |
+
# REQUIRED
|
| 44 |
+
def state(self):
|
| 45 |
+
|
| 46 |
+
return {
|
| 47 |
+
"files": self.fs.list_files(),
|
| 48 |
+
"step": self.current_step,
|
| 49 |
+
"task": self.task_name
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
# REQUIRED
|
| 53 |
+
def step(self, action):
|
| 54 |
+
|
| 55 |
+
if self.done:
|
| 56 |
+
return self.state(), 0.0, True, {}
|
| 57 |
+
|
| 58 |
+
self.current_step += 1
|
| 59 |
+
|
| 60 |
+
error = None
|
| 61 |
+
|
| 62 |
+
# Handle done action
|
| 63 |
+
if action.get("action") == "done":
|
| 64 |
+
|
| 65 |
+
self.done = True
|
| 66 |
+
|
| 67 |
+
return (
|
| 68 |
+
self.state(),
|
| 69 |
+
1.0,
|
| 70 |
+
True,
|
| 71 |
+
{
|
| 72 |
+
"last_action_error": None
|
| 73 |
+
}
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
try:
|
| 77 |
+
|
| 78 |
+
action_type = action.get("action")
|
| 79 |
+
|
| 80 |
+
params = {
|
| 81 |
+
k: v
|
| 82 |
+
for k, v in action.items()
|
| 83 |
+
if k != "action"
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
execute_action(
|
| 87 |
+
self.fs,
|
| 88 |
+
action_type,
|
| 89 |
+
params
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
except Exception as e:
|
| 93 |
+
|
| 94 |
+
error = str(e)
|
| 95 |
+
|
| 96 |
+
score = grade_state(
|
| 97 |
+
self.fs,
|
| 98 |
+
self.task_name
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
reward = float(score)
|
| 102 |
+
|
| 103 |
+
if score >= 1.0:
|
| 104 |
+
self.done = True
|
| 105 |
+
|
| 106 |
+
if self.current_step >= self.max_steps:
|
| 107 |
+
self.done = True
|
| 108 |
+
|
| 109 |
+
return (
|
| 110 |
+
self.state(),
|
| 111 |
+
reward,
|
| 112 |
+
self.done,
|
| 113 |
+
{
|
| 114 |
+
"last_action_error": error
|
| 115 |
+
}
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
def close(self):
|
| 119 |
+
|
| 120 |
+
pass
|
environment/file_system.py
ADDED
|
@@ -0,0 +1,310 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import copy
|
| 3 |
+
import zipfile
|
| 4 |
+
import io
|
| 5 |
+
from datetime import datetime, timedelta
|
| 6 |
+
from typing import Optional
|
| 7 |
+
from config import INITIAL_STATE_FILE, FINAL_STATE_FILE
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class FileSystem:
|
| 11 |
+
"""
|
| 12 |
+
Simulates a cloud file system environment.
|
| 13 |
+
Maintains an in-memory representation of the file tree and
|
| 14 |
+
records every action taken by the agent.
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
def __init__(self, initial_state: dict, criteria: dict = None):
|
| 18 |
+
|
| 19 |
+
# store original state
|
| 20 |
+
self.initial_state = copy.deepcopy(initial_state)
|
| 21 |
+
|
| 22 |
+
# working state
|
| 23 |
+
self.state = copy.deepcopy(initial_state)
|
| 24 |
+
|
| 25 |
+
# task criteria
|
| 26 |
+
self.criteria = criteria or {}
|
| 27 |
+
|
| 28 |
+
self.action_log = []
|
| 29 |
+
|
| 30 |
+
self.root = self.state.get("root", {})
|
| 31 |
+
|
| 32 |
+
# ------------------------------------------------------------------
|
| 33 |
+
# Internal helpers
|
| 34 |
+
# ------------------------------------------------------------------
|
| 35 |
+
|
| 36 |
+
def _find_file(
|
| 37 |
+
self,
|
| 38 |
+
filename: str,
|
| 39 |
+
node: Optional[dict] = None,
|
| 40 |
+
path: str = ""
|
| 41 |
+
) -> Optional[tuple]:
|
| 42 |
+
|
| 43 |
+
if node is None:
|
| 44 |
+
node = self.root
|
| 45 |
+
|
| 46 |
+
for name, meta in node.items():
|
| 47 |
+
|
| 48 |
+
current_path = f"{path}/{name}" if path else name
|
| 49 |
+
|
| 50 |
+
if isinstance(meta, dict):
|
| 51 |
+
|
| 52 |
+
if meta.get("type") == "file":
|
| 53 |
+
|
| 54 |
+
if name == filename:
|
| 55 |
+
return (name, meta, node, current_path)
|
| 56 |
+
|
| 57 |
+
elif meta.get("type") == "folder":
|
| 58 |
+
|
| 59 |
+
contents = meta.get("contents", {})
|
| 60 |
+
|
| 61 |
+
result = self._find_file(
|
| 62 |
+
filename,
|
| 63 |
+
contents,
|
| 64 |
+
current_path
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
if result:
|
| 68 |
+
return result
|
| 69 |
+
|
| 70 |
+
return None
|
| 71 |
+
|
| 72 |
+
def _find_folder(
|
| 73 |
+
self,
|
| 74 |
+
folder_name: str,
|
| 75 |
+
node: Optional[dict] = None,
|
| 76 |
+
path: str = ""
|
| 77 |
+
) -> Optional[tuple]:
|
| 78 |
+
|
| 79 |
+
if node is None:
|
| 80 |
+
node = self.root
|
| 81 |
+
|
| 82 |
+
for name, meta in node.items():
|
| 83 |
+
|
| 84 |
+
current_path = f"{path}/{name}" if path else name
|
| 85 |
+
|
| 86 |
+
if isinstance(meta, dict) and meta.get("type") == "folder":
|
| 87 |
+
|
| 88 |
+
if name == folder_name:
|
| 89 |
+
return (name, meta, node, current_path)
|
| 90 |
+
|
| 91 |
+
result = self._find_folder(
|
| 92 |
+
folder_name,
|
| 93 |
+
meta.get("contents", {}),
|
| 94 |
+
current_path
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
if result:
|
| 98 |
+
return result
|
| 99 |
+
|
| 100 |
+
return None
|
| 101 |
+
|
| 102 |
+
def _get_all_files(
|
| 103 |
+
self,
|
| 104 |
+
node: Optional[dict] = None,
|
| 105 |
+
path: str = ""
|
| 106 |
+
) -> list:
|
| 107 |
+
|
| 108 |
+
if node is None:
|
| 109 |
+
node = self.root
|
| 110 |
+
|
| 111 |
+
files = []
|
| 112 |
+
|
| 113 |
+
for name, meta in node.items():
|
| 114 |
+
|
| 115 |
+
current_path = f"{path}/{name}" if path else name
|
| 116 |
+
|
| 117 |
+
if isinstance(meta, dict):
|
| 118 |
+
|
| 119 |
+
if meta.get("type") == "file":
|
| 120 |
+
|
| 121 |
+
files.append({
|
| 122 |
+
"name": name,
|
| 123 |
+
"path": current_path,
|
| 124 |
+
**meta # 🔥 THIS IS THE FIX
|
| 125 |
+
})
|
| 126 |
+
|
| 127 |
+
elif meta.get("type") == "folder":
|
| 128 |
+
|
| 129 |
+
files.extend(
|
| 130 |
+
self._get_all_files(
|
| 131 |
+
meta.get("contents", {}),
|
| 132 |
+
current_path
|
| 133 |
+
)
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
return files
|
| 137 |
+
|
| 138 |
+
def _get_folder_size_mb(
|
| 139 |
+
self,
|
| 140 |
+
folder_meta: dict
|
| 141 |
+
) -> float:
|
| 142 |
+
|
| 143 |
+
total = 0.0
|
| 144 |
+
|
| 145 |
+
for name, meta in folder_meta.get("contents", {}).items():
|
| 146 |
+
|
| 147 |
+
if isinstance(meta, dict):
|
| 148 |
+
|
| 149 |
+
if meta.get("type") == "file":
|
| 150 |
+
|
| 151 |
+
total += meta.get("size_mb", 0)
|
| 152 |
+
|
| 153 |
+
elif meta.get("type") == "folder":
|
| 154 |
+
|
| 155 |
+
total += self._get_folder_size_mb(meta)
|
| 156 |
+
|
| 157 |
+
return total
|
| 158 |
+
|
| 159 |
+
# ------------------------------------------------------------------
|
| 160 |
+
# Observation
|
| 161 |
+
# ------------------------------------------------------------------
|
| 162 |
+
|
| 163 |
+
def get_observation(self) -> dict:
|
| 164 |
+
|
| 165 |
+
return {
|
| 166 |
+
"root": copy.deepcopy(self.root),
|
| 167 |
+
"action_log": list(self.action_log),
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
def list_files(self) -> list:
|
| 171 |
+
|
| 172 |
+
return self._get_all_files()
|
| 173 |
+
|
| 174 |
+
def get_file_info(
|
| 175 |
+
self,
|
| 176 |
+
filename: str
|
| 177 |
+
) -> Optional[dict]:
|
| 178 |
+
|
| 179 |
+
result = self._find_file(filename)
|
| 180 |
+
|
| 181 |
+
if result:
|
| 182 |
+
|
| 183 |
+
name, meta, parent, path = result
|
| 184 |
+
|
| 185 |
+
return {
|
| 186 |
+
"name": name,
|
| 187 |
+
"path": path,
|
| 188 |
+
**meta
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
return None
|
| 192 |
+
|
| 193 |
+
# ------------------------------------------------------------------
|
| 194 |
+
# Actions
|
| 195 |
+
# ------------------------------------------------------------------
|
| 196 |
+
|
| 197 |
+
def create_folder(self, name: str) -> dict:
|
| 198 |
+
|
| 199 |
+
if name in self.root:
|
| 200 |
+
|
| 201 |
+
return {
|
| 202 |
+
"success": False,
|
| 203 |
+
"message": f"Folder '{name}' already exists."
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
self.root[name] = {
|
| 207 |
+
"type": "folder",
|
| 208 |
+
"created_date": datetime.now().isoformat(),
|
| 209 |
+
"contents": {},
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
self.action_log.append({
|
| 213 |
+
"action": "create_folder",
|
| 214 |
+
"name": name
|
| 215 |
+
})
|
| 216 |
+
|
| 217 |
+
return {
|
| 218 |
+
"success": True,
|
| 219 |
+
"message": f"Folder '{name}' created."
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
def move_file(
|
| 223 |
+
self,
|
| 224 |
+
filename: str,
|
| 225 |
+
destination_folder: str
|
| 226 |
+
) -> dict:
|
| 227 |
+
|
| 228 |
+
file_result = self._find_file(filename)
|
| 229 |
+
|
| 230 |
+
if not file_result:
|
| 231 |
+
|
| 232 |
+
return {
|
| 233 |
+
"success": False,
|
| 234 |
+
"message": f"File '{filename}' not found."
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
f_name, f_meta, f_parent, f_path = file_result
|
| 238 |
+
|
| 239 |
+
folder_result = self._find_folder(destination_folder)
|
| 240 |
+
|
| 241 |
+
if not folder_result:
|
| 242 |
+
|
| 243 |
+
self.create_folder(destination_folder)
|
| 244 |
+
|
| 245 |
+
folder_result = self._find_folder(destination_folder)
|
| 246 |
+
|
| 247 |
+
_, folder_meta, _, folder_path = folder_result
|
| 248 |
+
|
| 249 |
+
dest_contents = folder_meta.get("contents", {})
|
| 250 |
+
|
| 251 |
+
if filename in dest_contents:
|
| 252 |
+
|
| 253 |
+
return {
|
| 254 |
+
"success": False,
|
| 255 |
+
"message": f"File '{filename}' already exists."
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
dest_contents[filename] = f_meta
|
| 259 |
+
|
| 260 |
+
del f_parent[f_name]
|
| 261 |
+
|
| 262 |
+
self.action_log.append({
|
| 263 |
+
"action": "move_file",
|
| 264 |
+
"file": filename,
|
| 265 |
+
"destination": destination_folder
|
| 266 |
+
})
|
| 267 |
+
|
| 268 |
+
return {
|
| 269 |
+
"success": True,
|
| 270 |
+
"message": f"Moved '{filename}' → '{destination_folder}'."
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
def delete_file(self, filename: str) -> dict:
|
| 274 |
+
|
| 275 |
+
result = self._find_file(filename)
|
| 276 |
+
|
| 277 |
+
if not result:
|
| 278 |
+
|
| 279 |
+
return {
|
| 280 |
+
"success": False,
|
| 281 |
+
"message": f"File '{filename}' not found."
|
| 282 |
+
}
|
| 283 |
+
|
| 284 |
+
f_name, f_meta, f_parent, f_path = result
|
| 285 |
+
|
| 286 |
+
size = f_meta.get("size_mb", 0)
|
| 287 |
+
|
| 288 |
+
del f_parent[f_name]
|
| 289 |
+
|
| 290 |
+
self.action_log.append({
|
| 291 |
+
"action": "delete_file",
|
| 292 |
+
"file": filename,
|
| 293 |
+
"size_mb": size
|
| 294 |
+
})
|
| 295 |
+
|
| 296 |
+
return {
|
| 297 |
+
"success": True,
|
| 298 |
+
"message": f"Deleted '{filename}'."
|
| 299 |
+
}
|
| 300 |
+
|
| 301 |
+
# ------------------------------------------------------------------
|
| 302 |
+
# REQUIRED FOR GRADER / OPENENV
|
| 303 |
+
# ------------------------------------------------------------------
|
| 304 |
+
|
| 305 |
+
def get_state(self) -> dict:
|
| 306 |
+
|
| 307 |
+
return {
|
| 308 |
+
"root": copy.deepcopy(self.root),
|
| 309 |
+
"action_log": list(self.action_log),
|
| 310 |
+
}
|
grader/grader.py
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# grader/grader.py
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
from datetime import datetime, timedelta
|
| 5 |
+
from utils.helpers import flatten_file_list, total_size_mb, format_size
|
| 6 |
+
from config import (
|
| 7 |
+
CORRECT_MOVE_SCORE, INCORRECT_PENALTY,
|
| 8 |
+
TASK_EASY, TASK_MEDIUM, TASK_HARD,
|
| 9 |
+
STORAGE_LIMIT_MB, RECENT_DAYS_THRESHOLD, OLD_FILE_THRESHOLD_YEARS,
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class Grader:
|
| 14 |
+
"""Evaluate the agent's performance on a given task."""
|
| 15 |
+
|
| 16 |
+
def __init__(self, task_type: str, initial_state: dict, criteria: dict):
|
| 17 |
+
self.task_type = task_type
|
| 18 |
+
self.initial_state = initial_state
|
| 19 |
+
self.criteria = criteria
|
| 20 |
+
|
| 21 |
+
# ------------------------------------------------------------------
|
| 22 |
+
# Public interface
|
| 23 |
+
# ------------------------------------------------------------------
|
| 24 |
+
|
| 25 |
+
def grade(self, final_state: dict) -> dict:
|
| 26 |
+
"""Grade the final filesystem state and return a detailed report."""
|
| 27 |
+
if self.task_type == TASK_EASY:
|
| 28 |
+
return self._grade_easy(final_state)
|
| 29 |
+
elif self.task_type == TASK_MEDIUM:
|
| 30 |
+
return self._grade_medium(final_state)
|
| 31 |
+
elif self.task_type == TASK_HARD:
|
| 32 |
+
return self._grade_hard(final_state)
|
| 33 |
+
else:
|
| 34 |
+
raise ValueError(f"Unknown task type: {self.task_type}")
|
| 35 |
+
|
| 36 |
+
# ------------------------------------------------------------------
|
| 37 |
+
# Easy grader
|
| 38 |
+
# ------------------------------------------------------------------
|
| 39 |
+
|
| 40 |
+
def _grade_easy(self, final_state: dict) -> dict:
|
| 41 |
+
root = final_state.get("root", {})
|
| 42 |
+
all_files = flatten_file_list(root)
|
| 43 |
+
file_locations: dict[str, str] = {f["name"]: f["path"] for f in all_files}
|
| 44 |
+
|
| 45 |
+
score = 0.0
|
| 46 |
+
max_score = 0.0
|
| 47 |
+
details = []
|
| 48 |
+
|
| 49 |
+
# Check PDFs in Documents
|
| 50 |
+
for fname in self.criteria["files_to_move_to_Documents"]:
|
| 51 |
+
max_score += CORRECT_MOVE_SCORE
|
| 52 |
+
path = file_locations.get(fname, "")
|
| 53 |
+
if "Documents" in path:
|
| 54 |
+
score += CORRECT_MOVE_SCORE
|
| 55 |
+
details.append({"file": fname, "status": "✅ Correctly in Documents", "points": CORRECT_MOVE_SCORE})
|
| 56 |
+
else:
|
| 57 |
+
details.append({"file": fname, "status": f"❌ Not in Documents (found at: {path or 'missing'})", "points": 0})
|
| 58 |
+
|
| 59 |
+
# Check JPGs in Images
|
| 60 |
+
for fname in self.criteria["files_to_move_to_Images"]:
|
| 61 |
+
max_score += CORRECT_MOVE_SCORE
|
| 62 |
+
path = file_locations.get(fname, "")
|
| 63 |
+
if "Images" in path:
|
| 64 |
+
score += CORRECT_MOVE_SCORE
|
| 65 |
+
details.append({"file": fname, "status": "✅ Correctly in Images", "points": CORRECT_MOVE_SCORE})
|
| 66 |
+
else:
|
| 67 |
+
details.append({"file": fname, "status": f"❌ Not in Images (found at: {path or 'missing'})", "points": 0})
|
| 68 |
+
|
| 69 |
+
# Check non-target files are untouched
|
| 70 |
+
collateral_damage = []
|
| 71 |
+
for fname in self.criteria["files_to_leave_untouched"]:
|
| 72 |
+
if fname not in file_locations:
|
| 73 |
+
collateral_damage.append(fname)
|
| 74 |
+
score += INCORRECT_PENALTY
|
| 75 |
+
details.append({"file": fname, "status": "❌ COLLATERAL DAMAGE – file was deleted!", "points": INCORRECT_PENALTY})
|
| 76 |
+
|
| 77 |
+
percentage = round((score / max_score) * 100, 1) if max_score > 0 else 0.0
|
| 78 |
+
|
| 79 |
+
return {
|
| 80 |
+
"task": TASK_EASY,
|
| 81 |
+
"score": round(score, 2),
|
| 82 |
+
"max_score": round(max_score, 2),
|
| 83 |
+
"percentage": percentage,
|
| 84 |
+
"collateral_damage": collateral_damage,
|
| 85 |
+
"details": details,
|
| 86 |
+
"passed": percentage >= 90.0,
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
# ------------------------------------------------------------------
|
| 90 |
+
# Medium grader
|
| 91 |
+
# ------------------------------------------------------------------
|
| 92 |
+
|
| 93 |
+
def _grade_medium(self, final_state: dict) -> dict:
|
| 94 |
+
root = final_state.get("root", {})
|
| 95 |
+
all_files = flatten_file_list(root)
|
| 96 |
+
remaining_names = {f["name"] for f in all_files}
|
| 97 |
+
|
| 98 |
+
to_delete = set(self.criteria["files_to_delete"])
|
| 99 |
+
to_preserve = set(self.criteria["files_to_preserve"])
|
| 100 |
+
|
| 101 |
+
correctly_deleted = to_delete - remaining_names
|
| 102 |
+
missed_duplicates = to_delete & remaining_names
|
| 103 |
+
collateral_damage = to_preserve - remaining_names
|
| 104 |
+
|
| 105 |
+
score = (len(correctly_deleted) * CORRECT_MOVE_SCORE
|
| 106 |
+
- len(collateral_damage) * abs(INCORRECT_PENALTY))
|
| 107 |
+
max_score = len(to_delete) * CORRECT_MOVE_SCORE
|
| 108 |
+
|
| 109 |
+
percentage = round((max(score, 0) / max_score) * 100, 1) if max_score > 0 else 0.0
|
| 110 |
+
|
| 111 |
+
return {
|
| 112 |
+
"task": TASK_MEDIUM,
|
| 113 |
+
"score": round(score, 2),
|
| 114 |
+
"max_score": round(max_score, 2),
|
| 115 |
+
"percentage": percentage,
|
| 116 |
+
"correctly_deleted": sorted(correctly_deleted),
|
| 117 |
+
"missed_duplicates": sorted(missed_duplicates),
|
| 118 |
+
"collateral_damage": sorted(collateral_damage),
|
| 119 |
+
"passed": percentage >= 90.0 and len(collateral_damage) == 0,
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
# ------------------------------------------------------------------
|
| 123 |
+
# Hard grader
|
| 124 |
+
# ------------------------------------------------------------------
|
| 125 |
+
|
| 126 |
+
def _grade_hard(self, final_state: dict) -> dict:
|
| 127 |
+
initial_root = self.initial_state.get("root", {})
|
| 128 |
+
final_root = final_state.get("root", {})
|
| 129 |
+
|
| 130 |
+
initial_files = flatten_file_list(initial_root)
|
| 131 |
+
final_files = flatten_file_list(final_root)
|
| 132 |
+
|
| 133 |
+
initial_total_mb = total_size_mb(initial_files)
|
| 134 |
+
final_total_mb = total_size_mb(final_files)
|
| 135 |
+
freed_mb = initial_total_mb - final_total_mb
|
| 136 |
+
|
| 137 |
+
goal_mb = self.criteria.get("storage_goal_mb", STORAGE_LIMIT_MB)
|
| 138 |
+
goal_met = freed_mb >= goal_mb
|
| 139 |
+
|
| 140 |
+
# Check for protected folder violations
|
| 141 |
+
protected = self.criteria.get("folders_to_protect", [])
|
| 142 |
+
violations = []
|
| 143 |
+
for folder_name in protected:
|
| 144 |
+
# Protected folder should still exist with same files
|
| 145 |
+
initial_folder = initial_root.get(folder_name, {})
|
| 146 |
+
final_folder = final_root.get(folder_name, {})
|
| 147 |
+
if not final_folder:
|
| 148 |
+
violations.append(f"Protected folder '{folder_name}' was deleted or moved!")
|
| 149 |
+
continue
|
| 150 |
+
initial_contents = set(initial_folder.get("contents", {}).keys())
|
| 151 |
+
final_contents = set(final_folder.get("contents", {}).keys())
|
| 152 |
+
missing = initial_contents - final_contents
|
| 153 |
+
if missing:
|
| 154 |
+
violations.append(f"Files removed from protected '{folder_name}': {missing}")
|
| 155 |
+
|
| 156 |
+
# Check for recently accessed file violations
|
| 157 |
+
cutoff = datetime.now() - timedelta(days=RECENT_DAYS_THRESHOLD)
|
| 158 |
+
initial_names = {f["name"]: f for f in initial_files}
|
| 159 |
+
final_names = {f["name"] for f in final_files}
|
| 160 |
+
recent_deleted = []
|
| 161 |
+
for fname, fmeta in initial_names.items():
|
| 162 |
+
accessed_str = fmeta.get("last_accessed", "")
|
| 163 |
+
try:
|
| 164 |
+
accessed = datetime.fromisoformat(accessed_str)
|
| 165 |
+
except (ValueError, TypeError):
|
| 166 |
+
continue
|
| 167 |
+
if accessed >= cutoff and fname not in final_names:
|
| 168 |
+
recent_deleted.append(fname)
|
| 169 |
+
|
| 170 |
+
# Score
|
| 171 |
+
score = 0.0
|
| 172 |
+
max_score = 3.0
|
| 173 |
+
if goal_met:
|
| 174 |
+
score += 1.0
|
| 175 |
+
if not violations:
|
| 176 |
+
score += 1.0
|
| 177 |
+
if not recent_deleted:
|
| 178 |
+
score += 1.0
|
| 179 |
+
|
| 180 |
+
percentage = round((score / max_score) * 100, 1)
|
| 181 |
+
|
| 182 |
+
return {
|
| 183 |
+
"task": TASK_HARD,
|
| 184 |
+
"score": round(score, 2),
|
| 185 |
+
"max_score": max_score,
|
| 186 |
+
"percentage": percentage,
|
| 187 |
+
"freed_mb": round(freed_mb, 2),
|
| 188 |
+
"freed_human": format_size(freed_mb),
|
| 189 |
+
"goal_mb": goal_mb,
|
| 190 |
+
"goal_met": goal_met,
|
| 191 |
+
"protected_folder_violations": violations,
|
| 192 |
+
"recent_files_deleted": recent_deleted,
|
| 193 |
+
"passed": goal_met and not violations and not recent_deleted,
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
# ------------------------------------------------------------------
|
| 198 |
+
# Convenience function
|
| 199 |
+
# ------------------------------------------------------------------
|
| 200 |
+
|
| 201 |
+
def grade_from_files(task_type: str, initial_path: str, final_path: str, criteria: dict) -> dict:
|
| 202 |
+
"""Load state from JSON files and grade."""
|
| 203 |
+
with open(initial_path) as f:
|
| 204 |
+
initial_state = json.load(f)
|
| 205 |
+
with open(final_path) as f:
|
| 206 |
+
final_state = json.load(f)
|
| 207 |
+
grader = Grader(task_type, initial_state, criteria)
|
| 208 |
+
return grader.grade(final_state)
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
def print_report(report: dict):
|
| 212 |
+
"""Print a human-readable grade report."""
|
| 213 |
+
print("\n" + "=" * 60)
|
| 214 |
+
print(f" GRADE REPORT – {report['task'].upper()} TASK")
|
| 215 |
+
print("=" * 60)
|
| 216 |
+
print(f" Score : {report['score']} / {report['max_score']}")
|
| 217 |
+
print(f" Grade : {report['percentage']}%")
|
| 218 |
+
print(f" Passed : {'✅ YES' if report['passed'] else '❌ NO'}")
|
| 219 |
+
|
| 220 |
+
if report["task"] == TASK_EASY:
|
| 221 |
+
dmg = report.get("collateral_damage", [])
|
| 222 |
+
print(f" Collateral damage: {len(dmg)} file(s)")
|
| 223 |
+
for d in report.get("details", []):
|
| 224 |
+
print(f" {d['status']} [{d['file']}]")
|
| 225 |
+
|
| 226 |
+
elif report["task"] == TASK_MEDIUM:
|
| 227 |
+
print(f" Correctly deleted : {len(report.get('correctly_deleted', []))}")
|
| 228 |
+
print(f" Missed duplicates : {len(report.get('missed_duplicates', []))}")
|
| 229 |
+
print(f" Collateral damage : {len(report.get('collateral_damage', []))}")
|
| 230 |
+
|
| 231 |
+
elif report["task"] == TASK_HARD:
|
| 232 |
+
goal = "✅" if report["goal_met"] else "❌"
|
| 233 |
+
print(f" Storage freed : {report['freed_human']} {goal}")
|
| 234 |
+
print(f" Goal : {format_size(report['goal_mb'])}")
|
| 235 |
+
if report["protected_folder_violations"]:
|
| 236 |
+
for v in report["protected_folder_violations"]:
|
| 237 |
+
print(f" ⚠️ {v}")
|
| 238 |
+
if report["recent_files_deleted"]:
|
| 239 |
+
print(f" ⚠️ Recent files deleted: {report['recent_files_deleted']}")
|
| 240 |
+
|
| 241 |
+
print("=" * 60 + "\n")
|
| 242 |
+
|
| 243 |
+
# ------------------------------------------------------------------
|
| 244 |
+
# OpenEnv compatibility wrapper
|
| 245 |
+
# ------------------------------------------------------------------
|
| 246 |
+
|
| 247 |
+
def grade_state(fs, task_name):
|
| 248 |
+
"""
|
| 249 |
+
Convert your existing grader output into
|
| 250 |
+
OpenEnv reward format (0.0–1.0)
|
| 251 |
+
"""
|
| 252 |
+
|
| 253 |
+
# Get current filesystem state
|
| 254 |
+
final_state = fs.get_state()
|
| 255 |
+
|
| 256 |
+
# Use the same initial state
|
| 257 |
+
initial_state = fs.initial_state
|
| 258 |
+
|
| 259 |
+
# Get task criteria from config or task loader
|
| 260 |
+
criteria = fs.criteria
|
| 261 |
+
|
| 262 |
+
grader = Grader(
|
| 263 |
+
task_type=task_name,
|
| 264 |
+
initial_state=initial_state,
|
| 265 |
+
criteria=criteria
|
| 266 |
+
)
|
| 267 |
+
|
| 268 |
+
report = grader.grade(final_state)
|
| 269 |
+
|
| 270 |
+
# Convert percentage to reward
|
| 271 |
+
reward = report["percentage"] / 100.0
|
| 272 |
+
|
| 273 |
+
# Ensure valid range
|
| 274 |
+
reward = max(0.0, min(1.0, reward))
|
| 275 |
+
|
| 276 |
+
return reward
|
inference.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from environment.cloud_env import CloudDataJanitorEnv
|
| 2 |
+
from agent.rule_agent import RuleBasedAgent
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
API_BASE_URL = os.getenv("API_BASE_URL", "local")
|
| 6 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "rule-agent")
|
| 7 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 8 |
+
LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME")
|
| 9 |
+
|
| 10 |
+
TASK_NAME = os.getenv("MY_ENV_V4_TASK", "easy")
|
| 11 |
+
MAX_STEPS = 10
|
| 12 |
+
|
| 13 |
+
env = CloudDataJanitorEnv(task_name=TASK_NAME)
|
| 14 |
+
|
| 15 |
+
agent = RuleBasedAgent()
|
| 16 |
+
|
| 17 |
+
rewards = []
|
| 18 |
+
|
| 19 |
+
# IMPORTANT — define success before try
|
| 20 |
+
success = False
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
|
| 24 |
+
print(
|
| 25 |
+
"[START] "
|
| 26 |
+
f"task={TASK_NAME} "
|
| 27 |
+
"env=cloud-data-janitor "
|
| 28 |
+
"model=rule-agent"
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
state = env.reset()
|
| 32 |
+
|
| 33 |
+
for step in range(1, MAX_STEPS + 1):
|
| 34 |
+
|
| 35 |
+
action = agent.choose_action(state)
|
| 36 |
+
state, reward, done, info = env.step(action)
|
| 37 |
+
|
| 38 |
+
rewards.append(reward)
|
| 39 |
+
|
| 40 |
+
error = info.get("last_action_error")
|
| 41 |
+
|
| 42 |
+
print(
|
| 43 |
+
"[STEP] "
|
| 44 |
+
f"step={step} "
|
| 45 |
+
f"action={action} "
|
| 46 |
+
f"reward={reward:.2f} "
|
| 47 |
+
f"done={str(done).lower()} "
|
| 48 |
+
f"error={error if error else 'null'}"
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
if done:
|
| 52 |
+
|
| 53 |
+
success = True
|
| 54 |
+
break
|
| 55 |
+
|
| 56 |
+
# If loop ended without done
|
| 57 |
+
if not success and rewards:
|
| 58 |
+
|
| 59 |
+
success = done
|
| 60 |
+
|
| 61 |
+
except Exception as e:
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
print(
|
| 66 |
+
"[STEP] "
|
| 67 |
+
"step=0 "
|
| 68 |
+
"action=error "
|
| 69 |
+
"reward=0.00 "
|
| 70 |
+
"done=true "
|
| 71 |
+
f"error={str(e)}"
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
finally:
|
| 75 |
+
|
| 76 |
+
env.close()
|
| 77 |
+
|
| 78 |
+
if rewards:
|
| 79 |
+
|
| 80 |
+
score = rewards[-1]
|
| 81 |
+
|
| 82 |
+
rewards_str = ",".join(
|
| 83 |
+
f"{r:.2f}" for r in rewards
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
else:
|
| 87 |
+
|
| 88 |
+
score = 0.0
|
| 89 |
+
rewards_str = "0.00"
|
| 90 |
+
|
| 91 |
+
print(
|
| 92 |
+
"[END] "
|
| 93 |
+
f"success={str(success).lower()} "
|
| 94 |
+
f"steps={len(rewards)} "
|
| 95 |
+
f"score={score:.2f} "
|
| 96 |
+
f"rewards={rewards_str}"
|
| 97 |
+
)
|
openenv.yaml
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: cloud-data-janitor
|
| 2 |
+
|
| 3 |
+
description: |
|
| 4 |
+
AI environment where an agent cleans and organizes
|
| 5 |
+
messy cloud storage systems.
|
| 6 |
+
|
| 7 |
+
version: 1.0
|
| 8 |
+
|
| 9 |
+
entry_point:
|
| 10 |
+
|
| 11 |
+
module: environment.cloud_env
|
| 12 |
+
class: CloudDataJanitorEnv
|
| 13 |
+
|
| 14 |
+
tasks:
|
| 15 |
+
|
| 16 |
+
- name: easy
|
| 17 |
+
description: organize files
|
| 18 |
+
|
| 19 |
+
- name: medium
|
| 20 |
+
description: remove duplicates
|
| 21 |
+
|
| 22 |
+
- name: hard
|
| 23 |
+
description: optimize storage
|
| 24 |
+
|
| 25 |
+
max_steps: 10
|
| 26 |
+
|
| 27 |
+
reward_range:
|
| 28 |
+
|
| 29 |
+
min: 0.0
|
| 30 |
+
max: 1.0
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pyyaml
|
| 2 |
+
openenv-core
|
tasks/easy_task.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# tasks/easy_task.py
|
| 2 |
+
|
| 3 |
+
import random
|
| 4 |
+
import json
|
| 5 |
+
from datetime import datetime, timedelta
|
| 6 |
+
from config import INITIAL_STATE_FILE
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def generate_easy_initial_state(seed: int = 42) -> dict:
|
| 10 |
+
"""
|
| 11 |
+
Generate a chaotic root directory of 50 loose files mixing .pdf and .jpg files
|
| 12 |
+
(plus a few other types as distractors).
|
| 13 |
+
|
| 14 |
+
Easy Task Goal: Move all .pdf files into 'Documents' and all .jpg into 'Images'.
|
| 15 |
+
"""
|
| 16 |
+
random.seed(seed)
|
| 17 |
+
|
| 18 |
+
pdf_names = [
|
| 19 |
+
"report_q1.pdf", "resume.pdf", "invoice_march.pdf", "lecture_notes.pdf",
|
| 20 |
+
"thesis_final.pdf", "budget_2023.pdf", "contract_v2.pdf", "manual_printer.pdf",
|
| 21 |
+
"summary_meeting.pdf", "tax_return.pdf", "handbook_employee.pdf", "research_paper.pdf",
|
| 22 |
+
"presentation_slides.pdf", "policy_document.pdf", "legal_agreement.pdf",
|
| 23 |
+
]
|
| 24 |
+
jpg_names = [
|
| 25 |
+
"vacation_beach.jpg", "birthday_party.jpg", "profile_photo.jpg", "cat_sleeping.jpg",
|
| 26 |
+
"sunset_view.jpg", "family_reunion.jpg", "graduation_photo.jpg", "dog_playing.jpg",
|
| 27 |
+
"concert_night.jpg", "wedding_photo.jpg", "landscape_mountain.jpg", "food_pizza.jpg",
|
| 28 |
+
"selfie_park.jpg", "baby_shower.jpg", "office_team.jpg",
|
| 29 |
+
]
|
| 30 |
+
other_names = [
|
| 31 |
+
"notes.txt", "script.py", "data_dump.csv", "app_backup.zip",
|
| 32 |
+
"config.json", "readme.md", "todo_list.txt", "analysis.xlsx",
|
| 33 |
+
"old_code.js", "music_playlist.mp3",
|
| 34 |
+
"video_clip.mp4", "presentation.pptx", "design.svg", "database.sql",
|
| 35 |
+
"archive_old.tar",
|
| 36 |
+
"spreadsheet.xls", "photo.png", "sample.docx", "audio_note.wav",
|
| 37 |
+
"temp_file.tmp",
|
| 38 |
+
]
|
| 39 |
+
|
| 40 |
+
def make_file(name: str, size_mb: float, days_ago: int) -> dict:
|
| 41 |
+
accessed = (datetime.now() - timedelta(days=days_ago)).isoformat()
|
| 42 |
+
created = (datetime.now() - timedelta(days=days_ago + 30)).isoformat()
|
| 43 |
+
ext = "." + name.rsplit(".", 1)[-1] if "." in name else ""
|
| 44 |
+
return {
|
| 45 |
+
"type": "file",
|
| 46 |
+
"extension": ext,
|
| 47 |
+
"size_mb": round(size_mb, 2),
|
| 48 |
+
"created_date": created,
|
| 49 |
+
"last_accessed": accessed,
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
root = {}
|
| 53 |
+
for name in pdf_names:
|
| 54 |
+
root[name] = make_file(name, random.uniform(0.5, 5.0), random.randint(10, 500))
|
| 55 |
+
for name in jpg_names:
|
| 56 |
+
root[name] = make_file(name, random.uniform(1.0, 8.0), random.randint(5, 400))
|
| 57 |
+
for name in other_names:
|
| 58 |
+
root[name] = make_file(name, random.uniform(0.1, 20.0), random.randint(1, 600))
|
| 59 |
+
|
| 60 |
+
state = {"root": root}
|
| 61 |
+
return state
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def get_easy_task_description() -> str:
|
| 65 |
+
return (
|
| 66 |
+
"EASY TASK – File Grouping:\n"
|
| 67 |
+
"You have 50 loose files in the root directory. Your job is to:\n"
|
| 68 |
+
"1. Create a folder called 'Documents' and move ALL .pdf files into it.\n"
|
| 69 |
+
"2. Create a folder called 'Images' and move ALL .jpg files into it.\n"
|
| 70 |
+
"3. Leave all other files exactly where they are.\n"
|
| 71 |
+
"Complete the task when all .pdf files are in 'Documents' and all .jpg files are in 'Images'."
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def get_easy_grading_criteria(initial_state: dict) -> dict:
|
| 76 |
+
"""Return the expected final state criteria for grading."""
|
| 77 |
+
root = initial_state["root"]
|
| 78 |
+
pdf_files = [name for name, meta in root.items()
|
| 79 |
+
if isinstance(meta, dict) and name.endswith(".pdf")]
|
| 80 |
+
jpg_files = [name for name, meta in root.items()
|
| 81 |
+
if isinstance(meta, dict) and name.endswith(".jpg")]
|
| 82 |
+
other_files = [name for name, meta in root.items()
|
| 83 |
+
if isinstance(meta, dict) and not name.endswith((".pdf", ".jpg"))]
|
| 84 |
+
return {
|
| 85 |
+
"files_to_move_to_Documents": pdf_files,
|
| 86 |
+
"files_to_move_to_Images": jpg_files,
|
| 87 |
+
"files_to_leave_untouched": other_files,
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
state = generate_easy_initial_state()
|
| 93 |
+
print(f"Generated {len(state['root'])} files.")
|
| 94 |
+
with open(INITIAL_STATE_FILE, "w") as f:
|
| 95 |
+
import os; os.makedirs("data", exist_ok=True)
|
| 96 |
+
json.dump(state, f, indent=2)
|
| 97 |
+
print(f"Saved to {INITIAL_STATE_FILE}")
|
tasks/hard_task.py
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# tasks/hard_task.py
|
| 2 |
+
|
| 3 |
+
import random
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
from datetime import datetime, timedelta
|
| 7 |
+
from config import INITIAL_STATE_FILE, STORAGE_LIMIT_MB
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def _make_file(name: str, size_mb: float, days_ago_created: int, days_ago_accessed: int) -> dict:
|
| 11 |
+
accessed = (datetime.now() - timedelta(days=days_ago_accessed)).isoformat()
|
| 12 |
+
created = (datetime.now() - timedelta(days=days_ago_created)).isoformat()
|
| 13 |
+
ext = "." + name.rsplit(".", 1)[-1] if "." in name else ""
|
| 14 |
+
return {
|
| 15 |
+
"type": "file",
|
| 16 |
+
"extension": ext,
|
| 17 |
+
"size_mb": round(size_mb, 2),
|
| 18 |
+
"created_date": created,
|
| 19 |
+
"last_accessed": accessed,
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def _make_folder(contents: dict, created_days_ago: int = 500) -> dict:
|
| 24 |
+
return {
|
| 25 |
+
"type": "folder",
|
| 26 |
+
"created_date": (datetime.now() - timedelta(days=created_days_ago)).isoformat(),
|
| 27 |
+
"contents": contents,
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def generate_hard_initial_state(seed: int = 99) -> dict:
|
| 32 |
+
"""
|
| 33 |
+
Generate a complex file system for the Storage Diet task.
|
| 34 |
+
|
| 35 |
+
Contains:
|
| 36 |
+
- Old files (>3 years) that should be deleted
|
| 37 |
+
- Large inactive folders that should be zipped
|
| 38 |
+
- Recent project files that must NOT be touched
|
| 39 |
+
- A mix of everything to make the agent think carefully
|
| 40 |
+
|
| 41 |
+
Hard Task Goal: Free up at least 500 MB without touching recent files.
|
| 42 |
+
"""
|
| 43 |
+
random.seed(seed)
|
| 44 |
+
|
| 45 |
+
# --- OLD files (>3 years, fair game for deletion) ---
|
| 46 |
+
old_files = {
|
| 47 |
+
"old_backup_2018.zip": _make_file("old_backup_2018.zip", 210.0, 1900, 1800),
|
| 48 |
+
"legacy_code_archive.tar": _make_file("legacy_code_archive.tar", 85.0, 1500, 1400),
|
| 49 |
+
"project_alpha_2019.docx": _make_file("project_alpha_2019.docx", 12.0, 1700, 1600),
|
| 50 |
+
"annual_report_2018.pdf": _make_file("annual_report_2018.pdf", 8.5, 1800, 1750),
|
| 51 |
+
"tax_return_2019.pdf": _make_file("tax_return_2019.pdf", 3.2, 1600, 1550),
|
| 52 |
+
"database_snapshot_2018.sql": _make_file("database_snapshot_2018.sql", 320.0, 2000, 1900),
|
| 53 |
+
"old_presentation_2017.pptx": _make_file("old_presentation_2017.pptx", 45.0, 2200, 2100),
|
| 54 |
+
"meeting_notes_2018.txt": _make_file("meeting_notes_2018.txt", 0.2, 1850, 1800),
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
# --- INACTIVE large folders (older, not recently accessed → zip candidates) ---
|
| 58 |
+
inactive_design_assets = _make_folder({
|
| 59 |
+
"logo_v1.psd": _make_file("logo_v1.psd", 95.0, 900, 750),
|
| 60 |
+
"banner_draft.psd": _make_file("banner_draft.psd", 110.0, 850, 800),
|
| 61 |
+
"icons_set.ai": _make_file("icons_set.ai", 60.0, 950, 900),
|
| 62 |
+
"mockup_website.fig": _make_file("mockup_website.fig", 75.0, 870, 820),
|
| 63 |
+
}, created_days_ago=950)
|
| 64 |
+
|
| 65 |
+
inactive_videos = _make_folder({
|
| 66 |
+
"tutorial_part1.mp4": _make_file("tutorial_part1.mp4", 800.0, 700, 650),
|
| 67 |
+
"tutorial_part2.mp4": _make_file("tutorial_part2.mp4", 750.0, 700, 650),
|
| 68 |
+
"raw_footage.avi": _make_file("raw_footage.avi", 1200.0, 720, 680),
|
| 69 |
+
}, created_days_ago=720)
|
| 70 |
+
|
| 71 |
+
# --- RECENT project files (must NOT be touched) ---
|
| 72 |
+
recent_projects = _make_folder({
|
| 73 |
+
"q4_strategy_2024.pptx": _make_file("q4_strategy_2024.pptx", 5.2, 20, 2),
|
| 74 |
+
"client_proposal_nov.docx": _make_file("client_proposal_nov.docx", 1.8, 15, 1),
|
| 75 |
+
"budget_forecast_2025.xlsx": _make_file("budget_forecast_2025.xlsx", 3.1, 10, 3),
|
| 76 |
+
"product_roadmap.pdf": _make_file("product_roadmap.pdf", 2.4, 5, 0),
|
| 77 |
+
}, created_days_ago=25)
|
| 78 |
+
|
| 79 |
+
recent_code = _make_folder({
|
| 80 |
+
"main.py": _make_file("main.py", 0.05, 7, 0),
|
| 81 |
+
"utils.py": _make_file("utils.py", 0.03, 7, 1),
|
| 82 |
+
"requirements.txt": _make_file("requirements.txt", 0.005, 7, 0),
|
| 83 |
+
"README.md": _make_file("README.md", 0.02, 7, 0),
|
| 84 |
+
"tests.py": _make_file("tests.py", 0.04, 7, 0),
|
| 85 |
+
}, created_days_ago=7)
|
| 86 |
+
|
| 87 |
+
# --- Medium-age files (borderline, agent must use judgment) ---
|
| 88 |
+
medium_files = {
|
| 89 |
+
"report_2022.pdf": _make_file("report_2022.pdf", 4.5, 700, 400),
|
| 90 |
+
"presentation_2021.pptx": _make_file("presentation_2021.pptx", 22.0, 1000, 800),
|
| 91 |
+
"archive_misc.zip": _make_file("archive_misc.zip", 55.0, 800, 600),
|
| 92 |
+
"notes_2020.txt": _make_file("notes_2020.txt", 0.1, 1200, 1100),
|
| 93 |
+
"photo_album_2022.zip": _make_file("photo_album_2022.zip", 130.0, 650, 500),
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
root = {
|
| 97 |
+
**old_files,
|
| 98 |
+
**medium_files,
|
| 99 |
+
"Design_Assets": inactive_design_assets,
|
| 100 |
+
"Tutorial_Videos": inactive_videos,
|
| 101 |
+
"Active_Projects": recent_projects,
|
| 102 |
+
"Current_Code": recent_code,
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
# Compute what needs to be freed
|
| 106 |
+
total_size = sum(
|
| 107 |
+
meta.get("size_mb", 0)
|
| 108 |
+
for meta in old_files.values()
|
| 109 |
+
if isinstance(meta, dict)
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
state = {
|
| 113 |
+
"root": root,
|
| 114 |
+
"_storage_goal_mb": STORAGE_LIMIT_MB,
|
| 115 |
+
"_recent_folders_to_protect": ["Active_Projects", "Current_Code"],
|
| 116 |
+
"_old_files_eligible": list(old_files.keys()),
|
| 117 |
+
"_inactive_folders_eligible_for_zip": ["Design_Assets", "Tutorial_Videos"],
|
| 118 |
+
}
|
| 119 |
+
return state
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
def get_hard_task_description() -> str:
|
| 123 |
+
return (
|
| 124 |
+
f"HARD TASK – The Storage Diet:\n"
|
| 125 |
+
f"You must free up at least {STORAGE_LIMIT_MB} MB of storage space.\n"
|
| 126 |
+
"Rules:\n"
|
| 127 |
+
"1. DELETE files older than 3 years that haven't been accessed recently.\n"
|
| 128 |
+
"2. ZIP large inactive folders (not accessed in 90+ days) to save space.\n"
|
| 129 |
+
"3. DO NOT touch or move any files accessed within the last 90 days.\n"
|
| 130 |
+
"4. DO NOT touch the 'Active_Projects' or 'Current_Code' folders.\n"
|
| 131 |
+
"5. Be strategic: inspect files before deleting if unsure.\n"
|
| 132 |
+
f"Goal: Free at least {STORAGE_LIMIT_MB} MB without harming recent or active files."
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
def get_hard_grading_criteria(initial_state: dict) -> dict:
|
| 137 |
+
"""Return grading criteria for the hard task."""
|
| 138 |
+
return {
|
| 139 |
+
"storage_goal_mb": initial_state.get("_storage_goal_mb", STORAGE_LIMIT_MB),
|
| 140 |
+
"old_files_eligible_for_deletion": initial_state.get("_old_files_eligible", []),
|
| 141 |
+
"folders_eligible_for_zipping": initial_state.get("_inactive_folders_eligible_for_zip", []),
|
| 142 |
+
"folders_to_protect": initial_state.get("_recent_folders_to_protect", []),
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
if __name__ == "__main__":
|
| 147 |
+
state = generate_hard_initial_state()
|
| 148 |
+
os.makedirs("data", exist_ok=True)
|
| 149 |
+
with open(INITIAL_STATE_FILE, "w") as f:
|
| 150 |
+
json.dump(state, f, indent=2)
|
| 151 |
+
print("Hard task state saved.")
|
| 152 |
+
eligible_size = sum(
|
| 153 |
+
state["root"][f].get("size_mb", 0)
|
| 154 |
+
for f in state["_old_files_eligible"]
|
| 155 |
+
)
|
| 156 |
+
print(f"Total size eligible for deletion: {eligible_size:.1f} MB")
|
| 157 |
+
print(f"Storage goal: {state['_storage_goal_mb']} MB")
|
tasks/medium_task.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# tasks/medium_task.py
|
| 2 |
+
|
| 3 |
+
import random
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
from datetime import datetime, timedelta
|
| 7 |
+
from config import INITIAL_STATE_FILE
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def _make_file(name: str, size_mb: float, days_ago: int) -> dict:
|
| 11 |
+
accessed = (datetime.now() - timedelta(days=days_ago)).isoformat()
|
| 12 |
+
created = (datetime.now() - timedelta(days=days_ago + 30)).isoformat()
|
| 13 |
+
ext = "." + name.rsplit(".", 1)[-1] if "." in name else ""
|
| 14 |
+
return {
|
| 15 |
+
"type": "file",
|
| 16 |
+
"extension": ext,
|
| 17 |
+
"size_mb": round(size_mb, 2),
|
| 18 |
+
"created_date": created,
|
| 19 |
+
"last_accessed": accessed,
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def generate_medium_initial_state(seed: int = 7) -> dict:
|
| 24 |
+
"""
|
| 25 |
+
Generate a file system with intentional duplicates.
|
| 26 |
+
|
| 27 |
+
Duplicates share the same base name and size but have trailing (1), (2) markers.
|
| 28 |
+
Medium Task Goal: Find and delete the duplicate copies, keeping only one original.
|
| 29 |
+
"""
|
| 30 |
+
random.seed(seed)
|
| 31 |
+
|
| 32 |
+
# Original files + their sizes (MB)
|
| 33 |
+
originals = [
|
| 34 |
+
("resume.pdf", 1.2),
|
| 35 |
+
("invoice_march.pdf", 0.8),
|
| 36 |
+
("vacation_beach.jpg", 3.5),
|
| 37 |
+
("project_report.docx", 2.1),
|
| 38 |
+
("music_mix.mp3", 8.4),
|
| 39 |
+
("data_export.csv", 0.5),
|
| 40 |
+
("family_photo.jpg", 4.2),
|
| 41 |
+
("budget_2023.xlsx", 1.7),
|
| 42 |
+
("presentation.pptx", 5.3),
|
| 43 |
+
("contract_signed.pdf", 0.9),
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
# Non-duplicate files (distractors)
|
| 47 |
+
unique_files = [
|
| 48 |
+
("notes.txt", 0.1, 20),
|
| 49 |
+
("app_backup.zip", 45.0, 90),
|
| 50 |
+
("source_code.py", 0.3, 5),
|
| 51 |
+
("readme.md", 0.05, 2),
|
| 52 |
+
("database_dump.sql", 12.0, 60),
|
| 53 |
+
("config.json", 0.02, 1),
|
| 54 |
+
("logo_design.svg", 0.4, 15),
|
| 55 |
+
("meeting_recording.mp4", 150.0, 30),
|
| 56 |
+
("financial_model.xlsx", 3.8, 45),
|
| 57 |
+
("legal_brief.pdf", 2.6, 10),
|
| 58 |
+
]
|
| 59 |
+
|
| 60 |
+
root = {}
|
| 61 |
+
duplicates_to_delete = []
|
| 62 |
+
|
| 63 |
+
for orig_name, size_mb in originals:
|
| 64 |
+
days = random.randint(30, 200)
|
| 65 |
+
root[orig_name] = _make_file(orig_name, size_mb, days)
|
| 66 |
+
|
| 67 |
+
# Create 1 or 2 duplicates
|
| 68 |
+
num_dupes = random.randint(1, 2)
|
| 69 |
+
ext = "." + orig_name.rsplit(".", 1)[-1]
|
| 70 |
+
base = orig_name.rsplit(".", 1)[0]
|
| 71 |
+
for i in range(1, num_dupes + 1):
|
| 72 |
+
dupe_name = f"{base}({i}){ext}"
|
| 73 |
+
# Same size ± tiny floating point noise (still rounds to same)
|
| 74 |
+
dupe_size = round(size_mb + random.uniform(-0.001, 0.001), 3)
|
| 75 |
+
root[dupe_name] = _make_file(dupe_name, dupe_size, days + random.randint(1, 10))
|
| 76 |
+
duplicates_to_delete.append(dupe_name)
|
| 77 |
+
|
| 78 |
+
for fname, size_mb, days in unique_files:
|
| 79 |
+
root[fname] = _make_file(fname, size_mb, days)
|
| 80 |
+
|
| 81 |
+
state = {"root": root, "_duplicates_to_delete": duplicates_to_delete}
|
| 82 |
+
return state
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def get_medium_task_description() -> str:
|
| 86 |
+
return (
|
| 87 |
+
"MEDIUM TASK – Deduplication:\n"
|
| 88 |
+
"The drive contains many duplicate files. Files with the same base name and "
|
| 89 |
+
"approximately the same size (e.g., 'resume.pdf' and 'resume(1).pdf') are duplicates.\n"
|
| 90 |
+
"Your job is to:\n"
|
| 91 |
+
"1. Scan the drive and identify all duplicate files.\n"
|
| 92 |
+
"2. Keep ONE copy of each file (the original without the number suffix).\n"
|
| 93 |
+
"3. Delete all duplicate copies (those with (1), (2), etc. in the name).\n"
|
| 94 |
+
"4. Do NOT delete or move any non-duplicate files.\n"
|
| 95 |
+
"Complete the task when all duplicates are removed."
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def get_medium_grading_criteria(initial_state: dict) -> dict:
|
| 100 |
+
"""Return grading criteria for the medium task."""
|
| 101 |
+
return {
|
| 102 |
+
"files_to_delete": initial_state.get("_duplicates_to_delete", []),
|
| 103 |
+
"files_to_preserve": [
|
| 104 |
+
name for name in initial_state["root"]
|
| 105 |
+
if name not in initial_state.get("_duplicates_to_delete", [])
|
| 106 |
+
],
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
if __name__ == "__main__":
|
| 111 |
+
state = generate_medium_initial_state()
|
| 112 |
+
os.makedirs("data", exist_ok=True)
|
| 113 |
+
with open(INITIAL_STATE_FILE, "w") as f:
|
| 114 |
+
json.dump(state, f, indent=2)
|
| 115 |
+
print(f"Generated {len(state['root'])} files ({len(state['_duplicates_to_delete'])} duplicates).")
|
| 116 |
+
print(f"Saved to {INITIAL_STATE_FILE}")
|
utils/helpers.py
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# utils/helpers.py
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
from datetime import datetime, timedelta
|
| 6 |
+
from typing import Any
|
| 7 |
+
from config import OLD_FILE_THRESHOLD_YEARS, RECENT_DAYS_THRESHOLD, FILE_CATEGORIES
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def load_json(filepath: str) -> dict:
|
| 11 |
+
"""Load and return a JSON file as a dict."""
|
| 12 |
+
with open(filepath, "r") as f:
|
| 13 |
+
return json.load(f)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def save_json(data: dict, filepath: str):
|
| 17 |
+
"""Save a dict to a JSON file (creates parent dirs if needed)."""
|
| 18 |
+
os.makedirs(os.path.dirname(filepath), exist_ok=True)
|
| 19 |
+
with open(filepath, "w") as f:
|
| 20 |
+
json.dump(data, f, indent=2)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def parse_date(date_str: str) -> datetime:
|
| 24 |
+
"""Parse an ISO-format date string to a datetime object."""
|
| 25 |
+
try:
|
| 26 |
+
return datetime.fromisoformat(date_str)
|
| 27 |
+
except (ValueError, TypeError):
|
| 28 |
+
return datetime.min
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def is_old_file(last_accessed_str: str, threshold_years: int = OLD_FILE_THRESHOLD_YEARS) -> bool:
|
| 32 |
+
"""Return True if the file hasn't been accessed within the threshold."""
|
| 33 |
+
last_accessed = parse_date(last_accessed_str)
|
| 34 |
+
cutoff = datetime.now() - timedelta(days=365 * threshold_years)
|
| 35 |
+
return last_accessed < cutoff
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def is_recent_file(last_accessed_str: str, threshold_days: int = RECENT_DAYS_THRESHOLD) -> bool:
|
| 39 |
+
"""Return True if the file was accessed within the recent threshold."""
|
| 40 |
+
last_accessed = parse_date(last_accessed_str)
|
| 41 |
+
cutoff = datetime.now() - timedelta(days=threshold_days)
|
| 42 |
+
return last_accessed >= cutoff
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def get_category_for_extension(extension: str) -> str:
|
| 46 |
+
"""Return the folder category for a given file extension."""
|
| 47 |
+
ext = extension.lower()
|
| 48 |
+
for category, extensions in FILE_CATEGORIES.items():
|
| 49 |
+
if ext in extensions:
|
| 50 |
+
return category
|
| 51 |
+
return "Misc"
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def format_size(size_mb: float) -> str:
|
| 55 |
+
"""Human-readable file size."""
|
| 56 |
+
if size_mb >= 1024:
|
| 57 |
+
return f"{size_mb / 1024:.2f} GB"
|
| 58 |
+
if size_mb >= 1:
|
| 59 |
+
return f"{size_mb:.2f} MB"
|
| 60 |
+
return f"{size_mb * 1024:.2f} KB"
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def flatten_file_list(root: dict, path: str = "") -> list:
|
| 64 |
+
"""
|
| 65 |
+
Recursively flatten the file system tree into a list of file dicts.
|
| 66 |
+
Each entry: {name, path, type, extension, size_mb, created_date, last_accessed}
|
| 67 |
+
"""
|
| 68 |
+
results = []
|
| 69 |
+
for name, meta in root.items():
|
| 70 |
+
current_path = f"{path}/{name}" if path else name
|
| 71 |
+
if not isinstance(meta, dict):
|
| 72 |
+
continue
|
| 73 |
+
if meta.get("type") == "file":
|
| 74 |
+
results.append({
|
| 75 |
+
"name": name,
|
| 76 |
+
"path": current_path,
|
| 77 |
+
**meta,
|
| 78 |
+
})
|
| 79 |
+
elif meta.get("type") == "folder":
|
| 80 |
+
results.extend(flatten_file_list(meta.get("contents", {}), current_path))
|
| 81 |
+
return results
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def find_duplicates(file_list: list) -> list:
|
| 85 |
+
"""
|
| 86 |
+
Find duplicate files: same base name (ignoring trailing numbers like (1), (2))
|
| 87 |
+
and same size.
|
| 88 |
+
Returns a list of lists, where each sub-list is a group of duplicate paths.
|
| 89 |
+
"""
|
| 90 |
+
import re
|
| 91 |
+
groups: dict[tuple, list] = {}
|
| 92 |
+
for f in file_list:
|
| 93 |
+
if f.get("type") != "file":
|
| 94 |
+
continue
|
| 95 |
+
name = f["name"]
|
| 96 |
+
size = f.get("size_mb", 0)
|
| 97 |
+
# Strip trailing duplicate markers: "file(1).pdf" → "file.pdf"
|
| 98 |
+
base = re.sub(r"\(\d+\)(\.[^.]+)$", r"\1", name)
|
| 99 |
+
key = (base.lower(), round(size, 3))
|
| 100 |
+
groups.setdefault(key, []).append(f)
|
| 101 |
+
|
| 102 |
+
return [group for group in groups.values() if len(group) > 1]
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def total_size_mb(file_list: list) -> float:
|
| 106 |
+
"""Return the total size in MB of a list of file dicts."""
|
| 107 |
+
return sum(f.get("size_mb", 0) for f in file_list)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def summarize_filesystem(root: dict) -> dict:
|
| 111 |
+
"""Return a high-level summary of the filesystem."""
|
| 112 |
+
all_files = flatten_file_list(root)
|
| 113 |
+
total_files = len(all_files)
|
| 114 |
+
total_mb = total_size_mb(all_files)
|
| 115 |
+
by_category: dict[str, float] = {}
|
| 116 |
+
for f in all_files:
|
| 117 |
+
cat = get_category_for_extension(f.get("extension", ""))
|
| 118 |
+
by_category[cat] = by_category.get(cat, 0) + f.get("size_mb", 0)
|
| 119 |
+
|
| 120 |
+
return {
|
| 121 |
+
"total_files": total_files,
|
| 122 |
+
"total_size_mb": round(total_mb, 2),
|
| 123 |
+
"total_size_human": format_size(total_mb),
|
| 124 |
+
"by_category": {k: round(v, 2) for k, v in by_category.items()},
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def build_prompt_context(observation: dict) -> str:
|
| 129 |
+
"""Convert the observation dict into a compact string for LLM prompts."""
|
| 130 |
+
root = observation.get("root", {})
|
| 131 |
+
summary = summarize_filesystem(root)
|
| 132 |
+
all_files = flatten_file_list(root)
|
| 133 |
+
|
| 134 |
+
lines = [
|
| 135 |
+
f"Total files: {summary['total_files']}",
|
| 136 |
+
f"Total size: {summary['total_size_human']}",
|
| 137 |
+
"Files:",
|
| 138 |
+
]
|
| 139 |
+
for f in all_files:
|
| 140 |
+
accessed = f.get("last_accessed", "unknown")
|
| 141 |
+
size = format_size(f.get("size_mb", 0))
|
| 142 |
+
lines.append(f" - {f['path']} | {size} | last accessed: {accessed}")
|
| 143 |
+
|
| 144 |
+
return "\n".join(lines)
|
| 145 |
+
|
| 146 |
+
# ------------------------------------------------------------------
|
| 147 |
+
# OpenEnv helpers
|
| 148 |
+
# ------------------------------------------------------------------
|
| 149 |
+
|
| 150 |
+
from config import INITIAL_STATE_FILE
|
| 151 |
+
import os
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
def load_initial_state():
|
| 155 |
+
"""
|
| 156 |
+
Load the initial filesystem state from JSON.
|
| 157 |
+
"""
|
| 158 |
+
|
| 159 |
+
if not os.path.exists(INITIAL_STATE_FILE):
|
| 160 |
+
raise FileNotFoundError(
|
| 161 |
+
f"Initial state file not found: {INITIAL_STATE_FILE}"
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
with open(INITIAL_STATE_FILE, "r") as f:
|
| 165 |
+
return json.load(f)
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
def load_task_criteria(task_name: str):
|
| 169 |
+
"""
|
| 170 |
+
Load task criteria file based on task name.
|
| 171 |
+
Example:
|
| 172 |
+
data/criteria_easy.json
|
| 173 |
+
data/criteria_medium.json
|
| 174 |
+
data/criteria_hard.json
|
| 175 |
+
"""
|
| 176 |
+
|
| 177 |
+
filename = f"criteria_{task_name}.json"
|
| 178 |
+
|
| 179 |
+
path = os.path.join(
|
| 180 |
+
"data",
|
| 181 |
+
filename
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
if not os.path.exists(path):
|
| 185 |
+
|
| 186 |
+
# fallback empty criteria
|
| 187 |
+
return {}
|
| 188 |
+
|
| 189 |
+
with open(path, "r") as f:
|
| 190 |
+
return json.load(f)
|