From 432ec4ae6812751f98403129c7e2e2f44b3f6e10 Mon Sep 17 00:00:00 2001 From: boojack Date: Sun, 15 Nov 2026 12:10:16 +0800 Subject: [PATCH] fix: upgrade Go and defer Space update refreshes (#6565) Use Go 1.27.4 in CI and Docker for the standard library security fixes. Wait for a memo's queued updates to settle before a Space change refreshes all memo queries, so older reads cannot replace newer optimistic edits. Cover successful and failed queued updates and unrelated pending memos. --- AGENTS.md | 6 +-- go.mod | 2 +- scripts/Dockerfile | 2 +- web/src/hooks/useMemoQueries.ts | 14 +++++- web/tests/memo-record.test.tsx | 75 +++++++++++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 7 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 331ac1ac1..9fcaae8ac 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -7,7 +7,7 @@ repo. If a fact here conflicts with source files or CI config, trust the source Memos is a self-hosted note-taking app. -- Backend: Go 1.27.2, net/http, Connect RPC, gRPC-Gateway, Protocol Buffers. +- Backend: Go 1.27.4, net/http, Connect RPC, gRPC-Gateway, Protocol Buffers. - Frontend: React 19, TypeScript 7, Vite 8, Tailwind CSS v4, `@usememos/ui` on Base UI, React Query v5. - Storage: SQLite, MySQL, PostgreSQL. - Generated API outputs: `proto/gen/` for Go/OpenAPI, `web/src/types/proto/` for TypeScript. @@ -43,7 +43,7 @@ go test -v ./store/... # Store tests, including DB drivers via TestC go test -v -race ./server/... # Server tests with race detector go test -v -race ./internal/... # Internal package tests with race detector go test -v -run TestFoo ./core/... # Run matching Go tests in one tree -go mod tidy -go=1.27.2 # Match CI tidy check +go mod tidy -go=1.27.4 # Match CI tidy check go fix ./... # Apply Go modernization fixes go fix -diff ./... # Match CI modernization check golangci-lint run # Go lint, config: .golangci.yaml (includes depguard layering rules) @@ -175,7 +175,7 @@ Black-box service tests live in `server/api/test/`, unit tests next to the code. - Workflows install toolchains through `.github/actions/setup-go` (Go version from `go.mod`) and `.github/actions/setup-web` (pnpm from `web/package.json`, Node 24, `pnpm install`); change versions there, not per workflow. -- Backend CI: Go 1.27.2, `go mod tidy`, `go fix -diff ./...`, golangci-lint v2.14.0 (including goimports), test groups +- Backend CI: Go 1.27.4, `go mod tidy`, `go fix -diff ./...`, golangci-lint v2.14.0 (including goimports), test groups `store`, `server`, `internal`, `other` (`cmd`, `core`, `markdown`, `filter`, `provider`, `proto`). - Frontend CI: Node 24, pnpm 11.0.1, `pnpm lint`, `pnpm test`, `pnpm build`. - Proto CI: `buf lint` and `buf format` check. diff --git a/go.mod b/go.mod index 00e3b306d..ff2208b9a 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/usememos/memos -go 1.27.2 +go 1.27.4 require ( connectrpc.com/connect v1.20.0 diff --git a/scripts/Dockerfile b/scripts/Dockerfile index 5d61582fa..3a348efd2 100644 --- a/scripts/Dockerfile +++ b/scripts/Dockerfile @@ -1,4 +1,4 @@ -FROM --platform=$BUILDPLATFORM golang:1.27.2-alpine AS backend +FROM --platform=$BUILDPLATFORM golang:1.27.4-alpine AS backend WORKDIR /backend-build # Install build dependencies diff --git a/web/src/hooks/useMemoQueries.ts b/web/src/hooks/useMemoQueries.ts index 9df785a8e..fead94e83 100644 --- a/web/src/hooks/useMemoQueries.ts +++ b/web/src/hooks/useMemoQueries.ts @@ -444,15 +444,25 @@ export function useUpdateMemo(name: string | undefined) { invalidateAround(previousMemo); }, onSuccess: (updatedMemo, { updateMask }) => { + const queued = queuedUpdates(); if (updateMask.includes("space")) { - queryClient.invalidateQueries({ queryKey: memoKeys.all }); + if (queued.length === 0) { + queryClient.invalidateQueries({ queryKey: memoKeys.all }); + } else { + // Awaiting the queue here would keep this mutation pending and prevent the next update from starting. + const cache = queryClient.getMutationCache(); + const unsubscribe = cache.subscribe(() => { + if (cache.findAll({ mutationKey, exact: true, status: "pending" }).length > 0) return; + unsubscribe(); + queryClient.invalidateQueries({ queryKey: memoKeys.all }); + }); + } } // Invalidate user stats queryClient.invalidateQueries({ queryKey: userKeys.stats() }); // Placement changes move linked attachments between scoped libraries. queryClient.invalidateQueries({ queryKey: attachmentKeys.lists() }); // Update cache with server response, under the patches of the updates queued behind this one - const queued = queuedUpdates(); const memo = rebaseQueued(updatedMemo, queued); queryClient.setQueryData(memoKeys.detail(updatedMemo.name), memo); patchMemoInCollectionQueries(queryClient, memo); diff --git a/web/tests/memo-record.test.tsx b/web/tests/memo-record.test.tsx index 4aa46359b..0bcf4ae7c 100644 --- a/web/tests/memo-record.test.tsx +++ b/web/tests/memo-record.test.tsx @@ -316,6 +316,81 @@ describe("useUpdateMemo", () => { return { client, server, result: rendered.result }; }; + it.each(["succeeds", "fails"])("defers a Space refresh until the last queued update %s", async (outcome) => { + const before = create(MemoSchema, { name: "memos/x", content: "before", space: "spaces/a" }); + const moved = create(MemoSchema, { ...before, space: "spaces/b" }); + const edited = create(MemoSchema, { ...moved, content: "edited" }); + const { client, server, result } = await onScreen(before); + const commentsKey = memoKeys.comments(before.name); + client.setQueryData(commentsKey, create(ListMemoCommentsResponseSchema, { comments: [] })); + const invalidate = vi.spyOn(client, "invalidateQueries"); + const replies: Array<{ resolve: (memo: Memo) => void; reject: (error: Error) => void }> = []; + api.updateMemo.mockImplementation(() => new Promise((resolve, reject) => replies.push({ resolve, reject }))); + + act(() => { + result.current.update.mutate({ update: { name: before.name, space: moved.space }, updateMask: ["space"] }); + result.current.update.mutate({ update: { name: before.name, content: edited.content }, updateMask: ["content"] }); + result.current.update.mutate({ update: { name: before.name, pinned: true }, updateMask: ["pinned"] }); + }); + await waitFor(() => expect(api.updateMemo).toHaveBeenCalledTimes(1)); + await waitFor(() => expect(result.current.detail.data).toMatchObject({ content: edited.content, pinned: true })); + + await act(async () => { + server.memo = moved; + replies[0].resolve(moved); + }); + await waitFor(() => expect(api.updateMemo).toHaveBeenCalledTimes(2)); + await waitFor(() => expect(client.isFetching()).toBe(0)); + expect(result.current.detail.data).toMatchObject({ content: edited.content, pinned: true }); + expect(invalidate.mock.calls.map(([filters]) => filters?.queryKey)).not.toContainEqual(memoKeys.all); + + await act(async () => { + server.memo = edited; + replies[1].resolve(edited); + }); + await waitFor(() => expect(api.updateMemo).toHaveBeenCalledTimes(3)); + expect(invalidate.mock.calls.map(([filters]) => filters?.queryKey)).not.toContainEqual(memoKeys.all); + expect(client.getQueryState(commentsKey)?.isInvalidated).toBe(false); + + await act(async () => { + if (outcome === "succeeds") { + server.memo = create(MemoSchema, { ...edited, pinned: true }); + replies[2].resolve(server.memo); + } else { + replies[2].reject(new Error("pin failed")); + } + }); + await waitFor(() => expect(client.isMutating()).toBe(0)); + await waitFor(() => expect(client.isFetching()).toBe(0)); + expect(result.current.detail.data).toMatchObject({ space: moved.space, content: edited.content, pinned: outcome === "succeeds" }); + expect(result.current.list.data?.memos[0]).toMatchObject({ content: edited.content, pinned: outcome === "succeeds" }); + expect(invalidate.mock.calls.map(([filters]) => filters?.queryKey)).toContainEqual(memoKeys.all); + expect(client.getQueryState(commentsKey)?.isInvalidated).toBe(true); + }); + + it("refreshes a Space change without waiting for an unrelated memo's update", async () => { + const before = create(MemoSchema, { name: "memos/x", space: "spaces/a" }); + const { client, server, result } = await onScreen(before); + // Both hooks must use the same client, as they do on a page with multiple memos. + const { result: other } = renderHook(() => useUpdateMemo("memos/other"), { + wrapper: ({ children }: { children: ReactNode }) => {children}, + }); + const invalidate = vi.spyOn(client, "invalidateQueries"); + api.updateMemo.mockImplementation(({ memo: update }: { memo: Memo }) => { + if (update.name !== before.name) return new Promise(() => {}); + server.memo = create(MemoSchema, { ...before, space: "spaces/b" }); + return Promise.resolve(server.memo); + }); + act(() => { + other.current.mutate({ update: { name: "memos/other", pinned: true }, updateMask: ["pinned"] }); + result.current.update.mutate({ update: { name: before.name, space: "spaces/b" }, updateMask: ["space"] }); + }); + await waitFor(() => expect(api.updateMemo).toHaveBeenCalledTimes(2)); + await waitFor(() => expect(result.current.update.isPending).toBe(false)); + expect(client.isMutating()).toBe(1); + expect(invalidate.mock.calls.map(([filters]) => filters?.queryKey)).toContainEqual(memoKeys.all); + }); + it("ends on the server's state when the queued update fails after the older one saved", async () => { const before = create(MemoSchema, { name: "memos/x", content: "- [ ] a", snippet: "[ ] a", property: { hasIncompleteTasks: true } }); // The server derives the snippet and the property from the content, so only its replies carry them.