Skip to content

Improve apply_patch diff UI and atomicity#47

Open
Waishnav wants to merge 6 commits into
mainfrom
codex/apply-patch-diff-ui
Open

Improve apply_patch diff UI and atomicity#47
Waishnav wants to merge 6 commits into
mainfrom
codex/apply-patch-diff-ui

Conversation

@Waishnav

@Waishnav Waishnav commented Jun 30, 2026

Copy link
Copy Markdown
Owner

Summary

  • render apply_patch cards with the multi-file review diff UI instead of the single-file edit renderer
  • stage apply_patch mutations before writing so failed patches do not leave partial filesystem changes
  • generate proper multi-hunk unified diffs for distant edits

Tests

  • npm test
  • npm run build
  • npm run typecheck

Summary by CodeRabbit

  • New Features

    • Improved patch action handling by generating unified diff-style output with consistent formatting.
    • Enhanced UI treatment of patch tools, including “diff” loading state and diff statistics badges, aligned with existing review rendering.
  • Bug Fixes

    • Patch changes are now safely staged and only written after all actions validate, avoiding leftover partial updates.
  • Tests

    • Expanded test coverage for patch failures, large multi-hunk diffs, trailing-space preservation, and patch-tool classification/expandability.
  • Chores

    • Added a dependency to support standardized diff generation.

@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: da75487d-004c-4532-9775-7b245c2ecab3

📥 Commits

Reviewing files that changed from the base of the PR and between 2d2c47f and daa729a.

📒 Files selected for processing (2)
  • src/apply-patch.test.ts
  • src/apply-patch.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/apply-patch.ts

📝 Walkthrough

Walkthrough

applyPatch now stages file changes in memory before writing or deleting files, and its diff output comes from the diff package. UI card helpers and workspace rendering now recognize apply_patch as a patch tool, with updated tests and patch-specific rendering behavior.

apply_patch staging, diff library, and UI patch tool classification

Layer / File(s) Summary
Staged in-memory apply_patch with diff library
package.json, src/apply-patch.ts, src/server.ts
Adds diff@^8.0.3, stages add/update/move/delete actions before filesystem writes, generates unified diff text with createTwoFilesPatch, and updates the apply_patch tool description to describe staged validation and writes.
isPatchTool helper and isEditTool narrowing
src/ui/card-types.ts, src/ui/card-types.test.ts
Removes apply_patch from isEditTool, adds isPatchTool, changes isExpandableCard to use patch payload presence, and updates helper tests.
Workspace rendering for patch tools
src/ui/workspace-app.tsx
Treats patch tools like review tools when rendering heavy payloads and includes patch tools in the diff statistics badge condition.
apply-patch tests for staging and hunk splitting
src/apply-patch.test.ts
Updates the missing-file expectation, adds a two-hunk long-file patch case, and adds a trailing-space preservation case.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • Waishnav/devspace#26: Introduced the apply_patch Codex tool that this PR directly refactors for staged writes and patch-tool UI handling.

Poem

🐇 I hopped through patches, neat and light,
Staging each file till all is right.
Two hunk headers twinkled in the night,
And diffed-up carrots came out bright.
Patch tools now bloom in the UI glow—
Hop hop, commit when all checks say “go”!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately captures the two main changes: the apply_patch diff UI update and the move to atomic staged application.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/apply-patch-diff-ui
⚔️ Resolve merge conflicts
  • Resolve merge conflict in branch codex/apply-patch-diff-ui

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/apply-patch.test.ts (1)

151-151: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Assert the patched file content too.

This test can pass on diff shape alone; add a readback assertion so it also catches regressions where the emitted patch has two hunks but the file was not updated correctly.

🧪 Proposed test strengthening
 assert.equal(splitHunkResult.patch.match(/^@@ /gm)?.length, 2);
+assert.equal(
+  await readFile(join(splitHunkRoot, "long.txt"), "utf8"),
+  [
+    "1", "two", "3", "4", "5", "6", "7", "8", "9", "10",
+    "11", "12", "13", "14", "15", "16", "17", "eighteen", "19", "20",
+  ].join("\n") + "\n",
+);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/apply-patch.test.ts` at line 151, The apply-patch test currently only
checks patch shape, so strengthen it by asserting the resulting file content
after applying the patch as well. In src/apply-patch.test.ts, update the test
around splitHunkResult so it reads back the patched file and verifies the
expected content, using the existing applyPatch/splitHunkResult flow to ensure
the patch emitted by the patching logic actually updates the file correctly.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/apply-patch.ts`:
- Line 434: The diff rendering in apply-patch should preserve intentional
trailing spaces on the final line instead of removing them with trimEnd().
Update the logic around the trimEnd() call to strip only the trailing newline
from the generated diff, keeping any spaces that are part of the last diff line;
use the surrounding diff-building code in apply-patch to locate the exact spot.

---

Nitpick comments:
In `@src/apply-patch.test.ts`:
- Line 151: The apply-patch test currently only checks patch shape, so
strengthen it by asserting the resulting file content after applying the patch
as well. In src/apply-patch.test.ts, update the test around splitHunkResult so
it reads back the patched file and verifies the expected content, using the
existing applyPatch/splitHunkResult flow to ensure the patch emitted by the
patching logic actually updates the file correctly.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 32c094a6-916c-4b71-ad3d-d3e84cd0b17b

📥 Commits

Reviewing files that changed from the base of the PR and between 6607926 and 2d2c47f.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (7)
  • package.json
  • src/apply-patch.test.ts
  • src/apply-patch.ts
  • src/server.ts
  • src/ui/card-types.test.ts
  • src/ui/card-types.ts
  • src/ui/workspace-app.tsx

Comment thread src/apply-patch.ts Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant