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(radar-stream): carry microseconds in the poll cursor so a precursor is streamed once, not every 1.5s

nirholasfa47a668d30e
diff --git a/api/sniper/radar-stream.js b/api/sniper/radar-stream.js
index 670afb914..4530ce9db 100644
--- a/api/sniper/radar-stream.js
+++ b/api/sniper/radar-stream.js
@@ -29,20 +29,31 @@ function trunc(addr) {
return `${addr.slice(0, 4)}…${addr.slice(-4)}`;
}
-/**
- * Advance the poll cursor past a row's timestamp.
- *
- * The cursor is an ISO string because that is what the query binds, but Neon
- * hands timestamptz columns back as Date objects. `someDate > someIsoString`
- * coerces BOTH sides to numbers, the string becomes NaN, and the comparison is
- * always false, so the cursor froze at connect time and every 1.5s poll
- * re-emitted the whole backlog it had already sent. Normalising to ISO text
- * makes the comparison lexicographic and correct (both sides are UTC, same
- * width), and keeps the cursor in the exact form the next query needs.
- */
+// The poll cursor is a UTC timestamp string with SIX fractional digits, and
+// every value that enters it is normalised to that width. Two independent
+// reasons, both of which used to re-send the whole backlog on every 1.5s tick:
+//
+// 1. Neon hands timestamptz columns back as Date objects, and
+// `someDate > someIsoString` coerces both sides to numbers, turning the
+// string into NaN. The comparison was always false, so the cursor never
+// moved off its connect-time value.
+// 2. Postgres keeps created_at to the microsecond, but a JS Date holds only
+// milliseconds. A cursor built by truncating one is strictly EARLIER than
+// the row it came from, so `created_at > cursor` stays true for that row
+// forever. This is why the query below asks Postgres for the cursor text
+// directly rather than deriving it from the parsed Date.
+//
+// Fixed width is what makes a plain lexicographic comparison exact here: every
+// value is `YYYY-MM-DDTHH:MM:SS.ffffffZ`, so string order is time order.
+
+/** A Date as the same fixed-width microsecond string Postgres emits. */
+export function microIso(date) {
+ return date.toISOString().replace(/(\.\d{3})Z$/, '$1000Z');
+}
+
export function advanceCursor(cursor, value) {
if (value == null) return cursor;
- const at = value instanceof Date ? value.toISOString() : String(value);
+ const at = value instanceof Date ? microIso(value) : String(value);
return at > cursor ? at : cursor;
}
@@ -84,14 +95,15 @@ export default async function handleRadarStream(req, res) {
// Start from "now" so we stream only fresh precursors; the REST endpoint
// supplies the backlog the page renders on load.
- let cursor = new Date().toISOString();
+ let cursor = microIso(new Date());
const poll = async () => {
if (!active) return;
try {
const rows = await sql`
select id, kind, trigger_wallet, new_wallet, mint, confidence,
- watch_reason, watch_score, observed_ts, created_at
+ watch_reason, watch_score, observed_ts, created_at,
+ to_char(created_at at time zone 'utc', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') as cursor_at
from radar_events
where network = ${network} and created_at > ${cursor}
order by created_at asc
@@ -129,7 +141,7 @@ export default async function handleRadarStream(req, res) {
at: r.created_at,
fired: isOwner ? firedMints.has(r.mint) : undefined,
});
- cursor = advanceCursor(cursor, r.created_at);
+ cursor = advanceCursor(cursor, r.cursor_at);
}
} catch {
send('error', { message: 'poll_failed' });
diff --git a/tests/sniper-endpoint-params.test.js b/tests/sniper-endpoint-params.test.js
index bccaab4cc..a07e93af7 100644
--- a/tests/sniper-endpoint-params.test.js
+++ b/tests/sniper-endpoint-params.test.js
@@ -3,7 +3,7 @@
// shrank to one trade, and an SSE cursor that never moved.
import { describe, it, expect } from 'vitest';
import { corpusQuery } from '../api/sniper/exit-lab.js';
-import { advanceCursor } from '../api/sniper/radar-stream.js';
+import { advanceCursor, microIso } from '../api/sniper/radar-stream.js';
const q = (search) => corpusQuery(new URLSearchParams(search));
@@ -42,26 +42,48 @@ describe('exit-lab corpusQuery', () => {
});
});
-describe('radar-stream advanceCursor', () => {
- const T0 = '2026-08-16T06:00:00.000Z';
+describe('radar-stream cursor', () => {
+ const T0 = microIso(new Date('2026-08-16T06:00:00.000Z'));
- // The bug: Neon returns timestamptz as a Date, and `aDate > anIsoString`
+ // Postgres renders created_at through
+ // to_char(… 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"'), which is what the poll query
+ // selects as cursor_at. Same shape, six fractional digits, always.
+ const pg = (s) => s;
+
+ it('pads a Date to the width Postgres emits', () => {
+ expect(microIso(new Date('2026-08-16T06:00:00.000Z'))).toBe('2026-08-16T06:00:00.000000Z');
+ expect(microIso(new Date('2026-08-16T06:00:00.007Z'))).toBe('2026-08-16T06:00:00.007000Z');
+ expect(microIso(new Date('2026-08-16T06:00:00.000Z'))).toHaveLength(27);
+ });
+
+ // Bug 1: Neon returns timestamptz as a Date, and `aDate > anIsoString`
// coerces both to numbers, making the string NaN and the comparison always
// false. The cursor froze at connect time, so every poll re-sent the same
// precursors and the live tape filled with duplicates.
it('advances past a Date row, which a raw > comparison never did', () => {
const row = new Date('2026-08-16T06:00:05.000Z');
expect(row > T0).toBe(false); // the original expression
- expect(advanceCursor(T0, row)).toBe('2026-08-16T06:00:05.000Z');
+ expect(advanceCursor(T0, row)).toBe('2026-08-16T06:00:05.000000Z');
});
- it('advances past an ISO string row', () => {
- expect(advanceCursor(T0, '2026-08-16T06:00:05.000Z')).toBe('2026-08-16T06:00:05.000Z');
+ // Bug 2: created_at carries microseconds a JS Date cannot hold, so a cursor
+ // truncated to milliseconds stayed strictly below the row it came from and
+ // `created_at > cursor` matched that same row on every subsequent poll. The
+ // cursor has to be the string Postgres itself rendered.
+ it('advances to the full microsecond precision Postgres reports', () => {
+ const cursorAt = pg('2026-08-16T06:00:05.216673Z');
+ expect(advanceCursor(T0, cursorAt)).toBe(cursorAt);
+ // The truncated form would have sorted BELOW the real row and re-matched it.
+ expect(microIso(new Date('2026-08-16T06:00:05.216Z')) < cursorAt).toBe(true);
+ // Advancing again with the same row is a no-op, so it is emitted once.
+ expect(advanceCursor(cursorAt, cursorAt)).toBe(cursorAt);
});
it('never rewinds on an older or equal row', () => {
expect(advanceCursor(T0, new Date('2026-08-16T05:59:00.000Z'))).toBe(T0);
- expect(advanceCursor(T0, new Date(T0))).toBe(T0);
+ expect(advanceCursor(T0, new Date('2026-08-16T06:00:00.000Z'))).toBe(T0);
+ expect(advanceCursor(pg('2026-08-16T06:00:05.216673Z'), pg('2026-08-16T06:00:05.216670Z')))
+ .toBe('2026-08-16T06:00:05.216673Z');
});
it('ignores a missing timestamp', () => {
@@ -71,12 +93,12 @@ describe('radar-stream advanceCursor', () => {
it('keeps the newest timestamp across a batch of rows', () => {
const rows = [
- new Date('2026-08-16T06:00:01.000Z'),
- new Date('2026-08-16T06:00:09.000Z'),
- new Date('2026-08-16T06:00:04.000Z'),
+ pg('2026-08-16T06:00:01.000100Z'),
+ pg('2026-08-16T06:00:09.000002Z'),
+ pg('2026-08-16T06:00:04.999999Z'),
];
let cursor = T0;
for (const at of rows) cursor = advanceCursor(cursor, at);
- expect(cursor).toBe('2026-08-16T06:00:09.000Z');
+ expect(cursor).toBe('2026-08-16T06:00:09.000002Z');
});
});