motif64

m64_91e58280e886 / cli_shim.py

490 lines, 20.4 KBraw
1"""CLI shim — gives a command-line agent harness a small HTTP API, from inside2its sandbox.34opencode ships an HTTP server; hermes, pi and openclaw are driven here through5their own non-interactive CLI instead, one process per prompt. That keeps each6session in its own working directory and home, relies only on each tool's most7stable interface (the documented one-shot mode + its "continue" flag), and needs8nothing but the Python stdlib.910Copied from gx10 ~/code/vlmtest/agents/shim/cli_shim.py. In the m64 capsule it listens on11$SHIM_HOST:$SHIM_PORT (127.0.0.1:4096): only the agent gateway in the same container calls it.1213  GET    /health14  POST   /sessions                 {id, directory}   create home + harness config15  GET    /sessions/{id}            {busy}16  DELETE /sessions/{id}17  POST   /sessions/{id}/prompt     {text}            blocks; {text, tools, exit_code, ...}18  GET    /sessions/{id}/messages   transcript19  GET    /sessions/{id}/events     SSE: raw harness output lines + turn start/end20  POST   /sessions/{id}/abort      kill the running turn21"""22import json23import os24import queue25import shutil26import signal27import subprocess28import threading29import time30from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer31from pathlib import Path3233HARNESS = os.environ["AGENT_HARNESS"]34MODEL = os.environ["AGENT_MODEL"]35LLM_BASE = os.environ.get("LLM_BASE_URL", "http://llm:9000/v1")36HOST = os.environ.get("SHIM_HOST", "127.0.0.1")37PORT = int(os.environ.get("SHIM_PORT", "4096"))38HOMES = Path(os.environ.get("SHIM_HOMES", "/work/.homes"))39# 0 = no limit: a turn runs until the harness finishes it (the caller can still40# POST /sessions/{id}/abort). Long agentic tasks outlast any fixed cap.41TURN_TIMEOUT = int(os.environ.get("TURN_TIMEOUT", "0"))42# The LLM's max-model-len; harnesses budget their context against it.43CONTEXT = int(os.environ.get("AGENT_CONTEXT", "131072"))44# The reply cap and pi's compaction match the airbench checkup harness (agent-checkup-benchmark45# checkup.py output_cap / compaction_reserve): at 262144 tokens, 32768 out and a 65536-token reserve,46# so one picture cannot overflow the window.47MAX_OUT = int(os.environ.get("AGENT_MAX_OUT", min(16384, CONTEXT // 8)))48COMPACTION_RESERVE = int(os.environ.get("AGENT_COMPACTION_RESERVE", "0"))49# Every agent LLM is a vision model; a harness told "text" only refuses to pass50# images on even though the server takes them.51INPUT = ["text", "image"] if os.environ.get("AGENT_VISION", "1") == "1" else ["text"]525354# ── Harness adapters ──────────────────────────────────────────────────────────55# Each: setup(home, directory) writes config; command(...) -> (argv, env);56# parse(lines) -> (text, tools).5758class Pi:59    binary = "pi"6061    def setup(self, home: Path, directory: str) -> None:62        agent_dir = home / "pi-agent"63        agent_dir.mkdir(parents=True, exist_ok=True)64        (agent_dir / "models.json").write_text(json.dumps({"providers": {"gx10": {65            "baseUrl": LLM_BASE, "api": "openai-completions", "apiKey": "sk-local",66            # vLLM rejects the `developer` role and reasoning_effort (the same compat as the checkup harness).67            "compat": {"supportsDeveloperRole": False, "supportsReasoningEffort": False},68            "models": [{"id": MODEL, "reasoning": True, "input": INPUT, "contextWindow": CONTEXT,69                        "maxTokens": MAX_OUT}]}}}, indent=2))70        (agent_dir / "settings.json").write_text(json.dumps(71            {"defaultProvider": "gx10", "defaultModel": MODEL, "quietStartup": True,72             **({"compaction": {"enabled": True, "reserveTokens": COMPACTION_RESERVE, "keepRecentTokens": 20000}}73                if COMPACTION_RESERVE else {})}))7475    def command(self, home: Path, directory: str, text: str, first: bool, sid: str):76        argv = ["pi", "-p", "--mode", "json", "--provider", "gx10", "--model", MODEL]77        if not first:78            argv.append("--continue")79        argv.append(text)80        env = {"PI_CODING_AGENT_DIR": str(home / "pi-agent"), "PI_OFFLINE": "1",81               "PI_TELEMETRY": "0"}82        return argv, env8384    def parse(self, lines: list[str]):85        text, tools, open_calls = "", [], {}86        for line in lines:87            try:88                ev = json.loads(line)89            except ValueError:90                continue91            t = ev.get("type")92            if t == "tool_execution_start":93                call = {"tool": ev.get("toolName"), "status": "running", "input": ev.get("args"),94                        "output": ""}95                open_calls[ev.get("toolCallId")] = call96                tools.append(call)97            elif t == "tool_execution_end":98                call = open_calls.get(ev.get("toolCallId"))99                if call:100                    call["status"] = "error" if ev.get("isError") else "completed"101                    call["output"] = _content_text(ev.get("result"))[:4000]102            elif t == "message_end" and (ev.get("message") or {}).get("role") == "assistant":103                t_ = _content_text((ev["message"] or {}).get("content"))104                if t_.strip():105                    text = t_106        return text, tools107108109class Hermes:110    binary = "hermes"111112    def setup(self, home: Path, directory: str) -> None:113        h = home / "hermes"114        h.mkdir(parents=True, exist_ok=True)115        (h / "config.yaml").write_text(116            "model:\n"117            f'  default: "{MODEL}"\n'118            '  provider: "custom"\n'119            f'  base_url: "{LLM_BASE}"\n'120            '  api_key: "sk-local"\n'121            '  api_mode: "chat_completions"\n'122            # Its terminal tool otherwise starts in HOME, not the session directory.123            "terminal:\n"124            f'  cwd: "{directory}"\n')125126    def command(self, home: Path, directory: str, text: str, first: bool, sid: str):127        argv = ["hermes", "-z", text, "--provider", "custom", "-m", MODEL, "--yolo",128                "--in", directory]129        if not first:130            argv.append("--continue")131        return argv, {"HERMES_HOME": str(home / "hermes")}132133    def parse(self, lines: list[str]):134        # -z prints only the final response.135        return "\n".join(lines).strip(), []136137138class OpenClaw:139    binary = "openclaw"140141    def setup(self, home: Path, directory: str) -> None:142        state = home / "openclaw"143        state.mkdir(parents=True, exist_ok=True)144        (state / "openclaw.json").write_text(json.dumps({145            "models": {"providers": {"vllm": {146                "baseUrl": LLM_BASE, "apiKey": "sk-local", "api": "openai-completions",147                "timeoutSeconds": 900,148                "models": [{"id": MODEL, "name": MODEL, "reasoning": True, "input": INPUT,149                            "cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0},150                            "contextWindow": CONTEXT, "maxTokens": MAX_OUT}]}}},151            "agents": {"defaults": {"model": {"primary": f"vllm/{MODEL}"},152                                    "workspace": directory}},153            **json.loads(os.environ.get("OPENCLAW_EXTRA_CONFIG", "{}")),154        }, indent=2))155156    def command(self, home: Path, directory: str, text: str, first: bool, sid: str):157        state = home / "openclaw"158        argv = ["openclaw", "agent", "--local", "--session-id", sid, "--message", text, "--json"]159        return argv, {"OPENCLAW_STATE_DIR": str(state),160                      "OPENCLAW_CONFIG_PATH": str(state / "openclaw.json")}161162    def parse(self, lines: list[str]):163        raw = "\n".join(lines)164        start = raw.find("{")165        try:166            doc = json.loads(raw[start:]) if start >= 0 else {}167        except ValueError:168            return raw.strip(), []169        payloads = doc.get("payloads") or (doc.get("result") or {}).get("payloads") or []170        text = "\n".join(p.get("text", "") for p in payloads if isinstance(p, dict)).strip()171        return text or raw.strip(), []172173174class Dsh:175    """DeepSeek Harness (`dsh --profile headless`). The LLM route is a --patch overlay176    on the shipped headless profile: one pi-ai openai-completions provider at the177    gateway, set as the default model. Approvals off via DSH_PERMISSION_MODE (the178    container is the sandbox). Everything else stays the profile's own default."""179    binary = "dsh"180181    def setup(self, home: Path, directory: str) -> None:182        d = home / "dsh"183        d.mkdir(parents=True, exist_ok=True)184        (d / "gx10.patch.yml").write_text(185            "- id: llm-pi-ai\n"186            "  config:\n"187            "    providers:\n"188            "      gx10:\n"189            "        api: openai-completions\n"190            f"        baseURL: {LLM_BASE}\n"191            "        apiKeyEnv: GX10_LLM_KEY\n"192            "        models:\n"193            f"          - id: {MODEL}\n"194            f"            contextWindow: {CONTEXT}\n"195            f"            input: [{', '.join(INPUT)}]\n"196            "- id: agent-default-model\n"197            "  config:\n"198            "    provider: gx10\n"199            f"    model: {MODEL}\n")200201    def command(self, home: Path, directory: str, text: str, first: bool, sid: str):202        d = home / "dsh"203        argv = ["dsh", "--profile", "headless", "--patch", str(d / "gx10.patch.yml"), "--json"]204        if not first:205            # Follow-ups adopt this session's persisted dsh Session (newest under DSH_HOME).206            ids = sorted((p for p in (d / "home" / "sessions").rglob("session-*")),207                         key=lambda p: p.stat().st_mtime)208            if ids:209                argv += ["--session-id", ids[-1].name.split(".")[0]]210        argv.append(text)211        # The gateway holds any real key; the route only needs a non-empty credential.212        return argv, {"DSH_HOME": str(d / "home"), "GX10_LLM_KEY": "sk-local",213                      "DSH_PERMISSION_MODE": "danger-full-access"}214215    def parse(self, lines: list[str]):216        text, tools, calls = "", [], {}217        for line in lines:218            try:219                ev = json.loads(line)220            except ValueError:221                continue222            t = ev.get("type")223            if t == "tool_call":224                call = {"tool": ev.get("tool"), "status": "running", "input": ev.get("input"), "output": ""}225                calls[ev.get("callId")] = call226                tools.append(call)227            elif t == "tool_result":228                call = calls.get(ev.get("callId"))229                if call:230                    r = ev.get("result")231                    call["status"] = ev.get("status") or "completed"232                    call["output"] = (r if isinstance(r, str) else _content_text(r))[:4000]233            elif t == "final":234                text = ev.get("text") or text235        return text, tools236237238ADAPTERS = {"pi": Pi, "hermes": Hermes, "openclaw": OpenClaw, "dsh": Dsh}239adapter = ADAPTERS[HARNESS]()240241242def _content_text(content) -> str:243    if isinstance(content, str):244        return content245    if isinstance(content, dict):246        return _content_text(content.get("content"))247    if isinstance(content, list):248        return "".join(c.get("text", "") for c in content if isinstance(c, dict)249                       and c.get("type") in (None, "text"))250    return ""251252253# ── Command watchdog ─────────────────────────────────────────────────────────254# A tool command that never ends (an infinite loop in the agent's own script) would hold the turn until the255# benchmark's clock runs out: the harness has no default tool timeout. Every WATCH_EVERY seconds, any process256# the harness started that is older than TOOL_TIMEOUT_S is killed with its whole subtree; the harness then257# reports the failed command to the model, which can fix it or move on. 0 turns the watchdog off.258TOOL_TIMEOUT_S = int(os.environ.get("TOOL_TIMEOUT_S", "600"))259WATCH_EVERY = 20260261262def _proc_table() -> dict:263    """{pid: (ppid, start_ticks)} for every process."""264    out = {}265    for d in os.listdir("/proc"):266        if not d.isdigit():267            continue268        try:269            f = open(f"/proc/{d}/stat").read().rsplit(")", 1)[1].split()270            out[int(d)] = (int(f[1]), int(f[19]))271        except Exception:272            pass273    return out274275276def _subtree(table: dict, root: int) -> list:277    kids, todo = [], [root]278    while todo:279        p = todo.pop()280        for c, (pp, _) in table.items():281            if pp == p:282                kids.append(c)283                todo.append(c)284    return kids285286287def watch_tools(harness_pid: int, alive, emit) -> None:288    """Kill the harness's child commands (and their subtrees) once they are older than TOOL_TIMEOUT_S."""289    hz = os.sysconf("SC_CLK_TCK")290    while alive():291        time.sleep(WATCH_EVERY)292        try:293            uptime = float(open("/proc/uptime").read().split()[0])294            table = _proc_table()295            for child, (ppid, start) in list(table.items()):296                if ppid != harness_pid:297                    continue298                age = uptime - start / hz299                if age < TOOL_TIMEOUT_S:300                    continue301                victims = _subtree(table, child) + [child]302                for v in reversed(victims):303                    try:304                        os.kill(v, signal.SIGKILL)305                    except ProcessLookupError:306                        pass307                emit({"type": "tool.killed", "pid": child, "age_s": round(age), "timeout_s": TOOL_TIMEOUT_S})308        except Exception:309            pass310311312# ── Sessions ──────────────────────────────────────────────────────────────────313314class Session:315    def __init__(self, sid: str, directory: str):316        self.id, self.directory = sid, directory317        self.home = HOMES / sid318        self.proc: subprocess.Popen | None = None319        self.turns = 0320        self.subscribers: list[queue.Queue] = []321        self.lock = threading.Lock()322        self.transcript_path = self.home / "transcript.json"323324    def emit(self, ev: dict) -> None:325        for q in list(self.subscribers):326            q.put(ev)327328    def transcript(self) -> list:329        try:330            return json.loads(self.transcript_path.read_text())331        except Exception:332            return []333334    def record(self, entry: dict) -> None:335        t = self.transcript()336        t.append(entry)337        self.transcript_path.write_text(json.dumps(t, indent=1))338339    def prompt(self, text: str) -> dict:340        if not self.lock.acquire(blocking=False):341            return {"error": "session busy", "status": 409}342        try:343            argv, extra_env = adapter.command(self.home, self.directory, text,344                                              self.turns == 0, self.id)345            env = {**os.environ, "HOME": str(self.home), **extra_env}346            self.record({"role": "user", "text": text, "time": time.time()})347            self.emit({"type": "turn.start", "text": text})348            t0 = time.time()349            self.proc = subprocess.Popen(argv, cwd=self.directory, env=env, text=True,350                                         stdout=subprocess.PIPE, stderr=subprocess.PIPE,351                                         start_new_session=True)352            err_lines: list[str] = []353            threading.Thread(target=lambda: err_lines.extend(self.proc.stderr), daemon=True).start()354            if TOOL_TIMEOUT_S > 0:355                proc = self.proc356                threading.Thread(target=watch_tools, args=(proc.pid, lambda: proc.poll() is None, self.emit),357                                 daemon=True).start()358            lines = []359            timer = threading.Timer(TURN_TIMEOUT, self.abort) if TURN_TIMEOUT > 0 else None360            if timer:361                timer.start()362            for line in self.proc.stdout:363                line = line.rstrip("\n")364                lines.append(line)365                self.emit({"type": "output", "line": line})366            code = self.proc.wait()367            if timer:368                timer.cancel()369            reply, tools = adapter.parse(lines)370            out = {"role": "assistant", "text": reply, "tools": tools, "exit_code": code,371                   "elapsed_s": round(time.time() - t0, 1), "time": time.time()}372            if code != 0:373                out["error"] = "".join(err_lines)[-2000:] or f"exit {code}"374            self.turns += 1375            self.record(out)376            self.emit({"type": "turn.end", **out})377            return out378        finally:379            self.proc = None380            self.lock.release()381382    def abort(self) -> None:383        p = self.proc384        if p and p.poll() is None:385            os.killpg(p.pid, signal.SIGTERM)386387388SESSIONS: dict[str, Session] = {}389390391class H(BaseHTTPRequestHandler):392    protocol_version = "HTTP/1.1"393394    def log_message(self, *a):395        pass396397    def _send(self, code: int, obj) -> None:398        body = json.dumps(obj).encode()399        self.send_response(code)400        self.send_header("Content-Type", "application/json")401        self.send_header("Content-Length", str(len(body)))402        self.end_headers()403        self.wfile.write(body)404405    def _body(self) -> dict:406        n = int(self.headers.get("Content-Length") or 0)407        return json.loads(self.rfile.read(n) or b"{}") if n else {}408409    def _session(self, sid: str) -> Session | None:410        s = SESSIONS.get(sid)411        if not s:412            self._send(404, {"error": f"unknown session {sid}"})413        return s414415    def do_GET(self):416        parts = self.path.split("?")[0].strip("/").split("/")417        if parts == ["health"]:418            ok = shutil.which(adapter.binary) is not None419            return self._send(200 if ok else 503, {"ok": ok, "harness": HARNESS})420        if len(parts) >= 2 and parts[0] == "sessions":421            s = self._session(parts[1])422            if not s:423                return424            if len(parts) == 2:425                return self._send(200, {"id": s.id, "busy": s.proc is not None, "turns": s.turns})426            if parts[2] == "messages":427                return self._send(200, {"messages": s.transcript()})428            if parts[2] == "events":429                return self._events(s)430        self._send(404, {"error": "not found"})431432    def _events(self, s: Session) -> None:433        q: queue.Queue = queue.Queue()434        s.subscribers.append(q)435        self.send_response(200)436        self.send_header("Content-Type", "text/event-stream")437        self.send_header("Cache-Control", "no-cache")438        self.end_headers()439        try:440            self.wfile.write(b'data: {"type": "server.connected"}\n\n')441            self.wfile.flush()442            while True:443                try:444                    ev = q.get(timeout=15)445                    self.wfile.write(f"data: {json.dumps(ev)}\n\n".encode())446                except queue.Empty:447                    self.wfile.write(b": keepalive\n\n")448                self.wfile.flush()449        except (BrokenPipeError, ConnectionResetError):450            pass451        finally:452            s.subscribers.remove(q)453454    def do_POST(self):455        parts = self.path.split("?")[0].strip("/").split("/")456        body = self._body()457        if parts == ["sessions"]:458            sid, directory = body["id"], body["directory"]459            s = Session(sid, directory)460            s.home.mkdir(parents=True, exist_ok=True)461            adapter.setup(s.home, directory)462            SESSIONS[sid] = s463            return self._send(201, {"id": sid})464        if len(parts) == 3 and parts[0] == "sessions":465            s = self._session(parts[1])466            if not s:467                return468            if parts[2] == "prompt":469                out = s.prompt(body.get("text", ""))470                return self._send(out.pop("status", 200) if "status" in out else 200, out)471            if parts[2] == "abort":472                s.abort()473                return self._send(200, {"aborted": s.id})474        self._send(404, {"error": "not found"})475476    def do_DELETE(self):477        parts = self.path.strip("/").split("/")478        if len(parts) == 2 and parts[0] == "sessions":479            s = SESSIONS.pop(parts[1], None)480            if s:481                s.abort()482                shutil.rmtree(s.home, ignore_errors=True)483            return self._send(200, {"deleted": parts[1]})484        self._send(404, {"error": "not found"})485486487if __name__ == "__main__":488    HOMES.mkdir(parents=True, exist_ok=True)489    print(f"[cli-shim] {HARNESS} ({MODEL}) on {HOST}:{PORT}", flush=True)490    ThreadingHTTPServer((HOST, PORT), H).serve_forever()