返回 DeepSeek-Reasonix
agent_contract_test.go
根目录 / internal / agent / agent_contract_test.go
1 package agent
2
3 // Core agent-loop contract tests (agent-core simplification baseline). Each
4 // test pins one behavior the simplified loop promises; suites covering the
5 // remaining contract items are listed in docs/AGENT_CORE_SIMPLIFICATION.md.
6
7 import (
8 "context"
9 "reflect"
10 "strings"
11 "testing"
12
13 "reasonix/internal/agent/testutil"
14 "reasonix/internal/event"
15 "reasonix/internal/provider"
16 "reasonix/internal/tool"
17 )
18
19 // TestContractCleanFinalMakesOneModelRequest pins the executor-only happy
20 // path: one user turn, one provider request, no host follow-ups.
21 func TestContractCleanFinalMakesOneModelRequest(t *testing.T) {
22 prov := &scriptedProvider{name: "p", turns: [][]provider.Chunk{
23 {{Type: provider.ChunkText, Text: "the answer"}, {Type: provider.ChunkDone}},
24 }}
25 a := New(prov, tool.NewRegistry(), NewSession(""), Options{}, event.Discard)
26
27 if err := a.Run(context.Background(), "question"); err != nil {
28 t.Fatalf("Run: %v", err)
29 }
30 if len(prov.requests) != 1 {
31 t.Fatalf("provider requests = %d, want exactly one for a clean final", len(prov.requests))
32 }
33 if got := lastAssistantContent(a.sess.conversation); got != "the answer" {
34 t.Fatalf("last assistant content = %q, want the final answer", got)
35 }
36 }
37
38 // TestContractToolCallAdvancesToNextStep pins the tool-loop shape: a tool-call
39 // round executes and the loop continues to a second request for the final.
40 func TestContractToolCallAdvancesToNextStep(t *testing.T) {
41 mp := testutil.NewMock("m",
42 testutil.Turn{ToolCalls: []provider.ToolCall{{ID: "c1", Name: "echo", Arguments: `{"text":"hi"}`}}},
43 testutil.Turn{Text: "all set"},
44 )
45 a := New(mp, echoRegistry(), NewSession(""), Options{}, event.Discard)
46
47 if err := a.Run(withNoClosedLoop(context.Background()), "go"); err != nil {
48 t.Fatalf("Run: %v", err)
49 }
50 if got := mp.CallCount(); got != 2 {
51 t.Fatalf("provider calls = %d, want tool round + final", got)
52 }
53 if got := lastToolResult(a.Session(), "echo"); got == "" {
54 t.Fatal("tool result missing from session; tool round did not execute")
55 }
56 if got := lastAssistantContent(a.sess.conversation); got != "all set" {
57 t.Fatalf("last assistant content = %q, want the post-tool final", got)
58 }
59 }
60
61 // TestContractThinkingSurvivesUnifiedRetry pins that the EMPTY_RESPONSE retry
62 // replays the same frozen request (thinking never disabled or reshaped) and
63 // that recovered reasoning stays on the committed assistant turn.
64 func TestContractThinkingSurvivesUnifiedRetry(t *testing.T) {
65 prov := &scriptedProvider{name: "p", turns: [][]provider.Chunk{
66 {{Type: provider.ChunkDone}},
67 {
68 {Type: provider.ChunkReasoning, Text: "need to think"},
69 {Type: provider.ChunkText, Text: "the answer"},
70 {Type: provider.ChunkDone},
71 },
72 }}
73 a := New(prov, tool.NewRegistry(), NewSession(""), Options{}, event.Discard)
74
75 if err := a.Run(context.Background(), "question"); err != nil {
76 t.Fatalf("Run: %v", err)
77 }
78 if len(prov.requests) != 2 || !reflect.DeepEqual(prov.requests[0], prov.requests[1]) {
79 t.Fatalf("retry must replay the frozen request verbatim:\nfirst=%#v\nsecond=%#v", prov.requests[0], prov.requests[1])
80 }
81 msgs := a.sess.conversation.Snapshot()
82 last := msgs[len(msgs)-1]
83 if last.Role != provider.RoleAssistant || last.ReasoningContent != "need to think" {
84 t.Fatalf("committed assistant turn lost reasoning: %+v", last)
85 }
86 }
87
88 // TestContractNoLongLivedFallbackStateAfterRetryExhaustion pins that exhausted
89 // protocol retries fail the turn without installing any persistent degraded
90 // mode: the next turn runs a normal single-request loop.
91 func TestContractNoLongLivedFallbackStateAfterRetryExhaustion(t *testing.T) {
92 turns := [][]provider.Chunk{}
93 for range maxSamplingAttempts {
94 turns = append(turns, []provider.Chunk{{Type: provider.ChunkDone}})
95 }
96 turns = append(turns, []provider.Chunk{{Type: provider.ChunkText, Text: "recovered"}, {Type: provider.ChunkDone}})
97 prov := &scriptedProvider{name: "p", turns: turns}
98 a := New(prov, tool.NewRegistry(), NewSession(""), Options{}, event.Discard)
99
100 if err := a.Run(context.Background(), "question"); err == nil {
101 t.Fatal("exhausted empty-response retries must fail the turn")
102 }
103 for _, m := range a.sess.conversation.Snapshot() {
104 if m.Role == provider.RoleAssistant && strings.TrimSpace(m.Content) != "" {
105 t.Fatalf("failed turn committed assistant content %q", m.Content)
106 }
107 }
108 if err := a.Run(context.Background(), "try again"); err != nil {
109 t.Fatalf("second Run after exhausted retries: %v", err)
110 }
111 if got := len(prov.requests) - maxSamplingAttempts; got != 1 {
112 t.Fatalf("second turn made %d requests, want a normal single request (no fallback mode)", got)
113 }
114 if got := lastAssistantContent(a.sess.conversation); got != "recovered" {
115 t.Fatalf("last assistant content = %q, want the clean recovery answer", got)
116 }
117 }
118
119 // TestContractCleanFinalAddsNoSyntheticContinuation pins that a direct Run
120 // ends at the first clean final: no host-generated user message may appear in
121 // the session, whatever the model text promises.
122 func TestContractCleanFinalAddsNoSyntheticContinuation(t *testing.T) {
123 prov := &scriptedProvider{name: "p", turns: [][]provider.Chunk{
124 {{Type: provider.ChunkText, Text: "I will handle it next round."}, {Type: provider.ChunkDone}},
125 }}
126 a := New(prov, tool.NewRegistry(), NewSession(""), Options{}, event.Discard)
127
128 if err := a.Run(context.Background(), "question"); err != nil {
129 t.Fatalf("Run: %v", err)
130 }
131 if len(prov.requests) != 1 {
132 t.Fatalf("provider requests = %d, want one; a promised-later answer must not buy a continuation", len(prov.requests))
133 }
134 for _, prefix := range []string{StandardTodoContinuationPrefix, "visible answer", executorHandoffMarker} {
135 if sessionHasUserMessageContaining(a.sess.conversation, prefix) {
136 t.Fatalf("session contains synthetic continuation prompt %q", prefix)
137 }
138 }
139 }
140
140 lines GO