codex: the takeover's end waits for the chats on magpie the Codex app has open (milianxinqi on X)
milianxinqi on X, the Codex app: "Model provider mismatch. This chat uses
magpie, but your current provider is OpenAI. Switch to magpie to
continue." ("用的好好的,codex 突然就弹这个了"), with nothing disconnected.
Cause. With the ChatGPT account Codex is signed in to held (out of its
allowance and credits), magpie is Codex's provider (the codex.out
takeover, #540) and new chats start on magpie. When the account has room
again (its window's reset, a switch to another account, or the account
the user keeps Codex on, a5e3972a7), Sync ended the takeover at once:
model_provider out of config.toml, Codex back on OpenAI. The Codex app
keeps the app-server the takeover started (codexAppStep leaves it
running unless Restart the Codex app to follow is on, off by default,
#4550), with its chats loaded and each one's writer lock held, so
bringBack could move none of them and codexRetagOpenThreads never ran
(it runs only before magpie ends the app-server). Every send in an open
chat then met the mismatch, with no step of the user's.
Fix. outStays (agent/codex.go): the takeover's end in Sync, both the
out_own way back and #540's, waits while a Codex holds a chat on magpie
open or wrote to one in the last minute (codexHeldOnMagpie:
sessions.CodexThreadsBusyOnBut, T3 Code's own threads left out, as
codexRetagOpenThreads leaves them), and codexThreadsLater asks for the
Sync that ends it once they are let go; bringBack then moves them onto
OpenAI. Meanwhile magpie serves them on the account, which has room, and
the app on the takeover's app-server shows no account either way. Nothing
waits when the app-server is restarted for the change (Restart the Codex
app to follow on, provider.CodexRestartsApp) or the user turned the
takeover off (Always ChatGPT): that restart moves the open chats first.
The [model_providers.magpie] table already stays after a Disconnect, so
an old thread still opens; a deliberate Disconnect restarts the
app-server (byHand) and retags its chats.
Other paths looked at and left: Disconnect, Unwire, Join and Codex's
account switch go through the same bringBack/retag; RestoreOnQuit (off by
default) runs at quit only; the update restart hands the gateway over on
macOS and Linux and skips it. On Windows a restart runs RestoreOnQuit
when it is on: filed separately.
Tests:
- TestCodexTakeoverWaitsForTheOpenChat (new): takeover, a chat on magpie
quiet but its lock held, account back: still magpie's provider, the
chat on magpie, no Sync asked; lock let go: a Sync asked, which ends
the takeover and moves the chat to openai.
- TestCodexTakeoverEndsAtOnceWhenTheAppFollows (new): with the app
restarted for the change, nothing waits.
- TestCodexTakeoverThreadComesBackOnceQuiet (nb5p): now the takeover
stays while the thread is written to, and ends at the later Sync.
Broken by hand: outStays' gate taken out of Sync: the new test and nb5p's
red ("Codex was put on OpenAI under it"), green with it.
Run (mica, mote over its load, at this tree on origin/main 48fffb60c):
- go test -count=1 -timeout 45m ./internal/agent ./internal/sessions
./internal/provider: ok (426s, 1917s, 237s).
- go vet of those on darwin, GOOS=windows, GOOS=linux: ok. gofmt -l clean.
- Rebased onto origin/main a2d402afe (provider and sessions changes
elsewhere): go build ./..., go vet of the three, and the takeover,
thread-move and retag tests of internal/agent and internal/sessions
again: ok.
Not tried: the real Codex app through a real reset (needs an account
held then given room); a full ./... run (mica had another session's full
suite and GUI suite running).
This commit is contained in:
File diff suppressed because one or more lines are too long
+56
-2
@@ -1409,6 +1409,32 @@ func codexIn(at place) *Agent {
|
||||
out := func() bool {
|
||||
return stashShared()[at.key("codex.login")] != "chatgpt" && codexUsedUp() && !codexSignInKept()
|
||||
}
|
||||
// outStays: the takeover's end (the account has room again, at its
|
||||
// reset, or is one the user keeps Codex on) waits while a Codex has a
|
||||
// chat on magpie open, unless the Codex app's app-server is restarted
|
||||
// for it. The app keeps the app-server the takeover started (#4550,
|
||||
// Restart the Codex app to follow off by default) and its chats with
|
||||
// it, each holding its lock, so bringBack can't move them, and with
|
||||
// config.toml back on OpenAI every send in one was refused: "This chat
|
||||
// uses magpie, but your current provider is OpenAI. Switch to magpie
|
||||
// to continue" (milianxinqi on X: "用的好好的,codex 突然就弹这个了").
|
||||
// magpie serves those chats on the account with room meanwhile, and
|
||||
// the app shows no account on that app-server either way. Once the
|
||||
// chats are let go (the app restarted or quit, the chat closed)
|
||||
// codexThreadsLater asks for the Sync that ends it, and bringBack
|
||||
// moves them where Codex is then. The takeover turned off by hand
|
||||
// (Always ChatGPT) restarts the app-server, moving its chats, and
|
||||
// ends at once.
|
||||
outStays := func() bool {
|
||||
if at.spell != nil || stashShared()[at.key("codex.login")] == "chatgpt" || codexAppFollows() {
|
||||
return false
|
||||
}
|
||||
if !codexHeldOnMagpie(dir) {
|
||||
return false
|
||||
}
|
||||
codexThreadsLater(dir, magpieID)
|
||||
return true
|
||||
}
|
||||
dropBase := func() error {
|
||||
forget(at.key("codex.failover"))
|
||||
if !viaBase() {
|
||||
@@ -2198,6 +2224,12 @@ func codexIn(at place) *Agent {
|
||||
}
|
||||
// put back before #540's way back, which would take
|
||||
// codex/<model> for a magpie model picked beside the sign-in
|
||||
// the takeover ends below once the account has room, unless a
|
||||
// Codex still has a chat on magpie open (outStays)
|
||||
ending := asProvider() && stashShared()[at.key("codex.out")] == "1" && !out()
|
||||
if ending && outStays() {
|
||||
ending = false
|
||||
}
|
||||
if own := stashShared()[at.key("codex.out_own")]; own != "" {
|
||||
m := get("model")
|
||||
switch {
|
||||
@@ -2206,7 +2238,7 @@ func codexIn(at place) *Agent {
|
||||
// moved since (another model picked, magpie API, a
|
||||
// disconnect): that stands
|
||||
forget(at.key("codex.out_own"), at.key("codex.out_stand"))
|
||||
case !out():
|
||||
case ending:
|
||||
// back beside the sign-in on the one picked, as Join
|
||||
// leaves it: Codex's own sign-in, magpie's models joined
|
||||
forget(at.key("codex.out_own"), at.key("codex.out"), at.key("codex.out_stand"))
|
||||
@@ -2234,7 +2266,7 @@ func codexIn(at place) *Agent {
|
||||
switch {
|
||||
case viaBase() && !asProvider() && out():
|
||||
return set(m)
|
||||
case asProvider() && stashShared()[at.key("codex.out")] == "1" && !out():
|
||||
case ending:
|
||||
return set(m)
|
||||
}
|
||||
}
|
||||
@@ -3109,6 +3141,28 @@ var codexUsedUp = func() bool {
|
||||
// user keeps it on (provider.CodexSignInKept). A var so tests can say.
|
||||
var codexSignInKept = provider.CodexSignInKept
|
||||
|
||||
// codexAppFollows reports whether magpie restarts the Codex app's
|
||||
// app-server for a change of Codex's route it makes by itself
|
||||
// (provider.CodexRestartsApp), moving the chats the app has open onto the
|
||||
// new provider first. A var so tests can say.
|
||||
var codexAppFollows = provider.CodexRestartsApp
|
||||
|
||||
// codexHeldOnMagpie says whether a Codex holds open, or wrote to this
|
||||
// minute, a thread of the Codex home dir on magpie that Codex resumes on
|
||||
// the provider config.toml names: T3 Code's own (t3CodexRunsIn) stay on
|
||||
// magpie whatever it names, and are left out.
|
||||
func codexHeldOnMagpie(dir string) bool {
|
||||
var skip func(string) bool
|
||||
if user, _ := homes(); t3CodexRunsIn(user, dir) {
|
||||
by, ok := sessions.CodexThreadsBy(dir, magpieID)
|
||||
skip = func(id string) bool {
|
||||
t, found := by[id]
|
||||
return ok && found && t3Started(t.Originator)
|
||||
}
|
||||
}
|
||||
return sessions.CodexThreadsBusyOnBut(dir, magpieID, skip)
|
||||
}
|
||||
|
||||
// codexFailover reports whether Codex is signed in to a ChatGPT account
|
||||
// with more of its accounts on in magpie, behind it, and the user hasn't
|
||||
// asked magpie to only switch its sign-in (provider.SwitchOnly).
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/filelock"
|
||||
)
|
||||
|
||||
// milianxinqi on X, the Codex app: "Model provider mismatch. This chat
|
||||
// uses magpie, but your current provider is OpenAI. Switch to magpie to
|
||||
// continue." ("用的好好的,codex 突然就弹这个了"). The ChatGPT account ran
|
||||
// out, magpie became Codex's provider (the takeover) and the chat was
|
||||
// started on magpie. At the account's reset Sync put Codex back beside
|
||||
// its sign-in, on OpenAI, while the Codex app kept the app-server the
|
||||
// takeover started (Restart the Codex app to follow is off by default)
|
||||
// with the chat loaded and its lock held, so bringBack couldn't move it
|
||||
// and every send in it was refused. The takeover now waits while a Codex
|
||||
// holds a chat on magpie, and ends once the app lets it go.
|
||||
func TestCodexTakeoverWaitsForTheOpenChat(t *testing.T) {
|
||||
const id = "019a0000-0000-7000-8000-0000000000c1"
|
||||
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, "")
|
||||
dir := filepath.Join(home, ".codex")
|
||||
cx := codex(home)
|
||||
if err := cx.Fields[0].Set("fake/m1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
asked := watchCodexThreads(t)
|
||||
t.Cleanup(func() { codexUsedUp = func() bool { return false } })
|
||||
|
||||
codexUsedUp = func() bool { return true }
|
||||
if err := cx.Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg := read(); !strings.Contains(cfg, `model_provider = "magpie"`) {
|
||||
t.Fatalf("used up, magpie not Codex's provider:\n%s", cfg)
|
||||
}
|
||||
// the chat started during the takeover, quiet, that the Codex app has
|
||||
// loaded (codex-rs rollout writer_lock.rs)
|
||||
path := codexThreadOn(t, dir, id, "magpie")
|
||||
old := time.Now().Add(-10 * time.Minute)
|
||||
os.Chtimes(path, old, old)
|
||||
letGo := holdCodexThreadLock(t, dir, id)
|
||||
|
||||
// the reset: the account has room again
|
||||
codexUsedUp = func() bool { return false }
|
||||
if err := cx.Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg := read(); !strings.Contains(cfg, `model_provider = "magpie"`) {
|
||||
t.Fatalf("the account back with a chat on magpie open in the Codex app, Codex was put on OpenAI under it (\"This chat uses magpie, but your current provider is OpenAI\"):\n%s", cfg)
|
||||
}
|
||||
if p := codexRowOn(t, dir, id); p != "magpie" {
|
||||
t.Fatalf("the open chat is on %q, want magpie, the provider Codex is on", p)
|
||||
}
|
||||
select {
|
||||
case <-asked:
|
||||
t.Fatal("a Sync asked for while the app still holds the chat")
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
}
|
||||
|
||||
// the app lets it go: restarted, quit, or the chat closed
|
||||
letGo()
|
||||
select {
|
||||
case <-asked:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("the chat let go, no Sync asked for to end the takeover")
|
||||
}
|
||||
if err := cx.Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg := read(); strings.Contains(cfg, "model_provider =") {
|
||||
t.Fatalf("the chat let go, magpie still Codex's provider:\n%s", cfg)
|
||||
}
|
||||
if p := codexRowOn(t, dir, id); p != "openai" {
|
||||
t.Fatalf("the chat is on %q after the takeover ended, want openai, where Codex resumes it", p)
|
||||
}
|
||||
}
|
||||
|
||||
// With Restart the Codex app to follow on, magpie restarts the app-server
|
||||
// for the takeover's end and moves the chats it has open first
|
||||
// (codexRetagOpenThreads): nothing waits.
|
||||
func TestCodexTakeoverEndsAtOnceWhenTheAppFollows(t *testing.T) {
|
||||
const id = "019a0000-0000-7000-8000-0000000000c2"
|
||||
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, "")
|
||||
dir := filepath.Join(home, ".codex")
|
||||
cx := codex(home)
|
||||
if err := cx.Fields[0].Set("fake/m1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
watchCodexThreads(t)
|
||||
oldFollows := codexAppFollows
|
||||
codexAppFollows = func() bool { return true }
|
||||
t.Cleanup(func() { codexUsedUp = func() bool { return false }; codexAppFollows = oldFollows })
|
||||
|
||||
codexUsedUp = func() bool { return true }
|
||||
if err := cx.Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
path := codexThreadOn(t, dir, id, "magpie")
|
||||
old := time.Now().Add(-10 * time.Minute)
|
||||
os.Chtimes(path, old, old)
|
||||
holdCodexThreadLock(t, dir, id)
|
||||
|
||||
codexUsedUp = func() bool { return false }
|
||||
if err := cx.Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg := read(); strings.Contains(cfg, "model_provider =") {
|
||||
t.Fatalf("the app restarted for the change, the takeover still waited:\n%s", cfg)
|
||||
}
|
||||
}
|
||||
|
||||
// holdCodexThreadLock holds a thread's writer lock as a Codex that has it
|
||||
// loaded does, till the returned func or the test's end.
|
||||
func holdCodexThreadLock(t *testing.T, dir, id string) (letGo func()) {
|
||||
t.Helper()
|
||||
locks := filepath.Join(dir, "thread-writer-locks")
|
||||
os.MkdirAll(locks, 0o755)
|
||||
lf, err := os.OpenFile(filepath.Join(locks, id+".lock"), os.O_CREATE|os.O_RDWR, 0o600)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, err := filelock.TryLock(lf); !got {
|
||||
t.Fatal("lock:", err)
|
||||
}
|
||||
done := false
|
||||
letGo = func() {
|
||||
if !done {
|
||||
done = true
|
||||
filelock.Unlock(lf)
|
||||
lf.Close()
|
||||
}
|
||||
}
|
||||
t.Cleanup(letGo)
|
||||
return letGo
|
||||
}
|
||||
@@ -46,8 +46,9 @@ func watchCodexThreads(t *testing.T) <-chan struct{} {
|
||||
// Codex went back to OpenAI, but the thread, written to that minute, was
|
||||
// left on magpie for a later Sync, and none came: the Codex app couldn't
|
||||
// go on with it on OpenAI, Retry failed the same way every time and its
|
||||
// model read "model unavailable". bringBack asks for that Sync once the
|
||||
// thread is quiet, and it moves the thread where Codex is.
|
||||
// model read "model unavailable". The takeover now stays while the thread
|
||||
// is written to (outStays), a Sync is asked for once it is quiet, and that
|
||||
// one ends the takeover and moves the thread where Codex is.
|
||||
func TestCodexTakeoverThreadComesBackOnceQuiet(t *testing.T) {
|
||||
const id = "019a0000-0000-7000-8000-0000000000b1"
|
||||
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, "")
|
||||
@@ -75,8 +76,8 @@ func TestCodexTakeoverThreadComesBackOnceQuiet(t *testing.T) {
|
||||
if err := cx.Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg := read(); strings.Contains(cfg, "model_provider =") {
|
||||
t.Fatalf("allowance back, magpie still Codex's provider:\n%s", cfg)
|
||||
if cfg := read(); !strings.Contains(cfg, `model_provider = "magpie"`) {
|
||||
t.Fatalf("allowance back with a thread on magpie written to this minute, the takeover ended under it:\n%s", cfg)
|
||||
}
|
||||
if p := codexRowOn(t, dir, id); p != "magpie" {
|
||||
t.Fatalf("a thread being written to was moved: %q", p)
|
||||
@@ -97,6 +98,9 @@ func TestCodexTakeoverThreadComesBackOnceQuiet(t *testing.T) {
|
||||
if err := cx.Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg := read(); strings.Contains(cfg, "model_provider =") {
|
||||
t.Fatalf("the thread quiet, magpie still Codex's provider:\n%s", cfg)
|
||||
}
|
||||
if p := codexRowOn(t, dir, id); p != "openai" {
|
||||
t.Fatalf("thread on %q after the later Sync, want openai, where Codex lists it", p)
|
||||
}
|
||||
|
||||
@@ -221,6 +221,12 @@ func SetRestartApp(id string, on bool) error {
|
||||
return Save(*p)
|
||||
}
|
||||
|
||||
// CodexRestartsApp is codexRestartsApp, for the agent package: a change
|
||||
// of Codex's route magpie makes by itself restarts the Codex app's
|
||||
// app-server (moving the chats it has open first, CodexBeforeAppEnd) only
|
||||
// when it says so.
|
||||
func CodexRestartsApp() bool { return codexRestartsApp() }
|
||||
|
||||
// codexRestartsApp is whether the user had magpie restart the Codex app's
|
||||
// app-server for a change it makes by itself (Provider.RestartApp). While
|
||||
// providers.json can't be read it is unknown, and the app is left be: a
|
||||
|
||||
@@ -623,6 +623,13 @@ type CodexThreadBy struct {
|
||||
// it was written to. A database that can't be read, and a rollout that
|
||||
// can't be found, say nothing.
|
||||
func CodexThreadsBusyOn(home, p string) bool {
|
||||
return CodexThreadsBusyOnBut(home, p, nil)
|
||||
}
|
||||
|
||||
// CodexThreadsBusyOnBut is CodexThreadsBusyOn leaving out the threads
|
||||
// skip names: another client's on p, which stay there whatever Codex's
|
||||
// config says (T3 Code's on magpie).
|
||||
func CodexThreadsBusyOnBut(home, p string, skip func(id string) bool) bool {
|
||||
now := time.Now()
|
||||
locked := codexLockedIDs(home)
|
||||
for _, path := range codexStateDBs(home) {
|
||||
@@ -640,7 +647,7 @@ func CodexThreadsBusyOn(home, p string) bool {
|
||||
if err == nil {
|
||||
for rows.Next() {
|
||||
var id, r string
|
||||
if rows.Scan(&id, &r) != nil {
|
||||
if rows.Scan(&id, &r) != nil || skip != nil && skip(id) {
|
||||
continue
|
||||
}
|
||||
if locked[strings.ToLower(id)] {
|
||||
|
||||
Reference in New Issue
Block a user