Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
test(model-trellis): prove the served contract inside the image, not in production
nirholasae0a6156625d
diff --git a/workers/model-trellis/Dockerfile b/workers/model-trellis/Dockerfileindex 31a7a9211..ffefd3703 100644--- a/workers/model-trellis/Dockerfile+++ b/workers/model-trellis/Dockerfile@@ -95,12 +95,17 @@ WORKDIR /appCOPY worker_security.py /app/worker_security.pyCOPY request_policy.py /app/request_policy.pyCOPY main.py /app/main.py-COPY test_request_policy.py /app/test_request_policy.py+COPY test_request_policy.py test_app_contract.py /app/-# Build-time gate: the quality tiers, the clamps that keep a caller value from-# crashing the nvdiffrast bake, and the image-fetch retry policy are all pure-# logic, so they are proven here rather than in production. Needs no GPU.-RUN python3 -m pytest /app/test_request_policy.py -q+# Build-time gate, neither half of which needs a GPU:+# 1. The quality tiers, the clamps that keep a caller value from crashing the+# nvdiffrast bake, and the image-fetch retry policy are pure logic, proven+# here rather than in production.+# 2. The served contract, against the exact dependency set that will run: the+# TRELLIS import chain (FlexiCubes submodule included), the auth boundary,+# the probe surfaces, request validation, and the SSRF guard.+RUN python3 -m pytest /app/test_request_policy.py -q \+ && python3 /app/test_app_contract.pyEXPOSE 8080diff --git a/workers/model-trellis/README.md b/workers/model-trellis/README.mdindex ea94d2303..f60254093 100644--- a/workers/model-trellis/README.md+++ b/workers/model-trellis/README.md@@ -216,10 +216,25 @@ python3 -m pip install pytest httpxpython3 -m pytest test_request_policy.py -q # 18 tests```-The same suite runs **inside `docker build`** (see the `RUN python3 -m pytest`-step in the Dockerfile), so a broken tier table or clamp fails the build instead-of a user's generation. The GPU-bound half of the service is covered by the-platform-side integration tests in+[`test_app_contract.py`](./test_app_contract.py) covers the served surface+instead, and needs the image's dependency set (torch, FastAPI, the cloned+TRELLIS tree), so it runs inside the container rather than on your machine:++```bash+docker run --rm model-trellis python3 test_app_contract.py+```++It asserts the TRELLIS import chain resolves (FlexiCubes submodule included, the+packaging bug that only ever surfaced minutes into a doomed revision), the auth+boundary rejects a wrong bearer, `/` and `/health` answer, `images` arrays+outside 1 to 6 entries are refused, and the SSRF guard rejects cleartext,+loopback, and metadata-server sources. No GPU, no weights, no credentials: the+ASGI lifespan is not started.++**Both suites run inside `docker build`** (see the `RUN` gate in the Dockerfile),+so a broken tier table, a bad dependency resolve, or a hole in the auth boundary+fails the build instead of a user's generation. The GPU-bound half of the+service is covered by the platform-side integration tests in[`tests/api/forge-trellis-selfhost.test.js`](../../tests/api/forge-trellis-selfhost.test.js).## Run locallydiff --git a/workers/model-trellis/test_app_contract.py b/workers/model-trellis/test_app_contract.pynew file mode 100644index 000000000..71cf35f39--- /dev/null+++ b/workers/model-trellis/test_app_contract.py@@ -0,0 +1,146 @@+"""Smoke test for the served contract, run INSIDE the built image (it needs+torch, FastAPI, PIL, google-cloud-storage, and the cloned TRELLIS tree on+PYTHONPATH):++ docker build -t model-trellis workers/model-trellis+ docker run --rm model-trellis python3 test_app_contract.py++It needs no GPU, no weights, and no GCP credentials: the ASGI lifespan (which+opens the GCS client and starts the ~3 GB model load) is deliberately not run,+so every assertion below is about request handling, not inference.++What it proves, and why each line earns its place:++ 1. The TRELLIS import chain resolves, including the FlexiCubes git submodule.+ A plain (non-recursive) clone leaves that path a bare gitlink, and the+ failure only surfaces at model load, minutes after the port opens, as a+ revision that answers /health with a load_error and can never generate.+ Importing it here turns that into a build failure.+ 2. The whole app module imports, so a bad dependency resolve cannot reach a+ revision as an opaque Cloud Run startup-probe failure.+ 3. The auth boundary rejects a wrong bearer on both authenticated routes.+ 4. The unauthenticated probe surfaces (/ and /health) answer, which is what+ the keep-warm cron and the platform's routing health read.+ 5. Request validation rejects an empty or oversized images array instead of+ queueing a job that can only fail on the GPU.+ 6. The image decoder accepts a real data URI and refuses everything the SSRF+ guard should refuse, reporting it as an ImageSourceError (the caller-facing+ class) rather than an opaque internal error.+"""++from __future__ import annotations++import base64+import io+import os+import sys++os.environ.setdefault("API_KEY", "smoke-test-key")+os.environ.setdefault("GCS_BUCKET", "smoke-test-bucket")++KEY = os.environ["API_KEY"]++PASSED = 0+++def check(name: str, condition: bool, detail: str = "") -> None:+ global PASSED+ if not condition:+ print(f"FAIL {name} {detail}")+ sys.exit(1)+ PASSED += 1+ print(f"ok {name}")+++def png_data_uri(color=(20, 20, 20, 255), size=(24, 24)) -> str:+ from PIL import Image++ img = Image.new("RGBA", size, color)+ buf = io.BytesIO()+ img.save(buf, format="PNG")+ return "data:image/png;base64," + base64.b64encode(buf.getvalue()).decode()+++def main() -> int:+ # 1. The vendored TRELLIS tree, including the FlexiCubes submodule the mesh+ # extractor imports. A ModuleNotFoundError here is the packaging bug this+ # check exists for; any other exception is a GPU-less builder refusing to+ # initialize a CUDA-backed module, which is not what is under test.+ try:+ from trellis.pipelines import TrellisImageTo3DPipeline # noqa: F401+ from trellis.representations.mesh.flexicubes.flexicubes import FlexiCubes # noqa: F401++ check("trellis import chain resolves", True)+ except ModuleNotFoundError as exc:+ check("trellis import chain resolves", False, str(exc))+ except Exception as exc: # noqa: BLE001 - no GPU on the builder, see above+ print(f"skip trellis import chain (no GPU on this host): {type(exc).__name__}: {exc}")++ # 2. The service module itself.+ import main as app_module+ from fastapi.testclient import TestClient++ client = TestClient(app_module.app)++ # 3. Auth boundary.+ bad = {"Authorization": "Bearer wrong-key"}+ res = client.get("/tasks/does-not-exist", headers=bad)+ check("GET /tasks rejects a wrong bearer", res.status_code == 401, f"got {res.status_code}")+ res = client.post("/infer", headers=bad, json={"images": [png_data_uri()]})+ check("POST /infer rejects a wrong bearer", res.status_code == 401, f"got {res.status_code}")++ # 4. Unauthenticated probe surfaces.+ res = client.get("/health")+ body = res.json()+ check("GET /health answers 200", res.status_code == 200, f"got {res.status_code}")+ check("health names the model", body.get("model") == "trellis-image-large", str(body)[:120])+ check(+ "health lists every tier",+ body.get("tiers") == ["draft", "standard", "high", "max"],+ str(body.get("tiers")),+ )+ check(+ "health publishes the default quality bar",+ body.get("default_quality", {}).get("texture_size") == 4096,+ str(body.get("default_quality")),+ )+ res = client.get("/")+ body = res.json()+ check("GET / answers 200 for the keep-warm ping", res.status_code == 200, f"got {res.status_code}")+ check("root names the service", body.get("service") == "model-trellis", str(body)[:120])+ check("root lists the working endpoints", "POST /infer" in body.get("endpoints", []), str(body)[:160])++ # 5. Request validation, before anything reaches the GPU.+ good = {"Authorization": f"Bearer {KEY}"}+ res = client.post("/infer", headers=good, json={"images": []})+ check("POST /infer rejects an empty images array", res.status_code == 422, f"got {res.status_code}")+ res = client.post("/infer", headers=good, json={"images": [png_data_uri()] * 7})+ check("POST /infer rejects more than six views", res.status_code == 422, f"got {res.status_code}")++ # 6. The image decoder and the SSRF guard, through the real code path.+ img = app_module._decode_image(png_data_uri())+ check("data uri decodes to RGB", img.mode == "RGB" and img.size == (24, 24), f"{img.mode} {img.size}")+ img = app_module._decode_image(png_data_uri(), keep_alpha=True)+ check("matted cutout keeps its alpha mask", img.mode == "RGBA", img.mode)++ for label, src in [+ ("cleartext http", "http://example.com/a.png"),+ ("bare path", "/etc/passwd"),+ ("loopback https", "https://127.0.0.1/a.png"),+ ("cloud metadata", "https://169.254.169.254/latest/meta-data/"),+ ("corrupt data uri", "data:image/png;base64,not-base64!!"),+ ]:+ try:+ app_module._decode_image(src)+ check(f"refuses {label}", False, "no exception raised")+ except app_module.ImageSourceError:+ check(f"refuses {label}", True)+ except Exception as exc: # noqa: BLE001 - must be the caller-facing class+ check(f"refuses {label}", False, f"raised {type(exc).__name__}: {exc}")++ print(f"\n{PASSED} contract assertions passed")+ return 0+++if __name__ == "__main__":+ sys.exit(main())