返回 DeepSeek-Reasonix
contextual_tool_test.go
根目录 / internal / agent / contextual_tool_test.go
1 package agent
2
3 import (
4 "context"
5 "encoding/json"
6 "strings"
7 "sync/atomic"
8 "testing"
9
10 "reasonix/internal/event"
11 "reasonix/internal/provider"
12 "reasonix/internal/tool"
13 )
14
15 type unavailableTool struct {
16 fakeTool
17 calls *int32
18 }
19
20 func (u unavailableTool) ProviderVisible(context.Context) bool { return false }
21
22 func (u unavailableTool) Execute(ctx context.Context, args json.RawMessage) (string, error) {
23 if u.calls != nil {
24 atomic.AddInt32(u.calls, 1)
25 }
26 return u.fakeTool.Execute(ctx, args)
27 }
28
29 func TestContextualToolHostGateRunsBeforePermissionAndExecute(t *testing.T) {
30 var executions int32
31 reg := tool.NewRegistry()
32 reg.Add(unavailableTool{fakeTool: fakeTool{name: "phase_tool", readOnly: false}, calls: &executions})
33 gate := &recordingPermissionGate{allow: true}
34 a := New(nil, reg, NewSession("sys"), Options{Gate: gate}, event.Discard)
35
36 out := a.executeOne(context.Background(), &a.turn, provider.ToolCall{ID: "phase", Name: "phase_tool", Arguments: `{}`})
37 if !out.blocked || !strings.Contains(out.output, "unavailable") {
38 t.Fatalf("contextual tool outcome = %+v", out)
39 }
40 if executions != 0 || len(gate.calls) != 0 {
41 t.Fatalf("unavailable tool crossed host gate: executions=%d permission=%+v", executions, gate.calls)
42 }
43 }
44
45 func TestMixedContextualBatchExecutesAvailableCallsOnce(t *testing.T) {
46 var executions int32
47 reg := tool.NewRegistry()
48 reg.Add(unavailableTool{fakeTool: fakeTool{name: "phase_tool", readOnly: true}, calls: &executions})
49 reg.Add(fakeTool{name: "read_file", readOnly: true, calls: &executions})
50 prov := &scriptedProvider{name: "p", turns: [][]provider.Chunk{
51 {toolCallChunk("phase", "phase_tool", `{}`), toolCallChunk("read", "read_file", `{}`), {Type: provider.ChunkDone}},
52 {{Type: provider.ChunkText, Text: "answer after the available read"}, {Type: provider.ChunkDone}},
53 }}
54 a := New(prov, reg, NewSession("sys"), Options{}, event.Discard)
55 if err := a.Run(context.Background(), "inspect the file"); err != nil {
56 t.Fatalf("mixed contextual batch failed: %v", err)
57 }
58 if got := atomic.LoadInt32(&executions); got != 1 {
59 t.Fatalf("available tool executions = %d, want exactly one", got)
60 }
61 if got := lastToolResult(a.Session(), "phase_tool"); !strings.Contains(got, "unavailable") {
62 t.Fatalf("contextual result = %q", got)
63 }
64 }
65
66 func TestRepeatedMixedContextualBatchKeepsIndependentCallsRunning(t *testing.T) {
67 var executions int32
68 reg := tool.NewRegistry()
69 reg.Add(unavailableTool{fakeTool: fakeTool{name: "phase_tool", readOnly: true}, calls: &executions})
70 reg.Add(fakeTool{name: "read_file", readOnly: true, calls: &executions})
71 prov := &scriptedProvider{name: "p", turns: [][]provider.Chunk{
72 {toolCallChunk("phase-1", "phase_tool", `{}`), toolCallChunk("read-1", "read_file", `{}`), {Type: provider.ChunkDone}},
73 {toolCallChunk("phase-2", "phase_tool", `{}`), toolCallChunk("read-2", "read_file", `{}`), {Type: provider.ChunkDone}},
74 {{Type: provider.ChunkText, Text: "answer after repeated local failures"}, {Type: provider.ChunkDone}},
75 }}
76 a := New(prov, reg, NewSession("sys"), Options{}, event.Discard)
77 if err := a.Run(context.Background(), "inspect the file"); err != nil {
78 t.Fatalf("repeated contextual batch failed: %v", err)
79 }
80 if got := atomic.LoadInt32(&executions); got != 2 {
81 t.Fatalf("available tool executions = %d, want both independent calls", got)
82 }
83 if got := lastToolResult(a.Session(), "phase_tool"); !strings.Contains(got, "unavailable") {
84 t.Fatalf("second unavailable call result = %q", got)
85 }
86 if prov.call != 3 {
87 t.Fatalf("provider calls = %d, want a normal final round", prov.call)
88 }
89 }
90
91 func TestRepeatedPureContextualCallWithAnswerRemainsRecoverable(t *testing.T) {
92 reg := tool.NewRegistry()
93 reg.Add(unavailableTool{fakeTool: fakeTool{name: "phase_tool", readOnly: true}})
94 prov := &scriptedProvider{name: "p", turns: [][]provider.Chunk{
95 {toolCallChunk("phase-1", "phase_tool", `{}`), {Type: provider.ChunkDone}},
96 {{Type: provider.ChunkText, Text: "占位 Lorem 占位 ipsum — the request is fully handled."}, toolCallChunk("phase-2", "phase_tool", `{}`), {Type: provider.ChunkDone}},
97 {{Type: provider.ChunkText, Text: "final answer"}, {Type: provider.ChunkDone}},
98 }}
99 a := New(prov, reg, NewSession("sys"), Options{}, event.Discard)
100 if err := a.Run(context.Background(), "answer normally"); err != nil {
101 t.Fatalf("repeated contextual call failed: %v", err)
102 }
103 if prov.call != 3 {
104 t.Fatalf("provider calls = %d, want a normal final round", prov.call)
105 }
106 if got := lastToolResult(a.Session(), "phase_tool"); !strings.Contains(got, "unavailable") {
107 t.Fatalf("second unavailable call result = %q", got)
108 }
109 }
110
110 lines GO