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: Invalidate document undo history after restoration https://github.com/siyuan-note/siyuan/issues/20934
Daniel4e207ad5d0ef
diff --git a/kernel/model/document_history_fts5_test.go b/kernel/model/document_history_fts5_test.goindex 1e38062abc..01d081387a 100644--- a/kernel/model/document_history_fts5_test.go+++ b/kernel/model/document_history_fts5_test.go@@ -3,13 +3,16 @@package modelimport (+ "bytes""net/http""net/http/httptest""os""path/filepath"+ "strings""testing""github.com/88250/lute/ast"+ "github.com/siyuan-note/siyuan/kernel/cache""github.com/siyuan-note/siyuan/kernel/conf""github.com/siyuan-note/siyuan/kernel/filesys""github.com/siyuan-note/siyuan/kernel/treenode"@@ -27,6 +30,73 @@ func seedDocumentHistory(t *testing.T, rootIDs ...string) {}}+func TestRestoreDocumentHistoryInvalidatesUndoRedoOnlyAfterWrite(t *testing.T) {+ for _, fail := range []bool{false, true} {+ t.Run(map[bool]string{false: "success", true: "write-failure"}[fail], func(t *testing.T) {+ fixture := setupStructureTransactionTest(t)+ setupFoldTransactionDatabase(t, fixture)+ tree, err := LoadTreeByBlockID(fixture.sourceID)+ if err != nil {+ t.Fatal(err)+ }+ originalWorkspace := util.WorkspaceDir+ util.WorkspaceDir = filepath.Dir(util.HistoryDir)+ t.Cleanup(func() { util.WorkspaceDir = originalWorkspace })+ historyPath := filepath.Join(util.HistoryDir, "2026-10-26-100000-update", tree.Box, tree.ID+".sy")+ if err = os.MkdirAll(filepath.Dir(historyPath), 0755); err != nil {+ t.Fatal(err)+ }+ docPath := filepath.Join(util.DataDir, tree.Box, tree.Path)+ historical, err := os.ReadFile(docPath)+ if err != nil {+ t.Fatal(err)+ }+ if err = os.WriteFile(historyPath, historical, 0600); err != nil {+ t.Fatal(err)+ }+ tree.Root.FirstChild.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte("new content")})+ if _, err = filesys.WriteTree(tree); err != nil {+ t.Fatal(err)+ }+ seedDocumentHistory(t, fixture.sourceID, fixture.targetID)+ if fail {+ if err = os.Remove(docPath); err != nil {+ t.Fatal(err)+ }+ if err = os.Mkdir(docPath, 0755); err != nil {+ t.Fatal(err)+ }+ if err = os.WriteFile(filepath.Join(docPath, "blocked"), []byte("preserved"), 0600); err != nil {+ t.Fatal(err)+ }+ }+ relative, relErr := filepath.Rel(util.WorkspaceDir, historyPath)+ if relErr != nil {+ t.Fatal(relErr)+ }+ err = RollbackDocHistory(relative)+ if fail {+ if err == nil || strings.Contains(err.Error(), "history path") {+ t.Fatal("expected restore write failure")+ }+ if undo, redo, _ := GlobalUndoLog.State(fixture.sourceID); !undo || !redo {+ t.Fatal("failed restore discarded working history")+ }+ return+ }+ if err != nil {+ t.Fatal(err)+ }+ requireDocumentHistoryCleared(t, fixture.sourceID, fixture.targetID)+ cache.RemoveTreeData(tree.ID)+ restored, loadErr := os.ReadFile(docPath)+ if loadErr != nil || !bytes.Equal(restored, historical) {+ t.Fatalf("restore did not preserve historical document: %v", loadErr)+ }+ })+ }+}+func requireDocumentHistoryCleared(t *testing.T, changed, unrelated string) {t.Helper()if canUndo, canRedo, _ := GlobalUndoLog.State(changed); canUndo || canRedo {diff --git a/kernel/model/encrypted_history_index_test.go b/kernel/model/encrypted_history_index_test.goindex b05a377598..9973982144 100644--- a/kernel/model/encrypted_history_index_test.go+++ b/kernel/model/encrypted_history_index_test.go@@ -88,6 +88,13 @@ func TestEncryptedHistoryAndIndexRecovery(t *testing.T) {dek = bytes.Clone(dek)defer clear(dek)rootID := "20260907100000-history"+ previousUndoLog := GlobalUndoLog+ defer func() { GlobalUndoLog = previousUndoLog }()+ seedHistory := func() {+ GlobalUndoLog = newUndoLog(64)+ entry := &UndoEntry{id: rootID, mutatedRootIDs: []string{rootID}}+ GlobalUndoLog.stacks[rootID] = &undoStack{undoStack: []*UndoEntry{entry}, redoStack: []*UndoEntry{entry}}+ }tree := treenode.NewTree(boxID, "/"+rootID+".sy", "/History", "History")if _, err = filesys.WriteTree(tree); err != nil {t.Fatal(err)@@ -139,6 +146,7 @@ func TestEncryptedHistoryAndIndexRecovery(t *testing.T) {})t.Run("write failure preserves current document and index", func(t *testing.T) {+ seedHistory()provider := filesys.DEKProviderdefer func() { filesys.DEKProvider = provider }()failure := errors.New("injected history write failure")@@ -147,12 +155,16 @@ func TestEncryptedHistoryAndIndexRecovery(t *testing.T) {t.Fatalf("write failure was not propagated: %v", err)}assertEncryptedFileBytes(t, sourcePath, currentData)+ if undo, redo, _ := GlobalUndoLog.State(rootID); !undo || !redo {+ t.Fatal("failed encrypted restore discarded document history")+ }if treenode.GetBlockTree(rootID) == nil {t.Fatal("failed rollback removed the current index")}})t.Run("rollback finishes while lock waits", func(t *testing.T) {+ seedHistory()acquire := filesys.DEKLockAcquiredefer func() { filesys.DEKLockAcquire = acquire }()var once sync.Once@@ -176,6 +188,9 @@ func TestEncryptedHistoryAndIndexRecovery(t *testing.T) {if err := RollbackDocHistory(relHistory); err != nil {t.Fatal(err)}+ if undo, redo, _ := GlobalUndoLog.State(rootID); undo || redo {+ t.Fatal("encrypted restore retained stale document history")+ }select {case <-locked:case <-time.After(3 * time.Second):diff --git a/kernel/model/history.go b/kernel/model/history.goindex 3963aa1807..71566c79e7 100644--- a/kernel/model/history.go+++ b/kernel/model/history.go@@ -465,6 +465,10 @@ func RollbackDocHistory(historyPath string) (err error) {if _, err = filesys.WriteTree(tree); err != nil {return}+ invalidateDocumentHistory(tree.ID)+ if workingDoc != nil && workingDoc.Type == "d" && workingDoc.BoxID == boxID && workingDoc.RootID != tree.ID {+ invalidateDocumentHistory(workingDoc.RootID)+ }if workingDoc != nil {if workingDoc.Type == "d" && workingDoc.Path != tree.Path {if err = filelock.Remove(filepath.Join(util.DataDir, boxID, workingDoc.Path)); err != nil {