Skip to content

feat(api): read and write documents by path, with managed-copy metadata and change notes - #754

Merged
pikann merged 11 commits into
Paca-AI:masterfrom
msammu07:feat/doc-by-path
Nov 14, 2026
Merged

pikann merged 11 commits into
Paca-AI:masterfrom
msammu07:feat/doc-by-path

Conversation

@msammu07

@msammu07 msammu07 commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Third part of #747 (docs pull / push / content check for coding agents), on top of #751. A coding agent can write and read a document by its path in one request, mark it as a managed copy of a repository file, and say why it wrote.

  • By path. PUT /projects/{projectId}/docs/by-path with {path, content, source?, lock?, note?, client?, task?} creates the document at Folder/Sub folder/Title (and any missing folder) or updates it: 201 when created, 200 otherwise, with the document plus changed. GET /projects/{projectId}/docs/by-path?path=… reads it. ETag, If-None-Match and If-Match work as on /docs/{docId} (feat(api): add a document content hash with ETag, If-Match and no-op writes #751).
  • Managed copies. source: {repository, path, commit?} records the repository file a document is a copy of. Documents created in Paca have no source.
  • Lock. lock: true (needs a source) marks a managed copy people should not edit in Paca: a content or title write that does not name the same source.repository gets 409 DOC_LOCKED. Moves stay allowed. PATCH /docs/{docId} accepts source_locked so a person with docs:write can unlock (or lock again).
  • Change note. Document writes (POST, PATCH, by-path PUT) accept note (at most 500 characters), client (an opaque label, at most 200) and task (at most 50). They are stored in the document's activity entry and returned in the details of a 412, so a writer that lost a conflict can see what the other write was for. client is never used for authorization.
  • MCP. The doc tools resolve a path in one request and fall back to the folder walk against an older server. write_doc takes optional note and task; client comes from PACA_CLIENT. list_doc_activities prints the note, client and task.
  • Web. The doc activity pane shows a change's note, client and task under its entry.

How a by-path write works

  • The path is resolved case-insensitively from the root (exact case first, then manual order); a document wins over a folder of the same name.
  • The handler authorizes where the write lands once the path is resolved: docs:write to change the existing document, or to create one in the deepest folder of the path that exists. The route gate can only check the project.
  • The write runs in one transaction under the project's folder advisory lock, resolves the path again and compares it with what was authorized. If it changed, the service resolves and authorizes again, at most 3 times, then answers 409 DOC_PATH_CHANGED. Two pushes of one path never create two documents or two folders.
  • GET by path answers the same 404 for a document the caller may not read as for a missing one.

Decisions, listed so they can be challenged

  1. The lock is a guard rail, not a security boundary. Any writer with docs:write can name the source repository. It stops accidental edits in the wrong place; permissions stay with roles.
  2. A source-only or lock-only change keeps updated_at, updated_by and the content hash and takes no snapshot: the document a reader sees is the same. The ETag still changes, because it covers the whole response. A lock change records a doc.updated activity; a source-only change records nothing.
  3. changed is true when the document was created or its content, title, folder or position changed. An identical push that only records a new commit is changed: false.
  4. If-Match on a path with no document is 412 with empty details (the document moved or was deleted).
  5. New error codes: DOC_LOCKED, DOC_LOCK_NEEDS_SOURCE, DOC_PATH_INVALID, DOC_PATH_IS_FOLDER, DOC_PATH_CHANGED, DOC_SOURCE_INVALID, DOC_NOTE_INVALID.
  6. The note on a 412 is read from the activity log, which is written asynchronously, so a conflict right after the other write may not carry its note yet.
  7. Folders created by a by-path write are announced over realtime but get no activity entry.
  8. A policy on an exact folder (doc.folder_id == X) is evaluated against the deepest existing folder when the write also creates folders below it.

Not in this PR

The web editor does not yet mark a locked managed copy as read-only: a save on one gets 409 DOC_LOCKED. Locked documents only exist once an agent pushes with lock, and the next PR (web UI) handles it.

Scope

This PR touches services/api, apps/mcp and apps/web (one optional prop on the shared ActivityPane and the doc activity pane). The commits are split by concern; if you prefer, I can raise the MCP and web commits as separate PRs after the API part.

Migration

000089_add_documents_source.sql (up only, idempotent): documents.source JSONB and documents.source_locked BOOLEAN NOT NULL DEFAULT false.

Testing

  • golangci-lint run: 0 issues.
  • Unit tests with -race and the full API e2e suite, run in a Linux container (golang:1.26) with CI's commands: pass. New API e2e tests: TestUpsertByPath_* (create with folders, update, no-op, If-Match, lock, concurrent pushes of one path, permissions).
  • apps/mcp: lint and unit tests pass. apps/web: biome check, tsc and unit tests pass.
  • Browser e2e (apps/e2e, Chromium, tests/docs and tests/realtime, on macOS against a stack built from this branch before the rebase onto current master): 207 passed, 6 skipped, 1 failed. The failure is docs.spec.ts "User can edit their own comment", which presses Control+a (not select-all on macOS), as reported in feat(api): add a document content hash with ETag, If-Match and no-op writes #751.

Relation to the change feed PR

The change feed (#753) has merged, and master is merged into this branch. The by-path write now takes the project's change counter before the folder advisory lock, like every other document and folder write, and TestDocSeq_ConcurrentWritesAndFolderDeleteDoNotDeadlock pushes by path against a folder delete (it fails with a deadlock without that lock). A document created or updated by path appears in the change feed like any other write.

Type of Change

  • Other (API feature, with MCP and web follow-through)

Checklist

  • The change is focused and scoped.
  • Related documentation is updated (handler, migration and MCP tool descriptions).
  • New structure or direction is explained clearly.
  • I avoided unnecessary detail or premature abstraction.

… conflict

A PUT by path that found the path changed three times answered 500:
ErrDocPathChanged had no mapping. It is now 409 DOC_PATH_CHANGED.

A document deleted by id between the write's resolve and its row lock
made the write answer 404 for a path the caller may create. The
repository now reports the path changed when the row is gone before
decide runs, so the service resolves again and creates the document.

@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

The MCP by-path client cannot recognise a server that predates these routes, so its folder-walk fallback never runs and read_doc, delete_doc and move_doc fail against an older API.

Reviewed changes

Read the full 43-file diff (all 8 commits, 0a3a5bd..f2e54e8) plus the routes and IAM guards it builds on.

  • By-path endpoints — PUT/GET /projects/{projectId}/docs/by-path resolve Folder/Sub/Title case-insensitively (exact case first, a document wins over a folder), create missing folders, and reuse the existing ETag/If-Match machinery.
  • Serialized writes — WriteDocumentAtPath runs under the same per-project advisory lock as UpdateFolderAtomic, re-resolves, and compares against the authorized resolution, with the service retrying three times before a retryable 409.
  • Managed copies — source/source_locked columns (migration 000089), with the lock enforced inside the update decision and source-only/lock-only writes keeping updated_at, updated_by, the hash and the snapshot history untouched.
  • Change notes — note/client/task on document writes, stored in the activity entry, printed by list_doc_activities, shown in the web doc activity pane, and echoed in the details of a 412.
  • Authorization — the route gate checks only the project; the handlers then authorize the resolved document (docs:read, 404 on deny) or the create target folder, matching the DocCreate/DocChange expansion.
  • MCP tools — write_doc/read_doc/delete_doc/move_doc go through the new client methods with a folder-walk fallback for older servers.

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

Comment thread apps/mcp/src/api/doc-client.ts Resolved
A server from before the by-path routes reads GET /docs/by-path as a
document whose id is "by-path" and answers 400 "invalid document id".
The client looked for "invalid doc id", so it never fell back to the
folder walk; the test mocked the same wrong text.
If-Match on a path with no document has no stored version to name. The
answer carried an empty content_hash and a zero updated_at; it now
carries no error_details.

@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 new issues found.

Reviewed changes

Re-reviewed the two commits since the previous pullfrog review (f2e54e8), both fixes for that review's findings, and verified each against the API it depends on.

  • Older-server detection — isMissingByPathRoute now matches the invalid document id message that parseDocID actually returns from the pre-PR GET /docs/{docId} route, so getByPath/putByPath report unsupported and the folder-walk fallback runs on an older API; the client test mocks the real message.
  • 412 with no stored version — the presenter only builds error_details when the VersionConflictError names a stored version, so an If-Match on a path with no document answers 412 with an empty details body; TestUpsertByPath_IfMatchStale412 now asserts that via errorDetails(raw).

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

@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! Thanks a lot for your contribution.
Could you please help resolve the merge conflicts so we can get this merged?

The change feed (Paca-AI#753) makes every document and folder write take the
project's change counter before any other lock. The by-path write now
takes it before the folder advisory lock, and document and folder
creates take it around the shared insert helpers. The deadlock test
pushes by path against a folder delete.

@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 new issues found.

Reviewed changes

Re-reviewed the one commit since the prior pullfrog review (dfa64cc): the merge of master (1a98043, the document change-feed PR) into feat/doc-by-path, and checked the conflict resolution against the change-feed machinery it now shares.

  • Lock order — WriteDocumentAtPath now takes the project change counter (lockDocSeq) before the folder advisory lock, matching UpdateFolderAtomic, DeleteFolder, CreateDocument, CreateFolder, UpdateDocumentAtomic and DeleteDocument, so the counter is always the first lock and no writer can invert it.
  • Shared write helpers — the merge keeps master's insertFolder/insertDocument/updateDocumentLocked/lockDocSeqOf extraction and threads source/source_locked through documentToRecord, the 14-column insert and the update's source=$9, source_locked=$10, so by-path and by-id writes no longer diverge on managed-copy metadata; documentCols/documentTreeCols carry the columns into the feed and batch-get queries.
  • Change-feed wiring — WithDocChanges added in bootstrap/app.go and the e2e env, with /changes and /batch-get registered before /{docId} and after /by-path.
  • Deadlock coverage — by-path pushes added to TestDocSeq_ConcurrentWritesAndFolderDeleteDoNotDeadlock, exercising the counter-then-folder-lock order against folder rename/delete (27 sends per round against a 4*perFolder+2 buffer).

I verified the resolution locally: go build ./... is clean and the doc domain, service, DTO and presenter unit tests pass. Both prior pullfrog threads are resolved, and the merge settles the conflict that blocked the earlier human approval.

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

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