Add PUT /api/v1/plans/:id/content for full content replacement#101
Merged
HamptonMakes merged 2 commits intomainfrom Apr 29, 2026
Merged
Add PUT /api/v1/plans/:id/content for full content replacement#101HamptonMakes merged 2 commits intomainfrom
HamptonMakes merged 2 commits intomainfrom
Conversation
The agent-friendly edit path: read the plan, edit markdown locally, PUT it back. Server diffs against current revision via Diff::LCS, decomposes into replace_exact ops (preserving comment anchors in unchanged regions via existing OT), and creates a new immutable PlanVersion. - Plans::DiffToOperations — line-level LCS diff to ordered ops with _pre_resolved_ranges and cumulative-delta-aware positions - Plans::ReplaceContent — orchestrator: locks plan, validates base_revision, normalizes CRLF to LF, applies ops, creates version, marks anchors out-of-date - Api::V1::ContentController — thin controller, optimistic concurrency via 409 - Disable wrap_parameters on API base controller (was silently nesting body under params[:content]) - Allow empty old_text in apply_replace_exact when _pre_resolved_ranges is set - Fix delta calculation to use range slice (not old_text.length) so mismatched caller-supplied old_text can't corrupt OT metadata - Update agent-instructions doc to feature this as the primary edit path Diff service is line-level for two reasons: anchors in unchanged regions survive intact via Plans::TransformRange, and operations_json stays compact. Tests cover: roundtrip property (incl. 30-iteration fuzz), single/multi-hunk shapes, append/insert/delete at every position, trailing-newline variants, unicode, very long single-line content, repeated identical lines (LCS ambiguity), shared prefix/suffix, contiguous delete-then-insert, large files with disjoint hunks, CRLF normalization, RoundtripFailureError, stale revision 409, anchor preservation (before/inside/after change), and a delta-calculation regression for the operations API surface. Amp-Thread-ID: https://ampcode.com/threads/T-019dda2a-85b2-706b-b5f8-67601e9dcf58 Co-authored-by: Amp <amp@ampcode.com>
Collaborator
Author
Collaborator
Author
|
@codex are you dead!?! |
|
To use Codex here, create an environment for this repo. |
Contributor
There was a problem hiding this comment.
Pull request overview
Adds an agent-friendly “full content replacement” edit path for plans by introducing a new PUT /api/v1/plans/:id/content endpoint that diffs incoming markdown into OT-compatible replace_exact operations, applies them to generate positional metadata, and persists a new immutable PlanVersion with optimistic concurrency via base_revision.
Changes:
- Introduce
PUT /api/v1/plans/:id/contentplus request specs and updated agent instructions recommending this workflow. - Add
Plans::DiffToOperations(line-level LCS → orderedreplace_exactops with_pre_resolved_ranges) andPlans::ReplaceContentorchestrator (locking, CRLF→LF normalization, apply+roundtrip check, anchor handling, broadcast). - Harden
ApplyOperations#replace_exactdelta computation to derive from the actual resolved range length (not caller-suppliedold_textlength) and relaxold_textblank validation when_pre_resolved_rangesis provided.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| spec/services/plans/replace_content_spec.rb | Service-level coverage for version creation, no-op, stale revision, CRLF normalization, roundtrip safety, and anchor behavior. |
| spec/services/plans/diff_to_operations_spec.rb | Extensive roundtrip/property and edge-case coverage for diff→ops generation and OT compatibility. |
| spec/services/plans/apply_operations_spec.rb | Regression test ensuring delta is computed from resolved range slice length. |
| spec/requests/api/v1/content_spec.rb | Request-level coverage for endpoint behavior: create/no-op, validation, auth, concurrency, anchor out-of-date. |
| engine/config/routes.rb | Adds singular content resource under plans for update (PUT/PATCH). |
| engine/app/views/coplan/agent_instructions/show.text.erb | Documents the new recommended edit workflow and relegates lease+ops to “Advanced”. |
| engine/app/services/coplan/plans/replace_content.rb | New orchestrator implementing lock+diff+apply+version creation + anchor marking + broadcast. |
| engine/app/services/coplan/plans/diff_to_operations.rb | New diff service generating ordered replace_exact ops with _pre_resolved_ranges. |
| engine/app/services/coplan/plans/apply_operations.rb | Updates replace_exact validation and delta calculation to avoid metadata corruption. |
| engine/app/controllers/coplan/api/v1/content_controller.rb | New API controller implementing PUT /content behavior and error mapping. |
| engine/app/controllers/coplan/api/v1/base_controller.rb | Disables wrap_parameters to prevent JSON body auto-wrapping collisions. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Agents are great at producing whole files, but the only existing edit path was the lease + semantic-operations dance, which agents (and humans!) get wrong constantly. The local agent skill already documented a
PUT /contentendpoint as if it existed — and Hampton's agent kept hitting 404s trying to use it. This makes the documented path real and promotes it to the recommended workflow.What
PUT /api/v1/plans/:id/content— read plan, edit markdown locally, PUT it back. Server diffs against current revision, decomposes into ops (preserving comment anchors in unchanged regions via existing OT), creates a new immutable PlanVersion. Optimistic concurrency viabase_revision(returns 409 on stale).Plans::DiffToOperations— line-level LCS diff → orderedreplace_exactops with cumulative-delta-aware_pre_resolved_ranges. Granularity chosen so anchors in unchanged regions survive intact.Plans::ReplaceContent— orchestrator service. Locks plan, normalizes CRLF→LF (so Windows/textarea content doesn't trigger wholesale rewrite), applies ops, marks anchors out-of-date, broadcasts.wrap_parameterson the API base controller — was silently nesting JSON body underparams[:content].apply_replace_exactdelta to be computed from the actual range slice (not fromold_text.length) so mismatched caller-suppliedold_textcan't corrupt OT metadata. Also relaxed theold_textblank check when_pre_resolved_rangesis supplied (needed for pure-insertion hunks)./agent-instructionsdoc to feature this as the primary edit path; lease + operations relegated to "Advanced".Risk Assessment
Low — purely additive endpoint; existing lease+operations path is unchanged in semantics. The two
apply_operations.rbmodifications were reviewed for backward compatibility (existing tests pass, plus a delta-bug regression test). Comment-anchor invariants are covered by tests at the diff, service, and request layers.References
DiffToOperations(viaTransformRange),ReplaceContent(anchors before/inside/after change), and request spec (marks overlapping anchors out-of-date)_pre_resolved_ranges, namedRoundtripFailureErrorexception with controller rescueGenerated with Amp