|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | + |
| 11 | + "github.com/SimplyLiz/CodeMCP/internal/query" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + unwiredFormat string |
| 16 | + unwiredScope []string |
| 17 | + unwiredLimit int |
| 18 | + unwiredMinConfidence float64 |
| 19 | + unwiredExclude []string |
| 20 | + unwiredIncludeTypes bool |
| 21 | + unwiredMaxNodes int |
| 22 | +) |
| 23 | + |
| 24 | +var unwiredCmd = &cobra.Command{ |
| 25 | + Use: "unwired", |
| 26 | + Short: "Find exported symbols not reachable from entrypoints", |
| 27 | + Long: `Find exported functions and methods that are never transitively called |
| 28 | +from any application entrypoint (main, server, CLI commands). |
| 29 | +
|
| 30 | +This detects the "built but never plugged in" pattern where modules exist |
| 31 | +and are tested but aren't wired into the execution pipeline. |
| 32 | +
|
| 33 | +Complements dead-code (which checks reference counts) by checking |
| 34 | +reachability from the actual runtime entry points. |
| 35 | +
|
| 36 | +Examples: |
| 37 | + ckb unwired |
| 38 | + ckb unwired --scope src/cost,src/multisampling |
| 39 | + ckb unwired --min-confidence 0.9 |
| 40 | + ckb unwired --include-types |
| 41 | + ckb unwired --format json`, |
| 42 | + Run: runUnwired, |
| 43 | +} |
| 44 | + |
| 45 | +func init() { |
| 46 | + unwiredCmd.Flags().StringVar(&unwiredFormat, "format", "human", "Output format (human, json)") |
| 47 | + unwiredCmd.Flags().StringSliceVar(&unwiredScope, "scope", nil, "Limit to specific packages/paths") |
| 48 | + unwiredCmd.Flags().IntVar(&unwiredLimit, "limit", 100, "Maximum results to return") |
| 49 | + unwiredCmd.Flags().Float64Var(&unwiredMinConfidence, "min-confidence", 0.80, "Minimum confidence threshold (0-1)") |
| 50 | + unwiredCmd.Flags().StringSliceVar(&unwiredExclude, "exclude", nil, "Patterns to exclude") |
| 51 | + unwiredCmd.Flags().BoolVar(&unwiredIncludeTypes, "include-types", false, "Include type definitions (higher FP rate)") |
| 52 | + unwiredCmd.Flags().IntVar(&unwiredMaxNodes, "max-nodes", 10000, "Max symbols in reachable set") |
| 53 | + rootCmd.AddCommand(unwiredCmd) |
| 54 | +} |
| 55 | + |
| 56 | +func runUnwired(cmd *cobra.Command, args []string) { |
| 57 | + start := time.Now() |
| 58 | + logger := newLogger(unwiredFormat) |
| 59 | + |
| 60 | + repoRoot := mustGetRepoRoot() |
| 61 | + engine := mustGetEngine(repoRoot, logger) |
| 62 | + ctx := newContext() |
| 63 | + |
| 64 | + opts := query.FindUnwiredModulesOptions{ |
| 65 | + Scope: unwiredScope, |
| 66 | + ExcludePatterns: unwiredExclude, |
| 67 | + MinConfidence: unwiredMinConfidence, |
| 68 | + IncludeTypes: unwiredIncludeTypes, |
| 69 | + MaxNodes: unwiredMaxNodes, |
| 70 | + Limit: unwiredLimit, |
| 71 | + } |
| 72 | + |
| 73 | + response, err := engine.FindUnwiredModules(ctx, opts) |
| 74 | + if err != nil { |
| 75 | + fmt.Fprintf(os.Stderr, "Error: %v\n", err) |
| 76 | + os.Exit(1) |
| 77 | + } |
| 78 | + |
| 79 | + if unwiredFormat == "json" { |
| 80 | + data, err := json.MarshalIndent(response, "", " ") |
| 81 | + if err != nil { |
| 82 | + fmt.Fprintf(os.Stderr, "Error formatting output: %v\n", err) |
| 83 | + os.Exit(1) |
| 84 | + } |
| 85 | + fmt.Println(string(data)) |
| 86 | + } else { |
| 87 | + printUnwiredHuman(response) |
| 88 | + } |
| 89 | + |
| 90 | + logger.Debug("Unwired analysis completed", |
| 91 | + "unwiredCount", response.Summary.UnwiredCount, |
| 92 | + "reachableCount", response.ReachableCount, |
| 93 | + "duration", time.Since(start).Milliseconds(), |
| 94 | + ) |
| 95 | +} |
| 96 | + |
| 97 | +func printUnwiredHuman(resp *query.FindUnwiredModulesResponse) { |
| 98 | + fmt.Println("Unwired Module Analysis") |
| 99 | + fmt.Println("============================================================") |
| 100 | + fmt.Println() |
| 101 | + |
| 102 | + if len(resp.Entrypoints) > 0 { |
| 103 | + fmt.Printf("Entrypoints: %d\n", len(resp.Entrypoints)) |
| 104 | + for _, ep := range resp.Entrypoints { |
| 105 | + fmt.Printf(" - %s\n", ep) |
| 106 | + } |
| 107 | + fmt.Println() |
| 108 | + } |
| 109 | + |
| 110 | + fmt.Printf("Reachable symbols: %d\n", resp.ReachableCount) |
| 111 | + fmt.Printf("Total exported: %d\n", resp.Summary.TotalExported) |
| 112 | + fmt.Printf("Unwired: %d\n", resp.Summary.UnwiredCount) |
| 113 | + if resp.Partial { |
| 114 | + fmt.Println(" (partial — reachable set budget exhausted)") |
| 115 | + } |
| 116 | + fmt.Println() |
| 117 | + |
| 118 | + if len(resp.UnwiredModules) == 0 { |
| 119 | + fmt.Println("No unwired modules found.") |
| 120 | + return |
| 121 | + } |
| 122 | + |
| 123 | + for _, mod := range resp.UnwiredModules { |
| 124 | + fmt.Printf("── %s (%d/%d exported symbols unwired)\n", |
| 125 | + mod.Path, mod.Summary.UnwiredCount, mod.Summary.TotalExported) |
| 126 | + for _, item := range mod.Items { |
| 127 | + fmt.Printf(" %s %s (%.0f%% confidence)\n", |
| 128 | + item.Kind, item.SymbolName, item.Confidence*100) |
| 129 | + fmt.Printf(" %s\n", item.Reason) |
| 130 | + if item.ReferenceCount > 0 { |
| 131 | + fmt.Printf(" refs: %d (test: %d)\n", item.ReferenceCount, item.TestReferences) |
| 132 | + } |
| 133 | + } |
| 134 | + fmt.Println() |
| 135 | + } |
| 136 | +} |
0 commit comments