atom_Ray on X: with a third-party model picked in the Codex app through
magpie, an hour of use still took 2% of the ChatGPT account's 5-hour
window, and at 0% left it could not be used at all.
The cause is the app's hidden side turns. ChatGPT.app 26.1013.40934 asks
for a thread's title, description and one-line summary
(ThreadMetadataGenerationService.generateTitle/generateDescription/
generateSummary in .vite/build/main-CLu1A25j.js; R0/z0/B0 via H0 and Jq
in src-DsLVGakC.js) on ephemeralGenerationModel, gpt-6-luna by default,
whatever model the conversation is on. A summary is asked after each
user and assistant message (bAu in app-initial). With magpie's model
beside the ChatGPT sign-in (openai_base_url), these reach
/backend-api/codex as gpt-6-luna. codexTitlesTo and codexDescriptionsTo
left an unset setting on the model Codex asked for, and thread_summary
had no route at all, so all three went to ChatGPT on the user's sign-in.
Now, with Codex's config model one of magpie's and magpie not Codex's
provider (agent.CodexSideModel, through gateway.CodexSideModel),
codexSideTo sends titles, descriptions and summaries on the Codex
backend path to that model. On Codex's own model nothing changes, and a
model the Titles or Descriptions setting names still wins. Summaries get
the same wrapper as descriptions (codexShaped): the plain answer is
handed back as the {"summary": ...} the schema asks for, with
compactSummary filled and cut to its maxLength (summaryJSON). This also
covers a summary on magpie's own API, where a Chat model's plain answer
was dropped by the app. No prompt, instructions or base_instructions are
changed: the request is sent to another model as Codex wrote it.
Run (this change on origin/main e57e78563 for the full suite; rebased
onto 3a47e377e, which touches internal/gateway, for the packages run
again; then onto 0e643b14e, internal/sessions only, built):
- gofmt -l; go vet darwin, GOOS=linux -tags nogui, GOOS=windows for
internal/gateway, internal/agent and internal/cli: clean.
- New tests: TestCodexSideTurnsOnThePickedModel,
TestCodexSummaryOnMagpieProvider, TestSummaryJSON (gateway) and
TestCodexSideModel (agent). Also the existing title, description and
#540 used-up tests: ok.
- Full suite (go test -tags nogui -timeout 45m ./..., one suite on the
machine, log tee'd): 70 packages ok, 19 without tests, no FAIL, exit 0
(gateway 594s, agent 449s, provider 220s, cli 151s).
- Rebased onto 3a47e377e: gofmt and vet (nogui) of gateway, agent and
cli clean; go test -tags nogui -timeout 45m of internal/gateway,
internal/agent, internal/cli and internal/usage: all ok.
- Rebased onto 0e643b14e (internal/sessions only): go build ./... ok;
not run again.
Broken by hand:
- codexSideTo's side-model branch turned off: TestCodexSideTurnsOn-
ThePickedModel red (the title went to ChatGPT, 0 magpie calls).
- Both codexSummariesTo call sites turned off: TestCodexSideTurnsOn-
ThePickedModel red on thread_summary, and
TestCodexSummaryOnMagpieProvider red (plain text came back).
- SideModel without the !asProvider() check: TestCodexSideModel red.
Not tried: the real Codex App with a real ChatGPT account (live ~/.codex
and the running magpie were left alone), Windows and Linux. The app's
ambient suggestions (ambientSuggestionsModel), safety recourse and
commit/PR message turns are not moved; they are asked rarely or only by
user action. Not changed: what ef37b6fb7 does at 0% (Send greyed in the
app until it is reopened). On macOS magpie reopens the app once when it
is idle. On Windows and Linux it only shows a notice
(internal/gui/codex_app_out.go:78). With account discovery off,
readLogins returns nil (internal/provider/logins.go:208), so a used-up
account is never known.
123 lines
3.8 KiB
Go
123 lines
3.8 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
func isTitleKind(kind string) bool {
|
|
return kind == "thread_title" || kind == "thread_title_reconsideration" || kind == "title_generation"
|
|
}
|
|
|
|
func isDescriptionKind(kind string) bool {
|
|
return kind == "thread_description"
|
|
}
|
|
|
|
// isSummaryKind: the Codex app's one-line summary of a thread's latest
|
|
// message, shown under its title (thread_summary)
|
|
func isSummaryKind(kind string) bool {
|
|
return kind == "thread_summary"
|
|
}
|
|
|
|
// isPredictionKind: the Codex app's guess at the user's next message,
|
|
// shown in its composer (composer_predictions, #1970)
|
|
func isPredictionKind(kind string) bool {
|
|
return kind == "composer_predictions"
|
|
}
|
|
|
|
// Codex projects turn metadata into headers, but its canonical transport is
|
|
// client_metadata in the Responses body. Read only identity fields; never keep
|
|
// prompts or the rest of the metadata in the trace.
|
|
type sessionMetadata struct {
|
|
Turn string `json:"turn_id"`
|
|
Source string `json:"thread_source"`
|
|
Parent string `json:"parent_thread_id"`
|
|
Forked string `json:"forked_from_thread_id"`
|
|
Installation string `json:"installation_id"`
|
|
}
|
|
|
|
func requestSessionMetadata(h http.Header, body []byte) sessionMetadata {
|
|
var metadata map[string]json.RawMessage
|
|
var m sessionMetadata
|
|
// Every Codex request asks this four or five times, so only the member
|
|
// is decoded: decoding the whole body walked the conversation each time
|
|
// (#3394). A long body is checked and split once (topMember).
|
|
if bytes.Contains(body, []byte("client_metadata")) && metadataOf(body, &metadata) {
|
|
if raw := metadata["x-codex-turn-metadata"]; len(raw) > 0 {
|
|
var text string
|
|
if json.Unmarshal(raw, &text) == nil {
|
|
raw = []byte(text)
|
|
}
|
|
if json.Unmarshal(raw, &m) == nil {
|
|
return m
|
|
}
|
|
} else if len(metadata) > 0 {
|
|
_ = json.Unmarshal(metadata["turn_id"], &m.Turn)
|
|
// Compatibility projections used by clients without the full blob.
|
|
_ = json.Unmarshal(metadata["thread_source"], &m.Source)
|
|
_ = json.Unmarshal(metadata["parent_thread_id"], &m.Parent)
|
|
if m.Parent == "" {
|
|
_ = json.Unmarshal(metadata["x-codex-parent-thread-id"], &m.Parent)
|
|
}
|
|
_ = json.Unmarshal(metadata["forked_from_thread_id"], &m.Forked)
|
|
_ = json.Unmarshal(metadata["installation_id"], &m.Installation)
|
|
if m.Source != "" || m.Parent != "" || m.Forked != "" {
|
|
return m
|
|
}
|
|
}
|
|
}
|
|
projectedTurn := m.Turn
|
|
m = sessionMetadata{}
|
|
if json.Unmarshal([]byte(h.Get("x-codex-turn-metadata")), &m) != nil {
|
|
return sessionMetadata{Turn: projectedTurn}
|
|
}
|
|
if projectedTurn != "" {
|
|
m.Turn = projectedTurn
|
|
}
|
|
return m
|
|
}
|
|
|
|
// metadataOf decodes body's client_metadata into md, as decoding body into
|
|
// a struct with that one field does: false when body isn't a JSON object or
|
|
// the member isn't an object or null.
|
|
func metadataOf(body []byte, md *map[string]json.RawMessage) bool {
|
|
raw, found, ok := topMember(body, "client_metadata")
|
|
if !ok {
|
|
return false
|
|
}
|
|
return !found || json.Unmarshal(raw, md) == nil
|
|
}
|
|
|
|
func requestCallKind(h http.Header, m sessionMetadata) string {
|
|
if h.Get("x-openai-subagent") != "" || h.Get("x-openai-memgen-request") != "" {
|
|
return callKind(h)
|
|
}
|
|
switch source := strings.TrimSpace(m.Source); source {
|
|
case "", "user", "subagent":
|
|
return callKind(h)
|
|
default:
|
|
if len(source) > 40 {
|
|
source = source[:40]
|
|
}
|
|
return source
|
|
}
|
|
}
|
|
|
|
// Only title helpers inherit their originating chat. A user-created fork is a
|
|
// separate conversation, even though it carries the same fork ancestry.
|
|
func titleParentSession(h http.Header, m sessionMetadata, kind string) string {
|
|
if !isTitleKind(kind) {
|
|
return ""
|
|
}
|
|
for _, id := range []string{m.Parent, m.Forked, h.Get("x-codex-parent-thread-id")} {
|
|
id = strings.TrimSpace(id)
|
|
if id == "" || len(id) > 128 || strings.ContainsAny(id, "\r\n\t") || id == sessionOf(h) {
|
|
continue
|
|
}
|
|
return id
|
|
}
|
|
return ""
|
|
}
|