Skip to content

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.

feat(forge): add a Tripo-lane completion rate and latency report

nirholas25081411931f
diff --git a/api/_lib/tripo-lane-report.js b/api/_lib/tripo-lane-report.js
new file mode 100644
index 000000000..137b0e4c9
--- /dev/null
+++ b/api/_lib/tripo-lane-report.js
@@ -0,0 +1,118 @@
+// @ts-check
+// Tripo-lane health: completion rate and median latency by day, for the
+// lane work order 084-partners-01-tripo-v3-house-lane.md (task 6) asked be
+// reportable on its own, separately from the all-backend forge-funnel.js
+// numbers. Scoped to backend = 'tripo' rows only (both the BYOK path and the
+// TRIPO_API_KEY house-key path write that same backend value).
+//
+// Read-only. Every query is a SELECT.
+
+import { sql } from './db.js';
+
+export const TRIPO_LANE_MIN_DAYS = 1;
+export const TRIPO_LANE_MAX_DAYS = 365;
+export const TRIPO_LANE_DEFAULT_DAYS = 30;
+
+/** Clamp a caller-supplied window to a whole number of days in range. */
+export function clampTripoLaneDays(value) {
+ if (value == null || value === '') return TRIPO_LANE_DEFAULT_DAYS;
+ const n = Math.floor(Number(value));
+ if (!Number.isFinite(n)) return TRIPO_LANE_DEFAULT_DAYS;
+ return Math.min(TRIPO_LANE_MAX_DAYS, Math.max(TRIPO_LANE_MIN_DAYS, n));
+}
+
+function int(v) {
+ const n = Number(v);
+ return Number.isFinite(n) ? Math.trunc(n) : 0;
+}
+
+function round1(v) {
+ const n = Number(v);
+ return Number.isFinite(n) ? Math.round(n * 10) / 10 : null;
+}
+
+/**
+ * One row per day: completion rate and median seconds-to-done, from the raw
+ * per-day counts the query below returns. Pure, so it is testable without a
+ * database.
+ *
+ * @param {Array<Record<string, unknown>>} rows
+ */
+export function summarizeTripoLaneDays(rows = []) {
+ return rows.map((r) => {
+ const done = int(r.done);
+ const failed = int(r.failed);
+ const total = int(r.total);
+ const terminal = done + failed;
+ return {
+ day: r.day instanceof Date ? r.day.toISOString().slice(0, 10) : String(r.day).slice(0, 10),
+ total,
+ done,
+ failed,
+ in_flight: Math.max(0, total - terminal),
+ completion_rate: terminal > 0 ? Math.round((done / terminal) * 1000) / 1000 : null,
+ median_latency_seconds: r.median_seconds == null ? null : round1(r.median_seconds),
+ };
+ });
+}
+
+/**
+ * @param {{ days?: number|string }} [opts]
+ */
+export async function gatherTripoLaneReport({ days } = {}) {
+ const windowDays = clampTripoLaneDays(days);
+ try {
+ const rows = await sql`
+ with totals as (
+ select
+ date_trunc('day', created_at) as day,
+ count(*)::int as total,
+ count(*) filter (where status = 'done')::int as done,
+ count(*) filter (where status = 'failed')::int as failed
+ from forge_creations
+ where backend = 'tripo'
+ and created_at > now() - make_interval(days => ${windowDays})
+ group by 1
+ ),
+ latency as (
+ select
+ date_trunc('day', created_at) as day,
+ percentile_cont(0.5) within group (
+ order by extract(epoch from (updated_at - created_at))
+ ) as median_seconds
+ from forge_creations
+ where backend = 'tripo' and status = 'done'
+ and created_at > now() - make_interval(days => ${windowDays})
+ group by 1
+ )
+ select t.day, t.total, t.done, t.failed, l.median_seconds
+ from totals t
+ left join latency l using (day)
+ order by t.day desc
+ `;
+ const byDay = summarizeTripoLaneDays(rows);
+ const totalDone = byDay.reduce((n, d) => n + d.done, 0);
+ const totalFailed = byDay.reduce((n, d) => n + d.failed, 0);
+ const totalTerminal = totalDone + totalFailed;
+ const allLatencies = byDay.map((d) => d.median_latency_seconds).filter((v) => v != null);
+ return {
+ ok: true,
+ window_days: windowDays,
+ total: byDay.reduce((n, d) => n + d.total, 0),
+ done: totalDone,
+ failed: totalFailed,
+ completion_rate: totalTerminal > 0 ? Math.round((totalDone / totalTerminal) * 1000) / 1000 : null,
+ // Median of the daily medians, not a second pass over every row: cheap,
+ // and stable against one noisy day dominating the window summary.
+ median_latency_seconds:
+ allLatencies.length === 0
+ ? null
+ : round1(
+ [...allLatencies].sort((a, b) => a - b)[Math.floor((allLatencies.length - 1) / 2)],
+ ),
+ by_day: byDay,
+ };
+ } catch (err) {
+ return { ok: false, error: err?.message || String(err), window_days: windowDays, by_day: [] };
+ }
+}
diff --git a/package.json b/package.json
index ee12908d3..925e01085 100644
--- a/package.json
+++ b/package.json
@@ -474,6 +474,7 @@
"db:restamp": "node scripts/apply-migrations.mjs --restamp",
"forge:errors": "node scripts/forge-error-report.mjs",
"forge:funnel": "node scripts/forge-funnel-report.mjs",
+ "forge:tripo": "node scripts/tripo-lane-report.mjs",
"partners:proof": "node scripts/partner-proof-brief.mjs",
"worker:oracle": "node --env-file-if-exists=.env workers/oracle/index.js",
"worker:sniper": "node workers/agent-sniper/index.js",
diff --git a/scripts/tripo-lane-report.mjs b/scripts/tripo-lane-report.mjs
new file mode 100644
index 000000000..71a501484
--- /dev/null
+++ b/scripts/tripo-lane-report.mjs
@@ -0,0 +1,94 @@
+#!/usr/bin/env node
+/**
+ * Tripo-lane report: completion rate and median latency by day, scoped to
+ * backend = 'tripo' (work order 084-partners-01-tripo-v3-house-lane.md,
+ * task 6). `npm run forge:funnel` answers the all-backend product question;
+ * this answers the narrower one the house-key decision needs: is the lane
+ * actually finishing jobs, and how fast.
+ *
+ * Usage:
+ * node scripts/tripo-lane-report.mjs # last 30 days
+ * node scripts/tripo-lane-report.mjs --days 7
+ * node scripts/tripo-lane-report.mjs --json
+ *
+ * Reads DATABASE_URL from .env.local, then .env, then the shell (same order as
+ * scripts/apply-migrations.mjs). Read-only: it runs SELECTs and nothing else.
+ */
+
+import { readFileSync } from 'node:fs';
+import { fileURLToPath } from 'node:url';
+import path from 'node:path';
+
+const HERE = path.dirname(fileURLToPath(import.meta.url));
+const REPO_ROOT = path.resolve(HERE, '..');
+
+for (const envFile of ['.env.local', '.env']) {
+ try {
+ const raw = readFileSync(path.resolve(REPO_ROOT, envFile), 'utf8');
+ for (const line of raw.split('\n')) {
+ const m = line.match(/^([A-Z_][A-Z0-9_]*)=(.*)$/);
+ if (!m || process.env[m[1]]) continue;
+ let val = m[2].trim();
+ if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'"))) {
+ val = val.slice(1, -1);
+ }
+ process.env[m[1]] = val;
+ }
+ } catch { /* file not present */ }
+}
+
+const args = process.argv.slice(2);
+const flag = (name, fallback) => {
+ const i = args.indexOf(`--${name}`);
+ return i >= 0 && args[i + 1] && !args[i + 1].startsWith('--') ? args[i + 1] : fallback;
+};
+const JSON_OUT = args.includes('--json');
+
+if (!process.env.DATABASE_URL) {
+ console.error('DATABASE_URL is not set. Add it to .env.local or export it in your shell.');
+ console.error('Production\'s value: node scripts/read-service-env.mjs \'^DATABASE_URL$\' --raw');
+ process.exit(2);
+}
+
+const { gatherTripoLaneReport } = await import('../api/_lib/tripo-lane-report.js');
+
+const report = await gatherTripoLaneReport({ days: flag('days', undefined) });
+
+if (JSON_OUT) {
+ console.log(JSON.stringify(report, null, 2));
+ process.exit(report.ok ? 0 : 1);
+}
+
+if (!report.ok) {
+ console.error(`Tripo lane report failed: ${report.error}`);
+ process.exit(1);
+}
+
+const pct = (v) => (v == null ? 'n/a' : `${(v * 100).toFixed(1)}%`);
+const secs = (v) => (v == null ? 'n/a' : `${v}s`);
+
+console.log(`\nTripo lane, last ${report.window_days} days`);
+console.log(` total ${report.total} done ${report.done} failed ${report.failed}`);
+console.log(` completion rate ${pct(report.completion_rate)} median latency ${secs(report.median_latency_seconds)}\n`);
+
+if (!report.by_day.length) {
+ console.log(' no Tripo-lane jobs in this window\n');
+ process.exit(0);
+}
+
+const headers = ['day', 'total', 'done', 'failed', 'in flight', 'completion', 'median s'];
+const rows = report.by_day.map((d) => [
+ d.day,
+ d.total,
+ d.done,
+ d.failed,
+ d.in_flight,
+ pct(d.completion_rate),
+ secs(d.median_latency_seconds),
+]);
+const widths = headers.map((h, i) => Math.max(h.length, ...rows.map((r) => String(r[i]).length)));
+const fmt = (r) => r.map((c, i) => (i === 0 ? String(c).padEnd(widths[i]) : String(c).padStart(widths[i]))).join(' ');
+console.log(` ${fmt(headers)}`);
+console.log(` ${widths.map((w) => '-'.repeat(w)).join(' ')}`);
+for (const r of rows) console.log(` ${fmt(r)}`);
+console.log('');
diff --git a/tests/tripo-lane-report.test.js b/tests/tripo-lane-report.test.js
new file mode 100644
index 000000000..0ff85e762
--- /dev/null
+++ b/tests/tripo-lane-report.test.js
@@ -0,0 +1,102 @@
+// Tripo-lane report (work order 084-partners-01-tripo-v3-house-lane.md, task
+// 6): completion rate and median latency by day, scoped to backend = 'tripo'.
+// summarizeTripoLaneDays is pure (tested without a database); gatherTripoLaneReport
+// is covered against a mocked sql so the query's day/done/failed/median shape
+// is pinned without a live Postgres instance.
+
+import { describe, it, expect, vi } from 'vitest';
+import {
+ clampTripoLaneDays,
+ summarizeTripoLaneDays,
+ gatherTripoLaneReport,
+ TRIPO_LANE_DEFAULT_DAYS,
+} from '../api/_lib/tripo-lane-report.js';
+
+describe('clampTripoLaneDays', () => {
+ it('defaults an absent or empty value', () => {
+ expect(clampTripoLaneDays(undefined)).toBe(TRIPO_LANE_DEFAULT_DAYS);
+ expect(clampTripoLaneDays('')).toBe(TRIPO_LANE_DEFAULT_DAYS);
+ });
+
+ it('clamps out-of-range and non-numeric values', () => {
+ expect(clampTripoLaneDays(0)).toBe(1);
+ expect(clampTripoLaneDays(-5)).toBe(1);
+ expect(clampTripoLaneDays(10_000)).toBe(365);
+ expect(clampTripoLaneDays('not a number')).toBe(TRIPO_LANE_DEFAULT_DAYS);
+ });
+
+ it('accepts a valid whole number of days', () => {
+ expect(clampTripoLaneDays(7)).toBe(7);
+ expect(clampTripoLaneDays('14')).toBe(14);
+ });
+});
+
+describe('summarizeTripoLaneDays', () => {
+ it('computes completion rate from done/failed and leaves in-flight out of it', () => {
+ const [row] = summarizeTripoLaneDays([
+ { day: '2026-10-10', total: 10, done: 7, failed: 1, median_seconds: 42.4 },
+ ]);
+ expect(row).toMatchObject({
+ day: '2026-10-10',
+ total: 10,
+ done: 7,
+ failed: 1,
+ in_flight: 2,
+ completion_rate: 0.875,
+ median_latency_seconds: 42.4,
+ });
+ });
+
+ it('reports null completion rate and latency for a day with no terminal rows', () => {
+ const [row] = summarizeTripoLaneDays([{ day: '2026-10-11', total: 3, done: 0, failed: 0, median_seconds: null }]);
+ expect(row.completion_rate).toBeNull();
+ expect(row.median_latency_seconds).toBeNull();
+ expect(row.in_flight).toBe(3);
+ });
+
+ it('normalizes a Date-typed day to YYYY-MM-DD', () => {
+ const [row] = summarizeTripoLaneDays([
+ { day: new Date('2026-10-12T00:00:00Z'), total: 1, done: 1, failed: 0, median_seconds: 10 },
+ ]);
+ expect(row.day).toBe('2026-10-12');
+ });
+});
+
+vi.mock('../api/_lib/db.js', () => ({
+ sql: vi.fn(),
+}));
+
+describe('gatherTripoLaneReport', () => {
+ it('summarizes multi-day rows and fails open on a query error', async () => {
+ const { sql } = await import('../api/_lib/db.js');
+ sql.mockResolvedValueOnce([
+ { day: new Date('2026-10-11T00:00:00Z'), total: 5, done: 4, failed: 1, median_seconds: 30 },
+ { day: new Date('2026-10-10T00:00:00Z'), total: 8, done: 8, failed: 0, median_seconds: 50 },
+ ]);
+ const report = await gatherTripoLaneReport({ days: 7 });
+ expect(report.ok).toBe(true);
+ expect(report.window_days).toBe(7);
+ expect(report.total).toBe(13);
+ expect(report.done).toBe(12);
+ expect(report.failed).toBe(1);
+ expect(report.completion_rate).toBeCloseTo(12 / 13, 3);
+ expect(report.by_day).toHaveLength(2);
+
+ sql.mockRejectedValueOnce(new Error('relation forge_creations does not exist'));
+ const failed = await gatherTripoLaneReport({ days: 7 });
+ expect(failed.ok).toBe(false);
+ expect(failed.error).toMatch(/forge_creations/);
+ expect(failed.by_day).toEqual([]);
+ });
+
+ it('answers an empty, ok report when there are no rows', async () => {
+ const { sql } = await import('../api/_lib/db.js');
+ sql.mockResolvedValueOnce([]);
+ const report = await gatherTripoLaneReport({});
+ expect(report.ok).toBe(true);
+ expect(report.total).toBe(0);
+ expect(report.completion_rate).toBeNull();
+ expect(report.median_latency_seconds).toBeNull();
+ expect(report.by_day).toEqual([]);
+ });
+});