Files
magpie/internal/provider/kiro_accounts.go
T
yetone 615732af65 provider: Kiro's built-in is retired; its plugin serves it
"kiro" joins retiredBuiltins (migrate.go, now one entry a line), so
PluginServes("kiro") holds moved or not, and the built-in's code goes,
as Zed's did in c0605abc2.

Removed:
- gateway/kiro.go (the CodeWhisperer translator) and its tests, and
  gateway.go's Account.Agent == "kiro" dispatch to serveKiro;
- provider/kiro_signin.go and its test: StartSignIn's "kiro" case, the
  AWS Location redirect in the pasted-address handler, kiroFlow and its
  callback branches in signin.go;
- the built-in account (kiroAccount, kiroProvider, kiroAcct), its model
  list (kiroModels, kiroModelList, KiroModel), its usage card
  (kiroQuotaAt, kiroLimits.windows, kiroLoginQuota, the fetch in
  subscription_usage.go), and its login-list, switch, on/off, forget and
  also-on cases (logins.go, logins_on.go, login_usage.go, account.go);
- "kiro" from Native (provider.go) and asksOwnBackend (modelprefs.go).

Kept, because the mover, the plugin paths or other built-ins need them:
- kiro.go's sign-in readers (readKiroAt, readKiroCLI, readKiroIDE,
  kiroCred), KiroAuthOf with its refresh and profile lookup, and
  kiroUsageLimits: kiroOwnUser (plugin_accounts.go) and addKiroLogin
  (the legacy-file adoption kiroLogins runs) ask Kiro whose account it
  is through them;
- kiro_accounts.go's homes and kiroLogins, plus kiroSaved/readKiroFile
  (moved there from kiro_signin.go), which migrate_kiro.go reads and
  writes; side_logins.go and cards_kept.go's Kiro key are untouched;
- the Save branch that keeps a key on "kiro": the mover takes it;
- newUUID and joinNonEmpty moved from gateway/kiro.go to gateway/uuid.go
  (Devin and others use them), and the said() test helper to
  gateway/said_test.go;
- kiro_accounts.go's PLUGIN-SERVED notice (TestMovedBuiltinsSayTheirPlugin).

Tests changed in meaning:
- TestKiroMover: retired, the agent's own sign-in (kiro-cli's or the
  IDE's) is the plugin's to read (movedAgent, since cfa262509), so it no
  longer goes in the move; the test now says so, and the own marker's
  way back is checked with a literal marker.
- TestKiroBuilderIDListsItsModels became
  TestKiroBuilderIDTakesItsServiceProfile: the model list is gone, the
  Builder ID profile KiroAuthOf finds (for whose-account) is still
  checked; TestKiroBuilderIDFoundByKirosAnswer checks the profile, not
  the models.
- Dropped as built-in only: TestKiroModelList, TestKiroAccount,
  TestKiroLimits' windows half, TestSaveKeepsKirosKey's account check,
  TestKiroHideOwnSignIn, TestKiroHideOnlySignIn, TestCodexToolImageKiro,
  the Kiro case of the required-tools test, the kiro sign-in test file,
  and "kiro" from TestLoopbackSignInsTakePastedAddress.
- TestSetUpstreamNameRefusesAnAccountBackend set up a Kiro account as
  its account backend; Kiro is no longer one, so it now signs Claude in
  (still in asksOwnBackend) and gives "claude/*", checking the refusal
  is the own-account one. Found by a later wider run (the regexp above
  didn't reach it); go test -run '^TestSetUpstreamName' ok 6s.
- kiro_test.go's reader tests call readKiroAt(key, "") where they
  called readKiro(key) (same result with no magpie-signed accounts).

Run at this tree (load ~300, so narrow runs only):
- gofmt -l internal: clean (after formatting said_test.go)
- go vet -tags nogui ./... ; GOOS=linux and GOOS=windows the same: ok
- go test -tags nogui -count=1 -run 'Kiro|Migrat|Mov|Retired|SignIn|
  AsksOwnBackend|Plugin|HandOver|NoWayBack|Adopt|Hide|RequiredTools|
  ToolImage' ./internal/provider ./internal/gateway ./internal/cli
  ./internal/gui: gateway ok 266s, cli ok 24s, gui ok 25s; provider
  failed TestKiroMover (the own sign-in, above), fixed, then
  provider ok 145s (go vet ./internal/provider ok first)
Not run: the whole packages, the full suite, the GUI node suite.
2026-10-17 23:11:00 +08:00

167 lines
5.5 KiB
Go

package provider
// PLUGIN-SERVED (see AGENTS.md): Kiro ("kiro") is a deprecated built-in
// subscription served by its plugin, @magpie-community/opencode-kiro-auth,
// once moved onto it (provider.Moved; the default for a new sign-in). A
// moved one's sign-ins, models, requests and usage are all the plugin's,
// never this code's (only the move, in migrate*.go, still reads its
// accounts). A fix here alone doesn't reach those users; fix the plugin
// (github.com/magpie-community/plugins, packages/kiro) and raise the mover's
// min in internal/provider/migrate_kiro.go.
// Several Kiro subscriptions. kiro-cli and the Kiro IDE keep one account,
// which magpie only ever reads. Each account magpie signs in gets a home of
// its own holding its kiro-auth-token.json, refreshed there, so each refresh
// token has one holder. logins.json names those homes; Kiro's own account is
// remembered there too, with no home, so it can stand behind another or be
// turned off.
import (
"encoding/json"
"errors"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
// kiroTokenFile is what an account's home holds, in the IDE's shape.
const kiroTokenFile = "kiro-auth-token.json"
// kiroAccountsDir holds the homes of the Kiro accounts magpie signed in.
func kiroAccountsDir() string { return filepath.Join(filepath.Dir(Path()), "kiro") }
// kiroLegacyPath is where magpie kept the one Kiro account it signed in
// before it kept several (v0.1.374).
func kiroLegacyPath() string { return filepath.Join(filepath.Dir(Path()), kiroTokenFile) }
// kiroLogin is a Kiro account and the home it is signed in in, "" for
// kiro-cli's or the IDE's.
type kiroLogin struct {
Login
Home string
}
// kiroOwnUser is who kiro-cli's or the IDE's sign-in is, "" for none; until
// Kiro has said, it is named for what it is.
func kiroOwnUser() string {
if _, ok := readKiroAt("", ""); !ok {
return ""
}
user, _ := kiroIdentity("", "")
return firstNonEmpty(user, "Kiro account")
}
// kiroLogins lists the Kiro accounts that are signed in, the first in use
// first, then the rest as they were added.
func kiroLogins() []kiroLogin {
kiroAdoptLegacy()
var out []kiroLogin
for _, l := range sideLogins("kiro", kiroOwnUser(), func(l savedLogin) bool {
_, ok := readKiroAt("", l.Home)
return l.Home != "" && ok
}) {
out = append(out, kiroLogin{l.Login, l.saved.Home})
}
return out
}
// kiroAdoptLegacy moves the account magpie kept alone into a home of its
// own, among the rest, once Kiro says whose it is; until it can, the file
// stays where it was and is tried again a few minutes on.
func kiroAdoptLegacy() {
kiroAdopt.Lock()
defer kiroAdopt.Unlock()
old := kiroLegacyPath()
if time.Since(kiroAdopt.tried) < 5*time.Minute {
return
}
if _, err := os.Stat(old); err != nil {
return
}
kiroAdopt.tried = time.Now()
home, err := newKiroHome()
if err != nil {
return
}
file := filepath.Join(home, kiroTokenFile)
if err := os.Rename(old, file); err != nil {
removeKiroHome(home)
return
}
if _, err := addKiroLogin(home); err != nil {
_ = os.Rename(file, old)
removeKiroHome(home)
}
}
var kiroAdopt struct {
sync.Mutex
tried time.Time
}
// newKiroHome makes the home a further Kiro account is signed in in.
func newKiroHome() (string, error) {
home := filepath.Join(kiroAccountsDir(), strings.ToLower(strings.NewReplacer("-", "", "_", "").Replace(randomToken(9))))
return home, os.MkdirAll(home, 0o700)
}
// removeKiroHome deletes a home magpie made, and nothing else.
func removeKiroHome(home string) {
if rel, err := filepath.Rel(kiroAccountsDir(), home); err == nil && rel != "." && !strings.HasPrefix(rel, "..") {
_ = os.RemoveAll(home)
}
}
// addKiroLogin keeps the account just signed in in home, in use beside the
// others, once Kiro says whose it is. Signed in again, an account keeps
// the newer home.
func addKiroLogin(home string) (string, error) {
user, plan := askKiroIdentity("", home)
if user == "" {
return "", errors.New("signed in, but Kiro didn't say whose account it is; try again")
}
kiroSaidNow("", home, user, plan)
return user, addSideLogin(savedLogin{Agent: "kiro", User: user, Plan: plan, Home: home}, kiroOwnUser(),
func(l savedLogin) { removeKiroHome(l.Home) })
}
// kiroSaved is a Kiro sign-in of magpie's as it is kept: the IDE's shape, with
// an AWS sign-in's client beside it.
type kiroSaved struct {
AccessToken string `json:"accessToken"`
RefreshToken string `json:"refreshToken"`
ExpiresAt string `json:"expiresAt"`
AuthMethod string `json:"authMethod"` // social | IdC
Provider string `json:"provider"` // Google, Github, BuilderId, Enterprise
Region string `json:"region,omitempty"`
ProfileArn string `json:"profileArn,omitempty"`
ClientID string `json:"clientId,omitempty"`
ClientSecret string `json:"clientSecret,omitempty"`
}
// readKiroFile reads a sign-in made in magpie.
func readKiroFile(path string) (kiroCred, bool) {
b, err := readAccountFile(path)
if err != nil {
return kiroCred{}, false
}
var t kiroSaved
if json.Unmarshal(b, &t) != nil || t.AccessToken == "" {
return kiroCred{}, false
}
c := kiroCred{access: t.AccessToken, refresh: t.RefreshToken, region: t.Region, profile: t.ProfileArn,
clientID: t.ClientID, clientSecret: t.ClientSecret, idePath: path, builderID: strings.EqualFold(t.Provider, "BuilderId")}
c.expires, _ = time.Parse(time.RFC3339Nano, t.ExpiresAt)
if c.region == "" {
c.region = "us-east-1"
}
if strings.EqualFold(t.AuthMethod, "IdC") && c.clientID != "" {
c.method = "idc"
} else {
c.method, c.social = "social", true
}
return c, true
}