sync

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sync provides write-through durability and ring-buffer synchronization for concurrent Engram sessions. When multiple agent sessions write memories simultaneously, this package ensures no writes are lost between Qdrant (vector store) and a local BoltDB commit log.

Architecture:

Session A ──┐
Session B ──┼──► WriteThrough ──► Qdrant (vector store)
Session C ──┘         │
                       └──► BoltDB (commit log)
                             ▲
                    RingBuffer (in-memory, 200 entries)
                    flush every 5 minutes

Usage:

store := sync.NewWriteThrough(qdrantStore, boltPath)
// store implements memory.Store, use as drop-in replacement

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewWriteThrough

func NewWriteThrough(store memory.Store, boltPath string, cfg RingBufferConfig) (WriteThroughStore, CommitLog, error)

NewWriteThrough creates a WriteThroughStore backed by a memory.Store and BoltDB commit log.

Types

type CommitEntry

type CommitEntry struct {
	ID        string         `json:"id"`
	Op        CommitOp       `json:"op"` // "insert" | "delete" | "update"
	Memory    *memory.Memory `json:"mem,omitempty"`
	Fields    map[string]any `json:"fields,omitempty"` // for update ops
	Timestamp float64        `json:"ts"`
}

CommitEntry represents a single write operation persisted to the commit log.

type CommitLog

type CommitLog interface {
	// Append durably persists a commit entry.
	Append(ctx context.Context, entry CommitEntry) error

	// ReadSince returns all entries after the given timestamp (for recovery).
	ReadSince(ctx context.Context, since float64) ([]CommitEntry, error)

	// Truncate removes entries older than the given timestamp.
	Truncate(ctx context.Context, before float64) error

	// Close releases resources.
	Close() error
}

CommitLog is the durable write-ahead log backend. Implementations must be safe for concurrent use.

func NewBoltCommitLog

func NewBoltCommitLog(path string) (CommitLog, error)

NewBoltCommitLog opens (or creates) a BoltDB file and returns a CommitLog.

type CommitOp

type CommitOp string

CommitOp is the type of write operation.

const (
	OpInsert CommitOp = "insert"
	OpDelete CommitOp = "delete"
	OpUpdate CommitOp = "update"
)

type RingBufferConfig

type RingBufferConfig struct {
	// Capacity is the maximum number of entries held in memory before flush.
	// Default: 200.
	Capacity int

	// FlushInterval is how often the buffer is persisted to the CommitLog.
	// Default: 5 minutes.
	FlushInterval time.Duration
}

RingBufferConfig configures the in-memory ring buffer.

func DefaultRingBufferConfig

func DefaultRingBufferConfig() RingBufferConfig

DefaultRingBufferConfig returns sensible defaults (200 entries, 5min flush).

type SyncStats

type SyncStats struct {
	TotalWrites     int64     // Total write ops processed
	BufferedEntries int       // Current ring buffer size
	LastFlushAt     time.Time // When the buffer was last flushed
	PendingFlush    int       // Entries not yet persisted to CommitLog
}

SyncStats holds runtime statistics for the write-through layer.

type WriteThroughStore

type WriteThroughStore interface {
	memory.Store

	// Replay returns all commit entries since the given timestamp.
	// Used by sessions starting up to catch up on concurrent writes.
	Replay(ctx context.Context, since float64) ([]CommitEntry, error)

	// Flush forces the ring buffer to persist to the CommitLog immediately.
	Flush(ctx context.Context) error

	// Stats returns sync-specific statistics.
	SyncStats() SyncStats
}

WriteThroughStore wraps a memory.Store with synchronous commit-log durability. Every Insert, Delete, and Update call is first written to the CommitLog, then forwarded to the underlying store. Reads are served directly from the store.

The ring buffer holds the most recent entries in memory to serve fast replay requests (e.g., session startup sync) without hitting BoltDB.

func NewWriteThroughWithLog

func NewWriteThroughWithLog(store memory.Store, log CommitLog, cfg RingBufferConfig) WriteThroughStore

NewWriteThroughWithLog creates a WriteThroughStore with a provided CommitLog (useful for testing).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL