Skip to content

The agent that grows with you

fix(gateway): a parseable non-dict transcript spool file no longer aborts drain_transcript_spool

teknium11c121280ac1a
diff --git a/gateway/shutdown_flush.py b/gateway/shutdown_flush.py
index 21cc61b3053..ee2fc742737 100644
--- a/gateway/shutdown_flush.py
+++ b/gateway/shutdown_flush.py
@@ -149,7 +149,10 @@ def drain_transcript_spool(session_id: str, replay) -> tuple[int, int]:
payload = json.loads(path.read_text(encoding="utf-8"))
except Exception:
continue
- if (payload.get("reason") != TRANSCRIPT_CAP_DROP_REASON
+ # A parseable non-object file (scalar/list) cannot be attributed to any session: skip it
+ # like unparseable JSON instead of letting ``.get`` abort the whole drain.
+ if (not isinstance(payload, dict)
+ or payload.get("reason") != TRANSCRIPT_CAP_DROP_REASON
or payload.get("session_key") != session_id):
continue
message = (payload.get("data") or {}).get("message")
diff --git a/tests/gateway/test_shutdown_flush.py b/tests/gateway/test_shutdown_flush.py
index 8c1b6e9a20a..3d370dac1a5 100644
--- a/tests/gateway/test_shutdown_flush.py
+++ b/tests/gateway/test_shutdown_flush.py
@@ -327,3 +327,18 @@ def test_flushed_overflow_is_replayed_by_recover_pending_to_db(tmp_path, monkeyp
def test_flush_overflow_noop_on_empty():
assert flush_overflow_to_file({}) == 0
assert flush_overflow_to_file({"k": []}) == 0
+
+
+def test_drain_transcript_spool_skips_parseable_non_dict_payload(tmp_path, monkeypatch):
+ """A scalar/list JSON spool file must not abort the drain; the healthy payload still replays."""
+ from gateway.shutdown_flush import drain_transcript_spool, spool_dropped_transcript_message
+
+ flush_dir = _make_flush_dir(tmp_path)
+ monkeypatch.setattr("gateway.shutdown_flush._get_flush_dir", lambda: flush_dir)
+ (flush_dir / "pending-00-scalar.json").write_text("42", encoding="utf-8")
+ (flush_dir / "pending-01-list.json").write_text("[1, 2]", encoding="utf-8")
+ spool_dropped_transcript_message("sess-1", {"role": "user", "content": "hi"})
+
+ replayed = []
+ assert drain_transcript_spool("sess-1", replayed.append) == (1, 0)
+ assert replayed == [{"role": "user", "content": "hi"}]