Skip to content

fix(tool): don't truncate auto tool name at hyphens in the module path#205

Open
Ricardo-M-L wants to merge 1 commit into
MoonshotAI:mainfrom
Ricardo-M-L:fix/tool-name-hyphen-truncation
Open

fix(tool): don't truncate auto tool name at hyphens in the module path#205
Ricardo-M-L wants to merge 1 commit into
MoonshotAI:mainfrom
Ricardo-M-L:fix/tool-name-hyphen-truncation

Conversation

@Ricardo-M-L

Copy link
Copy Markdown

Problem

getFunctionName derives a tool's name from the Go function when WithName() isn't provided. To drop the -fm suffix that the runtime appends to method values, it does:

fullName := fnInfo.Name()
// Remove -fm suffix for method values
if dashIdx := strings.Index(fullName, "-"); dashIdx >= 0 {
    fullName = fullName[:dashIdx]
}

strings.Index finds the first hyphen. But runtime.FuncForPC(...).Name() returns the full module path, and module paths routinely contain hyphens — including this SDK's own module, github.com/MoonshotAI/kimi-agent-sdk/go. So the name is truncated at the first path hyphen and the function name is lost entirely.

Reproduced on main (function Search defined in package kimi):

getFunctionName(Search) -> "github_com/MoonshotAI/kimi"

The Search part is gone — any user whose module path contains a hyphen and who relies on auto-naming gets a wrong, collision-prone tool name.

Fix

Strip only the exact -fm suffix:

fullName = strings.TrimSuffix(fullName, "-fm")

For names without a path hyphen this is identical to the previous behavior (the -fm method-value suffix is the only - in such names). It just stops mangling names whose import path contains a hyphen.

Tests

  • TestCreateTool_AutoNamePreservesFunctionName — the auto-derived name now ends with _Search instead of being truncated.
  • TestGetFunctionName_StripsMethodValueSuffix — method values still have -fm stripped, with the path/name preserved.

Verification

$ go test ./...
ok  github.com/MoonshotAI/kimi-agent-sdk/go
ok  github.com/MoonshotAI/kimi-agent-sdk/go/wire
ok  github.com/MoonshotAI/kimi-agent-sdk/go/wire/jsonrpc2
ok  github.com/MoonshotAI/kimi-agent-sdk/go/wire/transport
ok  github.com/MoonshotAI/kimi-agent-sdk/go/test/e2e
ok  github.com/MoonshotAI/kimi-agent-sdk/go/test/integration

$ go vet ./...    # clean
$ gofmt -l go/tool.go go/tool_test.go    # clean

getFunctionName stripped the runtime's -fm method-value suffix with
strings.Index(fullName, "-"), which cuts at the *first* hyphen. Go's
runtime function names include the full module path, and module paths
routinely contain hyphens — including this SDK's own
github.com/MoonshotAI/kimi-agent-sdk/go. So a tool created without
WithName() got its name truncated at the first path hyphen: a function
"...kimi-agent-sdk/go.Search" became "github_com/MoonshotAI/kimi",
dropping the function name entirely.

Strip only the exact "-fm" suffix with strings.TrimSuffix. For names
without path hyphens this is identical to the old behavior; it just stops
mangling names whose import path contains a hyphen.

Adds tests: the auto-derived name now ends with the real function name,
and method values still have -fm stripped.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant