Files
magpie/internal/gateway/codex_socket_keep.go
T
yetone c056b7c8c3 provider: a Responses provider's turns may go over the Responses WebSocket (#5781)
A provider the user added with an http(s) Responses address can now send
its Responses turns over the Responses WebSocket that magpie already
speaks to chatgpt.com (codexSocket), with `socket=auto|on|off`:

- off, or absent: today's behaviour exactly. Provider.Via hides any
  socket value an outer context carried, so such a provider never dials.
- auto: tries the socket, falls back to HTTP when the handshake is
  refused or the socket ends before the reply, and speaks HTTP to a host
  for 10 minutes once it refused the handshake (codexShun).
- on: the same fallback, but never shuns; the socket is tried each turn.

Provider.Via puts the provider's Responses host and path in the request's
context (withSocket); codexSocket's gate is provider.SocketTurn, which
takes a ChatGPT turn as before, or a POST to that host at
<Responses>/responses. The rest is the ChatGPT leg's: the kept socket and
its delta (codexKeepKey now also hashes the scheme, X-Api-Key and
Api-Key), the error frames, 1009 handling. A provider's reply with no
Content-Type is marked text/event-stream, since it is read as one.
Routing, keys, limits, usage and masking are unchanged: only the
transport under the request differs.

Set with `magpie provider set <id> socket=auto|on|off` (refused on a
provider with an account, no http(s) Responses address, or Vertex,
TakesSocket), saved in providers.json as "socket", shown by `provider
show`, and in the editor's Advanced as a WebSocket tick beside HTTP
version (ticked saves auto, keeps a CLI's on, unticked saves off; shown
only while the Responses address is http(s) and there is no account).
docs/subsystems/providers-accounts.md describes it.

app.js pays for its rows by three long comments cut to one line
(renderMove, the vendors' status pages, header rows): the core's scripts
are 478 bytes smaller than on 0eac4e6aa (1,533,069 of 1,533,802); no
budget raised.

Run on mote at this change on origin/main 0eac4e6aa (since then main
added a39b63e88 and 7cabf0da9, internal/sessions only):
- gofmt -l clean; go vet (darwin) of provider, gateway, cli, gui, clean.
- go test -tags nogui ./internal/gui ./internal/cli: ok;
  ./internal/provider -run TestSocket: ok.
- go test ./internal/gateway -run 'TestResponsesProvider|TestCodexSocket|
  TestCodexKept|TestCodexTurn|TestHTTP1|TestInsecureTLS': ok. The new
  tests use an httptest WebSocket upstream: a turn over the socket, a
  provider without the field never dialling (and one on the same host
  with it), a refused handshake shunned on auto and tried again on on, a
  socket closed before the first event going over HTTP, the kept
  socket's delta on the next turn, and the reply being a stream.
- node --test provider-socket (new; chromium and webkit, en zh zh-TW ja
  de, 1100 and 420 wide), provider-http1, provider-insecure-tls,
  i18n-en-no-cjk: pass. i18n-keys' "a plugin's words are its own" is
  red for three words in files this doesn't touch, filed as #5327/#5332,
  #5426 and #5482.
- One rebase earlier (on 2a48974e8, the same change): GOOS=linux (nogui)
  and GOOS=windows go vet of the four packages clean, and the whole of
  ./internal/provider ok.
- Broken by hand, each red then green: SocketTurn codex-only (all six
  gateway tests red); a path-suffix gate in place of the context
  (WithoutSocketNeverDials, TestSocketTurn red); the never-shun case
  removed (RefusedOnTriesAgain red); the Content-Type line removed
  (ReplyIsAStream red).

Not tried: a real Responses upstream speaking the WebSocket (no network
in tests); the keep key's X-Api-Key/Api-Key separation has no test of
its own; the editor in the Linux app's WebKitGTK; the full suite (run
after the push).

Reported-by: matrix0487 (Discord)
Reported-by: faithleysath (#5781)
2026-11-09 18:57:29 +08:00

366 lines
9.8 KiB
Go

package gateway
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"net/http"
"sync"
"time"
"github.com/coder/websocket"
"github.com/tidwall/gjson"
)
// A conversation's socket to the ChatGPT backend is kept between its
// turns, and a turn that carries on from the last one goes on it as only
// its new input after previous_response_id, as Codex's own client does it
// (codex-rs core/src/client.rs: WebsocketSession, get_incremental_items).
// With store false the backend holds a response for the socket it was
// made on, so the id is only ever named on that socket.
//
// A socket is kept for a turn whose request names its conversation
// (session-id or thread-id, or else prompt_cache_key) and answered
// response.completed with an id; under a key of the host, the sign-in
// (a digest of Authorization and chatgpt-account-id), the conversation's
// ids and the subagent headers, so another account's turn never finds it.
// The next turn of that key takes it out of codexKeepPool, so a second
// turn of the conversation at the same time opens its own socket, and the
// last to end is the one kept. A kept socket is let go after codexKeepIdle
// unused, at codexKeepAge (the backend ends a connection at 60 minutes),
// and the oldest past codexKeepMax.
//
// The turn goes as a delta when every field but input, client_metadata
// and stream_options is as the last turn sent it, and its input starts
// with the last turn's input and then that turn's output items
// (response.output_item.done, in order), each item compared as codexSocketItem
// has it. Anything else goes whole on the kept socket, as Codex sends it.
// What is compared is the body as it goes, after masking and every
// translation, so a turn masked otherwise than the last goes whole.
//
// A kept socket that fails before the reply is handed on (it was closed
// while kept, the write fails, an error frame comes first, such as
// previous_response_not_found or the connection limit) is closed, and the
// turn goes whole on a new socket, and from there over HTTP, as a turn
// with no kept socket does.
var (
// codexKeepIdle is how long a kept socket waits for its next turn
codexKeepIdle = 10 * time.Minute
// codexKeepAge is how long a socket is used, from its handshake
codexKeepAge = 55 * time.Minute
)
// codexKeepMax is how many sockets are kept at most.
const codexKeepMax = 32
var (
codexKeepMu sync.Mutex
codexKeepPool = map[string]*codexKept{}
)
// codexKept is a socket to the backend and what its last turn sent and
// got back.
type codexKept struct {
c *websocket.Conn
cancel context.CancelFunc
key string // "" for a turn that names no conversation
host string
header http.Header // the handshake's, as each reply carries them
opened time.Time
used time.Time
once sync.Once
timer *time.Timer
fields map[string]string
input []itemSum
output []itemSum
replyID string
}
// close ends the socket at once.
func (k *codexKept) close() {
k.once.Do(func() {
k.c.CloseNow()
k.cancel()
})
}
// closeNormally ends the socket with a normal close, as a turn's end.
func (k *codexKept) closeNormally() {
k.once.Do(func() {
go func() {
k.c.Close(websocket.StatusNormalClosure, "")
k.cancel()
}()
})
}
// codexKeepKey is req's conversation's key in codexKeepPool, "" for a
// request that names none.
func codexKeepKey(req *http.Request, body []byte) string {
sid, tid := req.Header.Get("Session-Id"), req.Header.Get("Thread-Id")
if sid == "" && tid == "" {
// Codex's prompt_cache_key is its thread's id
if sid = gjson.GetBytes(body, "prompt_cache_key").String(); sid == "" {
return ""
}
}
h := sha256.New()
for _, v := range []string{
req.URL.Scheme, req.URL.Host, req.URL.Path, req.Header.Get("Authorization"), req.Header.Get("Chatgpt-Account-Id"),
// a Responses provider's key may be said in either (#5781)
req.Header.Get("X-Api-Key"), req.Header.Get("Api-Key"),
sid, tid, req.Header.Get("X-Openai-Subagent"), req.Header.Get("X-Codex-Parent-Thread-Id"),
} {
h.Write([]byte(v))
h.Write([]byte{0})
}
return hex.EncodeToString(h.Sum(nil))
}
// codexKeepTake takes key's kept socket out of the pool, nil for none.
func codexKeepTake(key string) *codexKept {
codexKeepMu.Lock()
k := codexKeepPool[key]
if k != nil {
delete(codexKeepPool, key)
k.timer.Stop()
}
codexKeepMu.Unlock()
if k != nil && time.Since(k.opened) >= codexKeepAge {
k.close()
return nil
}
return k
}
// codexKeepPut keeps k for its conversation's next turn, in place of one
// kept before it.
func codexKeepPut(k *codexKept) {
if k.key == "" || time.Since(k.opened) >= codexKeepAge {
k.close()
return
}
k.used = time.Now()
codexKeepMu.Lock()
old := codexKeepPool[k.key]
if old != nil {
old.timer.Stop()
}
codexKeepPool[k.key] = k
k.timer = time.AfterFunc(codexKeepIdle, func() { codexKeepDrop(k) })
var evict *codexKept
if len(codexKeepPool) > codexKeepMax {
for _, e := range codexKeepPool {
if evict == nil || e.used.Before(evict.used) {
evict = e
}
}
delete(codexKeepPool, evict.key)
evict.timer.Stop()
}
codexKeepMu.Unlock()
if old != nil && old != k {
old.close()
}
if evict != nil {
evict.close()
}
}
// codexKeepDrop lets k go if it is still kept.
func codexKeepDrop(k *codexKept) {
codexKeepMu.Lock()
kept := codexKeepPool[k.key] == k
if kept {
delete(codexKeepPool, k.key)
}
codexKeepMu.Unlock()
if kept {
k.close()
}
}
// codexKeepDrain lets every kept socket go.
func codexKeepDrain() {
codexKeepMu.Lock()
all := make([]*codexKept, 0, len(codexKeepPool))
for key, k := range codexKeepPool {
k.timer.Stop()
all = append(all, k)
delete(codexKeepPool, key)
}
codexKeepMu.Unlock()
for _, k := range all {
k.close()
}
}
// codexKeptCount is how many sockets are kept.
func codexKeptCount() int {
codexKeepMu.Lock()
defer codexKeepMu.Unlock()
return len(codexKeepPool)
}
// codexTurnShape is what of a turn the next one is compared with.
type codexTurnShape struct {
fields map[string]string
input []json.RawMessage
sums []itemSum
}
// codexShape is body's shape, false for a body that is not continued
// from: one whose input isn't a list of items, or that names a previous
// response itself.
func codexShape(body []byte) (*codexTurnShape, bool) {
var top map[string]json.RawMessage
if json.Unmarshal(body, &top) != nil {
return nil, false
}
if _, ok := top["previous_response_id"]; ok {
return nil, false
}
var input []json.RawMessage
if json.Unmarshal(top["input"], &input) != nil {
return nil, false
}
s := &codexTurnShape{fields: map[string]string{}, input: input, sums: make([]itemSum, len(input))}
for k, v := range top {
switch k {
case "input", "client_metadata", "stream_options":
continue
}
var b bytes.Buffer
if json.Compact(&b, v) != nil {
return nil, false
}
s.fields[k] = b.String()
}
for i, it := range input {
sum, ok := codexSocketItem(it)
if !ok {
return nil, false
}
s.sums[i] = sum
}
return s, true
}
// codexSocketItem is an item's digest as the backend and Codex both have it:
// with neither its own id nor status (Codex sends its items back without
// ids the backend gave), and nothing null, an empty list, annotations or
// logprobs at any depth (Codex keeps none of them).
func codexSocketItem(raw []byte) (itemSum, bool) {
var v any
d := json.NewDecoder(bytes.NewReader(raw))
d.UseNumber()
if d.Decode(&v) != nil {
return itemSum{}, false
}
m, ok := v.(map[string]any)
if !ok {
return itemSum{}, false
}
delete(m, "id")
delete(m, "status")
b, err := json.Marshal(codexSocketItemBare(m))
if err != nil {
return itemSum{}, false
}
return sha256.Sum256(b), true
}
func codexSocketItemBare(v any) any {
switch x := v.(type) {
case map[string]any:
for k, e := range x {
if k == "annotations" || k == "logprobs" || e == nil {
delete(x, k)
continue
}
if l, ok := e.([]any); ok && len(l) == 0 {
delete(x, k)
continue
}
x[k] = codexSocketItemBare(e)
}
case []any:
for i, e := range x {
x[i] = codexSocketItemBare(e)
}
}
return v
}
// delta is body as its new input after previous_response_id, when s
// carries on from k's last turn.
func (k *codexKept) delta(body []byte, s *codexTurnShape) ([]byte, bool) {
if k.replyID == "" || k.fields == nil || len(s.fields) != len(k.fields) {
return nil, false
}
for f, v := range k.fields {
if s.fields[f] != v {
return nil, false
}
}
n := len(k.input) + len(k.output)
if len(s.sums) < n {
return nil, false
}
for i, sum := range k.input {
if s.sums[i] != sum {
return nil, false
}
}
for i, sum := range k.output {
if s.sums[len(k.input)+i] != sum {
return nil, false
}
}
in := gjson.GetBytes(body, "input")
if !in.IsArray() || in.Index <= 0 {
return nil, false
}
rest := []byte{'['}
for i, it := range s.input[n:] {
if i > 0 {
rest = append(rest, ',')
}
rest = append(rest, it...)
}
rest = append(rest, ']')
out := make([]byte, 0, len(body)-len(in.Raw)+len(rest))
out = append(out, body[:in.Index]...)
out = append(out, rest...)
out = append(out, body[in.Index+len(in.Raw):]...)
id, _ := json.Marshal(k.replyID)
return withField(out, "previous_response_id", id), true
}
// withField is body, a JSON object, with key set to raw ahead of the rest.
func withField(body []byte, key string, raw []byte) []byte {
rest := bytes.TrimSpace(body[1:])
out := make([]byte, 0, len(body)+len(key)+len(raw)+4)
out = append(out, '{')
k, _ := json.Marshal(key)
out = append(out, k...)
out = append(out, ':')
out = append(out, raw...)
if len(rest) > 0 && rest[0] != '}' {
out = append(out, ',')
}
return append(out, rest...)
}
// codexReplyItem is a response.output_item.done frame's item's digest.
func codexReplyItem(frame []byte) (itemSum, bool) {
it := gjson.GetBytes(frame, "item")
if !it.IsObject() {
return itemSum{}, false
}
return codexSocketItem([]byte(it.Raw))
}