Skip to content

The agent that grows with you

fix(gateway): keep one failed flush file from aborting pending-message recovery

briandevans38c77f4c2172
diff --git a/gateway/shutdown_flush.py b/gateway/shutdown_flush.py
index afce3a19f01..3d3a11e0142 100644
--- a/gateway/shutdown_flush.py
+++ b/gateway/shutdown_flush.py
@@ -220,13 +220,20 @@ def recover_pending_to_db(session_db=None, *, session_resolver=None) -> int:
recovered = 0
try:
for path in flush_files:
- payload = json.loads(path.read_text(encoding="utf-8"))
- # Agent-history snapshots are for manual operator recovery, not automatic DB insertion.
- if payload.get("reason") == "shutdown-with-unpersisted-agent-history":
- continue
- if _recover_one_payload(session_db, path, payload, session_resolver=session_resolver):
- recovered += 1
- path.unlink(missing_ok=True)
+ # One unparseable payload or rejected append must only skip THIS file: the file is
+ # never unlinked, so aborting the pass would re-poison every later boot.
+ try:
+ payload = json.loads(path.read_text(encoding="utf-8"))
+ # Agent-history snapshots are for manual operator recovery, not automatic DB
+ # insertion.
+ if payload.get("reason") == "shutdown-with-unpersisted-agent-history":
+ continue
+ if _recover_one_payload(session_db, path, payload,
+ session_resolver=session_resolver):
+ recovered += 1
+ path.unlink(missing_ok=True)
+ except Exception as exc:
+ logger.warning("Failed to recover pending message from %s: %s", path, exc)
finally:
if own_db: # shutdown cancellation/interrupt must not strand an owned DB
with contextlib.suppress(Exception):