返回 DeepSeek-Reasonix
planner_adaptive_research_test.go
根目录 / internal / agent / planner_adaptive_research_test.go
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 func TestCoordinatorPlannerDepthDoesNotCapResearchRounds(t *testing.T) {
14 planner := &mockProvider{name: "planner", streams: [][]provider.Chunk{
15 {{Type: provider.ChunkToolCall, ToolCall: &provider.ToolCall{ID: "call-1", Name: "read_file", Arguments: `{"path":"a"}`}}, {Type: provider.ChunkDone}},
16 {{Type: provider.ChunkToolCall, ToolCall: &provider.ToolCall{ID: "call-2", Name: "read_file", Arguments: `{"path":"b"}`}}, {Type: provider.ChunkDone}},
17 {{Type: provider.ChunkToolCall, ToolCall: &provider.ToolCall{ID: "call-3", Name: "read_file", Arguments: `{"path":"c"}`}}, {Type: provider.ChunkDone}},
18 {{Type: provider.ChunkToolCall, ToolCall: &provider.ToolCall{ID: "plan-1", Name: "submit_plan", Arguments: `{"objective":"apply the narrow change","steps":[{"title":"run the focused test","verified_files":["a","b","c"]}]}`}}, {Type: provider.ChunkDone}},
19 }}
20 exec := &mockProvider{name: "executor", chunks: []provider.Chunk{{Type: provider.ChunkText, Text: "Done."}, {Type: provider.ChunkDone}}}
21 reg := tool.NewRegistry()
22 reg.Add(coordinatorTestTool{name: "read_file", readOnly: true, output: "ok"})
23 policy := func(context.Context, string) PlannerDecision {
24 return PlannerDecision{Route: PlannerRoutePlanAndExecute, Reason: "adaptive_work"}
25 }
26 executor := New(exec, tool.NewRegistry(), NewSession("exec-sys"), Options{}, event.Discard)
27 coord := NewCoordinatorWithPlannerPolicy(planner, NewSession("planner-sys"), nil,
28 PlannerToolRegistry(reg), Options{}, executor, 0, event.Discard, policy)
29
30 if err := coord.Run(withNoClosedLoop(context.Background()), "make the adaptive change"); err != nil {
31 t.Fatalf("Run: %v", err)
32 }
33 if got := len(planner.requests); got != 4 {
34 t.Fatalf("planner requests = %d, want three evidence rounds plus submit_plan", got)
35 }
36 for i, req := range planner.requests {
37 if got := lastUser(req); strings.Contains(got, "research rounds") || strings.Contains(got, "safety boundary") {
38 t.Fatalf("planner request %d received a fixed-round nudge: %q", i, got)
39 }
40 }
41 if len(exec.requests) == 0 || !strings.Contains(lastUser(exec.requests[0]), executorHandoffMarker) {
42 t.Fatal("executor did not receive the submitted plan")
43 }
44 }
45
46 func TestCoordinatorEmergencyBoundedPlannerCanSubmitPlanInFinalizationRound(t *testing.T) {
47 planner := &mockProvider{name: "planner", streams: [][]provider.Chunk{
48 {{Type: provider.ChunkToolCall, ToolCall: &provider.ToolCall{ID: "call-1", Name: "read_file", Arguments: `{"path":"a"}`}}, {Type: provider.ChunkDone}},
49 {{Type: provider.ChunkToolCall, ToolCall: &provider.ToolCall{ID: "call-2", Name: "read_file", Arguments: `{"path":"b"}`}}, {Type: provider.ChunkDone}},
50 {{Type: provider.ChunkToolCall, ToolCall: &provider.ToolCall{ID: "plan-1", Name: "submit_plan", Arguments: `{"objective":"fix the bounded planner","steps":[{"title":"apply the owner-level fix","verified_files":["a","b"]}]}`}}, {Type: provider.ChunkDone}},
51 }}
52 exec := &mockProvider{name: "executor", chunks: []provider.Chunk{{Type: provider.ChunkText, Text: "Done."}, {Type: provider.ChunkDone}}}
53 reg := tool.NewRegistry()
54 reg.Add(coordinatorTestTool{name: "read_file", readOnly: true, output: strings.Repeat("research evidence\n", 10_000)})
55 var notices []event.Event
56 sink := event.FuncSink(func(e event.Event) {
57 if e.Kind == event.Notice {
58 notices = append(notices, e)
59 }
60 })
61 executor := New(exec, tool.NewRegistry(), NewSession("exec-sys"), Options{}, event.Discard)
62 plannerSess := NewSession("planner-sys")
63 coord := NewCoordinatorWithPlannerPolicy(planner, plannerSess, nil, PlannerToolRegistry(reg),
64 Options{MaxSteps: 2, MaxStepsKey: "planner emergency rounds"}, executor, 0, sink,
65 func(context.Context, string) PlannerDecision {
66 return PlannerDecision{Route: PlannerRoutePlanAndExecute, Reason: "emergency_bounded_work"}
67 })
68
69 if err := coord.Run(withNoClosedLoop(context.Background()), "fix the planner bug"); err != nil {
70 t.Fatalf("Run: %v", err)
71 }
72 if got := len(planner.requests); got != 3 {
73 t.Fatalf("planner requests = %d, want two research rounds plus terminal submission", got)
74 }
75 if got := lastUser(planner.requests[2]); !strings.Contains(got, "call submit_plan now") || strings.Contains(got, "Do not call any more tools") {
76 t.Fatalf("planner finalization nudge conflicts with submit_plan: %q", got)
77 }
78 sawTruncation := false
79 for _, notice := range notices {
80 sawTruncation = sawTruncation || strings.HasPrefix(notice.Text, "tool output truncated:")
81 }
82 if !sawTruncation {
83 t.Fatal("test setup did not reproduce truncated planner research")
84 }
85 if len(exec.requests) == 0 || !strings.Contains(lastUser(exec.requests[0]), "**Objective** — fix the bounded planner") {
86 t.Fatal("executor did not receive the structured bounded plan")
87 }
88 messages := plannerSess.Snapshot()
89 if last := messages[len(messages)-1]; last.Role != provider.RoleAssistant || last.Content != plannerPlanSubmittedClosure {
90 t.Fatalf("planner transcript did not close deterministically: %+v", last)
91 }
92 }
93
94 func TestCoordinatorTaskBudgetLetsPlannerSubmitTerminalPlan(t *testing.T) {
95 planner := &mockProvider{name: "planner", streams: [][]provider.Chunk{
96 {
97 {Type: provider.ChunkToolCall, ToolCall: &provider.ToolCall{ID: "call-1", Name: "read_file", Arguments: `{"path":"main.go"}`}},
98 {Type: provider.ChunkUsage, Usage: &provider.Usage{PromptTokens: 10, TotalTokens: 10}},
99 {Type: provider.ChunkDone},
100 },
101 {{Type: provider.ChunkToolCall, ToolCall: &provider.ToolCall{ID: "plan-1", Name: "submit_plan", Arguments: `{"objective":"finish within the safety envelope","steps":[{"title":"apply the verified change","verified_files":["main.go"]}]}`}}, {Type: provider.ChunkDone}},
102 }}
103 exec := &mockProvider{name: "executor", chunks: []provider.Chunk{{Type: provider.ChunkText, Text: "Done."}, {Type: provider.ChunkDone}}}
104 reg := tool.NewRegistry()
105 reg.Add(coordinatorTestTool{name: "read_file", readOnly: true, output: "package main"})
106 var notices []event.Event
107 sink := event.FuncSink(func(e event.Event) {
108 if e.Kind == event.Notice {
109 notices = append(notices, e)
110 }
111 })
112 executor := New(exec, tool.NewRegistry(), NewSession("exec-sys"), Options{}, event.Discard)
113 coord := NewCoordinatorWithPlannerPolicy(planner, NewSession("planner-sys"), nil, PlannerToolRegistry(reg),
114 Options{}, executor, 0, sink, func(context.Context, string) PlannerDecision {
115 return PlannerDecision{Route: PlannerRoutePlanAndExecute, Reason: "budgeted_work"}
116 })
117
118 ctx := withNoClosedLoop(WithTaskBudget(context.Background(), TaskBudget{Tokens: 1}))
119 if err := coord.Run(ctx, "plan the budgeted change"); err != nil {
120 t.Fatalf("Run: %v", err)
121 }
122 if got := len(planner.requests); got != 2 {
123 t.Fatalf("planner requests = %d, want research plus terminal submit_plan", got)
124 }
125 if got := lastUser(planner.requests[1]); !strings.Contains(got, "reached its token budget") || !strings.Contains(got, "call submit_plan now") {
126 t.Fatalf("task-budget finalization did not request terminal plan: %q", got)
127 }
128 if len(exec.requests) == 0 || !strings.Contains(lastUser(exec.requests[0]), "finish within the safety envelope") {
129 t.Fatal("executor did not receive the task-budget terminal plan")
130 }
131 }
132
132 lines GO