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.

refactor(pipeline): move recorderState and the heartbeat window into pipeline-summary and cover them

nirholasf772a61fdaf8
diff --git a/api/_lib/pipeline-summary.js b/api/_lib/pipeline-summary.js
index 5325e1b61..050fb2c7e 100644
--- a/api/_lib/pipeline-summary.js
+++ b/api/_lib/pipeline-summary.js
@@ -16,6 +16,69 @@
/** Number of labeled outcomes the learner needs before it trains weights. */
export const MIN_TRAINING_SAMPLES = 50;
+/**
+ * Freshness window for the recorder's heartbeat: 2x the 30s cadence plus slack,
+ * so one skipped beat under load cannot flap it between "live" and "down".
+ */
+export const HEARTBEAT_FRESH_MS = 90_000;
+
+/**
+ * Derive the recorder's operational truth from its bot_heartbeat row.
+ *
+ * bot_heartbeat is keyed by worker alone, so a single row describes whichever
+ * network that worker is actually recording. Asking about the other network
+ * must not inherit its liveness: a mainnet worker is honestly "offline" from
+ * devnet's point of view, and reporting otherwise made every downstream devnet
+ * stage read as "waiting on the next launch" when nothing was ever recording.
+ *
+ * @param {any} beat the bot_heartbeat row, or undefined
+ * @param {string} network the network the caller asked about
+ * @param {number} [now] epoch ms, injectable for tests
+ */
+export function recorderState(beat, network, now = Date.now()) {
+ if (!beat) return { state: 'offline', reason: 'never started' };
+ const lastBeatMs = beat.last_beat_at ? new Date(beat.last_beat_at).getTime() : 0;
+ const ageMs = lastBeatMs ? now - lastBeatMs : null;
+ const alive = ageMs != null && ageMs < HEARTBEAT_FRESH_MS;
+ const meta = beat.meta && typeof beat.meta === 'object' ? beat.meta : {};
+ if (meta.network && meta.network !== network) {
+ return {
+ state: 'offline',
+ reason: `recording ${meta.network}, not ${network}`,
+ mode: beat.mode || meta.mode || 'unknown',
+ network: meta.network,
+ feedLive: false,
+ feedSilent: false,
+ heartbeatAgeMs: ageMs,
+ lastEventAgeMs: null,
+ reconnects: null,
+ errors: null,
+ lastError: null,
+ intel: null,
+ bootAt: meta.bootAt ?? null,
+ };
+ }
+ const feedLive = alive && meta.feedConnected === true;
+ const watchdogMs = Number(meta.feedWatchdogMs) || 180_000;
+ const feedSilent = alive && Number(meta.lastEventAgeMs) > watchdogMs;
+ const state = !alive ? 'down' : feedSilent || !feedLive ? 'degraded' : 'live';
+ return {
+ state,
+ reason: state === 'down' ? 'heartbeat stale' : state === 'degraded' ? (feedLive ? 'feed silent' : 'feed disconnected') : null,
+ mode: beat.mode || meta.mode || 'unknown',
+ network: meta.network || null,
+ feedLive,
+ feedSilent: !!feedSilent,
+ heartbeatAgeMs: ageMs,
+ lastEventAgeMs: meta.lastEventAgeMs ?? null,
+ reconnects: meta.reconnects ?? null,
+ errors: meta.errors ?? null,
+ lastError: meta.lastError ?? null,
+ intel: meta.intel ?? null,
+ bootAt: meta.bootAt ?? null,
+ };
+}
+
/**
* @param {string} network
* @param {{recorder:any,intel:any,outcomes:any,oracle:any,reputation:any,learning:any,trading:any}} stages
diff --git a/api/pipeline.js b/api/pipeline.js
index ea5e7eb78..16c0fd6bc 100644
--- a/api/pipeline.js
+++ b/api/pipeline.js
@@ -25,72 +25,13 @@
import { cors, json, method, wrap, rateLimited } from './_lib/http.js';
import { limits, clientIp } from './_lib/rate-limit.js';
import { sql } from './_lib/db.js';
-import { summarize } from './_lib/pipeline-summary.js';
+import { summarize, recorderState } from './_lib/pipeline-summary.js';
const WORKER = 'agent-sniper';
-// 2× the 30s heartbeat cadence + slack: one skipped beat under load must not
-// flap the recorder between "live" and "down".
-const HEARTBEAT_FRESH_MS = 90_000;
// Public spend categories whose 24h counts the loop produces (mirror pulse.js).
const SNIPE = 'snipe';
const TRADE = 'trade';
-/**
- * Derive the recorder's operational truth from its heartbeat row.
- *
- * bot_heartbeat is keyed by worker alone, so a single row describes whichever
- * network that worker is actually recording. Asking about the other network
- * must not inherit its liveness: a mainnet worker is honestly "offline" from
- * devnet's point of view, and reporting otherwise made every downstream devnet
- * stage read as "waiting on the next launch" when nothing was ever recording.
- *
- * @param {any} beat the bot_heartbeat row, or undefined
- * @param {string} network the network the caller asked about
- */
-function recorderState(beat, network) {
- if (!beat) return { state: 'offline', reason: 'never started' };
- const lastBeatMs = beat.last_beat_at ? new Date(beat.last_beat_at).getTime() : 0;
- const ageMs = lastBeatMs ? Date.now() - lastBeatMs : null;
- const alive = ageMs != null && ageMs < HEARTBEAT_FRESH_MS;
- const meta = beat.meta && typeof beat.meta === 'object' ? beat.meta : {};
- if (meta.network && meta.network !== network) {
- return {
- state: 'offline',
- reason: `recording ${meta.network}, not ${network}`,
- mode: beat.mode || meta.mode || 'unknown',
- network: meta.network,
- feedLive: false,
- feedSilent: false,
- heartbeatAgeMs: ageMs,
- lastEventAgeMs: null,
- reconnects: null,
- errors: null,
- lastError: null,
- intel: null,
- bootAt: meta.bootAt ?? null,
- };
- }
- const feedLive = alive && meta.feedConnected === true;
- const watchdogMs = Number(meta.feedWatchdogMs) || 180_000;
- const feedSilent = alive && Number(meta.lastEventAgeMs) > watchdogMs;
- const state = !alive ? 'down' : feedSilent || !feedLive ? 'degraded' : 'live';
- return {
- state,
- reason: state === 'down' ? 'heartbeat stale' : state === 'degraded' ? (feedLive ? 'feed silent' : 'feed disconnected') : null,
- mode: beat.mode || meta.mode || 'unknown',
- network: meta.network || null,
- feedLive,
- feedSilent: !!feedSilent,
- heartbeatAgeMs: ageMs,
- lastEventAgeMs: meta.lastEventAgeMs ?? null,
- reconnects: meta.reconnects ?? null,
- errors: meta.errors ?? null,
- lastError: meta.lastError ?? null,
- intel: meta.intel ?? null,
- bootAt: meta.bootAt ?? null,
- };
-}
-
export default wrap(async (req, res) => {
if (cors(req, res, { methods: 'GET,OPTIONS', origins: '*' })) return;
if (!method(req, res, ['GET'])) return;
diff --git a/tests/pipeline-summary.test.js b/tests/pipeline-summary.test.js
index cc53788b4..276910107 100644
--- a/tests/pipeline-summary.test.js
+++ b/tests/pipeline-summary.test.js
@@ -6,7 +6,7 @@
// agent reading /api/pipeline) the wrong move.
import { describe, it, expect } from 'vitest';
-import { summarize, MIN_TRAINING_SAMPLES } from '../api/_lib/pipeline-summary.js';
+import { summarize, recorderState, MIN_TRAINING_SAMPLES, HEARTBEAT_FRESH_MS } from '../api/_lib/pipeline-summary.js';
/** Build a full stages object, overriding only what a case cares about. */
function stages(over = {}) {
@@ -99,3 +99,64 @@ describe('pipeline summarize() — summary string', () => {
}
});
});
+
+describe('recorderState(): the recorder is network-scoped', () => {
+ const NOW = Date.parse('2026-09-09T00:00:00.000Z');
+ const beat = (over = {}, meta = {}) => ({
+ mode: 'live',
+ last_beat_at: new Date(NOW - 5_000).toISOString(),
+ meta: { network: 'mainnet', feedConnected: true, lastEventAgeMs: 2_000, ...meta },
+ ...over,
+ });
+
+ it('reports a mainnet worker as live when mainnet is what was asked', () => {
+ const r = recorderState(beat(), 'mainnet', NOW);
+ expect(r.state).toBe('live');
+ expect(r.feedLive).toBe(true);
+ expect(r.reason).toBeNull();
+ });
+
+ it('never lends one network\'s liveness to the other', () => {
+ // bot_heartbeat is keyed by worker alone, so the devnet view of a mainnet
+ // worker used to read LIVE and made every downstream devnet stage claim it
+ // was "waiting on the next launch".
+ const r = recorderState(beat(), 'devnet', NOW);
+ expect(r.state).toBe('offline');
+ expect(r.reason).toBe('recording mainnet, not devnet');
+ expect(r.feedLive).toBe(false);
+ expect(r.network).toBe('mainnet');
+ });
+
+ it('a heartbeat older than the freshness window is down', () => {
+ const stale = beat({ last_beat_at: new Date(NOW - HEARTBEAT_FRESH_MS - 1_000).toISOString() });
+ const r = recorderState(stale, 'mainnet', NOW);
+ expect(r.state).toBe('down');
+ expect(r.reason).toBe('heartbeat stale');
+ });
+
+ it('a fresh beat with a silent feed is degraded, and says which kind', () => {
+ const silent = recorderState(beat({}, { lastEventAgeMs: 600_000 }), 'mainnet', NOW);
+ expect(silent.state).toBe('degraded');
+ expect(silent.reason).toBe('feed silent');
+
+ const cut = recorderState(beat({}, { feedConnected: false }), 'mainnet', NOW);
+ expect(cut.state).toBe('degraded');
+ expect(cut.reason).toBe('feed disconnected');
+ });
+
+ it('no row at all is an honest offline, not a crash', () => {
+ expect(recorderState(undefined, 'mainnet', NOW)).toEqual({ state: 'offline', reason: 'never started' });
+ });
+
+ it('a row with no network in meta is not second-guessed', () => {
+ const r = recorderState(beat({}, { network: undefined }), 'devnet', NOW);
+ expect(r.state).toBe('live');
+ });
+
+ it('feeds summarize(): a cross-network recorder makes the loop idle', () => {
+ const rec = recorderState(beat(), 'devnet', NOW);
+ const out = summarize('devnet', stages({ recorder: rec }));
+ expect(out.health).toBe('idle');
+ expect(out.next_action.step).toBe('deploy_recorder');
+ });
+});