Skip to content

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

:bug: Avoid false database changes in document comparison https://github.com/siyuan-note/siyuan/issues/11601

Daniel888d94ecb476
diff --git a/kernel/model/history_diff.go b/kernel/model/history_diff.go
index 3c085fcd87..c598c0e2ff 100644
--- a/kernel/model/history_diff.go
+++ b/kernel/model/history_diff.go
@@ -23,6 +23,7 @@ import (
"errors"
"fmt"
stdhtml "html"
+ "io"
"os"
"path/filepath"
"sort"
@@ -546,9 +547,16 @@ func loadDocVersionAttributeViewSignatures(ref *DocVersionRef, version *loadedDo
}
func docDiffAttributeViewSignature(data []byte) string {
- var compact bytes.Buffer
- if err := json.Compact(&compact, data); nil == err {
- data = compact.Bytes()
+ decoder := json.NewDecoder(bytes.NewReader(data))
+ decoder.UseNumber()
+ var value any
+ if err := decoder.Decode(&value); nil == err {
+ var extra any
+ if err = decoder.Decode(&extra); errors.Is(err, io.EOF) {
+ if normalized, marshalErr := json.Marshal(value); nil == marshalErr {
+ data = normalized
+ }
+ }
}
hash := sha256.Sum256(data)
return fmt.Sprintf("%x", hash)
diff --git a/kernel/model/history_diff_test.go b/kernel/model/history_diff_test.go
index 0c6f9d3bee..cae4625d57 100644
--- a/kernel/model/history_diff_test.go
+++ b/kernel/model/history_diff_test.go
@@ -298,6 +298,23 @@ func TestDocDiffBlockSignatureIncludesAttributeViewContent(t *testing.T) {
}
}
+func TestDocDiffAttributeViewSignatureNormalizesJSONObjects(t *testing.T) {
+ left := []byte(`{
+ "spec": 1,
+ "id": "20260729000000-view001",
+ "updatedAt": 1785342031853,
+ "keyValues": [{"key": {"id": "key", "type": "block"}, "values": []}]
+ }`)
+ right := []byte(`{"keyValues":[{"values":[],"key":{"type":"block","id":"key"}}],"updatedAt":1785342031853,"id":"20260729000000-view001","spec":1}`)
+ if docDiffAttributeViewSignature(left) != docDiffAttributeViewSignature(right) {
+ t.Fatal("expected equivalent attribute view JSON objects to have the same signature")
+ }
+ changed := []byte(`{"keyValues":[],"updatedAt":1785342031853,"id":"20260729000000-view001","spec":1}`)
+ if docDiffAttributeViewSignature(left) == docDiffAttributeViewSignature(changed) {
+ t.Fatal("expected changed attribute view content to have a different signature")
+ }
+}
+
func TestMergeDocDiffBlockOrder(t *testing.T) {
order := mergeDocDiffBlockOrder(
[]string{"a", "deleted-before", "b", "deleted-after"},