From 5ed6669cec2a377e385d7047dc6cdc8ff23c6ef4 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 28 Oct 2026 11:56:51 +0800 Subject: [PATCH] sync: the account an agent is kept signed in to stays each computer's own YsiCing on X: with only the library and usage ticked, the default account of a subscription with several followed the other computer: a personal one at home, a work one at the office, set again every day. The providers part always syncs, and provider.Mirror took the other computer's Provider whole, KeepLogin, KeepLoginAs and SwitchOnly with it. Mirror now keeps this computer's three for a provider it has, and a new one comes without KeepLoginAs (an account signed in over there). davsync's hashes blank the three, so changing which account is kept uploads nothing and two computers that differ only in it don't trade the part. The sign-ins themselves (logins.json) were never synced. A backup Restore is left as it is: it is asked for, and brings the backup's choice. Run on mote at this tree: go test -count=1 ./internal/provider ./internal/davsync ./internal/backup ok; go vet for darwin, linux and windows on both packages ok. Broken by hand: with backup.go and davsync.go reverted, TestMirrorKeepsKeepLogin and TestKeepLoginIsNoChange both fail. Not tried: two real computers syncing through a real WebDAV folder. --- docs/subsystems/sync-backup.md | 8 ++++ internal/davsync/davsync.go | 6 ++- internal/davsync/keep_login_test.go | 28 ++++++++++++++ internal/provider/backup.go | 8 +++- internal/provider/mirror_keep_login_test.go | 42 +++++++++++++++++++++ 5 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 internal/davsync/keep_login_test.go create mode 100644 internal/provider/mirror_keep_login_test.go diff --git a/docs/subsystems/sync-backup.md b/docs/subsystems/sync-backup.md index 7c4ea953f..2492fbb37 100644 --- a/docs/subsystems/sync-backup.md +++ b/docs/subsystems/sync-backup.md @@ -47,6 +47,14 @@ session resumes in (`SessionTerminal`) and what the menu bar shows. Launch at login is the system's own setting (`internal/autostart`) and the listen address is `MAGPIE_ADDR`, so neither is in a bundle. +The providers part works the same way for which account an agent is kept +signed in to (`KeepLogin`, `KeepLoginAs`, `SwitchOnly`): one person keeps a +personal account at home and a work account at the office, and sync set +each computer to the other's every day (YsiCing on X). `provider.Mirror` keeps +this computer's values for a provider it has, and brings a new one without +`KeepLoginAs`, an account signed in on the other computer; `hashes` blanks +all three (`TestMirrorKeepsKeepLogin`, `TestKeepLoginIsNoChange`). + Sharing turned on at one computer never turns it on or off at another (Lullaby on Discord). A backup restored onto a new computer leaves sharing as that computer has it, off on a fresh one: sharing opens the gateway to the diff --git a/internal/davsync/davsync.go b/internal/davsync/davsync.go index 6b087ef5b..a6da36935 100644 --- a/internal/davsync/davsync.go +++ b/internal/davsync/davsync.go @@ -794,7 +794,11 @@ func hashes(b backup.Bundle) map[string]string { if b.GatewayKeys != nil { // a missing store keeps older bundles' hash settingsHash = h([]any{s, *b.GatewayKeys}) } - providers := []any{b.Providers, b.Icons, b.Groups} + ps := slices.Clone(b.Providers) + for i := range ps { // this computer's own: kept by Mirror, never synced + ps[i].KeepLogin, ps[i].KeepLoginAs, ps[i].SwitchOnly = false, "", false + } + providers := []any{ps, b.Icons, b.Groups} if b.Searches != nil && len(*b.Searches) > 0 { // as before them, without one providers = append(providers, *b.Searches) } diff --git a/internal/davsync/keep_login_test.go b/internal/davsync/keep_login_test.go new file mode 100644 index 000000000..09b3a0e93 --- /dev/null +++ b/internal/davsync/keep_login_test.go @@ -0,0 +1,28 @@ +package davsync + +import ( + "testing" + + "github.com/yetone/magpie/internal/backup" + "github.com/yetone/magpie/internal/provider" +) + +// Which account an agent is kept signed in to is this computer's own +// (provider.Mirror keeps it), so changing it uploads nothing, and two +// computers that differ only in it don't trade the providers part. +func TestKeepLoginIsNoChange(t *testing.T) { + p := provider.Provider{ID: "codex", Name: "Codex"} + home := backup.Bundle{Providers: []provider.Provider{p}} + p.KeepLogin, p.KeepLoginAs, p.SwitchOnly = true, "work@example.com", true + office := backup.Bundle{Providers: []provider.Provider{p}} + if hashes(home)["providers"] != hashes(office)["providers"] { + t.Fatal("the account kept signed in changed the providers hash") + } + if office.Providers[0].KeepLoginAs != "work@example.com" { + t.Fatal("hashing changed the bundle") + } + p.Name = "Codex renamed" + if hashes(home)["providers"] == hashes(backup.Bundle{Providers: []provider.Provider{p}})["providers"] { + t.Fatal("a rename no longer changes the providers hash") + } +} diff --git a/internal/provider/backup.go b/internal/provider/backup.go index 1738f4ea9..09293538e 100644 --- a/internal/provider/backup.go +++ b/internal/provider/backup.go @@ -91,7 +91,10 @@ func RestoreGroups(gs []Group) error { // Mirror makes the providers and groups exactly these, as sync brings them // from another computer: one not among them goes, one that came without // keys keeps the keys it has here, and an access key or balance token it -// came without is kept too (KeepSecrets). +// came without is kept too (KeepSecrets). Which account an agent is kept +// signed in to (KeepLogin, KeepLoginAs, SwitchOnly) is this computer's own: +// one person signs in to a personal account at home and a work account at +// the office, and sync set each computer to the other's every day. func Mirror(ps []Provider, gs []Group) error { f, err := read() if err != nil { @@ -109,6 +112,9 @@ func Mirror(ps []Provider, gs []Group) error { } if h, ok := here[p.ID]; ok { p = KeepSecrets(p, h, p.Key == "" && len(p.Keys) == 0) + p.KeepLogin, p.KeepLoginAs, p.SwitchOnly = h.KeepLogin, h.KeepLoginAs, h.SwitchOnly + } else { + p.KeepLoginAs = "" // an account signed in on the other computer } out = append(out, p) } diff --git a/internal/provider/mirror_keep_login_test.go b/internal/provider/mirror_keep_login_test.go new file mode 100644 index 000000000..a307884a2 --- /dev/null +++ b/internal/provider/mirror_keep_login_test.go @@ -0,0 +1,42 @@ +package provider + +import ( + "path/filepath" + "testing" +) + +// YsiCing on X: with sync's library and usage the only boxes ticked, the +// account Codex is kept signed in to followed the other computer: a +// personal one at home, a work one at the office, set again each day. +// Which account the agent stays signed in to is this computer's own. +func TestMirrorKeepsKeepLogin(t *testing.T) { + isolate(t) + h := t.TempDir() + t.Setenv("HOME", h) + t.Setenv("USERPROFILE", h) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache")) + t.Setenv("PATH", h) + here := Provider{ID: "relay", Name: "Relay", Chat: "https://example.com/v1", Key: "sk-here", + KeepLogin: true, KeepLoginAs: "home@example.com"} + if err := Save(here); err != nil { + t.Fatal(err) + } + office := Provider{ID: "relay", Name: "Relay renamed", Chat: "https://example.com/v1", Key: "sk-here", + KeepLoginAs: "work@example.com", SwitchOnly: true} + fresh := Provider{ID: "other", Name: "Other", Chat: "https://example.org/v1", Key: "sk-other", + KeepLogin: true, KeepLoginAs: "work@example.com"} + if err := Mirror([]Provider{office, fresh}, nil); err != nil { + t.Fatal(err) + } + ps, _ := Stored() + if len(ps) != 2 { + t.Fatalf("providers: %+v", ps) + } + if got := ps[0]; got.Name != office.Name || !got.KeepLogin || got.KeepLoginAs != here.KeepLoginAs || got.SwitchOnly { + t.Fatalf("mirrored: name %q keepLogin %v as %q switchOnly %v, want the other's name and this computer's sign-in", got.Name, got.KeepLogin, got.KeepLoginAs, got.SwitchOnly) + } + if got := ps[1]; !got.KeepLogin || got.KeepLoginAs != "" { + t.Fatalf("new provider: keepLogin %v as %q, want kept on and no account from the other computer", got.KeepLogin, got.KeepLoginAs) + } +}