File size: 3,307 Bytes
e042d72 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | """Per-episode tool and argument names.
Every private arena task brings its own interface. If one task always calls its tools read_file and run_tests, a
policy can memorise the names instead of reading the tool list. Each reset therefore draws names from these pools
(the canonical name is one of the options). `submit` keeps its name and arguments: it is also the arena's finish
action.
"""
import random
import re
from .base import Tool
TOOL_NAMES = {
"list_files": ["list_files", "ls", "tree", "list_dir"],
"read_file": ["read_file", "open_file", "cat", "view_file", "show_file"],
"search": ["search", "grep", "find_text", "search_code"],
"edit_file": ["edit_file", "str_replace", "replace_in_file", "patch_file"],
"write_file": ["write_file", "save_file", "create_file", "put_file"],
"run": ["run", "bash", "shell", "exec", "run_command"],
"run_tests": ["run_tests", "pytest", "test", "check_tests"],
}
ARG_NAMES = {
"path": ["path", "file", "filename"],
"start": ["start", "from_line", "first"],
"end": ["end", "to_line", "last"],
"pattern": ["pattern", "regex", "query"],
"old": ["old", "old_str", "find", "before"],
"new": ["new", "new_str", "replace", "after"],
"content": ["content", "text", "body"],
"command": ["command", "cmd", "script"],
"target": ["target", "path", "tests"],
}
# Names that are also ordinary words ("the style guide", "check how...") are renamed in the tool list but not in
# running text; the others are replaced wherever they appear as a word.
TEXT_SAFE = {name for name in TOOL_NAMES if "_" in name}
class Interface:
def __init__(self, tools: list[Tool], rng: random.Random):
self.tool_names, used = {}, {"submit"}
for tool in tools:
options = [n for n in TOOL_NAMES.get(tool.name, [tool.name]) if n not in used] or [tool.name]
name = tool.name if tool.name == "submit" else rng.choice(options)
used.add(name)
self.tool_names[tool.name] = name
pairs = [(old, new) for old, new in self.tool_names.items() if old in TEXT_SAFE and old != new]
self._pattern = re.compile(r"\b(" + "|".join(re.escape(old) for old, _ in pairs) + r")\b") if pairs else None
self._lookup = dict(pairs)
self.tools = [self._rename(tool, rng) for tool in tools]
def _rename(self, tool: Tool, rng: random.Random) -> Tool:
if tool.name == "submit":
return tool
args, used = {}, set()
for arg in tool.args:
options = [n for n in ARG_NAMES.get(arg, [arg]) if n not in used] or [arg]
args[arg] = rng.choice(options)
used.add(args[arg])
back = {new: old for old, new in args.items()}
def fn(a, _fn=tool.fn):
return _fn({back.get(k, k): v for k, v in a.items()})
return Tool(self.tool_names[tool.name], self.text(tool.help), fn,
{args[k]: t for k, t in tool.args.items()}, tuple(args[k] for k in tool.optional), tool.final)
def text(self, s: str) -> str:
"""Rewrite canonical tool names in briefs and tool output to this episode's names."""
if not self._pattern or not isinstance(s, str):
return s
return self._pattern.sub(lambda m: self._lookup[m.group(1)], s)
|