motif64

m64_dbef96f4996d / agent_gateway.py

336 lines, 12.9 KBraw
1"""Agent gateway for a CLI-harness capsule (pi, hermes, openclaw, dsh).23Adapted from gx10 ~/code/vlmtest/agents/gateway/agent_gateway.py (its ShimHarness), the same4way as the opencode-dsflash-openrouter-gb10 capsule's gateway: m64 owns the network side5(internal network, the gate as the only egress, the OpenRouter key added by the gate), so6this file keeps just the harness-neutral control API. It drives cli_shim.py on7127.0.0.1:4096, which runs the harness's one-shot CLI, one process per prompt.89Routes (the agent.session kind; the m64 front proxies only these and /health):1011  GET  /health                          200 when the shim and the LLM relay answer12  GET  /v1/agent                        what this is13  POST /v1/sessions                     new session, fresh empty directory14  POST /v1/sessions/{id}/messages       {"text": ...}; wait=true (default) blocks for the reply15  GET  /v1/sessions/{id}/events         SSE stream of this session's events16  GET  /v1/sessions/{id}/files[?path=]  list a dir / download a file17  PUT  /v1/sessions/{id}/files?path=    upload a file (raw body)18  POST /v1/sessions/{id}/abort          stop the running turn19"""20import asyncio21import json22import logging23import mimetypes24import os25import stat26import time27import uuid28from contextlib import asynccontextmanager29from pathlib import Path30from urllib.parse import quote3132import httpx33import uvicorn34from fastapi import FastAPI, HTTPException, Request35from fastapi.responses import JSONResponse, StreamingResponse3637PORT = int(os.environ.get("M64_PORT", "9500"))38SHIM = os.environ.get("SHIM_URL", "http://127.0.0.1:4096")39RELAY = os.environ.get("RELAY_URL", "http://127.0.0.1:9000")40HARNESS = os.environ["AGENT_HARNESS"]41HARNESS_TITLE = os.environ.get("AGENT_HARNESS_TITLE", HARNESS)42MODEL = os.environ["UPSTREAM_MODEL"]43WORK = Path(os.environ.get("WORK_DIR", "/scratch/work"))44SESSIONS_DIR = WORK / "sessions"45REGISTRY = WORK / ".gateway" / "sessions.json"4647logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")48log = logging.getLogger("agent_gateway")495051# ── Session registry (lives in /scratch, so it goes with the run) ────────────────5253def _load_registry() -> dict:54    try:55        return json.loads(REGISTRY.read_text())56    except Exception:57        return {}585960def _save_registry(reg: dict) -> None:61    REGISTRY.parent.mkdir(parents=True, exist_ok=True)62    tmp = REGISTRY.with_suffix(".tmp")63    tmp.write_text(json.dumps(reg, indent=2))64    tmp.replace(REGISTRY)656667def _session(reg: dict, sid: str) -> dict:68    if sid not in reg:69        raise HTTPException(404, f"unknown session '{sid}'")70    return reg[sid]717273def _safe_path(root: Path, rel: str) -> Path:74    if "\x00" in rel:75        raise HTTPException(400, "invalid path")76    try:77        p = (root / rel.lstrip("/")).resolve()78    except (OSError, ValueError, RuntimeError):  # e.g. a symlink loop79        raise HTTPException(400, "invalid path")80    if p != root.resolve() and root.resolve() not in p.parents:81        raise HTTPException(400, "path escapes the session directory")82    return p838485def _kind(p: Path) -> str | None:86    """"file", "dir", "other" (FIFO, socket, device...), or None if it does not exist."""87    try:88        mode = p.stat().st_mode89    except (FileNotFoundError, NotADirectoryError):90        return None91    except OSError:92        return "other"93    return "file" if stat.S_ISREG(mode) else "dir" if stat.S_ISDIR(mode) else "other"949596# Files are written by the agent, so their bytes are untrusted. They are only ever served97# as downloads, sandboxed, never sniffed, and typed as one of these inert types or bytes.98_INERT_TYPES = {"text/plain", "image/png", "image/jpeg", "image/gif", "image/webp"}99_DOWNLOAD_HEADERS = {"X-Content-Type-Options": "nosniff", "Content-Security-Policy": "sandbox"}100101102def _open_regular(p: Path, flags: int):103    """Open p as a regular file, never blocking on a FIFO or following a final symlink."""104    try:105        fd = os.open(p, flags | os.O_NONBLOCK | os.O_NOFOLLOW, 0o644)106    except OSError:107        raise HTTPException(400, "not a regular file")108    if not stat.S_ISREG(os.fstat(fd).st_mode):109        os.close(fd)110        raise HTTPException(400, "not a regular file")111    os.set_blocking(fd, True)112    return os.fdopen(fd, "rb" if flags == os.O_RDONLY else "wb")113114115def _download_type(name: str) -> str:116    guessed = mimetypes.guess_type(name)[0]117    return guessed if guessed in _INERT_TYPES else "application/octet-stream"118119120# ── CLI-shim translator ─────────────────────────────────────────────────────────121122class Shim:123    """cli_shim.py's API: one session = one home + one working directory; a turn is one124    run of the harness CLI (with its "continue" flag after the first)."""125126    def __init__(self, base: str):127        # trust_env=False: this is loopback; never route it through HTTP(S)_PROXY.128        self.http = httpx.AsyncClient(base_url=base.rstrip("/"),129                                      timeout=httpx.Timeout(30, read=None), trust_env=False)130        self._bg: dict[str, asyncio.Task] = {}131132    async def healthy(self) -> bool:133        try:134            shim = (await self.http.get("/health", timeout=3)).status_code == 200135            relay = (await self.http.get(f"{RELAY}/health", timeout=3)).status_code == 200136            return shim and relay137        except Exception:138            return False139140    async def create(self, directory: str, title: str | None) -> str:141        sid = "ses_" + uuid.uuid4().hex[:20]142        r = await self.http.post("/sessions", json={"id": sid, "directory": directory})143        r.raise_for_status()144        return sid145146    async def send(self, sid: str, directory: str, text: str) -> dict:147        r = await self.http.post(f"/sessions/{sid}/prompt", json={"text": text})148        if r.status_code == 409:149            raise HTTPException(409, "session is busy with another turn")150        r.raise_for_status()151        out = r.json()152        return {"role": "assistant", "text": out.get("text", ""),153                "tools": out.get("tools", []), "exit_code": out.get("exit_code"),154                **({"error": out["error"]} if out.get("error") else {})}155156    async def send_async(self, sid: str, directory: str, text: str) -> None:157        if (t := self._bg.get(sid)) and not t.done():158            raise HTTPException(409, "session is busy with another turn")159160        async def run():161            try:162                await self.send(sid, directory, text)163            except Exception as e:164                log.warning("async turn %s failed: %s", sid, e)165        self._bg[sid] = asyncio.create_task(run())166167    async def abort(self, sid: str, directory: str) -> None:168        await self.http.post(f"/sessions/{sid}/abort")169170    async def events(self, sid: str, directory: str):171        """Yield SSE frames: turn.start, output (one raw harness line each), turn.end."""172        async with self.http.stream("GET", f"/sessions/{sid}/events") as r:173            async for line in r.aiter_lines():174                if line.startswith("data:"):175                    yield line + "\n\n"176177178harness = Shim(SHIM)179180181def _report(phase: str, detail: str) -> None:182    """Append a phase line to m64's status file (if running under m64)."""183    if path := os.environ.get("M64_STATUS"):184        with open(path, "a") as f:185            f.write(json.dumps({"phase": phase, "detail": detail}) + "\n")186187188async def _report_ready_when_up() -> None:189    while not await harness.healthy():190        await asyncio.sleep(1)191    _report("ready", f"{HARNESS} up, model {MODEL} via OpenRouter")192193194@asynccontextmanager195async def _lifespan(app: FastAPI):196    SESSIONS_DIR.mkdir(parents=True, exist_ok=True)197    task = asyncio.create_task(_report_ready_when_up())198    yield199    task.cancel()200201202app = FastAPI(title=f"{HARNESS} agent gateway", docs_url=None, redoc_url=None,203              openapi_url=None, lifespan=_lifespan)204205206@app.get("/health")207async def health():208    ok = await harness.healthy()209    return JSONResponse({"status": "ok" if ok else "starting", "harness": HARNESS},210                        status_code=200 if ok else 503)211212213@app.get("/v1/agent")214def describe():215    return {216        "harness": HARNESS_TITLE, "llm": MODEL, "llm_provider": "OpenRouter",217        "sandbox": {218            "network": "internal docker network; the only way out is the m64 gate, which "219                       "allows what this run was granted (it needs openrouter.ai:443, so the "220                       "public internet; never the LAN unless granted), adds the OpenRouter key "221                       "itself (the agent never holds it) and logs every connection",222            "filesystem": "each session gets a fresh empty directory under the run's scratch",223            "privileges": "non-root, all capabilities dropped, no-new-privileges",224        },225    }226227228@app.post("/v1/sessions", status_code=201)229async def create_session(request: Request):230    body = await _json(request)231    reg = _load_registry()232    d = SESSIONS_DIR / uuid.uuid4().hex[:12]233    d.mkdir(parents=True)234    sid = await harness.create(str(d), body.get("title"))235    reg[sid] = {"id": sid, "title": body.get("title"), "created": time.time(),236                "directory": str(d)}237    _save_registry(reg)238    log.info("SESSION_CREATE %s dir=%s", sid, d)239    return {k: v for k, v in reg[sid].items() if k != "directory"}240241242@app.post("/v1/sessions/{sid}/messages")243async def send_message(sid: str, request: Request):244    body = await _json(request)245    text = body.get("text") or body.get("prompt")246    if not text or not isinstance(text, str):247        raise HTTPException(400, "body needs 'text'")248    s = _session(_load_registry(), sid)249    if body.get("wait", True):250        t0 = time.time()251        reply = await harness.send(sid, s["directory"], text)252        return {**reply, "elapsed_s": round(time.time() - t0, 1)}253    await harness.send_async(sid, s["directory"], text)254    return JSONResponse({"session_id": sid, "status": "busy",255                         "events": f"GET /v1/sessions/{sid}/events"}, status_code=202)256257258@app.post("/v1/sessions/{sid}/abort")259async def abort(sid: str):260    s = _session(_load_registry(), sid)261    await harness.abort(sid, s["directory"])262    return {"aborted": sid}263264265@app.get("/v1/sessions/{sid}/events")266async def events(sid: str):267    s = _session(_load_registry(), sid)268    return StreamingResponse(harness.events(sid, s["directory"]), media_type="text/event-stream",269                             headers={"Cache-Control": "no-cache"})270271272@app.get("/v1/sessions/{sid}/files")273async def get_files(sid: str, path: str = "."):274    s = _session(_load_registry(), sid)275    root = Path(s["directory"])276    p = _safe_path(root, path)277    kind = _kind(p)278    if kind is None:279        raise HTTPException(404, f"{path} not found")280    if kind == "other":281        raise HTTPException(400, f"{path} is not a regular file or directory")282    if kind == "dir":283        entries = []284        for c in sorted(p.iterdir()):285            k = _kind(c) or "other"286            entries.append({"name": c.name, "type": k,287                            "size": c.stat().st_size if k == "file" else None})288        return {"path": str(p.relative_to(root)) if p != root else ".", "entries": entries}289    # The agent may swap the file for a FIFO after the check above: open without290    # blocking and check what was actually opened.291    f = _open_regular(p, os.O_RDONLY)292    size = os.fstat(f.fileno()).st_size293294    def chunks():295        with f:296            while data := f.read(65536):297                yield data298299    headers = {**_DOWNLOAD_HEADERS, "Content-Length": str(size),300               "Content-Disposition": "attachment; filename*=UTF-8''" + quote(p.name)}301    return StreamingResponse(chunks(), media_type=_download_type(p.name), headers=headers)302303304@app.put("/v1/sessions/{sid}/files", status_code=201)305async def put_file(sid: str, request: Request, path: str = ""):306    if not path.strip("/."):307        raise HTTPException(400, "?path= names the file to write")308    s = _session(_load_registry(), sid)309    p = _safe_path(Path(s["directory"]), path)310    if _kind(p) not in (None, "file"):311        raise HTTPException(400, f"{path} exists and is not a regular file")312    try:313        p.parent.mkdir(parents=True, exist_ok=True)314    except (FileExistsError, NotADirectoryError):315        raise HTTPException(400, f"a parent of {path} is not a directory")316    data = await request.body()317    with _open_regular(p, os.O_WRONLY | os.O_CREAT | os.O_TRUNC) as f:318        f.write(data)319    return {"written": path, "bytes": len(data)}320321322async def _json(request: Request) -> dict:323    raw = await request.body()324    if not raw:325        return {}326    try:327        data = json.loads(raw)328    except ValueError:329        raise HTTPException(400, "body must be JSON")330    if not isinstance(data, dict):331        raise HTTPException(400, "body must be a JSON object")332    return data333334335if __name__ == "__main__":336    uvicorn.run(app, host="0.0.0.0", port=PORT, log_level="info")