feat(memory): add Sync lifecycle method and graceful shutdown sync

MemoryStore.Sync() delegates to underlying replica syncer if available.
Agent loop now syncs memory before closing on shutdown, preventing data
loss for Turso embedded replicas.
This commit is contained in:
ZanzyTHEbar 2026-02-18 13:08:50 +00:00
parent 26f4df50b4
commit 16fb2e2c95
2 changed files with 14 additions and 0 deletions

View file

@ -274,6 +274,10 @@ func (al *AgentLoop) Run(ctx context.Context) error {
func (al *AgentLoop) Stop() {
al.running.Store(false)
if al.memoryStore != nil {
if err := al.memoryStore.Sync(); err != nil {
logger.WarnCF("agent", "Failed to sync memory before shutdown",
map[string]interface{}{"error": err.Error()})
}
al.memoryStore.Close()
}
}

View file

@ -500,6 +500,16 @@ func (m *MemoryStore) OffloadToolResult(ctx context.Context, toolName, content,
// --- Lifecycle ---
// Sync flushes pending writes to the remote replica (Turso).
// No-op if the underlying delegate doesn't support replication.
func (m *MemoryStore) Sync() error {
type syncer interface{ Sync() error }
if s, ok := m.delegate.(syncer); ok {
return s.Sync()
}
return nil
}
func (m *MemoryStore) Close() error {
return m.delegate.Close()
}