Skip to content

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

fix(desktop): ship the inbox repair with desktop builds

tianma-iff1219c390865
diff --git a/crates/desktop-sidecar/src/main.rs b/crates/desktop-sidecar/src/main.rs
index 822ffd2b..cdd3d861 100644
--- a/crates/desktop-sidecar/src/main.rs
+++ b/crates/desktop-sidecar/src/main.rs
@@ -766,4 +766,142 @@ mod tests {
1
);
}
+
+ #[test]
+ fn sync_apply_does_not_revive_a_second_inbox() {
+ let database = Connection::open_in_memory().unwrap();
+ database
+ .execute_batch(
+ "PRAGMA foreign_keys = ON;
+ CREATE TABLE notebooks (
+ id TEXT PRIMARY KEY,
+ parent_id TEXT REFERENCES notebooks(id),
+ name TEXT NOT NULL,
+ slug TEXT,
+ icon TEXT,
+ color TEXT,
+ sort_order INTEGER NOT NULL DEFAULT 0,
+ is_deleted INTEGER NOT NULL DEFAULT 0,
+ created_at TEXT NOT NULL DEFAULT 'now',
+ updated_at TEXT NOT NULL DEFAULT 'now',
+ deleted_at TEXT
+ );
+ CREATE UNIQUE INDEX idx_notebooks_one_active_inbox
+ ON notebooks(slug)
+ WHERE is_deleted = 0 AND slug = 'inbox';
+ CREATE TABLE memos (
+ id TEXT PRIMARY KEY,
+ notebook_id TEXT NOT NULL REFERENCES notebooks(id),
+ title TEXT,
+ excerpt TEXT NOT NULL,
+ tags_json TEXT NOT NULL,
+ is_pinned INTEGER NOT NULL,
+ is_archived INTEGER NOT NULL,
+ is_deleted INTEGER NOT NULL,
+ source_memo_ids TEXT NOT NULL,
+ merge_source_count INTEGER NOT NULL,
+ merged_into_memo_id TEXT REFERENCES memos(id),
+ created_at TEXT NOT NULL,
+ updated_at TEXT NOT NULL,
+ deleted_at TEXT
+ );
+ CREATE TABLE memo_contents (
+ memo_id TEXT PRIMARY KEY REFERENCES memos(id) ON DELETE CASCADE,
+ content_json TEXT NOT NULL,
+ content_markdown TEXT NOT NULL,
+ content_text TEXT NOT NULL,
+ content_hash TEXT NOT NULL,
+ revision INTEGER NOT NULL,
+ updated_at TEXT NOT NULL DEFAULT 'now'
+ );
+ INSERT INTO notebooks (id, name, slug, is_deleted)
+ VALUES ('nb_historical_inbox', '等待分类', 'inbox', 0);
+ INSERT INTO memos (
+ id, notebook_id, title, excerpt, tags_json, is_pinned, is_archived, is_deleted,
+ source_memo_ids, merge_source_count, created_at, updated_at
+ ) VALUES (
+ 'kept-memo', 'nb_historical_inbox', 'kept', '', '[]', 0, 0, 0,
+ '[]', 0, '2026-06-30T00:00:00.000Z', '2026-06-30T00:00:00.000Z'
+ );",
+ )
+ .unwrap();
+
+ apply_sync_changes(
+ &database,
+ &json!({ "changes": [{
+ "entityType": "notebook",
+ "operation": "upsert",
+ "entityId": "ws_default_inbox",
+ "notebook": {
+ "name": "等待分类",
+ "slug": "inbox",
+ "parentId": null,
+ "createdAt": "2026-09-10T00:00:00.000Z",
+ "updatedAt": "2026-09-10T00:00:00.000Z"
+ }
+ }] }),
+ )
+ .unwrap();
+
+ assert_eq!(
+ database
+ .query_row(
+ "SELECT is_deleted FROM notebooks WHERE id = 'nb_historical_inbox'",
+ [],
+ |row| row.get::<_, i64>(0),
+ )
+ .unwrap(),
+ 0
+ );
+ assert_eq!(
+ database
+ .query_row(
+ "SELECT is_deleted FROM notebooks WHERE id = 'ws_default_inbox'",
+ [],
+ |row| row.get::<_, i64>(0),
+ )
+ .unwrap(),
+ 1
+ );
+ assert_eq!(
+ database
+ .query_row(
+ "SELECT notebook_id FROM memos WHERE id = 'kept-memo'",
+ [],
+ |row| row.get::<_, String>(0),
+ )
+ .unwrap(),
+ "nb_historical_inbox"
+ );
+ assert_eq!(
+ database
+ .query_row(
+ "SELECT COUNT(*) FROM notebooks WHERE is_deleted = 0 AND slug = 'inbox'",
+ [],
+ |row| row.get::<_, i64>(0),
+ )
+ .unwrap(),
+ 1
+ );
+
+ apply_sync_changes(
+ &database,
+ &json!({ "changes": [{
+ "entityType": "notebook",
+ "operation": "delete",
+ "entityId": "ws_default_inbox"
+ }] }),
+ )
+ .unwrap();
+ assert_eq!(
+ database
+ .query_row(
+ "SELECT is_deleted FROM notebooks WHERE id = 'ws_default_inbox'",
+ [],
+ |row| row.get::<_, i64>(0),
+ )
+ .unwrap(),
+ 1
+ );
+ }
}
diff --git a/crates/desktop-sidecar/src/sync.rs b/crates/desktop-sidecar/src/sync.rs
index 83bdefec..d65d9699 100644
--- a/crates/desktop-sidecar/src/sync.rs
+++ b/crates/desktop-sidecar/src/sync.rs
@@ -741,6 +741,29 @@ fn resolve_sync_notebook_id(
ensure_active_inbox(tx)
}
+fn active_inbox_exists_except(tx: &Transaction<'_>, notebook_id: &str) -> Result<bool, String> {
+ tx.query_row(
+ "SELECT EXISTS(
+ SELECT 1 FROM notebooks
+ WHERE is_deleted = 0
+ AND id <> ?1
+ AND (slug = 'inbox' OR id = 'nb_inbox' OR id GLOB '*_inbox')
+ )",
+ [notebook_id],
+ |row| row.get(0),
+ )
+ .map_err(|e| e.to_string())
+}
+
+fn notebook_is_active(tx: &Transaction<'_>, notebook_id: &str) -> Result<bool, String> {
+ tx.query_row(
+ "SELECT EXISTS(SELECT 1 FROM notebooks WHERE id = ?1 AND is_deleted = 0)",
+ [notebook_id],
+ |row| row.get(0),
+ )
+ .map_err(|e| e.to_string())
+}
+
fn apply_one_sync_change(
tx: &Transaction<'_>,
change: &Value,
@@ -768,7 +791,18 @@ fn apply_one_sync_change(
}
if entity_type == "notebook" {
if operation == "delete" {
- tx.execute("UPDATE notebooks SET is_deleted = 1, deleted_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') WHERE id = ?1 AND slug <> 'inbox' AND id <> 'nb_inbox' AND id NOT GLOB '*_inbox'", [&entity_id]).map_err(|e| e.to_string())?;
+ if active_inbox_exists_except(tx, &entity_id)? {
+ tx.execute(
+ "UPDATE notebooks
+ SET is_deleted = 1, deleted_at = strftime('%Y-%m-%dT%H:%M:%fZ','now'),
+ updated_at = strftime('%Y-%m-%dT%H:%M:%fZ','now')
+ WHERE id = ?1",
+ [&entity_id],
+ )
+ .map_err(|e| e.to_string())?;
+ } else {
+ tx.execute("UPDATE notebooks SET is_deleted = 1, deleted_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') WHERE id = ?1 AND slug <> 'inbox' AND id <> 'nb_inbox' AND id NOT GLOB '*_inbox'", [&entity_id]).map_err(|e| e.to_string())?;
+ }
return Ok(());
}
let notebook = change
@@ -793,7 +827,11 @@ fn apply_one_sync_change(
None
};
let slug = if inbox { Some("inbox") } else { requested_slug };
- tx.execute("INSERT INTO notebooks (id, parent_id, name, slug, icon, color, sort_order, created_at, updated_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9) ON CONFLICT(id) DO UPDATE SET parent_id=excluded.parent_id, name=excluded.name, slug=excluded.slug, icon=excluded.icon, color=excluded.color, sort_order=excluded.sort_order, updated_at=excluded.updated_at, is_deleted=0", rusqlite::params![entity_id, parent_id, string_param(notebook, "name")?, slug, notebook.get("icon").and_then(Value::as_str), notebook.get("color").and_then(Value::as_str), notebook.get("sortOrder").and_then(Value::as_i64).unwrap_or(0), string_param(notebook, "createdAt")?, string_param(notebook, "updatedAt")?]).map_err(|e| e.to_string())?;
+ let keep_inactive = inbox
+ && active_inbox_exists_except(tx, &entity_id)?
+ && !notebook_is_active(tx, &entity_id)?;
+ let is_deleted = i64::from(keep_inactive);
+ tx.execute("INSERT INTO notebooks (id, parent_id, name, slug, icon, color, sort_order, created_at, updated_at, is_deleted, deleted_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, CASE WHEN ?10 = 1 THEN strftime('%Y-%m-%dT%H:%M:%fZ','now') ELSE NULL END) ON CONFLICT(id) DO UPDATE SET parent_id=excluded.parent_id, name=excluded.name, slug=excluded.slug, icon=excluded.icon, color=excluded.color, sort_order=excluded.sort_order, updated_at=excluded.updated_at, is_deleted=excluded.is_deleted, deleted_at=excluded.deleted_at", rusqlite::params![entity_id, parent_id, string_param(notebook, "name")?, slug, notebook.get("icon").and_then(Value::as_str), notebook.get("color").and_then(Value::as_str), notebook.get("sortOrder").and_then(Value::as_i64).unwrap_or(0), string_param(notebook, "createdAt")?, string_param(notebook, "updatedAt")?, is_deleted]).map_err(|e| e.to_string())?;
return Ok(());
}
if operation == "delete" {
diff --git a/scripts/plan-native-release.mjs b/scripts/plan-native-release.mjs
index cedfb692..4225b051 100644
--- a/scripts/plan-native-release.mjs
+++ b/scripts/plan-native-release.mjs
@@ -31,6 +31,7 @@ export const planNativeRelease = (platform, changedFiles) => {
"apps/desktop/",
"apps/web/",
"crates/desktop-sidecar/",
+ "migrations/",
"packages/client/",
"packages/shared/",
];
diff --git a/scripts/plan-native-release.test.mjs b/scripts/plan-native-release.test.mjs
index 77348845..f9df009a 100644
--- a/scripts/plan-native-release.test.mjs
+++ b/scripts/plan-native-release.test.mjs
@@ -94,4 +94,16 @@ describe("native release planning", () => {
planNativeRelease("desktop", ["package.json", "release-summary.json", "AGENTS.md"]),
).toEqual({ rebuild: false, relevantChanges: [] });
});
+
+ test("rebuilds desktop when bundled sidecar migrations change", () => {
+ expect(
+ planNativeRelease("desktop", [
+ "migrations/0053_collapse_duplicate_inbox_notebooks.sql",
+ "apps/api/src/notebook-service.test.mjs",
+ ]),
+ ).toEqual({
+ rebuild: true,
+ relevantChanges: ["migrations/0053_collapse_duplicate_inbox_notebooks.sql"],
+ });
+ });
});