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.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
feat(x-mentions): run make and avatar follow-ups every tick and deliver late replies through the X adapter
nirholas0c987134ec65
diff --git a/api/_lib/x-mention-deliver.js b/api/_lib/x-mention-deliver.jsnew file mode 100644index 000000000..46653d33e--- /dev/null+++ b/api/_lib/x-mention-deliver.js@@ -0,0 +1,57 @@+// The delivery bridge for late replies. `finishPendingMakes()` and+// `finishPendingAvatars()` settle a pending row long after the mention was+// handled, and hand the finished reply to an injected `deliver`. This builds+// that function on the order 049 X adapter (gateway/adapters/x.js), so a late+// reply goes through exactly the same safety shape as an immediate one:+//+// - dry run (the default): the adapter records the would-be reply on the+// x_mention_events row and makes no HTTP call; `deliver` resolves to null;+// - live (X_MENTION_BOT_LIVE=1, the row is not dry, and a token resolver is+// supplied): the adapter posts the reply, with its media, to the mention's+// author and `deliver` resolves to the new post id.+//+// Callers only invoke `deliver` for rows that are not dry, so the adapter's own+// policy gate is the second lock, never the only one.++import * as mentionStore from './x-mention-store.js';+import { createXAdapter } from './gateway/adapters/x.js';++/** The mention object the adapter needs, rebuilt from the recorded row. */+function mentionFromRow(row) {+ return {+ id: row.tweet_id,+ userId: row.author_id,+ username: row.author_username || null,+ text: row.mention_text || '',+ createdAt: row.mention_created_at || null,+ conversationId: row.conversation_id || null,+ account: { kind: row.account_kind, ref: row.account_ref },+ };+}++/**+ * @param {{ env?: Record<string,string|undefined>, store?: typeof mentionStore, adapterFactory?: typeof createXAdapter, getAccessToken?: ((row: object) => (() => Promise<string>)|null)|null }} [opts]+ * `getAccessToken(row)` returns the posting account's token resolver for that+ * row, or null when none exists (the adapter then stays in dry run).+ * @returns {(args: { text: string, mediaUrl?: string|null, inReplyToTweetId: string }) => Promise<string|null>}+ */+export function createDeliver({ env = process.env, store = mentionStore, adapterFactory = createXAdapter, getAccessToken = null } = {}) {+ return async function deliver({ text, mediaUrl = null, inReplyToTweetId }) {+ const row = await store.getMentionEvent(inReplyToTweetId);+ if (!row) return null;+ const resolver = typeof getAccessToken === 'function' ? getAccessToken(row) : null;+ const adapter = adapterFactory({+ mention: mentionFromRow(row),+ parsed: { intent: row.intent },+ policyDryRun: row.dry_run !== false,+ getAccessToken: resolver,+ env,+ store,+ });+ const chatId = String(row.tweet_id);+ const sent = mediaUrl+ ? await adapter.sendMedia(chatId, { url: mediaUrl, caption: text })+ : await adapter.sendText(chatId, text);+ return sent?.dry_run ? null : sent?.ref?.messageId || null;+ };+}diff --git a/api/_lib/x-mention-poll.js b/api/_lib/x-mention-poll.jsindex 4c3dc7c54..07509e500 100644--- a/api/_lib/x-mention-poll.js+++ b/api/_lib/x-mention-poll.js@@ -32,6 +32,8 @@ import { ensureKnownBots, loadKnownBots } from './x-mention-known-bots.js';import { answerOnBehalf } from './x-mention-on-behalf.js';import * as xBudget from './x-budget.js';import { handleAvatar, finishPendingAvatars } from './x-mention-avatar.js';+import { finishPendingMakes } from './x-mention-make.js';+import { createDeliver } from './x-mention-deliver.js';export const LOCK_KEY = 'x_mentions_lock';const DEFAULT_LOCK_SECONDS = 110;@@ -261,6 +263,30 @@ export async function listAccounts({ env = process.env, agentAccounts = async ()return accounts;}+/**+ * Settle the make and avatar jobs that outlived their mention. Gated like any+ * other reply (kill switch, then the post budget), each finisher isolated so+ * one failing never stops the other or the tick. Delivery goes through the X+ * adapter: dry rows are recorded, live rows are posted.+ */+export async function runFollowUps({ env = process.env, deps = {} } = {}) {+ const budget = deps.budget || xBudget;+ if (await (deps.paused || isPaused)(env)) return { followUps: { skipped: 'kill_switch' } };+ const gate = await budget.readGate({ env }).catch(() => ({ allow: true }));+ if (!gate.allow) return { followUps: { skipped: 'budget', reason: gate.reason } };+ const baseDeliver = deps.deliver || createDeliver({ env, adapterFactory: deps.adapterFactory, getAccessToken: deps.getAccessToken });+ const deliver = async (args) => {+ const id = await baseDeliver(args);+ if (id) await budget.recordPosts(1).catch(() => {});+ return id;+ };+ const run = (fn) => fn({}, { deliver }).catch((err) => ({ error: String(err?.message || err).slice(0, 200) }));+ return {+ makeFollowUp: await run(deps.finishPendingMakes || finishPendingMakes),+ avatarFollowUp: await run(deps.finishPendingAvatars || finishPendingAvatars),+ };+}+/** One cron tick: lock, poll every account, release. Never throws for X outages. */export async function runMentionTick({ env = process.env, deps = {} } = {}) {const holder = await (deps.acquireLock || acquireLock)(intEnv(env, 'X_MENTION_LOCK_SECONDS', DEFAULT_LOCK_SECONDS));@@ -276,7 +302,7 @@ export async function runMentionTick({ env = process.env, deps = {} } = {}) {report.accounts.push({ account: `${descriptor.kind}:${descriptor.ref}`, status: 'error', error: String(err?.message || err).slice(0, 200) });}}- report.avatarFollowUp = await (deps.finishPendingAvatars || finishPendingAvatars)().catch((err) => ({ error: String(err?.message || err).slice(0, 200) }));+ Object.assign(report, await runFollowUps({ env, deps }));return report;} finally {await (deps.releaseLock || releaseLock)(holder).catch(() => {});diff --git a/docs/x-mention-bot.md b/docs/x-mention-bot.mdindex 102493188..8a9a1d1f4 100644--- a/docs/x-mention-bot.md+++ b/docs/x-mention-bot.md@@ -73,3 +73,13 @@ Dry run applies as everywhere: the four-line reply is recorded on the row and nonpx vitest run tests/x-mention-guard.test.js tests/x-budget.test.js```The kill switch tests assert zero calls to the reply brain and the X adapter.++## Late replies: make and avatar follow-ups++A `make` or `avatar` mention whose 3D job outlives the bounded wait is recorded as `pending`. Every `x-mentions` tick then runs `runFollowUps()` (`api/_lib/x-mention-poll.js`), which calls `finishPendingMakes()` and `finishPendingAvatars()`:++- **Gated like any reply.** The kill switch skips both, and a closed read/post budget gate skips both. The report carries `followUps: { skipped: 'kill_switch' | 'budget' }`.+- **Isolated.** Each finisher runs in its own catch; one throwing reports `{ error }` under `makeFollowUp` or `avatarFollowUp` and the other still runs.+- **Delivered through the X adapter.** `api/_lib/x-mention-deliver.js` builds the `deliver` the finishers use on `createXAdapter`. A dry row (`dry_run = true`, the default) is only recorded on the `x_mention_events` row and nothing is posted. A live row is posted by the adapter only when `X_MENTION_BOT_LIVE=1` and a token resolver is supplied (`getAccessToken(row)`); each real post counts against the post budget. With no resolver the adapter stays in dry run, so rows are no longer held as `paused` for lack of a deliver.++Going live stays the separate owner step.diff --git a/prompts/finish/_context/x-grok-PROGRESS.md b/prompts/finish/_context/x-grok-PROGRESS.mdindex 73de94f7a..5e102be96 100644--- a/prompts/finish/_context/x-grok-PROGRESS.md+++ b/prompts/finish/_context/x-grok-PROGRESS.md@@ -187,3 +187,8 @@ Added `agent-get-started`, `daily-3d-brief`, `asset-pack`, `avatar-from-photo` t- Shipped `api/_lib/x-mention-avatar.js` (own profile image only, `_normal` upgraded to 400x400, vision moderation, image-to-3D then auto-rig, bounded wait plus `finishPendingAvatars` follow-up, `default_avatar` reply with the `/create` link), wired into `x-mention-poll.js`. Tests: `tests/x-mention-avatar.test.js` 14 passing, including own-image-only and the named-other-account case. Doc: `docs/x-mention-avatar.md`. Code landed in sweep commit `9997cdcd4`.- Evidence: `prompts/x-grok/_generated/avatar-dry-run.json`: 3 reply rows from real public profile images (profile image fetch 200, pose link 200) plus a default-image row.- Remaining line: media status codes. Production `/api/render/glb` caps input at 10 MB and the rigged avatars are 15 to 16 MB, so the poster answers 413 there. This commit raises the cap to 25 MB (the renderer's own default); against a local server the same rigged GLB renders 200 image/png. After the next deploy, rerun `node scripts/x-mention-avatar-dry-run.mjs` to record production media 200s, then delete the order file. Owner: whoever runs the deploy (gated).++## 2026-10-09: late-reply follow-ups wired into the x-mentions tick++- `runMentionTick` now calls `runFollowUps()` each tick: `finishPendingMakes()` and `finishPendingAvatars()`, behind the kill switch and the read/post budget gate, each error-isolated. Their `deliver` is `createDeliver()` (`api/_lib/x-mention-deliver.js`) on the order 049 X adapter: dry rows are recorded, live rows post only with `X_MENTION_BOT_LIVE=1` plus a token resolver, and posts are counted against the budget. Report keys: `makeFollowUp`, `avatarFollowUp`, `followUps`.+- Evidence: `tests/x-mention-poll.test.js` 16 passing (6 new, real PGlite migrations, adapter posting captured at its `post` boundary); make and avatar suites still pass. Nothing was posted to X. Remaining owner step: a company-account token resolver for live mode (order 928).diff --git a/tests/x-mention-poll.test.js b/tests/x-mention-poll.test.jsindex b3f9a84f7..74da15893 100644--- a/tests/x-mention-poll.test.js+++ b/tests/x-mention-poll.test.js@@ -22,6 +22,10 @@ vi.mock('../api/auth/x/[action].js', () => ({ encryptToken: (s) => s, decryptTokconst poll = await import('../api/_lib/x-mention-poll.js');const store = await import('../api/_lib/x-mention-store.js');const { XTierUnavailable, XRateLimited } = await import('../api/_lib/x-mentions.js');+const make = await import('../api/_lib/x-mention-make.js');+const avatar = await import('../api/_lib/x-mention-avatar.js');+const { createDeliver } = await import('../api/_lib/x-mention-deliver.js');+const { createXAdapter } = await import('../api/_lib/gateway/adapters/x.js');const MIG = new URL('../api/_lib/migrations/', import.meta.url);const MIGRATION = readFileSync(new URL('20261008170000_x_mention_events.sql', MIG), 'utf8');@@ -143,3 +147,85 @@ describe('the run lock', () => {expect(await poll.acquireLock(60)).toBeTruthy();});});++describe('late-reply follow-ups', () => {+ const BASE = 'https://three.test';+ const DONE = { status: 'done', glb_url: 'https://cdn.three.test/m.glb', creation_id: '11111111-1111-4111-8111-111111111111', preview_image_url: 'https://cdn.three.test/m.png' };+ const ids = { make: '1900000000000000001', avatar: '1900000000000000002' };+ const open = { paused: async () => false, budget: { readGate: async () => ({ allow: true }), recordPosts: vi.fn(async () => {}) } };++ async function pending(intent, id, dryRun) {+ await store.recordMention({+ mention: { id, userId: '42', username: 'alice', text: `@trythreews ${intent}`, conversationId: id, account: ACCT },+ parsed: { intent, args: { [intent]: { job_id: `job-${intent}`, stage: 'mesh', prompt: 'a dragon', started_at: new Date().toISOString() } } },+ dryRun,+ decision: 'pending',+ });+ }+ const finishers = (post) => ({+ finishPendingMakes: (o, d) => make.finishPendingMakes(o, { ...d, base: BASE, pollOnce: async () => DONE }),+ finishPendingAvatars: (o, d) => avatar.finishPendingAvatars(o, { ...d, base: BASE, pollOnce: async () => DONE, startRig: async () => { throw Object.assign(new Error('no rig'), { code: 'rig_down' }); } }),+ getAccessToken: post ? () => async () => 'tok' : undefined,+ adapterFactory: post ? (o) => createXAdapter({ ...o, post, fetchMedia: async () => ({ buffer: Buffer.from('x'), mimeType: 'image/png' }), uploadMedia: async () => 'media-1', env: { X_MENTION_BOT_LIVE: '1' } }) : undefined,+ });++ beforeEach(async () => {+ open.budget.recordPosts.mockClear();+ });++ it('a dry row is settled and recorded through the adapter, never posted', async () => {+ await pending('make', ids.make, true);+ const post = vi.fn();+ const out = await poll.runFollowUps({ env: { X_MENTION_BOT_LIVE: '1' }, deps: { ...open, ...finishers(post) } });+ expect(out.makeFollowUp).toMatchObject({ checked: 1, replied: 1 });+ expect(post).not.toHaveBeenCalled();+ expect(await store.getMentionEvent(ids.make)).toMatchObject({ decision: 'reply', dry_run: true, reply_tweet_id: null });+ });++ it('a live row is posted through the adapter and no longer held as paused', async () => {+ await pending('make', ids.make, false);+ await pending('avatar', ids.avatar, false);+ const post = vi.fn(async () => ({ id: '2900000000000000001' }));+ const out = await poll.runFollowUps({ env: { X_MENTION_BOT_LIVE: '1' }, deps: { ...open, ...finishers(post) } });+ expect(out.makeFollowUp).toMatchObject({ replied: 1 });+ expect(out.avatarFollowUp).toMatchObject({ replied: 1 });+ expect(post).toHaveBeenCalledTimes(2);+ expect(post.mock.calls[0][0]).toMatchObject({ accessToken: 'tok', replyTo: expect.any(String), mediaIds: ['media-1'] });+ const row = await store.getMentionEvent(ids.make);+ expect(row).toMatchObject({ decision: 'reply', dry_run: false, reply_tweet_id: '2900000000000000001' });+ expect(open.budget.recordPosts).toHaveBeenCalledTimes(2);+ });++ it('a live row without a token resolver stays in dry run instead of posting', async () => {+ await pending('make', ids.make, false);+ const deliver = createDeliver({ env: { X_MENTION_BOT_LIVE: '1' } });+ await dbState.pg.query(`update x_mention_events set decision = 'reply' where tweet_id = $1`, [ids.make]);+ expect(await deliver({ text: 'done', inReplyToTweetId: ids.make })).toBeNull();+ expect(await store.getMentionEvent(ids.make)).toMatchObject({ dry_run: true, reply_text: 'done', reply_tweet_id: null });+ });++ it('one failing finisher does not stop the other', async () => {+ const avatarOnly = vi.fn(async () => ({ checked: 0 }));+ const out = await poll.runFollowUps({ env: {}, deps: { ...open, finishPendingMakes: async () => { throw new Error('boom'); }, finishPendingAvatars: avatarOnly } });+ expect(out.makeFollowUp).toEqual({ error: 'boom' });+ expect(out.avatarFollowUp).toEqual({ checked: 0 });+ });++ it('the kill switch and the budget gate skip both finishers', async () => {+ const spy = vi.fn(async () => ({}));+ const killed = await poll.runFollowUps({ env: {}, deps: { ...open, paused: async () => true, finishPendingMakes: spy, finishPendingAvatars: spy } });+ expect(killed).toEqual({ followUps: { skipped: 'kill_switch' } });+ const broke = await poll.runFollowUps({ env: {}, deps: { ...open, budget: { readGate: async () => ({ allow: false, reason: 'daily_cap' }) }, finishPendingMakes: spy, finishPendingAvatars: spy } });+ expect(broke).toMatchObject({ followUps: { skipped: 'budget', reason: 'daily_cap' } });+ expect(spy).not.toHaveBeenCalled();+ });++ it('a tick runs both finishers', async () => {+ const m = vi.fn(async () => ({ checked: 0 }));+ const a = vi.fn(async () => ({ checked: 0 }));+ const report = await poll.runMentionTick({ env: {}, deps: { acquireLock: async () => 'h', releaseLock: async () => {}, listAccounts: async () => [DESCRIPTOR], request, resolvedAccount: COMPANY, ...open, finishPendingMakes: m, finishPendingAvatars: a } });+ expect(m).toHaveBeenCalledTimes(1);+ expect(a).toHaveBeenCalledTimes(1);+ expect(report).toMatchObject({ makeFollowUp: { checked: 0 }, avatarFollowUp: { checked: 0 } });+ });+});