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.

Make the irl-reap cron tolerate not-yet-created tables

nirholas29ecd6e21923
diff --git a/api/cron/irl-reap.js b/api/cron/irl-reap.js
index 725d2b9c6..724c0d8d1 100644
--- a/api/cron/irl-reap.js
+++ b/api/cron/irl-reap.js
@@ -41,20 +41,40 @@ export default wrap(async (req, res) => {
if (!method(req, res, ['GET'])) return;
if (!requireCron(req, res)) return;
- // Expired anon pins, ≥ 1 day past expiry. expires_at IS NULL ⇒ permanent ⇒ kept.
- const reapedPins = await sql`
- DELETE FROM irl_pins
- WHERE expires_at IS NOT NULL
- AND expires_at < NOW() - INTERVAL '1 day'
- RETURNING id
+ // Both tables are created lazily by their write endpoints (irl_pins by
+ // api/irl/pins.js, irl_pin_reports by api/irl/report.js), so on a fresh
+ // deployment — or before the first pin is placed / first report is filed —
+ // they may not exist yet. A reaper that hard-depended on them would throw
+ // `relation does not exist` and 500 the hourly cron. Probe with to_regclass
+ // and treat a missing table as "nothing to reap" rather than an error.
+ const [{ pins, reports }] = await sql`
+ SELECT
+ to_regclass('public.irl_pins') AS pins,
+ to_regclass('public.irl_pin_reports') AS reports
`;
- // Orphaned reports — their pin is gone, so the trail is moot.
- const reapedReports = await sql`
- DELETE FROM irl_pin_reports r
- WHERE NOT EXISTS (SELECT 1 FROM irl_pins p WHERE p.id = r.pin_id)
- RETURNING r.id
- `;
+ // Expired anon pins, ≥ 1 day past expiry. expires_at IS NULL ⇒ permanent ⇒ kept.
+ const reapedPins = pins
+ ? await sql`
+ DELETE FROM irl_pins
+ WHERE expires_at IS NOT NULL
+ AND expires_at < NOW() - INTERVAL '1 day'
+ RETURNING id
+ `
+ : [];
+
+ // Orphaned reports — their pin is gone, so the trail is moot. If the pins
+ // table itself is absent, every report is an orphan, so purge them all.
+ let reapedReports = [];
+ if (reports) {
+ reapedReports = pins
+ ? await sql`
+ DELETE FROM irl_pin_reports r
+ WHERE NOT EXISTS (SELECT 1 FROM irl_pins p WHERE p.id = r.pin_id)
+ RETURNING r.id
+ `
+ : await sql`DELETE FROM irl_pin_reports RETURNING id`;
+ }
return json(res, 200, {
ok: true,
diff --git a/tests/api/irl-reap-cron.test.js b/tests/api/irl-reap-cron.test.js
new file mode 100644
index 000000000..bf4e84eb5
--- /dev/null
+++ b/tests/api/irl-reap-cron.test.js
@@ -0,0 +1,92 @@
+// api/cron/irl-reap.js — IRL placement reaper.
+//
+// Regression: the reaper DELETEs from irl_pins and irl_pin_reports, both of
+// which are created LAZILY by their write endpoints (pins.js / report.js). On a
+// fresh deployment — or before the first pin is placed / first report filed —
+// the table doesn't exist, so the unguarded DELETE threw `relation does not
+// exist` and the hourly cron 500'd. The reaper must probe with to_regclass and
+// treat a missing table as "nothing to reap".
+
+import { describe, it, expect, beforeEach, vi } from 'vitest';
+
+const sqlMock = vi.fn();
+vi.mock('../../api/_lib/db.js', () => ({ sql: sqlMock }));
+vi.mock('../../api/_lib/env.js', () => ({ env: {} }));
+vi.mock('../../api/_lib/sentry.js', () => ({ captureException: vi.fn() }));
+vi.mock('../../api/_lib/alerts.js', () => ({ sendOpsAlert: vi.fn() }));
+vi.mock('../../api/_lib/zauth.js', () => ({ instrument: () => false, drain: vi.fn() }));
+
+const { default: handler } = await import('../../api/cron/irl-reap.js');
+
+process.env.CRON_SECRET = 'test-cron-secret';
+
+function makeReqRes() {
+ const req = {
+ method: 'GET',
+ url: '/api/cron/irl-reap',
+ headers: { authorization: 'Bearer test-cron-secret' },
+ };
+ const res = {
+ statusCode: 0,
+ body: null,
+ headers: {},
+ setHeader(k, v) { this.headers[k.toLowerCase()] = v; },
+ end(b) { this.body = b ? JSON.parse(b) : null; },
+ get headersSent() { return this.body !== null; },
+ get writableEnded() { return this.body !== null; },
+ };
+ return { req, res };
+}
+
+describe('irl-reap cron', () => {
+ beforeEach(() => sqlMock.mockReset());
+
+ it('returns 200 (not 500) when neither table exists yet — nothing to reap', async () => {
+ // to_regclass probe → both NULL. No DELETE should ever be issued.
+ sqlMock.mockResolvedValueOnce([{ pins: null, reports: null }]);
+
+ const { req, res } = makeReqRes();
+ await handler(req, res);
+
+ expect(res.statusCode).toBe(200);
+ expect(res.body).toMatchObject({ ok: true, reapedPins: 0, reapedReports: 0 });
+ // Exactly one query ran: the existence probe. No DELETE against a
+ // non-existent relation (which is what 500'd in production).
+ expect(sqlMock).toHaveBeenCalledTimes(1);
+ });
+
+ it('reaps both tables when they exist', async () => {
+ sqlMock
+ .mockResolvedValueOnce([{ pins: 'irl_pins', reports: 'irl_pin_reports' }]) // probe
+ .mockResolvedValueOnce([{ id: 'p1' }, { id: 'p2' }]) // pins delete
+ .mockResolvedValueOnce([{ id: 'r1' }]); // reports delete
+
+ const { req, res } = makeReqRes();
+ await handler(req, res);
+
+ expect(res.statusCode).toBe(200);
+ expect(res.body).toMatchObject({ ok: true, reapedPins: 2, reapedReports: 1 });
+ expect(sqlMock).toHaveBeenCalledTimes(3);
+ });
+
+ it('purges all reports when the pins table is gone but reports remain', async () => {
+ sqlMock
+ .mockResolvedValueOnce([{ pins: null, reports: 'irl_pin_reports' }]) // probe
+ .mockResolvedValueOnce([{ id: 'r1' }, { id: 'r2' }]); // unconditional reports delete
+
+ const { req, res } = makeReqRes();
+ await handler(req, res);
+
+ expect(res.statusCode).toBe(200);
+ expect(res.body).toMatchObject({ ok: true, reapedPins: 0, reapedReports: 2 });
+ expect(sqlMock).toHaveBeenCalledTimes(2);
+ });
+
+ it('rejects an unauthenticated request with 401', async () => {
+ const { req, res } = makeReqRes();
+ req.headers.authorization = 'Bearer wrong';
+ await handler(req, res);
+ expect(res.statusCode).toBe(401);
+ expect(sqlMock).not.toHaveBeenCalled();
+ });
+});