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: Synchronize UI readiness and preserve startup sync notifications https://github.com/siyuan-note/siyuan/issues/19846
Daniel03333248e610
diff --git a/kernel/api/system.go b/kernel/api/system.goindex 3f78c99494..3520fc03bd 100644--- a/kernel/api/system.go+++ b/kernel/api/system.go@@ -787,7 +787,7 @@ var getConf = contractHandler(apicontract.SystemGetConf, func(c *gin.Context, reif err != nil {return apicontract.Failure[apicontract.SystemConfData](-1, "get conf failed: "+err.Error())}- ret = apicontract.Success(apicontract.SystemConfData{Conf: config, Start: !util.IsUILoaded, IsPublish: isPublish})+ ret = apicontract.Success(apicontract.SystemConfData{Conf: config, Start: !util.IsUILoaded.Load(), IsPublish: isPublish})return})diff --git a/kernel/mobile/startup_test.go b/kernel/mobile/startup_test.goindex 76aa4f3daa..72e51c7124 100644--- a/kernel/mobile/startup_test.go+++ b/kernel/mobile/startup_test.go@@ -3,6 +3,7 @@ package mobileimport ("sync""testing"+ "testing/synctest""time""github.com/siyuan-note/siyuan/kernel/util"@@ -43,6 +44,37 @@ func TestIOSStartupOpensLocalDataBeforeCloudSync(t *testing.T) {waitMobileStartupSignal(t, syncStarted)}+func TestIOSStartupWaitsForUILoaded(t *testing.T) {+ previous := util.IsUILoaded.Swap(false)+ t.Cleanup(func() { util.IsUILoaded.Store(previous) })+ synctest.Test(t, func(t *testing.T) {+ syncStarted := make(chan struct{})+ localBootFinished := false+ finishMobileStartup(util.ContainerIOS, func() {+ close(syncStarted)+ }, func() {+ localBootFinished = true+ }, util.WaitForUILoaded)+ synctest.Wait()+ if !localBootFinished {+ t.Fatal("local boot did not finish before waiting for the UI")+ }+ select {+ case <-syncStarted:+ t.Fatal("iOS sync started before the UI was ready")+ default:+ }+ util.IsUILoaded.Store(true)+ time.Sleep(200 * time.Millisecond)+ synctest.Wait()+ select {+ case <-syncStarted:+ default:+ t.Fatal("iOS sync did not start after the UI became ready")+ }+ })+}+func TestAndroidStartupWaitsForBootSync(t *testing.T) {syncStarted := make(chan struct{})syncRelease := make(chan struct{})diff --git a/kernel/model/boot_sync_test.go b/kernel/model/boot_sync_test.gonew file mode 100644index 0000000000..d559c7e5d7--- /dev/null+++ b/kernel/model/boot_sync_test.go@@ -0,0 +1,162 @@+package model++import (+ "net/http"+ "net/http/httptest"+ "os"+ "os/exec"+ "path/filepath"+ "strings"+ "testing"+ "time"++ "github.com/gorilla/websocket"+ "github.com/siyuan-note/siyuan/kernel/util"+)++func TestIOSBootSyncPerception(t *testing.T) {+ // 启动状态和同步后的后台任务属于进程状态,使用子进程隔离其他测试。+ const childEnv = "SIYUAN_TEST_IOS_BOOT_SYNC"+ if os.Getenv(childEnv) == "" {+ cmd := exec.Command(os.Args[0], "-test.run=^TestIOSBootSyncPerception$", "-test.timeout=60s")+ cmd.Env = append(os.Environ(), childEnv+"=1")+ if output, err := cmd.CombinedOutput(); err != nil {+ t.Fatalf("iOS boot sync subprocess failed: %v\n%s", err, output)+ }+ return+ }++ full, _, _ := prepareAssetDownloadRepoTest(t)+ util.Container = util.ContainerIOS+ util.SetBooted()+ util.IsUILoaded.Store(true)+ lastAutoPurgeRepo = time.Now()+ Conf.Sync.Perception = true+ getPetals()++ // 使用本地仓库和独立感知连接,验证完整启动同步流程而不访问云服务。+ type syncMessage struct {+ Cmd string `json:"cmd"`+ Synced int64 `json:"synced"`+ }+ messages := make(chan syncMessage, 16)+ readerDone := make(chan struct{})+ upgrader := websocket.Upgrader{}+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ defer close(readerDone)+ conn, err := upgrader.Upgrade(w, r, nil)+ if err != nil {+ return+ }+ defer conn.Close()+ for {+ var msg syncMessage+ if err = conn.ReadJSON(&msg); err != nil {+ return+ }+ messages <- msg+ }+ }))+ defer server.Close()+ conn, _, err := websocket.DefaultDialer.Dial("ws"+strings.TrimPrefix(server.URL, "http"), nil)+ if err != nil {+ t.Fatal(err)+ }+ webSocketConn = conn+ defer func() {+ conn.Close()+ <-readerDone+ webSocketConn = nil+ }()++ assertNotifications := func(want int) {+ t.Helper()+ // 同一连接中的结束标记排在同步通知之后,避免靠等待时间判断没有通知。+ if err := conn.WriteJSON(syncMessage{Cmd: "testMarker"}); err != nil {+ t.Fatal(err)+ }+ count := 0+ timeout := time.NewTimer(5 * time.Second)+ defer timeout.Stop()+ for {+ select {+ case msg := <-messages:+ if msg.Cmd == "testMarker" {+ if count != want {+ t.Fatalf("got %d sync notifications, want %d", count, want)+ }+ return+ }+ if msg.Cmd != "synced" || msg.Synced != Conf.Sync.Synced {+ t.Fatalf("unexpected sync notification: %+v", msg)+ }+ count+++ case <-timeout.C:+ t.Fatal("sync notification marker was not received")+ }+ }+ }+ writeLocal := func(name string) {+ t.Helper()+ if err := os.WriteFile(filepath.Join(util.DataDir, name), []byte(name), 0644); err != nil {+ t.Fatal(err)+ }+ }+ assertResult := func(want int32) {+ t.Helper()+ if result := BootSyncSucc.Load(); result != want {+ t.Fatalf("boot sync result = %d, want %d: %s", result, want, Conf.Sync.Stat)+ }+ }+ assertPublished := func(name string) {+ t.Helper()+ if _, _, err := full.Sync(nil); err != nil {+ t.Fatal(err)+ }+ data, err := os.ReadFile(filepath.Join(full.DataPath, name))+ if err != nil || string(data) != name {+ t.Fatalf("local edit was not published: %q %v", data, err)+ }+ }++ writeLocal("ios-local-edit.txt")+ Conf.Sync.Enabled = false+ BootSyncData()+ assertResult(-1)+ assertNotifications(0)+ Conf.Sync.Enabled = true+ Conf.Sync.Mode = 3+ BootSyncData()+ assertResult(-1)+ assertNotifications(0)+ Conf.Sync.Mode = 1++ key := Conf.Repo.Key+ Conf.Repo.Key = nil+ BootSyncData()+ assertResult(1)+ assertNotifications(0)+ Conf.Repo.Key = key+ BootSyncData()+ assertResult(0)+ assertNotifications(1)+ assertPublished("ios-local-edit.txt")++ BootSyncData()+ assertResult(0)+ assertNotifications(0)++ Conf.Sync.Mode = 2+ writeLocal("manual-mode-edit.txt")+ BootSyncData()+ assertResult(0)+ assertNotifications(0)+ assertPublished("manual-mode-edit.txt")++ Conf.Sync.Mode = 1+ writeLocal("normal-sync-edit.txt")+ BootSyncSucc.Store(1)+ SyncData(false)+ assertResult(0)+ assertNotifications(1)+}diff --git a/kernel/model/sync.go b/kernel/model/sync.goindex fc747d148a..fc023b179b 100644--- a/kernel/model/sync.go+++ b/kernel/model/sync.go@@ -211,9 +211,10 @@ func BootSyncData() {notifySyncPending(true)}util.BroadcastByType("main", "syncing", 0, Conf.Language(81), nil)+ var cloudPublished boolvar err errorif iosAfterBoot {- _, err = syncRepoWithDNSRetry(false, false)+ cloudPublished, err = syncRepoWithDNSRetry(false, false)} else {err = bootSyncRepoWithDNSRetry()}@@ -237,6 +238,9 @@ func BootSyncData() {// 启动同步成功后消费本地速记临时文件,避免移动端开启云同步时需手动触发同步才能刷新闪念速记consumeShorthands()}+ if iosAfterBoot {+ completeSyncPerception(cloudPublished, err)+ }return}@@ -350,6 +354,12 @@ func syncDataLocked(exit, byHand bool) error {consumeShorthands()}+ completeSyncPerception(cloudPublished, err)+ return err+}++// completeSyncPerception 恢复感知连接,并在自动同步成功发布新版本后通知其他设备。+func completeSyncPerception(cloudPublished bool, err error) {if nil == webSocketConn && Conf.Sync.Perception {// 如果 websocket 连接已经断开,则重新连接connectSyncWebSocket()@@ -365,7 +375,6 @@ func syncDataLocked(exit, byHand bool) error {logging.LogErrorf("write websocket message failed: %v", writeErr)}}- return err}func checkSync(boot, exit, byHand bool) bool {diff --git a/kernel/util/runtime.go b/kernel/util/runtime.goindex 5988fc17f7..b591c4c76d 100644--- a/kernel/util/runtime.go+++ b/kernel/util/runtime.go@@ -70,11 +70,11 @@ func ExceedLargeFileWarningSize(fileSize int) bool {}// IsUILoaded 是否已经加载了 UI。-var IsUILoaded = false+var IsUILoaded atomic.Boolfunc WaitForUILoaded() {start := time.Now()- for !IsUILoaded {+ for !IsUILoaded.Load() {time.Sleep(200 * time.Millisecond)if time.Since(start) > 30*time.Second {logging.LogErrorf("wait for ui loaded timeout: %s", logging.ShortStack())@@ -84,9 +84,9 @@ func WaitForUILoaded() {}func HookUILoaded() {- for !IsUILoaded {+ for !IsUILoaded.Load() {if 0 < len(SessionsByType("main")) {- IsUILoaded = true+ IsUILoaded.Store(true)return}time.Sleep(200 * time.Millisecond)diff --git a/kernel/util/runtime_test.go b/kernel/util/runtime_test.goindex 78b607cba1..65c9d532b3 100644--- a/kernel/util/runtime_test.go+++ b/kernel/util/runtime_test.go@@ -18,9 +18,52 @@ package utilimport ("os"+ "sync""testing"+ "testing/synctest"+ "time"++ "github.com/olahol/melody")+func TestUILoadedWaitsForMainSession(t *testing.T) {+ previous := IsUILoaded.Swap(false)+ t.Cleanup(func() { IsUILoaded.Store(previous) })+ synctest.Test(t, func(t *testing.T) {+ defer IsUILoaded.Store(true)+ go HookUILoaded()+ done := make(chan struct{})+ go func() {+ WaitForUILoaded()+ close(done)+ }()+ synctest.Wait()+ select {+ case <-done:+ t.Fatal("UI wait completed without a main session")+ default:+ }++ const appID = "test-ui-loaded"+ appSessions := &sync.Map{}+ session := &melody.Session{}+ session.Set("type", "main")+ appSessions.Store("main", session)+ sessions.Store(appID, appSessions)+ defer sessions.Delete(appID)+ time.Sleep(400 * time.Millisecond)+ synctest.Wait()+ select {+ case <-done:+ default:+ t.Fatal("UI wait did not complete after the main session connected")+ }+ if !IsUILoaded.Load() {+ t.Fatal("main session did not mark the UI as loaded")+ }+ })+}+func TestSetNetworkProxy(t *testing.T) {for _, name := range networkProxyEnvironmentNames {t.Setenv(name, os.Getenv(name))