Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions plugins/wasm-go/extensions/ai-statistics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"regexp"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -423,20 +424,39 @@ func getToolCallsFromBuffer(buffer *StreamingToolCallsBuffer) []ToolCall {
return nil
}

// Find max index to create properly sized slice
maxIndex := 0
for idx := range buffer.ToolCalls {
if idx > maxIndex {
maxIndex = idx
if len(buffer.ToolCalls) == 1 {
for index, tc := range buffer.ToolCalls {
if index >= 0 {
return []ToolCall{*tc}
}
}
return []ToolCall{}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 单条负索引分支仍未覆盖 —— codecov 报的 "1 line missing" 就是这一行

return []ToolCall{} 的触发条件是:buffer 里恰好只有 1 个工具调用,且它的 index 为负。

当前测试表里 negative_only 用的是 {-1, -2} 两个条目,走的是稀疏路径,覆盖不到这条单条快路径;TestToolCallMaxIndex 用的是正的极大 index,同样覆盖不到。

建议在表里补一行即可:

{"single_negative", []int{-1}, []int{}},

want 写成 []int{}(非 nil)时会被构造成空非 nil 切片,与 []ToolCall{} 的 reflect.DeepEqual 一致,不会误判。

补完后 getToolCallsFromBuffer 的语句覆盖即可补齐,codecov 的 patch coverage 也会随之回到 100%。

}

result := make([]ToolCall, 0, len(buffer.ToolCalls))
for i := 0; i <= maxIndex; i++ {
if tc, exists := buffer.ToolCalls[i]; exists {
result = append(result, *tc)
// Probe only the entry count, never the maximum index. Contiguous indexes
// need no sorting or extra allocation.
for index := 0; index < len(buffer.ToolCalls); index++ {
tc, exists := buffer.ToolCalls[index]
if !exists {
break
}
result = append(result, *tc)
}
if len(result) == len(buffer.ToolCalls) {
return result
}
result = result[:0]
indexes := make([]int, 0, len(buffer.ToolCalls))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 分配数字与 Go 版本相关,建议在正文标注口径

正文里「稀疏三工具 224 → 248 B/op、1 → 2 allocs」是 Go 1.24.4 上的实测值。对本仓库锁定的工具链(go.mod toolchain go1.24.4、CI 用 Go 1.24.0)这个数字是准确的,不是错误。

但在 Go 1.26.x 上跑同一个基准,稀疏三工具是 224 B/op、1 alloc/op,与连续三工具完全相同 —— 逃逸分析输出 make([]int, 0, len(buffer.ToolCalls)) does not escape,新版本把这个索引切片栈分配了。

也就是说这「多一次分配」是工具链实现细节,而不是本仓库工具链下的稳定契约。建议正文补一句「分配数字以 go1.24.4 为准」,避免复现者用较新 Go 时得到相反结论而误判为数据不一致。

for index := range buffer.ToolCalls {
if index >= 0 {
indexes = append(indexes, index)
}
}
slices.Sort(indexes)
for _, index := range indexes {
result = append(result, *buffer.ToolCalls[index])
}
return result
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright (c) 2024 Alibaba Group Holding Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"fmt"
"reflect"
"testing"
)

func TestToolCallOrder(t *testing.T) {
cases := []struct {
name string
indexes, want []int
}{
{"empty", nil, nil},
{"contiguous", []int{0, 1, 2}, []int{0, 1, 2}},
{"contiguous_shuffled", []int{2, 0, 1}, []int{0, 1, 2}},
{"contiguous_reverse", []int{2, 1, 0}, []int{0, 1, 2}},
{"sparse", []int{10000000, 2, 0}, []int{0, 2, 10000000}},
{"mixed_negative", []int{2, -1, 0}, []int{0, 2}},
{"single_negative", []int{-1}, []int{}},
{"negative_only", []int{-1, -2}, []int{}},
}
if getToolCallsFromBuffer(nil) != nil {
t.Fatal("nil buffer changed")
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
buffer := &StreamingToolCallsBuffer{ToolCalls: make(map[int]*ToolCall)}
for _, index := range tc.indexes {
buffer.ToolCalls[index] = &ToolCall{Index: index, ID: fmt.Sprintf("call-%d", index), Function: ToolCallFunction{Name: "weather", Arguments: `{"city":"Beijing"}`}}
}
var want []ToolCall
if tc.want != nil {
want = make([]ToolCall, 0, len(tc.want))
}
for _, index := range tc.want {
want = append(want, ToolCall{Index: index, ID: fmt.Sprintf("call-%d", index), Function: ToolCallFunction{Name: "weather", Arguments: `{"city":"Beijing"}`}})
}
for attempt := 0; attempt < 20; attempt++ {
if got := getToolCallsFromBuffer(buffer); !reflect.DeepEqual(got, want) {
t.Fatalf("want %#v, got %#v", want, got)
}
}
})
}
}

var benchmarkToolCalls []ToolCall

func BenchmarkToolCallOrder(b *testing.B) {
for _, tc := range []struct {
name string
indexes []int
}{
{"single/index-0", []int{0}}, {"single/index-1", []int{1}},
{"single/index-10000000", []int{10000000}},
{"multi/indexes-0-1-2", []int{0, 1, 2}},
{"multi/indexes-0-2-10000000", []int{0, 2, 10000000}},
} {
buffer := &StreamingToolCallsBuffer{ToolCalls: make(map[int]*ToolCall)}
for _, index := range tc.indexes {
buffer.ToolCalls[index] = &ToolCall{Index: index}
}
b.Run(tc.name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
benchmarkToolCalls = getToolCallsFromBuffer(buffer)
}
})
}
}

func TestToolCallMaxIndex(t *testing.T) {
index := int(^uint(0) >> 1)
got := getToolCallsFromBuffer(&StreamingToolCallsBuffer{ToolCalls: map[int]*ToolCall{index: {Index: index}}})
if len(got) != 1 || got[0].Index != index {
t.Fatal(got)
}
}
Loading