@@ -773,13 +773,13 @@ as the top-level structure."
773773 (pi-coding-agent--display-thinking-delta " about this.")
774774 (should (string-match-p "Let me think... about this." (buffer-string)))))
775775
776- (ert-deftest pi-coding-agent-test-display-agent-end-adds-newlines ()
777- "agent_end event adds trailing newlines ."
776+ (ert-deftest pi-coding-agent-test-display-agent-end-adds-newline ()
777+ "agent_end normalizes trailing whitespace to single newline ."
778778 (with-temp-buffer
779779 (pi-coding-agent-chat-mode)
780780 (pi-coding-agent--append-to-chat "Some response")
781781 (pi-coding-agent--display-agent-end)
782- (should (string-suffix-p "\n \n" (buffer-string)))))
782+ (should (string-suffix-p "response \n" (buffer-string)))))
783783
784784(ert-deftest pi-coding-agent-test-spacing-no-blank-line-after-user-header ()
785785 "User header has no blank line after setext underline.
@@ -800,6 +800,26 @@ The hidden === provides visual spacing when `markdown-hide-markup' is t."
800800 ;; Pattern: setext heading (Assistant + underline), NO blank line, content
801801 (should (string-match-p "Assistant\n=+\nHi" (buffer-string)))))
802802
803+ (ert-deftest pi-coding-agent-test-spacing-delta-leading-newlines-stripped ()
804+ "Leading newlines from first text delta are stripped.
805+ Models often send \\n\\n before first content, which would create
806+ extra blank lines after the setext header."
807+ (with-temp-buffer
808+ (pi-coding-agent-chat-mode)
809+ (pi-coding-agent--display-agent-start)
810+ (pi-coding-agent--display-message-delta "\n\nHi")
811+ (should (string-match-p "Assistant\n=+\nHi" (buffer-string)))))
812+
813+ (ert-deftest pi-coding-agent-test-spacing-thinking-leading-newlines-stripped ()
814+ "Leading newlines before thinking block are stripped.
815+ Models may send \\n\\n before thinking content too."
816+ (with-temp-buffer
817+ (pi-coding-agent-chat-mode)
818+ (pi-coding-agent--display-agent-start)
819+ (pi-coding-agent--display-thinking-start)
820+ ;; Thinking start should appear directly after header, no blank line
821+ (should (string-match-p "Assistant\n=+\n>" (buffer-string)))))
822+
803823(ert-deftest pi-coding-agent-test-spacing-blank-line-before-tool ()
804824 "Tool block is preceded by blank line when after text."
805825 (with-temp-buffer
@@ -823,6 +843,34 @@ The hidden === provides visual spacing when `markdown-hide-markup' is t."
823843 ;; Should end with closing fence and blank line
824844 (should (string-match-p "```\n\n" (buffer-string)))))
825845
846+ (ert-deftest pi-coding-agent-test-spacing-single-blank-line-between-turns ()
847+ "Only one blank line between agent response and next section header.
848+ agent_end + next section's leading newline must not create triple newlines."
849+ (with-temp-buffer
850+ (pi-coding-agent-chat-mode)
851+ ;; Turn 1: user + assistant
852+ (pi-coding-agent--display-user-message "Hi")
853+ (pi-coding-agent--display-agent-start)
854+ (pi-coding-agent--display-message-delta "Hello!")
855+ (pi-coding-agent--render-complete-message)
856+ (pi-coding-agent--display-agent-end)
857+ ;; Turn 2: user message
858+ (setq pi-coding-agent--assistant-header-shown nil)
859+ (pi-coding-agent--display-user-message "Bye")
860+ ;; Should never have triple newlines (which would be two blank lines)
861+ (should-not (string-match-p "\n\n\n" (buffer-string)))))
862+
863+ (ert-deftest pi-coding-agent-test-spacing-single-blank-line-before-compaction ()
864+ "Only one blank line between agent response and compaction header."
865+ (with-temp-buffer
866+ (pi-coding-agent-chat-mode)
867+ (pi-coding-agent--display-agent-start)
868+ (pi-coding-agent--display-message-delta "Some response.")
869+ (pi-coding-agent--render-complete-message)
870+ (pi-coding-agent--display-agent-end)
871+ (pi-coding-agent--display-compaction-result 50000 "Summary.")
872+ (should-not (string-match-p "\n\n\n" (buffer-string)))))
873+
826874(ert-deftest pi-coding-agent-test-spacing-no-double-blank-between-tools ()
827875 "Consecutive tools have single blank line between them."
828876 (with-temp-buffer
0 commit comments