devbox/internal/daemon/daemon.go
shoemoney 03469958d3
Some checks are pending
CI / vet-build-test (push) Waiting to run
chore: prep for public release — MIT license + github.com module path 🌍⚖️
- LICENSE: MIT (enables the adoption push; public repo was all-rights-reserved).
- go.mod + all imports: git.shoemoney.ai/shoemoney/devbox -> github.com/shoemoney/devbox,
  so outsiders can 'go install github.com/shoemoney/devbox/cmd/devbox@latest'. Forgejo
  stays the self-hosted home; the registry (devbox-hub) + public dist (devbox-dist) are
  unchanged. vet + build + go test -race all green after the rename.
- README: document the go install path.

Security: full git-history scan (manual blob sweep + gitleaks, 84 commits) found
ZERO secrets — Forgejo token never committed, no keys/passwords/private keys.
2026-06-23 19:19:09 -05:00

326 lines
9.1 KiB
Go

// Package daemon runs the live sync loop: for each mount it watches the local
// tree (push on change) and subscribes to hub change events (pull on event),
// funnelling both triggers into a serialized Sync that keeps the mount in
// agreement with the hub.
package daemon
import (
"context"
"log"
"math/rand"
"sync"
"sync/atomic"
"time"
"github.com/shoemoney/devbox/internal/config"
"github.com/shoemoney/devbox/internal/control"
"github.com/shoemoney/devbox/internal/hooks"
"github.com/shoemoney/devbox/internal/secret"
"github.com/shoemoney/devbox/internal/syncer"
"github.com/shoemoney/devbox/internal/transport"
"github.com/shoemoney/devbox/internal/watch"
"github.com/shoemoney/devbox/pkg/proto"
)
// rescanInterval is the safety-net cadence for re-syncing a mount even when no
// trigger fired. It backstops a watcher that never started (e.g. inotify
// max_user_watches exhausted on a big Pi tree) or an event that slipped through:
// without it, such a mount would stop pushing local edits entirely. PRD risk #1.
// ponytail: fixed 60s; promote to a Settings knob only if a huge tree makes the
// periodic manifest rebuild measurably costly.
const rescanInterval = 60 * time.Second
// Daemon syncs a device's mounts against their hub.
type Daemon struct {
dir string
cfg config.Daemon
host string
guard *secret.Guard
maxBytesPerSec int
logf func(string, ...any)
paused atomic.Bool // set via control /pause; gates the sync loop
mu sync.Mutex
state map[string]string // mountKey -> last-applied snapshot
nudges map[string]func() // mountKey -> trigger a catch-up sync (for Resume)
mounts map[string]config.Mount // mountKey -> mount config (for StateSnapshot)
}
// New builds a daemon from a loaded config. logf may be nil (defaults to log.Printf).
func New(dir string, cfg config.Daemon, host string, logf func(string, ...any)) (*Daemon, error) {
settings, err := config.LoadSettings(dir)
if err != nil {
return nil, err
}
guard, err := secret.New(settings.Secrets.ExtraPatterns)
if err != nil {
return nil, err
}
state, err := config.LoadState(dir)
if err != nil {
return nil, err
}
if logf == nil {
logf = log.Printf
}
return &Daemon{
dir: dir, cfg: cfg, host: host, guard: guard,
maxBytesPerSec: settings.Transfer.MaxKbps * 1024,
logf: logf, state: state,
nudges: map[string]func(){},
mounts: map[string]config.Mount{},
}, nil
}
// Run syncs every mount until ctx is cancelled.
func (d *Daemon) Run(ctx context.Context) error {
// Start the control plane before mounts so `devbox status`/`pause` can reach
// the daemon immediately. A bind failure is non-fatal: sync must not depend on
// the control socket. Serve stops itself on ctx cancel.
if _, err := control.Serve(ctx, d.dir, d, d.logf); err != nil {
d.logf("control socket unavailable (introspection/pause disabled): %v", err)
}
var wg sync.WaitGroup
for _, m := range d.cfg.Mounts {
wg.Add(1)
go func(m config.Mount) {
defer wg.Done()
d.runMount(ctx, m)
}(m)
}
wg.Wait()
return nil
}
func mountKey(m config.Mount) string { return m.Share + "\x00" + m.Subpath + "\x00" + m.Local }
func (d *Daemon) getBase(m config.Mount) string {
d.mu.Lock()
defer d.mu.Unlock()
return d.state[mountKey(m)]
}
func (d *Daemon) setBase(m config.Mount, base string) {
d.mu.Lock()
defer d.mu.Unlock()
if d.state[mountKey(m)] != base {
d.state[mountKey(m)] = base
if err := config.SaveState(d.dir, d.state); err != nil {
d.logf("save state: %v", err)
}
}
}
func (d *Daemon) runMount(ctx context.Context, m config.Mount) {
if m.Pinned {
// Deployed to a fixed snapshot; never live-advance it. Held until re-mount.
d.logf("📌 %s pinned to %s — not live-syncing", m.Share, d.getBase(m))
<-ctx.Done()
return
}
c := transport.New(m.Hub)
c.SetBearer(d.cfg.Bearer)
if d.maxBytesPerSec > 0 {
c.SetRateLimit(d.maxBytesPerSec)
}
trigger := make(chan struct{}, 1)
nudge := func() {
select {
case trigger <- struct{}{}:
default: // a sync is already queued; coalesce
}
}
// Register this mount so the control plane can read its live state and so
// Resume can kick it into a catch-up sync. Unregister on exit.
d.register(m, nudge)
defer d.unregister(m)
// Local filesystem changes -> sync. If the watcher can't start (e.g. inotify
// limit), we don't bail: the periodic rescan below keeps the mount syncing.
if w, err := watch.New(m.Local, 300*time.Millisecond); err != nil {
d.logf("watch %s: %v — falling back to %s rescans (run: devbox doctor)", m.Local, err, rescanInterval)
} else {
defer w.Close()
go func() {
for {
select {
case <-ctx.Done():
return
case <-w.Events():
nudge()
}
}
}()
}
// Hub change events -> sync (reconnect with exponential backoff + jitter).
go func() {
backoff := time.Duration(0)
for ctx.Err() == nil {
start := time.Now()
err := c.Events(ctx, m.Share, func(proto.Event) { nudge() })
if ctx.Err() != nil {
return
}
// A stream that lived a while is a transient drop, not a hard failure:
// reset so the retry is prompt instead of inheriting a grown delay.
if time.Since(start) > healthyStream {
backoff = 0
}
backoff = nextBackoff(backoff)
wait := jittered(backoff)
d.logf("events %s reconnecting in ~%s: %v", m.Share, wait.Round(time.Millisecond), err)
select {
case <-ctx.Done():
return
case <-time.After(wait):
}
}
}()
// Periodic rescan: safety net so the mount keeps converging even if the
// watcher never started (inotify limit) or an event was missed. Stagger the
// first tick by a random offset so many mounts don't all rebuild manifests in
// lockstep every interval (a self-synchronized load spike on big trees).
go func() {
select {
case <-ctx.Done():
return
case <-time.After(time.Duration(rand.Int63n(int64(rescanInterval)))):
}
t := time.NewTicker(rescanInterval)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
nudge()
}
}
}()
if !d.paused.Load() {
nudge() // initial sync (skipped while paused)
}
for {
select {
case <-ctx.Done():
return
case <-trigger:
// While paused we drain the trigger but don't sync; Resume re-nudges
// every mount so nothing buffered while paused is lost.
if d.paused.Load() {
continue
}
d.syncMount(c, m)
}
}
}
// register records a mount's nudge and config so Resume can catch it up and the
// control plane can report its live state.
func (d *Daemon) register(m config.Mount, nudge func()) {
k := mountKey(m)
d.mu.Lock()
d.nudges[k] = nudge
d.mounts[k] = m
d.mu.Unlock()
}
func (d *Daemon) unregister(m config.Mount) {
k := mountKey(m)
d.mu.Lock()
delete(d.nudges, k)
delete(d.mounts, k)
d.mu.Unlock()
}
// Pause stops syncing until Resume. In-flight syncs finish; queued/new triggers
// are drained without effect while paused.
func (d *Daemon) Pause() {
d.paused.Store(true)
d.logf("⏸️ paused — syncing suspended (devbox resume to continue)")
}
// Resume clears the pause and nudges every registered mount into an immediate
// catch-up sync, so edits made while paused converge right away.
func (d *Daemon) Resume() {
d.paused.Store(false)
d.mu.Lock()
nudges := make([]func(), 0, len(d.nudges))
for _, n := range d.nudges {
nudges = append(nudges, n)
}
d.mu.Unlock()
for _, n := range nudges {
n()
}
d.logf("▶️ resumed — catching up %d mount(s)", len(nudges))
}
// StateSnapshot returns the running daemon's live per-mount + paused view for
// the control plane's GET /state.
func (d *Daemon) StateSnapshot() control.State {
d.mu.Lock()
defer d.mu.Unlock()
st := control.State{Paused: d.paused.Load()}
for k, m := range d.mounts {
st.Mounts = append(st.Mounts, control.MountState{
Share: m.Share,
Subpath: m.Subpath,
Local: m.Local,
ReadOnly: m.ReadOnly,
Pinned: m.Pinned,
BaseSnapshot: d.state[k],
})
}
return st
}
func (d *Daemon) syncMount(c *transport.Client, m config.Mount) {
ig, err := syncer.LoadIgnore(m.Local)
if err != nil {
d.logf("ignore %s: %v", m.Local, err)
return
}
base := d.getBase(m)
now := time.Now().UnixNano() // nanoseconds so conflict-copy names don't collide
hk := hooks.New(m.Local, m.Share, d.host, m.Hub)
if m.ReadOnly {
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)
return
}
d.setBase(m, pr.Base)
d.report(m, pr)
return
}
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)
return
}
d.setBase(m, newBase)
d.report(m, pr)
}
func (d *Daemon) report(m config.Mount, pr syncer.PullResult) {
if len(pr.Written)+len(pr.Deleted)+len(pr.Conflicts)+len(pr.Skipped) == 0 {
return
}
d.logf("📥 %s: %d written, %d deleted, %d conflicts, %d skipped",
m.Share, len(pr.Written), len(pr.Deleted), len(pr.Conflicts), len(pr.Skipped))
for _, cf := range pr.Conflicts {
d.logf(" 💥 conflict copy: %s", cf)
}
for _, sk := range pr.Skipped {
d.logf(" ⚠️ skipped (filesystem clash): %s", sk)
}
}