-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtools_contract_test.go
More file actions
388 lines (360 loc) · 14.6 KB
/
Copy pathtools_contract_test.go
File metadata and controls
388 lines (360 loc) · 14.6 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package adaptor_test
import (
"context"
"errors"
"fmt"
"net"
"net/url"
"strings"
"testing"
adaptor "github.com/agent-dance/agent-adaptor"
"github.com/agent-dance/agent-adaptor/driver"
"github.com/agent-dance/agent-adaptor/mcp"
"github.com/agent-dance/agent-adaptor/memory"
"github.com/agent-dance/agent-adaptor/tool"
)
const hostedToolServerKey = "agent-adaptor-tools"
type hostedToolInput struct {
Value string `json:"value" jsonschema:"required"`
}
type hostedToolOutput struct {
Value string `json:"value"`
}
func hostedToolDefinition(name string, opts ...tool.Option) tool.Definition {
return tool.Define(name, "Echo a value for the hosted Tool contract test.",
func(_ context.Context, input hostedToolInput) (hostedToolOutput, error) {
return hostedToolOutput{Value: input.Value}, nil
}, opts...)
}
func toolCapableFake() *fakeDriver {
fake := newFakeDriver()
descriptor := fake.Descriptor()
descriptor.MCP = driver.MCPCapability{Supported: true, HTTP: true}
fake.descriptor = &descriptor
return fake
}
func TestWithToolsUsesStableExistingRuntimeMCPPipeline(t *testing.T) {
fake := toolCapableFake()
agent := adaptor.New(fake, adaptor.WithTools(
hostedToolDefinition("echo", tool.ReadOnly(), tool.Revision("echo/v1")),
))
t.Cleanup(func() {
if err := agent.Close(context.Background()); err != nil {
t.Errorf("Close: %v", err)
}
})
for run := 0; run < 2; run++ {
if _, err := agent.Run(context.Background(), "use echo"); err != nil {
t.Fatalf("run %d: %v", run+1, err)
}
}
first := fake.request(t, 0)
second := fake.request(t, 1)
for index, request := range []driver.Request{first, second} {
if len(request.MCP.Servers) != 1 {
t.Fatalf("request %d MCP servers = %+v, want one hosted server", index+1, request.MCP.Servers)
}
server := request.MCP.Servers[0]
if server.Key != hostedToolServerKey || server.Transport != driver.MCPTransportHTTP {
t.Errorf("request %d server = %+v, want hosted HTTP server", index+1, server)
}
if !strings.HasPrefix(server.URL, "http://127.0.0.1:") || server.BearerTokenEnvVar == "" {
t.Errorf("request %d endpoint/auth = %+v, want authenticated numeric loopback", index+1, server)
}
if len(request.Runtime.Ensured) != 1 || request.Runtime.Ensured[0].ReuseKey == "" {
t.Errorf("request %d runtime = %+v, want catalog fingerprint in ReuseKey", index+1, request.Runtime)
}
if len(request.Runtime.SecretEnv) != 1 || request.Runtime.SecretEnv[0].Value == "" {
t.Errorf("request %d secret env bindings = %d, want one non-empty private bearer binding", index+1, len(request.Runtime.SecretEnv))
}
}
if first.MCP.Servers[0].URL != second.MCP.Servers[0].URL ||
first.Runtime.Ensured[0].ReuseKey != second.Runtime.Ensured[0].ReuseKey ||
first.Runtime.SecretEnv[0] != second.Runtime.SecretEnv[0] ||
first.Runtime.Fingerprint == "" || first.Runtime.Fingerprint != second.Runtime.Fingerprint {
t.Fatal("hosted Tool runtime identity changed between Agent runs")
}
}
func TestWithToolsInvalidAndDuplicateDefinitionsFailBeforeDriver(t *testing.T) {
tests := []struct {
name string
definitions []tool.Definition
}{
{name: "invalid", definitions: []tool.Definition{hostedToolDefinition("")}},
{name: "duplicate", definitions: []tool.Definition{
hostedToolDefinition("echo"),
hostedToolDefinition("echo"),
}},
{name: "nil", definitions: []tool.Definition{nil}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fake := toolCapableFake()
agent := adaptor.New(fake, adaptor.WithTools(test.definitions...))
t.Cleanup(func() { _ = agent.Close(context.Background()) })
if _, err := agent.Run(context.Background(), "must not launch"); !errors.Is(err, tool.ErrInvalidDefinition) {
t.Fatalf("Run error = %v, want tool.ErrInvalidDefinition", err)
}
if fake.runCount() != 0 {
t.Fatalf("driver runs = %d, want 0", fake.runCount())
}
})
}
t.Run("later empty declaration clears earlier set", func(t *testing.T) {
fake := toolCapableFake()
agent := adaptor.New(fake,
adaptor.WithTools(hostedToolDefinition("")),
adaptor.WithTools(),
)
t.Cleanup(func() { _ = agent.Close(context.Background()) })
if _, err := agent.Run(context.Background(), "launch without hosted tools"); err != nil {
t.Fatalf("Run: %v", err)
}
if got := fake.request(t, 0).MCP.Servers; len(got) != 0 {
t.Fatalf("MCP servers = %+v, want explicit WithTools clear", got)
}
})
}
func TestWithToolsIsIndependentOfPerCallWithMCPClearAndDetectsCollision(t *testing.T) {
fake := toolCapableFake()
agent := adaptor.New(fake, adaptor.WithTools(
hostedToolDefinition("echo", tool.Revision("echo/v1")),
))
t.Cleanup(func() { _ = agent.Close(context.Background()) })
if _, err := agent.Run(context.Background(), "still has tools", adaptor.WithMCP()); err != nil {
t.Fatalf("WithMCP clear run: %v", err)
}
if got := fake.request(t, 0).MCP.Servers; len(got) != 1 || got[0].Key != hostedToolServerKey {
t.Fatalf("MCP servers after per-call clear = %+v, want hosted Tools server", got)
}
_, err := agent.Run(context.Background(), "collision",
adaptor.WithMCP(mcp.HTTP(hostedToolServerKey, "https://example.com/mcp")),
)
if !errors.Is(err, adaptor.ErrInvalidMCPConfig) {
t.Fatalf("collision error = %v, want ErrInvalidMCPConfig", err)
}
if fake.runCount() != 1 {
t.Fatalf("driver runs = %d, want collision rejected before second launch", fake.runCount())
}
}
func TestWithToolsRejectsBearerEnvAliasingFromMCPAndRunServices(t *testing.T) {
fake := toolCapableFake()
agent := adaptor.New(fake, adaptor.WithTools(
hostedToolDefinition("echo", tool.Revision("echo/v1")),
))
t.Cleanup(func() { _ = agent.Close(context.Background()) })
if _, err := agent.Run(context.Background(), "learn resolved transport"); err != nil {
t.Fatalf("initial Run: %v", err)
}
ownedEnv := fake.request(t, 0).MCP.Servers[0].BearerTokenEnvVar
if ownedEnv == "" {
t.Fatal("hosted Tool request has no bearer environment variable")
}
_, err := agent.Run(context.Background(), "must reject explicit alias",
adaptor.WithMCP(mcp.HTTP("external", "https://example.com/mcp", mcp.WithBearerTokenEnv(ownedEnv))),
)
if !errors.Is(err, adaptor.ErrInvalidMCPConfig) {
t.Fatalf("explicit MCP alias error = %v, want ErrInvalidMCPConfig", err)
}
provider := &fakeProvider{
name: "external",
log: &callLog{},
attachment: adaptor.RunAttachment{Services: []adaptor.ServiceRef{{
ID: "external",
Name: "external",
URL: "https://example.com/mcp",
MCP: &driver.MCPServerSpec{
Key: "external-runtime",
Transport: driver.MCPTransportHTTP,
URL: "https://example.com/mcp",
BearerTokenEnvVar: ownedEnv,
},
}}},
}
_, err = agent.Run(context.Background(), "must reject runtime alias", adaptor.WithRunServices(provider))
if !errors.Is(err, adaptor.ErrInvalidMCPConfig) {
t.Fatalf("runtime MCP alias error = %v, want ErrInvalidMCPConfig", err)
}
if fake.runCount() != 1 {
t.Fatalf("driver runs = %d, want only initial run", fake.runCount())
}
}
func TestWithToolsRequiresDriverHTTPMCPSupportBeforeLaunch(t *testing.T) {
fake := newFakeDriver()
descriptor := fake.Descriptor()
descriptor.MCP = driver.MCPCapability{Supported: true, Stdio: true}
fake.descriptor = &descriptor
agent := adaptor.New(fake, adaptor.WithTools(
hostedToolDefinition("echo", tool.Revision("echo/v1")),
))
t.Cleanup(func() { _ = agent.Close(context.Background()) })
if _, err := agent.Run(context.Background(), "must not launch"); !errors.Is(err, adaptor.ErrMCPTransportUnsupported) {
t.Fatalf("Run error = %v, want ErrMCPTransportUnsupported", err)
}
if fake.runCount() != 0 {
t.Fatalf("driver runs = %d, want 0", fake.runCount())
}
}
func TestThreadWithToolsRequiresRevisionAndReusesStableRuntime(t *testing.T) {
t.Run("missing revision fails closed", func(t *testing.T) {
fake := toolCapableFake()
agent := adaptor.New(fake,
adaptor.WithThreadStore(memory.NewStore()),
adaptor.WithTools(hostedToolDefinition("echo")),
)
t.Cleanup(func() { _ = agent.Close(context.Background()) })
if _, err := agent.Thread("thread").Run(context.Background(), "must not launch"); !errors.Is(err, adaptor.ErrThreadIncompatible) {
t.Fatalf("Thread.Run error = %v, want ErrThreadIncompatible", err)
}
if fake.runCount() != 0 {
t.Fatalf("driver runs = %d, want 0", fake.runCount())
}
})
t.Run("stable revision resumes", func(t *testing.T) {
fake := newSessionFake("tools")
descriptor := fake.Descriptor()
descriptor.MCP = driver.MCPCapability{Supported: true, HTTP: true}
fake.descriptor = &descriptor
agent := adaptor.New(fake,
adaptor.WithThreadStore(memory.NewStore()),
adaptor.WithTools(hostedToolDefinition("echo", tool.Revision("echo/v1"))),
)
t.Cleanup(func() { _ = agent.Close(context.Background()) })
thread := agent.Thread("thread")
if _, err := thread.Run(context.Background(), "first"); err != nil {
t.Fatalf("first: %v", err)
}
if _, err := thread.Run(context.Background(), "second"); err != nil {
t.Fatalf("second: %v", err)
}
first := fake.request(t, 0)
second := fake.request(t, 1)
if second.Session == nil || second.Session.State == nil || second.Session.State.ResumeID == "" {
t.Fatalf("second request did not resume: %+v", second.Session)
}
if first.MCP.Fingerprint != second.MCP.Fingerprint ||
first.Runtime.Ensured[0].ReuseKey != second.Runtime.Ensured[0].ReuseKey {
t.Fatal("Tool runtime compatibility identity changed between Thread turns")
}
})
}
func TestToolCatalogRevisionChangesThreadFingerprintAtStableEndpoint(t *testing.T) {
fake := newSessionFake("tools-revision")
descriptor := fake.Descriptor()
descriptor.MCP = driver.MCPCapability{Supported: true, HTTP: true}
fake.descriptor = &descriptor
store := memory.NewStore()
firstAgent := adaptor.New(fake,
adaptor.WithThreadStore(store),
adaptor.WithTools(hostedToolDefinition("echo", tool.Revision("echo/v1"))),
)
defer func() { _ = firstAgent.Close(context.Background()) }()
if _, err := firstAgent.Thread("thread").Run(context.Background(), "first"); err != nil {
t.Fatalf("first: %v", err)
}
first := fake.request(t, 0)
secondAgent := adaptor.New(fake,
adaptor.WithThreadStore(store),
adaptor.WithTools(hostedToolDefinition("echo", tool.Revision("echo/v2"))),
)
defer func() { _ = secondAgent.Close(context.Background()) }()
if _, err := secondAgent.Thread("thread").Run(context.Background(), "changed revision"); err != nil {
t.Fatalf("changed revision: %v", err)
}
second := fake.request(t, 1)
if first.MCP.Servers[0].URL != second.MCP.Servers[0].URL {
t.Fatal("process-wide hosted Tool URL changed; test requires one shared gateway")
}
if first.MCP.Servers[0].BearerTokenEnvVar == second.MCP.Servers[0].BearerTokenEnvVar || first.MCP.Fingerprint == second.MCP.Fingerprint {
t.Fatal("distinct Agents reused concrete hosted Tool credential transport identity")
}
if first.Runtime.Ensured[0].ReuseKey == second.Runtime.Ensured[0].ReuseKey {
t.Fatal("catalog revision did not change the deterministic runtime compatibility identity")
}
if first.Runtime.Fingerprint == second.Runtime.Fingerprint {
t.Fatal("final driver RuntimePayload fingerprint ignored the changed Tool attachment")
}
if first.ProfilePayload.SessionFingerprint() == second.ProfilePayload.SessionFingerprint() {
t.Fatal("catalog revision did not change the session compatibility fingerprint")
}
if second.Session == nil || second.Session.State != nil {
t.Fatalf("changed catalog resumed old provider state: %+v", second.Session)
}
}
func TestToolCatalogResumesThreadAcrossAgentRestartAndEphemeralPortChange(t *testing.T) {
stableAttachment := func() *fakeProvider {
refs := make([]adaptor.ServiceRef, 0, 64)
for index := 0; index < 64; index++ {
refs = append(refs, adaptor.ServiceRef{
ID: fmt.Sprintf("stable-%02d", index),
Name: fmt.Sprintf("stable-%02d", index),
URL: fmt.Sprintf("https://runtime-%02d.example.test", index),
Lifecycle: driver.RuntimeLifecycleShared,
ReuseKey: fmt.Sprintf("stable/v%d", index),
})
}
return &fakeProvider{
name: "stable-services",
attachment: adaptor.RunAttachment{Services: refs},
log: &callLog{},
}
}
store := memory.NewStore()
firstDriver := newSessionFake("tools-restart")
firstDescriptor := firstDriver.Descriptor()
firstDescriptor.MCP = driver.MCPCapability{Supported: true, HTTP: true}
firstDriver.descriptor = &firstDescriptor
firstAgent := adaptor.New(firstDriver,
adaptor.WithThreadStore(store),
adaptor.WithTools(hostedToolDefinition("echo", tool.Revision("echo/v1"))),
adaptor.WithRunServices(stableAttachment()),
)
if _, err := firstAgent.Thread("thread").Run(context.Background(), "first"); err != nil {
t.Fatalf("first: %v", err)
}
first := firstDriver.request(t, 0)
if err := firstAgent.Close(context.Background()); err != nil {
t.Fatalf("close first Agent: %v", err)
}
parsed, err := url.Parse(first.MCP.Servers[0].URL)
if err != nil {
t.Fatalf("parse first endpoint: %v", err)
}
portGuard, err := net.Listen("tcp4", parsed.Host)
if err != nil {
t.Fatalf("reserve former endpoint %q: %v", parsed.Host, err)
}
defer portGuard.Close()
secondDriver := newSessionFake("tools-restart")
secondDescriptor := secondDriver.Descriptor()
secondDescriptor.MCP = driver.MCPCapability{Supported: true, HTTP: true}
secondDriver.descriptor = &secondDescriptor
secondAgent := adaptor.New(secondDriver,
adaptor.WithThreadStore(store),
adaptor.WithTools(hostedToolDefinition("echo", tool.Revision("echo/v1"))),
adaptor.WithRunServices(stableAttachment()),
)
defer func() { _ = secondAgent.Close(context.Background()) }()
if _, err := secondAgent.Thread("thread").Run(context.Background(), "second"); err != nil {
t.Fatalf("second: %v", err)
}
second := secondDriver.request(t, 0)
if first.MCP.Servers[0].URL == second.MCP.Servers[0].URL {
t.Fatal("test did not force a new loopback endpoint")
}
if first.MCP.Fingerprint == second.MCP.Fingerprint {
t.Fatal("concrete MCP materialization fingerprint ignored the new endpoint")
}
if first.Runtime.Fingerprint == second.Runtime.Fingerprint {
t.Fatal("driver runtime fingerprint ignored the concrete endpoint change")
}
if first.ProfilePayload.Fingerprint == second.ProfilePayload.Fingerprint {
t.Fatal("concrete ProfilePayload fingerprint ignored the new endpoint or credential carrier")
}
if first.ProfilePayload.SessionFingerprint() != second.ProfilePayload.SessionFingerprint() {
t.Fatal("session Tool profile compatibility changed with only ephemeral transport allocation")
}
if second.Session == nil || second.Session.State == nil || second.Session.State.ResumeID == "" {
t.Fatalf("second request did not resume the stored provider session: %+v", second.Session)
}
}