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: Reject unknown database item IDs https://github.com/siyuan-note/siyuan/issues/18604
Danielfa963d5b6d35
diff --git a/kernel/av/av.go b/kernel/av/av.goindex e43a4830d3..f57b07dc91 100644--- a/kernel/av/av.go+++ b/kernel/av/av.go@@ -1295,6 +1295,7 @@ var (ErrInvalidBoxID = errors.New("invalid box id")ErrViewNotFound = errors.New("view not found")ErrKeyNotFound = errors.New("key not found")+ ErrItemNotFound = errors.New("item not found")ErrWrongLayoutType = errors.New("wrong layout type")ErrInvalidColumnAlign = errors.New("invalid column align")ErrSpecTooNew = errors.New("attribute view spec is too new")diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.goindex 3e42ed7ec2..978ddb180e 100644--- a/kernel/model/attribute_view.go+++ b/kernel/model/attribute_view.go@@ -7587,48 +7587,30 @@ func UpdateAttributeViewCell(tx *Transaction, avID, keyID, itemID string, valueDfunc updateAttributeViewValue(tx *Transaction, attrView *av.AttributeView, keyID, itemID string, valueData any, save bool) (val *av.Value, err error) {avID := attrView.ID- var blockVal *av.Value- for _, kv := range attrView.KeyValues {- if av.KeyTypeBlock == kv.Key.Type {- for _, v := range kv.Values {- if itemID == v.BlockID {- blockVal = v- break- }- }- break- }+ keyValues, err := attrView.GetKeyValues(keyID)+ if nil != err {+ return}-- now := time.Now().UnixMilli()- oldIsDetached := true- var oldBoundBlockID string- if nil != blockVal {- oldIsDetached = blockVal.IsDetached- oldBoundBlockID = blockVal.Block.ID+ blockVal := attrView.GetBlockValue(itemID)+ if nil == blockVal {+ err = av.ErrItemNotFound+ return}- for _, keyValues := range attrView.KeyValues {- if keyID != keyValues.Key.ID {- continue- }- for _, value := range keyValues.Values {- if itemID == value.BlockID {- val = value- val.Type = keyValues.Key.Type- break- }- }-- if nil == val {- val = &av.Value{ID: ast.NewNodeID(), KeyID: keyID, BlockID: itemID, Type: keyValues.Key.Type, CreatedAt: now, UpdatedAt: now}- keyValues.Values = append(keyValues.Values, val)+ now := time.Now().UnixMilli()+ oldIsDetached := blockVal.IsDetached+ oldBoundBlockID := blockVal.Block.ID+ for _, value := range keyValues.Values {+ if itemID == value.BlockID {+ val = value+ val.Type = keyValues.Key.Type+ break}- break}+if nil == val {- err = av.ErrKeyNotFound- return+ val = &av.Value{ID: ast.NewNodeID(), KeyID: keyID, BlockID: itemID, Type: keyValues.Key.Type, CreatedAt: now, UpdatedAt: now}+ keyValues.Values = append(keyValues.Values, val)}valueID := val.IDdiff --git a/kernel/model/attribute_view_value_identity_test.go b/kernel/model/attribute_view_value_identity_test.goindex 2f3b26a9f0..b463f091dd 100644--- a/kernel/model/attribute_view_value_identity_test.go+++ b/kernel/model/attribute_view_value_identity_test.go@@ -81,17 +81,87 @@ func TestUpdateAttributeViewValuePreservesTargetIdentity(t *testing.T) {}func TestUpdateAttributeViewValueRejectsMissingKey(t *testing.T) {- attrView := &av.AttributeView{KeyValues: []*av.KeyValues{{- Key: &av.Key{ID: "20260806000000-existing", Type: av.KeyTypeText},- }}}+ const itemID = "20260806000002-item001"+ attrView := &av.AttributeView{KeyValues: []*av.KeyValues{+ {+ Key: &av.Key{ID: "20260806000000-existing", Type: av.KeyTypeBlock},+ Values: []*av.Value{{+ BlockID: itemID,+ Type: av.KeyTypeBlock,+ Block: &av.ValueBlock{},+ }},+ },+ }}- _, err := updateAttributeViewValue(nil, attrView, "20260806000001-missing", "20260806000002-item001",+ _, err := updateAttributeViewValue(nil, attrView, "20260806000001-missing", itemID,&av.Value{Type: av.KeyTypeText, Text: &av.ValueText{Content: "source"}}, false)if !errors.Is(err, av.ErrKeyNotFound) {t.Fatalf("missing key returned error [%v]", err)}}+func TestUpdateAttributeViewValueRejectsMissingItem(t *testing.T) {+ const (+ blockKeyID = "20260806000000-blockky"+ selectKeyID = "20260806000001-selectk"+ itemID = "20260806000002-item001"+ missingID = "20260806000003-missing"+ )+ selectKeyValues := &av.KeyValues{Key: &av.Key{ID: selectKeyID, Type: av.KeyTypeSelect}}+ attrView := &av.AttributeView{KeyValues: []*av.KeyValues{+ {+ Key: &av.Key{ID: blockKeyID, Type: av.KeyTypeBlock},+ Values: []*av.Value{{+ BlockID: itemID,+ Type: av.KeyTypeBlock,+ Block: &av.ValueBlock{},+ }},+ },+ selectKeyValues,+ }}++ _, err := updateAttributeViewValue(nil, attrView, selectKeyID, missingID,+ &av.Value{Type: av.KeyTypeSelect, MSelect: []*av.ValueSelect{{Content: "Closed", Color: "4"}}}, false)+ if !errors.Is(err, av.ErrItemNotFound) {+ t.Fatalf("missing item returned error [%v]", err)+ }+ if 0 != len(selectKeyValues.Values) {+ t.Fatalf("missing item created an orphan value: %+v", selectKeyValues.Values)+ }+}++func TestUpdateAttributeViewValueCreatesMissingCellForExistingItem(t *testing.T) {+ const (+ blockKeyID = "20260806000000-blockky"+ textKeyID = "20260806000001-textkey"+ itemID = "20260806000002-item001"+ )+ textKeyValues := &av.KeyValues{Key: &av.Key{ID: textKeyID, Type: av.KeyTypeText}}+ attrView := &av.AttributeView{KeyValues: []*av.KeyValues{+ {+ Key: &av.Key{ID: blockKeyID, Type: av.KeyTypeBlock},+ Values: []*av.Value{{+ BlockID: itemID,+ Type: av.KeyTypeBlock,+ Block: &av.ValueBlock{},+ }},+ },+ textKeyValues,+ }}++ updated, err := updateAttributeViewValue(nil, attrView, textKeyID, itemID,+ &av.Value{Type: av.KeyTypeText, Text: &av.ValueText{Content: "source"}}, false)+ if nil != err {+ t.Fatal(err)+ }+ if 1 != len(textKeyValues.Values) || textKeyValues.Values[0] != updated {+ t.Fatalf("missing cell was not created: %+v", textKeyValues.Values)+ }+ if itemID != updated.BlockID || textKeyID != updated.KeyID || nil == updated.Text || "source" != updated.Text.Content {+ t.Fatalf("created cell is invalid: %+v", updated)+ }+}+func TestCheckAttrViewCorrectsValueKeyID(t *testing.T) {const targetKeyID = "20260806000000-targetk"value := &av.Value{KeyID: "20260806000001-sourcek", Type: av.KeyTypeText}