@@ -1,16 +1,14 @@
1+# TODO: `LocalMiniCoderAgent` so we can run terminal bench with our current local modified code.
2+
13 from __future__ import annotations
24
35 import json
46 import shlex
5-import shutil
6-import subprocess
7+import tarfile
78 from pathlib import Path
89 from tempfile import TemporaryDirectory
910 from typing import Any
1011
11-
12-_FINAL_STOP_REASONS = frozenset({"stop", "error", "aborted"})
13-
1412 from harbor.agents.installed.base import BaseInstalledAgent, with_prompt_template
1513 from harbor.environments.base import BaseEnvironment
1614 from harbor.models.agent.context import AgentContext
@@ -18,476 +16,197 @@ from harbor.models.trial.paths import EnvironmentPaths
1816
1917
2018 class LocalMiniCoderAgent(BaseInstalledAgent):
21- """Harbor installed-agent wrapper for this local mini-coder checkout."""
22-
23- _JSON_LOG_FILENAME = "mini-coder.json"
24- _STDERR_LOG_FILENAME = "mini-coder.stderr.txt"
25- _PACKAGE_PATHS = ("bin/mc.ts", "src")
26- _PACKAGE_MANIFEST_KEYS = (
27- "name",
28- "version",
29- "type",
30- "bin",
31- "dependencies",
32- "packageManager",
33- )
34-
35- def __init__(self, *args, version: str = "local", **kwargs):
36- super().__init__(*args, version=version, **kwargs)
37- self._host_repo_root = Path(__file__).resolve().parents[1]
38- host_config_dir = Path.home() / ".config" / "mini-coder"
39- self._host_auth_path = host_config_dir / "auth.json"
40- self._host_settings_path = host_config_dir / "settings.json"
19+ """Minimal Harbor installed-agent wrapper for the local mini-coder checkout."""
20+
21+ CONTAINER_ROOT = "/tmp/mini-coder-next"
22+ STDOUT_LOG = "mini-coder.ndjson"
23+ STDERR_LOG = "mini-coder.stderr.txt"
4124
4225 @staticmethod
4326 def name() -> str:
4427 return "mini-coder-local"
4528
46- async def install(self, environment: BaseEnvironment) -> None:
47- self._require_host_checkout()
48- await self._install_system_dependencies(environment)
49- await self._install_bun(environment)
50-
51- with TemporaryDirectory(prefix="mini-coder-harbor-") as temp_dir:
52- tarball_path = self._package_local_checkout(Path(temp_dir))
53- container_tarball_path = f"/tmp/{tarball_path.name}"
54- await environment.upload_file(tarball_path, container_tarball_path)
55- await self._install_global_package(environment, container_tarball_path)
56-
57- def populate_context_post_run(self, context: AgentContext) -> None:
58- output_path = self.logs_dir / self._JSON_LOG_FILENAME
59- stderr_path = self.logs_dir / self._STDERR_LOG_FILENAME
60- metadata: dict[str, Any] = {
61- "json_log_path": str(output_path),
62- "stderr_log_path": str(stderr_path),
63- }
64-
65- if stderr_path.is_file():
66- stderr_text = stderr_path.read_text(
67- encoding="utf-8", errors="replace"
68- ).strip()
69- if stderr_text:
70- metadata["stderr_excerpt"] = self._truncate_metadata_text(stderr_text)
71-
72- try:
73- with output_path.open(encoding="utf-8") as handle:
74- result = json.load(handle)
75- except FileNotFoundError:
76- metadata["parse_error"] = f"Missing mini-coder JSON log: {output_path}"
77- self._merge_context_metadata(context, metadata)
78- return
79- except json.JSONDecodeError as exc:
80- metadata["parse_error"] = f"Invalid mini-coder JSON log: {exc}"
81- metadata["stdout_excerpt"] = self._read_text_excerpt(output_path)
82- self._merge_context_metadata(context, metadata)
83- return
84- except OSError as exc:
85- metadata["parse_error"] = f"Failed to read mini-coder JSON log: {exc}"
86- self._merge_context_metadata(context, metadata)
87- return
88-
89- parsed_result = self._extract_result_from_json_log(result)
90- metadata.update(parsed_result["metadata"])
91- result_message = parsed_result.get("message")
92- if not isinstance(result_message, dict):
93- self._merge_context_metadata(context, metadata)
94- return
95-
96- usage = result_message.get("usage")
97- usage_data = usage if isinstance(usage, dict) else {}
98-
99- context.n_input_tokens = self._optional_int(usage_data.get("input"))
100- context.n_output_tokens = self._optional_int(usage_data.get("output"))
101-
102- cache_read = self._optional_int(usage_data.get("cacheRead"))
103- cache_write = self._optional_int(usage_data.get("cacheWrite"))
104- if cache_read is not None or cache_write is not None:
105- context.n_cache_tokens = (cache_read or 0) + (cache_write or 0)
106-
107- cost = usage_data.get("cost")
108- if isinstance(cost, dict):
109- context.cost_usd = self._optional_float(cost.get("total"))
110-
111- for source_key, metadata_key in (
112- ("api", "api"),
113- ("provider", "provider"),
114- ("model", "model"),
115- ("responseId", "response_id"),
116- ("stopReason", "stop_reason"),
117- ("errorMessage", "error_message"),
118- ):
119- value = result_message.get(source_key)
120- if isinstance(value, str) and value:
121- metadata[metadata_key] = value
122-
123- if usage:
124- metadata["usage"] = usage
125-
126- self._merge_context_metadata(context, metadata)
127-
128- @classmethod
129- def _extract_result_from_json_log(
130- cls, result: Any
131- ) -> dict[str, dict[str, Any] | dict[str, Any] | None]:
132- if isinstance(result, dict):
133- return {"message": result, "metadata": {"json_log_shape": "object"}}
134-
135- if not isinstance(result, list):
136- return {
137- "message": None,
138- "metadata": {
139- "json_log_shape": type(result).__name__,
140- "parse_error": "mini-coder JSON log was neither an object nor an array",
141- },
142- }
143-
144- assistant_messages = [
145- item
146- for item in result
147- if isinstance(item, dict) and item.get("role") == "assistant"
148- ]
149- if not assistant_messages:
150- return {
151- "message": None,
152- "metadata": {
153- "json_log_shape": "array",
154- "event_count": len(result),
155- "assistant_message_count": 0,
156- "parse_error": "mini-coder JSON log array had no assistant messages",
157- },
158- }
159-
160- final_message = next(
161- (
162- item
163- for item in reversed(assistant_messages)
164- if item.get("stopReason") in _FINAL_STOP_REASONS
165- ),
166- assistant_messages[-1],
167- )
168- return {
169- "message": final_message,
170- "metadata": {
171- "json_log_shape": "array",
172- "event_count": len(result),
173- "assistant_message_count": len(assistant_messages),
174- },
175- }
176-
177- @staticmethod
178- def _optional_int(value: Any) -> int | None:
179- if isinstance(value, bool):
180- return None
181- if isinstance(value, int):
182- return value
183- if isinstance(value, float) and value.is_integer():
184- return int(value)
185- return None
186-
187- @staticmethod
188- def _optional_float(value: Any) -> float | None:
189- if isinstance(value, bool):
190- return None
191- if isinstance(value, (int, float)):
192- return float(value)
193- return None
194-
195- @staticmethod
196- def _truncate_metadata_text(text: str, max_chars: int = 4000) -> str:
197- if len(text) <= max_chars:
198- return text
199- return text[:max_chars] + " ... [truncated]"
200-
201- def _read_text_excerpt(self, path: Path) -> str:
202- try:
203- return self._truncate_metadata_text(
204- path.read_text(encoding="utf-8", errors="replace").strip()
205- )
206- except OSError as exc:
207- return f"<failed to read {path}: {exc}>"
208-
209- @staticmethod
210- def _merge_context_metadata(
211- context: AgentContext, metadata: dict[str, Any]
212- ) -> None:
213- existing = context.metadata or {}
214- context.metadata = {**existing, **metadata}
215-
216- def _require_host_checkout(self) -> None:
217- missing = [
218- path
219- for path in (
220- self._host_repo_root / "package.json",
221- self._host_repo_root / "bin" / "mc.ts",
222- self._host_repo_root / "src",
223- )
224- if not path.exists()
225- ]
226- if missing:
227- raise FileNotFoundError(
228- "Missing local mini-coder checkout files required for Harbor: "
229- + ", ".join(str(path) for path in missing)
230- )
231-
232- def _require_host_auth(self) -> None:
233- if not self._host_auth_path.is_file():
234- raise FileNotFoundError(
235- f"Missing mini-coder auth file required for Harbor: {self._host_auth_path}"
236- )
237-
238- def _package_local_checkout(self, target_dir: Path) -> Path:
239- target_dir.mkdir(parents=True, exist_ok=True)
240- package_root = target_dir / "mini-coder-package"
241- tarball_path = target_dir / "mini-coder-local.tgz"
242- self._stage_minimal_package(package_root)
243-
244- try:
245- result = subprocess.run(
246- [
247- "bun",
248- "pm",
249- "pack",
250- "--ignore-scripts",
251- "--filename",
252- str(tarball_path),
253- ],
254- cwd=package_root,
255- capture_output=True,
256- text=True,
257- check=False,
258- )
259- except FileNotFoundError as exc:
260- raise RuntimeError("bun is required on the host to package mini-coder") from exc
261-
262- if result.returncode != 0:
263- details = result.stderr.strip() or result.stdout.strip() or "unknown error"
264- raise RuntimeError(f"Failed to package mini-coder: {details}")
265- if not tarball_path.is_file():
266- raise RuntimeError(f"Expected mini-coder package at {tarball_path}")
267-
268- return tarball_path
269-
270- def _stage_minimal_package(self, package_root: Path) -> None:
271- if package_root.exists():
272- shutil.rmtree(package_root)
273- package_root.mkdir(parents=True)
274-
275- package_manifest = self._minimal_package_manifest()
276- (package_root / "package.json").write_text(
277- json.dumps(package_manifest, indent=2) + "\n", encoding="utf-8"
278- )
279-
280- for relative_path in self._PACKAGE_PATHS:
281- source_path = self._host_repo_root / relative_path
282- target_path = package_root / relative_path
283- if source_path.is_dir():
284- shutil.copytree(source_path, target_path)
285- else:
286- target_path.parent.mkdir(parents=True, exist_ok=True)
287- shutil.copy2(source_path, target_path)
288-
289- def _minimal_package_manifest(self) -> dict[str, Any]:
290- manifest_path = self._host_repo_root / "package.json"
291- source_manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
292- if not isinstance(source_manifest, dict):
293- raise RuntimeError(f"Invalid package manifest at {manifest_path}")
294-
295- package_manifest = {
296- key: source_manifest[key]
297- for key in self._PACKAGE_MANIFEST_KEYS
298- if key in source_manifest
299- }
300- missing = [
301- key
302- for key in ("name", "version", "type", "bin")
303- if key not in package_manifest
304- ]
305- if missing:
306- raise RuntimeError(
307- f"Missing required package manifest keys: {', '.join(missing)}"
308- )
309-
310- return package_manifest
311-
312- @staticmethod
313- def _pinned_bun_version(package_manager: Any) -> str | None:
314- if not isinstance(package_manager, str):
315- return None
316- tool_name, _, version = package_manager.partition("@")
317- if tool_name != "bun" or not version:
318- return None
319- if any(not part.isdigit() for part in version.split(".")):
320- return None
321- return version
322-
323- def _expected_bun_version(self) -> str | None:
324- manifest_path = self._host_repo_root / "package.json"
325- source_manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
326- if not isinstance(source_manifest, dict):
327- return None
328- return self._pinned_bun_version(source_manifest.get("packageManager"))
29+ def __init__(self, *args: Any, version: str = "local", **kwargs: Any) -> None:
30+ super().__init__(*args, version=version, **kwargs)
31+ self.repo_root = Path(__file__).resolve().parents[1]
32+ self.host_config_dir = Path.home() / ".config" / "mini-coder"
32933
330- async def _install_system_dependencies(self, environment: BaseEnvironment) -> None:
34+ async def install(self, environment: BaseEnvironment) -> None:
33135 await self.exec_as_root(
33236 environment,
33337 command=(
334- "if command -v apt-get >/dev/null 2>&1; then "
335- "apt-get update && apt-get install -y curl unzip ca-certificates bash; "
336- "elif command -v apk >/dev/null 2>&1; then "
337- "apk add --no-cache curl unzip ca-certificates bash; "
338- "elif command -v yum >/dev/null 2>&1; then "
339- "yum install -y curl unzip ca-certificates bash; "
340- "else echo 'Unsupported package manager for mini-coder install' >&2; exit 1; fi"
38+ "apt-get update && "
39+ "apt-get install -y curl unzip ca-certificates bash"
34140 ),
34241 env={"DEBIAN_FRONTEND": "noninteractive"},
34342 )
34443
345- async def _install_bun(self, environment: BaseEnvironment) -> None:
346- expected_version = self._expected_bun_version()
347- install_command = "curl -fsSL https://bun.sh/install | bash"
348- if expected_version is not None:
349- install_command = (
350- f"curl -fsSL https://bun.sh/install | bash -s -- bun-v{expected_version}"
351- )
352- await self.exec_as_agent(
353- environment,
354- command=(
355- f"expected_version={shlex.quote(expected_version)}; "
356- "current_version=''; "
357- "if command -v bun >/dev/null 2>&1; then "
358- "current_version=$(bun --version 2>/dev/null || true); "
359- "fi; "
360- "if [ -z \"$current_version\" ] || [ \"$current_version\" != \"$expected_version\" ]; then "
361- f"{install_command}; "
362- "fi; "
363- "export PATH=\"$HOME/.bun/bin:$PATH\"; "
364- "test -x \"$(command -v bun)\" && "
365- "[ \"$(bun --version)\" = \"$expected_version\" ]"
366- ),
367- )
368- return
369-
370- await self.exec_as_agent(
371- environment,
372- command=(
373- "if ! command -v bun >/dev/null 2>&1; then "
374- f"{install_command}; fi; "
375- "export PATH=\"$HOME/.bun/bin:$PATH\"; "
376- "test -x \"$(command -v bun)\""
377- ),
378- )
379-
380- async def _install_global_package(
381- self, environment: BaseEnvironment, package_spec: str
382- ) -> None:
38344 await self.exec_as_agent(
38445 environment,
38546 command=(
386- "export PATH=\"$HOME/.bun/bin:$PATH\"; "
387- f"bun add -g --ignore-scripts {shlex.quote(package_spec)} && "
388- "test -x ~/.bun/bin/mc"
47+ 'if ! command -v bun >/dev/null 2>&1; then '
48+ "curl -fsSL https://bun.sh/install | bash; "
49+ "fi"
38950 ),
39051 )
39152
392- async def _container_home(self, environment: BaseEnvironment) -> str:
393- result = await self.exec_as_agent(environment, command='printf %s "$HOME"')
394- home_dir = (result.stdout or "").strip()
395- if not home_dir:
396- raise RuntimeError("Failed to determine the agent home directory")
397- return home_dir
53+ with TemporaryDirectory(prefix="mini-coder-harbor-") as tmp:
54+ tarball = Path(tmp) / "mini-coder-next.tgz"
55+ self._make_source_tarball(tarball)
56+ await environment.upload_file(tarball, "/tmp/mini-coder-next.tgz")
39857
399- async def _container_config_dir(self, environment: BaseEnvironment, home_dir: str) -> str:
400- config_dir = f"{home_dir}/.config/mini-coder"
40158 await self.exec_as_agent(
40259 environment,
40360 command=(
404- f"export HOME={shlex.quote(home_dir)}; "
405- f"mkdir -p {shlex.quote(config_dir)}"
61+ f"rm -rf {shlex.quote(self.CONTAINER_ROOT)} && "
62+ f"mkdir -p {shlex.quote(self.CONTAINER_ROOT)} && "
63+ f"tar -xzf /tmp/mini-coder-next.tgz -C {shlex.quote(self.CONTAINER_ROOT)} && "
64+ 'export PATH="$HOME/.bun/bin:$PATH"; '
65+ f"cd {shlex.quote(self.CONTAINER_ROOT)} && "
66+ "bun install --frozen-lockfile"
40667 ),
40768 )
408- return config_dir
40969
410- async def _upload_host_config_file(
70+ @with_prompt_template
71+ async def run(
41172 self,
73+ instruction: str,
41274 environment: BaseEnvironment,
413- host_path: Path,
414- container_tmp_path: str,
415- target_path: str,
416- *,
417- required: bool,
75+ context: AgentContext,
41876 ) -> None:
419- if not host_path.is_file():
420- if required:
421- raise FileNotFoundError(
422- f"Missing mini-coder config file required for Harbor: {host_path}"
423- )
424- return
77+ del context
78+
79+ home = await self._container_home(environment)
80+ config_dir = f"{home}/.config/mini-coder"
42581
426- await environment.upload_file(host_path, container_tmp_path)
427- await self.exec_as_root(
428- environment, command=f"chmod 644 {shlex.quote(container_tmp_path)}"
429- )
43082 await self.exec_as_agent(
43183 environment,
432- command=(
433- f"cp {shlex.quote(container_tmp_path)} {shlex.quote(target_path)} && "
434- f"chmod 600 {shlex.quote(target_path)}"
435- ),
436- )
437- await self.exec_as_root(
438- environment, command=f"rm -f {shlex.quote(container_tmp_path)}"
84+ command=f"mkdir -p {shlex.quote(config_dir)}",
43985 )
44086
441- async def _upload_host_auth(
442- self, environment: BaseEnvironment, config_dir: str
443- ) -> None:
444- await self._upload_host_config_file(
87+ await self._upload_config_file(
44588 environment,
446- self._host_auth_path,
447- "/tmp/mini-coder-auth.json",
89+ self.host_config_dir / "auth.json",
44890 f"{config_dir}/auth.json",
44991 required=True,
45092 )
451-
452- async def _upload_host_settings(
453- self, environment: BaseEnvironment, config_dir: str
454- ) -> None:
455- await self._upload_host_config_file(
93+ await self._upload_config_file(
45694 environment,
457- self._host_settings_path,
458- "/tmp/mini-coder-settings.json",
95+ self.host_config_dir / "settings.json",
45996 f"{config_dir}/settings.json",
46097 required=False,
46198 )
46299
463- @with_prompt_template
464- async def run(
100+ stdout_path = (EnvironmentPaths.agent_dir / self.STDOUT_LOG).as_posix()
101+ stderr_path = (EnvironmentPaths.agent_dir / self.STDERR_LOG).as_posix()
102+
103+ await self.exec_as_agent(
104+ environment,
105+ command=(
106+ f"export HOME={shlex.quote(home)}; "
107+ f'export PATH="{home}/.bun/bin:$PATH"; '
108+ f"bun {shlex.quote(self.CONTAINER_ROOT + '/bin/mc.ts')} "
109+ f"--json -p {shlex.quote(instruction)} "
110+ f"</dev/null >{shlex.quote(stdout_path)} 2>{shlex.quote(stderr_path)}"
111+ ),
112+ )
113+
114+ def populate_context_post_run(self, context: AgentContext) -> None:
115+ stdout_path = self.logs_dir / self.STDOUT_LOG
116+ stderr_path = self.logs_dir / self.STDERR_LOG
117+
118+ metadata = {
119+ "mini_coder_stdout": str(stdout_path),
120+ "mini_coder_stderr": str(stderr_path),
121+ }
122+
123+ usage = self._extract_usage(stdout_path)
124+ if usage:
125+ context.n_input_tokens = self._as_int(usage.get("input"))
126+ context.n_output_tokens = self._as_int(usage.get("output"))
127+
128+ cost = usage.get("cost")
129+ if isinstance(cost, dict):
130+ context.cost_usd = self._as_float(cost.get("total"))
131+
132+ metadata["usage"] = usage
133+
134+ context.metadata = {**(context.metadata or {}), **metadata}
135+
136+ def _make_source_tarball(self, tarball: Path) -> None:
137+ required = ["package.json", "bun.lock", "bin", "src"]
138+
139+ for name in required:
140+ path = self.repo_root / name
141+ if not path.exists():
142+ raise FileNotFoundError(f"Missing required mini-coder file: {path}")
143+
144+ with tarfile.open(tarball, "w:gz") as tf:
145+ for name in required:
146+ tf.add(self.repo_root / name, arcname=name)
147+
148+ tsconfig = self.repo_root / "tsconfig.json"
149+ if tsconfig.exists():
150+ tf.add(tsconfig, arcname="tsconfig.json")
151+
152+ async def _container_home(self, environment: BaseEnvironment) -> str:
153+ result = await self.exec_as_agent(environment, command='printf %s "$HOME"')
154+ home = (result.stdout or "").strip()
155+ if not home:
156+ raise RuntimeError("Could not determine container HOME")
157+ return home
158+
159+ async def _upload_config_file(
465160 self,
466- instruction: str,
467161 environment: BaseEnvironment,
468- context: AgentContext,
162+ host_path: Path,
163+ container_path: str,
164+ *,
165+ required: bool,
469166 ) -> None:
470- del context
471- self._require_host_auth()
472- home_dir = await self._container_home(environment)
473- config_dir = await self._container_config_dir(environment, home_dir)
474- await self._upload_host_auth(environment, config_dir)
475- await self._upload_host_settings(environment, config_dir)
476-
477- stdout_path = (
478- EnvironmentPaths.agent_dir / self._JSON_LOG_FILENAME
479- ).as_posix()
480- stderr_path = (
481- EnvironmentPaths.agent_dir / self._STDERR_LOG_FILENAME
482- ).as_posix()
483- mc_path = f"{home_dir}/.bun/bin/mc"
167+ if not host_path.exists():
168+ if required:
169+ raise FileNotFoundError(f"Missing mini-coder config file: {host_path}")
170+ return
171+
172+ tmp_path = f"/tmp/{host_path.name}"
173+ await environment.upload_file(host_path, tmp_path)
484174 await self.exec_as_agent(
485175 environment,
486176 command=(
487- f"export HOME={shlex.quote(home_dir)}; "
488- f"export PATH={shlex.quote(f'{home_dir}/.bun/bin')}:$PATH; "
489- f"{shlex.quote(mc_path)} --json -p {shlex.quote(instruction)} </dev/null "
490- f">{shlex.quote(stdout_path)} "
491- f"2>{shlex.quote(stderr_path)}"
177+ f"cp {shlex.quote(tmp_path)} {shlex.quote(container_path)} && "
178+ f"chmod 600 {shlex.quote(container_path)}"
492179 ),
493180 )
181+
182+ @staticmethod
183+ def _extract_usage(path: Path) -> dict[str, Any] | None:
184+ if not path.exists():
185+ return None
186+
187+ # mini-coder --json currently emits newline-delimited JSON events.
188+ for line in reversed(path.read_text(encoding="utf-8", errors="replace").splitlines()):
189+ try:
190+ event = json.loads(line)
191+ except json.JSONDecodeError:
192+ continue
193+
194+ if not isinstance(event, dict):
195+ continue
196+
197+ message = event.get("message")
198+ if isinstance(message, dict) and isinstance(message.get("usage"), dict):
199+ return message["usage"]
200+
201+ if isinstance(event.get("usage"), dict):
202+ return event["usage"]
203+
204+ return None
205+
206+ @staticmethod
207+ def _as_int(value: Any) -> int | None:
208+ return value if isinstance(value, int) and not isinstance(value, bool) else None
209+
210+ @staticmethod
211+ def _as_float(value: Any) -> float | None:
212+ return float(value) if isinstance(value, int | float) and not isinstance(value, bool) else None