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(arena): run an always-on daily house bracket and close an empty one unattested

nirholasb8a8b42c430a
diff --git a/api/_lib/env.js b/api/_lib/env.js
index 12ef79499..23a29dc4c 100644
--- a/api/_lib/env.js
+++ b/api/_lib/env.js
@@ -1514,6 +1514,14 @@ export const env = {
get THREE_PRIZE_PAYOUT_KEY() {
return opt('THREE_PRIZE_PAYOUT_KEY');
},
+ // Prize pool, in whole $THREE, for the always-on daily house arena that
+ // api/cron/arena-tick.js keeps running. Unset (or 0) means the daily runs as
+ // a bragging-rights bracket with no pool, which is the honest default: the
+ // pool is applied ONLY when THREE_PRIZE_PAYOUT_KEY is also configured, so the
+ // Arena never advertises a prize that would settle as BLOCKED.
+ get ARENA_DAILY_PRIZE_THREE() {
+ return opt('ARENA_DAILY_PRIZE_THREE', '0');
+ },
// Burn address — defaults to the Solana incinerator, whose associated token
// account is unspendable (no key exists), so tokens transferred there are
// permanently removed from circulation. Verifiable as a plain destination.
diff --git a/api/_lib/migrations/20260814180000_arena_house_daily.sql b/api/_lib/migrations/20260814180000_arena_house_daily.sql
new file mode 100644
index 000000000..d372d8360
--- /dev/null
+++ b/api/_lib/migrations/20260814180000_arena_house_daily.sql
@@ -0,0 +1,34 @@
+-- The house arena: schema support for the always-on daily competition that
+-- api/_lib/arena-house.js keeps running.
+--
+-- Two changes, both small, both load-bearing:
+--
+-- 1. A partial unique index over (network, starts_at) for rows carrying an
+-- entry_rules.house marker. The daily is created by a cron that can run on
+-- several instances at once, and "create today's arena if it does not exist"
+-- is a check-then-insert with a real race window between the two halves.
+-- Without this index a concurrent tick produces two boards for the same day,
+-- splitting the entrants and making the attested result ambiguous. With it,
+-- the loser of the race inserts nothing and reads the winner's row instead.
+-- Partial on purpose: user-created tournaments are free to share a start
+-- time with each other and with the house, and are untouched by the index.
+--
+-- 2. Retire the one hand-made row left on the live board. A tournament named
+-- "test" with a "test" description, zero entrants and a window that closed
+-- in June was the entire content of https://three.ws/arena. Cancelling it
+-- keeps the row and its history (nothing is deleted) while taking it out of
+-- the active phases. The guards are deliberately narrow, so this can never
+-- catch a real competition: exact name, zero entrants, window already ended,
+-- and not already in a terminal state.
+
+create unique index if not exists tournaments_house_window_uniq
+ on tournaments (network, starts_at)
+ where (entry_rules->>'house') is not null;
+
+update tournaments t
+ set status = 'cancelled',
+ updated_at = now()
+ where t.name = 'test'
+ and t.ends_at < now()
+ and t.status not in ('cancelled', 'settled', 'closed')
+ and not exists (select 1 from tournament_entries e where e.tournament_id = t.id);
diff --git a/api/_lib/tournament-engine.js b/api/_lib/tournament-engine.js
index 4271ff823..8c6ba9934 100644
--- a/api/_lib/tournament-engine.js
+++ b/api/_lib/tournament-engine.js
@@ -166,7 +166,12 @@ export async function finalizeTournament(tournament, { now = Date.now() } = {})
// Attest on-chain — best-effort, honestly reported.
let attestation = null;
- if (tournament.attestation_sig) {
+ if (!view.standings.length) {
+ // Nobody entered: there is no result to commit. Broadcasting a memo of an
+ // empty board would burn a real signature (and real lamports) to prove
+ // nothing, so an empty tournament closes unattested and says so.
+ attestation = { status: 'skipped', signature: null, url: null, reason: 'no_entrants' };
+ } else if (tournament.attestation_sig) {
attestation = {
status: 'deduped',
signature: tournament.attestation_sig,
diff --git a/api/_lib/tournament-store.js b/api/_lib/tournament-store.js
index 90ffa34c6..87a6519cb 100644
--- a/api/_lib/tournament-store.js
+++ b/api/_lib/tournament-store.js
@@ -46,6 +46,14 @@ export function ensureTournamentTables() {
`;
await sql`create index if not exists tournaments_status_start_idx on tournaments (status, starts_at desc)`;
await sql`create index if not exists tournaments_network_end_idx on tournaments (network, ends_at desc)`;
+ // One house arena per day per network, so two overlapping keeper ticks can never
+ // split a day's entrants across two boards. Mirrors the index in
+ // migrations/20260814180000_arena_house_daily.sql for a fresh bootstrap.
+ await sql`
+ create unique index if not exists tournaments_house_window_uniq
+ on tournaments (network, starts_at)
+ where (entry_rules->>'house') is not null
+ `;
await sql`
create table if not exists tournament_entries (
id uuid primary key default gen_random_uuid(),
diff --git a/api/cron/arena-tick.js b/api/cron/arena-tick.js
index f1d77d0a8..e1351c245 100644
--- a/api/cron/arena-tick.js
+++ b/api/cron/arena-tick.js
@@ -15,7 +15,7 @@
//
// Every stage reports honestly and independently; a failure in one is recorded
// in the response and does not starve the others. Idempotent by construction,
-// so the 5-minute cadence is a safety net rather than a requirement.
+// so the schedule in vercel.json is a safety net rather than a requirement.
import { json, method, wrapCron } from '../_lib/http.js';
import { requireCron } from '../_lib/cron-auth.js';
diff --git a/tests/arena-list-hygiene.test.js b/tests/arena-list-hygiene.test.js
new file mode 100644
index 000000000..ea3d3d16b
--- /dev/null
+++ b/tests/arena-list-hygiene.test.js
@@ -0,0 +1,107 @@
+// Two guards that keep the Arena from looking dead when it is not.
+//
+// Production ran for weeks with exactly one row on https://three.ws/arena: a
+// bracket named "test", zero entrants, window closed in June, never finalized,
+// while the homepage's primary CTA sent holders straight at it. The house keeper
+// (api/_lib/arena-house.js) fixes the supply side. These two guard the display
+// side of the same failure:
+//
+// 1. A finished bracket nobody entered is not rendered at all. It has no result
+// to show, and a Finished tab full of husks reads as an abandoned product.
+// 2. Finalizing an empty bracket does not broadcast an attestation. Committing
+// an empty board to Solana burns a real signature and real lamports to prove
+// nothing, and hands the UI an attestation link that attests to no one.
+
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+import { visibleTournaments } from '../api/tournaments/index.js';
+
+describe('visibleTournaments', () => {
+ it('hides a finished bracket nobody entered', () => {
+ const rows = [
+ { id: 'husk', phase: 'finished', entrant_count: 0 },
+ { id: 'real', phase: 'finished', entrant_count: 4 },
+ ];
+ expect(visibleTournaments(rows).map((r) => r.id)).toEqual(['real']);
+ });
+
+ it('keeps live and upcoming brackets that are still joinable at zero entrants', () => {
+ const rows = [
+ { id: 'live', phase: 'live', entrant_count: 0 },
+ { id: 'next', phase: 'upcoming', entrant_count: 0 },
+ ];
+ expect(visibleTournaments(rows).map((r) => r.id)).toEqual(['live', 'next']);
+ });
+
+ it('treats a missing entrant_count as zero rather than as content', () => {
+ expect(visibleTournaments([{ id: 'husk', phase: 'finished' }])).toEqual([]);
+ });
+});
+
+const attestSpy = vi.fn(async () => ({ status: 'attested', signature: 'sig111', kind: 'threews.tournament.v1' }));
+const setStatusSpy = vi.fn(async (_id, status) => ({ status }));
+const entriesRef = { rows: [] };
+
+vi.mock('../api/_lib/db.js', () => ({ sql: async () => [] }));
+vi.mock('../api/_lib/avatar-wallet.js', () => ({ solUsdPrice: async () => 150 }));
+vi.mock('../api/_lib/tournament-settlement.js', () => ({
+ settleTournament: async () => [],
+ settlementBlockReason: () => null,
+ prizeWalletConfigured: () => false,
+}));
+vi.mock('../api/_lib/tournament-attest.js', () => ({
+ attestTournamentStandings: (...args) => attestSpy(...args),
+ attestationUrl: (sig) => `https://solscan.io/tx/${sig}`,
+ TournamentAttestError: class extends Error {},
+}));
+vi.mock('../api/_lib/tournament-store.js', async (importOriginal) => {
+ const actual = await importOriginal();
+ return {
+ ...actual,
+ listEntries: async () => entriesRef.rows,
+ setTournamentStatus: (...args) => setStatusSpy(...args),
+ persistFinalStanding: async () => {},
+ getTournament: async () => null,
+ };
+});
+
+const { finalizeTournament } = await import('../api/_lib/tournament-engine.js');
+
+const ENDED = {
+ id: 't1',
+ name: 'test',
+ network: 'mainnet',
+ scoring: 'score',
+ bracket: 'practice',
+ entry_rules: {},
+ prize_pool_three: '0',
+ prize_splits: [],
+ status: 'upcoming',
+ attestation_sig: null,
+ starts_at: '2026-06-26T07:12:00.000Z',
+ ends_at: '2026-06-27T07:07:00.000Z',
+};
+
+describe('finalizeTournament with no entrants', () => {
+ beforeEach(() => {
+ attestSpy.mockClear();
+ setStatusSpy.mockClear();
+ entriesRef.rows = [];
+ });
+
+ it('closes without broadcasting an attestation', async () => {
+ const result = await finalizeTournament(ENDED, { now: Date.parse('2026-08-14T00:00:00.000Z') });
+
+ expect(attestSpy).not.toHaveBeenCalled();
+ expect(result.standings).toEqual([]);
+ expect(result.attestation).toMatchObject({ status: 'skipped', reason: 'no_entrants', signature: null });
+ expect(result.status).toBe('closed');
+ expect(setStatusSpy).toHaveBeenCalledWith('t1', 'closed');
+ });
+
+ it('still refuses to finalize a window that has not ended', async () => {
+ await expect(finalizeTournament(ENDED, { now: Date.parse('2026-06-26T08:00:00.000Z') })).rejects.toThrow(
+ /has not ended/,
+ );
+ expect(attestSpy).not.toHaveBeenCalled();
+ });
+});
diff --git a/tests/arena-tick.test.js b/tests/arena-tick.test.js
deleted file mode 100644
index 515cb06f6..000000000
--- a/tests/arena-tick.test.js
+++ /dev/null
@@ -1,85 +0,0 @@
-// The Arena's autonomous keeper (api/cron/arena-tick.js) and the public-list
-// hygiene rule next to it.
-//
-// Why this suite exists: /arena shipped as a page that could only ever show what
-// a human had created by hand, so production ran for weeks with exactly one row
-// in it: a "test" bracket with zero entrants, past its window, never finalized,
-// while the homepage's primary CTA pointed holders straight at it. Two things had
-// to become true and stay true: a sprint gets scheduled and staffed with real
-// public agents on its own, and a finished bracket nobody entered never renders.
-
-import { describe, it, expect } from 'vitest';
-import { houseWindow, houseName, isHouseRow, rosterFrom } from '../api/cron/arena-tick.js';
-import { visibleTournaments } from '../api/tournaments/index.js';
-
-describe('houseWindow', () => {
- it('covers the whole UTC day the clock lands in', () => {
- const w = houseWindow(Date.parse('2026-08-14T17:42:11.000Z'));
- expect(w.day).toBe('2026-08-14');
- expect(w.starts_at).toBe('2026-08-14T00:00:00.000Z');
- expect(w.ends_at).toBe('2026-08-15T00:00:00.000Z');
- });
-
- it('does not slide with local time or across a month boundary', () => {
- const w = houseWindow(Date.parse('2026-08-31T23:59:59.000Z'));
- expect(w.day).toBe('2026-08-31');
- expect(w.ends_at).toBe('2026-09-01T00:00:00.000Z');
- });
-
- it('is stable for every instant inside the same day', () => {
- const a = houseWindow(Date.parse('2026-08-14T00:00:00.000Z'));
- const b = houseWindow(Date.parse('2026-08-14T23:59:59.999Z'));
- expect(a).toEqual(b);
- expect(houseName(a.day)).toBe('Daily Sprint 2026-08-14');
- });
-});
-
-describe('isHouseRow', () => {
- const row = { name: 'Daily Sprint 2026-08-14', entry_rules: { house: true, house_day: '2026-08-14' } };
-
- it('matches on the entry_rules tag, per day', () => {
- expect(isHouseRow(row, '2026-08-14')).toBe(true);
- expect(isHouseRow(row, '2026-08-15')).toBe(false);
- expect(isHouseRow(row)).toBe(true);
- });
-
- it('never matches a user tournament that merely copied the name', () => {
- expect(isHouseRow({ name: 'Daily Sprint 2026-08-14', entry_rules: {} }, '2026-08-14')).toBe(false);
- expect(isHouseRow({ name: 'Daily Sprint 2026-08-14' }, '2026-08-14')).toBe(false);
- });
-});
-
-describe('rosterFrom', () => {
- it('takes agents that closed a trade or are still holding one', () => {
- const roster = rosterFrom([
- { agent_id: 'a', closed: 12, open_positions: 0 },
- { agent_id: 'b', closed: 0, open_positions: 3 },
- { agent_id: 'c', closed: 0, open_positions: 0 },
- { closed: 9, open_positions: 1 },
- ]);
- expect(roster.map((r) => r.agent_id)).toEqual(['a', 'b']);
- });
-
- it('tolerates a leaderboard outage returning nothing', () => {
- expect(rosterFrom(null)).toEqual([]);
- expect(rosterFrom([])).toEqual([]);
- });
-});
-
-describe('visibleTournaments', () => {
- it('hides a finished bracket nobody entered', () => {
- const rows = [
- { id: 'dead', phase: 'finished', entrant_count: 0 },
- { id: 'real', phase: 'finished', entrant_count: 4 },
- ];
- expect(visibleTournaments(rows).map((r) => r.id)).toEqual(['real']);
- });
-
- it('keeps live and upcoming brackets that are still joinable at zero entrants', () => {
- const rows = [
- { id: 'live', phase: 'live', entrant_count: 0 },
- { id: 'next', phase: 'upcoming', entrant_count: 0 },
- ];
- expect(visibleTournaments(rows).map((r) => r.id)).toEqual(['live', 'next']);
- });
-});