| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/internal/event" |
| 9 | "reasonix/internal/provider" |
| 10 | "reasonix/internal/tool" |
| 11 | ) |
| 12 | |
| 13 | // A model that emits structurally-invalid JSON must be rejected by the host |
| 14 | // validator before hooks or Execute. The correction contract is deliberately |
| 15 | // value-free and bounded rather than echoing the full provider schema. |
| 16 | func TestMalformedToolArgsReturnHostValidationContract(t *testing.T) { |
| 17 | reg := tool.NewRegistry() |
| 18 | reg.Add(NewAskTool()) |
| 19 | prov := &scriptedProvider{name: "p", turns: [][]provider.Chunk{ |
| 20 | {toolCallChunk("c1", "ask", `{"questions":["q":1]}`), {Type: provider.ChunkDone}}, |
| 21 | {{Type: provider.ChunkText, Text: "done"}, {Type: provider.ChunkDone}}, |
| 22 | }} |
| 23 | a := New(prov, reg, NewSession(""), Options{}, event.Discard) |
| 24 | if err := a.Run(context.Background(), "ask me"); err != nil { |
| 25 | t.Fatalf("Run: %v", err) |
| 26 | } |
| 27 | got := toolResult(a.sess.conversation, "ask") |
| 28 | if !strings.Contains(got, "argument validation failed") || |
| 29 | !strings.Contains(got, "one valid JSON object") || |
| 30 | !strings.Contains(got, "remote_dispatched=false") { |
| 31 | t.Fatalf("malformed-args result should carry the host correction contract, got %q", got) |
| 32 | } |
| 33 | if strings.Contains(got, `"properties"`) || strings.Contains(got, `"options"`) { |
| 34 | t.Fatalf("malformed-args result must not echo the full schema, got %q", got) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // A valid-JSON arg that violates the schema must surface the precise keyword |
| 39 | // and expectation without echoing the full schema. |
| 40 | func TestValidArgsErrorOmitsSchema(t *testing.T) { |
| 41 | reg := tool.NewRegistry() |
| 42 | reg.Add(NewAskTool()) |
| 43 | prov := &scriptedProvider{name: "p", turns: [][]provider.Chunk{ |
| 44 | {toolCallChunk("c1", "ask", `{"questions":[{"question":"q","header":"h","options":[{"label":"a"}]}]}`), {Type: provider.ChunkDone}}, |
| 45 | {{Type: provider.ChunkText, Text: "done"}, {Type: provider.ChunkDone}}, |
| 46 | }} |
| 47 | a := New(prov, reg, NewSession(""), Options{}, event.Discard) |
| 48 | if err := a.Run(context.Background(), "ask me"); err != nil { |
| 49 | t.Fatalf("Run: %v", err) |
| 50 | } |
| 51 | got := toolResult(a.sess.conversation, "ask") |
| 52 | if strings.Contains(got, `"properties"`) { |
| 53 | t.Fatalf("a valid-JSON arg error must not get the full schema, got %q", got) |
| 54 | } |
| 55 | if !strings.Contains(got, "minItems") || !strings.Contains(got, "at least 2 items") { |
| 56 | t.Fatalf("expected the precise host validation error, got %q", got) |
| 57 | } |
| 58 | } |
| 59 |