Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(web): remap queued notebook dependencies

tianma-if3469ab82e302
diff --git a/apps/web/src/hooks/useWorkspaceQueuedSync.ts b/apps/web/src/hooks/useWorkspaceQueuedSync.ts
index 69806117..3f90605f 100644
--- a/apps/web/src/hooks/useWorkspaceQueuedSync.ts
+++ b/apps/web/src/hooks/useWorkspaceQueuedSync.ts
@@ -83,9 +83,10 @@ export const useWorkspaceQueuedSync = ({
const hasRemote = current.notebooks.some((notebook) => notebook.id === remoteId);
return {
...current,
- notebooks: hasRemote
+ notebooks: (hasRemote
? current.notebooks.filter((notebook) => notebook.id !== temporaryId)
- : current.notebooks.map((notebook) => notebook.id === temporaryId ? { ...notebook, id: remoteId } : notebook),
+ : current.notebooks.map((notebook) => notebook.id === temporaryId ? { ...notebook, id: remoteId } : notebook))
+ .map((notebook) => notebook.parentId === temporaryId ? { ...notebook, parentId: remoteId } : notebook),
};
});
setSelectedNotebookId((current) => current === temporaryId ? remoteId : current);
@@ -146,7 +147,6 @@ export const useWorkspaceQueuedSync = ({
if (!result || !["memo.merge", "notebook.create", "notebook.update", "template.create", "template.update", "resource.create"].includes(item.kind)) return;
const {
deleteLocalMemo,
- deleteLocalNotebook,
deleteLocalTemplate,
getLocalMemo,
putLocalMemo: putMemo,
@@ -168,8 +168,8 @@ export const useWorkspaceQueuedSync = ({
if (source) await putMemo(localDataScope, { ...source, mergedIntoMemoId: (result as MemoDetail).id });
}));
} else if (item.kind === "notebook.create" && "name" in result && temporaryId) {
- await deleteLocalNotebook(localDataScope, temporaryId);
- await putLocalNotebook(localDataScope, result as Notebook);
+ // The sync core committed the mirror, references, mapping and
+ // acknowledgement together before notifying the live UI.
notifyNotebookIdRemapped(temporaryId, (result as Notebook).id);
} else if (item.kind === "notebook.update" && "name" in result) {
await putLocalNotebook(localDataScope, result as Notebook);
diff --git a/apps/web/src/lib/notebook-id-remap.test.mjs b/apps/web/src/lib/notebook-id-remap.test.mjs
new file mode 100644
index 00000000..a24db648
--- /dev/null
+++ b/apps/web/src/lib/notebook-id-remap.test.mjs
@@ -0,0 +1,134 @@
+import { afterEach, expect, test } from "bun:test";
+import "fake-indexeddb/auto";
+const { localDb } = await import("./local-db.ts");
+const { api } = await import("./api.ts");
+const { createLocalNotebook, createLocalMemo, getLocalMemo } = await import("./local-mirror.ts");
+const { queueLocalAction, queueMemoCreate, syncQueuedChanges } = await import("./sync-queue.ts");
+const { acknowledgeNotebookId, notebookRemapTables, repairQueuedNotebookReferences } = await import("./notebook-id-remap.ts");
+const scope = "notebook-remap-test";
+const original = { createNotebook: api.createNotebook, createMemo: api.createMemo };
+const navigatorDescriptor = Object.getOwnPropertyDescriptor(globalThis, "navigator");
+afterEach(async () => {
+ Object.assign(api, original);
+ if (navigatorDescriptor) Object.defineProperty(globalThis, "navigator", navigatorDescriptor);
+ else delete globalThis.navigator;
+ await Promise.all(localDb.tables.map((table) => table.clear()));
+});
+const online = () => Object.defineProperty(globalThis, "navigator", { configurable: true, value: { onLine: true } });
+const enqueueNotebook = async (name, parentId) => {
+ const notebook = await createLocalNotebook(scope, { name, parentId });
+ await queueLocalAction(scope, "notebook.create", notebook.id, { name, parentId, temporaryId: notebook.id });
+ return notebook;
+};
+const enqueueMemo = async (notebookId) => {
+ const memo = await createLocalMemo(scope, { notebookId, title: "Keep my note" });
+ await queueMemoCreate(scope, { temporaryId: memo.id, notebookId, title: memo.title, tags: [], createdAt: memo.createdAt, updatedAt: memo.updatedAt });
+ return memo;
+};
+
+test("slow notebook creation remaps existing and in-flight child writes without a UI callback", async () => {
+ online();
+ const parent = await enqueueNotebook("parent");
+ const child = await enqueueNotebook("child", parent.id);
+ const memo = await enqueueMemo(child.id);
+ let lateMemo;
+ const sent = [];
+ api.createNotebook = async (input) => {
+ sent.push(input);
+ if (input.name === "parent") {
+ // User keeps writing while the parent's network request is in flight.
+ lateMemo = await enqueueMemo(parent.id);
+ return { notebook: { ...parent, id: "nb_parent" } };
+ }
+ expect(input.parentId).toBe("nb_parent");
+ return { notebook: { ...child, id: "nb_child", parentId: "nb_parent" } };
+ };
+ api.createMemo = async (input) => {
+ expect(["nb_parent", "nb_child"]).toContain(input.notebookId);
+ return { memo: { ...memo, ...input, id: `memo_${input.notebookId}`, revision: 1, contentHash: "hash" } };
+ };
+ // Equal timestamps can put children before parents; subsequent runs must settle.
+ for (let index = 0; index < 4; index++) await syncQueuedChanges({ scope });
+ expect(await localDb.syncQueue.count()).toBe(0);
+ expect(sent).toHaveLength(2);
+ expect((await getLocalMemo(scope, memo.id)).notebookId).toBe("nb_child");
+ expect((await getLocalMemo(scope, lateMemo.id)).notebookId).toBe("nb_parent");
+ expect(await localDb.notebooks.get([scope, parent.id])).toBeUndefined();
+ expect((await localDb.notebooks.get([scope, "nb_child"])).parentId).toBe("nb_parent");
+});
+
+test("failed parents and unmapped legacy orphans stay local without invalid API requests", async () => {
+ online();
+ const parent = await enqueueNotebook("parent");
+ const memo = await enqueueMemo(parent.id);
+ await enqueueNotebook("child", parent.id);
+ await enqueueMemo("local_legacy_parent_without_mapping");
+ api.createNotebook = async () => { throw new Error("offline"); };
+ api.createMemo = async () => { throw new Error("must not send a temporary notebook id"); };
+ const result = await syncQueuedChanges({ scope });
+ expect(result).toMatchObject({ attempted: 1, failed: 1 });
+ expect((await localDb.syncQueue.get(`memo.create:${memo.id}`)).attemptCount).toBe(0);
+ expect((await getLocalMemo(scope, memo.id)).title).toBe("Keep my note");
+});
+
+test("persisted mappings repair delayed errors, batch targets and stale-tab writes after reopening", async () => {
+ const parent = await createLocalNotebook(scope, { name: "parent" });
+ await localDb.transaction("rw", notebookRemapTables, () => acknowledgeNotebookId(scope, parent.id, { ...parent, id: "nb_parent" }));
+ localDb.close();
+ await localDb.open();
+ const kinds = ["memo.create", "memo.moveBatch", "memo.merge", "notebook.create", "notebook.update", "notebook.delete"];
+ for (const kind of kinds) {
+ await queueLocalAction(scope, kind, parent.id, {
+ notebookId: parent.id, parentId: parent.id, notebookIds: [parent.id, "nb_other"],
+ });
+ }
+ await queueLocalAction("another-account", "memo.moveBatch", "other", { notebookId: parent.id });
+ await localDb.syncQueue.toCollection().modify({ status: "error", lastError: "Notebook not found", nextAttemptAt: "2099-01-01T00:00:00Z" });
+ await repairQueuedNotebookReferences(scope);
+ const repaired = (await localDb.syncQueue.toArray()).filter((item) => item.scope === scope);
+ expect(repaired).toHaveLength(kinds.length);
+ for (const item of repaired) {
+ expect(item).toMatchObject({ status: "pending", nextAttemptAt: null, lastError: null });
+ expect(item.payload).toMatchObject({ notebookId: "nb_parent", parentId: "nb_parent", notebookIds: ["nb_parent", "nb_other"] });
+ if (item.kind === "notebook.update" || item.kind === "notebook.delete") expect(item.memoId).toBe("nb_parent");
+ }
+ expect((await localDb.syncQueue.toArray()).find((item) => item.scope === "another-account").payload.notebookId).toBe(parent.id);
+});
+
+test("acknowledgement failure rolls back notebook mapping, local relationships and queue rewrites", async () => {
+ online();
+ const parent = await enqueueNotebook("parent");
+ const memo = await enqueueMemo(parent.id);
+ const child = await enqueueNotebook("child", parent.id);
+ api.createNotebook = async () => ({ notebook: { ...parent, id: "nb_parent" } });
+ const failDelete = () => { throw new Error("injected acknowledgement failure"); };
+ localDb.syncQueue.hook("deleting", failDelete);
+ try {
+ expect(await syncQueuedChanges({ scope })).toMatchObject({ synced: 0, failed: 1 });
+ } finally {
+ localDb.syncQueue.hook("deleting").unsubscribe(failDelete);
+ }
+ expect(await localDb.syncMeta.count()).toBe(0);
+ expect(await localDb.notebooks.get([scope, "nb_parent"])).toBeUndefined();
+ expect(await localDb.notebooks.get([scope, parent.id])).toBeDefined();
+ expect((await localDb.notebooks.get([scope, child.id])).parentId).toBe(parent.id);
+ expect((await getLocalMemo(scope, memo.id)).notebookId).toBe(parent.id);
+ expect((await localDb.syncQueue.get(`memo.create:${memo.id}`)).payload.notebookId).toBe(parent.id);
+});
+
+test("remapping preserves drafts, conflicts, in-flight claims and content containing the temporary id", async () => {
+ const parent = await createLocalNotebook(scope, { name: "parent" });
+ const memo = await enqueueMemo(parent.id);
+ const draft = { memoId: memo.id, title: "Latest unsaved edit", contentJson: { type: "doc", content: [] }, tagsText: "", updatedAt: new Date().toISOString() };
+ await localDb.drafts.put(draft);
+ await queueLocalAction(scope, "notebook.update", parent.id, { notebookId: parent.id, name: parent.id });
+ const conflict = (await localDb.syncQueue.toArray()).find((item) => item.kind === "notebook.update");
+ await localDb.syncQueue.update(conflict.id, { status: "conflict", lastError: "Keep this conflict" });
+ await queueLocalAction(scope, "memo.moveBatch", "claimed", { notebookId: parent.id });
+ const claimed = (await localDb.syncQueue.toArray()).find((item) => item.kind === "memo.moveBatch");
+ await localDb.syncQueue.update(claimed.id, { status: "syncing", claimId: "another-tab" });
+ await localDb.transaction("rw", notebookRemapTables, () => acknowledgeNotebookId(scope, parent.id, { ...parent, id: "nb_parent" }));
+ expect(await localDb.drafts.get(memo.id)).toEqual(draft);
+ expect(await localDb.syncQueue.get(conflict.id)).toMatchObject({ status: "conflict", lastError: "Keep this conflict", payload: { notebookId: "nb_parent", name: parent.id } });
+ expect(await localDb.syncQueue.get(claimed.id)).toMatchObject({ status: "syncing", claimId: "another-tab", payload: { notebookId: parent.id } });
+});
diff --git a/apps/web/src/lib/notebook-id-remap.ts b/apps/web/src/lib/notebook-id-remap.ts
new file mode 100644
index 00000000..b59afd5e
--- /dev/null
+++ b/apps/web/src/lib/notebook-id-remap.ts
@@ -0,0 +1,79 @@
+import type { Notebook } from "@edgeever/shared";
+import { localDb, type SyncQueueItem } from "@/lib/local-db";
+
+// Keep notebook mappings separate from the memo mappings observed by editors.
+const mappingPrefix = "notebook-id:";
+export const notebookRemapTables = [localDb.syncQueue, localDb.syncMeta, localDb.notebooks, localDb.memos];
+
+const notebookReferences = (item: SyncQueueItem) => {
+ const payload = item.payload as Record<string, unknown>;
+ return [payload.notebookId, payload.parentId,
+ ...(Array.isArray(payload.notebookIds) ? payload.notebookIds : []),
+ ...(item.kind === "notebook.update" || item.kind === "notebook.delete" ? [item.memoId] : []),
+ ].filter((id): id is string => typeof id === "string");
+};
+
+export const hasTemporaryNotebookDependency = (item: SyncQueueItem) =>
+ notebookReferences(item).some((id) => id.startsWith("local_"));
+
+// Called inside a transaction, including the create acknowledgement when present.
+const rewriteNotebookReferences = async (scope: string, temporaryId: string, remoteId: string) => {
+ for (const item of await localDb.syncQueue.toArray()) {
+ if (item.scope !== scope || item.status === "syncing" || !notebookReferences(item).includes(temporaryId)) continue;
+ const payload = { ...item.payload } as Record<string, unknown>;
+ for (const field of ["notebookId", "parentId"]) {
+ if (payload[field] === temporaryId) payload[field] = remoteId;
+ }
+ if (Array.isArray(payload.notebookIds)) {
+ payload.notebookIds = payload.notebookIds.map((id) => id === temporaryId ? remoteId : id);
+ }
+ await localDb.syncQueue.put({
+ ...item,
+ memoId: item.kind.startsWith("notebook.") && item.memoId === temporaryId ? remoteId : item.memoId,
+ payload,
+ // Dependency errors can retry immediately; preserve genuine conflicts.
+ ...(item.status === "error" ? {
+ status: "pending" as const, nextAttemptAt: null, lastError: null,
+ lastErrorCode: null, lastErrorDetails: null, claimId: null,
+ } : {}),
+ });
+ }
+ await localDb.memos.where("scope").equals(scope).filter((memo) => memo.notebookId === temporaryId)
+ .modify((memo) => { memo.notebookId = remoteId; });
+ await localDb.notebooks.where("scope").equals(scope).filter((notebook) => notebook.parentId === temporaryId)
+ .modify({ parentId: remoteId });
+};
+
+export const acknowledgeNotebookId = async (scope: string, temporaryId: string, notebook: Notebook) => {
+ if (temporaryId === notebook.id) return;
+ await localDb.syncMeta.put({
+ scope, key: `${mappingPrefix}${temporaryId}`, value: notebook.id, updatedAt: new Date().toISOString(),
+ });
+ await rewriteNotebookReferences(scope, temporaryId, notebook.id);
+ await localDb.notebooks.delete([scope, temporaryId]);
+ await localDb.notebooks.put({ ...notebook, scope });
+};
+
+// Replay persisted mappings after reload, including writes from a stale UI/tab.
+export const repairQueuedNotebookReferences = async (scope?: string, itemId?: string) => {
+ await localDb.transaction("rw", notebookRemapTables, async () => {
+ const item = itemId ? await localDb.syncQueue.get(itemId) : undefined;
+ if (itemId && (!item || !hasTemporaryNotebookDependency(item))) return;
+ const queued = item ? [item] : await localDb.syncQueue.toArray();
+ const references = new Set(queued
+ .filter((entry) => !scope || entry.scope === scope)
+ .flatMap((entry) => notebookReferences(entry)
+ .filter((id) => id.startsWith("local_"))
+ .map((id) => JSON.stringify([entry.scope, id]))));
+ if (references.size === 0) return;
+ const mappings = scope
+ ? await localDb.syncMeta.where("scope").equals(scope).toArray()
+ : await localDb.syncMeta.toArray();
+ for (const mapping of mappings) {
+ if (!mapping.key.startsWith(mappingPrefix)) continue;
+ const temporaryId = mapping.key.slice(mappingPrefix.length);
+ if (!references.has(JSON.stringify([mapping.scope, temporaryId]))) continue;
+ await rewriteNotebookReferences(mapping.scope, temporaryId, mapping.value);
+ }
+ });
+};
diff --git a/apps/web/src/lib/sync-queue.test.mjs b/apps/web/src/lib/sync-queue.test.mjs
index cc47b199..18538b1e 100644
--- a/apps/web/src/lib/sync-queue.test.mjs
+++ b/apps/web/src/lib/sync-queue.test.mjs
@@ -400,7 +400,8 @@ describe("web sync conflict recovery", () => {
test("replays offline appearance edits and explicit resets without dropping fields", async () => {
const { createLocalNotebook, updateLocalNotebook, getLocalNotebook } = await import("./local-mirror.ts");
const scope = createLocalDataScope("https://demo.edgeever.org", "appearance-user");
- const notebook = await createLocalNotebook(scope, { name: "Heimnetz" });
+ const notebook = { ...await createLocalNotebook(scope, { name: "Heimnetz" }), id: "nb_appearance" };
+ await localDb.notebooks.put({ ...notebook, scope });
const original = api.updateNotebook;
const previousNavigator = globalThis.navigator;
globalThis.navigator = { onLine: true };
diff --git a/apps/web/src/lib/sync-queue.ts b/apps/web/src/lib/sync-queue.ts
index ed6724e4..49977d7f 100644
--- a/apps/web/src/lib/sync-queue.ts
+++ b/apps/web/src/lib/sync-queue.ts
@@ -33,6 +33,7 @@ import { isBrowserOffline } from "@/lib/network-status";
import { parseTagsText } from "@/lib/utils";
import { createClientUuid } from "@/lib/client-id";
import { notebookDeleteIdsFromPayload } from "@/lib/notebook-delete";
+import { acknowledgeNotebookId, hasTemporaryNotebookDependency, notebookRemapTables, repairQueuedNotebookReferences } from "@/lib/notebook-id-remap";
export type { SyncQueueSummary, SyncRunResult } from "@edgeever/shared";
export type SyncQueueResult = MemoDetail | Notebook | MemoTemplate | Resource | null;
@@ -390,6 +391,7 @@ const runQueuedChanges = async (options: {
}
const now = new Date();
+ await repairQueuedNotebookReferences(options.scope);
const candidates = (await localDb.syncQueue.where("status").anyOf("pending", "error").toArray())
.filter((item) => !item.nextAttemptAt || new Date(item.nextAttemptAt) <= now)
.sort((left, right) => left.createdAt.localeCompare(right.createdAt));
@@ -413,6 +415,9 @@ const runQueuedChanges = async (options: {
}
for (const candidate of items) {
+ // Creates earlier in this batch may have resolved this dependency. Also
+ // repair actions written by another tab since the initial queue snapshot.
+ await repairQueuedNotebookReferences(candidate.scope, candidate.id);
const item = await claimQueueItem(candidate.id);
if (!item) {
continue;
@@ -487,6 +492,8 @@ const claimQueueItem = (id: string) =>
if (!item || (item.status !== "pending" && item.status !== "error")) {
return null;
}
+ if (hasTemporaryNotebookDependency(item)) return null;
+ if (["memo.update", "memo.delete", "memo.restore"].includes(item.kind) && item.memoId.startsWith("local_")) return null;
const claimId = createClientUuid();
const claimedItem: SyncQueueItem = {
@@ -515,13 +522,18 @@ const deletedMemoIdsFromAction = (item: SyncQueueItem): string[] => {
};
const acknowledgeClaimedQueueItem = (item: SyncQueueItem, result: SyncQueueResult): Promise<MemoUpdateAcknowledgement> =>
- localDb.transaction("rw", [localDb.syncQueue, localDb.drafts, localDb.memos], async () => {
+ localDb.transaction("rw", item.kind === "notebook.create"
+ ? [...notebookRemapTables, localDb.drafts]
+ : [localDb.syncQueue, localDb.drafts, localDb.memos], async () => {
const current = await localDb.syncQueue.get(item.id);
const memo = result && item.kind === "memo.update" && "contentHash" in result
? result as MemoDetail
: null;
if (current?.claimId === item.claimId && current.status === "syncing") {
+ if (item.kind === "notebook.create" && result && "name" in result && item.scope) {
+ await acknowledgeNotebookId(item.scope, item.memoId, result as Notebook);
+ }
await localDb.syncQueue.delete(item.id);
// A successful delete supersedes older conflicted edits to those notes.
// Keep the draft until the server has confirmed the delete, so a failed