diff --git a/internal/gateway/grok_namespace.go b/internal/gateway/grok_namespace.go index ee533dd4d..279abe0e2 100644 --- a/internal/gateway/grok_namespace.go +++ b/internal/gateway/grok_namespace.go @@ -13,6 +13,7 @@ package gateway import ( "bytes" "encoding/json" + "slices" "strings" "github.com/yetone/magpie/internal/provider" @@ -180,9 +181,9 @@ func (t *nsTidy) carries(b []byte) bool { // 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. +// needs no naming, only the word which of its arguments are sealed +// (sealedArgs): Codex reads a namespaced call without the 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 @@ -192,8 +193,11 @@ func (t *nsTidy) call(it map[string]any) bool { 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 + args, _ := it["arguments"].(string) + if sealed, ok := sealedArgs(args); ok { + it["encrypted_function_args"] = sealed + return true + } } break } @@ -214,6 +218,36 @@ func (t *nsTidy) call(it map[string]any) bool { return true } +// sealedArgs is the encrypted_function_args a relayed call's arguments +// say: the names of those the ChatGPT backend sealed (a "gAAAAA…" token, +// nativeSealedAgentPayload), [] when none is. A relay of that backend +// (CPA, Sub2API) seals spawn_agent's, send_message's and followup_task's +// message as the backend does, but leaves the list out; given [] Codex +// handed the token to the other agent as plain text, and a subagent's +// task, or its result to its lead, arrived as ciphertext neither could +// read (brethhbbm on X, oevery and WilliamK7 on #3183). Given the names, +// Codex delivers it sealed, which the relay opens again. ok is false when +// the arguments say nothing yet (a stream's output_item.added, before +// they come) or aren't a JSON object: the call goes on as it came. +func sealedArgs(args string) ([]any, bool) { + var a map[string]any + if strings.TrimSpace(args) == "" || json.Unmarshal([]byte(args), &a) != nil || a == nil { + return nil, false + } + sealed := []any{} + keys := make([]string, 0, len(a)) + for k := range a { + keys = append(keys, k) + } + slices.Sort(keys) + for _, k := range keys { + if v, _ := a[k].(string); nativeSealedAgentPayload.MatchString(v) { + sealed = append(sealed, k) + } + } + return sealed, 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 diff --git a/internal/gateway/native_relay_collab_test.go b/internal/gateway/native_relay_collab_test.go index 765492ac0..fd3940e86 100644 --- a/internal/gateway/native_relay_collab_test.go +++ b/internal/gateway/native_relay_collab_test.go @@ -117,6 +117,14 @@ func TestNativeRelayNamespacedCallSaysUnsealed(t *testing.T) { t.Fatalf("stream %v: %s", stream, rec.Body) } for _, it := range items { + if it["arguments"] == "" { + // a stream's first item has no arguments yet, so + // nothing can say whether they are sealed + if v, ok := it["encrypted_function_args"]; ok { + t.Fatalf("stream %v: the argument-less item was given %v", stream, v) + } + continue + } sealed, ok := it["encrypted_function_args"].([]any) if it["name"] != "spawn_agent" || it["namespace"] != "collaboration" || !ok || len(sealed) != 0 { t.Fatalf("stream %v: Codex was given %v", stream, it) diff --git a/internal/gateway/relay_sealed_args_test.go b/internal/gateway/relay_sealed_args_test.go new file mode 100644 index 000000000..ee65ba5cc --- /dev/null +++ b/internal/gateway/relay_sealed_args_test.go @@ -0,0 +1,176 @@ +package gateway + +import ( + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + + "github.com/yetone/magpie/internal/provider" +) + +// relaySealedToken stands for what the ChatGPT backend puts in a +// collaboration call's message when it seals it (a Fernet token). +const relaySealedToken = "gAAAAABpTestSealedResult_0123456789abcdefABCDEF-_xyz==" + +// codexSendTools is Codex's collaboration namespace with send_message, as +// a subagent is offered it. +const codexSendTools = `"tools":[ + {"type":"function","name":"exec_command","parameters":{"type":"object"}}, + {"type":"namespace","name":"collaboration","description":"Tools in the collaboration namespace.","tools":[ + {"type":"function","name":"send_message","description":"Sends a message.","parameters":{"type":"object","properties":{"target":{"type":"string"},"message":{"type":"string","encrypted":true}}}}, + {"type":"function","name":"spawn_agent","description":"Spawns an agent.","parameters":{"type":"object","properties":{"message":{"type":"string","encrypted":true}}}} + ]} +]` + +// brethhbbm on X ("codex subagent 返回的加密内容主agent 无法解析"), and +// oevery and WilliamK7 on #3183: a Responses relay of the ChatGPT backend +// (CPA, Sub2API) seals a subagent's send_message as the backend does but +// leaves encrypted_function_args out. magpie gave every such call the +// empty list, saying the message was plain text, so Codex handed the lead +// the token as text and the lead could read nothing of its subagent's +// result. A call whose message is sealed now names it; a plain one still +// gets the empty list, and a stream's first, argument-less item is left +// as it came. +func TestRelaySealedMessageNamedSealed(t *testing.T) { + fresh(t) + var mu sync.Mutex + var sent []string + reply := `{"type":"function_call","id":"fc_1","call_id":"c9","name":"send_message","namespace":"collaboration","arguments":"{\"target\":\"/root\",\"message\":\"` + relaySealedToken + `\"}","status":"completed"}` + up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + b, _ := io.ReadAll(r.Body) + mu.Lock() + sent = append(sent, string(b)) + mu.Unlock() + var q map[string]any + json.Unmarshal(b, &q) + done := `{"id":"r1","object":"response","status":"completed","output":[` + reply + `],"usage":{"input_tokens":5,"output_tokens":3}}` + if q["stream"] != true { + w.Header().Set("Content-Type", "application/json") + io.WriteString(w, done) + return + } + w.Header().Set("Content-Type", "text/event-stream") + io.WriteString(w, sse( + `event: response.created`+"\n"+`data: {"type":"response.created","response":{"id":"r1","object":"response","status":"in_progress","output":[]}}`, + `event: response.output_item.added`+"\n"+`data: {"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"c9","name":"send_message","namespace":"collaboration","arguments":""}}`, + `event: response.output_item.done`+"\n"+`data: {"type":"response.output_item.done","output_index":0,"item":`+reply+`}`, + `event: response.completed`+"\n"+`data: {"type":"response.completed","response":`+done+`}`)) + })) + defer up.Close() + if err := provider.Save(provider.Provider{ID: "cpa", Name: "CPA", Key: "k", Models: []string{"gpt-6-astra"}, Responses: up.URL + "/v1"}); err != nil { + t.Fatal(err) + } + s := New() + for _, stream := range []bool{true, false} { + body := `{"model":"cpa/gpt-6-astra","stream":` + map[bool]string{true: "true", false: "false"}[stream] + `,` + codexSendTools + `,"input":[` + plainTask + `]}` + rec := httptest.NewRecorder() + req := httptest.NewRequest("POST", "/v1/responses", strings.NewReader(body)) + req.Header.Set("session_id", "worker-1") + req.Header.Set("x-codex-parent-thread-id", "lead-1") + s.Handler().ServeHTTP(rec, req) + if rec.Code != 200 { + t.Fatalf("stream %v: %d %s", stream, rec.Code, rec.Body) + } + var items []map[string]any + var added map[string]any + if stream { + for _, line := range strings.Split(rec.Body.String(), "\n") { + data, ok := strings.CutPrefix(line, "data: ") + if !ok { + continue + } + var ev struct { + Type string `json:"type"` + Item map[string]any `json:"item"` + Response struct { + Output []map[string]any `json:"output"` + } `json:"response"` + } + if err := json.Unmarshal([]byte(data), &ev); err != nil { + t.Fatalf("%v: %s", err, data) + } + if ev.Type == "response.output_item.added" { + added = ev.Item + continue + } + if ev.Item != nil { + items = append(items, ev.Item) + } + items = append(items, ev.Response.Output...) + } + if added == nil { + t.Fatalf("no output_item.added: %s", rec.Body) + } + if v, ok := added["encrypted_function_args"]; ok { + t.Fatalf("the argument-less first item was given %v", v) + } + } else { + var res struct { + Output []map[string]any `json:"output"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &res); err != nil { + t.Fatalf("%v: %s", err, rec.Body) + } + items = res.Output + } + if len(items) == 0 || stream && len(items) != 2 { + t.Fatalf("stream %v: %s", stream, rec.Body) + } + for _, it := range items { + sealed, _ := it["encrypted_function_args"].([]any) + if len(sealed) != 1 || sealed[0] != "message" { + t.Fatalf("stream %v: Codex was given %v, the sealed message read as plain text", stream, it) + } + if args, _ := it["arguments"].(string); !strings.Contains(args, relaySealedToken) { + t.Fatalf("stream %v: the token was changed: %v", stream, it) + } + } + } + + // Codex then hands the lead the message sealed, and the lead's turn, + // on the same relay, carries it to the relay as it came: it opens it + mu.Lock() + sent = nil + mu.Unlock() + sealed := `{"type":"agent_message","author":"/root/worker","recipient":"/root","content":[{"type":"input_text","text":"Message Type: MESSAGE\nTask name: /root\nSender: /root/worker\nPayload:\n"},{"type":"encrypted_content","encrypted_content":"` + relaySealedToken + `"}]}` + for i, input := range []string{leadAsk, leadAsk + `,` + sealed} { + body := `{"model":"cpa/gpt-6-astra","stream":false,` + codexSendTools + `,"input":[` + input + `]}` + rec := httptest.NewRecorder() + req := httptest.NewRequest("POST", "/v1/responses", strings.NewReader(body)) + req.Header.Set("session_id", "lead-1") + s.Handler().ServeHTTP(rec, req) + if rec.Code != 200 { + t.Fatalf("lead turn %d: %d %s", i, rec.Code, rec.Body) + } + } + mu.Lock() + defer mu.Unlock() + if len(sent) != 2 || !strings.Contains(sent[1], `"encrypted_content":"`+relaySealedToken+`"`) { + t.Fatalf("the lead's turn reached the relay as %q", sent) + } +} + +// A plain message on a relay still says so, and one whose arguments +// aren't an object is left as it came. +func TestSealedArgs(t *testing.T) { + for _, tc := range []struct { + args string + want []any + ok bool + }{ + {`{"message":"go"}`, []any{}, true}, + {`{"target":"/root","message":"` + relaySealedToken + `"}`, []any{"message"}, true}, + {`{"message":"gAAAAA is how a token starts"}`, []any{}, true}, + {``, nil, false}, + {relaySealedToken, nil, false}, + } { + got, ok := sealedArgs(tc.args) + if ok != tc.ok || len(got) != len(tc.want) || len(got) == 1 && got[0] != tc.want[0] { + t.Errorf("sealedArgs(%q) = %v %v, want %v %v", tc.args, got, ok, tc.want, tc.ok) + } + } +}