syncer

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 19 Imported by: 0 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 syncer uploads spooled events to the server: it resolves local project/session hints to server IDs, uploads in batches, deletes events only after the server ACKs them, and backs off exponentially while the server is unreachable.

Index

Constants

This section is empty.

Variables

View Source
var ErrBackoff = errors.New("in backoff after a failed sync")

ErrBackoff means the previous attempt failed and the retry delay has not elapsed. Use force to retry immediately.

View Source
var ErrLocked = errors.New("another sync is running")

ErrLocked means another sync is already running.

View Source
var ErrNotConnected = errors.New("device not connected (run 'memspace client connect <server-url>')")

ErrNotConnected means `memspace client connect` has not been run.

Functions

func SaveCredentials

func SaveCredentials(path string, c *Credentials) error

SaveCredentials writes the credentials file with owner-only permissions.

func ShouldTrigger

func ShouldTrigger(stateDir string) bool

ShouldTrigger reports whether a hook should start a background sync: not when one is already running, and not while backing off after a failure.

func SpawnDetached

func SpawnDetached(exe string, args []string, logPath string) error

SpawnDetached starts `exe args...` in its own session/process group so it outlives the hook process (agents may kill a hook's process group when it exits or times out). Output is appended to logPath.

Types

type Credentials

type Credentials struct {
	ServerURL   string    `json:"server_url"`
	DeviceID    string    `json:"device_id"`
	DeviceName  string    `json:"device_name"`
	Token       string    `json:"token"`
	ConnectedAt time.Time `json:"connected_at"`
	// MCPToken is the device's read-only token for the MCP endpoint, handed
	// to agents at runtime (never written into their configuration).
	MCPToken string `json:"mcp_token,omitempty"`
}

Credentials identify this device to the server. Written by `memspace client connect`, mode 0600.

func LoadCredentials

func LoadCredentials(path string) (*Credentials, error)

LoadCredentials reads the credentials file.

type Importer

type Importer interface {
	Name() string
	Import(ctx context.Context, sp *spool.Spool, heartbeat func()) (int, error)
}

Importer pulls events from an agent's own records (e.g. Codex rollout files) into the spool. heartbeat must be called periodically during long imports to keep the sync lock alive.

type Result

type Result struct {
	Imported   int
	Uploaded   int
	Duplicates int
	Rejected   int
	Retried    int
}

Result summarizes one Run.

type State

type State struct {
	ConsecutiveFailures int       `json:"consecutive_failures"`
	NextAttemptAt       time.Time `json:"next_attempt_at"`
	LastAttemptAt       time.Time `json:"last_attempt_at"`
	LastSuccessAt       time.Time `json:"last_success_at"`
	LastError           string    `json:"last_error,omitempty"`
	TotalUploaded       int64     `json:"total_uploaded"`
	TotalDuplicates     int64     `json:"total_duplicates"`
	TotalRejected       int64     `json:"total_rejected"`
}

State is the persisted outcome of recent sync attempts, used for exponential backoff and for `memspace client status`.

func LoadState

func LoadState(stateDir string) State

LoadState reads the sync state; a missing file is a zero state.

func (State) Offline

func (s State) Offline() bool

Offline reports whether the last sync attempt failed.

type Syncer

type Syncer struct {
	Cfg       config.ClientConfig
	Spool     *spool.Spool
	Creds     *Credentials
	Client    *client.Client
	Importers []Importer
	Log       io.Writer
	// AfterSync, when set, runs after a sync that reached the server, with
	// the server's version ("" from servers that do not report it); the
	// client upgrades itself there. It runs outside the sync lock.
	AfterSync func(ctx context.Context, serverVersion string)
	// UpgradeStatus, when set, returns a failed automatic upgrade (target
	// version and error) to report to the server.
	UpgradeStatus func() (target, err string)
	// contains filtered or unexported fields
}

Syncer runs one device's upload loop.

func New

func New(cfg config.ClientConfig, sp *spool.Spool, creds *Credentials, importers ...Importer) *Syncer

New builds a Syncer from config and saved credentials.

func (*Syncer) Run

func (s *Syncer) Run(ctx context.Context, force bool) (Result, error)

Run imports and uploads until the spool is empty or an attempt fails. Failures are recorded in the persisted state and schedule the next attempt.

Jump to

Keyboard shortcuts

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