From 79a69e5dceb4d0df7a8e27ca3563c839756c8050 Mon Sep 17 00:00:00 2001 From: sausheong Date: Sat, 13 Jun 2026 12:42:41 +0800 Subject: [PATCH] fix(qwen): index-order tool emit + strings.Builder accumulation (R7a/R7c) --- providers/qwen/qwen.go | 57 ++++++++++++++++++++++++------------- providers/qwen/qwen_test.go | 19 +++++++++++++ 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/providers/qwen/qwen.go b/providers/qwen/qwen.go index 2505a20..70eca44 100644 --- a/providers/qwen/qwen.go +++ b/providers/qwen/qwen.go @@ -14,6 +14,41 @@ import ( "github.com/sausheong/harness/llm" ) +// pendingTC accumulates a streamed tool call across deltas. args uses +// strings.Builder so repeated argument fragments append in O(n) total +// (not O(n²) from string concatenation). +type pendingTC struct { + id string + name string + args strings.Builder +} + +// emitToolCalls sends EventToolCallDone for each completed tool call in +// ascending index order so tool_use block order is deterministic across +// turns (prompt-cache stability). +func emitToolCalls(events chan<- llm.ChatEvent, toolCalls map[int]*pendingTC) { + maxIdx := -1 + for idx := range toolCalls { + if idx > maxIdx { + maxIdx = idx + } + } + for idx := 0; idx <= maxIdx; idx++ { + tc, ok := toolCalls[idx] + if !ok || tc.name == "" { + continue + } + events <- llm.ChatEvent{ + Type: llm.EventToolCallDone, + ToolCall: &llm.ToolCall{ + ID: tc.id, + Name: tc.name, + Input: json.RawMessage(tc.args.String()), + }, + } + } +} + // QwenProvider implements llm.LLMProvider using the Qwen Chat Completions API. // Qwen uses an OpenAI-compatible API, so we reuse the go-openai client. type QwenProvider struct { @@ -217,11 +252,6 @@ func (p *QwenProvider) ChatStream(ctx context.Context, req llm.ChatRequest) (<-c defer stream.Close() // Track tool calls being built up across deltas - type pendingTC struct { - id string - name string - argsJSON string - } toolCalls := make(map[int]*pendingTC) for { @@ -271,25 +301,14 @@ func (p *QwenProvider) ChatStream(ctx context.Context, req llm.ChatRequest) (<-c } } if tc.Function.Arguments != "" { - pending.argsJSON += tc.Function.Arguments + pending.args.WriteString(tc.Function.Arguments) } } // Finish reason if choice.FinishReason == openai.FinishReasonToolCalls || choice.FinishReason == openai.FinishReasonStop { - // Emit completed tool calls - for _, tc := range toolCalls { - if tc.name != "" { - events <- llm.ChatEvent{ - Type: llm.EventToolCallDone, - ToolCall: &llm.ToolCall{ - ID: tc.id, - Name: tc.name, - Input: json.RawMessage(tc.argsJSON), - }, - } - } - } + // Emit completed tool calls in index order. + emitToolCalls(events, toolCalls) } } } diff --git a/providers/qwen/qwen_test.go b/providers/qwen/qwen_test.go index 8cd897a..051a526 100644 --- a/providers/qwen/qwen_test.go +++ b/providers/qwen/qwen_test.go @@ -24,3 +24,22 @@ func TestQwenResolveSystemPromptEmpty(t *testing.T) { got := qwenResolveSystemPrompt(llm.ChatRequest{}) require.Equal(t, "", got) } + +func TestEmitToolCalls_OrderedByIndex(t *testing.T) { + mk := func(id, name string) *pendingTC { + p := &pendingTC{id: id, name: name} + p.args.WriteString("{}") + return p + } + toolCalls := map[int]*pendingTC{1: mk("b", "second"), 0: mk("a", "first")} + ch := make(chan llm.ChatEvent, 10) + emitToolCalls(ch, toolCalls) + close(ch) + var ids []string + for ev := range ch { + if ev.Type == llm.EventToolCallDone { + ids = append(ids, ev.ToolCall.ID) + } + } + require.Equal(t, []string{"a", "b"}, ids) +}