Skip to content

fix(ai): clarify stale proposal reanchor conflicts#154

Merged
iipanda merged 1 commit into
mainfrom
fix/ai-proposal-reanchor-conflict
May 28, 2026
Merged

fix(ai): clarify stale proposal reanchor conflicts#154
iipanda merged 1 commit into
mainfrom
fix/ai-proposal-reanchor-conflict

Conversation

@iipanda
Copy link
Copy Markdown
Collaborator

@iipanda iipanda commented May 28, 2026

Summary

  • Return source-anchor conflict messages when stale replace proposals cannot reanchor.
  • Reuse replace-selection match validation for stale revision fallback.
  • Add regression coverage for missing source text.

Test Plan

  • bun test --cwd packages/modules/core.ai ./src/server/routes.test.ts
  • bun run format:check
  • bun run changeset:check
  • bun run check

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced conflict detection when applying AI proposals with replace-selection operations on stale drafts, now providing more specific error messages when original text cannot be re-anchored.
  • Tests

    • Added test coverage validating proper error handling for stale replace-selection proposals.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 28, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 549743a1-b110-4420-8634-97f3fa810430

📥 Commits

Reviewing files that changed from the base of the PR and between a5716ee and 2eaf8ef.

📒 Files selected for processing (2)
  • packages/modules/core.ai/src/server/apply.ts
  • packages/modules/core.ai/src/server/routes.test.ts

📝 Walkthrough

Walkthrough

This PR improves error handling for AI proposal applications by extracting selection-matching logic into a reusable helper and prioritizing specific conflict errors. When replace_selection proposals become stale and cannot locate their original text in the current draft, the system now signals a selection-specific conflict instead of a generic base-draft mismatch.

Changes

Replace Selection Conflict Detection

Layer / File(s) Summary
Helper type and conflict detection
packages/modules/core.ai/src/server/apply.ts
Introduces ReplaceSelectionOperation type alias and findReplaceSelectionMatch helper that locates the unique occurrence of operation.originalText in a draft body, routing match failures to aiProposalConflict with selection-specific error details.
Apply operation integration
packages/modules/core.ai/src/server/apply.ts
applyReplaceSelection now uses the helper to compute replacement position. applyAiProposal adds an intermediate call to the helper in the base-draft mismatch failure path for replace_selection proposals, ensuring specific conflict conditions trigger before the generic mismatch error.
Test coverage for stale selection conflicts
packages/modules/core.ai/src/server/routes.test.ts
New route test verifies that stale replace_selection proposals fail with 409 AI_PROPOSAL_CONFLICT when the original selection text cannot be re-anchored in the current draft, asserting the error response includes selection-specific details and confirms no document update occurs.

Sequence Diagram

sequenceDiagram
  participant Client
  participant applyAiProposal
  participant findReplaceSelectionMatch
  participant applyReplaceSelection
  participant aiProposalConflict
  Client->>applyAiProposal: POST /api/v1/ai/proposals/:id/apply
  alt baseDraftRevision matches
    applyAiProposal->>applyReplaceSelection: call with operation
    applyReplaceSelection->>findReplaceSelectionMatch: locate originalText
    alt originalText found uniquely
      findReplaceSelectionMatch-->>applyReplaceSelection: return match index
      applyReplaceSelection->>applyReplaceSelection: update body at index
      applyReplaceSelection-->>applyAiProposal: success
      applyAiProposal-->>Client: 200 OK
    else originalText missing or ambiguous
      findReplaceSelectionMatch-->>aiProposalConflict: emit selection details
      aiProposalConflict-->>applyAiProposal: conflict error
      applyAiProposal-->>Client: 409 AI_PROPOSAL_CONFLICT
    end
  else baseDraftRevision does not match
    applyAiProposal->>findReplaceSelectionMatch: check if replace_selection still valid
    alt originalText found uniquely
      findReplaceSelectionMatch-->>applyAiProposal: match index
      applyAiProposal->>applyReplaceSelection: proceed with stale revision
      applyReplaceSelection-->>applyAiProposal: success
      applyAiProposal-->>Client: 200 OK
    else originalText missing or ambiguous
      findReplaceSelectionMatch-->>aiProposalConflict: emit selection details
      aiProposalConflict-->>applyAiProposal: selection conflict error
      applyAiProposal-->>Client: 409 AI_PROPOSAL_CONFLICT
    end
  end
Loading

Estimated Code Review Effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

A selection once made, now must stand its ground,
When drafts grow stale and sources cannot be found,
A rabbit refined the detection with care,
Specific conflicts now float through the air,
No more generic errors to spare! 🐰✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.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 PR title accurately describes the main change: it addresses how stale AI proposals handle reanchoring conflicts by clarifying the error messages returned.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 fix/ai-proposal-reanchor-conflict

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


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.

❤️ Share

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

@iipanda iipanda merged commit 55e7e4e into main May 28, 2026
6 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