Skip to content

The agent that grows with you

fix(gateway): an out-of-range spool ts or seq no longer aborts recovery

John Paul Soliva0eb265543ceb
diff --git a/gateway/shutdown_flush.py b/gateway/shutdown_flush.py
index 37c5b63afea..a1c873ae624 100644
--- a/gateway/shutdown_flush.py
+++ b/gateway/shutdown_flush.py
@@ -15,6 +15,7 @@ import contextlib
import itertools
import json
import logging
+import math
import os
import time
import uuid
@@ -212,11 +213,17 @@ def _sort_number(value: Any) -> float:
Ordering fields are read back from JSON on disk and may be missing or
corrupt. A sort key that mixes ``str`` and ``int`` raises ``TypeError``
and would abort the entire recovery pass, so anything non-numeric is
- normalised to ``0.0``.
+ normalised to ``0.0``. So is a JSON integer too large for a float
+ (``float(10**400)`` raises ``OverflowError``) and a NaN/Infinity literal
+ (NaN compares false both ways and would scramble the sort).
"""
if isinstance(value, bool) or not isinstance(value, (int, float)):
return 0.0
- return float(value)
+ try:
+ number = float(value)
+ except OverflowError:
+ return 0.0
+ return number if math.isfinite(number) else 0.0
def _order_flush_files(paths) -> list[tuple[Path, Optional[Dict[str, Any]]]]:
diff --git a/tests/gateway/test_shutdown_flush_recovery.py b/tests/gateway/test_shutdown_flush_recovery.py
index dd358bb9641..0776cb38273 100644
--- a/tests/gateway/test_shutdown_flush_recovery.py
+++ b/tests/gateway/test_shutdown_flush_recovery.py
@@ -78,6 +78,19 @@ def test_replays_in_drop_order_not_file_name_order(flush_dir):
assert _contents(mock_db) == ["first", "second", "third"]
+def test_an_out_of_range_ordering_field_does_not_abort_recovery(flush_dir):
+ """Ordering runs before the per-file error handling, so one file whose ``ts`` is a JSON
+ integer no float can hold must not stop every other file from being recovered."""
+ _write_spool(flush_dir, "pending-bad.json", "sess-1",
+ {"role": "user", "content": "huge ts"}, ts=10**400, seq=0)
+ _write_spool(flush_dir, "pending-ok.json", "sess-2",
+ {"role": "user", "content": "valid"}, ts=100, seq=1)
+
+ mock_db = MagicMock()
+ assert recover_pending_to_db(mock_db) == 2
+ assert sorted(_contents(mock_db)) == ["huge ts", "valid"]
+
+
def test_a_replayed_row_is_the_row_the_live_writer_writes(flush_dir):
"""Every field the live drain persists survives the restart round trip: losing
tool_call_id orphans a tool result, losing api_content makes the next replay diverge,