fix(core): keep buffered messages pending until their storage write succeeds
Description
MemoryPersistQueue.persist() drained the whole pending batch before saving anything, and ConversationBuffer.drainPendingMessages() cleared the pending set immediately. When a storage write failed mid-batch, the failed message and every unattempted message after it were no longer pending — a later flush found nothing to write and the persisted conversation history stayed incomplete. MemoryManager.saveMessage() compounded this by catching the storage error and resolving, so the queue acknowledged writes that never succeeded.
Changes:
ConversationBuffer.peekPendingMessages()returns the pending batch without touching the pending set;acknowledgeSavedMessage(snapshot)clears one message after its write succeeded, and skips the acknowledgment when the message changed since the snapshot — streaming updates that land during an in-flight write stay pending and are saved by a later flush. The active-assistant marker is cleared on success instead of on drain, so a failed flush keeps merging new chunks into the retried message.MemoryPersistQueuepersists the peeked snapshot and acknowledges exactly the messages that reached storage (also on the failure path), then rethrows so callers observe the failure.MemoryManager.saveMessage()rethrows after its span/log cleanup. Best-effort callers keep their current behavior:saveCurrentInputalready had a catch, and the two directagent.tspersistence sites now wrap the call so their best-effort semantics are explicit.drainPendingMessages()stays as-is for compatibility.
Fixes #1425
Summary by cubic
Fixes conversation history being silently lost when a storage write fails mid-batch. MemoryPersistQueue used to drain and clear the entire pending batch before saving anything; now it peeks the batch, saves message by message, and acknowledges each only after its write succeeds, so a failure leaves the failed message plus the unattempted rest pending for the next flush. MemoryManager.saveMessage now rethrows storage errors instead of resolving, and the two persistence call sites in agent.ts wrap the call to preserve their best-effort behavior.
Behavior
- A message updated by streaming during an in-flight write stays pending and is saved with its newer content.
- The active-assistant marker is cleared on successful save rather than on drain.
drainPendingMessagesremains unchanged for compatibility.
Summary by CodeRabbit
- Bug Fixes
- Messages that fail to save, along with messages not yet attempted, remain available for a later save attempt.
- Updates made to a message while it is being saved are preserved for a subsequent save.
- Failures to save generated assistant messages no longer prevent the generated result from being processed.