Repository navigation
docs: Add Prettier on AI Actions problem analysis & solution report - #371
Conversation
Comprehensive analysis of the formatting issues caused by client-side Prettier running during user editing sessions. Documents: - Core problem: Prettier deletes trailing spaces (user data loss) - All impacted AI tools (read_page, replace_lines, MCP operations) - All impacted UI surfaces (RichEditor, DocumentView, stores) - Data flow diagrams (current vs proposed) - Solution: Replace Prettier with minimal line-break inserter - Implementation plan with 5 phases - Testing strategy and rollback plan https://claude.ai/code/session_01V4nPAS89ocPuHtjevTdMFn
Expands the report with: - Appendix B: Tests requiring updates (MCP security test, tool tests) - Appendix C: Documentation requiring updates (5 doc files) - Appendix D: RichEditor.tsx specific line removals - Appendix E: MCP documents route specific changes - New test scenarios for addLineBreaksForAI https://claude.ai/code/session_01V4nPAS89ocPuHtjevTdMFn
Confirms that tool call renderers (RichContentRenderer, RichDiffRenderer, ToolCallRenderer, CompactToolCallRenderer) require NO changes: - HTML rendering ignores extra newlines (whitespace collapsed) - Line-based diffs actually improve with consistent newlines - Line number stripping already handles prefixed content Reorganized appendices: A (renderers), B (code), C (tests), D (docs), E (RichEditor), F (MCP route) https://claude.ai/code/session_01V4nPAS89ocPuHtjevTdMFn
Replaces Prettier for AI tool usage. Key differences: - ONLY adds newlines between block tags - NEVER removes trailing spaces (preserves user's mid-thought content) - Deterministic and fast (regex-based, not full HTML parser) - Idempotent (safe to run multiple times) Includes comprehensive test suite (22 tests) covering: - Trailing/leading space preservation - Block-level tag handling (p, div, h1-h6, ul, ol, li, table, etc.) - Inline element preservation - Nested structures - Edge cases (null, undefined, empty, plain text) - Idempotency - Real-world TipTap content https://claude.ai/code/session_01V4nPAS89ocPuHtjevTdMFn
Formats content with line breaks before returning to AI, ensuring consistent line numbers for replace_lines operations. The formatting preserves all user content including trailing spaces. https://claude.ai/code/session_01V4nPAS89ocPuHtjevTdMFn
Formats content with line breaks before applying line replacement, ensuring consistent line numbers match what read_page returns. Preserves all user content including trailing spaces. https://claude.ai/code/session_01V4nPAS89ocPuHtjevTdMFn
Remove client-side Prettier formatting that was causing data loss: - Trailing spaces deleted during editing - Visual disruption at 2500ms intervals - Race conditions between formatting and saving Solution: - New addLineBreaksForAI() utility that ONLY adds newlines, never removes content - Applied in AI tools (read_page, replace_lines) and MCP routes - Client-side formatting completely removed from RichEditor Changes: - Create lib/editor/line-breaks.ts with comprehensive tests - Update page-read-tools.ts and page-write-tools.ts to use addLineBreaksForAI - Replace formatHtml in MCP documents route with addLineBreaksForAI - Remove debouncedFormat, onFormatChange, updateContentSilently - Delete unused lib/editor/prettier.ts https://claude.ai/code/session_01V4nPAS89ocPuHtjevTdMFn
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
📝 WalkthroughWalkthroughReplaces Prettier-based HTML formatting with a new Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant RichEditor
participant DocumentView
participant useDocument
participant ContentStorage
rect rgba(200, 100, 100, 0.5)
Note over RichEditor,ContentStorage: OLD: Debounced Formatting Flow
User->>RichEditor: Edit content
RichEditor->>RichEditor: start debounce timer
RichEditor->>DocumentView: onFormatChange (delayed)
DocumentView->>useDocument: handleFormatChange
useDocument->>ContentStorage: updateContentSilently (no dirty flag)
end
rect rgba(100, 150, 100, 0.5)
Note over RichEditor,ContentStorage: NEW: Direct Update Flow
User->>RichEditor: Edit content
RichEditor->>DocumentView: onUpdate (immediate)
DocumentView->>useDocument: onChange (direct)
useDocument->>ContentStorage: update (marks dirty)
end
sequenceDiagram
participant AITools as AI Read/Write Tools
participant PageContent as Page Content
participant LineBreaks as addLineBreaksForAI
participant AIProcessor as AI Processor
participant MCP as MCP Route
rect rgba(100, 150, 200, 0.5)
Note over AITools,MCP: Content Processing with Line Breaks
AITools->>PageContent: fetch page.content
PageContent-->>AITools: raw HTML content
AITools->>LineBreaks: addLineBreaksForAI(content)
LineBreaks-->>AITools: formatted content (line breaks around blocks)
AITools->>AIProcessor: split into lines & process
MCP->>PageContent: fetch for document operation
PageContent-->>MCP: raw HTML
MCP->>LineBreaks: addLineBreaksForAI(newLines.join('\n'))
LineBreaks-->>MCP: formatted content
MCP->>AIProcessor: apply operation (replace/insert/delete)
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
This PR resolves issues with Prettier formatting in AI action workflows. The previous client-side Prettier formatting caused data loss (trailing spaces), visual disruption, and race conditions between user editing and AI operations. This has been replaced with a minimal
addLineBreaksForAI()utility.Key Changes
Core Solution:
addLineBreaksForAI()UtilityCreated
/apps/web/src/lib/editor/line-breaks.ts- a minimal line-break inserter that:What This Fixes
replace_linesFiles Changed
lib/editor/line-breaks.tslib/ai/tools/page-read-tools.tsaddLineBreaksForAI()lib/ai/tools/page-write-tools.tsaddLineBreaksForAI()app/api/mcp/documents/route.tsaddLineBreaksForAI()components/editors/RichEditor.tsxcomponents/.../DocumentView.tsxhandleFormatChange(-8 lines)hooks/useDocument.tsupdateContentSilently(-12 lines)lib/editor/prettier.tsdocs/.../prettier-on-ai-actions-report.mdTest Results
Documentation
Full problem analysis and solution report available in
docs/3.0-guides-and-tools/prettier-on-ai-actions-report.mdSummary by CodeRabbit
New Features
Improvements
Tests