Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(qoder): deliver final answer only (MUL-5394) (#6022)
YYClaw54469766fdd6
diff --git a/server/pkg/agent/qoder.go b/server/pkg/agent/qoder.goindex 6f2d7e241e..032d9ad646 100644--- a/server/pkg/agent/qoder.go+++ b/server/pkg/agent/qoder.go@@ -142,7 +142,14 @@ func (b *qoderBackend) Execute(ctx context.Context, prompt string, opts ExecOptiresCh := make(chan Result, 1)var outputMu sync.Mutex- var output strings.Builder+ // Result.Output is the final user-facing output selected by the backend.+ // Qoder emits interim narration and the final answer as the same ACP+ // AgentMessageChunk type, with tool calls as the only reliable boundary.+ // Keep the complete text for provider-error detection, while deliverable+ // holds only the text block after the latest tool call. This boundary is a+ // heuristic until Qoder exposes an explicit final-answer marker.+ var fullOutput, deliverableOutput strings.Builder+ var lastTextBlock stringvar streamingCurrentTurn atomic.BoolpromptDone := make(chan hermesPromptResult, 1)@@ -162,11 +169,18 @@ func (b *qoderBackend) Execute(ctx context.Context, prompt string, opts ExecOptiif msg.Type == MessageToolUse {msg.Tool = kimiToolNameFromTitle(msg.Tool)}- if msg.Type == MessageText {- outputMu.Lock()- output.WriteString(msg.Content)- outputMu.Unlock()+ outputMu.Lock()+ switch msg.Type {+ case MessageText:+ fullOutput.WriteString(msg.Content)+ deliverableOutput.WriteString(msg.Content)+ case MessageToolUse:+ if block := deliverableOutput.String(); strings.TrimSpace(block) != "" {+ lastTextBlock = block+ }+ deliverableOutput.Reset()}+ outputMu.Unlock()msgStream.send(msg)},onPromptDone: func(result hermesPromptResult) {@@ -406,7 +420,11 @@ func (b *qoderBackend) Execute(ctx context.Context, prompt string, opts ExecOptistreamingCurrentTurn.Store(false)outputMu.Lock()- finalOutput := output.String()+ finalOutput := deliverableOutput.String()+ if strings.TrimSpace(finalOutput) == "" {+ finalOutput = lastTextBlock+ }+ providerErrorOutput := fullOutput.String()outputMu.Unlock()// Promote completed→failed when stderr or the agent text stream show a@@ -414,7 +432,7 @@ func (b *qoderBackend) Execute(ctx context.Context, prompt string, opts ExecOpti// Mirrors hermes/kimi/kiro; without it a run that exhausts retries still// reports "completed" because session/prompt ends with stopReason=end_turn// even though qodercli wrote a terminal error to stderr.- finalStatus, finalError = promoteACPResultOnProviderError(finalStatus, finalError, finalOutput, providerErr)+ finalStatus, finalError = promoteACPResultOnProviderError(finalStatus, finalError, providerErrorOutput, providerErr)c.usageMu.Lock()u := c.usagediff --git a/server/pkg/agent/qoder_test.go b/server/pkg/agent/qoder_test.goindex 889ed76f0f..21b03529af 100644--- a/server/pkg/agent/qoder_test.go+++ b/server/pkg/agent/qoder_test.go@@ -42,6 +42,47 @@ done`}+func fakeQoderACPScriptWithNarrationAndFinalAnswer() string {+ return `#!/bin/sh+# Mirrors a real Qoder ACP turn: interim narration, a tool call, then the+# user-facing answer. All text chunks belong in the transcript, but only the+# post-tool chunk is the backend's deliverable Result.Output.+while IFS= read -r line; do+ id=$(printf '%s' "$line" | sed -n 's/.*"id":\([0-9]*\).*/\1/p')+ case "$line" in+ *'"method":"initialize"'*)+ printf '{"jsonrpc":"2.0","id":%s,"result":{"protocolVersion":1,"agentCapabilities":{}}}\n' "$id"+ ;;+ *'"method":"session/new"'*)+ printf '{"jsonrpc":"2.0","id":%s,"result":{"sessionId":"ses_final"}}\n' "$id"+ ;;+ *'"method":"session/prompt"'*)+ narration='I will inspect the attachment.'+ if [ -n "$QODER_TEXT_BEFORE_TOOL" ]; then+ narration=$QODER_TEXT_BEFORE_TOOL+ fi+ if [ "$QODER_TERMINAL_ERROR_BEFORE_TOOL" = "1" ]; then+ narration='API call failed after 3 retries: HTTP 429'+ fi+ printf '{"jsonrpc":"2.0","method":"session/notification","params":{"sessionId":"ses_final","update":{"type":"AgentMessageChunk","content":{"type":"text","text":"%s"}}}}\n' "$narration"+ if [ "$QODER_DEFERRED_TOOL" = "1" ]; then+ printf '{"jsonrpc":"2.0","method":"session/notification","params":{"sessionId":"ses_final","update":{"type":"ToolCall","toolCallId":"tc-read","name":"read_file","status":"pending"}}}\n'+ else+ printf '{"jsonrpc":"2.0","method":"session/notification","params":{"sessionId":"ses_final","update":{"type":"ToolCall","toolCallId":"tc-read","name":"read_file","status":"pending","parameters":{"path":"diagram.svg"}}}}\n'+ fi+ printf '{"jsonrpc":"2.0","method":"session/notification","params":{"sessionId":"ses_final","update":{"type":"ToolCallUpdate","toolCallId":"tc-read","name":"read_file","status":"completed","output":"<svg />"}}}\n'+ if [ "$QODER_TOOL_TERMINATED" != "1" ]; then+ printf '{"jsonrpc":"2.0","method":"session/notification","params":{"sessionId":"ses_final","update":{"type":"AgentMessageChunk","content":{"type":"text","text":"The diagram shows "}}}}\n'+ printf '{"jsonrpc":"2.0","method":"session/notification","params":{"sessionId":"ses_final","update":{"type":"AgentMessageChunk","content":{"type":"text","text":"the service architecture."}}}}\n'+ fi+ printf '{"jsonrpc":"2.0","id":%s,"result":{"stopReason":"end_turn","usage":{"inputTokens":10,"outputTokens":20}}}\n' "$id"+ exit 0+ ;;+ esac+done+`+}+func fakeQoderACPStaleResumeScript() string {return `#!/bin/shwhile IFS= read -r line; do@@ -360,6 +401,149 @@ func TestQoderBackendHappyPath(t *testing.T) {}}+func TestQoderBackendDeliversFinalAnswerWithoutNarration(t *testing.T) {+ for _, test := range []struct {+ name string+ env map[string]string+ }{+ {name: "tool use emitted at start"},+ {name: "tool use deferred until completion", env: map[string]string{"QODER_DEFERRED_TOOL": "1"}},+ } {+ t.Run(test.name, func(t *testing.T) {+ t.Parallel()++ fakePath := filepath.Join(t.TempDir(), "qodercli")+ writeTestExecutable(t, fakePath, []byte(fakeQoderACPScriptWithNarrationAndFinalAnswer()))++ backend, err := New("qoder", Config{+ ExecutablePath: fakePath,+ Logger: slog.Default(),+ Env: test.env,+ })+ if err != nil {+ t.Fatalf("new qoder backend: %v", err)+ }++ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)+ defer cancel()++ session, err := backend.Execute(ctx, "inspect this attachment", ExecOptions{Timeout: 30 * time.Second})+ if err != nil {+ t.Fatalf("execute: %v", err)+ }++ var streamed []Message+ messagesDone := make(chan struct{})+ go func() {+ defer close(messagesDone)+ for msg := range session.Messages {+ streamed = append(streamed, msg)+ }+ }()++ result := <-session.Result+ <-messagesDone++ const want = "The diagram shows the service architecture."+ if result.Status != "completed" {+ t.Fatalf("status=%q err=%q", result.Status, result.Error)+ }+ if result.Output != want {+ t.Fatalf("Result.Output = %q, want %q", result.Output, want)+ }++ var text []string+ for _, msg := range streamed {+ if msg.Type == MessageText {+ text = append(text, msg.Content)+ }+ }+ if got := strings.Join(text, "|"); got != "I will inspect the attachment.|The diagram shows |the service architecture." {+ t.Fatalf("transcript text = %q, want narration and final answer", got)+ }+ })+ }+}++func TestQoderBackendToolTerminatedTurnFallsBackToLatestTextBlock(t *testing.T) {+ t.Parallel()++ fakePath := filepath.Join(t.TempDir(), "qodercli")+ writeTestExecutable(t, fakePath, []byte(fakeQoderACPScriptWithNarrationAndFinalAnswer()))++ const want = "Done. Posted the summary to the issue."+ backend, err := New("qoder", Config{+ ExecutablePath: fakePath,+ Logger: slog.Default(),+ Env: map[string]string{+ "QODER_TEXT_BEFORE_TOOL": want,+ "QODER_TOOL_TERMINATED": "1",+ },+ })+ if err != nil {+ t.Fatalf("new qoder backend: %v", err)+ }++ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)+ defer cancel()++ session, err := backend.Execute(ctx, "post the summary", ExecOptions{Timeout: 30 * time.Second})+ if err != nil {+ t.Fatalf("execute: %v", err)+ }+ go func() {+ for range session.Messages {+ }+ }()++ result := <-session.Result+ if result.Status != "completed" {+ t.Fatalf("status=%q err=%q", result.Status, result.Error)+ }+ if result.Output != want {+ t.Fatalf("Result.Output = %q, want fallback %q", result.Output, want)+ }+}++func TestQoderBackendProviderErrorDetectionUsesFullOutput(t *testing.T) {+ t.Parallel()++ fakePath := filepath.Join(t.TempDir(), "qodercli")+ writeTestExecutable(t, fakePath, []byte(fakeQoderACPScriptWithNarrationAndFinalAnswer()))++ backend, err := New("qoder", Config{+ ExecutablePath: fakePath,+ Logger: slog.Default(),+ Env: map[string]string{"QODER_TERMINAL_ERROR_BEFORE_TOOL": "1"},+ })+ if err != nil {+ t.Fatalf("new qoder backend: %v", err)+ }++ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)+ defer cancel()++ session, err := backend.Execute(ctx, "inspect this attachment", ExecOptions{Timeout: 30 * time.Second})+ if err != nil {+ t.Fatalf("execute: %v", err)+ }+ go func() {+ for range session.Messages {+ }+ }()++ result := <-session.Result+ if result.Status != "failed" {+ t.Fatalf("status=%q, want failed (output=%q error=%q)", result.Status, result.Output, result.Error)+ }+ if result.Output != "The diagram shows the service architecture." {+ t.Fatalf("Result.Output = %q, want final answer only", result.Output)+ }+ if !strings.Contains(result.Error, "API call failed after 3 retries") {+ t.Fatalf("error=%q, want provider error from the full text stream", result.Error)+ }+}+func TestQoderBackendNonPositiveTimeoutDoesNotImposeDeadline(t *testing.T) {t.Parallel()