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(changelog): back off on X rate limits, skip duplicates and re-chain around deleted thread tweets

nirholasad1991eb6553
diff --git a/api/_lib/changelog-push.js b/api/_lib/changelog-push.js
index 3dd1143c3..5ba5caef9 100644
--- a/api/_lib/changelog-push.js
+++ b/api/_lib/changelog-push.js
@@ -35,6 +35,8 @@ const TELEGRAM_PACE_MS = 3500;
const X_LIMIT = 3; // per run
const X_PACE_MS = 10000;
const X_DAILY_CAP = 15; // free tier caps user writes at ~17/24h, stay under
+// Wait applied after a 429 whose headers carry no usable reset time.
+const X_BACKOFF_FALLBACK_MS = 15 * 60_000;
const LOCK_KEY = 'changelog_push_lock';
const LOCK_TTL_S = 240;
// Bounded, and well under LOCK_TTL_S on purpose: a send that hangs longer than
@@ -135,9 +137,8 @@ export function pendingEntries(feed, posted, limit, { newestWin = false } = {})
return true;
})
.sort((a, b) => (a.date < b.date ? -1 : a.date > b.date ? 1 : 0));
- // Telegram keeps the NEWEST entries when over the limit (newestWin); the X
- // thread takes the OLDEST so the chain stays chronological. Both post in
- // chronological order.
+ // Both lanes pass newestWin, keeping the NEWEST entries when over the limit;
+ // without it the OLDEST win. Either way the batch posts in chronological order.
const pending = newestWin ? unposted.slice(-limit) : unposted.slice(0, limit);
return { pending, backlog: unposted.length };
}
@@ -259,6 +260,33 @@ export function formatTweet(e) {
return `${body}${suffix}`;
}
+// How an X write failure is handled, decided from the error alone so the
+// policy is testable without the network. twitter-api-v2 raises an
+// ApiResponseError carrying the HTTP status (`code`), the v2 problem body
+// (`data`), and the parsed rate-limit headers (`rateLimit`).
+// rate_limited wait until X's own reset time, then resume (not a failure)
+// duplicate X already holds this exact text; count it posted and move on,
+// otherwise the same entry blocks the lane on every tick
+// thread_gone the reply target was deleted; re-chain and retry
+// fatal auth, suspension, outage: surface it as a lane error
+export function classifyXError(err, now = Date.now()) {
+ const detail = [err?.data?.detail, err?.data?.title, err?.data?.errors?.[0]?.message, err?.message]
+ .filter(Boolean)
+ .join(' ');
+ if (err?.code === 429 || err?.rateLimitError) {
+ const rl = err?.rateLimit;
+ // The 24h user/app write windows reset far later than the 15-minute one;
+ // when one of them is the window that ran out, its reset is the real one.
+ const window = [rl?.userDay, rl?.day].find((w) => w && w.remaining === 0) || rl;
+ const resetMs = Number(window?.reset) * 1000;
+ const until = Number.isFinite(resetMs) && resetMs > now ? resetMs : now + X_BACKOFF_FALLBACK_MS;
+ return { kind: 'rate_limited', until };
+ }
+ if (/duplicate content/i.test(detail)) return { kind: 'duplicate' };
+ if (/deleted or not visible|in_reply_to_tweet_id/i.test(detail)) return { kind: 'thread_gone' };
+ return { kind: 'fatal' };
+}
+
export async function pushXLane(feed) {
const creds = {
appKey: process.env.X_API_KEY,
@@ -280,14 +308,24 @@ export async function pushXLane(feed) {
let thread = state.thread || null;
// Free-tier write budget: timestamps of posts in the trailing 24h.
let recent = (state.recent || []).filter((t) => Date.now() - t < 86400000);
+ let backoffUntil = state.backoffUntil || 0;
+ let lastPostAt = state.lastPostAt || null;
+ let lastError = state.lastError || null;
- const saveState = () => setState('changelog_push_x', { posted: [...posted], thread, recent });
+ const saveState = () =>
+ setState('changelog_push_x', { posted: [...posted], thread, recent, backoffUntil, lastPostAt, lastError });
- const { pending, backlog } = pendingEntries(feed, posted, X_LIMIT);
+ // X is a rate-limited showcase, not the full log: when there is more to say
+ // than the daily budget allows, the newest releases win (as on Telegram),
+ // otherwise a busy week keeps the lane posting entries days late.
+ const { pending, backlog } = pendingEntries(feed, posted, X_LIMIT, { newestWin: true });
if (pending.length === 0) {
await saveState(); // persist the pruned 24h window
return { posted: 0, backlog };
}
+ if (backoffUntil > Date.now()) {
+ return { posted: 0, backlog, skipped: 'rate_limited', until: new Date(backoffUntil).toISOString() };
+ }
if (recent.length >= X_DAILY_CAP) {
return { posted: 0, backlog, skipped: 'daily_cap' };
}
@@ -298,34 +336,74 @@ export async function pushXLane(feed) {
const payload = replyToId ? { reply: { in_reply_to_tweet_id: replyToId } } : undefined;
const { data } = await client.v2.tweet(text, payload);
recent.push(Date.now());
+ lastPostAt = new Date().toISOString();
return data.id;
};
let sent = 0;
let anchorPosted = null;
- try {
- if (!thread) {
- // One-time: the profile keeps a single pinned anchor; every release
- // chains under it as a reply. The owner pins the anchor once.
- const id = await post(ANCHOR_TEXT);
- thread = { anchor: id, last: id };
- anchorPosted = id;
- await saveState();
- await sleep(X_PACE_MS);
+ // The profile keeps a single pinned anchor; every release chains under it
+ // as a reply. The owner pins the anchor once, and again after a re-anchor.
+ const startThread = async () => {
+ let id;
+ try {
+ id = await post(ANCHOR_TEXT);
+ } catch (err) {
+ // Never let an anchor failure read as a per-entry duplicate below: that
+ // would mark releases posted that never went out.
+ if (classifyXError(err).kind !== 'duplicate') throw err;
+ throw Object.assign(new Error(`X rejected the thread anchor as duplicate content: ${err.message}`), { code: err.code });
}
+ thread = { anchor: id, last: id };
+ anchorPosted = id;
+ await saveState();
+ await sleep(X_PACE_MS);
+ };
+ // Reply under the chain. A deleted tweet in the chain would otherwise fail
+ // every tick forever: fall back to the anchor, then to a fresh anchor.
+ const postReply = async (text) => {
+ for (let attempt = 0; ; attempt++) {
+ if (!thread) await startThread();
+ try {
+ return await post(text, thread.last);
+ } catch (err) {
+ if (attempt >= 2 || classifyXError(err).kind !== 'thread_gone') throw err;
+ thread = thread.last !== thread.anchor ? { ...thread, last: thread.anchor } : null;
+ await saveState();
+ }
+ }
+ };
+
+ try {
for (const e of pending) {
if (recent.length >= X_DAILY_CAP) break;
- const id = await post(formatTweet(e), thread.last);
+ let id;
+ try {
+ id = await postReply(formatTweet(e));
+ } catch (err) {
+ if (classifyXError(err).kind !== 'duplicate') throw err;
+ posted.add(entryKey(e));
+ await saveState();
+ continue;
+ }
posted.add(entryKey(e));
thread = { ...thread, last: id };
sent++;
+ lastError = null;
// Persist after every post so a crash can't double-post or fork the chain.
await saveState();
await sleep(X_PACE_MS);
}
} catch (err) {
+ const verdict = classifyXError(err);
+ if (verdict.kind === 'rate_limited') {
+ backoffUntil = verdict.until;
+ await saveState().catch(() => {});
+ return { posted: sent, backlog, anchor: anchorPosted, skipped: 'rate_limited', until: new Date(backoffUntil).toISOString() };
+ }
+ lastError = { at: new Date().toISOString(), status: err?.code ?? null, message: String(err?.message || err).slice(0, 300) };
await saveState().catch(() => {});
- return { posted: sent, backlog, anchor: anchorPosted, error: String(err?.message || err) };
+ return { posted: sent, backlog, anchor: anchorPosted, error: lastError.message };
}
return { posted: sent, backlog, anchor: anchorPosted };
}