motif64

m64_0f5b8210b0a4 / agent_gateway.py

335 lines, 12.8 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} on Strata in this container")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": "Strata 0.2.13, in this container",217        "sandbox": {218            "network": "internal docker network; the only way out is the m64 gate, which "219                       "allows only what the person serving this run granted (by default "220                       "nothing: the model is local) and logs every connection",221            "filesystem": "each session gets a fresh empty directory under the run's scratch",222            "privileges": "non-root, all capabilities dropped, no-new-privileges",223        },224    }225226227@app.post("/v1/sessions", status_code=201)228async def create_session(request: Request):229    body = await _json(request)230    reg = _load_registry()231    d = SESSIONS_DIR / uuid.uuid4().hex[:12]232    d.mkdir(parents=True)233    sid = await harness.create(str(d), body.get("title"))234    reg[sid] = {"id": sid, "title": body.get("title"), "created": time.time(),235                "directory": str(d)}236    _save_registry(reg)237    log.info("SESSION_CREATE %s dir=%s", sid, d)238    return {k: v for k, v in reg[sid].items() if k != "directory"}239240241@app.post("/v1/sessions/{sid}/messages")242async def send_message(sid: str, request: Request):243    body = await _json(request)244    text = body.get("text") or body.get("prompt")245    if not text or not isinstance(text, str):246        raise HTTPException(400, "body needs 'text'")247    s = _session(_load_registry(), sid)248    if body.get("wait", True):249        t0 = time.time()250        reply = await harness.send(sid, s["directory"], text)251        return {**reply, "elapsed_s": round(time.time() - t0, 1)}252    await harness.send_async(sid, s["directory"], text)253    return JSONResponse({"session_id": sid, "status": "busy",254                         "events": f"GET /v1/sessions/{sid}/events"}, status_code=202)255256257@app.post("/v1/sessions/{sid}/abort")258async def abort(sid: str):259    s = _session(_load_registry(), sid)260    await harness.abort(sid, s["directory"])261    return {"aborted": sid}262263264@app.get("/v1/sessions/{sid}/events")265async def events(sid: str):266    s = _session(_load_registry(), sid)267    return StreamingResponse(harness.events(sid, s["directory"]), media_type="text/event-stream",268                             headers={"Cache-Control": "no-cache"})269270271@app.get("/v1/sessions/{sid}/files")272async def get_files(sid: str, path: str = "."):273    s = _session(_load_registry(), sid)274    root = Path(s["directory"])275    p = _safe_path(root, path)276    kind = _kind(p)277    if kind is None:278        raise HTTPException(404, f"{path} not found")279    if kind == "other":280        raise HTTPException(400, f"{path} is not a regular file or directory")281    if kind == "dir":282        entries = []283        for c in sorted(p.iterdir()):284            k = _kind(c) or "other"285            entries.append({"name": c.name, "type": k,286                            "size": c.stat().st_size if k == "file" else None})287        return {"path": str(p.relative_to(root)) if p != root else ".", "entries": entries}288    # The agent may swap the file for a FIFO after the check above: open without289    # blocking and check what was actually opened.290    f = _open_regular(p, os.O_RDONLY)291    size = os.fstat(f.fileno()).st_size292293    def chunks():294        with f:295            while data := f.read(65536):296                yield data297298    headers = {**_DOWNLOAD_HEADERS, "Content-Length": str(size),299               "Content-Disposition": "attachment; filename*=UTF-8''" + quote(p.name)}300    return StreamingResponse(chunks(), media_type=_download_type(p.name), headers=headers)301302303@app.put("/v1/sessions/{sid}/files", status_code=201)304async def put_file(sid: str, request: Request, path: str = ""):305    if not path.strip("/."):306        raise HTTPException(400, "?path= names the file to write")307    s = _session(_load_registry(), sid)308    p = _safe_path(Path(s["directory"]), path)309    if _kind(p) not in (None, "file"):310        raise HTTPException(400, f"{path} exists and is not a regular file")311    try:312        p.parent.mkdir(parents=True, exist_ok=True)313    except (FileExistsError, NotADirectoryError):314        raise HTTPException(400, f"a parent of {path} is not a directory")315    data = await request.body()316    with _open_regular(p, os.O_WRONLY | os.O_CREAT | os.O_TRUNC) as f:317        f.write(data)318    return {"written": path, "bytes": len(data)}319320321async def _json(request: Request) -> dict:322    raw = await request.body()323    if not raw:324        return {}325    try:326        data = json.loads(raw)327    except ValueError:328        raise HTTPException(400, "body must be JSON")329    if not isinstance(data, dict):330        raise HTTPException(400, "body must be a JSON object")331    return data332333334if __name__ == "__main__":335    uvicorn.run(app, host="0.0.0.0", port=PORT, log_level="info")