| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "testing" |
| 8 | |
| 9 | "reasonix/internal/event" |
| 10 | "reasonix/internal/extension" |
| 11 | "reasonix/internal/extension/dispatch" |
| 12 | "reasonix/internal/extension/protocol" |
| 13 | "reasonix/internal/provider" |
| 14 | "reasonix/internal/tool" |
| 15 | ) |
| 16 | |
| 17 | func TestSamplingTranscriptGateBlocksInvalidArgumentsBeforeProvider(t *testing.T) { |
| 18 | for _, args := range []string{`{"path":`, `[]`, `null`, `"string"`} { |
| 19 | t.Run(args, func(t *testing.T) { |
| 20 | mp := &mockProvider{name: "p", chunks: []provider.Chunk{{Type: provider.ChunkDone}}} |
| 21 | a := New(mp, tool.NewRegistry(), NewSession("sys"), Options{}, event.Discard) |
| 22 | req := provider.Request{Messages: []provider.Message{ |
| 23 | {Role: provider.RoleAssistant, ToolCalls: []provider.ToolCall{{ID: "a", Name: "read_file", Arguments: args}}}, |
| 24 | {Role: provider.RoleTool, ToolCallID: "a", Name: "read_file", Content: "result"}, |
| 25 | }} |
| 26 | _, err := a.streamProviderRequest(context.Background(), req) |
| 27 | if err == nil { |
| 28 | t.Fatal("invalid tool arguments reached provider") |
| 29 | } |
| 30 | if len(mp.requests) != 0 { |
| 31 | t.Fatalf("invalid request sent %d times", len(mp.requests)) |
| 32 | } |
| 33 | }) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestSamplingTranscriptGateRunsAfterProviderInterceptor(t *testing.T) { |
| 38 | client := &fakeDispatchClient{interceptFn: func(ev protocol.InterceptEvent, payload json.RawMessage) (protocol.InterceptResult, error) { |
| 39 | if ev != protocol.EventProviderRequest { |
| 40 | return protocol.InterceptResult{Decision: protocol.DecisionContinue}, nil |
| 41 | } |
| 42 | var in dispatch.ProviderRequestPayload |
| 43 | if err := json.Unmarshal(payload, &in); err != nil { |
| 44 | return protocol.InterceptResult{}, err |
| 45 | } |
| 46 | // A schema-valid replacement can still contain a protocol-invalid tool |
| 47 | // transcript. The final gate must inspect this replacement. |
| 48 | var extra []protocol.ProviderMessage |
| 49 | if err := json.Unmarshal([]byte(`[{"role":"assistant","tool_calls":[{"id":"a","name":"read_file","arguments":"[]"}]},{"role":"tool","tool_call_id":"a","name":"read_file","content":"result"}]`), &extra); err != nil { |
| 50 | return protocol.InterceptResult{}, err |
| 51 | } |
| 52 | in.Request.Messages = append(in.Request.Messages, extra...) |
| 53 | return replaceWith(t, in), nil |
| 54 | }} |
| 55 | d := newExtDispatcher(client, true, nil, extension.PointProviderRequest) |
| 56 | mp := &mockProvider{name: "p", chunks: []provider.Chunk{{Type: provider.ChunkDone}}} |
| 57 | a := New(mp, tool.NewRegistry(), NewSession("sys"), Options{Extensions: d}, event.Discard) |
| 58 | if _, err := a.buildSamplingRequest(context.Background(), CompactionTriggerPressure); err == nil { |
| 59 | t.Fatal("final interceptor replacement bypassed transcript gate") |
| 60 | } |
| 61 | if len(mp.requests) != 0 { |
| 62 | t.Fatal("request preparation contacted provider") |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func TestSamplingTranscriptGatePreservesHealthyRequestBytes(t *testing.T) { |
| 67 | mp := &mockProvider{name: "p", chunks: []provider.Chunk{{Type: provider.ChunkDone}}} |
| 68 | a := New(mp, tool.NewRegistry(), NewSession("sys"), Options{}, event.Discard) |
| 69 | req := provider.Request{MaxTokens: 1234, Temperature: provider.OptionalTemperature(0.25), Messages: []provider.Message{ |
| 70 | {Role: provider.RoleSystem, Content: "stable cache prefix"}, |
| 71 | {Role: provider.RoleUser, Content: "read both"}, |
| 72 | {Role: provider.RoleAssistant, ReasoningContent: "read files", ToolCalls: []provider.ToolCall{{ID: "a", Name: "read_file", Arguments: ` { "path": "one" } `}, {ID: "b", Name: "read_file", Arguments: `{}`}}}, |
| 73 | {Role: provider.RoleTool, ToolCallID: "b", Name: "read_file", Content: "two"}, |
| 74 | {Role: provider.RoleTool, ToolCallID: "a", Name: "read_file", Content: "one"}, |
| 75 | }} |
| 76 | before, err := json.Marshal(req) |
| 77 | if err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | ch, err := a.streamProviderRequest(context.Background(), req) |
| 81 | if err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | for range ch { |
| 85 | } |
| 86 | if len(mp.requests) != 1 { |
| 87 | t.Fatalf("provider requests=%d", len(mp.requests)) |
| 88 | } |
| 89 | got, err := json.Marshal(mp.requests[0]) |
| 90 | if err != nil { |
| 91 | t.Fatal(err) |
| 92 | } |
| 93 | if !bytes.Equal(before, got) { |
| 94 | t.Fatalf("healthy request changed\nbefore=%s\nafter=%s", before, got) |
| 95 | } |
| 96 | after, _ := json.Marshal(req) |
| 97 | if !bytes.Equal(before, after) { |
| 98 | t.Fatal("transcript gate mutated caller request") |
| 99 | } |
| 100 | } |
| 101 |