Files
magpie/internal/sessions/codex_resume.go
T
yetone c28220a5e7 sessions: a Codex thread saved on another provider resumes from Sessions
mrten_95615 on Discord: opening a Codex session from Sessions ran
`codex resume <id>` and Codex 0.166 stopped at "This chat uses custom,
but your current provider is magpie. Switch to custom to continue."
(modelProviderMismatch). Codex 0.166's app-server resumes a thread only
on the provider it was saved with (its state-database row, else its
session_meta), checked against the one config.toml (with -c) selects
(codex-rs app-server config_manager.rs load_for_resume); no flag lets a
resume go on another provider, and a provider config.toml no longer has
fails as modelProviderNotConfigured. Codex 0.165 resumed it on its own
provider without asking.

sessions.ResumeNow is the line that runs when the user opens a session
(GUI Terminal, TUI enter); the listed line is unchanged and nothing more
is read on a listing. For a Codex thread saved on a provider other than
the one Codex is on:
- config.toml still has that provider's table, or it is a Codex
  built-in (openai, amazon-bedrock(-runtime), gcp, ollama, lmstudio):
  `codex resume -c model_provider=<it> <id>`. Codex runs that embedded
  (no shared daemon) on the thread's own provider.
- the provider is gone: MoveCodexProvider moves the thread to the
  provider Codex is on (files copied to magpie's trash first, the state
  row too) and it resumes there, the only way Codex resumes it at all. A
  thread written to in the last minute or open in a Codex is left
  (ErrActive) and the plain line runs.
A WSL distro's Codex session and every other agent are as listed.

Run at this head (mote):
- go test -count=1 -run 'TestCodexResume|TestMoveCodexProvider|TestCodexProviderIn' ./internal/sessions: ok
- go vet ./internal/{sessions,gui,tui}, GOOS=linux -tags nogui and GOOS=windows vet of them: ok; gofmt -l: clean
- At this head (rebased on origin/main bf05291e2): go test -count=1
  ./internal/sessions ./internal/tui: ok; ./internal/gui: ok but
  TestMainWindowCorners/macOS_26_SDK (#3949, mote is macOS 27; this
  change touches only gui/sessions.go's terminal route); go vet darwin,
  GOOS=linux -tags nogui, GOOS=windows of the three: ok; gofmt -l clean.
- Full suite before the rebase (go test -tags nogui -timeout 45m ./...
  on mote, at eb779ce92 + this change): 86 packages ok; internal/gateway
  red on TestCodexPoolSaysTheClientsVersion alone (#4807, open, a full
  package run's known red). Not re-run after the rebase.
Broken by hand: ResumeNow returning the listed line, TestCodexResumeOnItsProvider
fails "resume ... && codex resume 01a0bdc9-..., want ... codex resume -c
model_provider=custom 01a0bdc9-..."; with the move dropped it fails
"the thread's rollout wasn't moved to magpie".
Real Codex: codex-cli 0.166.0 (npm darwin-arm64) in a sandbox
CODEX_HOME with two fake Responses servers: a thread made on "custom",
config.toml switched to magpie. Plain `codex exec resume` failed with
the reporter's modelProviderMismatch; ResumeNow's line resumed it and the
turn reached custom's server. With custom's table removed, plain resume
failed with modelProviderNotConfigured; ResumeNow moved it and the turn
reached magpie's server. The TUI `codex resume -c model_provider=custom`
was also driven in tmux (resumed, turn on custom, "Running without the
shared background server" warning). Codex 0.165.0 resumes it plainly.
Not tried: a Codex profile that names its own model_provider (a -c
override may lose to it); the Codex desktop app's resume; Windows'
PowerShell line (same flag, not run there).
2026-11-03 10:50:30 +08:00

95 lines
3.6 KiB
Go

package sessions
import (
"database/sql"
"log"
"net/url"
"path/filepath"
"strings"
)
// Codex 0.166 resumes a thread only on the provider it was started on:
// the provider its row in the state database names, else its rollout's
// session_meta. On any other, `codex resume <id>` stops at "This chat uses
// custom, but your current provider is magpie. Switch to custom to
// continue." (modelProviderMismatch, codex-rs app-server
// config_manager.rs load_for_resume), and nothing lets a resume go on
// another provider: the check compares the provider the thread was saved
// with against the one config.toml (with -c) selects. So a session started
// through CC Switch's "custom" couldn't be opened from Sessions once Codex
// was on magpie (mrten_95615 on Discord). Codex 0.165 and before resumed it
// on its own provider without asking.
// codexBuiltinProviders are the providers Codex knows without a table of
// config.toml (model-provider-info built_in_model_providers).
var codexBuiltinProviders = []string{"openai", "amazon-bedrock", "amazon-bedrock-runtime", "gcp", "ollama", "lmstudio"}
// codexSavedProvider is the provider Codex resumes a thread of a Codex
// folder on: its row's in the state databases, else its rollout's
// session_meta's, as Codex's thread/resume reads them; "" when neither
// names one.
func codexSavedProvider(home, id, path string) string {
for _, p := range codexStateDBs(home) {
u := url.URL{Scheme: "file", Path: filepath.ToSlash(p), RawQuery: "mode=ro&_pragma=busy_timeout(2000)"}
if len(u.Path) >= 2 && u.Path[1] == ':' {
u.Path = "/" + u.Path
}
db, err := sql.Open("sqlite", u.String())
if err != nil {
continue
}
var s sql.NullString
err = db.QueryRow(`SELECT model_provider FROM threads WHERE id = ?`, id).Scan(&s)
db.Close()
if err == nil && s.String != "" {
return s.String
}
}
return codexMetaProvider(path)
}
// codexProviderKnown says whether the Codex of a folder can run on the
// provider p: a table of config.toml's [model_providers], or a built-in.
func codexProviderKnown(home, p string) bool {
if contains(codexBuiltinProviders, p) {
return true
}
_, ok := readCodexConfig(home).Providers[p]
return ok
}
// ResumeNow is the line that resumes a session as its agent takes it now,
// made when the user opens it, not on each listing. For a Codex session
// started on a provider other than the one Codex is on now, that is
// `codex resume -c model_provider=<its provider> <id>` while config.toml
// still has that provider, so it goes on where it was made; when the
// provider is gone the thread is moved to the one Codex is on
// (MoveCodexProvider, its files kept in magpie's trash first) and resumed
// there, the only way Codex resumes it at all. Any other session, and a
// Codex one in a WSL distro, is its Resume as listed.
func ResumeNow(s Session) string {
if s.Agent != "codex" || s.Resume == "" || s.WSL != "" || s.Path == "" || !safeID.MatchString(s.ID) {
return s.Resume
}
home := codexHomeOf(s.Path)
if home == "" {
return s.Resume
}
now := CodexProviderIn(home)
was := codexSavedProvider(home, s.ID, s.Path)
if was == "" || was == now || !codexProviderRe.MatchString(was) {
return s.Resume
}
if codexProviderKnown(home, was) {
run := "codex resume " + s.ID
if i := strings.LastIndex(s.Resume, run); i >= 0 {
return s.Resume[:i] + "codex resume -c model_provider=" + was + " " + s.ID + s.Resume[i+len(run):]
}
return s.Resume
}
if _, err := MoveCodexProvider(s.ID, now); err != nil {
log.Printf("sessions: resuming Codex thread %s, started on %s, which config.toml no longer has: moving it to %s: %v", s.ID, was, now, err)
}
return s.Resume
}