Skip to content

Commit b4175da

Browse files
Copilotpelikhan
andcommitted
Fix markdown log RPC title rendering - remove double wrapping
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
1 parent f273c26 commit b4175da

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

β€Žinternal/logger/markdown_logger.goβ€Ž

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,15 @@ func (ml *MarkdownLogger) Log(level LogLevel, category, format string, args ...i
147147
// Format as markdown bullet point with emoji
148148
// Use code blocks for multi-line content or technical details
149149
var logLine string
150-
if strings.Contains(message, "\n") || strings.Contains(message, "command=") || strings.Contains(message, "args=") {
150+
151+
// Check if message is already pre-formatted (RPC messages with markdown formatting)
152+
// RPC messages start with ** and contain β†’ or ← arrows
153+
isPreformatted := strings.HasPrefix(message, "**") && (strings.Contains(message, "β†’") || strings.Contains(message, "←"))
154+
155+
if isPreformatted {
156+
// Pre-formatted content (like RPC messages) - just add bullet and emoji
157+
logLine = fmt.Sprintf("- %s %s %s\n", emoji, category, message)
158+
} else if strings.Contains(message, "\n") || strings.Contains(message, "command=") || strings.Contains(message, "args=") {
151159
// Multi-line or technical content - use code block
152160
logLine = fmt.Sprintf("- %s **%s**\n ```\n %s\n ```\n", emoji, category, message)
153161
} else {

β€Žinternal/logger/markdown_logger_test.goβ€Ž

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,58 @@ func TestMarkdownLoggerFallback(t *testing.T) {
309309
// Should not panic when logging
310310
LogInfoMd("test", "This should not crash")
311311
}
312+
313+
func TestMarkdownLoggerRPCFormatting(t *testing.T) {
314+
tmpDir := t.TempDir()
315+
logDir := filepath.Join(tmpDir, "logs")
316+
fileName := "rpc-format-test.md"
317+
318+
err := InitMarkdownLogger(logDir, fileName)
319+
if err != nil {
320+
t.Fatalf("InitMarkdownLogger failed: %v", err)
321+
}
322+
323+
// Test RPC-style pre-formatted messages (should NOT be wrapped in code blocks)
324+
LogDebugMd("rpc", "**github**β†’`tools/list`")
325+
LogDebugMd("rpc", "**safeoutputs**β†’`tools/call`\n\n```json\n{\n \"id\": 1\n}\n```")
326+
LogDebugMd("rpc", "**backend**←`resp`")
327+
328+
// Test regular messages (should use code blocks for multi-line)
329+
LogInfoMd("backend", "command=/usr/bin/docker args=[run]")
330+
LogInfoMd("backend", "Multi\nline\ncontent")
331+
332+
CloseMarkdownLogger()
333+
334+
// Read the log file
335+
logPath := filepath.Join(logDir, fileName)
336+
content, err := os.ReadFile(logPath)
337+
if err != nil {
338+
t.Fatalf("Failed to read log file: %v", err)
339+
}
340+
341+
logContent := string(content)
342+
343+
// RPC messages should be on single lines (not wrapped in code blocks)
344+
// Check that "- πŸ” rpc **github**β†’`tools/list`" is on one line
345+
if !strings.Contains(logContent, "- πŸ” rpc **github**β†’`tools/list`") {
346+
t.Errorf("RPC message should be on single line without extra code block wrapping")
347+
}
348+
349+
// Check that RPC messages with JSON blocks are properly formatted
350+
// The title should be on one line, followed by the JSON block
351+
if !strings.Contains(logContent, "- πŸ” rpc **safeoutputs**β†’`tools/call`") {
352+
t.Errorf("RPC message with JSON should have title on single line")
353+
}
354+
355+
// Regular multi-line messages should still use code blocks
356+
if !strings.Contains(logContent, "- βœ“ **backend**\n ```\n command=") {
357+
t.Errorf("Regular multi-line messages should still use code blocks")
358+
}
359+
360+
// Count occurrences of nested code blocks (should not happen)
361+
// A nested code block would look like: ``` \n **server** \n ```
362+
nestedPattern := "```\n **"
363+
if strings.Contains(logContent, nestedPattern) {
364+
t.Errorf("Found nested code blocks - RPC messages should not be double-wrapped")
365+
}
366+
}

0 commit comments

Comments
Β (0)