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(x-mentions): stop orphaning mention rows at decision=null on a post-insert throw

nirholas17ca98e194ce
diff --git a/api/_lib/x-mention-health.js b/api/_lib/x-mention-health.js
index a1c0e3249..005080b12 100644
--- a/api/_lib/x-mention-health.js
+++ b/api/_lib/x-mention-health.js
@@ -77,6 +77,7 @@ export async function readMentionBotSignals({ env = process.env } = {}) {
percentile_disc(0.5) within group (order by extract(epoch from (decided_at - created_at)))
filter (where decision = 'reply' and decided_at is not null and created_at > now() - interval '1 day') as median_reply_seconds
from x_mention_events
+ where account_ref not like '%-evidence'
`,
QUERY_TIMEOUT_MS,
),
diff --git a/api/_lib/x-mention-poll.js b/api/_lib/x-mention-poll.js
index 8e4f7fec6..c5f1e34f7 100644
--- a/api/_lib/x-mention-poll.js
+++ b/api/_lib/x-mention-poll.js
@@ -121,17 +121,17 @@ export async function handleMention({ mention, account, limits, env, store = men
const { inserted } = await store.recordMention({ mention, parsed, dryRun });
if (!inserted) return { tweetId: mention.id, decision: 'duplicate', reason: null };
- if (await paused(env)) {
- await store.updateDecision(mention.id, { decision: 'paused', reason: 'kill_switch' });
- return { tweetId: mention.id, intent: parsed.intent, decision: 'paused', reason: 'kill_switch' };
- }
+ try {
+ if (await paused(env)) {
+ await store.updateDecision(mention.id, { decision: 'paused', reason: 'kill_switch' });
+ return { tweetId: mention.id, intent: parsed.intent, decision: 'paused', reason: 'kill_switch' };
+ }
- if (parsed.intent === 'ignore') {
- await store.updateDecision(mention.id, { decision: 'skip', reason: parsed.reason });
- return { tweetId: mention.id, intent: 'ignore', decision: 'skip', reason: parsed.reason };
- }
+ if (parsed.intent === 'ignore') {
+ await store.updateDecision(mention.id, { decision: 'skip', reason: parsed.reason });
+ return { tweetId: mention.id, intent: 'ignore', decision: 'skip', reason: parsed.reason };
+ }
- try {
const verdict = await guard({ mention, account: mention.account, env, store });
if (!verdict.allow && verdict.reason === 'known_bot' && !agentCtx) {
// A known xAI account tagged us: answer on behalf of the human at the
diff --git a/tests/x-mention-health.test.js b/tests/x-mention-health.test.js
index 65a6fa33e..9dd4cd3e9 100644
--- a/tests/x-mention-health.test.js
+++ b/tests/x-mention-health.test.js
@@ -9,8 +9,50 @@
* paused the bot on purpose.
*/
-import { describe, it, expect } from 'vitest';
-import { mentionBotVerdict, LAG_DEGRADED_MS, ERROR_RATE_DEGRADED } from '../api/_lib/x-mention-health.js';
+import { readFileSync } from 'node:fs';
+import { describe, it, expect, beforeEach, vi } from 'vitest';
+import { PGlite } from '@electric-sql/pglite';
+
+const dbState = { pg: null };
+
+vi.mock('../api/_lib/db.js', () => {
+ const sql = async (strings, ...values) => {
+ const text = strings.reduce((acc, part, i) => acc + part + (i < values.length ? `$${i + 1}` : ''), '');
+ return (await dbState.pg.query(text, values)).rows;
+ };
+ return { sql, isDbUnavailableError: () => false, isDbCapacityError: () => false };
+});
+
+const { mentionBotVerdict, LAG_DEGRADED_MS, ERROR_RATE_DEGRADED, readMentionBotSignals } = await import('../api/_lib/x-mention-health.js');
+
+const MIG = new URL('../api/_lib/migrations/', import.meta.url);
+const MIGRATION = readFileSync(new URL('20261008170000_x_mention_events.sql', MIG), 'utf8');
+const APP_SETTINGS = (() => {
+ const src = readFileSync(new URL('20260922210000_free_tier_models.sql', MIG), 'utf8');
+ const start = src.indexOf('CREATE TABLE IF NOT EXISTS app_settings');
+ return src.slice(start, src.indexOf(');', start) + 2);
+})();
+
+beforeEach(async () => {
+ dbState.pg = new PGlite();
+ await dbState.pg.exec(APP_SETTINGS);
+ await dbState.pg.exec(MIGRATION);
+});
+
+describe('readMentionBotSignals', () => {
+ it('excludes synthetic -evidence rows from the unprocessed count, and counts a real row', async () => {
+ await dbState.pg.query(
+ `insert into x_mention_events (tweet_id, account_kind, account_ref, author_id, intent, decision) values ($1,'company','trythreews-evidence','9000000000000000001','ignore',null)`,
+ ['7514357880548491265'],
+ );
+ await dbState.pg.query(
+ `insert into x_mention_events (tweet_id, account_kind, account_ref, author_id, intent, decision) values ($1,'company','trythreews','9000000000000000002','chat',null)`,
+ ['1900000000000000900'],
+ );
+ const signals = await readMentionBotSignals({ env: {} });
+ expect(signals.unprocessed).toBe(1);
+ });
+});
describe('mentionBotVerdict', () => {
it('reads unknown before any mention has ever been recorded', () => {
diff --git a/tests/x-mention-poll.test.js b/tests/x-mention-poll.test.js
index edfe082a4..13019f764 100644
--- a/tests/x-mention-poll.test.js
+++ b/tests/x-mention-poll.test.js
@@ -157,6 +157,21 @@ describe('a poll with a cursor', () => {
expect(out.status).toBe('partial');
expect(BigInt(await store.getCursor(ACCT))).toBeLessThan(1800000000000000104n);
});
+
+ it('records decision:error instead of orphaning the row when paused() throws after the insert', async () => {
+ const id = '1900000000000000200';
+ const mention = {
+ id, userId: '42', username: 'alice', text: '@trythreews hello there', createdAt: new Date().toISOString(), conversationId: id, account: ACCT,
+ mentions: [{ username: 'trythreews', id: COMPANY.userId }], urls: [],
+ media: [], inReplyToUserId: null, repliedTo: null, quoted: null, isRetweet: false, fromSelf: false,
+ };
+ const paused = async () => { throw new Error('paused check exploded'); };
+ await expect(poll.handleMention({
+ mention, account: COMPANY, limits: poll.replyLimits({}), env: {}, paused,
+ guard: async () => ({ allow: true }),
+ })).resolves.toMatchObject({ decision: 'error' });
+ expect(await store.getMentionEvent(id)).toMatchObject({ decision: 'error' });
+ });
});
describe('the run lock', () => {