fix: only advance dream_cursor on completed batches to prevent silent loss

This commit is contained in:
Jiajun Xie 2026-05-05 09:24:02 +00:00 committed by Xubin Ren
parent c30e4d86f3
commit 9fa90b1034

View File

@ -974,12 +974,10 @@ class Dream:
if event["status"] == "ok": if event["status"] == "ok":
changelog.append(f"{event['name']}: {event['detail']}") changelog.append(f"{event['name']}: {event['detail']}")
# Advance cursor — always, to avoid re-processing Phase 1 # Only advance cursor on successful completion to prevent silent loss
if result and result.stop_reason == "completed":
new_cursor = batch[-1]["cursor"] new_cursor = batch[-1]["cursor"]
self.store.set_last_dream_cursor(new_cursor) self.store.set_last_dream_cursor(new_cursor)
self.store.compact_history()
if result and result.stop_reason == "completed":
logger.info( logger.info(
"Dream done: {} change(s), cursor advanced to {}", "Dream done: {} change(s), cursor advanced to {}",
len(changelog), new_cursor, len(changelog), new_cursor,
@ -987,10 +985,12 @@ class Dream:
else: else:
reason = result.stop_reason if result else "exception" reason = result.stop_reason if result else "exception"
logger.warning( logger.warning(
"Dream incomplete ({}): cursor advanced to {}", "Dream incomplete ({}): cursor NOT advanced, will retry next cron cycle",
reason, new_cursor, reason,
) )
self.store.compact_history()
# Git auto-commit (only when there are actual changes) # Git auto-commit (only when there are actual changes)
if changelog and self.store.git.is_initialized(): if changelog and self.store.git.is_initialized():
ts = batch[-1]["timestamp"] ts = batch[-1]["timestamp"]