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: Stage notebook history restoration before publishing https://github.com/siyuan-note/siyuan/issues/20951
Daniel4d86fa5e7963
diff --git a/kernel/model/attribute_view_recovery_fts5_test.go b/kernel/model/attribute_view_recovery_fts5_test.goindex a60d38dbb7..0fdabb6144 100644--- a/kernel/model/attribute_view_recovery_fts5_test.go+++ b/kernel/model/attribute_view_recovery_fts5_test.go@@ -12,6 +12,7 @@ import ("github.com/88250/lute/ast""github.com/88250/lute/parse"+ "github.com/siyuan-note/filelock""github.com/siyuan-note/siyuan/kernel/av""github.com/siyuan-note/siyuan/kernel/cache""github.com/siyuan-note/siyuan/kernel/filesys"@@ -160,11 +161,13 @@ func TestAttributeViewNotebookHistoryRestoresBindings(t *testing.T) {for _, scenario := range []struct {name stringcleanup, oneWay bool+ publishFailure bool}{- {"bound rows", false, false},- {"cleaned embedded database", true, false},- {"one-way relations", false, true},- {"cleaned embedded one-way relations", true, true},+ {"bound rows", false, false, false},+ {"cleaned embedded database", true, false, false},+ {"one-way relations", false, true, false},+ {"cleaned embedded one-way relations", true, true, false},+ {"publication failure", false, true, true},} {t.Run(scenario.name, func(t *testing.T) {cleanup := scenario.cleanup@@ -231,6 +234,25 @@ func TestAttributeViewNotebookHistoryRestoresBindings(t *testing.T) {if err != nil {t.Fatal(err)}+ if scenario.publishFailure {+ current := readAttributeViewItemsTest(t, before.ID)+ currentDest := readAttributeViewItemsTest(t, dest.ID)+ destination := filepath.Join(util.DataDir, fixture.box.ID)+ err = rollbackNotebookHistory(relative, func(from, staging string) error {+ if err := filelock.CopyNewtimes(from, staging); err != nil {+ return err+ }+ return os.MkdirAll(destination, 0755)+ })+ if err == nil {+ t.Fatal("notebook publication collision was accepted")+ }+ assertAttributeViewFieldsTest(t, current, readAttributeViewItemsTest(t, before.ID))+ assertAttributeViewFieldsTest(t, currentDest, readAttributeViewItemsTest(t, dest.ID))+ if err = os.Remove(destination); err != nil {+ t.Fatal(err)+ }+ }if err = RollbackNotebookHistory(relative); err != nil {t.Fatal(err)}diff --git a/kernel/model/history.go b/kernel/model/history.goindex 2fc79ed65f..351bbd61df 100644--- a/kernel/model/history.go+++ b/kernel/model/history.go@@ -711,6 +711,10 @@ func IsEncryptedHistoryPath(absPath string) bool {}func RollbackNotebookHistory(historyPath string) (err error) {+ return rollbackNotebookHistory(historyPath, filelock.CopyNewtimes)+}++func rollbackNotebookHistory(historyPath string, copyHistory func(string, string) error) (err error) {historyPath, err = validateHistoryPath(historyPath)if err != nil {return@@ -732,14 +736,34 @@ func RollbackNotebookHistory(historyPath string) (err error) {if err = checkDocumentAddressingCopy(from, boxID); err != nil {return}+ if err = os.MkdirAll(util.DataDir, 0755); err != nil {+ return+ }+ staging, err := os.MkdirTemp(util.DataDir, ".notebook-history-"+boxID+"-")+ if err != nil {+ return+ }+ defer func() { err = errors.Join(err, os.RemoveAll(staging)) }()+ if err = copyHistory(from, staging); err != nil {+ return+ }++ FlushTxQueue()+ flushLock.Lock()+ defer flushLock.Unlock()boundRecovery := &Transaction{trees: map[string]*parse.Tree{}}defer func() { boundRecovery.finishAttributeViewMutation(err != nil) }()if err = boundRecovery.restoreNotebookAttributeViewHistory(from, boxID); err != nil {return err}- if err = filelock.CopyNewtimes(from, to); err != nil {- logging.LogErrorf("copy file [%s] to [%s] failed: %s", from, to, err)+ if _, statErr := os.Lstat(to); !os.IsNotExist(statErr) {+ if statErr != nil {+ return statErr+ }+ return errors.New(Conf.Language(371))+ }+ if err = os.Rename(staging, to); err != nil {return}diff --git a/kernel/model/history_test.go b/kernel/model/history_test.goindex 0c851e4803..7e18a19db8 100644--- a/kernel/model/history_test.go+++ b/kernel/model/history_test.go@@ -18,6 +18,7 @@ package modelimport ("bytes"+ "errors""os""path/filepath""strings"@@ -213,6 +214,54 @@ func TestRollbackNotebookHistoryRejectsExistingNotebook(t *testing.T) {}}+func TestRollbackNotebookHistoryCopyFailureAllowsRetry(t *testing.T) {+ historyPath, boxID := setupNotebookHistoryRollbackTest(t)+ destination := filepath.Join(util.DataDir, boxID)+ failure := errors.New("injected partial notebook copy failure")+ err := rollbackNotebookHistory(historyPath, func(from, staging string) error {+ if err := os.WriteFile(filepath.Join(staging, "partial.sy"), []byte("partial copy"), 0600); err != nil {+ t.Fatal(err)+ }+ return failure+ })+ if !errors.Is(err, failure) {+ t.Fatalf("copy failure was not returned: %v", err)+ }+ if _, err := os.Stat(destination); !os.IsNotExist(err) {+ t.Fatalf("failed restore published an incomplete notebook: %v", err)+ }+ if paths, err := filepath.Glob(filepath.Join(util.DataDir, ".notebook-history-*")); err != nil || len(paths) != 0 {+ t.Fatalf("failed restore left staging directories: %v, %v", paths, err)+ }+ if err := RollbackNotebookHistory(historyPath); err != nil {+ t.Fatalf("restoration could not be retried: %v", err)+ }+ if _, err := os.Stat(filepath.Join(destination, "20260807000001-abcdefg.sy")); err != nil {+ t.Fatal(err)+ }+}++func TestRollbackNotebookHistoryPreservesNewDestination(t *testing.T) {+ historyPath, boxID := setupNotebookHistoryRollbackTest(t)+ destination := filepath.Join(util.DataDir, boxID)+ marker := filepath.Join(destination, "preserved.txt")+ err := rollbackNotebookHistory(historyPath, func(from, staging string) error {+ if err := filelock.CopyNewtimes(from, staging); err != nil {+ return err+ }+ if err := os.MkdirAll(destination, 0755); err != nil {+ return err+ }+ return os.WriteFile(marker, []byte("concurrent destination"), 0600)+ })+ if err == nil {+ t.Fatal("restore replaced a destination created during copying")+ }+ if data, err := os.ReadFile(marker); err != nil || string(data) != "concurrent destination" {+ t.Fatal("restore modified an existing destination")+ }+}+func setupNotebookHistoryRollbackTest(t *testing.T) (historyPath, boxID string) {originalConf := ConforiginalWorkspaceDir := util.WorkspaceDir