Files
magpie/internal/davsync/plan.go
T
yetone ff88ec264a sync: subscriptions whose sign-ins stay on this computer (Ethereal499 on X)
Sync's sign-ins can now leave chosen subscriptions out on one computer
(`signInsExcept`, provider ids; Ethereal499 on X keeps Claude's accounts
off other computers). On that computer:

- its accounts of those subscriptions never go up;
- the server's accounts of them are never merged in, so a removal or a
  new account on another computer changes nothing here;
- the server keeps them as they are for the computers that do sync them
  (syncOnce splits the remote set with SignIns.Except and puts the held
  part back with With after the merge, so the hash doesn't change);
- the preview and Restore count and restore none of them.

An id names both the built-in and its -plugin id. An old config has no
field and syncs everything as before. The setup text doesn't carry the
choice, since it belongs to one computer.

GUI: Settings > Sync > edit > Advanced, under "Sign-ins", a "Not synced"
row with a multi-select openProtoMenu of the subscriptions with an
account. The row shows only while the sign-ins tick is on. The strings
already existed in en, zh, zh-TW, ja and de. CLI: `sign-ins-except=`
on the WebDAV, S3 and GitHub setups ("none" clears it), and status
shows the excluded ids. docs/subsystems/sync-backup.md says it.

Tests:
- TestSignInsExcept (provider) and TestSyncSignInsExcept (davsync, two
  computers on a fake DAV).
- sync-signins-except.test.cjs: both engines, 1100 and 420px, all 5
  languages.

Broken by hand, each red and then restored:
- the Except in collect;
- the Except on the remote set before the merge;
- the Except in the preview;
- the With after the merge;
- the GUI form's post of signInsExcept.

Run:
- At ac4a4cf84 on mote: `go test -timeout 45m ./...`, 102 packages ok,
  exit 0.
- At ac4a4cf84, the whole GUI suite on mote. The first part ran at
  concurrency 6 under load 25 to 37, and mote rebooted mid-run (not
  this session's doing). The 410 files it never reached ran at
  concurrency 4. Reds fell in 125 files, none of them a sync-* file
  except s3-sync (see below).
- At the rebased head b781d0bcb on mote:
  - go test ./internal/davsync ./internal/provider ./internal/cli
    ./internal/gui: ok.
  - Every sync-*.test.cjs, webdav-settings-refresh and
    sync-signins-except: ok.
- gofmt -l: clean.
- On mica: go vet ./..., GOOS=linux go vet -tags nogui ./... and
  GOOS=windows go vet ./... ok, plus go build, GOOS=linux go build
  -tags nogui and GOOS=windows go build.

Red the same on origin/main 873b1f7d5, run there alone:
- s3-sync webkit en ("picking S3 must not scroll the page"), #3413 and
  #2448.
- i18n-keys (", with an image" in plugins/routing), #6877.

Not run: the full Go suite at the rebased head. Another session's full
suite held mote, and mica was full. The 125 GUI files red under load
were not each run alone on main.

Not tried: a real sync between two computers with real accounts.
2026-11-17 14:08:48 +08:00

446 lines
12 KiB
Go

package davsync
import (
"context"
"encoding/json"
"errors"
"fmt"
"maps"
"os"
"slices"
"strconv"
"time"
"github.com/yetone/magpie/internal/backup"
"github.com/yetone/magpie/internal/library"
"github.com/yetone/magpie/internal/memory"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// applied is the last sync that brought parts in from the server (#6428):
// which, the copy of this computer kept before (keepSealed), and the
// parts' hashes right after, so UndoSync can tell a change made since.
type applied struct {
At time.Time `json:"at"`
Parts []string `json:"parts"`
File string `json:"file"`
After map[string]string `json:"after,omitempty"`
}
// Applied is what Status tells of the last sync that brought parts in,
// while its copy is there to undo it.
type Applied struct {
At time.Time `json:"at"`
Parts []string `json:"parts"`
}
// Plan is what a sync would do now (Preview), nothing done.
type Plan struct {
// Empty: the server has no file yet, and this computer's goes up.
Empty bool `json:"empty,omitempty"`
// Follow: this computer follows the server, never writing it.
Follow bool `json:"follow,omitempty"`
Parts []PartPlan `json:"parts"`
// Memory is how many memories come in here and go up, by id.
Memory *MemoryPlan `json:"memory,omitempty"`
// SignIns is how many sign-ins change here, when they sync.
SignIns int `json:"signIns,omitempty"`
// PluginsGone are the plugins removed here because another computer
// removed them.
PluginsGone []string `json:"pluginsGone,omitempty"`
// Token tells this plan from another: SyncPreviewed syncs only while
// the sync would still do what it says.
Token string `json:"token"`
}
// PartPlan is what a sync would do with one part.
type PartPlan struct {
Part string `json:"part"`
// Do is "in" (the server's comes in here), "up" (this computer's goes
// to the server), "same", or "off" (not synced: stays as it is here).
Do string `json:"do"`
// Replaces: a change made here too is replaced, a copy kept first.
Replaces bool `json:"replaces,omitempty"`
Items []Item `json:"items,omitempty"`
}
// Item is one thing in a part that the sync adds, removes or changes on
// the side it writes. Only names: never a value, which may be a key.
type Item struct {
Kind string `json:"kind"`
Name string `json:"name"`
Change string `json:"change"` // added, removed, changed
}
// MemoryPlan counts the memories a sync would change on each side.
type MemoryPlan struct {
In int `json:"in"`
Up int `json:"up"`
}
// ErrPlanChanged is SyncPreviewed's when the sync would now do something
// other than the preview said: something changed here or on the server.
var ErrPlanChanged = errors.New("something changed since the preview: look at it again before syncing")
// Preview is what syncing now would do, with the setup saved; nothing is
// changed here or on the server.
func Preview(ctx context.Context) (Plan, error) {
var p Plan
err := locked(func() error {
c, ok := Load()
if !ok {
return ErrOff
}
st := loadState()
if st.Key != stateKey(c) {
st = state{Key: stateKey(c), Brought: st.Brought, Replaced: st.Replaced, Own: st.Own, Follow: st.Follow, Here: st.Here}
}
var err error
p, err = preview(ctx, c, st)
return err
})
return p, err
}
// PreviewWith is Preview for a setup not saved yet: Settings shows what
// saving it would do first. A password or passphrase left empty is the
// saved one's, as Configure takes it.
func PreviewWith(ctx context.Context, c Config) (Plan, error) {
var p Plan
err := locked(func() error {
if old, ok := Load(); ok {
if c.Password == "" && c.URL == old.URL && c.User == old.User && c.Endpoint == old.Endpoint {
c.Password = old.Password
}
if c.Passphrase == "" {
c.Passphrase = old.Passphrase
}
}
st := loadState()
if st.Key != stateKey(c) {
st = state{Key: stateKey(c), Brought: st.Brought, Replaced: st.Replaced, Own: st.Own, Follow: st.Follow, Here: st.Here}
}
var err error
p, err = preview(ctx, c, st)
return err
})
return p, err
}
// preview runs syncOnce as a preview over a copy of st: the server's file
// is read whole, whatever was seen of it before.
func preview(ctx context.Context, c Config, st state) (Plan, error) {
st.Server = version{}
st.Local, st.Remote = maps.Clone(st.Local), maps.Clone(st.Remote)
st.Own, st.Brought, st.Replaced = maps.Clone(st.Own), maps.Clone(st.Brought), maps.Clone(st.Replaced)
var p Plan
if err := syncOnce(ctx, c, &st, &p); err != nil {
return Plan{}, err
}
p.Follow = c.FollowServer
p.Token = ""
j, _ := json.Marshal(p)
p.Token = sum(j)[:16]
return p, nil
}
// previewed fills pv from what syncOnce decided.
func previewed(pv *Plan, c Config, local, remote, merged backup.Bundle, bringIn, here, there, goneThere []string, prevSignIns map[string]string) error {
M := hashes(merged)
Rh := hashes(remote)
for _, p := range Parts {
pp := PartPlan{Part: p, Do: "same"}
switch {
case !c.syncs(p):
pp.Do = "off"
case slices.Contains(bringIn, p):
pp.Do = "in"
pp.Replaces = slices.Contains(here, p)
pp.Items = diffItems(itemsOf(local, p), itemsOf(remote, p))
case M[p] != Rh[p] && !c.FollowServer:
pp.Do = "up"
pp.Replaces = slices.Contains(there, p)
pp.Items = diffItems(itemsOf(remote, p), itemsOf(local, p))
}
pv.Parts = append(pv.Parts, pp)
}
pv.PluginsGone = slices.Clone(goneThere)
if c.memoryOn() && (local.Memory != nil || remote.Memory != nil) {
both := memory.Combine(local.Memory, remote.Memory)
in, up := memoryDiff(local.Memory, both), memoryDiff(remote.Memory, both)
if c.FollowServer {
up = 0
}
if in+up > 0 {
pv.Memory = &MemoryPlan{In: in, Up: up}
}
}
if c.SignIns {
theirs, _ := remote.SignIns.Except(c.SignInsExcept) // the server's left out here: not put in
_, plan, _ := provider.MergeSignIns(local.SignIns, theirs, prevSignIns)
pv.SignIns = plan.Count()
}
return nil
}
// memoryDiff is how many memories of to aren't in from as they are there:
// new, changed, or deleted.
func memoryDiff(from, to *memory.Bundle) int {
key := func(m *memory.Memory) string {
k := *m
k.Used, k.Uses = time.Time{}, 0
j, _ := json.Marshal(k)
return string(j)
}
have := map[string]string{}
if from != nil {
for _, m := range from.Memories {
if m != nil {
have[m.ID] = key(m)
}
}
}
n := 0
seen := map[string]bool{}
for _, m := range to.Memories {
if m == nil {
continue
}
seen[m.ID] = true
if have[m.ID] != key(m) {
n++
}
}
for id := range have {
if !seen[id] {
n++
}
}
return n
}
// itemsOf is part p of b as named items (kind\x00key → its JSON), as its
// hash sees them: what is this computer's own left out.
func itemsOf(b backup.Bundle, p string) map[string]item {
out := map[string]item{}
switch p {
case "providers":
flatten(out, "provider", syncedProviders(b.Providers))
flatten(out, "group", b.Groups)
if b.Searches != nil {
flatten(out, "search", *b.Searches)
}
if b.Plugins != nil {
flatten(out, "plugin", *b.Plugins)
}
if b.Proxies != nil {
flatten(out, "proxy", *b.Proxies)
}
if len(b.Order) > 0 || len(b.GroupOrder) > 0 {
flatten(out, "order", map[string]any{"providers": b.Order, "groups": b.GroupOrder})
}
case "settings":
var s settings.Settings
if b.Settings != nil {
s = *b.Settings
}
s.KeepOwn(settings.Settings{})
flatten(out, "setting", s)
if b.GatewayKeys != nil {
flatten(out, "gatewayKey", *b.GatewayKeys)
}
if b.AccountGroups != nil {
flatten(out, "accountGroup", *b.AccountGroups)
}
case "profiles":
flatten(out, "profile", b.Profiles)
case "agents":
flatten(out, "agent", b.Agents)
case "library":
if b.Library == nil {
break
}
var top map[string]json.RawMessage
j, _ := json.Marshal(b.Library)
json.Unmarshal(j, &top)
for k, v := range top {
if k == "icons" {
continue
}
var x any
json.Unmarshal(v, &x)
if k == "instructions" {
out["instructions\x00"] = item{Kind: "instructions", Raw: string(v)}
continue
}
// billion-context's own config is one file, shown by its name
if k == "bili" {
out["bili\x00"] = item{Kind: "file", Name: "billion-context.json", Raw: string(v)}
continue
}
// Claude Code's own plugins: each marketplace by its name, each
// plugin by its id
if k == "claudePlugins" {
var cp library.ClaudePlugins
json.Unmarshal(v, &cp)
for name, m := range cp.Marketplaces {
out["claudeMarketplace\x00"+name] = item{Kind: "claudeMarketplace", Name: name, Raw: string(m)}
}
for _, id := range cp.Enabled {
out["claudePlugin\x00"+id] = item{Kind: "claudePlugin", Name: id, Raw: "true"}
}
continue
}
flatten(out, k, x)
}
}
return out
}
type item struct {
Kind, Name, Raw string
}
// flatten puts v's items in out: an object's by key, a list's by each
// element's id, name or spec.
func flatten(out map[string]item, kind string, v any) {
j, _ := json.Marshal(v)
var x any
json.Unmarshal(j, &x)
switch x := x.(type) {
case map[string]any:
for k, e := range x {
r, _ := json.Marshal(e)
out[kind+"\x00"+k] = item{Kind: kind, Name: k, Raw: string(r)}
}
case []any:
for i, e := range x {
r, _ := json.Marshal(e)
key, name := strconv.Itoa(i), ""
if o, ok := e.(map[string]any); ok {
for _, f := range []string{"id", "name", "spec", "key", "path"} {
if s, ok := o[f].(string); ok && s != "" {
key = s
break
}
}
for _, f := range []string{"name", "label", "id", "spec"} {
if s, ok := o[f].(string); ok && s != "" {
name = s
break
}
}
}
if name == "" {
name = key
}
out[kind+"\x00"+key] = item{Kind: kind, Name: name, Raw: string(r)}
}
}
}
// diffItems is what going from a to b adds, removes and changes, in order.
func diffItems(a, b map[string]item) []Item {
var out []Item
for _, k := range slices.Sorted(maps.Keys(b)) {
was, ok := a[k]
switch {
case !ok:
out = append(out, Item{Kind: b[k].Kind, Name: b[k].Name, Change: "added"})
case was.Raw != b[k].Raw:
out = append(out, Item{Kind: b[k].Kind, Name: b[k].Name, Change: "changed"})
}
}
for _, k := range slices.Sorted(maps.Keys(a)) {
if _, ok := b[k]; !ok {
out = append(out, Item{Kind: a[k].Kind, Name: a[k].Name, Change: "removed"})
}
}
return out
}
// UndoSync puts back the parts the last sync brought in, as they were
// before it (#6428). It stays on this computer: the next sync doesn't take
// it to the server, and the server's copy comes in again only when it
// changes there. A part changed here since that sync is not written over
// unless force, and the error says which.
func UndoSync(force bool) ([]string, error) {
var back []string
err := locked(func() error {
c, ok := Load()
if !ok {
return ErrOff
}
st := loadState()
a := st.Applied
if st.Key != stateKey(c) || a == nil || !isFile(a.File) {
return errors.New("there is no sync to undo")
}
data, err := os.ReadFile(a.File)
if err != nil {
return fmt.Errorf("the copy kept before the sync is gone: %w", err)
}
kept, err := backup.Open(data, c.Passphrase)
if err != nil {
return err
}
local, err := collect(c)
if err != nil {
return err
}
ownNow := withHere(st.Own, st.Here)
H := hashesOwn(local, ownNow)
if !force {
var since []string
for _, p := range a.Parts {
if a.After != nil && H[p] != a.After[p] {
since = append(since, p)
}
}
if len(since) > 0 {
return &ChangedSince{Parts: since}
}
}
if _, err := keepSealed(c, "before-undo"); err != nil {
return fmt.Errorf("keeping a copy before undoing: %w", err)
}
for _, p := range a.Parts {
if !c.syncs(p) || p == "library" && kept.Library == nil {
continue
}
if p == "agents" {
if _, err := bringAgents(kept.Agents, ownNow); err != nil {
return fmt.Errorf("bringing back the %s: %w", p, err)
}
} else if err := bring(kept, p); err != nil {
return fmt.Errorf("bringing back the %s: %w", p, err)
}
back = append(back, p)
}
// as the last sync left them: what is here now is not a change
// to take up, so the undo stays on this computer
if local, err = collect(c); err != nil {
return err
}
H = hashesOwn(local, ownNow)
if st.Local == nil {
st.Local = map[string]string{}
}
for _, p := range back {
st.Local[p] = H[p]
}
st.Applied = nil
saveState(st)
return nil
})
return back, err
}
// ChangedSince is UndoSync's when parts changed here after the sync.
type ChangedSince struct{ Parts []string }
func (e *ChangedSince) Error() string {
return fmt.Sprintf("changed here since the sync: %v", e.Parts)
}