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
fix(regen): poll a job on the provider that ran it, not the current primary
nirholas4af1f867b3d6
diff --git a/api/_lib/regen-provider.js b/api/_lib/regen-provider.jsindex 9e20a841c..986e04e50 100644--- a/api/_lib/regen-provider.js+++ b/api/_lib/regen-provider.js@@ -290,9 +290,29 @@ export async function getRegenProviderCandidates() {export async function getRegenProviderForJob(jobProvider, req) {if (!jobProvider) return { name: 'none', instance: null };- // Platform providers: use the existing env-credential path.+ // Platform providers: load the adapter for the provider THIS job was+ // submitted to, not whichever one currently leads the precedence order.+ // getRegenProvider() returns the primary, so the moment a second platform+ // credential is added (REPLICATE_API_TOKEN sits above gcp in+ // PLATFORM_PROVIDER_ORDER) every in-flight gcp job would be polled with the+ // Replicate adapter, handed a base64 gcp envelope as a prediction id. The+ // poll then reports a failure that has nothing to do with the real job, and+ // the capture the user is waiting on dies on a provider that never ran it.+ // The reconstruct/auto-rig sweeps already refuse to poll across a provider+ // mismatch; this makes the browser poll agree with them.if (['replicate', 'gcp', 'huggingface', 'hf'].includes(jobProvider)) {- return getRegenProvider();+ const name = canonicalProviderName(jobProvider);+ // Credentials rotated away since submission: report the job's own+ // provider with no instance so the caller leaves the row untouched and+ // the sweep ages it out with an honest reason, rather than polling a+ // different backend for an id it has never seen.+ if (!platformProviderConfigured(name)) return { name, instance: null };+ try {+ return await loadPlatformProvider(name);+ } catch (err) {+ console.warn('[regen] job provider load failed:', name, '-', err?.message);+ return { name, instance: null };+ }}// BYOK providers: re-resolve the user's stored key at poll time. The keydiff --git a/api/_providers/livepeer.js b/api/_providers/livepeer.jsindex 08b21f34f..80e1f6ba4 100644--- a/api/_providers/livepeer.js+++ b/api/_providers/livepeer.js@@ -84,6 +84,22 @@ export function livepeerFederationEnabled() {return truthy(process.env.LIVEPEER_FEDERATION_ENABLED);}+// Node's fetch reports every transport failure as the opaque message "fetch+// failed" and hides the real reason on err.cause. That distinction is the whole+// diagnosis for a federated lane: a certificate that belongs to somebody else+// (the public gateway's current state) is a different problem, with a different+// fix, than a refused connection or a DNS miss. Flatten the cause chain into+// the message so a bench report and a production log line both name the actual+// fault instead of "fetch failed".+function describeTransportFailure(err) {+ const parts = [];+ for (let cur = err, depth = 0; cur && depth < 5; cur = cur.cause, depth++) {+ const piece = [cur.code, cur.message].filter(Boolean).join(' ');+ if (piece && !parts.includes(piece)) parts.push(piece);+ }+ return parts.join(': ') || 'unknown transport failure';+}+// Resolve a possibly gateway-relative image URL against the gateway origin.function resolveImageUrl(raw, base) {if (typeof raw !== 'string' || !raw) return null;@@ -130,7 +146,7 @@ export async function livepeerTextToImage(prompt, { aspectRatio = '1:1', seed, msignal: AbortSignal.timeout(T2I_TIMEOUT_MS),});} catch (err) {- throw Object.assign(new Error(`livepeer gateway unreachable: ${err?.message}`), {+ throw Object.assign(new Error(`livepeer gateway unreachable: ${describeTransportFailure(err)}`), {code: 'provider_unreachable',});}@@ -164,7 +180,7 @@ export async function livepeerTextToImage(prompt, { aspectRatio = '1:1', seed, msignal: AbortSignal.timeout(T2I_TIMEOUT_MS),});} catch (err) {- throw Object.assign(new Error(`livepeer image fetch failed: ${err?.message}`), {+ throw Object.assign(new Error(`livepeer image fetch failed: ${describeTransportFailure(err)}`), {code: 'provider_unreachable',});}diff --git a/scripts/livepeer-federation-bench.mjs b/scripts/livepeer-federation-bench.mjsindex 7874ed4a1..f550cbc1f 100644--- a/scripts/livepeer-federation-bench.mjs+++ b/scripts/livepeer-federation-bench.mjs@@ -90,11 +90,15 @@ async function timed(fn) {} catch (err) {const msg = String(err?.message || err);let failureClass = 'error';- if (err?.code === 'provider_unreachable') failureClass = /certificate|tls|altname/i.test(msg) ? 'tls' : 'unreachable';+ // The TLS test reads the flattened cause chain the adapter now builds+ // (describeTransportFailure): Node's own fetch message is the useless+ // "fetch failed", so matching on it alone could never surface a+ // certificate fault, which is exactly the failure the public gateway has.+ if (err?.code === 'provider_unreachable') failureClass = /certificate|tls|altname|ERR_TLS/i.test(msg) ? 'tls' : 'unreachable';else if (err?.code === 'rate_limited') failureClass = 'rate_limited';else if (err?.code === 'verification_failed') failureClass = 'verification_failed';else if (err?.providerStatus) failureClass = `http_${err.providerStatus}`;- else if (/unconfigured/.test(msg)) failureClass = 'unconfigured';+ else if (/unconfigured|not configured/i.test(msg)) failureClass = 'unconfigured';return { ok: false, latencyMs: Math.round(performance.now() - t0), error: msg.slice(0, 240), failureClass };}}diff --git a/tests/api/regen-provider-job-routing.test.js b/tests/api/regen-provider-job-routing.test.jsnew file mode 100644index 000000000..05ede8b9a--- /dev/null+++ b/tests/api/regen-provider-job-routing.test.js@@ -0,0 +1,114 @@+// getRegenProviderForJob has to resolve the adapter for the provider a job was+// SUBMITTED to. The regenerate-status endpoint (the poll a /scan capture rides+// from submission to a stored rigged GLB) calls it with `job.provider` straight+// off the row, then hands the row's `ext_job_id` to whatever comes back.+//+// That id is provider-shaped: the gcp adapter packs a base64url envelope+// carrying the worker base URL and its task id, Replicate stores a bare+// prediction id. Resolve the wrong adapter and the poll asks the wrong backend+// about an id it has never issued, so a capture that is generating perfectly+// well reports a failure from a service that never ran it.+//+// The regression this pins is a configuration change, not a code change: with+// only GCP_RECONSTRUCTION_URL set, the primary and the job provider are the+// same and nothing looks wrong. Add REPLICATE_API_TOKEN (it outranks gcp in the+// platform precedence order) and every gcp job in flight starts being polled as+// a Replicate prediction.++import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';++const gcpInstance = { kind: 'gcp', supportsMode: () => true, submit: async () => ({}), status: async () => ({}) };+const replicateInstance = { kind: 'replicate', supportsMode: () => true, submit: async () => ({}), status: async () => ({}) };+const hfInstance = { kind: 'huggingface', supportsMode: () => true, submit: async () => ({}), status: async () => ({}) };++vi.mock('../../api/_providers/gcp.js', () => ({ createRegenProvider: () => gcpInstance }));+vi.mock('../../api/_providers/replicate.js', () => ({ createRegenProvider: () => replicateInstance }));+vi.mock('../../api/_providers/huggingface.js', () => ({ createRegenProvider: () => hfInstance }));++const ENV_KEYS = [+ 'AVATAR_REGEN_PROVIDER',+ 'REPLICATE_API_TOKEN',+ 'GCP_RECONSTRUCTION_URL',+ 'GCP_RECONSTRUCTION_KEY',+ 'HF_TOKEN',+];+const saved = {};++beforeEach(() => {+ for (const k of ENV_KEYS) {+ saved[k] = process.env[k];+ delete process.env[k];+ }+ vi.resetModules();+});++afterEach(() => {+ for (const k of ENV_KEYS) {+ if (saved[k] === undefined) delete process.env[k];+ else process.env[k] = saved[k];+ }+});++async function load() {+ return import('../../api/_lib/regen-provider.js');+}++describe('getRegenProviderForJob: platform jobs poll their own provider', () => {+ it('resolves gcp for a gcp job even when Replicate outranks it as primary', async () => {+ process.env.GCP_RECONSTRUCTION_URL = 'https://avatar-reconstruction.example.run.app';+ process.env.GCP_RECONSTRUCTION_KEY = 'k';+ process.env.REPLICATE_API_TOKEN = 'r';++ const { getRegenProviderForJob, resolveProviderName } = await load();+ // Precondition: the primary really is the other provider, so a+ // primary-based resolution would be observably wrong.+ expect(resolveProviderName()).toBe('replicate');++ const resolved = await getRegenProviderForJob('gcp', {});+ expect(resolved.name).toBe('gcp');+ expect(resolved.instance).toBe(gcpInstance);+ });++ it('resolves replicate for a replicate job when gcp is the only other credential', async () => {+ process.env.GCP_RECONSTRUCTION_URL = 'https://avatar-reconstruction.example.run.app';+ process.env.GCP_RECONSTRUCTION_KEY = 'k';+ process.env.REPLICATE_API_TOKEN = 'r';+ process.env.AVATAR_REGEN_PROVIDER = 'gcp';++ const { getRegenProviderForJob } = await load();+ const resolved = await getRegenProviderForJob('replicate', {});+ expect(resolved.name).toBe('replicate');+ expect(resolved.instance).toBe(replicateInstance);+ });++ it('canonicalizes the legacy "hf" job provider to huggingface', async () => {+ process.env.HF_TOKEN = 'h';++ const { getRegenProviderForJob } = await load();+ const resolved = await getRegenProviderForJob('hf', {});+ expect(resolved.name).toBe('huggingface');+ expect(resolved.instance).toBe(hfInstance);+ });++ it('returns no instance when the job provider lost its credentials', async () => {+ // gcp job, but only Replicate is credentialed now. Polling it as a+ // Replicate prediction is worse than not polling: the status endpoint+ // leaves the row alone and reconstruct-sweep ages it out with a reason+ // that names the real provider.+ process.env.REPLICATE_API_TOKEN = 'r';++ const { getRegenProviderForJob } = await load();+ const resolved = await getRegenProviderForJob('gcp', {});+ expect(resolved.name).toBe('gcp');+ expect(resolved.instance).toBeNull();+ });++ it('returns no instance for an unknown provider name', async () => {+ process.env.GCP_RECONSTRUCTION_URL = 'https://avatar-reconstruction.example.run.app';+ process.env.GCP_RECONSTRUCTION_KEY = 'k';++ const { getRegenProviderForJob } = await load();+ const resolved = await getRegenProviderForJob('some-retired-vendor', {});+ expect(resolved.instance).toBeNull();+ });+});