Files
beardrive/cmd/bdrive/sync_run.go
6628718f1d fix(cli): agent hooks never sync folders this device didn't opt into (#44)
* fix(cli): agent hooks never sync folders this device didn't opt into

The turn hooks decided "this folder is managed" from the mere presence of
.bdrive/config.json — a file designed to travel with the folder. Two holes:

- A config.json arriving via git clone / copied dir made one hook firing
  silently mint a device identity, register the mount, create a volume
  store, journal the whole folder, and inject the hub-link formula — on a
  device that never ran init or login.
- `bdrive stop` only killed the daemon: the next agent turn's
  `bdrive sync --hook` resumed a full sync cycle and kept injecting links,
  and `stop --forget` was undone within one turn by registry self-heal.

Fix: one gate (`syncBlocked`) in the paths all hooks route through —
sync/sync --hook/read-log now require the mount to already be enrolled in
this device's mounts.json (read without ResolveMount's enrolling
self-heal) and not paused. Hook mode exits silently; plain `bdrive sync`
errors with a `bdrive init` pointer. New per-device paused marker in the
volume dir: set by `bdrive stop`, cleared by `bdrive init` (startSync).
Only init enrolls or resumes; folder moves still self-heal since
enrollment is keyed by mount id, not path.

Docs updated (README, SKILL.md, docs cli reference, CHANGELOG). Tests:
hook/read-log no-op + no-enrollment on unenrolled and paused mounts,
plain-sync refusals, stop→pause→forget regression, paused marker contract.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* docs: architecture-diagram PRs must show before/after excerpts of changed classes

The "Architecture changes" PR section now names exactly what changed and
shows Before and After mermaid excerpts scoped to the affected classes and
their immediate relationships — never the full diagram (Before = merge
base). Convention updated in CLAUDE.md, architecture/README.md, and the
pre-PR hook's reminder text.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* docs: add cli-sync architecture diagram; widen diagram convention to the CLI

architecture/cli-sync.md draws the CLI and sync engine (cmd/bdrive +
internal/{syncer,store,journal,config,daemon,agenthooks}): the Session
cycle over Store/journal/remote, and the command layer with the new
syncBlocked opt-in gate, paused marker, and enrollment ownership. The
pre-PR hook and CLAUDE.md now watch these packages too, so CLI-side
structural changes trigger the before/after-excerpt convention the same
way server changes do.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* docs: full-coverage architecture diagrams — overview, frontend, agentskills

Every application package is now drawn somewhere: overview.md (system
diagram — package map, device↔hub↔storage flow, agent surfaces, and the
private cloud/ repo as an external seam consumer), webapp-frontend.md (the
hub SPA's modules: App/HubApp/VolumeApp/Browser, the in-repo nav/router,
api layer, hooks, components), and agentskills added to cli-sync.md. The
pre-PR hook now watches all of cmd/, internal Go code, and frontend/src
(generated static/ excluded); CLAUDE.md and architecture/README.md state
the coverage rule: every code change lands in exactly one detail diagram's
scope, web/docs and cloud/ deliberately excluded.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* docs: PR bodies start with a TL;DR — max 5 informal one-liners

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-24 15:53:23 +09:00

118 lines
3.5 KiB
Go

package main
import (
"context"
"fmt"
"time"
"github.com/spf13/cobra"
"github.com/runbear-io/beardrive/internal/config"
"github.com/runbear-io/beardrive/internal/daemon"
"github.com/runbear-io/beardrive/internal/store"
)
// startSync brings a project folder live: register the mount, open the
// volume store, run the initial cycle, start the background daemon. Called
// by `bdrive init` (and by anything that needs to resume a stopped project).
func startSync(ctx context.Context, folder string, proj config.Project, foreground bool, scanInterval, remoteInterval time.Duration) error {
if _, _, err := config.ResolveMount(folder); err != nil { // registers/updates the registry entry
return err
}
vdir, err := config.VolumeDir(proj.ID)
if err != nil {
return err
}
if _, err := store.Open(vdir); err != nil {
return err
}
// init is the one gesture that (re)consents to syncing: clear any pause
// left by `bdrive stop` so the daemon and the agent hooks run again.
if err := store.SetPaused(vdir, false); err != nil {
return err
}
// Initial cycle: import existing files, pull remote state.
sess, _, err := openSession(ctx, folder, true)
if err != nil {
return err
}
sess.OnProgress = progressReporter() // the initial import is the slow one
res, err := sess.Cycle(ctx)
closeSession(sess)
if err != nil {
return err
}
printCycle(res)
if foreground {
return daemon.Run(folder, scanInterval, remoteInterval)
}
pid, err := daemon.Start(folder, vdir, scanInterval, remoteInterval)
if err != nil {
return fmt.Errorf("start sync daemon: %w", err)
}
fmt.Printf(" daemon: running (pid %d, scan %s, remote sync %s)\n", pid, scanInterval, remoteInterval)
return nil
}
// stopCmd pauses syncing for a project folder (files stay on disk; run
// `bdrive init` again to resume).
func stopCmd() *cobra.Command {
var forget bool
c := &cobra.Command{
Use: "stop [folder]",
Aliases: []string{"pause"},
Short: "Stop syncing a project folder",
Long: `Stop the sync daemon for a project folder. Files stay on disk and the
project's local history is kept; run "bdrive init" in the folder to resume.
With --forget the mount is also removed from this device's registry (the
folder's .bdrive settings and the local volume data are kept).`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
folder, err := absFolder(args)
if err != nil {
return err
}
proj, err := mustProject(folder)
if err != nil {
return err
}
vdir, err := config.VolumeDir(proj.ID)
if err != nil {
return err
}
stopped, err := daemon.Stop(vdir)
if err != nil {
return err
}
// The pause must outlive the daemon: agent turn hooks run
// `bdrive sync` in this folder on every turn and would silently
// resume without it. Cleared by `bdrive init`.
if err := store.SetPaused(vdir, true); err != nil {
return err
}
if stopped {
fmt.Printf("stopped syncing %s (run `bdrive init` to resume)\n", folder)
} else {
fmt.Printf("no sync daemon running for %s; syncing paused (run `bdrive init` to resume)\n", folder)
}
if forget {
mounts, err := config.LoadMounts()
if err != nil {
return err
}
delete(mounts, proj.ID)
if err := config.SaveMounts(mounts); err != nil {
return err
}
fmt.Printf("forgot mount %s (local volume data kept under ~/.bdrive/volumes/%s)\n", proj.ID, proj.ID)
}
return nil
},
}
c.Flags().BoolVar(&forget, "forget", false, "also remove the mount from this device's registry")
return c
}