@@ -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\n line\n content" )
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