Skip to content

The agent that grows with you

fix(gateway): replay the pending spool before boot resume turns and queued inbound

unsupportedpastels557e61d226e7
diff --git a/gateway/run.py b/gateway/run.py
index 591473ce736..657c04fd93b 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -5968,13 +5968,6 @@ async def start_gateway(config: Optional[GatewayConfig] = None, replace: bool =
if not success:
_shutdown_gateway_health_export(runner)
return False
-
- def _recover_pending() -> None:
- recovered = _recover_pending_flushes(runner)
- if recovered:
- logger.info("Recovered %d pending message(s) from shutdown flush", recovered)
-
- _best_effort(_recover_pending)
if runner.should_exit_cleanly:
_shutdown_gateway_health_export(runner)
if runner.exit_reason:
diff --git a/gateway/run_startup.py b/gateway/run_startup.py
index a20ba923f33..fbcf3863888 100644
--- a/gateway/run_startup.py
+++ b/gateway/run_startup.py
@@ -1507,7 +1507,8 @@ class GatewayStartupMixin:
async def _start_finish_wiring(self, connected_count: int) -> None:
"""Post-connect wiring: services, boot notifications, startup restore, recovered watchers."""
- from gateway.run import _planned_restart_notification_pending, _restart_notification_pending
+ from gateway.run import (
+ _planned_restart_notification_pending, _recover_pending_flushes, _restart_notification_pending)
await self._start_post_connect_services(connected_count)
# Let fresh adapters settle before lifecycle sends (helps Discord thread deliveries).
if connected_count > 0:
@@ -1523,6 +1524,12 @@ class GatewayStartupMixin:
await self._await_startup_boot_sends(
planned_restart_notification_pending=_planned_restart_notification_pending(),
)
+ # Replay the previous run's pending spool before resume turns and queued inbound write live
+ # rows: only this pass tells the store which sessions have spooled rows, and a live row
+ # written first would get a lower row id than them for good.
+ with _log_suppressed(logging.WARNING, "Pending-message recovery failed: %s"):
+ if recovered := _recover_pending_flushes(self):
+ logger.info("Recovered %d pending message(s) from shutdown flush", recovered)
# Auto-resume restart-interrupted sessions (ledger-answered ones were cleared above); a failed
# auto-resume stays visible on the next user message.
self._schedule_resume_pending_sessions()
diff --git a/tests/gateway/test_shutdown_flush_recovery.py b/tests/gateway/test_shutdown_flush_recovery.py
index 8cbf5f03c71..e8ce49f06b0 100644
--- a/tests/gateway/test_shutdown_flush_recovery.py
+++ b/tests/gateway/test_shutdown_flush_recovery.py
@@ -228,3 +228,33 @@ def test_live_write_waits_while_held_back_files_still_fail_to_drain(flush_dir, m
assert _contents(mock_db) == ["old0", "old0", "old0", "old1", "new-live", "new-live-2"]
assert not list(flush_dir.glob("*.json"))
assert not store._dirty_transcripts
+
+
+def test_boot_recovery_runs_before_resume_turns_and_queued_inbound(monkeypatch):
+ """Resume turns and queued inbound write live rows. The store knows nothing about the previous
+ run's spool until recovery has run, so a live row written first lands ahead of it for good."""
+ import asyncio
+
+ import gateway.run as gateway_run
+
+ order = []
+ monkeypatch.setattr(gateway_run, "_recover_pending_flushes",
+ lambda runner: order.append("recover") or 0)
+ monkeypatch.setattr(gateway_run, "_restart_notification_pending", lambda: False)
+ monkeypatch.setattr(gateway_run, "_planned_restart_notification_pending", lambda: False)
+
+ async def noop(*_args, **_kwargs):
+ return None
+
+ async def finish_startup_restore():
+ order.append("drain inbound")
+
+ runner = SimpleNamespace(
+ _start_post_connect_services=noop, _await_startup_boot_sends=noop,
+ _schedule_resume_pending_sessions=lambda: order.append("resume"),
+ _finish_startup_restore=finish_startup_restore,
+ _send_session_db_warning_notifications=noop, _spawn_supervised=lambda *a, **k: None,
+ )
+ asyncio.run(gateway_run.GatewayRunner._start_finish_wiring(runner, 0))
+
+ assert order == ["recover", "resume", "drain inbound"]