Files
magpie/internal/provider/codex_homes_removed.go
T
yetone e484b1e936 provider: an account Codex holds in another of its folders can be removed in magpie (charliepu)
charliepu on Discord (Windows), after v0.1.2385: the account that read no
allowance couldn't be removed. Remove answered "<email> is signed in to
Codex in C:\Users\charl\.codex-db-backup-20260803: sign it out there
(CODEX_HOME=... codex logout) to remove it". That folder is a backup no
Codex runs in. appdir.CodexHomes finds it by name (".codex*" holding an
auth.json), and rememberLogins read its account back at every look, so
forgetLogin refused rather than have it come back.

Now Remove forgets the account and sets that folder's sign-in aside in a
record of magpie's own (codex-homes-removed.json: the folder to a sha256
of its refresh token, or of its access token when it has none).
codexHomeLogins passes over a folder while it holds that sign-in. The
folder's files are left as they are: nothing in it is written, moved or
deleted. A sign-in made
in that folder again (Codex run there, a new token) isn't named by the
record, and is listed again with no step of the user's. With the record
unreadable, which sign-ins were removed is unknown, and no other folder's
account is read that time (accounts saved already stay listed).

Not done: treating ".codex-db-backup-*" folders as not Codex homes by
name. They are found by the same rule as ~/.codex-work, and their
sessions/ are still read for Sessions and usage as before; a name rule
would guess at what a user's folder is for. Removing is the user's own
answer, and works for any such folder.

Run at this head (origin/main 70d50254c plus this change; the doc line
added after):
- mote: go test -timeout 45m -count=1 ./internal/provider ./internal/appdir: ok (provider 142s;
  135s at the earlier base e90a635a9)
- mote: GOOS=windows and GOOS=linux go vet ./internal/provider: ok
- xiaomi-gpu (Windows 11, win-run): go test ./internal/provider -run
  'TestCodexHeldFolderAccountRemoved|TestCodexOtherHome|TestCodexHeldInOtherFolder':
  all four pass
- gofmt -l internal/provider: empty

Broken by hand, each red:
- logins.go as on main: TestCodexHeldFolderAccountRemoved fails with the
  "codex logout) to remove it" refusal, and so does
  TestCodexOtherHomeAccountReadsQuota (its last check pinned the refusal;
  it now expects the removal to go through).
- codexHomeLogins ignoring the record: "removed, then read back from the
  backup folder".
- an unreadable record read as empty: "with the record unreadable, the
  removed account came back".

docs/subsystems/providers-accounts.md's "Codex's other folders" row
said such an account couldn't be removed; it now says what Remove and
v0.1.2385's Sign in again do.

Not tried: the real app on Windows with a real backup folder (the test
runs on Windows with a sandbox HOME); a real account. No GUI change: the
row's Remove posts login/forget as before, which now goes through.
2026-10-31 19:42:52 +08:00

96 lines
2.8 KiB
Go

package provider
// An account Codex is signed in to in another of its folders (codexHomeLogins)
// was read again from there at every look, so Remove refused it and said to
// run `CODEX_HOME=… codex logout` (charliepu on Discord, Windows: the
// folder was C:\Users\…\.codex-db-backup-20260803, a backup no Codex runs
// in). Removed now, the folder's sign-in is set aside here, magpie's own
// record: that folder is listed no more while it holds that sign-in. Its
// files are left as they are. A sign-in made there again is another token,
// which nothing here names: it is listed again with no step of the user's.
import (
"encoding/json"
"errors"
"io/fs"
"os"
"path/filepath"
"sync"
"github.com/yetone/magpie/internal/lastgood"
)
// codexAsideMu guards the record's file.
var codexAsideMu sync.Mutex
// codexAsidePath is the record: a Codex folder to the hash of the sign-in
// in it that the user removed from magpie (codexAsideKey).
func codexAsidePath() string {
return filepath.Join(filepath.Dir(Path()), "codex-homes-removed.json")
}
// codexAsideKey names the sign-in auth holds without keeping it: its
// refresh token, or its access token when it has none. "" for none.
func codexAsideKey(auth []byte) string {
var a codexAuth
if json.Unmarshal(auth, &a) != nil {
return ""
}
if a.Tokens.RefreshToken != "" {
return codexGoneHash(a.Tokens.RefreshToken)
}
if a.Tokens.AccessToken != "" {
return codexGoneHash(a.Tokens.AccessToken)
}
return ""
}
// readCodexAside reads the record. None yet is empty; one that can't be
// read or doesn't parse is an error: what it sets aside is unknown.
func readCodexAside() (map[string]string, error) {
m := map[string]string{}
b, err := os.ReadFile(codexAsidePath())
if errors.Is(err, fs.ErrNotExist) {
return m, nil
}
if err != nil {
return nil, err
}
if err := json.Unmarshal(b, &m); err != nil {
return nil, err
}
return m, nil
}
// setCodexAside records that the sign-in auth in the Codex folder dir was
// removed from magpie.
func setCodexAside(dir string, auth []byte) error {
key := codexAsideKey(auth)
if key == "" {
return nil
}
codexAsideMu.Lock()
defer codexAsideMu.Unlock()
m, err := readCodexAside()
if err != nil {
// a record that doesn't parse is kept aside, then begun again
m = map[string]string{}
}
m[filepath.Clean(dir)] = key
out, err := json.MarshalIndent(m, "", " ")
if err == nil {
err = lastgood.KeepUnparsed(codexAsidePath(), lastgood.Into[map[string]string])
}
if err == nil {
err = writePrivate(codexAsidePath(), append(out, '\n'))
}
return err
}
// codexSetAside says whether l, read from its folder l.Home, is the sign-in
// the user removed from magpie there, by the record m (readCodexAside).
func codexSetAside(l savedLogin, m map[string]string) bool {
key := codexAsideKey(l.Auth)
return key != "" && m[filepath.Clean(l.Home)] == key
}