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.

fix(oracle): durable all-time scored counter + honest stats read

fudjia5486a07863f3
diff --git a/api/_lib/migrations/20260709220000_oracle_lifetime_counters.sql b/api/_lib/migrations/20260709220000_oracle_lifetime_counters.sql
new file mode 100644
index 000000000..5638f2dce
--- /dev/null
+++ b/api/_lib/migrations/20260709220000_oracle_lifetime_counters.sql
@@ -0,0 +1,25 @@
+-- Oracle lifetime counters — durable scored-coin tally.
+--
+-- oracle_conviction is part of the db-retention firehose family (pruned to a
+-- 14-day window, 3 days under storage pressure), so count(*) over it is a
+-- rolling-window count, not "all-time". The /api/oracle/stats scored_total
+-- figure was silently shrinking with every prune. This table holds a monotonic
+-- counter incremented once per first-ever score of a (mint, network) —
+-- retention never touches it.
+
+CREATE TABLE IF NOT EXISTS oracle_counters (
+ network text NOT NULL,
+ key text NOT NULL,
+ value bigint NOT NULL DEFAULT 0,
+ updated_at timestamptz NOT NULL DEFAULT now(),
+ PRIMARY KEY (network, key)
+);
+
+-- Seed from the live cache. The pre-retention history is already gone, so the
+-- current cache count is the best available floor; the counter is monotonic
+-- from here on.
+INSERT INTO oracle_counters (network, key, value)
+SELECT network, 'scored_lifetime', count(*)
+FROM oracle_conviction
+GROUP BY network
+ON CONFLICT (network, key) DO NOTHING;
diff --git a/api/_lib/oracle/store.js b/api/_lib/oracle/store.js
index 9ee03c5e7..2d5835b56 100644
--- a/api/_lib/oracle/store.js
+++ b/api/_lib/oracle/store.js
@@ -35,7 +35,7 @@ export async function upsertConviction({ mint, network, intel, verdict }) {
select score from oracle_conviction where mint = ${mint} and network = ${network} limit 1
`.then((r) => (r[0] ? Number(r[0].score) : null)).catch(() => null);
- await sql`
+ const upserted = await sql`
insert into oracle_conviction (
mint, network, symbol, name, image_uri,
score, tier, pedigree, structure, narrative, momentum, structure_cap,
@@ -57,8 +57,22 @@ export async function upsertConviction({ mint, network, intel, verdict }) {
reasons = excluded.reasons, components = excluded.components,
category = excluded.category, smart_wallet_count = excluded.smart_wallet_count,
scored_at = now()
+ returning (xmax = 0) as inserted
`;
+ // oracle_conviction is retention-pruned, so count(*) over it can never be a
+ // lifetime figure. Bump the durable counter exactly when this was a
+ // first-ever score for the (mint, network). Non-fatal — the counter is a
+ // stat, never worth failing a score over.
+ if (upserted[0]?.inserted) {
+ await sql`
+ insert into oracle_counters (network, key, value)
+ values (${network}, 'scored_lifetime', 1)
+ on conflict (network, key) do update set
+ value = oracle_counters.value + 1, updated_at = now()
+ `.catch(() => { /* counter table may predate migration */ });
+ }
+
// Write a history row when the score changes by ≥3 pts or it's the first score.
const delta = prev == null ? Infinity : Math.abs(verdict.score - prev);
if (delta >= 3) {
diff --git a/api/oracle/stats.js b/api/oracle/stats.js
index 9c792027f..e8436cc0e 100644
--- a/api/oracle/stats.js
+++ b/api/oracle/stats.js
@@ -5,7 +5,9 @@
*
* Returns a fast summary of the oracle engine's current state:
* scored_24h — coins scored in the last 24 h
- * scored_total — all-time scored coins in oracle_conviction
+ * scored_total — all-time scored coins, from the durable oracle_counters
+ * ledger (oracle_conviction itself is retention-pruned, so
+ * its row count is a rolling window, not a lifetime figure)
* prime_count — coins currently sitting at prime tier (score ≥ 86)
* strong_count — coins at strong tier
*
@@ -46,7 +48,7 @@ export default wrap(async (req, res) => {
const params = new URL(req.url, `http://${req.headers.host || 'x'}`).searchParams;
const network = NETWORKS.has(params.get('network')) ? params.get('network') : 'mainnet';
- const [convRow, actRow, outcomeRow, armedRow] = await Promise.all([
+ const [convRow, actRow, outcomeRow, armedRow, counterRow] = await Promise.all([
// Conviction table summary.
sql`
select
@@ -94,12 +96,27 @@ export default wrap(async (req, res) => {
where network = ${network}
and armed = true
`.catch(() => [{}]),
+
+ // Durable lifetime scored counter. oracle_conviction is retention-pruned
+ // (db-retention firehose family), so its count(*) is a rolling-window
+ // figure — this counter is the real all-time number.
+ sql`
+ select value as scored_lifetime
+ from oracle_counters
+ where network = ${network} and key = 'scored_lifetime'
+ `.catch(() => [{}]),
]);
const c = convRow[0] || {};
const a = actRow[0] || {};
const o = outcomeRow[0] || {};
const ar = armedRow[0] || {};
+ const ct = counterRow[0] || {};
+
+ // The counter can briefly trail the live cache (rows seeded before the
+ // counter existed, counter write races) — the true lifetime figure is never
+ // smaller than what is sitting in the cache right now.
+ const scoredTotal = Math.max(Number(ct.scored_lifetime) || 0, Number(c.scored_total) || 0);
const totalResolved = Number(o.total_resolved) || 0;
const totalWins = Number(o.total_wins) || 0;
@@ -111,7 +128,7 @@ export default wrap(async (req, res) => {
return json(res, 200, {
network,
scored_24h: Number(c.scored_24h) || 0,
- scored_total: Number(c.scored_total) || 0,
+ scored_total: scoredTotal,
prime_count: Number(c.prime_count) || 0,
strong_count: Number(c.strong_count) || 0,
open_actions: Number(a.open_actions) || 0,