// Package davsync keeps magpie's setup the same on every computer through // a WebDAV folder or an S3-compatible bucket. What goes there is a backup (see internal/backup), // sealed with a passphrase before it leaves the computer, so the server // only ever holds a file it can't read. // // The magpie serving the gateway syncs every few minutes. The setup is // taken in five parts: providers (with their pictures and groups), // settings, profiles, the agents' models and the library (instructions, // MCP servers and skills). A part changed only here is // pushed; one changed only on the server is brought in; one changed on // both since the last sync keeps the newer, and the one it replaced is // saved in the sync folder beside magpie's files and named in a notice. // What the server holds is mirrored: a provider removed on one computer // goes from the others. package davsync import ( "bytes" "context" "crypto/sha256" "encoding/hex" "encoding/json" "errors" "fmt" "log" "maps" "net/url" "os" "path/filepath" "slices" "strings" "sync" "time" "github.com/yetone/magpie/internal/access" "github.com/yetone/magpie/internal/agent" "github.com/yetone/magpie/internal/backup" "github.com/yetone/magpie/internal/edit" "github.com/yetone/magpie/internal/library" "github.com/yetone/magpie/internal/profile" "github.com/yetone/magpie/internal/provider" "github.com/yetone/magpie/internal/settings" "github.com/yetone/magpie/internal/usage" ) // Every is how often the gateway's magpie syncs. const Every = 3 * time.Minute // Parts, in the order they are told. var Parts = []string{"providers", "settings", "profiles", "agents", "library"} // Config is the sync's setup, kept in sync.json beside magpie's other // files, readable by the user alone, as provider keys are. An s3://bucket/prefix // address is an S3-compatible bucket, User its access key ID and Password // the secret, kept as a WebDAV password is. type Config struct { URL string `json:"url"` User string `json:"user,omitempty"` Password string `json:"password,omitempty"` Passphrase string `json:"passphrase"` // S3 alone: the server (none is AWS), its region (none is us-east-1, // auto for R2), and the bucket in the path rather than the host name Endpoint string `json:"endpoint,omitempty"` Region string `json:"region,omitempty"` PathStyle bool `json:"pathStyle,omitempty"` Keys bool `json:"keys"` // providers carry their API keys Agents bool `json:"agents"` // the agents' models go too // Library is whether the library goes too; nil, as in a setup made // before it could, is yes Library *bool `json:"library,omitempty"` // Usage is whether this computer shares its usage with the others // syncing to the server, and sees theirs (#542) Usage bool `json:"usage,omitempty"` // Other is the other kind's server, kept when sync moved from it (a // WebDAV folder's while an S3 bucket is synced to, or the other way): // moving back finds it as it was, its password too. Nothing syncs to it. Other *Server `json:"other,omitempty"` // AutoEvery is how many minutes apart the gateway's magpie syncs by // itself: 0 is Every's 3, Manual never — then only Sync now syncs // (#847). Set by SetAuto alone; Configure keeps it. AutoEvery int `json:"autoEvery,omitempty"` } // Manual is AutoEvery's never: sync runs only when asked. const Manual = -1 // AutoChoices are the minutes SetAuto takes, Manual first. var AutoChoices = []int{Manual, 3, 15, 30, 60} // auto is how often c syncs by itself; 0 when it doesn't. func (c Config) auto() time.Duration { switch { case c.AutoEvery == Manual: return 0 case c.AutoEvery > 0: return time.Duration(c.AutoEvery) * time.Minute } return Every } // Server is where a Config syncs to, without what it syncs or the // passphrase: the other kind's, kept beside the one synced to. type Server struct { URL string `json:"url"` User string `json:"user,omitempty"` Password string `json:"password,omitempty"` Endpoint string `json:"endpoint,omitempty"` Region string `json:"region,omitempty"` PathStyle bool `json:"pathStyle,omitempty"` } func (c Config) where() Server { return Server{URL: c.URL, User: c.User, Password: c.Password, Endpoint: c.Endpoint, Region: c.Region, PathStyle: c.PathStyle} } func (s Server) config() Config { return Config{URL: s.URL, User: s.User, Password: s.Password, Endpoint: s.Endpoint, Region: s.Region, PathStyle: s.PathStyle} } // saved is the setup whose password c may keep: old, or, for c of the // other kind, the server of c's kind kept beside it, when there is one. func (old Config) saved(c Config) Config { if o := old.Other; o != nil && old.S3() != c.S3() && o.config().S3() == c.S3() { return o.config() } return old } func (c Config) library() bool { return c.Library == nil || *c.Library } // S3 is whether c keeps the file in an S3 bucket rather than a WebDAV folder. func (c Config) S3() bool { return strings.HasPrefix(strings.ToLower(strings.TrimSpace(c.URL)), "s3://") } // Kind is the server's kind, as the user reads it: "WebDAV" or "S3". func (c Config) Kind() string { if c.S3() { return "S3" } return "WebDAV" } // Notice says what a sync replaced when a part had changed on both sides, // or when this computer first joined a folder with a setup in it. type Notice struct { At time.Time `json:"at"` Here []string `json:"here,omitempty"` // this computer's parts, replaced by the server's There []string `json:"there,omitempty"` // the server's parts, replaced by this computer's Saved string `json:"saved,omitempty"` // the folder the replaced copies are kept in // Restored is a Restore's notice: Here are the parts it brought in, // what was here before kept in Saved, which Undo brings back Restored bool `json:"restored,omitempty"` } // state is what the last sync saw: the parts here and on the server, by // hash, and the server's file. type state struct { Key string `json:"key"` // the address, user and passphrase it was for Last time.Time `json:"last,omitzero"` Error string `json:"error,omitempty"` Notice *Notice `json:"notice,omitempty"` Sum string `json:"sum,omitempty"` // the server's file, hashed // Server is that file's version, as the server tells it: the next sync // asks for the file only if it isn't still this one Server version `json:"server,omitzero"` Local map[string]string `json:"local,omitempty"` Remote map[string]string `json:"remote,omitempty"` Usage *usageState `json:"usage,omitempty"` // Undo is the file the last Restore kept this computer's setup in, from // just before it: Undo brings it back Undo string `json:"undo,omitempty"` // File is what the server held where the backup should be, when the // last sync couldn't read it as one File *ServerFile `json:"file,omitempty"` // Blind is the last write, when the server didn't check the version // it went over (version.Blind), until a sync reads it back Blind *blind `json:"blind,omitempty"` } // blind is a write the server didn't check: the file it wrote (Sum), and // the parts on the server and here as they were before it, by hash. When // the next sync finds another file, another computer's write may have // gone over this one in the moment between the HEAD and the PUT, both // told they wrote (#1776); that sync merges from before this write, so // what this computer changed counts as changed here again and goes back // up beside the other's, where taking the other's file as having seen // this one's would bring the old parts in over this computer's changes. // Remote and Local are nil for a write to an empty server: the next sync // is then a first one, as it would have been. type blind struct { Sum string `json:"sum"` Remote map[string]string `json:"remote,omitempty"` Local map[string]string `json:"local,omitempty"` } func path(name string) string { return filepath.Join(settings.Dir(), name) } // Load reads the setup; false when sync is off. func Load() (Config, bool) { var c Config b, err := os.ReadFile(path("sync.json")) if err != nil || json.Unmarshal(b, &c) != nil || c.URL == "" { return Config{}, false } return c, true } // Configure turns sync on, or changes it. A password or passphrase left // empty keeps the one set before — the password only for the same server // and user: it is never sent to another, and is asked for again there. func Configure(c Config) error { c.Other = nil // kept here, never given c.URL, c.User = strings.TrimSpace(c.URL), strings.TrimSpace(c.User) c.Endpoint, c.Region = strings.TrimSpace(c.Endpoint), strings.TrimSpace(c.Region) if !c.S3() { // WebDAV's own: nothing of an S3 setup left behind c.Endpoint, c.Region, c.PathStyle = "", "", false } if err := Check(c); err != nil { return err } if c.S3() && c.User == "" { return errors.New("S3 sync needs an access key ID, and its secret") } // after a sync in progress, which would save its state for the setup it // began with return locked(func() error { if old, ok := Load(); ok { kept, needed := password(old.saved(c), c) if kept { c.Password = old.saved(c).Password } if needed { if c.S3() { return fmt.Errorf("type the secret for the access key %s on %s: the one saved is only used with the server and key it was given for", c.User, c.server()) } who := c.server() if c.User != "" { who = c.User + " on " + who } return fmt.Errorf("type the password for %s: the one saved is only sent to the server and user it was given for", who) } if c.Passphrase == "" { c.Passphrase = old.Passphrase } // the other kind's server is kept: the one moved from, or the // one kept before c.Other = old.Other c.AutoEvery = old.AutoEvery // SetAuto's, not the form's if old.S3() != c.S3() { o := old.where() c.Other = &o } } if c.S3() && c.Password == "" { return fmt.Errorf("type the secret for the access key %s", c.User) } if c.Passphrase == "" { return errors.New("sync needs a passphrase: the file is sealed with it before it leaves this computer") } if c.Password != "" && c.Passphrase == c.Password { // the server is sent the password (an S3 server holds the // secret): with it, it could open the file if c.S3() { return errors.New("the passphrase is the access key's secret: the S3 server holds it, and could open the file with it. Pick a passphrase of its own") } return errors.New("the passphrase is the server's password: the server is sent the password, and could open the file with it. Pick a passphrase of its own") } b, err := json.MarshalIndent(c, "", " ") if err != nil { return err } os.MkdirAll(settings.Dir(), 0o755) if err := edit.WriteAtomic(path("sync.json"), b); err != nil { return err } return os.Chmod(path("sync.json"), 0o600) }) } // Check is Configure's look at the address (and an S3 endpoint), for a // caller to make before asking for the password and passphrase. func Check(c Config) error { _, err := newRemote(c) return err } // server is the host c's password or secret goes with: the WebDAV // address's, or the S3 endpoint's (AWS's when none is given). func (c Config) server() string { a := strings.TrimSpace(c.URL) if c.S3() { if strings.TrimSpace(c.Endpoint) == "" { return "AWS" } a = s3Endpoint(c.Endpoint, "") } if u, err := url.Parse(a); err == nil && u.Host != "" { return u.Host } return a } // password is what becomes of the saved password when old is changed to // c, with c's own left empty: kept, for the same server and user; or // needed, a new one typed, for another — unless the user name was just // taken away, for a server that asks for no sign-in. func password(old, c Config) (kept, needed bool) { if c.Password != "" || old.Password == "" { return false, false } if c.sameAccount(old) { return true, false } cleared := strings.TrimSpace(c.User) == "" && strings.TrimSpace(old.User) != "" return false, !cleared } // SavedPassword is Configure's look at the saved password for c, with c's // own left empty, for a caller to make before asking for one: whether it // is kept, or a new one is needed. func SavedPassword(c Config) (kept, needed bool) { old, ok := Load() if !ok { return false, false } c.Password = "" return password(old.saved(c), c) } // sameAccount is whether c and o are one user on one server: the password // given for one is only ever sent to the other when they are. func (c Config) sameAccount(o Config) bool { if c.S3() || o.S3() { // one access key at one endpoint, whatever the bucket return c.S3() && o.S3() && strings.TrimSpace(c.User) == strings.TrimSpace(o.User) && strings.EqualFold(s3Endpoint(c.Endpoint, ""), s3Endpoint(o.Endpoint, "")) } a, err := url.Parse(strings.TrimSpace(c.URL)) if err != nil { return false } b, err := url.Parse(strings.TrimSpace(o.URL)) if err != nil { return false } return a.Host != "" && a.Scheme == b.Scheme && strings.EqualFold(a.Host, b.Host) && strings.TrimSpace(c.User) == strings.TrimSpace(o.User) } // Off turns sync off. The file on the server stays, and so does what this // computer last synced with it: turned on again to the same server, it is // not a new computer, so what was changed here while it was off goes up // rather than the server's older setup coming down over it (#939: every // agent back to how it was when sync was turned off). How the last sync // went, its notice and a restore's undo go. func Off() error { return locked(func() error { st := loadState() if st.Local == nil { os.Remove(path("sync-state.json")) os.Remove(path(cacheName)) } else { st.Error, st.Notice, st.Undo, st.Usage = "", nil, "", nil saveState(st) } usage.DropAllShared() // the others' usage came by sync: it goes with it (#542) if err := os.Remove(path("sync.json")); err != nil && !os.IsNotExist(err) { return err } return nil }) } // View is sync as the Settings page shows it: never the secrets. type View struct { On bool `json:"on"` URL string `json:"url,omitempty"` User string `json:"user,omitempty"` PasswordSet bool `json:"passwordSet,omitempty"` PassphraseSet bool `json:"passphraseSet,omitempty"` Keys bool `json:"keys"` Agents bool `json:"agents"` Kind string `json:"kind,omitempty"` // "webdav" or "s3", when on Endpoint string `json:"endpoint,omitempty"` Region string `json:"region,omitempty"` PathStyle bool `json:"pathStyle,omitempty"` Library bool `json:"library"` Usage bool `json:"usage,omitempty"` // UsageError is why usage couldn't be shared the last time it was tried UsageError string `json:"usageError,omitempty"` Last time.Time `json:"last,omitzero"` Error string `json:"error,omitempty"` Notice *Notice `json:"notice,omitempty"` // Other is the other kind's server, kept for moving back to: not // synced to Other *OtherView `json:"other,omitempty"` // Auto is how many minutes apart it syncs by itself; 0 never: only when // asked Auto int `json:"auto"` // Undo is whether the last Restore can be undone Undo bool `json:"undo,omitempty"` // File is what the server holds instead of a backup, when Error is // that it isn't one; File.Replace offers Upload File *ServerFile `json:"serverFile,omitempty"` } // OtherView is the other kind's server as the Settings page shows it. type OtherView struct { Kind string `json:"kind"` // "webdav" or "s3" URL string `json:"url"` User string `json:"user,omitempty"` PasswordSet bool `json:"passwordSet,omitempty"` Endpoint string `json:"endpoint,omitempty"` Region string `json:"region,omitempty"` PathStyle bool `json:"pathStyle,omitempty"` } // Status is sync's setup and how the last sync went. func Status() View { c, ok := Load() if !ok { return View{Keys: true, Agents: true, Library: true} } st := loadState() v := View{On: true, URL: c.URL, User: c.User, PasswordSet: c.Password != "", PassphraseSet: c.Passphrase != "", Keys: c.Keys, Agents: c.Agents, Library: c.library(), Error: st.Error, Notice: st.Notice, Kind: strings.ToLower(c.Kind()), Endpoint: c.Endpoint, Region: c.Region, PathStyle: c.PathStyle, Auto: int(c.auto() / time.Minute)} if st.Key == stateKey(c) { v.Last = st.Last v.Undo = st.Undo != "" && isFile(st.Undo) if st.Error != "" { v.File = st.File } if c.Usage && st.Usage != nil { v.UsageError = st.Usage.Error } } v.Usage = c.Usage if o := c.Other; o != nil { v.Other = &OtherView{Kind: strings.ToLower(o.config().Kind()), URL: o.URL, User: o.User, PasswordSet: o.Password != "", Endpoint: o.Endpoint, Region: o.Region, PathStyle: o.PathStyle} } return v } // Dismiss clears the notice, and with a restore's, its undo. func Dismiss() error { return locked(func() error { st := loadState() st.Notice, st.Undo = nil, "" // a restore's undo goes with its notice saveState(st) return nil }) } func loadState() state { var st state if b, err := os.ReadFile(path("sync-state.json")); err == nil { json.Unmarshal(b, &st) } return st } func saveState(st state) { b, _ := json.MarshalIndent(st, "", " ") edit.WriteAtomic(path("sync-state.json"), b) } // stateKey is the setup the state is for: an S3 endpoint too, when there is one func stateKey(c Config) string { k := c.URL if c.Endpoint != "" { k += "\x00" + c.Endpoint } return sum([]byte(k + "\x00" + c.User + "\x00" + c.Passphrase)) } // cacheName is the copy of the server's file as last read or written, kept // readable by the user alone: a sync that finds it unchanged on the server // but something changed here merges with it, rather than reading it again. const cacheName = "sync-server" + backup.Ext // remember keeps data as the server's file last seen. func remember(data []byte) { if b, err := os.ReadFile(path(cacheName)); err == nil && sum(b) == sum(data) { return } os.MkdirAll(settings.Dir(), 0o755) if edit.WriteAtomic(path(cacheName), data) == nil { os.Chmod(path(cacheName), 0o600) } } // cached is the server's file as last seen, when it is the one hashed to // want; nil when it isn't kept. func cached(want string) []byte { b, err := os.ReadFile(path(cacheName)) if err != nil || want == "" || sum(b) != want { return nil } return b } // keepDamaged writes a server file that couldn't be read aside under the // sync folder, so the damage is inspectable rather than silently replaced. // It keeps the newest few: a sync that rebuilds on every tick (a server // that always answers the same unreadable way) would otherwise pile up a // copy each time. The name is sorted by time, so the oldest are the first // to go. func keepDamaged(data []byte) { keepAside(data, "server-damaged", 3) } // keepAside writes data under the sync folder as