| 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 for a tool's args should get the |
| 14 | // tool's schema echoed back, so the retry lands a valid shape instead of |
| 15 | // repeating the same broken one (the "invalid character ':' after array element" |
| 16 | // case from the ask tool). |
| 17 | func TestMalformedToolArgsEchoSchema(t *testing.T) { |
| 18 | reg := tool.NewRegistry() |
| 19 | reg.Add(NewAskTool()) |
| 20 | prov := &scriptedProvider{name: "p", turns: [][]provider.Chunk{ |
| 21 | {toolCallChunk("c1", "ask", `{"questions":["q":1]}`), {Type: provider.ChunkDone}}, |
| 22 | {{Type: provider.ChunkText, Text: "done"}, {Type: provider.ChunkDone}}, |
| 23 | }} |
| 24 | a := New(prov, reg, NewSession(""), Options{}, event.Discard) |
| 25 | if err := a.Run(context.Background(), "ask me"); err != nil { |
| 26 | t.Fatalf("Run: %v", err) |
| 27 | } |
| 28 | got := toolResult(a.session, "ask") |
| 29 | if !strings.Contains(got, "not valid JSON") || !strings.Contains(got, `"options"`) { |
| 30 | t.Fatalf("malformed-args result should echo the schema, got %q", got) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // A valid-JSON arg that fails the tool's own validation must surface that error |
| 35 | // as-is, without the schema hint (the shape was fine; the values weren't). |
| 36 | func TestValidArgsErrorOmitsSchema(t *testing.T) { |
| 37 | reg := tool.NewRegistry() |
| 38 | reg.Add(NewAskTool()) |
| 39 | prov := &scriptedProvider{name: "p", turns: [][]provider.Chunk{ |
| 40 | {toolCallChunk("c1", "ask", `{"questions":[{"question":"q","header":"h","options":[{"label":"a"}]}]}`), {Type: provider.ChunkDone}}, |
| 41 | {{Type: provider.ChunkText, Text: "done"}, {Type: provider.ChunkDone}}, |
| 42 | }} |
| 43 | a := New(prov, reg, NewSession(""), Options{}, event.Discard) |
| 44 | if err := a.Run(context.Background(), "ask me"); err != nil { |
| 45 | t.Fatalf("Run: %v", err) |
| 46 | } |
| 47 | got := toolResult(a.session, "ask") |
| 48 | if strings.Contains(got, "not valid JSON") { |
| 49 | t.Fatalf("a valid-JSON arg error must not get the schema hint, got %q", got) |
| 50 | } |
| 51 | if !strings.Contains(got, "two options") { |
| 52 | t.Fatalf("expected the real validation error, got %q", got) |
| 53 | } |
| 54 | } |
| 55 |