Codex offers its collaboration tools in a namespace, and marks spawn_agent's message "encrypted". On a vendor serving the Responses API natively the request is relayed as it came, and the vendor's reply went back as it came too: the function_call for collaboration.spawn_agent reached Codex with its plain-text arguments and no encrypted_function_args field at all. Codex reads a namespaced call as DirectPlaintextMessage only when that field is there and empty (tools/router.rs, rust-v0.163.0), so it took the task for sealed, wrapped the plain text in an encrypted_content of its own, and the subagent — asked over that — replied "tell me the task" and did nothing. The translated path has said "not sealed" since #613 (callTo's encrypted_function_args []), and the Grok and Zed relays have had it through nsTidy since #404; the relay to any other vendor is the branch left out. What changed: the namespaced tools a Responses request offers are now read on every relay, not only Grok's and Zed's, and a call that comes back with its namespace named is answered with the empty encrypted_function_args list when it carries none. A call carrying a seal — OpenAI's own, or any upstream that answers the mark — goes back with that seal untouched, and calls to tools outside every namespace say nothing about seals either way. The field Codex replays of it in a later turn's history is still what forVendor leaves out of what a non-OpenAI vendor is asked (openaiOnly), so the loop closes without the vendor ever seeing it. TestNativeRelayNamespacedCallSaysUnsealed relays a request with the collaboration namespace to a stub vendor whose reply carries the call streamed and whole, and reads every function_call Codex would see: both shapes say the arguments are plain text, and the namespace went up as Codex sent it (the request was relayed, not translated). Without the fix the call arrives with no encrypted_function_args. TestNativeRelayLeavesOtherCallsBe holds a sealed call's seal and leaves a plain call alone. Run: go test ./internal/gateway -count=1 ok (322s); the new tests and the #404/#613 namespace tests green with and without -race; gofmt -l clean; go vet of the package clean; CGO_ENABLED=0 go build -tags nogui for darwin/arm64, linux/amd64 and windows/amd64. Not tried against the reporter's vendor; the stub's reply is the shape their probe recorded (namespace and plain arguments kept, no encrypted list). Generated-by: GLM-5.3-Flash (ZCode) Landed from ggbdpq's branch (patch posted on the issue; sha256/patch-id checked against the issue) by yetone, rebased on origin/main 2026-11-03. Run at the pushed tree, on mote (macOS): go test -tags nogui -count=1 of internal/gateway with -run over this batch's tests and their families (PlainSchema, Antigravity, AutoMode, WebP, ClaudeTUI, NativeRelay, Namespace, Grok, Zed, Codex, ClinePass, rewrite) ok; -race -count=3 of the new tests ok; GOOS=linux and GOOS=windows go vet of internal/gateway and gofmt -l clean. The full gateway-and-importers run, started at an earlier base, was still running at the push; its reds get issues. Changed in review: the widening is to every Responses relay except OpenAI's own (openai.com, Azure) and a ChatGPT account (accountAgent codex), whose replies go back as they came; OpenAI answers the seal itself, and this does not add the empty list to its replies. TestSealsItsOwnIsOpenAIOnly holds the exclusion (sealsItsOwn); with the openai.com and Azure checks taken out its OpenAI and Azure rows go red. Broken by hand: the product change reverted, TestNativeRelayNamespacedCallSaysUnsealed red; restored, green. Not tried against the reporter's vendor or a real Codex V2 subagent.
224 lines
6.2 KiB
Go
224 lines
6.2 KiB
Go
package gateway
|
|
|
|
// PLUGIN-SERVED (see AGENTS.md): Grok ("grok") is a retired built-in
|
|
// subscription, served by its plugin alone,
|
|
// @magpie-community/opencode-grok-auth (provider.BuiltinRetired). Its
|
|
// sign-in, models, requests and usage are the plugin's; what is left here
|
|
// reads the accounts signed in to the built-in for the move onto the plugin
|
|
// (migrate_side.go), installs and finds the Grok CLI the plugin signs in
|
|
// with, and shapes the plugin's request bodies (grokBody). A fix belongs in
|
|
// the plugin (github.com/magpie-community/plugins, packages/grok), with the
|
|
// mover's min raised in internal/provider/migrate_side.go.
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// Codex groups some tools in a namespace (collaboration's spawn_agent,
|
|
// codex_app's set_thread_title), which a Grok subscription's backend turns
|
|
// a request away over. Its functions go to Grok flat (provider's grokBody,
|
|
// collaboration__spawn_agent) as they go to a backend magpie translates
|
|
// for, and a call the model makes to one comes back to Codex under its own
|
|
// name and namespace (#404).
|
|
|
|
// namespacedIn is the namespaced functions a Responses request offers or
|
|
// has called, by the flat name Grok is given them under.
|
|
func namespacedIn(body []byte) map[string]nsTool {
|
|
if !bytes.Contains(body, []byte(`"namespace"`)) {
|
|
return nil
|
|
}
|
|
var q struct {
|
|
Tools []rTool `json:"tools"`
|
|
Input json.RawMessage `json:"input"` // a list, or the text alone
|
|
}
|
|
if json.Unmarshal(body, &q) != nil {
|
|
return nil
|
|
}
|
|
var items []struct {
|
|
Name string `json:"name"`
|
|
Namespace string `json:"namespace"`
|
|
}
|
|
json.Unmarshal(q.Input, &items)
|
|
named := map[string]nsTool{}
|
|
add := func(space, name string) {
|
|
if space != "" && space != liteNamespace && name != "" {
|
|
named[flatName(space, name)] = nsTool{Namespace: space, Name: name}
|
|
}
|
|
}
|
|
for _, t := range q.Tools {
|
|
if t.Type != "namespace" {
|
|
continue
|
|
}
|
|
for _, nt := range t.Tools {
|
|
if nt.Type == "function" {
|
|
add(t.Name, nt.Name)
|
|
}
|
|
}
|
|
}
|
|
for _, it := range items {
|
|
add(it.Namespace, it.Name)
|
|
}
|
|
if len(named) == 0 {
|
|
return nil
|
|
}
|
|
return named
|
|
}
|
|
|
|
// nsTidy gives, in a relayed Responses reply, the model's calls to a
|
|
// namespaced function their name and namespace back. A stream's lines pass
|
|
// as they came unless one carries such a call; a reply that isn't one is
|
|
// held whole and gone through at the end.
|
|
type nsTidy struct {
|
|
named map[string]nsTool
|
|
// back maps a declared tool's name a ChatGPT account was given in its
|
|
// place to the agent's own (codexRenamedIn)
|
|
back map[string]string
|
|
sse bool
|
|
buf []byte
|
|
}
|
|
|
|
func (t *nsTidy) write(b []byte) []byte {
|
|
t.buf = append(t.buf, b...)
|
|
if !t.sse {
|
|
return nil
|
|
}
|
|
i := bytes.LastIndexByte(t.buf, '\n')
|
|
if i < 0 {
|
|
return nil
|
|
}
|
|
out := t.lines(t.buf[:i+1])
|
|
t.buf = append(t.buf[:0], t.buf[i+1:]...)
|
|
return out
|
|
}
|
|
|
|
func (t *nsTidy) flush() []byte {
|
|
b := t.buf
|
|
t.buf = nil
|
|
if !t.sse {
|
|
if out, ok := t.restore(b); ok {
|
|
return out
|
|
}
|
|
return b
|
|
}
|
|
return t.lines(b)
|
|
}
|
|
|
|
func (t *nsTidy) lines(b []byte) []byte {
|
|
if !t.carries(b) {
|
|
return append([]byte(nil), b...)
|
|
}
|
|
var out []byte
|
|
for len(b) > 0 {
|
|
line := b
|
|
if i := bytes.IndexByte(b, '\n'); i >= 0 {
|
|
line, b = b[:i+1], b[i+1:]
|
|
} else {
|
|
b = nil
|
|
}
|
|
body := bytes.TrimRight(line, "\r\n")
|
|
data, ok := bytes.CutPrefix(body, []byte("data:"))
|
|
if !ok || !t.carries(data) {
|
|
out = append(out, line...)
|
|
continue
|
|
}
|
|
nb, ok := t.restore(data)
|
|
if !ok {
|
|
out = append(out, line...)
|
|
continue
|
|
}
|
|
out = append(append(append(out, "data: "...), nb...), line[len(body):]...)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// restore is an event, or a whole reply, with its namespaced calls named
|
|
// as Codex knows them, and whether there was one.
|
|
func (t *nsTidy) restore(data []byte) ([]byte, bool) {
|
|
dec := json.NewDecoder(bytes.NewReader(data))
|
|
dec.UseNumber()
|
|
var ev map[string]any
|
|
if dec.Decode(&ev) != nil || ev == nil {
|
|
return nil, false
|
|
}
|
|
changed := false
|
|
if it, _ := ev["item"].(map[string]any); t.call(it) {
|
|
changed = true
|
|
}
|
|
res, _ := ev["response"].(map[string]any)
|
|
if res == nil && ev["object"] == "response" {
|
|
res = ev
|
|
}
|
|
if res != nil {
|
|
out, _ := res["output"].([]any)
|
|
for _, it := range out {
|
|
if im, _ := it.(map[string]any); t.call(im) {
|
|
changed = true
|
|
}
|
|
}
|
|
}
|
|
if !changed {
|
|
return nil, false
|
|
}
|
|
nb, err := marshalPlain(ev)
|
|
if err != nil {
|
|
return nil, false
|
|
}
|
|
return nb, true
|
|
}
|
|
|
|
// carries reports whether b may hold a call restore names again: a
|
|
// function call, or a custom tool's to one renamed.
|
|
func (t *nsTidy) carries(b []byte) bool {
|
|
return bytes.Contains(b, []byte(`"function_call"`)) || t.back != nil && bytes.Contains(b, []byte(`"custom_tool_call"`))
|
|
}
|
|
|
|
// call names a function_call to a namespaced function by its name and
|
|
// namespace, or a call to a tool renamed for a ChatGPT account by the
|
|
// agent's name, and reports whether it was one. A call whose namespace
|
|
// the upstream said itself (a vendor asked as Codex sends it, #3183)
|
|
// needs no naming, only the word that its arguments are not sealed: Codex
|
|
// reads a namespaced call without the empty list as sealed, and the
|
|
// plain-text task would reach its subagent as ciphertext.
|
|
func (t *nsTidy) call(it map[string]any) bool {
|
|
if it == nil || it["type"] != "function_call" && it["type"] != "custom_tool_call" {
|
|
return false
|
|
}
|
|
name, _ := it["name"].(string)
|
|
if ns, _ := it["namespace"].(string); ns != "" {
|
|
for _, q := range t.named {
|
|
if q.Namespace == ns && q.Name == name {
|
|
if _, ok := it["encrypted_function_args"]; !ok {
|
|
it["encrypted_function_args"] = []any{}
|
|
return true
|
|
}
|
|
break
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
if n, ok := t.back[name]; ok {
|
|
it["name"] = n
|
|
return true
|
|
}
|
|
if it["type"] != "function_call" {
|
|
return false
|
|
}
|
|
if _, ok := t.named[name]; !ok {
|
|
return false
|
|
}
|
|
callTo(it, name, t.named)
|
|
return true
|
|
}
|
|
|
|
// sealsItsOwn says p is OpenAI's own Responses API (openai.com, Azure) or a
|
|
// ChatGPT account: it answers a namespaced call's seal itself, and its
|
|
// replies go back as they came, never given the empty
|
|
// encrypted_function_args a relay's are (#3183).
|
|
func sealsItsOwn(p provider.Provider) bool {
|
|
return accountAgent(p) == "codex" || strings.HasSuffix(p.Host(), "openai.com") || p.IsAzure()
|
|
}
|