返回 DeepSeek-Reasonix
tool_execution_wire_test.go
根目录 / internal / provider / openai / tool_execution_wire_test.go
1 package openai
2
3 import (
4 "encoding/json"
5 "strings"
6 "testing"
7
8 "reasonix/internal/provider"
9 )
10
11 func TestBuildRequestExcludesToolExecution(t *testing.T) {
12 code := 1
13 // Agent applies ModelMessages before Stream; mirror that order.
14 msgs := provider.ModelMessages([]provider.Message{
15 {Role: provider.RoleUser, Content: "run tests"},
16 {Role: provider.RoleAssistant, ToolCalls: []provider.ToolCall{
17 {ID: "call_1", Name: "bash", Arguments: `{"command":"go test ./..."}`},
18 }},
19 {
20 Role: provider.RoleTool, ToolCallID: "call_1", Name: "bash", Content: "FAIL",
21 ToolExecution: &provider.ToolExecution{
22 Kind: "shell", Shell: "bash", State: "failed", ExitCode: &code,
23 FailurePhase: "execution", OutputTail: "中文stderr-marker-must-not-leak",
24 MutationRisk: "may_be_partial", Verification: "failed",
25 },
26 },
27 })
28 // Also prove Sanitize-only path (without ModelMessages) still cannot emit
29 // local fields because chatMessage is a closed wire struct.
30 withMeta := []provider.Message{
31 {Role: provider.RoleUser, Content: "hi"},
32 {Role: provider.RoleAssistant, ToolCalls: []provider.ToolCall{{ID: "c2", Name: "bash", Arguments: `{"command":"false"}`}}},
33 {Role: provider.RoleTool, ToolCallID: "c2", Name: "bash", Content: "x",
34 ToolExecution: &provider.ToolExecution{Kind: "shell", OutputTail: "中文stderr-marker-must-not-leak"}},
35 }
36
37 for _, tc := range []struct {
38 name string
39 msgs []provider.Message
40 }{
41 {name: "after ModelMessages", msgs: msgs},
42 {name: "raw with metadata", msgs: withMeta},
43 } {
44 t.Run(tc.name, func(t *testing.T) {
45 req := (&client{model: "deepseek-v4"}).buildRequest(provider.Request{Messages: tc.msgs})
46 body, err := json.Marshal(req)
47 if err != nil {
48 t.Fatal(err)
49 }
50 s := string(body)
51 for _, banned := range []string{"tool_execution", "outputTail", "中文stderr-marker-must-not-leak", "failurePhase", "mutationRisk"} {
52 if strings.Contains(s, banned) {
53 t.Fatalf("openai wire leaked %q: %s", banned, s)
54 }
55 }
56 if !strings.Contains(s, `"role":"tool"`) {
57 t.Fatalf("missing tool role: %s", s)
58 }
59 })
60 }
61 }
62
63 func TestBuildRequestStableWhenLocalExecutionAdded(t *testing.T) {
64 base := []provider.Message{
65 {Role: provider.RoleUser, Content: "run"},
66 {Role: provider.RoleAssistant, ToolCalls: []provider.ToolCall{{ID: "c1", Name: "bash", Arguments: `{"command":"true"}`}}},
67 {Role: provider.RoleTool, ToolCallID: "c1", Name: "bash", Content: "ok"},
68 }
69 code := 0
70 withMeta := append([]provider.Message(nil), base...)
71 withMeta[2].ToolExecution = &provider.ToolExecution{
72 Kind: "shell", Shell: "bash", State: "completed", ExitCode: &code, OutputTail: "noise",
73 }
74 // Same ModelMessages strip the agent uses.
75 a, err := json.Marshal((&client{model: "deepseek-v4"}).buildRequest(provider.Request{Messages: provider.ModelMessages(base)}))
76 if err != nil {
77 t.Fatal(err)
78 }
79 b, err := json.Marshal((&client{model: "deepseek-v4"}).buildRequest(provider.Request{Messages: provider.ModelMessages(withMeta)}))
80 if err != nil {
81 t.Fatal(err)
82 }
83 if string(a) != string(b) {
84 t.Fatalf("openai request diverged after local execution metadata\nbase=%s\nmeta=%s", a, b)
85 }
86 }
87
87 lines GO