sync

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

ABOUTME: Daemon process orchestrating fsnotify watcher, periodic reconciler, and single-writer sync loop. ABOUTME: Watches agentsview SQLite for changes and triggers sync engine with debounce and status reporting.

ABOUTME: Core sync orchestration: read-mask-write pipeline with incremental message append. ABOUTME: Composes reader, secrets, and store into an idempotent sync engine with watermark tracking.

ABOUTME: Watermark state persistence for idempotent sync. ABOUTME: Tracks which sessions have been synced via per-session hash and last ordinal.

Index

Constants

View Source
const SyncVersion = 3

SyncVersion is incremented when the sync logic changes in a way that requires a full re-sync of all sessions.

Variables

This section is empty.

Functions

This section is empty.

Types

type Daemon

type Daemon struct {
	// contains filtered or unexported fields
}

Daemon watches for agentsview SQLite changes and triggers sync.

func NewDaemon

func NewDaemon(cfg *config.Config) (*Daemon, error)

NewDaemon creates a daemon from config, initializing reader, store, and sync engine.

func NewDaemonWithDeps

func NewDaemonWithDeps(cfg *config.Config, s store.Store, r *reader.Reader) (*Daemon, error)

NewDaemonWithDeps creates a daemon using a pre-built store and reader. The caller is responsible for closing the store and reader if this returns an error.

func (*Daemon) Close

func (d *Daemon) Close() error

Close releases daemon resources (reader and store).

func (*Daemon) Run

func (d *Daemon) Run(ctx context.Context) error

Run starts the watcher, reconciler, and writer goroutines and blocks until the context is cancelled. On cancellation, the writer loop finishes its current sync cycle before returning.

func (*Daemon) Status

func (d *Daemon) Status() DaemonStatus

Status returns a snapshot of the current daemon status.

type DaemonStatus

type DaemonStatus struct {
	LastSyncAt           time.Time
	TotalSessionsSynced  int64
	TotalSecretsDetected int64
	LastError            string
	Running              bool
}

DaemonStatus reports the current state of the daemon.

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine orchestrates the read-mask-write sync pipeline.

func NewEngine

func NewEngine(cfg *config.Config, r *reader.Reader, st store.Store) (*Engine, error)

NewEngine creates a sync engine. It loads the watermark state from the config DataDir, or initializes empty state if no file exists.

func (*Engine) ForceResync

func (e *Engine) ForceResync()

ForceResync resets the watermark state so the next RunOnce will re-sync all sessions.

func (*Engine) RunOnce

func (e *Engine) RunOnce(ctx context.Context) (*SyncResult, error)

RunOnce executes one sync cycle: reads changed sessions, masks secrets, writes to the store, and advances the watermark. Returns a result summarizing what happened.

type SessionWatermark

type SessionWatermark struct {
	FileHash    string `json:"file_hash"`
	LastOrdinal int    `json:"last_ordinal"`
}

SessionWatermark holds the sync position for a single session.

type SyncResult

type SyncResult struct {
	SessionsSynced  int
	SessionsSkipped int
	SecretsDetected int
	Errors          map[string]error
}

SyncResult reports what happened during a single RunOnce invocation.

type SyncState

type SyncState struct {
	Version              int                         `json:"version"`
	LastSessionCreatedAt string                      `json:"last_session_created_at"`
	Sessions             map[string]SessionWatermark `json:"sessions"`
	LastSyncedAt         time.Time                   `json:"last_synced_at"`
	TotalSynced          int64                       `json:"total_synced"`
	TotalMasked          int64                       `json:"total_masked"`
}

SyncState tracks the sync position and per-session watermarks for idempotency.

func LoadSyncState

func LoadSyncState(path string) (*SyncState, error)

LoadSyncState reads sync state from a JSON file. Returns an empty state if the file does not exist.

func (*SyncState) GetLastOrdinal

func (s *SyncState) GetLastOrdinal(sessionID string) int

GetLastOrdinal returns the last synced ordinal for a session, or -1 if not tracked.

func (*SyncState) IsSessionChanged

func (s *SyncState) IsSessionChanged(sessionID, fileHash string) bool

IsSessionChanged returns true if the session's file hash differs from the stored hash, or if the session has not been tracked. Used to detect sessions that need re-syncing.

func (*SyncState) MarkSynced

func (s *SyncState) MarkSynced(sessionID, fileHash string, lastOrdinal int, createdAt string)

MarkSynced records a session as synced with its file hash and last ordinal, and advances the watermark if createdAt is later than the current position.

func (*SyncState) NeedsFullResync

func (s *SyncState) NeedsFullResync(currentVersion int) bool

NeedsFullResync returns true if the stored version is older than currentVersion, indicating all sessions must be re-synced from scratch.

func (*SyncState) ResetForResync

func (s *SyncState) ResetForResync(newVersion int)

ResetForResync clears all session watermarks and sets the new version, forcing a full re-sync on the next sync run.

func (*SyncState) Save

func (s *SyncState) Save(path string) error

Save writes the sync state to a JSON file.

Jump to

Keyboard shortcuts

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