Skip to content

Improve apply_patch diff UI and validation#47

Merged
Waishnav merged 10 commits into
mainfrom
codex/apply-patch-diff-ui
Jun 30, 2026
Merged

Improve apply_patch diff UI and validation#47
Waishnav merged 10 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 apply_patch output with more accurate unified-diff formatting (headers, file modes) and consistent trailing-newline handling.
    • Enhanced UI rendering for patch results, including “Loading diff...”, diff statistics badge behavior, and patch-specific title/icon/tone.
  • Bug Fixes
    • Patch changes are now staged in memory and written only after all patch actions validate, preventing partial filesystem updates on failure.
  • Tests
    • Added/expanded tests for staged-failure behavior, multi-hunk output, trailing-space preservation, and patch-tool classification/expandability.
  • Chores
    • Added a diff dependency to standardize patch generation.

@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

applyPatch now stages file changes in memory before persisting them, and its diff output comes from the diff package. Patch-related UI types, display logic, and workspace rendering were updated to treat apply_patch as a patch tool.

Changes

apply_patch staging and UI patch handling

Layer / File(s) Summary
Staged applyPatch flow
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 match staged validation and writes.
applyPatch behavior tests
src/apply-patch.test.ts
Updates missing-file behavior, verifies multi-hunk output, and checks trailing-space preservation.
Patch display metadata
src/ui/patch-display.ts, src/ui/patch-display.test.ts
Adds patch display helpers that derive titles, tones, and icon operations from file operations, with tests covering add, update, move, and delete combinations.
Patch tool card types
src/ui/card-types.ts, src/ui/card-types.test.ts
Adds patch operation typing, introduces isPatchTool, narrows isEditTool, changes expandability rules for patch cards, and updates helper tests.
Workspace patch rendering
src/ui/workspace-app.tsx
Routes apply_patch through patch-aware payload rendering, loading text, diff badges, and display selection.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

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

Poem

🐇 I hopped through patches, neat and light,
Staging each file till all was right.
Two hunks twinkled in the carrot moon,
And trailing spaces danced a tune.
The patch tool shines in UI glow—
Hop hop, when all is green, let it 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.
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.
Title check ✅ Passed It clearly summarizes the main changes: the apply_patch diff UI update and staged validation/atomicity behavior.
✨ 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

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
@Waishnav

Copy link
Copy Markdown
Owner Author

@coderabbitai review it again

@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown

@Waishnav Re-reviewing the changes in #47 now.

✅ Action performed

Full review finished.

@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: 2

🤖 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`:
- Around line 374-380: The final commit in applyPatch is still applied
incrementally, so a failure in writeTextFile or rm can leave a partial workspace
state. Update the commit path in applyPatch to make the write/delete phase
transactional or rollback-safe, using the staged map and the existing validation
flow to ensure all filesystem mutations either complete together or are reverted
on error; focus on the two loops that apply file.content and delete missing
files.
- Around line 362-365: The move handling in apply-patch.ts is using raw
path-string comparison to decide whether to stage the source as deleted, which
breaks case-only renames on case-insensitive filesystems. Update the logic
around the destination/absolute check in the patch application flow so `Foo.ts`
-> `foo.ts` is treated as the same filesystem entry and does not trigger a
delete of the newly written file. Use the existing move/staging symbols like
`readStagedOptional`, `staged.set`, and `unifiedFilePatch` to locate the rename
path and replace the string inequality with a filesystem-identity-aware check.
🪄 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: 5d175ebf-a573-42af-b94e-3fb46a345294

📥 Commits

Reviewing files that changed from the base of the PR and between 6607926 and 0464e55.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (9)
  • 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/patch-display.test.ts
  • src/ui/patch-display.ts
  • src/ui/workspace-app.tsx

Comment thread src/apply-patch.ts Outdated
Comment thread src/apply-patch.ts
@Waishnav

Copy link
Copy Markdown
Owner Author

@coderabbitai regarding the write/delete phase transactional rollback comment: we are intentionally not tackling that in this PR.

This branch improves validation-time safety: patch actions are parsed, paths are resolved, hunks are applied, and final file states are prepared before any filesystem writes happen. That prevents validation/hunk failures from leaving earlier patch actions applied.

Making the final filesystem mutation phase fully transactional or rollback-safe is a larger and different guarantee. Once writes/deletes begin, true all-or-nothing behavior across multiple files is difficult to guarantee portably, and a best-effort rollback path would add meaningful complexity while still being fallible under I/O errors, permission changes, crashes, cross-device behavior, etc.

We also removed the tool-description wording that implied staged/atomic writes, so the implementation no longer over-promises this guarantee to model callers. For this PR, we fixed the concrete case-only move issue and are leaving full rollback-safe commits as a separate design decision.

@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown

Tip

For best results, initiate chat on the files or code changes.

@Waishnav Thanks for the clarification — that makes sense.

I agree this PR’s guarantee is specifically validation-time safety before filesystem mutation, not a fully transactional/rollback-safe commit phase. Since the tool description was also updated to avoid implying stronger atomicity than the implementation provides, I’m withdrawing that rollback comment for #47.

(ᵔᴥᵔ)


✏️ Learnings added
Learnt from: Waishnav
URL: https://github.com/Waishnav/devspace/pull/47

Timestamp: 2026-06-30T19:22:03.168Z
Learning: In pull request `#47` for the repository `Waishnav/devspace`, the changes in `src/apply-patch.ts` intentionally improve validation-time safety only: patch actions are parsed, paths are resolved, hunks are applied, and final file states are prepared before any filesystem writes occur, but the final write/delete phase is not intended to be rollback-safe or fully transactional across multiple files. The wording in `src/server.ts` was updated accordingly so the `apply_patch` tool description no longer implies staged or atomic writes.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

@Waishnav Waishnav changed the title Improve apply_patch diff UI and atomicity Improve apply_patch diff UI and validation Jun 30, 2026
@Waishnav Waishnav merged commit e08f5b2 into main Jun 30, 2026
4 checks passed
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