Files
magpie/internal/provider/plugin_checkin.go
T
yetone 8c5b9f928d provider, agent, gui: subscription accounts can be turned off, and then no agent's sign-in is read (#1535, MaxHaiCom)
What changed: Settings → Privacy → Subscription accounts (On/Off),
`magpie accounts discovery [on|off]` and MAGPIE_NO_ACCOUNTS=1 turn off
account discovery. It stays on by default. While it is off, magpie reads
none of the agents' sign-ins: ~/.claude/.credentials.json, .claude.json's
account, ~/.codex/auth.json, Copilot's, Cursor's, Devin's, Kiro's,
Gemini's, ZCode's, Command Code's and OpenCode's files. It runs no
`security` / `secret-tool` for the Keychain or Secret Service, and asks no
agent CLI who is signed in. It serves no subscription. API-key providers,
middleware plugins and Pi extensions work as before.

Why: the reporter runs magpie on machines where reading other tools'
credentials isn't wanted. The owner delegated the call.

How: settings.NoAccountDiscovery (kept per computer by KeepOwn) and
settings.AccountDiscoveryOff(). The variable only ever turns discovery
off. The gates sit at every reader (readAccountFile, discoveryOff) and at
every route that reads or writes a sign-in, so a route that skips
Accounts still reads nothing:
- Accounts, All, Logins, usage, renewal, warm-up, session identities,
  check-ins;
- switching, forgetting, SetLoginOn, StartSignIn, the imports;
- moves onto plugins.
The ones that write refuse with an error that names how to turn it back
on. On the agent side, Agent.Source and OpenCode's auth.json are gated,
and Codex wiring goes by magpie's stash instead of auth.json, so a
toggle never rewires Codex. logins.json and plugin-auth.json are left
untouched and serve again when discovery is turned back on. A change
applies at once (ForgetAccounts).

Docs: providers-accounts.md, README (en, zh-CN), and the site's CLI table
(en, zh, ja).

Run:
- TestAccountDiscoveryOffReadsNoSignIn (provider) and
  TestAccountDiscoveryOffAgentsReadNoSignIn (agent). Every credential
  path is a FIFO that records each open, and security, secret-tool,
  claude, codex, cursor-agent, devin, kiro-cli, copilot and grok are fakes
  on PATH. Both fail with the gate removed: they show the opens and
  `security find-generic-password -s Claude Code-credentials`, among
  others. Both pass with the gate in place.
- TestAccountDiscoverySetting and TestAccountsDiscoveryCmd.
- account-discovery.test.cjs: chromium and webkit, 900 and 360 wide,
  en/zh/zh-TW/ja/de, 32/32. The neighbouring privacy, redact,
  settings-sections and i18n-keys GUI tests: 49/49.
- The full `go test -tags nogui ./...`, gofmt -l, and go vet for darwin,
  linux and windows on the touched packages.

Not tried: a real machine under fs_usage, and Linux and Windows at
runtime. The FIFO tests are !windows.
2026-10-11 06:38:40 +08:00

220 lines
6.8 KiB
Go

package provider
// A plugin's own daily check-in (Lemon on Discord: provider 每日签到的方法能
// 放到插件里面去做吗): a plugin whose auth hook has checkin presses its
// vendor's 签到 itself, and magpie runs it as it runs its own check-ins
// (checkin.go): each account in use once a Beijing day while the user has
// it on, a failure tried again later that day, what came of it on the
// account's Usage card and in Settings. A plugin that checks in takes over
// from magpie's own check-in through its fetch (WorkBuddy, Trae CN,
// MiniMax Code, Qoder), so an account is never checked in twice.
//
// What came of it is kept in plugin-checkin.json by provider and account.
import (
"context"
"path/filepath"
"sort"
"sync"
"time"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/settings"
)
// pluginCheckinBy is the WorkBuddyCheckin.By of a plugin's check-in.
func pluginCheckinBy(id string) string { return "plugin:" + id }
func pluginCheckinPath() string { return filepath.Join(filepath.Dir(Path()), "plugin-checkin.json") }
var pluginCheckinMu sync.Mutex
// pluginCheckinAcct is a plugin's account checked in.
type pluginCheckinAcct struct {
User string
On bool
key string // provider|account
card string
do func(context.Context) WorkBuddyCheckin
}
// pluginCheckinProvider is a plugin's provider that checks in, and its
// accounts.
type pluginCheckinProvider struct {
ID, Name string
accounts []pluginCheckinAcct
}
// pluginCheckinProviders are the plugins' providers whose plugin checks
// in, those signed in to.
var pluginCheckinProviders = func() []pluginCheckinProvider {
var out []pluginCheckinProvider
for _, pp := range heldPlugins() {
if !pp.Checkin || movingNow(pp.ID) {
continue
}
p := pluginCheckinProvider{ID: pp.ID, Name: pp.Name}
card := PluginID(pp.ID)
for _, l := range pluginLogins(pp) {
acct := l.acct.Key
who := l.acct.AccountID
if who == "" {
who = acct
}
id := pp.ID
p.accounts = append(p.accounts, pluginCheckinAcct{User: l.User, On: l.On, key: id + "|" + who, card: card,
do: func(ctx context.Context) WorkBuddyCheckin {
c, err := plugin.AccountCheckin(ctx, id, acct)
if err != nil {
return wbCheckinFailed(err)
}
return WorkBuddyCheckin{Outcome: c.Outcome, Credit: c.Credit, Streak: c.Streak, Msg: c.Message}
}})
}
if len(p.accounts) > 0 {
out = append(out, p)
}
}
return out
}
// pluginChecksIn says whether pp's plugin presses the check-in itself:
// magpie's own check-in through its fetch leaves its accounts to it.
func pluginChecksIn(pp plugin.Provider) bool { return pp.Checkin }
// PluginCheckinOn says whether a plugin provider's accounts are checked in
// each day: as the user set it for the provider, else as the built-in
// check-in of its vendor is set, which the plugin took over.
func PluginCheckinOn(s settings.Settings, id string) bool {
if on, ok := s.PluginCheckins[id]; ok {
return on
}
switch id {
case wbCN.id:
return s.WorkBuddyCheckin
case TraeCNID:
return s.TraeCheckin
case MiniMaxCodeID, MiniMaxCodeGlobalID:
return s.MiniMaxCheckin
case "qoder", QoderCNID:
return s.QoderCheckin
}
return false
}
func (p pluginCheckinProvider) checkiner(now func() time.Time) checkiner {
return checkiner{path: pluginCheckinPath(), now: now, mu: &pluginCheckinMu, by: pluginCheckinBy(p.ID), label: p.ID, accounts: func() []checkinAcct {
var out []checkinAcct
for _, a := range p.accounts {
out = append(out, checkinAcct{User: a.User, On: a.On, key: a.key, do: a.do})
}
return out
}}
}
// pluginCheckiner checks in the plugins' accounts: of those whose
// provider only says, all of them when only is nil.
type pluginCheckiner struct {
now func() time.Time
only func(id string) bool
}
func (c pluginCheckiner) clock() time.Time { return c.now() }
func (c pluginCheckiner) checkinNow(ctx context.Context, soon bool) []WorkBuddyCheckin {
if discoveryOff() {
return nil
}
var out []WorkBuddyCheckin
for _, p := range pluginCheckinProviders() {
if c.only != nil && !c.only(p.ID) {
continue
}
for _, r := range p.checkiner(c.now).checkinNow(ctx, soon) {
r.Vendor = p.Name
out = append(out, r)
}
}
return out
}
// CheckInPlugins checks the accounts of each plugin that checks in in for
// today now, those not in yet; with ids, only those providers'. It says
// how each stands.
func CheckInPlugins(ctx context.Context, ids ...string) []WorkBuddyCheckin {
c := pluginCheckiner{now: time.Now}
if len(ids) > 0 {
c.only = func(id string) bool {
for _, x := range ids {
if x == id {
return true
}
}
return false
}
}
return c.checkinNow(ctx, true)
}
// PluginCheckin is a plugin's provider that checks in, for Settings: its
// switch and how each account's last check-in went.
type PluginCheckin struct {
ID string `json:"id"`
Name string `json:"name"`
On bool `json:"on"`
Checkins []WorkBuddyCheckin `json:"checkins,omitempty"`
}
// PluginCheckins are the plugins' providers that check in, each with its
// accounts' last check-ins as kept; asks nothing.
func PluginCheckins() []PluginCheckin {
st := readCheckins(pluginCheckinPath())
s := settings.Load()
var out []PluginCheckin
for _, p := range pluginCheckinProviders() {
pc := PluginCheckin{ID: p.ID, Name: p.Name, On: PluginCheckinOn(s, p.ID)}
listed := map[string]bool{}
for _, a := range p.accounts {
if r, ok := st[a.key]; ok && a.On && !listed[a.key] {
listed[a.key] = true
r.User, r.By, r.Vendor = a.User, pluginCheckinBy(p.ID), p.Name
pc.Checkins = append(pc.Checkins, r)
}
}
sort.SliceStable(pc.Checkins, func(i, j int) bool { return pc.Checkins[i].User < pc.Checkins[j].User })
out = append(out, pc)
}
return out
}
// HasPluginCheckin says whether a plugin that checks in is signed in.
func HasPluginCheckin() bool { return len(pluginCheckinProviders()) > 0 }
// pluginCheckinMarks is qs with the cards of the plugins' accounts that
// check in marked, as WithCheckins marks the built-ins'.
func pluginCheckinMarks(qs []SubscriptionQuota) []SubscriptionQuota {
ps := pluginCheckinProviders()
if len(ps) == 0 {
return qs
}
st := readCheckins(pluginCheckinPath())
for _, p := range ps {
var on []checkinCard
for _, a := range p.accounts {
if a.On {
on = append(on, checkinCard{User: a.User, card: a.card, key: a.key})
}
}
qs = markCheckins(qs, on, st, pluginCheckinBy(p.ID))
}
return qs
}
// KeepPluginsCheckedIn checks the plugins' accounts in each day, each
// provider's while settings say to, as KeepWorkBuddyCheckedIn does
// WorkBuddy's.
func KeepPluginsCheckedIn(ctx context.Context) {
c := pluginCheckiner{now: time.Now, only: func(id string) bool { return PluginCheckinOn(settings.Load(), id) }}
keepCheckedIn(ctx, "plugin", c, func() bool { return true })
}