From 5c8f2f400a536e51233667e2c5dda88081aee6a4 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 15 Sep 2026 10:23:17 +1000 Subject: [PATCH] refactor(tools): delete unreachable AST-based tool discovery Registry.Discover, DefaultRegistry and the go/ast extraction helpers have no production caller; static source discovery has no PRD and no consumer. Production registration goes through bootstrap.go's toolReg.Register and the provider Registry's RegisterBatch (tools-core-3). --- internal/tools/registry.go | 335 -------------------------------- internal/tools/registry_test.go | 182 ----------------- 2 files changed, 517 deletions(-) diff --git a/internal/tools/registry.go b/internal/tools/registry.go index 93a66cd4d..707e12ab0 100644 --- a/internal/tools/registry.go +++ b/internal/tools/registry.go @@ -3,12 +3,6 @@ package tools import ( "errors" "fmt" - "go/ast" - "go/parser" - "go/token" - "io/fs" - "path" - "strconv" "sync" ) @@ -16,15 +10,6 @@ import ( // same name has already been registered. var ErrDuplicateTool = errors.New("duplicate tool name") -// defaultRegistry is the package-level singleton. -var defaultRegistry = NewRegistry() - -// DefaultRegistry returns the package-level singleton registry. Most -// callers should use this rather than creating their own. -func DefaultRegistry() *Registry { - return defaultRegistry -} - // Registry is a thread-safe tool registry. It maps tool names to // [ToolEntry] values and supports lookup by toolset and availability. type Registry struct { @@ -149,323 +134,3 @@ func (r *Registry) Available() []ToolEntry { } return out } - -// Discover scans a directory of Go source files for Register calls -// that pass a [ToolEntry] composite literal. It uses [go/parser] and -// [go/ast] to statically analyse source files without executing them. -// -// Discover returns the number of Register calls found. Function fields -// (Handler, CheckFn, DynamicSchemaOverrides) cannot be resolved from -// static source analysis and remain nil. Entries that fail -// [ToolEntry.Validate] -- typically because Handler is nil -- are -// counted but not registered. -// -// Parse errors in individual files do not stop the scan; they are -// accumulated and returned as a joined error alongside the count of -// Register calls found in successfully-parsed files. -func (r *Registry) Discover(fsys fs.FS, dir string) (int, error) { - entries, err := fs.Glob(fsys, path.Join(dir, "*.go")) - if err != nil { - return 0, err - } - - count := 0 - var errs []error - for _, filePath := range entries { - src, err := fs.ReadFile(fsys, filePath) - if err != nil { - errs = append(errs, fmt.Errorf("read %s: %w", filePath, err)) - continue - } - - fset := token.NewFileSet() - f, err := parser.ParseFile(fset, filePath, src, parser.ParseComments) - if err != nil { - errs = append(errs, fmt.Errorf("parse %s: %w", filePath, err)) - continue - } - - count += countRegisterCalls(f) - - for _, entry := range extractToolEntries(f) { - // Best-effort registration: only entries that pass - // Validate (have Name + Handler) are registered. - _ = r.Register(entry) - } - } - - return count, errors.Join(errs...) -} - -// countRegisterCalls counts the number of Register method calls in a Go AST. -func countRegisterCalls(f *ast.File) int { - count := 0 - ast.Inspect(f, func(n ast.Node) bool { - call, ok := n.(*ast.CallExpr) - if !ok { - return true - } - if isRegisterCall(call) { - count++ - } - return true - }) - return count -} - -// extractToolEntries walks the AST and extracts [ToolEntry] composite -// literals from Register calls. -func extractToolEntries(f *ast.File) []ToolEntry { - var entries []ToolEntry - ast.Inspect(f, func(n ast.Node) bool { - call, ok := n.(*ast.CallExpr) - if !ok { - return true - } - if !isRegisterCall(call) { - return true - } - if len(call.Args) == 0 { - return true - } - comp, ok := call.Args[0].(*ast.CompositeLit) - if !ok { - return true - } - entries = append(entries, toolEntryFromCompositeLit(comp)) - return true - }) - return entries -} - -// isRegisterCall reports whether call is a .Register(...) method call on -// a *Registry receiver. It accepts common patterns: -// -// - DefaultRegistry().Register(...) -// - tools.DefaultRegistry().Register(...) -// - r.Register(...) where r is a known registry variable -func isRegisterCall(call *ast.CallExpr) bool { - sel, ok := call.Fun.(*ast.SelectorExpr) - if !ok { - return false - } - if sel.Sel.Name != "Register" { - return false - } - - // The receiver must be a call to DefaultRegistry, with or without - // a package qualifier (e.g. DefaultRegistry() or tools.DefaultRegistry()). - rcv, ok := sel.X.(*ast.CallExpr) - if !ok { - return false - } - return callsDefaultRegistry(rcv) -} - -// callsDefaultRegistry reports whether call is a DefaultRegistry() -// invocation, with or without a package qualifier. -func callsDefaultRegistry(call *ast.CallExpr) bool { - switch fn := call.Fun.(type) { - case *ast.Ident: - return fn.Name == "DefaultRegistry" - case *ast.SelectorExpr: - // pkg.DefaultRegistry() - return fn.Sel.Name == "DefaultRegistry" - default: - return false - } -} - -// toolEntryFromCompositeLit extracts a [ToolEntry] from an AST composite -// literal. Only basic literal values (strings, bools, ints) are extracted; -// function references are ignored. -func toolEntryFromCompositeLit(lit *ast.CompositeLit) ToolEntry { - var e ToolEntry - for _, elt := range lit.Elts { - kv, ok := elt.(*ast.KeyValueExpr) - if !ok { - continue - } - key, ok := kv.Key.(*ast.Ident) - if !ok { - continue - } - switch key.Name { - case "Name": - e.Name = stringLit(kv.Value) - case "Toolset": - e.Toolset = stringLit(kv.Value) - case "Description": - e.Description = stringLit(kv.Value) - case "Emoji": - e.Emoji = stringLit(kv.Value) - case "IsAsync": - e.IsAsync = boolLit(kv.Value) - case "MaxResultSizeChars": - e.MaxResultSizeChars = intLit(kv.Value) - case "RequiresEnv": - e.RequiresEnv = stringSliceLit(kv.Value) - case "Schema": - e.Schema = schemaLit(kv.Value) - } - } - return e -} - -// --- AST value extractors --- - -func stringLit(expr ast.Expr) string { - lit, ok := expr.(*ast.BasicLit) - if !ok || lit.Kind != token.STRING { - return "" - } - // strconv.Unquote handles double-quoted, backtick-delimited (raw), - // and interpreted string literals with escape sequences. - s, err := strconv.Unquote(lit.Value) - if err != nil { - return "" - } - return s -} - -func boolLit(expr ast.Expr) bool { - ident, ok := expr.(*ast.Ident) - return ok && ident.Name == "true" -} - -func intLit(expr ast.Expr) int { - // Handle negative literals: the AST represents -5 as - // UnaryExpr{Op: token.SUB, X: BasicLit{Kind: INT, Value: "5"}}. - if un, ok := expr.(*ast.UnaryExpr); ok && un.Op == token.SUB { - lit, ok := un.X.(*ast.BasicLit) - if !ok || lit.Kind != token.INT { - return 0 - } - v, err := strconv.ParseInt(lit.Value, 0, 0) - if err != nil { - return 0 - } - return -int(v) - } - - lit, ok := expr.(*ast.BasicLit) - if !ok || lit.Kind != token.INT { - return 0 - } - // base=0 auto-detects decimal, hex (0x), octal (0o), binary (0b). - v, err := strconv.ParseInt(lit.Value, 0, 0) - if err != nil { - return 0 - } - return int(v) -} - -func stringSliceLit(expr ast.Expr) []string { - comp, ok := expr.(*ast.CompositeLit) - if !ok { - return nil - } - out := make([]string, 0, len(comp.Elts)) - for _, elt := range comp.Elts { - if s := stringLit(elt); s != "" || isStringLit(elt) { - out = append(out, s) - } - } - return out -} - -// isStringLit reports whether expr is a string literal (including empty). -func isStringLit(expr ast.Expr) bool { - lit, ok := expr.(*ast.BasicLit) - return ok && lit.Kind == token.STRING -} - -func schemaLit(expr ast.Expr) JSONSchema { - comp, ok := expr.(*ast.CompositeLit) - if !ok { - return nil - } - s := make(JSONSchema) - for _, elt := range comp.Elts { - kv, ok := elt.(*ast.KeyValueExpr) - if !ok { - continue - } - key := schemaKey(kv.Key) - if key == "" { - continue - } - if v := schemaValue(kv.Value); v != nil { - s[key] = v - } - } - if len(s) == 0 { - return nil - } - return s -} - -// schemaKey extracts a key string from a schema literal key expression. -// Handles string literals and identifier names (e.g. Type: "object"). -func schemaKey(expr ast.Expr) string { - if s := stringLit(expr); s != "" { - return s - } - if ident, ok := expr.(*ast.Ident); ok { - return ident.Name - } - return "" -} - -// schemaValue extracts a value from a schema literal, handling strings, -// ints, bools, nested maps, and arrays of strings. -func schemaValue(expr ast.Expr) any { - switch v := expr.(type) { - case *ast.BasicLit: - switch v.Kind { - case token.STRING: - return stringLit(v) - case token.INT: - return intLit(v) - default: - return nil - } - case *ast.Ident: - if v.Name == "true" { - return true - } - if v.Name == "false" { - return false - } - return nil - case *ast.CompositeLit: - // Check if it's a slice/array (no key-value pairs) or a map. - if isArrayLit(v) { - return schemaArrayLit(v) - } - return schemaLit(v) - default: - return nil - } -} - -// isArrayLit reports whether a composite literal looks like a slice/array -// (elements without keys) rather than a map. -func isArrayLit(lit *ast.CompositeLit) bool { - if len(lit.Elts) == 0 { - return false - } - _, ok := lit.Elts[0].(*ast.KeyValueExpr) - return !ok -} - -// schemaArrayLit extracts a []any from an array composite literal. -func schemaArrayLit(lit *ast.CompositeLit) []any { - var out []any - for _, elt := range lit.Elts { - if v := schemaValue(elt); v != nil { - out = append(out, v) - } - } - return out -} diff --git a/internal/tools/registry_test.go b/internal/tools/registry_test.go index 6a5fab6db..6030005ba 100644 --- a/internal/tools/registry_test.go +++ b/internal/tools/registry_test.go @@ -4,7 +4,6 @@ import ( "errors" "slices" "testing" - "testing/fstest" ) func TestRegistryRegister(t *testing.T) { @@ -191,116 +190,6 @@ func TestRegistryAvailable(t *testing.T) { }) } -func TestRegistryDiscover(t *testing.T) { - t.Run("no go files", func(t *testing.T) { - r := NewRegistry() - fsys := fstest.MapFS{} - n, err := r.Discover(fsys, ".") - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - if n != 0 { - t.Errorf("expected 0 discovered, got %d", n) - } - }) - - t.Run("go file with Register call", func(t *testing.T) { - r := NewRegistry() - fsys := fstest.MapFS{ - "mytool.go": &fstest.MapFile{ - Data: []byte(`package tools - -import "github.com/samcharles93/archie-core/internal/tools" - -func init() { - tools.DefaultRegistry().Register(tools.ToolEntry{ - Name: "discovered_hello", - Description: "A discovered tool", - }) -} -`), - }, - } - // Discover finds and counts Register calls. The entry is not - // actually registered because Handler (a function field) cannot - // be resolved from source via static AST analysis. - n, err := r.Discover(fsys, ".") - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - if n != 1 { - t.Errorf("expected 1 discovered, got %d", n) - } - }) - - t.Run("go file without Register call", func(t *testing.T) { - r := NewRegistry() - fsys := fstest.MapFS{ - "plain.go": &fstest.MapFile{ - Data: []byte(`package tools - -func PlainFunction() string { - return "hello" -} -`), - }, - } - n, err := r.Discover(fsys, ".") - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - if n != 0 { - t.Errorf("expected 0 discovered, got %d", n) - } - }) - - t.Run("syntax error in go file", func(t *testing.T) { - r := NewRegistry() - fsys := fstest.MapFS{ - "broken.go": &fstest.MapFile{ - Data: []byte(`package tools - -func broken( {`), - }, - } - _, err := r.Discover(fsys, ".") - if err == nil { - t.Error("expected error for broken Go file") - } - }) - - t.Run("multiple Register calls in one file", func(t *testing.T) { - r := NewRegistry() - fsys := fstest.MapFS{ - "tools.go": &fstest.MapFile{ - Data: []byte(`package tools - -func init() { - DefaultRegistry().Register(ToolEntry{Name: "tool-a", Description: "First tool"}) - DefaultRegistry().Register(ToolEntry{Name: "tool-b", Description: "Second tool"}) -} -`), - }, - } - n, err := r.Discover(fsys, ".") - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - if n != 2 { - t.Errorf("expected 2 discovered, got %d", n) - } - }) -} - -func TestRegistrySingleton(t *testing.T) { - // DefaultRegistry returns the same instance. - d1 := DefaultRegistry() - d2 := DefaultRegistry() - if d1 != d2 { - t.Error("DefaultRegistry should be a singleton") - } -} - // toolNames extracts names from a slice of ToolEntry. func toolNames(entries []ToolEntry) []string { names := make([]string, len(entries)) @@ -375,79 +264,8 @@ func TestRegistryRegisterDefensiveCopy(t *testing.T) { } } -func TestRegistryDiscoverContinuesOnError(t *testing.T) { - // Regression: Discover aborted on first parse error, losing prior - // registrations. After fix, it continues to remaining files. - r := NewRegistry() - fsys := fstest.MapFS{ - "good.go": &fstest.MapFile{ - Data: []byte(`package tools - -func init() { - DefaultRegistry().Register(ToolEntry{Name: "good-tool", Description: "Valid"}) -} -`), - }, - "broken.go": &fstest.MapFile{ - Data: []byte(`package tools - -func broken( {`), - }, - "another.go": &fstest.MapFile{ - Data: []byte(`package tools - -func init() { - DefaultRegistry().Register(ToolEntry{Name: "another-tool", Description: "Also valid"}) -} -`), - }, - } - n, err := r.Discover(fsys, ".") - // Should report the parse error. - if err == nil { - t.Error("expected error for broken.go") - } - // Should still count Register calls from good files. - if n != 2 { - t.Errorf("expected 2 discovered (good.go + another.go), got %d", n) - } -} - -func TestRegistryDiscoverNonRegistryRegisterIgnored(t *testing.T) { - // Regression: isRegisterCall matched ANY .Register() method. - // After fix, only DefaultRegistry().Register() or bare identifiers - // are matched. - r := NewRegistry() - fsys := fstest.MapFS{ - "not_a_tool.go": &fstest.MapFile{ - Data: []byte(`package tools - -type Other struct{} -func (o *Other) Register(name string) {} - -func init() { - var o Other - o.Register("not-a-tool") - DefaultRegistry().Register(ToolEntry{Name: "real-tool", Description: "A real tool"}) -} -`), - }, - } - n, err := r.Discover(fsys, ".") - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - // Only the DefaultRegistry().Register() call should be counted. - if n != 1 { - t.Errorf("expected 1 (only DefaultRegistry call), got %d", n) - } -} - // Ensure registry implements basic sanity at the package level. func TestRegistryPackageLevelHelpers(t *testing.T) { - // Reset state by creating a fresh registry. - // The package-level helpers work against DefaultRegistry. - t.Run("Register and All via package helpers", func(t *testing.T) { r := NewRegistry() e := ToolEntry{Name: "pkg_helper", Handler: noopHandler}