From 3ea8df85aaad262b8d5d566d73ba6e8eda5976f6 Mon Sep 17 00:00:00 2001 From: shoemoney Date: Tue, 30 Jun 2026 14:23:07 -0500 Subject: [PATCH] round4-client: surface last sync error per mount in status MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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). --- cmd/devbox/main.go | 6 ++++- internal/control/control.go | 1 + internal/daemon/daemon.go | 19 +++++++++++++-- internal/daemon/daemon_test.go | 42 ++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 3 deletions(-) diff --git a/cmd/devbox/main.go b/cmd/devbox/main.go index dc5ada9..919133e 100644 --- a/cmd/devbox/main.go +++ b/cmd/devbox/main.go @@ -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 } diff --git a/internal/control/control.go b/internal/control/control.go index 4082e2f..f1a2923 100644 --- a/internal/control/control.go +++ b/internal/control/control.go @@ -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 diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 6d632a8..1391bb9 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -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) diff --git a/internal/daemon/daemon_test.go b/internal/daemon/daemon_test.go index 2e1aeff..eeb1cbc 100644 --- a/internal/daemon/daemon_test.go +++ b/internal/daemon/daemon_test.go @@ -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) {