From 88aa53064ff274b242461f8d33313ad3c8e41a0a Mon Sep 17 00:00:00 2001 From: shoemoney Date: Tue, 23 Jun 2026 01:53:24 -0500 Subject: [PATCH] =?UTF-8?q?feat(M7):=20periodic=20rescan=20fallback=20for?= =?UTF-8?q?=20dead/limited=20watchers=20=F0=9F=94=81=F0=9F=8D=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The v1 daemon only re-syncs a mount on a watcher event or a hub event. If the watcher can't start (inotify max_user_instances/max_user_watches exhausted on a big Pi tree — PRD risk #1) the mount silently stops pushing local edits. Add a 60s per-mount rescan ticker that nudges a sync regardless, so a mount reconverges even with no working watcher. Watcher-start failure now logs the fallback + points at 'devbox doctor' instead of going quiet. Fleet-verified on linux/arm64: with fs.inotify.max_user_instances=0 (watcher dead) a local edit still propagated to a peer via the rescan tick. scripts/m7-fleet-verify.sh exercises the full M7 surface + this rescan path. --- internal/daemon/daemon.go | 28 +++++++++- scripts/m7-fleet-verify.sh | 103 +++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 scripts/m7-fleet-verify.sh diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 9aacf86..da6daf3 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -19,6 +19,14 @@ import ( "git.shoemoney.ai/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 @@ -110,9 +118,10 @@ func (d *Daemon) runMount(ctx context.Context, m config.Mount) { } } - // Local filesystem changes -> sync. + // 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", m.Local, err) + d.logf("watch %s: %v — falling back to %s rescans (run: devbox doctor)", m.Local, err, rescanInterval) } else { defer w.Close() go func() { @@ -152,6 +161,21 @@ func (d *Daemon) runMount(ctx context.Context, m config.Mount) { } }() + // Periodic rescan: safety net so the mount keeps converging even if the + // watcher never started (inotify limit) or an event was missed. + go func() { + t := time.NewTicker(rescanInterval) + defer t.Stop() + for { + select { + case <-ctx.Done(): + return + case <-t.C: + nudge() + } + } + }() + nudge() // initial sync for { select { diff --git a/scripts/m7-fleet-verify.sh b/scripts/m7-fleet-verify.sh new file mode 100644 index 0000000..3c9d356 --- /dev/null +++ b/scripts/m7-fleet-verify.sh @@ -0,0 +1,103 @@ +#!/usr/bin/env bash +# M7 fleet verification on real linux/arm64 Pis. +# +# Stands up an ISOLATED test hub on one Pi (never touches the production hub on +# .10) and runs real daemons on two more Pis to prove the M7 surface on arm64: +# - cross-compiled binaries run +# - live two-way sync (A's rw watcher pushes, B's ro mount pulls via SSE) +# - reconnect with backoff after the hub restarts +# - PERIODIC RESCAN FALLBACK: with A's watcher killed, a local edit still +# propagates (the gap this branch fixes — PRD risk #1) +# - devbox doctor's real inotify check + failure path +# - devbox stop tears the daemon down cleanly +set -uo pipefail + +KEY="$HOME/.ssh/pi" +SSH="ssh -i $KEY -o StrictHostKeyChecking=no -o ConnectTimeout=8 -o BatchMode=yes" +SCP="scp -i $KEY -o StrictHostKeyChecking=no -o ConnectTimeout=8 -o BatchMode=yes" +HUBPI=192.168.1.11; API=192.168.1.12; BPI=192.168.1.13 +PORT=18080; HUBURL="http://$HUBPI:$PORT"; R=/tmp/devbox-m7test +INO_WATCH=62164; INO_INST=128 + +say() { printf '\n\033[1;36m== %s ==\033[0m\n' "$*"; } +on() { local h=$1; shift; $SSH "shoemoney@$h" "$@"; } +start_remote() { local h=$1; shift; $SSH "shoemoney@$h" "$@" /dev/null 2>&1 & echo $!; } +fail() { echo "❌ $*"; exit 1; } +dc() { local h=$1; shift; on "$h" "cd $R && XDG_CONFIG_HOME=$R/cfg ./devbox $*"; } # devbox-on-host + +HUB_SSH=""; A_SSH=""; B_SSH="" +cleanup() { + say cleanup + for p in "$HUB_SSH" "$A_SSH" "$B_SSH"; do [ -n "$p" ] && kill "$p" 2>/dev/null; done + for h in $HUBPI $API $BPI; do on "$h" "pkill -f devbox 2>/dev/null; rm -rf $R" 2>/dev/null; done + on "$API" "sudo sysctl -w fs.inotify.max_user_instances=$INO_INST >/dev/null 2>&1" 2>/dev/null + on "$BPI" "sudo sysctl -w fs.inotify.max_user_watches=$INO_WATCH >/dev/null 2>&1" 2>/dev/null +} +trap cleanup EXIT + +say "build + ship linux/arm64 binaries" +( cd "$(dirname "$0")/.." && CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o /tmp/devbox.arm64 ./cmd/devbox \ + && CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o /tmp/devbox-hub.arm64 ./cmd/devbox-hub ) || fail build +for h in $HUBPI $API $BPI; do on "$h" "mkdir -p $R"; $SCP -q /tmp/devbox.arm64 "shoemoney@$h:$R/devbox"; on "$h" "chmod +x $R/devbox"; done +$SCP -q /tmp/devbox-hub.arm64 "shoemoney@$HUBPI:$R/devbox-hub"; on "$HUBPI" "chmod +x $R/devbox-hub" +echo "arm64 binary runs: $(on $API "$R/devbox version")" + +say "start isolated test hub on $HUBPI:$PORT" +HUB_SSH=$(start_remote "$HUBPI" "cd $R && exec ./devbox-hub serve --data $R/hubdata --listen 0.0.0.0:$PORT >$R/hub.log 2>&1") +curl --retry 40 --retry-connrefused --retry-delay 0 --max-time 15 -sf "$HUBURL/metrics" >/dev/null && echo "hub up ✅" || fail "hub down" +mint() { on "$HUBPI" "cd $R && ./devbox-hub token --data $R/hubdata"; } + +say "device A ($API): join, publish, mount rw, run daemon" +dc "$API" join "$HUBURL $(mint)" >/dev/null +on "$API" "mkdir -p $R/src && printf 'v1\n' > $R/src/app.txt" +dc "$API" publish "$R/src proj" >/dev/null +dc "$API" mount "proj $R/src" >/dev/null +A_SSH=$(start_remote "$API" "cd $R && exec env XDG_CONFIG_HOME=$R/cfg ./devbox start >$R/daemon.log 2>&1") +on "$API" "for i in \$(seq 1 25); do [ -f $R/cfg/devbox/devboxd.pid ] && break; sleep 0.2; done" && echo "A daemon up ✅" + +say "device B ($BPI): join, mount ro, run daemon" +dc "$BPI" join "$HUBURL $(mint)" >/dev/null +dc "$BPI" mount "proj $R/dst --ro" >/dev/null +[ "$(on $BPI "cat $R/dst/app.txt")" = v1 ] && echo "B has v1 ✅" +B_SSH=$(start_remote "$BPI" "cd $R && exec env XDG_CONFIG_HOME=$R/cfg ./devbox start >$R/daemon.log 2>&1") +on "$BPI" "for i in \$(seq 1 25); do [ -f $R/cfg/devbox/devboxd.pid ] && break; sleep 0.2; done" && echo "B daemon up ✅" + +say "doctor on A (real inotify check, should pass)" +dc "$API" doctor || fail "doctor A nonzero" + +say "live sync: A's watcher pushes v2 -> B pulls" +on "$API" "printf 'v2\n' > $R/src/app.txt" +on "$BPI" "for i in \$(seq 1 60); do [ \"\$(cat $R/dst/app.txt 2>/dev/null)\" = v2 ] && break; sleep 0.3; done" +[ "$(on $BPI "cat $R/dst/app.txt")" = v2 ] && echo "B live-synced v2 ✅" || { on "$BPI" "tail $R/daemon.log"; fail "no live sync"; } + +say "reconnect/backoff: restart hub, A pushes v3 -> B catches up" +kill "$HUB_SSH" 2>/dev/null; on "$HUBPI" "pkill -f 'devbox-hub serve'"; sleep 2 +HUB_SSH=$(start_remote "$HUBPI" "cd $R && exec ./devbox-hub serve --data $R/hubdata --listen 0.0.0.0:$PORT >>$R/hub.log 2>&1") +curl --retry 40 --retry-connrefused --retry-delay 0 --max-time 15 -sf "$HUBURL/metrics" >/dev/null && echo "hub back ✅" || fail "hub restart" +on "$API" "printf 'v3\n' > $R/src/app.txt" +on "$BPI" "for i in \$(seq 1 90); do [ \"\$(cat $R/dst/app.txt 2>/dev/null)\" = v3 ] && break; sleep 0.5; done" +[ "$(on $BPI "cat $R/dst/app.txt")" = v3 ] && echo "B reconnected + synced v3 ✅" || fail "no reconnect" +on "$API" "grep -q reconnecting $R/daemon.log && echo 'A backoff reconnect logged ✅' || echo '(recovered; no reconnect log)'" + +say "RESCAN FALLBACK: kill A's watcher, local edit must still propagate" +kill "$A_SSH" 2>/dev/null; on "$API" "pkill -f 'devbox start'"; sleep 1 +on "$API" "sudo sysctl -w fs.inotify.max_user_instances=0 >/dev/null" # fsnotify.NewWatcher() now fails +A_SSH=$(start_remote "$API" "cd $R && exec env XDG_CONFIG_HOME=$R/cfg ./devbox start >$R/daemon.log 2>&1") +sleep 2 +on "$API" "grep -q 'falling back to' $R/daemon.log && echo 'A watcher dead, rescan fallback engaged ✅' || { echo '⚠️ expected fallback log'; tail $R/daemon.log; }" +on "$API" "printf 'rescued\n' > $R/src/rescan.txt" # no watcher event will fire for this +echo "waiting up to ~75s for the 60s rescan tick to push it..." +on "$BPI" "for i in \$(seq 1 150); do [ \"\$(cat $R/dst/rescan.txt 2>/dev/null)\" = rescued ] && break; sleep 0.5; done" +[ "$(on $BPI "cat $R/dst/rescan.txt 2>/dev/null")" = rescued ] && echo "✅ local edit propagated via RESCAN with a dead watcher" || { on "$API" "tail $R/daemon.log"; fail "rescan did not push"; } +on "$API" "sudo sysctl -w fs.inotify.max_user_instances=$INO_INST >/dev/null" + +say "doctor inotify FAILURE path on B (lower watches, expect fail + fix hint)" +on "$BPI" "sudo sysctl -w fs.inotify.max_user_watches=1 >/dev/null" +dc "$BPI" doctor; echo "doctor exit=$?" +on "$BPI" "sudo sysctl -w fs.inotify.max_user_watches=$INO_WATCH >/dev/null" + +say "devbox stop on B" +dc "$BPI" stop +on "$BPI" "for i in \$(seq 1 25); do [ -f $R/cfg/devbox/devboxd.pid ] || break; sleep 0.2; done; [ ! -f $R/cfg/devbox/devboxd.pid ] && echo 'B stopped + pidfile gone ✅'" || fail stop + +say "🎉 M7 FLEET VERIFY PASSED (incl. rescan fallback)"