Spaces:
Sleeping
Sleeping
Pranjal Parashar commited on
changes
Browse files- baseline.py +18 -2
- inference.py +67 -15
- server/developer_control_room_environment.py +41 -0
baseline.py
CHANGED
|
@@ -256,6 +256,17 @@ def action_is_valid(action: Optional[dict[str, Any]], observation: Any) -> bool:
|
|
| 256 |
read_paths = set(queried.get("read_file", {}))
|
| 257 |
inspected_assets = set(queried.get("inspect_schema", {})) | set(queried.get("inspect_lineage", {}))
|
| 258 |
has_edit = bool(observation.edited_files)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
if action_type == "read_file":
|
| 261 |
path = (params.get("path") or "").strip()
|
|
@@ -299,8 +310,13 @@ def action_is_valid(action: Optional[dict[str, Any]], observation: Any) -> bool:
|
|
| 299 |
|
| 300 |
if action_type == "submit_repair":
|
| 301 |
submission = {"root_cause", "fix_path", "summary"}
|
| 302 |
-
|
| 303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
)
|
| 305 |
|
| 306 |
if action_type == "submit_workspace":
|
|
|
|
| 256 |
read_paths = set(queried.get("read_file", {}))
|
| 257 |
inspected_assets = set(queried.get("inspect_schema", {})) | set(queried.get("inspect_lineage", {}))
|
| 258 |
has_edit = bool(observation.edited_files)
|
| 259 |
+
repair_all_validators_passed = (
|
| 260 |
+
observation.task_id.startswith("repair_")
|
| 261 |
+
and bool(observation.available_validators)
|
| 262 |
+
and all(
|
| 263 |
+
observation.validator_status.get(validator, {}).get("passed", False)
|
| 264 |
+
for validator in observation.available_validators
|
| 265 |
+
)
|
| 266 |
+
)
|
| 267 |
+
|
| 268 |
+
if repair_all_validators_passed and action_type != "submit_repair":
|
| 269 |
+
return False
|
| 270 |
|
| 271 |
if action_type == "read_file":
|
| 272 |
path = (params.get("path") or "").strip()
|
|
|
|
| 310 |
|
| 311 |
if action_type == "submit_repair":
|
| 312 |
submission = {"root_cause", "fix_path", "summary"}
|
| 313 |
+
fix_path = str(params.get("fix_path") or "").strip()
|
| 314 |
+
return (
|
| 315 |
+
has_edit
|
| 316 |
+
and bool(observation.validator_status)
|
| 317 |
+
and submission.issubset(params)
|
| 318 |
+
and all(str(params.get(key) or "").strip() for key in submission)
|
| 319 |
+
and fix_path in set(observation.editable_targets)
|
| 320 |
)
|
| 321 |
|
| 322 |
if action_type == "submit_workspace":
|
inference.py
CHANGED
|
@@ -119,6 +119,43 @@ def format_action_str(action_dict: Optional[dict[str, Any]]) -> str:
|
|
| 119 |
return f"{action_type}({','.join(parts)})"
|
| 120 |
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
async def create_env() -> DeveloperControlRoomEnv:
|
| 123 |
if ENV_URL:
|
| 124 |
env = DeveloperControlRoomEnv(base_url=ENV_URL)
|
|
@@ -158,22 +195,37 @@ async def run_task(
|
|
| 158 |
break
|
| 159 |
|
| 160 |
action_dict: Optional[dict[str, Any]] = None
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
action_dict = fallback_action(task_name, observation)
|
| 175 |
-
|
| 176 |
-
action = build_action(action_dict)
|
| 177 |
result = await env.step(action)
|
| 178 |
observation = result.observation
|
| 179 |
|
|
|
|
| 119 |
return f"{action_type}({','.join(parts)})"
|
| 120 |
|
| 121 |
|
| 122 |
+
def action_with_provenance(action_dict: dict[str, Any], source: str) -> dict[str, Any]:
|
| 123 |
+
params = dict(action_dict.get("parameters", {}))
|
| 124 |
+
params["agent_source"] = source
|
| 125 |
+
return {
|
| 126 |
+
"action_type": action_dict.get("action_type", ""),
|
| 127 |
+
"parameters": params,
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
def repair_ready_to_submit(task_name: str, observation: Any) -> bool:
|
| 132 |
+
if not task_name.startswith("repair_"):
|
| 133 |
+
return False
|
| 134 |
+
if not observation.edited_files or not observation.available_validators:
|
| 135 |
+
return False
|
| 136 |
+
return all(
|
| 137 |
+
observation.validator_status.get(validator, {}).get("passed", False)
|
| 138 |
+
for validator in observation.available_validators
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
def repeated_failed_repair_edit(task_name: str, observation: Any, action_dict: Optional[dict[str, Any]]) -> bool:
|
| 143 |
+
if not task_name.startswith("repair_") or not isinstance(action_dict, dict):
|
| 144 |
+
return False
|
| 145 |
+
if action_dict.get("action_type") != "edit_file":
|
| 146 |
+
return False
|
| 147 |
+
params = action_dict.get("parameters", {})
|
| 148 |
+
if not isinstance(params, dict):
|
| 149 |
+
return False
|
| 150 |
+
path = str(params.get("path") or "").strip()
|
| 151 |
+
content = str(params.get("content") or "")
|
| 152 |
+
if not path or path not in observation.edited_files:
|
| 153 |
+
return False
|
| 154 |
+
if observation.edited_files.get(path, "") != content:
|
| 155 |
+
return False
|
| 156 |
+
return any(not status.get("passed", False) for status in observation.validator_status.values())
|
| 157 |
+
|
| 158 |
+
|
| 159 |
async def create_env() -> DeveloperControlRoomEnv:
|
| 160 |
if ENV_URL:
|
| 161 |
env = DeveloperControlRoomEnv(base_url=ENV_URL)
|
|
|
|
| 195 |
break
|
| 196 |
|
| 197 |
action_dict: Optional[dict[str, Any]] = None
|
| 198 |
+
action_source = "fallback"
|
| 199 |
+
try:
|
| 200 |
+
if repair_ready_to_submit(task_name, observation):
|
| 201 |
+
action_dict = fallback_action(task_name, observation)
|
| 202 |
+
elif step <= MAX_MODEL_STEPS:
|
| 203 |
+
action_dict = get_model_action(
|
| 204 |
+
client,
|
| 205 |
+
MODEL_NAME,
|
| 206 |
+
TEMPERATURE,
|
| 207 |
+
MAX_TOKENS,
|
| 208 |
+
MODEL_TIMEOUT_SECONDS,
|
| 209 |
+
step,
|
| 210 |
+
observation,
|
| 211 |
+
history,
|
| 212 |
+
"",
|
| 213 |
+
)
|
| 214 |
+
if action_dict is not None:
|
| 215 |
+
action_source = "llm"
|
| 216 |
+
if repeated_failed_repair_edit(task_name, observation, action_dict):
|
| 217 |
+
action_dict = None
|
| 218 |
+
if not action_is_valid(action_dict, observation):
|
| 219 |
+
action_source = "fallback_after_llm" if action_dict is not None else "fallback"
|
| 220 |
+
action_dict = fallback_action(task_name, observation)
|
| 221 |
+
|
| 222 |
+
action = build_action(action_with_provenance(action_dict, action_source))
|
| 223 |
+
except Exception as exc:
|
| 224 |
+
debug_log(f"[DEBUG] Step {step} action preparation failed: {exc}")
|
| 225 |
+
debug_log(traceback.format_exc().rstrip())
|
| 226 |
+
action_source = "fallback_after_error"
|
| 227 |
action_dict = fallback_action(task_name, observation)
|
| 228 |
+
action = build_action(action_with_provenance(action_dict, action_source))
|
|
|
|
| 229 |
result = await env.step(action)
|
| 230 |
observation = result.observation
|
| 231 |
|
server/developer_control_room_environment.py
CHANGED
|
@@ -3,6 +3,7 @@
|
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import os
|
|
|
|
| 6 |
from uuid import uuid4
|
| 7 |
|
| 8 |
from openenv.core.env_server.interfaces import Environment
|
|
@@ -42,6 +43,7 @@ class DeveloperControlRoomEnvironment(
|
|
| 42 |
SOLVED_TERMINAL_REWARD_BONUS = float(
|
| 43 |
os.getenv("DEVELOPER_CONTROL_ROOM_SOLVED_TERMINAL_REWARD_BONUS", "0.10")
|
| 44 |
)
|
|
|
|
| 45 |
|
| 46 |
def __init__(self) -> None:
|
| 47 |
self._task_def: dict = {}
|
|
@@ -54,6 +56,29 @@ class DeveloperControlRoomEnvironment(
|
|
| 54 |
)
|
| 55 |
self._ready = False
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
def reset(
|
| 58 |
self,
|
| 59 |
seed: int | None = None,
|
|
@@ -86,6 +111,9 @@ class DeveloperControlRoomEnvironment(
|
|
| 86 |
last_action_error=None,
|
| 87 |
)
|
| 88 |
self._ready = True
|
|
|
|
|
|
|
|
|
|
| 89 |
return self._build_observation(reward=0.0, done=False)
|
| 90 |
|
| 91 |
def step(
|
|
@@ -154,6 +182,19 @@ class DeveloperControlRoomEnvironment(
|
|
| 154 |
self._state.cumulative_reward = round(self._state.cumulative_reward + reward, 4)
|
| 155 |
self._state.feedback = " | ".join(part for part in feedback if part) or after_result["feedback"]
|
| 156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
return self._build_observation(
|
| 158 |
reward=round(reward, 4),
|
| 159 |
done=self._state.done,
|
|
|
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import os
|
| 6 |
+
import sys
|
| 7 |
from uuid import uuid4
|
| 8 |
|
| 9 |
from openenv.core.env_server.interfaces import Environment
|
|
|
|
| 43 |
SOLVED_TERMINAL_REWARD_BONUS = float(
|
| 44 |
os.getenv("DEVELOPER_CONTROL_ROOM_SOLVED_TERMINAL_REWARD_BONUS", "0.10")
|
| 45 |
)
|
| 46 |
+
SERVER_STEP_LOGS = os.getenv("DEVELOPER_CONTROL_ROOM_SERVER_STEP_LOGS", "true").lower() == "true"
|
| 47 |
|
| 48 |
def __init__(self) -> None:
|
| 49 |
self._task_def: dict = {}
|
|
|
|
| 56 |
)
|
| 57 |
self._ready = False
|
| 58 |
|
| 59 |
+
def _server_log(self, message: str) -> None:
|
| 60 |
+
if self.SERVER_STEP_LOGS:
|
| 61 |
+
print(message, file=sys.stderr, flush=True)
|
| 62 |
+
|
| 63 |
+
def _fmt_value(self, value: object) -> str:
|
| 64 |
+
if value is None:
|
| 65 |
+
return "null"
|
| 66 |
+
if isinstance(value, bool):
|
| 67 |
+
return str(value).lower()
|
| 68 |
+
if isinstance(value, (int, float)):
|
| 69 |
+
return str(value)
|
| 70 |
+
text = str(value).replace("\\", "\\\\").replace("'", "\\'")
|
| 71 |
+
text = text.replace("\n", "\\n").replace("\r", "\\r")
|
| 72 |
+
return f"'{text}'"
|
| 73 |
+
|
| 74 |
+
def _format_action(self, action: DeveloperControlRoomAction) -> str:
|
| 75 |
+
payload = action.parameters.model_dump(exclude_none=True)
|
| 76 |
+
payload.pop("agent_source", None)
|
| 77 |
+
if not payload:
|
| 78 |
+
return f"{action.action_type}()"
|
| 79 |
+
parts = [f"{key}={self._fmt_value(payload[key])}" for key in sorted(payload)]
|
| 80 |
+
return f"{action.action_type}({','.join(parts)})"
|
| 81 |
+
|
| 82 |
def reset(
|
| 83 |
self,
|
| 84 |
seed: int | None = None,
|
|
|
|
| 111 |
last_action_error=None,
|
| 112 |
)
|
| 113 |
self._ready = True
|
| 114 |
+
self._server_log(
|
| 115 |
+
f"[SERVER-START] task={task_id} scenario={self._scenario['scenario_id']} max_steps={self._task_def['max_steps']}"
|
| 116 |
+
)
|
| 117 |
return self._build_observation(reward=0.0, done=False)
|
| 118 |
|
| 119 |
def step(
|
|
|
|
| 182 |
self._state.cumulative_reward = round(self._state.cumulative_reward + reward, 4)
|
| 183 |
self._state.feedback = " | ".join(part for part in feedback if part) or after_result["feedback"]
|
| 184 |
|
| 185 |
+
source = payload.get("agent_source", "unknown")
|
| 186 |
+
self._server_log(
|
| 187 |
+
f"[SERVER-STEP] step={self._state.step_count} scenario={self._state.scenario_id} "
|
| 188 |
+
f"source={source} action={self._format_action(action)} reward={round(reward, 4):.2f} "
|
| 189 |
+
f"done={str(self._state.done).lower()} error={self._state.last_action_error or 'null'}"
|
| 190 |
+
)
|
| 191 |
+
if self._state.done:
|
| 192 |
+
self._server_log(
|
| 193 |
+
f"[SERVER-END] task={self._state.task_id} scenario={self._state.scenario_id} "
|
| 194 |
+
f"steps={self._state.step_count} total={after_result['total']:.2f} "
|
| 195 |
+
f"solved={str(after_result.get('solved', False)).lower()}"
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
return self._build_observation(
|
| 199 |
reward=round(reward, 4),
|
| 200 |
done=self._state.done,
|