| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "errors" |
| 7 | "io" |
| 8 | "net/http" |
| 9 | "net/http/httptest" |
| 10 | "sync" |
| 11 | "testing" |
| 12 | |
| 13 | "reasonix/internal/event" |
| 14 | "reasonix/internal/provider" |
| 15 | "reasonix/internal/provider/anthropic" |
| 16 | ) |
| 17 | |
| 18 | const missingReasoningToolSSE = `data: {"type":"message_start","message":{"usage":{"input_tokens":10}}} |
| 19 | |
| 20 | data: {"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"toolu_1","name":"echo"}} |
| 21 | |
| 22 | data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"{\"text\":\"hi\"}"}} |
| 23 | |
| 24 | data: {"type":"content_block_stop","index":0} |
| 25 | |
| 26 | data: {"type":"message_delta","delta":{"stop_reason":"tool_use"},"usage":{"output_tokens":8}} |
| 27 | |
| 28 | data: {"type":"message_stop"} |
| 29 | |
| 30 | ` |
| 31 | |
| 32 | const recoveredReasoningToolSSE = `data: {"type":"message_start","message":{"usage":{"input_tokens":10}}} |
| 33 | |
| 34 | data: {"type":"content_block_start","index":0,"content_block":{"type":"thinking"}} |
| 35 | |
| 36 | data: {"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"call echo safely"}} |
| 37 | |
| 38 | data: {"type":"content_block_stop","index":0} |
| 39 | |
| 40 | data: {"type":"content_block_start","index":1,"content_block":{"type":"tool_use","id":"toolu_1","name":"echo"}} |
| 41 | |
| 42 | data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"{\"text\":\"hi\"}"}} |
| 43 | |
| 44 | data: {"type":"content_block_stop","index":1} |
| 45 | |
| 46 | data: {"type":"message_delta","delta":{"stop_reason":"tool_use"},"usage":{"output_tokens":12}} |
| 47 | |
| 48 | data: {"type":"message_stop"} |
| 49 | |
| 50 | ` |
| 51 | |
| 52 | const finalAnswerSSE = `data: {"type":"message_start","message":{"usage":{"input_tokens":20}}} |
| 53 | |
| 54 | data: {"type":"content_block_start","index":0,"content_block":{"type":"text"}} |
| 55 | |
| 56 | data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"done"}} |
| 57 | |
| 58 | data: {"type":"content_block_stop","index":0} |
| 59 | |
| 60 | data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":2}} |
| 61 | |
| 62 | data: {"type":"message_stop"} |
| 63 | |
| 64 | ` |
| 65 | |
| 66 | func TestOpenCodeGoAnthropicMissingReasoningRecoversBeforeToolExecution(t *testing.T) { |
| 67 | var mu sync.Mutex |
| 68 | var bodies [][]byte |
| 69 | responses := []string{missingReasoningToolSSE, recoveredReasoningToolSSE, finalAnswerSSE} |
| 70 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 71 | body, err := io.ReadAll(r.Body) |
| 72 | if err != nil { |
| 73 | t.Errorf("read request: %v", err) |
| 74 | w.WriteHeader(http.StatusInternalServerError) |
| 75 | return |
| 76 | } |
| 77 | mu.Lock() |
| 78 | bodies = append(bodies, body) |
| 79 | i := len(bodies) - 1 |
| 80 | mu.Unlock() |
| 81 | if i >= len(responses) { |
| 82 | t.Errorf("unexpected request %d", i+1) |
| 83 | w.WriteHeader(http.StatusInternalServerError) |
| 84 | return |
| 85 | } |
| 86 | w.Header().Set("Content-Type", "text/event-stream") |
| 87 | _, _ = io.WriteString(w, responses[i]) |
| 88 | })) |
| 89 | defer srv.Close() |
| 90 | |
| 91 | prov, err := anthropic.New(provider.Config{ |
| 92 | Name: "opencode-go-deepseek", BaseURL: srv.URL, Model: "deepseek-v4-flash", APIKey: "test-key", |
| 93 | Extra: map[string]any{"reasoning_protocol": "deepseek", "thinking": "adaptive", "effort": "high", "web_search": true}, |
| 94 | }) |
| 95 | if err != nil { |
| 96 | t.Fatalf("new provider: %v", err) |
| 97 | } |
| 98 | sink := &recordSink{} |
| 99 | stateDir := t.TempDir() |
| 100 | agent := New(prov, echoRegistry(), NewSession(""), Options{MissingReasoningWarnStateDir: stateDir}, sink) |
| 101 | if err := agent.Run(withNoClosedLoop(context.Background()), "go"); err != nil { |
| 102 | t.Fatalf("Run: %v", err) |
| 103 | } |
| 104 | |
| 105 | mu.Lock() |
| 106 | defer mu.Unlock() |
| 107 | if len(bodies) != 3 { |
| 108 | t.Fatalf("HTTP requests = %d, want malformed turn, exact retry, and final turn", len(bodies)) |
| 109 | } |
| 110 | if !bytes.Equal(bodies[0], bodies[1]) { |
| 111 | t.Fatal("missing-reasoning recovery did not retry the exact frozen request") |
| 112 | } |
| 113 | for _, wire := range [][]byte{ |
| 114 | []byte(`"thinking":{"type":"enabled"}`), |
| 115 | []byte(`"output_config":{"effort":"high"}`), |
| 116 | []byte(`{"type":"web_search_20250305","name":"web_search"}`), |
| 117 | } { |
| 118 | if !bytes.Contains(bodies[0], wire) { |
| 119 | t.Fatalf("OpenCode Go preset request is missing %s", wire) |
| 120 | } |
| 121 | } |
| 122 | if got := sink.recoveryCount(event.ProtocolRecoveryMissingReasoningRetryAttempted); got != 1 { |
| 123 | t.Fatalf("missing-reasoning retries = %d, want 1", got) |
| 124 | } |
| 125 | if got := len(sink.kinds(event.ToolResult)); got != 1 { |
| 126 | t.Fatalf("tool results = %d, want exactly one execution after recovery", got) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func TestOpenCodeGoAnthropicRepeatedMissingReasoningStopsAfterOneExactRetry(t *testing.T) { |
| 131 | var mu sync.Mutex |
| 132 | var bodies [][]byte |
| 133 | responses := []string{missingReasoningToolSSE, missingReasoningToolSSE} |
| 134 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 135 | body, err := io.ReadAll(r.Body) |
| 136 | if err != nil { |
| 137 | t.Errorf("read request: %v", err) |
| 138 | w.WriteHeader(http.StatusInternalServerError) |
| 139 | return |
| 140 | } |
| 141 | mu.Lock() |
| 142 | bodies = append(bodies, body) |
| 143 | i := len(bodies) - 1 |
| 144 | mu.Unlock() |
| 145 | if i >= len(responses) { |
| 146 | t.Errorf("unexpected request %d", i+1) |
| 147 | w.WriteHeader(http.StatusInternalServerError) |
| 148 | return |
| 149 | } |
| 150 | w.Header().Set("Content-Type", "text/event-stream") |
| 151 | _, _ = io.WriteString(w, responses[i]) |
| 152 | })) |
| 153 | defer srv.Close() |
| 154 | |
| 155 | prov, err := anthropic.New(provider.Config{ |
| 156 | Name: "opencode-go-deepseek", BaseURL: srv.URL, Model: "deepseek-v4-flash", APIKey: "test-key", |
| 157 | Extra: map[string]any{"reasoning_protocol": "deepseek", "thinking": "adaptive", "effort": "high", "web_search": true}, |
| 158 | }) |
| 159 | if err != nil { |
| 160 | t.Fatalf("new provider: %v", err) |
| 161 | } |
| 162 | sink := &recordSink{} |
| 163 | agent := New(prov, echoRegistry(), NewSession(""), Options{}, sink) |
| 164 | var replayErr *ReasoningReplayError |
| 165 | if err := agent.Run(withNoClosedLoop(context.Background()), "go"); !errors.As(err, &replayErr) { |
| 166 | t.Fatalf("Run error = %v, want ReasoningReplayError", err) |
| 167 | } |
| 168 | |
| 169 | mu.Lock() |
| 170 | defer mu.Unlock() |
| 171 | if len(bodies) != 2 { |
| 172 | t.Fatalf("HTTP requests = %d, want one original request and one exact retry", len(bodies)) |
| 173 | } |
| 174 | if !bytes.Equal(bodies[0], bodies[1]) { |
| 175 | t.Fatal("protocol retry changed the frozen request") |
| 176 | } |
| 177 | if bytes.Contains(bodies[1], []byte(`"thinking":{"type":"disabled"}`)) { |
| 178 | t.Fatal("repeated missing reasoning entered a disabled-thinking fallback") |
| 179 | } |
| 180 | if got := sink.recoveryCount(event.ProtocolRecoveryMissingReasoningRetryAttempted); got != 1 { |
| 181 | t.Fatalf("missing-reasoning retries = %d, want one", got) |
| 182 | } |
| 183 | } |
| 184 |