Decide whether the bot's timers should keep the database compute awake continuously

Open
#40 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
12/100
Issue type
Refactor
Clarity
Needs clarification
Activity status
Active
Tech stack
postgresql, typescript

Research direction

This is a design decision, not a code change. Start in bot/index.ts by reading the timer constants (LISTEN_HEARTBEAT_INTERVAL_MS, the 5-minute sweeps) and in lib/slack/poller.ts the Slack poll interval, then check railway.bot.toml for deployment settings. Done means a written decision on the four options, coordinated with #128 / PR #130 for the dashboard heartbeat, and any follow-up code is agreed with maintainers first.

Written by the indexing model from the issue text.

Description

backend question

Copied from DeadantCo/answerLoops#30 · originally opened by sethr07 on 2026-11-20

Copied from https://github.com/answerLoops/answerLoops/issues/131 — originally opened by @FaithTarbert on 2026-09-08

Background

The bot is a long-running process, not a webhook receiver: it holds a Discord Gateway connection (discord.js Client with GatewayIntentBits), is started as tsx bot/index.ts, and is deployed by railway.bot.toml with restartPolicyType = "on_failure". It must stay up to ingest Discord messages — app/api/discord/* is OAuth and channel listing only, with no HTTP ingestion path.

While it runs, several of its timers query the database often enough that a serverless compute can never reach its idle window (Neon's default is 5 minutes):

Timer (constant in bot/index.ts) Interval What it does
LISTEN_HEARTBEAT_INTERVAL_MS 4 min SELECT 1 on the dedicated LISTEN connection; a failure triggers reconnect and re-LISTEN. Database work every beat.
STUCK_TICKET_SWEEP_INTERVAL_MS 5 min getStuckPendingTickets(), then forwards each stuck ticket to /api/ingest/retry-stuck. Skipped entirely when BOT_SECRET is unset.
EMBEDDING_MIGRATION_SWEEP_INTERVAL_MS 5 min Calls the app route that finds and re-embeds workspaces whose vectors are not on their target model (the request itself is bounded by EMBEDDING_MIGRATION_REQUEST_TIMEOUT_MS). Skipped without BOT_SECRET.
KB_SYNC_SAFETY_SWEEP_INTERVAL_MS 5 min Safety net for the knowledge-base sync queue: reclaims jobs stuck in running (KB_SYNC_STUCK_THRESHOLD_MS) and claims queued ones. The queue is normally driven by a kb_sync_job_queued notification, so this only matters when a notification is dropped.
KB_RETENTION_SWEEP_INTERVAL_MS 24 hours Blanks visitor text on old review items and prunes expired article-usage rows. Also runs once at startup.
ORG_PURGE_SWEEP_INTERVAL_MS 6 hours Hard-purges workspaces whose deletion grace period has elapsed. One indexed query when nothing is due. Also runs once at startup.
Slack poller (DEFAULT_INTERVAL_MS in lib/slack/poller.ts, from SLACK_POLL_INTERVAL_SECONDS) 60 sec No work on cloud unless a workspace has the slack_force_polling flag, because the bot only hands it the flagged orgs. Does database and network work when self-hosted.

The three 5-minute sweeps and the 4-minute heartbeat all sit at or under a 5-minute idle window, so each one on its own is enough to keep a serverless compute awake. The 6-hour and 24-hour sweeps are negligible.

The effect is that the compute runs continuously whenever the bot is up, rather than suspending when nothing is happening. Because this is a single database with row-level tenancy (org_id), that is one shared compute for every tenant — there is no per-customer endpoint to suspend independently, so this is a fixed floor rather than something that scales with usage.

Why this is worth a decision

The 4-minute heartbeat interval appears chosen to sit under a 5-minute autosuspend window, i.e. it prevents suspension as much as it detects a dead connection. That was a reasonable trade when it was written — #71 / #77 exist because a postgres.js LISTEN goes silently stale on a suspending compute, with nothing logged and config hot-reload quietly dead until the process restarted.

But the cost of that choice is now the dominant term in idle database spend, and it is worth being deliberate about rather than inheriting. Note that lengthening the interval alone does not recover suspension: any periodic query restarts the idle countdown, so a 10-minute beat keeps the compute up roughly half the time rather than all of it. The 5-minute sweeps have the same property and would all have to move too.

Options

  1. Accept it. Config changes reach the bot within seconds and a dead LISTEN is caught within four minutes. Simplest, and defensible if compute cost is not a real constraint.
  2. Lengthen the heartbeat and all of the 5-minute sweeps together, accepting slower config propagation and slower stuck-ticket recovery in exchange for partial suspension. Only meaningful if both move past the idle window.
  3. Drop the periodic heartbeat and re-check config lazily. The bot only needs current config when it processes a message — and processing a message already queries the database, which wakes the compute anyway. A config version check at message time (with LISTEN kept as a fast path rather than the source of truth) would let the compute suspend while genuinely idle, at the cost of a config change not reaching an idle bot until its next message. Needs care: this is the failure #71 / #77 were about, so it should be correct-by-construction rather than relying on the LISTEN staying alive.
  4. Make the stuck-ticket sweep (and the embedding migration sweep) event-driven rather than 5-minute polls, the way the KB sync worker already is, independently of whichever heartbeat option is chosen.

Interaction with #128

Decide together with #128 / PR #130 (dashboard heartbeat).

PR #130 adds a 4-minute heartbeat to the dashboard's own SSE LISTEN connection. That was justified partly on the grounds that it costs nothing extra in compute-hours because the bot already holds the compute awake. If this issue is resolved by letting the compute suspend, that reasoning no longer holds and the dashboard heartbeat becomes the thing preventing suspension whenever a tab is open. The two should be decided together.

The dashboard case is less severe — its stream exists only while a tab is open, and closes when the tab is backgrounded — but it would need revisiting.

Acceptance

Decide and document. If the decision is to change behaviour, the bot should still detect and recover from a dead LISTEN connection without relying on a process restart, and a config change should still reach the bot before it acts on the next message.

Start here: new to the project? Read CONTRIBUTING.md first. It covers the ground rules, running answerLoops locally, and how to open a pull request.

Dominant language
TypeScript
Stars
0
Forks
0
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from DeadantCo/answerLoopsdev

All issues in DeadantCo/answerLoopsdev

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.