Commit Graph
4 Commits
Author SHA1 Message Date
yetone 1d704f8190 codex: OpenAI's threads follow a provider of the user's at the top, where Codex resumes them
harris on Discord (the Codex app, after switching from CC Switch):
"This chat uses openai, but your current provider is custom. Switch to
openai to continue." CC Switch left its "custom" as config.toml's
model_provider, which magpie keeps since #4347, and Codex 0.166 resumes
a thread only on the provider it is on now. Every thread started on
OpenAI before was refused, and switching to OpenAI would refuse
custom's threads instead, so the user was stuck. Nothing moved them:
bringOver moves only onto magpie, bringBeside only from such a provider
onto OpenAI.

lendOwn (each Sync) moves OpenAI's threads onto that provider, as
bringBeside does the other way: in place at OpenAI's length, by the row
for a paginated thread, the last 30 days rewritten with a copy kept
otherwise. The ids go in stash codex.lent; giveLent returns exactly
those to OpenAI once Codex is on it again (CC Switch's official card),
keeps the record while Codex is on that provider or on magpie, and
drops it on any other. Nothing moves while a profile names openai,
after the user disconnected Codex, or from a distro's Codex.
TestCodexThreadsComeBackOffMagpie/by_hand expected an OpenAI thread to
stay on OpenAI with custom set by hand at the top, the stuck state
itself; it now expects it on custom. docs/subsystems/agent-wiring.md
says so.

Run (mote, go 1.26.8): go test -count=1 ./internal/agent/ ok (250s);
GOOS=windows, GOOS=linux and native go vet ./internal/agent/ ok;
gofmt -l clean.
Broken by hand: Sync without lendOwn, TestCodexOwnProviderTakesOpenAIThreads
red (thread left on "openai", rollout not on custom); without the
disconnected and profile guards, TestCodexOwnProviderLeavesOpenAIThreads
red for both cases.
Not tried: a real Codex app resuming a moved thread on a real relay;
harris's config.toml wasn't seen, so the "custom" here is CC Switch's
relay table as it writes it. A thread moved from the ChatGPT backend to
a relay carries its encrypted reasoning, which a relay on another
account may not accept; the thread then fails on the relay as a
switch to it by hand would.
2026-11-06 00:51:01 +08:00
yetone 0ee424e3ee codex: a thread Codex 0.166 won't resume for its provider moves with Codex (毅, AcMilan on Discord)
Codex 0.166 (openai/codex#57840) refuses to resume a thread whose saved
provider isn't the configured one: "This chat uses openai, but your
current provider is magpie. Switch to openai to continue." (毅), and
"This chat uses cc-switch-official, but your current provider is
openai" (AcMilan). The rollout's session_meta provider wins over the
state DB row, and no -c override gets past it (openai/codex#58865).

Cause on magpie's side: bringOver relabelled only plain rollouts in
place. A thread Codex had packed to .jsonl.zst (a week after its last
turn) stayed on openai, so with magpie as provider it can't resume.
cc-switch-official is the id CC Switch 3.17-3.20 gave the official
account; CC Switch 4 moves those threads back to openai itself (#7948
there), but a user who left CC Switch keeps them stranded.

- sessions.MovePackedCodexThreads: packed threads on `from` updated in
  the last 30 days, newest first, up to 128 MB per sync, each moved by
  the existing packed rewrite with its copy kept in the trash. Rows
  are checked against the file (plain absent, .zst present, meta on
  `from`, same home). A state DB without updated_at moves nothing.
- bringOver calls it after RelabelCodexThreads; bringBack already
  moves packed threads back.
- bringOfficial: while magpie wires Codex and Codex is on openai or
  magpie, cc-switch-official threads move there, unless the user's
  top-level model_provider or a profile names cc-switch-official.
- Other providers and packed threads older than 30 days are left; the
  Sessions page's Move or Resume takes them.

Run (mote, at the pushed base c7bbd3faf):
- gofmt -l clean; go vet darwin, GOOS=linux, GOOS=windows on
  internal/agent and internal/sessions: ok.
- go test ./internal/agent ./internal/sessions: ok.
- Real codex-cli 0.166.0 app-server in a sandbox CODEX_HOME with a
  fake Responses server: an openai thread packed, Codex on magpie,
  thread/resume gave 毅's error word for word; RelabelCodexThreads
  alone left it; after MovePackedCodexThreads the resume said
  provider magpie and a turn started. A cc-switch-official thread with
  Codex on openai gave AcMilan's error; after the move it resumed on
  openai and a turn started.

Broken by hand (each red, then restored):
- budget check off: TestMovePackedCodexThreads red (both moved).
- the packed call in bringOver removed: TestCodexThreadsFollowMagpieIn
  red (packed thread stayed on openai).
- defer bringOfficial removed: TestCodexMovesCCSwitchOfficialThreads red.

Not tried: the Codex App GUI itself; agent Sync against a real Codex
(unit tests only); Windows and Linux runs; threads on other provider
ids, which 0.166 also refuses (left to the Sessions page).
2026-11-04 15:12:08 +08:00
yetone a93190ae19 agent: the Codex threads test names what its HOME still holds after removal (#3422)
TestCodexThreadsComeBackOffMagpie/disconnect went red once on macOS CI
(27a8255fd, run 40004148936) with "TempDir RemoveAll cleanup: directory
not empty", its own checks passing. It doesn't reproduce here: 40 runs
alone and three full internal/agent runs with a probe for late writes
were all ok, nothing written after the test. A cleanup registered before
the TempDir's now runs after its removal and logs each entry left, so
the next red names the writer.

Run: go vet ./internal/agent, gofmt; go test ./internal/agent -run
TestCodexThreadsComeBackOffMagpie -count=3 ok. Test-only.
2026-10-24 02:58:50 +08:00
yetone 85534cd078 codex: threads started on magpie come back to the provider Codex is on
kaizaoji on X (10-22, zwaydot's Codex thread): after using magpie with
Codex and then disconnecting or editing the config, projects say their
context doesn't exist, and earlier threads can't be read.

Cause: Codex lists threads per provider. app-server thread/list filters
on the current model_provider when the caller names none
(thread_processor.rs), and the TUI resume picker does the same; no
setting turns that off. magpie sets model_provider = "magpie" with an
API-key sign-in, with no sign-in, with a ChatGPT account out of
allowance (codex.out), in the "magpie API" login mode and for a gateway
on another machine. Every thread started then is tagged "magpie" in its
rollout's session_meta and in state_5.sqlite. After Disconnect (or a
hand edit back to openai) Codex lists only "openai" threads, so those
threads are gone from resume and from the app.

Not done: routing through the built-in openai provider with
openai_base_url instead of switching model_provider. The built-in
requires OpenAI auth, so with no sign-in the TUI shows its login
screen; with an API key Codex skips /models (#322, why 953bce2cf
reverted c3931c91d's base-URL-only path); the Codex app blocks a
used-up ChatGPT account before any request (#540); "magpie API" is the
user's own choice of provider; a remote gateway needs its key (#2145).
In a sandbox, API key + openai_base_url does reach {base}/responses with
the key, but that covers only API-key users on this machine and reopens
#322.

Fix: magpie's own threads come back to whatever provider Codex is on.
sessions.MoveCodexThreads(home, from, to) takes the ids state_5.sqlite
has on "magpie", finds their rollout files (sessions/ and
archived_sessions/, segments too) and moves each with the same code as
MoveCodexProvider (now moveCodexPaths): a copy in magpie's trash first,
the rollout's session_meta and the DB row rewritten, a thread written
to in the last minute left for the next pass. The agent runs it after
Unwire and after every Sync (start, catalog change, the out() check),
only when the config doesn't name magpie, by model_provider or by any
profile, and not for a WSL distro's Codex. Only threads tagged "magpie"
move: a CC Switch or custom provider's threads (#887, #1372, #1796)
stay where they are, and a hand-set model_provider = "custom" gets
magpie's threads, since that is where Codex now lists.

Not moved the other way: while magpie is the provider, threads on
openai stay hidden, as before. Moving them onto magpie would strand
them if magpie goes away without a Disconnect (an uninstall, or the
reporter's hand edit), and the out() flip comes every allowance window,
which would rewrite and back up every thread each time.

Run:
- go test -run 'Codex|Thread|Provider' ./internal/agent
  ./internal/sessions on e6bed2f06: ok (27.2s,
  3.2s). The packages' full run waits behind another session's suite
  and runs after the push (owner: ship each verified fix at once).
  Rebased after onto 97902e938, which touches internal/provider only;
  go build ./... and go vet of both packages ran again there.
- gofmt -l clean; go vet ./... (darwin), GOOS=windows go vet ./...,
  GOOS=linux go vet -tags nogui ./... clean.
- Real codex 0.162.1 in a sandbox HOME/CODEX_HOME with a fake Responses
  server: two exec threads made on model_provider magpie, one on openai.
  Config back on openai: app-server thread/list (sourceKinds cli,
  vscode, exec) listed only the openai thread; with modelProviders []
  all three, two tagged magpie. MoveCodexThreads moved the two (backups
  in trash/codex-provider), and thread/list then listed all three on
  openai. `codex exec resume` of a moved thread went through and was
  appended to the same rollout, its first message still there.

Broken by hand, each red:
- bringBack off: TestCodexThreadsComeBackOffMagpie disconnect and
  by_hand fail ("on magpie, want openai").
- the namesMagpie guard removed: by_hand fails (moved while a profile
  is on magpie).
- the provider query widened (OR 1): TestMoveCodexThreads fails (the
  custom thread moved).

Not tried: a real OpenAI key or ChatGPT account (the sandbox used a
fake key and server); the TUI resume picker itself (it uses the same
provider filter as thread/list); a WSL distro's Codex (skipped on
purpose); a Codex old enough to have no state_5.sqlite (nothing moves
there).
2026-10-23 16:24:13 +08:00