Skip to content

feat(mcp): edit documents through the local mirror with a three-way merge - #773

Merged
pikann merged 41 commits into
Paca-AI:masterfrom
msammu07:feat/mcp-mirror-merge
Nov 15, 2026
Merged

pikann merged 41 commits into
Paca-AI:masterfrom
msammu07:feat/mcp-mirror-merge

Conversation

@msammu07

@msammu07 msammu07 commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Last part of #747 (docs pull / push / content check for coding agents). A file that pull wrote can now be edited and pushed back, and pull merges local edits with changes made in Paca. Builds on #769 (pull, push) and #770 (edit_doc). Only apps/mcp changes, plus one browser spec; nothing is added on the server.

npx @paca-ai/paca-mcp pull
$EDITOR .paca/docs/Plans/Roadmap.md
npx @paca-ai/paca-mcp push .paca/docs/Plans/Roadmap.md --note "move the launch to May"

push of a mirror file

  • push <file> on a file inside a pull mirror edits the document it was pulled from (no source, no lock), instead of writing a repository file.
  • Only the blocks the edit reaches are rewritten. The whole file goes through the same range code and final audit as edit_doc (feat(mcp): read, grep and edit parts of a document #770), so every other block is sent back exactly as stored, and a block with formatting Markdown cannot show is edited in place or the push is refused with its line.
  • The write carries If-Match with the pulled version. If the document changed in Paca the push stops (exit 2) with who changed it and when: pull merges, then push again. Whether it changed is asked of Paca on every push.
  • Editor habits are not edits: a byte order mark, CRLF, trailing spaces outside code, runs of blank lines, -/+ bullets, blank lines between list items, tab or four-space nesting, numbered items all written 1., table padding. These rules are one small line-based normaliser (utils/md-normalise.ts); anything else must be written as Paca writes it, or the push is refused with the line and what Paca would save there. After such a push the file is rewritten to Paca's Markdown (normalised <file>).
  • Refused before anything is sent: a file pull did not write, a managed copy, a deleted document, an empty file, a file with conflict markers, a file an interrupted pull left unfinished (exit 2, run pull first), a file whose base copy is gone or not what the last pull or push wrote.

pull merges local edits

  • A file edited here whose document changed in Paca is merged with git merge-file --diff3, with the pulled version (.paca-base/<id>.md) as the base. A clean merge is written (merged <file>); a conflict writes markers (<<<<<<< local, ||||||| base, >>>>>>> Paca (<writer>, <time>)), exits 2, and push refuses the file until they are resolved, inside fenced code too.
  • Behaviour change: until now such a file was left untouched (kept …; use --force to overwrite). Every line written locally is still in the file, between the markers.
  • A merge is journalled (Paca's version as .paca-base/<id>.next.md and a record in the state, then the file, then the base copy, then the final state), and the next pull settles a stopped one from what is on disk. A file is never merged twice and markers never nest.
  • When the version a file was edited from is not known (base copy and snapshot gone, or an interrupted pull may have written into the file before it was edited), the file is kept, Paca's version is written next to it as <file>.remote.md, and push refuses it. Only the user settles it: delete the file and pull, or pull --force. This holds even when the document goes back to an earlier version, so a push never re-applies a change that was taken back in Paca.
  • --force keeps its reach: it replaces files whose document changed or that still hold merge markers, never a file whose document did not change. Deleting one kept or conflicted file and pulling replaces that file alone.
  • Without git, the file is kept as before and the message says why.

Fix in the shared Markdown parser (own commit)

A checklist item next to a blank line was parsed with a space before its text (" task" for * [ ] task in a loose list). Mirror push makes this reachable in ordinary editing, so it is fixed at the source; write_doc and edit_doc use the same parser and now store the text without the space.

Decisions, listed so they can be challenged

  1. No new pairing logic. A whole-file edit is line-diffed into ranges and handed to feat(mcp): read, grep and edit parts of a document #770's code; the final audit runs over the whole document. What cannot pass is refused, never guessed.
  2. Conflict markers are written into the file, as git does, instead of leaving the file untouched.
  3. The normaliser is explicit and line-based. The file is not compared through the parser, which could hide content the parser drops.
  4. No push --force for a mirror file. pull, then push, is the path; overwriting another writer from a file is what this feature exists to prevent.
  5. A repository file under a folder that holds .paca-sync.json is refused, not pushed as a repository file (it would become a managed copy by accident). Keep the mirror in its own folder.

Known limits (all in the README section "Editing documents as files")

  • A reorder is refused when the document holds any block with formatting Markdown cannot show; in a document without such blocks it is sent, and a moved plain block may get a new id.
  • Some edits in several places at once are refused though each would pass alone (for example deleting a formatted paragraph and adding one elsewhere).
  • Three hand-written differences are read as no edit (blank runs inside indented code, | a against | a | on a line of text, blank lines at the end of an unclosed fence); Paca's own Markdown never has them.
  • A code block nested in a list item cannot be edited through the file (refused).
  • If .paca-base is wiped, edited files are refused until they are pulled again.
  • Not fixed here, already on master: 1. a followed by 2. [ ] b loses its checkbox in write_doc and edit_doc; mirror push refuses that file.

Type of Change

  • New feature (MCP package), plus one bug fix in the Markdown parser

Testing

  • apps/mcp: tsc clean; lint reports 4 warnings, all in files this PR does not touch; vitest 1283 of 1283 (54 files), also in a Linux container limited to 2 CPUs with the workflow's lint, build and test steps.
    • tools/doc-splice-markdown.test.ts: whole-file edits, reorders, formatted blocks, a seeded fuzz whose checks are written in the test.
    • cli/push-mirror.test.ts, cli/push-mirror-interrupt.test.ts: refusals, conflict, normalising push, a stop after every write.
    • cli/pull-merge.test.ts, cli/pull-merge-interrupt.test.ts: clean merge, conflict, missing base, no git, moved and edited; a stop (kill and thrown error) at every write of a plain pull and of a merge, then an optional edit, then Paca returning to the earlier version, then either pull and push, or push straight away: Paca never ends up with text that was taken back.
    • utils/md-normalise.test.ts: each rule, and a property test that a file differing from the render by one visible character never reads as equal.
    • Three existing expectations in cli/pull.test.ts changed, for the behaviour change above (markers instead of an untouched file); the other pull and push suites are unchanged.
    • No test asserts wall-clock time.
  • apps/e2e: new tests/docs/mcp-mirror.spec.ts runs the built CLI against the stack through the gateway: a person edits paragraph 1 in the web editor while a mirror file changes paragraph 3; the push is refused, pull merges, the push succeeds, both changes are in the document and the other blocks are stored unchanged. It passes, as does mcp-edit-doc.spec.ts.
  • Browser specs (tests/docs and tests/realtime, chromium) on a stack built from this branch: 217 passed, 6 skipped, 2 failed. Both failures are in docs.spec.ts and not in code this PR touches (apps/web and services are unchanged): :640 (edit own comment, uses Control+a on macOS) fails the same way on master; :350 (rename from the sidebar) reads the API before the save lands and failed 4 of 5 runs.

Checklist

  • The change is focused and scoped: one feature; the parser fix it depends on is its own commit.
  • Related documentation is updated: apps/mcp/README.md (section "Editing documents as files"), apps/mcp/CHANGELOG.md, CLI help.

spliceMarkdown diffs a document's render against an edited file by
lines and applies every changed run through edit_doc's range code,
alignment and audit in one pass, then audits the whole result and
requires it to render to exactly the file. Blocks outside the changes
stay the objects read. spliceEdit's body is split into prepare and
applyRanges without changing what it returns or refuses; the line diff
moves to utils/line-diff.ts with an optional line weight.
push <file> on a file pull wrote sends it back to the document it came
from: spliceMarkdown turns the file into an edit of the blocks it
changes, written with If-Match on the hash pull recorded. Managed,
removed, reserved and unknown files, conflict markers, --path, --lock
and --force are refused before any write. A changed document exits 2
with its last writer. The base copy, then the state, are recorded under
the mirror's lock; a push stopped between its write and its records is
settled as unchanged by the next one.
…ts read; narrow the splice fuzz checker

The every-block-type fixture now goes through the real pull into a file. The fuzz checker counts a block as reached only when an edit changes one of its own lines, removes the blank line before it, or adds lines inside it; every other block must be the object read. A numbered item after an edited one may lose a start prop it cannot show (edit_doc behaviour), and a reached twin may stand in for an identical block.
A file edited here whose document changed in Paca is merged with git merge-file --diff3 against the base copy (or the snapshot of the recorded version). A clean merge writes the file, then the base, then the state; a conflict writes the markers the same way and exits 2. Files an interrupted pull wrote into, with no copy of their version, or without git are kept as before. pull --force now also replaces a file edited here when Paca did not change, and a file edited here is reported as modified locally.
…fenced code included, and flag the conflict in the state
…y the normalising rules differ, and keep --force to files pull would not write

A merge writes Paca's version as .paca-base/<id>.next.md and records
merge_pending in the state before it writes the merged file, then moves
.next.md over the base copy; the next pull finishes or drops a merge it
finds pending, so a stop never merges a file twice or nests markers.
A push refuses a file mid-merge until pull has run.

A locally edited file that equals Paca's version under the normalising
rules (a push that stopped after its write) takes Paca's version.

--force replaces only files whose document changed in Paca or that hold
pull's conflict markers, as before the merge.
… edited here is never taken for pull's write
…tles it, and decide a stopped merge by what is on disk

A file an interrupted pull wrote into, or whose base copy is gone, stays
unmerged when Paca goes back to the recorded version: pull keeps it with
Paca's version next to it on every pull, and push refuses it, naming the
ways out (delete the file and pull, or pull --force). The interim state
of a pull records unmerged for such a file, so a pull stopped part way
cannot drop it.

The merge journal also records the file's sha before the merge and the
sha of Paca's version. A restart finishes the merge when .next.md is gone
and the base copy is Paca's version, starts over when the file still has
its sha from before the merge, finishes when it has the merged sha or
pull's marker lines, and otherwise starts over.
… or four-space indent the way Paca writes them, and leave a thematic break's characters alone
…d merge is settled, when a file can be merged twice, and what a push conflict shows
…irror file without the state knowing, so a revert in Paca is never pushed back
…tart or end of its text, which the read-back cannot see
…checklist items as Paca does, read list indents only where nothing depends on them, and say what to write when a blank line or a wrapped paragraph is refused
…it, refresh it when Paca moves on, and name it only while it holds Paca's version
…spaces Markdown cannot show, so text next to a link is not refused

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Important

One integrity gap in the push side of the pending-write invariant: a push can still send text Paca took back when a pull stopped between writing a mirror file and its base copy. Details below.

Reviewed changes

  • Mirror push — new cli/push-mirror.ts: push <file> inside a pull mirror edits the source document through spliceMarkdown, sends If-Match against the pulled version, refuses orphan/reserved/merge-pending/unmerged/managed/empty/marker/base-unknown files, and rewrites the file to Paca's render after a write.
  • Pull merge — new cli/merge.ts (git merge-file --diff3) plus the pull.ts rework: journal (merge_pending + .paca-base/<id>.next.md), settleMerges from disk, unmerged entries with Paca's version in <file>.remote.md, narrower --force reach, vacating cleanup, and modified locally instead of kept … use --force for an edit whose document did not change.
  • Whole-file edit engine — spliceMarkdown/canonical/countChanged in tools/doc-edit.ts (built on the existing range, alignment and audit code), shared utils/line-diff.ts, and the new line-based utils/md-normalise.ts.
  • Checklist parser fix — converters.ts drops the space the parser adds to a loose-list checklist item's text, affecting write_doc, edit_doc and mirror push alike.
  • Docs, CHANGELOG, CLI help, and one new e2e spec (tests/docs/mcp-mirror.spec.ts) covering browser edit + mirror edit, refused push, merge, then push.

⚠️ A push can re-apply text Paca took back after a pull that stopped mid-write

push-mirror.ts never consults entry.pending_sha256, so it cannot tell a user's edit from a file an interrupted pull wrote. When a pull writes a mirror file and then fails or is killed before writing the base copy (pull.ts:787-796 writes the file first), the state keeps the older content_hash and records the write only in pending_sha256; if Paca is then back at that older version, moved is false, the base copy still matches entry.file_sha256, and spliceMarkdown turns Paca's newer text back into an edit. The push then restores exactly the change Paca took back, which is the case pull.ts's header invariant and the README ("a push can never send a change that was taken back in Paca") say cannot happen.

Technical details
# A push can re-apply what Paca took back

## Affected sites
- `apps/mcp/src/cli/push-mirror.ts:210-217` — the refusal list has no `pending_sha256` case
  (compare `merge_pending`, which protects the merge path the same way).
- `apps/mcp/src/cli/push-mirror.ts:242-248` — the file's hash is compared only with
  `entry.file_sha256`, never with `entry.pending_sha256`.
- `apps/mcp/src/cli/pull.ts:787-796` — the plain write path writes the file, then the base
  copy; a failure between the two leaves the file ahead of both the base copy and
  `file_sha256`, and the rollback in `pullLocked`'s catch only restores moves and created
  files, not overwritten ones.
- `apps/mcp/src/__tests__/cli/pull-stop-revert.test.ts:124-133` — the stop-then-revert
  cases always `pull` before `push`, so this path is not covered.
- `apps/mcp/src/__tests__/cli/push-mirror-interrupt.test.ts:122-148` — covers a push
  killed after its own writes (base, then state), which is the safe order already.

## Reproduction
1. Pull a document (V1), then let Ann edit it to V2.
2. Run `pull`: it writes the mirror file as V2, then fails at the base-copy write
   (ENOSPC, or the process is killed there). State: `content_hash` = V1,
   `pending_sha256` = sha256(V2 render), base copy = V1.
3. Ann reverts the document to V1.
4. Run `push <file>` without `pull`: `moved` is false (live hash V1 = recorded V1), the
   base copy still matches `entry.file_sha256`, so the V1 → V2 difference is sent and Paca
   holds V2 again.

## Required outcome
A push must never treat text that only an interrupted pull wrote as a local edit when
Paca no longer holds that text. Either refuse the file while its hash is in
`entry.pending_sha256` and Paca's current render differs from it (a `pull` then rewrites
the file, losing nothing the user wrote), or write the base copy before the file in pull's
plain path so the existing base-copy check at `push-mirror.ts:344` catches it.

ℹ️ CLI help still describes the old --force reach and exit code

The help text in cli/index.ts was not updated with the behaviour change this PR makes: line 11 still says --force overwrites mirror files that were edited locally, and lines 24-25 still say exit 2 means "a mirror file was edited locally". After this PR an edited file whose document did not change prints modified locally and exits 0, and --force leaves it alone; the README's exit table and pull section were updated, but this help and runPull's doc comment (pull.ts:100-105, "2 a locally edited file was kept") were not. Worth aligning, since exit codes are what scripts wrap.

Technical details
# Stale CLI help and doc comment

## Affected sites
- `apps/mcp/src/cli/index.ts:11` — "--force overwrites mirror files that were edited locally"
- `apps/mcp/src/cli/index.ts:24-25` — exit 2 description
- `apps/mcp/src/cli/pull.ts:100-105` — `runPull` doc comment, "2 a locally edited file was kept"

## Required outcome
The help and the doc comment describe the exit codes and `--force` reach this PR actually
implements (README table at `apps/mcp/README.md:722` is already correct).

Pullfrog  | Fix it ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

…ished, so a revert in Paca is never pushed back without a pull
…every refusal that waits for a pull, and say what pull --force and exit 2 mean now
@msammu07

Copy link
Copy Markdown
Contributor Author

Pushed 48abecf6..d7fcbc6c.

Push after an interrupted pull. Confirmed: with the extended stop-then-revert test, a push straight after a stopped pull sent the taken-back change again, in all 8 combinations of plain or merging pull, kill or thrown error, with or without a local edit. push now refuses (exit 2, run pull first) any mirror file whose entry carries a marker an interrupted pull leaves (pending_sha256, merge_pending, reserved); it never decides by itself whether the text is pull's or the user's. The test now runs every combination in both orders: pull then push, and push directly. The thrown-error path does keep pending_sha256 in the state on disk; a new test reads it back.

Help and exit codes. The --force line and the exit-code text in the CLI help, runPull's doc comment and the README table now say what the code does. Every mirror-push refusal that waits for a pull or for the user to settle the file exits 2 (the base-unknown, reserved and merge_pending refusals were exit 1).

Checks on this tip: tsc, lint (4 old warnings), vitest 1283 of 1283, build, also in a Linux container limited to 2 CPUs.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ℹ️ No critical issues — the interrupt gap from the last review is closed. One stale doc comment inline.

Reviewed changes

Since the prior pullfrog review at 48abecf, two commits close both findings raised there.

  • push refuses a file an interrupted pull left unfinished — push-mirror.ts now returns exit 2 with "run pull first" whenever the state entry carries pending_sha256, checked before the file is read or spliced, so a pull that stopped between writing a mirror file and its base copy can no longer have Paca's taken-back text re-sent as a local edit. A pull then settles the entry (settlePending → rewrite, or unmerged with the file kept and Paca's version beside it), so no user edit is lost.
  • In-flight markers now exit 2 instead of 1 — reserved, merge_pending, pending_sha256 and unmerged all report a conflict that only a pull settles, matching the README exit table rather than the generic error code.
  • Help and doc comments aligned with the behaviour — USAGE drops the old --force claim and describes the new exit 2, and runPull's doc comment was rewritten for the merge/keep/refusal outcomes.
  • Tests extended — pull-stop-revert now runs every stop-then-revert case in both orders (pull first, and push straight away), and a new case pins that a pull failing between the file write and the base copy leaves the written hash in pending_sha256 on disk; the merge and interrupt suites are updated for exit 2.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

Comment thread apps/mcp/src/cli/push-mirror.ts Resolved

@pikann pikann left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! Thank you for the contribution! 🚀

@pikann
pikann merged commit ff43873 into Paca-AI:master Nov 15, 2026
3 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.

[Feature] Docs: efficient pull/push/content-check for coding agents (change feed, ETags, upsert by path, partial reads)

2 participants