Repository navigation
feat(mcp): edit documents through the local mirror with a three-way merge - #773
Conversation
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.
…t to the render after a push
…lled file on disk
…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.
…as written when normalising a mirror file
…fenced code included, and flag the conflict in the state
…erge, and say how to get out of a kept file
…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.
…ith or without block ids
…ave through the API
…reach and how blocks are counted
… edited here is never taken for pull's write
…has pull's conflict markers
…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
…not the version the state records
…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
There was a problem hiding this comment.
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 throughspliceMarkdown, sendsIf-Matchagainst 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 thepull.tsrework: journal (merge_pending+.paca-base/<id>.next.md),settleMergesfrom disk,unmergedentries with Paca's version in<file>.remote.md, narrower--forcereach,vacatingcleanup, andmodified locallyinstead ofkept … use --forcefor an edit whose document did not change. - Whole-file edit engine —
spliceMarkdown/canonical/countChangedintools/doc-edit.ts(built on the existing range, alignment and audit code), sharedutils/line-diff.ts, and the new line-basedutils/md-normalise.ts. - Checklist parser fix —
converters.tsdrops the space the parser adds to a loose-list checklist item's text, affectingwrite_doc,edit_docand 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).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
|
Pushed Push after an interrupted pull. Confirmed: with the extended stop-then-revert test, a Help and exit codes. The Checks on this tip: |
There was a problem hiding this comment.
ℹ️ 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.
pushrefuses a file an interrupted pull left unfinished —push-mirror.tsnow returns exit 2 with "run pull first" whenever the state entry carriespending_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, orunmergedwith 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_sha256andunmergedall 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 —
USAGEdrops the old--forceclaim and describes the new exit 2, andrunPull's doc comment was rewritten for the merge/keep/refusal outcomes. - Tests extended —
pull-stop-revertnow 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 inpending_sha256on disk; the merge and interrupt suites are updated for exit 2.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏
pikann
left a comment
There was a problem hiding this comment.
LGTM! Thank you for the contribution! 🚀

Summary
Last part of #747 (docs pull / push / content check for coding agents). A file that
pullwrote can now be edited and pushed back, andpullmerges local edits with changes made in Paca. Builds on #769 (pull,push) and #770 (edit_doc). Onlyapps/mcpchanges, plus one browser spec; nothing is added on the server.pushof a mirror filepush <file>on a file inside a pull mirror edits the document it was pulled from (nosource, no lock), instead of writing a repository file.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.If-Matchwith the pulled version. If the document changed in Paca the push stops (exit 2) with who changed it and when:pullmerges, then push again. Whether it changed is asked of Paca on every push.-/+bullets, blank lines between list items, tab or four-space nesting, numbered items all written1., 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>).pulldid 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.pullmerges local editsgit 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, andpushrefuses the file until they are resolved, inside fenced code too.kept …; use --force to overwrite). Every line written locally is still in the file, between the markers..paca-base/<id>.next.mdand 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.<file>.remote.md, andpushrefuses it. Only the user settles it: delete the file and pull, orpull --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.--forcekeeps 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.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* [ ] taskin a loose list). Mirror push makes this reachable in ordinary editing, so it is fixed at the source;write_docandedit_docuse the same parser and now store the text without the space.Decisions, listed so they can be challenged
push --forcefor a mirror file.pull, thenpush, is the path; overwriting another writer from a file is what this feature exists to prevent..paca-sync.jsonis 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")
| aagainst| a |on a line of text, blank lines at the end of an unclosed fence); Paca's own Markdown never has them..paca-baseis wiped, edited files are refused until they are pulled again.master:1. afollowed by2. [ ] bloses its checkbox inwrite_docandedit_doc; mirror push refuses that file.Type of Change
Testing
apps/mcp:tscclean;lintreports 4 warnings, all in files this PR does not touch;vitest1283 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.cli/pull.test.tschanged, for the behaviour change above (markers instead of an untouched file); the other pull and push suites are unchanged.apps/e2e: newtests/docs/mcp-mirror.spec.tsruns 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,pullmerges, the push succeeds, both changes are in the document and the other blocks are stored unchanged. It passes, as doesmcp-edit-doc.spec.ts.tests/docsandtests/realtime, chromium) on a stack built from this branch: 217 passed, 6 skipped, 2 failed. Both failures are indocs.spec.tsand not in code this PR touches (apps/webandservicesare unchanged)::640(edit own comment, usesControl+aon macOS) fails the same way onmaster;:350(rename from the sidebar) reads the API before the save lands and failed 4 of 5 runs.Checklist
apps/mcp/README.md(section "Editing documents as files"),apps/mcp/CHANGELOG.md, CLI help.