Skip to content

Commit f44567d

Browse files
committed
🤖 refactor: simplify terminal-bench code (-11.5% LoC)
Simplifications: - Removed redundant validation (env vars already have defaults) - Used walrus operator for cleaner conditionals - Inlined single-use methods (_build_archive) - Removed unnecessary checks (Path truthy, buffer.seek(0)) - Simplified bash conditionals and error paths - Eliminated ensure_bun() wrapper function - Condensed git repo initialization logic Net: -76 lines (38 insertions, 114 deletions) across 4 files
1 parent abd22b3 commit f44567d

File tree

4 files changed

+38
-114
lines changed

4 files changed

+38
-114
lines changed

benchmarks/terminal_bench/cmux-run.sh

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ CMUX_WORKSPACE_ID="${CMUX_WORKSPACE_ID:-cmux-bench}"
3030
CMUX_THINKING_LEVEL="${CMUX_THINKING_LEVEL:-high}"
3131
CMUX_MODE="${CMUX_MODE:-exec}"
3232

33-
ensure_bun() {
34-
if ! command -v bun >/dev/null 2>&1; then
35-
fatal "bun must be installed before running the cmux agent"
36-
fi
37-
}
38-
3933
resolve_project_path() {
4034
if [[ -n "${CMUX_PROJECT_PATH}" ]]; then
4135
if [[ -d "${CMUX_PROJECT_PATH}" ]]; then
@@ -59,40 +53,27 @@ resolve_project_path() {
5953
ensure_git_repo() {
6054
local project_path=$1
6155

62-
if command -v git >/dev/null 2>&1; then
63-
if git -C "${project_path}" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
64-
# Ensure trunk branch exists even on pre-existing repos.
65-
if ! git -C "${project_path}" rev-parse --verify "${CMUX_TRUNK}" >/dev/null 2>&1; then
66-
git -C "${project_path}" checkout -b "${CMUX_TRUNK}" >/dev/null 2>&1 || true
67-
else
68-
git -C "${project_path}" checkout "${CMUX_TRUNK}" >/dev/null 2>&1 || true
69-
fi
70-
return 0
71-
fi
56+
command -v git >/dev/null 2>&1 || return 0
7257

73-
log "initialising git repository at ${project_path}"
74-
if git -C "${project_path}" init --initial-branch="${CMUX_TRUNK}" >/dev/null 2>&1; then
75-
:
76-
else
77-
git -C "${project_path}" init >/dev/null
78-
git -C "${project_path}" checkout -B "${CMUX_TRUNK}" >/dev/null
79-
fi
80-
git -C "${project_path}" config user.name "cmux-bench"
81-
git -C "${project_path}" config user.email "bench@cmux.local"
82-
git -C "${project_path}" add -A >/dev/null
83-
git -C "${project_path}" commit -m "chore: initial snapshot" --allow-empty >/dev/null
84-
git -C "${project_path}" branch -M "${CMUX_TRUNK}" >/dev/null
85-
else
86-
log "git not available; skipping repository initialisation"
58+
if git -C "${project_path}" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
59+
git -C "${project_path}" checkout "${CMUX_TRUNK}" 2>/dev/null || \
60+
git -C "${project_path}" checkout -b "${CMUX_TRUNK}" 2>/dev/null || true
61+
return 0
8762
fi
63+
64+
log "initialising git repository at ${project_path}"
65+
git -C "${project_path}" init --initial-branch="${CMUX_TRUNK}" 2>/dev/null || \
66+
(git -C "${project_path}" init && git -C "${project_path}" checkout -B "${CMUX_TRUNK}") >/dev/null
67+
git -C "${project_path}" config user.name "cmux-bench"
68+
git -C "${project_path}" config user.email "bench@cmux.local"
69+
git -C "${project_path}" add -A >/dev/null
70+
git -C "${project_path}" commit -m "chore: initial snapshot" --allow-empty >/dev/null
8871
}
8972

90-
ensure_bun
73+
command -v bun >/dev/null 2>&1 || fatal "bun is not installed"
9174
project_path=$(resolve_project_path)
9275
ensure_git_repo "${project_path}"
9376

94-
bun --version >/dev/null 2>&1 || fatal "bun not available after ensure_bun"
95-
9677
log "starting cmux agent session for ${project_path}"
9778
cd "${CMUX_APP_ROOT}"
9879

benchmarks/terminal_bench/cmux_agent.py

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -141,33 +141,17 @@ def _env(self) -> dict[str, str]:
141141
else:
142142
raise ValueError("CMUX_MODE must be one of plan, exec, or execute")
143143

144-
config_root = env["CMUX_CONFIG_ROOT"].strip()
145-
app_root = env["CMUX_APP_ROOT"].strip()
146-
workspace_id = env["CMUX_WORKSPACE_ID"].strip()
147-
project_candidates = env["CMUX_PROJECT_CANDIDATES"].strip()
148-
if not config_root:
149-
raise ValueError("CMUX_CONFIG_ROOT must be set")
150-
if not app_root:
151-
raise ValueError("CMUX_APP_ROOT must be set")
152-
if not workspace_id:
153-
raise ValueError("CMUX_WORKSPACE_ID must be set")
154-
if not project_candidates:
155-
raise ValueError("CMUX_PROJECT_CANDIDATES must be set")
156-
env["CMUX_CONFIG_ROOT"] = config_root
157-
env["CMUX_APP_ROOT"] = app_root
158-
env["CMUX_WORKSPACE_ID"] = workspace_id
159-
env["CMUX_PROJECT_CANDIDATES"] = project_candidates
160-
161-
timeout_value = env.get("CMUX_TIMEOUT_MS")
162-
if timeout_value:
163-
timeout_value = timeout_value.strip()
164-
if not timeout_value.isdigit():
165-
raise ValueError("CMUX_TIMEOUT_MS must be an integer expressed in ms")
166-
env["CMUX_TIMEOUT_MS"] = timeout_value
167-
168-
project_path = env.get("CMUX_PROJECT_PATH")
169-
if project_path is not None and not project_path.strip():
170-
raise ValueError("CMUX_PROJECT_PATH must be non-empty when provided")
144+
# These env vars are all set with defaults above, no need to validate
145+
for key in ("CMUX_CONFIG_ROOT", "CMUX_APP_ROOT", "CMUX_WORKSPACE_ID", "CMUX_PROJECT_CANDIDATES"):
146+
env[key] = env[key].strip()
147+
148+
if timeout_value := env.get("CMUX_TIMEOUT_MS"):
149+
if not timeout_value.strip().isdigit():
150+
raise ValueError("CMUX_TIMEOUT_MS must be an integer")
151+
152+
if project_path := env.get("CMUX_PROJECT_PATH"):
153+
if not project_path.strip():
154+
raise ValueError("CMUX_PROJECT_PATH must be non-empty when provided")
171155

172156
return env
173157

@@ -181,37 +165,21 @@ def perform_task(
181165
session: TmuxSession,
182166
logging_dir=None,
183167
) -> AgentResult:
184-
if not instruction or not instruction.strip():
168+
if not instruction.strip():
185169
raise ValueError("instruction must be a non-empty string")
186-
187170
self._ensure_payload_staged(session)
188-
return super().perform_task(
189-
instruction=instruction, session=session, logging_dir=logging_dir
190-
)
171+
return super().perform_task(instruction, session, logging_dir)
191172

192173
def _ensure_payload_staged(self, session: TmuxSession) -> None:
193174
container_id = getattr(session.container, "id", None)
194-
if container_id and container_id == self._staged_container_id:
175+
if container_id == self._staged_container_id:
195176
return
196177

197-
archive = self._build_archive()
198-
stage_payload(
199-
session=session,
200-
archive_bytes=archive,
201-
archive_name=self._ARCHIVE_NAME,
202-
runner_path=self._runner_path,
203-
)
204-
205-
if container_id:
206-
self._staged_container_id = container_id
207-
208-
def _build_archive(self) -> bytes:
209-
if self._archive_bytes is not None:
210-
return self._archive_bytes
178+
if not self._archive_bytes:
179+
self._archive_bytes = build_app_archive(self._repo_root, self._INCLUDE_PATHS)
211180

212-
archive = build_app_archive(self._repo_root, self._INCLUDE_PATHS)
213-
self._archive_bytes = archive
214-
return archive
181+
stage_payload(session, self._archive_bytes, self._ARCHIVE_NAME, self._runner_path)
182+
self._staged_container_id = container_id
215183

216184
def _run_agent_commands(self, instruction: str) -> list[TerminalCommand]:
217185
escaped = shlex.quote(instruction)

benchmarks/terminal_bench/cmux_payload.py

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
def build_app_archive(repo_root: Path, include_paths: Iterable[str]) -> bytes:
1313
"""Pack the cmux workspace into a gzipped tarball."""
14-
15-
if not repo_root or not repo_root.exists():
14+
if not repo_root.exists():
1615
raise FileNotFoundError(f"cmux repo root {repo_root} not found")
1716

1817
buffer = io.BytesIO()
@@ -22,8 +21,6 @@ def build_app_archive(repo_root: Path, include_paths: Iterable[str]) -> bytes:
2221
if not source.exists():
2322
raise FileNotFoundError(f"Required file {source} missing")
2423
archive.add(source, arcname=relative_path, recursive=True)
25-
26-
buffer.seek(0)
2724
return buffer.getvalue()
2825

2926

@@ -34,27 +31,13 @@ def stage_payload(
3431
runner_path: Path,
3532
) -> None:
3633
"""Copy the cmux bundle and runner into the task container."""
37-
38-
if not archive_bytes:
39-
raise ValueError("archive_bytes must be non-empty")
40-
if not runner_path or not runner_path.is_file():
41-
raise FileNotFoundError(f"cmux runner missing at {runner_path}")
42-
4334
with tempfile.NamedTemporaryFile(suffix=".tar.gz", delete=False) as temp_file:
4435
temp_file.write(archive_bytes)
4536
temp_path = Path(temp_file.name)
4637

4738
try:
48-
session.copy_to_container(
49-
paths=temp_path,
50-
container_dir="/installed-agent",
51-
container_filename=archive_name,
52-
)
39+
session.copy_to_container(temp_path, "/installed-agent", archive_name)
5340
finally:
5441
temp_path.unlink(missing_ok=True)
5542

56-
session.copy_to_container(
57-
paths=runner_path,
58-
container_dir="/installed-agent",
59-
container_filename=runner_path.name,
60-
)
43+
session.copy_to_container(runner_path, "/installed-agent", runner_path.name)

benchmarks/terminal_bench/cmux_setup.sh.j2

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,15 @@ CMUX_APP_ROOT="${CMUX_APP_ROOT:-/opt/cmux-app}"
3737
CMUX_CONFIG_ROOT="${CMUX_CONFIG_ROOT:-/root/.cmux}"
3838
CMUX_AGENT_VERSION="{{ version if version is not none else '' }}"
3939

40-
mkdir -p "$CMUX_APP_ROOT"
41-
40+
rm -rf "${CMUX_APP_ROOT}"
4241
if [[ -n "${CMUX_AGENT_VERSION}" ]]; then
43-
: "${CMUX_AGENT_GIT_URL:?CMUX_AGENT_GIT_URL must be set when version is provided}"
42+
: "${CMUX_AGENT_GIT_URL:?CMUX_AGENT_GIT_URL required when version is set}"
4443
log "cloning cmux from ${CMUX_AGENT_GIT_URL} @ ${CMUX_AGENT_VERSION}"
45-
rm -rf "${CMUX_APP_ROOT}"
4644
git clone --depth 1 --branch "${CMUX_AGENT_VERSION}" "${CMUX_AGENT_GIT_URL}" "${CMUX_APP_ROOT}"
4745
else
48-
ARCHIVE_PATH="/installed-agent/cmux-app.tar.gz"
49-
if [[ ! -s "${ARCHIVE_PATH}" ]]; then
50-
printf 'Expected cmux archive at %s\n' "${ARCHIVE_PATH}" >&2
51-
exit 1
52-
fi
5346
log "extracting cmux archive"
54-
rm -rf "${CMUX_APP_ROOT}"
5547
mkdir -p "${CMUX_APP_ROOT}"
56-
tar -xzf "${ARCHIVE_PATH}" -C "${CMUX_APP_ROOT}"
48+
tar -xzf "/installed-agent/cmux-app.tar.gz" -C "${CMUX_APP_ROOT}"
5749
fi
5850

5951
cd "${CMUX_APP_ROOT}"

0 commit comments

Comments
 (0)