From dc8cf1e5d04c196cfbf344723f2594ffeae8ca34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Okan=20Y=C3=BCcel?= Date: Wed, 25 Nov 2026 00:34:11 +0300 Subject: [PATCH] fix(documents): return the new body only after a document update or restore (#21036) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Agents and board users keep plans and specs as issue documents, and they change them through the documents API > - An update (`PUT /api/issues/{id}/documents/{key}`) and a revision restore return the saved document > - The service builds that result from the row it read before the write, so the result also carries the old `latestBody` > - A client that reads `latestBody` from the response gets the previous revision's text, next to the new `latestRevisionId` and `latestRevisionNumber` > - This pull request removes the stale field from the update and restore results, so they have the same shape as a document read > - The benefit is that a write response never shows old text as the latest text ## Linked Issues or Issue Description I searched open and closed issues and pull requests for this bug and found no match. The related work below touches the same service, but not this bug: - Refs #18277 (open: locks the document row during upsert and restore; it does not change the result shape) - Refs #18171 and #12015 (plugin upsert drops `baseRevisionId`; a different bug) **What happened?** `PUT /api/issues/{id}/documents/{key}` on an existing document returned 200. The response had the new `body`, `latestRevisionId` and `latestRevisionNumber`. It also had a `latestBody` field with the text of the previous revision. A `GET` on the same document right after returned only the new body. A restore (`POST /api/issues/{id}/documents/{key}/revisions/{revisionId}/restore`) has the same problem. **Expected behavior** The update and restore responses have the same document shape as `GET /api/issues/{id}/documents/{key}` (the shared `IssueDocument` type). That type has `body` and no `latestBody`. No field in the response holds text from an older revision. **Steps to reproduce** 1. Create an issue document with body `A`. 2. Update it with body `B` and `baseRevisionId` set to the current revision. 3. Read the response: `body` is `B`, but `latestBody` is `A`. 4. Restore revision 1. The response `latestBody` is `B`, the body before the restore. **Paperclip version or commit** Seen on a current npm release. Reproduced on `master` at 51d77648bc with the new service test. **Deployment mode** Not deployment-specific. The bug is in `server/src/services/documents.ts`. **Agent adapter(s) involved** Not adapter-specific (core bug). ## What Changed - `server/src/services/documents.ts`: the update path of `upsertIssueDocument` and the `restoreIssueDocumentRevision` path remove `latestBody` from the row they read before the write. Then they spread the remaining fields into the result. The `body` field stays the new text, as before. - `server/src/__tests__/documents-service.test.ts`: a new test updates a document and restores its first revision. It checks that each result is equal to a fresh `getIssueDocumentByKey` read and has no `latestBody` field. ## Verification - Red-green, as AGENTS.md section 10 asks: - Before the fix, the new test failed on the update result: `latestBody: "# Plan"` (the old body) was in the response. - With only the update fix applied, the test failed on the restore result: `latestBody: "# Second plan"` (the body before the restore). - With both fixes, the test passes. - `npx vitest run` on the 9 test files that write or read issue documents through the service and routes: `documents`, `documents-service`, `issue-document-restore-routes`, `document-annotation-routes`, `document-annotations-service`, `artifact-review-documents-service`, `artifact-review-document-routes`, `native-plan-proposal-sync`, `case-mutation-fence.integration`. Result: 9 files, 68 tests passed. - `pnpm --filter @paperclipai/server typecheck` passed. - I did not run `pnpm verify` locally. The change is in a service that the routes import, so the selection is very broad. The required CI checks supply the broad coverage, as AGENTS.md section 7 allows. ## Risks - Low risk. The response no longer has the undocumented `latestBody` field. That field is not in the shared `IssueDocument` type, and after an update or restore it always held old text. A client that read it got wrong data. The routes read `id`, `key`, `title`, `format`, `latestRevisionId`, `latestRevisionNumber` and `body` from the result. Those fields do not change. - No database or migration change. ## Model Used - Provider: Anthropic. Model: Claude Opus 5.5 (`claude-opus-5-5`), in Claude Code, with tool use (shell, file edit) and extended thinking. The run did not show the context window size to the model, so I do not state a number. - The model found the cause, wrote the test and the fix, and ran the checks above. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes (no update needed: the documented response shape does not change) - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 --- .../src/__tests__/documents-service.test.ts | 25 +++++++++++++++++++ server/src/services/documents.ts | 6 +++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/server/src/__tests__/documents-service.test.ts b/server/src/__tests__/documents-service.test.ts index 37909508ec7..c1d76910dbd 100644 --- a/server/src/__tests__/documents-service.test.ts +++ b/server/src/__tests__/documents-service.test.ts @@ -189,6 +189,31 @@ describeEmbeddedPostgres("documentService system issue documents", () => { }); }); + it("returns the saved document from update and restore exactly as a fresh read returns it", async () => { + const { issueId } = await createIssueWithDocuments(); + const original = (await svc.getIssueDocumentByKey(issueId, "plan"))!; + + const updated = await svc.upsertIssueDocument({ + issueId, + key: "plan", + title: "Plan", + format: "markdown", + body: "# Second plan", + baseRevisionId: original.latestRevisionId, + }); + expect(updated.document).toEqual(await svc.getIssueDocumentByKey(issueId, "plan")); + expect(updated.document).not.toHaveProperty("latestBody"); + + const restored = await svc.restoreIssueDocumentRevision({ + issueId, + key: "plan", + revisionId: original.latestRevisionId!, + }); + expect(restored.document.body).toBe(original.body); + expect(restored.document).toEqual(await svc.getIssueDocumentByKey(issueId, "plan")); + expect(restored.document).not.toHaveProperty("latestBody"); + }); + it("locks and unlocks issue documents", async () => { const { issueId } = await createIssueWithDocuments(); diff --git a/server/src/services/documents.ts b/server/src/services/documents.ts index f4d7029149a..1f99f012ef8 100644 --- a/server/src/services/documents.ts +++ b/server/src/services/documents.ts @@ -464,10 +464,11 @@ export function documentService(db: Db) { .set({ updatedAt: now }) .where(eq(issueDocuments.documentId, existing.id)); + const { latestBody: _previousBody, ...existingMetadata } = existing; return { created: false as const, document: { - ...existing, + ...existingMetadata, title: input.title ?? null, format: input.format, body: input.body, @@ -751,11 +752,12 @@ export function documentService(db: Db) { .set({ updatedAt: now }) .where(eq(issueDocuments.documentId, existing.id)); + const { latestBody: _previousBody, ...existingMetadata } = existing; return { restoredFromRevisionId: revision.id, restoredFromRevisionNumber: revision.revisionNumber, document: { - ...existing, + ...existingMetadata, title: revision.title ?? null, format: revision.format, body: revision.body,