Commit Graph
2 Commits
Author SHA1 Message Date
yetone c688211d13 gateway: a ChatGPT (Codex) account gets loaded tools where they were loaded, keeping its cache (#4738)
infinitex233 (#4738, Discord): Claude Code -> magpie -> a Codex account,
and every ToolSearch load dropped cache_read to 0. loadsInPlace sent the
additional_tools item (#2660) only to OpenAI's own API, so for an
account the loaded tool went into the top-level tools and the cached
prefix broke at the tools.

Tried on the real ChatGPT backend first (owner's account, sandbox HOME
on mote with a copied auth.json whose refresh_token was replaced, so the
live token was never rotated; ~/.codex untouched). A probe client sent
Claude Code's shape: ToolSearch with two deferred mcp tools, then the
tool_reference result, then the calls, 4 requests per run.
- additional_tools item (on, and this commit's default): request 2..4
  read 7680-8320 tokens from cache on gpt-5.6-terra, gpt-6-luna and
  gpt-6-sol; the model called read_a and read_b from the item; no
  "turned away additional_tools" in the log.
- tools top-level (off): request 2 and 3 read 0 from cache (8044 and
  8163 tokens in), as the reporter saw.
So the backend takes it, and this makes it the default for a ChatGPT
account (p.Account.Agent == "codex"). Copilot and other sign-ins stay
off: not tried.

- loadsInPlace: AdditionalToolsOff wins, then AdditionalTools, then an
  account is in only if it's Codex's, then OpenAI's API as before.
- `magpie provider set <id> additional-tools=on|off|default`: off is new
  (AdditionalToolsOff, kept through an account's Save by accountSettings
  and through the GUI editor's save like AdditionalTools). The old
  "off"/"no" meant default; now they turn it off where the default is on.
- The 400/422 fallback that names the item still applies, so a backend
  that stops taking it costs one retry and then sends tools top-level.
- No prompt, instructions or identity line changed.

Run (mote, at base 586b59fe7, this commit and the previous together):
- go test -count=1 -timeout 45m ./internal/provider ./internal/gateway
  ./internal/cli ./internal/gui: provider ok 248s, gateway ok 830s,
  cli ok 105s; gui FAIL on two filed reds that this doesn't touch:
  TestSettingsSaveKeepsWhatItDoesNotSend (BusyLoad, #4739) and
  TestMainWindowCorners/macOS_26_SDK on macOS 27 (#3949).
- Rebased on origin/main 9feb1024c, then again at this head: gofmt
  clean, vet darwin/linux/windows ok, provider ok 179s, cli ok 88s,
  gateway -run 'LoadsInPlace|LoadedTools|Codex|AdditionalTools|InPlace|
  Account' ok 99s, gui -run 'Provider|Settings|Account' red only on
  #4739. The whole gateway and gui packages ran at the base only.
- gofmt -l clean; go vet -tags nogui ./... for darwin, linux, windows ok.
- New TestCodexAccountLoadsInPlace (a fake ChatGPT backend through the
  gateway: the item with read_a, then tools top-level after
  additionalToolsOff in providers.json); TestLoadsInPlaceScope extended
  (Codex on, Codex off, OpenAI off, Copilot off by default and on when
  set); CLI test for off, on after off, default.
- Broken by hand: with the Codex default taken out of loadsInPlace,
  TestCodexAccountLoadsInPlace and TestLoadsInPlaceScope go red.
- GUI node suite not run: no JS changed; the Go editor save only keeps
  one more field, covered by the Go tests above.

Not tried: the reporter's own setup (Windows, gpt-6.1-terra), a real
Claude Code client (the probe sent its request shape), Copilot or any
other signed-in vendor with the item.
2026-11-01 14:59:44 +08:00
yetone dd25997a45 codex: a client's access_programs and the account's extraBody reach the ChatGPT backend, and its refusal comes back as it said it (magpie-releases#18)
NonMirror (0.1.1239, OpenCode, codex/gpt-daybreak-blue-latest on a ChatGPT
sign-in): a top-level Responses access_programs never reached upstream,
and the codex provider's extraBody wasn't applied.

Cause:
- A Codex account is streamed upstream, so a whole (stream:false)
  Responses request and every Chat or Messages request is translated
  (parseResponses/parseChat -> Request -> buildResponses). Request had no
  field for access_programs, so the rebuilt body dropped it. Only a
  streamed Responses request, relayed through codexBody, kept it.
- WithExtraBody skipped every signed-in account, and withPicks didn't
  copy the providers.json ExtraBody onto the account.
- writeError rewrapped every vendor error with code and param null, so
  even a forwarded refusal lost what it was about.
- The backend refuses a program the workspace hasn't got inside a 200
  stream (error event, invalid_request_error, access_program_not_enabled).
  The whole-translated path answered that 502 api_error, and translated
  streams wrote it as api_error, which holdWriter reads as 502: the same
  request went three times, then to the other accounts, and every one
  rested.

Fix:
- Request.AccessPrograms (Responses and Chat), built into the Responses
  body as it came; a null is kept.
- A ChatGPT (Codex) account takes extraBody; store, tool_choice and
  include stay magpie's (codexOwned). withPicks keeps ExtraBody.
- writeVendorError keeps the vendor's error.code and error.param on the
  relayed, whole-translated and streamed-translated status-error paths.
- requestRefused: a Responses stream error/failed whose error type is
  invalid_request_error carries status 400 and the vendor's error
  (Event.Said). The whole path answers that 400 with code and param; the
  Chat, Anthropic and Responses encoders write its type/code/param
  (saidError). Read by the type, never by words.
- Codex CLI sends access_programs as a top-level object on its Responses
  request (codex-rs ResponsesApiRequest.access_programs); OpenAI's Daybreak
  guide documents the same field.

Run:
- New: internal/gateway/codex_access_programs_test.go
  (TestCodexAccessProgramsReachBackend, TestCodexAccountTakesExtraBody,
  TestAccessProgramsNullKept), passes -count=3.
- Broken by hand, each went red: buildResponses not emitting the field
  (probe answered 200, the reporter's symptom); extraBodyTakes' Codex case;
  withPicks' line; codexOwned emptied; writeVendorError ignoring the body;
  the continuation site alone; retryable(400) true (asked twice, rested);
  requestRefused returning nothing (502, asked 4 times, both accounts
  rested); saidError a no-op (streamed: asked 4 times, rested); the whole
  site back to 502 (same).
- go test -timeout 30m ./...: all 62 packages ok (gateway 1559s at load
  ~26; an earlier run at the default 10m timed out in gateway under that
  load, not a failure). gofmt -l clean. GOOS=linux and GOOS=windows
  go vet -tags nogui ./... clean.

Real account (sandbox HOME, own port 3486, copied auth.json with a dead
refresh token; owner's ChatGPT Pro account, model codex/gpt-5.6-luna):
- "__daybreak_forwarding_probe__": 400 invalid_value, param
  access_programs.cyber ("Supported values are: 'standard',
  'daybreak_blue', and 'daybreak_red'") on Responses whole and streamed,
  Chat whole and streamed. The backend says invalid_value, not the
  guide's invalid_access_program.
- standard: 200. null: 400 invalid_type.
- daybreak_blue (not enabled for this workspace): 400
  access_program_not_enabled with param on Responses and Chat whole, the
  error event with that code on Responses and Chat streamed; ~1s each,
  one try (was 3 tries and a 502 before this change).
- The next plain request was served 200 by the same account: nothing
  rested.

Not verified: the Daybreak model itself (not listed for this account),
so no 200 with daybreak_blue; OpenCode's own wire; the Messages route
against the real backend (fake backend only).
2026-10-15 16:51:31 +08:00