Files
magpie/internal/provider/forget_account.go
T
yetone 0f1666b387 provider: a built-in can be retired, its plugin serving it moved or not, and its unmoved accounts get a Move line
The owner (10-16) wants every deprecated built-in taken out of magpie,
one at a time. This is the ground for that; it retires nothing yet.

- retiredBuiltins / BuiltinRetired(id) in migrate.go name a built-in
  whose code is gone. Only an id with a mover can be retired.
- PluginServes(id) is Moved(id) or retired. Every gate that asked
  Moved(id) asks it now: sign-in, Factory keys, usage, logins, cards,
  forget, accounts, check-ins, plugin sign-in, the CLI's accounts and
  providers, the GUI's account label.
- A retired row's move.state is plugin, and its id is in onPlugins
  once the plugin is installed.
- /api/providers lists a retired subscription's built-in accounts that
  haven't moved in `unmoved` (MoveCandidate, now with the failed move's
  error and why). The GUI shows a quiet line over the Providers list
  for each, with Move / Try again (unmovedLine), in en, zh, zh-TW, ja
  and de.
- AGENTS.md and docs/subsystems/provider-plugins.md say what a retired
  built-in is.

Run at 270aa1c9c with this change:
- go test -tags nogui ./... -count=1 -timeout 90m: 66 packages ok.
- gofmt -l internal: nothing. GOOS=linux and GOOS=windows
  go vet -tags nogui ./...: clean.
- GUI, both engines, en and zh: retired-move (new, 4/4), plugin-move,
  deprecation-notice, deprecated-subscriptions, plugin-move-unserved,
  plugin-movable-review, i18n-keys: 39 pass.
- TestRetiredBuiltinIsThePlugins -count=20 (it picks a mover at random).

The Move line is the Providers list's own, not a page that could be a
plugin. It took the core's scripts to 1,427,218 bytes, which the budget
main raised in 81313b912 (1,447,000, issue 2612) already holds;
TestCoreScriptBudget passes.

Broken by hand: retired-move goes red with unmovedLine not appended;
TestRetiredBuiltinIsThePlugins goes red with BuiltinRetired's Movable
check dropped.
2026-10-17 23:10:59 +08:00

127 lines
3.6 KiB
Go

package provider
import (
"context"
"errors"
"fmt"
"slices"
"strings"
"time"
"github.com/yetone/magpie/internal/plugin"
)
// ForgetAccount signs a subscription's accounts out of magpie for good, the
// way past a removal (#694: a removed Qoder came back with its old account
// each time it was added back, and nothing took it away). A plugin's
// sign-ins leave plugin-auth.json (the plugin host's signOut when one runs,
// else under plugin-auth.json.lock) and logins.json, and a moved built-in's
// backup of them goes too, so moving back doesn't bring them either; a
// built-in's saved accounts go as each account's Remove takes them. An
// agent's own sign-in, which magpie only reads, is hidden as Remove hides
// it. Signing in again starts afresh.
func ForgetAccount(id string) error {
p, ok := find(Accounts(), strings.ToLower(strings.TrimSpace(id)))
if !ok || p.Account == nil {
return fmt.Errorf("no signed-in account %q", id)
}
if p.IsPlugin() {
pp, ok := PluginOf(p.ID)
if !ok {
pp = *p.Account.plugin
}
return forgetPluginAccounts(pp)
}
return forgetLogins(p.Account.Agent)
}
// forgetLogins forgets each of a built-in's accounts, the one in use first
// last, as the accounts list removes them one by one.
func forgetLogins(agent string) error {
ls := Logins(agent)
slices.SortStableFunc(ls, func(a, b Login) int {
switch {
case a.Active == b.Active:
return 0
case a.Active:
return 1
}
return -1
})
var errs []error
for _, l := range ls {
if err := ForgetLogin(agent, l.User); err != nil {
errs = append(errs, err)
}
}
ForgetAccounts()
return errors.Join(errs...)
}
// forgetPluginAccounts signs every account of the plugin's provider out:
// the ones magpie signed in to through it from plugin-auth.json, and the
// agent's own (a CLI's sign-in the plugin reads) hidden.
func forgetPluginAccounts(pp plugin.Provider) error {
agent := pluginAgent(pp)
var errs []error
keys, users := map[string]bool{}, map[string]bool{}
var owners []string
for _, l := range pluginLogins(pp) {
if l.Own {
owners = append(owners, l.User)
continue
}
keys[l.acct.Key], users[strings.ToLower(l.User)] = true, true
}
own := len(owners) > 0
// the agent's own sign-in first, while the list still has every
// account: hidden, as each account's Remove hides it
for _, u := range owners {
if err := forgetPluginLogin(pp, u); err != nil {
errs = append(errs, err)
}
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if !own {
// every sign-in kept under the provider's id, listed or not
if err := plugin.SignOut(ctx, pp.ID, ""); err != nil {
errs = append(errs, err)
}
} else {
for k := range keys {
if err := plugin.SignOut(ctx, pp.ID, k); err != nil {
errs = append(errs, err)
}
}
}
loginsMu.Lock()
ls := readLogins()
n := len(ls)
ls = slices.DeleteFunc(ls, func(l savedLogin) bool {
return l.Agent == agent && (!own || keys[l.Home]) && !l.own()
})
if len(ls) != n {
if err := writeLogins(ls); err != nil {
errs = append(errs, err)
}
}
loginsMu.Unlock()
if PluginServes(pp.ID) {
// moving back would bring the accounts forgotten back from the
// backup the move set aside
mv := movers[pp.ID]
err := setMigration(pp.ID, func(m *Migration) {
m.Accounts = slices.DeleteFunc(m.Accounts, func(a movedAccount) bool { return !a.Own && (!own || keys[a.Key]) })
m.Backup = slices.DeleteFunc(m.Backup, func(b savedLogin) bool {
return mv != nil && slices.Contains(mv.agents, b.Agent) && !b.own() && (!own || users[strings.ToLower(b.User)])
})
})
if err != nil {
errs = append(errs, err)
}
}
ForgetAccounts()
return errors.Join(errs...)
}