-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstructured_contract_test.go
More file actions
660 lines (592 loc) · 24.1 KB
/
Copy pathstructured_contract_test.go
File metadata and controls
660 lines (592 loc) · 24.1 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
package adaptor_test
// Structured-output contract for the v1 surface. The consumer declares only
// the desired schema. Core then selects provider-native enforcement when it is
// compatible and otherwise falls back to exact-JSON prompting plus local
// validation. Provider transport selection remains independent from whether
// the consumer called Run or Stream.
import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
"sync"
"testing"
adaptor "github.com/agent-dance/agent-adaptor"
"github.com/agent-dance/agent-adaptor/driver"
"github.com/agent-dance/agent-adaptor/memory"
)
// structuredFake mirrors the root structuredTestDriver on the driver SPI:
// it mints/echoes session checkpoints exactly like a resumable CLI and emits
// native structured output when core selected the native mechanism (or when
// forced, to probe unrequested-output suppression).
type structuredFake struct {
mu sync.Mutex
requests []driver.Request
caps driver.StructuredOutputCapability
output string
// structuredRaw, when non-empty, diverges the emitted RawJSON from
// Output so suppression tests can prove which surface Decode used.
structuredRaw string
forceStructured bool
sessionCounter int
}
var _ driver.Driver = (*structuredFake)(nil)
var _ driver.SessionConfigFingerprinter = (*structuredFake)(nil)
var _ driver.SessionCodecProvider = (*structuredFake)(nil)
var _ driver.StreamSupport = (*structuredFake)(nil)
func (d *structuredFake) Descriptor() driver.Descriptor {
return driver.Descriptor{
Type: "structured-fake",
DisplayName: "Structured Fake",
Sessions: driver.SessionCapability{SupportsResume: true},
StructuredOutput: d.caps,
}
}
func (d *structuredFake) ValidateConfig(any) error { return nil }
func (d *structuredFake) SessionConfigFingerprint() (string, error) {
return "structured-fake-config/v1", nil
}
func (d *structuredFake) SessionCodec() driver.SessionCodec { return fakeSessionCodec{} }
func (d *structuredFake) StreamCapability() driver.StreamCapability {
return driver.StreamCapability{Native: true}
}
func (d *structuredFake) Run(_ context.Context, req driver.Request, _ driver.EventSink) (driver.Response, error) {
d.mu.Lock()
d.requests = append(d.requests, req)
var checkpoint *driver.Checkpoint
if req.Session != nil {
state := req.Session.State
if state == nil || state.ResumeID == "" {
d.sessionCounter++
state = &driver.SessionState{
ResumeID: fmt.Sprintf("structured-session-%d", d.sessionCounter),
DisplayID: fmt.Sprintf("Structured Session %d", d.sessionCounter),
}
}
checkpoint = &driver.Checkpoint{State: state, Valid: true}
}
var structured *driver.StructuredOutput
if d.forceStructured || (req.OutputSchema != nil && req.StructuredOutputSource == driver.StructuredOutputSourceNative) {
raw := d.output
if d.structuredRaw != "" {
raw = d.structuredRaw
}
structured = &driver.StructuredOutput{
Format: driver.OutputFormatJSONSchema,
Source: driver.StructuredOutputSourceNative,
RawJSON: []byte(raw),
Valid: true,
}
}
output := d.output
d.mu.Unlock()
return driver.Response{
Output: output,
RawStreams: &driver.RawStreams{},
Checkpoint: checkpoint,
StructuredOutput: structured,
}, nil
}
func (d *structuredFake) request(t *testing.T, i int) driver.Request {
t.Helper()
d.mu.Lock()
defer d.mu.Unlock()
if i >= len(d.requests) {
t.Fatalf("structured fake saw %d request(s), want index %d", len(d.requests), i)
}
return d.requests[i]
}
func (d *structuredFake) lastRequest(t *testing.T) driver.Request {
t.Helper()
d.mu.Lock()
defer d.mu.Unlock()
if len(d.requests) == 0 {
t.Fatal("structured fake saw no requests")
}
return d.requests[len(d.requests)-1]
}
func (d *structuredFake) runCount() int {
d.mu.Lock()
defer d.mu.Unlock()
return len(d.requests)
}
// fullStructuredCaps advertises both provider transports so these fixtures
// exercise native streaming unless a focused test requests batch fallback.
func fullStructuredCaps() driver.StructuredOutputCapability {
return driver.StructuredOutputCapability{
JSONSchemaNative: true,
JSONSchemaPromptValidate: true,
WorksWithRun: true,
WorksWithStreaming: true,
WorksWithHITL: true,
}
}
// The schema fixture types are byte-for-byte copies of the root baseline so
// the generated schemas stay comparable across the two surfaces.
type projectMetadata struct {
ProjectName string `json:"project_name"`
ProgrammingLanguages []string `json:"programming_languages,omitempty"`
}
type nestedProjectMetadata struct {
ProjectName string `json:"project_name"`
Artifact projectMetadata `json:"artifact"`
}
type recursiveProjectMetadata struct {
ProjectName string `json:"project_name"`
Children []*recursiveProjectMetadata `json:"children"`
}
type recursiveList []recursiveList
type recursiveMap map[string]recursiveMap
type recursivePointer *recursivePointer
type recursivePointerMap map[recursivePointer]string
type largeSchemaNumber struct {
ID int64 `json:"id" jsonschema:"minimum=9007199254740993"`
}
func TestWithSchemaGeneratesDeterministicSchema(t *testing.T) {
ctx := context.Background()
fake := &structuredFake{caps: fullStructuredCaps(), output: `{"project_name":"agent-adaptor","programming_languages":["go"]}`}
agent := adaptor.New(fake)
res, err := agent.Run(ctx, "extract", adaptor.WithSchema[projectMetadata]())
if err != nil {
t.Fatalf("run 1: %v", err)
}
if _, err := agent.Run(ctx, "extract again", adaptor.WithSchema[projectMetadata]()); err != nil {
t.Fatalf("run 2: %v", err)
}
firstReq := fake.request(t, 0)
secondReq := fake.request(t, 1)
first := firstReq.OutputSchema
second := secondReq.OutputSchema
if first == nil || second == nil {
t.Fatalf("output schema missing: first=%v second=%v", first, second)
}
if string(first.SchemaJSON) != string(second.SchemaJSON) {
t.Fatalf("expected deterministic schema bytes:\nfirst=%s\nsecond=%s", first.SchemaJSON, second.SchemaJSON)
}
if !strings.Contains(string(first.SchemaJSON), `"additionalProperties":false`) {
t.Errorf("expected strict object schema, got %s", first.SchemaJSON)
}
if !strings.Contains(string(first.SchemaJSON), `"project_name"`) {
t.Errorf("expected json tag name in schema, got %s", first.SchemaJSON)
}
if first.OnInvalid != driver.StructuredOutputFailRun {
t.Errorf("default invalid policy = %q, want fail_run", first.OnInvalid)
}
if firstReq.StructuredOutputSource != driver.StructuredOutputSourceNative || secondReq.StructuredOutputSource != driver.StructuredOutputSourceNative {
t.Errorf("resolved sources = %q / %q, want native", firstReq.StructuredOutputSource, secondReq.StructuredOutputSource)
}
var decoded projectMetadata
if err := res.Decode(&decoded); err != nil {
t.Fatalf("decode: %v", err)
}
if decoded.ProjectName != "agent-adaptor" || len(decoded.ProgrammingLanguages) != 1 || decoded.ProgrammingLanguages[0] != "go" {
t.Errorf("unexpected decoded value: %#v", decoded)
}
}
func TestWithSchemaSupportsRecursiveTypes(t *testing.T) {
fake := &structuredFake{caps: fullStructuredCaps(), output: `{"project_name":"root","children":[]}`}
agent := adaptor.New(fake)
res, err := agent.Run(context.Background(), "extract tree", adaptor.WithSchema[recursiveProjectMetadata]())
if err != nil {
t.Fatalf("run: %v", err)
}
req := fake.lastRequest(t)
if req.OutputSchema == nil || !strings.Contains(string(req.OutputSchema.SchemaJSON), `"$ref"`) {
t.Fatalf("expected recursive schema references, got %#v", req.OutputSchema)
}
var decoded recursiveProjectMetadata
if err := res.Decode(&decoded); err != nil {
t.Fatalf("decode: %v", err)
}
if decoded.ProjectName != "root" {
t.Errorf("unexpected decoded value: %#v", decoded)
}
}
func TestWithSchemaRejectsInliningRecursiveTypes(t *testing.T) {
fake := &structuredFake{caps: fullStructuredCaps(), output: `{}`}
agent := adaptor.New(fake)
_, err := agent.Run(context.Background(), "extract", adaptor.WithSchema[recursiveProjectMetadata](adaptor.SchemaInlineReferences()))
if !errors.Is(err, adaptor.ErrInvalidOutputSchema) || !strings.Contains(err.Error(), "cannot inline recursive Go type") {
t.Fatalf("error = %v, want recursive ErrInvalidOutputSchema", err)
}
if fake.runCount() != 0 {
t.Errorf("driver ran %d time(s), want pre-launch failure", fake.runCount())
}
}
func TestWithSchemaSupportsRecursiveCollections(t *testing.T) {
tests := []struct {
name string
output string
run func(agent *adaptor.Agent) (*adaptor.Result, error)
}{
{
name: "slice",
output: `[]`,
run: func(agent *adaptor.Agent) (*adaptor.Result, error) {
return agent.Run(context.Background(), "extract list", adaptor.WithSchema[recursiveList]())
},
},
{
name: "map",
output: `{}`,
run: func(agent *adaptor.Agent) (*adaptor.Result, error) {
return agent.Run(context.Background(), "extract map", adaptor.WithSchema[recursiveMap]())
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
fake := &structuredFake{caps: fullStructuredCaps(), output: tc.output}
if _, err := tc.run(adaptor.New(fake)); err != nil {
t.Fatalf("run: %v", err)
}
req := fake.lastRequest(t)
if req.OutputSchema == nil || !strings.Contains(string(req.OutputSchema.SchemaJSON), `"$ref"`) {
t.Fatalf("schema = %#v", req.OutputSchema)
}
})
}
}
func TestWithSchemaRejectsSelfReferentialPointer(t *testing.T) {
fake := &structuredFake{caps: fullStructuredCaps()}
_, err := adaptor.New(fake).Run(context.Background(), "extract", adaptor.WithSchema[recursivePointer]())
if !errors.Is(err, adaptor.ErrInvalidOutputSchema) || !strings.Contains(err.Error(), "self-referential pointer") {
t.Fatalf("error = %v, want self-referential pointer ErrInvalidOutputSchema", err)
}
if fake.runCount() != 0 {
t.Errorf("driver ran %d time(s), want pre-launch failure", fake.runCount())
}
}
func TestWithSchemaRejectsSelfReferentialMapKeyPointer(t *testing.T) {
fake := &structuredFake{caps: fullStructuredCaps()}
_, err := adaptor.New(fake).Run(context.Background(), "extract", adaptor.WithSchema[recursivePointerMap]())
if !errors.Is(err, adaptor.ErrInvalidOutputSchema) || !strings.Contains(err.Error(), "self-referential map key") {
t.Fatalf("error = %v, want self-referential map key ErrInvalidOutputSchema", err)
}
if fake.runCount() != 0 {
t.Errorf("driver ran %d time(s), want pre-launch failure", fake.runCount())
}
}
func TestWithSchemaRejectsUnsupportedTypes(t *testing.T) {
type invalid struct {
Done chan struct{} `json:"done"`
}
fake := &structuredFake{caps: fullStructuredCaps()}
_, err := adaptor.New(fake).Run(context.Background(), "extract", adaptor.WithSchema[invalid]())
if !errors.Is(err, adaptor.ErrInvalidOutputSchema) {
t.Fatalf("expected ErrInvalidOutputSchema, got %v", err)
}
var typed *adaptor.InvalidOutputSchemaError
if !errors.As(err, &typed) {
t.Fatalf("error = %v, want *InvalidOutputSchemaError in the chain", err)
}
if fake.runCount() != 0 {
t.Errorf("driver ran %d time(s), want pre-launch failure", fake.runCount())
}
}
func TestWithSchemaPreservesLargeConstraintNumbers(t *testing.T) {
// SchemaReturnInvalid keeps the run alive regardless of how the local
// validator treats the extreme constraint — the assertion is that the
// generated document preserves the integer
// verbatim on its way to the driver.
caps := fullStructuredCaps()
caps.JSONSchemaNative = false
fake := &structuredFake{caps: caps, output: `{"id":9007199254740993}`}
_, err := adaptor.New(fake).Run(context.Background(), "extract",
adaptor.WithSchema[largeSchemaNumber](adaptor.SchemaReturnInvalid()))
if err != nil {
t.Fatalf("run: %v", err)
}
req := fake.lastRequest(t)
if req.OutputSchema == nil || !strings.Contains(string(req.OutputSchema.SchemaJSON), `"minimum":9007199254740993`) {
t.Fatalf("large schema number was not preserved: %#v", req.OutputSchema)
}
}
func TestStructuredOutputRejectsInvalidSchemaBeforeLaunch(t *testing.T) {
fake := &structuredFake{caps: fullStructuredCaps(), output: `{"project_name":"agent-adaptor"}`}
agent := adaptor.New(fake)
_, err := agent.Run(context.Background(), "extract", adaptor.WithSchemaJSON([]byte(`{`)))
if !errors.Is(err, adaptor.ErrInvalidOutputSchema) {
t.Fatalf("expected ErrInvalidOutputSchema, got %v", err)
}
if fake.runCount() != 0 {
t.Errorf("driver ran %d time(s), want pre-launch failure", fake.runCount())
}
}
func TestStructuredOutputRejectsUnsupportedBeforeLaunch(t *testing.T) {
caps := fullStructuredCaps()
caps.JSONSchemaNative = false
caps.JSONSchemaPromptValidate = false
fake := &structuredFake{caps: caps, output: `{"project_name":"agent-adaptor"}`}
agent := adaptor.New(fake)
_, err := agent.Run(context.Background(), "extract", adaptor.WithSchema[projectMetadata]())
if !errors.Is(err, adaptor.ErrStructuredOutputUnsupported) {
t.Fatalf("expected ErrStructuredOutputUnsupported, got %v", err)
}
if fake.runCount() != 0 {
t.Errorf("driver ran %d time(s), want pre-launch failure", fake.runCount())
}
}
func TestStructuredSchemaFallsBackToCompatibleProviderTransport(t *testing.T) {
caps := fullStructuredCaps()
caps.WorksWithStreaming = false
fake := &structuredFake{caps: caps, output: `{"project_name":"agent-adaptor"}`}
agent := adaptor.New(fake)
res, err := agent.Stream(context.Background(), "extract", adaptor.WithSchema[projectMetadata]()).Result()
if err != nil {
t.Fatalf("Stream.Result: %v", err)
}
if res == nil {
t.Fatal("missing Result")
}
if req := fake.lastRequest(t); req.Streaming {
t.Fatalf("request selected provider streaming despite schema capability %+v", caps)
}
}
func TestStructuredOutputFallsBackToPromptValidation(t *testing.T) {
caps := fullStructuredCaps()
caps.JSONSchemaNative = false
fake := &structuredFake{caps: caps, output: `{"project_name":"agent-adaptor","programming_languages":["go"]}`}
agent := adaptor.New(fake)
schema := []byte(`{"type":"object","properties":{"project_name":{"type":"string"},"programming_languages":{"type":"array","items":{"type":"string"}}},"required":["project_name"],"additionalProperties":false}`)
opt := adaptor.WithSchemaJSON(schema, adaptor.SchemaName("project_metadata"))
schema[0] = '[' // prove WithSchemaJSON made its own copy.
res, err := agent.Run(context.Background(), "extract metadata", opt)
if err != nil {
t.Fatalf("run: %v", err)
}
req := fake.lastRequest(t)
if !strings.Contains(req.Prompt, "Return only a single JSON value") || !strings.Contains(req.Prompt, "project_metadata") {
t.Fatalf("prompt-validation instructions were not injected: %q", req.Prompt)
}
if !strings.HasSuffix(req.Prompt, "extract metadata") {
t.Errorf("original prompt must trail the injected instructions, got %q", req.Prompt)
}
if req.OutputSchema == nil || len(req.OutputSchema.SchemaJSON) == 0 || req.OutputSchema.SchemaJSON[0] != '{' {
t.Fatalf("expected deep-copied schema on the driver request, got %#v", req.OutputSchema)
}
if req.StructuredOutputSource != driver.StructuredOutputSourcePromptValidate {
t.Fatalf("resolved source = %q, want automatic prompt-validation fallback", req.StructuredOutputSource)
}
// The fake emits no native structured output on the fallback path,
// so a successful Decode proves the SDK validated the raw text itself.
var decoded projectMetadata
if err := res.Decode(&decoded); err != nil {
t.Fatalf("decode: %v", err)
}
if decoded.ProjectName != "agent-adaptor" {
t.Errorf("unexpected decoded result: %#v", decoded)
}
}
func TestStructuredOutputFallbackFailurePolicies(t *testing.T) {
schema := []byte(`{"type":"object","properties":{"project_name":{"type":"string"}},"required":["project_name"],"additionalProperties":false}`)
caps := fullStructuredCaps()
caps.JSONSchemaNative = false
t.Run("default fail-run policy", func(t *testing.T) {
// Code fences around the JSON: the fallback path demands an exact
// JSON document, so this output is invalid and the default policy
// fails the run as a policy violation.
fake := &structuredFake{caps: caps, output: "```json\n{\"project_name\":\"agent-adaptor\"}\n```"}
agent := adaptor.New(fake)
_, err := agent.Run(context.Background(), "extract", adaptor.WithSchemaJSON(schema))
if !errors.Is(err, adaptor.ErrPolicyViolation) {
t.Fatalf("error = %v, want ErrPolicyViolation", err)
}
var re *adaptor.RunError
if !errors.As(err, &re) {
t.Fatalf("error = %v, want *RunError", err)
}
if re.Reason != adaptor.ReasonPolicyViolation {
t.Errorf("reason = %q, want policy_violation", re.Reason)
}
if re.Result == nil {
t.Fatal("RunError.Result missing — the completed-but-invalid run must stay auditable")
}
var decoded projectMetadata
if decodeErr := re.Result.Decode(&decoded); decodeErr == nil || !strings.Contains(decodeErr.Error(), "structured output invalid") {
t.Errorf("decode error = %v, want the invalid-structured-output contract", decodeErr)
}
if fake.runCount() != 1 {
t.Errorf("driver ran %d time(s), want exactly one post-launch failure", fake.runCount())
}
})
t.Run("SchemaReturnInvalid", func(t *testing.T) {
// Type mismatch (42 for a string): invalid, but the run succeeds
// and Decode reports the invalidity.
fake := &structuredFake{caps: caps, output: `{"project_name":42}`}
agent := adaptor.New(fake)
res, err := agent.Run(context.Background(), "extract",
adaptor.WithSchemaJSON(schema, adaptor.SchemaReturnInvalid()))
if err != nil {
t.Fatalf("run: %v", err)
}
var decoded projectMetadata
if decodeErr := res.Decode(&decoded); decodeErr == nil || !strings.Contains(decodeErr.Error(), "structured output invalid") {
t.Errorf("decode error = %v, want the invalid-structured-output contract", decodeErr)
}
})
}
func TestStructuredOutputIgnoredWithoutSchemaRequest(t *testing.T) {
// The fake emits structured output nobody asked for, with RawJSON
// deliberately different from Output. Decode must fall back to Text —
// seeing "structured" here would mean unrequested output leaked.
fake := &structuredFake{
caps: fullStructuredCaps(),
output: `{"from":"text"}`,
structuredRaw: `{"from":"structured"}`,
forceStructured: true,
}
agent := adaptor.New(fake)
res, err := agent.Run(context.Background(), "ordinary run")
if err != nil {
t.Fatalf("run: %v", err)
}
var decoded map[string]string
if err := res.Decode(&decoded); err != nil {
t.Fatalf("decode: %v", err)
}
if decoded["from"] != "text" {
t.Errorf("decoded %v — unrequested structured output leaked into the Result", decoded)
}
}
func TestStructuredOutputFallbackPreservesLargeNumbers(t *testing.T) {
caps := fullStructuredCaps()
caps.JSONSchemaNative = false
fake := &structuredFake{caps: caps, output: `{"id":9007199254740993}`}
agent := adaptor.New(fake)
schema := []byte(`{"type":"object","properties":{"id":{"type":"integer","const":9007199254740993}},"required":["id"],"additionalProperties":false}`)
res, err := agent.Run(context.Background(), "extract", adaptor.WithSchemaJSON(schema))
if err != nil {
t.Fatalf("run: %v", err)
}
var decoded struct {
ID json.Number `json:"id"`
}
if err := res.Decode(&decoded); err != nil {
t.Fatalf("decode: %v", err)
}
if decoded.ID.String() != "9007199254740993" {
t.Errorf("large integer was not preserved: %s", decoded.ID)
}
}
func TestRunAndStreamReturnEquivalentStructuredOutput(t *testing.T) {
ctx := context.Background()
caps := fullStructuredCaps()
caps.JSONSchemaNative = false
fake := &structuredFake{caps: caps, output: `{"project_name":"agent-adaptor"}`}
agent := adaptor.New(fake)
opt := adaptor.WithSchema[projectMetadata]()
runRes, err := agent.Run(ctx, "extract", opt)
if err != nil {
t.Fatalf("run: %v", err)
}
stream := agent.Stream(ctx, "extract", opt)
for range stream.Events() {
// drain
}
streamRes, err := stream.Result()
if err != nil {
t.Fatalf("stream result: %v", err)
}
var fromRun, fromStream projectMetadata
if err := runRes.Decode(&fromRun); err != nil {
t.Fatalf("decode run: %v", err)
}
if err := streamRes.Decode(&fromStream); err != nil {
t.Fatalf("decode stream: %v", err)
}
if !reflect.DeepEqual(fromRun, fromStream) {
t.Errorf("Run and Stream structured output diverged: run=%#v stream=%#v", fromRun, fromStream)
}
}
// TestSchemaChangeDoesNotAffectThreadSessionFingerprint: the thread
// fingerprint folds the environment payload (skills/MCP/profile), never the
// output schema — changing schema metadata between runs must resume the same
// provider session.
func TestSchemaChangeDoesNotAffectThreadSessionFingerprint(t *testing.T) {
ctx := context.Background()
fake := &structuredFake{caps: fullStructuredCaps(), output: `{"project_name":"agent-adaptor"}`}
store := memory.NewStore()
agent := adaptor.New(fake, adaptor.WithThreadStore(store))
const key = "company/issue-structured-output"
if _, err := agent.Thread(key).Run(ctx, "extract metadata", adaptor.WithSchema[projectMetadata]()); err != nil {
t.Fatalf("first run: %v", err)
}
req1 := fake.request(t, 0)
if req1.Session == nil || req1.Session.State != nil {
t.Fatalf("first run session = %#v, want a fresh session request", req1.Session)
}
rec1 := activeRecord(t, store, key)
if _, err := agent.Thread(key).Run(ctx, "extract metadata again", adaptor.WithSchema[projectMetadata](adaptor.SchemaName("renamed_schema"))); err != nil {
t.Fatalf("second run: %v", err)
}
req2 := fake.request(t, 1)
if req2.Session == nil || req2.Session.State == nil || req2.Session.State.ResumeID != "structured-session-1" {
t.Fatalf("second run session = %#v, want a resume of structured-session-1 (schema changes must not split the session)", req2.Session)
}
rec2 := activeRecord(t, store, key)
if rec2.ID != rec1.ID {
t.Errorf("thread record changed %s → %s, want the same record across schema changes", rec1.ID, rec2.ID)
}
}
// TestRunAsDecodesAcrossRunners: RunAs accepts any Runner — the stateless
// Agent and a Thread behave identically — and propagates pre-launch errors
// with a zero T.
func TestRunAsDecodesAcrossRunners(t *testing.T) {
ctx := context.Background()
t.Run("agent", func(t *testing.T) {
fake := &structuredFake{caps: fullStructuredCaps(), output: `{"project_name":"agent-adaptor","programming_languages":["go"]}`}
agent := adaptor.New(fake)
decoded, res, err := adaptor.RunAs[projectMetadata](ctx, agent, "extract")
if err != nil {
t.Fatalf("RunAs: %v", err)
}
if res == nil {
t.Fatal("RunAs returned no Result")
}
if decoded.ProjectName != "agent-adaptor" {
t.Errorf("unexpected decoded value: %#v", decoded)
}
})
t.Run("thread", func(t *testing.T) {
fake := &structuredFake{caps: fullStructuredCaps(), output: `{"project_name":"agent-adaptor"}`}
agent := adaptor.New(fake, adaptor.WithThreadStore(memory.NewStore()))
var runner adaptor.Runner = agent.Thread("company/run-as") // compile-time Runner proof
decoded, _, err := adaptor.RunAs[projectMetadata](ctx, runner, "extract")
if err != nil {
t.Fatalf("RunAs: %v", err)
}
if decoded.ProjectName != "agent-adaptor" {
t.Errorf("unexpected decoded value: %#v", decoded)
}
})
t.Run("explicit schema options win over the implicit schema", func(t *testing.T) {
fake := &structuredFake{caps: fullStructuredCaps(), output: `{"project_name":"agent-adaptor"}`}
agent := adaptor.New(fake)
if _, _, err := adaptor.RunAs[projectMetadata](ctx, agent, "extract", adaptor.WithSchema[projectMetadata](adaptor.SchemaName("explicit"))); err != nil {
t.Fatalf("RunAs: %v", err)
}
if got := fake.lastRequest(t).OutputSchema; got == nil || got.Name != "explicit" {
t.Errorf("schema = %#v, want the explicit SchemaName to win", got)
}
})
t.Run("pre-launch error yields zero value", func(t *testing.T) {
caps := fullStructuredCaps()
caps.JSONSchemaNative = false
caps.JSONSchemaPromptValidate = false
fake := &structuredFake{caps: caps, output: `{"project_name":"agent-adaptor"}`}
agent := adaptor.New(fake)
decoded, _, err := adaptor.RunAs[projectMetadata](ctx, agent, "extract")
if !errors.Is(err, adaptor.ErrStructuredOutputUnsupported) {
t.Fatalf("error = %v, want ErrStructuredOutputUnsupported", err)
}
if !reflect.DeepEqual(decoded, projectMetadata{}) {
t.Errorf("decoded = %#v, want the zero value on error", decoded)
}
if fake.runCount() != 0 {
t.Errorf("driver ran %d time(s), want pre-launch failure", fake.runCount())
}
})
}