Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Reject stale asynchronous attribute view cache entries

Daniel804432d89c2a
diff --git a/kernel/cache/av.go b/kernel/cache/av.go
index 85f2ff7119..27d341ed6a 100644
--- a/kernel/cache/av.go
+++ b/kernel/cache/av.go
@@ -62,11 +62,17 @@ func GetAVDataInBox(avID, boxID string) (raw []byte, ok bool) {
}
func GetAVDataWithVersionInBox(avID, boxID string) (raw []byte, version uint64, ok bool) {
- v, _ := avCache.Get(avCacheKey(avID, boxID))
+ avSearchDataCacheLock.RLock()
+ defer avSearchDataCacheLock.RUnlock()
+ key := avCacheKey(avID, boxID)
+ v, _ := avCache.Get(key)
if nil == v {
return
}
entry := v.(*avCacheEntry)
+ if entry.version != avDataVersions[key] {
+ return
+ }
return entry.raw, entry.version, true
}
diff --git a/kernel/cache/av_test.go b/kernel/cache/av_test.go
index 1c1e7297eb..e90b26221c 100644
--- a/kernel/cache/av_test.go
+++ b/kernel/cache/av_test.go
@@ -16,7 +16,12 @@
package cache
-import "testing"
+import (
+ "bytes"
+ "testing"
+
+ "github.com/dgraph-io/ristretto"
+)
func TestAVSearchDataInvalidation(t *testing.T) {
const avID = "20260728120000-search"
@@ -81,3 +86,62 @@ func TestAVCacheGeneration(t *testing.T) {
t.Fatalf("unexpected AV cache generation: %d", current)
}
}
+
+func TestAVCacheRejectsStaleAsyncAdmission(t *testing.T) {
+ original := avCache
+ entered, release := make(chan struct{}), make(chan struct{})
+ controlled, err := ristretto.NewCache(&ristretto.Config{
+ NumCounters: 100, MaxCost: 1 << 20, BufferItems: 64,
+ Cost: func(value interface{}) int64 {
+ close(entered)
+ <-release
+ return 1
+ },
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ avCache = controlled
+ t.Cleanup(func() {
+ ClearAVCache()
+ controlled.Close()
+ avCache = original
+ })
+ // 暂停准入线程,让同一数据库的两次写入都在首次缓存建立前入队。
+ SetAVDataInBox("blocker", "box", []byte{})
+ <-entered
+ SetAVDataInBox("database", "box", []byte("old"))
+ latestVersion := SetAVDataWithVersionInBox("database", "box", []byte("latest"))
+ close(release)
+ controlled.Wait()
+ if raw, version, ok := GetAVDataWithVersionInBox("database", "box"); ok && (!bytes.Equal(raw, []byte("latest")) || version != latestVersion) {
+ t.Fatalf("returned stale database after a newer write: %q", raw)
+ }
+ // 未命中后重新读取源文件,可以恢复当前版本的缓存。
+ latestVersion = SetAVDataWithVersionInBox("database", "box", []byte("latest"))
+ controlled.Wait()
+ if raw, version, ok := GetAVDataWithVersionInBox("database", "box"); !ok || !bytes.Equal(raw, []byte("latest")) || version != latestVersion {
+ t.Fatalf("latest database was not cached: %q, %v", raw, ok)
+ }
+}
+
+func TestAVCacheRemovalKeepsNotebookIsolation(t *testing.T) {
+ t.Cleanup(ClearAVCache)
+ for _, box := range []string{"", "plain", "encrypted"} {
+ SetAVDataInBox("database", box, []byte(box+" content"))
+ }
+ avCache.Wait()
+ RemoveAVDataInBox("database", "encrypted")
+ if _, ok := GetAVDataInBox("database", "encrypted"); ok {
+ t.Fatal("removed notebook cache remained readable")
+ }
+ if raw, ok := GetAVDataInBox("database", "plain"); !ok || string(raw) != "plain content" {
+ t.Fatal("removing another notebook changed the plain cache")
+ }
+ RemoveAVData("database")
+ for _, box := range []string{"", "plain", "encrypted"} {
+ if _, ok := GetAVDataInBox("database", box); ok {
+ t.Fatalf("database cache remained readable in notebook %q", box)
+ }
+ }
+}