Skip to content

The agent that grows with you

fix(gateway): fall back to the payload clock only when a timestamp is absent

briandevans4a36bf7783f7
diff --git a/gateway/shutdown_flush.py b/gateway/shutdown_flush.py
index 19a01dcdcb9..e5c3313852c 100644
--- a/gateway/shutdown_flush.py
+++ b/gateway/shutdown_flush.py
@@ -284,6 +284,11 @@ def _transcript_append_kwargs(
# Reasoning columns are assistant-only in the live writer; copying them
# onto another role would fabricate rows the gateway never produces.
is_assistant = message.get("role") == "assistant"
+ # Only a *missing* timestamp falls back to the payload clock. A truthiness
+ # test would rewrite epoch 0, which is a valid timestamp.
+ timestamp = message.get("timestamp")
+ if timestamp is None:
+ timestamp = payload.get("ts")
return {
"session_id": session_id,
"role": message.get("role", "unknown"),
@@ -296,7 +301,7 @@ def _transcript_append_kwargs(
message.get("platform_message_id") or message.get("message_id")
),
"observed": bool(message.get("observed")),
- "timestamp": message.get("timestamp") or payload.get("ts"),
+ "timestamp": timestamp,
# The api_content sidecar is the exact bytes sent to the API for this
# row; the live writer requires it to survive every persistence path
# or the next replay diverges at this row.
diff --git a/tests/gateway/test_shutdown_flush_recovery.py b/tests/gateway/test_shutdown_flush_recovery.py
index 58f73228514..4b3d984f6b6 100644
--- a/tests/gateway/test_shutdown_flush_recovery.py
+++ b/tests/gateway/test_shutdown_flush_recovery.py
@@ -230,6 +230,19 @@ class TestSpoolReplayFidelity:
== "tg-7"
)
+ def test_epoch_zero_timestamp_is_not_replaced_by_the_fallback(
+ self, flush_dir
+ ):
+ """0 is a valid timestamp; only a missing one may fall back."""
+ _write_spool(
+ flush_dir, "pending-aaa.json", "sess-1",
+ {"role": "user", "content": "hi", "timestamp": 0}, ts=999, seq=0,
+ )
+
+ mock_db = MagicMock()
+ assert recover_pending_to_db(mock_db) == 1
+ assert mock_db.append_message.call_args.kwargs["timestamp"] == 0
+
def test_payload_ts_is_the_timestamp_fallback(self, flush_dir):
_write_spool(
flush_dir, "pending-aaa.json", "sess-1",