An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: Detect kernel plugin source metadata changes on macOS https://github.com/siyuan-note/siyuan/issues/18387
Daniel061351a988db
diff --git a/kernel/plugin/manager.go b/kernel/plugin/manager.goindex a35b8e8e0a..dcc8837ec7 100644--- a/kernel/plugin/manager.go+++ b/kernel/plugin/manager.go@@ -260,14 +260,14 @@ func (m *PluginManager) StartPlugin(petal *model.Petal) (ok bool) {// Stop any running instance inside the same lock so that concurrent hot-reload goroutines queue here and each one sees the instance started by the previous.m.stopLocked(petal.Name)- m.addPluginSourceWatch(petal.Name, petal.Kernel.JS)-p := NewKernelPlugin(m.context, petal)m.plugins.Store(p.Name, p)- if err := p.start(); err != nil {- logging.LogErrorf("[plugin:%s] start failed: %s", p.Name, err)+ startErr := p.start()+ m.addPluginSourceWatch(petal.Name, petal.Kernel.JS)+ if startErr != nil {+ logging.LogErrorf("[plugin:%s] start failed: %s", p.Name, startErr)ok = falsereturn}diff --git a/kernel/plugin/source_file_identity_darwin.go b/kernel/plugin/source_file_identity_darwin.gonew file mode 100644index 0000000000..7b162093b9--- /dev/null+++ b/kernel/plugin/source_file_identity_darwin.go@@ -0,0 +1,35 @@+// SiYuan - Refactor your thinking+// Copyright (c) 2020-present, b3log.org+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as published by+// the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++//go:build darwin++package plugin++import (+ "os"+ "syscall"+)++func readPluginSourceFileIdentity(info os.FileInfo) (device, inode uint64, ctime int64) {+ stat, ok := info.Sys().(*syscall.Stat_t)+ if !ok {+ return+ }+ device = uint64(stat.Dev)+ inode = stat.Ino+ ctime = stat.Ctimespec.Nano()+ return+}diff --git a/kernel/plugin/source_file_identity_darwin_test.go b/kernel/plugin/source_file_identity_darwin_test.gonew file mode 100644index 0000000000..932893a183--- /dev/null+++ b/kernel/plugin/source_file_identity_darwin_test.go@@ -0,0 +1,55 @@+// SiYuan - Refactor your thinking+// Copyright (c) 2020-present, b3log.org+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as published by+// the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++//go:build darwin++package plugin++import (+ "os"+ "path/filepath"+ "testing"+ "time"+)++func TestReadPluginSourceFileIdentityDetectsInPlaceWrite(t *testing.T) {+ path := filepath.Join(t.TempDir(), "kernel.js")+ if err := os.WriteFile(path, []byte("initial"), 0644); err != nil {+ t.Fatal(err)+ }+ before, err := os.Stat(path)+ if err != nil {+ t.Fatal(err)+ }+ beforeDevice, beforeInode, beforeCTime := readPluginSourceFileIdentity(before)++ time.Sleep(10 * time.Millisecond)+ if err = os.WriteFile(path, []byte("changed"), 0644); err != nil {+ t.Fatal(err)+ }+ after, err := os.Stat(path)+ if err != nil {+ t.Fatal(err)+ }+ afterDevice, afterInode, afterCTime := readPluginSourceFileIdentity(after)++ if beforeDevice != afterDevice || beforeInode != afterInode {+ t.Fatal("in-place write must preserve the file identity")+ }+ if beforeCTime == afterCTime {+ t.Fatal("in-place write must update ctime")+ }+}diff --git a/kernel/plugin/source_file_identity_other.go b/kernel/plugin/source_file_identity_other.gonew file mode 100644index 0000000000..e5f744cab6--- /dev/null+++ b/kernel/plugin/source_file_identity_other.go@@ -0,0 +1,25 @@+// SiYuan - Refactor your thinking+// Copyright (c) 2020-present, b3log.org+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as published by+// the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++//go:build !darwin++package plugin++import "os"++func readPluginSourceFileIdentity(info os.FileInfo) (device, inode uint64, ctime int64) {+ return+}diff --git a/kernel/plugin/source_watcher.go b/kernel/plugin/source_watcher.goindex 26e510e89f..d517aba302 100644--- a/kernel/plugin/source_watcher.go+++ b/kernel/plugin/source_watcher.go@@ -31,9 +31,8 @@ import ()const (- pluginSourceReloadDelay = 300 * time.Millisecond- pluginSourcePollInterval = time.Second- pluginSourceFullScanInterval = 30 * time.Second+ pluginSourceReloadDelay = 300 * time.Millisecond+ pluginSourcePollInterval = time.Second)var errPluginFileWatchUnsupported = errors.New("plugin file watcher is not supported on mobile")@@ -52,7 +51,6 @@ type pluginSourceWatchEntry struct {fileState pluginSourceFileStatelastError stringverified bool- nextScan time.Timegeneration uint64timer *time.Timer}@@ -61,6 +59,9 @@ type pluginSourceFileState struct {exists boolsize int64modTime int64+ device uint64+ inode uint64+ ctime int64}type pluginSourceWatchState struct {@@ -68,18 +69,14 @@ type pluginSourceWatchState struct {entries map[string]*pluginSourceWatchEntrygeneration uint64delay time.Duration- fullScan time.Duration- now func() time.Timereload func(name string)}func newPluginSourceWatchState(reload func(name string)) pluginSourceWatchState {return pluginSourceWatchState{- entries: map[string]*pluginSourceWatchEntry{},- delay: pluginSourceReloadDelay,- fullScan: pluginSourceFullScanInterval,- now: time.Now,- reload: reload,+ entries: map[string]*pluginSourceWatchEntry{},+ delay: pluginSourceReloadDelay,+ reload: reload,}}@@ -140,19 +137,17 @@ func (s *pluginSourceWatchState) scan() {generation: entry.generation,fileState: entry.fileState,verified: entry.verified,- nextScan: entry.nextScan,}}s.mu.Unlock()- now := s.now()for name, snapshot := range entries {fileState, err := readPluginSourceFileState(snapshot.path)if err != nil {s.logReadError(name, snapshot.generation, snapshot.path, err)continue}- if snapshot.verified && fileState == snapshot.fileState && now.Before(snapshot.nextScan) {+ if snapshot.verified && fileState == snapshot.fileState {s.clearReadError(name, snapshot.generation)continue}@@ -255,7 +250,6 @@ func (s *pluginSourceWatchState) finishPluginSourceCheck(entry.lastError = ""entry.signature = signatureentry.verified = true- entry.nextScan = s.now().Add(s.fullScan)entry.timer = nilreload := s.reloads.mu.Unlock()@@ -319,7 +313,6 @@ type pluginSourceWatchSnapshot struct {generation uint64fileState pluginSourceFileStateverified bool- nextScan time.Time}func readPluginSourceFileState(path string) (pluginSourceFileState, error) {@@ -330,10 +323,14 @@ func readPluginSourceFileState(path string) (pluginSourceFileState, error) {if err != nil {return pluginSourceFileState{}, err}+ device, inode, ctime := readPluginSourceFileIdentity(info)return pluginSourceFileState{exists: true,size: info.Size(),modTime: info.ModTime().UnixNano(),+ device: device,+ inode: inode,+ ctime: ctime,}, nil}diff --git a/kernel/plugin/watcher_test.go b/kernel/plugin/watcher_test.goindex 928cca3d83..faeb92e200 100644--- a/kernel/plugin/watcher_test.go+++ b/kernel/plugin/watcher_test.go@@ -199,7 +199,7 @@ func TestPluginSourceWatchStateVerifiesRegistrationContent(t *testing.T) {waitForPluginReload(t, reloaded, name)}-func TestPluginSourceWatchStatePeriodicallyChecksContent(t *testing.T) {+func TestPluginSourceWatchStateChecksFileIdentity(t *testing.T) {pluginsDir := t.TempDir()name := "test-plugin"sourcePath := filepath.Join(pluginsDir, "kernel.js")@@ -207,15 +207,11 @@ func TestPluginSourceWatchStatePeriodicallyChecksContent(t *testing.T) {t.Fatal(err)}- now := time.Now()reloaded := make(chan string, 1)state := newPluginSourceWatchState(func(pluginName string) {reloaded <- pluginName})state.delay = 20 * time.Millisecond- state.now = func() time.Time {- return now- }state.register(name, sourcePath, "initial")t.Cleanup(func() {state.unregister(name)@@ -232,7 +228,7 @@ func TestPluginSourceWatchStatePeriodicallyChecksContent(t *testing.T) {state.mu.Lock()entry := state.entries[name]entry.fileState = fileState- entry.nextScan = now+ entry.fileState.inode++state.mu.Unlock()state.scan()