codex: OpenAI's threads follow Codex onto magpie from a provider of the user's, and a move the budget stopped asks for the next sync (Lice9735 on X)
Lice9735 on X, Windows, magpie 0.1.3233, Codex app 26.1104: an old chat (10-13) answered "模型提供商不匹配 — 此聊天使用的是 OpenAI,但你当前 的提供商是 magpie。请切换至 OpenAI 以继续", only a new chat worked, afterd620ff6b5andd2c29abe7. Their config.toml has a provider table of their own, openai-http. Cause. The banner is the app-server's modelProviderMismatch on thread/resume (app 26.1103: providerBlock is set only from that error; codex 0.167 load_for_resume compares the stored thread's provider, the row for a paginated thread, the rollout's session_meta for a legacy one, with config.toml's). With Codex on a provider of the user's at the takeover, the stash's codex.provider is that provider, and bringOver moved only its threads: OpenAI's stayed on openai by design ("whose threads bringBack would hand them to"). The Codex app lists them and refuses each one on magpie. A second gap on the same path: the rewriting moves (MovePackedCodexThreads, MoveRecentCodexThreads) go 128 MB a sync, newest first, and the rest waited for a sync nothing asked for, which can be hours away. - bringOver, with Codex on a provider of the user's before magpie (not OpenAI, magpie or cc-switch-official, and no profile naming OpenAI), moves OpenAI's threads to magpie too: in place, a paginated one by its row, the last 30 days rewritten. They are recorded in codex.lent with that provider as To, as lendOwn records its own: the Disconnect gives every magpie thread to that provider (bringBack), where Codex resumes them, and giveLent gives these back to OpenAI once Codex is on OpenAI again. One the app holds open waits (codexThreadsLater on openai). - MovePackedCodexThreads and MoveRecentCodexThreads say more when the budget stopped them with a thread left, and bringOver, bringBeside and lendOwn ask for the next sync at once (codexThreadsAgain). The three share one mover now (codexOnto); bringBeside also waits on a held thread as the other two do. packedBudget is a var, for the test. - Tests: TestCodexOpenAIThreadsFollowMagpieFromOwnProvider (new: an openai-http table named "OpenAI", a plain and a packed OpenAI thread, moved, lent, to openai-http at the Disconnect, back to OpenAI after; none while a profile names openai), TestCodexThreadsLeftByTheBudgetAskAgain (new), TestMovePackedCodexThreads checks more. TestCodexThreadsFollowMagpieIn's two user-provider subtests now expect OpenAI's thread on magpie, then on that provider after the Disconnect. - docs/subsystems/agent-wiring.md says so. Run: - Real codex-cli 0.166.0 on mote, sandbox HOME and CODEX_HOME, a fake Responses server: a thread started on the built-in openai (API key, openai_base_url), a second after switching to model_provider openai-http (name "OpenAI"); both paginated. On openai-http the first was refused ("This chat uses openai, but your current provider is openai-http"). A throwaway test (not committed) ran magpie's takeover (Set fake/m1, Sync) on a copy of that home: with origin/main's code the openai row stayed and `codex exec resume` on magpie (the copy's base URLs pointed at the fake) failed with exactly "This chat uses openai, but your current provider is magpie" (modelProviderMismatch); with this change the row was magpie, codex.lent {"to":"openai-http",...}, and the resume answered. - New and changed tests red on origin/main191f3c0af(the openai thread on "openai", want "magpie"; after the Disconnect "openai", want the user's provider). - Broken by hand: bringOver's `if more` dropped -> TestCodexThreadsLeftByTheBudgetAskAgain red ("0 sync(s) asked for"); `more = true` turned false -> TestMovePackedCodexThreads red. - mote: gofmt -l clean; GOOS=windows go vet and GOOS=linux go vet -tags nogui of internal/agent and internal/sessions ok; go test ./internal/sessions ./internal/agent ok at this head (this change on e6cdeac9c; rebased after onto5dbdfc1bd, which changes internal/provider only), agent 292.2s, sessions 47.4s. Pushed and tagged without waiting for the full `go test -timeout 45m ./...`, per the owner's rule to ship each verified fix at once; it runs on mote after another session's suite, and a red it shows is filed or fixed in a follow-up. - win-run (xiaomi-gpu), go test ./internal/sessions ./internal/agent (at the commit's parent before a rebase ontoabe7b0063, which touches neither package): every test this change adds or touches passes; 29 reds, 28 of them red the same at origin/main191f3c0afrun alone there. The 29th, TestHermesProfilesFollowMagpieModel, passes alone 3/3 on this tree and is in #4115. Not tried: the Codex app itself (the CLI's app-server gives the same error the app shows); Windows with a real Codex; a legacy (non-paginated) thread with a real Codex, covered by the unit tests only; a history over 128 MB with real files. The chat the app has loaded at the switch goes on once the app lets it go: fully quit the Codex app and reopen it after magpie updates.
This commit is contained in:
File diff suppressed because one or more lines are too long
+113
-108
@@ -810,6 +810,18 @@ func codexIn(at place) *Agent {
|
||||
codexThreadsLater(dir, magpieID)
|
||||
}
|
||||
}
|
||||
// lent are the threads lendOwn moved off OpenAI, and the provider of
|
||||
// the user's they went to; or bringOver, onto magpie, from Codex on
|
||||
// that provider before
|
||||
type lentMove struct {
|
||||
To string `json:"to"`
|
||||
IDs []string `json:"ids"`
|
||||
}
|
||||
lent := func() lentMove {
|
||||
var l lentMove
|
||||
json.Unmarshal([]byte(stashShared()[at.key("codex.lent")]), &l)
|
||||
return l
|
||||
}
|
||||
// bringOver is bringBack the other way: while magpie is Codex's
|
||||
// provider, the threads Codex started on OpenAI before are listed
|
||||
// where Codex is now. Codex's resume picker and the app's thread list
|
||||
@@ -821,12 +833,11 @@ func codexIn(at place) *Agent {
|
||||
// their last turn, stay on OpenAI, as a rewrite of every one would
|
||||
// copy the whole history. bringBack moves them back as Codex leaves
|
||||
// magpie. Only the threads of the provider Codex was on before magpie
|
||||
// (stash codex.provider, OpenAI when none was set): those of any other
|
||||
// provider stay, and so do OpenAI's when Codex was on another one,
|
||||
// whose threads bringBack would hand them to. None while a profile is
|
||||
// chosen, whose provider is the one Codex lists, none of a provider a
|
||||
// profile names, which the user picks to go on with them there, and
|
||||
// none from a distro's Codex.
|
||||
// (stash codex.provider, OpenAI when none was set), and OpenAI's when
|
||||
// that was another one (below): those of any other provider stay.
|
||||
// None while a profile is chosen, whose provider is the one Codex
|
||||
// lists, none of a provider a profile names, which the user picks to
|
||||
// go on with them there, and none from a distro's Codex.
|
||||
//
|
||||
// The provider Codex was on before can be one of the user's own, a
|
||||
// relay's table or CC Switch's "custom". Its threads stayed on it at
|
||||
@@ -847,7 +858,9 @@ func codexIn(at place) *Agent {
|
||||
// turn was in the last 30 days move too, rewritten with a copy kept
|
||||
// as bringBack's are (MovePackedCodexThreads), newest first and
|
||||
// packedBudget bytes a sync: the rest of a big history follows over
|
||||
// the next syncs, and older ones from Sessions (ResumeNow moves one
|
||||
// the next syncs, each asked for at once while some are left (one
|
||||
// left for a sync hours away was refused all that while, Lice9735 on
|
||||
// X), and older ones from Sessions (ResumeNow moves one
|
||||
// as it is reopened).
|
||||
//
|
||||
// A paginated thread (Codex 0.166 on) goes by its row in Codex's state
|
||||
@@ -870,48 +883,52 @@ func codexIn(at place) *Agent {
|
||||
if from == magpieID || from == ccSwitchOfficial {
|
||||
return
|
||||
}
|
||||
if from != "openai" {
|
||||
names, _ := edit.TOMLTables(path)
|
||||
if profileNamed(names)[from] {
|
||||
return
|
||||
}
|
||||
}
|
||||
if len(from) == len(magpieID) {
|
||||
moved, err := sessions.RelabelCodexThreads(dir, from, magpieID)
|
||||
if len(moved) > 0 {
|
||||
log.Printf("codex: %d thread(s) started on %s moved to magpie, where Codex lists them now", len(moved), from)
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("codex: moving %s's threads to magpie: %v", from, err)
|
||||
}
|
||||
}
|
||||
// every paginated thread by its row, of any age: Codex resumes
|
||||
// one on the provider its row names (Parry_Moody and Cyrus on
|
||||
// Discord, whose old OpenAI threads the file moves below left)
|
||||
retagged, err := sessions.RetagPagedCodexThreads(dir, from, magpieID)
|
||||
if len(retagged) > 0 {
|
||||
log.Printf("codex: %d paginated thread(s) started on %s moved to magpie, where Codex resumes them now", len(retagged), from)
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("codex: moving %s's paginated threads to magpie: %v", from, err)
|
||||
}
|
||||
move := sessions.MovePackedCodexThreads
|
||||
if len(from) != len(magpieID) {
|
||||
move = sessions.MoveRecentCodexThreads
|
||||
}
|
||||
packed, err := move(dir, from, magpieID, time.Now().Add(-packedSince), packedBudget)
|
||||
if len(packed) > 0 {
|
||||
log.Printf("codex: %d thread(s) started on %s rewritten onto magpie, where Codex resumes them now", len(packed), from)
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("codex: moving %s's threads to magpie: %v", from, err)
|
||||
names, _ := edit.TOMLTables(path)
|
||||
if from != "openai" && profileNamed(names)[from] {
|
||||
return
|
||||
}
|
||||
_, more := codexOnto(dir, from, magpieID)
|
||||
// one the Codex app has open (the chat on screen as magpie became
|
||||
// the provider) or wrote to this minute stayed on from: it moves
|
||||
// once it is let go (crypto0xfish on Discord)
|
||||
if sessions.CodexThreadsBusyOn(dir, from) {
|
||||
codexThreadsLater(dir, from)
|
||||
}
|
||||
// Codex on a provider of the user's before magpie (openai-http, a
|
||||
// relay's table, CC Switch's custom) still has the threads it
|
||||
// started on OpenAI before that one: the Codex app lists them and
|
||||
// refuses each on magpie as it did on that provider ("此聊天使用的
|
||||
// 是 OpenAI,但你当前的提供商是 magpie", Lice9735 on X). They
|
||||
// come to magpie too, kept as lendOwn keeps those it lends that
|
||||
// provider (codex.lent): bringBack gives every magpie thread to
|
||||
// the provider Codex goes back to, and giveLent gives these back
|
||||
// to OpenAI once Codex is on it again. None while a profile names
|
||||
// OpenAI, whose threads the user goes on with there.
|
||||
if from != "openai" && !profileNamed(names)["openai"] {
|
||||
ids, m := codexOnto(dir, "openai", magpieID)
|
||||
more = more || m
|
||||
if len(ids) > 0 {
|
||||
l := lent()
|
||||
if l.To != from {
|
||||
l = lentMove{To: from}
|
||||
}
|
||||
for _, id := range ids {
|
||||
if !slices.Contains(l.IDs, id) {
|
||||
l.IDs = append(l.IDs, id)
|
||||
}
|
||||
}
|
||||
out, _ := json.Marshal(l)
|
||||
stash(map[string]string{at.key("codex.lent"): string(out)})
|
||||
}
|
||||
if sessions.CodexThreadsBusyOn(dir, "openai") {
|
||||
codexThreadsLater(dir, "openai")
|
||||
}
|
||||
}
|
||||
// the budget left threads for the next sync, which may be hours
|
||||
// away: ask for it now
|
||||
if more {
|
||||
codexThreadsAgain()
|
||||
}
|
||||
}
|
||||
// mirrorsTaken are the mirrors takeMirrors pointed at magpie (#1796)
|
||||
mirrorsTaken := func() []string {
|
||||
@@ -1027,34 +1044,16 @@ func codexIn(at place) *Agent {
|
||||
if profileNamed(names)[from] {
|
||||
return
|
||||
}
|
||||
var ids []string
|
||||
if len(from) == len("openai") {
|
||||
moved, err := sessions.RelabelCodexThreads(dir, from, "openai")
|
||||
ids = append(ids, moved...)
|
||||
if err != nil {
|
||||
log.Printf("codex: moving %s's threads to OpenAI: %v", from, err)
|
||||
}
|
||||
ids, more := codexOnto(dir, from, "openai")
|
||||
if sessions.CodexThreadsBusyOn(dir, from) {
|
||||
codexThreadsLater(dir, from)
|
||||
}
|
||||
retagged, err := sessions.RetagPagedCodexThreads(dir, from, "openai")
|
||||
ids = append(ids, retagged...)
|
||||
if err != nil {
|
||||
log.Printf("codex: moving %s's paginated threads to OpenAI: %v", from, err)
|
||||
}
|
||||
move := sessions.MovePackedCodexThreads
|
||||
if len(from) != len("openai") {
|
||||
move = sessions.MoveRecentCodexThreads
|
||||
}
|
||||
packed, err := move(dir, from, "openai", time.Now().Add(-packedSince), packedBudget)
|
||||
for _, m := range packed {
|
||||
ids = append(ids, m.ID)
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("codex: moving %s's threads to OpenAI: %v", from, err)
|
||||
if more {
|
||||
codexThreadsAgain()
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
return
|
||||
}
|
||||
log.Printf("codex: %d thread(s) started on %s moved to OpenAI, where Codex resumes them beside its sign-in", len(ids), from)
|
||||
b := brought()
|
||||
if b.From != from {
|
||||
b = besideMove{From: from}
|
||||
@@ -1387,17 +1386,6 @@ func codexIn(at place) *Agent {
|
||||
// writes nothing of its own back (paynezhuang on Discord: the base URL
|
||||
// was back after every update)
|
||||
disconnected := func() bool { return stashShared()[at.key("codex"+disconnectedKey)] == "1" }
|
||||
// lent are the threads lendOwn moved off OpenAI, and the provider of
|
||||
// the user's they went to
|
||||
type lentMove struct {
|
||||
To string `json:"to"`
|
||||
IDs []string `json:"ids"`
|
||||
}
|
||||
lent := func() lentMove {
|
||||
var l lentMove
|
||||
json.Unmarshal([]byte(stashShared()[at.key("codex.lent")]), &l)
|
||||
return l
|
||||
}
|
||||
// lendOwn is bringBeside the other way: Codex on a provider of the
|
||||
// user's at the top (CC Switch's "custom", left there when the user
|
||||
// came to magpie, which keeps it since #4347), the threads it started
|
||||
@@ -1430,37 +1418,16 @@ func codexIn(at place) *Agent {
|
||||
if profileNamed(names)["openai"] {
|
||||
return
|
||||
}
|
||||
var ids []string
|
||||
if len(to) == len("openai") {
|
||||
moved, err := sessions.RelabelCodexThreads(dir, "openai", to)
|
||||
ids = append(ids, moved...)
|
||||
if err != nil {
|
||||
log.Printf("codex: moving OpenAI's threads to %s: %v", to, err)
|
||||
}
|
||||
}
|
||||
retagged, err := sessions.RetagPagedCodexThreads(dir, "openai", to)
|
||||
ids = append(ids, retagged...)
|
||||
if err != nil {
|
||||
log.Printf("codex: moving OpenAI's paginated threads to %s: %v", to, err)
|
||||
}
|
||||
move := sessions.MovePackedCodexThreads
|
||||
if len(to) != len("openai") {
|
||||
move = sessions.MoveRecentCodexThreads
|
||||
}
|
||||
packed, err := move(dir, "openai", to, time.Now().Add(-packedSince), packedBudget)
|
||||
for _, m := range packed {
|
||||
ids = append(ids, m.ID)
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("codex: moving OpenAI's threads to %s: %v", to, err)
|
||||
}
|
||||
ids, more := codexOnto(dir, "openai", to)
|
||||
if sessions.CodexThreadsBusyOn(dir, "openai") {
|
||||
codexThreadsLater(dir, "openai")
|
||||
}
|
||||
if more {
|
||||
codexThreadsAgain()
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
return
|
||||
}
|
||||
log.Printf("codex: %d thread(s) started on OpenAI moved to %s, the provider Codex is on, where it resumes them", len(ids), to)
|
||||
l := lent()
|
||||
if l.To != to {
|
||||
l = lentMove{To: to}
|
||||
@@ -2489,13 +2456,13 @@ func codexIn(at place) *Agent {
|
||||
// official ChatGPT account through its local proxy.
|
||||
const ccSwitchOfficial = "cc-switch-official"
|
||||
|
||||
// packedSince and packedBudget bound bringOver's move of the threads
|
||||
// Codex has packed: those with a turn in the last 30 days, 128 MB of
|
||||
// them a sync.
|
||||
const (
|
||||
packedSince = 30 * 24 * time.Hour
|
||||
packedBudget = 128 << 20
|
||||
)
|
||||
// packedSince and packedBudget bound codexOnto's move of the threads it
|
||||
// rewrites: those with a turn in the last 30 days, 128 MB of them a call,
|
||||
// another asked for at once while some are left. packedBudget is a var
|
||||
// for a test.
|
||||
const packedSince = 30 * 24 * time.Hour
|
||||
|
||||
var packedBudget int64 = 128 << 20
|
||||
|
||||
// ccSwitchProvider matches the ids of the provider tables CC Switch writes
|
||||
// into Codex's config: "custom", and "cc-switch", "cc-switch-2"… from its
|
||||
@@ -2842,6 +2809,44 @@ func codexStaleCache(path, tag string) error {
|
||||
return edit.WriteAtomic(path, out)
|
||||
}
|
||||
|
||||
// codexOnto moves the threads of the Codex home dir on the provider from
|
||||
// to the provider to, where Codex lists and resumes them: in place when
|
||||
// the ids are the same length (RelabelCodexThreads), a paginated one by
|
||||
// its row at any age (RetagPagedCodexThreads), and the rest whose last
|
||||
// turn is in the last packedSince rewritten with a copy kept, newest
|
||||
// first and packedBudget bytes a call. It says the ids it moved, and more
|
||||
// when the budget left some for another call. bringOver, bringBeside and
|
||||
// lendOwn move with it.
|
||||
func codexOnto(dir, from, to string) (ids []string, more bool) {
|
||||
if len(from) == len(to) {
|
||||
moved, err := sessions.RelabelCodexThreads(dir, from, to)
|
||||
ids = append(ids, moved...)
|
||||
if err != nil {
|
||||
log.Printf("codex: moving %s's threads to %s: %v", from, to, err)
|
||||
}
|
||||
}
|
||||
retagged, err := sessions.RetagPagedCodexThreads(dir, from, to)
|
||||
ids = append(ids, retagged...)
|
||||
if err != nil {
|
||||
log.Printf("codex: moving %s's paginated threads to %s: %v", from, to, err)
|
||||
}
|
||||
move := sessions.MovePackedCodexThreads
|
||||
if len(from) != len(to) {
|
||||
move = sessions.MoveRecentCodexThreads
|
||||
}
|
||||
packed, more, err := move(dir, from, to, time.Now().Add(-packedSince), packedBudget)
|
||||
for _, m := range packed {
|
||||
ids = append(ids, m.ID)
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("codex: moving %s's threads to %s: %v", from, to, err)
|
||||
}
|
||||
if len(ids) > 0 {
|
||||
log.Printf("codex: %d thread(s) started on %s moved to %s, where Codex resumes them", len(ids), from, to)
|
||||
}
|
||||
return ids, more
|
||||
}
|
||||
|
||||
// codexThreadsAgain is the Sync codexThreadsLater asks for; a test
|
||||
// replaces it. Set in init: a var's initializer naming SyncCatalogSoon
|
||||
// would be an initialization cycle through the agents' list.
|
||||
|
||||
@@ -0,0 +1,170 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/edit"
|
||||
)
|
||||
|
||||
// Lice9735 on X, the Codex app on Windows: a chat from 25 days before
|
||||
// answered "模型提供商不匹配 — 此聊天使用的是 OpenAI,但你当前的提供商
|
||||
// 是 magpie。请切换至 OpenAI 以继续", and only a new chat worked. Codex
|
||||
// had been on a provider table of the user's own (openai-http) before
|
||||
// magpie became its provider, so bringOver moved that provider's threads
|
||||
// to magpie and left OpenAI's, which the Codex app lists and refuses to
|
||||
// go on with on magpie. Now OpenAI's come too, recorded as lendOwn's are
|
||||
// (codex.lent, to the user's provider): the Disconnect gives them to that
|
||||
// provider, where Codex resumes them, and Codex on OpenAI again has them
|
||||
// back on OpenAI.
|
||||
func TestCodexOpenAIThreadsFollowMagpieFromOwnProvider(t *testing.T) {
|
||||
const (
|
||||
plain = "019a0000-0000-7000-8000-0000000000f1"
|
||||
packed = "019a0000-0000-7000-8000-0000000000f2"
|
||||
theirs = "019a0000-0000-7000-8000-0000000000f3"
|
||||
)
|
||||
table := "[model_providers.openai-http]\nname = \"OpenAI\"\nbase_url = \"https://api.openai.com/v1\"\nwire_api = \"responses\"\nservice_tier = \"flex\"\n"
|
||||
t.Run("moved and back", func(t *testing.T) {
|
||||
home, read := codexHome(t, "", "model = \"gpt-5.4\"\nmodel_provider = \"openai-http\"\n\n"+table)
|
||||
dir := filepath.Join(home, ".codex")
|
||||
cfgPath := filepath.Join(dir, "config.toml")
|
||||
plainPath := codexThreadOn(t, dir, plain, "openai")
|
||||
packedPath := codexThreadOn(t, dir, packed, "openai")
|
||||
packThread(t, packedPath)
|
||||
codexThreadOn(t, dir, theirs, "openai-http")
|
||||
cx := codex(home)
|
||||
if err := cx.Fields[0].Set("fake/m1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if p, _ := edit.GetTOMLTop(cfgPath, "model_provider"); p != "magpie" {
|
||||
t.Fatalf("provider %q, want magpie:\n%s", p, read())
|
||||
}
|
||||
for id, want := range map[string]string{plain: "magpie", packed: "magpie", theirs: "magpie"} {
|
||||
if p := codexRowOn(t, dir, id); p != want {
|
||||
t.Errorf("connected: thread %s on %q, want %q", id, p, want)
|
||||
}
|
||||
}
|
||||
if b, _ := os.ReadFile(plainPath); !strings.Contains(string(b), `"model_provider":"magpie"`) {
|
||||
t.Fatalf("rollout still on OpenAI, which the Codex app reads for a legacy thread:\n%s", b)
|
||||
}
|
||||
if p := zstMetaProvider(t, packedPath+".zst"); p != "magpie" {
|
||||
t.Fatalf("packed rollout on %q, want magpie", p)
|
||||
}
|
||||
var l struct {
|
||||
To string `json:"to"`
|
||||
IDs []string `json:"ids"`
|
||||
}
|
||||
json.Unmarshal([]byte(stashLoad()[here(home).key("codex.lent")]), &l)
|
||||
if l.To != "openai-http" || !slices.Contains(l.IDs, plain) || !slices.Contains(l.IDs, packed) || slices.Contains(l.IDs, theirs) {
|
||||
t.Fatalf("codex.lent %+v, want OpenAI's two lent to openai-http", l)
|
||||
}
|
||||
// a sync leaves them so
|
||||
if err := cx.Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if p := codexRowOn(t, dir, plain); p != "magpie" {
|
||||
t.Fatalf("after a sync: %q", p)
|
||||
}
|
||||
// the Disconnect puts openai-http back, and every thread on magpie
|
||||
// goes there, where Codex resumes it
|
||||
if err := cx.Unwire(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if p, _ := edit.GetTOMLTop(cfgPath, "model_provider"); p != "openai-http" {
|
||||
t.Fatalf("provider %q after the Disconnect, want openai-http back:\n%s", p, read())
|
||||
}
|
||||
for id, want := range map[string]string{plain: "openai-http", packed: "openai-http", theirs: "openai-http"} {
|
||||
if p := codexRowOn(t, dir, id); p != want {
|
||||
t.Errorf("disconnected: thread %s on %q, want %q", id, p, want)
|
||||
}
|
||||
}
|
||||
// on OpenAI again: OpenAI's go back to it, openai-http's stay
|
||||
if err := edit.DelTOMLTop(cfgPath, "model_provider"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := cx.Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for id, want := range map[string]string{plain: "openai", packed: "openai", theirs: "openai-http"} {
|
||||
if p := codexRowOn(t, dir, id); p != want {
|
||||
t.Errorf("back on OpenAI: thread %s on %q, want %q", id, p, want)
|
||||
}
|
||||
}
|
||||
if _, ok := stashLoad()[here(home).key("codex.lent")]; ok {
|
||||
t.Error("record left after the threads went back")
|
||||
}
|
||||
})
|
||||
// a profile naming OpenAI is the user's way to go on with its threads
|
||||
t.Run("a profile names OpenAI", func(t *testing.T) {
|
||||
home, _ := codexHome(t, "", "model = \"gpt-5.4\"\nmodel_provider = \"openai-http\"\n\n"+table+"\n[profiles.official]\nmodel_provider = \"openai\"\n")
|
||||
dir := filepath.Join(home, ".codex")
|
||||
codexThreadOn(t, dir, plain, "openai")
|
||||
codexThreadOn(t, dir, theirs, "openai-http")
|
||||
cx := codex(home)
|
||||
if err := cx.Fields[0].Set("fake/m1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cx.Sync()
|
||||
for id, want := range map[string]string{plain: "openai", theirs: "magpie"} {
|
||||
if p := codexRowOn(t, dir, id); p != want {
|
||||
t.Errorf("thread %s on %q, want %q", id, p, want)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// The threads a move rewrites go packedBudget bytes a sync, and the rest
|
||||
// waited for the next one, which may be hours away: the Codex app refused
|
||||
// each one left all that while. A move the budget stopped asks for the
|
||||
// next sync at once.
|
||||
func TestCodexThreadsLeftByTheBudgetAskAgain(t *testing.T) {
|
||||
const (
|
||||
a = "019a0000-0000-7000-8000-0000000000f4"
|
||||
b = "019a0000-0000-7000-8000-0000000000f5"
|
||||
)
|
||||
home, _ := codexHome(t, "", "model = \"gpt-5.4\"\n")
|
||||
dir := filepath.Join(home, ".codex")
|
||||
for _, id := range []string{a, b} {
|
||||
packThread(t, codexThreadOn(t, dir, id, "openai"))
|
||||
}
|
||||
asked := 0
|
||||
oldAgain, oldBudget := codexThreadsAgain, packedBudget
|
||||
codexThreadsAgain = func() { asked++ }
|
||||
packedBudget = 1
|
||||
t.Cleanup(func() { codexThreadsAgain, packedBudget = oldAgain, oldBudget })
|
||||
cx := codex(home)
|
||||
if err := cx.Fields[0].Set("fake/m1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
on := func() (n int) {
|
||||
for _, id := range []string{a, b} {
|
||||
if codexRowOn(t, dir, id) == "magpie" {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
if n := on(); n != 1 {
|
||||
t.Fatalf("%d thread(s) moved at the switch, want one of two at a 1-byte budget", n)
|
||||
}
|
||||
if asked != 1 {
|
||||
t.Fatalf("%d sync(s) asked for with a thread left, want 1", asked)
|
||||
}
|
||||
// the sync asked for moves the other, and asks for none past it
|
||||
if err := cx.Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n := on(); n != 2 {
|
||||
t.Fatalf("%d thread(s) on magpie after the next sync, want both", n)
|
||||
}
|
||||
if err := cx.Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if asked != 1 {
|
||||
t.Fatalf("%d syncs asked for, want none once nothing is left", asked)
|
||||
}
|
||||
}
|
||||
@@ -24,8 +24,9 @@ import (
|
||||
// Now the threads Codex started on OpenAI move to magpie while it is
|
||||
// Codex's provider, in place (the id is one length), the file's time
|
||||
// kept, and back at the Disconnect, with no copy of each. Another
|
||||
// provider's threads stay, and OpenAI's stay when Codex was on another
|
||||
// provider before magpie, whose threads move instead (hxy920830 on X).
|
||||
// provider's threads stay. Codex on another provider before magpie, its
|
||||
// threads move too (hxy920830 on X), and OpenAI's go to that provider at
|
||||
// the Disconnect (Lice9735 on X, TestCodexOpenAIThreadsFollowMagpieFromOwnProvider).
|
||||
//
|
||||
// Threads Codex has packed (.zst) stayed on OpenAI, which Codex 0.166's
|
||||
// app refuses to resume on magpie ("This chat uses openai, but your
|
||||
@@ -103,8 +104,8 @@ func TestCodexThreadsFollowMagpieIn(t *testing.T) {
|
||||
// and refused to open them ("This chat uses custom, but your current
|
||||
// provider is magpie": "原来的会话可以看到,但是加载不出来模型了").
|
||||
// The threads of the provider Codex was on before magpie move as
|
||||
// OpenAI's do, and back to it at the Disconnect; OpenAI's stay, as
|
||||
// bringBack would give them to that provider.
|
||||
// OpenAI's do, and back to it at the Disconnect; OpenAI's with them,
|
||||
// to that provider at the Disconnect, where Codex resumes them.
|
||||
t.Run("on a provider of the user's before", func(t *testing.T) {
|
||||
cfg := "model = \"m\"\nmodel_provider = \"custom\"\n\n[model_providers.custom]\nname = \"custom\"\nbase_url = \"https://relay.example/v1\"\nwire_api = \"responses\"\n"
|
||||
home, _ := codexHome(t, "", cfg)
|
||||
@@ -119,7 +120,7 @@ func TestCodexThreadsFollowMagpieIn(t *testing.T) {
|
||||
t.Fatalf("provider %q, want magpie", p)
|
||||
}
|
||||
cx.Sync()
|
||||
for id, want := range map[string]string{plain: "openai", theirs: "magpie"} {
|
||||
for id, want := range map[string]string{plain: "magpie", theirs: "magpie"} {
|
||||
if p := codexRowOn(t, dir, id); p != want {
|
||||
t.Errorf("connected: thread %s on %q, want %q", id, p, want)
|
||||
}
|
||||
@@ -134,7 +135,7 @@ func TestCodexThreadsFollowMagpieIn(t *testing.T) {
|
||||
if p, _ := edit.GetTOMLTop(filepath.Join(dir, "config.toml"), "model_provider"); p != "custom" {
|
||||
t.Fatalf("provider %q after the Disconnect, want custom back", p)
|
||||
}
|
||||
for id, want := range map[string]string{plain: "openai", theirs: "custom"} {
|
||||
for id, want := range map[string]string{plain: "custom", theirs: "custom"} {
|
||||
if p := codexRowOn(t, dir, id); p != want {
|
||||
t.Errorf("disconnected: thread %s on %q, want %q", id, p, want)
|
||||
}
|
||||
@@ -167,7 +168,7 @@ func TestCodexThreadsFollowMagpieIn(t *testing.T) {
|
||||
if err := cx.Fields[0].Set("fake/m1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for id, want := range map[string]string{plain: "openai", theirs: "magpie", packed: "magpie", old: "myrelay"} {
|
||||
for id, want := range map[string]string{plain: "magpie", theirs: "magpie", packed: "magpie", old: "myrelay"} {
|
||||
if p := codexRowOn(t, dir, id); p != want {
|
||||
t.Errorf("connected: thread %s on %q, want %q", id, p, want)
|
||||
}
|
||||
@@ -184,7 +185,7 @@ func TestCodexThreadsFollowMagpieIn(t *testing.T) {
|
||||
if err := cx.Unwire(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for id, want := range map[string]string{plain: "openai", theirs: "myrelay", packed: "myrelay", old: "myrelay"} {
|
||||
for id, want := range map[string]string{plain: "myrelay", theirs: "myrelay", packed: "myrelay", old: "myrelay"} {
|
||||
if p := codexRowOn(t, dir, id); p != want {
|
||||
t.Errorf("disconnected: thread %s on %q, want %q", id, p, want)
|
||||
}
|
||||
|
||||
@@ -61,20 +61,24 @@ func TestMovePackedCodexThreads(t *testing.T) {
|
||||
d.Close()
|
||||
since := now.Add(-30 * 24 * time.Hour)
|
||||
|
||||
// a budget the first thread's file reaches: that one alone
|
||||
moved, err := MovePackedCodexThreads(home, "openai", "magpie", since, 1)
|
||||
// a budget the first thread's file reaches: that one alone, and it
|
||||
// says one is left, for its caller to come back for it
|
||||
moved, more, err := MovePackedCodexThreads(home, "openai", "magpie", since, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(moved) != 1 || moved[0].ID != newest || moved[0].Backup == "" {
|
||||
t.Fatalf("moved %+v, want the newest alone", moved)
|
||||
}
|
||||
if !more {
|
||||
t.Fatal("stopped by the budget with a thread left, and said none was")
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(moved[0].Backup, "move.json")); err != nil {
|
||||
t.Fatalf("no copy kept: %v", err)
|
||||
}
|
||||
// the next call goes on with the rest
|
||||
if moved, err = MovePackedCodexThreads(home, "openai", "magpie", since, 64<<20); err != nil || len(moved) != 1 || moved[0].ID != newer {
|
||||
t.Fatalf("again: %+v %v", moved, err)
|
||||
if moved, more, err = MovePackedCodexThreads(home, "openai", "magpie", since, 64<<20); err != nil || len(moved) != 1 || moved[0].ID != newer || more {
|
||||
t.Fatalf("again: %+v more %v %v", moved, more, err)
|
||||
}
|
||||
want := map[string]string{newest: "magpie", newer: "magpie", old: "openai", plain: "openai", theirs: "custom"}
|
||||
for id, prov := range want {
|
||||
@@ -101,8 +105,8 @@ func TestMovePackedCodexThreads(t *testing.T) {
|
||||
t.Fatalf("packed rollout after the move: %v\n%s", err, out)
|
||||
}
|
||||
// nothing left to move
|
||||
if moved, err = MovePackedCodexThreads(home, "openai", "magpie", since, 64<<20); err != nil || len(moved) != 0 {
|
||||
t.Fatalf("a third time: %+v %v", moved, err)
|
||||
if moved, more, err = MovePackedCodexThreads(home, "openai", "magpie", since, 64<<20); err != nil || len(moved) != 0 || more {
|
||||
t.Fatalf("a third time: %+v more %v %v", moved, more, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,7 +127,7 @@ func TestMovePackedCodexThreadsNoUpdatedAt(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d.Close()
|
||||
if moved, err := MovePackedCodexThreads(home, "openai", "magpie", time.Time{}, 64<<20); err != nil || len(moved) != 0 {
|
||||
if moved, _, err := MovePackedCodexThreads(home, "openai", "magpie", time.Time{}, 64<<20); err != nil || len(moved) != 0 {
|
||||
t.Fatalf("moved %+v %v", moved, err)
|
||||
}
|
||||
if got, _, _ := threadProvider(t, db, id); got != "openai" {
|
||||
@@ -169,7 +173,7 @@ func TestMoveRecentCodexThreads(t *testing.T) {
|
||||
}
|
||||
d.Close()
|
||||
since := now.Add(-30 * 24 * time.Hour)
|
||||
moved, err := MoveRecentCodexThreads(home, "myrelay", "magpie", since, 64<<20)
|
||||
moved, _, err := MoveRecentCodexThreads(home, "myrelay", "magpie", since, 64<<20)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -198,7 +202,7 @@ func TestMoveRecentCodexThreads(t *testing.T) {
|
||||
t.Fatalf("plain rollout after the move:\n%s", b)
|
||||
}
|
||||
// the packed-only move of the same leaves a plain thread
|
||||
if moved, err = MovePackedCodexThreads(home, "custom", "magpie", since, 64<<20); err != nil || len(moved) != 0 {
|
||||
if moved, _, err = MovePackedCodexThreads(home, "custom", "magpie", since, 64<<20); err != nil || len(moved) != 0 {
|
||||
t.Fatalf("packed-only: %+v %v", moved, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -646,7 +646,8 @@ func MoveCodexThreads(home, from, to string) ([]CodexMove, error) {
|
||||
if !codexProviderRe.MatchString(from) || !codexProviderRe.MatchString(to) || from == to {
|
||||
return nil, fmt.Errorf("can't move Codex's threads from %q to %q", from, to)
|
||||
}
|
||||
return moveCodexThreadIDs(home, from, to, codexThreadsOn(home, from), 0)
|
||||
moved, _, err := moveCodexThreadIDs(home, from, to, codexThreadsOn(home, from), 0)
|
||||
return moved, err
|
||||
}
|
||||
|
||||
// MoveCodexThreadIDs moves those of the threads ids that a Codex folder's
|
||||
@@ -664,16 +665,18 @@ func MoveCodexThreadIDs(home, from, to string, ids []string) ([]CodexMove, error
|
||||
still = append(still, id)
|
||||
}
|
||||
}
|
||||
return moveCodexThreadIDs(home, from, to, still, 0)
|
||||
moved, _, err := moveCodexThreadIDs(home, from, to, still, 0)
|
||||
return moved, err
|
||||
}
|
||||
|
||||
// moveCodexThreadIDs moves the threads ids of a Codex folder from the
|
||||
// provider from to the provider to, as MoveCodexThreads says, in the
|
||||
// order given. With a budget over 0 it stops once the files it has moved
|
||||
// come to that many bytes, a copy of each kept.
|
||||
func moveCodexThreadIDs(home, from, to string, ids []string, budget int64) ([]CodexMove, error) {
|
||||
// come to that many bytes, a copy of each kept, and says more when it
|
||||
// stopped there with a thread still to move.
|
||||
func moveCodexThreadIDs(home, from, to string, ids []string, budget int64) (moved []CodexMove, more bool, first error) {
|
||||
if len(ids) == 0 {
|
||||
return nil, nil
|
||||
return nil, false, nil
|
||||
}
|
||||
storeMu.Lock()
|
||||
defer storeMu.Unlock()
|
||||
@@ -697,8 +700,6 @@ func moveCodexThreadIDs(home, from, to string, ids []string, budget int64) ([]Co
|
||||
return nil
|
||||
})
|
||||
}
|
||||
var moved []CodexMove
|
||||
var first error
|
||||
var spent int64
|
||||
now := time.Now()
|
||||
for _, id := range ids {
|
||||
@@ -706,6 +707,7 @@ func moveCodexThreadIDs(home, from, to string, ids []string, budget int64) ([]Co
|
||||
continue
|
||||
}
|
||||
if budget > 0 && spent >= budget {
|
||||
more = true
|
||||
break
|
||||
}
|
||||
for _, p := range files[id] {
|
||||
@@ -723,7 +725,7 @@ func moveCodexThreadIDs(home, from, to string, ids []string, budget int64) ([]Co
|
||||
mv.From = from
|
||||
moved = append(moved, mv)
|
||||
}
|
||||
return moved, first
|
||||
return moved, more, first
|
||||
}
|
||||
|
||||
// MovePackedCodexThreads moves the threads of a Codex folder that
|
||||
@@ -735,12 +737,14 @@ func moveCodexThreadIDs(home, from, to string, ids []string, budget int64) ([]Co
|
||||
// since since, newest first, and no more once the files moved come to
|
||||
// budget bytes: a history of thousands of packed threads is gigabytes,
|
||||
// and the threads a user goes back to are the recent ones. The rest wait
|
||||
// for the next call. A database whose threads have no updated_at (an
|
||||
// for the next call, and more says there are some: nothing else may call
|
||||
// again for hours, and the Codex app refuses each one left (Lice9735 on
|
||||
// X, a chat from 25 days before). A database whose threads have no updated_at (an
|
||||
// older Codex) moves none. Codex 0.166 resumes a thread only on the
|
||||
// provider it is on now ("This chat uses openai, but your current
|
||||
// provider is magpie"), so a packed thread left on OpenAI couldn't be
|
||||
// reopened while magpie was Codex's provider.
|
||||
func MovePackedCodexThreads(home, from, to string, since time.Time, budget int64) ([]CodexMove, error) {
|
||||
func MovePackedCodexThreads(home, from, to string, since time.Time, budget int64) (moved []CodexMove, more bool, err error) {
|
||||
return moveRecentCodexThreads(home, from, to, since, budget, true)
|
||||
}
|
||||
|
||||
@@ -751,13 +755,13 @@ func MovePackedCodexThreads(home, from, to string, since time.Time, budget int64
|
||||
// the user's own provider (a relay's, "myrelay") was listed by the Codex
|
||||
// app once magpie was its provider, and refused: "This chat uses myrelay,
|
||||
// but your current provider is magpie" (hxy920830 on X).
|
||||
func MoveRecentCodexThreads(home, from, to string, since time.Time, budget int64) ([]CodexMove, error) {
|
||||
func MoveRecentCodexThreads(home, from, to string, since time.Time, budget int64) (moved []CodexMove, more bool, err error) {
|
||||
return moveRecentCodexThreads(home, from, to, since, budget, false)
|
||||
}
|
||||
|
||||
func moveRecentCodexThreads(home, from, to string, since time.Time, budget int64, packedOnly bool) ([]CodexMove, error) {
|
||||
func moveRecentCodexThreads(home, from, to string, since time.Time, budget int64, packedOnly bool) ([]CodexMove, bool, error) {
|
||||
if !codexProviderRe.MatchString(from) || !codexProviderRe.MatchString(to) || from == to || budget <= 0 {
|
||||
return nil, fmt.Errorf("can't move Codex's threads from %q to %q", from, to)
|
||||
return nil, false, fmt.Errorf("can't move Codex's threads from %q to %q", from, to)
|
||||
}
|
||||
var ids []string
|
||||
for _, p := range codexStateDBs(home) {
|
||||
|
||||
Reference in New Issue
Block a user