"""Agent gateway for a CLI-harness capsule (pi, hermes, openclaw, dsh). Adapted from gx10 ~/code/vlmtest/agents/gateway/agent_gateway.py (its ShimHarness), the same way as the opencode-dsflash-openrouter-gb10 capsule's gateway: m64 owns the network side (internal network, the gate as the only egress, the OpenRouter key added by the gate), so this file keeps just the harness-neutral control API. It drives cli_shim.py on 127.0.0.1:4096, which runs the harness's one-shot CLI, one process per prompt. Routes (the agent.session kind; the m64 front proxies only these and /health): GET /health 200 when the shim and the LLM relay answer GET /v1/agent what this is POST /v1/sessions new session, fresh empty directory POST /v1/sessions/{id}/messages {"text": ...}; wait=true (default) blocks for the reply GET /v1/sessions/{id}/events SSE stream of this session's events GET /v1/sessions/{id}/files[?path=] list a dir / download a file PUT /v1/sessions/{id}/files?path= upload a file (raw body) POST /v1/sessions/{id}/abort stop the running turn """ import asyncio import json import logging import mimetypes import os import stat import time import uuid from contextlib import asynccontextmanager from pathlib import Path from urllib.parse import quote import httpx import uvicorn from fastapi import FastAPI, HTTPException, Request from fastapi.responses import JSONResponse, StreamingResponse PORT = int(os.environ.get("M64_PORT", "9500")) SHIM = os.environ.get("SHIM_URL", "http://127.0.0.1:4096") RELAY = os.environ.get("RELAY_URL", "http://127.0.0.1:9000") HARNESS = os.environ["AGENT_HARNESS"] HARNESS_TITLE = os.environ.get("AGENT_HARNESS_TITLE", HARNESS) MODEL = os.environ["UPSTREAM_MODEL"] WORK = Path(os.environ.get("WORK_DIR", "/scratch/work")) SESSIONS_DIR = WORK / "sessions" REGISTRY = WORK / ".gateway" / "sessions.json" logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s") log = logging.getLogger("agent_gateway") # ── Session registry (lives in /scratch, so it goes with the run) ──────────────── def _load_registry() -> dict: try: return json.loads(REGISTRY.read_text()) except Exception: return {} def _save_registry(reg: dict) -> None: REGISTRY.parent.mkdir(parents=True, exist_ok=True) tmp = REGISTRY.with_suffix(".tmp") tmp.write_text(json.dumps(reg, indent=2)) tmp.replace(REGISTRY) def _session(reg: dict, sid: str) -> dict: if sid not in reg: raise HTTPException(404, f"unknown session '{sid}'") return reg[sid] def _safe_path(root: Path, rel: str) -> Path: if "\x00" in rel: raise HTTPException(400, "invalid path") try: p = (root / rel.lstrip("/")).resolve() except (OSError, ValueError, RuntimeError): # e.g. a symlink loop raise HTTPException(400, "invalid path") if p != root.resolve() and root.resolve() not in p.parents: raise HTTPException(400, "path escapes the session directory") return p def _kind(p: Path) -> str | None: """"file", "dir", "other" (FIFO, socket, device...), or None if it does not exist.""" try: mode = p.stat().st_mode except (FileNotFoundError, NotADirectoryError): return None except OSError: return "other" return "file" if stat.S_ISREG(mode) else "dir" if stat.S_ISDIR(mode) else "other" # Files are written by the agent, so their bytes are untrusted. They are only ever served # as downloads, sandboxed, never sniffed, and typed as one of these inert types or bytes. _INERT_TYPES = {"text/plain", "image/png", "image/jpeg", "image/gif", "image/webp"} _DOWNLOAD_HEADERS = {"X-Content-Type-Options": "nosniff", "Content-Security-Policy": "sandbox"} def _open_regular(p: Path, flags: int): """Open p as a regular file, never blocking on a FIFO or following a final symlink.""" try: fd = os.open(p, flags | os.O_NONBLOCK | os.O_NOFOLLOW, 0o644) except OSError: raise HTTPException(400, "not a regular file") if not stat.S_ISREG(os.fstat(fd).st_mode): os.close(fd) raise HTTPException(400, "not a regular file") os.set_blocking(fd, True) return os.fdopen(fd, "rb" if flags == os.O_RDONLY else "wb") def _download_type(name: str) -> str: guessed = mimetypes.guess_type(name)[0] return guessed if guessed in _INERT_TYPES else "application/octet-stream" # ── CLI-shim translator ───────────────────────────────────────────────────────── class Shim: """cli_shim.py's API: one session = one home + one working directory; a turn is one run of the harness CLI (with its "continue" flag after the first).""" def __init__(self, base: str): # trust_env=False: this is loopback; never route it through HTTP(S)_PROXY. self.http = httpx.AsyncClient(base_url=base.rstrip("/"), timeout=httpx.Timeout(30, read=None), trust_env=False) self._bg: dict[str, asyncio.Task] = {} async def healthy(self) -> bool: try: shim = (await self.http.get("/health", timeout=3)).status_code == 200 relay = (await self.http.get(f"{RELAY}/health", timeout=3)).status_code == 200 return shim and relay except Exception: return False async def create(self, directory: str, title: str | None) -> str: sid = "ses_" + uuid.uuid4().hex[:20] r = await self.http.post("/sessions", json={"id": sid, "directory": directory}) r.raise_for_status() return sid async def send(self, sid: str, directory: str, text: str) -> dict: r = await self.http.post(f"/sessions/{sid}/prompt", json={"text": text}) if r.status_code == 409: raise HTTPException(409, "session is busy with another turn") r.raise_for_status() out = r.json() return {"role": "assistant", "text": out.get("text", ""), "tools": out.get("tools", []), "exit_code": out.get("exit_code"), **({"error": out["error"]} if out.get("error") else {})} async def send_async(self, sid: str, directory: str, text: str) -> None: if (t := self._bg.get(sid)) and not t.done(): raise HTTPException(409, "session is busy with another turn") async def run(): try: await self.send(sid, directory, text) except Exception as e: log.warning("async turn %s failed: %s", sid, e) self._bg[sid] = asyncio.create_task(run()) async def abort(self, sid: str, directory: str) -> None: await self.http.post(f"/sessions/{sid}/abort") async def events(self, sid: str, directory: str): """Yield SSE frames: turn.start, output (one raw harness line each), turn.end.""" async with self.http.stream("GET", f"/sessions/{sid}/events") as r: async for line in r.aiter_lines(): if line.startswith("data:"): yield line + "\n\n" harness = Shim(SHIM) def _report(phase: str, detail: str) -> None: """Append a phase line to m64's status file (if running under m64).""" if path := os.environ.get("M64_STATUS"): with open(path, "a") as f: f.write(json.dumps({"phase": phase, "detail": detail}) + "\n") async def _report_ready_when_up() -> None: while not await harness.healthy(): await asyncio.sleep(1) _report("ready", f"{HARNESS} up, model {MODEL} on Strata in this container") @asynccontextmanager async def _lifespan(app: FastAPI): SESSIONS_DIR.mkdir(parents=True, exist_ok=True) task = asyncio.create_task(_report_ready_when_up()) yield task.cancel() app = FastAPI(title=f"{HARNESS} agent gateway", docs_url=None, redoc_url=None, openapi_url=None, lifespan=_lifespan) @app.get("/health") async def health(): ok = await harness.healthy() return JSONResponse({"status": "ok" if ok else "starting", "harness": HARNESS}, status_code=200 if ok else 503) @app.get("/v1/agent") def describe(): return { "harness": HARNESS_TITLE, "llm": MODEL, "llm_provider": "Strata 0.2.13, in this container", "sandbox": { "network": "internal docker network; the only way out is the m64 gate, which " "allows only what the person serving this run granted (by default " "nothing: the model is local) and logs every connection", "filesystem": "each session gets a fresh empty directory under the run's scratch", "privileges": "non-root, all capabilities dropped, no-new-privileges", }, } @app.post("/v1/sessions", status_code=201) async def create_session(request: Request): body = await _json(request) reg = _load_registry() d = SESSIONS_DIR / uuid.uuid4().hex[:12] d.mkdir(parents=True) sid = await harness.create(str(d), body.get("title")) reg[sid] = {"id": sid, "title": body.get("title"), "created": time.time(), "directory": str(d)} _save_registry(reg) log.info("SESSION_CREATE %s dir=%s", sid, d) return {k: v for k, v in reg[sid].items() if k != "directory"} @app.post("/v1/sessions/{sid}/messages") async def send_message(sid: str, request: Request): body = await _json(request) text = body.get("text") or body.get("prompt") if not text or not isinstance(text, str): raise HTTPException(400, "body needs 'text'") s = _session(_load_registry(), sid) if body.get("wait", True): t0 = time.time() reply = await harness.send(sid, s["directory"], text) return {**reply, "elapsed_s": round(time.time() - t0, 1)} await harness.send_async(sid, s["directory"], text) return JSONResponse({"session_id": sid, "status": "busy", "events": f"GET /v1/sessions/{sid}/events"}, status_code=202) @app.post("/v1/sessions/{sid}/abort") async def abort(sid: str): s = _session(_load_registry(), sid) await harness.abort(sid, s["directory"]) return {"aborted": sid} @app.get("/v1/sessions/{sid}/events") async def events(sid: str): s = _session(_load_registry(), sid) return StreamingResponse(harness.events(sid, s["directory"]), media_type="text/event-stream", headers={"Cache-Control": "no-cache"}) @app.get("/v1/sessions/{sid}/files") async def get_files(sid: str, path: str = "."): s = _session(_load_registry(), sid) root = Path(s["directory"]) p = _safe_path(root, path) kind = _kind(p) if kind is None: raise HTTPException(404, f"{path} not found") if kind == "other": raise HTTPException(400, f"{path} is not a regular file or directory") if kind == "dir": entries = [] for c in sorted(p.iterdir()): k = _kind(c) or "other" entries.append({"name": c.name, "type": k, "size": c.stat().st_size if k == "file" else None}) return {"path": str(p.relative_to(root)) if p != root else ".", "entries": entries} # The agent may swap the file for a FIFO after the check above: open without # blocking and check what was actually opened. f = _open_regular(p, os.O_RDONLY) size = os.fstat(f.fileno()).st_size def chunks(): with f: while data := f.read(65536): yield data headers = {**_DOWNLOAD_HEADERS, "Content-Length": str(size), "Content-Disposition": "attachment; filename*=UTF-8''" + quote(p.name)} return StreamingResponse(chunks(), media_type=_download_type(p.name), headers=headers) @app.put("/v1/sessions/{sid}/files", status_code=201) async def put_file(sid: str, request: Request, path: str = ""): if not path.strip("/."): raise HTTPException(400, "?path= names the file to write") s = _session(_load_registry(), sid) p = _safe_path(Path(s["directory"]), path) if _kind(p) not in (None, "file"): raise HTTPException(400, f"{path} exists and is not a regular file") try: p.parent.mkdir(parents=True, exist_ok=True) except (FileExistsError, NotADirectoryError): raise HTTPException(400, f"a parent of {path} is not a directory") data = await request.body() with _open_regular(p, os.O_WRONLY | os.O_CREAT | os.O_TRUNC) as f: f.write(data) return {"written": path, "bytes": len(data)} async def _json(request: Request) -> dict: raw = await request.body() if not raw: return {} try: data = json.loads(raw) except ValueError: raise HTTPException(400, "body must be JSON") if not isinstance(data, dict): raise HTTPException(400, "body must be a JSON object") return data if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=PORT, log_level="info")