An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Keep dynamic block references updated across notebooks https://github.com/siyuan-note/siyuan/issues/18949
Danielf047cbd5ece1
diff --git a/kernel/sql/cache.go b/kernel/sql/cache.goindex 42c028f972..4c7a98433c 100644--- a/kernel/sql/cache.go+++ b/kernel/sql/cache.go@@ -152,8 +152,16 @@ func removeBlockCacheKey(id, key string) {var defIDRefsCache = gcache.New(30*time.Minute, 5*time.Minute)+// refCacheScopeBoxID 按加密边界规范化引用缓存范围:普通笔记本共享全局范围,加密笔记本保持独立范围。+func refCacheScopeBoxID(boxID string) string {+ if IsEncryptedBoxFn != nil && IsEncryptedBoxFn(boxID) {+ return boxID+ }+ return ""+}+func refCacheKey(defBlockID, boxID string) string {- return boxID + "\x00" + defBlockID+ return refCacheScopeBoxID(boxID) + "\x00" + defBlockID}func GetRefsCacheByDefID(defID string) (ret []*Ref) {@@ -164,6 +172,7 @@ func GetRefsCacheByDefIDInBox(defID, boxID string) (ret []*Ref) {if encryptedBoxCacheUnavailable(boxID) {return}+ boxID = refCacheScopeBoxID(boxID)key := refCacheKey(defID, boxID)for k, refs := range defIDRefsCache.Items() {if k == key {diff --git a/kernel/sql/cache_test.go b/kernel/sql/cache_test.goindex 14b15187ae..ddbe33173c 100644--- a/kernel/sql/cache_test.go+++ b/kernel/sql/cache_test.go@@ -16,7 +16,10 @@package sql-import "testing"+import (+ gosql "database/sql"+ "testing"+)// TestBlockCacheIsolatedByEncryptedBox 验证加密笔记本块不会污染全局缓存,且不同加密笔记本可安全使用相同块 ID。func TestBlockCacheIsolatedByEncryptedBox(t *testing.T) {@@ -104,3 +107,125 @@ func TestEncryptedBlockCacheUnavailableAfterLock(t *testing.T) {t.Fatalf("locked encrypted notebook accepted cached plaintext: %#v", block)}}++func TestNormalRefCacheSharesCryptoBoundary(t *testing.T) {+ originalIsEncryptedBoxFn := IsEncryptedBoxFn+ originalIsBoxUnlockedFn := IsBoxUnlockedFn+ defer func() {+ IsEncryptedBoxFn = originalIsEncryptedBoxFn+ IsBoxUnlockedFn = originalIsBoxUnlockedFn+ ClearCache()+ }()++ IsEncryptedBoxFn = func(string) bool {+ return false+ }+ IsBoxUnlockedFn = func(string) bool {+ return true+ }+ ClearCache()++ const defBlockID = "20260824000000-def0001"+ putRefCache("20260824000001-box0001", &Ref{+ DefBlockID: defBlockID,+ BlockID: "20260824000002-ref0001",+ Box: "20260824000001-box0001",+ })+ refs := GetRefsCacheByDefIDInBox(defBlockID, "20260824000001-box0001")+ if 1 != len(refs) {+ t.Fatalf("unexpected initial refs count: got %d, want 1", len(refs))+ }++ putRefCache("20260824000003-box0002", &Ref{+ DefBlockID: defBlockID,+ BlockID: "20260824000004-ref0002",+ Box: "20260824000003-box0002",+ })+ refs = GetRefsCacheByDefIDInBox(defBlockID, "20260824000001-box0001")+ if 2 != len(refs) {+ t.Fatalf("cross-notebook ref was not added to the shared cache: got %d refs", len(refs))+ }+}++func TestNormalRefCacheMissQueriesAllNotebooks(t *testing.T) {+ testDB, err := gosql.Open("sqlite3_extended", ":memory:")+ if nil != err {+ t.Fatalf("open test database failed: %s", err)+ }+ testDB.SetMaxOpenConns(1)+ defer testDB.Close()+ if _, err = testDB.Exec("CREATE TABLE refs (id TEXT, def_block_id TEXT, def_block_parent_id TEXT, " ++ "def_block_root_id TEXT, def_block_path TEXT, block_id TEXT, root_id TEXT, box TEXT, path TEXT, " ++ "content TEXT, markdown TEXT, type TEXT)"); nil != err {+ t.Fatalf("create refs table failed: %s", err)+ }+ if _, err = testDB.Exec(`INSERT INTO refs VALUES+ ('ref-a', 'def', '', '', '', 'block-a', 'root-a', 'normal-a', '/a.sy', '', '', ''),+ ('ref-b', 'def', '', '', '', 'block-b', 'root-b', 'normal-b', '/b.sy', '', '', '')`); nil != err {+ t.Fatalf("insert refs failed: %s", err)+ }++ originalDB := db+ originalIsEncryptedBoxFn := IsEncryptedBoxFn+ originalIsBoxUnlockedFn := IsBoxUnlockedFn+ db = testDB+ defer func() {+ db = originalDB+ IsEncryptedBoxFn = originalIsEncryptedBoxFn+ IsBoxUnlockedFn = originalIsBoxUnlockedFn+ ClearCache()+ }()+ IsEncryptedBoxFn = func(string) bool {+ return false+ }+ IsBoxUnlockedFn = func(string) bool {+ return true+ }+ ClearCache()++ refs := GetRefsCacheByDefIDInBox("def", "normal-a")+ if 2 != len(refs) {+ t.Fatalf("cross-notebook refs query returned %d refs, want 2", len(refs))+ }+}++func TestEncryptedRefCacheIsolatedAndUnavailableAfterLock(t *testing.T) {+ originalIsEncryptedBoxFn := IsEncryptedBoxFn+ originalIsBoxUnlockedFn := IsBoxUnlockedFn+ unlocked := true+ defer func() {+ IsEncryptedBoxFn = originalIsEncryptedBoxFn+ IsBoxUnlockedFn = originalIsBoxUnlockedFn+ ClearCache()+ }()++ IsEncryptedBoxFn = func(boxID string) bool {+ return boxID == "encrypted-a" || boxID == "encrypted-b"+ }+ IsBoxUnlockedFn = func(boxID string) bool {+ return !IsEncryptedBoxFn(boxID) || unlocked+ }+ ClearCache()++ const defBlockID = "20260824000005-def0002"+ putRefCache("normal", &Ref{DefBlockID: defBlockID, BlockID: "ref-normal", Box: "normal"})+ putRefCache("encrypted-a", &Ref{DefBlockID: defBlockID, BlockID: "ref-a", Box: "encrypted-a"})+ putRefCache("encrypted-b", &Ref{DefBlockID: defBlockID, BlockID: "ref-b", Box: "encrypted-b"})++ refsA := GetRefsCacheByDefIDInBox(defBlockID, "encrypted-a")+ if 1 != len(refsA) || "encrypted-a" != refsA[0].Box {+ t.Fatalf("encrypted-a cache returned cross-boundary refs: %#v", refsA)+ }+ refsB := GetRefsCacheByDefIDInBox(defBlockID, "encrypted-b")+ if 1 != len(refsB) || "encrypted-b" != refsB[0].Box {+ t.Fatalf("encrypted-b cache returned cross-boundary refs: %#v", refsB)+ }+ if refs := GetRefsCacheByDefID(defBlockID); 1 != len(refs) || "normal" != refs[0].Box {+ t.Fatalf("global cache returned encrypted refs: %#v", refs)+ }++ unlocked = false+ if refs := GetRefsCacheByDefIDInBox(defBlockID, "encrypted-a"); 0 != len(refs) {+ t.Fatalf("locked encrypted cache returned plaintext refs: %#v", refs)+ }+}