Files
magpie/internal/davsync/davsync.go
T
yetone 3132fa1cd9 library: Rules, a folder of rules given to Claude Code, Cline and Kiro CLI (JonahFu on X)
JonahFu asked on X (tweet 2123237371739316685) for Claude's rules
folder in the Library. A new Rules tab keeps rules once and gives each
to the agents ticked for it, in the folder each reads by itself:

- Claude Code: <claude dir>/rules/<name>.md. `paths` is the only
  front matter key it reads (code.claude.com/docs/en/memory). Checked
  on real 2.1.327 in a sandbox: it reads every .md under the folder,
  loads one without paths at launch, and one with paths when
  Read/Write/Edit touches a matching file.
- Cline: <cline dir>/rules (CLINE_DIR, else ~/.cline). Cline 3.0.70
  reads every rule always and has no paths key, so a rule with paths
  isn't written for it; the agent gets a problem and the editor says
  why under the paths.
- Kiro CLI: ~/.kiro/steering, `inclusion: fileMatch` +
  `fileMatchPattern` for paths, else always. A rule named "agents" is
  refused there (it would be AGENTS.md on a disk that ignores case).
- Not listed: Cursor (user rules live in its settings; .cursor/rules is
  per project), Codex (its rules are execpolicy), Droid (command
  permissions), Gemini CLI, Qwen Code and OpenCode (no rules folder).

A body starting with --- gets an empty front matter first. Files are
recorded with their hash in Applied.Rules through the same
syncOwnFiles as subagents and slash commands: a file of the user's own
by that name, or one magpie wrote that the user edited, is never
written over or taken away. Bundles (backup, WebDAV) carry rules; a
bundle from an older magpie leaves this computer's alone. The editor
lists each agent's file to reveal. No CLI yet.

docs/subsystems/library.md: a Rules row and its tests.

Run (mica-run, at this tree):
- go vet -tags nogui ./internal/library ./internal/gui ./internal/davsync
  ./internal/cli: ok. gofmt -l: none.
- go test -tags nogui -timeout 45m ./internal/library ./internal/davsync:
  ok (TestRulesEachAgentsFile, TestRulesKeptToTheirPaths,
  TestRulesLeaveTheUsersFiles, TestRuleCheck, TestRulesCarried).
- node --test library-rules, library-subagents, library-commands,
  library-hooks, library-loading, library-groups, i18n-keys,
  i18n-en-no-cjk (Chromium and WebKit, 420 and 1100 wide, en zh zh-TW
  ja de): 78 of 78.
- The full suite and cross-OS vet ran at the next commit, which this
  one is under.
- Real use: Claude Code 2.1.327 in a sandbox HOME loaded a rule without
  paths at launch and one with paths only on touching a match; a
  sandbox magpie on mica wrote ~/.claude/rules/go-style.md with
  paths: ["**/*.go"].
Not tried: Cline and Kiro CLI loading the files for real, project-level
rules (.claude/rules in a repository), Copilot and CodeBuddy (not
verified, not listed), Windows and Linux.
2026-11-19 12:20:02 +08:00

1904 lines
68 KiB
Go

// Package davsync keeps magpie's setup the same on every computer through
// a WebDAV folder, an S3-compatible bucket or a GitHub repository. 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. A computer joining a folder keeps the gateway keys
// the folder's store has never seen; after the join, a key removed on the
// folder goes from every computer.
package davsync
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"log"
"maps"
"net/url"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"sync/atomic"
"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/filememo"
"github.com/yetone/magpie/internal/lastgood"
"github.com/yetone/magpie/internal/library"
"github.com/yetone/magpie/internal/memory"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/profile"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/usage"
)
// addPlugins is backup.AddPlugins: a var so a test installs nothing.
var addPlugins = backup.AddPlugins
// removePlugin is provider.RemovePlugin, as the Plugins page removes one:
// a var so a test removes nothing.
var removePlugin = func(name string) error {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
return provider.RemovePlugin(ctx, name)
}
// 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. For github://owner/repo/prefix,
// Password is the repository's Contents token, independent of GitHubToken
// in settings (the library's).
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"`
// GitHub alone: an empty branch uses the repository's default branch.
Branch string `json:"branch,omitempty"`
Keys bool `json:"keys"` // providers carry their API keys
Agents bool `json:"agents"` // the agents' models go too
// Providers, Settings and Profiles are whether each of those parts
// goes; nil, as in a setup made before they could be left out, is yes.
// A part left out stays as it is on each computer (jinpwng on X,
// #6428: only the library and memory synced, each computer keeping its
// own providers, settings and models).
Providers *bool `json:"providers,omitempty"`
Settings *bool `json:"settings,omitempty"`
Profiles *bool `json:"profiles,omitempty"`
// Library is whether the library goes too; nil, as in a setup made
// before it could, is yes
Library *bool `json:"library,omitempty"`
// Memory is whether memory goes too (memory.Combine says how two
// computers' are merged); nil, as in a setup made before it could,
// is yes
Memory *bool `json:"memory,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"`
// SignIns is whether the subscription accounts and the plugins'
// sign-ins go too, with their credentials, inside the file sealed with
// Passphrase: off unless the user turns it on
// (provider.MergeSignIns says how two computers' are merged).
SignIns bool `json:"signIns,omitempty"`
// SignInsExcept are the subscriptions (provider ids) whose sign-ins
// this computer leaves out of SignIns (Ethereal499 on X: Claude's, as
// Claude is wary of where its accounts are used): theirs here never go
// up, the server's are never put in here, and theirs here are kept as
// they are, none taken away for the server lacking it. The server's
// copy goes on as it was for the computers that sync them. None, as
// in a setup made before the choice, leaves none out.
SignInsExcept []string `json:"signInsExcept,omitempty"`
// Other is the most recently left server, retained for older clients.
// Servers holds every inactive kind. Nothing syncs to them.
Other *Server `json:"other,omitempty"`
// Servers keeps every inactive kind, including Other for older clients.
Servers map[string]Server `json:"servers,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"`
// FollowServer is this computer following the setup on the server
// rather than syncing both ways (Ramon317317 on X: one computer the
// others all take theirs from). Each sync brings in every part that
// changed on either side since the last one, and nothing goes up: a
// change made here is replaced, a copy of it kept in the sync folder
// and named in a notice. The computer the others follow keeps syncing
// both ways. Set by SetFollowServer alone; Configure keeps it.
FollowServer bool `json:"followServer,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"`
Branch string `json:"branch,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, Branch: c.Branch}
}
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, Branch: s.Branch}
}
// Kept is an inactive server, including setups saved before Servers existed.
func (c Config) Kept(kind string) (Server, bool) {
kind = strings.ToLower(kind)
if s, ok := c.Servers[kind]; ok {
return s, true
}
if c.Other != nil && strings.EqualFold(c.Other.config().Kind(), kind) {
return *c.Other, true
}
return Server{}, false
}
// 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 old.Kind() != c.Kind() {
if s, ok := old.Kept(c.Kind()); ok {
return s.config()
}
return Config{}
}
return old
}
func (c Config) library() bool { return c.Library == nil || *c.Library }
func (c Config) memoryOn() bool { return c.Memory == nil || *c.Memory }
// yes is a part's choice, nil (a setup from before it could be made) yes.
func yes(b *bool) bool { return b == nil || *b }
// 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://")
}
// GitHub is whether c keeps the file in a GitHub repository.
func (c Config) GitHub() bool {
return strings.HasPrefix(strings.ToLower(strings.TrimSpace(c.URL)), "github://")
}
// Kind is the server's kind, as the user reads it.
func (c Config) Kind() string {
if c.GitHub() {
return "GitHub"
}
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
// Followed is that the parts in Here were changed here and replaced
// because this computer follows the server (Config.FollowServer), not
// because the server's were newer
Followed bool `json:"followed,omitempty"`
// 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"`
// Brought is each agent field's value as the last sync that changed it
// here wrote it, Replaced the value picked here that it wrote over,
// and Own the fields put back to that value since: this computer's
// own, never brought in or taken up (own.go). Follow is Settings
// asking for the own ones to follow the others again, on the next
// sync. They are this computer's choices, kept when sync moves to
// another server.
Brought map[string]string `json:"brought,omitempty"`
Replaced map[string]string `json:"replaced,omitempty"`
Own map[string]bool `json:"own,omitempty"`
Follow bool `json:"follow,omitempty"`
// Here is the agents whose picks stayed on this computer (settings'
// AgentsHere) as the last sync left them: an agent turned so since is
// a change the next sync settles, its hashes made as before it.
Here []string `json:"here,omitempty"`
// SignIns is the sign-ins (id → hash) the last sync left both on the
// server and here: one gone from a side since was removed there. Nil
// when not known (never synced with them, or a blind write went
// over), when none is taken for removed.
SignIns map[string]string `json:"signIns"`
// Plugins is the plugins (by name) the last sync left both on the
// server and here: one gone from a side since was removed there, and
// goes from the other (pluginPlan). Nil when not known (never synced
// with them, or a blind write went over), when none is taken for removed.
Plugins []string `json:"plugins,omitempty"`
// Off is the parts the last sync left out (Config.syncs): one synced
// again since is joined again, each side's own as on a first sync,
// rather than taken as unchanged on both sides while they differ.
Off []string `json:"off,omitempty"`
// Applied is the last sync that brought parts in here, for UndoSync.
Applied *applied `json:"applied,omitempty"`
// LibraryLeft is the library's skills and plugins whose files the
// last sync left out of the server's file, too big for it (#5715);
// LibraryProblems what of the library the last sync that brought it
// in couldn't put here, such as a skill not fetched from its source
// yet. Both are said on the Settings page, never a silent success.
LibraryLeft []string `json:"libraryLeft,omitempty"`
LibraryProblems []library.Problem `json:"libraryProblems,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) }
// loadReads counts Load's reads of the file, for a test.
var loadReads atomic.Int64
// Load reads the setup; false when sync is off. The file's bytes are
// read once until it changes (filememo), each caller given a Config of
// its own: every Providers page poll asked it, most often of a file that
// isn't there.
func Load() (Config, bool) {
var c Config
b, err := filememo.Read("davsync-setup", path("sync.json"), func(b []byte) ([]byte, error) {
loadReads.Add(1)
return bytes.Clone(b), nil // its own, not a buffer filememo reads into again
})
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.Servers = nil
c.URL, c.User = strings.TrimSpace(c.URL), strings.TrimSpace(c.User)
c.Endpoint, c.Region = strings.TrimSpace(c.Endpoint), strings.TrimSpace(c.Region)
c.Branch = strings.TrimSpace(c.Branch)
if !c.S3() { // WebDAV's own: nothing of an S3 setup left behind
c.Endpoint, c.Region, c.PathStyle = "", "", false
}
if !c.GitHub() {
c.Branch = ""
} else {
c.User = ""
c.Password = strings.TrimSpace(c.Password)
}
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.GitHub() {
return errors.New("type a GitHub token for this repository: the saved token is only used with the repository it was given for")
}
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.Servers = maps.Clone(old.Servers)
if c.Servers == nil {
c.Servers = map[string]Server{}
}
if old.Other != nil {
c.Servers[strings.ToLower(old.Other.config().Kind())] = *old.Other
}
c.AutoEvery = old.AutoEvery // SetAuto's, not the form's
c.FollowServer = old.FollowServer // SetFollowServer's, not the form's
if old.Kind() != c.Kind() {
o := old.where()
c.Other = &o
c.Servers[strings.ToLower(old.Kind())] = o
}
delete(c.Servers, strings.ToLower(c.Kind()))
}
if c.GitHub() && c.Password == "" {
return errors.New("GitHub sync needs a token with Contents read and write permission for the repository")
}
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 {
if c.GitHub() {
return errors.New("the passphrase is the GitHub token: GitHub receives it, and could open the file with it. Pick a passphrase of its own")
}
// 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.GitHub() || o.GitHub() {
a, ea := newGitHub(c)
b, eb := newGitHub(o)
return c.GitHub() && o.GitHub() && ea == nil && eb == nil && strings.EqualFold(a.repo, b.repo)
}
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, st.Applied = "", nil, "", nil, nil
saveState(st)
}
usage.DropAllShared() // the others' usage came by sync: it goes with it (#542)
defer filememo.Forget() // read again, where a request holds it
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", "s3" or "github", when on
Endpoint string `json:"endpoint,omitempty"`
Region string `json:"region,omitempty"`
PathStyle bool `json:"pathStyle,omitempty"`
Branch string `json:"branch,omitempty"`
Library bool `json:"library"`
Memory bool `json:"memory"`
Providers bool `json:"providers"`
Settings bool `json:"settings"`
Profiles bool `json:"profiles"`
Usage bool `json:"usage,omitempty"`
SignIns bool `json:"signIns,omitempty"`
// SignInsExcept are the subscriptions whose sign-ins stay out
// (Config.SignInsExcept)
SignInsExcept []string `json:"signInsExcept,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"`
Servers map[string]OtherView `json:"servers,omitempty"`
// Auto is how many minutes apart it syncs by itself; 0 never: only when
// asked
Auto int `json:"auto"`
// FollowServer is whether this computer takes the server's setup and
// sends none of its own (Config.FollowServer)
FollowServer bool `json:"followServer,omitempty"`
// Undo is whether the last Restore can be undone
Undo bool `json:"undo,omitempty"`
// Applied is the last sync that brought parts in here, which
// UndoSync takes back on this computer
Applied *Applied `json:"applied,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"`
// SignIn is the subscriptions whose settings sync brought with no
// account here: sync carries no sign-in, so each is to be signed in to
// on this computer before it is listed
SignIn []string `json:"signIn,omitempty"`
// Own is the agent fields picked on this computer after a sync set
// them, which sync leaves as they are here (own.go)
Own []OwnField `json:"own,omitempty"`
// LibraryLeft and LibraryProblems are the last sync's (state)
LibraryLeft []string `json:"libraryLeft,omitempty"`
LibraryProblems []string `json:"libraryProblems,omitempty"`
}
// OtherView is the other kind's server as the Settings page shows it.
type OtherView struct {
Kind string `json:"kind"` // "webdav", "s3" or "github"
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"`
Branch string `json:"branch,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, Memory: true, Providers: true, Settings: true, Profiles: 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(), Memory: c.memoryOn(), Error: st.Error, Notice: st.Notice,
Providers: c.syncs("providers"), Settings: c.syncs("settings"), Profiles: c.syncs("profiles"),
Kind: strings.ToLower(c.Kind()), Endpoint: c.Endpoint, Region: c.Region, PathStyle: c.PathStyle, Branch: c.Branch,
Auto: int(c.auto() / time.Minute), FollowServer: c.FollowServer}
if st.Key == stateKey(c) {
v.Last = st.Last
v.Undo = st.Undo != "" && isFile(st.Undo)
if a := st.Applied; a != nil && isFile(a.File) {
v.Applied = &Applied{At: a.At, Parts: a.Parts}
}
if st.Error != "" {
v.File = st.File
}
if c.Usage && st.Usage != nil {
v.UsageError = st.Usage.Error
}
if c.library() {
// one settled since (Update fetched the skill) isn't said
v.LibraryLeft, v.LibraryProblems = st.LibraryLeft, said(library.Unsettled(st.LibraryProblems))
}
if !st.Last.IsZero() {
if ps, err := provider.Stored(); err == nil {
v.SignIn = provider.NotSignedIn(ps)
}
}
}
v.Usage, v.SignIns, v.SignInsExcept = c.Usage, c.SignIns, c.SignInsExcept
if c.Agents {
v.Own = ownFields(st)
}
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, Branch: o.Branch}
}
for kind, s := range c.Servers {
if v.Servers == nil {
v.Servers = map[string]OtherView{}
}
v.Servers[kind] = OtherView{Kind: kind, URL: s.URL, User: s.User, PasswordSet: s.Password != "",
Endpoint: s.Endpoint, Region: s.Region, PathStyle: s.PathStyle, Branch: s.Branch}
}
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
}
// saveState writes st. loadState reads a file that doesn't parse as no
// state, which starts sync afresh and loses a restore's Undo and the Blind
// guard: such a file is kept aside first, and one that can't be isn't
// written over.
func saveState(st state) {
if lastgood.KeepUnparsed(path("sync-state.json"), lastgood.Into[state]) != nil {
return
}
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
}
if c.GitHub() {
k += "\x00" + c.Branch
}
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 <time>-<tag>, keeping the
// newest n so tagged.
func keepAside(data []byte, tag string, n int) error {
dir := path("sync")
if err := os.MkdirAll(dir, 0o700); err != nil {
return err
}
name := filepath.Join(dir, time.Now().Format("2006-01-02-150405")+"-"+tag+backup.Ext)
if err := edit.WriteAtomic(name, data); err != nil {
return err
}
os.Chmod(name, 0o600)
old, _ := filepath.Glob(filepath.Join(dir, "*-"+tag+backup.Ext))
slices.Sort(old) // names begin with the time, so oldest first
for len(old) > n {
os.Remove(old[0])
old = old[1:]
}
return nil
}
func sum(b []byte) string {
h := sha256.Sum256(b)
return hex.EncodeToString(h[:])
}
var mu sync.Mutex
// Now syncs once; nothing when sync is off. Usage is shared at most every
// usageEvery.
func Now(ctx context.Context) error { return syncNow(ctx, false, "") }
// SyncNow is Now asked for: usage is shared whatever the time.
func SyncNow(ctx context.Context) error { return syncNow(ctx, true, "") }
// SyncPreviewed is SyncNow for what Preview told, its Token: when the
// sync would now do anything else, nothing is done, and it is
// ErrPlanChanged.
func SyncPreviewed(ctx context.Context, token string) error {
if token == "" {
return errors.New("no preview to sync")
}
return syncNow(ctx, true, token)
}
func syncNow(ctx context.Context, force bool, expect string) error {
if _, ok := Load(); !ok { // off: no lock taken, so none made
return nil
}
// a sync has no time to end in (see stallAfter), a wait for another's
// does
lctx, lcancel := context.WithTimeout(ctx, wait)
unlock, err := lock(lctx)
lcancel()
if err != nil {
return err
}
defer unlock()
mu.Lock()
defer mu.Unlock()
// read again under the lock: another magpie may have turned sync off,
// or changed it, while this one waited
c, ok := Load()
if !ok {
return nil
}
st := loadState()
if st.Key != stateKey(c) { // another folder or passphrase: start afresh
st = state{Key: stateKey(c), Brought: st.Brought, Replaced: st.Replaced, Own: st.Own, Follow: st.Follow, Here: st.Here}
}
if expect != "" { // what the user saw previewed, or nothing
p, err := preview(ctx, c, st)
if err != nil {
return err
}
if p.Token != expect {
return ErrPlanChanged
}
}
err = syncOnce(ctx, c, &st, nil)
if errors.Is(err, errChanged) { // another computer got in between: again, over its version
err = syncOnce(ctx, c, &st, nil)
}
st.Error, st.File = "", nil
var nb *notBackup
if errors.As(err, &nb) {
st.File = &nb.f
}
if err != nil {
st.Error = err.Error()
} else {
st.Last = time.Now()
shareUsage(ctx, c, &st, force)
}
saveState(st)
return err
}
// maxWait is the longest Run waits for a server that is limiting requests
// and didn't say for how long; one that says is waited for as long as it
// asks, up to limitWait.
const (
maxWait = 30 * time.Minute
limitWait = 6 * time.Hour
)
// backoff is how long Run waits after a sync that ended with err, the last
// wait having been prev: Every, but longer for a server limiting requests
// (429, 503) — as long as its Retry-After says, or else twice the last
// wait, up to maxWait — so that a limit is not run into again and again.
func backoff(err error, prev time.Duration) time.Duration { return backoffFrom(err, prev, Every) }
// backoffFrom is backoff for a sync every base.
func backoffFrom(err error, prev, base time.Duration) time.Duration {
var rl *rateLimited
if !errors.As(err, &rl) {
return base
}
if rl.after > 0 {
return min(max(rl.after, base), max(limitWait, base))
}
return min(max(2*prev, 2*base), max(maxWait, base))
}
// poll is how often Run looks at the setup: whether sync is on, by
// itself, and how often (#847). Looking costs no request. A var for tests.
var poll = time.Minute
// Run syncs a little after it starts and every AutoEvery after that, until
// ctx ends — less often while the server is limiting requests, and never
// while sync is set to run only when asked. A failure is logged once, not
// on every try.
func Run(ctx context.Context) { run(ctx, 20*time.Second) }
func run(ctx context.Context, first time.Duration) {
t := time.NewTimer(first)
defer t.Stop()
last := ""
var tried time.Time // the last sync Run made
var gap time.Duration
limited := false
for {
select {
case <-ctx.Done():
return
case <-t.C:
}
t.Reset(poll)
cfg, ok := Load()
every := cfg.auto()
if !ok || every == 0 {
continue
}
// a server limiting requests is waited for as backoff says; else the
// time set now, which may have changed since the last sync
w := every
if limited {
w = max(gap, every)
}
if !tried.IsZero() && time.Since(tried) < w {
continue
}
// no request is let stand still for long (stallAfter), but a large
// backup on a slow line takes what it takes: only an hour ends it
c, cancel := context.WithTimeout(ctx, time.Hour)
err := Now(c)
cancel()
tried = time.Now()
if msg := fmt.Sprint(err); err != nil && msg != last {
log.Printf("syncing through %s: %s", cfg.Kind(), msg)
last = msg
} else if err == nil {
last = ""
}
gap = backoffFrom(err, w, every)
var rl *rateLimited
limited = errors.As(err, &rl)
}
}
func collect(c Config) (backup.Bundle, error) {
b, err := backup.Collect(c.Keys, "magpie")
if b.Settings == nil {
s := settings.Load()
b.Settings = &s
}
// a provider kept on this computer (Provider.ThisDevice) never goes out
b.Providers, b.Order = provider.SyncedOnly(b.Providers, b.Order)
if !c.Agents {
b.Agents = nil
}
if !c.library() {
b.Library = nil
}
if !c.memoryOn() {
b.Memory = nil
}
if err == nil && c.SignIns {
if serr := backup.WithSignIns(&b); serr != nil {
return b, fmt.Errorf("the sign-ins couldn't be read: %w", serr)
}
// the subscriptions left out here never go up
b.SignIns, _ = b.SignIns.Except(c.SignInsExcept)
}
return b, err
}
// hashes is each part of b, hashed: what is compared to tell a change.
func hashes(b backup.Bundle) map[string]string {
h := func(v any) string { j, _ := json.Marshal(v); return sum(j) }
var s settings.Settings
if b.Settings != nil {
s = *b.Settings
}
s.KeepOwn(settings.Settings{}) // this computer's own: never synced
settingsHash := h(s)
if b.GatewayKeys != nil { // a missing store keeps older bundles' hash
settingsHash = h([]any{s, *b.GatewayKeys})
if b.AccountGroups != nil { // nor do older bundles without groups
settingsHash = h([]any{s, *b.GatewayKeys, *b.AccountGroups})
}
}
providers := []any{syncedProviders(b.Providers), b.Icons, b.Groups}
if b.Searches != nil && len(*b.Searches) > 0 { // as before them, without one
providers = append(providers, *b.Searches)
}
if len(b.Order) > 0 { // as before it, when never arranged
providers = append(providers, map[string][]string{"order": b.Order})
}
if len(b.GroupOrder) > 0 { // as before it, when never arranged
providers = append(providers, map[string][]string{"groupOrder": b.GroupOrder})
}
if b.Plugins != nil && len(*b.Plugins) > 0 { // as before them, without one
providers = append(providers, map[string][]plugin.Entry{"plugins": *b.Plugins})
}
if b.Proxies != nil && len(*b.Proxies) > 0 { // as before them, without one
providers = append(providers, map[string][]settings.NamedProxy{"proxies": *b.Proxies})
}
lib := b.Library
if lib != nil && lib.Subagents != nil && len(*lib.Subagents) == 0 { // as before them, without one
c := *lib
c.Subagents = nil
lib = &c
}
if lib != nil && lib.Commands != nil && len(*lib.Commands) == 0 { // as before them, without one
c := *lib
c.Commands = nil
lib = &c
}
if lib != nil && lib.Rules != nil && len(*lib.Rules) == 0 { // as before them, without one
c := *lib
c.Rules = nil
lib = &c
}
if lib != nil && lib.Hooks != nil && len(*lib.Hooks) == 0 { // as before them, without one
c := *lib
c.Hooks = nil
lib = &c
}
if lib != nil && lib.Plugins != nil && len(*lib.Plugins) == 0 { // as before them, without one
c := *lib
c.Plugins = nil
lib = &c
}
out := map[string]string{
"providers": h(providers),
"settings": settingsHash,
"profiles": h(orEmpty(b.Profiles)),
"agents": h(orEmpty(b.Agents)),
"library": h(lib),
}
if b.SignIns != nil { // as before them, without them
out["signins"] = h(b.SignIns.Hashes())
}
if b.Memory != nil { // as before it, without it
out["memory"] = b.Memory.Hash()
}
return out
}
// syncedProviders is ps as they are compared: this computer's own fields,
// kept by Mirror and never synced, left out.
func syncedProviders(ps []provider.Provider) []provider.Provider {
ps = slices.Clone(ps)
for i := range ps {
ps[i].KeepLogin, ps[i].KeepLoginAs, ps[i].SwitchOnly, ps[i].RestartApp = false, "", false, false
ps[i].KeepSelf = false
}
return ps
}
// unset says whether b holds nothing of its own in part p: no provider
// or routing group, settings as a new magpie has them, no profile, a
// library with nothing in it. A computer joining a folder whose file's
// part is unset is joining a folder whose first writer had none of that
// part, not one where it was removed (#5642): a part is said unset only
// when nothing a user made is in it, so a part with anything in it is
// merged as before. The agents' part is never unset: it holds the models
// every installed agent reads as it is now, set up or not.
func unset(b backup.Bundle, p string) bool {
switch p {
case "providers":
return len(b.Providers) == 0 && len(b.Groups) == 0 && (b.Proxies == nil || len(*b.Proxies) == 0)
case "settings":
var s settings.Settings
if b.Settings != nil {
s = *b.Settings
}
d := settings.Defaults()
s.KeepOwn(settings.Settings{})
d.KeepOwn(settings.Settings{})
j, _ := json.Marshal(s)
dj, _ := json.Marshal(d)
return bytes.Equal(j, dj)
case "profiles":
return len(b.Profiles) == 0
case "library":
l := b.Library
if l == nil {
return false // from a magpie before the library went: handled apart
}
for _, t := range l.Texts {
if strings.TrimSpace(t) != "" {
return false
}
}
for _, t := range l.Extra {
if strings.TrimSpace(t) != "" {
return false
}
}
return len(l.MCP) == 0 && len(l.Skills) == 0 &&
(l.Subagents == nil || len(*l.Subagents) == 0) && (l.Commands == nil || len(*l.Commands) == 0) && (l.Rules == nil || len(*l.Rules) == 0) && (l.Hooks == nil || len(*l.Hooks) == 0) &&
(l.Bili == nil || strings.TrimSpace(*l.Bili) == "") && l.ClaudePlugins == nil && (l.Plugins == nil || len(*l.Plugins) == 0)
}
return false
}
func orEmpty[V any](m map[string]V) map[string]V {
if m == nil {
return map[string]V{}
}
return m
}
// take puts from's part in to. The merged bundle is a sync one (BundleVersion):
// its per-part markers, not the whole-bundle Keys bit, say which part
// carries credentials, so a keyed providers upload no longer tells a reader
// the settings and the library came with keys too. If an older writer drops
// those markers, readers fall back to Keys even if the version stays 2.
func take(to *backup.Bundle, from backup.Bundle, part string) {
// Preserve every legacy part's scope before a providers upload changes
// Keys for older readers. An untouched nil marker would otherwise inherit
// that new value and turn a redaction into a credential-bearing update.
if to.ProvidersKeys == nil {
to.ProvidersKeys = backup.Flag(to.Keys)
}
if to.SettingsKeys == nil {
to.SettingsKeys = backup.Flag(to.Keys)
}
if to.LibraryKeys == nil {
to.LibraryKeys = backup.Flag(to.Keys)
}
switch part {
case "providers":
keys := from.Keys || (from.ProvidersKeys != nil && *from.ProvidersKeys)
ps := from.Providers
if to.Keys || (to.ProvidersKeys != nil && *to.ProvidersKeys) {
// Keep the server's secrets the upload hasn't: all its keys when
// it was sent without, and an access key or balance token a
// keyed one from an older magpie doesn't know (#1821).
server := map[string]provider.Provider{}
for _, p := range to.Providers {
server[p.ID] = p
}
ps = slices.Clone(ps)
for i, p := range ps {
if k, ok := server[p.ID]; ok {
ps[i] = provider.KeepSecrets(p, k, !keys && p.Key == "" && len(p.Keys) == 0)
}
}
}
searches := from.Searches
if searches != nil && !keys && (to.Keys || (to.ProvidersKeys != nil && *to.ProvidersKeys)) && to.Searches != nil { // the same for the search APIs
keys := map[string]provider.SearchAPI{}
for _, a := range *to.Searches {
keys[a.Vendor] = a
}
ss := slices.Clone(*searches)
for i, a := range ss {
ss[i].KeepKeysOf(keys[a.Vendor]) // those switched off too
}
searches = &ss
}
to.Providers, to.Icons, to.Groups, to.Searches, to.Order, to.GroupOrder = ps, from.Icons, from.Groups, searches, from.Order, from.GroupOrder
to.Plugins = from.Plugins
if from.Proxies != nil { // the saved proxies the providers name, their passwords as the keys are
px := slices.Clone(*from.Proxies)
if !keys && to.Proxies != nil && (to.Keys || (to.ProvidersKeys != nil && *to.ProvidersKeys)) {
for i := range px {
if j := slices.IndexFunc(*to.Proxies, func(o settings.NamedProxy) bool { return o.ID == px[i].ID }); j >= 0 {
px[i].KeepSecretsOf((*to.Proxies)[j]) // an SSH proxy's key with its password
}
}
}
to.Proxies = &px
}
to.ProvidersKeys = backup.Flag(keys || (to.ProvidersKeys != nil && *to.ProvidersKeys))
to.Keys = to.Keys || keys // the whole-bundle bit follows it, for a magpie that reads no per-part ones: it keeps the server's keys on its own upload then
case "settings":
keys := from.Keys || (from.SettingsKeys != nil && *from.SettingsKeys)
s := from.Settings
if s != nil && !keys && (to.Keys || (to.SettingsKeys != nil && *to.SettingsKeys)) && to.Settings != nil {
// Sent without keys: keep the ones the server has, as for providers.
copy := *s
copy.LANKey, copy.LANKeyID = to.Settings.LANKey, to.Settings.LANKeyID
copy.GitHubToken = to.Settings.GitHubToken
copy.BarkKey = to.Settings.BarkKey
copy.OTel.Headers = nil
if strings.TrimRight(strings.TrimSpace(copy.OTel.Endpoint), "/") == to.Settings.OTel.Endpoint {
copy.OTel.Headers = to.Settings.OTel.Headers
}
s = &copy
}
if s != nil {
to.Settings = s
}
if keys && from.GatewayKeys != nil {
to.GatewayKeys = from.GatewayKeys // an explicit empty store clears it
}
if keys && from.AccountGroups != nil {
to.AccountGroups = from.AccountGroups
}
to.SettingsKeys = backup.Flag(keys || (to.SettingsKeys != nil && *to.SettingsKeys))
case "profiles":
to.Profiles = from.Profiles
case "agents":
to.Agents = from.Agents
case "library":
keys := from.Keys || (from.LibraryKeys != nil && *from.LibraryKeys)
lib := from.Library
// An empty secret is not a clear: a keyed computer's library holds
// "" for a server it synced and never held a token for, and writes
// that over the token the keyless computer that set it up has. The
// server's value is kept for such an entry whatever the policy; a
// server taken out here is gone, as a part's own clearing works.
if lib != nil && (to.Keys || (to.LibraryKeys != nil && *to.LibraryKeys)) {
lib = lib.WithSecrets(to.Library, backup.Secret)
}
to.Library = lib
to.LibraryKeys = backup.Flag(keys || (to.LibraryKeys != nil && *to.LibraryKeys))
}
to.Version = backup.BundleVersion // the merged bundle is a sync one, whatever the server's file was
}
// changed is when a part was last changed here, as its files say.
func changed(part string, keys bool) time.Time {
mtime := func(p string) time.Time {
if fi, err := os.Stat(p); err == nil {
return fi.ModTime()
}
return time.Time{}
}
switch part {
case "providers":
return mtime(provider.Path())
case "settings":
m := mtime(settings.Path())
if k := mtime(access.Path()); keys && k.After(m) {
m = k
}
if g := mtime(access.GroupsPath()); keys && g.After(m) {
m = g
}
return m
case "profiles":
return mtime(profile.Path())
case "library":
return library.Changed()
}
var t time.Time
for _, a := range agent.Detected() {
if m := mtime(a.Path); m.After(t) {
t = m
}
}
return t
}
// bring puts the server's part in here: providers and profiles mirrored,
// so what went elsewhere goes here too.
func bring(b backup.Bundle, part string) error {
switch part {
case "providers":
for name, data := range b.Icons {
if f := provider.IconFile(name); f != "" && len(data) <= provider.MaxIcon {
if _, err := os.Stat(f); err != nil {
os.MkdirAll(filepath.Dir(f), 0o755)
edit.WriteAtomic(f, data)
}
}
}
if err := provider.Mirror(b.Providers, b.Groups); err != nil {
return err
}
if err := provider.MirrorOrder(b.Order); err != nil {
return err
}
if err := provider.MirrorGroupOrder(b.GroupOrder); err != nil {
return err
}
// the plugins the providers come from, added here; never taken
// away, and one that can't be added is said, not a failed sync
if b.Plugins != nil {
if _, failed := addPlugins(*b.Plugins); len(failed) > 0 {
for spec, err := range failed {
log.Printf("sync: plugin %s: %s", spec, err)
}
}
}
if b.Searches == nil { // from a magpie before them: the ones here stay
return nil
}
return provider.MirrorSearchAPIs(*b.Searches)
case "settings":
_, err := backup.Restore(b, backup.Parts{Settings: true})
return err
case "profiles":
here, err := profile.Load()
if err != nil {
return err
}
for name := range here {
if _, ok := b.Profiles[name]; !ok {
if err := profile.Delete(name); err != nil {
return err
}
}
}
_, err = backup.Restore(b, backup.Parts{Profiles: true})
return err
case "agents":
_, err := backup.Restore(b, backup.Parts{Agents: true})
return err
case "library":
_, err := bringLibrary(b)
return err
}
return nil
}
// bringLibrary puts the library b carries here, and says what of it
// couldn't be: a skill not fetched from its source yet, one kept as it was
// changed here (library.Put).
func bringLibrary(b backup.Bundle) ([]library.Problem, error) {
r, err := backup.Restore(b, backup.Parts{Library: true})
return r.LibraryProblems, err
}
// said is library problems as the Settings page says them: "pdf: why".
func said(probs []library.Problem) []string {
var out []string
for _, p := range probs {
_, name, ok := strings.Cut(p.What, ":")
if !ok {
name = p.What
}
out = append(out, name+": "+p.Error)
}
return out
}
// leftOut is the skills and plugins whose files this computer's library
// leaves out of a sync, too big for it (library.Collect), or doesn't have
// as another computer left them out (library.Skill's Missing). A skill
// carried as its source isn't: each computer fetches it from there.
func leftOut(l *library.Bundle) []string {
if l == nil {
return nil
}
var names []string
for _, s := range l.Skills {
if len(s.Left) > 0 && !s.Fetch {
names = append(names, s.Name)
}
}
if l.Plugins != nil {
for _, p := range *l.Plugins {
if len(p.Left) > 0 {
names = append(names, p.Name)
}
}
}
return names
}
// keepUnseenGatewayKeys puts the gateway keys this computer holds that the
// folder's store has never seen into the store a first sync is about to
// bring in (#1714, 8ugMak1r). The store rides the settings part, so a
// computer joining a folder whose store has none of its keys would have
// them replaced — credentials the folder never held a chance to revoke. A
// key the folder holds by id or by value is the folder's, and its copy
// wins; the default key (LAN) follows the folder's settings either way.
// This runs on the first sync alone: afterwards absence from a store this
// computer has seen means a removal, which must go from every computer.
func keepUnseenGatewayKeys(remote *backup.Bundle, local backup.Bundle) {
// the account groups this computer's keys may name, by id, the same
// way (LeiWang_2000 on X): one the folder holds is the folder's
if local.AccountGroups != nil && len(*local.AccountGroups) > 0 {
if remote.AccountGroups == nil {
remote.AccountGroups = &[]access.AccountGroup{}
}
for _, g := range *local.AccountGroups {
if !slices.ContainsFunc(*remote.AccountGroups, func(o access.AccountGroup) bool { return o.ID == g.ID }) {
*remote.AccountGroups = append(*remote.AccountGroups, g)
}
}
}
if local.GatewayKeys == nil || len(*local.GatewayKeys) == 0 || remote.GatewayKeys == nil {
return
}
seen := map[string]bool{}
for _, k := range *remote.GatewayKeys {
seen[k.ID], seen[k.Secret] = true, true
}
for _, k := range *local.GatewayKeys {
if k.LAN || k.Secret == "" || seen[k.ID] || seen[k.Secret] {
continue
}
*remote.GatewayKeys = append(*remote.GatewayKeys, k)
seen[k.ID], seen[k.Secret] = true, true
}
}
// syncOnce syncs once. With pv, it is a preview: the server's file is
// read whole, what the sync would do is told in pv, and nothing is changed
// here or on the server.
func syncOnce(ctx context.Context, c Config, st *state, pv *Plan) error {
d, err := newRemote(c)
if err != nil {
return err
}
// the file only if it changed since the last sync: one unchanged costs
// a request, not a download
data, ver, err := d.get(ctx, st.Server)
unchanged := errors.Is(err, errNotModified)
if err != nil && !unchanged {
return err
}
// a blind write read back: the server still has it, or another
// computer's write went over it (blind)
if b := st.Blind; b != nil {
if !unchanged && sum(data) != b.Sum {
st.Remote, st.Local = b.Remote, b.Local
st.SignIns = nil // what went up may be gone: nothing is taken for removed
st.Plugins = nil
}
st.Blind = nil
}
local, err := collect(c)
if err != nil {
return err
}
// An agent field picked here after a sync set it is this computer's
// own from now on (own.go). prev is the own fields the last sync's
// hashes left out, so this one's compare with them alike: a field
// owned now is a change here, never a change on both sides. own is
// every field kept here this time, and after what the next sync's
// hashes leave out: none of them, once Settings gave them back.
// An agent whose picks stay here (hereOf) is own in every field, as
// the last sync had it in prev, as it is now in keep and afterW.
prev := withHere(maps.Clone(st.Own), st.Here)
if c.Agents {
noticeOwn(st, local.Agents)
}
own, after := st.Own, st.Own
follow := st.Follow && len(own) > 0 && c.Agents
if follow {
after = nil
}
st.Follow = false
kept := hereOf(c, local)
moved := !slices.Equal(kept, st.Here)
keep, afterW := withHere(own, kept), withHere(after, kept)
L := hashesOwn(local, prev)
// parts left out at the last sync and synced now: joined again
var rejoin []string
for _, p := range st.Off {
if slices.Contains(Parts, p) && c.syncs(p) {
rejoin = append(rejoin, p)
}
}
// a state written before Off was recorded (#3356): the last sync left
// a part out but wrote no mark of it, and all it left of the part here
// is the nothing-hash a left-out part carries. One that holds something
// of its own now joins like one turned back on, its older content never
// taken as a change here over the server's newer one.
nothing := hashes(backup.Bundle{})
for _, p := range Parts {
if !c.syncs(p) || slices.Contains(rejoin, p) || st.Local == nil {
continue
}
if st.Local[p] == nothing[p] && L[p] != st.Local[p] && !unset(local, p) {
rejoin = append(rejoin, p)
}
}
if unchanged {
if maps.Equal(L, st.Local) && !follow && !moved && len(rejoin) == 0 {
return nil // nothing changed on either side
}
// changed here: merged with the server's file as last seen, or,
// when that copy is gone, with the file read again
if data = cached(st.Sum); data == nil {
if data, ver, err = d.get(ctx, version{}); err != nil {
return err
}
}
}
etag := ver.ETag
rebuilt := false // a damaged server file replaced by this computer's merge
// push writes b over etag; were, as the parts on the server and here
// were before it, is kept when the write is blind
push := func(b backup.Bundle, etag string, were blind) error {
b.Created, b.App = time.Now().UTC(), "magpie"
sealed, err := backup.Seal(b, c.Passphrase)
if err != nil {
return err
}
v, err := d.put(ctx, sealed, etag)
if err != nil {
return err
}
st.Sum, st.Server, st.Remote = sum(sealed), v, hashesOwn(b, afterW)
if v.Blind {
were.Sum = st.Sum
st.Blind = &were
}
remember(sealed)
return nil
}
if data == nil && c.FollowServer {
return errNothingToFollow
}
if data == nil && pv != nil {
pv.Empty = true
for _, p := range c.Synced() {
pv.Parts = append(pv.Parts, PartPlan{Part: p, Do: "up"})
}
return nil
}
if data == nil { // nothing there yet: this computer's setup is the first
// the parts not synced go as unset, never this computer's (only)
if err := push(c.only(local), "", blind{}); err != nil {
return err
}
st.Own, st.Here = after, kept
st.Local = hashesOwn(local, afterW)
st.SignIns = nil
if c.SignIns {
st.SignIns = local.SignIns.Hashes()
}
st.Plugins = nil
if c.syncs("providers") {
st.Plugins = pluginNames(local.Plugins)
}
st.Off = c.left()
st.LibraryLeft = nil
if c.library() {
st.LibraryLeft = leftOut(local.Library)
}
return nil
}
if sum(data) == st.Sum && maps.Equal(L, st.Local) && !follow && !moved && len(rejoin) == 0 {
st.Server = ver
remember(data)
return nil // nothing changed on either side
}
remote, err := backup.Open(data, c.Passphrase)
if errors.Is(err, backup.ErrPassphrase) {
return errors.New("the passphrase doesn't open the file on the server: it was sealed with another one; use the passphrase set on your other computers")
}
if err != nil {
// The server's file can't be read as a backup: a write a relay or
// tunnel cut short, or one damaged since. Failing here would stop
// every sync after, because this computer's own setup is fine and
// could never be pushed over the unreadable file. But rebuilding
// must not lose another computer's newer setup that the damaged
// write swallowed, so this is not a place to push this computer's
// bundle as it is. It is a place to fall back to the last good copy
// of the server's file — the one this computer read whole before it
// broke — and merge over it as a normal sync would. That needs three
// things to hold, and any one missing means the error stands:
// - this computer has synced before (st.Local): a fresh one has no
// last-good copy and no right to rebuild what it never saw;
// - the cached copy is the file this sync hashed (sum == st.Sum),
// so it is the version the damage replaced, not some older one;
// - the body really reads as a damaged backup, not as some other
// answer a server gave (a captive portal's page): a body that
// isn't backup-shaped at all is its own error, never rebuilt.
// The merge then runs with the cached copy as the remote and the
// damaged file's ETag as the version to replace, so the push is
// still checked against the server and another computer's parts
// survive the way they would on any sync.
// A computer following the server never writes it, a rebuild
// included: the computer it follows, or Upload, replaces the file.
if !errors.Is(err, backup.ErrCorrupt) || st.Local == nil || c.FollowServer || pv != nil {
return described(c, data, err)
}
good := cached(st.Sum) // the version this computer last read whole
// The damaged body must be that version cut short — a prefix of it,
// which is what a relay that stops a long write leaves. But Seal
// writes with json.MarshalIndent, so every version starts with the
// same constant head up to the salt; a body cut inside that head is a
// prefix of every version, including an out-of-date computer's older
// cached copy. So the prefix check alone lets that computer rebuild
// and drop a newer one's data. Only a body reaching past the random
// fields — to the "data" key that follows them — is tied to the one
// version it was cut from. Requiring it keeps an out-of-date computer
// from rebuilding (its copy isn't the version that broke, so the body
// isn't its prefix and the error stands), and a body cut before the
// random fields stays an error for every computer, even the one that
// saw the good version: better to fail loud than rebuild blind. A
// sealed file holds a whole setup — hundreds of bytes at its very
// smallest — and a relay cuts a real write deep into the data, well
// past "data", so legit rebuilds still go.
if good == nil || !bytes.HasPrefix(good, data) || len(data) <= bytes.Index(good, []byte(`"data"`)) {
return described(c, data, err)
}
remote, err = backup.Open(good, c.Passphrase)
if err != nil {
return err
}
keepDamaged(data) // the unreadable body, kept aside before it is replaced
data = good
rebuilt = true // push the merged result even if it matches the cached copy
}
// R compares with the last sync as its hashes were; Rafter is the
// server's file as this sync leaves it read, both before the merge
// below changes remote (a join's unseen gateway keys)
R, Rafter := hashesOwn(remote, prev), hashesOwn(remote, afterW)
first := st.Local == nil
// plugins removed on one side since the last sync: gone from the other
// too, and not brought back by the part that carries them
// providers not synced: their plugins neither (they ride that part)
var goneHere, goneThere []string
if c.syncs("providers") {
goneHere, goneThere = pluginPlan(st.Plugins, local.Plugins, remote.Plugins)
}
if len(goneHere) > 0 && remote.Plugins != nil {
kept := without(*remote.Plugins, goneHere)
remote.Plugins = &kept
}
merged := remote
var bringIn, here, there []string
for _, p := range Parts {
if L[p] == R[p] || !c.syncs(p) {
continue
}
if p == "library" && remote.Library == nil { // from a magpie before the library went: this computer's goes up
take(&merged, local, p)
continue
}
lc := !first && L[p] != st.Local[p]
rc := first || R[p] != st.Remote[p]
again := !first && slices.Contains(rejoin, p)
if again {
// synced again after it was left out: each side's own counts
// as changed there, as on joining; a side with none of its own
// takes the other's (#5642), both with some, the newer stays
lc, rc = !unset(local, p), !unset(remote, p)
if !lc && !rc {
lc, rc = true, true
}
}
if c.FollowServer {
// following the server: what changed on either side is the
// server's, a change here kept aside first. A part the server's
// file holds nothing of on joining is left as it is here
// (#5642): nothing has been set there to follow yet.
if remote.Library == nil && p == "library" || (first || again) && unset(remote, p) && !unset(local, p) {
continue
}
if lc || rc {
bringIn = append(bringIn, p)
}
if lc || first {
here = append(here, p)
}
continue
}
switch {
case lc && !rc:
take(&merged, local, p)
if p == "agents" {
keepServersOwn(&merged, remote, keep)
}
case rc && !lc && first && unset(remote, p) && !unset(local, p):
// Joining a file whose part holds nothing of its own (its
// first writer had no providers, no settings of its own, an
// empty library): what this computer has goes up, never the
// empty part down over it (#5642: three computers syncing the
// library, and the one with providers and routing groups lost
// them to a fresh one that wrote the file first). After the
// join, a part emptied on purpose spreads as before.
take(&merged, local, p)
there = append(there, p)
case rc && !lc:
if first && p == "settings" {
keepUnseenGatewayKeys(&remote, local)
}
bringIn = append(bringIn, p)
if first {
here = append(here, p)
}
case lc && rc: // both: the newer stays
if changed(p, c.Keys).After(remote.Created) {
take(&merged, local, p)
if p == "agents" {
keepServersOwn(&merged, remote, keep)
}
there = append(there, p)
} else {
bringIn = append(bringIn, p)
here = append(here, p)
}
}
}
if pv != nil {
return previewed(pv, c, local, remote, merged, bringIn, here, there, goneThere, st.SignIns)
}
var saved string
if len(here)+len(there) > 0 {
dir := path("sync")
os.MkdirAll(dir, 0o700)
stamp := time.Now().Format("2006-01-02-150405")
if len(here) > 0 {
if b, err := backup.Seal(local, c.Passphrase); err == nil {
edit.WriteAtomic(filepath.Join(dir, stamp+"-this-computer"+backup.Ext), b)
}
}
if len(there) > 0 {
edit.WriteAtomic(filepath.Join(dir, stamp+"-server"+backup.Ext), data)
}
saved = dir
}
// what is here before the server's parts come in, sealed with the
// keys, for UndoSync; a copy that can't be kept stops nothing
var before string
if len(bringIn) > 0 {
var err error
if before, err = keepSealed(c, "before-sync"); err != nil {
log.Printf("sync: keeping this computer's setup before bringing in %v: %v", bringIn, err)
}
}
broughtLib := false
for _, p := range bringIn {
if p == "agents" {
got, err := bringAgents(remote.Agents, afterW)
broughtIn(st, got)
if err != nil {
return fmt.Errorf("bringing in the %s: %w", p, err)
}
continue
}
if p == "library" {
probs, err := bringLibrary(remote)
st.LibraryProblems, broughtLib = probs, true
if err != nil {
return fmt.Errorf("bringing in the %s: %w", p, err)
}
continue
}
if err := bring(remote, p); err != nil {
return fmt.Errorf("bringing in the %s: %w", p, err)
}
}
if follow && c.Agents && !slices.Contains(bringIn, "agents") {
// the fields given back to sync take the server's values, the
// agents' other fields left as they are here
fields := map[string]string{}
for k := range own {
if v, ok := remote.Agents[k]; ok {
fields[k] = v
}
}
got, err := bringAgents(fields, nil)
broughtIn(st, got)
if err != nil {
return fmt.Errorf("bringing in the agents: %w", err)
}
}
st.Own, st.Here, R = after, kept, Rafter
// the plugins the other computers removed, removed here as Settings
// removes one; one that can't be (its agent is on magpie) is tried
// again at the next sync, and isn't sent up meanwhile
var stuck []string
for _, name := range goneThere {
if err := removePlugin(name); err != nil {
log.Printf("sync: removing plugin %s, which another computer removed: %v", name, err)
stuck = append(stuck, name)
}
}
if gone := slices.Concat(goneHere, goneThere); len(gone) > 0 && merged.Plugins != nil {
kept := without(*merged.Plugins, gone)
merged.Plugins = &kept
}
// the server's file is in; what is pushed next counts as changed here
// until the push is done
if len(bringIn) > 0 || follow || len(goneThere) > 0 {
if local, err = collect(c); err != nil {
return err
}
}
// the sign-ins, when they go: merged one by one, the later renewed of
// each account staying, and what came from the server put in here
// without writing over what changed here meanwhile
var shared map[string]string
if c.SignIns {
var plan provider.SignInPlan
// the server's of the subscriptions left out here: not put in
// here, and going on up as they were, for the others
theirs, held := remote.SignIns.Except(c.SignInsExcept)
merged.SignIns, plan, shared = provider.MergeSignIns(local.SignIns, theirs, st.SignIns)
merged.SignIns = merged.SignIns.With(held)
if !plan.Empty() {
if _, err := provider.ApplySignIns(plan); err != nil {
return fmt.Errorf("bringing in the sign-ins: %w", err)
}
if local, err = collect(c); err != nil {
return err
}
}
}
// memory, when it goes: merged memory by memory (memory.Combine),
// what the server has put in here, and both sides' going up
if c.memoryOn() && (local.Memory != nil || remote.Memory != nil) {
merged.Memory = memory.Combine(local.Memory, remote.Memory)
changed, err := memory.Put(remote.Memory)
if err != nil {
return fmt.Errorf("bringing in memory: %w", err)
}
if changed {
// the agents' instructions given what came, as after a change
// the user made to memory
if _, err := library.PublishMemory(false); err != nil {
log.Printf("sync: memory digest: %v", err)
}
if local, err = collect(c); err != nil {
return err
}
}
}
now := hashesOwn(local, afterW)
M, pending := hashesOwn(merged, afterW), map[string]string{}
for _, p := range append(slices.Clone(Parts), "signins", "memory") {
if _, ok := now[p]; !ok && (p == "signins" || p == "memory") {
continue
}
pending[p] = now[p]
if M[p] != R[p] && st.Local != nil {
pending[p] = st.Local[p]
}
}
st.Local, st.Sum, st.Server, st.Remote = pending, sum(data), ver, R
remember(data)
if len(here)+len(there) > 0 {
st.Notice = &Notice{At: time.Now(), Here: here, There: there, Saved: saved, Followed: c.FollowServer && !first}
st.Undo = "" // a restore's undo goes with its notice
}
// A rebuild always writes back: the server's file is the damaged one,
// so even a merge that matches the cached copy must replace it.
if !c.FollowServer && (rebuilt || !maps.Equal(M, R)) {
if err := push(merged, etag, blind{Remote: R, Local: pending}); err != nil {
return err
}
}
st.Local = now
st.Off = c.left()
if !broughtLib {
st.LibraryProblems = library.Unsettled(st.LibraryProblems)
}
st.LibraryLeft = nil
if c.library() {
st.LibraryLeft = leftOut(local.Library)
}
if before != "" {
st.Applied = &applied{At: time.Now(), Parts: bringIn, File: before, After: now}
}
st.SignIns = nil
if c.SignIns {
st.SignIns = bothSides(shared, local.SignIns.Hashes())
}
st.Plugins = nil
if merged.Plugins != nil && c.syncs("providers") {
// on both sides now; a removal that failed here stays known, so
// the next sync tries it again
both := slices.DeleteFunc(pluginNames(local.Plugins), func(n string) bool {
return !slices.Contains(pluginNames(merged.Plugins), n)
})
st.Plugins = append(both, stuck...)
}
return nil
}
// pluginNames is the names of the plugins in es, nil for none.
func pluginNames(es *[]plugin.Entry) []string {
if es == nil {
return nil
}
var out []string
for _, e := range *es {
out = append(out, plugin.Name(e.Spec))
}
return out
}
// without is es less the plugins named.
func without(es []plugin.Entry, names []string) []plugin.Entry {
out := []plugin.Entry{}
for _, e := range es {
if !slices.Contains(names, plugin.Name(e.Spec)) {
out = append(out, e)
}
}
return out
}
// pluginPlan tells which plugins one side removed since the last sync (base,
// the names both had then), as sign-ins are told: a plugin base had that
// here lacks was removed here (goneHere), and one the server lacks was
// removed there (goneThere, still here). A plugin on one side that base
// never had is a new one, and stays. Nothing is told when base is unknown
// or the server's file carries no plugin list (an older magpie).
func pluginPlan(base []string, local, remote *[]plugin.Entry) (goneHere, goneThere []string) {
if len(base) == 0 || remote == nil {
return nil, nil
}
here, there := pluginNames(local), pluginNames(remote)
for _, n := range base {
switch inHere, inThere := slices.Contains(here, n), slices.Contains(there, n); {
case !inHere && inThere:
goneHere = append(goneHere, n)
case inHere && !inThere:
goneThere = append(goneThere, n)
}
}
return goneHere, goneThere
}
// bothSides is the sign-ins on the server (shared) that are here too, as
// the server has them: one the server has that wasn't put in here (more
// than one account here could be it, the agent's own here) isn't taken
// for removed here at the next sync.
func bothSides(shared, here map[string]string) map[string]string {
out := map[string]string{}
for id, h := range shared {
if _, ok := here[id]; ok {
out[id] = h
}
}
return out
}