round4-client: surface last sync error per mount in status

Track lastErr per mount (mirroring lastSync pattern), cleared on
success so a recovered mount shows clean. Errors from LoadIgnoreWith,
Pull, and Sync all land in the map and surface through StateSnapshot
into 'devbox status' (human ⚠️ warning) and --json (last_err field).
This commit is contained in:
shoemoney 2026-06-30 14:23:07 -05:00
parent b8b9bd1f22
commit 3ea8df85aa
4 changed files with 65 additions and 3 deletions

View file

@ -775,6 +775,7 @@ type statusMount struct {
Pinned bool `json:"pinned"`
BaseSnapshot string `json:"base_snapshot,omitempty"`
LastSyncUnix int64 `json:"last_sync_unix,omitempty"`
LastErr string `json:"last_err,omitempty"`
}
type statusJSON struct {
@ -818,7 +819,7 @@ func statusCmd() *cobra.Command {
s.Mounts = append(s.Mounts, statusMount{
Share: m.Share, Subpath: m.Subpath, Local: m.Local,
ReadOnly: m.ReadOnly, Pinned: m.Pinned, BaseSnapshot: m.BaseSnapshot,
LastSyncUnix: m.LastSyncUnix,
LastSyncUnix: m.LastSyncUnix, LastErr: m.LastErr,
})
}
} else {
@ -856,6 +857,9 @@ func statusCmd() *cobra.Command {
}
fmt.Fprintf(out, " - %s/%s -> %s [%s] @%s (%s)\n",
m.Share, m.Subpath, m.Local, mode, short(orNone(m.BaseSnapshot)), syncAge(m.LastSyncUnix))
if m.LastErr != "" {
fmt.Fprintf(out, " ⚠️ last sync failed: %s\n", m.LastErr)
}
}
return nil
}

View file

@ -41,6 +41,7 @@ type MountState struct {
Pinned bool `json:"pinned"`
BaseSnapshot string `json:"base_snapshot"`
LastSyncUnix int64 `json:"last_sync_unix"` // 0 = not synced since daemon start
LastErr string `json:"last_err,omitempty"` // last sync error message; empty = healthy
}
// Daemon is the slice of the running daemon the control server steers. The

View file

@ -50,6 +50,7 @@ type Daemon struct {
nudges map[string]func() // mountKey -> trigger a catch-up sync (for Resume)
mounts map[string]config.Mount // mountKey -> mount config (for StateSnapshot)
lastSync map[string]int64 // mountKey -> unix secs of last successful sync (0 = never)
lastErr map[string]string // mountKey -> last sync error message (empty = healthy)
resumeTimer *time.Timer // pending auto-resume from PauseFor; nil if none
}
@ -84,13 +85,23 @@ func New(dir string, cfg config.Daemon, host string, logf func(string, ...any))
nudges: map[string]func(){},
mounts: map[string]config.Mount{},
lastSync: map[string]int64{},
lastErr: map[string]string{},
}, nil
}
// recordSync stamps a mount's last successful sync time (for StateSnapshot / status).
// recordSync stamps a mount's last successful sync time and clears any prior error.
func (d *Daemon) recordSync(m config.Mount) {
d.mu.Lock()
d.lastSync[mountKey(m)] = time.Now().Unix()
k := mountKey(m)
d.lastSync[k] = time.Now().Unix()
delete(d.lastErr, k)
d.mu.Unlock()
}
// recordErr stores the last sync error for a mount (surfaced via StateSnapshot / status).
func (d *Daemon) recordErr(m config.Mount, err error) {
d.mu.Lock()
d.lastErr[mountKey(m)] = err.Error()
d.mu.Unlock()
}
@ -328,6 +339,7 @@ func (d *Daemon) StateSnapshot() control.State {
Pinned: m.Pinned,
BaseSnapshot: d.state[k],
LastSyncUnix: d.lastSync[k],
LastErr: d.lastErr[k],
})
}
return st
@ -341,6 +353,7 @@ func (d *Daemon) syncMount(c *transport.Client, m config.Mount) {
ig, err := syncer.LoadIgnoreWith(m.Local, excludes)
if err != nil {
d.logf("ignore %s: %v", m.Local, err)
d.recordErr(m, err)
return
}
base := d.getBase(m)
@ -351,6 +364,7 @@ func (d *Daemon) syncMount(c *transport.Client, m config.Mount) {
pr, err := syncer.Pull(c, m.Local, m.Share, m.Subpath, base, d.host, now, ig, d.guard, hk)
if err != nil {
d.logf("pull %s: %v", m.Share, err)
d.recordErr(m, err)
return
}
d.setBase(m, pr.Base)
@ -362,6 +376,7 @@ func (d *Daemon) syncMount(c *transport.Client, m config.Mount) {
newBase, pr, err := syncer.Sync(c, m.Local, m.Share, m.Subpath, base, d.host, now, ig, d.guard, hk)
if err != nil {
d.logf("sync %s: %v", m.Share, err)
d.recordErr(m, err)
return
}
d.setBase(m, newBase)

View file

@ -4,6 +4,7 @@ import (
"context"
"crypto/ed25519"
"crypto/rand"
"errors"
"net/http/httptest"
"os"
"path/filepath"
@ -56,6 +57,47 @@ func waitFor(path, want string, d time.Duration) bool {
return false
}
// TestRecordErrSurfacedAndCleared verifies that recordErr surfaces the error in
// StateSnapshot and that a subsequent recordSync clears it.
func TestRecordErrSurfacedAndCleared(t *testing.T) {
d, err := New(t.TempDir(), config.Daemon{}, "h", t.Logf)
if err != nil {
t.Fatal(err)
}
m := config.Mount{Share: "s", Local: t.TempDir(), Hub: "http://hub"}
k := mountKey(m)
// Register so StateSnapshot emits this mount.
d.mu.Lock()
d.mounts[k] = m
d.mu.Unlock()
d.recordErr(m, errors.New("boom"))
snap := d.StateSnapshot()
var got string
for _, ms := range snap.Mounts {
if mountKey(config.Mount{Share: ms.Share, Subpath: ms.Subpath, Local: ms.Local}) == k {
got = ms.LastErr
}
}
if got != "boom" {
t.Fatalf("expected LastErr=boom, got %q", got)
}
d.recordSync(m)
snap = d.StateSnapshot()
for _, ms := range snap.Mounts {
if mountKey(config.Mount{Share: ms.Share, Subpath: ms.Subpath, Local: ms.Local}) == k {
if ms.LastErr != "" {
t.Fatalf("expected LastErr cleared after recordSync, got %q", ms.LastErr)
}
if ms.LastSyncUnix == 0 {
t.Fatal("expected LastSyncUnix != 0 after recordSync")
}
}
}
}
// TestLiveTwoWaySyncViaSSE runs two daemons against one hub and proves a change
// on one machine reaches the other automatically (push -> SSE event -> pull).
func TestLiveTwoWaySyncViaSSE(t *testing.T) {