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
/**
* gpu-capacity — unit tests for the two pure decision points.
*
* The gcloud calls are integration surface, but the YAML retarget and the
* recommendation ranking are where a mistake is expensive: a bad retarget
* either fails the replace or, worse, clones a service on top of its own
* revision history, and a bad ranking sends an agent to file a quota request
* when a region already has idle GPUs sitting unused.
*/
import { describe, it, expect } from 'vitest';
import { retargetExport, recommend, bucketUsage } from '../scripts/gpu-capacity.mjs';
// A trimmed `gcloud run services describe --format=export` body, same shape as
// the real one for a GPU worker.
const EXPORTED = `apiVersion: serving.knative.dev/v1
kind: Service
metadata:
annotations:
run.googleapis.com/ingress: all
run.googleapis.com/urls: '["https://model-triposr-93741856042.us-central1.run.app"]'
labels:
cloud.googleapis.com/location: us-central1
name: model-triposr
spec:
template:
metadata:
annotations:
autoscaling.knative.dev/maxScale: '2'
run.googleapis.com/accelerator: nvidia-l4
name: model-triposr-00011-rpl
spec:
containers:
- image: us-central1-docker.pkg.dev/p/model-triposr/server:manual02
name: server-1
resources:
limits:
nvidia.com/gpu: '1'
`;
describe('retargetExport', () => {
const out = retargetExport(EXPORTED, 'us-central1', 'us-east4');
it('moves the location label to the destination region', () => {
expect(out).toContain('cloud.googleapis.com/location: us-east4');
expect(out).not.toContain('cloud.googleapis.com/location: us-central1');
});
it('drops the read-only per-region URLs annotation', () => {
expect(out).not.toContain('run.googleapis.com/urls');
});
it('drops the pinned revision name so the new region mints its own', () => {
expect(out).not.toContain('model-triposr-00011-rpl');
});
it('keeps the service name, image and GPU request intact', () => {
expect(out).toContain('name: model-triposr');
expect(out).toContain('image: us-central1-docker.pkg.dev/p/model-triposr/server:manual02');
expect(out).toContain("nvidia.com/gpu: '1'");
expect(out).toContain('run.googleapis.com/accelerator: nvidia-l4');
});
it('leaves the source-region image registry alone — images pull cross-region', () => {
expect(out).toContain('us-central1-docker.pkg.dev');
});
it('is a no-op on a body that does not mention the source region', () => {
const untouched = retargetExport('kind: Service\nspec: {}\n', 'us-central1', 'us-east4');
expect(untouched).toBe('kind: Service\nspec: {}\n');
});
});
const region = (over) => ({
region: 'us-central1',
quotas: [],
services: [],
pinned: 0,
ceiling: 0,
granted: null,
headroom: null,
starved: [],
...over,
});
const l4Quota = (over) => ({
quotaId: 'NvidiaL4GpuAllocNoZonalRedundancyPerProjectRegion',
label: 'L4',
granted: 3,
preferred: 3,
reconciling: false,
name: 'l4-pref',
updated: '2026-07-16T00:00:00Z',
...over,
});
describe('recommend', () => {
it('ranks using an already-granted idle GPU above asking for more quota', () => {
const recs = recommend({
project: 'p',
regions: [
region({ region: 'us-central1', quotas: [l4Quota()], granted: 3, pinned: 3, headroom: 0, ceiling: 8, services: [{ name: 'model-triposr', min: 0 }], starved: [{ name: 'model-triposr', min: 0 }] }),
region({ region: 'us-east4', quotas: [l4Quota()], granted: 3, pinned: 0, headroom: 3, ceiling: 2, services: [{ name: 'model-text2motion', min: 0 }] }),
],
});
expect(recs[0].kind).toBe('use-existing-grant');
expect(recs[0].region).toBe('us-east4');
// The starved service is told to move to the region with headroom, not to
// wait behind a quota request.
const starved = recs.find((r) => r.kind === 'starved-service');
expect(starved.service).toBe('model-triposr');
expect(starved.command).toContain('--to us-east4');
});
it('falls back to a quota request when no region has headroom', () => {
const recs = recommend({
project: 'p',
regions: [
region({ granted: 3, pinned: 3, headroom: 0, ceiling: 3, services: [{ name: 'model-triposr', min: 0 }], starved: [{ name: 'model-triposr', min: 0 }], quotas: [l4Quota()] }),
],
});
const starved = recs.find((r) => r.kind === 'starved-service');
expect(starved.command).toContain('--request');
});
it('flags a region with no preference filed as a free thing to try', () => {
const recs = recommend({ project: 'p', regions: [region({ region: 'us-west1' })] });
expect(recs.some((r) => r.kind === 'no-preference-filed' && r.region === 'us-west1')).toBe(true);
});
it('reports a pending grant with its age instead of treating it as capacity', () => {
const recs = recommend({
project: 'p',
regions: [region({ granted: 3, pinned: 1, headroom: 2, ceiling: 3, quotas: [l4Quota({ preferred: 16, reconciling: true })] })],
});
const pending = recs.find((r) => r.kind === 'pending-grant');
expect(pending.detail).toContain('3 → 16');
expect(pending.detail).toMatch(/day\(s\)/);
});
it('warns when the burst ceiling exceeds the grant', () => {
const recs = recommend({
project: 'p',
regions: [region({ granted: 3, pinned: 2, headroom: 1, ceiling: 9, quotas: [l4Quota()] })],
});
const burst = recs.find((r) => r.kind === 'burst-exceeds-grant');
expect(burst.command).toContain('--request 9');
});
it('says nothing when a fully-used region has a settled grant and no starved service', () => {
const recs = recommend({
project: 'p',
regions: [region({ granted: 3, pinned: 3, headroom: 0, ceiling: 3, quotas: [l4Quota()], services: [{ name: 'model-trellis', min: 3 }] })],
});
expect(recs).toEqual([]);
});
});
const L4 = 'NvidiaL4GpuAllocNoZonalRedundancyPerProjectRegion';
const RTX = 'NvidiaRtxPro6000GpuAllocNoZonalRedundancyPerProjectRegion';
describe('bucketUsage', () => {
// The bug this pins: model-hunyuan3d-21-rtx runs on an RTX PRO 6000, but its
// accelerator lives in nodeSelector, not annotations. Reading the wrong place
// defaulted it to L4 and charged it to the shared pool, so us-central1 read
// "3/3 pinned, 0 free" while it actually had a spare L4.
it('charges each service to its own accelerator pool', () => {
const buckets = bucketUsage(
[
{ name: 'model-rig', quotaId: L4, pinned: 1, ceiling: 2 },
{ name: 'model-trellis', quotaId: L4, pinned: 1, ceiling: 3 },
{ name: 'model-hunyuan3d-21-rtx', quotaId: RTX, pinned: 1, ceiling: 1 },
],
[
{ quotaId: L4, granted: 3 },
{ quotaId: RTX, granted: 1000 },
],
);
const l4 = buckets.find((b) => b.quotaId === L4);
expect(l4.pinned).toBe(2);
expect(l4.headroom).toBe(1);
const rtx = buckets.find((b) => b.quotaId === RTX);
expect(rtx.pinned).toBe(1);
expect(rtx.headroom).toBe(999);
});
it('surfaces a filed grant with nothing running in it — that is the free capacity', () => {
const buckets = bucketUsage([], [{ quotaId: L4, granted: 8 }]);
expect(buckets).toHaveLength(1);
expect(buckets[0]).toMatchObject({ pinned: 0, granted: 8, headroom: 8 });
});
it('reports headroom as null when no grant has been filed for a pool in use', () => {
const buckets = bucketUsage([{ name: 'x', quotaId: L4, pinned: 1, ceiling: 1 }], []);
expect(buckets[0].granted).toBeNull();
expect(buckets[0].headroom).toBeNull();
});
it('ignores a service on an accelerator with no known quota bucket', () => {
const buckets = bucketUsage([{ name: 'x', quotaId: null, pinned: 1, ceiling: 1 }], []);
expect(buckets).toEqual([]);
});
});