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: Preserve unread push notifications on queue read failures https://github.com/siyuan-note/siyuan/issues/20983
Daniela0ffc9118416
diff --git a/kernel/model/push_queue.go b/kernel/model/push_queue.goindex e2c920a600..f5a06e4626 100644--- a/kernel/model/push_queue.go+++ b/kernel/model/push_queue.go@@ -19,6 +19,7 @@ package modelimport ("bufio""encoding/json"+ "io""os""path/filepath""sync"@@ -134,7 +135,11 @@ func PollPushQueue() {_ = pushFlock.Lock()defer func() { _ = pushFlock.Unlock() }()- entries := loadPushQueue()+ entries, err := loadPushQueue()+ if err != nil {+ logging.LogErrorf("read push queue failed: %s", err)+ return+ }if 1 > len(entries) {return}@@ -211,21 +216,30 @@ func StartPushQueueConsumer() {}()}-func loadPushQueue() (entries []pushEntry) {+func loadPushQueue() (entries []pushEntry, err error) {f, err := os.Open(pushQueuePath)if err != nil {- if !os.IsNotExist(err) {- logging.LogErrorf("open push queue for reading failed: %s", err)+ if os.IsNotExist(err) {+ return nil, nil}- return+ return nil, err}defer f.Close()- scanner := bufio.NewScanner(f)- for scanner.Scan() {- line := scanner.Bytes()+ return readPushQueue(f)+}++// Read the complete queue before dispatching anything. A failed read must never+// turn a partial prefix into permission to truncate unread notifications.+func readPushQueue(reader io.Reader) (entries []pushEntry, err error) {+ buffer := bufio.NewReader(reader)+ for {+ line, readErr := buffer.ReadBytes('\n')+ if readErr != nil && readErr != io.EOF {+ return nil, readErr+ }if 0 == len(line) {- continue+ return entries, nil}var entry pushEntryif err = json.Unmarshal(line, &entry); err != nil {@@ -233,11 +247,10 @@ func loadPushQueue() (entries []pushEntry) {continue}entries = append(entries, entry)+ if readErr == io.EOF {+ return entries, nil+ }}- if err = scanner.Err(); err != nil {- logging.LogErrorf("scan push queue failed: %s", err)- }- return}func clearPushQueue() {diff --git a/kernel/model/push_queue_test.go b/kernel/model/push_queue_test.gonew file mode 100644index 0000000000..8435bde232--- /dev/null+++ b/kernel/model/push_queue_test.go@@ -0,0 +1,55 @@+package model++import (+ "encoding/json"+ "errors"+ "io"+ "os"+ "path/filepath"+ "reflect"+ "strings"+ "testing"+)++func TestReadPushQueueLongRecord(t *testing.T) {+ want := []pushEntry{{Action: "rename", Title: "first"}, {Action: "rename", Title: strings.Repeat(" ", 128*1024) + "title"}, {Action: "rename", Title: "last"}}+ var queue strings.Builder+ for _, entry := range want {+ data, err := json.Marshal(entry)+ if err != nil {+ t.Fatal(err)+ }+ queue.Write(data)+ queue.WriteByte('\n')+ }+ for _, data := range []string{queue.String(), strings.TrimSuffix(queue.String(), "\n"), "\ninvalid JSON\n" + queue.String()} {+ got, err := readPushQueue(strings.NewReader(data))+ if err != nil || !reflect.DeepEqual(got, want) {+ t.Fatalf("queue records changed: %v, %v", got, err)+ }+ }+}++type failingPushQueueReader struct{ err error }++func (reader failingPushQueueReader) Read([]byte) (int, error) { return 0, reader.err }++func TestReadPushQueueRejectsPartialRead(t *testing.T) {+ want := errors.New("injected read failure")+ reader := io.MultiReader(strings.NewReader("{\"action\":\"reloadUI\"}\n"), failingPushQueueReader{want})+ if entries, err := readPushQueue(reader); !errors.Is(err, want) || len(entries) != 0 {+ t.Fatalf("partial queue must not be dispatched: %v, %v", entries, err)+ }+}++func TestLoadPushQueueReadFailure(t *testing.T) {+ previous := pushQueuePath+ pushQueuePath = filepath.Join(t.TempDir(), "queue-directory")+ t.Cleanup(func() { pushQueuePath = previous })+ if err := os.Mkdir(pushQueuePath, 0700); err != nil {+ t.Fatal(err)+ }+ if entries, err := loadPushQueue(); err == nil || len(entries) != 0 {+ t.Fatalf("unreadable queue must return an error: %v, %v", entries, err)+ }+}