Download anycode_agent.py from incadawr/anycode-terminal-bench-2-1: direct link, hf CLI and curl.
- Browser
- Download file 6.85 kB
-
https://huggingface.co/datasets/incadawr/anycode-terminal-bench-2-1/resolve/main/anycode_agent.py
- Command line
-
hf download hf://datasets/incadawr/anycode-terminal-bench-2-1/anycode_agent.py
-
curl -L -o anycode_agent.py https://huggingface.co/datasets/incadawr/anycode-terminal-bench-2-1/resolve/main/anycode_agent.py
6.85 kB
| """Harbor adapter for the AnyCode CLI (Native engine), modeled on the leaderboard's | |
| HFDeepSeekHarness. Install: nvm Node 22 + `npm install -g <tarball>` (tarball uploaded from the | |
| host). Run: `anycode --yolo --output-format stream-json -p "<instruction>"` (-p takes the NEXT | |
| token as the prompt, so it must come last). The key reaches the container only as an env var of | |
| the exec call; it is never written to a file or log by this adapter. | |
| ANYCODE_ADAPTER_PROMPT_FILE=1 (host env) passes the instruction via stdin from an uploaded file | |
| instead of argv. | |
| Usage: ANYCODE_TARBALL=/abs/path/anycode-cli-X.tgz harbor run ... \ | |
| --agent-import-path anycode_agent:AnyCodeNative | |
| """ | |
| from __future__ import annotations | |
| import contextlib | |
| import json | |
| import os | |
| import shlex | |
| import tempfile | |
| from pathlib import Path | |
| from typing import override | |
| from harbor.agents.installed.base import BaseInstalledAgent, with_prompt_template | |
| from harbor.environments.base import BaseEnvironment | |
| from harbor.models.agent.context import AgentContext | |
| EVENTS = "anycode-events.jsonl" | |
| STDERR = "anycode-stderr.txt" | |
| FORWARD_ENV = ( | |
| "ANYCODE_API_KEY", "ANYCODE_BASE_URL", "ANYCODE_MODEL", "ANYCODE_REASONING_EFFORT", | |
| "ANYCODE_CONTEXT_WINDOW", "ANYCODE_MAX_OUTPUT_TOKENS", "ANYCODE_MAX_TURNS", | |
| "ANYCODE_REASONING_HISTORY", "ANYCODE_PROVIDER_TRANSPORT", "ANYCODE_BACKGROUND_TASKS", "ANYCODE_COMPLETION_CHECK", "ANYCODE_STALL_TIMEOUT_MS", "NODE_DEBUG", | |
| ) | |
| PROMPT_REMOTE = "/installed-agent/prompt.txt" | |
| NODE_VERSION = "v22.23.3" | |
| NVM = "export PATH=$HOME/node/bin:$PATH" # name kept: puts the agent's Node 22 first | |
| # Official Node tarball (linux x64/arm64, musl build on Alpine), 4 attempts; no nvm / git clone. | |
| NODE_INSTALL = ( | |
| '[ -x "$HOME/node/bin/node" ] || { arch=$(uname -m); case $arch in x86_64) arch=x64;; aarch64|arm64) arch=arm64;; esac; ' | |
| f'build=node-{NODE_VERSION}-linux-$arch; ' | |
| 'if ldd --version 2>&1 | grep -qi musl || [ -f /etc/alpine-release ]; then build=$build-musl; fi; ' | |
| 'ok=0; for i in 1 2 3 4; do tmp=$(mktemp -d) && ' | |
| f'curl -fsSL --retry 3 --connect-timeout 20 https://nodejs.org/dist/{NODE_VERSION}/$build.tar.xz | tar -xJ -C "$tmp" && ' | |
| 'rm -rf "$HOME/node" && mv "$tmp/$build" "$HOME/node" && ok=1 && break; sleep $((i*5)); done; [ $ok = 1 ]; }') | |
| class AnyCodeNative(BaseInstalledAgent): | |
| def name() -> str: | |
| return "anycode-native" | |
| def get_version_command(self) -> str | None: | |
| return f"{NVM} && anycode --version" | |
| def parse_version(self, stdout: str) -> str: | |
| return stdout.strip().splitlines()[-1].strip() | |
| async def install(self, environment: BaseEnvironment) -> None: | |
| tarball = Path(os.environ["ANYCODE_TARBALL"]) | |
| await self.ensure_system_dependencies(environment, ("curl", "git", "ripgrep", "xz")) | |
| remote = f"/installed-agent/{tarball.name}" | |
| await environment.upload_file(tarball, remote) | |
| await self.exec_as_root(environment, command=f"chmod 644 {shlex.quote(remote)}") | |
| await self.exec_as_agent(environment, command=( | |
| "set -uo pipefail; " + NODE_INSTALL + | |
| f" && export PATH=$HOME/node/bin:$PATH && npm install -g {shlex.quote(remote)} && anycode --version")) | |
| async def run(self, instruction: str, environment: BaseEnvironment, context: AgentContext) -> None: | |
| env = {k: v for k in FORWARD_ENV if (v := self._get_env(k))} | |
| logs = self.environment_logs_dir | |
| try: | |
| if os.environ.get("ANYCODE_ADAPTER_PROMPT_FILE") == "1": | |
| # Prompt via stdin from a file: keeps the instruction text out of the agent's argv, | |
| # so `pkill -f <word from the task>` inside the task cannot match the agent itself. | |
| with tempfile.NamedTemporaryFile("w", suffix=".txt", delete=False) as fh: | |
| fh.write(instruction) | |
| local = fh.name | |
| try: | |
| await environment.upload_file(Path(local), PROMPT_REMOTE) | |
| finally: | |
| os.unlink(local) | |
| await self.exec_as_root(environment, command=f"chmod 644 {PROMPT_REMOTE}") | |
| prompt_args = f"-p <{PROMPT_REMOTE}" | |
| else: | |
| prompt_args = f"-p {shlex.quote(instruction)} </dev/null" | |
| await self.exec_as_agent(environment, env=env, command=( | |
| f"{NVM} && anycode --yolo --output-format stream-json " | |
| f"{prompt_args} 2>{logs}/{STDERR} | stdbuf -oL tee {logs}/{EVENTS}")) | |
| finally: | |
| # copy session DB/state dir if present (kept small); never fails the trial | |
| with contextlib.suppress(Exception): | |
| await self.exec_as_agent(environment, timeout_sec=60, | |
| command=f"ls -la ~/.anycode > {logs}/anycode-home-ls.txt 2>&1 || true") | |
| def populate_context_post_run(self, context: AgentContext) -> None: | |
| path = self.logs_dir / EVENTS | |
| if not path.exists(): | |
| return | |
| n_in = n_cache = n_out = tool_calls = turns = 0 | |
| result: dict = {} | |
| c_in = c_out = c_ok = c_fail = 0 | |
| for line in path.read_text(errors="replace").splitlines(): | |
| try: | |
| ev = json.loads(line) | |
| except ValueError: | |
| continue | |
| t = ev.get("type") | |
| if t == "finish": | |
| u = ev.get("usage") or {} | |
| n_in += u.get("inputTokens") or 0 | |
| n_cache += u.get("cachedInputTokens") or 0 | |
| n_out += u.get("outputTokens") or 0 | |
| elif t == "context_usage": | |
| for u in ev.get("reasoningCompactorUsage") or []: | |
| c_in += u.get("inputTokens") or 0 | |
| c_out += u.get("outputTokens") or 0 | |
| c_ok += 1 | |
| if ev.get("reasoningCompactorStatus"): | |
| c_fail += 1 | |
| elif t == "tool_call": | |
| tool_calls += 1 | |
| elif t == "turn_end": | |
| turns += 1 | |
| elif t == "result": | |
| result = ev | |
| # main-loop usage; compactor calls are billed separately and added on top | |
| context.n_input_tokens = n_in + c_in | |
| context.n_cache_tokens = n_cache | |
| context.n_output_tokens = n_out + c_out | |
| context.metadata = {"tool_calls": tool_calls, "turns": turns, "result_subtype": result.get("subtype"), | |
| "is_error": result.get("isError"), "model": result.get("model"), | |
| "main_in": n_in, "main_out": n_out, "compactor_in": c_in, "compactor_out": c_out, | |
| "compactor_calls_with_usage": c_ok, "compactor_failed_or_cancelled": c_fail} | |