Repository navigation
Expand file tree
/
Copy pathcodemode_decision_test.go
More file actions
190 lines (172 loc) · 6.5 KB
/
Copy pathcodemode_decision_test.go
File metadata and controls
190 lines (172 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package agent
import (
"context"
"errors"
"strings"
"testing"
"github.com/Protocol-Lattice/go-agent/src/memory"
"github.com/universal-tool-calling-protocol/go-utcp/src/plugins/codemode"
utcpTools "github.com/universal-tool-calling-protocol/go-utcp/src/tools"
)
type stubCodeModeDecisionLayer struct {
decision ToolDecision
err error
calls int
input ToolDecisionInput
}
func (s *stubCodeModeDecisionLayer) DecideTool(_ context.Context, input ToolDecisionInput) (ToolDecision, error) {
s.calls++
s.input = input
return s.decision, s.err
}
func TestCodeModeJevDecisionRestrictsGeneratedTool(t *testing.T) {
model := &dynamicStubModel{responses: map[string]string{
"A separate decision model (Jev) already selected the exact next UTCP tool": "{\"tools\":[\"beta\"],\"code\":\"let result = codemode.CallTool(\\\"beta\\\", {\\\"input\\\": \\\"hello\\\"}); result\",\"stream\":false}",
}}
client := &stubUTCPClient{searchTools: []utcpTools.Tool{
{
Name: "alpha",
Description: "Alpha tool",
Inputs: utcpTools.ToolInputOutputSchema{
Type: "object",
Properties: map[string]any{"input": map[string]any{"type": "string"}},
Required: []string{"input"},
},
},
{
Name: "beta",
Description: "Beta tool",
Inputs: utcpTools.ToolInputOutputSchema{
Type: "object",
Properties: map[string]any{"input": map[string]any{"type": "string"}},
Required: []string{"input"},
},
},
}}
layer := &stubCodeModeDecisionLayer{decision: ToolDecision{
UseTool: true,
ToolName: "beta",
Confidence: 0.94,
Provider: "TypeSafe",
RequestID: "gen-dec-test",
}}
a, err := New(Options{
Model: model,
Memory: memory.NewSessionMemory(&memory.MemoryBank{}, 4),
UTCPClient: client,
CodeMode: codemode.NewCodeModeUTCP(client, model),
CodeModePlannerModel: model,
CodeModeDecisionLayer: layer,
AllowUnsafeTools: true,
})
if err != nil {
t.Fatalf("New returned error: %v", err)
}
out, err := a.Generate(context.Background(), "session", "Run code with CodeMode using the beta tool with input hello.")
if err != nil {
t.Fatalf("Generate returned error: %v", err)
}
if layer.calls != 1 {
t.Fatalf("decision layer calls = %d, want 1", layer.calls)
}
if client.callCount != 1 || client.lastToolName != "beta" {
t.Fatalf("UTCP execution = count %d tool %q, want one beta call", client.callCount, client.lastToolName)
}
if _, ok := out.(codemode.CodeModeResult); !ok {
t.Fatalf("Generate output type = %T, want codemode.CodeModeResult", out)
}
foundBeta := false
for _, tool := range layer.input.Tools {
if tool.Name == "beta" {
foundBeta = true
break
}
}
if !foundBeta {
t.Fatalf("Jev candidates did not include beta: %#v", layer.input.Tools)
}
}
func TestCodeModeJevDecisionRejectsDifferentGeneratedTool(t *testing.T) {
model := &dynamicStubModel{responses: map[string]string{
"A separate decision model (Jev) already selected the exact next UTCP tool": "{\"tools\":[\"beta\"],\"code\":\"codemode.CallTool(\\\"alpha\\\", {\\\"input\\\": \\\"hello\\\"})\",\"stream\":false}",
}}
client := &stubUTCPClient{searchTools: []utcpTools.Tool{
{Name: "alpha", Description: "Alpha tool"},
{Name: "beta", Description: "Beta tool"},
}}
layer := &stubCodeModeDecisionLayer{decision: ToolDecision{UseTool: true, ToolName: "beta"}}
a, err := New(Options{
Model: model,
Memory: memory.NewSessionMemory(&memory.MemoryBank{}, 4),
UTCPClient: client,
CodeMode: codemode.NewCodeModeUTCP(client, model),
CodeModePlannerModel: model,
CodeModeDecisionLayer: layer,
AllowUnsafeTools: true,
})
if err != nil {
t.Fatalf("New returned error: %v", err)
}
_, err = a.Generate(context.Background(), "session", "Run code with CodeMode using beta.")
if err == nil || !strings.Contains(err.Error(), "generated code references") {
t.Fatalf("Generate error = %v, want selected-tool mismatch", err)
}
if client.callCount != 0 {
t.Fatalf("UTCP call count = %d, want 0 after mismatch", client.callCount)
}
}
func TestCodeModeLowConfidenceDoesNotBypassJevWithNativeFallback(t *testing.T) {
model := &dynamicStubModel{responses: map[string]string{
"You are a strict UTCP CodeMode planner and executor": "{\"tools\":[\"alpha\"],\"code\":\"codemode.CallTool(\\\"alpha\\\", {})\",\"stream\":false}",
}}
client := &stubUTCPClient{searchTools: []utcpTools.Tool{{Name: "alpha", Description: "Alpha tool"}}}
layer := &stubCodeModeDecisionLayer{err: &ToolDecisionConfidenceError{
Confidence: 0.42,
Minimum: 0.80,
}}
a, err := New(Options{
Model: model,
Memory: memory.NewSessionMemory(&memory.MemoryBank{}, 4),
UTCPClient: client,
CodeMode: codemode.NewCodeModeUTCP(client, model),
CodeModePlannerModel: model,
CodeModeDecisionLayer: layer,
AllowUnsafeTools: true,
})
if err != nil {
t.Fatalf("New returned error: %v", err)
}
_, err = a.Generate(context.Background(), "session", "Run code with CodeMode using alpha.")
if err == nil || !strings.Contains(err.Error(), "below minimum") {
t.Fatalf("Generate error = %v, want Jev confidence error", err)
}
if client.callCount != 0 {
t.Fatalf("UTCP call count = %d, want 0 when Jev is below confidence threshold", client.callCount)
}
}
func TestCodeModeDecisionLayerFailureFallsBackToNativePlanner(t *testing.T) {
model := &dynamicStubModel{responses: map[string]string{
"You are a strict UTCP CodeMode planner and executor": "{\"tools\":[\"alpha\"],\"code\":\"codemode.CallTool(\\\"alpha\\\", {})\",\"stream\":false}",
}}
client := &stubUTCPClient{searchTools: []utcpTools.Tool{{Name: "alpha", Description: "Alpha tool"}}}
layer := &stubCodeModeDecisionLayer{err: errors.New("decision backend unavailable")}
a, err := New(Options{
Model: model,
Memory: memory.NewSessionMemory(&memory.MemoryBank{}, 4),
UTCPClient: client,
CodeMode: codemode.NewCodeModeUTCP(client, model),
CodeModePlannerModel: model,
CodeModeDecisionLayer: layer,
AllowUnsafeTools: true,
})
if err != nil {
t.Fatalf("New returned error: %v", err)
}
_, err = a.Generate(context.Background(), "session", "Run code with CodeMode using alpha.")
if err != nil {
t.Fatalf("Generate returned error: %v", err)
}
if client.callCount != 1 || client.lastToolName != "alpha" {
t.Fatalf("fallback execution = count %d tool %q, want one alpha call", client.callCount, client.lastToolName)
}
}