Skip to content

fix: keep inboxes alive past their session and compare ISO timestamps correctly - #4

Merged
bmc08gt merged 1 commit into
mainfrom
fix/inbox-lifetime-and-expiry
Oct 1, 2026
Merged

bmc08gt merged 1 commit into
mainfrom
fix/inbox-lifetime-and-expiry

Conversation

@bmc08gt

@bmc08gt bmc08gt commented Oct 1, 2026

Copy link
Copy Markdown
Member

Expired sessions were deleted unconditionally, and inboxes.session_id is ON DELETE CASCADE. A PRO inbox (90-day TTL) created by a 24h anonymous session was deleted, with its emails, webhooks and forwarding rules, when that session expired.

Changes

  • Session cleanup. Delete an expired session only if it owns no unexpired inbox (AND id NOT IN (SELECT session_id FROM inboxes WHERE expires_at > ?)). Kept sessions are still rejected at auth: SessionService.getSessionByToken already checks expires_at.
  • Tier resolution. The tier came from the creating session. EmailRoutes fell back to FREE once the session was gone, and the retention query inner-joined sessions, so orphaned inboxes were never retention-cleaned. InboxService.tierForInbox now uses inboxes.user_id -> users.has_pro / has_agent when a user is set, else the session tier, else FREE. If a user has both flags, PRO wins. Used for attachment stripping and the retention query. Webhook and forwarding tier checks only happen at rule creation, from the request's own session, so they are unchanged.
  • Timestamps. Columns are written with Instant.toString() (2026-09-30T10:00:00Z) but were compared to datetime('now') (2026-09-30 11:00:00). 'T' > ' ', so same-day expiry was delayed by up to a day. Cleanup (retention cutoffs, sniper sealing, inbox and session deletion), listInboxes and getInboxByAddress now bind Instant.now().toString(). The remaining datetime('now') uses are the created_at/received_at column defaults and verified_at in DomainService (writes, not comparisons), and webhook_deliveries.next_retry_at in WebhookService.retryFailedDeliveries, which is written as ISO; that comparison is left alone here.
  • Expired inboxes. getInbox and getInboxByAddress treat an expired inbox as not found, so ingest returns 404 for it.

Must land as one PR

The timestamp fix alone would make cleanup start expiring sessions that today linger up to a day past expiry, flushing them and cascading away their inboxes. The session guard has to ship with it.

Tests

The existing tests mock TursoClient, which cannot exercise cascades or string comparison. This adds SqliteTurso, an in-memory SQLite stand-in that runs the real Migrations with foreign keys on, plus a sqlite-jdbc test dependency, and InboxLifetimeSqlTest covering: an expired session with a live inbox is kept (inbox and emails survive); an expired session with only expired inboxes is deleted; an inbox that expired an hour ago today is deleted; tierForInbox prefers users.has_pro; retention follows the user tier; getInbox, getInboxByAddress and listInboxes hide expired inboxes. Existing mock-based tests were updated for the new bound parameters. SQLite is not libSQL, but the statements used are plain SQL.

Deploy

blip-cloud production pins blip via blip.sha after the pin PR, so this ships when blip-cloud bumps it.

Out of scope

  • A full user_id-ownership migration (moving inbox ownership off session_id). No schema change is made here: no table rebuild, no change to the foreign key.
  • Webhooks and forwarding rules are still owned by session_id; deleting an expired session that owns none of the live inboxes still cascades whatever hangs off that session.

@bmc08gt bmc08gt self-assigned this Oct 1, 2026
… correctly

CleanupService deleted every expired session. inboxes.session_id is
ON DELETE CASCADE, so a PRO inbox (90d TTL) created by a 24h anonymous
session was deleted along with its emails when the session expired.
Sessions are now deleted only if they own no unexpired inbox; expired
sessions are still rejected at auth by getSessionByToken.

Tier for inbox-scoped behaviour came from the creating session, which
falls back to FREE once the session is gone and left orphaned inboxes
out of retention cleanup. InboxService.tierForInbox now resolves
users.has_pro / has_agent via inboxes.user_id, then the session tier,
then FREE. It is used for attachment stripping and retention.

Timestamps are stored as ISO-8601 (Instant.toString()) but were compared
to datetime('now') ("YYYY-MM-DD HH:MM:SS"). 'T' sorts after ' ', so
same-day expiry was delayed by up to a day. Comparisons in cleanup,
listInboxes and getInboxByAddress now bind Instant.now().toString().
getInbox and getInboxByAddress treat expired inboxes as not found.

Tests run the real migrations on in-memory SQLite with foreign keys on.
@bmc08gt
bmc08gt force-pushed the fix/inbox-lifetime-and-expiry branch from b81e1fd to 35275fc Compare October 1, 2026 02:20
@bmc08gt
bmc08gt merged commit 082e1a3 into main Oct 1, 2026
3 checks passed
@bmc08gt
bmc08gt deleted the fix/inbox-lifetime-and-expiry branch October 1, 2026 02:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant