mirror of
https://github.com/Twigpine/zero.git
synced 2026-11-10 05:01:28 -10:00
* chore: remove code unreachable on every supported platform The Security and code health job's advisory deadcode step reports 152 unreachable functions on main. Running the same check under GOOS=linux, windows, and darwin shows 13 of those are false positives whose callers live in windows build-tagged files, so only functions unreachable on all three platforms are touched. Functions with no references anywhere are deleted, including the internal/reasoning package, which nothing imports. Functions that only tests reference move into per-package export_test.go files so tests keep their seams while the production binary sheds the code. Test call sites of deleted convenience wrappers are ported to the live APIs (scoped tool constructors, tools.DeferredSource, FinishReason checks). Kept despite the report: cron.Store.Runs (only reader of runs.jsonl, which production writes), and agenteval.TraceEventKeys and MissingTraceEvents (used across packages by the trace contract test). * docs(config): correct ValidateFile's errors.As claim Message is a flattened string (.Error() already called on the parse error), not a wrapped error, so callers can't recover the concrete json error type via errors.As. Fix the comment to match. Co-Authored-By: Claude Sonnet 5 <[email protected]> * chore(trace,skills): drop obsolete Sink adapters and fix stale doc references The dead-code pass removed the Emit methods but left Sink, NDJSONSink, and TextSink exported with a contract nothing implements; remove the types with their contract. Also remove the stray blank line at EOF of trace.go flagged by gofmt, and update skills package docs and HOW_ZERO_WORKS.md to stop directing callers to DiscoveryRoots/Duplicates, which are now test seams. Co-Authored-By: Claude Fable 5 <[email protected]> * chore: clean up remaining review nits on dead code removal Replace a pointless fmt.Errorf(...%w...).Error() with fmt.Sprintf using %v, since the wrapped error was discarded immediately anyway. Remove a comment block that was duplicated verbatim. * docs(skills): drop remaining references to test-only List/Get Load and MergedSkills comments still named List/Get as production entry points after they moved to export_test.go. Point callers at Load and ListFromRoots instead. * chore: remove now-unreferenced export_test.go seams execSessionManager.len() and likelySandboxDenied/sandboxDenialKind had no remaining callers after the sandbox unify-command-execution rebase; the production code they wrapped for tests no longer exists. --------- Co-authored-by: Claude Sonnet 5 <[email protected]>
126 lines
3.9 KiB
Go
126 lines
3.9 KiB
Go
package tools
|
|
|
|
import (
|
|
"regexp"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
defaultShortenMax = 200
|
|
maxSchemaHintParams = 4
|
|
maxSchemaHintLen = 360
|
|
)
|
|
|
|
var (
|
|
headingPrefix = regexp.MustCompile(`^#{1,6}\s+(.+)$`)
|
|
genericHeading = regexp.MustCompile(`(?i)^(overview|description|summary)$`)
|
|
collapseWhitespace = regexp.MustCompile(`\s+`)
|
|
)
|
|
|
|
// mcpToolNamePrefix mirrors the prefix used by mcp.registryToolName.
|
|
const mcpToolNamePrefix = "mcp_"
|
|
|
|
// mcpServerFromToolName extracts the server token from a synthesized MCP tool
|
|
// name produced by mcp.registryToolName ("mcp_<server>_<tool>"). It returns ""
|
|
// for non-MCP names and for names that lack both a server and a tool segment.
|
|
func mcpServerFromToolName(name string) string {
|
|
rest, ok := strings.CutPrefix(name, mcpToolNamePrefix)
|
|
if !ok {
|
|
return ""
|
|
}
|
|
sep := strings.IndexByte(rest, '_')
|
|
if sep <= 0 || sep == len(rest)-1 {
|
|
// No server token, or nothing after the server token (no tool part).
|
|
return ""
|
|
}
|
|
return rest[:sep]
|
|
}
|
|
|
|
// mcpServerNamed is an optional interface a deferred MCP tool implements to
|
|
// report its true (un-sanitized-token) server name for discovery labels. When
|
|
// a tool provides it, DeferredLine prefers it over the name-derived token, which
|
|
// would mislabel a server whose sanitized name itself contains an underscore
|
|
// (e.g. "git_hub" → "git"). It affects the cosmetic discovery label only; tool
|
|
// resolution never depends on this.
|
|
type mcpServerNamed interface {
|
|
MCPServerName() string
|
|
}
|
|
|
|
// DeferredSource reports the compact source label used in tool_search's dynamic
|
|
// description. MCP tools use their configured server name; other deferred tools
|
|
// fall back to the first name segment so families such as swarm_* are grouped.
|
|
func DeferredSource(t Tool) string {
|
|
if t == nil {
|
|
return ""
|
|
}
|
|
if named, ok := t.(mcpServerNamed); ok {
|
|
if reported := strings.TrimSpace(named.MCPServerName()); reported != "" {
|
|
return reported
|
|
}
|
|
}
|
|
if server := mcpServerFromToolName(t.Name()); server != "" {
|
|
return server
|
|
}
|
|
name := strings.TrimSpace(t.Name())
|
|
if name == "" {
|
|
return ""
|
|
}
|
|
if prefix, _, ok := strings.Cut(name, "_"); ok && prefix != "" {
|
|
return prefix
|
|
}
|
|
return name
|
|
}
|
|
|
|
// DeferredToolDiscoveryLine renders the compact name/source entry used in
|
|
// tool_search's dynamic description. It intentionally omits descriptions and
|
|
// schemas; those are revealed only after tool_search loads a matching tool.
|
|
func DeferredToolDiscoveryLine(t Tool) string {
|
|
if t == nil {
|
|
return ""
|
|
}
|
|
name := strings.TrimSpace(t.Name())
|
|
if name == "" {
|
|
return ""
|
|
}
|
|
source := strings.TrimSpace(DeferredSource(t))
|
|
if source == "" || source == name {
|
|
return name
|
|
}
|
|
return name + " — " + source
|
|
}
|
|
|
|
// BuildToolSearchDescription renders the model-facing discovery instructions for
|
|
// deferred tools. This belongs on the tool_search tool definition, not as an
|
|
// extra user message, so the model can discover tools without treating discovery
|
|
// metadata as something to answer or acknowledge.
|
|
func BuildToolSearchDescription(deferred []Tool) string {
|
|
seen := make(map[string]bool)
|
|
lines := make([]string, 0, len(deferred))
|
|
for _, tool := range deferred {
|
|
line := DeferredToolDiscoveryLine(tool)
|
|
if line != "" && !seen[line] {
|
|
seen[line] = true
|
|
lines = append(lines, line)
|
|
}
|
|
}
|
|
sort.Strings(lines)
|
|
|
|
toolText := "No deferred tools are currently hidden."
|
|
if len(lines) > 0 {
|
|
for i, line := range lines {
|
|
lines[i] = "- " + line
|
|
}
|
|
toolText = strings.Join(lines, "\n")
|
|
}
|
|
|
|
var b strings.Builder
|
|
b.WriteString("# Tool discovery\n\n")
|
|
b.WriteString("Searches over deferred tool metadata and exposes matching tools for the next model call.\n\n")
|
|
b.WriteString("Deferred tools available through `tool_search`:\n")
|
|
b.WriteString(toolText)
|
|
b.WriteString("\n")
|
|
b.WriteString("Use query `select:Name1,Name2` for exact names from this list, or use keywords to search tool names and descriptions. Do not call `tool_search` for tools already present in the current tool list.")
|
|
return b.String()
|
|
}
|