A Codex Responses request carries the whole conversation, and the gateway
walked it several times per request for what it reads at the top level:
- requestSessionMetadata searched the body for "client_metadata", which
Codex sends last, before asking topMember. A long body now asks the
memoized split straight away.
- bodyTier asked gjson for speed and service_tier, walking the body each
time; a long body now asks topMember too (topValue).
- userWords ran its reminder and "## My request" regexes on every text;
each now runs only when its marker is present.
- readFacts (turn count, work folder) ran the full parse. It now parses
in facts mode (readResponses): views over the raw bytes, no parts for
assistant replies, and tool outputs as bare ToolResult parts, which is
all turnIn and workPlace read.
Bench (a fake ChatGPT backend, ~2MB Codex body, client_metadata last),
per request: 31.7ms, 24.2MB, 71.2k allocs on origin/main; 19.2-21.4ms,
17.2MB, 59.6k allocs with these and the provider commit after it.
Real account: a sandbox HOME with only the deepseek provider, 310KB
Responses bodies to deepseek/deepseek-flash through `magpie serve`:
base 15ms process CPU per request (19/20 ok), new 13ms (20/20, then
8/8 again).
TestResponsesFactsAsTheWholeParse compares readFacts with the full
parse's turnIn and workPlace over constructed bodies and five real Codex
fixtures. Broken by hand twice (user parts skipped; ToolResult turned
into Text): red both times. tier_record_test adds four long-body cases
(tier after the conversation, a nested one ignored, Anthropic speed
fast, a Chat numeric tier).
Run at this head (720e491d2 + these two commits): gofmt clean, go build,
GOOS=linux and GOOS=windows go vet -tags nogui on gateway and provider;
go test -run 'Facts|Tier|Session|Responses|Metadata|UserWords|Prompt|Memo'
./internal/gateway ok. The full gateway and provider packages are
running and are reported on #3394.
125 lines
4.0 KiB
Go
125 lines
4.0 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), so it is
|
|
// asked straight away: Codex sends client_metadata last, after the
|
|
// conversation, and looking for the word walked all of it each time.
|
|
if (len(body) >= validMemoMin || 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 ""
|
|
}
|