/** * DELIVERIES KEPT AND TRIED AGAIN (RESTORE 2 SERVER 3, the coordinator's open point 1, 27 Nov 2026). A frozen account * (src/restore.ts) refuses every call from another object with "account_frozen: … not delivered, try again", and one * that could not say answers "try_again". Before this, the senders of chat messages, sends, invitations and secret * shares handed over fire-and-forget and swallowed any refusal: a delivery to a frozen account was lost. * * WHY THE SENDER KEEPS IT (not a holding table on the receiver): the receiver is the account being restored, and a * point-in-time restore puts its whole storage back, a holding table with it; keeping deliveries outside its storage * would mean parking other people's sealed boxes in R2. The sender's storage is not touched by the receiver's * restore, already holds the sealed delivery, and has an alarm. So a delivery refused as retryable is kept here, in the * sender's object, and tried again on its alarm with a growing wait, for up to two days; any other refusal (blocked, * over a limit, not one of their people) stays what it was: dropped unsaid. */ /** `secret_take` (AUDIT D): a take-back the holder's object could not take yet (frozen), tried again until it lands. */ export type Kind = "chat" | "send" | "invite" | "secret" | "secret_take"; export const SCHEMA = `CREATE TABLE IF NOT EXISTS handover_retry ( id INTEGER PRIMARY KEY AUTOINCREMENT, kind TEXT NOT NULL, to_number TEXT NOT NULL, delivery TEXT NOT NULL, tries INTEGER NOT NULL DEFAULT 0, next_at INTEGER NOT NULL, first_at INTEGER NOT NULL )`; /** How long a delivery is tried for, the waits between tries, and what one sender keeps at most. */ export const KEEP_MS = 2 * 24 * 3_600_000; const WAITS_MS = [60_000, 5 * 60_000, 15 * 60_000, 60 * 60_000]; export const MAX_KEPT = 500; const MAX_BYTES = 256 * 1024; /** Whether a refusal from the receiving object means "not delivered, try again". */ export function retryable(error: unknown): boolean { // By code, never by words (RESTORE 2 SERVER 4, L5): a refusal's own `code` when it is here, or the code that leads // its message (`: …`, which a call between objects keeps when it drops every other field). const code = (error as { code?: unknown } | null)?.code; if (code === RETRY_CODES[0] || code === RETRY_CODES[1]) return true; const message = error instanceof Error ? error.message : String(error); const lead = /^([a-z_]+):/.exec(message)?.[1]; return lead === RETRY_CODES[0] || lead === RETRY_CODES[1]; } /** The codes that mean "not delivered, try again": a frozen account, and one that could not say whether it is. */ export const RETRY_CODES = ["account_frozen", "try_again"] as const; /** A delivery refused as retryable, kept to be tried again (the oldest kept first; none past the cap or the size). */ export function keep(sql: SqlStorage, kind: Kind, to: string, delivery: unknown, now = Date.now()): boolean { sql.exec(SCHEMA); const text = JSON.stringify(delivery); if (text.length > MAX_BYTES) return false; if (sql.exec<{ n: number }>("SELECT count(*) AS n FROM handover_retry").one().n >= MAX_KEPT) return false; sql.exec("INSERT INTO handover_retry (kind, to_number, delivery, tries, next_at, first_at) VALUES (?, ?, ?, 0, ?, ?)", kind, to, text, now + WAITS_MS[0]!, now); return true; } /** When the next kept delivery is due, or null. */ export function nextDue(sql: SqlStorage): number | null { try { return sql.exec<{ at: number | null }>("SELECT min(next_at) AS at FROM handover_retry").one().at ?? null; } catch { return null; } } /** * The deliveries due, tried again: delivered or refused for good, they go; refused as retryable, they wait longer; * two days old, they go. `deliver` finds the number's object and hands it over (throwing what the receiver threw). */ export async function retryDue(sql: SqlStorage, deliver: (kind: Kind, to: string, delivery: unknown) => Promise, now = Date.now()): Promise<{ delivered: number; waiting: number; dropped: number }> { const done = { delivered: 0, waiting: 0, dropped: 0 }; let due: { id: number; kind: Kind; to_number: string; delivery: string; tries: number; first_at: number }[]; try { due = sql.exec<{ id: number; kind: Kind; to_number: string; delivery: string; tries: number; first_at: number }>("SELECT id, kind, to_number, delivery, tries, first_at FROM handover_retry WHERE next_at <= ? ORDER BY id LIMIT 50", now).toArray(); } catch { return done; } for (const row of due) { try { await deliver(row.kind, row.to_number, JSON.parse(row.delivery)); sql.exec("DELETE FROM handover_retry WHERE id = ?", row.id); done.delivered += 1; } catch (error) { if (retryable(error) && now - row.first_at < KEEP_MS) { const wait = WAITS_MS[Math.min(row.tries + 1, WAITS_MS.length - 1)]!; sql.exec("UPDATE handover_retry SET tries = tries + 1, next_at = ? WHERE id = ?", now + wait, row.id); done.waiting += 1; } else { sql.exec("DELETE FROM handover_retry WHERE id = ?", row.id); done.dropped += 1; } } } return done; }