From 83c5605b5e7b8071b691c35bf08bcfd220bc83c7 Mon Sep 17 00:00:00 2001 From: Rudy Mizrahi Celekli <47457359+rudycelekli@users.noreply.github.com> Date: Wed, 21 Oct 2026 23:31:59 -0400 Subject: [PATCH] fix(cli): keep truncated table cells on a rune boundary MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Finishes the UTF-8-safe truncation sweep (#1647, #1664, #1665) in the part of cmd/mcpproxy those PRs set aside. Four table cuts existed as independent copies of the same arithmetic — shorten (profile list), truncateStr (registry list/search, catalog list) and inline s[:n] + "..." in the upstream tool list and the token list — and every one cut at a raw byte offset. Table cells carry upstream- and operator-authored text: registry and tool descriptions, catalog titles, server names. A non-ASCII value longer than its column puts a multi-byte rune astride the cut, so the cell written to the terminal ends in a partial rune. All four now go through one truncateCell built on stringutil.UTF8Prefix, in a file of its own so a fifth table does not grow a fifth copy. joinInterfaceSlice keeps maxLen == 0 meaning "do not truncate". ASCII output is byte-for-byte unchanged and the result still never exceeds the column width. Left alone: security_cmd.go and main.go carry the same shape but are under open PRs (#1714, #1720, #1725). The activity-type and hash cuts are over internal enums and hex, where no multi-byte rune can reach the boundary. --- cmd/mcpproxy/catalog_cmd.go | 2 +- cmd/mcpproxy/credential_cmd.go | 10 ---- cmd/mcpproxy/profile_cmd.go | 9 +-- cmd/mcpproxy/registry_cmd.go | 11 +--- cmd/mcpproxy/table_cell.go | 30 ++++++++++ cmd/mcpproxy/table_cell_utf8_test.go | 82 ++++++++++++++++++++++++++++ cmd/mcpproxy/token_cmd.go | 4 +- cmd/mcpproxy/upstream_cmd.go | 5 +- 8 files changed, 119 insertions(+), 34 deletions(-) create mode 100644 cmd/mcpproxy/table_cell.go create mode 100644 cmd/mcpproxy/table_cell_utf8_test.go diff --git a/cmd/mcpproxy/catalog_cmd.go b/cmd/mcpproxy/catalog_cmd.go index af64b3c01..354f4a67a 100644 --- a/cmd/mcpproxy/catalog_cmd.go +++ b/cmd/mcpproxy/catalog_cmd.go @@ -429,7 +429,7 @@ func printCatalogTable(formatter clioutput.OutputFormatter, results []registries if r.FromCache { source += " (cached)" } - rows = append(rows, []string{source, r.ID, truncateStr(r.Title, 40), r.Transport, added}) + rows = append(rows, []string{source, r.ID, truncateCell(r.Title, 40), r.Transport, added}) } out, err := formatter.FormatTable(headers, rows) if err == nil { diff --git a/cmd/mcpproxy/credential_cmd.go b/cmd/mcpproxy/credential_cmd.go index e963e9acd..0286c8522 100644 --- a/cmd/mcpproxy/credential_cmd.go +++ b/cmd/mcpproxy/credential_cmd.go @@ -333,16 +333,6 @@ func renderCredentialDetail(c cliclient.CredentialStatus) string { return strings.TrimRight(b.String(), "\n") } -func truncateCell(s string, maxLen int) string { - if len(s) <= maxLen { - return s - } - if maxLen <= 3 { - return s[:maxLen] - } - return s[:maxLen-3] + "..." -} - // printCredentialBanner emits the FR-034 honesty line. Human-readable output // begins with it on stdout (contracts/rest-endpoints.md §12); structured // json/yaml output must stay machine-parseable, so there it goes to stderr. diff --git a/cmd/mcpproxy/profile_cmd.go b/cmd/mcpproxy/profile_cmd.go index cef685688..1f1193a01 100644 --- a/cmd/mcpproxy/profile_cmd.go +++ b/cmd/mcpproxy/profile_cmd.go @@ -90,7 +90,7 @@ func runProfileList(_ *cobra.Command, _ []string) error { rows := make([][]string, 0, len(list.Profiles)) for _, p := range list.Profiles { rows = append(rows, []string{ - p.Name, dash(p.Title), shorten(dash(strings.Join(p.EffectiveServers, ",")), 30), dash(p.MaxTier), + p.Name, dash(p.Title), truncateCell(dash(strings.Join(p.EffectiveServers, ",")), 30), dash(p.MaxTier), strconv.Itoa(p.ToolCounts.Read), strconv.Itoa(p.ToolCounts.Write), strconv.Itoa(p.ToolCounts.Destructive), strconv.Itoa(p.ToolCounts.UnannotatedHidden), usedBySummary(p.UsedBy), strconv.Itoa(p.Calls24h), strconv.Itoa(p.Blocked24h), @@ -125,13 +125,6 @@ func plural(n int) string { return "s" } -func shorten(s string, max int) string { - if len(s) <= max { - return s - } - return s[:max-3] + "..." -} - type profileShowOpts struct { effective bool client string diff --git a/cmd/mcpproxy/registry_cmd.go b/cmd/mcpproxy/registry_cmd.go index 8b957d69c..407a3c95a 100644 --- a/cmd/mcpproxy/registry_cmd.go +++ b/cmd/mcpproxy/registry_cmd.go @@ -559,7 +559,7 @@ func renderRegistries(formatter clioutput.OutputFormatter, regs []map[string]int headers := []string{"ID", "NAME", "DESCRIPTION"} rows := make([][]string, 0, len(regs)) for _, r := range regs { - rows = append(rows, []string{mapString(r, "id"), mapString(r, "name"), truncateStr(mapString(r, "description"), 60)}) + rows = append(rows, []string{mapString(r, "id"), mapString(r, "name"), truncateCell(mapString(r, "description"), 60)}) } out, err := formatter.FormatTable(headers, rows) if err != nil { @@ -586,7 +586,7 @@ func renderRegistryServers(formatter clioutput.OutputFormatter, servers []map[st if installCmd == "" { installCmd = "-" } - rows = append(rows, []string{mapString(s, "id"), mapString(s, "name"), truncateStr(mapString(s, "description"), 45), installCmd}) + rows = append(rows, []string{mapString(s, "id"), mapString(s, "name"), truncateCell(mapString(s, "description"), 45), installCmd}) } out, err := formatter.FormatTable(headers, rows) if err != nil { @@ -611,13 +611,6 @@ func mapString(m map[string]interface{}, key string) string { return "" } -func truncateStr(s string, max int) string { - if len(s) > max { - return s[:max-3] + "..." - } - return s -} - // loadRegistryConfig loads config for the registry commands, honoring the // command's --config flag and the global --data-dir, falling back to defaults // so 'list'/'search' still work without a config file. diff --git a/cmd/mcpproxy/table_cell.go b/cmd/mcpproxy/table_cell.go new file mode 100644 index 000000000..f1b02c1eb --- /dev/null +++ b/cmd/mcpproxy/table_cell.go @@ -0,0 +1,30 @@ +package main + +import ( + "github.com/smart-mcp-proxy/mcpproxy-go/internal/stringutil" +) + +// truncateCell bounds one CLI table cell to maxLen bytes, marking a shortened +// value with an ellipsis. It is the single cut used by every table in this +// package: `shorten` (profile), `truncateStr` (registry, catalog) and two +// inline `s[:n] + "..."` sites (upstream tool list, token list) each carried +// an independent copy of the same arithmetic, and all of them cut at a raw +// byte offset. +// +// The cut goes through stringutil.UTF8Prefix, finishing the sweep of #1647 / +// #1664 / #1665 in the part of cmd/mcpproxy those PRs set aside. Table cells +// carry upstream- and operator-authored text — registry and tool +// descriptions, catalog titles, server names — so a non-ASCII value longer +// than its column can put a multi-byte rune astride the cut, and a raw byte +// slice then writes a partial rune to the terminal. +// +// The result never exceeds maxLen; ASCII behaviour is unchanged. +func truncateCell(s string, maxLen int) string { + if len(s) <= maxLen { + return s + } + if maxLen <= 3 { + return stringutil.UTF8Prefix(s, maxLen) + } + return stringutil.UTF8Prefix(s, maxLen-3) + "..." +} diff --git a/cmd/mcpproxy/table_cell_utf8_test.go b/cmd/mcpproxy/table_cell_utf8_test.go new file mode 100644 index 000000000..3a45ac63c --- /dev/null +++ b/cmd/mcpproxy/table_cell_utf8_test.go @@ -0,0 +1,82 @@ +package main + +import ( + "fmt" + "strings" + "testing" + "unicode/utf8" +) + +// Finishing the sweep of #1647 / #1664 / #1665 in cmd/mcpproxy. Every CLI +// table cut was a raw byte slice, so a non-ASCII registry/tool description, +// catalog title or server name longer than its column ended in a partial +// rune on the terminal. +// +// The 1-byte padding shifts the cut through all three offsets inside a 3-byte +// rune; only two of the three are mid-rune, so a single fixed width would +// pass by luck. +func TestTruncateCellKeepsRuneBoundary(t *testing.T) { + for _, maxLen := range []int{10, 24, 30, 40, 45, 60} { + for pad := 0; pad < 3; pad++ { + cell := strings.Repeat("a", pad) + strings.Repeat("語", 100) + + got := truncateCell(cell, maxLen) + if len(got) > maxLen { + t.Errorf("maxLen=%d pad=%d: %d bytes, over the column width", maxLen, pad, len(got)) + } + if !utf8.ValidString(got) { + t.Errorf("maxLen=%d pad=%d: cell ends in a partial rune: % x", maxLen, pad, []byte(got)) + } + } + } +} + +// Short cells and exact fits are returned verbatim, and ASCII truncation is +// byte-for-byte what the four removed helpers produced. +func TestTruncateCellASCIIUnchanged(t *testing.T) { + cases := []struct { + s string + maxLen int + want string + }{ + {"short", 10, "short"}, + {"exactlyten", 10, "exactlyten"}, + {strings.Repeat("x", 30), 10, strings.Repeat("x", 7) + "..."}, + {strings.Repeat("x", 100), 60, strings.Repeat("x", 57) + "..."}, + {strings.Repeat("x", 10), 3, "xxx"}, + {strings.Repeat("x", 10), 2, "xx"}, + } + for _, c := range cases { + if got := truncateCell(c.s, c.maxLen); got != c.want { + t.Errorf("truncateCell(%d chars, %d) = %q, want %q", len(c.s), c.maxLen, got, c.want) + } + } +} + +// joinInterfaceSlice keeps maxLen == 0 meaning "do not truncate", and takes +// the rune-safe cut for a positive width. allowed_servers holds server names, +// which the config only forbids from being empty or containing ':'. +func TestJoinInterfaceSliceTruncationIsRuneSafe(t *testing.T) { + name := strings.Repeat("語", 20) + m := map[string]interface{}{"allowed_servers": []interface{}{name, name}} + + if got := joinInterfaceSlice(m, "allowed_servers", 0); got != name+","+name { + t.Errorf("maxLen 0 must not truncate: got %d bytes, want %d", len(got), len(name)*2+1) + } + + got := joinInterfaceSlice(m, "allowed_servers", 23) + if len(got) > 23 { + t.Errorf("got %d bytes, over the 23-byte column", len(got)) + } + if !utf8.ValidString(got) { + t.Errorf("joined server names end in a partial rune: % x", []byte(got)) + } + if !strings.HasSuffix(got, "...") { + t.Errorf("a shortened value must stay marked: %q", got) + } + fmt.Fprint(nopWriter{}, got) +} + +type nopWriter struct{} + +func (nopWriter) Write(p []byte) (int, error) { return len(p), nil } diff --git a/cmd/mcpproxy/token_cmd.go b/cmd/mcpproxy/token_cmd.go index 50b8f5334..309304bd6 100644 --- a/cmd/mcpproxy/token_cmd.go +++ b/cmd/mcpproxy/token_cmd.go @@ -668,8 +668,8 @@ func joinInterfaceSlice(m map[string]interface{}, key string, maxLen int) string strs[i] = fmt.Sprintf("%v", s) } result := strings.Join(strs, ",") - if maxLen > 0 && len(result) > maxLen { - result = result[:maxLen-3] + "..." + if maxLen > 0 { + result = truncateCell(result, maxLen) } return result } diff --git a/cmd/mcpproxy/upstream_cmd.go b/cmd/mcpproxy/upstream_cmd.go index 092378e8c..baf75253b 100644 --- a/cmd/mcpproxy/upstream_cmd.go +++ b/cmd/mcpproxy/upstream_cmd.go @@ -2482,10 +2482,7 @@ func runUpstreamInspect(_ *cobra.Command, args []string) error { approvedCount++ } - desc := r.Description - if len(desc) > 60 { - desc = desc[:57] + "..." - } + desc := truncateCell(r.Description, 60) hash := r.Hash if len(hash) > 12 {