Download faisi/W3-01-guided-rl/src/rl_env/guided_loop.py from ppJustin/micro: direct link, hf CLI and curl.
- Browser
- Download file 1.81 kB
-
https://huggingface.co/datasets/ppJustin/micro/resolve/main/faisi/W3-01-guided-rl/src/rl_env/guided_loop.py
- Command line
-
hf download hf://datasets/ppJustin/micro/faisi/W3-01-guided-rl/src/rl_env/guided_loop.py
-
curl -L -o guided_loop.py https://huggingface.co/datasets/ppJustin/micro/resolve/main/faisi/W3-01-guided-rl/src/rl_env/guided_loop.py
1.81 kB
| """Reuse W2-01 sampling, adding terminal traces and opt-in evaluation feedback.""" | |
| import asyncio | |
| import json | |
| import math | |
| import time | |
| from rl_env.common import SOCKET_LIMIT, json_bytes | |
| from rl_env.consistency_scorer import guard_feedback, guard_phase, rule_library | |
| from rl_env.sandbox_env import SandboxAgentLoop | |
| class GuidedAgentLoop(SandboxAgentLoop): | |
| def handle_tool_result(self, key, initial, steps, name, args, result, forced): | |
| if guard_phase(key["phase"]) and name == "submit_diagnosis" and result.get("status") == "accepted": | |
| return guard_feedback(initial, steps, args, forced, rule_library()) or result | |
| return result | |
| async def score_episode(self, key, step, trace): | |
| train = key["phase"] == "train" | |
| path = "/ipc/score.sock" if train else "/ipc/eval.sock" | |
| began = time.monotonic_ns() | |
| body = json_bytes(key | {"step": step, "trace": trace}) | |
| encode_ns = time.monotonic_ns() - began | |
| reader, writer = await asyncio.open_unix_connection(path, limit=SOCKET_LIMIT) | |
| try: | |
| # Separate serialization, socket delivery, and scorer work in the ledger. | |
| sent_ns = time.monotonic_ns() | |
| header = f'{{"sent_ns":{sent_ns},"encode_ns":{encode_ns},'.encode() | |
| writer.write(header + body[1:]) | |
| await writer.drain() | |
| line = await asyncio.wait_for(reader.readline(), 120) | |
| result = json.loads(line) | |
| score = result.get("score") | |
| if type(score) not in (float, int) or not math.isfinite(score) or not ( | |
| score == -0.1 or 0 <= score <= 1 | |
| ): | |
| raise RuntimeError("invalid_isolated_score") | |
| return score | |
| finally: | |
| writer.close() | |
| await writer.wait_closed() | |