Skip to content

Commit eeaa38e

Browse files
roberttwangclaude
andcommitted
fix(settings): name the model when CodeBuddy answers with an empty result
A mistyped or unavailable model ID makes CodeBuddy stream the reason only in an assistant text block ("400 model [x] service info not found") and finish with an empty result payload, so the connection test classified it as a generic MISSING_RESULT and told the user to retry. Keep the last assistant text and pass it to the failure classifier, accept the bracketed model form, and have the connection test name the model it could not use. Co-Authored-By: Claude Code <noreply@anthropic.com>
1 parent 436436a commit eeaa38e

4 files changed

Lines changed: 78 additions & 1 deletion

File tree

‎internal/httpapi/assistant.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,10 @@ func runAssistantCLI(ctx context.Context, req assistantCLIRequest, user domain.U
17431743
var streamFilter assistantStreamFilter
17441744
var stderrTail []string
17451745
var missingSession, modelStarted, credentialRejected bool
1746+
// The CLI sometimes reports the reason (e.g. an unknown model) only in an
1747+
// assistant text block and exits with an empty result payload; keep it so
1748+
// the failure classification can name the real cause.
1749+
var lastAssistantText string
17461750
var usageCollector assistantCLIUsage
17471751
if engine == assistantEngineCodeBuddy || engine == assistantEngineClaude {
17481752
usageCollector.session = newAssistantSessionUsage(root, sessionID, req.Resume, engine)
@@ -1833,6 +1837,11 @@ func runAssistantCLI(ctx context.Context, req assistantCLIRequest, user domain.U
18331837
resetClaudeText := result.Message.ID == "" || result.Message.ID != claudeMessageID
18341838
for _, block := range result.Message.Content {
18351839
credentialRejected = credentialRejected || isCredentialRejection(block.Text)
1840+
if block.Type == "text" {
1841+
if text := strings.TrimSpace(block.Text); text != "" && !assistantCLINoticeText(text) {
1842+
lastAssistantText = text
1843+
}
1844+
}
18361845
if !captureClaudeText || result.ParentToolUseID != "" || block.Type != "text" {
18371846
continue
18381847
}
@@ -1910,6 +1919,9 @@ func runAssistantCLI(ctx context.Context, req assistantCLIRequest, user domain.U
19101919
assistantDiagnosticsFrom(ctx).platform("protocol.result_text_recovered", "Claude Code 本轮成功但结果正文为空,采用已收到的回复", user)
19111920
}
19121921
out, err := decodeAssistantCLIResult(final, resultError, err, stderrTail)
1922+
if err != nil && errors.Is(err, errAssistantMissingResult) && lastAssistantText != "" {
1923+
err = fmt.Errorf("%w; assistant=%s", err, truncateRunes(lastAssistantText, 500))
1924+
}
19131925
if err != nil && req.RunnerLogin && ctx.Err() == nil && !errors.Is(err, runtime.ErrConversationNotStarted) {
19141926
text := strings.ToLower(err.Error())
19151927
if assistantAuthFailureText(text) || strings.Contains(text, "not logged in") || strings.Contains(text, "login expired") || strings.Contains(text, "please run /login") {

‎internal/httpapi/assistant_failure.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,10 @@ func classifyAssistantFailureParts(f domain.AssistantFailure, err error) (domain
162162
return c, false
163163
}
164164

165-
var assistantModelMissingRe = regexp.MustCompile("(?i)\\bmodel\\s+[`'\"]?[^\\s`'\"]*[`'\"]?\\s*(?:does not exist|not found|is not available)")
165+
// CodeBuddy reports an unknown model as `400 model [id] service info not found`
166+
// and OpenAI-compatible providers as `The model `x` does not exist ...`, so the
167+
// pattern accepts a bracketed, quoted or bare ID plus the provider's wording.
168+
var assistantModelMissingRe = regexp.MustCompile("(?i)\\bmodel\\s*[`'\"]?(?:\\[[^\\]]+\\]|[^\\s`'\"]*)[`'\"]?\\s*(?:does not exist|not found|is not available|service info not found)")
166169

167170
func classifyAssistantFailureText(f domain.AssistantFailure, err error, text string) domain.AssistantFailure {
168171
message := strings.ToLower(text)

‎internal/httpapi/assistant_failure_test.go‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,56 @@ func TestFailedSharedGenerationSnapshotKeepsCauseAndRetryHistory(t *testing.T) {
328328
})
329329
}
330330
}
331+
332+
func TestAssistantCLIReportsModelRejectionFromAssistantText(t *testing.T) {
333+
// CodeBuddy answers a mistyped model ID with an assistant text block and a
334+
// result whose payload is empty; the model error must survive the decode so
335+
// the connection test can say the model is unavailable instead of "retry".
336+
for _, tc := range []struct {
337+
name, text string
338+
}{
339+
{"bracketed model", "400 model [deepseek-v4.1-flsh] service info not found"},
340+
{"plain model", "400 model deepseek-v4.1-flsh not found"},
341+
} {
342+
t.Run(tc.name, func(t *testing.T) {
343+
dir := t.TempDir()
344+
bin := filepath.Join(dir, "codebuddy")
345+
script := "#!/bin/sh\ncat <<'RESULT'\n" +
346+
`{"type":"assistant","message":{"content":[{"type":"text","text":"` + tc.text + `"}]}}` + "\n" +
347+
`{"type":"result","subtype":"error_during_execution","is_error":true,"result":""}` + "\nRESULT\n"
348+
if err := os.WriteFile(bin, []byte(script), 0o700); err != nil {
349+
t.Fatal(err)
350+
}
351+
t.Setenv("AGENTFLOW_CODEBUDDY_BIN", bin)
352+
_, _, err := runAssistantCLI(context.Background(), assistantCLIRequest{Prompt: "Reply with OK.", SessionRoot: dir}, domain.User{ID: 1, CodeBuddyAPIKey: "test-private-key"}, nil)
353+
if err == nil {
354+
t.Fatal("empty result was accepted")
355+
}
356+
failure := classifyAssistantFailureText(domain.AssistantFailure{}, err, err.Error())
357+
if failure.Code != "MODEL_UNAVAILABLE" {
358+
t.Fatalf("code=%s message=%q err=%v", failure.Code, failure.Message, err)
359+
}
360+
})
361+
}
362+
}
363+
364+
func TestTestCodeBuddyConnectionNamesInvalidModel(t *testing.T) {
365+
dir := t.TempDir()
366+
bin := filepath.Join(dir, "codebuddy")
367+
script := "#!/bin/sh\n" +
368+
"for a in \"$@\"; do case \"$a\" in --help) echo 'Available models:'; echo ' deepseek-v4.1-flash'; exit 0;; esac; done\n" +
369+
"cat <<'RESULT'\n" +
370+
`{"type":"assistant","message":{"content":[{"type":"text","text":"400 model [deepseek-v4.1-flsh] service info not found"}]}}` + "\n" +
371+
`{"type":"result","subtype":"error_during_execution","is_error":true,"result":""}` + "\nRESULT\n"
372+
if err := os.WriteFile(bin, []byte(script), 0o700); err != nil {
373+
t.Fatal(err)
374+
}
375+
t.Setenv("AGENTFLOW_CODEBUDDY_BIN", bin)
376+
result := testOfficialCodeBuddyConnection(context.Background(), "ioa", "test-private-key", "deepseek-v4.1-flsh")
377+
if result.Connected || result.Error == "" {
378+
t.Fatalf("mistyped model reported as connected: %+v", result)
379+
}
380+
if !strings.Contains(result.Error, "deepseek-v4.1-flsh") || strings.Contains(result.Error, "未返回完整回答") {
381+
t.Fatalf("error %q does not name the model", result.Error)
382+
}
383+
}

‎internal/httpapi/cli_models_probe.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,15 @@ func testOfficialCodeBuddyConnection(ctx context.Context, environment, apiKey, m
213213
return cliModelProbeResult{Connected: true, Source: "official"}
214214
}
215215
failure := classifyAssistantFailureText(domain.AssistantFailure{}, err, err.Error())
216+
switch failure.Code {
217+
case "MODEL_UNAVAILABLE":
218+
failure.Message = "CodeBuddy 无法使用模型 " + model + ",请确认模型 ID 拼写正确,并在当前网络环境下可用"
219+
case "MISSING_RESULT":
220+
// An empty answer with no recognisable reason is almost always a model
221+
// the current network environment cannot serve, so name the model and
222+
// the way out instead of asking the user to retry blindly.
223+
failure.Message = "CodeBuddy 没有返回回答,请确认模型 " + model + " 在当前网络环境下可用,或换成模型目录里的其他模型"
224+
}
216225
if failure.Message != "" {
217226
return cliModelProbeResult{Error: failure.Message}
218227
}

0 commit comments

Comments
 (0)