Spaces:
Configuration error
Configuration error
Upload folder using huggingface_hub
Browse files- client.py +24 -53
- inference.py +80 -73
- models.py +45 -16
- requirements.txt +33 -0
- server/lint_coding_agent_environment.py +70 -71
client.py
CHANGED
|
@@ -4,57 +4,27 @@
|
|
| 4 |
# This source code is licensed under the BSD-style license found in the
|
| 5 |
# LICENSE file in the root directory of this source tree.
|
| 6 |
|
| 7 |
-
"""Lint Coding Agent Environment Client."""
|
| 8 |
-
|
| 9 |
-
from typing import Dict
|
| 10 |
-
|
| 11 |
-
from openenv.core import EnvClient
|
| 12 |
-
from openenv.core.client_types import StepResult
|
| 13 |
-
from openenv.core.env_server.types import State
|
| 14 |
-
|
| 15 |
-
from .models import LintCodingAgentAction, LintCodingAgentObservation
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
class LintCodingAgentEnv(
|
| 19 |
-
EnvClient[LintCodingAgentAction, LintCodingAgentObservation, State]
|
| 20 |
-
):
|
| 21 |
-
"""
|
| 22 |
-
Client for the Lint Coding Agent Environment.
|
| 23 |
-
|
| 24 |
-
This client maintains a persistent WebSocket connection to the environment server,
|
| 25 |
-
enabling efficient multi-step interactions with lower latency.
|
| 26 |
-
Each client instance has its own dedicated environment session on the server.
|
| 27 |
-
|
| 28 |
-
Example:
|
| 29 |
-
>>> # Connect to a running server
|
| 30 |
-
>>> with LintCodingAgentEnv(base_url="http://localhost:8000") as client:
|
| 31 |
-
... result = client.reset()
|
| 32 |
-
... print(result.observation.echoed_message)
|
| 33 |
-
...
|
| 34 |
-
... result = client.step(LintCodingAgentAction(message="Hello!"))
|
| 35 |
-
... print(result.observation.echoed_message)
|
| 36 |
-
|
| 37 |
-
Example with Docker:
|
| 38 |
-
>>> # Automatically start container and connect
|
| 39 |
-
>>> client = LintCodingAgentEnv.from_docker_image("lint_coding_agent-env:latest")
|
| 40 |
-
>>> try:
|
| 41 |
-
... result = client.reset()
|
| 42 |
-
... result = client.step(LintCodingAgentAction(message="Test"))
|
| 43 |
-
... finally:
|
| 44 |
-
... client.close()
|
| 45 |
-
"""
|
| 46 |
-
|
| 47 |
"""Lint Coding Agent Environment Client Implementation."""
|
| 48 |
|
|
|
|
|
|
|
| 49 |
from typing import Dict, Any
|
|
|
|
| 50 |
from openenv.core import EnvClient
|
| 51 |
from openenv.core.client_types import StepResult
|
| 52 |
from openenv.core.env_server.types import State
|
| 53 |
|
|
|
|
|
|
|
| 54 |
try:
|
| 55 |
-
from .models import LintCodingAgentAction, LintCodingAgentObservation
|
| 56 |
-
except ImportError:
|
| 57 |
from models import LintCodingAgentAction, LintCodingAgentObservation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
|
| 60 |
class LintCodingAgentEnv(
|
|
@@ -63,16 +33,17 @@ class LintCodingAgentEnv(
|
|
| 63 |
"""
|
| 64 |
Lead Architect Client for the Lint Coding Agent Environment.
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
|
|
|
| 68 |
"""
|
| 69 |
|
| 70 |
def _step_payload(self, action: LintCodingAgentAction) -> Dict[str, Any]:
|
| 71 |
"""
|
| 72 |
-
Serializes the Action for the OpenEnv
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
"""
|
| 77 |
return {
|
| 78 |
"code_solution": action.code_solution,
|
|
@@ -86,15 +57,15 @@ class LintCodingAgentEnv(
|
|
| 86 |
"""
|
| 87 |
obs_data = payload.get("observation", {})
|
| 88 |
|
| 89 |
-
# Mapping
|
| 90 |
observation = LintCodingAgentObservation(
|
| 91 |
level=obs_data.get("level", 1),
|
| 92 |
language=obs_data.get("language", "Python"),
|
| 93 |
-
problem_statement=obs_data.get("problem_statement", ""),
|
| 94 |
-
code_context=obs_data.get("code_context", "{}"), #
|
| 95 |
last_test_results=obs_data.get("last_test_results", ""),
|
| 96 |
-
reward=
|
| 97 |
-
done=
|
| 98 |
metadata=obs_data.get("metadata", {}),
|
| 99 |
)
|
| 100 |
|
|
@@ -106,7 +77,7 @@ class LintCodingAgentEnv(
|
|
| 106 |
|
| 107 |
def _parse_state(self, payload: Dict[str, Any]) -> State:
|
| 108 |
"""
|
| 109 |
-
Maintains
|
| 110 |
"""
|
| 111 |
return State(
|
| 112 |
episode_id=payload.get("episode_id"),
|
|
|
|
| 4 |
# This source code is licensed under the BSD-style license found in the
|
| 5 |
# LICENSE file in the root directory of this source tree.
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
"""Lint Coding Agent Environment Client Implementation."""
|
| 8 |
|
| 9 |
+
import os
|
| 10 |
+
import sys
|
| 11 |
from typing import Dict, Any
|
| 12 |
+
|
| 13 |
from openenv.core import EnvClient
|
| 14 |
from openenv.core.client_types import StepResult
|
| 15 |
from openenv.core.env_server.types import State
|
| 16 |
|
| 17 |
+
# 🚀 CRITICAL FIX: Robust Absolute/Relative Import logic
|
| 18 |
+
# This ensures that whether running via 'uv run' or as a module, it finds the models.
|
| 19 |
try:
|
|
|
|
|
|
|
| 20 |
from models import LintCodingAgentAction, LintCodingAgentObservation
|
| 21 |
+
except ImportError:
|
| 22 |
+
try:
|
| 23 |
+
from .models import LintCodingAgentAction, LintCodingAgentObservation
|
| 24 |
+
except ImportError:
|
| 25 |
+
# Final fallback for unusual container environments
|
| 26 |
+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
| 27 |
+
from models import LintCodingAgentAction, LintCodingAgentObservation
|
| 28 |
|
| 29 |
|
| 30 |
class LintCodingAgentEnv(
|
|
|
|
| 33 |
"""
|
| 34 |
Lead Architect Client for the Lint Coding Agent Environment.
|
| 35 |
|
| 36 |
+
Maintains the persistent WebSocket/HTTP handshake with the Hugging Face Space.
|
| 37 |
+
Handles the 15-level VFS state transitions and serializes specialized
|
| 38 |
+
Pydantic models for code-based architectural fixes.
|
| 39 |
"""
|
| 40 |
|
| 41 |
def _step_payload(self, action: LintCodingAgentAction) -> Dict[str, Any]:
|
| 42 |
"""
|
| 43 |
+
Serializes the Action for the OpenEnv transport layer.
|
| 44 |
|
| 45 |
+
The 'explanation' field is utilized by the server to monitor
|
| 46 |
+
agency taxation and sub-agent spawning logic.
|
| 47 |
"""
|
| 48 |
return {
|
| 49 |
"code_solution": action.code_solution,
|
|
|
|
| 57 |
"""
|
| 58 |
obs_data = payload.get("observation", {})
|
| 59 |
|
| 60 |
+
# 🛡️ Defensive Mapping: Ensure no field is None to prevent Pydantic crashes
|
| 61 |
observation = LintCodingAgentObservation(
|
| 62 |
level=obs_data.get("level", 1),
|
| 63 |
language=obs_data.get("language", "Python"),
|
| 64 |
+
problem_statement=obs_data.get("problem_statement", "Task initiated."),
|
| 65 |
+
code_context=obs_data.get("code_context", "{}"), # VFS JSON string
|
| 66 |
last_test_results=obs_data.get("last_test_results", ""),
|
| 67 |
+
reward=payload.get("reward", 0.0), # SDK priority on top-level payload
|
| 68 |
+
done=payload.get("done", False),
|
| 69 |
metadata=obs_data.get("metadata", {}),
|
| 70 |
)
|
| 71 |
|
|
|
|
| 77 |
|
| 78 |
def _parse_state(self, payload: Dict[str, Any]) -> State:
|
| 79 |
"""
|
| 80 |
+
Maintains session integrity for persistent agent tracking across 15 levels.
|
| 81 |
"""
|
| 82 |
return State(
|
| 83 |
episode_id=payload.get("episode_id"),
|
inference.py
CHANGED
|
@@ -44,115 +44,122 @@ STDOUT FORMAT
|
|
| 44 |
|
| 45 |
import asyncio
|
| 46 |
import os
|
| 47 |
-
import
|
| 48 |
import json
|
|
|
|
| 49 |
from typing import List, Optional
|
| 50 |
from openai import OpenAI
|
| 51 |
from dotenv import load_dotenv
|
| 52 |
|
| 53 |
-
# 🚀 THE
|
| 54 |
-
|
| 55 |
-
from
|
| 56 |
-
|
| 57 |
-
load_dotenv()
|
| 58 |
|
| 59 |
try:
|
|
|
|
| 60 |
from models import LintCodingAgentAction
|
| 61 |
-
except ImportError:
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
# --- CONFIGURATION ---
|
| 65 |
API_KEY = os.environ.get("API_KEY") or os.environ.get("HF_TOKEN")
|
| 66 |
API_BASE_URL = os.environ.get("API_BASE_URL") or "https://router.huggingface.co/hf-inference/v1"
|
| 67 |
MODEL_NAME = os.environ.get("MODEL_NAME") or "Qwen/Qwen2.5-72B-Instruct"
|
| 68 |
-
ADDRESS = "https://anonajay-lint-coding-agent.hf.space"
|
| 69 |
|
| 70 |
TASK_NAME = "multi-lang-lint-v1"
|
| 71 |
BENCHMARK = "lint-coding-v1"
|
| 72 |
-
MAX_STEPS =
|
| 73 |
|
| 74 |
-
SYSTEM_PROMPT =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
def log_start(task: str, env: str, model: str)
|
| 77 |
print(f"[START] task={task} env={env} model={model}", flush=True)
|
| 78 |
|
| 79 |
-
def log_step(step: int, action: str, reward: float, done: bool, error: Optional[str])
|
|
|
|
| 80 |
action_clean = action.replace("\n", " ").strip()[:50]
|
| 81 |
print(f"[STEP] step={step} action={action_clean} reward={reward:.2f} done={str(done).lower()} error={error or 'null'}", flush=True)
|
| 82 |
|
| 83 |
-
def log_end(success: bool, steps: int, score: float, rewards: List[float])
|
| 84 |
rewards_str = ",".join(f"{r:.2f}" for r in rewards)
|
| 85 |
print(f"[END] success={str(success).lower()} steps={steps} score={score:.2f} rewards={rewards_str}", flush=True)
|
| 86 |
|
| 87 |
-
def main()
|
| 88 |
-
client
|
|
|
|
| 89 |
|
| 90 |
-
|
| 91 |
-
# We try positional first, then 'address', then 'base_url'
|
| 92 |
-
env = None
|
| 93 |
-
try:
|
| 94 |
-
env = SyncEnvClient(ADDRESS)
|
| 95 |
-
except TypeError:
|
| 96 |
-
try:
|
| 97 |
-
env = SyncEnvClient(address=ADDRESS)
|
| 98 |
-
except TypeError:
|
| 99 |
-
env = SyncEnvClient(base_url=ADDRESS)
|
| 100 |
-
|
| 101 |
-
rewards: List[float] = []
|
| 102 |
-
steps_taken = 0
|
| 103 |
-
success = False
|
| 104 |
|
| 105 |
log_start(task=TASK_NAME, env=BENCHMARK, model=MODEL_NAME)
|
| 106 |
|
| 107 |
try:
|
| 108 |
-
#
|
| 109 |
-
#
|
| 110 |
-
|
| 111 |
-
with env.sync() as synced_env:
|
| 112 |
-
execute_loop(synced_env, client, rewards)
|
| 113 |
-
else:
|
| 114 |
-
execute_loop(env, client, rewards)
|
| 115 |
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
except Exception as e:
|
| 119 |
-
log_step(1, "
|
| 120 |
|
| 121 |
finally:
|
| 122 |
-
|
| 123 |
-
score = sum(rewards) / steps_taken if steps_taken > 0 else 0.0
|
| 124 |
log_end(success, steps_taken, score, rewards)
|
| 125 |
|
| 126 |
-
def execute_loop(env, client, rewards):
|
| 127 |
-
"""The actual logic loop shared between sync/synced-context modes"""
|
| 128 |
-
result = env.reset()
|
| 129 |
-
for step in range(1, MAX_STEPS + 1):
|
| 130 |
-
obs = result.observation
|
| 131 |
-
vfs_display = obs.code_context # Already JSON stringified in our server
|
| 132 |
-
|
| 133 |
-
user_prompt = f"VFS:\n{vfs_display}\n\nTask: {obs.problem_statement}"
|
| 134 |
-
|
| 135 |
-
completion = client.chat.completions.create(
|
| 136 |
-
model=MODEL_NAME,
|
| 137 |
-
messages=[
|
| 138 |
-
{"role": "system", "content": SYSTEM_PROMPT},
|
| 139 |
-
{"role": "user", "content": user_prompt},
|
| 140 |
-
],
|
| 141 |
-
temperature=0.2,
|
| 142 |
-
)
|
| 143 |
-
|
| 144 |
-
action_text = completion.choices[0].message.content.strip()
|
| 145 |
-
|
| 146 |
-
result = env.step(LintCodingAgentAction(
|
| 147 |
-
code_solution=action_text,
|
| 148 |
-
explanation=f"Architect fix"
|
| 149 |
-
))
|
| 150 |
-
|
| 151 |
-
rewards.append(result.reward or 0.0)
|
| 152 |
-
log_step(step, action_text, result.reward, result.done, None)
|
| 153 |
-
|
| 154 |
-
if result.done:
|
| 155 |
-
break
|
| 156 |
-
|
| 157 |
if __name__ == "__main__":
|
| 158 |
-
main()
|
|
|
|
| 44 |
|
| 45 |
import asyncio
|
| 46 |
import os
|
| 47 |
+
import sys
|
| 48 |
import json
|
| 49 |
+
import textwrap
|
| 50 |
from typing import List, Optional
|
| 51 |
from openai import OpenAI
|
| 52 |
from dotenv import load_dotenv
|
| 53 |
|
| 54 |
+
# 🚀 THE BREAKPOINT FIX
|
| 55 |
+
# Force the current directory into the front of the path.
|
| 56 |
+
# This ensures 'from client import ...' hits YOUR file, not a library.
|
| 57 |
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
| 58 |
|
| 59 |
try:
|
| 60 |
+
from client import LintCodingAgentEnv
|
| 61 |
from models import LintCodingAgentAction
|
| 62 |
+
except ImportError as e:
|
| 63 |
+
print(f"CRITICAL: Local architecture files missing: {e}")
|
| 64 |
+
sys.exit(1)
|
| 65 |
+
|
| 66 |
+
load_dotenv()
|
| 67 |
|
| 68 |
# --- CONFIGURATION ---
|
| 69 |
API_KEY = os.environ.get("API_KEY") or os.environ.get("HF_TOKEN")
|
| 70 |
API_BASE_URL = os.environ.get("API_BASE_URL") or "https://router.huggingface.co/hf-inference/v1"
|
| 71 |
MODEL_NAME = os.environ.get("MODEL_NAME") or "Qwen/Qwen2.5-72B-Instruct"
|
| 72 |
+
ADDRESS = os.environ.get("ADDRESS") or "https://anonajay-lint-coding-agent.hf.space"
|
| 73 |
|
| 74 |
TASK_NAME = "multi-lang-lint-v1"
|
| 75 |
BENCHMARK = "lint-coding-v1"
|
| 76 |
+
MAX_STEPS = 15
|
| 77 |
|
| 78 |
+
SYSTEM_PROMPT = textwrap.dedent(
|
| 79 |
+
"""
|
| 80 |
+
You are a Lead Architect. You will receive a VFS (Virtual File System) JSON.
|
| 81 |
+
Identify the bug in the code provided in the context.
|
| 82 |
+
Respond ONLY with the corrected code. No explanations. No markdown blocks.
|
| 83 |
+
"""
|
| 84 |
+
).strip()
|
| 85 |
|
| 86 |
+
def log_start(task: str, env: str, model: str):
|
| 87 |
print(f"[START] task={task} env={env} model={model}", flush=True)
|
| 88 |
|
| 89 |
+
def log_step(step: int, action: str, reward: float, done: bool, error: Optional[str]):
|
| 90 |
+
# Cleanup for clean STDOUT logging required by Scaler
|
| 91 |
action_clean = action.replace("\n", " ").strip()[:50]
|
| 92 |
print(f"[STEP] step={step} action={action_clean} reward={reward:.2f} done={str(done).lower()} error={error or 'null'}", flush=True)
|
| 93 |
|
| 94 |
+
def log_end(success: bool, steps: int, score: float, rewards: List[float]):
|
| 95 |
rewards_str = ",".join(f"{r:.2f}" for r in rewards)
|
| 96 |
print(f"[END] success={str(success).lower()} steps={steps} score={score:.2f} rewards={rewards_str}", flush=True)
|
| 97 |
|
| 98 |
+
async def main():
|
| 99 |
+
# OpenAI client handles the handshake with the Qwen/Scaler proxy
|
| 100 |
+
llm_client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
|
| 101 |
|
| 102 |
+
rewards, steps_taken, success = [], 0, False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
log_start(task=TASK_NAME, env=BENCHMARK, model=MODEL_NAME)
|
| 105 |
|
| 106 |
try:
|
| 107 |
+
# 🏁 THE ASYNC HANDSHAKE
|
| 108 |
+
# Using the local wrapper ensures we can handle the 15-level VFS state
|
| 109 |
+
async with LintCodingAgentEnv(base_url=ADDRESS) as env:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
+
# Reset triggers the server to load Level 1 from templates
|
| 112 |
+
result = await env.reset()
|
| 113 |
+
|
| 114 |
+
for step in range(1, MAX_STEPS + 1):
|
| 115 |
+
obs = result.observation
|
| 116 |
+
|
| 117 |
+
# Context parsing for the 15 levels
|
| 118 |
+
vfs_display = obs.code_context
|
| 119 |
+
|
| 120 |
+
user_prompt = (
|
| 121 |
+
f"LEVEL: {obs.level}\n"
|
| 122 |
+
f"LANGUAGE: {obs.language}\n"
|
| 123 |
+
f"VFS STATE:\n{vfs_display}\n\n"
|
| 124 |
+
f"OBJECTIVE: {obs.problem_statement}"
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
# LLM Inference with Architect precision
|
| 128 |
+
completion = llm_client.chat.completions.create(
|
| 129 |
+
model=MODEL_NAME,
|
| 130 |
+
messages=[
|
| 131 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 132 |
+
{"role": "user", "content": user_prompt}
|
| 133 |
+
],
|
| 134 |
+
temperature=0.1
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
action_text = (completion.choices[0].message.content or "").strip()
|
| 138 |
+
|
| 139 |
+
# 🚀 STEP TRANSITION
|
| 140 |
+
# The await here is critical because LintCodingAgentEnv is Async
|
| 141 |
+
result = await env.step(LintCodingAgentAction(
|
| 142 |
+
code_solution=action_text,
|
| 143 |
+
explanation=f"Architect Fix Level {obs.level}"
|
| 144 |
+
))
|
| 145 |
+
|
| 146 |
+
reward = result.reward or 0.0
|
| 147 |
+
rewards.append(reward)
|
| 148 |
+
steps_taken = step
|
| 149 |
+
|
| 150 |
+
log_step(step, action_text, reward, result.done, None)
|
| 151 |
+
|
| 152 |
+
if result.done:
|
| 153 |
+
# Success logic: Average reward threshold for the sprint
|
| 154 |
+
success = (sum(rewards) / len(rewards)) >= 0.1
|
| 155 |
+
break
|
| 156 |
|
| 157 |
except Exception as e:
|
| 158 |
+
log_step(steps_taken + 1, "phase2_exec_error", 0.0, True, str(e))
|
| 159 |
|
| 160 |
finally:
|
| 161 |
+
score = sum(rewards) / len(rewards) if rewards else 0.0
|
|
|
|
| 162 |
log_end(success, steps_taken, score, rewards)
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
if __name__ == "__main__":
|
| 165 |
+
asyncio.run(main())
|
models.py
CHANGED
|
@@ -17,32 +17,61 @@ from typing import Dict, Any, Optional
|
|
| 17 |
class LintCodingAgentAction(Action):
|
| 18 |
"""
|
| 19 |
Action schema for the Lead Architect.
|
|
|
|
| 20 |
The agent provides the fix and an explanation that tracks orchestration intent.
|
| 21 |
-
Optional fields prevent 422 Validation Errors during
|
|
|
|
| 22 |
"""
|
| 23 |
code_solution: Optional[str] = Field(
|
| 24 |
default="",
|
| 25 |
-
description="The full code or specific
|
| 26 |
)
|
| 27 |
explanation: Optional[str] = Field(
|
| 28 |
-
default="Direct intervention",
|
| 29 |
-
description="Reasoning for the fix;
|
| 30 |
)
|
| 31 |
|
| 32 |
class LintCodingAgentObservation(Observation):
|
| 33 |
"""
|
| 34 |
-
Observation schema for the VFS Search Space.
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
"""
|
| 37 |
-
level: int = Field(
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
metadata: Dict[str, Any] = Field(
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
class LintCodingAgentAction(Action):
|
| 18 |
"""
|
| 19 |
Action schema for the Lead Architect.
|
| 20 |
+
|
| 21 |
The agent provides the fix and an explanation that tracks orchestration intent.
|
| 22 |
+
Optional fields with defaults prevent 422 Validation Errors during
|
| 23 |
+
high-latency inference or manual UI testing.
|
| 24 |
"""
|
| 25 |
code_solution: Optional[str] = Field(
|
| 26 |
default="",
|
| 27 |
+
description="The full code fix or specific patch for the repository."
|
| 28 |
)
|
| 29 |
explanation: Optional[str] = Field(
|
| 30 |
+
default="Direct architectural intervention.",
|
| 31 |
+
description="Reasoning for the fix; utilized for tracking sub-agent logic or taxation."
|
| 32 |
)
|
| 33 |
|
| 34 |
class LintCodingAgentObservation(Observation):
|
| 35 |
"""
|
| 36 |
+
Observation schema for the VFS (Virtual File System) Search Space.
|
| 37 |
+
|
| 38 |
+
Provides the multi-file repository state, environment feedback, and
|
| 39 |
+
curriculum progression.
|
| 40 |
"""
|
| 41 |
+
level: int = Field(
|
| 42 |
+
default=1,
|
| 43 |
+
description="Current difficulty level in the 15-level roadmap."
|
| 44 |
+
)
|
| 45 |
+
language: str = Field(
|
| 46 |
+
default="Python",
|
| 47 |
+
description="The primary programming language of the current task."
|
| 48 |
+
)
|
| 49 |
+
problem_statement: str = Field(
|
| 50 |
+
default="",
|
| 51 |
+
description="The technical objective, bug description, or mission prompt."
|
| 52 |
+
)
|
| 53 |
+
code_context: str = Field(
|
| 54 |
+
default="{}",
|
| 55 |
+
description="JSON-stringified Virtual File System (VFS) map containing repo files."
|
| 56 |
+
)
|
| 57 |
+
last_test_results: Optional[str] = Field(
|
| 58 |
+
default=None,
|
| 59 |
+
description="Standard output from linters, compilers, or test suites."
|
| 60 |
+
)
|
| 61 |
|
| 62 |
+
# --- MANDATORY FIELDS FOR OPENENV PROTOCOL ---
|
| 63 |
+
# These must be explicitly defined to allow the SDK to handle state transitions.
|
| 64 |
+
reward: float = Field(
|
| 65 |
+
default=0.0,
|
| 66 |
+
description="The scalar reward signal from the previous action transition."
|
| 67 |
+
)
|
| 68 |
+
done: bool = Field(
|
| 69 |
+
default=False,
|
| 70 |
+
description="Boolean flag indicating if the episode/curriculum has concluded."
|
| 71 |
+
)
|
| 72 |
|
| 73 |
+
# --- EXTENSIBILITY ---
|
| 74 |
+
metadata: Dict[str, Any] = Field(
|
| 75 |
+
default_factory=dict,
|
| 76 |
+
description="Extended context for tool discovery (MCP), file structure, or sub-agent logs."
|
| 77 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# --- CORE INFRASTRUCTURE ---
|
| 2 |
+
# High-performance package management
|
| 3 |
+
uv>=0.1.0
|
| 4 |
+
|
| 5 |
+
# --- OPENENV & AGENTIC STACK ---
|
| 6 |
+
# The foundation for the environment server and client
|
| 7 |
+
openenv-core>=0.2.0
|
| 8 |
+
# Pydantic is critical for the models.py validation
|
| 9 |
+
pydantic>=2.0.0
|
| 10 |
+
# Async support for the client.py architecture
|
| 11 |
+
asyncio>=3.4.3
|
| 12 |
+
# Environment variable management for your .env
|
| 13 |
+
python-dotenv>=1.0.0
|
| 14 |
+
|
| 15 |
+
# --- LLM & INFERENCE ---
|
| 16 |
+
# For the handshake with Qwen/Scaler Proxy
|
| 17 |
+
openai>=1.10.0
|
| 18 |
+
# Deep parsing for the VFS search space
|
| 19 |
+
typing-extensions>=4.8.0
|
| 20 |
+
|
| 21 |
+
# --- SERVER & NETWORKING ---
|
| 22 |
+
# Mandatory for app.py to run the FastAPI instance
|
| 23 |
+
uvicorn[standard]>=0.23.0
|
| 24 |
+
# HTTP transport for the Hugging Face Space
|
| 25 |
+
httpx>=0.25.0
|
| 26 |
+
# Multi-file processing and VFS logic
|
| 27 |
+
pathspec>=0.11.0
|
| 28 |
+
|
| 29 |
+
# --- UTILS & LINTING ---
|
| 30 |
+
# Used in your LintCodingAgentEnvironment for AST parsing
|
| 31 |
+
asttokens>=2.4.0
|
| 32 |
+
# Recommended for level verification if you add a 'test' step
|
| 33 |
+
pytest>=7.4.0
|
server/lint_coding_agent_environment.py
CHANGED
|
@@ -21,16 +21,17 @@ from typing import Tuple, Dict, Any, List
|
|
| 21 |
from openenv.core.env_server.interfaces import Environment
|
| 22 |
from openenv.core.env_server.types import State
|
| 23 |
|
|
|
|
| 24 |
try:
|
| 25 |
-
from ..models import LintCodingAgentAction, LintCodingAgentObservation
|
| 26 |
-
except ImportError:
|
| 27 |
from models import LintCodingAgentAction, LintCodingAgentObservation
|
|
|
|
|
|
|
| 28 |
|
| 29 |
class LintCodingAgentEnvironment(Environment):
|
| 30 |
"""
|
| 31 |
Advanced Repository Sandbox.
|
| 32 |
-
|
| 33 |
-
|
| 34 |
"""
|
| 35 |
SUPPORTS_CONCURRENT_SESSIONS: bool = True
|
| 36 |
|
|
@@ -40,35 +41,36 @@ class LintCodingAgentEnvironment(Environment):
|
|
| 40 |
self.failed_queue = []
|
| 41 |
self.vfs: Dict[str, str] = {}
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 45 |
-
json_path = os.path.join(current_dir, "QUESTIONS.json")
|
|
|
|
| 46 |
|
| 47 |
try:
|
| 48 |
with open(json_path, "r") as f:
|
| 49 |
self.curriculum = json.load(f)
|
| 50 |
-
|
|
|
|
| 51 |
except Exception as e:
|
| 52 |
print(f"CRITICAL DATA ERROR: {e}")
|
| 53 |
-
self.curriculum = {"1": {"lang": "Python", "task": "Init", "template_dir": "level_1", "ans": "print
|
| 54 |
self.all_levels = ["1"]
|
| 55 |
|
| 56 |
-
self.max_levels =
|
| 57 |
|
| 58 |
def reset(self) -> LintCodingAgentObservation:
|
| 59 |
-
"""
|
| 60 |
self._state = State(episode_id=str(uuid4()), step_count=0)
|
| 61 |
self.level = 1
|
| 62 |
self.failed_queue = []
|
| 63 |
-
return self._load_template_state("Environment Reset:
|
| 64 |
|
| 65 |
def _load_template_state(self, msg: str) -> LintCodingAgentObservation:
|
| 66 |
-
"""
|
| 67 |
-
task_data = self.curriculum.get(str(self.level))
|
|
|
|
| 68 |
template_dir_name = task_data.get("template_dir", f"level_{self.level}")
|
| 69 |
-
|
| 70 |
-
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 71 |
-
template_path = os.path.join(current_dir, "templates", template_dir_name)
|
| 72 |
|
| 73 |
self.vfs = {}
|
| 74 |
if os.path.exists(template_path):
|
|
@@ -80,103 +82,100 @@ class LintCodingAgentEnvironment(Environment):
|
|
| 80 |
with open(full_p, 'r', encoding='utf-8') as file:
|
| 81 |
self.vfs[rel_p] = file.read()
|
| 82 |
except Exception as e:
|
| 83 |
-
print(f"
|
| 84 |
else:
|
| 85 |
-
print(f"
|
| 86 |
|
| 87 |
return self._get_observation(msg, 0.0, False)
|
| 88 |
|
| 89 |
-
def _check_hijacking(self,
|
| 90 |
-
"""
|
| 91 |
-
|
| 92 |
-
- Ghosting: Deleting more than 70% of the repository.
|
| 93 |
-
- Wireheading: Comment-only solutions or early bypass returns.
|
| 94 |
-
"""
|
| 95 |
-
new_code = action.code_solution
|
| 96 |
|
| 97 |
-
# 1. Check for 'Ghosting' (
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
if len(new_code) < len(original_code) * 0.3 and len(original_code) > 20:
|
| 101 |
-
return True, "HIJACK DETECTED: Significant repository wiping.", -2.0
|
| 102 |
|
| 103 |
-
# 2. Check for
|
| 104 |
code_only = re.sub(r'#.*', '', new_code).strip()
|
| 105 |
-
if len(code_only) < 5
|
| 106 |
-
return True, "HIJACK
|
| 107 |
-
|
| 108 |
-
# 3. Check for early return hijacking
|
| 109 |
-
if new_code.strip().startswith("return") and not original_code.strip().startswith("return"):
|
| 110 |
-
return True, "HIJACK DETECTED: Early return injection.", -1.0
|
| 111 |
|
| 112 |
return False, "", 0.0
|
| 113 |
|
| 114 |
-
def _is_syntactically_valid(self, code: str) -> Tuple[bool, str]:
|
| 115 |
-
"""
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
| 123 |
|
| 124 |
def step(self, action: LintCodingAgentAction) -> LintCodingAgentObservation:
|
| 125 |
self._state.step_count += 1
|
| 126 |
task_data = self.curriculum.get(str(self.level))
|
| 127 |
|
| 128 |
-
# 1.
|
| 129 |
-
is_hijacked, hijack_msg, penalty = self._check_hijacking(
|
| 130 |
if is_hijacked:
|
| 131 |
return self._get_observation(hijack_msg, penalty, False)
|
| 132 |
|
| 133 |
-
# 2. Agency
|
| 134 |
-
agency_tax = -0.
|
| 135 |
|
| 136 |
-
# 3.
|
| 137 |
-
syntax_ok, error_msg = self._is_syntactically_valid(action.code_solution)
|
| 138 |
|
| 139 |
if not syntax_ok:
|
| 140 |
-
reward = -0.
|
| 141 |
-
feedback = f"
|
| 142 |
else:
|
|
|
|
| 143 |
logic_ok = task_data["ans"].lower() in action.code_solution.lower()
|
| 144 |
|
| 145 |
if logic_ok:
|
| 146 |
reward = 1.0 + agency_tax
|
| 147 |
-
feedback = f"Level {self.level} Success
|
| 148 |
self.level += 1
|
| 149 |
else:
|
| 150 |
-
reward = 0.
|
| 151 |
-
feedback = f"Syntax
|
| 152 |
if str(self.level) not in self.failed_queue:
|
| 153 |
self.failed_queue.append(str(self.level))
|
| 154 |
|
| 155 |
-
# 4. Handle Progression
|
| 156 |
done = self.level > self.max_levels
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
-
return self._get_observation(feedback, reward, done
|
| 164 |
|
| 165 |
-
def _get_observation(self, feedback: str, reward: float, done: bool
|
| 166 |
-
|
|
|
|
|
|
|
| 167 |
|
| 168 |
return LintCodingAgentObservation(
|
| 169 |
level=self.level,
|
| 170 |
language=task_data["lang"],
|
| 171 |
-
problem_statement=f"{feedback} |
|
| 172 |
-
code_context=json.dumps(self.vfs),
|
| 173 |
-
last_test_results=
|
| 174 |
reward=reward,
|
| 175 |
done=done,
|
| 176 |
metadata={
|
| 177 |
"step": self._state.step_count,
|
| 178 |
"vfs_files": list(self.vfs.keys()),
|
| 179 |
-
"
|
| 180 |
}
|
| 181 |
)
|
| 182 |
|
|
|
|
| 21 |
from openenv.core.env_server.interfaces import Environment
|
| 22 |
from openenv.core.env_server.types import State
|
| 23 |
|
| 24 |
+
# 🚀 ABSOLUTE IMPORT FIX for Phase 2
|
| 25 |
try:
|
|
|
|
|
|
|
| 26 |
from models import LintCodingAgentAction, LintCodingAgentObservation
|
| 27 |
+
except ImportError:
|
| 28 |
+
from ..models import LintCodingAgentAction, LintCodingAgentObservation
|
| 29 |
|
| 30 |
class LintCodingAgentEnvironment(Environment):
|
| 31 |
"""
|
| 32 |
Advanced Repository Sandbox.
|
| 33 |
+
Handles VFS synchronization for 15 levels and enforces
|
| 34 |
+
high-friction architectural guardrails.
|
| 35 |
"""
|
| 36 |
SUPPORTS_CONCURRENT_SESSIONS: bool = True
|
| 37 |
|
|
|
|
| 41 |
self.failed_queue = []
|
| 42 |
self.vfs: Dict[str, str] = {}
|
| 43 |
|
| 44 |
+
# --- DYNAMIC PATH RESOLUTION ---
|
| 45 |
+
self.current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 46 |
+
json_path = os.path.join(self.current_dir, "QUESTIONS.json")
|
| 47 |
+
self.templates_base = os.path.join(self.current_dir, "templates")
|
| 48 |
|
| 49 |
try:
|
| 50 |
with open(json_path, "r") as f:
|
| 51 |
self.curriculum = json.load(f)
|
| 52 |
+
# Ensure levels are sorted numerically for the sprint
|
| 53 |
+
self.all_levels = sorted(list(self.curriculum.keys()), key=int)
|
| 54 |
except Exception as e:
|
| 55 |
print(f"CRITICAL DATA ERROR: {e}")
|
| 56 |
+
self.curriculum = {"1": {"lang": "Python", "task": "Fallback: Init", "template_dir": "level_1", "ans": "print"}}
|
| 57 |
self.all_levels = ["1"]
|
| 58 |
|
| 59 |
+
self.max_levels = int(self.all_levels[-1]) if self.all_levels else 1
|
| 60 |
|
| 61 |
def reset(self) -> LintCodingAgentObservation:
|
| 62 |
+
"""Full state reset for Phase 2 scoring."""
|
| 63 |
self._state = State(episode_id=str(uuid4()), step_count=0)
|
| 64 |
self.level = 1
|
| 65 |
self.failed_queue = []
|
| 66 |
+
return self._load_template_state("Environment Reset: 15-Level Sprint Initialized.")
|
| 67 |
|
| 68 |
def _load_template_state(self, msg: str) -> LintCodingAgentObservation:
|
| 69 |
+
"""Loads the physical files from /templates into the VFS."""
|
| 70 |
+
task_data = self.curriculum.get(str(self.level), {})
|
| 71 |
+
# Default to level_{n} if template_dir isn't in JSON
|
| 72 |
template_dir_name = task_data.get("template_dir", f"level_{self.level}")
|
| 73 |
+
template_path = os.path.join(self.templates_base, template_dir_name)
|
|
|
|
|
|
|
| 74 |
|
| 75 |
self.vfs = {}
|
| 76 |
if os.path.exists(template_path):
|
|
|
|
| 82 |
with open(full_p, 'r', encoding='utf-8') as file:
|
| 83 |
self.vfs[rel_p] = file.read()
|
| 84 |
except Exception as e:
|
| 85 |
+
print(f"VFS Read Error {rel_p}: {e}")
|
| 86 |
else:
|
| 87 |
+
print(f"CRITICAL: Level path missing: {template_path}")
|
| 88 |
|
| 89 |
return self._get_observation(msg, 0.0, False)
|
| 90 |
|
| 91 |
+
def _check_hijacking(self, action: LintCodingAgentAction) -> Tuple[bool, str, float]:
|
| 92 |
+
"""Anti-adversarial monitor to prevent agent shortcuts."""
|
| 93 |
+
new_code = action.code_solution or ""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
# 1. Check for 'Ghosting' (Solution too small compared to expected)
|
| 96 |
+
if len(new_code.strip()) < 10:
|
| 97 |
+
return True, "HIJACK: Solution rejected for insufficient depth.", -2.0
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
# 2. Check for Comment-Only bypassing
|
| 100 |
code_only = re.sub(r'#.*', '', new_code).strip()
|
| 101 |
+
if len(code_only) < 5:
|
| 102 |
+
return True, "HIJACK: Solution contains only comments.", -1.5
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
return False, "", 0.0
|
| 105 |
|
| 106 |
+
def _is_syntactically_valid(self, code: str, lang: str) -> Tuple[bool, str]:
|
| 107 |
+
"""Verifies solution validity based on language type."""
|
| 108 |
+
if lang.lower() == "python":
|
| 109 |
+
try:
|
| 110 |
+
ast.parse(code)
|
| 111 |
+
return True, "Syntax Valid"
|
| 112 |
+
except SyntaxError as e:
|
| 113 |
+
return False, f"SyntaxError: {e.msg} (Line {e.lineno})"
|
| 114 |
+
# Fallback for non-python tasks in multi-lang sprint
|
| 115 |
+
return (True, "Non-Python/No-Verify") if len(code) > 5 else (False, "Empty Solution")
|
| 116 |
|
| 117 |
def step(self, action: LintCodingAgentAction) -> LintCodingAgentObservation:
|
| 118 |
self._state.step_count += 1
|
| 119 |
task_data = self.curriculum.get(str(self.level))
|
| 120 |
|
| 121 |
+
# 1. Monitoring & Taxation
|
| 122 |
+
is_hijacked, hijack_msg, penalty = self._check_hijacking(action)
|
| 123 |
if is_hijacked:
|
| 124 |
return self._get_observation(hijack_msg, penalty, False)
|
| 125 |
|
| 126 |
+
# 2. Agency Tax (Deducts ROI for using 'spawn' or 'delegate' keywords)
|
| 127 |
+
agency_tax = -0.1 if any(word in action.explanation.lower() for word in ["spawn", "delegate"]) else 0.0
|
| 128 |
|
| 129 |
+
# 3. Execution & Verification
|
| 130 |
+
syntax_ok, error_msg = self._is_syntactically_valid(action.code_solution, task_data["lang"])
|
| 131 |
|
| 132 |
if not syntax_ok:
|
| 133 |
+
reward = -0.2 + agency_tax
|
| 134 |
+
feedback = f"Transition Failed: {error_msg}"
|
| 135 |
else:
|
| 136 |
+
# Check logic against the 'ans' substring in manifest
|
| 137 |
logic_ok = task_data["ans"].lower() in action.code_solution.lower()
|
| 138 |
|
| 139 |
if logic_ok:
|
| 140 |
reward = 1.0 + agency_tax
|
| 141 |
+
feedback = f"Level {self.level} Success. Moving to next challenge."
|
| 142 |
self.level += 1
|
| 143 |
else:
|
| 144 |
+
reward = 0.1 + agency_tax
|
| 145 |
+
feedback = f"Syntax OK | Logic Missing: {task_data['task']}"
|
| 146 |
if str(self.level) not in self.failed_queue:
|
| 147 |
self.failed_queue.append(str(self.level))
|
| 148 |
|
| 149 |
+
# 4. Handle Progression
|
| 150 |
done = self.level > self.max_levels
|
| 151 |
+
|
| 152 |
+
# Failure Recovery: 30% chance to force retry of a failed level
|
| 153 |
+
if not done and reward > 0.8 and self.failed_queue and random.random() < 0.3:
|
| 154 |
+
self.level = int(self.failed_queue.pop(0))
|
| 155 |
+
feedback = f"Re-challenging failed level: {self.level}"
|
| 156 |
+
|
| 157 |
+
if not done and reward > 0.8:
|
| 158 |
+
return self._load_template_state(feedback)
|
| 159 |
|
| 160 |
+
return self._get_observation(feedback, reward, done)
|
| 161 |
|
| 162 |
+
def _get_observation(self, feedback: str, reward: float, done: bool) -> LintCodingAgentObservation:
|
| 163 |
+
# Prevent OOB errors during the final level transition
|
| 164 |
+
lookup_level = min(self.level, self.max_levels)
|
| 165 |
+
task_data = self.curriculum.get(str(lookup_level), self.curriculum.get("1"))
|
| 166 |
|
| 167 |
return LintCodingAgentObservation(
|
| 168 |
level=self.level,
|
| 169 |
language=task_data["lang"],
|
| 170 |
+
problem_statement=f"{feedback} | Current Task: {task_data['task']}",
|
| 171 |
+
code_context=json.dumps(self.vfs),
|
| 172 |
+
last_test_results=feedback,
|
| 173 |
reward=reward,
|
| 174 |
done=done,
|
| 175 |
metadata={
|
| 176 |
"step": self._state.step_count,
|
| 177 |
"vfs_files": list(self.vfs.keys()),
|
| 178 |
+
"total_levels": self.max_levels
|
| 179 |
}
|
| 180 |
)
|
| 181 |
|