|
| 1 | +import os |
| 2 | +import shlex |
| 3 | +import shutil |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from harbor.agents.installed.base import BaseInstalledAgent, with_prompt_template |
| 7 | +from harbor.environments.base import BaseEnvironment |
| 8 | +from harbor.models.agent.context import AgentContext |
| 9 | + |
| 10 | + |
| 11 | +REPO_ROOT = Path(__file__).resolve().parents[2] |
| 12 | +CONTAINER_REPO_DIR = "/installed-agent/ouroboros" |
| 13 | +AGENT_LOG_NAME = "ouroboros.txt" |
| 14 | +AGENT_STDOUT_NAME = "ouroboros-stdout.txt" |
| 15 | +AGENT_STDERR_NAME = "ouroboros-stderr.txt" |
| 16 | + |
| 17 | + |
| 18 | +def _env_or_default(name: str, default: str) -> str: |
| 19 | + value = os.environ.get(name) |
| 20 | + return value if value and value.strip() else default |
| 21 | + |
| 22 | + |
| 23 | +class OuroborosInstalledAgent(BaseInstalledAgent): |
| 24 | + """Harbor installed-agent adapter for running Ouroboros CLI on TB 2.0.""" |
| 25 | + |
| 26 | + SUPPORTS_ATIF = False |
| 27 | + |
| 28 | + @staticmethod |
| 29 | + def name() -> str: |
| 30 | + return "ouroboros" |
| 31 | + |
| 32 | + def get_version_command(self) -> str | None: |
| 33 | + return ( |
| 34 | + f"cd {shlex.quote(CONTAINER_REPO_DIR)} && " |
| 35 | + "./packages/cli/dist/ouroboros --version" |
| 36 | + ) |
| 37 | + |
| 38 | + async def install(self, environment: BaseEnvironment) -> None: |
| 39 | + await self.exec_as_root( |
| 40 | + environment, |
| 41 | + command=( |
| 42 | + "if command -v apk >/dev/null 2>&1; then " |
| 43 | + "apk add --no-cache bash curl unzip ca-certificates tar; " |
| 44 | + "elif command -v apt-get >/dev/null 2>&1; then " |
| 45 | + "apt-get update && apt-get install -y " |
| 46 | + "bash curl unzip ca-certificates tar; " |
| 47 | + "elif command -v yum >/dev/null 2>&1; then " |
| 48 | + "yum install -y bash curl unzip ca-certificates tar; " |
| 49 | + "else " |
| 50 | + "echo 'No supported package manager found; assuming prerequisites exist' >&2; " |
| 51 | + "fi" |
| 52 | + ), |
| 53 | + env={"DEBIAN_FRONTEND": "noninteractive"}, |
| 54 | + timeout_sec=300, |
| 55 | + ) |
| 56 | + |
| 57 | + await self.exec_as_agent( |
| 58 | + environment, |
| 59 | + command=( |
| 60 | + "if ! command -v bun >/dev/null 2>&1; then " |
| 61 | + "curl -fsSL https://bun.sh/install | bash; " |
| 62 | + "fi" |
| 63 | + ), |
| 64 | + timeout_sec=300, |
| 65 | + ) |
| 66 | + |
| 67 | + upload_dir = self._prepare_repo_upload() |
| 68 | + await environment.upload_dir(upload_dir, CONTAINER_REPO_DIR) |
| 69 | + |
| 70 | + await self.exec_as_agent( |
| 71 | + environment, |
| 72 | + command=( |
| 73 | + 'export BUN_INSTALL="$HOME/.bun"; ' |
| 74 | + 'export PATH="$BUN_INSTALL/bin:$PATH"; ' |
| 75 | + "bun install && bun run --filter @ouroboros/cli build" |
| 76 | + ), |
| 77 | + cwd=CONTAINER_REPO_DIR, |
| 78 | + timeout_sec=900, |
| 79 | + ) |
| 80 | + |
| 81 | + @with_prompt_template |
| 82 | + async def run( |
| 83 | + self, instruction: str, environment: BaseEnvironment, context: AgentContext |
| 84 | + ) -> None: |
| 85 | + model = _env_or_default("OUROBOROS_TBENCH_MODEL", "openai/gpt-5.5") |
| 86 | + reasoning = _env_or_default("OUROBOROS_TBENCH_REASONING", "medium") |
| 87 | + max_steps = _env_or_default("OUROBOROS_TBENCH_MAX_STEPS", "50") |
| 88 | + |
| 89 | + env = { |
| 90 | + "OPENAI_API_KEY": os.environ.get("OPENAI_API_KEY", ""), |
| 91 | + "OUROBOROS_TBENCH_MODEL": model, |
| 92 | + "OUROBOROS_TBENCH_REASONING": reasoning, |
| 93 | + "OUROBOROS_TBENCH_MAX_STEPS": max_steps, |
| 94 | + } |
| 95 | + env = {key: value for key, value in env.items() if value} |
| 96 | + |
| 97 | + command = ( |
| 98 | + "mkdir -p /logs/agent && " |
| 99 | + 'export BUN_INSTALL="$HOME/.bun"; ' |
| 100 | + 'export PATH="$BUN_INSTALL/bin:$PATH"; ' |
| 101 | + f"{shlex.quote(CONTAINER_REPO_DIR)}/packages/cli/dist/ouroboros " |
| 102 | + f"--model {shlex.quote(model)} " |
| 103 | + f"--reasoning-effort {shlex.quote(reasoning)} " |
| 104 | + "--no-stream --no-rsi " |
| 105 | + f"--max-steps {shlex.quote(max_steps)} " |
| 106 | + f"-m {shlex.quote(instruction)} " |
| 107 | + f"> /logs/agent/{AGENT_STDOUT_NAME} " |
| 108 | + f"2> /logs/agent/{AGENT_STDERR_NAME}; " |
| 109 | + "status=$?; " |
| 110 | + f"cat /logs/agent/{AGENT_STDOUT_NAME} " |
| 111 | + f"/logs/agent/{AGENT_STDERR_NAME} > /logs/agent/{AGENT_LOG_NAME}; " |
| 112 | + "exit $status" |
| 113 | + ) |
| 114 | + |
| 115 | + await self.exec_as_agent( |
| 116 | + environment, |
| 117 | + command=command, |
| 118 | + env=env, |
| 119 | + timeout_sec=int(_env_or_default("OUROBOROS_TBENCH_TIMEOUT_SEC", "3600")), |
| 120 | + ) |
| 121 | + |
| 122 | + def populate_context_post_run(self, context: AgentContext) -> None: |
| 123 | + log_path = self.logs_dir / AGENT_LOG_NAME |
| 124 | + stdout_path = self.logs_dir / AGENT_STDOUT_NAME |
| 125 | + stderr_path = self.logs_dir / AGENT_STDERR_NAME |
| 126 | + |
| 127 | + context.metadata = { |
| 128 | + "agent": self.name(), |
| 129 | + "model": _env_or_default("OUROBOROS_TBENCH_MODEL", "openai/gpt-5.5"), |
| 130 | + "reasoning_effort": _env_or_default("OUROBOROS_TBENCH_REASONING", "medium"), |
| 131 | + "max_steps": _env_or_default("OUROBOROS_TBENCH_MAX_STEPS", "50"), |
| 132 | + "log_path": str(log_path), |
| 133 | + "stdout_path": str(stdout_path), |
| 134 | + "stderr_path": str(stderr_path), |
| 135 | + "log_excerpt": self._read_excerpt(log_path), |
| 136 | + "stdout_excerpt": self._read_excerpt(stdout_path), |
| 137 | + "stderr_excerpt": self._read_excerpt(stderr_path), |
| 138 | + } |
| 139 | + |
| 140 | + def _prepare_repo_upload(self) -> Path: |
| 141 | + target = self.logs_dir / "repo-upload" |
| 142 | + if target.exists(): |
| 143 | + shutil.rmtree(target) |
| 144 | + |
| 145 | + ignore = shutil.ignore_patterns( |
| 146 | + ".git", |
| 147 | + ".DS_Store", |
| 148 | + "node_modules", |
| 149 | + "dist", |
| 150 | + "out", |
| 151 | + "coverage", |
| 152 | + ".cache", |
| 153 | + ".turbo", |
| 154 | + "tmp", |
| 155 | + "logs", |
| 156 | + "*.log", |
| 157 | + ".ouroboros-transcripts.db", |
| 158 | + ) |
| 159 | + shutil.copytree(REPO_ROOT, target, ignore=ignore) |
| 160 | + return target |
| 161 | + |
| 162 | + def _read_excerpt(self, path: Path, limit: int = 4000) -> str | None: |
| 163 | + if not path.exists(): |
| 164 | + return None |
| 165 | + |
| 166 | + text = path.read_text(errors="replace") |
| 167 | + if len(text) <= limit: |
| 168 | + return text |
| 169 | + return text[:limit] + "\n...[truncated]" |
0 commit comments