mirror of
https://github.com/runbear-io/beardrive.git
synced 2026-08-25 08:08:08 +02:00
* 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>
189 lines
5.5 KiB
Go
189 lines
5.5 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/runbear-io/beardrive/internal/config"
|
|
"github.com/runbear-io/beardrive/internal/store"
|
|
)
|
|
|
|
// `bdrive sync --hook` must emit the gated-link formula as Claude Code
|
|
// hook JSON, stamp the session note, and stay a silent no-op everywhere
|
|
// else — a hook must never fail the turn.
|
|
func TestSyncHookMode(t *testing.T) {
|
|
t.Setenv("BDRIVE_HOME", t.TempDir())
|
|
folder := t.TempDir()
|
|
folder, _ = filepath.EvalSymlinks(folder)
|
|
proj, err := config.SaveProject(folder, config.Project{
|
|
Volume: "wiki",
|
|
Remote: "https://hub.example.com/p/p-12345678", // unreachable: cycle degrades offline, formula still valid
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, err := config.ResolveMount(folder); err != nil { // enroll, as `bdrive init` would
|
|
t.Fatal(err)
|
|
}
|
|
|
|
c := syncCmd()
|
|
var out bytes.Buffer
|
|
c.SetOut(&out)
|
|
c.SetIn(strings.NewReader(`{"session_id":"sess-42","prompt":"hello"}`))
|
|
c.SetArgs([]string{folder, "--hook", "claude-code"})
|
|
if err := c.Execute(); err != nil {
|
|
t.Fatalf("hook mode must never fail: %v", err)
|
|
}
|
|
got := out.String()
|
|
for _, want := range []string{
|
|
`"hookSpecificOutput"`,
|
|
`"hookEventName":"UserPromptSubmit"`,
|
|
"https://hub.example.com/p-12345678", // base URL: remote minus /p
|
|
"[🔗](", // the emoji-link convention
|
|
"code blocks", // paths in code blocks stay plain
|
|
"PUBLIC", // bdrive share stays opt-in
|
|
} {
|
|
if !strings.Contains(got, want) {
|
|
t.Errorf("hook output missing %q:\n%s", want, got)
|
|
}
|
|
}
|
|
|
|
// The session note was stamped for the daemon's follow-up scans.
|
|
vdir, err := config.VolumeDir(proj.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
st, err := store.Open(vdir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if note := st.LoadNote(); note != "claude-code session sess-42" {
|
|
t.Errorf("note = %q, want the stamped session", note)
|
|
}
|
|
}
|
|
|
|
func TestSyncHookModeNoOps(t *testing.T) {
|
|
t.Setenv("BDRIVE_HOME", t.TempDir())
|
|
|
|
// Not a mount: silent success, no output.
|
|
c := syncCmd()
|
|
var out bytes.Buffer
|
|
c.SetOut(&out)
|
|
c.SetIn(strings.NewReader(`{"session_id":"x"}`))
|
|
c.SetArgs([]string{t.TempDir(), "--hook", "claude-code"})
|
|
if err := c.Execute(); err != nil {
|
|
t.Fatalf("non-mount must be a silent no-op: %v", err)
|
|
}
|
|
if out.Len() != 0 {
|
|
t.Fatalf("non-mount emitted output: %s", out.String())
|
|
}
|
|
|
|
// A config.json that arrived with the folder (git clone, copied dir)
|
|
// but was never enrolled on this device via `bdrive init`: silent no-op,
|
|
// and — crucially — no device enrollment as a side effect.
|
|
folder := t.TempDir()
|
|
folder, _ = filepath.EvalSymlinks(folder)
|
|
proj, err := config.SaveProject(folder, config.Project{
|
|
Volume: "wiki", Remote: "https://hub.example.com/p/p-12345678",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c2 := syncCmd()
|
|
out.Reset()
|
|
c2.SetOut(&out)
|
|
c2.SetIn(strings.NewReader(`{"session_id":"x"}`))
|
|
c2.SetArgs([]string{folder, "--hook", "claude-code"})
|
|
if err := c2.Execute(); err != nil {
|
|
t.Fatalf("unenrolled mount must be a silent no-op: %v", err)
|
|
}
|
|
if out.Len() != 0 {
|
|
t.Fatalf("unenrolled mount emitted output: %s", out.String())
|
|
}
|
|
mounts, err := config.LoadMounts()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, enrolled := mounts[proj.ID]; enrolled {
|
|
t.Fatal("hook auto-enrolled the mount; only `bdrive init` may do that")
|
|
}
|
|
|
|
// Enrolled but paused by `bdrive stop`: silent no-op too.
|
|
if _, _, err := config.ResolveMount(folder); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
vdir, err := config.VolumeDir(proj.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.SetPaused(vdir, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c3 := syncCmd()
|
|
out.Reset()
|
|
c3.SetOut(&out)
|
|
c3.SetIn(strings.NewReader(`{"session_id":"x"}`))
|
|
c3.SetArgs([]string{folder, "--hook", "claude-code"})
|
|
if err := c3.Execute(); err != nil {
|
|
t.Fatalf("paused mount must be a silent no-op: %v", err)
|
|
}
|
|
if out.Len() != 0 {
|
|
t.Fatalf("paused mount emitted output: %s", out.String())
|
|
}
|
|
|
|
// Garbage stdin on a live mount: still sync, still emit, never fail.
|
|
if err := store.SetPaused(vdir, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c4 := syncCmd()
|
|
out.Reset()
|
|
c4.SetOut(&out)
|
|
c4.SetIn(strings.NewReader("not json at all"))
|
|
c4.SetArgs([]string{folder, "--hook", "claude-code"})
|
|
if err := c4.Execute(); err != nil {
|
|
t.Fatalf("garbage stdin must not fail: %v", err)
|
|
}
|
|
if !strings.Contains(out.String(), `"hookSpecificOutput"`) {
|
|
t.Fatalf("formula not emitted on garbage stdin: %s", out.String())
|
|
}
|
|
}
|
|
|
|
// Plain `bdrive sync` (the push hook's form, and what users type) refuses
|
|
// unenrolled and paused mounts with instructions instead of silently
|
|
// enrolling or resuming.
|
|
func TestSyncRefusesUnenrolledAndPaused(t *testing.T) {
|
|
t.Setenv("BDRIVE_HOME", t.TempDir())
|
|
folder := t.TempDir()
|
|
folder, _ = filepath.EvalSymlinks(folder)
|
|
proj, err := config.SaveProject(folder, config.Project{Volume: "wiki"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
c := syncCmd()
|
|
c.SetArgs([]string{folder})
|
|
err = c.Execute()
|
|
if err == nil || !strings.Contains(err.Error(), "bdrive init") {
|
|
t.Fatalf("unenrolled sync error = %v, want a `bdrive init` pointer", err)
|
|
}
|
|
|
|
if _, _, err := config.ResolveMount(folder); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
vdir, err := config.VolumeDir(proj.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.SetPaused(vdir, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c2 := syncCmd()
|
|
c2.SetArgs([]string{folder})
|
|
err = c2.Execute()
|
|
if err == nil || !strings.Contains(err.Error(), "paused") {
|
|
t.Fatalf("paused sync error = %v, want a paused message", err)
|
|
}
|
|
}
|