Skip to content

Commit c3f598a

Browse files
authored
Extract duplicate shortenCommand() to shared utility function (#2682)
1 parent df7a13a commit c3f598a

File tree

5 files changed

+77
-23
lines changed

5 files changed

+77
-23
lines changed

.changeset/patch-extract-shorten-command-utility.md

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/workflow/claude_logs.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ func (e *ClaudeEngine) parseToolCallsWithSequence(contentArray []any, toolCallMa
387387
if command, exists := inputMap["command"]; exists {
388388
if commandStr, ok := command.(string); ok {
389389
// Create unique bash entry with command info, avoiding colons
390-
uniqueBashName := fmt.Sprintf("bash_%s", e.shortenCommand(commandStr))
390+
uniqueBashName := fmt.Sprintf("bash_%s", ShortenCommand(commandStr))
391391
prettifiedName = uniqueBashName
392392
}
393393
}
@@ -472,7 +472,7 @@ func (e *ClaudeEngine) parseToolCalls(contentArray []any, toolCallMap map[string
472472
if command, exists := inputMap["command"]; exists {
473473
if commandStr, ok := command.(string); ok {
474474
// Create unique bash entry with command info, avoiding colons
475-
uniqueBashName := fmt.Sprintf("bash_%s", e.shortenCommand(commandStr))
475+
uniqueBashName := fmt.Sprintf("bash_%s", ShortenCommand(commandStr))
476476
prettifiedName = uniqueBashName
477477
}
478478
}
@@ -531,16 +531,6 @@ func (e *ClaudeEngine) parseToolCalls(contentArray []any, toolCallMap map[string
531531
}
532532
}
533533

534-
// shortenCommand creates a short identifier for bash commands
535-
func (e *ClaudeEngine) shortenCommand(command string) string {
536-
// Take first 20 characters and remove newlines
537-
shortened := strings.ReplaceAll(command, "\n", " ")
538-
if len(shortened) > 20 {
539-
shortened = shortened[:20] + "..."
540-
}
541-
return shortened
542-
}
543-
544534
// estimateInputSize estimates the input size in tokens from a tool input object
545535
func (e *ClaudeEngine) estimateInputSize(input any) int {
546536
// Convert input to JSON string to get approximate size

pkg/workflow/codex_engine.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ func (e *CodexEngine) parseCodexToolCallsWithSequence(line string, toolCallMap m
391391

392392
if execCommand != "" {
393393
// Create unique bash entry with command info, avoiding colons
394-
uniqueBashName := fmt.Sprintf("bash_%s", e.shortenCommand(execCommand))
394+
uniqueBashName := fmt.Sprintf("bash_%s", ShortenCommand(execCommand))
395395

396396
// Initialize or update tool call info
397397
if toolInfo, exists := toolCallMap[uniqueBashName]; exists {
@@ -440,16 +440,6 @@ func (e *CodexEngine) updateMostRecentToolWithDuration(toolCallMap map[string]*T
440440
}
441441
}
442442

443-
// shortenCommand creates a short identifier for bash commands
444-
func (e *CodexEngine) shortenCommand(command string) string {
445-
// Take first 20 characters and remove newlines
446-
shortened := strings.ReplaceAll(command, "\n", " ")
447-
if len(shortened) > 20 {
448-
shortened = shortened[:20] + "..."
449-
}
450-
return shortened
451-
}
452-
453443
// extractCodexTokenUsage extracts token usage from Codex-specific log lines
454444
func (e *CodexEngine) extractCodexTokenUsage(line string) int {
455445
// Codex format: "tokens used: 13934"

pkg/workflow/strings.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,14 @@ func SanitizeWorkflowName(name string) string {
4747

4848
return name
4949
}
50+
51+
// ShortenCommand creates a short identifier for bash commands.
52+
// It replaces newlines with spaces and truncates to 20 characters if needed.
53+
func ShortenCommand(command string) string {
54+
// Take first 20 characters and remove newlines
55+
shortened := strings.ReplaceAll(command, "\n", " ")
56+
if len(shortened) > 20 {
57+
shortened = shortened[:20] + "..."
58+
}
59+
return shortened
60+
}

pkg/workflow/strings_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,61 @@ func TestSanitizeWorkflowName(t *testing.T) {
8989
})
9090
}
9191
}
92+
93+
func TestShortenCommand(t *testing.T) {
94+
tests := []struct {
95+
name string
96+
input string
97+
expected string
98+
}{
99+
{
100+
name: "short command",
101+
input: "ls -la",
102+
expected: "ls -la",
103+
},
104+
{
105+
name: "exactly 20 characters",
106+
input: "12345678901234567890",
107+
expected: "12345678901234567890",
108+
},
109+
{
110+
name: "long command gets truncated",
111+
input: "this is a very long command that exceeds the limit",
112+
expected: "this is a very long ...",
113+
},
114+
{
115+
name: "newlines replaced with spaces",
116+
input: "echo hello\nworld",
117+
expected: "echo hello world",
118+
},
119+
{
120+
name: "multiple newlines",
121+
input: "line1\nline2\nline3",
122+
expected: "line1 line2 line3",
123+
},
124+
{
125+
name: "long command with newlines",
126+
input: "echo this is\na very long\ncommand with newlines",
127+
expected: "echo this is a very ...",
128+
},
129+
{
130+
name: "empty string",
131+
input: "",
132+
expected: "",
133+
},
134+
{
135+
name: "only newlines",
136+
input: "\n\n\n",
137+
expected: " ",
138+
},
139+
}
140+
141+
for _, tt := range tests {
142+
t.Run(tt.name, func(t *testing.T) {
143+
result := ShortenCommand(tt.input)
144+
if result != tt.expected {
145+
t.Errorf("ShortenCommand(%q) = %q, want %q", tt.input, result, tt.expected)
146+
}
147+
})
148+
}
149+
}

0 commit comments

Comments
 (0)