Skip to content

Add PUT /api/v1/plans/:id/content for full content replacement#101

Merged
HamptonMakes merged 2 commits intomainfrom
hampton/content-replacement-endpoint
Apr 29, 2026
Merged

Add PUT /api/v1/plans/:id/content for full content replacement#101
HamptonMakes merged 2 commits intomainfrom
hampton/content-replacement-endpoint

Conversation

@HamptonMakes
Copy link
Copy Markdown
Collaborator

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 /content endpoint 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

  • New 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 via base_revision (returns 409 on stale).
  • Plans::DiffToOperations — line-level LCS diff → ordered replace_exact ops 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.
  • Disabled wrap_parameters on the API base controller — was silently nesting JSON body under params[:content].
  • Fixed apply_replace_exact delta to be computed from the actual range slice (not from old_text.length) so mismatched caller-supplied old_text can't corrupt OT metadata. Also relaxed the old_text blank check when _pre_resolved_ranges is supplied (needed for pure-insertion hunks).
  • Updated /agent-instructions doc 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.rb modifications 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

  • 765/765 specs pass (13 new tests across diff/replace/apply specs)
  • Diff service has a 30-iteration random fuzz roundtrip plus targeted tests for: trailing-newline variants, unicode, repeated identical lines (LCS ambiguity), shared prefix/suffix, contiguous delete-then-insert, 100k-char single line, large files with many disjoint hunks
  • Anchor preservation tested at three layers: DiffToOperations (via TransformRange), ReplaceContent (anchors before/inside/after change), and request spec (marks overlapping anchors out-of-date)
  • Code review surfaced and fixed: CRLF normalization, delta-corruption attack surface via _pre_resolved_ranges, named RoundtripFailureError exception with controller rescue
  • Agent thread: https://ampcode.com/threads/T-019dda2a-85b2-706b-b5f8-67601e9dcf58

Generated with Amp

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>
@HamptonMakes HamptonMakes requested a review from Copilot April 29, 2026 18:35
@HamptonMakes
Copy link
Copy Markdown
Collaborator Author

@codex

@HamptonMakes HamptonMakes marked this pull request as ready for review April 29, 2026 18:38
@HamptonMakes
Copy link
Copy Markdown
Collaborator Author

@codex are you dead!?!

@chatgpt-codex-connector
Copy link
Copy Markdown

To use Codex here, create an environment for this repo.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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/content plus request specs and updated agent instructions recommending this workflow.
  • Add Plans::DiffToOperations (line-level LCS → ordered replace_exact ops with _pre_resolved_ranges) and Plans::ReplaceContent orchestrator (locking, CRLF→LF normalization, apply+roundtrip check, anchor handling, broadcast).
  • Harden ApplyOperations#replace_exact delta computation to derive from the actual resolved range length (not caller-supplied old_text length) and relax old_text blank validation when _pre_resolved_ranges is 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.

Comment thread engine/app/controllers/coplan/api/v1/content_controller.rb Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@HamptonMakes HamptonMakes merged commit e47dcea into main Apr 29, 2026
3 checks passed
@HamptonMakes HamptonMakes deleted the hampton/content-replacement-endpoint branch April 29, 2026 18:48
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.

2 participants