provider: Remove takes the account listed, by its slot, not a hidden one of the same name
dev (Discord) couldn't remove a ZCode account. The ZCode app's own
account had been removed in magpie (hidden), and a second one signed in
through magpie had the same name. Two things in the source caused it:
- editSideLogin matched the first saved entry by agent and name, hidden
ones included, so Forget took the hidden "zcode" slot. It signed out
the ZCode app's own account and left the listed zcode#l47q19 one.
- pluginLogins applied the own account's name (pluginOwnUser) after
pluginLabels had added the slot suffix to tell same-name accounts
apart, so both read "dev@z".
Now:
- pluginLabels applies the own names first and adds the suffix after,
so the listed names are unique ("dev@z (l47q19)").
- Forget, Switch and On/Off resolve the name against the accounts as
listed, then edit that saved entry by its slot (Home) or own-ness
(listedMatch/sameSaved). editSideLogin, which the other callers use
(chatgpt_api, copilot_lapsed, google, signin_revoked), prefers a shown
entry over a hidden one of the same name.
- A saved id doesn't help here: it is made from the name, so both
accounts had the same id (66dbee89c5d33fcd).
Existing data: an account whose name collided with another one is shown
with its slot suffix from the next read on. Nothing is rewritten.
Run:
- go test -tags nogui -timeout 45m ./... in a sandbox HOME: ok, every
package, at 7edf5b0a3 plus this change.
- gofmt -l is clean; go vet ./..., GOOS=linux go vet -tags nogui ./...
and GOOS=windows go vet ./... pass; go build for darwin, linux (nogui)
and windows pass.
- Sandbox repro with real binaries built from origin/main and from this
change, a sandbox HOME seeded with dev's layout (the hidden zcode slot
and zcode#l47q19, both dev@z), and the real Bun plugin host, using
`magpie accounts zcode` and `magpie accounts forget zcode <name>`:
- old: listed as "dev@z", and forget left plugin-auth with only
zcode#l47q19 (the wrong one signed out);
- new: listed as "dev@z (l47q19)", and forget left plugin-auth with
only zcode and logins.json with the hidden zcode entry.
Broken by hand: the three new tests in same_name_login_test.go fail on
the old code:
- TestRemoveListedAccountLeavesHiddenNamesake: "the account removed is
still signed in", "the hidden ZCode app's account was signed out in
its place";
- TestPluginOwnNameKeepsSlotApart: "accounts *dev@z (own),dev@z";
- TestForgetSideLoginTakesTheListedOne: "left [own hidden, magpie's]".
Not tried:
- a real ZCode account;
- clicking Remove in the GUI. The GUI sends {agent, user} as before,
and the server now resolves it by slot;
- Copilot and Google. Their lists carry no saved entry, so they get the
prefer-shown fallback only.
This commit is contained in:
@@ -22,13 +22,20 @@ import (
|
||||
// pluginAgent is what logins.json lists a plugin provider's accounts as.
|
||||
func pluginAgent(pp plugin.Provider) string { return "plugin:" + pp.ID }
|
||||
|
||||
// pluginLabels names each account: its id, else the end of its key, else
|
||||
// what it signed in with; a name two share gets the account's slot.
|
||||
func pluginLabels(pp plugin.Provider) map[string]string {
|
||||
// pluginLabels names each account: by named (the agent's own, as the
|
||||
// agent is signed in now), else its id, else the end of its key, else what
|
||||
// it signed in with; a name two share gets the account's slot. The slot
|
||||
// goes on after the own name, so the own account never reads as another
|
||||
// signed in to the same one (dev on Discord: ZCode's, its id "ZCode",
|
||||
// named as one magpie signed in, and Remove on that one took the own).
|
||||
func pluginLabels(pp plugin.Provider, named map[string]string) map[string]string {
|
||||
out := map[string]string{}
|
||||
seen := map[string]int{}
|
||||
for _, a := range pp.Accounts {
|
||||
l := a.AccountID
|
||||
l := named[a.Key]
|
||||
if l == "" {
|
||||
l = a.AccountID
|
||||
}
|
||||
if l == "" && a.Hint != "" {
|
||||
l = "API key …" + a.Hint
|
||||
}
|
||||
@@ -51,6 +58,36 @@ func pluginLabels(pp plugin.Provider) map[string]string {
|
||||
return out
|
||||
}
|
||||
|
||||
// pluginNames is pluginLabels as the accounts list names them, the
|
||||
// agent's own as the agent is signed in now.
|
||||
func pluginNames(pp plugin.Provider) map[string]string {
|
||||
_, named := pluginOwnNames(pp)
|
||||
return pluginLabels(pp, named)
|
||||
}
|
||||
|
||||
// pluginOwnNames tells the provider's accounts that are the agent's own,
|
||||
// and names them as the agent is signed in now, where that can be read.
|
||||
func pluginOwnNames(pp plugin.Provider) (own map[string]bool, named map[string]string) {
|
||||
own, named = map[string]bool{}, map[string]string{}
|
||||
listed := map[string]bool{}
|
||||
for _, a := range pp.Accounts {
|
||||
listed[a.Key] = true
|
||||
}
|
||||
for k, a := range plugin.Auths(pp.ID) {
|
||||
if listed[k] && pluginOwn(pp.ID, a) {
|
||||
own[k] = true
|
||||
}
|
||||
}
|
||||
if len(own) > 0 {
|
||||
if u := pluginOwnUser(pp.ID); u != "" {
|
||||
for k := range own {
|
||||
named[k] = u
|
||||
}
|
||||
}
|
||||
}
|
||||
return own, named
|
||||
}
|
||||
|
||||
type pluginLogin struct {
|
||||
sideLogin
|
||||
acct plugin.Account
|
||||
@@ -125,26 +162,16 @@ func pluginLogins(pp plugin.Provider) []pluginLogin {
|
||||
return nil
|
||||
}
|
||||
agent := pluginAgent(pp)
|
||||
labels := pluginLabels(pp)
|
||||
byKey := map[string]plugin.Account{}
|
||||
for _, a := range pp.Accounts {
|
||||
byKey[a.Key] = a
|
||||
}
|
||||
own := map[string]bool{}
|
||||
for k, a := range plugin.Auths(pp.ID) {
|
||||
if _, ok := byKey[k]; ok && pluginOwn(pp.ID, a) {
|
||||
own[k] = true
|
||||
}
|
||||
}
|
||||
own, named := pluginOwnNames(pp)
|
||||
mark := ""
|
||||
if len(own) > 0 {
|
||||
if u := pluginOwnUser(pp.ID); u != "" {
|
||||
for k := range own {
|
||||
labels[k] = u
|
||||
}
|
||||
}
|
||||
mark = ownMark(pp.ID)
|
||||
}
|
||||
labels := pluginLabels(pp, named)
|
||||
loginsMu.Lock()
|
||||
ls := readLogins()
|
||||
changed := false
|
||||
@@ -252,10 +279,15 @@ func forgetPluginLogin(pp plugin.Provider, user string) error {
|
||||
}
|
||||
}
|
||||
key := ls[i].saved.Home
|
||||
err := editSideLogin(agent, user, func(saved []savedLogin, j int) ([]savedLogin, error) {
|
||||
isNext := listedMatch(agent, next, ls)
|
||||
err := editListedLogin(agent, user, ls, func(saved []savedLogin, j int) ([]savedLogin, error) {
|
||||
n := -1
|
||||
if next != "" {
|
||||
n = pickSaved(saved, isNext)
|
||||
}
|
||||
for k := range saved {
|
||||
if saved[k].Agent == agent && (first || k == j) {
|
||||
saved[k].First = next != "" && strings.EqualFold(saved[k].User, next)
|
||||
saved[k].First = k == n
|
||||
}
|
||||
}
|
||||
if own {
|
||||
@@ -401,7 +433,7 @@ func clearPluginLapse(saved plugin.Saved) {
|
||||
notePluginLapseOf("plugin:"+saved.Provider, saved.Account, http.StatusOK, toldNow, nil)
|
||||
for _, pp := range plugin.Cached() {
|
||||
if pp.ID == saved.Provider {
|
||||
if user := pluginLabels(pp)[saved.Account]; user != "" {
|
||||
if user := pluginNames(pp)[saved.Account]; user != "" {
|
||||
StaleAllowance(pluginAgent(pp), user)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -255,7 +255,7 @@ func pluginUsing(saved plugin.Saved, user string) bool {
|
||||
func pluginUser(saved plugin.Saved) string {
|
||||
for _, pp := range plugin.Cached() {
|
||||
if pp.ID == saved.Provider {
|
||||
return pluginLabels(pp)[saved.Account]
|
||||
return pluginNames(pp)[saved.Account]
|
||||
}
|
||||
}
|
||||
return ""
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/plugin"
|
||||
)
|
||||
|
||||
// Two ZCode accounts of one name, the ZCode app's own removed in magpie
|
||||
// (hidden) and one signed in through magpie beside it: Remove on the one
|
||||
// listed takes that one, by its slot, and leaves the hidden one as it was
|
||||
// (dev on Discord). The own account's saved id is the mover's "ZCode",
|
||||
// named after the app's sign-in now, so the two read alike unless the
|
||||
// slot tells them apart.
|
||||
func TestRemoveListedAccountLeavesHiddenNamesake(t *testing.T) {
|
||||
movedPlugin(t, "zcode", map[string]map[string]any{
|
||||
"zcode": {"type": "oauth", "access": "a", "refresh": `{"source":"zcode"}`, "expires": 0, "accountId": "ZCode"},
|
||||
"zcode#l47q19": {"type": "oauth", "access": "b", "refresh": "r", "expires": 0, "accountId": "dev@z"},
|
||||
})
|
||||
pluginOwnUser = func(id string) string {
|
||||
if id == "zcode" {
|
||||
return "dev@z"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
writeFile(t, loginsPath(), []savedLogin{
|
||||
{Agent: "plugin:zcode", User: "dev@z", Home: "zcode", Hidden: hiddenNoMark},
|
||||
{Agent: "plugin:zcode", User: "dev@z", Home: "zcode#l47q19", On: true},
|
||||
})
|
||||
ls := Logins("zcode")
|
||||
if len(ls) != 1 || ls[0].Own {
|
||||
t.Fatalf("listed %+v, want the one magpie signed in", ls)
|
||||
}
|
||||
if err := ForgetLogin("zcode", ls[0].User); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
auths := plugin.Auths("zcode")
|
||||
if _, ok := auths["zcode#l47q19"]; ok {
|
||||
t.Error("the account removed is still signed in")
|
||||
}
|
||||
if _, ok := auths["zcode"]; !ok {
|
||||
t.Error("the hidden ZCode app's account was signed out in its place")
|
||||
}
|
||||
hidden := false
|
||||
for _, l := range readLogins() {
|
||||
if l.Agent == "plugin:zcode" {
|
||||
if l.Home != "zcode" {
|
||||
t.Errorf("still saved: %+v", l)
|
||||
}
|
||||
hidden = hidden || l.Hidden != ""
|
||||
}
|
||||
}
|
||||
if !hidden {
|
||||
t.Error("the ZCode app's account is no longer kept hidden")
|
||||
}
|
||||
if got := listed("zcode"); got != "" {
|
||||
t.Errorf("listed after the remove: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// Two accounts that share a name and differ only in the slot, the one
|
||||
// listed and one that would read alike: pluginLabels keeps them apart
|
||||
// after the agent's own is named as the agent is signed in.
|
||||
func TestPluginOwnNameKeepsSlotApart(t *testing.T) {
|
||||
movedPlugin(t, "zcode", map[string]map[string]any{
|
||||
"zcode": {"type": "oauth", "access": "a", "refresh": `{"source":"zcode"}`, "expires": 0, "accountId": "ZCode"},
|
||||
"zcode#l47q19": {"type": "oauth", "access": "b", "refresh": "r", "expires": 0, "accountId": "dev@z"},
|
||||
})
|
||||
pluginOwnUser = func(id string) string {
|
||||
if id == "zcode" {
|
||||
return "dev@z"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
if got := listed("zcode"); got != "*dev@z (own),dev@z (l47q19)" {
|
||||
t.Fatalf("accounts %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
// The built-in path: an agent's own account hidden in magpie and one of
|
||||
// magpie's of the same name, the latter listed. Removing it removes it,
|
||||
// not the hidden one.
|
||||
func TestForgetSideLoginTakesTheListedOne(t *testing.T) {
|
||||
claudeHome(t)
|
||||
writeFile(t, loginsPath(), []savedLogin{
|
||||
{Agent: "side", User: "a@x", Hidden: hiddenNoMark},
|
||||
{Agent: "side", User: "a@x", Auth: json.RawMessage(`{"t":1}`), On: true},
|
||||
})
|
||||
usable := func(savedLogin) bool { return true }
|
||||
ls := sideLogins("side", "a@x", usable)
|
||||
if len(ls) != 1 || ls[0].saved.own() {
|
||||
t.Fatalf("listed %+v, want magpie's", ls)
|
||||
}
|
||||
var gone savedLogin
|
||||
if err := forgetSideLogin("side", "a@x", ls, func(l savedLogin) { gone = l }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(gone.Auth) == 0 {
|
||||
t.Errorf("told of %+v going, not magpie's", gone)
|
||||
}
|
||||
var left []savedLogin
|
||||
for _, l := range readLogins() {
|
||||
if l.Agent == "side" {
|
||||
left = append(left, l)
|
||||
}
|
||||
}
|
||||
if len(left) != 1 || !left[0].own() || left[0].Hidden == "" {
|
||||
t.Errorf("left %+v, want the agent's own, hidden", left)
|
||||
}
|
||||
}
|
||||
@@ -160,21 +160,69 @@ func loginsOf(ls []sideLogin) []Login {
|
||||
return out
|
||||
}
|
||||
|
||||
// editSideLogin changes the saved account of user.
|
||||
// editSideLogin changes the saved account of user: one shown before one
|
||||
// hidden of the same name, the agent's own removed in magpie, which only
|
||||
// a slot (Home) tells apart (dev on Discord: Remove on a ZCode account took
|
||||
// the hidden one beside it).
|
||||
func editSideLogin(agent, user string, f func(ls []savedLogin, i int) ([]savedLogin, error)) error {
|
||||
return editSavedLogin(agent, user, func(l savedLogin) bool {
|
||||
return l.Agent == agent && strings.EqualFold(l.User, user)
|
||||
}, f)
|
||||
}
|
||||
|
||||
// editListedLogin changes the account of user in ls, the list the user
|
||||
// picked it from: the very one saved, where ls knows it, not the first of
|
||||
// its name.
|
||||
func editListedLogin(agent, user string, ls []sideLogin, f func(ls []savedLogin, i int) ([]savedLogin, error)) error {
|
||||
return editSavedLogin(agent, user, listedMatch(agent, user, ls), f)
|
||||
}
|
||||
|
||||
func editSavedLogin(agent, user string, match func(savedLogin) bool, f func(ls []savedLogin, i int) ([]savedLogin, error)) error {
|
||||
loginsMu.Lock()
|
||||
defer loginsMu.Unlock()
|
||||
ls := readLogins()
|
||||
for i := range ls {
|
||||
if ls[i].Agent == agent && strings.EqualFold(ls[i].User, user) {
|
||||
ls, err := f(ls, i)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return writeLogins(ls)
|
||||
i := pickSaved(ls, match)
|
||||
if i < 0 {
|
||||
return fmt.Errorf("no %s account %q", agent, user)
|
||||
}
|
||||
ls, err := f(ls, i)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return writeLogins(ls)
|
||||
}
|
||||
|
||||
// pickSaved is the saved account match takes, one shown before one
|
||||
// hidden; -1 for none.
|
||||
func pickSaved(ls []savedLogin, match func(savedLogin) bool) int {
|
||||
i := -1
|
||||
for j := range ls {
|
||||
if match(ls[j]) && (i < 0 || ls[i].Hidden != "" && ls[j].Hidden == "") {
|
||||
i = j
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("no %s account %q", agent, user)
|
||||
return i
|
||||
}
|
||||
|
||||
// listedMatch tells the saved account of user in ls: by its slot and
|
||||
// whether it is the agent's own where ls has what was saved of it (a
|
||||
// plugin's account by its key), else by its name.
|
||||
func listedMatch(agent, user string, ls []sideLogin) func(savedLogin) bool {
|
||||
i := slices.IndexFunc(ls, func(l sideLogin) bool { return strings.EqualFold(l.User, user) })
|
||||
if i < 0 || ls[i].saved.Agent == "" {
|
||||
return func(l savedLogin) bool { return l.Agent == agent && strings.EqualFold(l.User, user) }
|
||||
}
|
||||
want := ls[i].saved
|
||||
return func(l savedLogin) bool { return sameSaved(l, want) }
|
||||
}
|
||||
|
||||
// sameSaved says two saved accounts are one: a plugin's by its key, an
|
||||
// agent's own or one of magpie's with no home of its own by its name.
|
||||
func sameSaved(a, b savedLogin) bool {
|
||||
if a.Agent != b.Agent || a.Home != b.Home || a.own() != b.own() {
|
||||
return false
|
||||
}
|
||||
return a.Home != "" || strings.EqualFold(a.User, b.User)
|
||||
}
|
||||
|
||||
func activeOf(ls []sideLogin) string {
|
||||
@@ -190,10 +238,15 @@ func activeOf(ls []sideLogin) string {
|
||||
// it is: magpie only changes which account its gateway uses first.
|
||||
func switchSideLogin(agent, user string, ls []sideLogin) error {
|
||||
was := activeOf(ls)
|
||||
return editSideLogin(agent, user, func(saved []savedLogin, i int) ([]savedLogin, error) {
|
||||
isWas := listedMatch(agent, was, ls)
|
||||
return editListedLogin(agent, user, ls, func(saved []savedLogin, i int) ([]savedLogin, error) {
|
||||
w := -1
|
||||
if was != "" {
|
||||
w = pickSaved(saved, isWas)
|
||||
}
|
||||
for j := range saved {
|
||||
if saved[j].Agent == agent {
|
||||
if j != i && strings.EqualFold(saved[j].User, was) {
|
||||
if j != i && j == w {
|
||||
saved[j].On = true // the one it replaces is next in line
|
||||
}
|
||||
saved[j].First = j == i
|
||||
@@ -207,7 +260,7 @@ func setSideLoginOn(agent, user string, on bool, ls []sideLogin) error {
|
||||
if !on && strings.EqualFold(activeOf(ls), user) {
|
||||
return fmt.Errorf("magpie uses %s first; put another account first to stop using it", user)
|
||||
}
|
||||
return editSideLogin(agent, user, func(saved []savedLogin, i int) ([]savedLogin, error) {
|
||||
return editListedLogin(agent, user, ls, func(saved []savedLogin, i int) ([]savedLogin, error) {
|
||||
saved[i].On = on
|
||||
return saved, nil
|
||||
})
|
||||
@@ -238,10 +291,15 @@ func forgetSideLogin(agent, user string, ls []sideLogin, gone func(savedLogin))
|
||||
mark = firstNonEmpty(ownMark(agent), hiddenNoMark)
|
||||
}
|
||||
var old savedLogin
|
||||
err := editSideLogin(agent, user, func(saved []savedLogin, i int) ([]savedLogin, error) {
|
||||
isNext := listedMatch(agent, next, ls)
|
||||
err := editListedLogin(agent, user, ls, func(saved []savedLogin, i int) ([]savedLogin, error) {
|
||||
n := -1
|
||||
if next != "" {
|
||||
n = pickSaved(saved, isNext)
|
||||
}
|
||||
for j := range saved {
|
||||
if next != "" && j != i && saved[j].Agent == agent {
|
||||
saved[j].First = strings.EqualFold(saved[j].User, next)
|
||||
saved[j].First = j == n
|
||||
saved[j].On = saved[j].On || saved[j].First
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user